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
30 #include "libavutil/intreadwrite.h"
31
32 #include "avformat.h"
33 #include "internal.h"
34 #include "riff.h"
35 #include "libavutil/avassert.h"
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. */
55 typedef struct XMVVideoPacket {
56 int created;
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
65 int has_extradata; ///< Does the video packet contain extra data?
66 uint8_t extradata[4]; ///< The extra data
67
68 int64_t last_pts; ///< PTS of the last video frame.
69 int64_t pts; ///< PTS of the most current video frame.
70 } XMVVideoPacket;
71
72 /** An audio packet with an XMV file. */
73 typedef struct XMVAudioPacket {
74 int created;
75 int stream_index; ///< The decoder stream index for this audio packet.
76
77 /* Stream format properties. */
78 uint16_t compression; ///< The type of compression.
79 uint16_t channels; ///< Number of channels.
80 int32_t sample_rate; ///< Sampling rate.
81 uint16_t bits_per_sample; ///< Bits per compressed sample.
82 uint64_t bit_rate; ///< Bits of compressed data per second.
83 uint16_t flags; ///< Flags
84 unsigned block_align; ///< Bytes per compressed block.
85 uint16_t block_samples; ///< Decompressed samples per compressed block.
86
87 enum AVCodecID codec_id; ///< The codec ID of the compression scheme.
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.
95 } XMVAudioPacket;
96
97 /** Context for demuxing an XMV file. */
98 typedef struct XMVDemuxContext {
99 uint16_t audio_track_count; ///< Number of audio track in this file.
100
101 uint32_t this_packet_size; ///< Size of the current packet.
102 uint32_t next_packet_size; ///< Size of the next packet.
103
104 uint64_t this_packet_offset; ///< Offset of the current packet.
105 uint64_t next_packet_offset; ///< Offset of the next packet.
106
107 uint16_t current_stream; ///< The index of the stream currently handling.
108 uint16_t stream_count; ///< The number of streams in this file.
109
110 uint32_t video_duration;
111 uint32_t video_width;
112 uint32_t video_height;
113
114 XMVVideoPacket video; ///< The video packet contained in each packet.
115 XMVAudioPacket *audio; ///< The audio packets contained in each packet.
116 } XMVDemuxContext;
117
xmv_probe(const AVProbeData * p)118 static int xmv_probe(const AVProbeData *p)
119 {
120 uint32_t file_version;
121
122 if (p->buf_size < XMV_MIN_HEADER_SIZE)
123 return 0;
124
125 file_version = AV_RL32(p->buf + 16);
126 if ((file_version == 0) || (file_version > 4))
127 return 0;
128
129 if (!memcmp(p->buf + 12, "xobX", 4))
130 return AVPROBE_SCORE_MAX;
131
132 return 0;
133 }
134
xmv_read_close(AVFormatContext * s)135 static int xmv_read_close(AVFormatContext *s)
136 {
137 XMVDemuxContext *xmv = s->priv_data;
138
139 av_freep(&xmv->audio);
140
141 return 0;
142 }
143
xmv_read_header(AVFormatContext * s)144 static int xmv_read_header(AVFormatContext *s)
145 {
146 XMVDemuxContext *xmv = s->priv_data;
147 AVIOContext *pb = s->pb;
148
149 uint32_t file_version;
150 uint32_t this_packet_size;
151 uint16_t audio_track;
152
153 s->ctx_flags |= AVFMTCTX_NOHEADER;
154
155 avio_skip(pb, 4); /* Next packet size */
156
157 this_packet_size = avio_rl32(pb);
158
159 avio_skip(pb, 4); /* Max packet size */
160 avio_skip(pb, 4); /* "xobX" */
161
162 file_version = avio_rl32(pb);
163 if ((file_version != 4) && (file_version != 2))
164 avpriv_request_sample(s, "Uncommon version %"PRIu32"", file_version);
165
166 /* Video tracks */
167
168 xmv->video_width = avio_rl32(pb);
169 xmv->video_height = avio_rl32(pb);
170 xmv->video_duration = avio_rl32(pb);
171
172 /* Audio tracks */
173
174 xmv->audio_track_count = avio_rl16(pb);
175
176 avio_skip(pb, 2); /* Unknown (padding?) */
177
178 xmv->audio = av_calloc(xmv->audio_track_count, sizeof(*xmv->audio));
179 if (!xmv->audio)
180 return AVERROR(ENOMEM);
181
182 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
183 XMVAudioPacket *packet = &xmv->audio[audio_track];
184
185 packet->compression = avio_rl16(pb);
186 packet->channels = avio_rl16(pb);
187 packet->sample_rate = avio_rl32(pb);
188 packet->bits_per_sample = avio_rl16(pb);
189 packet->flags = avio_rl16(pb);
190
191 packet->bit_rate = (uint64_t)packet->bits_per_sample *
192 packet->sample_rate *
193 packet->channels;
194 packet->block_align = XMV_BLOCK_ALIGN_SIZE * packet->channels;
195 packet->block_samples = 64;
196 packet->codec_id = ff_wav_codec_get_id(packet->compression,
197 packet->bits_per_sample);
198
199 packet->stream_index = -1;
200
201 packet->frame_size = 0;
202 packet->block_count = 0;
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. */
206 if (packet->flags & XMV_AUDIO_ADPCM51)
207 av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
208 "(0x%04X)\n", packet->flags);
209
210 if (!packet->channels || packet->sample_rate <= 0 ||
211 packet->channels >= UINT16_MAX / XMV_BLOCK_ALIGN_SIZE) {
212 av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %"PRIu16".\n",
213 audio_track);
214 return AVERROR_INVALIDDATA;
215 }
216 }
217
218
219 /* Initialize the packet context */
220 xmv->next_packet_offset = avio_tell(pb);
221 if (this_packet_size < xmv->next_packet_offset)
222 return AVERROR_INVALIDDATA;
223 xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
224 xmv->stream_count = xmv->audio_track_count + 1;
225
226 return 0;
227 }
228
xmv_read_extradata(uint8_t * extradata,AVIOContext * pb)229 static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
230 {
231 /* Read the XMV extradata */
232
233 uint32_t data = avio_rl32(pb);
234
235 int mspel_bit = !!(data & 0x01);
236 int loop_filter = !!(data & 0x02);
237 int abt_flag = !!(data & 0x04);
238 int j_type_bit = !!(data & 0x08);
239 int top_left_mv_flag = !!(data & 0x10);
240 int per_mb_rl_bit = !!(data & 0x20);
241 int slice_count = (data >> 6) & 7;
242
243 /* Write it back as standard WMV2 extradata */
244
245 data = 0;
246
247 data |= mspel_bit << 15;
248 data |= loop_filter << 14;
249 data |= abt_flag << 13;
250 data |= j_type_bit << 12;
251 data |= top_left_mv_flag << 11;
252 data |= per_mb_rl_bit << 10;
253 data |= slice_count << 7;
254
255 AV_WB32(extradata, data);
256 }
257
xmv_process_packet_header(AVFormatContext * s)258 static int xmv_process_packet_header(AVFormatContext *s)
259 {
260 XMVDemuxContext *xmv = s->priv_data;
261 AVIOContext *pb = s->pb;
262 int ret;
263
264 uint8_t data[8];
265 uint16_t audio_track;
266 uint64_t data_offset;
267
268 /* Next packet size */
269 xmv->next_packet_size = avio_rl32(pb);
270
271 /* Packet video header */
272
273 if (avio_read(pb, data, 8) != 8)
274 return AVERROR(EIO);
275
276 xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
277
278 xmv->video.current_frame = 0;
279 xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
280
281 xmv->video.has_extradata = (data[3] & 0x80) != 0;
282
283 if (!xmv->video.created) {
284 AVStream *vst = avformat_new_stream(s, NULL);
285 if (!vst)
286 return AVERROR(ENOMEM);
287
288 avpriv_set_pts_info(vst, 32, 1, 1000);
289
290 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
291 vst->codecpar->codec_id = AV_CODEC_ID_WMV2;
292 vst->codecpar->codec_tag = MKBETAG('W', 'M', 'V', '2');
293 vst->codecpar->width = xmv->video_width;
294 vst->codecpar->height = xmv->video_height;
295
296 vst->duration = xmv->video_duration;
297
298 xmv->video.stream_index = vst->index;
299
300 xmv->video.created = 1;
301 }
302
303 /* Adding the audio data sizes and the video data size keeps you 4 bytes
304 * short for every audio track. But as playing around with XMV files with
305 * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
306 * you either completely distorted audio or click (when skipping the
307 * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
308 * audio track from the video data works at least for the audio. Probably
309 * some alignment thing?
310 * The video data has (always?) lots of padding, so it should work out...
311 */
312 xmv->video.data_size -= xmv->audio_track_count * 4;
313
314 xmv->current_stream = 0;
315 if (!xmv->video.frame_count) {
316 xmv->video.frame_count = 1;
317 xmv->current_stream = xmv->stream_count > 1;
318 }
319
320 /* Packet audio header */
321
322 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
323 XMVAudioPacket *packet = &xmv->audio[audio_track];
324
325 if (avio_read(pb, data, 4) != 4)
326 return AVERROR(EIO);
327
328 if (!packet->created) {
329 AVStream *ast = avformat_new_stream(s, NULL);
330 if (!ast)
331 return AVERROR(ENOMEM);
332
333 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
334 ast->codecpar->codec_id = packet->codec_id;
335 ast->codecpar->codec_tag = packet->compression;
336 ast->codecpar->ch_layout.nb_channels = packet->channels;
337 ast->codecpar->sample_rate = packet->sample_rate;
338 ast->codecpar->bits_per_coded_sample = packet->bits_per_sample;
339 ast->codecpar->bit_rate = packet->bit_rate;
340 ast->codecpar->block_align = 36 * packet->channels;
341
342 avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate);
343
344 packet->stream_index = ast->index;
345
346 ast->duration = xmv->video_duration;
347
348 packet->created = 1;
349 }
350
351 packet->data_size = AV_RL32(data) & 0x007FFFFF;
352 if ((packet->data_size == 0) && (audio_track != 0))
353 /* This happens when I create an XMV with several identical audio
354 * streams. From the size calculations, duplicating the previous
355 * stream's size works out, but the track data itself is silent.
356 * Maybe this should also redirect the offset to the previous track?
357 */
358 packet->data_size = xmv->audio[audio_track - 1].data_size;
359
360 /* Carve up the audio data in frame_count slices */
361 packet->frame_size = packet->data_size / xmv->video.frame_count;
362 packet->frame_size -= packet->frame_size % packet->block_align;
363 }
364
365 /* Packet data offsets */
366
367 data_offset = avio_tell(pb);
368
369 xmv->video.data_offset = data_offset;
370 data_offset += xmv->video.data_size;
371
372 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
373 xmv->audio[audio_track].data_offset = data_offset;
374 data_offset += xmv->audio[audio_track].data_size;
375 }
376
377 /* Video frames header */
378
379 /* Read new video extra data */
380 if (xmv->video.data_size > 0) {
381 if (xmv->video.has_extradata) {
382 xmv_read_extradata(xmv->video.extradata, pb);
383
384 xmv->video.data_size -= 4;
385 xmv->video.data_offset += 4;
386
387 if (xmv->video.stream_index >= 0) {
388 AVStream *vst = s->streams[xmv->video.stream_index];
389
390 av_assert0(xmv->video.stream_index < s->nb_streams);
391
392 if (vst->codecpar->extradata_size < 4) {
393 if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0)
394 return ret;
395 }
396
397 memcpy(vst->codecpar->extradata, xmv->video.extradata, 4);
398 }
399 }
400 }
401
402 return 0;
403 }
404
xmv_fetch_new_packet(AVFormatContext * s)405 static int xmv_fetch_new_packet(AVFormatContext *s)
406 {
407 XMVDemuxContext *xmv = s->priv_data;
408 AVIOContext *pb = s->pb;
409 int result;
410
411 if (xmv->this_packet_offset == xmv->next_packet_offset)
412 return AVERROR_EOF;
413
414 /* Seek to it */
415 xmv->this_packet_offset = xmv->next_packet_offset;
416 if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
417 return AVERROR(EIO);
418
419 /* Update the size */
420 xmv->this_packet_size = xmv->next_packet_size;
421 if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
422 return AVERROR(EIO);
423
424 /* Process the header */
425 result = xmv_process_packet_header(s);
426 if (result)
427 return result;
428
429 /* Update the offset */
430 xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
431
432 return 0;
433 }
434
xmv_fetch_audio_packet(AVFormatContext * s,AVPacket * pkt,uint32_t stream)435 static int xmv_fetch_audio_packet(AVFormatContext *s,
436 AVPacket *pkt, uint32_t stream)
437 {
438 XMVDemuxContext *xmv = s->priv_data;
439 AVIOContext *pb = s->pb;
440 XMVAudioPacket *audio = &xmv->audio[stream];
441
442 uint32_t data_size;
443 uint32_t block_count;
444 int result;
445
446 /* Seek to it */
447 if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
448 return AVERROR(EIO);
449
450 if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
451 /* Not the last frame, get at most frame_size bytes. */
452 data_size = FFMIN(audio->frame_size, audio->data_size);
453 else
454 /* Last frame, get the rest. */
455 data_size = audio->data_size;
456
457 /* Read the packet */
458 result = av_get_packet(pb, pkt, data_size);
459 if (result <= 0)
460 return result;
461
462 pkt->stream_index = audio->stream_index;
463
464 /* Calculate the PTS */
465
466 block_count = data_size / audio->block_align;
467
468 pkt->duration = block_count;
469 pkt->pts = audio->block_count;
470 pkt->dts = AV_NOPTS_VALUE;
471
472 audio->block_count += block_count;
473
474 /* Advance offset */
475 audio->data_size -= data_size;
476 audio->data_offset += data_size;
477
478 return 0;
479 }
480
xmv_fetch_video_packet(AVFormatContext * s,AVPacket * pkt)481 static int xmv_fetch_video_packet(AVFormatContext *s,
482 AVPacket *pkt)
483 {
484 XMVDemuxContext *xmv = s->priv_data;
485 AVIOContext *pb = s->pb;
486 XMVVideoPacket *video = &xmv->video;
487
488 int result;
489 uint32_t frame_header;
490 uint32_t frame_size, frame_timestamp;
491 uint8_t *data, *end;
492
493 /* Seek to it */
494 if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
495 return AVERROR(EIO);
496
497 /* Read the frame header */
498 frame_header = avio_rl32(pb);
499
500 frame_size = (frame_header & 0x1FFFF) * 4 + 4;
501 frame_timestamp = (frame_header >> 17);
502
503 if ((frame_size + 4) > video->data_size)
504 return AVERROR(EIO);
505
506 /* Get the packet data */
507 result = av_get_packet(pb, pkt, frame_size);
508 if (result != frame_size)
509 return result;
510
511 /* Contrary to normal WMV2 video, the bit stream in XMV's
512 * WMV2 is little-endian.
513 * TODO: This manual swap is of course suboptimal.
514 */
515 for (data = pkt->data, end = pkt->data + frame_size; data < end; data += 4)
516 AV_WB32(data, AV_RL32(data));
517
518 pkt->stream_index = video->stream_index;
519
520 /* Calculate the PTS */
521
522 video->last_pts = frame_timestamp + video->pts;
523
524 pkt->duration = 0;
525 pkt->pts = video->last_pts;
526 pkt->dts = AV_NOPTS_VALUE;
527
528 video->pts += frame_timestamp;
529
530 /* Keyframe? */
531 pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
532
533 /* Advance offset */
534 video->data_size -= frame_size + 4;
535 video->data_offset += frame_size + 4;
536
537 return 0;
538 }
539
xmv_read_packet(AVFormatContext * s,AVPacket * pkt)540 static int xmv_read_packet(AVFormatContext *s,
541 AVPacket *pkt)
542 {
543 XMVDemuxContext *xmv = s->priv_data;
544 int result;
545
546 if (xmv->video.current_frame == xmv->video.frame_count) {
547 /* No frames left in this packet, so we fetch a new one */
548
549 result = xmv_fetch_new_packet(s);
550 if (result)
551 return result;
552 }
553
554 if (xmv->current_stream == 0) {
555 /* Fetch a video frame */
556
557 result = xmv_fetch_video_packet(s, pkt);
558 } else {
559 /* Fetch an audio frame */
560
561 result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
562 }
563 if (result) {
564 xmv->current_stream = 0;
565 xmv->video.current_frame = xmv->video.frame_count;
566 return result;
567 }
568
569
570 /* Increase our counters */
571 if (++xmv->current_stream >= xmv->stream_count) {
572 xmv->current_stream = 0;
573 xmv->video.current_frame += 1;
574 }
575
576 return 0;
577 }
578
579 const AVInputFormat ff_xmv_demuxer = {
580 .name = "xmv",
581 .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
582 .extensions = "xmv",
583 .priv_data_size = sizeof(XMVDemuxContext),
584 .flags_internal = FF_FMT_INIT_CLEANUP,
585 .read_probe = xmv_probe,
586 .read_header = xmv_read_header,
587 .read_packet = xmv_read_packet,
588 .read_close = xmv_read_close,
589 };
590