FFmpeg
Loading...
Searching...
No Matches
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 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
39#include "libavutil/mem.h"
40#include "avformat.h"
41#include "demux.h"
42#include "internal.h"
43
44#define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
45
46#define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
47#define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
48#define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
49
50typedef struct Rl2DemuxContext {
51 unsigned int index_pos[2]; ///< indexes in the sample tables
53
54
55/**
56 * check if the file is in rl2 format
57 * @param p probe buffer
58 * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
59 */
60static int rl2_probe(const AVProbeData *p)
61{
62
63 if(AV_RB32(&p->buf[0]) != FORM_TAG)
64 return 0;
65
66 if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
67 AV_RB32(&p->buf[8]) != RLV3_TAG)
68 return 0;
69
70 return AVPROBE_SCORE_MAX;
71}
72
73/**
74 * read rl2 header data and setup the avstreams
75 * @param s demuxer context
76 * @return 0 on success, AVERROR otherwise
77 */
79{
80 AVIOContext *pb = s->pb;
81 AVStream *st;
82 unsigned int frame_count;
83 unsigned int audio_frame_counter = 0;
84 unsigned int video_frame_counter = 0;
85 unsigned int back_size;
86 unsigned short sound_rate;
87 unsigned short rate;
88 unsigned short channels;
89 unsigned short def_sound_size;
90 unsigned int signature;
91 unsigned int pts_den = 11025; /* video only case */
92 unsigned int pts_num = 1103;
93 unsigned int* chunk_offset = NULL;
94 int* chunk_size = NULL;
95 int* audio_size = NULL;
96 int i;
97 int ret = 0;
98
99 avio_skip(pb,4); /* skip FORM tag */
100 back_size = avio_rl32(pb); /**< get size of the background frame */
101 signature = avio_rb32(pb);
102 avio_skip(pb, 4); /* data size */
103 frame_count = avio_rl32(pb);
104
105 /* disallow back_sizes and frame_counts that may lead to overflows later */
106 if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
107 return AVERROR_INVALIDDATA;
108
109 avio_skip(pb, 2); /* encoding method */
110 sound_rate = avio_rl16(pb);
111 rate = avio_rl16(pb);
112 channels = avio_rl16(pb);
113 def_sound_size = avio_rl16(pb);
114
115 /** setup video stream */
117 if(!st)
118 return AVERROR(ENOMEM);
119
122 st->codecpar->codec_tag = 0; /* no fourcc */
123 st->codecpar->width = 320;
124 st->codecpar->height = 200;
125
126 /** allocate and fill extradata */
128
129 if(signature == RLV3_TAG && back_size > 0)
130 st->codecpar->extradata_size += back_size;
131
132 ret = ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size);
133 if (ret < 0)
134 return ret;
135
136 /** setup audio stream if present */
137 if(sound_rate){
138 if (!channels || channels > 42) {
139 av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
140 return AVERROR_INVALIDDATA;
141 }
142
143 pts_num = def_sound_size;
144 pts_den = rate;
145
147 if (!st)
148 return AVERROR(ENOMEM);
151 st->codecpar->codec_tag = 1;
154 st->codecpar->sample_rate = rate;
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 if (avio_feof(pb)) {
179 goto end;
180 }
181 chunk_size[i] = avio_rl32(pb);
182 }
183 for(i=0; i < frame_count;i++) {
184 if (avio_feof(pb)) {
186 goto end;
187 }
188 chunk_offset[i] = avio_rl32(pb);
189 }
190 for(i=0; i < frame_count;i++) {
191 if (avio_feof(pb)) {
193 goto end;
194 }
195 audio_size[i] = avio_rl32(pb) & 0xFFFF;
196 }
197
198 /** build the sample index */
199 for(i=0;i<frame_count;i++){
200 if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
202 break;
203 }
204
205 if(sound_rate && audio_size[i]){
206 av_add_index_entry(s->streams[1], chunk_offset[i],
207 audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
208 audio_frame_counter += audio_size[i] / channels;
209 }
210 av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
211 video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
212 ++video_frame_counter;
213 }
214
215end:
216 av_free(chunk_size);
217 av_free(audio_size);
218 av_free(chunk_offset);
219
220 return ret;
221}
222
223/**
224 * read a single audio or video packet
225 * @param s demuxer context
226 * @param pkt the packet to be filled
227 * @return 0 on success, AVERROR otherwise
228 */
230 AVPacket *pkt)
231{
232 Rl2DemuxContext *rl2 = s->priv_data;
233 AVIOContext *pb = s->pb;
235 int i;
236 int ret = 0;
237 int stream_id = -1;
238 int64_t pos = INT64_MAX;
239
240 /** check if there is a valid video or audio entry that can be used */
241 for(i=0; i<s->nb_streams; i++){
242 const FFStream *const sti = ffstream(s->streams[i]);
243 if (rl2->index_pos[i] < sti->nb_index_entries
244 && sti->index_entries[ rl2->index_pos[i] ].pos < pos) {
245 sample = &sti->index_entries[ rl2->index_pos[i] ];
246 pos= sample->pos;
247 stream_id= i;
248 }
249 }
250
251 if(stream_id == -1)
252 return AVERROR_EOF;
253
254 ++rl2->index_pos[stream_id];
255
256 /** position the stream (will probably be there anyway) */
257 avio_seek(pb, sample->pos, SEEK_SET);
258
259 /** fill the packet */
260 ret = av_get_packet(pb, pkt, sample->size);
261 if(ret != sample->size){
262 return AVERROR_INVALIDDATA;
263 }
264
265 pkt->stream_index = stream_id;
266 pkt->pts = sample->timestamp;
267
268 return ret;
269}
270
271/**
272 * seek to a new timestamp
273 * @param s demuxer context
274 * @param stream_index index of the stream that should be seeked
275 * @param timestamp wanted timestamp
276 * @param flags direction and seeking mode
277 * @return 0 on success, -1 otherwise
278 */
279static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
280{
281 AVStream *st = s->streams[stream_index];
282 Rl2DemuxContext *rl2 = s->priv_data;
283 int i;
284 int index = av_index_search_timestamp(st, timestamp, flags);
285 if(index < 0)
286 return -1;
287
288 rl2->index_pos[stream_index] = index;
289 timestamp = ffstream(st)->index_entries[index].timestamp;
290
291 for(i=0; i < s->nb_streams; i++){
292 AVStream *st2 = s->streams[i];
294 av_rescale_q(timestamp, st->time_base, st2->time_base),
296
297 if(index < 0)
298 index = 0;
299
300 rl2->index_pos[i] = index;
301 }
302
303 return 0;
304}
305
307 .p.name = "rl2",
308 .p.long_name = NULL_IF_CONFIG_SMALL("RL2"),
309 .priv_data_size = sizeof(Rl2DemuxContext),
314};
const FFInputFormat ff_rl2_demuxer
Definition rl2.c:306
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2616
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define sample
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_RL2
Definition codec_id.h:164
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
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.
Definition seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int index
Definition gxfenc.c:90
#define AV_RB32(p)
static const char signature[]
Definition ipmovie.c:592
#define EXTRADATA1_SIZE
video base, clr count, palette
Definition rl2.c:39
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define RLV2_TAG
Definition rl2.c:47
static int rl2_probe(const AVProbeData *p)
check if the file is in rl2 format
Definition rl2.c:60
static int rl2_read_packet(AVFormatContext *s, AVPacket *pkt)
read a single audio or video packet
Definition rl2.c:229
static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
seek to a new timestamp
Definition rl2.c:279
#define RLV3_TAG
Definition rl2.c:48
static av_cold int rl2_read_header(AVFormatContext *s)
read rl2 header data and setup the avstreams
Definition rl2.c:78
#define FORM_TAG
Definition rl2.c:46
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int64_t pos
Definition avformat.h:621
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:622
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
unsigned int index_pos[2]
indexes in the sample tables
Definition rl2.c:51
#define av_free(p)
#define av_log(a,...)