3ef8be2b |
/* |
ffa5ed24 |
* id RoQ (.roq) File Demuxer |
41ed7ab4 |
* Copyright (c) 2003 The FFmpeg project |
3ef8be2b |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
3ef8be2b |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
3ef8be2b |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
3ef8be2b |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
3ef8be2b |
*/
/** |
ba87f080 |
* @file |
ffa5ed24 |
* id RoQ format file demuxer |
3ef8be2b |
* by Mike Melanson (melanson@pcisys.net)
* for more information on the .roq file format, visit:
* http://www.csse.monash.edu.au/~timf/
*/
|
73e2007f |
#include "libavutil/channel_layout.h" |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
3ef8be2b |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
c1acd581 |
#include "avio_internal.h" |
3ef8be2b |
#define RoQ_MAGIC_NUMBER 0x1084
#define RoQ_CHUNK_PREAMBLE_SIZE 8
#define RoQ_AUDIO_SAMPLE_RATE 22050
#define RoQ_CHUNKS_TO_SCAN 30
#define RoQ_INFO 0x1001
#define RoQ_QUAD_CODEBOOK 0x1002
#define RoQ_QUAD_VQ 0x1011
#define RoQ_SOUND_MONO 0x1020
#define RoQ_SOUND_STEREO 0x1021
typedef struct RoqDemuxContext {
|
3f9257c5 |
int frame_rate; |
3ef8be2b |
int width;
int height;
int audio_channels;
int video_stream_index;
int audio_stream_index;
int64_t video_pts;
unsigned int audio_frame_count;
} RoqDemuxContext;
static int roq_probe(AVProbeData *p)
{ |
fead30d4 |
if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
(AV_RL32(&p->buf[2]) != 0xFFFFFFFF)) |
3ef8be2b |
return 0;
return AVPROBE_SCORE_MAX;
}
|
6e9651d1 |
static int roq_read_header(AVFormatContext *s) |
3ef8be2b |
{
RoqDemuxContext *roq = s->priv_data; |
471fe57e |
AVIOContext *pb = s->pb; |
3ef8be2b |
unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
/* get the main header */ |
e63a3628 |
if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != |
3ef8be2b |
RoQ_CHUNK_PREAMBLE_SIZE) |
6f3e0b21 |
return AVERROR(EIO); |
3f9257c5 |
roq->frame_rate = AV_RL16(&preamble[6]); |
3ef8be2b |
/* init private context parameters */ |
115329f1 |
roq->width = roq->height = roq->audio_channels = roq->video_pts = |
3ef8be2b |
roq->audio_frame_count = 0; |
15969b55 |
roq->audio_stream_index = -1; |
3f9257c5 |
roq->video_stream_index = -1; |
3ef8be2b |
|
3f9257c5 |
s->ctx_flags |= AVFMTCTX_NOHEADER; |
3ef8be2b |
return 0;
}
static int roq_read_packet(AVFormatContext *s,
AVPacket *pkt)
{
RoqDemuxContext *roq = s->priv_data; |
471fe57e |
AVIOContext *pb = s->pb; |
3ef8be2b |
int ret = 0;
unsigned int chunk_size;
unsigned int chunk_type;
unsigned int codebook_size;
unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
int packet_read = 0; |
bc5c918e |
int64_t codebook_offset; |
3ef8be2b |
while (!packet_read) {
|
d34ec64a |
if (avio_feof(s->pb)) |
6f3e0b21 |
return AVERROR(EIO); |
3ef8be2b |
/* get the next chunk preamble */ |
e63a3628 |
if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) != |
3ef8be2b |
RoQ_CHUNK_PREAMBLE_SIZE) |
6f3e0b21 |
return AVERROR(EIO); |
3ef8be2b |
|
fead30d4 |
chunk_type = AV_RL16(&preamble[0]);
chunk_size = AV_RL32(&preamble[2]); |
0ecca7a4 |
if(chunk_size > INT_MAX)
return AVERROR_INVALIDDATA; |
3ef8be2b |
|
c1acd581 |
chunk_size = ffio_limit(pb, chunk_size);
|
3ef8be2b |
switch (chunk_type) {
case RoQ_INFO: |
3f9257c5 |
if (roq->video_stream_index == -1) {
AVStream *st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);
avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
roq->video_stream_index = st->index; |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_ROQ;
st->codecpar->codec_tag = 0; /* no fourcc */ |
3f9257c5 |
|
e63a3628 |
if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE) |
15969b55 |
return AVERROR(EIO); |
9200514a |
st->codecpar->width = roq->width = AV_RL16(preamble);
st->codecpar->height = roq->height = AV_RL16(preamble + 2); |
15969b55 |
break;
} |
3ef8be2b |
/* don't care about this chunk anymore */ |
45a8a02a |
avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE); |
3ef8be2b |
break;
case RoQ_QUAD_CODEBOOK: |
bcbe4f3c |
if (roq->video_stream_index < 0)
return AVERROR_INVALIDDATA; |
3ef8be2b |
/* packet needs to contain both this codebook and next VQ chunk */ |
384c9c2f |
codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE; |
3ef8be2b |
codebook_size = chunk_size; |
45a8a02a |
avio_skip(pb, codebook_size); |
e63a3628 |
if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != |
3ef8be2b |
RoQ_CHUNK_PREAMBLE_SIZE) |
6f3e0b21 |
return AVERROR(EIO); |
fead30d4 |
chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 + |
3ef8be2b |
codebook_size;
|
744a0b52 |
if (chunk_size > INT_MAX)
return AVERROR_INVALIDDATA;
|
3ef8be2b |
/* rewind */ |
f59d8ff8 |
avio_seek(pb, codebook_offset, SEEK_SET); |
3ef8be2b |
/* load up the packet */ |
2692067a |
ret= av_get_packet(pb, pkt, chunk_size);
if (ret != chunk_size) |
6f3e0b21 |
return AVERROR(EIO); |
3ef8be2b |
pkt->stream_index = roq->video_stream_index; |
abb785f1 |
pkt->pts = roq->video_pts++; |
3ef8be2b |
packet_read = 1;
break;
case RoQ_SOUND_MONO:
case RoQ_SOUND_STEREO: |
15969b55 |
if (roq->audio_stream_index == -1) { |
84ad31ff |
AVStream *st = avformat_new_stream(s, NULL); |
15969b55 |
if (!st)
return AVERROR(ENOMEM); |
c3f9ebf7 |
avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE); |
15969b55 |
roq->audio_stream_index = st->index; |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = AV_CODEC_ID_ROQ_DPCM;
st->codecpar->codec_tag = 0; /* no tag */ |
73e2007f |
if (chunk_type == RoQ_SOUND_STEREO) { |
9200514a |
st->codecpar->channels = 2;
st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; |
73e2007f |
} else { |
9200514a |
st->codecpar->channels = 1;
st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; |
73e2007f |
} |
9200514a |
roq->audio_channels = st->codecpar->channels;
st->codecpar->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
st->codecpar->bits_per_coded_sample = 16;
st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
st->codecpar->bits_per_coded_sample;
st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample; |
15969b55 |
} |
3ef8be2b |
case RoQ_QUAD_VQ: |
bcbe4f3c |
if (chunk_type == RoQ_QUAD_VQ) {
if (roq->video_stream_index < 0)
return AVERROR_INVALIDDATA;
}
|
3ef8be2b |
/* load up the packet */
if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE)) |
6f3e0b21 |
return AVERROR(EIO); |
3ef8be2b |
/* copy over preamble */
memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
if (chunk_type == RoQ_QUAD_VQ) {
pkt->stream_index = roq->video_stream_index; |
abb785f1 |
pkt->pts = roq->video_pts++; |
3ef8be2b |
} else {
pkt->stream_index = roq->audio_stream_index;
pkt->pts = roq->audio_frame_count;
roq->audio_frame_count += (chunk_size / roq->audio_channels);
}
|
384c9c2f |
pkt->pos= avio_tell(pb); |
e63a3628 |
ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE, |
d42f74b8 |
chunk_size); |
c0175fa9 |
if (ret != chunk_size) {
av_packet_unref(pkt); |
6f3e0b21 |
ret = AVERROR(EIO); |
c0175fa9 |
} |
3ef8be2b |
packet_read = 1;
break;
default: |
bc874dae |
av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type); |
3ef8be2b |
return AVERROR_INVALIDDATA;
}
}
return ret;
}
|
66355be3 |
AVInputFormat ff_roq_demuxer = { |
68aef0b4 |
.name = "roq", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("id RoQ"), |
dfc2c4d9 |
.priv_data_size = sizeof(RoqDemuxContext),
.read_probe = roq_probe,
.read_header = roq_read_header,
.read_packet = roq_read_packet, |
3ef8be2b |
}; |