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 width;
00049 int height;
00050 int audio_channels;
00051
00052 int video_stream_index;
00053 int audio_stream_index;
00054
00055 int64_t video_pts;
00056 unsigned int audio_frame_count;
00057
00058 } RoqDemuxContext;
00059
00060 static int roq_probe(AVProbeData *p)
00061 {
00062 if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
00063 (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
00064 return 0;
00065
00066 return AVPROBE_SCORE_MAX;
00067 }
00068
00069 static int roq_read_header(AVFormatContext *s,
00070 AVFormatParameters *ap)
00071 {
00072 RoqDemuxContext *roq = s->priv_data;
00073 AVIOContext *pb = s->pb;
00074 int framerate;
00075 AVStream *st;
00076 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
00077
00078
00079 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
00080 RoQ_CHUNK_PREAMBLE_SIZE)
00081 return AVERROR(EIO);
00082 framerate = AV_RL16(&preamble[6]);
00083
00084
00085 roq->width = roq->height = roq->audio_channels = roq->video_pts =
00086 roq->audio_frame_count = 0;
00087 roq->audio_stream_index = -1;
00088
00089 st = avformat_new_stream(s, NULL);
00090 if (!st)
00091 return AVERROR(ENOMEM);
00092 avpriv_set_pts_info(st, 63, 1, framerate);
00093 roq->video_stream_index = st->index;
00094 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00095 st->codec->codec_id = CODEC_ID_ROQ;
00096 st->codec->codec_tag = 0;
00097
00098 return 0;
00099 }
00100
00101 static int roq_read_packet(AVFormatContext *s,
00102 AVPacket *pkt)
00103 {
00104 RoqDemuxContext *roq = s->priv_data;
00105 AVIOContext *pb = s->pb;
00106 int ret = 0;
00107 unsigned int chunk_size;
00108 unsigned int chunk_type;
00109 unsigned int codebook_size;
00110 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
00111 int packet_read = 0;
00112 int64_t codebook_offset;
00113
00114 while (!packet_read) {
00115
00116 if (url_feof(s->pb))
00117 return AVERROR(EIO);
00118
00119
00120 if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
00121 RoQ_CHUNK_PREAMBLE_SIZE)
00122 return AVERROR(EIO);
00123
00124 chunk_type = AV_RL16(&preamble[0]);
00125 chunk_size = AV_RL32(&preamble[2]);
00126 if(chunk_size > INT_MAX)
00127 return AVERROR_INVALIDDATA;
00128
00129 chunk_size = ffio_limit(pb, chunk_size);
00130
00131 switch (chunk_type) {
00132
00133 case RoQ_INFO:
00134 if (!roq->width || !roq->height) {
00135 AVStream *st = s->streams[roq->video_stream_index];
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 = 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 format"),
00225 .priv_data_size = sizeof(RoqDemuxContext),
00226 .read_probe = roq_probe,
00227 .read_header = roq_read_header,
00228 .read_packet = roq_read_packet,
00229 };