Libav
rpl.c
Go to the documentation of this file.
1 /*
2  * ARMovie/RPL demuxer
3  * Copyright (c) 2007 Christian Ohm, 2008 Eli Friedman
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <inttypes.h>
23 #include <stdlib.h>
24 
25 #include "libavutil/avstring.h"
26 #include "libavutil/dict.h"
27 #include "avformat.h"
28 #include "internal.h"
29 
30 #define RPL_SIGNATURE "ARMovie\x0A"
31 #define RPL_SIGNATURE_SIZE 8
32 
34 #define RPL_LINE_LENGTH 256
35 
36 static int rpl_probe(AVProbeData *p)
37 {
38  if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
39  return 0;
40 
41  return AVPROBE_SCORE_MAX;
42 }
43 
44 typedef struct RPLContext {
45  // RPL header data
47 
48  // Stream position data
49  uint32_t chunk_number;
50  uint32_t chunk_part;
51  uint32_t frame_in_part;
52 } RPLContext;
53 
54 static int read_line(AVIOContext * pb, char* line, int bufsize)
55 {
56  int i;
57  for (i = 0; i < bufsize - 1; i++) {
58  int b = avio_r8(pb);
59  if (b == 0)
60  break;
61  if (b == '\n') {
62  line[i] = '\0';
63  return 0;
64  }
65  line[i] = b;
66  }
67  line[i] = '\0';
68  return -1;
69 }
70 
71 static int32_t read_int(const char* line, const char** endptr, int* error)
72 {
73  unsigned long result = 0;
74  for (; *line>='0' && *line<='9'; line++) {
75  if (result > (0x7FFFFFFF - 9) / 10)
76  *error = -1;
77  result = 10 * result + *line - '0';
78  }
79  *endptr = line;
80  return result;
81 }
82 
83 static int32_t read_line_and_int(AVIOContext * pb, int* error)
84 {
85  char line[RPL_LINE_LENGTH];
86  const char *endptr;
87  *error |= read_line(pb, line, sizeof(line));
88  return read_int(line, &endptr, error);
89 }
90 
95 static AVRational read_fps(const char* line, int* error)
96 {
97  int64_t num, den = 1;
98  AVRational result;
99  num = read_int(line, &line, error);
100  if (*line == '.')
101  line++;
102  for (; *line>='0' && *line<='9'; line++) {
103  // Truncate any numerator too large to fit into an int64_t
104  if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
105  break;
106  num = 10 * num + *line - '0';
107  den *= 10;
108  }
109  if (!num)
110  *error = -1;
111  av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
112  return result;
113 }
114 
116 {
117  AVIOContext *pb = s->pb;
118  RPLContext *rpl = s->priv_data;
119  AVStream *vst = NULL, *ast = NULL;
120  int total_audio_size;
121  int error = 0;
122 
123  uint32_t i;
124 
125  int32_t audio_format, chunk_catalog_offset, number_of_chunks;
126  AVRational fps;
127 
128  char line[RPL_LINE_LENGTH];
129 
130  // The header for RPL/ARMovie files is 21 lines of text
131  // containing the various header fields. The fields are always
132  // in the same order, and other text besides the first
133  // number usually isn't important.
134  // (The spec says that there exists some significance
135  // for the text in a few cases; samples needed.)
136  error |= read_line(pb, line, sizeof(line)); // ARMovie
137  error |= read_line(pb, line, sizeof(line)); // movie name
138  av_dict_set(&s->metadata, "title" , line, 0);
139  error |= read_line(pb, line, sizeof(line)); // date/copyright
140  av_dict_set(&s->metadata, "copyright", line, 0);
141  error |= read_line(pb, line, sizeof(line)); // author and other
142  av_dict_set(&s->metadata, "author" , line, 0);
143 
144  // video headers
145  vst = avformat_new_stream(s, NULL);
146  if (!vst)
147  return AVERROR(ENOMEM);
149  vst->codec->codec_tag = read_line_and_int(pb, &error); // video format
150  vst->codec->width = read_line_and_int(pb, &error); // video width
151  vst->codec->height = read_line_and_int(pb, &error); // video height
152  vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
153  error |= read_line(pb, line, sizeof(line)); // video frames per second
154  fps = read_fps(line, &error);
155  avpriv_set_pts_info(vst, 32, fps.den, fps.num);
156 
157  // Figure out the video codec
158  switch (vst->codec->codec_tag) {
159 #if 0
160  case 122:
161  vst->codec->codec_id = AV_CODEC_ID_ESCAPE122;
162  break;
163 #endif
164  case 124:
166  // The header is wrong here, at least sometimes
167  vst->codec->bits_per_coded_sample = 16;
168  break;
169  case 130:
171  break;
172  default:
174  "RPL video format %i not supported yet!\n",
175  vst->codec->codec_tag);
177  }
178 
179  // Audio headers
180 
181  // ARMovie supports multiple audio tracks; I don't have any
182  // samples, though. This code will ignore additional tracks.
183  audio_format = read_line_and_int(pb, &error); // audio format ID
184  if (audio_format) {
185  ast = avformat_new_stream(s, NULL);
186  if (!ast)
187  return AVERROR(ENOMEM);
188  ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
189  ast->codec->codec_tag = audio_format;
190  ast->codec->sample_rate = read_line_and_int(pb, &error); // audio bitrate
191  ast->codec->channels = read_line_and_int(pb, &error); // number of audio channels
192  ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // audio bits per sample
193  // At least one sample uses 0 for ADPCM, which is really 4 bits
194  // per sample.
195  if (ast->codec->bits_per_coded_sample == 0)
196  ast->codec->bits_per_coded_sample = 4;
197 
198  ast->codec->bit_rate = ast->codec->sample_rate *
199  ast->codec->bits_per_coded_sample *
200  ast->codec->channels;
201 
202  ast->codec->codec_id = AV_CODEC_ID_NONE;
203  switch (audio_format) {
204  case 1:
205  if (ast->codec->bits_per_coded_sample == 16) {
206  // 16-bit audio is always signed
207  ast->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
208  break;
209  }
210  // There are some other formats listed as legal per the spec;
211  // samples needed.
212  break;
213  case 101:
214  if (ast->codec->bits_per_coded_sample == 8) {
215  // The samples with this kind of audio that I have
216  // are all unsigned.
217  ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
218  break;
219  } else if (ast->codec->bits_per_coded_sample == 4) {
220  ast->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
221  break;
222  }
223  break;
224  }
225  if (ast->codec->codec_id == AV_CODEC_ID_NONE) {
227  "RPL audio format %"PRId32" not supported yet!\n",
228  audio_format);
229  }
230  avpriv_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
231  } else {
232  for (i = 0; i < 3; i++)
233  error |= read_line(pb, line, sizeof(line));
234  }
235 
236  rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
237  if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
239  "Don't know how to split frames for video format %i. "
240  "Video stream will be broken!\n", vst->codec->codec_tag);
241 
242  number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
243  // The number in the header is actually the index of the last chunk.
244  number_of_chunks++;
245 
246  error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
247  error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
248  chunk_catalog_offset = // offset of the "chunk catalog"
249  read_line_and_int(pb, &error); // (file index)
250  error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
251  error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
252  error |= read_line(pb, line, sizeof(line)); // offset to key frame list
253 
254  // Read the index
255  avio_seek(pb, chunk_catalog_offset, SEEK_SET);
256  total_audio_size = 0;
257  for (i = 0; i < number_of_chunks; i++) {
258  int64_t offset, video_size, audio_size;
259  error |= read_line(pb, line, sizeof(line));
260  if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
261  &offset, &video_size, &audio_size))
262  error = -1;
263  av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
264  video_size, rpl->frames_per_chunk, 0);
265  if (ast)
266  av_add_index_entry(ast, offset + video_size, total_audio_size,
267  audio_size, audio_size * 8, 0);
268  total_audio_size += audio_size * 8;
269  }
270 
271  if (error) return AVERROR(EIO);
272 
273  return 0;
274 }
275 
277 {
278  RPLContext *rpl = s->priv_data;
279  AVIOContext *pb = s->pb;
280  AVStream* stream;
281  AVIndexEntry* index_entry;
282  uint32_t ret;
283 
284  if (rpl->chunk_part == s->nb_streams) {
285  rpl->chunk_number++;
286  rpl->chunk_part = 0;
287  }
288 
289  stream = s->streams[rpl->chunk_part];
290 
291  if (rpl->chunk_number >= stream->nb_index_entries)
292  return -1;
293 
294  index_entry = &stream->index_entries[rpl->chunk_number];
295 
296  if (rpl->frame_in_part == 0)
297  if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
298  return AVERROR(EIO);
299 
300  if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
301  stream->codec->codec_tag == 124) {
302  // We have to split Escape 124 frames because there are
303  // multiple frames per chunk in Escape 124 samples.
304  uint32_t frame_size;
305 
306  avio_skip(pb, 4); /* flags */
307  frame_size = avio_rl32(pb);
308  if (avio_seek(pb, -8, SEEK_CUR) < 0)
309  return AVERROR(EIO);
310 
311  ret = av_get_packet(pb, pkt, frame_size);
312  if (ret != frame_size) {
313  av_free_packet(pkt);
314  return AVERROR(EIO);
315  }
316  pkt->duration = 1;
317  pkt->pts = index_entry->timestamp + rpl->frame_in_part;
318  pkt->stream_index = rpl->chunk_part;
319 
320  rpl->frame_in_part++;
321  if (rpl->frame_in_part == rpl->frames_per_chunk) {
322  rpl->frame_in_part = 0;
323  rpl->chunk_part++;
324  }
325  } else {
326  ret = av_get_packet(pb, pkt, index_entry->size);
327  if (ret != index_entry->size) {
328  av_free_packet(pkt);
329  return AVERROR(EIO);
330  }
331 
332  if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
333  // frames_per_chunk should always be one here; the header
334  // parsing will warn if it isn't.
335  pkt->duration = rpl->frames_per_chunk;
336  } else {
337  // All the audio codecs supported in this container
338  // (at least so far) are constant-bitrate.
339  pkt->duration = ret * 8;
340  }
341  pkt->pts = index_entry->timestamp;
342  pkt->stream_index = rpl->chunk_part;
343  rpl->chunk_part++;
344  }
345 
346  // None of the Escape formats have keyframes, and the ADPCM
347  // format used doesn't have keyframes.
348  if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
349  pkt->flags |= AV_PKT_FLAG_KEY;
350 
351  return ret;
352 }
353 
355  .name = "rpl",
356  .long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
357  .priv_data_size = sizeof(RPLContext),
361 };
static AVRational read_fps(const char *line, int *error)
Parsing for fps, which can be a fraction.
Definition: rpl.c:95
Bytestream IO Context.
Definition: avio.h:68
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
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:1181
Definition: rpl.c:44
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:2821
int64_t pos
Definition: avformat.h:660
static int read_line(AVIOContext *pb, char *line, int bufsize)
Definition: rpl.c:54
uint32_t frame_in_part
Definition: rpl.c:51
uint32_t chunk_part
Definition: rpl.c:50
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:878
int32_t frames_per_chunk
Definition: rpl.c:46
Format I/O context.
Definition: avformat.h:922
Public dictionary API.
uint32_t chunk_number
Definition: rpl.c:49
#define b
Definition: input.c:52
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
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:117
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
static int rpl_read_header(AVFormatContext *s)
Definition: rpl.c:115
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
int64_t timestamp
Definition: avformat.h:661
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
Definition: graph2dot.c:49
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
static int rpl_probe(AVProbeData *p)
Definition: rpl.c:36
static int32_t read_int(const char *line, const char **endptr, int *error)
Definition: rpl.c:71
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1224
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
int32_t
AVInputFormat ff_rpl_demuxer
Definition: rpl.c:354
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
int nb_index_entries
Definition: avformat.h:880
rational number numerator/denominator
Definition: rational.h:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static int32_t read_line_and_int(AVIOContext *pb, int *error)
Definition: rpl.c:83
#define RPL_SIGNATURE
Definition: rpl.c:30
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rpl.c:276
int den
denominator
Definition: rational.h:45
void * priv_data
Format private data.
Definition: avformat.h:950
#define RPL_LINE_LENGTH
256 is arbitrary, but should be big enough for any reasonable file.
Definition: rpl.c:34
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
This structure stores compressed data.
Definition: avcodec.h:950
#define RPL_SIGNATURE_SIZE
Definition: rpl.c:31
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966