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 
75 {
76  AVIOContext *pb = s->pb;
77  AVStream *st;
78  unsigned int frame_count;
79  unsigned int audio_frame_counter = 0;
80  unsigned int video_frame_counter = 0;
81  unsigned int back_size;
82  unsigned short sound_rate;
83  unsigned short rate;
84  unsigned short channels;
85  unsigned short def_sound_size;
86  unsigned int signature;
87  unsigned int pts_den = 11025; /* video only case */
88  unsigned int pts_num = 1103;
89  unsigned int* chunk_offset = NULL;
90  int* chunk_size = NULL;
91  int* audio_size = NULL;
92  int i;
93  int ret = 0;
94 
95  avio_skip(pb,4); /* skip FORM tag */
96  back_size = avio_rl32(pb);
97  signature = avio_rb32(pb);
98  avio_skip(pb, 4); /* data size */
99  frame_count = avio_rl32(pb);
100 
101  /* disallow back_sizes and frame_counts that may lead to overflows later */
102  if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
103  return AVERROR_INVALIDDATA;
104 
105  avio_skip(pb, 2); /* encoding mentod */
106  sound_rate = avio_rl16(pb);
107  rate = avio_rl16(pb);
108  channels = avio_rl16(pb);
109  def_sound_size = avio_rl16(pb);
110 
112  st = avformat_new_stream(s, NULL);
113  if(!st)
114  return AVERROR(ENOMEM);
115 
118  st->codec->codec_tag = 0; /* no fourcc */
119  st->codec->width = 320;
120  st->codec->height = 200;
121 
124 
125  if(signature == RLV3_TAG && back_size > 0)
126  st->codec->extradata_size += back_size;
127 
130  if(!st->codec->extradata)
131  return AVERROR(ENOMEM);
132 
133  if(avio_read(pb,st->codec->extradata,st->codec->extradata_size) !=
134  st->codec->extradata_size)
135  return AVERROR(EIO);
136 
138  if(sound_rate){
139  pts_num = def_sound_size;
140  pts_den = rate;
141 
142  st = avformat_new_stream(s, NULL);
143  if (!st)
144  return AVERROR(ENOMEM);
147  st->codec->codec_tag = 1;
148  st->codec->channels = channels;
149  st->codec->bits_per_coded_sample = 8;
150  st->codec->sample_rate = rate;
151  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
153  st->codec->block_align = st->codec->channels *
154  st->codec->bits_per_coded_sample / 8;
155  avpriv_set_pts_info(st,32,1,rate);
156  }
157 
158  avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
159 
160  chunk_size = av_malloc(frame_count * sizeof(uint32_t));
161  audio_size = av_malloc(frame_count * sizeof(uint32_t));
162  chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
163 
164  if(!chunk_size || !audio_size || !chunk_offset){
165  av_free(chunk_size);
166  av_free(audio_size);
167  av_free(chunk_offset);
168  return AVERROR(ENOMEM);
169  }
170 
172  for(i=0; i < frame_count;i++)
173  chunk_size[i] = avio_rl32(pb);
174  for(i=0; i < frame_count;i++)
175  chunk_offset[i] = avio_rl32(pb);
176  for(i=0; i < frame_count;i++)
177  audio_size[i] = avio_rl32(pb) & 0xFFFF;
178 
180  for(i=0;i<frame_count;i++){
181  if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
182  ret = AVERROR_INVALIDDATA;
183  break;
184  }
185 
186  if(sound_rate && audio_size[i]){
187  av_add_index_entry(s->streams[1], chunk_offset[i],
188  audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
189  audio_frame_counter += audio_size[i] / channels;
190  }
191  av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
192  video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
193  ++video_frame_counter;
194  }
195 
196 
197  av_free(chunk_size);
198  av_free(audio_size);
199  av_free(chunk_offset);
200 
201  return ret;
202 }
203 
211  AVPacket *pkt)
212 {
213  Rl2DemuxContext *rl2 = s->priv_data;
214  AVIOContext *pb = s->pb;
216  int i;
217  int ret = 0;
218  int stream_id = -1;
219  int64_t pos = INT64_MAX;
220 
222  for(i=0; i<s->nb_streams; i++){
223  if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
224  && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
225  sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
226  pos= sample->pos;
227  stream_id= i;
228  }
229  }
230 
231  if(stream_id == -1)
232  return AVERROR(EIO);
233 
234  ++rl2->index_pos[stream_id];
235 
237  avio_seek(pb, sample->pos, SEEK_SET);
238 
240  ret = av_get_packet(pb, pkt, sample->size);
241  if(ret != sample->size){
242  av_free_packet(pkt);
243  return AVERROR(EIO);
244  }
245 
246  pkt->stream_index = stream_id;
247  pkt->pts = sample->timestamp;
248 
249  return ret;
250 }
251 
260 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
261 {
262  AVStream *st = s->streams[stream_index];
263  Rl2DemuxContext *rl2 = s->priv_data;
264  int i;
265  int index = av_index_search_timestamp(st, timestamp, flags);
266  if(index < 0)
267  return -1;
268 
269  rl2->index_pos[stream_index] = index;
270  timestamp = st->index_entries[index].timestamp;
271 
272  for(i=0; i < s->nb_streams; i++){
273  AVStream *st2 = s->streams[i];
274  index = av_index_search_timestamp(st2,
275  av_rescale_q(timestamp, st->time_base, st2->time_base),
276  flags | AVSEEK_FLAG_BACKWARD);
277 
278  if(index < 0)
279  index = 0;
280 
281  rl2->index_pos[i] = index;
282  }
283 
284  return 0;
285 }
286 
288  .name = "rl2",
289  .long_name = NULL_IF_CONFIG_SMALL("RL2"),
290  .priv_data_size = sizeof(Rl2DemuxContext),
295 };