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 <stdint.h>
36
41
42 #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
43
44 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
45 #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
46 #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
47
49 unsigned int index_pos[2];
///< indexes in the sample tables
51
52
53 /**
54 * check if the file is in rl2 format
55 * @param p probe buffer
56 * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
57 */
59 {
60
62 return 0;
63
66 return 0;
67
69 }
70
71 /**
72 * read rl2 header data and setup the avstreams
73 * @param s demuxer context
74 * @return 0 on success, AVERROR otherwise
75 */
77 {
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;
87 unsigned short def_sound_size;
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
98 back_size =
avio_rl32(pb);
/**< get size of the background frame */
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))
106
112
113 /** setup video stream */
115 if(!st)
117
123
124 /** allocate and fill extradata */
126
127 if(signature ==
RLV3_TAG && back_size > 0)
129
132
133 /** setup audio stream if present */
134 if(sound_rate){
135 if (!channels || channels > 42) {
138 }
139
140 pts_num = def_sound_size;
141 pts_den = rate;
142
144 if (!st)
157 }
158
160
161 chunk_size =
av_malloc(frame_count *
sizeof(uint32_t));
162 audio_size =
av_malloc(frame_count *
sizeof(uint32_t));
163 chunk_offset =
av_malloc(frame_count *
sizeof(uint32_t));
164
165 if(!chunk_size || !audio_size || !chunk_offset){
170 }
171
172 /** read offset and size tables */
173 for(i=0; i < frame_count;i++) {
177 }
178 for(i=0; i < frame_count;i++) {
182 }
183 for(i=0; i < frame_count;i++) {
187 }
188
189 /** build the sample index */
190 for(i=0;i<frame_count;i++){
191 if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
193 break;
194 }
195
196 if(sound_rate && audio_size[i]){
199 audio_frame_counter += audio_size[i] /
channels;
200 }
203 ++video_frame_counter;
204 }
205
206
210
211 return ret;
212 }
213
214 /**
215 * read a single audio or video packet
216 * @param s demuxer context
217 * @param pkt the packet to be filled
218 * @return 0 on success, AVERROR otherwise
219 */
222 {
226 int i;
227 int ret = 0;
228 int stream_id = -1;
229 int64_t pos = INT64_MAX;
230
231 /** check if there is a valid video or audio entry that can be used */
237 stream_id= i;
238 }
239 }
240
241 if(stream_id == -1)
243
245
246 /** position the stream (will probably be there anyway) */
248
249 /** fill the packet */
251 if(ret != sample->
size){
254 }
255
258
259 return ret;
260 }
261
262 /**
263 * seek to a new timestamp
264 * @param s demuxer context
265 * @param stream_index index of the stream that should be seeked
266 * @param timestamp wanted timestamp
267 * @param flags direction and seeking mode
268 * @return 0 on success, -1 otherwise
269 */
271 {
274 int i;
276 if(index < 0)
277 return -1;
278
281
287
288 if(index < 0)
289 index = 0;
290
292 }
293
294 return 0;
295 }
296
305 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
static const char signature[]
unsigned int avio_rb32(AVIOContext *s)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
#define AVERROR_EOF
End of file.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
unsigned int avio_rl32(AVIOContext *s)
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
enum AVMediaType codec_type
General type of the encoded data.
int extradata_size
Size of the extradata content in bytes.
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int block_align
Audio only.
static int read_header(FFV1Context *f)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
AVIOContext * pb
I/O context.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
This structure contains the data a format has to probe a file.
int sample_rate
Audio only.
unsigned int avio_rl16(AVIOContext *s)
unsigned int index_pos[2]
indexes in the sample tables
void * priv_data
Format private data.
int bits_per_coded_sample
The number of bits per sample in the codedwords.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...