FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_dec.c
Date: 2026-08-11 17:55:23
Exec Total Coverage
Lines: 632 962 65.7%
Functions: 30 32 93.8%
Branches: 369 631 58.5%

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