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
32
38
39 /** The min size of an XMV header. */
40 #define XMV_MIN_HEADER_SIZE 36
41
42 /** Audio flag: ADPCM'd 5.1 stream, front left / right channels */
43 #define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1
44 /** Audio flag: ADPCM'd 5.1 stream, front center / low frequency channels */
45 #define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2
46 /** Audio flag: ADPCM'd 5.1 stream, rear left / right channels */
47 #define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4
48
49 /** Audio flag: Any of the ADPCM'd 5.1 stream flags. */
50 #define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \
51 XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \
52 XMV_AUDIO_ADPCM51_REARLEFTRIGHT)
53
54 #define XMV_BLOCK_ALIGN_SIZE 36
55
56 /** A video packet with an XMV file. */
59 int stream_index;
///< The decoder stream index for this video packet.
60
61 uint32_t
data_size;
///< The size of the remaining video data.
62 uint64_t
data_offset;
///< The offset of the video data within the file.
63
64 uint32_t
current_frame;
///< The current frame within this video packet.
65 uint32_t
frame_count;
///< The amount of frames within this video packet.
66
69
73
74 /** An audio packet with an XMV file. */
77 int stream_index;
///< The decoder stream index for this audio packet.
78
79 /* Stream format properties. */
84 uint64_t
bit_rate;
///< Bits of compressed data per second.
88
90
91 uint32_t
data_size;
///< The size of the remaining audio data.
92 uint64_t
data_offset;
///< The offset of the audio data within the file.
93
94 uint32_t
frame_size;
///< Number of bytes to put into an audio frame.
95
96 uint64_t
block_count;
///< Running counter of decompressed audio block.
98
99 /** Context for demuxing an XMV file. */
102
105
108
111
115
119
121 {
122 uint32_t file_version;
123
125 return 0;
126
127 file_version =
AV_RL32(
p->buf + 16);
128 if ((file_version == 0) || (file_version > 4))
129 return 0;
130
131 if (!memcmp(
p->buf + 12,
"xobX", 4))
133
134 return 0;
135 }
136
138 {
140
142
143 return 0;
144 }
145
147 {
150
151 uint32_t file_version;
152 uint32_t this_packet_size;
153 uint16_t audio_track;
154
156
158
160
163
165 if ((file_version != 4) && (file_version != 2))
167
168 /* Video tracks */
169
173
174 /* Audio tracks */
175
177
178 avio_skip(pb, 2);
/* Unknown (padding?) */
179
183
186
192
200
202
205
206 /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
207 * Those need to be interleaved to a proper 5.1 stream. */
210 "(0x%04X)\n", packet->
flags);
211
215 audio_track);
217 }
218 }
219
220
221 /* Initialize the packet context */
222
224 if (this_packet_size < xmv->next_packet_offset)
228
229 return 0;
230 }
231
233 {
234 /* Read the XMV extradata */
235
237
238 int mspel_bit = !!(
data & 0x01);
240 int abt_flag = !!(
data & 0x04);
241 int j_type_bit = !!(
data & 0x08);
242 int top_left_mv_flag = !!(
data & 0x10);
243 int per_mb_rl_bit = !!(
data & 0x20);
244 int slice_count = (
data >> 6) & 7;
245
246 /* Write it back as standard WMV2 extradata */
247
249
250 data |= mspel_bit << 15;
252 data |= abt_flag << 13;
253 data |= j_type_bit << 12;
254 data |= top_left_mv_flag << 11;
255 data |= per_mb_rl_bit << 10;
256 data |= slice_count << 7;
257
259 }
260
262 {
266
268 uint16_t audio_track;
269 uint64_t data_offset;
270
271 /* Next packet size */
273
274 /* Packet video header */
275
278
280
283
285
288 if (!vst)
290
292
298
300
302
304 }
305
306 /* Adding the audio data sizes and the video data size keeps you 4 bytes
307 * short for every audio track. But as playing around with XMV files with
308 * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
309 * you either completely distorted audio or click (when skipping the
310 * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
311 * audio track from the video data works at least for the audio. Probably
312 * some alignment thing?
313 * The video data has (always?) lots of padding, so it should work out...
314 */
316
321 }
322
323 /* Packet audio header */
324
327
330
333 if (!ast)
335
344
346
348
350
352 }
353
355 if ((packet->
data_size == 0) && (audio_track != 0))
356 /* This happens when I create an XMV with several identical audio
357 * streams. From the size calculations, duplicating the previous
358 * stream's size works out, but the track data itself is silent.
359 * Maybe this should also redirect the offset to the previous track?
360 */
362
363 /* Carve up the audio data in frame_count slices */
366 }
367
368 /* Packet data offsets */
369
371
374
378 }
379
380 /* Video frames header */
381
382 /* Read new video extra data */
386
389
392
394
398 }
399
401 }
402 }
403 }
404
405 return 0;
406 }
407
409 {
413
416
417 /* Seek to it */
421
422 /* Update the size */
426
427 /* Process the header */
431
432 /* Update the offset */
434
435 return 0;
436 }
437
440 {
444
445 uint32_t data_size;
446 uint32_t block_count;
448
449 /* Seek to it */
452
454 /* Not the last frame, get at most frame_size bytes. */
456 else
457 /* Last frame, get the rest. */
459
460 /* Read the packet */
464
466
467 /* Calculate the PTS */
468
470
474
476
477 /* Advance offset */
480
481 return 0;
482 }
483
486 {
490
495
496 /* Seek to it */
499
500 /* Read the frame header */
502
505
508
509 /* Get the packet data */
513
514 /* Contrary to normal WMV2 video, the bit stream in XMV's
515 * WMV2 is little-endian.
516 * TODO: This manual swap is of course suboptimal.
517 */
520
522
523 /* Calculate the PTS */
524
525 video->last_pts = frame_timestamp +
video->pts;
526
530
531 video->pts += frame_timestamp;
532
533 /* Keyframe? */
535
536 /* Advance offset */
539
540 return 0;
541 }
542
545 {
548
550 /* No frames left in this packet, so we fetch a new one */
551
555 }
556
558 /* Fetch a video frame */
559
561 } else {
562 /* Fetch an audio frame */
563
565 }
570 }
571
572
573 /* Increase our counters */
577 }
578
579 return 0;
580 }
581
585 .p.extensions = "xmv",
592 };