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