Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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