FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rpl.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 163 205 79.5%
Branches: 71 125 56.8%

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