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 FFmpeg.
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.
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.
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
25 * Microsoft XMV demuxer
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mem.h"
37 #include "libavutil/avassert.h"
39 /** The min size of an XMV header. */
40 #define XMV_MIN_HEADER_SIZE 36
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
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)
54 #define XMV_BLOCK_ALIGN_SIZE 36
56 /** A video packet with an XMV file. */
57 typedef struct XMVVideoPacket
{
59 int stream_index
; ///< The decoder stream index for this video packet.
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.
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.
67 int has_extradata
; ///< Does the video packet contain extra data?
68 uint8_t extradata
[4]; ///< The extra data
70 int64_t last_pts
; ///< PTS of the last video frame.
71 int64_t pts
; ///< PTS of the most current video frame.
74 /** An audio packet with an XMV file. */
75 typedef struct XMVAudioPacket
{
77 int stream_index
; ///< The decoder stream index for this audio packet.
79 /* Stream format properties. */
80 uint16_t compression
; ///< The type of compression.
81 uint16_t channels
; ///< Number of channels.
82 int32_t sample_rate
; ///< Sampling rate.
83 uint16_t bits_per_sample
; ///< Bits per compressed sample.
84 uint64_t bit_rate
; ///< Bits of compressed data per second.
85 uint16_t flags
; ///< Flags
86 unsigned block_align
; ///< Bytes per compressed block.
87 uint16_t block_samples
; ///< Decompressed samples per compressed block.
89 enum AVCodecID codec_id
; ///< The codec ID of the compression scheme.
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.
94 uint32_t frame_size
; ///< Number of bytes to put into an audio frame.
96 uint64_t block_count
; ///< Running counter of decompressed audio block.
99 /** Context for demuxing an XMV file. */
100 typedef struct XMVDemuxContext
{
101 uint16_t audio_track_count
; ///< Number of audio track in this file.
103 uint32_t this_packet_size
; ///< Size of the current packet.
104 uint32_t next_packet_size
; ///< Size of the next packet.
106 uint64_t this_packet_offset
; ///< Offset of the current packet.
107 uint64_t next_packet_offset
; ///< Offset of the next packet.
109 uint16_t current_stream
; ///< The index of the stream currently handling.
110 uint16_t stream_count
; ///< The number of streams in this file.
112 uint32_t video_duration
;
113 uint32_t video_width
;
114 uint32_t video_height
;
116 XMVVideoPacket video
; ///< The video packet contained in each packet.
117 XMVAudioPacket
*audio
; ///< The audio packets contained in each packet.
120 static int xmv_probe(const AVProbeData
*p
)
122 uint32_t file_version
;
124 if (p
->buf_size
< XMV_MIN_HEADER_SIZE
)
127 file_version
= AV_RL32(p
->buf
+ 16);
128 if ((file_version
== 0) || (file_version
> 4))
131 if (!memcmp(p
->buf
+ 12, "xobX", 4))
132 return AVPROBE_SCORE_MAX
;
137 static int xmv_read_close(AVFormatContext
*s
)
139 XMVDemuxContext
*xmv
= s
->priv_data
;
141 av_freep(&xmv
->audio
);
146 static int xmv_read_header(AVFormatContext
*s
)
148 XMVDemuxContext
*xmv
= s
->priv_data
;
149 AVIOContext
*pb
= s
->pb
;
151 uint32_t file_version
;
152 uint32_t this_packet_size
;
153 uint16_t audio_track
;
155 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
157 avio_skip(pb
, 4); /* Next packet size */
159 this_packet_size
= avio_rl32(pb
);
161 avio_skip(pb
, 4); /* Max packet size */
162 avio_skip(pb
, 4); /* "xobX" */
164 file_version
= avio_rl32(pb
);
165 if ((file_version
!= 4) && (file_version
!= 2))
166 avpriv_request_sample(s
, "Uncommon version %"PRIu32
"", file_version
);
170 xmv
->video_width
= avio_rl32(pb
);
171 xmv
->video_height
= avio_rl32(pb
);
172 xmv
->video_duration
= avio_rl32(pb
);
176 xmv
->audio_track_count
= avio_rl16(pb
);
178 avio_skip(pb
, 2); /* Unknown (padding?) */
180 xmv
->audio
= av_calloc(xmv
->audio_track_count
, sizeof(*xmv
->audio
));
182 return AVERROR(ENOMEM
);
184 for (audio_track
= 0; audio_track
< xmv
->audio_track_count
; audio_track
++) {
185 XMVAudioPacket
*packet
= &xmv
->audio
[audio_track
];
187 packet
->compression
= avio_rl16(pb
);
188 packet
->channels
= avio_rl16(pb
);
189 packet
->sample_rate
= avio_rl32(pb
);
190 packet
->bits_per_sample
= avio_rl16(pb
);
191 packet
->flags
= avio_rl16(pb
);
193 packet
->bit_rate
= (uint64_t)packet
->bits_per_sample
*
194 packet
->sample_rate
*
196 packet
->block_align
= XMV_BLOCK_ALIGN_SIZE
* packet
->channels
;
197 packet
->block_samples
= 64;
198 packet
->codec_id
= ff_wav_codec_get_id(packet
->compression
,
199 packet
->bits_per_sample
);
201 packet
->stream_index
= -1;
203 packet
->frame_size
= 0;
204 packet
->block_count
= 0;
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. */
208 if (packet
->flags
& XMV_AUDIO_ADPCM51
)
209 av_log(s
, AV_LOG_WARNING
, "Unsupported 5.1 ADPCM audio stream "
210 "(0x%04X)\n", packet
->flags
);
212 if (!packet
->channels
|| packet
->sample_rate
<= 0 ||
213 packet
->channels
>= UINT16_MAX
/ XMV_BLOCK_ALIGN_SIZE
) {
214 av_log(s
, AV_LOG_ERROR
, "Invalid parameters for audio track %"PRIu16
".\n",
216 return AVERROR_INVALIDDATA
;
221 /* Initialize the packet context */
223 xmv
->next_packet_offset
= avio_tell(pb
);
224 if (this_packet_size
< xmv
->next_packet_offset
)
225 return AVERROR_INVALIDDATA
;
226 xmv
->next_packet_size
= this_packet_size
- xmv
->next_packet_offset
;
227 xmv
->stream_count
= xmv
->audio_track_count
+ 1;
232 static void xmv_read_extradata(uint8_t *extradata
, AVIOContext
*pb
)
234 /* Read the XMV extradata */
236 uint32_t data
= avio_rl32(pb
);
238 int mspel_bit
= !!(data
& 0x01);
239 int loop_filter
= !!(data
& 0x02);
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;
246 /* Write it back as standard WMV2 extradata */
250 data
|= mspel_bit
<< 15;
251 data
|= loop_filter
<< 14;
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;
258 AV_WB32(extradata
, data
);
261 static int xmv_process_packet_header(AVFormatContext
*s
)
263 XMVDemuxContext
*xmv
= s
->priv_data
;
264 AVIOContext
*pb
= s
->pb
;
268 uint16_t audio_track
;
269 uint64_t data_offset
;
271 /* Next packet size */
272 xmv
->next_packet_size
= avio_rl32(pb
);
274 /* Packet video header */
276 if (avio_read(pb
, data
, 8) != 8)
279 xmv
->video
.data_size
= AV_RL32(data
) & 0x007FFFFF;
281 xmv
->video
.current_frame
= 0;
282 xmv
->video
.frame_count
= (AV_RL32(data
) >> 23) & 0xFF;
284 xmv
->video
.has_extradata
= (data
[3] & 0x80) != 0;
286 if (!xmv
->video
.created
) {
287 AVStream
*vst
= avformat_new_stream(s
, NULL
);
289 return AVERROR(ENOMEM
);
291 avpriv_set_pts_info(vst
, 32, 1, 1000);
293 vst
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
294 vst
->codecpar
->codec_id
= AV_CODEC_ID_WMV2
;
295 vst
->codecpar
->codec_tag
= MKBETAG('W', 'M', 'V', '2');
296 vst
->codecpar
->width
= xmv
->video_width
;
297 vst
->codecpar
->height
= xmv
->video_height
;
299 vst
->duration
= xmv
->video_duration
;
301 xmv
->video
.stream_index
= vst
->index
;
303 xmv
->video
.created
= 1;
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...
315 xmv
->video
.data_size
-= xmv
->audio_track_count
* 4;
317 xmv
->current_stream
= 0;
318 if (!xmv
->video
.frame_count
) {
319 xmv
->video
.frame_count
= 1;
320 xmv
->current_stream
= xmv
->stream_count
> 1;
323 /* Packet audio header */
325 for (audio_track
= 0; audio_track
< xmv
->audio_track_count
; audio_track
++) {
326 XMVAudioPacket
*packet
= &xmv
->audio
[audio_track
];
328 if (avio_read(pb
, data
, 4) != 4)
331 if (!packet
->created
) {
332 AVStream
*ast
= avformat_new_stream(s
, NULL
);
334 return AVERROR(ENOMEM
);
336 ast
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
337 ast
->codecpar
->codec_id
= packet
->codec_id
;
338 ast
->codecpar
->codec_tag
= packet
->compression
;
339 ast
->codecpar
->ch_layout
.nb_channels
= packet
->channels
;
340 ast
->codecpar
->sample_rate
= packet
->sample_rate
;
341 ast
->codecpar
->bits_per_coded_sample
= packet
->bits_per_sample
;
342 ast
->codecpar
->bit_rate
= packet
->bit_rate
;
343 ast
->codecpar
->block_align
= 36 * packet
->channels
;
345 avpriv_set_pts_info(ast
, 32, packet
->block_samples
, packet
->sample_rate
);
347 packet
->stream_index
= ast
->index
;
349 ast
->duration
= xmv
->video_duration
;
354 packet
->data_size
= AV_RL32(data
) & 0x007FFFFF;
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?
361 packet
->data_size
= xmv
->audio
[audio_track
- 1].data_size
;
363 /* Carve up the audio data in frame_count slices */
364 packet
->frame_size
= packet
->data_size
/ xmv
->video
.frame_count
;
365 packet
->frame_size
-= packet
->frame_size
% packet
->block_align
;
368 /* Packet data offsets */
370 data_offset
= avio_tell(pb
);
372 xmv
->video
.data_offset
= data_offset
;
373 data_offset
+= xmv
->video
.data_size
;
375 for (audio_track
= 0; audio_track
< xmv
->audio_track_count
; audio_track
++) {
376 xmv
->audio
[audio_track
].data_offset
= data_offset
;
377 data_offset
+= xmv
->audio
[audio_track
].data_size
;
380 /* Video frames header */
382 /* Read new video extra data */
383 if (xmv
->video
.data_size
> 0) {
384 if (xmv
->video
.has_extradata
) {
385 xmv_read_extradata(xmv
->video
.extradata
, pb
);
387 xmv
->video
.data_size
-= 4;
388 xmv
->video
.data_offset
+= 4;
390 if (xmv
->video
.stream_index
>= 0) {
391 AVStream
*vst
= s
->streams
[xmv
->video
.stream_index
];
393 av_assert0(xmv
->video
.stream_index
< s
->nb_streams
);
395 if (vst
->codecpar
->extradata_size
< 4) {
396 if ((ret
= ff_alloc_extradata(vst
->codecpar
, 4)) < 0)
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
;
414 if (xmv
->this_packet_offset
== xmv
->next_packet_offset
)
418 xmv
->this_packet_offset
= xmv
->next_packet_offset
;
419 if (avio_seek(pb
, xmv
->this_packet_offset
, SEEK_SET
) != xmv
->this_packet_offset
)
422 /* Update the size */
423 xmv
->this_packet_size
= xmv
->next_packet_size
;
424 if (xmv
->this_packet_size
< (12 + xmv
->audio_track_count
* 4))
427 /* Process the header */
428 result
= xmv_process_packet_header(s
);
432 /* Update the offset */
433 xmv
->next_packet_offset
= xmv
->this_packet_offset
+ xmv
->this_packet_size
;
438 static int xmv_fetch_audio_packet(AVFormatContext
*s
,
439 AVPacket
*pkt
, uint32_t stream
)
441 XMVDemuxContext
*xmv
= s
->priv_data
;
442 AVIOContext
*pb
= s
->pb
;
443 XMVAudioPacket
*audio
= &xmv
->audio
[stream
];
446 uint32_t block_count
;
450 if (avio_seek(pb
, audio
->data_offset
, SEEK_SET
) != audio
->data_offset
)
453 if ((xmv
->video
.current_frame
+ 1) < xmv
->video
.frame_count
)
454 /* Not the last frame, get at most frame_size bytes. */
455 data_size
= FFMIN(audio
->frame_size
, audio
->data_size
);
457 /* Last frame, get the rest. */
458 data_size
= audio
->data_size
;
460 /* Read the packet */
461 result
= av_get_packet(pb
, pkt
, data_size
);
465 pkt
->stream_index
= audio
->stream_index
;
467 /* Calculate the PTS */
469 block_count
= data_size
/ audio
->block_align
;
471 pkt
->duration
= block_count
;
472 pkt
->pts
= audio
->block_count
;
473 pkt
->dts
= AV_NOPTS_VALUE
;
475 audio
->block_count
+= block_count
;
478 audio
->data_size
-= data_size
;
479 audio
->data_offset
+= data_size
;
484 static int xmv_fetch_video_packet(AVFormatContext
*s
,
487 XMVDemuxContext
*xmv
= s
->priv_data
;
488 AVIOContext
*pb
= s
->pb
;
489 XMVVideoPacket
*video
= &xmv
->video
;
492 uint32_t frame_header
;
493 uint32_t frame_size
, frame_timestamp
;
497 if (avio_seek(pb
, video
->data_offset
, SEEK_SET
) != video
->data_offset
)
500 /* Read the frame header */
501 frame_header
= avio_rl32(pb
);
503 frame_size
= (frame_header
& 0x1FFFF) * 4 + 4;
504 frame_timestamp
= (frame_header
>> 17);
506 if ((frame_size
+ 4) > video
->data_size
)
509 /* Get the packet data */
510 result
= av_get_packet(pb
, pkt
, frame_size
);
511 if (result
!= frame_size
)
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.
518 for (data
= pkt
->data
, end
= pkt
->data
+ frame_size
; data
< end
; data
+= 4)
519 AV_WB32(data
, AV_RL32(data
));
521 pkt
->stream_index
= video
->stream_index
;
523 /* Calculate the PTS */
525 video
->last_pts
= frame_timestamp
+ video
->pts
;
528 pkt
->pts
= video
->last_pts
;
529 pkt
->dts
= AV_NOPTS_VALUE
;
531 video
->pts
+= frame_timestamp
;
534 pkt
->flags
= (pkt
->data
[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY
;
537 video
->data_size
-= frame_size
+ 4;
538 video
->data_offset
+= frame_size
+ 4;
543 static int xmv_read_packet(AVFormatContext
*s
,
546 XMVDemuxContext
*xmv
= s
->priv_data
;
549 if (xmv
->video
.current_frame
== xmv
->video
.frame_count
) {
550 /* No frames left in this packet, so we fetch a new one */
552 result
= xmv_fetch_new_packet(s
);
557 if (xmv
->current_stream
== 0) {
558 /* Fetch a video frame */
560 result
= xmv_fetch_video_packet(s
, pkt
);
562 /* Fetch an audio frame */
564 result
= xmv_fetch_audio_packet(s
, pkt
, xmv
->current_stream
- 1);
567 xmv
->current_stream
= 0;
568 xmv
->video
.current_frame
= xmv
->video
.frame_count
;
573 /* Increase our counters */
574 if (++xmv
->current_stream
>= xmv
->stream_count
) {
575 xmv
->current_stream
= 0;
576 xmv
->video
.current_frame
+= 1;
582 const FFInputFormat ff_xmv_demuxer
= {
584 .p
.long_name
= NULL_IF_CONFIG_SMALL("Microsoft XMV"),
585 .p
.extensions
= "xmv",
586 .priv_data_size
= sizeof(XMVDemuxContext
),
587 .flags_internal
= FF_INFMT_FLAG_INIT_CLEANUP
,
588 .read_probe
= xmv_probe
,
589 .read_header
= xmv_read_header
,
590 .read_packet
= xmv_read_packet
,
591 .read_close
= xmv_read_close
,