libavformat/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 "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
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  * @return 0 on success, AVERROR otherwise
73  */
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); /**< get size of the background frame */
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 
111  /** setup video stream */
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 
122  /** allocate and fill extradata */
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 
137  /** setup audio stream if present */
138  if(sound_rate){
139  if(channels <= 0)
140  return AVERROR_INVALIDDATA;
141 
142  pts_num = def_sound_size;
143  pts_den = rate;
144 
145  st = avformat_new_stream(s, NULL);
146  if (!st)
147  return AVERROR(ENOMEM);
150  st->codec->codec_tag = 1;
151  st->codec->channels = channels;
152  st->codec->bits_per_coded_sample = 8;
153  st->codec->sample_rate = rate;
154  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
156  st->codec->block_align = st->codec->channels *
157  st->codec->bits_per_coded_sample / 8;
158  avpriv_set_pts_info(st,32,1,rate);
159  }
160 
161  avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
162 
163  chunk_size = av_malloc(frame_count * sizeof(uint32_t));
164  audio_size = av_malloc(frame_count * sizeof(uint32_t));
165  chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
166 
167  if(!chunk_size || !audio_size || !chunk_offset){
168  av_free(chunk_size);
169  av_free(audio_size);
170  av_free(chunk_offset);
171  return AVERROR(ENOMEM);
172  }
173 
174  /** read offset and size tables */
175  for(i=0; i < frame_count;i++)
176  chunk_size[i] = avio_rl32(pb);
177  for(i=0; i < frame_count;i++)
178  chunk_offset[i] = avio_rl32(pb);
179  for(i=0; i < frame_count;i++)
180  audio_size[i] = avio_rl32(pb) & 0xFFFF;
181 
182  /** build the sample index */
183  for(i=0;i<frame_count;i++){
184  if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
185  ret = AVERROR_INVALIDDATA;
186  break;
187  }
188 
189  if(sound_rate && audio_size[i]){
190  av_add_index_entry(s->streams[1], chunk_offset[i],
191  audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
192  audio_frame_counter += audio_size[i] / channels;
193  }
194  av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
195  video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
196  ++video_frame_counter;
197  }
198 
199 
200  av_free(chunk_size);
201  av_free(audio_size);
202  av_free(chunk_offset);
203 
204  return ret;
205 }
206 
207 /**
208  * read a single audio or video packet
209  * @param s demuxer context
210  * @param pkt the packet to be filled
211  * @return 0 on success, AVERROR otherwise
212  */
214  AVPacket *pkt)
215 {
216  Rl2DemuxContext *rl2 = s->priv_data;
217  AVIOContext *pb = s->pb;
219  int i;
220  int ret = 0;
221  int stream_id = -1;
222  int64_t pos = INT64_MAX;
223 
224  /** check if there is a valid video or audio entry that can be used */
225  for(i=0; i<s->nb_streams; i++){
226  if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
227  && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
228  sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
229  pos= sample->pos;
230  stream_id= i;
231  }
232  }
233 
234  if(stream_id == -1)
235  return AVERROR_EOF;
236 
237  ++rl2->index_pos[stream_id];
238 
239  /** position the stream (will probably be there anyway) */
240  avio_seek(pb, sample->pos, SEEK_SET);
241 
242  /** fill the packet */
243  ret = av_get_packet(pb, pkt, sample->size);
244  if(ret != sample->size){
245  av_free_packet(pkt);
246  return AVERROR(EIO);
247  }
248 
249  pkt->stream_index = stream_id;
250  pkt->pts = sample->timestamp;
251 
252  return ret;
253 }
254 
255 /**
256  * seek to a new timestamp
257  * @param s demuxer context
258  * @param stream_index index of the stream that should be seeked
259  * @param timestamp wanted timestamp
260  * @param flags direction and seeking mode
261  * @return 0 on success, -1 otherwise
262  */
263 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
264 {
265  AVStream *st = s->streams[stream_index];
266  Rl2DemuxContext *rl2 = s->priv_data;
267  int i;
268  int index = av_index_search_timestamp(st, timestamp, flags);
269  if(index < 0)
270  return -1;
271 
272  rl2->index_pos[stream_index] = index;
273  timestamp = st->index_entries[index].timestamp;
274 
275  for(i=0; i < s->nb_streams; i++){
276  AVStream *st2 = s->streams[i];
277  index = av_index_search_timestamp(st2,
278  av_rescale_q(timestamp, st->time_base, st2->time_base),
279  flags | AVSEEK_FLAG_BACKWARD);
280 
281  if(index < 0)
282  index = 0;
283 
284  rl2->index_pos[i] = index;
285  }
286 
287  return 0;
288 }
289 
291  .name = "rl2",
292  .long_name = NULL_IF_CONFIG_SMALL("RL2"),
293  .priv_data_size = sizeof(Rl2DemuxContext),
298 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1743
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
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.
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
int64_t pos
Definition: avformat.h:592
static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
seek to a new timestamp
static int64_t audio_size
Definition: ffmpeg.c:126
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:822
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define sample
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
AVInputFormat ff_rl2_demuxer
Format I/O context.
Definition: avformat.h:944
static const char signature[]
Definition: ipmovie.c:526
#define av_cold
Definition: attributes.h:78
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
#define AVERROR_EOF
End of file.
Definition: error.h:55
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.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
#define AVINDEX_KEYFRAME
Definition: avformat.h:599
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
struct Rl2DemuxContext Rl2DemuxContext
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)
Definition: aviobuf.c:579
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:593
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int rl2_probe(AVProbeData *p)
check if the file is in rl2 format
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
int bit_rate
the average bitrate
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
int width
picture width / height.
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define FORM_TAG
static int rl2_read_packet(AVFormatContext *s, AVPacket *pkt)
read a single audio or video packet
#define RLV2_TAG
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int nb_index_entries
Definition: avformat.h:824
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
int index
Definition: gxfenc.c:89
synthesis window for stochastic i
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define RLV3_TAG
#define EXTRADATA1_SIZE
video base, clr, palette
static int flags
Definition: cpu.c:23
static av_cold int rl2_read_header(AVFormatContext *s)
read rl2 header data and setup the avstreams
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
static int frame_count
Definition: muxing.c:234
unsigned int index_pos[2]
indexes in the sample tables
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...