00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00035 #include "libavutil/intreadwrite.h"
00036 #include "libavutil/mathematics.h"
00037 #include "avformat.h"
00038 #include "internal.h"
00039
00040 #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
00041
00042 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
00043 #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
00044 #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
00045
00046 typedef struct Rl2DemuxContext {
00047 unsigned int index_pos[2];
00048 } Rl2DemuxContext;
00049
00050
00056 static int rl2_probe(AVProbeData *p)
00057 {
00058
00059 if(AV_RB32(&p->buf[0]) != FORM_TAG)
00060 return 0;
00061
00062 if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
00063 AV_RB32(&p->buf[8]) != RLV3_TAG)
00064 return 0;
00065
00066 return AVPROBE_SCORE_MAX;
00067 }
00068
00075 static av_cold int rl2_read_header(AVFormatContext *s,
00076 AVFormatParameters *ap)
00077 {
00078 AVIOContext *pb = s->pb;
00079 AVStream *st;
00080 unsigned int frame_count;
00081 unsigned int audio_frame_counter = 0;
00082 unsigned int video_frame_counter = 0;
00083 unsigned int back_size;
00084 unsigned short sound_rate;
00085 unsigned short rate;
00086 unsigned short channels;
00087 unsigned short def_sound_size;
00088 unsigned int signature;
00089 unsigned int pts_den = 11025;
00090 unsigned int pts_num = 1103;
00091 unsigned int* chunk_offset = NULL;
00092 int* chunk_size = NULL;
00093 int* audio_size = NULL;
00094 int i;
00095 int ret = 0;
00096
00097 avio_skip(pb,4);
00098 back_size = avio_rl32(pb);
00099 signature = avio_rb32(pb);
00100 avio_skip(pb, 4);
00101 frame_count = avio_rl32(pb);
00102
00103
00104 if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
00105 return AVERROR_INVALIDDATA;
00106
00107 avio_skip(pb, 2);
00108 sound_rate = avio_rl16(pb);
00109 rate = avio_rl16(pb);
00110 channels = avio_rl16(pb);
00111 def_sound_size = avio_rl16(pb);
00112 if (!channels || channels > 42) {
00113 av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
00114 return AVERROR_INVALIDDATA;
00115 }
00116
00118 st = avformat_new_stream(s, NULL);
00119 if(!st)
00120 return AVERROR(ENOMEM);
00121
00122 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00123 st->codec->codec_id = CODEC_ID_RL2;
00124 st->codec->codec_tag = 0;
00125 st->codec->width = 320;
00126 st->codec->height = 200;
00127
00129 st->codec->extradata_size = EXTRADATA1_SIZE;
00130
00131 if(signature == RLV3_TAG && back_size > 0)
00132 st->codec->extradata_size += back_size;
00133
00134 st->codec->extradata = av_mallocz(st->codec->extradata_size +
00135 FF_INPUT_BUFFER_PADDING_SIZE);
00136 if(!st->codec->extradata)
00137 return AVERROR(ENOMEM);
00138
00139 if(avio_read(pb,st->codec->extradata,st->codec->extradata_size) !=
00140 st->codec->extradata_size)
00141 return AVERROR(EIO);
00142
00144 if(sound_rate){
00145 pts_num = def_sound_size;
00146 pts_den = rate;
00147
00148 st = avformat_new_stream(s, NULL);
00149 if (!st)
00150 return AVERROR(ENOMEM);
00151 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00152 st->codec->codec_id = CODEC_ID_PCM_U8;
00153 st->codec->codec_tag = 1;
00154 st->codec->channels = channels;
00155 st->codec->bits_per_coded_sample = 8;
00156 st->codec->sample_rate = rate;
00157 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00158 st->codec->bits_per_coded_sample;
00159 st->codec->block_align = st->codec->channels *
00160 st->codec->bits_per_coded_sample / 8;
00161 avpriv_set_pts_info(st,32,1,rate);
00162 }
00163
00164 avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
00165
00166 chunk_size = av_malloc(frame_count * sizeof(uint32_t));
00167 audio_size = av_malloc(frame_count * sizeof(uint32_t));
00168 chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
00169
00170 if(!chunk_size || !audio_size || !chunk_offset){
00171 av_free(chunk_size);
00172 av_free(audio_size);
00173 av_free(chunk_offset);
00174 return AVERROR(ENOMEM);
00175 }
00176
00178 for(i=0; i < frame_count;i++)
00179 chunk_size[i] = avio_rl32(pb);
00180 for(i=0; i < frame_count;i++)
00181 chunk_offset[i] = avio_rl32(pb);
00182 for(i=0; i < frame_count;i++)
00183 audio_size[i] = avio_rl32(pb) & 0xFFFF;
00184
00186 for(i=0;i<frame_count;i++){
00187 if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
00188 ret = AVERROR_INVALIDDATA;
00189 break;
00190 }
00191
00192 if(sound_rate && audio_size[i]){
00193 av_add_index_entry(s->streams[1], chunk_offset[i],
00194 audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
00195 audio_frame_counter += audio_size[i] / channels;
00196 }
00197 av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
00198 video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
00199 ++video_frame_counter;
00200 }
00201
00202
00203 av_free(chunk_size);
00204 av_free(audio_size);
00205 av_free(chunk_offset);
00206
00207 return ret;
00208 }
00209
00216 static int rl2_read_packet(AVFormatContext *s,
00217 AVPacket *pkt)
00218 {
00219 Rl2DemuxContext *rl2 = s->priv_data;
00220 AVIOContext *pb = s->pb;
00221 AVIndexEntry *sample = NULL;
00222 int i;
00223 int ret = 0;
00224 int stream_id = -1;
00225 int64_t pos = INT64_MAX;
00226
00228 for(i=0; i<s->nb_streams; i++){
00229 if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
00230 && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
00231 sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
00232 pos= sample->pos;
00233 stream_id= i;
00234 }
00235 }
00236
00237 if(stream_id == -1)
00238 return AVERROR(EIO);
00239
00240 ++rl2->index_pos[stream_id];
00241
00243 avio_seek(pb, sample->pos, SEEK_SET);
00244
00246 ret = av_get_packet(pb, pkt, sample->size);
00247 if(ret != sample->size){
00248 av_free_packet(pkt);
00249 return AVERROR(EIO);
00250 }
00251
00252 pkt->stream_index = stream_id;
00253 pkt->pts = sample->timestamp;
00254
00255 return ret;
00256 }
00257
00266 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00267 {
00268 AVStream *st = s->streams[stream_index];
00269 Rl2DemuxContext *rl2 = s->priv_data;
00270 int i;
00271 int index = av_index_search_timestamp(st, timestamp, flags);
00272 if(index < 0)
00273 return -1;
00274
00275 rl2->index_pos[stream_index] = index;
00276 timestamp = st->index_entries[index].timestamp;
00277
00278 for(i=0; i < s->nb_streams; i++){
00279 AVStream *st2 = s->streams[i];
00280 index = av_index_search_timestamp(st2,
00281 av_rescale_q(timestamp, st->time_base, st2->time_base),
00282 flags | AVSEEK_FLAG_BACKWARD);
00283
00284 if(index < 0)
00285 index = 0;
00286
00287 rl2->index_pos[i] = index;
00288 }
00289
00290 return 0;
00291 }
00292
00293 AVInputFormat ff_rl2_demuxer = {
00294 .name = "rl2",
00295 .long_name = NULL_IF_CONFIG_SMALL("RL2 format"),
00296 .priv_data_size = sizeof(Rl2DemuxContext),
00297 .read_probe = rl2_probe,
00298 .read_header = rl2_read_header,
00299 .read_packet = rl2_read_packet,
00300 .read_seek = rl2_read_seek,
00301 };
00302