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