00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/intreadwrite.h"
00031 #include "avformat.h"
00032 #include "internal.h"
00033 #include "avio_internal.h"
00034
00035 #define RoQ_MAGIC_NUMBER 0x1084
00036 #define RoQ_CHUNK_PREAMBLE_SIZE 8
00037 #define RoQ_AUDIO_SAMPLE_RATE 22050
00038 #define RoQ_CHUNKS_TO_SCAN 30
00039
00040 #define RoQ_INFO 0x1001
00041 #define RoQ_QUAD_CODEBOOK 0x1002
00042 #define RoQ_QUAD_VQ 0x1011
00043 #define RoQ_SOUND_MONO 0x1020
00044 #define RoQ_SOUND_STEREO 0x1021
00045
00046 typedef struct RoqDemuxContext {
00047
00048 int frame_rate;
00049 int width;
00050 int height;
00051 int audio_channels;
00052
00053 int video_stream_index;
00054 int audio_stream_index;
00055
00056 int64_t video_pts;
00057 unsigned int audio_frame_count;
00058
00059 } RoqDemuxContext;
00060
00061 static int roq_probe(AVProbeData *p)
00062 {
00063 if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
00064 (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
00065 return 0;
00066
00067 return AVPROBE_SCORE_MAX;
00068 }
00069
00070 static int roq_read_header(AVFormatContext *s)
00071 {
00072 RoqDemuxContext *roq = s->priv_data;
00073 AVIOContext *pb = s->pb;
00074 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
00075
00076
00077 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
00078 RoQ_CHUNK_PREAMBLE_SIZE)
00079 return AVERROR(EIO);
00080 roq->frame_rate = AV_RL16(&preamble[6]);
00081
00082
00083 roq->width = roq->height = roq->audio_channels = roq->video_pts =
00084 roq->audio_frame_count = 0;
00085 roq->audio_stream_index = -1;
00086 roq->video_stream_index = -1;
00087
00088 s->ctx_flags |= AVFMTCTX_NOHEADER;
00089
00090 return 0;
00091 }
00092
00093 static int roq_read_packet(AVFormatContext *s,
00094 AVPacket *pkt)
00095 {
00096 RoqDemuxContext *roq = s->priv_data;
00097 AVIOContext *pb = s->pb;
00098 int ret = 0;
00099 unsigned int chunk_size;
00100 unsigned int chunk_type;
00101 unsigned int codebook_size;
00102 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
00103 int packet_read = 0;
00104 int64_t codebook_offset;
00105
00106 while (!packet_read) {
00107
00108 if (url_feof(s->pb))
00109 return AVERROR(EIO);
00110
00111
00112 if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
00113 RoQ_CHUNK_PREAMBLE_SIZE)
00114 return AVERROR(EIO);
00115
00116 chunk_type = AV_RL16(&preamble[0]);
00117 chunk_size = AV_RL32(&preamble[2]);
00118 if(chunk_size > INT_MAX)
00119 return AVERROR_INVALIDDATA;
00120
00121 chunk_size = ffio_limit(pb, chunk_size);
00122
00123 switch (chunk_type) {
00124
00125 case RoQ_INFO:
00126 if (roq->video_stream_index == -1) {
00127 AVStream *st = avformat_new_stream(s, NULL);
00128 if (!st)
00129 return AVERROR(ENOMEM);
00130 avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
00131 roq->video_stream_index = st->index;
00132 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00133 st->codec->codec_id = AV_CODEC_ID_ROQ;
00134 st->codec->codec_tag = 0;
00135
00136 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
00137 return AVERROR(EIO);
00138 st->codec->width = roq->width = AV_RL16(preamble);
00139 st->codec->height = roq->height = AV_RL16(preamble + 2);
00140 break;
00141 }
00142
00143 avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
00144 break;
00145
00146 case RoQ_QUAD_CODEBOOK:
00147
00148 codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
00149 codebook_size = chunk_size;
00150 avio_skip(pb, codebook_size);
00151 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
00152 RoQ_CHUNK_PREAMBLE_SIZE)
00153 return AVERROR(EIO);
00154 chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
00155 codebook_size;
00156
00157
00158 avio_seek(pb, codebook_offset, SEEK_SET);
00159
00160
00161 ret= av_get_packet(pb, pkt, chunk_size);
00162 if (ret != chunk_size)
00163 return AVERROR(EIO);
00164 pkt->stream_index = roq->video_stream_index;
00165 pkt->pts = roq->video_pts++;
00166
00167 packet_read = 1;
00168 break;
00169
00170 case RoQ_SOUND_MONO:
00171 case RoQ_SOUND_STEREO:
00172 if (roq->audio_stream_index == -1) {
00173 AVStream *st = avformat_new_stream(s, NULL);
00174 if (!st)
00175 return AVERROR(ENOMEM);
00176 avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
00177 roq->audio_stream_index = st->index;
00178 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00179 st->codec->codec_id = AV_CODEC_ID_ROQ_DPCM;
00180 st->codec->codec_tag = 0;
00181 st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
00182 st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
00183 st->codec->bits_per_coded_sample = 16;
00184 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00185 st->codec->bits_per_coded_sample;
00186 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00187 }
00188 case RoQ_QUAD_VQ:
00189
00190 if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
00191 return AVERROR(EIO);
00192
00193 memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
00194
00195 if (chunk_type == RoQ_QUAD_VQ) {
00196 pkt->stream_index = roq->video_stream_index;
00197 pkt->pts = roq->video_pts++;
00198 } else {
00199 pkt->stream_index = roq->audio_stream_index;
00200 pkt->pts = roq->audio_frame_count;
00201 roq->audio_frame_count += (chunk_size / roq->audio_channels);
00202 }
00203
00204 pkt->pos= avio_tell(pb);
00205 ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
00206 chunk_size);
00207 if (ret != chunk_size)
00208 ret = AVERROR(EIO);
00209
00210 packet_read = 1;
00211 break;
00212
00213 default:
00214 av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
00215 return AVERROR_INVALIDDATA;
00216 }
00217 }
00218
00219 return ret;
00220 }
00221
00222 AVInputFormat ff_roq_demuxer = {
00223 .name = "roq",
00224 .long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
00225 .priv_data_size = sizeof(RoqDemuxContext),
00226 .read_probe = roq_probe,
00227 .read_header = roq_read_header,
00228 .read_packet = roq_read_packet,
00229 };