FFmpeg coverage


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