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 CodecID 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_header(AVFormatContext *s,
130 AVFormatParameters *ap)
132 XMVDemuxContext *xmv = s->priv_data;
133 AVIOContext *pb = s->pb;
134 AVStream *vst = NULL;
136 uint32_t file_version;
137 uint32_t this_packet_size;
138 uint16_t audio_track;
140 avio_skip(pb, 4); /* Next packet size */
142 this_packet_size = avio_rl32(pb);
144 avio_skip(pb, 4); /* Max packet size */
145 avio_skip(pb, 4); /* "xobX" */
147 file_version = avio_rl32(pb);
148 if ((file_version != 4) && (file_version != 2))
149 av_log_ask_for_sample(s, "Found uncommon version %d\n", file_version);
154 vst = avformat_new_stream(s, NULL);
156 return AVERROR(ENOMEM);
158 avpriv_set_pts_info(vst, 32, 1, 1000);
160 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
161 vst->codec->codec_id = CODEC_ID_WMV2;
162 vst->codec->codec_tag = MKBETAG('W', 'M', 'V', '2');
163 vst->codec->width = avio_rl32(pb);
164 vst->codec->height = avio_rl32(pb);
166 vst->duration = avio_rl32(pb);
168 xmv->video.stream_index = vst->index;
172 xmv->audio_track_count = avio_rl16(pb);
174 avio_skip(pb, 2); /* Unknown (padding?) */
176 xmv->audio_tracks = av_malloc(xmv->audio_track_count * sizeof(XMVAudioTrack));
177 if (!xmv->audio_tracks)
178 return AVERROR(ENOMEM);
180 xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
182 return AVERROR(ENOMEM);
184 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
185 XMVAudioTrack *track = &xmv->audio_tracks[audio_track];
186 XMVAudioPacket *packet = &xmv->audio [audio_track];
187 AVStream *ast = NULL;
189 track->compression = avio_rl16(pb);
190 track->channels = avio_rl16(pb);
191 track->sample_rate = avio_rl32(pb);
192 track->bits_per_sample = avio_rl16(pb);
193 track->flags = avio_rl16(pb);
195 track->bit_rate = track->bits_per_sample *
198 track->block_align = 36 * track->channels;
199 track->block_samples = 64;
200 track->codec_id = ff_wav_codec_get_id(track->compression,
201 track->bits_per_sample);
203 packet->track = track;
204 packet->stream_index = -1;
206 packet->frame_size = 0;
207 packet->block_count = 0;
209 /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
210 * Those need to be interleaved to a proper 5.1 stream. */
211 if (track->flags & XMV_AUDIO_ADPCM51)
212 av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
213 "(0x%04X)\n", track->flags);
215 ast = avformat_new_stream(s, NULL);
217 return AVERROR(ENOMEM);
219 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
220 ast->codec->codec_id = track->codec_id;
221 ast->codec->codec_tag = track->compression;
222 ast->codec->channels = track->channels;
223 ast->codec->sample_rate = track->sample_rate;
224 ast->codec->bits_per_coded_sample = track->bits_per_sample;
225 ast->codec->bit_rate = track->bit_rate;
226 ast->codec->block_align = 36 * track->channels;
228 avpriv_set_pts_info(ast, 32, track->block_samples, track->sample_rate);
230 packet->stream_index = ast->index;
232 ast->duration = vst->duration;
236 /** Initialize the packet context */
238 xmv->next_packet_offset = avio_tell(pb);
239 xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
240 xmv->stream_count = xmv->audio_track_count + 1;
245 static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
247 /* Read the XMV extradata */
249 uint32_t data = avio_rl32(pb);
251 int mspel_bit = !!(data & 0x01);
252 int loop_filter = !!(data & 0x02);
253 int abt_flag = !!(data & 0x04);
254 int j_type_bit = !!(data & 0x08);
255 int top_left_mv_flag = !!(data & 0x10);
256 int per_mb_rl_bit = !!(data & 0x20);
257 int slice_count = (data >> 6) & 7;
259 /* Write it back as standard WMV2 extradata */
263 data |= mspel_bit << 15;
264 data |= loop_filter << 14;
265 data |= abt_flag << 13;
266 data |= j_type_bit << 12;
267 data |= top_left_mv_flag << 11;
268 data |= per_mb_rl_bit << 10;
269 data |= slice_count << 7;
271 AV_WB32(extradata, data);
274 static int xmv_process_packet_header(AVFormatContext *s)
276 XMVDemuxContext *xmv = s->priv_data;
277 AVIOContext *pb = s->pb;
280 uint16_t audio_track;
281 uint32_t data_offset;
283 /* Next packet size */
284 xmv->next_packet_size = avio_rl32(pb);
286 /* Packet video header */
288 if (avio_read(pb, data, 8) != 8)
291 xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
293 xmv->video.current_frame = 0;
294 xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
296 xmv->video.has_extradata = (data[3] & 0x80) != 0;
298 /* Adding the audio data sizes and the video data size keeps you 4 bytes
299 * short for every audio track. But as playing around with XMV files with
300 * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
301 * you either completely distorted audio or click (when skipping the
302 * remaining 68 bytes of the ADPCM block). Substracting 4 bytes for every
303 * audio track from the video data works at least for the audio. Probably
304 * some alignment thing?
305 * The video data has (always?) lots of padding, so it should work out...
307 xmv->video.data_size -= xmv->audio_track_count * 4;
309 xmv->current_stream = 0;
310 if (!xmv->video.frame_count) {
311 xmv->video.frame_count = 1;
312 xmv->current_stream = 1;
315 /* Packet audio header */
317 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
318 XMVAudioPacket *packet = &xmv->audio[audio_track];
320 if (avio_read(pb, data, 4) != 4)
323 packet->data_size = AV_RL32(data) & 0x007FFFFF;
324 if ((packet->data_size == 0) && (audio_track != 0))
325 /* This happens when I create an XMV with several identical audio
326 * streams. From the size calculations, duplicating the previous
327 * stream's size works out, but the track data itself is silent.
328 * Maybe this should also redirect the offset to the previous track?
330 packet->data_size = xmv->audio[audio_track - 1].data_size;
332 /** Carve up the audio data in frame_count slices */
333 packet->frame_size = packet->data_size / xmv->video.frame_count;
334 packet->frame_size -= packet->frame_size % packet->track->block_align;
337 /* Packet data offsets */
339 data_offset = avio_tell(pb);
341 xmv->video.data_offset = data_offset;
342 data_offset += xmv->video.data_size;
344 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
345 xmv->audio[audio_track].data_offset = data_offset;
346 data_offset += xmv->audio[audio_track].data_size;
349 /* Video frames header */
351 /* Read new video extra data */
352 if (xmv->video.data_size > 0) {
353 if (xmv->video.has_extradata) {
354 xmv_read_extradata(xmv->video.extradata, pb);
356 xmv->video.data_size -= 4;
357 xmv->video.data_offset += 4;
359 if (xmv->video.stream_index >= 0) {
360 AVStream *vst = s->streams[xmv->video.stream_index];
362 assert(xmv->video.stream_index < s->nb_streams);
364 if (vst->codec->extradata_size < 4) {
365 av_free(vst->codec->extradata);
367 vst->codec->extradata =
368 av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
369 vst->codec->extradata_size = 4;
372 memcpy(vst->codec->extradata, xmv->video.extradata, 4);
380 static int xmv_fetch_new_packet(AVFormatContext *s)
382 XMVDemuxContext *xmv = s->priv_data;
383 AVIOContext *pb = s->pb;
387 xmv->this_packet_offset = xmv->next_packet_offset;
388 if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
391 /* Update the size */
392 xmv->this_packet_size = xmv->next_packet_size;
393 if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
396 /* Process the header */
397 result = xmv_process_packet_header(s);
401 /* Update the offset */
402 xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
407 static int xmv_fetch_audio_packet(AVFormatContext *s,
408 AVPacket *pkt, uint32_t stream)
410 XMVDemuxContext *xmv = s->priv_data;
411 AVIOContext *pb = s->pb;
412 XMVAudioPacket *audio = &xmv->audio[stream];
415 uint32_t block_count;
419 if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
422 if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
423 /* Not the last frame, get at most frame_size bytes. */
424 data_size = FFMIN(audio->frame_size, audio->data_size);
426 /* Last frame, get the rest. */
427 data_size = audio->data_size;
429 /* Read the packet */
430 result = av_get_packet(pb, pkt, data_size);
434 pkt->stream_index = audio->stream_index;
436 /* Calculate the PTS */
438 block_count = data_size / audio->track->block_align;
440 pkt->duration = block_count;
441 pkt->pts = audio->block_count;
442 pkt->dts = AV_NOPTS_VALUE;
444 audio->block_count += block_count;
447 audio->data_size -= data_size;
448 audio->data_offset += data_size;
453 static int xmv_fetch_video_packet(AVFormatContext *s,
456 XMVDemuxContext *xmv = s->priv_data;
457 AVIOContext *pb = s->pb;
458 XMVVideoPacket *video = &xmv->video;
461 uint32_t frame_header;
462 uint32_t frame_size, frame_timestamp;
466 if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
469 /* Read the frame header */
470 frame_header = avio_rl32(pb);
472 frame_size = (frame_header & 0x1FFFF) * 4 + 4;
473 frame_timestamp = (frame_header >> 17);
475 if ((frame_size + 4) > video->data_size)
478 /* Create the packet */
479 result = av_new_packet(pkt, frame_size);
483 /* Contrary to normal WMV2 video, the bit stream in XMV's
484 * WMV2 is little-endian.
485 * TODO: This manual swap is of course suboptimal.
487 for (i = 0; i < frame_size; i += 4)
488 AV_WB32(pkt->data + i, avio_rl32(pb));
490 pkt->stream_index = video->stream_index;
492 /* Calculate the PTS */
494 video->last_pts = frame_timestamp + video->pts;
497 pkt->pts = video->last_pts;
498 pkt->dts = AV_NOPTS_VALUE;
500 video->pts += frame_timestamp;
503 pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
506 video->data_size -= frame_size + 4;
507 video->data_offset += frame_size + 4;
512 static int xmv_read_packet(AVFormatContext *s,
515 XMVDemuxContext *xmv = s->priv_data;
518 if (xmv->video.current_frame == xmv->video.frame_count) {
519 /* No frames left in this packet, so we fetch a new one */
521 result = xmv_fetch_new_packet(s);
526 if (xmv->current_stream == 0) {
527 /* Fetch a video frame */
529 result = xmv_fetch_video_packet(s, pkt);
534 /* Fetch an audio frame */
536 result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
541 /* Increase our counters */
542 if (++xmv->current_stream >= xmv->stream_count) {
543 xmv->current_stream = 0;
544 xmv->video.current_frame += 1;
550 static int xmv_read_close(AVFormatContext *s)
552 XMVDemuxContext *xmv = s->priv_data;
555 av_free(xmv->audio_tracks);
560 AVInputFormat ff_xmv_demuxer = {
562 .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
563 .priv_data_size = sizeof(XMVDemuxContext),
564 .read_probe = xmv_probe,
565 .read_header = xmv_read_header,
566 .read_packet = xmv_read_packet,
567 .read_close = xmv_read_close,