FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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(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 mentod */
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->codec->codec_tag = 0; /* no fourcc */
121  st->codec->width = 320;
122  st->codec->height = 200;
123 
124  /** allocate and fill extradata */
126 
127  if(signature == RLV3_TAG && back_size > 0)
128  st->codec->extradata_size += back_size;
129 
130  if(ff_get_extradata(st->codec, pb, st->codec->extradata_size) < 0)
131  return AVERROR(ENOMEM);
132 
133  /** setup audio stream if present */
134  if(sound_rate){
135  if (!channels || channels > 42) {
136  av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
137  return AVERROR_INVALIDDATA;
138  }
139 
140  pts_num = def_sound_size;
141  pts_den = rate;
142 
143  st = avformat_new_stream(s, NULL);
144  if (!st)
145  return AVERROR(ENOMEM);
148  st->codec->codec_tag = 1;
149  st->codec->channels = channels;
150  st->codec->bits_per_coded_sample = 8;
151  st->codec->sample_rate = rate;
152  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
154  st->codec->block_align = st->codec->channels *
155  st->codec->bits_per_coded_sample / 8;
156  avpriv_set_pts_info(st,32,1,rate);
157  }
158 
159  avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
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){
166  av_free(chunk_size);
167  av_free(audio_size);
168  av_free(chunk_offset);
169  return AVERROR(ENOMEM);
170  }
171 
172  /** read offset and size tables */
173  for(i=0; i < frame_count;i++)
174  chunk_size[i] = avio_rl32(pb);
175  for(i=0; i < frame_count;i++)
176  chunk_offset[i] = avio_rl32(pb);
177  for(i=0; i < frame_count;i++)
178  audio_size[i] = avio_rl32(pb) & 0xFFFF;
179 
180  /** build the sample index */
181  for(i=0;i<frame_count;i++){
182  if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
183  ret = AVERROR_INVALIDDATA;
184  break;
185  }
186 
187  if(sound_rate && audio_size[i]){
188  av_add_index_entry(s->streams[1], chunk_offset[i],
189  audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
190  audio_frame_counter += audio_size[i] / channels;
191  }
192  av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
193  video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
194  ++video_frame_counter;
195  }
196 
197 
198  av_free(chunk_size);
199  av_free(audio_size);
200  av_free(chunk_offset);
201 
202  return ret;
203 }
204 
205 /**
206  * read a single audio or video packet
207  * @param s demuxer context
208  * @param pkt the packet to be filled
209  * @return 0 on success, AVERROR otherwise
210  */
212  AVPacket *pkt)
213 {
214  Rl2DemuxContext *rl2 = s->priv_data;
215  AVIOContext *pb = s->pb;
217  int i;
218  int ret = 0;
219  int stream_id = -1;
220  int64_t pos = INT64_MAX;
221 
222  /** check if there is a valid video or audio entry that can be used */
223  for(i=0; i<s->nb_streams; i++){
224  if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
225  && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
226  sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
227  pos= sample->pos;
228  stream_id= i;
229  }
230  }
231 
232  if(stream_id == -1)
233  return AVERROR_EOF;
234 
235  ++rl2->index_pos[stream_id];
236 
237  /** position the stream (will probably be there anyway) */
238  avio_seek(pb, sample->pos, SEEK_SET);
239 
240  /** fill the packet */
241  ret = av_get_packet(pb, pkt, sample->size);
242  if(ret != sample->size){
243  av_free_packet(pkt);
244  return AVERROR(EIO);
245  }
246 
247  pkt->stream_index = stream_id;
248  pkt->pts = sample->timestamp;
249 
250  return ret;
251 }
252 
253 /**
254  * seek to a new timestamp
255  * @param s demuxer context
256  * @param stream_index index of the stream that should be seeked
257  * @param timestamp wanted timestamp
258  * @param flags direction and seeking mode
259  * @return 0 on success, -1 otherwise
260  */
261 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
262 {
263  AVStream *st = s->streams[stream_index];
264  Rl2DemuxContext *rl2 = s->priv_data;
265  int i;
266  int index = av_index_search_timestamp(st, timestamp, flags);
267  if(index < 0)
268  return -1;
269 
270  rl2->index_pos[stream_index] = index;
271  timestamp = st->index_entries[index].timestamp;
272 
273  for(i=0; i < s->nb_streams; i++){
274  AVStream *st2 = s->streams[i];
275  index = av_index_search_timestamp(st2,
276  av_rescale_q(timestamp, st->time_base, st2->time_base),
277  flags | AVSEEK_FLAG_BACKWARD);
278 
279  if(index < 0)
280  index = 0;
281 
282  rl2->index_pos[i] = index;
283  }
284 
285  return 0;
286 }
287 
289  .name = "rl2",
290  .long_name = NULL_IF_CONFIG_SMALL("RL2"),
291  .priv_data_size = sizeof(Rl2DemuxContext),
296 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2266
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#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:280
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: utils.c:1742
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.
Definition: utils.c:4006
int64_t pos
Definition: avformat.h:784
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional FF_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2885
static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
seek to a new timestamp
Definition: rl2.c:261
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:203
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1046
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:276
static AVPacket pkt
#define sample
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2022
AVInputFormat ff_rl2_demuxer
Definition: rl2.c:288
Format I/O context.
Definition: avformat.h:1272
static const char signature[]
Definition: ipmovie.c:529
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:689
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3672
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:85
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
#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.
Definition: utils.c:241
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2720
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
#define AVINDEX_KEYFRAME
Definition: avformat.h:791
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1784
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:658
#define AVERROR(e)
Definition: error.h:43
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:785
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
static int rl2_probe(AVProbeData *p)
check if the file is in rl2 format
Definition: rl2.c:58
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1328
int bit_rate
the average bitrate
Definition: avcodec.h:1305
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:620
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1249
enum AVCodecID codec_id
Definition: avcodec.h:1258
int sample_rate
samples per second
Definition: avcodec.h:1985
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
#define FORM_TAG
Definition: rl2.c:44
static int rl2_read_packet(AVFormatContext *s, AVPacket *pkt)
read a single audio or video packet
Definition: rl2.c:211
#define RLV2_TAG
Definition: rl2.c:45
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1273
int extradata_size
Definition: avcodec.h:1356
int nb_index_entries
Definition: avformat.h:1048
int index
Definition: gxfenc.c:89
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define RLV3_TAG
Definition: rl2.c:46
#define EXTRADATA1_SIZE
video base, clr, palette
Definition: rl2.c:42
static int flags
Definition: cpu.c:47
static av_cold int rl2_read_header(AVFormatContext *s)
read rl2 header data and setup the avstreams
Definition: rl2.c:76
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:642
Main libavformat public API header.
unsigned int index_pos[2]
indexes in the sample tables
Definition: rl2.c:49
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:1986
void * priv_data
Format private data.
Definition: avformat.h:1300
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int stream_index
Definition: avcodec.h:1164
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:884
This structure stores compressed data.
Definition: avcodec.h:1139
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155