2011-08-16 22:26:45 +03:00
|
|
|
/*
|
|
|
|
* Microsoft XMV demuxer
|
|
|
|
* Copyright (c) 2011 Sven Hesse <drmccoy@drmccoy.de>
|
|
|
|
* Copyright (c) 2011 Matthew Hoops <clone2727@gmail.com>
|
|
|
|
*
|
2011-08-18 17:50:22 +03:00
|
|
|
* This file is part of FFmpeg.
|
2011-08-16 22:26:45 +03:00
|
|
|
*
|
2011-08-18 17:50:22 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-08-16 22:26:45 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-08-18 17:50:22 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-08-16 22:26:45 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-08-18 17:50:22 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-08-16 22:26:45 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Microsoft XMV demuxer
|
|
|
|
*/
|
|
|
|
|
2014-03-10 17:35:59 +03:00
|
|
|
#include <inttypes.h>
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2011-08-16 22:26:45 +03:00
|
|
|
#include "riff.h"
|
2012-06-07 22:09:06 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/** The min size of an XMV header. */
|
2011-08-16 22:26:45 +03:00
|
|
|
#define XMV_MIN_HEADER_SIZE 36
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/** Audio flag: ADPCM'd 5.1 stream, front left / right channels */
|
2011-08-16 22:26:45 +03:00
|
|
|
#define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1
|
2011-08-18 20:00:21 +03:00
|
|
|
/** Audio flag: ADPCM'd 5.1 stream, front center / low frequency channels */
|
2011-08-16 22:26:45 +03:00
|
|
|
#define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2
|
2011-08-18 20:00:21 +03:00
|
|
|
/** Audio flag: ADPCM'd 5.1 stream, rear left / right channels */
|
2011-08-16 22:26:45 +03:00
|
|
|
#define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/** Audio flag: Any of the ADPCM'd 5.1 stream flags. */
|
2011-08-16 22:26:45 +03:00
|
|
|
#define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \
|
|
|
|
XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \
|
|
|
|
XMV_AUDIO_ADPCM51_REARLEFTRIGHT)
|
|
|
|
|
2013-09-16 21:27:49 +03:00
|
|
|
#define XMV_BLOCK_ALIGN_SIZE 36
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/** A video packet with an XMV file. */
|
2011-08-16 22:26:45 +03:00
|
|
|
typedef struct XMVVideoPacket {
|
2016-04-10 21:58:15 +02:00
|
|
|
int created;
|
2011-08-18 20:00:21 +03:00
|
|
|
int stream_index; ///< The decoder stream index for this video packet.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint32_t data_size; ///< The size of the remaining video data.
|
|
|
|
uint64_t data_offset; ///< The offset of the video data within the file.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint32_t current_frame; ///< The current frame within this video packet.
|
|
|
|
uint32_t frame_count; ///< The amount of frames within this video packet.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
int has_extradata; ///< Does the video packet contain extra data?
|
|
|
|
uint8_t extradata[4]; ///< The extra data
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
int64_t last_pts; ///< PTS of the last video frame.
|
|
|
|
int64_t pts; ///< PTS of the most current video frame.
|
2011-08-16 22:26:45 +03:00
|
|
|
} XMVVideoPacket;
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/** An audio packet with an XMV file. */
|
2011-08-16 22:26:45 +03:00
|
|
|
typedef struct XMVAudioPacket {
|
2016-04-10 21:58:15 +02:00
|
|
|
int created;
|
2011-08-18 20:00:21 +03:00
|
|
|
int stream_index; ///< The decoder stream index for this audio packet.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/* Stream format properties. */
|
|
|
|
uint16_t compression; ///< The type of compression.
|
|
|
|
uint16_t channels; ///< Number of channels.
|
2016-10-23 17:25:15 +02:00
|
|
|
int32_t sample_rate; ///< Sampling rate.
|
2011-08-18 20:00:21 +03:00
|
|
|
uint16_t bits_per_sample; ///< Bits per compressed sample.
|
|
|
|
uint32_t bit_rate; ///< Bits of compressed data per second.
|
|
|
|
uint16_t flags; ///< Flags
|
2012-11-09 19:17:37 +03:00
|
|
|
unsigned block_align; ///< Bytes per compressed block.
|
2011-08-18 20:00:21 +03:00
|
|
|
uint16_t block_samples; ///< Decompressed samples per compressed block.
|
2011-08-18 19:37:14 +03:00
|
|
|
|
2012-08-07 23:45:46 +03:00
|
|
|
enum AVCodecID codec_id; ///< The codec ID of the compression scheme.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint32_t data_size; ///< The size of the remaining audio data.
|
|
|
|
uint64_t data_offset; ///< The offset of the audio data within the file.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint32_t frame_size; ///< Number of bytes to put into an audio frame.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint64_t block_count; ///< Running counter of decompressed audio block.
|
2011-08-16 22:26:45 +03:00
|
|
|
} XMVAudioPacket;
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/** Context for demuxing an XMV file. */
|
2011-08-16 22:26:45 +03:00
|
|
|
typedef struct XMVDemuxContext {
|
2011-08-18 20:00:21 +03:00
|
|
|
uint16_t audio_track_count; ///< Number of audio track in this file.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint32_t this_packet_size; ///< Size of the current packet.
|
|
|
|
uint32_t next_packet_size; ///< Size of the next packet.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint64_t this_packet_offset; ///< Offset of the current packet.
|
|
|
|
uint64_t next_packet_offset; ///< Offset of the next packet.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
uint16_t current_stream; ///< The index of the stream currently handling.
|
|
|
|
uint16_t stream_count; ///< The number of streams in this file.
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
uint32_t video_duration;
|
|
|
|
uint32_t video_width;
|
|
|
|
uint32_t video_height;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
XMVVideoPacket video; ///< The video packet contained in each packet.
|
|
|
|
XMVAudioPacket *audio; ///< The audio packets contained in each packet.
|
2011-08-16 22:26:45 +03:00
|
|
|
} XMVDemuxContext;
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int xmv_probe(const AVProbeData *p)
|
2011-08-16 22:26:45 +03:00
|
|
|
{
|
|
|
|
uint32_t file_version;
|
|
|
|
|
|
|
|
if (p->buf_size < XMV_MIN_HEADER_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
file_version = AV_RL32(p->buf + 16);
|
|
|
|
if ((file_version == 0) || (file_version > 4))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!memcmp(p->buf + 12, "xobX", 4))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-28 12:33:02 +03:00
|
|
|
static int xmv_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
|
2013-04-04 15:20:51 +03:00
|
|
|
av_freep(&xmv->audio);
|
2013-03-28 12:33:02 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int xmv_read_header(AVFormatContext *s)
|
2011-08-16 22:26:45 +03:00
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
|
|
|
|
uint32_t file_version;
|
|
|
|
uint32_t this_packet_size;
|
|
|
|
uint16_t audio_track;
|
2013-03-28 12:33:02 +03:00
|
|
|
int ret;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
|
2011-08-16 22:26:45 +03:00
|
|
|
avio_skip(pb, 4); /* Next packet size */
|
|
|
|
|
|
|
|
this_packet_size = avio_rl32(pb);
|
|
|
|
|
|
|
|
avio_skip(pb, 4); /* Max packet size */
|
|
|
|
avio_skip(pb, 4); /* "xobX" */
|
|
|
|
|
|
|
|
file_version = avio_rl32(pb);
|
|
|
|
if ((file_version != 4) && (file_version != 2))
|
2014-03-10 17:35:59 +03:00
|
|
|
avpriv_request_sample(s, "Uncommon version %"PRIu32"", file_version);
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
/* Video tracks */
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
xmv->video_width = avio_rl32(pb);
|
|
|
|
xmv->video_height = avio_rl32(pb);
|
|
|
|
xmv->video_duration = avio_rl32(pb);
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
/* Audio tracks */
|
|
|
|
|
|
|
|
xmv->audio_track_count = avio_rl16(pb);
|
|
|
|
|
|
|
|
avio_skip(pb, 2); /* Unknown (padding?) */
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
xmv->audio = av_mallocz_array(xmv->audio_track_count, sizeof(XMVAudioPacket));
|
2013-03-28 12:34:47 +03:00
|
|
|
if (!xmv->audio) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
|
2011-08-18 19:37:14 +03:00
|
|
|
XMVAudioPacket *packet = &xmv->audio[audio_track];
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2011-08-18 19:37:14 +03:00
|
|
|
packet->compression = avio_rl16(pb);
|
|
|
|
packet->channels = avio_rl16(pb);
|
|
|
|
packet->sample_rate = avio_rl32(pb);
|
|
|
|
packet->bits_per_sample = avio_rl16(pb);
|
|
|
|
packet->flags = avio_rl16(pb);
|
|
|
|
|
|
|
|
packet->bit_rate = packet->bits_per_sample *
|
|
|
|
packet->sample_rate *
|
|
|
|
packet->channels;
|
2013-09-18 12:24:27 +03:00
|
|
|
packet->block_align = XMV_BLOCK_ALIGN_SIZE * packet->channels;
|
2011-08-18 19:37:14 +03:00
|
|
|
packet->block_samples = 64;
|
|
|
|
packet->codec_id = ff_wav_codec_get_id(packet->compression,
|
|
|
|
packet->bits_per_sample);
|
|
|
|
|
2011-08-16 22:26:45 +03:00
|
|
|
packet->stream_index = -1;
|
|
|
|
|
|
|
|
packet->frame_size = 0;
|
|
|
|
packet->block_count = 0;
|
|
|
|
|
|
|
|
/* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
|
|
|
|
* Those need to be interleaved to a proper 5.1 stream. */
|
2011-08-18 19:37:14 +03:00
|
|
|
if (packet->flags & XMV_AUDIO_ADPCM51)
|
2011-08-16 22:26:45 +03:00
|
|
|
av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
|
2011-08-18 19:37:14 +03:00
|
|
|
"(0x%04X)\n", packet->flags);
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2016-10-23 17:25:15 +02:00
|
|
|
if (!packet->channels || packet->sample_rate <= 0 ||
|
2013-09-18 12:24:27 +03:00
|
|
|
packet->channels >= UINT16_MAX / XMV_BLOCK_ALIGN_SIZE) {
|
2014-03-10 17:35:59 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %"PRIu16".\n",
|
2013-03-28 12:33:02 +03:00
|
|
|
audio_track);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-08-16 22:26:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/* Initialize the packet context */
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
xmv->next_packet_offset = avio_tell(pb);
|
2011-08-18 20:03:28 +03:00
|
|
|
xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
|
|
|
|
xmv->stream_count = xmv->audio_track_count + 1;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
return 0;
|
2013-03-28 12:33:02 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
xmv_read_close(s);
|
|
|
|
return ret;
|
2011-08-16 22:26:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
|
|
|
|
{
|
|
|
|
/* Read the XMV extradata */
|
|
|
|
|
|
|
|
uint32_t data = avio_rl32(pb);
|
|
|
|
|
|
|
|
int mspel_bit = !!(data & 0x01);
|
|
|
|
int loop_filter = !!(data & 0x02);
|
|
|
|
int abt_flag = !!(data & 0x04);
|
|
|
|
int j_type_bit = !!(data & 0x08);
|
|
|
|
int top_left_mv_flag = !!(data & 0x10);
|
|
|
|
int per_mb_rl_bit = !!(data & 0x20);
|
|
|
|
int slice_count = (data >> 6) & 7;
|
|
|
|
|
|
|
|
/* Write it back as standard WMV2 extradata */
|
|
|
|
|
|
|
|
data = 0;
|
|
|
|
|
|
|
|
data |= mspel_bit << 15;
|
|
|
|
data |= loop_filter << 14;
|
|
|
|
data |= abt_flag << 13;
|
|
|
|
data |= j_type_bit << 12;
|
|
|
|
data |= top_left_mv_flag << 11;
|
|
|
|
data |= per_mb_rl_bit << 10;
|
|
|
|
data |= slice_count << 7;
|
|
|
|
|
|
|
|
AV_WB32(extradata, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xmv_process_packet_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
2014-12-15 16:51:04 +02:00
|
|
|
int ret;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
uint8_t data[8];
|
|
|
|
uint16_t audio_track;
|
2011-08-18 19:22:40 +03:00
|
|
|
uint64_t data_offset;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
/* Next packet size */
|
|
|
|
xmv->next_packet_size = avio_rl32(pb);
|
|
|
|
|
|
|
|
/* Packet video header */
|
|
|
|
|
|
|
|
if (avio_read(pb, data, 8) != 8)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
|
|
|
|
|
|
|
|
xmv->video.current_frame = 0;
|
|
|
|
xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
|
|
|
|
|
|
|
|
xmv->video.has_extradata = (data[3] & 0x80) != 0;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (!xmv->video.created) {
|
|
|
|
AVStream *vst = avformat_new_stream(s, NULL);
|
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
avpriv_set_pts_info(vst, 32, 1, 1000);
|
|
|
|
|
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_WMV2;
|
|
|
|
vst->codecpar->codec_tag = MKBETAG('W', 'M', 'V', '2');
|
|
|
|
vst->codecpar->width = xmv->video_width;
|
|
|
|
vst->codecpar->height = xmv->video_height;
|
|
|
|
|
|
|
|
vst->duration = xmv->video_duration;
|
|
|
|
|
|
|
|
xmv->video.stream_index = vst->index;
|
|
|
|
|
|
|
|
xmv->video.created = 1;
|
|
|
|
}
|
|
|
|
|
2011-08-16 22:26:45 +03:00
|
|
|
/* Adding the audio data sizes and the video data size keeps you 4 bytes
|
|
|
|
* short for every audio track. But as playing around with XMV files with
|
|
|
|
* ADPCM audio showed, taking the extra 4 bytes from the audio data gives
|
|
|
|
* you either completely distorted audio or click (when skipping the
|
2012-07-06 02:38:53 +03:00
|
|
|
* remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
|
2011-08-16 22:26:45 +03:00
|
|
|
* audio track from the video data works at least for the audio. Probably
|
|
|
|
* some alignment thing?
|
|
|
|
* The video data has (always?) lots of padding, so it should work out...
|
|
|
|
*/
|
|
|
|
xmv->video.data_size -= xmv->audio_track_count * 4;
|
|
|
|
|
|
|
|
xmv->current_stream = 0;
|
|
|
|
if (!xmv->video.frame_count) {
|
|
|
|
xmv->video.frame_count = 1;
|
2012-04-21 20:41:54 +03:00
|
|
|
xmv->current_stream = xmv->stream_count > 1;
|
2011-08-16 22:26:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet audio header */
|
|
|
|
|
|
|
|
for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
|
|
|
|
XMVAudioPacket *packet = &xmv->audio[audio_track];
|
|
|
|
|
|
|
|
if (avio_read(pb, data, 4) != 4)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (!packet->created) {
|
|
|
|
AVStream *ast = avformat_new_stream(s, NULL);
|
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
ast->codecpar->codec_id = packet->codec_id;
|
|
|
|
ast->codecpar->codec_tag = packet->compression;
|
|
|
|
ast->codecpar->channels = packet->channels;
|
|
|
|
ast->codecpar->sample_rate = packet->sample_rate;
|
|
|
|
ast->codecpar->bits_per_coded_sample = packet->bits_per_sample;
|
|
|
|
ast->codecpar->bit_rate = packet->bit_rate;
|
|
|
|
ast->codecpar->block_align = 36 * packet->channels;
|
|
|
|
|
|
|
|
avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate);
|
|
|
|
|
|
|
|
packet->stream_index = ast->index;
|
|
|
|
|
|
|
|
ast->duration = xmv->video_duration;
|
|
|
|
|
|
|
|
packet->created = 1;
|
|
|
|
}
|
|
|
|
|
2011-08-16 22:26:45 +03:00
|
|
|
packet->data_size = AV_RL32(data) & 0x007FFFFF;
|
|
|
|
if ((packet->data_size == 0) && (audio_track != 0))
|
|
|
|
/* This happens when I create an XMV with several identical audio
|
|
|
|
* streams. From the size calculations, duplicating the previous
|
|
|
|
* stream's size works out, but the track data itself is silent.
|
|
|
|
* Maybe this should also redirect the offset to the previous track?
|
|
|
|
*/
|
|
|
|
packet->data_size = xmv->audio[audio_track - 1].data_size;
|
|
|
|
|
2011-08-18 20:00:21 +03:00
|
|
|
/* Carve up the audio data in frame_count slices */
|
2011-08-16 22:26:45 +03:00
|
|
|
packet->frame_size = packet->data_size / xmv->video.frame_count;
|
2011-08-18 19:37:14 +03:00
|
|
|
packet->frame_size -= packet->frame_size % packet->block_align;
|
2011-08-16 22:26:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet data offsets */
|
|
|
|
|
|
|
|
data_offset = avio_tell(pb);
|
|
|
|
|
|
|
|
xmv->video.data_offset = data_offset;
|
|
|
|
data_offset += xmv->video.data_size;
|
|
|
|
|
|
|
|
for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
|
|
|
|
xmv->audio[audio_track].data_offset = data_offset;
|
|
|
|
data_offset += xmv->audio[audio_track].data_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Video frames header */
|
|
|
|
|
|
|
|
/* Read new video extra data */
|
|
|
|
if (xmv->video.data_size > 0) {
|
|
|
|
if (xmv->video.has_extradata) {
|
|
|
|
xmv_read_extradata(xmv->video.extradata, pb);
|
|
|
|
|
|
|
|
xmv->video.data_size -= 4;
|
|
|
|
xmv->video.data_offset += 4;
|
|
|
|
|
|
|
|
if (xmv->video.stream_index >= 0) {
|
|
|
|
AVStream *vst = s->streams[xmv->video.stream_index];
|
|
|
|
|
2012-06-07 22:09:06 +03:00
|
|
|
av_assert0(xmv->video.stream_index < s->nb_streams);
|
2011-08-16 22:26:45 +03:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (vst->codecpar->extradata_size < 4) {
|
2016-04-10 21:58:15 +02:00
|
|
|
av_freep(&vst->codecpar->extradata);
|
2011-08-16 22:26:45 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0)
|
2014-12-15 16:51:04 +02:00
|
|
|
return ret;
|
2011-08-16 22:26:45 +03:00
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
memcpy(vst->codecpar->extradata, xmv->video.extradata, 4);
|
2011-08-16 22:26:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xmv_fetch_new_packet(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
int result;
|
|
|
|
|
2012-11-22 23:37:24 +03:00
|
|
|
if (xmv->this_packet_offset == xmv->next_packet_offset)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2011-08-16 22:26:45 +03:00
|
|
|
/* Seek to it */
|
|
|
|
xmv->this_packet_offset = xmv->next_packet_offset;
|
|
|
|
if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
/* Update the size */
|
|
|
|
xmv->this_packet_size = xmv->next_packet_size;
|
|
|
|
if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
/* Process the header */
|
|
|
|
result = xmv_process_packet_header(s);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Update the offset */
|
|
|
|
xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xmv_fetch_audio_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt, uint32_t stream)
|
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
XMVAudioPacket *audio = &xmv->audio[stream];
|
|
|
|
|
|
|
|
uint32_t data_size;
|
|
|
|
uint32_t block_count;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* Seek to it */
|
|
|
|
if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
|
|
|
|
/* Not the last frame, get at most frame_size bytes. */
|
|
|
|
data_size = FFMIN(audio->frame_size, audio->data_size);
|
|
|
|
else
|
|
|
|
/* Last frame, get the rest. */
|
|
|
|
data_size = audio->data_size;
|
|
|
|
|
|
|
|
/* Read the packet */
|
|
|
|
result = av_get_packet(pb, pkt, data_size);
|
|
|
|
if (result <= 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
pkt->stream_index = audio->stream_index;
|
|
|
|
|
|
|
|
/* Calculate the PTS */
|
|
|
|
|
2011-08-18 19:37:14 +03:00
|
|
|
block_count = data_size / audio->block_align;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
pkt->duration = block_count;
|
|
|
|
pkt->pts = audio->block_count;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
audio->block_count += block_count;
|
|
|
|
|
|
|
|
/* Advance offset */
|
|
|
|
audio->data_size -= data_size;
|
|
|
|
audio->data_offset += data_size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xmv_fetch_video_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
XMVVideoPacket *video = &xmv->video;
|
|
|
|
|
|
|
|
int result;
|
|
|
|
uint32_t frame_header;
|
|
|
|
uint32_t frame_size, frame_timestamp;
|
2011-08-18 20:23:52 +03:00
|
|
|
uint8_t *data, *end;
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
/* Seek to it */
|
|
|
|
if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
/* Read the frame header */
|
|
|
|
frame_header = avio_rl32(pb);
|
|
|
|
|
|
|
|
frame_size = (frame_header & 0x1FFFF) * 4 + 4;
|
|
|
|
frame_timestamp = (frame_header >> 17);
|
|
|
|
|
|
|
|
if ((frame_size + 4) > video->data_size)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
2011-08-18 20:23:52 +03:00
|
|
|
/* Get the packet data */
|
|
|
|
result = av_get_packet(pb, pkt, frame_size);
|
|
|
|
if (result != frame_size)
|
2011-08-16 22:26:45 +03:00
|
|
|
return result;
|
|
|
|
|
|
|
|
/* Contrary to normal WMV2 video, the bit stream in XMV's
|
|
|
|
* WMV2 is little-endian.
|
|
|
|
* TODO: This manual swap is of course suboptimal.
|
|
|
|
*/
|
2011-08-18 20:23:52 +03:00
|
|
|
for (data = pkt->data, end = pkt->data + frame_size; data < end; data += 4)
|
|
|
|
AV_WB32(data, AV_RL32(data));
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
pkt->stream_index = video->stream_index;
|
|
|
|
|
|
|
|
/* Calculate the PTS */
|
|
|
|
|
|
|
|
video->last_pts = frame_timestamp + video->pts;
|
|
|
|
|
|
|
|
pkt->duration = 0;
|
|
|
|
pkt->pts = video->last_pts;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
video->pts += frame_timestamp;
|
|
|
|
|
|
|
|
/* Keyframe? */
|
|
|
|
pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
|
|
|
|
|
|
|
|
/* Advance offset */
|
|
|
|
video->data_size -= frame_size + 4;
|
|
|
|
video->data_offset += frame_size + 4;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xmv_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
XMVDemuxContext *xmv = s->priv_data;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (xmv->video.current_frame == xmv->video.frame_count) {
|
|
|
|
/* No frames left in this packet, so we fetch a new one */
|
|
|
|
|
|
|
|
result = xmv_fetch_new_packet(s);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xmv->current_stream == 0) {
|
|
|
|
/* Fetch a video frame */
|
|
|
|
|
|
|
|
result = xmv_fetch_video_packet(s, pkt);
|
|
|
|
} else {
|
|
|
|
/* Fetch an audio frame */
|
|
|
|
|
|
|
|
result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
|
|
|
|
}
|
2015-11-06 03:13:36 +02:00
|
|
|
if (result) {
|
|
|
|
xmv->current_stream = 0;
|
|
|
|
xmv->video.current_frame = xmv->video.frame_count;
|
2015-11-06 03:11:01 +02:00
|
|
|
return result;
|
2015-11-06 03:13:36 +02:00
|
|
|
}
|
2015-11-06 03:11:01 +02:00
|
|
|
|
2011-08-16 22:26:45 +03:00
|
|
|
|
|
|
|
/* Increase our counters */
|
|
|
|
if (++xmv->current_stream >= xmv->stream_count) {
|
|
|
|
xmv->current_stream = 0;
|
|
|
|
xmv->video.current_frame += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_xmv_demuxer = {
|
|
|
|
.name = "xmv",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
|
2015-12-17 01:08:23 +02:00
|
|
|
.extensions = "xmv",
|
2011-08-16 22:26:45 +03:00
|
|
|
.priv_data_size = sizeof(XMVDemuxContext),
|
|
|
|
.read_probe = xmv_probe,
|
|
|
|
.read_header = xmv_read_header,
|
|
|
|
.read_packet = xmv_read_packet,
|
|
|
|
.read_close = xmv_read_close,
|
|
|
|
};
|