1 /*
2 * Microsoft XMV demuxer
3 * Copyright (c) 2011 Sven Hesse <drmccoy@drmccoy.de>
4 * Copyright (c) 2011 Matthew Hoops <clone2727@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Microsoft XMV demuxer
26 */
27
28 #include <inttypes.h>
29
31
36
37 /** The min size of an XMV header. */
38 #define XMV_MIN_HEADER_SIZE 36
39
40 /** Audio flag: ADPCM'd 5.1 stream, front left / right channels */
41 #define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1
42 /** Audio flag: ADPCM'd 5.1 stream, front center / low frequency channels */
43 #define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2
44 /** Audio flag: ADPCM'd 5.1 stream, rear left / right channels */
45 #define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4
46
47 /** Audio flag: Any of the ADPCM'd 5.1 stream flags. */
48 #define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \
49 XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \
50 XMV_AUDIO_ADPCM51_REARLEFTRIGHT)
51
52 #define XMV_BLOCK_ALIGN_SIZE 36
53
54 /** A video packet with an XMV file. */
57 int stream_index;
///< The decoder stream index for this video packet.
58
59 uint32_t
data_size;
///< The size of the remaining video data.
60 uint64_t
data_offset;
///< The offset of the video data within the file.
61
62 uint32_t
current_frame;
///< The current frame within this video packet.
63 uint32_t
frame_count;
///< The amount of frames within this video packet.
64
67
68 int64_t
last_pts;
///< PTS of the last video frame.
69 int64_t
pts;
///< PTS of the most current video frame.
71
72 /** An audio packet with an XMV file. */
75 int stream_index;
///< The decoder stream index for this audio packet.
76
77 /* Stream format properties. */
82 uint64_t
bit_rate;
///< Bits of compressed data per second.
86
88
89 uint32_t
data_size;
///< The size of the remaining audio data.
90 uint64_t
data_offset;
///< The offset of the audio data within the file.
91
92 uint32_t
frame_size;
///< Number of bytes to put into an audio frame.
93
94 uint64_t
block_count;
///< Running counter of decompressed audio block.
96
97 /** Context for demuxing an XMV file. */
100
103
106
109
113
117
119 {
120 uint32_t file_version;
121
123 return 0;
124
126 if ((file_version == 0) || (file_version > 4))
127 return 0;
128
129 if (!memcmp(p->
buf + 12,
"xobX", 4))
131
132 return 0;
133 }
134
136 {
138
140
141 return 0;
142 }
143
145 {
148
149 uint32_t file_version;
150 uint32_t this_packet_size;
151 uint16_t audio_track;
152
154
156
158
161
163 if ((file_version != 4) && (file_version != 2))
165
166 /* Video tracks */
167
171
172 /* Audio tracks */
173
175
176 avio_skip(pb, 2);
/* Unknown (padding?) */
177
181
184
190
198
200
203
204 /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
205 * Those need to be interleaved to a proper 5.1 stream. */
208 "(0x%04X)\n", packet->
flags);
209
213 audio_track);
215 }
216 }
217
218
219 /* Initialize the packet context */
220
224
225 return 0;
226 }
227
229 {
230 /* Read the XMV extradata */
231
233
234 int mspel_bit = !!(
data & 0x01);
236 int abt_flag = !!(
data & 0x04);
237 int j_type_bit = !!(
data & 0x08);
238 int top_left_mv_flag = !!(
data & 0x10);
239 int per_mb_rl_bit = !!(
data & 0x20);
240 int slice_count = (
data >> 6) & 7;
241
242 /* Write it back as standard WMV2 extradata */
243
245
246 data |= mspel_bit << 15;
248 data |= abt_flag << 13;
249 data |= j_type_bit << 12;
250 data |= top_left_mv_flag << 11;
251 data |= per_mb_rl_bit << 10;
252 data |= slice_count << 7;
253
255 }
256
258 {
262
264 uint16_t audio_track;
265 uint64_t data_offset;
266
267 /* Next packet size */
269
270 /* Packet video header */
271
274
276
279
281
284 if (!vst)
286
288
294
296
298
300 }
301
302 /* Adding the audio data sizes and the video data size keeps you 4 bytes
303 * short for every audio track. But as playing around with XMV files with
304 * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
305 * you either completely distorted audio or click (when skipping the
306 * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
307 * audio track from the video data works at least for the audio. Probably
308 * some alignment thing?
309 * The video data has (always?) lots of padding, so it should work out...
310 */
312
317 }
318
319 /* Packet audio header */
320
323
326
329 if (!ast)
331
340
342
344
346
348 }
349
351 if ((packet->
data_size == 0) && (audio_track != 0))
352 /* This happens when I create an XMV with several identical audio
353 * streams. From the size calculations, duplicating the previous
354 * stream's size works out, but the track data itself is silent.
355 * Maybe this should also redirect the offset to the previous track?
356 */
358
359 /* Carve up the audio data in frame_count slices */
362 }
363
364 /* Packet data offsets */
365
367
370
374 }
375
376 /* Video frames header */
377
378 /* Read new video extra data */
382
385
388
390
394 }
395
397 }
398 }
399 }
400
401 return 0;
402 }
403
405 {
409
412
413 /* Seek to it */
417
418 /* Update the size */
422
423 /* Process the header */
427
428 /* Update the offset */
430
431 return 0;
432 }
433
436 {
440
441 uint32_t data_size;
442 uint32_t block_count;
444
445 /* Seek to it */
448
450 /* Not the last frame, get at most frame_size bytes. */
452 else
453 /* Last frame, get the rest. */
455
456 /* Read the packet */
460
462
463 /* Calculate the PTS */
464
466
470
472
473 /* Advance offset */
476
477 return 0;
478 }
479
482 {
486
491
492 /* Seek to it */
495
496 /* Read the frame header */
498
501
504
505 /* Get the packet data */
509
510 /* Contrary to normal WMV2 video, the bit stream in XMV's
511 * WMV2 is little-endian.
512 * TODO: This manual swap is of course suboptimal.
513 */
516
518
519 /* Calculate the PTS */
520
521 video->last_pts = frame_timestamp +
video->pts;
522
526
527 video->pts += frame_timestamp;
528
529 /* Keyframe? */
531
532 /* Advance offset */
535
536 return 0;
537 }
538
541 {
544
546 /* No frames left in this packet, so we fetch a new one */
547
551 }
552
554 /* Fetch a video frame */
555
557 } else {
558 /* Fetch an audio frame */
559
561 }
566 }
567
568
569 /* Increase our counters */
573 }
574
575 return 0;
576 }
577
581 .extensions = "xmv",
588 };