OSDN Git Service

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