FFmpeg
Loading...
Searching...
No Matches
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 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#include <inttypes.h>
23#include <stdlib.h>
24
25#include "libavutil/avstring.h"
26#include "libavutil/dict.h"
27#include "avformat.h"
28#include "demux.h"
29#include "internal.h"
30
31#define RPL_SIGNATURE "ARMovie\x0A"
32#define RPL_SIGNATURE_SIZE 8
33
34/** 256 is arbitrary, but should be big enough for any reasonable file. */
35#define RPL_LINE_LENGTH 256
36
37static int rpl_probe(const AVProbeData *p)
38{
39 if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
40 return 0;
41
42 return AVPROBE_SCORE_MAX;
43}
44
45typedef struct RPLContext {
46 // RPL header data
48
49 // Stream position data
50 uint32_t chunk_number;
51 uint32_t chunk_part;
52 uint32_t frame_in_part;
54
55static int read_line(AVIOContext * pb, char* line, int bufsize)
56{
57 int i;
58 for (i = 0; i < bufsize - 1; i++) {
59 int b = avio_r8(pb);
60 if (b == 0)
61 break;
62 if (b == '\n') {
63 line[i] = '\0';
64 return avio_feof(pb) ? -1 : 0;
65 }
66 line[i] = b;
67 }
68 line[i] = '\0';
69 return -1;
70}
71
72static int32_t read_int(const char* line, const char** endptr, int* error)
73{
74 unsigned long result = 0;
75 for (; *line>='0' && *line<='9'; line++) {
76 if (result > (0x7FFFFFFF - 9) / 10)
77 *error = -1;
78 result = 10 * result + *line - '0';
79 }
80 *endptr = line;
81 return result;
82}
83
85{
87 const char *endptr;
88 *error |= read_line(pb, line, sizeof(line));
89 return read_int(line, &endptr, error);
90}
91
92/** Parsing for fps, which can be a fraction. Unfortunately,
93 * the spec for the header leaves out a lot of details,
94 * so this is mostly guessing.
95 */
96static AVRational read_fps(const char* line, int* error)
97{
98 int64_t num, den = 1;
99 AVRational result;
100 num = read_int(line, &line, error);
101 if (*line == '.')
102 line++;
103 for (; *line>='0' && *line<='9'; line++) {
104 // Truncate any numerator too large to fit into an int64_t
105 if (num > (INT64_MAX - 9) / 10ULL || den > INT64_MAX / 10ULL)
106 break;
107 num = 10 * num + (*line - '0');
108 den *= 10;
109 }
110 if (!num)
111 *error = -1;
112 av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
113 return result;
114}
115
117{
118 AVIOContext *pb = s->pb;
119 RPLContext *rpl = s->priv_data;
120 AVStream *vst = NULL, *ast = NULL;
121 int64_t total_audio_size;
122 int error = 0;
123 const char *endptr;
124 char audio_type[RPL_LINE_LENGTH];
125 char audio_codec[RPL_LINE_LENGTH];
126
127 uint32_t i;
128
129 int32_t video_format, audio_format, chunk_catalog_offset, number_of_chunks;
130 AVRational fps;
131
132 char line[RPL_LINE_LENGTH];
133
134 // The header for RPL/ARMovie files is 21 lines of text
135 // containing the various header fields. The fields are always
136 // in the same order, and other text besides the first
137 // number usually isn't important.
138 // (The spec says that there exists some significance
139 // for the text in a few cases; samples needed.)
140 error |= read_line(pb, line, sizeof(line)); // ARMovie
141 error |= read_line(pb, line, sizeof(line)); // movie name
142 av_dict_set(&s->metadata, "title" , line, 0);
143 error |= read_line(pb, line, sizeof(line)); // date/copyright
144 av_dict_set(&s->metadata, "copyright", line, 0);
145 error |= read_line(pb, line, sizeof(line)); // author and other
146 av_dict_set(&s->metadata, "author" , line, 0);
147
148 // video headers
149 video_format = read_line_and_int(pb, &error);
150 if (video_format) {
151 vst = avformat_new_stream(s, NULL);
152 if (!vst)
153 return AVERROR(ENOMEM);
155 vst->codecpar->codec_tag = video_format;
156 vst->codecpar->width = read_line_and_int(pb, &error); // video width
157 vst->codecpar->height = read_line_and_int(pb, &error); // video height
158 vst->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
159
160 // Figure out the video codec
161 switch (vst->codecpar->codec_tag) {
162#if 0
163 case 122:
164 vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE122;
165 break;
166#endif
167 case 124:
169 // The header is wrong here, at least sometimes
171 break;
172 case 130:
174 break;
175 default:
176 avpriv_report_missing_feature(s, "Video format %s",
179 }
180 } else {
181 for (i = 0; i < 3; i++)
182 error |= read_line(pb, line, sizeof(line));
183 }
184
185 error |= read_line(pb, line, sizeof(line)); // video frames per second
186 fps = read_fps(line, &error);
187 if (vst)
188 avpriv_set_pts_info(vst, 32, fps.den, fps.num);
189
190 // Audio headers
191
192 // ARMovie supports multiple audio tracks; I don't have any
193 // samples, though. This code will ignore additional tracks.
194 error |= read_line(pb, line, sizeof(line));
195 audio_format = read_int(line, &endptr, &error); // audio format ID
196 av_strlcpy(audio_codec, endptr, RPL_LINE_LENGTH);
197 if (audio_format) {
198 int channels;
199 ast = avformat_new_stream(s, NULL);
200 if (!ast)
201 return AVERROR(ENOMEM);
202 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
203 ast->codecpar->codec_tag = audio_format;
204 ast->codecpar->sample_rate = read_line_and_int(pb, &error); // audio bitrate
205 if (ast->codecpar->sample_rate < 0)
206 return AVERROR_INVALIDDATA;
207 channels = read_line_and_int(pb, &error); // number of audio channels
208 if (channels <= 0)
209 return AVERROR_INVALIDDATA;
210 error |= read_line(pb, line, sizeof(line));
211 ast->codecpar->bits_per_coded_sample = read_int(line, &endptr, &error); // audio bits per sample
212 av_strlcpy(audio_type, endptr, RPL_LINE_LENGTH);
213 ast->codecpar->ch_layout.nb_channels = channels;
214 // At least one sample uses 0 for ADPCM, which is really 4 bits
215 // per sample.
216 if (ast->codecpar->bits_per_coded_sample == 0)
217 ast->codecpar->bits_per_coded_sample = 4;
218
219 ast->codecpar->bit_rate = ast->codecpar->sample_rate *
220 (int64_t)ast->codecpar->ch_layout.nb_channels;
221 if (ast->codecpar->bit_rate > INT64_MAX / ast->codecpar->bits_per_coded_sample)
222 return AVERROR_INVALIDDATA;
223 ast->codecpar->bit_rate *= ast->codecpar->bits_per_coded_sample;
224
225 ast->codecpar->codec_id = AV_CODEC_ID_NONE;
226 switch (audio_format) {
227 case 1:
228 if (ast->codecpar->bits_per_coded_sample == 16) {
229 // 16-bit audio is always signed
230 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
231 } else if (ast->codecpar->bits_per_coded_sample == 8) {
232 if (av_stristr(audio_type, "unsigned") != NULL)
233 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
234 else if (av_stristr(audio_type, "linear") != NULL)
235 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
236 else
237 ast->codecpar->codec_id = AV_CODEC_ID_PCM_VIDC;
238 }
239 // There are some other formats listed as legal per the spec;
240 // samples needed.
241 break;
242 case 2:
243 if (av_stristr(audio_codec, "adpcm") != NULL) {
244 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ACORN;
245 }
246 break;
247 case 101:
248 if (ast->codecpar->bits_per_coded_sample == 8) {
249 // The samples with this kind of audio that I have
250 // are all unsigned.
251 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
252 } else if (ast->codecpar->bits_per_coded_sample == 4) {
253 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ESCAPE;
254 }
255 break;
256 }
257 if (ast->codecpar->codec_id == AV_CODEC_ID_NONE)
258 avpriv_request_sample(s, "Audio format %"PRId32" (%s)",
259 audio_format, audio_codec);
260 avpriv_set_pts_info(ast, 32, 1, ast->codecpar->bit_rate);
261 } else {
262 for (i = 0; i < 3; i++)
263 error |= read_line(pb, line, sizeof(line));
264 }
265
266 if (s->nb_streams == 0)
267 return AVERROR_INVALIDDATA;
268
269 rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
270 if (vst && rpl->frames_per_chunk > 1 && vst->codecpar->codec_tag != 124)
272 "Don't know how to split frames for video format %s. "
273 "Video stream will be broken!\n", av_fourcc2str(vst->codecpar->codec_tag));
274
275 number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
276 if (number_of_chunks == INT_MAX)
277 return AVERROR_INVALIDDATA;
278
279 // The number in the header is actually the index of the last chunk.
280 number_of_chunks++;
281
282 error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
283 error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
284 chunk_catalog_offset = // offset of the "chunk catalog"
285 read_line_and_int(pb, &error); // (file index)
286 error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
287 error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
288 if (vst) {
289 error |= read_line(pb, line, sizeof(line)); // offset to key frame list
290 vst->duration = number_of_chunks * (int64_t)rpl->frames_per_chunk;
291 }
292
293 // Read the index
294 avio_seek(pb, chunk_catalog_offset, SEEK_SET);
295 total_audio_size = 0;
296 for (i = 0; !error && i < number_of_chunks; i++) {
297 int64_t offset, video_size, audio_size;
298 error |= read_line(pb, line, sizeof(line));
299 if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
300 &offset, &video_size, &audio_size)) {
301 error = -1;
302 continue;
303 }
304 if (vst)
306 video_size, rpl->frames_per_chunk, 0);
307 if (ast)
308 av_add_index_entry(ast, offset + video_size, total_audio_size,
309 audio_size, audio_size * 8, 0);
310 if (total_audio_size/8 + (uint64_t)audio_size >= INT64_MAX/8)
311 return AVERROR_INVALIDDATA;
312 total_audio_size += audio_size * 8;
313 }
314
315 if (error)
316 return AVERROR_INVALIDDATA;
317
318 return 0;
319}
320
322{
323 RPLContext *rpl = s->priv_data;
324 AVIOContext *pb = s->pb;
325 AVStream* stream;
326 FFStream *sti;
327 AVIndexEntry* index_entry;
328 int ret;
329
330 if (rpl->chunk_part == s->nb_streams) {
331 rpl->chunk_number++;
332 rpl->chunk_part = 0;
333 }
334
335 stream = s->streams[rpl->chunk_part];
336 sti = ffstream(stream);
337
338 if (rpl->chunk_number >= sti->nb_index_entries)
339 return AVERROR_EOF;
340
341 index_entry = &sti->index_entries[rpl->chunk_number];
342
343 if (rpl->frame_in_part == 0) {
344 int64_t ret64 = avio_seek(pb, index_entry->pos, SEEK_SET);
345 if (ret64 < 0)
346 return (int)ret64;
347 }
348
349 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
350 stream->codecpar->codec_tag == 124) {
351 // We have to split Escape 124 frames because there are
352 // multiple frames per chunk in Escape 124 samples.
353 uint32_t frame_size;
354 int64_t ret64;
355
356 avio_skip(pb, 4); /* flags */
357 frame_size = avio_rl32(pb);
358 if (avio_feof(pb) || !frame_size)
359 return AVERROR_INVALIDDATA;
360 if ((ret64 = avio_seek(pb, -8, SEEK_CUR)) < 0)
361 return (int)ret64;
362
363 ret = av_get_packet(pb, pkt, frame_size);
364 if (ret < 0)
365 return ret;
366 if (ret != frame_size)
367 return AVERROR_INVALIDDATA;
368
369 pkt->duration = 1;
370 pkt->pts = index_entry->timestamp + rpl->frame_in_part;
371 pkt->stream_index = rpl->chunk_part;
372
373 rpl->frame_in_part++;
374 if (rpl->frame_in_part == rpl->frames_per_chunk) {
375 rpl->frame_in_part = 0;
376 rpl->chunk_part++;
377 }
378 } else {
379 ret = av_get_packet(pb, pkt, index_entry->size);
380 if (ret < 0)
381 return ret;
382 if (ret != index_entry->size)
383 return AVERROR_INVALIDDATA;
384
385 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
386 // frames_per_chunk should always be one here; the header
387 // parsing will warn if it isn't.
388 pkt->duration = rpl->frames_per_chunk;
389 } else {
390 // All the audio codecs supported in this container
391 // (at least so far) are constant-bitrate.
392 pkt->duration = ret * 8;
393 }
394 pkt->pts = index_entry->timestamp;
395 pkt->stream_index = rpl->chunk_part;
396 rpl->chunk_part++;
397 }
398
399 // None of the Escape formats have keyframes, and the ADPCM
400 // format used doesn't have keyframes.
401 if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
402 pkt->flags |= AV_PKT_FLAG_KEY;
403
404 return ret;
405}
406
408 .p.name = "rpl",
409 .p.long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
410 .priv_data_size = sizeof(RPLContext),
414};
const FFInputFormat ff_rpl_demuxer
Definition rpl.c:407
channels
Definition aptx.h:31
int32_t
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 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
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
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
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#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
static AVPacket * pkt
Public dictionary API.
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static const uint8_t frame_size[4]
Definition g723_1.h:222
@ AV_CODEC_ID_ESCAPE124
Definition codec_id.h:165
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_ESCAPE130
Definition codec_id.h:219
@ AV_CODEC_ID_ADPCM_IMA_ESCAPE
Definition codec_id.h:431
@ AV_CODEC_ID_PCM_S8
Definition codec_id.h:334
@ AV_CODEC_ID_PCM_VIDC
Definition codec_id.h:365
@ AV_CODEC_ID_ADPCM_IMA_ACORN
Definition codec_id.h:420
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
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_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:86
#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_WARNING
Something somehow does not look correct.
Definition log.h:216
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
#define av_fourcc2str(fourcc)
Definition avutil.h:323
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
Definition avstring.c:58
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
#define b
Definition input.c:43
unsigned offset
Definition libaomenc.c:763
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define RPL_LINE_LENGTH
256 is arbitrary, but should be big enough for any reasonable file.
Definition rpl.c:35
static int read_line(AVIOContext *pb, char *line, int bufsize)
Definition rpl.c:55
static int rpl_read_header(AVFormatContext *s)
Definition rpl.c:116
static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition rpl.c:321
static int32_t read_int(const char *line, const char **endptr, int *error)
Definition rpl.c:72
#define RPL_SIGNATURE
Definition rpl.c:31
#define RPL_SIGNATURE_SIZE
Definition rpl.c:32
static int32_t read_line_and_int(AVIOContext *pb, int *error)
Definition rpl.c:84
static int rpl_probe(const AVProbeData *p)
Definition rpl.c:37
static AVRational read_fps(const char *line, int *error)
Parsing for fps, which can be a fraction.
Definition rpl.c:96
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
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
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
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
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
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
uint32_t chunk_number
Definition rpl.c:50
uint32_t frame_in_part
Definition rpl.c:52
int32_t frames_per_chunk
Definition rpl.c:47
uint32_t chunk_part
Definition rpl.c:51
#define avpriv_request_sample(...)
#define av_log(a,...)
static void error(const char *err)