FFmpeg
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 
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mathematics.h"
39 #include "avformat.h"
40 #include "internal.h"
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 
48 typedef struct Rl2DemuxContext {
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  */
58 static int rl2_probe(const AVProbeData *p)
59 {
60 
61  if(AV_RB32(&p->buf[0]) != FORM_TAG)
62  return 0;
63 
64  if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
65  AV_RB32(&p->buf[8]) != RLV3_TAG)
66  return 0;
67 
68  return AVPROBE_SCORE_MAX;
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 {
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); /**< get size of the background frame */
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 method */
108  sound_rate = avio_rl16(pb);
109  rate = avio_rl16(pb);
110  channels = avio_rl16(pb);
111  def_sound_size = avio_rl16(pb);
112 
113  /** setup video stream */
114  st = avformat_new_stream(s, NULL);
115  if(!st)
116  return AVERROR(ENOMEM);
117 
120  st->codecpar->codec_tag = 0; /* no fourcc */
121  st->codecpar->width = 320;
122  st->codecpar->height = 200;
123 
124  /** allocate and fill extradata */
126 
127  if(signature == RLV3_TAG && back_size > 0)
128  st->codecpar->extradata_size += back_size;
129 
131  if (ret < 0)
132  return ret;
133 
134  /** setup audio stream if present */
135  if(sound_rate){
136  if (!channels || channels > 42) {
137  av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
138  return AVERROR_INVALIDDATA;
139  }
140 
141  pts_num = def_sound_size;
142  pts_den = rate;
143 
144  st = avformat_new_stream(s, NULL);
145  if (!st)
146  return AVERROR(ENOMEM);
149  st->codecpar->codec_tag = 1;
150  st->codecpar->channels = channels;
152  st->codecpar->sample_rate = rate;
155  st->codecpar->block_align = st->codecpar->channels *
157  avpriv_set_pts_info(st,32,1,rate);
158  }
159 
160  avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
161 
162  chunk_size = av_malloc(frame_count * sizeof(uint32_t));
163  audio_size = av_malloc(frame_count * sizeof(uint32_t));
164  chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
165 
166  if(!chunk_size || !audio_size || !chunk_offset){
167  av_free(chunk_size);
168  av_free(audio_size);
169  av_free(chunk_offset);
170  return AVERROR(ENOMEM);
171  }
172 
173  /** read offset and size tables */
174  for(i=0; i < frame_count;i++) {
175  if (avio_feof(pb)) {
177  goto end;
178  }
179  chunk_size[i] = avio_rl32(pb);
180  }
181  for(i=0; i < frame_count;i++) {
182  if (avio_feof(pb)) {
184  goto end;
185  }
186  chunk_offset[i] = avio_rl32(pb);
187  }
188  for(i=0; i < frame_count;i++) {
189  if (avio_feof(pb)) {
191  goto end;
192  }
193  audio_size[i] = avio_rl32(pb) & 0xFFFF;
194  }
195 
196  /** build the sample index */
197  for(i=0;i<frame_count;i++){
198  if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
200  break;
201  }
202 
203  if(sound_rate && audio_size[i]){
204  av_add_index_entry(s->streams[1], chunk_offset[i],
205  audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
206  audio_frame_counter += audio_size[i] / channels;
207  }
208  av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
209  video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
210  ++video_frame_counter;
211  }
212 
213 end:
214  av_free(chunk_size);
215  av_free(audio_size);
216  av_free(chunk_offset);
217 
218  return ret;
219 }
220 
221 /**
222  * read a single audio or video packet
223  * @param s demuxer context
224  * @param pkt the packet to be filled
225  * @return 0 on success, AVERROR otherwise
226  */
228  AVPacket *pkt)
229 {
230  Rl2DemuxContext *rl2 = s->priv_data;
231  AVIOContext *pb = s->pb;
233  int i;
234  int ret = 0;
235  int stream_id = -1;
236  int64_t pos = INT64_MAX;
237 
238  /** check if there is a valid video or audio entry that can be used */
239  for(i=0; i<s->nb_streams; i++){
240  const FFStream *const sti = ffstream(s->streams[i]);
241  if (rl2->index_pos[i] < sti->nb_index_entries
242  && sti->index_entries[ rl2->index_pos[i] ].pos < pos) {
243  sample = &sti->index_entries[ rl2->index_pos[i] ];
244  pos= sample->pos;
245  stream_id= i;
246  }
247  }
248 
249  if(stream_id == -1)
250  return AVERROR_EOF;
251 
252  ++rl2->index_pos[stream_id];
253 
254  /** position the stream (will probably be there anyway) */
255  avio_seek(pb, sample->pos, SEEK_SET);
256 
257  /** fill the packet */
258  ret = av_get_packet(pb, pkt, sample->size);
259  if(ret != sample->size){
260  return AVERROR(EIO);
261  }
262 
263  pkt->stream_index = stream_id;
264  pkt->pts = sample->timestamp;
265 
266  return ret;
267 }
268 
269 /**
270  * seek to a new timestamp
271  * @param s demuxer context
272  * @param stream_index index of the stream that should be seeked
273  * @param timestamp wanted timestamp
274  * @param flags direction and seeking mode
275  * @return 0 on success, -1 otherwise
276  */
277 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
278 {
279  AVStream *st = s->streams[stream_index];
280  Rl2DemuxContext *rl2 = s->priv_data;
281  int i;
282  int index = av_index_search_timestamp(st, timestamp, flags);
283  if(index < 0)
284  return -1;
285 
286  rl2->index_pos[stream_index] = index;
287  timestamp = ffstream(st)->index_entries[index].timestamp;
288 
289  for(i=0; i < s->nb_streams; i++){
290  AVStream *st2 = s->streams[i];
292  av_rescale_q(timestamp, st->time_base, st2->time_base),
294 
295  if(index < 0)
296  index = 0;
297 
298  rl2->index_pos[i] = index;
299  }
300 
301  return 0;
302 }
303 
305  .name = "rl2",
306  .long_name = NULL_IF_CONFIG_SMALL("RL2"),
307  .priv_data_size = sizeof(Rl2DemuxContext),
312 };
ff_rl2_demuxer
const AVInputFormat ff_rl2_demuxer
Definition: rl2.c:304
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
FORM_TAG
#define FORM_TAG
Definition: rl2.c:44
ff_get_extradata
int ff_get_extradata(AVFormatContext *s, 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...
Definition: utils.c:469
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
rl2_read_header
static av_cold int rl2_read_header(AVFormatContext *s)
read rl2 header data and setup the avstreams
Definition: rl2.c:76
index
fg index
Definition: ffmpeg_filter.c:167
rl2_read_seek
static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
seek to a new timestamp
Definition: rl2.c:277
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
mathematics.h
AVIndexEntry
Definition: avformat.h:801
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:809
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:432
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:149
av_add_index_entry
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:117
rl2_probe
static int rl2_probe(const AVProbeData *p)
check if the file is in rl2 format
Definition: rl2.c:58
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
signature
static const char signature[]
Definition: ipmovie.c:592
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
av_cold
#define av_cold
Definition: attributes.h:90
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_CODEC_ID_RL2
@ AV_CODEC_ID_RL2
Definition: codec_id.h:164
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:803
channels
channels
Definition: aptx.h:33
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
EXTRADATA1_SIZE
#define EXTRADATA1_SIZE
video base, clr, palette
Definition: rl2.c:42
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2275
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:965
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:279
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
FFStream
Definition: internal.h:194
Rl2DemuxContext
Definition: rl2.c:48
Rl2DemuxContext::index_pos
unsigned int index_pos[2]
indexes in the sample tables
Definition: rl2.c:49
sample
#define sample
Definition: flacdsp_template.c:44
AV_RB32
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
Definition: bytestream.h:96
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
AVCodecParameters::height
int height
Definition: codec_par.h:127
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:177
RLV3_TAG
#define RLV3_TAG
Definition: rl2.c:46
av_get_packet
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:197
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:935
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:802
avpriv_set_pts_info
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: utils.c:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:277
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:319
RLV2_TAG
#define RLV2_TAG
Definition: rl2.c:45
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
rl2_read_packet
static int rl2_read_packet(AVFormatContext *s, AVPacket *pkt)
read a single audio or video packet
Definition: rl2.c:227
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: seek.c:237
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:375