2 * Microsoft XMV demuxer
3 * Copyright (c) 2011 Sven Hesse <drmccoy@drmccoy.de>
4 * Copyright (c) 2011 Matthew Hoops <clone2727@gmail.com>
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Microsoft XMV demuxer
30 #include "libavutil/intreadwrite.h"
36 #define XMV_MIN_HEADER_SIZE 36
38 #define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1
39 #define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2
40 #define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4
42 #define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \
43 XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \
44 XMV_AUDIO_ADPCM51_REARLEFTRIGHT)
46 typedef struct XMVAudioTrack {
50 uint16_t bits_per_sample;
54 uint16_t block_samples;
56 enum AVCodecID codec_id;
59 typedef struct XMVVideoPacket {
60 /* The decoder stream index for this video packet. */
66 uint32_t current_frame;
69 /* Does the video packet contain extra data? */
79 typedef struct XMVAudioPacket {
80 /* The decoder stream index for this audio packet. */
83 /* The audio track this packet encodes. */
94 typedef struct XMVDemuxContext {
95 uint16_t audio_track_count;
97 XMVAudioTrack *audio_tracks;
99 uint32_t this_packet_size;
100 uint32_t next_packet_size;
102 uint32_t this_packet_offset;
103 uint32_t next_packet_offset;
105 uint16_t current_stream;
106 uint16_t stream_count;
108 XMVVideoPacket video;
109 XMVAudioPacket *audio;
112 static int xmv_probe(AVProbeData *p)
114 uint32_t file_version;
116 if (p->buf_size < XMV_MIN_HEADER_SIZE)
119 file_version = AV_RL32(p->buf + 16);
120 if ((file_version == 0) || (file_version > 4))
123 if (!memcmp(p->buf + 12, "xobX", 4))
124 return AVPROBE_SCORE_MAX;
129 static int xmv_read_close(AVFormatContext *s)
131 XMVDemuxContext *xmv = s->priv_data;
134 av_free(xmv->audio_tracks);
139 static int xmv_read_header(AVFormatContext *s)
141 XMVDemuxContext *xmv = s->priv_data;
142 AVIOContext *pb = s->pb;
143 AVStream *vst = NULL;
145 uint32_t file_version;
146 uint32_t this_packet_size;
147 uint16_t audio_track;
150 avio_skip(pb, 4); /* Next packet size */
152 this_packet_size = avio_rl32(pb);
154 avio_skip(pb, 4); /* Max packet size */
155 avio_skip(pb, 4); /* "xobX" */
157 file_version = avio_rl32(pb);
158 if ((file_version != 4) && (file_version != 2))
159 avpriv_request_sample(s, "Uncommon version %d", file_version);
164 vst = avformat_new_stream(s, NULL);
166 return AVERROR(ENOMEM);
168 avpriv_set_pts_info(vst, 32, 1, 1000);
170 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
171 vst->codec->codec_id = AV_CODEC_ID_WMV2;
172 vst->codec->codec_tag = MKBETAG('W', 'M', 'V', '2');
173 vst->codec->width = avio_rl32(pb);
174 vst->codec->height = avio_rl32(pb);
176 vst->duration = avio_rl32(pb);
178 xmv->video.stream_index = vst->index;
182 xmv->audio_track_count = avio_rl16(pb);
184 avio_skip(pb, 2); /* Unknown (padding?) */
186 xmv->audio_tracks = av_malloc(xmv->audio_track_count * sizeof(XMVAudioTrack));
187 if (!xmv->audio_tracks)
188 return AVERROR(ENOMEM);
190 xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
192 ret = AVERROR(ENOMEM);
196 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
197 XMVAudioTrack *track = &xmv->audio_tracks[audio_track];
198 XMVAudioPacket *packet = &xmv->audio [audio_track];
199 AVStream *ast = NULL;
201 track->compression = avio_rl16(pb);
202 track->channels = avio_rl16(pb);
203 track->sample_rate = avio_rl32(pb);
204 track->bits_per_sample = avio_rl16(pb);
205 track->flags = avio_rl16(pb);
207 track->bit_rate = track->bits_per_sample *
210 track->block_align = 36 * track->channels;
211 track->block_samples = 64;
212 track->codec_id = ff_wav_codec_get_id(track->compression,
213 track->bits_per_sample);
215 packet->track = track;
216 packet->stream_index = -1;
218 packet->frame_size = 0;
219 packet->block_count = 0;
221 /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
222 * Those need to be interleaved to a proper 5.1 stream. */
223 if (track->flags & XMV_AUDIO_ADPCM51)
224 av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
225 "(0x%04X)\n", track->flags);
227 if (!track->channels || !track->sample_rate) {
228 av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %d.\n",
230 ret = AVERROR_INVALIDDATA;
234 ast = avformat_new_stream(s, NULL);
236 ret = AVERROR(ENOMEM);
240 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
241 ast->codec->codec_id = track->codec_id;
242 ast->codec->codec_tag = track->compression;
243 ast->codec->channels = track->channels;
244 ast->codec->sample_rate = track->sample_rate;
245 ast->codec->bits_per_coded_sample = track->bits_per_sample;
246 ast->codec->bit_rate = track->bit_rate;
247 ast->codec->block_align = 36 * track->channels;
249 avpriv_set_pts_info(ast, 32, track->block_samples, track->sample_rate);
251 packet->stream_index = ast->index;
253 ast->duration = vst->duration;
257 /** Initialize the packet context */
259 xmv->next_packet_offset = avio_tell(pb);
260 xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
261 xmv->stream_count = xmv->audio_track_count + 1;
270 static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
272 /* Read the XMV extradata */
274 uint32_t data = avio_rl32(pb);
276 int mspel_bit = !!(data & 0x01);
277 int loop_filter = !!(data & 0x02);
278 int abt_flag = !!(data & 0x04);
279 int j_type_bit = !!(data & 0x08);
280 int top_left_mv_flag = !!(data & 0x10);
281 int per_mb_rl_bit = !!(data & 0x20);
282 int slice_count = (data >> 6) & 7;
284 /* Write it back as standard WMV2 extradata */
288 data |= mspel_bit << 15;
289 data |= loop_filter << 14;
290 data |= abt_flag << 13;
291 data |= j_type_bit << 12;
292 data |= top_left_mv_flag << 11;
293 data |= per_mb_rl_bit << 10;
294 data |= slice_count << 7;
296 AV_WB32(extradata, data);
299 static int xmv_process_packet_header(AVFormatContext *s)
301 XMVDemuxContext *xmv = s->priv_data;
302 AVIOContext *pb = s->pb;
305 uint16_t audio_track;
306 uint32_t data_offset;
308 /* Next packet size */
309 xmv->next_packet_size = avio_rl32(pb);
311 /* Packet video header */
313 if (avio_read(pb, data, 8) != 8)
316 xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
318 xmv->video.current_frame = 0;
319 xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
321 xmv->video.has_extradata = (data[3] & 0x80) != 0;
323 /* Adding the audio data sizes and the video data size keeps you 4 bytes
324 * short for every audio track. But as playing around with XMV files with
325 * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
326 * you either completely distorted audio or click (when skipping the
327 * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
328 * audio track from the video data works at least for the audio. Probably
329 * some alignment thing?
330 * The video data has (always?) lots of padding, so it should work out...
332 xmv->video.data_size -= xmv->audio_track_count * 4;
334 xmv->current_stream = 0;
335 if (!xmv->video.frame_count) {
336 xmv->video.frame_count = 1;
337 xmv->current_stream = 1;
340 /* Packet audio header */
342 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
343 XMVAudioPacket *packet = &xmv->audio[audio_track];
345 if (avio_read(pb, data, 4) != 4)
348 packet->data_size = AV_RL32(data) & 0x007FFFFF;
349 if ((packet->data_size == 0) && (audio_track != 0))
350 /* This happens when I create an XMV with several identical audio
351 * streams. From the size calculations, duplicating the previous
352 * stream's size works out, but the track data itself is silent.
353 * Maybe this should also redirect the offset to the previous track?
355 packet->data_size = xmv->audio[audio_track - 1].data_size;
357 /** Carve up the audio data in frame_count slices */
358 packet->frame_size = packet->data_size / xmv->video.frame_count;
359 packet->frame_size -= packet->frame_size % packet->track->block_align;
362 /* Packet data offsets */
364 data_offset = avio_tell(pb);
366 xmv->video.data_offset = data_offset;
367 data_offset += xmv->video.data_size;
369 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
370 xmv->audio[audio_track].data_offset = data_offset;
371 data_offset += xmv->audio[audio_track].data_size;
374 /* Video frames header */
376 /* Read new video extra data */
377 if (xmv->video.data_size > 0) {
378 if (xmv->video.has_extradata) {
379 xmv_read_extradata(xmv->video.extradata, pb);
381 xmv->video.data_size -= 4;
382 xmv->video.data_offset += 4;
384 if (xmv->video.stream_index >= 0) {
385 AVStream *vst = s->streams[xmv->video.stream_index];
387 assert(xmv->video.stream_index < s->nb_streams);
389 if (vst->codec->extradata_size < 4) {
390 av_free(vst->codec->extradata);
392 vst->codec->extradata =
393 av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
394 vst->codec->extradata_size = 4;
397 memcpy(vst->codec->extradata, xmv->video.extradata, 4);
405 static int xmv_fetch_new_packet(AVFormatContext *s)
407 XMVDemuxContext *xmv = s->priv_data;
408 AVIOContext *pb = s->pb;
412 xmv->this_packet_offset = xmv->next_packet_offset;
413 if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
416 /* Update the size */
417 xmv->this_packet_size = xmv->next_packet_size;
418 if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
421 /* Process the header */
422 result = xmv_process_packet_header(s);
426 /* Update the offset */
427 xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
432 static int xmv_fetch_audio_packet(AVFormatContext *s,
433 AVPacket *pkt, uint32_t stream)
435 XMVDemuxContext *xmv = s->priv_data;
436 AVIOContext *pb = s->pb;
437 XMVAudioPacket *audio = &xmv->audio[stream];
440 uint32_t block_count;
444 if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
447 if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
448 /* Not the last frame, get at most frame_size bytes. */
449 data_size = FFMIN(audio->frame_size, audio->data_size);
451 /* Last frame, get the rest. */
452 data_size = audio->data_size;
454 /* Read the packet */
455 result = av_get_packet(pb, pkt, data_size);
459 pkt->stream_index = audio->stream_index;
461 /* Calculate the PTS */
463 block_count = data_size / audio->track->block_align;
465 pkt->duration = block_count;
466 pkt->pts = audio->block_count;
467 pkt->dts = AV_NOPTS_VALUE;
469 audio->block_count += block_count;
472 audio->data_size -= data_size;
473 audio->data_offset += data_size;
478 static int xmv_fetch_video_packet(AVFormatContext *s,
481 XMVDemuxContext *xmv = s->priv_data;
482 AVIOContext *pb = s->pb;
483 XMVVideoPacket *video = &xmv->video;
486 uint32_t frame_header;
487 uint32_t frame_size, frame_timestamp;
491 if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
494 /* Read the frame header */
495 frame_header = avio_rl32(pb);
497 frame_size = (frame_header & 0x1FFFF) * 4 + 4;
498 frame_timestamp = (frame_header >> 17);
500 if ((frame_size + 4) > video->data_size)
503 /* Create the packet */
504 result = av_new_packet(pkt, frame_size);
508 /* Contrary to normal WMV2 video, the bit stream in XMV's
509 * WMV2 is little-endian.
510 * TODO: This manual swap is of course suboptimal.
512 for (i = 0; i < frame_size; i += 4)
513 AV_WB32(pkt->data + i, avio_rl32(pb));
515 pkt->stream_index = video->stream_index;
517 /* Calculate the PTS */
519 video->last_pts = frame_timestamp + video->pts;
522 pkt->pts = video->last_pts;
523 pkt->dts = AV_NOPTS_VALUE;
525 video->pts += frame_timestamp;
528 pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
531 video->data_size -= frame_size + 4;
532 video->data_offset += frame_size + 4;
537 static int xmv_read_packet(AVFormatContext *s,
540 XMVDemuxContext *xmv = s->priv_data;
543 if (xmv->video.current_frame == xmv->video.frame_count) {
544 /* No frames left in this packet, so we fetch a new one */
546 result = xmv_fetch_new_packet(s);
551 if (xmv->current_stream == 0) {
552 /* Fetch a video frame */
554 result = xmv_fetch_video_packet(s, pkt);
559 /* Fetch an audio frame */
561 result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
566 /* Increase our counters */
567 if (++xmv->current_stream >= xmv->stream_count) {
568 xmv->current_stream = 0;
569 xmv->video.current_frame += 1;
575 AVInputFormat ff_xmv_demuxer = {
577 .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
578 .priv_data_size = sizeof(XMVDemuxContext),
579 .read_probe = xmv_probe,
580 .read_header = xmv_read_header,
581 .read_packet = xmv_read_packet,
582 .read_close = xmv_read_close,