FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rpl.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 165 209 78.9%
Functions: 7 7 100.0%
Branches: 73 129 56.6%

Line Branch Exec Source
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
37 7124 static int rpl_probe(const AVProbeData *p)
38 {
39
2/2
✓ Branch 0 taken 7122 times.
✓ Branch 1 taken 2 times.
7124 if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
40 7122 return 0;
41
42 2 return AVPROBE_SCORE_MAX;
43 }
44
45 typedef struct RPLContext {
46 // RPL header data
47 int32_t frames_per_chunk;
48
49 // Stream position data
50 uint32_t chunk_number;
51 uint32_t chunk_part;
52 uint32_t frame_in_part;
53 } RPLContext;
54
55 405 static int read_line(AVIOContext * pb, char* line, int bufsize)
56 {
57 int i;
58
1/2
✓ Branch 0 taken 7645 times.
✗ Branch 1 not taken.
7645 for (i = 0; i < bufsize - 1; i++) {
59 7645 int b = avio_r8(pb);
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7645 times.
7645 if (b == 0)
61 break;
62
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 7240 times.
7645 if (b == '\n') {
63 405 line[i] = '\0';
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 405 times.
405 return avio_feof(pb) ? -1 : 0;
65 }
66 7240 line[i] = b;
67 }
68 line[i] = '\0';
69 return -1;
70 }
71
72 24 static int32_t read_int(const char* line, const char** endptr, int* error)
73 {
74 24 unsigned long result = 0;
75
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
90 for (; *line>='0' && *line<='9'; line++) {
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (result > (0x7FFFFFFF - 9) / 10)
77 *error = -1;
78 66 result = 10 * result + *line - '0';
79 }
80 24 *endptr = line;
81 24 return result;
82 }
83
84 18 static int32_t read_line_and_int(AVIOContext * pb, int* error)
85 {
86 char line[RPL_LINE_LENGTH];
87 const char *endptr;
88 18 *error |= read_line(pb, line, sizeof(line));
89 18 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 */
96 2 static AVRational read_fps(const char* line, int* error)
97 {
98 2 int64_t num, den = 1;
99 AVRational result;
100 2 num = read_int(line, &line, error);
101
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (*line == '.')
102 2 line++;
103
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
14 for (; *line>='0' && *line<='9'; line++) {
104 // Truncate any numerator too large to fit into an int64_t
105
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
106 break;
107 12 num = 10 * num + (*line - '0');
108 12 den *= 10;
109 }
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!num)
111 *error = -1;
112 2 av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
113 2 return result;
114 }
115
116 2 static int rpl_read_header(AVFormatContext *s)
117 {
118 2 AVIOContext *pb = s->pb;
119 2 RPLContext *rpl = s->priv_data;
120 2 AVStream *vst = NULL, *ast = NULL;
121 int64_t total_audio_size;
122 2 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 2 error |= read_line(pb, line, sizeof(line)); // ARMovie
141 2 error |= read_line(pb, line, sizeof(line)); // movie name
142 2 av_dict_set(&s->metadata, "title" , line, 0);
143 2 error |= read_line(pb, line, sizeof(line)); // date/copyright
144 2 av_dict_set(&s->metadata, "copyright", line, 0);
145 2 error |= read_line(pb, line, sizeof(line)); // author and other
146 2 av_dict_set(&s->metadata, "author" , line, 0);
147
148 // video headers
149 2 video_format = read_line_and_int(pb, &error);
150
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (video_format) {
151 2 vst = avformat_new_stream(s, NULL);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vst)
153 return AVERROR(ENOMEM);
154 2 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
155 2 vst->codecpar->codec_tag = video_format;
156 2 vst->codecpar->width = read_line_and_int(pb, &error); // video width
157 2 vst->codecpar->height = read_line_and_int(pb, &error); // video height
158 2 vst->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error); // video bits per sample
159
160 // Figure out the video codec
161
2/3
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 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 1 case 124:
168 1 vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE124;
169 // The header is wrong here, at least sometimes
170 1 vst->codecpar->bits_per_coded_sample = 16;
171 2 break;
172 1 case 130:
173 1 vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE130;
174 1 break;
175 default:
176 avpriv_report_missing_feature(s, "Video format %s",
177 av_fourcc2str(vst->codecpar->codec_tag));
178 vst->codecpar->codec_id = AV_CODEC_ID_NONE;
179 }
180 } else {
181 for (i = 0; i < 3; i++)
182 error |= read_line(pb, line, sizeof(line));
183 }
184
185 2 error |= read_line(pb, line, sizeof(line)); // video frames per second
186 2 fps = read_fps(line, &error);
187
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (vst)
188 2 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 2 error |= read_line(pb, line, sizeof(line));
195 2 audio_format = read_int(line, &endptr, &error); // audio format ID
196 2 av_strlcpy(audio_codec, endptr, RPL_LINE_LENGTH);
197
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (audio_format) {
198 int channels;
199 2 ast = avformat_new_stream(s, NULL);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ast)
201 return AVERROR(ENOMEM);
202 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
203 2 ast->codecpar->codec_tag = audio_format;
204 2 ast->codecpar->sample_rate = read_line_and_int(pb, &error); // audio bitrate
205 2 channels = read_line_and_int(pb, &error); // number of audio channels
206 2 error |= read_line(pb, line, sizeof(line));
207 2 ast->codecpar->bits_per_coded_sample = read_int(line, &endptr, &error); // audio bits per sample
208 2 av_strlcpy(audio_type, endptr, RPL_LINE_LENGTH);
209 2 ast->codecpar->ch_layout.nb_channels = channels;
210 // At least one sample uses 0 for ADPCM, which is really 4 bits
211 // per sample.
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ast->codecpar->bits_per_coded_sample == 0)
213 ast->codecpar->bits_per_coded_sample = 4;
214
215 2 ast->codecpar->bit_rate = ast->codecpar->sample_rate *
216 2 (int64_t)ast->codecpar->ch_layout.nb_channels;
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ast->codecpar->bit_rate > INT64_MAX / ast->codecpar->bits_per_coded_sample)
218 return AVERROR_INVALIDDATA;
219 2 ast->codecpar->bit_rate *= ast->codecpar->bits_per_coded_sample;
220
221 2 ast->codecpar->codec_id = AV_CODEC_ID_NONE;
222
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 switch (audio_format) {
223 1 case 1:
224
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ast->codecpar->bits_per_coded_sample == 16) {
225 // 16-bit audio is always signed
226 1 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
227 } else if (ast->codecpar->bits_per_coded_sample == 8) {
228 if (av_stristr(audio_type, "unsigned") != NULL)
229 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
230 else if (av_stristr(audio_type, "linear") != NULL)
231 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
232 else
233 ast->codecpar->codec_id = AV_CODEC_ID_PCM_VIDC;
234 }
235 // There are some other formats listed as legal per the spec;
236 // samples needed.
237 1 break;
238 case 2:
239 if (av_stristr(audio_codec, "adpcm") != NULL) {
240 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ACORN;
241 }
242 break;
243 1 case 101:
244
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ast->codecpar->bits_per_coded_sample == 8) {
245 // The samples with this kind of audio that I have
246 // are all unsigned.
247 1 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
248 } else if (ast->codecpar->bits_per_coded_sample == 4) {
249 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
250 }
251 1 break;
252 }
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ast->codecpar->codec_id == AV_CODEC_ID_NONE)
254 avpriv_request_sample(s, "Audio format %"PRId32" (%s)",
255 audio_format, audio_codec);
256 2 avpriv_set_pts_info(ast, 32, 1, ast->codecpar->bit_rate);
257 } else {
258 for (i = 0; i < 3; i++)
259 error |= read_line(pb, line, sizeof(line));
260 }
261
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->nb_streams == 0)
263 return AVERROR_INVALIDDATA;
264
265 2 rpl->frames_per_chunk = read_line_and_int(pb, &error); // video frames per chunk
266
4/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
2 if (vst && rpl->frames_per_chunk > 1 && vst->codecpar->codec_tag != 124)
267 av_log(s, AV_LOG_WARNING,
268 "Don't know how to split frames for video format %s. "
269 "Video stream will be broken!\n", av_fourcc2str(vst->codecpar->codec_tag));
270
271 2 number_of_chunks = read_line_and_int(pb, &error); // number of chunks in the file
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (number_of_chunks == INT_MAX)
273 return AVERROR_INVALIDDATA;
274
275 // The number in the header is actually the index of the last chunk.
276 2 number_of_chunks++;
277
278 2 error |= read_line(pb, line, sizeof(line)); // "even" chunk size in bytes
279 2 error |= read_line(pb, line, sizeof(line)); // "odd" chunk size in bytes
280 chunk_catalog_offset = // offset of the "chunk catalog"
281 2 read_line_and_int(pb, &error); // (file index)
282 2 error |= read_line(pb, line, sizeof(line)); // offset to "helpful" sprite
283 2 error |= read_line(pb, line, sizeof(line)); // size of "helpful" sprite
284
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (vst) {
285 2 error |= read_line(pb, line, sizeof(line)); // offset to key frame list
286 2 vst->duration = number_of_chunks * (int64_t)rpl->frames_per_chunk;
287 }
288
289 // Read the index
290 2 avio_seek(pb, chunk_catalog_offset, SEEK_SET);
291 2 total_audio_size = 0;
292
3/4
✓ Branch 0 taken 365 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 363 times.
✓ Branch 3 taken 2 times.
365 for (i = 0; !error && i < number_of_chunks; i++) {
293 int64_t offset, video_size, audio_size;
294 363 error |= read_line(pb, line, sizeof(line));
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
296 &offset, &video_size, &audio_size)) {
297 error = -1;
298 continue;
299 }
300
1/2
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
363 if (vst)
301 363 av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
302 video_size, rpl->frames_per_chunk, 0);
303
1/2
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
363 if (ast)
304 363 av_add_index_entry(ast, offset + video_size, total_audio_size,
305 audio_size, audio_size * 8, 0);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363 times.
363 if (total_audio_size/8 + (uint64_t)audio_size >= INT64_MAX/8)
307 return AVERROR_INVALIDDATA;
308 363 total_audio_size += audio_size * 8;
309 }
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (error)
312 return AVERROR(EIO);
313
314 2 return 0;
315 }
316
317 824 static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
318 {
319 824 RPLContext *rpl = s->priv_data;
320 824 AVIOContext *pb = s->pb;
321 AVStream* stream;
322 FFStream *sti;
323 AVIndexEntry* index_entry;
324 int ret;
325
326
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 461 times.
824 if (rpl->chunk_part == s->nb_streams) {
327 363 rpl->chunk_number++;
328 363 rpl->chunk_part = 0;
329 }
330
331 824 stream = s->streams[rpl->chunk_part];
332 824 sti = ffstream(stream);
333
334
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 822 times.
824 if (rpl->chunk_number >= sti->nb_index_entries)
335 2 return AVERROR_EOF;
336
337 822 index_entry = &sti->index_entries[rpl->chunk_number];
338
339
2/2
✓ Branch 0 taken 726 times.
✓ Branch 1 taken 96 times.
822 if (rpl->frame_in_part == 0) {
340
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 726 times.
726 if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
341 return AVERROR(EIO);
342 }
343
344
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 363 times.
822 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
345
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 359 times.
559 stream->codecpar->codec_tag == 124) {
346 // We have to split Escape 124 frames because there are
347 // multiple frames per chunk in Escape 124 samples.
348 uint32_t frame_size;
349
350 100 avio_skip(pb, 4); /* flags */
351 100 frame_size = avio_rl32(pb);
352
3/6
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 100 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 100 times.
100 if (avio_feof(pb) || avio_seek(pb, -8, SEEK_CUR) < 0 || !frame_size)
353 return AVERROR(EIO);
354
355 100 ret = av_get_packet(pb, pkt, frame_size);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
100 if (ret < 0)
357 return ret;
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
100 if (ret != frame_size)
359 return AVERROR(EIO);
360
361 100 pkt->duration = 1;
362 100 pkt->pts = index_entry->timestamp + rpl->frame_in_part;
363 100 pkt->stream_index = rpl->chunk_part;
364
365 100 rpl->frame_in_part++;
366
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 96 times.
100 if (rpl->frame_in_part == rpl->frames_per_chunk) {
367 4 rpl->frame_in_part = 0;
368 4 rpl->chunk_part++;
369 }
370 } else {
371 722 ret = av_get_packet(pb, pkt, index_entry->size);
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 722 times.
722 if (ret < 0)
373 return ret;
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 722 times.
722 if (ret != index_entry->size)
375 return AVERROR(EIO);
376
377
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 363 times.
722 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
378 // frames_per_chunk should always be one here; the header
379 // parsing will warn if it isn't.
380 359 pkt->duration = rpl->frames_per_chunk;
381 } else {
382 // All the audio codecs supported in this container
383 // (at least so far) are constant-bitrate.
384 363 pkt->duration = ret * 8;
385 }
386 722 pkt->pts = index_entry->timestamp;
387 722 pkt->stream_index = rpl->chunk_part;
388 722 rpl->chunk_part++;
389 }
390
391 // None of the Escape formats have keyframes, and the ADPCM
392 // format used doesn't have keyframes.
393
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 794 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 24 times.
822 if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
394 4 pkt->flags |= AV_PKT_FLAG_KEY;
395
396 822 return ret;
397 }
398
399 const FFInputFormat ff_rpl_demuxer = {
400 .p.name = "rpl",
401 .p.long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
402 .priv_data_size = sizeof(RPLContext),
403 .read_probe = rpl_probe,
404 .read_header = rpl_read_header,
405 .read_packet = rpl_read_packet,
406 };
407