FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_dec.c
Date: 2025-11-10 11:20:04
Exec Total Coverage
Lines: 663 947 70.0%
Functions: 31 32 96.9%
Branches: 382 613 62.3%

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 12619 static DecoderPriv *dp_from_dec(Decoder *d)
108 {
109 12619 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 7950 void dec_free(Decoder **pdec)
119 {
120 7950 Decoder *dec = *pdec;
121 DecoderPriv *dp;
122
123
2/2
✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 6933 times.
7950 if (!dec)
124 1017 return;
125 6933 dp = dp_from_dec(dec);
126
127 6933 avcodec_free_context(&dp->dec_ctx);
128
129 6933 av_frame_free(&dp->frame);
130 6933 av_frame_free(&dp->frame_tmp_ref);
131 6933 av_packet_free(&dp->pkt);
132
133 6933 av_dict_free(&dp->standalone_init.opts);
134
135
2/2
✓ Branch 0 taken 13866 times.
✓ Branch 1 taken 6933 times.
20799 for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++)
136 13866 av_frame_free(&dp->sub_prev[i]);
137 6933 av_frame_free(&dp->sub_heartbeat);
138
139 6933 av_freep(&dp->parent_name);
140
141 6933 av_freep(&dp->views_requested);
142 6933 av_freep(&dp->view_map);
143
144 6933 av_freep(pdec);
145 }
146
147 572 static const char *dec_item_name(void *obj)
148 {
149 572 const DecoderPriv *dp = obj;
150
151 572 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 6933 static int dec_alloc(DecoderPriv **pdec, Scheduler *sch, int send_end_ts)
164 {
165 DecoderPriv *dp;
166 6933 int ret = 0;
167
168 6933 *pdec = NULL;
169
170 6933 dp = av_mallocz(sizeof(*dp));
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dp)
172 return AVERROR(ENOMEM);
173
174 6933 dp->frame = av_frame_alloc();
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dp->frame)
176 goto fail;
177
178 6933 dp->pkt = av_packet_alloc();
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dp->pkt)
180 goto fail;
181
182 6933 dp->index = -1;
183 6933 dp->dec.class = &dec_class;
184 6933 dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
185 6933 dp->last_frame_pts = AV_NOPTS_VALUE;
186 6933 dp->last_frame_tb = (AVRational){ 1, 1 };
187 6933 dp->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
188
189 6933 ret = sch_add_dec(sch, decoder_thread, dp, send_end_ts);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret < 0)
191 goto fail;
192 6933 dp->sch = sch;
193 6933 dp->sch_idx = ret;
194
195 6933 *pdec = dp;
196
197 6933 return 0;
198 fail:
199 dec_free((Decoder**)&dp);
200 return ret >= 0 ? AVERROR(ENOMEM) : ret;
201 }
202
203 260259 static AVRational audio_samplerate_update(DecoderPriv *dp,
204 const AVFrame *frame)
205 {
206 260259 const int prev = dp->last_frame_tb.den;
207 260259 const int sr = frame->sample_rate;
208
209 AVRational tb_new;
210 int64_t gcd;
211
212
2/2
✓ Branch 0 taken 259048 times.
✓ Branch 1 taken 1211 times.
260259 if (frame->sample_rate == dp->last_frame_sample_rate)
213 259048 goto finish;
214
215 1211 gcd = av_gcd(prev, sr);
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1211 times.
1211 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 1211 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 1195 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 105 times.
✓ Branch 3 taken 1090 times.
1211 if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
230
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 65 times.
105 !(frame->time_base.den % tb_new.den))
231 40 tb_new = frame->time_base;
232
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1211 times.
1211 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 1211 dp->last_frame_duration_est = av_rescale_q(dp->last_frame_duration_est,
237 dp->last_frame_tb, tb_new);
238
239 1211 dp->last_frame_tb = tb_new;
240 1211 dp->last_frame_sample_rate = frame->sample_rate;
241
242 260259 finish:
243 260259 return dp->last_frame_tb;
244 }
245
246 260259 static void audio_ts_process(DecoderPriv *dp, AVFrame *frame)
247 {
248 260259 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 260259 tb = audio_samplerate_update(dp, frame);
256
2/2
✓ Branch 0 taken 259048 times.
✓ Branch 1 taken 1211 times.
260259 pts_pred = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
257 259048 dp->last_frame_pts + dp->last_frame_duration_est;
258
259
2/2
✓ Branch 0 taken 50468 times.
✓ Branch 1 taken 209791 times.
260259 if (frame->pts == AV_NOPTS_VALUE) {
260 50468 frame->pts = pts_pred;
261 50468 frame->time_base = tb;
262
2/2
✓ Branch 0 taken 208596 times.
✓ Branch 1 taken 1195 times.
209791 } else if (dp->last_frame_pts != AV_NOPTS_VALUE &&
263
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 207986 times.
208596 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 610 dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
267 }
268
269 260259 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 260259 dp->last_frame_pts = frame->pts;
274 260259 dp->last_frame_duration_est = av_rescale_q(frame->nb_samples,
275 tb_filter, tb);
276
277 // finally convert to filtering timebase
278 260259 frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
279 260259 frame->duration = frame->nb_samples;
280 260259 frame->time_base = tb_filter;
281 260259 }
282
283 125988 static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame)
284 {
285 125988 const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE;
286 125988 const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED;
287 125988 int64_t codec_duration = 0;
288 // difference between this and last frame's timestamps
289 125988 const int64_t ts_diff =
290
2/2
✓ Branch 0 taken 120313 times.
✓ Branch 1 taken 5675 times.
125988 (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE) ?
291
1/2
✓ Branch 0 taken 125988 times.
✗ Branch 1 not taken.
251976 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 91695 times.
✓ Branch 1 taken 34293 times.
✓ Branch 2 taken 17059 times.
✓ Branch 3 taken 74636 times.
125988 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 125770 times.
✓ Branch 1 taken 218 times.
125988 if (fr_forced ||
305
6/6
✓ Branch 0 taken 124138 times.
✓ Branch 1 taken 1632 times.
✓ Branch 2 taken 90256 times.
✓ Branch 3 taken 33882 times.
✓ Branch 4 taken 89661 times.
✓ Branch 5 taken 595 times.
125770 (frame->duration > 0 && !ts_unreliable && !duration_unreliable))
306 89879 return frame->duration;
307
308
3/4
✓ Branch 0 taken 36109 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5414 times.
✓ Branch 3 taken 30695 times.
36109 if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) {
309 5414 int fields = frame->repeat_pict + 2;
310 5414 AVRational field_rate = av_mul_q(dp->dec_ctx->framerate,
311 5414 (AVRational){ 2, 1 });
312 5414 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 5414 times.
✓ Branch 1 taken 30695 times.
✓ Branch 2 taken 5395 times.
✓ Branch 3 taken 19 times.
36109 if (codec_duration > 0 && ts_unreliable)
318 5395 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 30190 times.
✓ Branch 1 taken 524 times.
30714 if (ts_diff > 0)
323 30190 return ts_diff;
324
325 // try frame/codec duration
326
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 69 times.
524 if (frame->duration > 0)
327 455 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 125988 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 125988 times.
125988 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 125988 times.
125988 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 125988 frame->pts = frame->best_effort_timestamp;
402
403 // forced fixed framerate
404
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 125770 times.
125988 if (dp->flags & DECODER_FLAG_FRAMERATE_FORCED) {
405 218 frame->pts = AV_NOPTS_VALUE;
406 218 frame->duration = 1;
407 218 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 34161 times.
✓ Branch 1 taken 91827 times.
125988 if (frame->pts == AV_NOPTS_VALUE)
412
2/2
✓ Branch 0 taken 33592 times.
✓ Branch 1 taken 569 times.
67753 frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
413 33592 dp->last_frame_pts + dp->last_frame_duration_est;
414
415 // update timestamp history
416 125988 dp->last_frame_duration_est = video_duration_estimate(dp, frame);
417 125988 dp->last_frame_pts = frame->pts;
418 125988 dp->last_frame_tb = frame->time_base;
419
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125988 times.
125988 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 15327 times.
✓ Branch 1 taken 110661 times.
125988 if (dp->sar_override.num)
437 15327 frame->sample_aspect_ratio = dp->sar_override;
438
439
2/2
✓ Branch 0 taken 125987 times.
✓ Branch 1 taken 1 times.
125988 if (dp->apply_cropping) {
440 // lavfi does not require aligned frame data
441 125987 int ret = av_frame_apply_cropping(frame, AV_FRAME_CROP_UNALIGNED);
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125987 times.
125987 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 673 times.
✓ Branch 1 taken 125315 times.
125988 if (frame->opaque)
449 673 *outputs_mask = (uintptr_t)frame->opaque;
450
451 125988 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 994 static void subtitle_free(void *opaque, uint8_t *data)
535 {
536 994 AVSubtitle *sub = (AVSubtitle*)data;
537 994 avsubtitle_free(sub);
538 994 av_free(sub);
539 994 }
540
541 994 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 993 times.
994 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 993 sub = av_memdup(subtitle, sizeof(*subtitle));
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 993 times.
993 if (!sub)
557 return AVERROR(ENOMEM);
558 993 memset(subtitle, 0, sizeof(*subtitle));
559 }
560
561 994 buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
562 subtitle_free, NULL, 0);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 994 times.
994 if (!buf) {
564 avsubtitle_free(sub);
565 av_freep(&sub);
566 return AVERROR(ENOMEM);
567 }
568
569 994 frame->buf[0] = buf;
570
571 994 return 0;
572 }
573
574 994 static int process_subtitle(DecoderPriv *dp, AVFrame *frame)
575 {
576 994 const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
577 994 int ret = 0;
578
579
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 977 times.
994 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 992 times.
994 if (!subtitle)
608 2 return 0;
609
610 992 ret = sch_dec_send(dp->sch, dp->sch_idx, 0, frame);
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 992 times.
992 if (ret < 0)
612 av_frame_unref(frame);
613
614
1/2
✓ Branch 0 taken 992 times.
✗ Branch 1 not taken.
992 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 2818 static int transcode_subtitles(DecoderPriv *dp, const AVPacket *pkt,
640 AVFrame *frame)
641 {
642 2818 AVPacket *flush_pkt = NULL;
643 AVSubtitle subtitle;
644 int got_output;
645 int ret;
646
647
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) {
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 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) {
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 44 times.
✓ Branch 1 taken 1822 times.
1866 if (!pkt) {
660 44 flush_pkt = av_packet_alloc();
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!flush_pkt)
662 return AVERROR(ENOMEM);
663 }
664
665
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 44 times.
1866 ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output,
666 pkt ? pkt : flush_pkt);
667 1866 av_packet_free(&flush_pkt);
668
669
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1787 times.
1866 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 794 times.
✓ Branch 1 taken 993 times.
1787 if (!got_output)
677
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 44 times.
794 return pkt ? 0 : AVERROR_EOF;
678
679 993 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 993 ret = subtitle_wrap_frame(frame, &subtitle, 0);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 993 times.
993 if (ret < 0) {
687 avsubtitle_free(&subtitle);
688 return ret;
689 }
690
691 993 frame->width = dp->dec_ctx->width;
692 993 frame->height = dp->dec_ctx->height;
693
694 993 return process_subtitle(dp, frame);
695 }
696
697 370213 static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame)
698 {
699 370213 AVCodecContext *dec = dp->dec_ctx;
700 370213 const char *type_desc = av_get_media_type_string(dec->codec_type);
701 int ret;
702
703
2/2
✓ Branch 0 taken 2818 times.
✓ Branch 1 taken 367395 times.
370213 if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
704 2818 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 364048 times.
✓ Branch 1 taken 3347 times.
✓ Branch 2 taken 1480 times.
✓ Branch 3 taken 362568 times.
367395 if (pkt && pkt->size == 0)
710 1480 return 0;
711
712
4/4
✓ Branch 0 taken 362568 times.
✓ Branch 1 taken 3347 times.
✓ Branch 2 taken 58744 times.
✓ Branch 3 taken 303824 times.
365915 if (pkt && (dp->flags & DECODER_FLAG_TS_UNRELIABLE)) {
713 58744 pkt->pts = AV_NOPTS_VALUE;
714 58744 pkt->dts = AV_NOPTS_VALUE;
715 }
716
717
2/2
✓ Branch 0 taken 362568 times.
✓ Branch 1 taken 3347 times.
365915 if (pkt) {
718 362568 FrameData *fd = packet_data(pkt);
719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 362568 times.
362568 if (!fd)
720 return AVERROR(ENOMEM);
721 362568 fd->wallclock[LATENCY_PROBE_DEC_PRE] = av_gettime_relative();
722 }
723
724 365915 ret = avcodec_send_packet(dec, pkt);
725
3/6
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 365515 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 400 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
365915 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 400 times.
400 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 400 times.
✗ Branch 1 not taken.
400 av_log(dp, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
734 400 pkt ? "packet" : "EOF", av_err2str(ret));
735
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (ret == AVERROR_EOF)
737 return ret;
738
739 400 dp->dec.decode_errors++;
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (exit_on_error)
741 return ret;
742 }
743
744 382705 while (1) {
745 FrameData *fd;
746 748620 unsigned outputs_mask = 1;
747
748 748620 av_frame_unref(frame);
749
750 748620 update_benchmark(NULL);
751 748620 ret = avcodec_receive_frame(dec, frame);
752 748620 update_benchmark("decode_%s %s", type_desc, dp->parent_name);
753
754
2/2
✓ Branch 0 taken 359020 times.
✓ Branch 1 taken 389600 times.
748620 if (ret == AVERROR(EAGAIN)) {
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 359020 times.
359020 av_assert0(pkt); // should never happen during flushing
756 365915 return 0;
757
2/2
✓ Branch 0 taken 3347 times.
✓ Branch 1 taken 386253 times.
389600 } else if (ret == AVERROR_EOF) {
758 3347 return ret;
759
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 386247 times.
386253 } else if (ret < 0) {
760 6 av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
761 6 dp->dec.decode_errors++;
762
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (exit_on_error)
764 return ret;
765
766 6 continue;
767 }
768
769
4/4
✓ Branch 0 taken 386210 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 386173 times.
386247 if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) {
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 av_log(dp, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
771 "corrupt decoded frame\n");
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (exit_on_error)
773 return AVERROR_INVALIDDATA;
774 }
775
776 386247 fd = frame_data(frame);
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386247 times.
386247 if (!fd) {
778 av_frame_unref(frame);
779 return AVERROR(ENOMEM);
780 }
781 386247 fd->dec.pts = frame->pts;
782 386247 fd->dec.tb = dec->pkt_timebase;
783 386247 fd->dec.frame_num = dec->frame_num - 1;
784 386247 fd->bits_per_raw_sample = dec->bits_per_raw_sample;
785
786 386247 fd->wallclock[LATENCY_PROBE_DEC_POST] = av_gettime_relative();
787
788 386247 frame->time_base = dec->pkt_timebase;
789
790
2/2
✓ Branch 0 taken 260259 times.
✓ Branch 1 taken 125988 times.
386247 if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
791 260259 dp->dec.samples_decoded += frame->nb_samples;
792
793 260259 audio_ts_process(dp, frame);
794 } else {
795 125988 ret = video_frame_process(dp, frame, &outputs_mask);
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125988 times.
125988 if (ret < 0) {
797 av_log(dp, AV_LOG_FATAL,
798 "Error while processing the decoded data\n");
799 return ret;
800 }
801 }
802
803 386247 dp->dec.frames_decoded++;
804
805
2/2
✓ Branch 0 taken 386247 times.
✓ Branch 1 taken 382699 times.
768946 for (int i = 0; i < stdc_count_ones(outputs_mask); i++) {
806 386247 AVFrame *to_send = frame;
807 int pos;
808
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386247 times.
386247 av_assert0(outputs_mask);
810
1/2
✓ Branch 0 taken 386247 times.
✗ Branch 1 not taken.
386247 pos = stdc_trailing_zeros(outputs_mask);
811 386247 outputs_mask &= ~(1U << pos);
812
813 // this is not the last output and sch_dec_send() consumes the frame
814 // given to it, so make a temporary reference
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386247 times.
386247 if (outputs_mask) {
816 to_send = dp->frame_tmp_ref;
817 ret = av_frame_ref(to_send, frame);
818 if (ret < 0)
819 return ret;
820 }
821
822 386247 ret = sch_dec_send(dp->sch, dp->sch_idx, pos, to_send);
823
2/2
✓ Branch 0 taken 3548 times.
✓ Branch 1 taken 382699 times.
386247 if (ret < 0) {
824 3548 av_frame_unref(to_send);
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3548 times.
3548 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
826 }
827 }
828 }
829 }
830
831 static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
832 const DecoderOpts *o, AVFrame *param_out);
833
834 1 static int dec_standalone_open(DecoderPriv *dp, const AVPacket *pkt)
835 {
836 DecoderOpts o;
837 const FrameData *fd;
838 char name[16];
839
840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pkt->opaque_ref)
841 return AVERROR_BUG;
842 1 fd = (FrameData *)pkt->opaque_ref->data;
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fd->par_enc)
845 return AVERROR_BUG;
846
847 1 memset(&o, 0, sizeof(o));
848
849 1 o.par = fd->par_enc;
850 1 o.time_base = pkt->time_base;
851
852 1 o.codec = dp->standalone_init.codec;
853
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!o.codec)
854 1 o.codec = avcodec_find_decoder(o.par->codec_id);
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!o.codec) {
856 const AVCodecDescriptor *desc = avcodec_descriptor_get(o.par->codec_id);
857
858 av_log(dp, AV_LOG_ERROR, "Cannot find a decoder for codec ID '%s'\n",
859 desc ? desc->name : "?");
860 return AVERROR_DECODER_NOT_FOUND;
861 }
862
863 1 snprintf(name, sizeof(name), "dec%d", dp->index);
864 1 o.name = name;
865
866 1 return dec_open(dp, &dp->standalone_init.opts, &o, NULL);
867 }
868
869 6933 static void dec_thread_set_name(const DecoderPriv *dp)
870 {
871 6933 char name[16] = "dec";
872
873
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6932 times.
6933 if (dp->index >= 0)
874 1 av_strlcatf(name, sizeof(name), "%d", dp->index);
875
1/2
✓ Branch 0 taken 6932 times.
✗ Branch 1 not taken.
6932 else if (dp->parent_name)
876 6932 av_strlcat(name, dp->parent_name, sizeof(name));
877
878
2/2
✓ Branch 0 taken 6932 times.
✓ Branch 1 taken 1 times.
6933 if (dp->dec_ctx)
879 6932 av_strlcatf(name, sizeof(name), ":%s", dp->dec_ctx->codec->name);
880
881 6933 ff_thread_setname(name);
882 6933 }
883
884 6933 static void dec_thread_uninit(DecThreadContext *dt)
885 {
886 6933 av_packet_free(&dt->pkt);
887 6933 av_frame_free(&dt->frame);
888
889 6933 memset(dt, 0, sizeof(*dt));
890 6933 }
891
892 6933 static int dec_thread_init(DecThreadContext *dt)
893 {
894 6933 memset(dt, 0, sizeof(*dt));
895
896 6933 dt->frame = av_frame_alloc();
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dt->frame)
898 goto fail;
899
900 6933 dt->pkt = av_packet_alloc();
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dt->pkt)
902 goto fail;
903
904 6933 return 0;
905
906 fail:
907 dec_thread_uninit(dt);
908 return AVERROR(ENOMEM);
909 }
910
911 6933 static int decoder_thread(void *arg)
912 {
913 6933 DecoderPriv *dp = arg;
914 DecThreadContext dt;
915 6933 int ret = 0, input_status = 0;
916
917 6933 ret = dec_thread_init(&dt);
918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret < 0)
919 goto finish;
920
921 6933 dec_thread_set_name(dp);
922
923
1/2
✓ Branch 0 taken 370213 times.
✗ Branch 1 not taken.
370213 while (!input_status) {
924 int flush_buffers, have_data;
925
926 370213 input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt);
927
2/2
✓ Branch 0 taken 366828 times.
✓ Branch 1 taken 3385 times.
737041 have_data = input_status >= 0 &&
928
3/4
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 365870 times.
✓ Branch 2 taken 958 times.
✗ Branch 3 not taken.
366828 (dt.pkt->buf || dt.pkt->side_data_elems ||
929
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 948 times.
958 (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
930
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
931
4/4
✓ Branch 0 taken 366828 times.
✓ Branch 1 taken 3385 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 366822 times.
370213 flush_buffers = input_status >= 0 && !have_data;
932
2/2
✓ Branch 0 taken 3391 times.
✓ Branch 1 taken 366822 times.
370213 if (!have_data)
933
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3385 times.
3391 av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
934 flush_buffers ? "flush" : "EOF");
935
936 // this is a standalone decoder that has not been initialized yet
937
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 370212 times.
370213 if (!dp->dec_ctx) {
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (flush_buffers)
939 continue;
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (input_status < 0) {
941 av_log(dp, AV_LOG_ERROR,
942 "Cannot initialize a standalone decoder\n");
943 ret = input_status;
944 goto finish;
945 }
946
947 1 ret = dec_standalone_open(dp, dt.pkt);
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
949 goto finish;
950 }
951
952
2/2
✓ Branch 0 taken 366822 times.
✓ Branch 1 taken 3391 times.
370213 ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame);
953
954 370213 av_packet_unref(dt.pkt);
955 370213 av_frame_unref(dt.frame);
956
957 // AVERROR_EOF - EOF from the decoder
958 // AVERROR_EXIT - EOF from the scheduler
959 // we treat them differently when flushing
960
2/2
✓ Branch 0 taken 3548 times.
✓ Branch 1 taken 366665 times.
370213 if (ret == AVERROR_EXIT) {
961 3548 ret = AVERROR_EOF;
962 3548 flush_buffers = 0;
963 }
964
965
2/2
✓ Branch 0 taken 6939 times.
✓ Branch 1 taken 363274 times.
370213 if (ret == AVERROR_EOF) {
966
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6933 times.
6939 av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
967 flush_buffers ? "resetting" : "finishing");
968
969
2/2
✓ Branch 0 taken 6933 times.
✓ Branch 1 taken 6 times.
6939 if (!flush_buffers)
970 6933 break;
971
972 /* report last frame duration to the scheduler */
973
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
974 3 dt.pkt->pts = dp->last_frame_pts + dp->last_frame_duration_est;
975 3 dt.pkt->time_base = dp->last_frame_tb;
976 }
977
978 6 avcodec_flush_buffers(dp->dec_ctx);
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 363274 times.
363274 } else if (ret < 0) {
980 av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
981 av_err2str(ret));
982 break;
983 }
984 }
985
986 // EOF is normal thread termination
987
1/2
✓ Branch 0 taken 6933 times.
✗ Branch 1 not taken.
6933 if (ret == AVERROR_EOF)
988 6933 ret = 0;
989
990 // on success send EOF timestamp to our downstreams
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret >= 0) {
992 float err_rate;
993
994 6933 av_frame_unref(dt.frame);
995
996 6933 dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
997
2/2
✓ Branch 0 taken 6886 times.
✓ Branch 1 taken 47 times.
6933 dt.frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
998 6886 dp->last_frame_pts + dp->last_frame_duration_est;
999 6933 dt.frame->time_base = dp->last_frame_tb;
1000
1001 6933 ret = sch_dec_send(dp->sch, dp->sch_idx, 0, dt.frame);
1002
3/4
✓ Branch 0 taken 3554 times.
✓ Branch 1 taken 3379 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3554 times.
6933 if (ret < 0 && ret != AVERROR_EOF) {
1003 av_log(dp, AV_LOG_FATAL,
1004 "Error signalling EOF timestamp: %s\n", av_err2str(ret));
1005 goto finish;
1006 }
1007 6933 ret = 0;
1008
1009
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ?
1010
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6930 times.
6936 (float)dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f;
1011
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6932 times.
6933 if (err_rate > max_error_rate) {
1012 1 av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
1013 err_rate, max_error_rate);
1014 1 ret = FFMPEG_ERROR_RATE_EXCEEDED;
1015
2/2
✓ Branch 0 taken 6866 times.
✓ Branch 1 taken 66 times.
6932 } else if (err_rate)
1016 66 av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
1017 }
1018
1019 6866 finish:
1020 6933 dec_thread_uninit(&dt);
1021 6933 avcodec_free_context(&dp->dec_ctx);
1022
1023 6933 return ret;
1024 }
1025
1026 5685 int dec_request_view(Decoder *d, const ViewSpecifier *vs,
1027 SchedulerNode *src)
1028 {
1029 5685 DecoderPriv *dp = dp_from_dec(d);
1030 5685 unsigned out_idx = 0;
1031 int ret;
1032
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5685 times.
5685 if (dp->multiview_user_config) {
1034 if (!vs || vs->type == VIEW_SPECIFIER_TYPE_NONE) {
1035 *src = SCH_DEC_OUT(dp->sch_idx, 0);
1036 return 0;
1037 }
1038
1039 av_log(dp, AV_LOG_ERROR,
1040 "Manually selecting views with -view_ids cannot be combined "
1041 "with view selection via stream specifiers. It is strongly "
1042 "recommended you always use stream specifiers only.\n");
1043 return AVERROR(EINVAL);
1044 }
1045
1046 // when multiview_user_config is not set, NONE specifier is treated
1047 // as requesting the base view
1048
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 5605 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 14 times.
5685 vs = (vs && vs->type != VIEW_SPECIFIER_TYPE_NONE) ? vs :
1049 5671 &(ViewSpecifier){ .type = VIEW_SPECIFIER_TYPE_IDX, .val = 0 };
1050
1051 // check if the specifier matches an already-existing one
1052
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5681 times.
5691 for (int i = 0; i < dp->nb_views_requested; i++) {
1053 10 const ViewSpecifier *vs1 = &dp->views_requested[i].vs;
1054
1055
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (vs->type == vs1->type &&
1056
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)) {
1057 4 *src = SCH_DEC_OUT(dp->sch_idx, dp->views_requested[i].out_idx);
1058 4 return 0;
1059 }
1060 }
1061
1062 // we use a bitmask to map view IDs to decoder outputs, which
1063 // limits the number of outputs allowed
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5681 times.
5681 if (dp->nb_views_requested >= sizeof(dp->view_map[0].out_mask) * 8) {
1065 av_log(dp, AV_LOG_ERROR, "Too many view specifiers\n");
1066 return AVERROR(ENOSYS);
1067 }
1068
1069 5681 ret = GROW_ARRAY(dp->views_requested, dp->nb_views_requested);
1070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5681 times.
5681 if (ret < 0)
1071 return ret;
1072
1073
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5675 times.
5681 if (dp->nb_views_requested > 1) {
1074 6 ret = sch_add_dec_output(dp->sch, dp->sch_idx);
1075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1076 return ret;
1077 6 out_idx = ret;
1078 }
1079
1080 5681 dp->views_requested[dp->nb_views_requested - 1].out_idx = out_idx;
1081 5681 dp->views_requested[dp->nb_views_requested - 1].vs = *vs;
1082
1083 5681 *src = SCH_DEC_OUT(dp->sch_idx,
1084 dp->views_requested[dp->nb_views_requested - 1].out_idx);
1085
1086 5681 return 0;
1087 }
1088
1089 1031 static int multiview_setup(DecoderPriv *dp, AVCodecContext *dec_ctx)
1090 {
1091 1031 unsigned views_wanted = 0;
1092
1093 unsigned nb_view_ids_av, nb_view_ids;
1094 1031 unsigned *view_ids_av = NULL, *view_pos_av = NULL;
1095 1031 int *view_ids = NULL;
1096 int ret;
1097
1098 // no views/only base view were requested - do nothing
1099
1/2
✓ Branch 0 taken 1031 times.
✗ Branch 1 not taken.
1031 if (!dp->nb_views_requested ||
1100
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 9 times.
1031 (dp->nb_views_requested == 1 &&
1101
2/2
✓ Branch 0 taken 1021 times.
✓ Branch 1 taken 1 times.
1022 dp->views_requested[0].vs.type == VIEW_SPECIFIER_TYPE_IDX &&
1102
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1 times.
1021 dp->views_requested[0].vs.val == 0))
1103 1020 return 0;
1104
1105 11 av_freep(&dp->view_map);
1106 11 dp->nb_view_map = 0;
1107
1108 // retrieve views available in current CVS
1109 11 ret = av_opt_get_array_size(dec_ctx, "view_ids_available",
1110 AV_OPT_SEARCH_CHILDREN, &nb_view_ids_av);
1111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0) {
1112 av_log(dp, AV_LOG_ERROR,
1113 "Multiview decoding requested, but decoder '%s' does not "
1114 "support it\n", dec_ctx->codec->name);
1115 return AVERROR(ENOSYS);
1116 }
1117
1118
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
11 if (nb_view_ids_av) {
1119 unsigned nb_view_pos_av;
1120
1121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (nb_view_ids_av >= sizeof(views_wanted) * 8) {
1122 av_log(dp, AV_LOG_ERROR, "Too many views in video: %u\n", nb_view_ids_av);
1123 ret = AVERROR(ENOSYS);
1124 goto fail;
1125 }
1126
1127 9 view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
1128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!view_ids_av) {
1129 ret = AVERROR(ENOMEM);
1130 goto fail;
1131 }
1132
1133 9 ret = av_opt_get_array(dec_ctx, "view_ids_available",
1134 AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
1135 AV_OPT_TYPE_UINT, view_ids_av);
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1137 goto fail;
1138
1139 9 ret = av_opt_get_array_size(dec_ctx, "view_pos_available",
1140 AV_OPT_SEARCH_CHILDREN, &nb_view_pos_av);
1141
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) {
1142 1 view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!view_pos_av) {
1144 ret = AVERROR(ENOMEM);
1145 goto fail;
1146 }
1147
1148 1 ret = av_opt_get_array(dec_ctx, "view_pos_available",
1149 AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
1150 AV_OPT_TYPE_UINT, view_pos_av);
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1152 goto fail;
1153 }
1154 } else {
1155 // assume there is a single view with ID=0
1156 2 nb_view_ids_av = 1;
1157 2 view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
1158 2 view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
1159
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) {
1160 ret = AVERROR(ENOMEM);
1161 goto fail;
1162 }
1163 2 view_pos_av[0] = AV_STEREO3D_VIEW_UNSPEC;
1164 }
1165
1166 11 dp->view_map = av_calloc(nb_view_ids_av, sizeof(*dp->view_map));
1167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!dp->view_map) {
1168 ret = AVERROR(ENOMEM);
1169 goto fail;
1170 }
1171 11 dp->nb_view_map = nb_view_ids_av;
1172
1173
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 11 times.
31 for (int i = 0; i < dp->nb_view_map; i++)
1174 20 dp->view_map[i].id = view_ids_av[i];
1175
1176 // figure out which views should go to which output
1177
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 11 times.
31 for (int i = 0; i < dp->nb_views_requested; i++) {
1178 20 const ViewSpecifier *vs = &dp->views_requested[i].vs;
1179
1180
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) {
1181 9 case VIEW_SPECIFIER_TYPE_IDX:
1182
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (vs->val >= nb_view_ids_av) {
1183 2 av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
1184 "View with index %u requested, but only %u views available "
1185 "in current video sequence (more views may or may not be "
1186 "available in later sequences).\n",
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 vs->val, nb_view_ids_av);
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (exit_on_error) {
1189 ret = AVERROR(EINVAL);
1190 goto fail;
1191 }
1192
1193 2 continue;
1194 }
1195 7 views_wanted |= 1U << vs->val;
1196 7 dp->view_map[vs->val].out_mask |= 1ULL << i;
1197
1198 7 break;
1199 8 case VIEW_SPECIFIER_TYPE_ID: {
1200 8 int view_idx = -1;
1201
1202
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (unsigned j = 0; j < nb_view_ids_av; j++) {
1203
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (view_ids_av[j] == vs->val) {
1204 8 view_idx = j;
1205 8 break;
1206 }
1207 }
1208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (view_idx < 0) {
1209 av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
1210 "View with ID %u requested, but is not available "
1211 "in the video sequence\n", vs->val);
1212 if (exit_on_error) {
1213 ret = AVERROR(EINVAL);
1214 goto fail;
1215 }
1216
1217 continue;
1218 }
1219 8 views_wanted |= 1U << view_idx;
1220 8 dp->view_map[view_idx].out_mask |= 1ULL << i;
1221
1222 8 break;
1223 }
1224 2 case VIEW_SPECIFIER_TYPE_POS: {
1225 2 int view_idx = -1;
1226
1227
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++) {
1228
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (view_pos_av[j] == vs->val) {
1229 2 view_idx = j;
1230 2 break;
1231 }
1232 }
1233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (view_idx < 0) {
1234 av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
1235 "View position '%s' requested, but is not available "
1236 "in the video sequence\n", av_stereo3d_view_name(vs->val));
1237 if (exit_on_error) {
1238 ret = AVERROR(EINVAL);
1239 goto fail;
1240 }
1241
1242 continue;
1243 }
1244 2 views_wanted |= 1U << view_idx;
1245 2 dp->view_map[view_idx].out_mask |= 1ULL << i;
1246
1247 2 break;
1248 }
1249 1 case VIEW_SPECIFIER_TYPE_ALL:
1250 1 views_wanted |= (1U << nb_view_ids_av) - 1;
1251
1252
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int j = 0; j < dp->nb_view_map; j++)
1253 2 dp->view_map[j].out_mask |= 1ULL << i;
1254
1255 1 break;
1256 }
1257 }
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!views_wanted) {
1259 av_log(dp, AV_LOG_ERROR, "No views were selected for decoding\n");
1260 ret = AVERROR(EINVAL);
1261 goto fail;
1262 }
1263
1264 // signal to decoder which views we want
1265 11 nb_view_ids = stdc_count_ones(views_wanted);
1266 11 view_ids = av_malloc_array(nb_view_ids, sizeof(*view_ids));
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!view_ids) {
1268 ret = AVERROR(ENOMEM);
1269 goto fail;
1270 }
1271
1272
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 11 times.
30 for (unsigned i = 0; i < nb_view_ids; i++) {
1273 int pos;
1274
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 av_assert0(views_wanted);
1276
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 pos = stdc_trailing_zeros(views_wanted);
1277 19 views_wanted &= ~(1U << pos);
1278
1279 19 view_ids[i] = view_ids_av[pos];
1280 }
1281
1282 // unset view_ids in case we set it earlier
1283 11 av_opt_set(dec_ctx, "view_ids", NULL, AV_OPT_SEARCH_CHILDREN);
1284
1285 11 ret = av_opt_set_array(dec_ctx, "view_ids", AV_OPT_SEARCH_CHILDREN,
1286 0, nb_view_ids, AV_OPT_TYPE_INT, view_ids);
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
1288 goto fail;
1289
1290
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
11 if (!dp->frame_tmp_ref) {
1291 8 dp->frame_tmp_ref = av_frame_alloc();
1292
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!dp->frame_tmp_ref) {
1293 ret = AVERROR(ENOMEM);
1294 goto fail;
1295 }
1296 }
1297
1298 11 fail:
1299 11 av_freep(&view_ids_av);
1300 11 av_freep(&view_pos_av);
1301 11 av_freep(&view_ids);
1302
1303 11 return ret;
1304 }
1305
1306 6933 static void multiview_check_manual(DecoderPriv *dp, const AVDictionary *dec_opts)
1307 {
1308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6933 times.
6933 if (av_dict_get(dec_opts, "view_ids", NULL, 0)) {
1309 av_log(dp, AV_LOG_WARNING, "Manually selecting views with -view_ids "
1310 "is not recommended, use view specifiers instead\n");
1311 dp->multiview_user_config = 1;
1312 }
1313 6933 }
1314
1315 1031 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
1316 {
1317 1031 DecoderPriv *dp = s->opaque;
1318 const enum AVPixelFormat *p;
1319 int ret;
1320
1321 1031 ret = multiview_setup(dp, s);
1322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1031 times.
1031 if (ret < 0) {
1323 av_log(dp, AV_LOG_ERROR, "Error setting up multiview decoding: %s\n",
1324 av_err2str(ret));
1325 return AV_PIX_FMT_NONE;
1326 }
1327
1328
1/2
✓ Branch 0 taken 2782 times.
✗ Branch 1 not taken.
2782 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
1329 2782 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
1330 2782 const AVCodecHWConfig *config = NULL;
1331
1332
2/2
✓ Branch 0 taken 1031 times.
✓ Branch 1 taken 1751 times.
2782 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1333 1031 break;
1334
1335
1/2
✓ Branch 0 taken 1751 times.
✗ Branch 1 not taken.
1751 if (dp->hwaccel_id == HWACCEL_GENERIC ||
1336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1751 times.
1751 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 1751 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1751 if (config && config->device_type == dp->hwaccel_device_type) {
1349 dp->hwaccel_pix_fmt = *p;
1350 break;
1351 }
1352 }
1353
1354 1031 return *p;
1355 }
1356
1357 359739 static int get_buffer(AVCodecContext *dec_ctx, AVFrame *frame, int flags)
1358 {
1359 359739 DecoderPriv *dp = dec_ctx->opaque;
1360
1361 // for multiview video, store the output mask in frame opaque
1362
2/2
✓ Branch 0 taken 739 times.
✓ Branch 1 taken 359000 times.
359739 if (dp->nb_view_map) {
1363 739 const AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIEW_ID);
1364
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 59 times.
739 int view_id = sd ? *(int*)sd->data : 0;
1365
1366
1/2
✓ Branch 0 taken 1079 times.
✗ Branch 1 not taken.
1079 for (int i = 0; i < dp->nb_view_map; i++) {
1367
2/2
✓ Branch 0 taken 739 times.
✓ Branch 1 taken 340 times.
1079 if (dp->view_map[i].id == view_id) {
1368 739 frame->opaque = (void*)dp->view_map[i].out_mask;
1369 739 break;
1370 }
1371 }
1372 }
1373
1374 359739 return avcodec_default_get_buffer2(dec_ctx, frame, flags);
1375 }
1376
1377 6933 static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
1378 {
1379 const AVCodecHWConfig *config;
1380 HWDevice *dev;
1381 6933 for (int i = 0;; i++) {
1382 8677 config = avcodec_get_hw_config(codec, i);
1383
2/2
✓ Branch 0 taken 6933 times.
✓ Branch 1 taken 1744 times.
8677 if (!config)
1384 6933 return NULL;
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1744 times.
1744 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1386 continue;
1387 1744 dev = hw_device_get_by_type(config->device_type);
1388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1744 times.
1744 if (dev)
1389 return dev;
1390 }
1391 }
1392
1393 6933 static int hw_device_setup_for_decode(DecoderPriv *dp,
1394 const AVCodec *codec,
1395 const char *hwaccel_device)
1396 {
1397 const AVCodecHWConfig *config;
1398 enum AVHWDeviceType type;
1399 6933 HWDevice *dev = NULL;
1400 6933 int err, auto_device = 0;
1401
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (hwaccel_device) {
1403 dev = hw_device_get_by_name(hwaccel_device);
1404 if (!dev) {
1405 if (dp->hwaccel_id == HWACCEL_AUTO) {
1406 auto_device = 1;
1407 } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
1408 type = dp->hwaccel_device_type;
1409 err = hw_device_init_from_type(type, hwaccel_device,
1410 &dev);
1411 } else {
1412 // This will be dealt with by API-specific initialisation
1413 // (using hwaccel_device), so nothing further needed here.
1414 return 0;
1415 }
1416 } else {
1417 if (dp->hwaccel_id == HWACCEL_AUTO) {
1418 dp->hwaccel_device_type = dev->type;
1419 } else if (dp->hwaccel_device_type != dev->type) {
1420 av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device "
1421 "specified for decoder: device %s of type %s is not "
1422 "usable with hwaccel %s.\n", dev->name,
1423 av_hwdevice_get_type_name(dev->type),
1424 av_hwdevice_get_type_name(dp->hwaccel_device_type));
1425 return AVERROR(EINVAL);
1426 }
1427 }
1428 } else {
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (dp->hwaccel_id == HWACCEL_AUTO) {
1430 auto_device = 1;
1431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
1432 type = dp->hwaccel_device_type;
1433 dev = hw_device_get_by_type(type);
1434
1435 // When "-qsv_device device" is used, an internal QSV device named
1436 // as "__qsv_device" is created. Another QSV device is created too
1437 // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
1438 // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
1439 // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
1440 // To keep back-compatibility with the removed ad-hoc libmfx setup code,
1441 // call hw_device_get_by_name("__qsv_device") to select the internal QSV
1442 // device.
1443 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
1444 dev = hw_device_get_by_name("__qsv_device");
1445
1446 if (!dev)
1447 err = hw_device_init_from_type(type, NULL, &dev);
1448 } else {
1449 6933 dev = hw_device_match_by_codec(codec);
1450
1/2
✓ Branch 0 taken 6933 times.
✗ Branch 1 not taken.
6933 if (!dev) {
1451 // No device for this codec, but not using generic hwaccel
1452 // and therefore may well not need one - ignore.
1453 6933 return 0;
1454 }
1455 }
1456 }
1457
1458 if (auto_device) {
1459 if (!avcodec_get_hw_config(codec, 0)) {
1460 // Decoder does not support any hardware devices.
1461 return 0;
1462 }
1463 for (int i = 0; !dev; i++) {
1464 config = avcodec_get_hw_config(codec, i);
1465 if (!config)
1466 break;
1467 type = config->device_type;
1468 dev = hw_device_get_by_type(type);
1469 if (dev) {
1470 av_log(dp, AV_LOG_INFO, "Using auto "
1471 "hwaccel type %s with existing device %s.\n",
1472 av_hwdevice_get_type_name(type), dev->name);
1473 }
1474 }
1475 for (int i = 0; !dev; i++) {
1476 config = avcodec_get_hw_config(codec, i);
1477 if (!config)
1478 break;
1479 type = config->device_type;
1480 // Try to make a new device of this type.
1481 err = hw_device_init_from_type(type, hwaccel_device,
1482 &dev);
1483 if (err < 0) {
1484 // Can't make a device of this type.
1485 continue;
1486 }
1487 if (hwaccel_device) {
1488 av_log(dp, AV_LOG_INFO, "Using auto "
1489 "hwaccel type %s with new device created "
1490 "from %s.\n", av_hwdevice_get_type_name(type),
1491 hwaccel_device);
1492 } else {
1493 av_log(dp, AV_LOG_INFO, "Using auto "
1494 "hwaccel type %s with new default device.\n",
1495 av_hwdevice_get_type_name(type));
1496 }
1497 }
1498 if (dev) {
1499 dp->hwaccel_device_type = type;
1500 } else {
1501 av_log(dp, AV_LOG_INFO, "Auto hwaccel "
1502 "disabled: no device found.\n");
1503 dp->hwaccel_id = HWACCEL_NONE;
1504 return 0;
1505 }
1506 }
1507
1508 if (!dev) {
1509 av_log(dp, AV_LOG_ERROR, "No device available "
1510 "for decoder: device type %s needed for codec %s.\n",
1511 av_hwdevice_get_type_name(type), codec->name);
1512 return err;
1513 }
1514
1515 dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
1516 if (!dp->dec_ctx->hw_device_ctx)
1517 return AVERROR(ENOMEM);
1518
1519 return 0;
1520 }
1521
1522 6933 static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
1523 const DecoderOpts *o, AVFrame *param_out)
1524 {
1525 6933 const AVCodec *codec = o->codec;
1526 int ret;
1527
1528 6933 dp->flags = o->flags;
1529 6933 dp->log_parent = o->log_parent;
1530
1531 6933 dp->dec.type = codec->type;
1532 6933 dp->framerate_in = o->framerate;
1533
1534 6933 dp->hwaccel_id = o->hwaccel_id;
1535 6933 dp->hwaccel_device_type = o->hwaccel_device_type;
1536 6933 dp->hwaccel_output_format = o->hwaccel_output_format;
1537
1538 6933 snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
1539
1540
1/2
✓ Branch 0 taken 6933 times.
✗ Branch 1 not taken.
6933 dp->parent_name = av_strdup(o->name ? o->name : "");
1541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dp->parent_name)
1542 return AVERROR(ENOMEM);
1543
1544
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 6889 times.
6933 if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
1545
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 42 times.
44 (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) {
1546
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++) {
1547 4 dp->sub_prev[i] = av_frame_alloc();
1548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!dp->sub_prev[i])
1549 return AVERROR(ENOMEM);
1550 }
1551 2 dp->sub_heartbeat = av_frame_alloc();
1552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!dp->sub_heartbeat)
1553 return AVERROR(ENOMEM);
1554 }
1555
1556 6933 dp->sar_override = o->par->sample_aspect_ratio;
1557
1558 6933 dp->dec_ctx = avcodec_alloc_context3(codec);
1559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (!dp->dec_ctx)
1560 return AVERROR(ENOMEM);
1561
1562 6933 ret = avcodec_parameters_to_context(dp->dec_ctx, o->par);
1563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret < 0) {
1564 av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1565 return ret;
1566 }
1567
1568 6933 dp->dec_ctx->opaque = dp;
1569 6933 dp->dec_ctx->get_format = get_format;
1570 6933 dp->dec_ctx->get_buffer2 = get_buffer;
1571 6933 dp->dec_ctx->pkt_timebase = o->time_base;
1572
1573
2/2
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 6872 times.
6933 if (!av_dict_get(*dec_opts, "threads", NULL, 0))
1574 61 av_dict_set(dec_opts, "threads", "auto", 0);
1575
1576 6933 ret = hw_device_setup_for_decode(dp, codec, o->hwaccel_device);
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret < 0) {
1578 av_log(dp, AV_LOG_ERROR,
1579 "Hardware device setup failed for decoder: %s\n",
1580 av_err2str(ret));
1581 return ret;
1582 }
1583
1584 6933 ret = av_opt_set_dict2(dp->dec_ctx, dec_opts, AV_OPT_SEARCH_CHILDREN);
1585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret < 0) {
1586 av_log(dp, AV_LOG_ERROR, "Error applying decoder options: %s\n",
1587 av_err2str(ret));
1588 return ret;
1589 }
1590 6933 ret = check_avoptions(*dec_opts);
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (ret < 0)
1592 return ret;
1593
1594 6933 dp->dec_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
1595
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 6854 times.
6933 if (o->flags & DECODER_FLAG_BITEXACT)
1596 79 dp->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1597
1598 // we apply cropping ourselves
1599 6933 dp->apply_cropping = dp->dec_ctx->apply_cropping;
1600 6933 dp->dec_ctx->apply_cropping = 0;
1601
1602
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6933 times.
6933 if ((ret = avcodec_open2(dp->dec_ctx, codec, NULL)) < 0) {
1603 av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
1604 av_err2str(ret));
1605 return ret;
1606 }
1607
1608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6933 times.
6933 if (dp->dec_ctx->hw_device_ctx) {
1609 // Update decoder extra_hw_frames option to account for the
1610 // frames held in queues inside the ffmpeg utility. This is
1611 // called after avcodec_open2() because the user-set value of
1612 // extra_hw_frames becomes valid in there, and we need to add
1613 // this on top of it.
1614 int extra_frames = DEFAULT_FRAME_THREAD_QUEUE_SIZE;
1615 if (dp->dec_ctx->extra_hw_frames >= 0)
1616 dp->dec_ctx->extra_hw_frames += extra_frames;
1617 else
1618 dp->dec_ctx->extra_hw_frames = extra_frames;
1619 }
1620
1621 6933 dp->dec.subtitle_header = dp->dec_ctx->subtitle_header;
1622 6933 dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
1623
1624
2/2
✓ Branch 0 taken 6932 times.
✓ Branch 1 taken 1 times.
6933 if (param_out) {
1625
2/2
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 5718 times.
6932 if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1626 1214 param_out->format = dp->dec_ctx->sample_fmt;
1627 1214 param_out->sample_rate = dp->dec_ctx->sample_rate;
1628
1629 1214 ret = av_channel_layout_copy(&param_out->ch_layout, &dp->dec_ctx->ch_layout);
1630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1214 times.
1214 if (ret < 0)
1631 return ret;
1632
2/2
✓ Branch 0 taken 5674 times.
✓ Branch 1 taken 44 times.
5718 } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1633 5674 param_out->format = dp->dec_ctx->pix_fmt;
1634 5674 param_out->width = dp->dec_ctx->width;
1635 5674 param_out->height = dp->dec_ctx->height;
1636 5674 param_out->sample_aspect_ratio = dp->dec_ctx->sample_aspect_ratio;
1637 5674 param_out->colorspace = dp->dec_ctx->colorspace;
1638 5674 param_out->color_range = dp->dec_ctx->color_range;
1639 5674 param_out->alpha_mode = dp->dec_ctx->alpha_mode;
1640 }
1641
1642 6932 av_frame_side_data_free(&param_out->side_data, &param_out->nb_side_data);
1643 6932 ret = clone_side_data(&param_out->side_data, &param_out->nb_side_data,
1644 6932 dp->dec_ctx->decoded_side_data, dp->dec_ctx->nb_decoded_side_data, 0);
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6932 times.
6932 if (ret < 0)
1646 return ret;
1647 6932 param_out->time_base = dp->dec_ctx->pkt_timebase;
1648 }
1649
1650 6933 return 0;
1651 }
1652
1653 6932 int dec_init(Decoder **pdec, Scheduler *sch,
1654 AVDictionary **dec_opts, const DecoderOpts *o,
1655 AVFrame *param_out)
1656 {
1657 DecoderPriv *dp;
1658 int ret;
1659
1660 6932 *pdec = NULL;
1661
1662 6932 ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
1663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6932 times.
6932 if (ret < 0)
1664 return ret;
1665
1666 6932 multiview_check_manual(dp, *dec_opts);
1667
1668 6932 ret = dec_open(dp, dec_opts, o, param_out);
1669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6932 times.
6932 if (ret < 0)
1670 goto fail;
1671
1672 6932 *pdec = &dp->dec;
1673
1674 6932 return dp->sch_idx;
1675 fail:
1676 dec_free((Decoder**)&dp);
1677 return ret;
1678 }
1679
1680 1 int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch)
1681 {
1682 DecoderPriv *dp;
1683
1684 OutputFile *of;
1685 OutputStream *ost;
1686 int of_index, ost_index;
1687 char *p;
1688
1689 unsigned enc_idx;
1690 int ret;
1691
1692 1 ret = dec_alloc(&dp, sch, 0);
1693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1694 return ret;
1695
1696 1 dp->index = nb_decoders;
1697
1698 1 ret = GROW_ARRAY(decoders, nb_decoders);
1699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1700 dec_free((Decoder **)&dp);
1701 return ret;
1702 }
1703
1704 1 decoders[nb_decoders - 1] = (Decoder *)dp;
1705
1706 1 of_index = strtol(arg, &p, 0);
1707
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) {
1708 av_log(dp, AV_LOG_ERROR, "Invalid output file index '%d' in %s\n", of_index, arg);
1709 return AVERROR(EINVAL);
1710 }
1711 1 of = output_files[of_index];
1712
1713 1 ost_index = strtol(p + 1, NULL, 0);
1714
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) {
1715 av_log(dp, AV_LOG_ERROR, "Invalid output stream index '%d' in %s\n", ost_index, arg);
1716 return AVERROR(EINVAL);
1717 }
1718 1 ost = of->streams[ost_index];
1719
1720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ost->enc) {
1721 av_log(dp, AV_LOG_ERROR, "Output stream %s has no encoder\n", arg);
1722 return AVERROR(EINVAL);
1723 }
1724
1725 1 dp->dec.type = ost->type;
1726
1727 1 ret = enc_loopback(ost->enc);
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1729 return ret;
1730 1 enc_idx = ret;
1731
1732 1 ret = sch_connect(sch, SCH_ENC(enc_idx), SCH_DEC_IN(dp->sch_idx));
1733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1734 return ret;
1735
1736 1 ret = av_dict_copy(&dp->standalone_init.opts, o->g->codec_opts, 0);
1737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1738 return ret;
1739
1740 1 multiview_check_manual(dp, dp->standalone_init.opts);
1741
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (o->codec_names.nb_opt) {
1743 const char *name = o->codec_names.opt[o->codec_names.nb_opt - 1].u.str;
1744 dp->standalone_init.codec = avcodec_find_decoder_by_name(name);
1745 if (!dp->standalone_init.codec) {
1746 av_log(dp, AV_LOG_ERROR, "No such decoder: %s\n", name);
1747 return AVERROR_DECODER_NOT_FOUND;
1748 }
1749 }
1750
1751 1 return 0;
1752 }
1753
1754 1 int dec_filter_add(Decoder *d, InputFilter *ifilter, InputFilterOptions *opts,
1755 const ViewSpecifier *vs, SchedulerNode *src)
1756 {
1757 1 DecoderPriv *dp = dp_from_dec(d);
1758 char name[16];
1759
1760 1 snprintf(name, sizeof(name), "dec%d", dp->index);
1761 1 opts->name = av_strdup(name);
1762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!opts->name)
1763 return AVERROR(ENOMEM);
1764
1765 1 return dec_request_view(d, vs, src);
1766 }
1767