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 #define XMV_BLOCK_ALIGN_SIZE 36
48 typedef struct XMVAudioTrack {
52 uint16_t bits_per_sample;
56 uint16_t block_samples;
58 enum AVCodecID codec_id;
61 typedef struct XMVVideoPacket {
62 /* The decoder stream index for this video packet. */
68 uint32_t current_frame;
71 /* Does the video packet contain extra data? */
81 typedef struct XMVAudioPacket {
82 /* The decoder stream index for this audio packet. */
85 /* The audio track this packet encodes. */
96 typedef struct XMVDemuxContext {
97 uint16_t audio_track_count;
99 XMVAudioTrack *audio_tracks;
101 uint32_t this_packet_size;
102 uint32_t next_packet_size;
104 uint32_t this_packet_offset;
105 uint32_t next_packet_offset;
107 uint16_t current_stream;
108 uint16_t stream_count;
110 XMVVideoPacket video;
111 XMVAudioPacket *audio;
114 static int xmv_probe(AVProbeData *p)
116 uint32_t file_version;
118 if (p->buf_size < XMV_MIN_HEADER_SIZE)
121 file_version = AV_RL32(p->buf + 16);
122 if ((file_version == 0) || (file_version > 4))
125 if (!memcmp(p->buf + 12, "xobX", 4))
126 return AVPROBE_SCORE_MAX;
131 static int xmv_read_close(AVFormatContext *s)
133 XMVDemuxContext *xmv = s->priv_data;
136 av_free(xmv->audio_tracks);
141 static int xmv_read_header(AVFormatContext *s)
143 XMVDemuxContext *xmv = s->priv_data;
144 AVIOContext *pb = s->pb;
145 AVStream *vst = NULL;
147 uint32_t file_version;
148 uint32_t this_packet_size;
149 uint16_t audio_track;
152 avio_skip(pb, 4); /* Next packet size */
154 this_packet_size = avio_rl32(pb);
156 avio_skip(pb, 4); /* Max packet size */
157 avio_skip(pb, 4); /* "xobX" */
159 file_version = avio_rl32(pb);
160 if ((file_version != 4) && (file_version != 2))
161 avpriv_request_sample(s, "Uncommon version %"PRIu32"", file_version);
166 vst = avformat_new_stream(s, NULL);
168 return AVERROR(ENOMEM);
170 avpriv_set_pts_info(vst, 32, 1, 1000);
172 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
173 vst->codecpar->codec_id = AV_CODEC_ID_WMV2;
174 vst->codecpar->codec_tag = MKBETAG('W', 'M', 'V', '2');
175 vst->codecpar->width = avio_rl32(pb);
176 vst->codecpar->height = avio_rl32(pb);
178 vst->duration = avio_rl32(pb);
180 xmv->video.stream_index = vst->index;
184 xmv->audio_track_count = avio_rl16(pb);
186 avio_skip(pb, 2); /* Unknown (padding?) */
188 xmv->audio_tracks = av_malloc(xmv->audio_track_count * sizeof(XMVAudioTrack));
189 if (!xmv->audio_tracks)
190 return AVERROR(ENOMEM);
192 xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
194 ret = AVERROR(ENOMEM);
198 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
199 XMVAudioTrack *track = &xmv->audio_tracks[audio_track];
200 XMVAudioPacket *packet = &xmv->audio [audio_track];
201 AVStream *ast = NULL;
203 track->compression = avio_rl16(pb);
204 track->channels = avio_rl16(pb);
205 track->sample_rate = avio_rl32(pb);
206 track->bits_per_sample = avio_rl16(pb);
207 track->flags = avio_rl16(pb);
209 track->bit_rate = track->bits_per_sample *
212 track->block_align = XMV_BLOCK_ALIGN_SIZE * track->channels;
213 track->block_samples = 64;
214 track->codec_id = ff_wav_codec_get_id(track->compression,
215 track->bits_per_sample);
217 packet->track = track;
218 packet->stream_index = -1;
220 packet->frame_size = 0;
221 packet->block_count = 0;
223 /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
224 * Those need to be interleaved to a proper 5.1 stream. */
225 if (track->flags & XMV_AUDIO_ADPCM51)
226 av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
227 "(0x%04X)\n", track->flags);
229 if (!track->channels || !track->sample_rate ||
230 track->channels >= UINT16_MAX / XMV_BLOCK_ALIGN_SIZE) {
231 av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %"PRIu16".\n",
233 ret = AVERROR_INVALIDDATA;
237 ast = avformat_new_stream(s, NULL);
239 ret = AVERROR(ENOMEM);
243 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
244 ast->codecpar->codec_id = track->codec_id;
245 ast->codecpar->codec_tag = track->compression;
246 ast->codecpar->channels = track->channels;
247 ast->codecpar->sample_rate = track->sample_rate;
248 ast->codecpar->bits_per_coded_sample = track->bits_per_sample;
249 ast->codecpar->bit_rate = track->bit_rate;
250 ast->codecpar->block_align = 36 * track->channels;
252 avpriv_set_pts_info(ast, 32, track->block_samples, track->sample_rate);
254 packet->stream_index = ast->index;
256 ast->duration = vst->duration;
260 /** Initialize the packet context */
262 xmv->next_packet_offset = avio_tell(pb);
263 xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
264 xmv->stream_count = xmv->audio_track_count + 1;
273 static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
275 /* Read the XMV extradata */
277 uint32_t data = avio_rl32(pb);
279 int mspel_bit = !!(data & 0x01);
280 int loop_filter = !!(data & 0x02);
281 int abt_flag = !!(data & 0x04);
282 int j_type_bit = !!(data & 0x08);
283 int top_left_mv_flag = !!(data & 0x10);
284 int per_mb_rl_bit = !!(data & 0x20);
285 int slice_count = (data >> 6) & 7;
287 /* Write it back as standard WMV2 extradata */
291 data |= mspel_bit << 15;
292 data |= loop_filter << 14;
293 data |= abt_flag << 13;
294 data |= j_type_bit << 12;
295 data |= top_left_mv_flag << 11;
296 data |= per_mb_rl_bit << 10;
297 data |= slice_count << 7;
299 AV_WB32(extradata, data);
302 static int xmv_process_packet_header(AVFormatContext *s)
304 XMVDemuxContext *xmv = s->priv_data;
305 AVIOContext *pb = s->pb;
308 uint16_t audio_track;
309 uint32_t data_offset;
311 /* Next packet size */
312 xmv->next_packet_size = avio_rl32(pb);
314 /* Packet video header */
316 if (avio_read(pb, data, 8) != 8)
319 xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
321 xmv->video.current_frame = 0;
322 xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
324 xmv->video.has_extradata = (data[3] & 0x80) != 0;
326 /* Adding the audio data sizes and the video data size keeps you 4 bytes
327 * short for every audio track. But as playing around with XMV files with
328 * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
329 * you either completely distorted audio or click (when skipping the
330 * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
331 * audio track from the video data works at least for the audio. Probably
332 * some alignment thing?
333 * The video data has (always?) lots of padding, so it should work out...
335 xmv->video.data_size -= xmv->audio_track_count * 4;
337 xmv->current_stream = 0;
338 if (!xmv->video.frame_count) {
339 xmv->video.frame_count = 1;
340 xmv->current_stream = 1;
343 /* Packet audio header */
345 for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
346 XMVAudioPacket *packet = &xmv->audio[audio_track];
348 if (avio_read(pb, data, 4) != 4)
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?
358 packet->data_size = xmv->audio[audio_track - 1].data_size;
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->track->block_align;
365 /* Packet data offsets */
367 data_offset = avio_tell(pb);
369 xmv->video.data_offset = data_offset;
370 data_offset += xmv->video.data_size;
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;
377 /* Video frames header */
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);
384 xmv->video.data_size -= 4;
385 xmv->video.data_offset += 4;
387 if (xmv->video.stream_index >= 0) {
388 AVStream *vst = s->streams[xmv->video.stream_index];
390 assert(xmv->video.stream_index < s->nb_streams);
392 if (vst->codecpar->extradata_size < 4) {
393 av_free(vst->codecpar->extradata);
395 vst->codecpar->extradata =
396 av_malloc(4 + AV_INPUT_BUFFER_PADDING_SIZE);
397 vst->codecpar->extradata_size = 4;
400 memcpy(vst->codecpar->extradata, xmv->video.extradata, 4);
408 static int xmv_fetch_new_packet(AVFormatContext *s)
410 XMVDemuxContext *xmv = s->priv_data;
411 AVIOContext *pb = s->pb;
415 xmv->this_packet_offset = xmv->next_packet_offset;
416 if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
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))
424 /* Process the header */
425 result = xmv_process_packet_header(s);
429 /* Update the offset */
430 xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
435 static int xmv_fetch_audio_packet(AVFormatContext *s,
436 AVPacket *pkt, uint32_t stream)
438 XMVDemuxContext *xmv = s->priv_data;
439 AVIOContext *pb = s->pb;
440 XMVAudioPacket *audio = &xmv->audio[stream];
443 uint32_t block_count;
447 if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
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);
454 /* Last frame, get the rest. */
455 data_size = audio->data_size;
457 /* Read the packet */
458 result = av_get_packet(pb, pkt, data_size);
462 pkt->stream_index = audio->stream_index;
464 /* Calculate the PTS */
466 block_count = data_size / audio->track->block_align;
468 pkt->duration = block_count;
469 pkt->pts = audio->block_count;
470 pkt->dts = AV_NOPTS_VALUE;
472 audio->block_count += block_count;
475 audio->data_size -= data_size;
476 audio->data_offset += data_size;
481 static int xmv_fetch_video_packet(AVFormatContext *s,
484 XMVDemuxContext *xmv = s->priv_data;
485 AVIOContext *pb = s->pb;
486 XMVVideoPacket *video = &xmv->video;
489 uint32_t frame_header;
490 uint32_t frame_size, frame_timestamp;
494 if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
497 /* Read the frame header */
498 frame_header = avio_rl32(pb);
500 frame_size = (frame_header & 0x1FFFF) * 4 + 4;
501 frame_timestamp = (frame_header >> 17);
503 if ((frame_size + 4) > video->data_size)
506 /* Create the packet */
507 result = av_new_packet(pkt, frame_size);
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.
515 for (i = 0; i < frame_size; i += 4)
516 AV_WB32(pkt->data + i, avio_rl32(pb));
518 pkt->stream_index = video->stream_index;
520 /* Calculate the PTS */
522 video->last_pts = frame_timestamp + video->pts;
525 pkt->pts = video->last_pts;
526 pkt->dts = AV_NOPTS_VALUE;
528 video->pts += frame_timestamp;
531 pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
534 video->data_size -= frame_size + 4;
535 video->data_offset += frame_size + 4;
540 static int xmv_read_packet(AVFormatContext *s,
543 XMVDemuxContext *xmv = s->priv_data;
546 if (xmv->video.current_frame == xmv->video.frame_count) {
547 /* No frames left in this packet, so we fetch a new one */
549 result = xmv_fetch_new_packet(s);
554 if (xmv->current_stream == 0) {
555 /* Fetch a video frame */
557 result = xmv_fetch_video_packet(s, pkt);
562 /* Fetch an audio frame */
564 result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
569 /* Increase our counters */
570 if (++xmv->current_stream >= xmv->stream_count) {
571 xmv->current_stream = 0;
572 xmv->video.current_frame += 1;
578 AVInputFormat ff_xmv_demuxer = {
580 .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
581 .priv_data_size = sizeof(XMVDemuxContext),
582 .read_probe = xmv_probe,
583 .read_header = xmv_read_header,
584 .read_packet = xmv_read_packet,
585 .read_close = xmv_read_close,