FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_dec.c
Date: 2025-02-01 04:38:32
Exec Total Coverage
Lines: 666 949 70.2%
Functions: 31 32 96.9%
Branches: 381 615 62.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdbit.h>
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/error.h"
25 #include "libavutil/log.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/pixfmt.h"
30 #include "libavutil/stereo3d.h"
31 #include "libavutil/time.h"
32 #include "libavutil/timestamp.h"
33
34 #include "libavcodec/avcodec.h"
35 #include "libavcodec/codec.h"
36
37 #include "ffmpeg.h"
38
39 typedef struct DecoderPriv {
40 Decoder dec;
41
42 AVCodecContext *dec_ctx;
43
44 AVFrame *frame;
45 AVFrame *frame_tmp_ref;
46 AVPacket *pkt;
47
48 // override output video sample aspect ratio with this value
49 AVRational sar_override;
50
51 AVRational framerate_in;
52
53 // a combination of DECODER_FLAG_*, provided to dec_open()
54 int flags;
55 int apply_cropping;
56
57 enum AVPixelFormat hwaccel_pix_fmt;
58 enum HWAccelID hwaccel_id;
59 enum AVHWDeviceType hwaccel_device_type;
60 enum AVPixelFormat hwaccel_output_format;
61
62 // pts/estimated duration of the last decoded frame
63 // * in decoder timebase for video,
64 // * in last_frame_tb (may change during decoding) for audio
65 int64_t last_frame_pts;
66 int64_t last_frame_duration_est;
67 AVRational last_frame_tb;
68 int64_t last_filter_in_rescale_delta;
69 int last_frame_sample_rate;
70
71 /* previous decoded subtitles */
72 AVFrame *sub_prev[2];
73 AVFrame *sub_heartbeat;
74
75 Scheduler *sch;
76 unsigned sch_idx;
77
78 // this decoder's index in decoders or -1
79 int index;
80 void *log_parent;
81 char log_name[32];
82 char *parent_name;
83
84 // user specified decoder multiview options manually
85 int multiview_user_config;
86
87 struct {
88 ViewSpecifier vs;
89 unsigned out_idx;
90 } *views_requested;
91 int nb_views_requested;
92
93 /* A map of view ID to decoder outputs.
94 * MUST NOT be accessed outside of get_format()/get_buffer() */
95 struct {
96 unsigned id;
97 uintptr_t out_mask;
98 } *view_map;
99 int nb_view_map;
100
101 struct {
102 AVDictionary *opts;
103 const AVCodec *codec;
104 } standalone_init;
105 } DecoderPriv;
106
107 12278 static DecoderPriv *dp_from_dec(Decoder *d)
108 {
109 12278 return (DecoderPriv*)d;
110 }
111
112 // data that is local to the decoder thread and not visible outside of it
113 typedef struct DecThreadContext {
114 AVFrame *frame;
115 AVPacket *pkt;
116 } DecThreadContext;
117
118 7732 void dec_free(Decoder **pdec)
119 {
120 7732 Decoder *dec = *pdec;
121 DecoderPriv *dp;
122
123
2/2
✓ Branch 0 taken 975 times.
✓ Branch 1 taken 6757 times.
7732 if (!dec)
124 975 return;
125 6757 dp = dp_from_dec(dec);
126
127 6757 avcodec_free_context(&dp->dec_ctx);
128
129 6757 av_frame_free(&dp->frame);
130 6757 av_frame_free(&dp->frame_tmp_ref);
131 6757 av_packet_free(&dp->pkt);
132
133 6757 av_dict_free(&dp->standalone_init.opts);
134
135
2/2
✓ Branch 0 taken 13514 times.
✓ Branch 1 taken 6757 times.
20271 for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++)
136 13514 av_frame_free(&dp->sub_prev[i]);
137 6757 av_frame_free(&dp->sub_heartbeat);
138
139 6757 av_freep(&dp->parent_name);
140
141 6757 av_freep(&dp->views_requested);
142 6757 av_freep(&dp->view_map);
143
144 6757 av_freep(pdec);
145 }
146
147 549 static const char *dec_item_name(void *obj)
148 {
149 549 const DecoderPriv *dp = obj;
150
151 549 return dp->log_name;
152 }
153
154 static const AVClass dec_class = {
155 .class_name = "Decoder",
156 .version = LIBAVUTIL_VERSION_INT,
157 .parent_log_context_offset = offsetof(DecoderPriv, log_parent),
158 .item_name = dec_item_name,
159 };
160
161 static int decoder_thread(void *arg);
162
163 6757 static int dec_alloc(DecoderPriv **pdec, Scheduler *sch, int send_end_ts)
164 {
165 DecoderPriv *dp;
166 6757 int ret = 0;
167
168 6757 *pdec = NULL;
169
170 6757 dp = av_mallocz(sizeof(*dp));
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dp)
172 return AVERROR(ENOMEM);
173
174 6757 dp->frame = av_frame_alloc();
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dp->frame)
176 goto fail;
177
178 6757 dp->pkt = av_packet_alloc();
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dp->pkt)
180 goto fail;
181
182 6757 dp->index = -1;
183 6757 dp->dec.class = &dec_class;
184 6757 dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
185 6757 dp->last_frame_pts = AV_NOPTS_VALUE;
186 6757 dp->last_frame_tb = (AVRational){ 1, 1 };
187 6757 dp->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
188
189 6757 ret = sch_add_dec(sch, decoder_thread, dp, send_end_ts);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret < 0)
191 goto fail;
192 6757 dp->sch = sch;
193 6757 dp->sch_idx = ret;
194
195 6757 *pdec = dp;
196
197 6757 return 0;
198 fail:
199 dec_free((Decoder**)&dp);
200 return ret >= 0 ? AVERROR(ENOMEM) : ret;
201 }
202
203 254602 static AVRational audio_samplerate_update(DecoderPriv *dp,
204 const AVFrame *frame)
205 {
206 254602 const int prev = dp->last_frame_tb.den;
207 254602 const int sr = frame->sample_rate;
208
209 AVRational tb_new;
210 int64_t gcd;
211
212
2/2
✓ Branch 0 taken 253398 times.
✓ Branch 1 taken 1204 times.
254602 if (frame->sample_rate == dp->last_frame_sample_rate)
213 253398 goto finish;
214
215 1204 gcd = av_gcd(prev, sr);
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1204 times.
1204 if (prev / gcd >= INT_MAX / sr) {
218 av_log(dp, AV_LOG_WARNING,
219 "Audio timestamps cannot be represented exactly after "
220 "sample rate change: %d -> %d\n", prev, sr);
221
222 // LCM of 192000, 44100, allows to represent all common samplerates
223 tb_new = (AVRational){ 1, 28224000 };
224 } else
225 1204 tb_new = (AVRational){ 1, prev / gcd * sr };
226
227 // keep the frame timebase if it is strictly better than
228 // the samplerate-defined one
229
4/4
✓ Branch 0 taken 1188 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 1082 times.
1204 if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
230
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 65 times.
106 !(frame->time_base.den % tb_new.den))
231 41 tb_new = frame->time_base;
232
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1204 times.
1204 if (dp->last_frame_pts != AV_NOPTS_VALUE)
234 dp->last_frame_pts = av_rescale_q(dp->last_frame_pts,
235 dp->last_frame_tb, tb_new);
236 1204 dp->last_frame_duration_est = av_rescale_q(dp->last_frame_duration_est,
237 dp->last_frame_tb, tb_new);
238
239 1204 dp->last_frame_tb = tb_new;
240 1204 dp->last_frame_sample_rate = frame->sample_rate;
241
242 254602 finish:
243 254602 return dp->last_frame_tb;
244 }
245
246 254602 static void audio_ts_process(DecoderPriv *dp, AVFrame *frame)
247 {
248 254602 AVRational tb_filter = (AVRational){1, frame->sample_rate};
249 AVRational tb;
250 int64_t pts_pred;
251
252 // on samplerate change, choose a new internal timebase for timestamp
253 // generation that can represent timestamps from all the samplerates
254 // seen so far
255 254602 tb = audio_samplerate_update(dp, frame);
256
2/2
✓ Branch 0 taken 253398 times.
✓ Branch 1 taken 1204 times.
254602 pts_pred = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
257 253398 dp->last_frame_pts + dp->last_frame_duration_est;
258
259
2/2
✓ Branch 0 taken 50348 times.
✓ Branch 1 taken 204254 times.
254602 if (frame->pts == AV_NOPTS_VALUE) {
260 50348 frame->pts = pts_pred;
261 50348 frame->time_base = tb;
262
2/2
✓ Branch 0 taken 203063 times.
✓ Branch 1 taken 1191 times.
204254 } else if (dp->last_frame_pts != AV_NOPTS_VALUE &&
263
2/2
✓ Branch 0 taken 613 times.
✓ Branch 1 taken 202450 times.
203063 frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
264 AV_ROUND_UP)) {
265 // there was a gap in timestamps, reset conversion state
266 613 dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
267 }
268
269 254602 frame->pts = av_rescale_delta(frame->time_base, frame->pts,
270 tb, frame->nb_samples,
271 &dp->last_filter_in_rescale_delta, tb);
272
273 254602 dp->last_frame_pts = frame->pts;
274 254602 dp->last_frame_duration_est = av_rescale_q(frame->nb_samples,
275 tb_filter, tb);
276
277 // finally convert to filtering timebase
278 254602 frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
279 254602 frame->duration = frame->nb_samples;
280 254602 frame->time_base = tb_filter;
281 254602 }
282
283 137767 static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame)
284 {
285 137767 const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE;
286 137767 const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED;
287 137767 int64_t codec_duration = 0;
288 // difference between this and last frame's timestamps
289 137767 const int64_t ts_diff =
290
2/2
✓ Branch 0 taken 132257 times.
✓ Branch 1 taken 5510 times.
137767 (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE) ?
291
1/2
✓ Branch 0 taken 137767 times.
✗ Branch 1 not taken.
275534 frame->pts - dp->last_frame_pts : -1;
292
293 // XXX lavf currently makes up frame durations when they are not provided by
294 // the container. As there is no way to reliably distinguish real container
295 // durations from the fake made-up ones, we use heuristics based on whether
296 // the container has timestamps. Eventually lavf should stop making up
297 // durations, then this should be simplified.
298
299 // frame duration is unreliable (typically guessed by lavf) when it is equal
300 // to 1 and the actual duration of the last frame is more than 2x larger
301
4/4
✓ Branch 0 taken 103658 times.
✓ Branch 1 taken 34109 times.
✓ Branch 2 taken 17046 times.
✓ Branch 3 taken 86612 times.
137767 const int duration_unreliable = frame->duration == 1 && ts_diff > 2 * frame->duration;
302
303 // prefer frame duration for containers with timestamps
304
2/2
✓ Branch 0 taken 137373 times.
✓ Branch 1 taken 394 times.
137767 if (fr_forced ||
305
6/6
✓ Branch 0 taken 135730 times.
✓ Branch 1 taken 1643 times.
✓ Branch 2 taken 101962 times.
✓ Branch 3 taken 33768 times.
✓ Branch 4 taken 101366 times.
✓ Branch 5 taken 596 times.
137373 (frame->duration > 0 && !ts_unreliable && !duration_unreliable))
306 101760 return frame->duration;
307
308
4/4
✓ Branch 0 taken 35926 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 5412 times.
✓ Branch 3 taken 30514 times.
36007 if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) {
309 5412 int fields = frame->repeat_pict + 2;
310 5412 AVRational field_rate = av_mul_q(dp->dec_ctx->framerate,
311 5412 (AVRational){ 2, 1 });
312 5412 codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
313 frame->time_base);
314 }
315
316 // prefer codec-layer duration for containers without timestamps
317
4/4
✓ Branch 0 taken 5412 times.
✓ Branch 1 taken 30595 times.
✓ Branch 2 taken 5393 times.
✓ Branch 3 taken 19 times.
36007 if (codec_duration > 0 && ts_unreliable)
318 5393 return codec_duration;
319
320 // when timestamps are available, repeat last frame's actual duration
321 // (i.e. pts difference between this and last frame)
322
2/2
✓ Branch 0 taken 30097 times.
✓ Branch 1 taken 517 times.
30614 if (ts_diff > 0)
323 30097 return ts_diff;
324
325 // try frame/codec duration
326
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 69 times.
517 if (frame->duration > 0)
327 448 return frame->duration;
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (codec_duration > 0)
329 return codec_duration;
330
331 // try average framerate
332
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
69 if (dp->framerate_in.num && dp->framerate_in.den) {
333 43 int64_t d = av_rescale_q(1, av_inv_q(dp->framerate_in),
334 frame->time_base);
335
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (d > 0)
336 43 return d;
337 }
338
339 // last resort is last frame's estimated duration, and 1
340 26 return FFMAX(dp->last_frame_duration_est, 1);
341 }
342
343 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
344 {
345 DecoderPriv *dp = avctx->opaque;
346 AVFrame *output = NULL;
347 enum AVPixelFormat output_format = dp->hwaccel_output_format;
348 int err;
349
350 if (input->format == output_format) {
351 // Nothing to do.
352 return 0;
353 }
354
355 output = av_frame_alloc();
356 if (!output)
357 return AVERROR(ENOMEM);
358
359 output->format = output_format;
360
361 err = av_hwframe_transfer_data(output, input, 0);
362 if (err < 0) {
363 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
364 "output frame: %d.\n", err);
365 goto fail;
366 }
367
368 err = av_frame_copy_props(output, input);
369 if (err < 0) {
370 av_frame_unref(output);
371 goto fail;
372 }
373
374 av_frame_unref(input);
375 av_frame_move_ref(input, output);
376 av_frame_free(&output);
377
378 return 0;
379
380 fail:
381 av_frame_free(&output);
382 return err;
383 }
384
385 137767 static int video_frame_process(DecoderPriv *dp, AVFrame *frame,
386 unsigned *outputs_mask)
387 {
388 #if FFMPEG_OPT_TOP
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137767 times.
137767 if (dp->flags & DECODER_FLAG_TOP_FIELD_FIRST) {
390 av_log(dp, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
391 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
392 }
393 #endif
394
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137767 times.
137767 if (frame->format == dp->hwaccel_pix_fmt) {
396 int err = hwaccel_retrieve_data(dp->dec_ctx, frame);
397 if (err < 0)
398 return err;
399 }
400
401 137767 frame->pts = frame->best_effort_timestamp;
402
403 // forced fixed framerate
404
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 137373 times.
137767 if (dp->flags & DECODER_FLAG_FRAMERATE_FORCED) {
405 394 frame->pts = AV_NOPTS_VALUE;
406 394 frame->duration = 1;
407 394 frame->time_base = av_inv_q(dp->framerate_in);
408 }
409
410 // no timestamp available - extrapolate from previous frame duration
411
2/2
✓ Branch 0 taken 34223 times.
✓ Branch 1 taken 103544 times.
137767 if (frame->pts == AV_NOPTS_VALUE)
412
2/2
✓ Branch 0 taken 33661 times.
✓ Branch 1 taken 562 times.
67884 frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
413 33661 dp->last_frame_pts + dp->last_frame_duration_est;
414
415 // update timestamp history
416 137767 dp->last_frame_duration_est = video_duration_estimate(dp, frame);
417 137767 dp->last_frame_pts = frame->pts;
418 137767 dp->last_frame_tb = frame->time_base;
419
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137767 times.
137767 if (debug_ts) {
421 av_log(dp, AV_LOG_INFO,
422 "decoder -> pts:%s pts_time:%s "
423 "pkt_dts:%s pkt_dts_time:%s "
424 "duration:%s duration_time:%s "
425 "keyframe:%d frame_type:%d time_base:%d/%d\n",
426 av_ts2str(frame->pts),
427 av_ts2timestr(frame->pts, &frame->time_base),
428 av_ts2str(frame->pkt_dts),
429 av_ts2timestr(frame->pkt_dts, &frame->time_base),
430 av_ts2str(frame->duration),
431 av_ts2timestr(frame->duration, &frame->time_base),
432 !!(frame->flags & AV_FRAME_FLAG_KEY), frame->pict_type,
433 frame->time_base.num, frame->time_base.den);
434 }
435
436
2/2
✓ Branch 0 taken 13588 times.
✓ Branch 1 taken 124179 times.
137767 if (dp->sar_override.num)
437 13588 frame->sample_aspect_ratio = dp->sar_override;
438
439
1/2
✓ Branch 0 taken 137767 times.
✗ Branch 1 not taken.
137767 if (dp->apply_cropping) {
440 // lavfi does not require aligned frame data
441 137767 int ret = av_frame_apply_cropping(frame, AV_FRAME_CROP_UNALIGNED);
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137767 times.
137767 if (ret < 0) {
443 av_log(dp, AV_LOG_ERROR, "Error applying decoder cropping\n");
444 return ret;
445 }
446 }
447
448
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 137157 times.
137767 if (frame->opaque)
449 610 *outputs_mask = (uintptr_t)frame->opaque;
450
451 137767 return 0;
452 }
453
454 1 static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
455 {
456 1 int ret = AVERROR_BUG;
457 1 AVSubtitle tmp = {
458 1 .format = src->format,
459 1 .start_display_time = src->start_display_time,
460 1 .end_display_time = src->end_display_time,
461 .num_rects = 0,
462 .rects = NULL,
463 1 .pts = src->pts
464 };
465
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!src->num_rects)
467 goto success;
468
469
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
470 return AVERROR(ENOMEM);
471
472
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int i = 0; i < src->num_rects; i++) {
473 1 AVSubtitleRect *src_rect = src->rects[i];
474 AVSubtitleRect *dst_rect;
475
476
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
477 ret = AVERROR(ENOMEM);
478 goto cleanup;
479 }
480
481 1 tmp.num_rects++;
482
483 1 dst_rect->type = src_rect->type;
484 1 dst_rect->flags = src_rect->flags;
485
486 1 dst_rect->x = src_rect->x;
487 1 dst_rect->y = src_rect->y;
488 1 dst_rect->w = src_rect->w;
489 1 dst_rect->h = src_rect->h;
490 1 dst_rect->nb_colors = src_rect->nb_colors;
491
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (src_rect->text)
493 if (!(dst_rect->text = av_strdup(src_rect->text))) {
494 ret = AVERROR(ENOMEM);
495 goto cleanup;
496 }
497
498
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (src_rect->ass)
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
500 ret = AVERROR(ENOMEM);
501 goto cleanup;
502 }
503
504
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int j = 0; j < 4; j++) {
505 // SUBTITLE_BITMAP images are special in the sense that they
506 // are like PAL8 images. first pointer to data, second to
507 // palette. This makes the size calculation match this.
508 size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 AVPALETTE_SIZE :
510 4 src_rect->h * src_rect->linesize[j];
511
512
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!src_rect->data[j])
513 4 continue;
514
515 if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
516 ret = AVERROR(ENOMEM);
517 goto cleanup;
518 }
519 dst_rect->linesize[j] = src_rect->linesize[j];
520 }
521 }
522
523 1 success:
524 1 *dst = tmp;
525
526 1 return 0;
527
528 cleanup:
529 avsubtitle_free(&tmp);
530
531 return ret;
532 }
533
534 834 static void subtitle_free(void *opaque, uint8_t *data)
535 {
536 834 AVSubtitle *sub = (AVSubtitle*)data;
537 834 avsubtitle_free(sub);
538 834 av_free(sub);
539 834 }
540
541 834 static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
542 {
543 AVBufferRef *buf;
544 AVSubtitle *sub;
545 int ret;
546
547
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 833 times.
834 if (copy) {
548 1 sub = av_mallocz(sizeof(*sub));
549
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
551 av_freep(&sub);
552 return ret;
553 }
554 } else {
555 833 sub = av_memdup(subtitle, sizeof(*subtitle));
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
833 if (!sub)
557 return AVERROR(ENOMEM);
558 833 memset(subtitle, 0, sizeof(*subtitle));
559 }
560
561 834 buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
562 subtitle_free, NULL, 0);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834 times.
834 if (!buf) {
564 avsubtitle_free(sub);
565 av_freep(&sub);
566 return AVERROR(ENOMEM);
567 }
568
569 834 frame->buf[0] = buf;
570
571 834 return 0;
572 }
573
574 834 static int process_subtitle(DecoderPriv *dp, AVFrame *frame)
575 {
576 834 const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
577 834 int ret = 0;
578
579
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 817 times.
834 if (dp->flags & DECODER_FLAG_FIX_SUB_DURATION) {
580 34 AVSubtitle *sub_prev = dp->sub_prev[0]->buf[0] ?
581
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
582 17 int end = 1;
583
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 if (sub_prev) {
584 15 end = av_rescale(subtitle->pts - sub_prev->pts,
585 1000, AV_TIME_BASE);
586
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (end < sub_prev->end_display_time) {
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_log(dp, AV_LOG_DEBUG,
588 "Subtitle duration reduced from %"PRId32" to %d%s\n",
589 sub_prev->end_display_time, end,
590 end <= 0 ? ", dropping it" : "");
591 15 sub_prev->end_display_time = end;
592 }
593 }
594
595 17 av_frame_unref(dp->sub_prev[1]);
596 17 av_frame_move_ref(dp->sub_prev[1], frame);
597
598 17 frame = dp->sub_prev[0];
599
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL;
600
601 17 FFSWAP(AVFrame*, dp->sub_prev[0], dp->sub_prev[1]);
602
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (end <= 0)
604 return 0;
605 }
606
607
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 832 times.
834 if (!subtitle)
608 2 return 0;
609
610 832 ret = sch_dec_send(dp->sch, dp->sch_idx, 0, frame);
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 832 times.
832 if (ret < 0)
612 av_frame_unref(frame);
613
614
1/2
✓ Branch 0 taken 832 times.
✗ Branch 1 not taken.
832 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
615 }
616
617 4 static int fix_sub_duration_heartbeat(DecoderPriv *dp, int64_t signal_pts)
618 {
619 4 int ret = AVERROR_BUG;
620 8 AVSubtitle *prev_subtitle = dp->sub_prev[0]->buf[0] ?
621
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
622 AVSubtitle *subtitle;
623
624
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 if (!(dp->flags & DECODER_FLAG_FIX_SUB_DURATION) || !prev_subtitle ||
625
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts)
626 3 return 0;
627
628 1 av_frame_unref(dp->sub_heartbeat);
629 1 ret = subtitle_wrap_frame(dp->sub_heartbeat, prev_subtitle, 1);
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
631 return ret;
632
633 1 subtitle = (AVSubtitle*)dp->sub_heartbeat->buf[0]->data;
634 1 subtitle->pts = signal_pts;
635
636 1 return process_subtitle(dp, dp->sub_heartbeat);
637 }
638
639 2654 static int transcode_subtitles(DecoderPriv *dp, const AVPacket *pkt,
640 AVFrame *frame)
641 {
642 2654 AVPacket *flush_pkt = NULL;
643 AVSubtitle subtitle;
644 int got_output;
645 int ret;
646
647
4/4
✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 1666 times.
2654 if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT) {
648 948 frame->pts = pkt->pts;
649 948 frame->time_base = pkt->time_base;
650 948 frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT;
651
652 948 ret = sch_dec_send(dp->sch, dp->sch_idx, 0, frame);
653
1/2
✓ Branch 0 taken 948 times.
✗ Branch 1 not taken.
948 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
654
4/4
✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1662 times.
1706 } else if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION) {
655 4 return fix_sub_duration_heartbeat(dp, av_rescale_q(pkt->pts, pkt->time_base,
656 4 AV_TIME_BASE_Q));
657 }
658
659
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1662 times.
1702 if (!pkt) {
660 40 flush_pkt = av_packet_alloc();
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!flush_pkt)
662 return AVERROR(ENOMEM);
663 }
664
665
2/2
✓ Branch 0 taken 1662 times.
✓ Branch 1 taken 40 times.
1702 ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output,
666 pkt ? pkt : flush_pkt);
667 1702 av_packet_free(&flush_pkt);
668
669
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1623 times.
1702 if (ret < 0) {
670 79 av_log(dp, AV_LOG_ERROR, "Error decoding subtitles: %s\n",
671 79 av_err2str(ret));
672 79 dp->dec.decode_errors++;
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 return exit_on_error ? ret : 0;
674 }
675
676
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 833 times.
1623 if (!got_output)
677
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 40 times.
790 return pkt ? 0 : AVERROR_EOF;
678
679 833 dp->dec.frames_decoded++;
680
681 // XXX the queue for transferring data to consumers runs
682 // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
683 // inside the frame
684 // eventually, subtitles should be switched to use AVFrames natively
685 833 ret = subtitle_wrap_frame(frame, &subtitle, 0);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
833 if (ret < 0) {
687 avsubtitle_free(&subtitle);
688 return ret;
689 }
690
691 833 frame->width = dp->dec_ctx->width;
692 833 frame->height = dp->dec_ctx->height;
693
694 833 return process_subtitle(dp, frame);
695 }
696
697 376116 static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame)
698 {
699 376116 AVCodecContext *dec = dp->dec_ctx;
700 376116 const char *type_desc = av_get_media_type_string(dec->codec_type);
701 int ret;
702
703
2/2
✓ Branch 0 taken 2654 times.
✓ Branch 1 taken 373462 times.
376116 if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
704 2654 return transcode_subtitles(dp, pkt, frame);
705
706 // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
707 // reason. This seems like a semi-critical bug. Don't trigger EOF, and
708 // skip the packet.
709
4/4
✓ Branch 0 taken 370171 times.
✓ Branch 1 taken 3291 times.
✓ Branch 2 taken 1480 times.
✓ Branch 3 taken 368691 times.
373462 if (pkt && pkt->size == 0)
710 1480 return 0;
711
712
4/4
✓ Branch 0 taken 368691 times.
✓ Branch 1 taken 3291 times.
✓ Branch 2 taken 58628 times.
✓ Branch 3 taken 310063 times.
371982 if (pkt && (dp->flags & DECODER_FLAG_TS_UNRELIABLE)) {
713 58628 pkt->pts = AV_NOPTS_VALUE;
714 58628 pkt->dts = AV_NOPTS_VALUE;
715 }
716
717
2/2
✓ Branch 0 taken 368691 times.
✓ Branch 1 taken 3291 times.
371982 if (pkt) {
718 368691 FrameData *fd = packet_data(pkt);
719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 368691 times.
368691 if (!fd)
720 return AVERROR(ENOMEM);
721 368691 fd->wallclock[LATENCY_PROBE_DEC_PRE] = av_gettime_relative();
722 }
723
724 371982 ret = avcodec_send_packet(dec, pkt);
725
3/6
✓ Branch 0 taken 371568 times.
✓ Branch 1 taken 414 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 414 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
371982 if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) {
726 // In particular, we don't expect AVERROR(EAGAIN), because we read all
727 // decoded frames with avcodec_receive_frame() until done.
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 if (ret == AVERROR(EAGAIN)) {
729 av_log(dp, AV_LOG_FATAL, "A decoder returned an unexpected error code. "
730 "This is a bug, please report it.\n");
731 return AVERROR_BUG;
732 }
733
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 av_log(dp, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
734 414 pkt ? "packet" : "EOF", av_err2str(ret));
735
736
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 if (ret != AVERROR_EOF) {
737 414 dp->dec.decode_errors++;
738
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 if (!exit_on_error)
739 414 ret = 0;
740 }
741
742 414 return ret;
743 }
744
745 388942 while (1) {
746 FrameData *fd;
747 760510 unsigned outputs_mask = 1;
748
749 760510 av_frame_unref(frame);
750
751 760510 update_benchmark(NULL);
752 760510 ret = avcodec_receive_frame(dec, frame);
753 760510 update_benchmark("decode_%s %s", type_desc, dp->parent_name);
754
755
2/2
✓ Branch 0 taken 364845 times.
✓ Branch 1 taken 395665 times.
760510 if (ret == AVERROR(EAGAIN)) {
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 364845 times.
364845 av_assert0(pkt); // should never happen during flushing
757 371568 return 0;
758
2/2
✓ Branch 0 taken 3290 times.
✓ Branch 1 taken 392375 times.
395665 } else if (ret == AVERROR_EOF) {
759 3290 return ret;
760
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 392369 times.
392375 } else if (ret < 0) {
761 6 av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
762 6 dp->dec.decode_errors++;
763
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (exit_on_error)
765 return ret;
766
767 6 continue;
768 }
769
770
3/4
✓ Branch 0 taken 392332 times.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 392332 times.
392369 if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) {
771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 av_log(dp, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
772 "corrupt decoded frame\n");
773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (exit_on_error)
774 return AVERROR_INVALIDDATA;
775 }
776
777 392369 fd = frame_data(frame);
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392369 times.
392369 if (!fd) {
779 av_frame_unref(frame);
780 return AVERROR(ENOMEM);
781 }
782 392369 fd->dec.pts = frame->pts;
783 392369 fd->dec.tb = dec->pkt_timebase;
784 392369 fd->dec.frame_num = dec->frame_num - 1;
785 392369 fd->bits_per_raw_sample = dec->bits_per_raw_sample;
786
787 392369 fd->wallclock[LATENCY_PROBE_DEC_POST] = av_gettime_relative();
788
789 392369 frame->time_base = dec->pkt_timebase;
790
791 392369 ret = clone_side_data(&frame->side_data, &frame->nb_side_data,
792 392369 dec->decoded_side_data, dec->nb_decoded_side_data, 0);
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392369 times.
392369 if (ret < 0)
794 return ret;
795
796
2/2
✓ Branch 0 taken 254602 times.
✓ Branch 1 taken 137767 times.
392369 if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
797 254602 dp->dec.samples_decoded += frame->nb_samples;
798
799 254602 audio_ts_process(dp, frame);
800 } else {
801 137767 ret = video_frame_process(dp, frame, &outputs_mask);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137767 times.
137767 if (ret < 0) {
803 av_log(dp, AV_LOG_FATAL,
804 "Error while processing the decoded data\n");
805 return ret;
806 }
807 }
808
809 392369 dp->dec.frames_decoded++;
810
811
2/2
✓ Branch 0 taken 392369 times.
✓ Branch 1 taken 388936 times.
781305 for (int i = 0; i < stdc_count_ones(outputs_mask); i++) {
812 392369 AVFrame *to_send = frame;
813 int pos;
814
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392369 times.
392369 av_assert0(outputs_mask);
816
1/2
✓ Branch 0 taken 392369 times.
✗ Branch 1 not taken.
392369 pos = stdc_trailing_zeros(outputs_mask);
817 392369 outputs_mask &= ~(1U << pos);
818
819 // this is not the last output and sch_dec_send() consumes the frame
820 // given to it, so make a temporary reference
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392369 times.
392369 if (outputs_mask) {
822 to_send = dp->frame_tmp_ref;
823 ret = av_frame_ref(to_send, frame);
824 if (ret < 0)
825 return ret;
826 }
827
828 392369 ret = sch_dec_send(dp->sch, dp->sch_idx, pos, to_send);
829
2/2
✓ Branch 0 taken 3433 times.
✓ Branch 1 taken 388936 times.
392369 if (ret < 0) {
830 3433 av_frame_unref(to_send);
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3433 times.
3433 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
832 }
833 }
834 }
835 }
836
837 static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
838 const DecoderOpts *o, AVFrame *param_out);
839
840 1 static int dec_standalone_open(DecoderPriv *dp, const AVPacket *pkt)
841 {
842 DecoderOpts o;
843 const FrameData *fd;
844 char name[16];
845
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pkt->opaque_ref)
847 return AVERROR_BUG;
848 1 fd = (FrameData *)pkt->opaque_ref->data;
849
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fd->par_enc)
851 return AVERROR_BUG;
852
853 1 memset(&o, 0, sizeof(o));
854
855 1 o.par = fd->par_enc;
856 1 o.time_base = pkt->time_base;
857
858 1 o.codec = dp->standalone_init.codec;
859
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!o.codec)
860 1 o.codec = avcodec_find_decoder(o.par->codec_id);
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!o.codec) {
862 const AVCodecDescriptor *desc = avcodec_descriptor_get(o.par->codec_id);
863
864 av_log(dp, AV_LOG_ERROR, "Cannot find a decoder for codec ID '%s'\n",
865 desc ? desc->name : "?");
866 return AVERROR_DECODER_NOT_FOUND;
867 }
868
869 1 snprintf(name, sizeof(name), "dec%d", dp->index);
870 1 o.name = name;
871
872 1 return dec_open(dp, &dp->standalone_init.opts, &o, NULL);
873 }
874
875 6757 static void dec_thread_set_name(const DecoderPriv *dp)
876 {
877 6757 char name[16] = "dec";
878
879
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6756 times.
6757 if (dp->index >= 0)
880 1 av_strlcatf(name, sizeof(name), "%d", dp->index);
881
1/2
✓ Branch 0 taken 6756 times.
✗ Branch 1 not taken.
6756 else if (dp->parent_name)
882 6756 av_strlcat(name, dp->parent_name, sizeof(name));
883
884
2/2
✓ Branch 0 taken 6756 times.
✓ Branch 1 taken 1 times.
6757 if (dp->dec_ctx)
885 6756 av_strlcatf(name, sizeof(name), ":%s", dp->dec_ctx->codec->name);
886
887 6757 ff_thread_setname(name);
888 6757 }
889
890 6757 static void dec_thread_uninit(DecThreadContext *dt)
891 {
892 6757 av_packet_free(&dt->pkt);
893 6757 av_frame_free(&dt->frame);
894
895 6757 memset(dt, 0, sizeof(*dt));
896 6757 }
897
898 6757 static int dec_thread_init(DecThreadContext *dt)
899 {
900 6757 memset(dt, 0, sizeof(*dt));
901
902 6757 dt->frame = av_frame_alloc();
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dt->frame)
904 goto fail;
905
906 6757 dt->pkt = av_packet_alloc();
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dt->pkt)
908 goto fail;
909
910 6757 return 0;
911
912 fail:
913 dec_thread_uninit(dt);
914 return AVERROR(ENOMEM);
915 }
916
917 6757 static int decoder_thread(void *arg)
918 {
919 6757 DecoderPriv *dp = arg;
920 DecThreadContext dt;
921 6757 int ret = 0, input_status = 0;
922
923 6757 ret = dec_thread_init(&dt);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret < 0)
925 goto finish;
926
927 6757 dec_thread_set_name(dp);
928
929
1/2
✓ Branch 0 taken 376116 times.
✗ Branch 1 not taken.
376116 while (!input_status) {
930 int flush_buffers, have_data;
931
932 376116 input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt);
933
2/2
✓ Branch 0 taken 372791 times.
✓ Branch 1 taken 3325 times.
748907 have_data = input_status >= 0 &&
934
3/4
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 371833 times.
✓ Branch 2 taken 958 times.
✗ Branch 3 not taken.
372791 (dt.pkt->buf || dt.pkt->side_data_elems ||
935
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 948 times.
958 (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
936
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
937
4/4
✓ Branch 0 taken 372791 times.
✓ Branch 1 taken 3325 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 372785 times.
376116 flush_buffers = input_status >= 0 && !have_data;
938
2/2
✓ Branch 0 taken 3331 times.
✓ Branch 1 taken 372785 times.
376116 if (!have_data)
939
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3325 times.
3331 av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
940 flush_buffers ? "flush" : "EOF");
941
942 // this is a standalone decoder that has not been initialized yet
943
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 376115 times.
376116 if (!dp->dec_ctx) {
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (flush_buffers)
945 continue;
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (input_status < 0) {
947 av_log(dp, AV_LOG_ERROR,
948 "Cannot initialize a standalone decoder\n");
949 ret = input_status;
950 goto finish;
951 }
952
953 1 ret = dec_standalone_open(dp, dt.pkt);
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
955 goto finish;
956 }
957
958
2/2
✓ Branch 0 taken 372785 times.
✓ Branch 1 taken 3331 times.
376116 ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame);
959
960 376116 av_packet_unref(dt.pkt);
961 376116 av_frame_unref(dt.frame);
962
963 // AVERROR_EOF - EOF from the decoder
964 // AVERROR_EXIT - EOF from the scheduler
965 // we treat them differently when flushing
966
2/2
✓ Branch 0 taken 3433 times.
✓ Branch 1 taken 372683 times.
376116 if (ret == AVERROR_EXIT) {
967 3433 ret = AVERROR_EOF;
968 3433 flush_buffers = 0;
969 }
970
971
2/2
✓ Branch 0 taken 6763 times.
✓ Branch 1 taken 369353 times.
376116 if (ret == AVERROR_EOF) {
972
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6757 times.
6763 av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
973 flush_buffers ? "resetting" : "finishing");
974
975
2/2
✓ Branch 0 taken 6757 times.
✓ Branch 1 taken 6 times.
6763 if (!flush_buffers)
976 6757 break;
977
978 /* report last frame duration to the scheduler */
979
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
980 3 dt.pkt->pts = dp->last_frame_pts + dp->last_frame_duration_est;
981 3 dt.pkt->time_base = dp->last_frame_tb;
982 }
983
984 6 avcodec_flush_buffers(dp->dec_ctx);
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369353 times.
369353 } else if (ret < 0) {
986 av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
987 av_err2str(ret));
988 break;
989 }
990 }
991
992 // EOF is normal thread termination
993
1/2
✓ Branch 0 taken 6757 times.
✗ Branch 1 not taken.
6757 if (ret == AVERROR_EOF)
994 6757 ret = 0;
995
996 // on success send EOF timestamp to our downstreams
997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret >= 0) {
998 float err_rate;
999
1000 6757 av_frame_unref(dt.frame);
1001
1002 6757 dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
1003
2/2
✓ Branch 0 taken 6714 times.
✓ Branch 1 taken 43 times.
6757 dt.frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
1004 6714 dp->last_frame_pts + dp->last_frame_duration_est;
1005 6757 dt.frame->time_base = dp->last_frame_tb;
1006
1007 6757 ret = sch_dec_send(dp->sch, dp->sch_idx, 0, dt.frame);
1008
3/4
✓ Branch 0 taken 3443 times.
✓ Branch 1 taken 3314 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3443 times.
6757 if (ret < 0 && ret != AVERROR_EOF) {
1009 av_log(dp, AV_LOG_FATAL,
1010 "Error signalling EOF timestamp: %s\n", av_err2str(ret));
1011 goto finish;
1012 }
1013 6757 ret = 0;
1014
1015
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ?
1016
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6754 times.
6760 dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f;
1017
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6756 times.
6757 if (err_rate > max_error_rate) {
1018 1 av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
1019 err_rate, max_error_rate);
1020 1 ret = FFMPEG_ERROR_RATE_EXCEEDED;
1021
2/2
✓ Branch 0 taken 6755 times.
✓ Branch 1 taken 1 times.
6756 } else if (err_rate)
1022 1 av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
1023 }
1024
1025 6755 finish:
1026 6757 dec_thread_uninit(&dt);
1027
1028 6757 return ret;
1029 }
1030
1031 5520 int dec_request_view(Decoder *d, const ViewSpecifier *vs,
1032 SchedulerNode *src)
1033 {
1034 5520 DecoderPriv *dp = dp_from_dec(d);
1035 5520 unsigned out_idx = 0;
1036 int ret;
1037
1038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5520 times.
5520 if (dp->multiview_user_config) {
1039 if (!vs || vs->type == VIEW_SPECIFIER_TYPE_NONE) {
1040 *src = SCH_DEC_OUT(dp->sch_idx, 0);
1041 return 0;
1042 }
1043
1044 av_log(dp, AV_LOG_ERROR,
1045 "Manually selecting views with -view_ids cannot be combined "
1046 "with view selection via stream specifiers. It is strongly "
1047 "recommended you always use stream specifiers only.\n");
1048 return AVERROR(EINVAL);
1049 }
1050
1051 // when multiview_user_config is not set, NONE specifier is treated
1052 // as requesting the base view
1053
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 5454 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 13 times.
5520 vs = (vs && vs->type != VIEW_SPECIFIER_TYPE_NONE) ? vs :
1054 5507 &(ViewSpecifier){ .type = VIEW_SPECIFIER_TYPE_IDX, .val = 0 };
1055
1056 // check if the specifier matches an already-existing one
1057
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5516 times.
5526 for (int i = 0; i < dp->nb_views_requested; i++) {
1058 10 const ViewSpecifier *vs1 = &dp->views_requested[i].vs;
1059
1060
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (vs->type == vs1->type &&
1061
3/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
10 (vs->type == VIEW_SPECIFIER_TYPE_ALL || vs->val == vs1->val)) {
1062 4 *src = SCH_DEC_OUT(dp->sch_idx, dp->views_requested[i].out_idx);
1063 4 return 0;
1064 }
1065 }
1066
1067 // we use a bitmask to map view IDs to decoder outputs, which
1068 // limits the number of outputs allowed
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5516 times.
5516 if (dp->nb_views_requested >= sizeof(dp->view_map[0].out_mask) * 8) {
1070 av_log(dp, AV_LOG_ERROR, "Too many view specifiers\n");
1071 return AVERROR(ENOSYS);
1072 }
1073
1074 5516 ret = GROW_ARRAY(dp->views_requested, dp->nb_views_requested);
1075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5516 times.
5516 if (ret < 0)
1076 return ret;
1077
1078
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5510 times.
5516 if (dp->nb_views_requested > 1) {
1079 6 ret = sch_add_dec_output(dp->sch, dp->sch_idx);
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1081 return ret;
1082 6 out_idx = ret;
1083 }
1084
1085 5516 dp->views_requested[dp->nb_views_requested - 1].out_idx = out_idx;
1086 5516 dp->views_requested[dp->nb_views_requested - 1].vs = *vs;
1087
1088 5516 *src = SCH_DEC_OUT(dp->sch_idx,
1089 dp->views_requested[dp->nb_views_requested - 1].out_idx);
1090
1091 5516 return 0;
1092 }
1093
1094 1202 static int multiview_setup(DecoderPriv *dp, AVCodecContext *dec_ctx)
1095 {
1096 1202 unsigned views_wanted = 0;
1097
1098 unsigned nb_view_ids_av, nb_view_ids;
1099 1202 unsigned *view_ids_av = NULL, *view_pos_av = NULL;
1100 1202 int *view_ids = NULL;
1101 int ret;
1102
1103 // no views/only base view were requested - do nothing
1104
1/2
✓ Branch 0 taken 1202 times.
✗ Branch 1 not taken.
1202 if (!dp->nb_views_requested ||
1105
2/2
✓ Branch 0 taken 1193 times.
✓ Branch 1 taken 9 times.
1202 (dp->nb_views_requested == 1 &&
1106
2/2
✓ Branch 0 taken 1192 times.
✓ Branch 1 taken 1 times.
1193 dp->views_requested[0].vs.type == VIEW_SPECIFIER_TYPE_IDX &&
1107
1/2
✓ Branch 0 taken 1192 times.
✗ Branch 1 not taken.
1192 dp->views_requested[0].vs.val == 0))
1108 1192 return 0;
1109
1110 10 av_freep(&dp->view_map);
1111 10 dp->nb_view_map = 0;
1112
1113 // retrieve views available in current CVS
1114 10 ret = av_opt_get_array_size(dec_ctx, "view_ids_available",
1115 AV_OPT_SEARCH_CHILDREN, &nb_view_ids_av);
1116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0) {
1117 av_log(dp, AV_LOG_ERROR,
1118 "Multiview decoding requested, but decoder '%s' does not "
1119 "support it\n", dec_ctx->codec->name);
1120 return AVERROR(ENOSYS);
1121 }
1122
1123
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (nb_view_ids_av) {
1124 unsigned nb_view_pos_av;
1125
1126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (nb_view_ids_av >= sizeof(views_wanted) * 8) {
1127 av_log(dp, AV_LOG_ERROR, "Too many views in video: %u\n", nb_view_ids_av);
1128 ret = AVERROR(ENOSYS);
1129 goto fail;
1130 }
1131
1132 8 view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!view_ids_av) {
1134 ret = AVERROR(ENOMEM);
1135 goto fail;
1136 }
1137
1138 8 ret = av_opt_get_array(dec_ctx, "view_ids_available",
1139 AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
1140 AV_OPT_TYPE_UINT, view_ids_av);
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
1142 goto fail;
1143
1144 8 ret = av_opt_get_array_size(dec_ctx, "view_pos_available",
1145 AV_OPT_SEARCH_CHILDREN, &nb_view_pos_av);
1146
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7 times.
8 if (ret >= 0 && nb_view_pos_av == nb_view_ids_av) {
1147 1 view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
1148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!view_pos_av) {
1149 ret = AVERROR(ENOMEM);
1150 goto fail;
1151 }
1152
1153 1 ret = av_opt_get_array(dec_ctx, "view_pos_available",
1154 AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
1155 AV_OPT_TYPE_UINT, view_pos_av);
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1157 goto fail;
1158 }
1159 } else {
1160 // assume there is a single view with ID=0
1161 2 nb_view_ids_av = 1;
1162 2 view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
1163 2 view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
1164
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!view_ids_av || !view_pos_av) {
1165 ret = AVERROR(ENOMEM);
1166 goto fail;
1167 }
1168 2 view_pos_av[0] = AV_STEREO3D_VIEW_UNSPEC;
1169 }
1170
1171 10 dp->view_map = av_calloc(nb_view_ids_av, sizeof(*dp->view_map));
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!dp->view_map) {
1173 ret = AVERROR(ENOMEM);
1174 goto fail;
1175 }
1176 10 dp->nb_view_map = nb_view_ids_av;
1177
1178
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 for (int i = 0; i < dp->nb_view_map; i++)
1179 18 dp->view_map[i].id = view_ids_av[i];
1180
1181 // figure out which views should go to which output
1182
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 10 times.
29 for (int i = 0; i < dp->nb_views_requested; i++) {
1183 19 const ViewSpecifier *vs = &dp->views_requested[i].vs;
1184
1185
4/5
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
19 switch (vs->type) {
1186 8 case VIEW_SPECIFIER_TYPE_IDX:
1187
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (vs->val >= nb_view_ids_av) {
1188 2 av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
1189 "View with index %u requested, but only %u views available "
1190 "in current video sequence (more views may or may not be "
1191 "available in later sequences).\n",
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 vs->val, nb_view_ids_av);
1193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (exit_on_error) {
1194 ret = AVERROR(EINVAL);
1195 goto fail;
1196 }
1197
1198 2 continue;
1199 }
1200 6 views_wanted |= 1U << vs->val;
1201 6 dp->view_map[vs->val].out_mask |= 1ULL << i;
1202
1203 6 break;
1204 8 case VIEW_SPECIFIER_TYPE_ID: {
1205 8 int view_idx = -1;
1206
1207
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (unsigned j = 0; j < nb_view_ids_av; j++) {
1208
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (view_ids_av[j] == vs->val) {
1209 8 view_idx = j;
1210 8 break;
1211 }
1212 }
1213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (view_idx < 0) {
1214 av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
1215 "View with ID %u requested, but is not available "
1216 "in the video sequence\n", vs->val);
1217 if (exit_on_error) {
1218 ret = AVERROR(EINVAL);
1219 goto fail;
1220 }
1221
1222 continue;
1223 }
1224 8 views_wanted |= 1U << view_idx;
1225 8 dp->view_map[view_idx].out_mask |= 1ULL << i;
1226
1227 8 break;
1228 }
1229 2 case VIEW_SPECIFIER_TYPE_POS: {
1230 2 int view_idx = -1;
1231
1232
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 for (unsigned j = 0; view_pos_av && j < nb_view_ids_av; j++) {
1233
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (view_pos_av[j] == vs->val) {
1234 2 view_idx = j;
1235 2 break;
1236 }
1237 }
1238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (view_idx < 0) {
1239 av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
1240 "View position '%s' requested, but is not available "
1241 "in the video sequence\n", av_stereo3d_view_name(vs->val));
1242 if (exit_on_error) {
1243 ret = AVERROR(EINVAL);
1244 goto fail;
1245 }
1246
1247 continue;
1248 }
1249 2 views_wanted |= 1U << view_idx;
1250 2 dp->view_map[view_idx].out_mask |= 1ULL << i;
1251
1252 2 break;
1253 }
1254 1 case VIEW_SPECIFIER_TYPE_ALL:
1255 1 views_wanted |= (1U << nb_view_ids_av) - 1;
1256
1257
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int j = 0; j < dp->nb_view_map; j++)
1258 2 dp->view_map[j].out_mask |= 1ULL << i;
1259
1260 1 break;
1261 }
1262 }
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!views_wanted) {
1264 av_log(dp, AV_LOG_ERROR, "No views were selected for decoding\n");
1265 ret = AVERROR(EINVAL);
1266 goto fail;
1267 }
1268
1269 // signal to decoder which views we want
1270 10 nb_view_ids = stdc_count_ones(views_wanted);
1271 10 view_ids = av_malloc_array(nb_view_ids, sizeof(*view_ids));
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!view_ids) {
1273 ret = AVERROR(ENOMEM);
1274 goto fail;
1275 }
1276
1277
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 for (unsigned i = 0; i < nb_view_ids; i++) {
1278 int pos;
1279
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 av_assert0(views_wanted);
1281
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 pos = stdc_trailing_zeros(views_wanted);
1282 18 views_wanted &= ~(1U << pos);
1283
1284 18 view_ids[i] = view_ids_av[pos];
1285 }
1286
1287 // unset view_ids in case we set it earlier
1288 10 av_opt_set(dec_ctx, "view_ids", NULL, AV_OPT_SEARCH_CHILDREN);
1289
1290 10 ret = av_opt_set_array(dec_ctx, "view_ids", AV_OPT_SEARCH_CHILDREN,
1291 0, nb_view_ids, AV_OPT_TYPE_INT, view_ids);
1292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
1293 goto fail;
1294
1295
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7 times.
10 if (!dp->frame_tmp_ref) {
1296 7 dp->frame_tmp_ref = av_frame_alloc();
1297
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (!dp->frame_tmp_ref) {
1298 ret = AVERROR(ENOMEM);
1299 goto fail;
1300 }
1301 }
1302
1303 10 fail:
1304 10 av_freep(&view_ids_av);
1305 10 av_freep(&view_pos_av);
1306 10 av_freep(&view_ids);
1307
1308 10 return ret;
1309 }
1310
1311 6757 static void multiview_check_manual(DecoderPriv *dp, const AVDictionary *dec_opts)
1312 {
1313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6757 times.
6757 if (av_dict_get(dec_opts, "view_ids", NULL, 0)) {
1314 av_log(dp, AV_LOG_WARNING, "Manually selecting views with -view_ids "
1315 "is not recommended, use view specifiers instead\n");
1316 dp->multiview_user_config = 1;
1317 }
1318 6757 }
1319
1320 1202 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
1321 {
1322 1202 DecoderPriv *dp = s->opaque;
1323 const enum AVPixelFormat *p;
1324 int ret;
1325
1326 1202 ret = multiview_setup(dp, s);
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1202 times.
1202 if (ret < 0) {
1328 av_log(dp, AV_LOG_ERROR, "Error setting up multiview decoding: %s\n",
1329 av_err2str(ret));
1330 return AV_PIX_FMT_NONE;
1331 }
1332
1333
1/2
✓ Branch 0 taken 2914 times.
✗ Branch 1 not taken.
2914 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
1334 2914 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
1335 2914 const AVCodecHWConfig *config = NULL;
1336
1337
2/2
✓ Branch 0 taken 1202 times.
✓ Branch 1 taken 1712 times.
2914 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1338 1202 break;
1339
1340
1/2
✓ Branch 0 taken 1712 times.
✗ Branch 1 not taken.
1712 if (dp->hwaccel_id == HWACCEL_GENERIC ||
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1712 times.
1712 dp->hwaccel_id == HWACCEL_AUTO) {
1342 for (int i = 0;; i++) {
1343 config = avcodec_get_hw_config(s->codec, i);
1344 if (!config)
1345 break;
1346 if (!(config->methods &
1347 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1348 continue;
1349 if (config->pix_fmt == *p)
1350 break;
1351 }
1352 }
1353
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1712 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1712 if (config && config->device_type == dp->hwaccel_device_type) {
1354 dp->hwaccel_pix_fmt = *p;
1355 break;
1356 }
1357 }
1358
1359 1202 return *p;
1360 }
1361
1362 367388 static int get_buffer(AVCodecContext *dec_ctx, AVFrame *frame, int flags)
1363 {
1364 367388 DecoderPriv *dp = dec_ctx->opaque;
1365
1366 // for multiview video, store the output mask in frame opaque
1367
2/2
✓ Branch 0 taken 613 times.
✓ Branch 1 taken 366775 times.
367388 if (dp->nb_view_map) {
1368 613 const AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIEW_ID);
1369
2/2
✓ Branch 0 taken 554 times.
✓ Branch 1 taken 59 times.
613 int view_id = sd ? *(int*)sd->data : 0;
1370
1371
1/2
✓ Branch 0 taken 890 times.
✗ Branch 1 not taken.
890 for (int i = 0; i < dp->nb_view_map; i++) {
1372
2/2
✓ Branch 0 taken 613 times.
✓ Branch 1 taken 277 times.
890 if (dp->view_map[i].id == view_id) {
1373 613 frame->opaque = (void*)dp->view_map[i].out_mask;
1374 613 break;
1375 }
1376 }
1377 }
1378
1379 367388 return avcodec_default_get_buffer2(dec_ctx, frame, flags);
1380 }
1381
1382 6757 static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
1383 {
1384 const AVCodecHWConfig *config;
1385 HWDevice *dev;
1386 6757 for (int i = 0;; i++) {
1387 8460 config = avcodec_get_hw_config(codec, i);
1388
2/2
✓ Branch 0 taken 6757 times.
✓ Branch 1 taken 1703 times.
8460 if (!config)
1389 6757 return NULL;
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1703 times.
1703 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1391 continue;
1392 1703 dev = hw_device_get_by_type(config->device_type);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1703 times.
1703 if (dev)
1394 return dev;
1395 }
1396 }
1397
1398 6757 static int hw_device_setup_for_decode(DecoderPriv *dp,
1399 const AVCodec *codec,
1400 const char *hwaccel_device)
1401 {
1402 const AVCodecHWConfig *config;
1403 enum AVHWDeviceType type;
1404 6757 HWDevice *dev = NULL;
1405 6757 int err, auto_device = 0;
1406
1407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (hwaccel_device) {
1408 dev = hw_device_get_by_name(hwaccel_device);
1409 if (!dev) {
1410 if (dp->hwaccel_id == HWACCEL_AUTO) {
1411 auto_device = 1;
1412 } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
1413 type = dp->hwaccel_device_type;
1414 err = hw_device_init_from_type(type, hwaccel_device,
1415 &dev);
1416 } else {
1417 // This will be dealt with by API-specific initialisation
1418 // (using hwaccel_device), so nothing further needed here.
1419 return 0;
1420 }
1421 } else {
1422 if (dp->hwaccel_id == HWACCEL_AUTO) {
1423 dp->hwaccel_device_type = dev->type;
1424 } else if (dp->hwaccel_device_type != dev->type) {
1425 av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device "
1426 "specified for decoder: device %s of type %s is not "
1427 "usable with hwaccel %s.\n", dev->name,
1428 av_hwdevice_get_type_name(dev->type),
1429 av_hwdevice_get_type_name(dp->hwaccel_device_type));
1430 return AVERROR(EINVAL);
1431 }
1432 }
1433 } else {
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (dp->hwaccel_id == HWACCEL_AUTO) {
1435 auto_device = 1;
1436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
1437 type = dp->hwaccel_device_type;
1438 dev = hw_device_get_by_type(type);
1439
1440 // When "-qsv_device device" is used, an internal QSV device named
1441 // as "__qsv_device" is created. Another QSV device is created too
1442 // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
1443 // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
1444 // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
1445 // To keep back-compatibility with the removed ad-hoc libmfx setup code,
1446 // call hw_device_get_by_name("__qsv_device") to select the internal QSV
1447 // device.
1448 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
1449 dev = hw_device_get_by_name("__qsv_device");
1450
1451 if (!dev)
1452 err = hw_device_init_from_type(type, NULL, &dev);
1453 } else {
1454 6757 dev = hw_device_match_by_codec(codec);
1455
1/2
✓ Branch 0 taken 6757 times.
✗ Branch 1 not taken.
6757 if (!dev) {
1456 // No device for this codec, but not using generic hwaccel
1457 // and therefore may well not need one - ignore.
1458 6757 return 0;
1459 }
1460 }
1461 }
1462
1463 if (auto_device) {
1464 if (!avcodec_get_hw_config(codec, 0)) {
1465 // Decoder does not support any hardware devices.
1466 return 0;
1467 }
1468 for (int i = 0; !dev; i++) {
1469 config = avcodec_get_hw_config(codec, i);
1470 if (!config)
1471 break;
1472 type = config->device_type;
1473 dev = hw_device_get_by_type(type);
1474 if (dev) {
1475 av_log(dp, AV_LOG_INFO, "Using auto "
1476 "hwaccel type %s with existing device %s.\n",
1477 av_hwdevice_get_type_name(type), dev->name);
1478 }
1479 }
1480 for (int i = 0; !dev; i++) {
1481 config = avcodec_get_hw_config(codec, i);
1482 if (!config)
1483 break;
1484 type = config->device_type;
1485 // Try to make a new device of this type.
1486 err = hw_device_init_from_type(type, hwaccel_device,
1487 &dev);
1488 if (err < 0) {
1489 // Can't make a device of this type.
1490 continue;
1491 }
1492 if (hwaccel_device) {
1493 av_log(dp, AV_LOG_INFO, "Using auto "
1494 "hwaccel type %s with new device created "
1495 "from %s.\n", av_hwdevice_get_type_name(type),
1496 hwaccel_device);
1497 } else {
1498 av_log(dp, AV_LOG_INFO, "Using auto "
1499 "hwaccel type %s with new default device.\n",
1500 av_hwdevice_get_type_name(type));
1501 }
1502 }
1503 if (dev) {
1504 dp->hwaccel_device_type = type;
1505 } else {
1506 av_log(dp, AV_LOG_INFO, "Auto hwaccel "
1507 "disabled: no device found.\n");
1508 dp->hwaccel_id = HWACCEL_NONE;
1509 return 0;
1510 }
1511 }
1512
1513 if (!dev) {
1514 av_log(dp, AV_LOG_ERROR, "No device available "
1515 "for decoder: device type %s needed for codec %s.\n",
1516 av_hwdevice_get_type_name(type), codec->name);
1517 return err;
1518 }
1519
1520 dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
1521 if (!dp->dec_ctx->hw_device_ctx)
1522 return AVERROR(ENOMEM);
1523
1524 return 0;
1525 }
1526
1527 6757 static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
1528 const DecoderOpts *o, AVFrame *param_out)
1529 {
1530 6757 const AVCodec *codec = o->codec;
1531 int ret;
1532
1533 6757 dp->flags = o->flags;
1534 6757 dp->log_parent = o->log_parent;
1535
1536 6757 dp->dec.type = codec->type;
1537 6757 dp->framerate_in = o->framerate;
1538
1539 6757 dp->hwaccel_id = o->hwaccel_id;
1540 6757 dp->hwaccel_device_type = o->hwaccel_device_type;
1541 6757 dp->hwaccel_output_format = o->hwaccel_output_format;
1542
1543 6757 snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
1544
1545
1/2
✓ Branch 0 taken 6757 times.
✗ Branch 1 not taken.
6757 dp->parent_name = av_strdup(o->name ? o->name : "");
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dp->parent_name)
1547 return AVERROR(ENOMEM);
1548
1549
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 6717 times.
6757 if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
1550
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
40 (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) {
1551
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) {
1552 4 dp->sub_prev[i] = av_frame_alloc();
1553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!dp->sub_prev[i])
1554 return AVERROR(ENOMEM);
1555 }
1556 2 dp->sub_heartbeat = av_frame_alloc();
1557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!dp->sub_heartbeat)
1558 return AVERROR(ENOMEM);
1559 }
1560
1561 6757 dp->sar_override = o->par->sample_aspect_ratio;
1562
1563 6757 dp->dec_ctx = avcodec_alloc_context3(codec);
1564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (!dp->dec_ctx)
1565 return AVERROR(ENOMEM);
1566
1567 6757 ret = avcodec_parameters_to_context(dp->dec_ctx, o->par);
1568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret < 0) {
1569 av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1570 return ret;
1571 }
1572
1573 6757 dp->dec_ctx->opaque = dp;
1574 6757 dp->dec_ctx->get_format = get_format;
1575 6757 dp->dec_ctx->get_buffer2 = get_buffer;
1576 6757 dp->dec_ctx->pkt_timebase = o->time_base;
1577
1578
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 6700 times.
6757 if (!av_dict_get(*dec_opts, "threads", NULL, 0))
1579 57 av_dict_set(dec_opts, "threads", "auto", 0);
1580
1581 6757 ret = hw_device_setup_for_decode(dp, codec, o->hwaccel_device);
1582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret < 0) {
1583 av_log(dp, AV_LOG_ERROR,
1584 "Hardware device setup failed for decoder: %s\n",
1585 av_err2str(ret));
1586 return ret;
1587 }
1588
1589 6757 ret = av_opt_set_dict2(dp->dec_ctx, dec_opts, AV_OPT_SEARCH_CHILDREN);
1590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret < 0) {
1591 av_log(dp, AV_LOG_ERROR, "Error applying decoder options: %s\n",
1592 av_err2str(ret));
1593 return ret;
1594 }
1595 6757 ret = check_avoptions(*dec_opts);
1596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (ret < 0)
1597 return ret;
1598
1599 6757 dp->dec_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
1600
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 6679 times.
6757 if (o->flags & DECODER_FLAG_BITEXACT)
1601 78 dp->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1602
1603 // we apply cropping outselves
1604 6757 dp->apply_cropping = dp->dec_ctx->apply_cropping;
1605 6757 dp->dec_ctx->apply_cropping = 0;
1606
1607
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6757 times.
6757 if ((ret = avcodec_open2(dp->dec_ctx, codec, NULL)) < 0) {
1608 av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
1609 av_err2str(ret));
1610 return ret;
1611 }
1612
1613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6757 times.
6757 if (dp->dec_ctx->hw_device_ctx) {
1614 // Update decoder extra_hw_frames option to account for the
1615 // frames held in queues inside the ffmpeg utility. This is
1616 // called after avcodec_open2() because the user-set value of
1617 // extra_hw_frames becomes valid in there, and we need to add
1618 // this on top of it.
1619 int extra_frames = DEFAULT_FRAME_THREAD_QUEUE_SIZE;
1620 if (dp->dec_ctx->extra_hw_frames >= 0)
1621 dp->dec_ctx->extra_hw_frames += extra_frames;
1622 else
1623 dp->dec_ctx->extra_hw_frames = extra_frames;
1624 }
1625
1626 6757 dp->dec.subtitle_header = dp->dec_ctx->subtitle_header;
1627 6757 dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
1628
1629
2/2
✓ Branch 0 taken 6756 times.
✓ Branch 1 taken 1 times.
6757 if (param_out) {
1630
2/2
✓ Branch 0 taken 1207 times.
✓ Branch 1 taken 5549 times.
6756 if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1631 1207 param_out->format = dp->dec_ctx->sample_fmt;
1632 1207 param_out->sample_rate = dp->dec_ctx->sample_rate;
1633
1634 1207 ret = av_channel_layout_copy(&param_out->ch_layout, &dp->dec_ctx->ch_layout);
1635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1207 times.
1207 if (ret < 0)
1636 return ret;
1637
2/2
✓ Branch 0 taken 5509 times.
✓ Branch 1 taken 40 times.
5549 } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1638 5509 param_out->format = dp->dec_ctx->pix_fmt;
1639 5509 param_out->width = dp->dec_ctx->width;
1640 5509 param_out->height = dp->dec_ctx->height;
1641 5509 param_out->sample_aspect_ratio = dp->dec_ctx->sample_aspect_ratio;
1642 5509 param_out->colorspace = dp->dec_ctx->colorspace;
1643 5509 param_out->color_range = dp->dec_ctx->color_range;
1644 }
1645
1646 6756 av_frame_side_data_free(&param_out->side_data, &param_out->nb_side_data);
1647 6756 ret = clone_side_data(&param_out->side_data, &param_out->nb_side_data,
1648 6756 dp->dec_ctx->decoded_side_data, dp->dec_ctx->nb_decoded_side_data, 0);
1649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
6756 if (ret < 0)
1650 return ret;
1651 6756 param_out->time_base = dp->dec_ctx->pkt_timebase;
1652 }
1653
1654 6757 return 0;
1655 }
1656
1657 6756 int dec_init(Decoder **pdec, Scheduler *sch,
1658 AVDictionary **dec_opts, const DecoderOpts *o,
1659 AVFrame *param_out)
1660 {
1661 DecoderPriv *dp;
1662 int ret;
1663
1664 6756 *pdec = NULL;
1665
1666 6756 ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
6756 if (ret < 0)
1668 return ret;
1669
1670 6756 multiview_check_manual(dp, *dec_opts);
1671
1672 6756 ret = dec_open(dp, dec_opts, o, param_out);
1673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
6756 if (ret < 0)
1674 goto fail;
1675
1676 6756 *pdec = &dp->dec;
1677
1678 6756 return dp->sch_idx;
1679 fail:
1680 dec_free((Decoder**)&dp);
1681 return ret;
1682 }
1683
1684 1 int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch)
1685 {
1686 DecoderPriv *dp;
1687
1688 OutputFile *of;
1689 OutputStream *ost;
1690 int of_index, ost_index;
1691 char *p;
1692
1693 unsigned enc_idx;
1694 int ret;
1695
1696 1 ret = dec_alloc(&dp, sch, 0);
1697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1698 return ret;
1699
1700 1 dp->index = nb_decoders;
1701
1702 1 ret = GROW_ARRAY(decoders, nb_decoders);
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1704 dec_free((Decoder **)&dp);
1705 return ret;
1706 }
1707
1708 1 decoders[nb_decoders - 1] = (Decoder *)dp;
1709
1710 1 of_index = strtol(arg, &p, 0);
1711
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (of_index < 0 || of_index >= nb_output_files) {
1712 av_log(dp, AV_LOG_ERROR, "Invalid output file index '%d' in %s\n", of_index, arg);
1713 return AVERROR(EINVAL);
1714 }
1715 1 of = output_files[of_index];
1716
1717 1 ost_index = strtol(p + 1, NULL, 0);
1718
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (ost_index < 0 || ost_index >= of->nb_streams) {
1719 av_log(dp, AV_LOG_ERROR, "Invalid output stream index '%d' in %s\n", ost_index, arg);
1720 return AVERROR(EINVAL);
1721 }
1722 1 ost = of->streams[ost_index];
1723
1724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ost->enc) {
1725 av_log(dp, AV_LOG_ERROR, "Output stream %s has no encoder\n", arg);
1726 return AVERROR(EINVAL);
1727 }
1728
1729 1 dp->dec.type = ost->type;
1730
1731 1 ret = enc_loopback(ost->enc);
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1733 return ret;
1734 1 enc_idx = ret;
1735
1736 1 ret = sch_connect(sch, SCH_ENC(enc_idx), SCH_DEC_IN(dp->sch_idx));
1737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1738 return ret;
1739
1740 1 ret = av_dict_copy(&dp->standalone_init.opts, o->g->codec_opts, 0);
1741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1742 return ret;
1743
1744 1 multiview_check_manual(dp, dp->standalone_init.opts);
1745
1746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (o->codec_names.nb_opt) {
1747 const char *name = o->codec_names.opt[o->codec_names.nb_opt - 1].u.str;
1748 dp->standalone_init.codec = avcodec_find_decoder_by_name(name);
1749 if (!dp->standalone_init.codec) {
1750 av_log(dp, AV_LOG_ERROR, "No such decoder: %s\n", name);
1751 return AVERROR_DECODER_NOT_FOUND;
1752 }
1753 }
1754
1755 1 return 0;
1756 }
1757
1758 1 int dec_filter_add(Decoder *d, InputFilter *ifilter, InputFilterOptions *opts,
1759 const ViewSpecifier *vs, SchedulerNode *src)
1760 {
1761 1 DecoderPriv *dp = dp_from_dec(d);
1762 char name[16];
1763
1764 1 snprintf(name, sizeof(name), "dec%d", dp->index);
1765 1 opts->name = av_strdup(name);
1766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!opts->name)
1767 return AVERROR(ENOMEM);
1768
1769 1 return dec_request_view(d, vs, src);
1770 }
1771