Move/add COSTABLE/SINTABLE macros to dsputil to add extern definitions
[FFMpeg-mirror/lagarith.git] / libavformat / rpl.c
blobad713ffada80989731f67e058b8b2a55c73091d5
1 /*
2 * ARMovie/RPL demuxer
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
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include <stdlib.h>
26 #define RPL_SIGNATURE "ARMovie\x0A"
27 #define RPL_SIGNATURE_SIZE 8
29 /** 256 is arbitrary, but should be big enough for any reasonable file. */
30 #define RPL_LINE_LENGTH 256
32 static int rpl_probe(AVProbeData *p)
34 if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
35 return 0;
37 return AVPROBE_SCORE_MAX;
40 typedef struct RPLContext {
41 // RPL header data
42 int32_t frames_per_chunk;
44 // Stream position data
45 uint32_t chunk_number;
46 uint32_t chunk_part;
47 uint32_t frame_in_part;
48 } RPLContext;
50 static int read_line(ByteIOContext * pb, char* line, int bufsize)
52 int i;
53 for (i = 0; i < bufsize - 1; i++) {
54 int b = get_byte(pb);
55 if (b == 0)
56 break;
57 if (b == '\n') {
58 line[i] = '\0';
59 return 0;
61 line[i] = b;
63 line[i] = '\0';
64 return -1;
67 static int32_t read_int(const char* line, const char** endptr, int* error)
69 unsigned long result = 0;
70 for (; *line>='0' && *line<='9'; line++) {
71 if (result > (0x7FFFFFFF - 9) / 10)
72 *error = -1;
73 result = 10 * result + *line - '0';
75 *endptr = line;
76 return result;
79 static int32_t read_line_and_int(ByteIOContext * pb, int* error)
81 char line[RPL_LINE_LENGTH];
82 const char *endptr;
83 *error |= read_line(pb, line, sizeof(line));
84 return read_int(line, &endptr, error);
87 /** Parsing for fps, which can be a fraction. Unfortunately,
88 * the spec for the header leaves out a lot of details,
89 * so this is mostly guessing.
91 static AVRational read_fps(const char* line, int* error)
93 int64_t num, den = 1;
94 AVRational result;
95 num = read_int(line, &line, error);
96 if (*line == '.')
97 line++;
98 for (; *line>='0' && *line<='9'; line++) {
99 // Truncate any numerator too large to fit into an int64_t
100 if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
101 break;
102 num = 10 * num + *line - '0';
103 den *= 10;
105 if (!num)
106 *error = -1;
107 av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
108 return result;
111 static int rpl_read_header(AVFormatContext *s, AVFormatParameters *ap)
113 ByteIOContext *pb = s->pb;
114 RPLContext *rpl = s->priv_data;
115 AVStream *vst = NULL, *ast = NULL;
116 int total_audio_size;
117 int error = 0;
119 uint32_t i;
121 int32_t audio_format, chunk_catalog_offset, number_of_chunks;
122 AVRational fps;
124 char line[RPL_LINE_LENGTH];
126 // The header for RPL/ARMovie files is 21 lines of text
127 // containing the various header fields. The fields are always
128 // in the same order, and other text besides the first
129 // number usually isn't important.
130 // (The spec says that there exists some significance
131 // for the text in a few cases; samples needed.)
132 error |= read_line(pb, line, sizeof(line)); // ARMovie
133 error |= read_line(pb, line, sizeof(line)); // movie name
134 av_metadata_set(&s->metadata, "title" , line);
135 error |= read_line(pb, line, sizeof(line)); // date/copyright
136 av_metadata_set(&s->metadata, "copyright", line);
137 error |= read_line(pb, line, sizeof(line)); // author and other
138 av_metadata_set(&s->metadata, "author" , line);
140 // video headers
141 vst = av_new_stream(s, 0);
142 if (!vst)
143 return AVERROR(ENOMEM);
144 vst->codec->codec_type = CODEC_TYPE_VIDEO;
145 vst->codec->codec_tag = read_line_and_int(pb, &error); // video format
146 vst->codec->width = read_line_and_int(pb, &error); // video width
147 vst->codec->height = read_line_and_int(pb, &error); // video height
148 vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
149 error |= read_line(pb, line, sizeof(line)); // video frames per second
150 fps = read_fps(line, &error);
151 av_set_pts_info(vst, 32, fps.den, fps.num);
153 // Figure out the video codec
154 switch (vst->codec->codec_tag) {
155 #if 0
156 case 122:
157 vst->codec->codec_id = CODEC_ID_ESCAPE122;
158 break;
159 #endif
160 case 124:
161 vst->codec->codec_id = CODEC_ID_ESCAPE124;
162 // The header is wrong here, at least sometimes
163 vst->codec->bits_per_coded_sample = 16;
164 break;
165 #if 0
166 case 130:
167 vst->codec->codec_id = CODEC_ID_ESCAPE130;
168 break;
169 #endif
170 default:
171 av_log(s, AV_LOG_WARNING,
172 "RPL video format %i not supported yet!\n",
173 vst->codec->codec_tag);
174 vst->codec->codec_id = CODEC_ID_NONE;
177 // Audio headers
179 // ARMovie supports multiple audio tracks; I don't have any
180 // samples, though. This code will ignore additional tracks.
181 audio_format = read_line_and_int(pb, &error); // audio format ID
182 if (audio_format) {
183 ast = av_new_stream(s, 0);
184 if (!ast)
185 return AVERROR(ENOMEM);
186 ast->codec->codec_type = CODEC_TYPE_AUDIO;
187 ast->codec->codec_tag = audio_format;
188 ast->codec->sample_rate = read_line_and_int(pb, &error); // audio bitrate
189 ast->codec->channels = read_line_and_int(pb, &error); // number of audio channels
190 ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error); // audio bits per sample
191 // At least one sample uses 0 for ADPCM, which is really 4 bits
192 // per sample.
193 if (ast->codec->bits_per_coded_sample == 0)
194 ast->codec->bits_per_coded_sample = 4;
196 ast->codec->bit_rate = ast->codec->sample_rate *
197 ast->codec->bits_per_coded_sample *
198 ast->codec->channels;
200 ast->codec->codec_id = CODEC_ID_NONE;
201 switch (audio_format) {
202 case 1:
203 if (ast->codec->bits_per_coded_sample == 16) {
204 // 16-bit audio is always signed
205 ast->codec->codec_id = CODEC_ID_PCM_S16LE;
206 break;
208 // There are some other formats listed as legal per the spec;
209 // samples needed.
210 break;
211 case 101:
212 if (ast->codec->bits_per_coded_sample == 8) {
213 // The samples with this kind of audio that I have
214 // are all unsigned.
215 ast->codec->codec_id = CODEC_ID_PCM_U8;
216 break;
217 } else if (ast->codec->bits_per_coded_sample == 4) {
218 ast->codec->codec_id = CODEC_ID_ADPCM_IMA_EA_SEAD;
219 break;
221 break;
223 if (ast->codec->codec_id == CODEC_ID_NONE) {
224 av_log(s, AV_LOG_WARNING,
225 "RPL audio format %i not supported yet!\n",
226 audio_format);
228 av_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
229 } else {
230 for (i = 0; i < 3; i++)
231 error |= read_line(pb, line, sizeof(line));
234 rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
235 if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
236 av_log(s, AV_LOG_WARNING,
237 "Don't know how to split frames for video format %i. "
238 "Video stream will be broken!\n", vst->codec->codec_tag);
240 number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
241 // The number in the header is actually the index of the last chunk.
242 number_of_chunks++;
244 error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
245 error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
246 chunk_catalog_offset = // offset of the "chunk catalog"
247 read_line_and_int(pb, &error); // (file index)
248 error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
249 error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
250 error |= read_line(pb, line, sizeof(line)); // offset to key frame list
252 // Read the index
253 url_fseek(pb, chunk_catalog_offset, SEEK_SET);
254 total_audio_size = 0;
255 for (i = 0; i < number_of_chunks; i++) {
256 int64_t offset, video_size, audio_size;
257 error |= read_line(pb, line, sizeof(line));
258 if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
259 &offset, &video_size, &audio_size))
260 error = -1;
261 av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
262 video_size, rpl->frames_per_chunk, 0);
263 if (ast)
264 av_add_index_entry(ast, offset + video_size, total_audio_size,
265 audio_size, audio_size * 8, 0);
266 total_audio_size += audio_size * 8;
269 if (error) return AVERROR(EIO);
271 return 0;
274 static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
276 RPLContext *rpl = s->priv_data;
277 ByteIOContext *pb = s->pb;
278 AVStream* stream;
279 AVIndexEntry* index_entry;
280 uint32_t ret;
282 if (rpl->chunk_part == s->nb_streams) {
283 rpl->chunk_number++;
284 rpl->chunk_part = 0;
287 stream = s->streams[rpl->chunk_part];
289 if (rpl->chunk_number >= stream->nb_index_entries)
290 return -1;
292 index_entry = &stream->index_entries[rpl->chunk_number];
294 if (rpl->frame_in_part == 0)
295 if (url_fseek(pb, index_entry->pos, SEEK_SET) < 0)
296 return AVERROR(EIO);
298 if (stream->codec->codec_type == CODEC_TYPE_VIDEO &&
299 stream->codec->codec_tag == 124) {
300 // We have to split Escape 124 frames because there are
301 // multiple frames per chunk in Escape 124 samples.
302 uint32_t frame_size, frame_flags;
304 frame_flags = get_le32(pb);
305 frame_size = get_le32(pb);
306 if (url_fseek(pb, -8, SEEK_CUR) < 0)
307 return AVERROR(EIO);
309 ret = av_get_packet(pb, pkt, frame_size);
310 if (ret != frame_size) {
311 av_free_packet(pkt);
312 return AVERROR(EIO);
314 pkt->duration = 1;
315 pkt->pts = index_entry->timestamp + rpl->frame_in_part;
316 pkt->stream_index = rpl->chunk_part;
318 rpl->frame_in_part++;
319 if (rpl->frame_in_part == rpl->frames_per_chunk) {
320 rpl->frame_in_part = 0;
321 rpl->chunk_part++;
323 } else {
324 ret = av_get_packet(pb, pkt, index_entry->size);
325 if (ret != index_entry->size) {
326 av_free_packet(pkt);
327 return AVERROR(EIO);
330 if (stream->codec->codec_type == CODEC_TYPE_VIDEO) {
331 // frames_per_chunk should always be one here; the header
332 // parsing will warn if it isn't.
333 pkt->duration = rpl->frames_per_chunk;
334 } else {
335 // All the audio codecs supported in this container
336 // (at least so far) are constant-bitrate.
337 pkt->duration = ret * 8;
339 pkt->pts = index_entry->timestamp;
340 pkt->stream_index = rpl->chunk_part;
341 rpl->chunk_part++;
344 // None of the Escape formats have keyframes, and the ADPCM
345 // format used doesn't have keyframes.
346 if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
347 pkt->flags |= PKT_FLAG_KEY;
349 return ret;
352 AVInputFormat rpl_demuxer = {
353 "rpl",
354 NULL_IF_CONFIG_SMALL("RPL/ARMovie format"),
355 sizeof(RPLContext),
356 rpl_probe,
357 rpl_read_header,
358 rpl_read_packet,