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 #include <SDL2/SDL.h>
25
26 #include "../tiny_codec.h"
27 #include "../codecs/avcodec.h"
28
29
30 #define RPL_SIGNATURE "ARMovie"
31
32 /** 256 is arbitrary, but should be big enough for any reasonable file. */
33 #define RPL_LINE_LENGTH 256
34
35 typedef struct RPLContext
36 {
37 // RPL header data
38 int32_t frames_per_chunk;
39
40 // Stream position data
41 uint32_t chunk_part;
42 uint32_t frame_in_part;
43 } RPLContext;
44
read_line(SDL_RWops * pb,char * line,int bufsize)45 static int read_line(SDL_RWops *pb, char* line, int bufsize)
46 {
47 int i;
48 uint8_t b = 0;
49 for (i = 0; (i < bufsize - 1) && SDL_RWread(pb, &b, 1, 1); i++)
50 {
51 if (b == 0)
52 break;
53
54 if(b == '\n')
55 {
56 line[i] = '\0';
57 return 0;
58 }
59 line[i] = b;
60 }
61 line[i] = '\0';
62 return -1;
63 }
64
read_int(const char * line,const char ** endptr,int * error)65 static int32_t read_int(const char* line, const char** endptr, int* error)
66 {
67 unsigned long result = 0;
68 for (; *line>='0' && *line <= '9'; line++)
69 {
70 if (result > (0x7FFFFFFF - 9) / 10)
71 *error = -1;
72 result = 10 * result + *line - '0';
73 }
74 *endptr = line;
75 return result;
76 }
77
read_line_and_int(SDL_RWops * pb,int * error)78 static int32_t read_line_and_int(SDL_RWops *pb, int* error)
79 {
80 char line[RPL_LINE_LENGTH];
81 const char *endptr;
82 *error |= read_line(pb, line, sizeof(line));
83 return read_int(line, &endptr, error);
84 }
85
86 /** Parsing for fps, which can be a fraction. Unfortunately,
87 * the spec for the header leaves out a lot of details,
88 * so this is mostly guessing.
89 */
read_fps(const char * line,uint64_t * num,uint64_t * denum)90 static int read_fps(const char* line, uint64_t *num, uint64_t *denum)
91 {
92 int error = 0;
93 *num = read_int(line, &line, &error);
94 *denum = 1;
95
96 if (*line == '.')
97 line++;
98
99 for (; (*line >= '0') && (*line <= '9'); line++)
100 {
101 // Truncate any numerator too large to fit into an int64_t
102 if (*num > (INT64_MAX - 9) / 10 || *denum > INT64_MAX / 10)
103 break;
104 *num = *num * 10 + *line - '0';
105 *denum *= 10;
106 }
107 if (!num)
108 error = -1;
109
110 return error;
111 }
112
113
rpl_read_packet(struct tiny_codec_s * s,struct AVPacket * pkt)114 static int rpl_read_packet(struct tiny_codec_s *s, struct AVPacket *pkt)
115 {
116 SDL_RWops *pb = s->input;
117 RPLContext *rpl = (RPLContext*)s->private_context;
118 int ret = -1;
119 index_entry_p entry;
120
121 if(pkt->is_video)
122 {
123 if (s->video.entry_current >= s->video.entry_size)
124 return -1;
125
126 entry = &s->video.entry[s->video.entry_current];
127
128 pkt->pos = (rpl->frame_in_part == 0) ? (entry->pos) : pkt->pos;
129 if(SDL_RWseek(pb, pkt->pos, RW_SEEK_SET) < 0)
130 return -1;
131
132 if(s->video.codec_tag == AV_CODEC_ID_ESCAPE124)
133 {
134 // We have to split Escape 124 frames because there are
135 // multiple frames per chunk in Escape 124 samples.
136 uint32_t frame_size;
137 SDL_ReadLE32(pb); // flags
138 frame_size = SDL_ReadLE32(pb);
139 if (SDL_RWseek(pb, -8, RW_SEEK_CUR) < 0)
140 return -1;
141
142 ret = av_get_packet(pb, pkt, frame_size);
143 if (ret < 0)
144 return ret;
145 if (ret != frame_size)
146 {
147 return -1;
148 }
149 pkt->duration = 1;
150 pkt->pts = entry->timestamp + rpl->frame_in_part;
151 pkt->stream_index = rpl->chunk_part;
152
153 rpl->frame_in_part++;
154 if (rpl->frame_in_part >= rpl->frames_per_chunk)
155 {
156 rpl->frame_in_part = 0;
157 s->video.entry_current++;
158 }
159 }
160 else if(s->video.codec_tag == AV_CODEC_ID_ESCAPE130)
161 {
162 pkt->pos = entry->pos;
163 if(SDL_RWseek(pb, pkt->pos, RW_SEEK_SET) < 0)
164 return -1;
165
166 ret = av_get_packet(pb, pkt, entry->size);
167 if (ret < 0)
168 return ret;
169 if (ret != entry->size)
170 {
171 return -1;
172 }
173
174 // frames_per_chunk should always be one here; the header
175 // parsing will warn if it isn't.
176 pkt->duration = rpl->frames_per_chunk;
177
178 pkt->pts = entry->timestamp;
179 pkt->stream_index = rpl->chunk_part;
180 s->video.entry_current++;
181 }
182 }
183 else
184 {
185 if(s->audio.entry_current >= s->audio.entry_size)
186 return -1;
187
188 entry = &s->audio.entry[s->audio.entry_current];
189
190 pkt->pos = entry->pos;
191 if(SDL_RWseek(pb, pkt->pos, RW_SEEK_SET) < 0)
192 return -1;
193
194 ret = av_get_packet(pb, pkt, entry->size);
195 if (ret < 0)
196 return ret;
197 if (ret != entry->size)
198 {
199 return -1;
200 }
201
202 // All the audio codecs supported in this container
203 // (at least so far) are constant-bitrate.
204 pkt->duration = ret * 8;
205
206 pkt->pts = entry->timestamp;
207 pkt->stream_index = rpl->chunk_part;
208 s->audio.entry_current++;
209 }
210
211 // None of the Escape formats have keyframes, and the ADPCM
212 // format used doesn't have keyframes.
213
214 return ret;
215 }
216
217
218 void escape124_decode_init(struct tiny_codec_s *avctx);
219 void escape130_decode_init(struct tiny_codec_s *avctx);
220 void pcm_decode_init(struct tiny_codec_s *avctx);
221 void adpcm_decode_init(struct tiny_codec_s *avctx);
222
codec_open_rpl(struct tiny_codec_s * s)223 int codec_open_rpl(struct tiny_codec_s *s)
224 {
225 SDL_RWops *pb = s->input;
226 s->private_context = (RPLContext*)calloc(sizeof(RPLContext), 1);
227 s->free_context = free;
228 RPLContext *rpl = (RPLContext*)s->private_context;
229 int total_audio_size;
230 int codec_tag;
231 int error = 0;
232
233 uint32_t i;
234 int32_t audio_format, chunk_catalog_offset, number_of_chunks;
235
236 char line[RPL_LINE_LENGTH];
237
238 SDL_RWseek(pb, 0, RW_SEEK_SET);
239 // The header for RPL/ARMovie files is 21 lines of text
240 // containing the various header fields. The fields are always
241 // in the same order, and other text besides the first
242 // number usually isn't important.
243 // (The spec says that there exists some significance
244 // for the text in a few cases; samples needed.)
245 error |= read_line(pb, line, sizeof(line)); // ARMovie
246 if(strncmp(line, RPL_SIGNATURE, RPL_LINE_LENGTH))
247 {
248 return -1;
249 }
250 error |= read_line(pb, line, sizeof(line)); // movie name
251 error |= read_line(pb, line, sizeof(line)); // date/copyright
252 error |= read_line(pb, line, sizeof(line)); // author and other
253
254 // video headers
255 codec_tag = read_line_and_int(pb, &error); // video format
256 s->packet = rpl_read_packet;
257 s->video.decode = NULL;
258 s->video.width = read_line_and_int(pb, &error); // video width
259 s->video.height = read_line_and_int(pb, &error); // video height
260 read_line_and_int(pb, &error); // video bits per sample
261 error |= read_line(pb, line, sizeof(line)); // video frames per second
262 error |= read_fps(line, &s->fps_num, &s->fps_denum);
263 codec_simplify_fps(s);
264
265 // Figure out the video codec
266 switch (codec_tag)
267 {
268 case 122:
269 s->video.codec_tag = 0;//AV_CODEC_ID_ESCAPE122;
270 break;
271
272 case 124:
273 s->video.codec_tag = AV_CODEC_ID_ESCAPE124;
274 escape124_decode_init(s);
275 // The header is wrong here, at least sometimes
276 break;
277
278 case 130:
279 s->video.codec_tag = AV_CODEC_ID_ESCAPE130;
280 escape130_decode_init(s);
281 break;
282
283 default:
284 s->video.codec_tag = 0;
285 break;
286 }
287
288 // Audio headers
289
290 // ARMovie supports multiple audio tracks; I don't have any
291 // samples, though. This code will ignore additional tracks.
292 audio_format = read_line_and_int(pb, &error); // audio format ID
293 s->audio.buff = NULL;
294 s->audio.decode = NULL;
295 if(audio_format)
296 {
297 s->audio.format = audio_format;
298 s->audio.sample_rate = read_line_and_int(pb, &error); // audio bitrate
299 s->audio.channels = read_line_and_int(pb, &error); // number of audio channels
300 s->audio.bits_per_coded_sample = read_line_and_int(pb, &error); // audio bits per sample
301 // At least one sample uses 0 for ADPCM, which is really 4 bits
302 // per sample.
303 if (s->audio.bits_per_coded_sample == 0)
304 s->audio.bits_per_coded_sample = 4;
305
306 s->audio.bit_rate = s->audio.sample_rate * s->audio.bits_per_coded_sample * s->audio.channels;
307
308 switch (audio_format)
309 {
310 case 1:
311 if(s->audio.bits_per_coded_sample == 16)
312 {
313 // 16-bit audio is always signed
314 s->audio.codec_tag = AV_CODEC_ID_PCM_S16LE;
315 pcm_decode_init(s);
316 }
317 // There are some other formats listed as legal per the spec;
318 // samples needed.
319 break;
320
321 case 101:
322 if (s->audio.bits_per_coded_sample == 8)
323 {
324 // The samples with this kind of audio that I have
325 // are all unsigned.
326 s->audio.codec_tag = AV_CODEC_ID_PCM_U8;
327 pcm_decode_init(s);
328 }
329 else if (s->audio.bits_per_coded_sample == 4)
330 {
331 s->audio.codec_tag = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
332 adpcm_decode_init(s);
333 }
334 break;
335 }
336 }
337 else
338 {
339 for (i = 0; i < 3; i++)
340 error |= read_line(pb, line, sizeof(line));
341 }
342
343 rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
344 //if (rpl->frames_per_chunk > 1 && vst->codecpar->codec_tag != 124)
345 // av_log(s, AV_LOG_WARNING,
346 // "Don't know how to split frames for video format %s. "
347 // "Video stream will be broken!\n", av_fourcc2str(vst->codecpar->codec_tag));
348
349 number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
350 // The number in the header is actually the index of the last chunk.
351 number_of_chunks++;
352
353 error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
354 error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
355 chunk_catalog_offset = // offset of the "chunk catalog"
356 read_line_and_int(pb, &error); // (file index)
357 error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
358 error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
359 error |= read_line(pb, line, sizeof(line)); // offset to key frame list
360
361 // Read the index
362 SDL_RWseek(pb, chunk_catalog_offset, RW_SEEK_SET);
363 total_audio_size = 0;
364
365 s->video.rgba = (uint8_t*)malloc(4 * s->video.width * s->video.height);
366 s->video.entry_size = number_of_chunks;
367 s->video.entry = (index_entry_p)malloc(number_of_chunks * sizeof(index_entry_t));
368 s->audio.entry_size = number_of_chunks;
369 s->audio.entry = (index_entry_p)malloc(number_of_chunks * sizeof(index_entry_t));
370
371 for (i = 0; !error && i < number_of_chunks; i++)
372 {
373 int64_t offset, video_size, audio_size;
374 error |= read_line(pb, line, sizeof(line));
375 if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
376 &offset, &video_size, &audio_size))
377 {
378 error = -1;
379 continue;
380 }
381 s->video.entry[i].pos = offset;
382 s->video.entry[i].timestamp = i * rpl->frames_per_chunk;
383 s->video.entry[i].size = video_size;
384 s->video.entry[i].distance = rpl->frames_per_chunk;
385 s->video.entry[i].flags = 0;
386
387 s->audio.entry[i].pos = offset + video_size;
388 s->audio.entry[i].timestamp = total_audio_size;
389 s->audio.entry[i].size = audio_size;
390 s->audio.entry[i].distance = audio_size * 8;
391 s->audio.entry[i].flags = 0;
392
393 total_audio_size += audio_size * 8;
394 }
395
396 return error;
397 }
398