]> git.sesse.net Git - ffmpeg/blob - libavformat/rl2.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / rl2.c
1 /*
2  * RL2 Format Demuxer
3  * Copyright (c) 2008 Sascha Sommer (saschasommer@freenet.de)
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * RL2 file demuxer
24  * @file
25  * @author Sascha Sommer (saschasommer@freenet.de)
26  * @see http://wiki.multimedia.cx/index.php?title=RL2
27  *
28  * extradata:
29  * 2 byte le initial drawing offset within 320x200 viewport
30  * 4 byte le number of used colors
31  * 256 * 3 bytes rgb palette
32  * optional background_frame
33  */
34
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) ///< video base, clr, palette
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];   ///< indexes in the sample tables
48 } Rl2DemuxContext;
49
50
51 /**
52  * check if the file is in rl2 format
53  * @param p probe buffer
54  * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
55  */
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
69 /**
70  * read rl2 header data and setup the avstreams
71  * @param s demuxer context
72  * @param ap format parameters
73  * @return 0 on success, AVERROR otherwise
74  */
75 static av_cold int rl2_read_header(AVFormatContext *s)
76 {
77     AVIOContext *pb = s->pb;
78     AVStream *st;
79     unsigned int frame_count;
80     unsigned int audio_frame_counter = 0;
81     unsigned int video_frame_counter = 0;
82     unsigned int back_size;
83     unsigned short sound_rate;
84     unsigned short rate;
85     unsigned short channels;
86     unsigned short def_sound_size;
87     unsigned int signature;
88     unsigned int pts_den = 11025; /* video only case */
89     unsigned int pts_num = 1103;
90     unsigned int* chunk_offset = NULL;
91     int* chunk_size = NULL;
92     int* audio_size = NULL;
93     int i;
94     int ret = 0;
95
96     avio_skip(pb,4);          /* skip FORM tag */
97     back_size = avio_rl32(pb); /**< get size of the background frame */
98     signature = avio_rb32(pb);
99     avio_skip(pb, 4);         /* data size */
100     frame_count = avio_rl32(pb);
101
102     /* disallow back_sizes and frame_counts that may lead to overflows later */
103     if(back_size > INT_MAX/2  || frame_count > INT_MAX / sizeof(uint32_t))
104         return AVERROR_INVALIDDATA;
105
106     avio_skip(pb, 2);         /* encoding mentod */
107     sound_rate = avio_rl16(pb);
108     rate = avio_rl16(pb);
109     channels = avio_rl16(pb);
110     def_sound_size = avio_rl16(pb);
111
112     /** setup video stream */
113     st = avformat_new_stream(s, NULL);
114     if(!st)
115          return AVERROR(ENOMEM);
116
117     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
118     st->codec->codec_id = CODEC_ID_RL2;
119     st->codec->codec_tag = 0;  /* no fourcc */
120     st->codec->width = 320;
121     st->codec->height = 200;
122
123     /** allocate and fill extradata */
124     st->codec->extradata_size = EXTRADATA1_SIZE;
125
126     if(signature == RLV3_TAG && back_size > 0)
127         st->codec->extradata_size += back_size;
128
129     st->codec->extradata = av_mallocz(st->codec->extradata_size +
130                                           FF_INPUT_BUFFER_PADDING_SIZE);
131     if(!st->codec->extradata)
132         return AVERROR(ENOMEM);
133
134     if(avio_read(pb,st->codec->extradata,st->codec->extradata_size) !=
135                       st->codec->extradata_size)
136         return AVERROR(EIO);
137
138     /** setup audio stream if present */
139     if(sound_rate){
140         if(channels <= 0)
141             return AVERROR_INVALIDDATA;
142
143         pts_num = def_sound_size;
144         pts_den = rate;
145
146         st = avformat_new_stream(s, NULL);
147         if (!st)
148             return AVERROR(ENOMEM);
149         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
150         st->codec->codec_id = CODEC_ID_PCM_U8;
151         st->codec->codec_tag = 1;
152         st->codec->channels = channels;
153         st->codec->bits_per_coded_sample = 8;
154         st->codec->sample_rate = rate;
155         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
156             st->codec->bits_per_coded_sample;
157         st->codec->block_align = st->codec->channels *
158             st->codec->bits_per_coded_sample / 8;
159         avpriv_set_pts_info(st,32,1,rate);
160     }
161
162     avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
163
164     chunk_size =   av_malloc(frame_count * sizeof(uint32_t));
165     audio_size =   av_malloc(frame_count * sizeof(uint32_t));
166     chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
167
168     if(!chunk_size || !audio_size || !chunk_offset){
169         av_free(chunk_size);
170         av_free(audio_size);
171         av_free(chunk_offset);
172         return AVERROR(ENOMEM);
173     }
174
175     /** read offset and size tables */
176     for(i=0; i < frame_count;i++)
177         chunk_size[i] = avio_rl32(pb);
178     for(i=0; i < frame_count;i++)
179         chunk_offset[i] = avio_rl32(pb);
180     for(i=0; i < frame_count;i++)
181         audio_size[i] = avio_rl32(pb) & 0xFFFF;
182
183     /** build the sample index */
184     for(i=0;i<frame_count;i++){
185         if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
186             ret = AVERROR_INVALIDDATA;
187             break;
188         }
189
190         if(sound_rate && audio_size[i]){
191             av_add_index_entry(s->streams[1], chunk_offset[i],
192                 audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
193             audio_frame_counter += audio_size[i] / channels;
194         }
195         av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
196             video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
197         ++video_frame_counter;
198     }
199
200
201     av_free(chunk_size);
202     av_free(audio_size);
203     av_free(chunk_offset);
204
205     return ret;
206 }
207
208 /**
209  * read a single audio or video packet
210  * @param s demuxer context
211  * @param pkt the packet to be filled
212  * @return 0 on success, AVERROR otherwise
213  */
214 static int rl2_read_packet(AVFormatContext *s,
215                             AVPacket *pkt)
216 {
217     Rl2DemuxContext *rl2 = s->priv_data;
218     AVIOContext *pb = s->pb;
219     AVIndexEntry *sample = NULL;
220     int i;
221     int ret = 0;
222     int stream_id = -1;
223     int64_t pos = INT64_MAX;
224
225     /** check if there is a valid video or audio entry that can be used */
226     for(i=0; i<s->nb_streams; i++){
227         if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
228               && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
229             sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
230             pos= sample->pos;
231             stream_id= i;
232         }
233     }
234
235     if(stream_id == -1)
236         return AVERROR(EIO);
237
238     ++rl2->index_pos[stream_id];
239
240     /** position the stream (will probably be there anyway) */
241     avio_seek(pb, sample->pos, SEEK_SET);
242
243     /** fill the packet */
244     ret = av_get_packet(pb, pkt, sample->size);
245     if(ret != sample->size){
246         av_free_packet(pkt);
247         return AVERROR(EIO);
248     }
249
250     pkt->stream_index = stream_id;
251     pkt->pts = sample->timestamp;
252
253     return ret;
254 }
255
256 /**
257  * seek to a new timestamp
258  * @param s demuxer context
259  * @param stream_index index of the stream that should be seeked
260  * @param timestamp wanted timestamp
261  * @param flags direction and seeking mode
262  * @return 0 on success, -1 otherwise
263  */
264 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
265 {
266     AVStream *st = s->streams[stream_index];
267     Rl2DemuxContext *rl2 = s->priv_data;
268     int i;
269     int index = av_index_search_timestamp(st, timestamp, flags);
270     if(index < 0)
271         return -1;
272
273     rl2->index_pos[stream_index] = index;
274     timestamp = st->index_entries[index].timestamp;
275
276     for(i=0; i < s->nb_streams; i++){
277         AVStream *st2 = s->streams[i];
278         index = av_index_search_timestamp(st2,
279                     av_rescale_q(timestamp, st->time_base, st2->time_base),
280                     flags | AVSEEK_FLAG_BACKWARD);
281
282         if(index < 0)
283             index = 0;
284
285         rl2->index_pos[i] = index;
286     }
287
288     return 0;
289 }
290
291 AVInputFormat ff_rl2_demuxer = {
292     .name           = "rl2",
293     .long_name      = NULL_IF_CONFIG_SMALL("RL2 format"),
294     .priv_data_size = sizeof(Rl2DemuxContext),
295     .read_probe     = rl2_probe,
296     .read_header    = rl2_read_header,
297     .read_packet    = rl2_read_packet,
298     .read_seek      = rl2_read_seek,
299 };
300