FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/r3d.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 144 227 63.4%
Functions: 7 10 70.0%
Branches: 41 93 44.1%

Line Branch Exec Source
1 /*
2 * R3D REDCODE demuxer
3 * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
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 "libavutil/intreadwrite.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mathematics.h"
25 #include "avformat.h"
26 #include "internal.h"
27
28 typedef struct R3DContext {
29 unsigned video_offsets_count;
30 unsigned rdvo_offset;
31
32 int audio_channels;
33 } R3DContext;
34
35 typedef struct Atom {
36 unsigned size;
37 uint32_t tag;
38 uint64_t offset;
39 } Atom;
40
41 9 static int read_atom(AVFormatContext *s, Atom *atom)
42 {
43 9 atom->offset = avio_tell(s->pb);
44 9 atom->size = avio_rb32(s->pb);
45
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (atom->size < 8)
46 2 return -1;
47 7 atom->tag = avio_rl32(s->pb);
48 7 av_log(s, AV_LOG_TRACE, "atom %u %.4s offset %#"PRIx64"\n",
49 7 atom->size, (char*)&atom->tag, atom->offset);
50 7 return atom->size;
51 }
52
53 1 static int r3d_read_red1(AVFormatContext *s)
54 {
55 1 AVStream *st = avformat_new_stream(s, NULL);
56 1 R3DContext *r3d = s->priv_data;
57 char filename[258];
58 int tmp;
59 int ret;
60 int av_unused tmp2;
61 AVRational framerate;
62
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
64 return AVERROR(ENOMEM);
65 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
66 1 st->codecpar->codec_id = AV_CODEC_ID_JPEG2000;
67
68 1 tmp = avio_r8(s->pb); // major version
69 1 tmp2 = avio_r8(s->pb); // minor version
70 1 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
71
72 1 tmp = avio_rb16(s->pb); // unknown
73 1 av_log(s, AV_LOG_TRACE, "unknown1 %d\n", tmp);
74
75 1 tmp = avio_rb32(s->pb);
76 1 avpriv_set_pts_info(st, 32, 1, tmp);
77
78 1 tmp = avio_rb32(s->pb); // filenum
79 1 av_log(s, AV_LOG_TRACE, "filenum %d\n", tmp);
80
81 1 avio_skip(s->pb, 32); // unknown
82
83 1 st->codecpar->width = avio_rb32(s->pb);
84 1 st->codecpar->height = avio_rb32(s->pb);
85
86 1 tmp = avio_rb16(s->pb); // unknown
87 1 av_log(s, AV_LOG_TRACE, "unknown2 %d\n", tmp);
88
89 1 framerate.num = avio_rb16(s->pb);
90 1 framerate.den = avio_rb16(s->pb);
91
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (framerate.num > 0 && framerate.den > 0) {
92 #if FF_API_R_FRAME_RATE
93 1 st->r_frame_rate =
94 #endif
95 1 st->avg_frame_rate = framerate;
96 }
97
98 1 r3d->audio_channels = avio_r8(s->pb); // audio channels
99 1 av_log(s, AV_LOG_TRACE, "audio channels %d\n", tmp);
100
101 1 ret = avio_read(s->pb, filename, 257);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 257)
103 return ret < 0 ? ret : AVERROR_EOF;
104 1 filename[sizeof(filename)-1] = 0;
105 1 av_dict_set(&st->metadata, "filename", filename, 0);
106
107 1 av_log(s, AV_LOG_TRACE, "filename %s\n", filename);
108 1 av_log(s, AV_LOG_TRACE, "resolution %dx%d\n", st->codecpar->width, st->codecpar->height);
109 1 av_log(s, AV_LOG_TRACE, "timescale %d\n", st->time_base.den);
110 1 av_log(s, AV_LOG_TRACE, "frame rate %d/%d\n",
111 framerate.num, framerate.den);
112
113 1 return 0;
114 }
115
116 static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
117 {
118 R3DContext *r3d = s->priv_data;
119 AVStream *st = s->streams[0];
120 int i;
121
122 r3d->video_offsets_count = (atom->size - 8) / 4;
123
124 for (i = 0; i < r3d->video_offsets_count; i++) {
125 unsigned video_offset = avio_rb32(s->pb);
126 if (!video_offset) {
127 r3d->video_offsets_count = i;
128 break;
129 }
130 av_log(s, AV_LOG_TRACE, "video offset %d: %#x\n", i, video_offset);
131 }
132
133 if (st->avg_frame_rate.num)
134 st->duration = av_rescale_q(r3d->video_offsets_count,
135 av_inv_q(st->avg_frame_rate),
136 st->time_base);
137 av_log(s, AV_LOG_TRACE, "duration %"PRId64"\n", st->duration);
138
139 return 0;
140 }
141
142 static void r3d_read_reos(AVFormatContext *s)
143 {
144 R3DContext *r3d = s->priv_data;
145 int av_unused tmp;
146
147 r3d->rdvo_offset = avio_rb32(s->pb);
148 avio_rb32(s->pb); // rdvs offset
149 avio_rb32(s->pb); // rdao offset
150 avio_rb32(s->pb); // rdas offset
151
152 tmp = avio_rb32(s->pb);
153 av_log(s, AV_LOG_TRACE, "num video chunks %d\n", tmp);
154
155 tmp = avio_rb32(s->pb);
156 av_log(s, AV_LOG_TRACE, "num audio chunks %d\n", tmp);
157
158 avio_skip(s->pb, 6*4);
159 }
160
161 1 static int r3d_read_header(AVFormatContext *s)
162 {
163 1 FFFormatContext *const si = ffformatcontext(s);
164 1 R3DContext *r3d = s->priv_data;
165 Atom atom;
166 int ret;
167
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (read_atom(s, &atom) < 0) {
169 av_log(s, AV_LOG_ERROR, "error reading atom\n");
170 return -1;
171 }
172
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (atom.tag == MKTAG('R','E','D','1')) {
173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = r3d_read_red1(s)) < 0) {
174 av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
175 return ret;
176 }
177 } else {
178 av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
179 return -1;
180 }
181
182 /* we cannot create the audio stream now because we do not know the
183 * sample rate */
184
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (r3d->audio_channels)
185 1 s->ctx_flags |= AVFMTCTX_NOHEADER;
186
187 1 si->data_offset = avio_tell(s->pb);
188 1 av_log(s, AV_LOG_TRACE, "data offset %#"PRIx64"\n", si->data_offset);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
190 return 0;
191 // find REOB/REOF/REOS to load index
192 1 avio_seek(s->pb, avio_size(s->pb)-48-8, SEEK_SET);
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (read_atom(s, &atom) < 0)
194 av_log(s, AV_LOG_ERROR, "error reading end atom\n");
195
196
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (atom.tag != MKTAG('R','E','O','B') &&
197
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 atom.tag != MKTAG('R','E','O','F') &&
198
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 atom.tag != MKTAG('R','E','O','S'))
199 1 goto out;
200
201 r3d_read_reos(s);
202
203 if (r3d->rdvo_offset) {
204 avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
205 if (read_atom(s, &atom) < 0)
206 av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
207 if (atom.tag == MKTAG('R','D','V','O')) {
208 if (r3d_read_rdvo(s, &atom) < 0)
209 av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
210 }
211 }
212
213 out:
214 1 avio_seek(s->pb, si->data_offset, SEEK_SET);
215 1 return 0;
216 }
217
218 3 static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
219 {
220 3 AVStream *st = s->streams[0];
221 int tmp;
222 int av_unused tmp2;
223 3 int64_t pos = avio_tell(s->pb);
224 unsigned dts;
225 int ret;
226
227 3 dts = avio_rb32(s->pb);
228
229 3 tmp = avio_rb32(s->pb);
230 3 av_log(s, AV_LOG_TRACE, "frame num %d\n", tmp);
231
232 3 tmp = avio_r8(s->pb); // major version
233 3 tmp2 = avio_r8(s->pb); // minor version
234 3 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
235
236 3 tmp = avio_rb16(s->pb); // unknown
237 3 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
238
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (tmp > 4) {
240 tmp = avio_rb16(s->pb); // unknown
241 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
242
243 tmp = avio_rb16(s->pb); // unknown
244 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
245
246 tmp = avio_rb32(s->pb);
247 av_log(s, AV_LOG_TRACE, "width %d\n", tmp);
248 tmp = avio_rb32(s->pb);
249 av_log(s, AV_LOG_TRACE, "height %d\n", tmp);
250
251 tmp = avio_rb32(s->pb);
252 av_log(s, AV_LOG_TRACE, "metadata len %d\n", tmp);
253 }
254 3 tmp = atom->size - 8 - (avio_tell(s->pb) - pos);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (tmp < 0)
256 return -1;
257 3 ret = av_get_packet(s->pb, pkt, tmp);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
259 av_log(s, AV_LOG_ERROR, "error reading video packet\n");
260 return -1;
261 }
262
263 3 pkt->stream_index = 0;
264 3 pkt->dts = dts;
265
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st->avg_frame_rate.num)
266 3 pkt->duration = (uint64_t)st->time_base.den*
267 3 st->avg_frame_rate.den/st->avg_frame_rate.num;
268 3 av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64"\n", pkt->dts, pkt->duration);
269
270 3 return 0;
271 }
272
273 2 static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
274 {
275 2 R3DContext *r3d = s->priv_data;
276 AVStream *st;
277 int av_unused tmp, tmp2;
278 int samples, size;
279 2 int64_t pos = avio_tell(s->pb);
280 unsigned dts;
281 int ret;
282
283
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->nb_streams < 2) {
284 1 st = avformat_new_stream(s, NULL);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
286 return AVERROR(ENOMEM);
287 1 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
288 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32BE;
289 1 st->codecpar->ch_layout.nb_channels = r3d->audio_channels;
290 1 avpriv_set_pts_info(st, 32, 1, s->streams[0]->time_base.den);
291 } else {
292 1 st = s->streams[1];
293 }
294
295 2 dts = avio_rb32(s->pb);
296
297 2 st->codecpar->sample_rate = avio_rb32(s->pb);
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (st->codecpar->sample_rate <= 0) {
299 av_log(s, AV_LOG_ERROR, "Bad sample rate\n");
300 return AVERROR_INVALIDDATA;
301 }
302
303 2 samples = avio_rb32(s->pb);
304
305 2 tmp = avio_rb32(s->pb);
306 2 av_log(s, AV_LOG_TRACE, "packet num %d\n", tmp);
307
308 2 tmp = avio_rb16(s->pb); // unknown
309 2 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
310
311 2 tmp = avio_r8(s->pb); // major version
312 2 tmp2 = avio_r8(s->pb); // minor version
313 2 av_log(s, AV_LOG_TRACE, "version %d.%d\n", tmp, tmp2);
314
315 2 tmp = avio_rb32(s->pb); // unknown
316 2 av_log(s, AV_LOG_TRACE, "unknown %d\n", tmp);
317
318 2 size = atom->size - 8 - (avio_tell(s->pb) - pos);
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 0)
320 return -1;
321 2 ret = av_get_packet(s->pb, pkt, size);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
323 av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
324 return ret;
325 }
326
327 2 pkt->stream_index = 1;
328 2 pkt->dts = dts;
329
330
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (st->codecpar->sample_rate && samples > 0)
331 2 pkt->duration = av_rescale(samples, st->time_base.den, st->codecpar->sample_rate);
332 2 av_log(s, AV_LOG_TRACE, "pkt dts %"PRId64" duration %"PRId64" samples %d sample rate %d\n",
333 2 pkt->dts, pkt->duration, samples, st->codecpar->sample_rate);
334
335 2 return 0;
336 }
337
338 7 static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
339 {
340 7 R3DContext *r3d = s->priv_data;
341 Atom atom;
342 7 int err = 0;
343
344
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 while (!err) {
345
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
7 if (read_atom(s, &atom) < 0) {
346 2 err = -1;
347 2 break;
348 }
349
2/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
5 switch (atom.tag) {
350 3 case MKTAG('R','E','D','V'):
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->streams[0]->discard == AVDISCARD_ALL)
352 goto skip;
353
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (!(err = r3d_read_redv(s, pkt, &atom)))
354 3 return 0;
355 break;
356 2 case MKTAG('R','E','D','A'):
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!r3d->audio_channels)
358 return -1;
359
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 if (s->nb_streams >= 2 && s->streams[1]->discard == AVDISCARD_ALL)
360 goto skip;
361
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!(err = r3d_read_reda(s, pkt, &atom)))
362 2 return 0;
363 break;
364 default:
365 skip:
366 avio_skip(s->pb, atom.size-8);
367 }
368 }
369 2 return err;
370 }
371
372 6782 static int r3d_probe(const AVProbeData *p)
373 {
374
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6781 times.
6782 if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
375 1 return AVPROBE_SCORE_MAX;
376 6781 return 0;
377 }
378
379 static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
380 {
381 AVStream *st = s->streams[0]; // video stream
382 R3DContext *r3d = s->priv_data;
383 int frame_num;
384
385 if (!st->avg_frame_rate.num)
386 return -1;
387
388 frame_num = av_rescale_q(sample_time, st->time_base,
389 av_inv_q(st->avg_frame_rate));
390 av_log(s, AV_LOG_TRACE, "seek frame num %d timestamp %"PRId64"\n",
391 frame_num, sample_time);
392
393 if (frame_num < r3d->video_offsets_count) {
394 if (avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET) < 0)
395 return -1;
396 } else {
397 av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
398 return -1;
399 }
400
401 return 0;
402 }
403
404 const AVInputFormat ff_r3d_demuxer = {
405 .name = "r3d",
406 .long_name = NULL_IF_CONFIG_SMALL("REDCODE R3D"),
407 .priv_data_size = sizeof(R3DContext),
408 .read_probe = r3d_probe,
409 .read_header = r3d_read_header,
410 .read_packet = r3d_read_packet,
411 .read_seek = r3d_seek,
412 };
413