3 * Copyright (c) 2007 Christian Ohm, 2008 Eli Friedman
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/avstring.h"
26 #include "libavutil/dict.h"
30 #define RPL_SIGNATURE "ARMovie\x0A"
31 #define RPL_SIGNATURE_SIZE 8
33 /** 256 is arbitrary, but should be big enough for any reasonable file. */
34 #define RPL_LINE_LENGTH 256
36 static int rpl_probe(const AVProbeData *p)
38 if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
41 return AVPROBE_SCORE_MAX;
44 typedef struct RPLContext {
46 int32_t frames_per_chunk;
48 // Stream position data
49 uint32_t chunk_number;
51 uint32_t frame_in_part;
54 static int read_line(AVIOContext * pb, char* line, int bufsize)
57 for (i = 0; i < bufsize - 1; i++) {
63 return avio_feof(pb) ? -1 : 0;
71 static int32_t read_int(const char* line, const char** endptr, int* error)
73 unsigned long result = 0;
74 for (; *line>='0' && *line<='9'; line++) {
75 if (result > (0x7FFFFFFF - 9) / 10)
77 result = 10 * result + *line - '0';
83 static int32_t read_line_and_int(AVIOContext * pb, int* error)
85 char line[RPL_LINE_LENGTH];
87 *error |= read_line(pb, line, sizeof(line));
88 return read_int(line, &endptr, error);
91 /** Parsing for fps, which can be a fraction. Unfortunately,
92 * the spec for the header leaves out a lot of details,
93 * so this is mostly guessing.
95 static AVRational read_fps(const char* line, int* error)
99 num = read_int(line, &line, error);
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)
106 num = 10 * num + *line - '0';
111 av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
115 static int rpl_read_header(AVFormatContext *s)
117 AVIOContext *pb = s->pb;
118 RPLContext *rpl = s->priv_data;
119 AVStream *vst = NULL, *ast = NULL;
120 int total_audio_size;
123 char audio_type[RPL_LINE_LENGTH];
127 int32_t audio_format, chunk_catalog_offset, number_of_chunks;
130 char line[RPL_LINE_LENGTH];
132 // The header for RPL/ARMovie files is 21 lines of text
133 // containing the various header fields. The fields are always
134 // in the same order, and other text besides the first
135 // number usually isn't important.
136 // (The spec says that there exists some significance
137 // for the text in a few cases; samples needed.)
138 error |= read_line(pb, line, sizeof(line)); // ARMovie
139 error |= read_line(pb, line, sizeof(line)); // movie name
140 av_dict_set(&s->metadata, "title" , line, 0);
141 error |= read_line(pb, line, sizeof(line)); // date/copyright
142 av_dict_set(&s->metadata, "copyright", line, 0);
143 error |= read_line(pb, line, sizeof(line)); // author and other
144 av_dict_set(&s->metadata, "author" , line, 0);
147 vst = avformat_new_stream(s, NULL);
149 return AVERROR(ENOMEM);
150 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
151 vst->codecpar->codec_tag = read_line_and_int(pb, &error); // video format
152 vst->codecpar->width = read_line_and_int(pb, &error); // video width
153 vst->codecpar->height = read_line_and_int(pb, &error); // video height
154 vst->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
155 error |= read_line(pb, line, sizeof(line)); // video frames per second
156 fps = read_fps(line, &error);
157 avpriv_set_pts_info(vst, 32, fps.den, fps.num);
159 // Figure out the video codec
160 switch (vst->codecpar->codec_tag) {
163 vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE122;
167 vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE124;
168 // The header is wrong here, at least sometimes
169 vst->codecpar->bits_per_coded_sample = 16;
172 vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE130;
175 avpriv_report_missing_feature(s, "Video format %s",
176 av_fourcc2str(vst->codecpar->codec_tag));
177 vst->codecpar->codec_id = AV_CODEC_ID_NONE;
182 // ARMovie supports multiple audio tracks; I don't have any
183 // samples, though. This code will ignore additional tracks.
184 audio_format = read_line_and_int(pb, &error); // audio format ID
186 ast = avformat_new_stream(s, NULL);
188 return AVERROR(ENOMEM);
189 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
190 ast->codecpar->codec_tag = audio_format;
191 ast->codecpar->sample_rate = read_line_and_int(pb, &error); // audio bitrate
192 ast->codecpar->channels = read_line_and_int(pb, &error); // number of audio channels
193 error |= read_line(pb, line, sizeof(line));
194 ast->codecpar->bits_per_coded_sample = read_int(line, &endptr, &error); // audio bits per sample
195 strcpy(audio_type, endptr);
196 // At least one sample uses 0 for ADPCM, which is really 4 bits
198 if (ast->codecpar->bits_per_coded_sample == 0)
199 ast->codecpar->bits_per_coded_sample = 4;
201 ast->codecpar->bit_rate = ast->codecpar->sample_rate *
202 ast->codecpar->bits_per_coded_sample *
203 ast->codecpar->channels;
205 ast->codecpar->codec_id = AV_CODEC_ID_NONE;
206 switch (audio_format) {
208 if (ast->codecpar->bits_per_coded_sample == 16) {
209 // 16-bit audio is always signed
210 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
212 } else if (ast->codecpar->bits_per_coded_sample == 8) {
213 if(strstr(audio_type, "unsigned") != NULL) {
214 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
216 } else if(strstr(audio_type, "linear") != NULL) {
217 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
220 ast->codecpar->codec_id = AV_CODEC_ID_PCM_VIDC;
224 // There are some other formats listed as legal per the spec;
228 if (ast->codecpar->bits_per_coded_sample == 8) {
229 // The samples with this kind of audio that I have
231 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
233 } else if (ast->codecpar->bits_per_coded_sample == 4) {
234 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
239 if (ast->codecpar->codec_id == AV_CODEC_ID_NONE)
240 avpriv_request_sample(s, "Audio format %"PRId32,
242 avpriv_set_pts_info(ast, 32, 1, ast->codecpar->bit_rate);
244 for (i = 0; i < 3; i++)
245 error |= read_line(pb, line, sizeof(line));
248 rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
249 if (rpl->frames_per_chunk > 1 && vst->codecpar->codec_tag != 124)
250 av_log(s, AV_LOG_WARNING,
251 "Don't know how to split frames for video format %s. "
252 "Video stream will be broken!\n", av_fourcc2str(vst->codecpar->codec_tag));
254 number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
255 // The number in the header is actually the index of the last chunk.
258 error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
259 error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
260 chunk_catalog_offset = // offset of the "chunk catalog"
261 read_line_and_int(pb, &error); // (file index)
262 error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
263 error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
264 error |= read_line(pb, line, sizeof(line)); // offset to key frame list
267 avio_seek(pb, chunk_catalog_offset, SEEK_SET);
268 total_audio_size = 0;
269 for (i = 0; !error && i < number_of_chunks; i++) {
270 int64_t offset, video_size, audio_size;
271 error |= read_line(pb, line, sizeof(line));
272 if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
273 &offset, &video_size, &audio_size)) {
277 av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
278 video_size, rpl->frames_per_chunk, 0);
280 av_add_index_entry(ast, offset + video_size, total_audio_size,
281 audio_size, audio_size * 8, 0);
282 total_audio_size += audio_size * 8;
285 if (error) return AVERROR(EIO);
290 static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
292 RPLContext *rpl = s->priv_data;
293 AVIOContext *pb = s->pb;
295 AVIndexEntry* index_entry;
298 if (rpl->chunk_part == s->nb_streams) {
303 stream = s->streams[rpl->chunk_part];
305 if (rpl->chunk_number >= stream->nb_index_entries)
308 index_entry = &stream->index_entries[rpl->chunk_number];
310 if (rpl->frame_in_part == 0)
311 if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
314 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
315 stream->codecpar->codec_tag == 124) {
316 // We have to split Escape 124 frames because there are
317 // multiple frames per chunk in Escape 124 samples.
320 avio_skip(pb, 4); /* flags */
321 frame_size = avio_rl32(pb);
322 if (avio_seek(pb, -8, SEEK_CUR) < 0)
325 ret = av_get_packet(pb, pkt, frame_size);
328 if (ret != frame_size) {
329 av_packet_unref(pkt);
333 pkt->pts = index_entry->timestamp + rpl->frame_in_part;
334 pkt->stream_index = rpl->chunk_part;
336 rpl->frame_in_part++;
337 if (rpl->frame_in_part == rpl->frames_per_chunk) {
338 rpl->frame_in_part = 0;
342 ret = av_get_packet(pb, pkt, index_entry->size);
345 if (ret != index_entry->size) {
346 av_packet_unref(pkt);
350 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
351 // frames_per_chunk should always be one here; the header
352 // parsing will warn if it isn't.
353 pkt->duration = rpl->frames_per_chunk;
355 // All the audio codecs supported in this container
356 // (at least so far) are constant-bitrate.
357 pkt->duration = ret * 8;
359 pkt->pts = index_entry->timestamp;
360 pkt->stream_index = rpl->chunk_part;
364 // None of the Escape formats have keyframes, and the ADPCM
365 // format used doesn't have keyframes.
366 if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
367 pkt->flags |= AV_PKT_FLAG_KEY;
372 AVInputFormat ff_rpl_demuxer = {
374 .long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
375 .priv_data_size = sizeof(RPLContext),
376 .read_probe = rpl_probe,
377 .read_header = rpl_read_header,
378 .read_packet = rpl_read_packet,