rl2.c
Go to the documentation of this file.
1 /*
2  * RL2 Format Demuxer
3  * Copyright (c) 2008 Sascha Sommer (saschasommer@freenet.de)
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/mathematics.h"
37 #include "avformat.h"
38 #include "internal.h"
39 
40 #define EXTRADATA1_SIZE (6 + 256 * 3)
41 
42 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
43 #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
44 #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
45 
46 typedef struct Rl2DemuxContext {
47  unsigned int index_pos[2];
49 
50 
56 static int rl2_probe(AVProbeData *p)
57 {
58 
59  if(AV_RB32(&p->buf[0]) != FORM_TAG)
60  return 0;
61 
62  if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
63  AV_RB32(&p->buf[8]) != RLV3_TAG)
64  return 0;
65 
66  return AVPROBE_SCORE_MAX;
67 }
68 
77 {
78  AVIOContext *pb = s->pb;
79  AVStream *st;
80  unsigned int frame_count;
81  unsigned int audio_frame_counter = 0;
82  unsigned int video_frame_counter = 0;
83  unsigned int back_size;
84  unsigned short sound_rate;
85  unsigned short rate;
86  unsigned short channels;
87  unsigned short def_sound_size;
88  unsigned int signature;
89  unsigned int pts_den = 11025; /* video only case */
90  unsigned int pts_num = 1103;
91  unsigned int* chunk_offset = NULL;
92  int* chunk_size = NULL;
93  int* audio_size = NULL;
94  int i;
95  int ret = 0;
96 
97  avio_skip(pb,4); /* skip FORM tag */
98  back_size = avio_rl32(pb);
99  signature = avio_rb32(pb);
100  avio_skip(pb, 4); /* data size */
101  frame_count = avio_rl32(pb);
102 
103  /* disallow back_sizes and frame_counts that may lead to overflows later */
104  if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
105  return AVERROR_INVALIDDATA;
106 
107  avio_skip(pb, 2); /* encoding mentod */
108  sound_rate = avio_rl16(pb);
109  rate = avio_rl16(pb);
110  channels = avio_rl16(pb);
111  def_sound_size = avio_rl16(pb);
112  if (!channels || channels > 42) {
113  av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
114  return AVERROR_INVALIDDATA;
115  }
116 
118  st = avformat_new_stream(s, NULL);
119  if(!st)
120  return AVERROR(ENOMEM);
121 
123  st->codec->codec_id = CODEC_ID_RL2;
124  st->codec->codec_tag = 0; /* no fourcc */
125  st->codec->width = 320;
126  st->codec->height = 200;
127 
130 
131  if(signature == RLV3_TAG && back_size > 0)
132  st->codec->extradata_size += back_size;
133 
136  if(!st->codec->extradata)
137  return AVERROR(ENOMEM);
138 
139  if(avio_read(pb,st->codec->extradata,st->codec->extradata_size) !=
140  st->codec->extradata_size)
141  return AVERROR(EIO);
142 
144  if(sound_rate){
145  pts_num = def_sound_size;
146  pts_den = rate;
147 
148  st = avformat_new_stream(s, NULL);
149  if (!st)
150  return AVERROR(ENOMEM);
153  st->codec->codec_tag = 1;
154  st->codec->channels = channels;
155  st->codec->bits_per_coded_sample = 8;
156  st->codec->sample_rate = rate;
157  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
159  st->codec->block_align = st->codec->channels *
160  st->codec->bits_per_coded_sample / 8;
161  avpriv_set_pts_info(st,32,1,rate);
162  }
163 
164  avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
165 
166  chunk_size = av_malloc(frame_count * sizeof(uint32_t));
167  audio_size = av_malloc(frame_count * sizeof(uint32_t));
168  chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
169 
170  if(!chunk_size || !audio_size || !chunk_offset){
171  av_free(chunk_size);
172  av_free(audio_size);
173  av_free(chunk_offset);
174  return AVERROR(ENOMEM);
175  }
176 
178  for(i=0; i < frame_count;i++)
179  chunk_size[i] = avio_rl32(pb);
180  for(i=0; i < frame_count;i++)
181  chunk_offset[i] = avio_rl32(pb);
182  for(i=0; i < frame_count;i++)
183  audio_size[i] = avio_rl32(pb) & 0xFFFF;
184 
186  for(i=0;i<frame_count;i++){
187  if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
188  ret = AVERROR_INVALIDDATA;
189  break;
190  }
191 
192  if(sound_rate && audio_size[i]){
193  av_add_index_entry(s->streams[1], chunk_offset[i],
194  audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
195  audio_frame_counter += audio_size[i] / channels;
196  }
197  av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
198  video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
199  ++video_frame_counter;
200  }
201 
202 
203  av_free(chunk_size);
204  av_free(audio_size);
205  av_free(chunk_offset);
206 
207  return ret;
208 }
209 
217  AVPacket *pkt)
218 {
219  Rl2DemuxContext *rl2 = s->priv_data;
220  AVIOContext *pb = s->pb;
221  AVIndexEntry *sample = NULL;
222  int i;
223  int ret = 0;
224  int stream_id = -1;
225  int64_t pos = INT64_MAX;
226 
228  for(i=0; i<s->nb_streams; i++){
229  if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
230  && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
231  sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
232  pos= sample->pos;
233  stream_id= i;
234  }
235  }
236 
237  if(stream_id == -1)
238  return AVERROR(EIO);
239 
240  ++rl2->index_pos[stream_id];
241 
243  avio_seek(pb, sample->pos, SEEK_SET);
244 
246  ret = av_get_packet(pb, pkt, sample->size);
247  if(ret != sample->size){
248  av_free_packet(pkt);
249  return AVERROR(EIO);
250  }
251 
252  pkt->stream_index = stream_id;
253  pkt->pts = sample->timestamp;
254 
255  return ret;
256 }
257 
266 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
267 {
268  AVStream *st = s->streams[stream_index];
269  Rl2DemuxContext *rl2 = s->priv_data;
270  int i;
271  int index = av_index_search_timestamp(st, timestamp, flags);
272  if(index < 0)
273  return -1;
274 
275  rl2->index_pos[stream_index] = index;
276  timestamp = st->index_entries[index].timestamp;
277 
278  for(i=0; i < s->nb_streams; i++){
279  AVStream *st2 = s->streams[i];
280  index = av_index_search_timestamp(st2,
281  av_rescale_q(timestamp, st->time_base, st2->time_base),
282  flags | AVSEEK_FLAG_BACKWARD);
283 
284  if(index < 0)
285  index = 0;
286 
287  rl2->index_pos[i] = index;
288  }
289 
290  return 0;
291 }
292 
294  .name = "rl2",
295  .long_name = NULL_IF_CONFIG_SMALL("RL2 format"),
296  .priv_data_size = sizeof(Rl2DemuxContext),
301 };
302