FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_dec.c
Date: 2024-05-26 22:00:00
Exec Total Coverage
Lines: 511 735 69.5%
Functions: 27 28 96.4%
Branches: 288 468 61.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 "libavutil/avassert.h"
20 #include "libavutil/avstring.h"
21 #include "libavutil/dict.h"
22 #include "libavutil/error.h"
23 #include "libavutil/log.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/pixfmt.h"
28 #include "libavutil/time.h"
29 #include "libavutil/timestamp.h"
30
31 #include "libavcodec/avcodec.h"
32 #include "libavcodec/codec.h"
33
34 #include "ffmpeg.h"
35
36 typedef struct DecoderPriv {
37 Decoder dec;
38
39 AVCodecContext *dec_ctx;
40
41 AVFrame *frame;
42 AVPacket *pkt;
43
44 // override output video sample aspect ratio with this value
45 AVRational sar_override;
46
47 AVRational framerate_in;
48
49 // a combination of DECODER_FLAG_*, provided to dec_open()
50 int flags;
51 int apply_cropping;
52
53 enum AVPixelFormat hwaccel_pix_fmt;
54 enum HWAccelID hwaccel_id;
55 enum AVHWDeviceType hwaccel_device_type;
56 enum AVPixelFormat hwaccel_output_format;
57
58 // pts/estimated duration of the last decoded frame
59 // * in decoder timebase for video,
60 // * in last_frame_tb (may change during decoding) for audio
61 int64_t last_frame_pts;
62 int64_t last_frame_duration_est;
63 AVRational last_frame_tb;
64 int64_t last_filter_in_rescale_delta;
65 int last_frame_sample_rate;
66
67 /* previous decoded subtitles */
68 AVFrame *sub_prev[2];
69 AVFrame *sub_heartbeat;
70
71 Scheduler *sch;
72 unsigned sch_idx;
73
74 // this decoder's index in decoders or -1
75 int index;
76 void *log_parent;
77 char log_name[32];
78 char *parent_name;
79
80 struct {
81 AVDictionary *opts;
82 const AVCodec *codec;
83 } standalone_init;
84 } DecoderPriv;
85
86 6392 static DecoderPriv *dp_from_dec(Decoder *d)
87 {
88 6392 return (DecoderPriv*)d;
89 }
90
91 // data that is local to the decoder thread and not visible outside of it
92 typedef struct DecThreadContext {
93 AVFrame *frame;
94 AVPacket *pkt;
95 } DecThreadContext;
96
97 7304 void dec_free(Decoder **pdec)
98 {
99 7304 Decoder *dec = *pdec;
100 DecoderPriv *dp;
101
102
2/2
✓ Branch 0 taken 913 times.
✓ Branch 1 taken 6391 times.
7304 if (!dec)
103 913 return;
104 6391 dp = dp_from_dec(dec);
105
106 6391 avcodec_free_context(&dp->dec_ctx);
107
108 6391 av_frame_free(&dp->frame);
109 6391 av_packet_free(&dp->pkt);
110
111 6391 av_dict_free(&dp->standalone_init.opts);
112
113
2/2
✓ Branch 0 taken 12782 times.
✓ Branch 1 taken 6391 times.
19173 for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++)
114 12782 av_frame_free(&dp->sub_prev[i]);
115 6391 av_frame_free(&dp->sub_heartbeat);
116
117 6391 av_freep(&dp->parent_name);
118
119 6391 av_freep(pdec);
120 }
121
122 531 static const char *dec_item_name(void *obj)
123 {
124 531 const DecoderPriv *dp = obj;
125
126 531 return dp->log_name;
127 }
128
129 static const AVClass dec_class = {
130 .class_name = "Decoder",
131 .version = LIBAVUTIL_VERSION_INT,
132 .parent_log_context_offset = offsetof(DecoderPriv, log_parent),
133 .item_name = dec_item_name,
134 };
135
136 static int decoder_thread(void *arg);
137
138 6391 static int dec_alloc(DecoderPriv **pdec, Scheduler *sch, int send_end_ts)
139 {
140 DecoderPriv *dp;
141 6391 int ret = 0;
142
143 6391 *pdec = NULL;
144
145 6391 dp = av_mallocz(sizeof(*dp));
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dp)
147 return AVERROR(ENOMEM);
148
149 6391 dp->frame = av_frame_alloc();
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dp->frame)
151 goto fail;
152
153 6391 dp->pkt = av_packet_alloc();
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dp->pkt)
155 goto fail;
156
157 6391 dp->index = -1;
158 6391 dp->dec.class = &dec_class;
159 6391 dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
160 6391 dp->last_frame_pts = AV_NOPTS_VALUE;
161 6391 dp->last_frame_tb = (AVRational){ 1, 1 };
162 6391 dp->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
163
164 6391 ret = sch_add_dec(sch, decoder_thread, dp, send_end_ts);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret < 0)
166 goto fail;
167 6391 dp->sch = sch;
168 6391 dp->sch_idx = ret;
169
170 6391 *pdec = dp;
171
172 6391 return 0;
173 fail:
174 dec_free((Decoder**)&dp);
175 return ret >= 0 ? AVERROR(ENOMEM) : ret;
176 }
177
178 250531 static AVRational audio_samplerate_update(DecoderPriv *dp,
179 const AVFrame *frame)
180 {
181 250531 const int prev = dp->last_frame_tb.den;
182 250531 const int sr = frame->sample_rate;
183
184 AVRational tb_new;
185 int64_t gcd;
186
187
2/2
✓ Branch 0 taken 249334 times.
✓ Branch 1 taken 1197 times.
250531 if (frame->sample_rate == dp->last_frame_sample_rate)
188 249334 goto finish;
189
190 1197 gcd = av_gcd(prev, sr);
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1197 times.
1197 if (prev / gcd >= INT_MAX / sr) {
193 av_log(dp, AV_LOG_WARNING,
194 "Audio timestamps cannot be represented exactly after "
195 "sample rate change: %d -> %d\n", prev, sr);
196
197 // LCM of 192000, 44100, allows to represent all common samplerates
198 tb_new = (AVRational){ 1, 28224000 };
199 } else
200 1197 tb_new = (AVRational){ 1, prev / gcd * sr };
201
202 // keep the frame timebase if it is strictly better than
203 // the samplerate-defined one
204
4/4
✓ Branch 0 taken 1181 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 1075 times.
1197 if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
205
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 65 times.
106 !(frame->time_base.den % tb_new.den))
206 41 tb_new = frame->time_base;
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1197 times.
1197 if (dp->last_frame_pts != AV_NOPTS_VALUE)
209 dp->last_frame_pts = av_rescale_q(dp->last_frame_pts,
210 dp->last_frame_tb, tb_new);
211 1197 dp->last_frame_duration_est = av_rescale_q(dp->last_frame_duration_est,
212 dp->last_frame_tb, tb_new);
213
214 1197 dp->last_frame_tb = tb_new;
215 1197 dp->last_frame_sample_rate = frame->sample_rate;
216
217 250531 finish:
218 250531 return dp->last_frame_tb;
219 }
220
221 250531 static void audio_ts_process(DecoderPriv *dp, AVFrame *frame)
222 {
223 250531 AVRational tb_filter = (AVRational){1, frame->sample_rate};
224 AVRational tb;
225 int64_t pts_pred;
226
227 // on samplerate change, choose a new internal timebase for timestamp
228 // generation that can represent timestamps from all the samplerates
229 // seen so far
230 250531 tb = audio_samplerate_update(dp, frame);
231
2/2
✓ Branch 0 taken 249334 times.
✓ Branch 1 taken 1197 times.
250531 pts_pred = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
232 249334 dp->last_frame_pts + dp->last_frame_duration_est;
233
234
2/2
✓ Branch 0 taken 50348 times.
✓ Branch 1 taken 200183 times.
250531 if (frame->pts == AV_NOPTS_VALUE) {
235 50348 frame->pts = pts_pred;
236 50348 frame->time_base = tb;
237
2/2
✓ Branch 0 taken 198999 times.
✓ Branch 1 taken 1184 times.
200183 } else if (dp->last_frame_pts != AV_NOPTS_VALUE &&
238
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 198389 times.
198999 frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
239 AV_ROUND_UP)) {
240 // there was a gap in timestamps, reset conversion state
241 610 dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
242 }
243
244 250531 frame->pts = av_rescale_delta(frame->time_base, frame->pts,
245 tb, frame->nb_samples,
246 &dp->last_filter_in_rescale_delta, tb);
247
248 250531 dp->last_frame_pts = frame->pts;
249 250531 dp->last_frame_duration_est = av_rescale_q(frame->nb_samples,
250 tb_filter, tb);
251
252 // finally convert to filtering timebase
253 250531 frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
254 250531 frame->duration = frame->nb_samples;
255 250531 frame->time_base = tb_filter;
256 250531 }
257
258 129941 static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame)
259 {
260 129941 const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE;
261 129941 const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED;
262 129941 int64_t codec_duration = 0;
263
264 // XXX lavf currently makes up frame durations when they are not provided by
265 // the container. As there is no way to reliably distinguish real container
266 // durations from the fake made-up ones, we use heuristics based on whether
267 // the container has timestamps. Eventually lavf should stop making up
268 // durations, then this should be simplified.
269
270 // prefer frame duration for containers with timestamps
271
6/6
✓ Branch 0 taken 128309 times.
✓ Branch 1 taken 1632 times.
✓ Branch 2 taken 33072 times.
✓ Branch 3 taken 95237 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 33065 times.
129941 if (frame->duration > 0 && (!ts_unreliable || fr_forced))
272 95244 return frame->duration;
273
274
4/4
✓ Branch 0 taken 34616 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 5354 times.
✓ Branch 3 taken 29262 times.
34697 if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) {
275 5354 int fields = frame->repeat_pict + 2;
276 5354 AVRational field_rate = av_mul_q(dp->dec_ctx->framerate,
277 5354 (AVRational){ 2, 1 });
278 5354 codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
279 frame->time_base);
280 }
281
282 // prefer codec-layer duration for containers without timestamps
283
4/4
✓ Branch 0 taken 5354 times.
✓ Branch 1 taken 29343 times.
✓ Branch 2 taken 5333 times.
✓ Branch 3 taken 21 times.
34697 if (codec_duration > 0 && ts_unreliable)
284 5333 return codec_duration;
285
286 // when timestamps are available, repeat last frame's actual duration
287 // (i.e. pts difference between this and last frame)
288
3/4
✓ Branch 0 taken 29364 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28857 times.
✓ Branch 3 taken 507 times.
29364 if (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE &&
289
1/2
✓ Branch 0 taken 28857 times.
✗ Branch 1 not taken.
28857 frame->pts > dp->last_frame_pts)
290 28857 return frame->pts - dp->last_frame_pts;
291
292 // try frame/codec duration
293
2/2
✓ Branch 0 taken 438 times.
✓ Branch 1 taken 69 times.
507 if (frame->duration > 0)
294 438 return frame->duration;
295
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 68 times.
69 if (codec_duration > 0)
296 1 return codec_duration;
297
298 // try average framerate
299
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
68 if (dp->framerate_in.num && dp->framerate_in.den) {
300 43 int64_t d = av_rescale_q(1, av_inv_q(dp->framerate_in),
301 frame->time_base);
302
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (d > 0)
303 43 return d;
304 }
305
306 // last resort is last frame's estimated duration, and 1
307 25 return FFMAX(dp->last_frame_duration_est, 1);
308 }
309
310 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
311 {
312 DecoderPriv *dp = avctx->opaque;
313 AVFrame *output = NULL;
314 enum AVPixelFormat output_format = dp->hwaccel_output_format;
315 int err;
316
317 if (input->format == output_format) {
318 // Nothing to do.
319 return 0;
320 }
321
322 output = av_frame_alloc();
323 if (!output)
324 return AVERROR(ENOMEM);
325
326 output->format = output_format;
327
328 err = av_hwframe_transfer_data(output, input, 0);
329 if (err < 0) {
330 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
331 "output frame: %d.\n", err);
332 goto fail;
333 }
334
335 err = av_frame_copy_props(output, input);
336 if (err < 0) {
337 av_frame_unref(output);
338 goto fail;
339 }
340
341 av_frame_unref(input);
342 av_frame_move_ref(input, output);
343 av_frame_free(&output);
344
345 return 0;
346
347 fail:
348 av_frame_free(&output);
349 return err;
350 }
351
352 129941 static int video_frame_process(DecoderPriv *dp, AVFrame *frame)
353 {
354 #if FFMPEG_OPT_TOP
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129941 times.
129941 if (dp->flags & DECODER_FLAG_TOP_FIELD_FIRST) {
356 av_log(dp, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
357 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
358 }
359 #endif
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129941 times.
129941 if (frame->format == dp->hwaccel_pix_fmt) {
362 int err = hwaccel_retrieve_data(dp->dec_ctx, frame);
363 if (err < 0)
364 return err;
365 }
366
367 129941 frame->pts = frame->best_effort_timestamp;
368
369 // forced fixed framerate
370
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 129622 times.
129941 if (dp->flags & DECODER_FLAG_FRAMERATE_FORCED) {
371 319 frame->pts = AV_NOPTS_VALUE;
372 319 frame->duration = 1;
373 319 frame->time_base = av_inv_q(dp->framerate_in);
374 }
375
376 // no timestamp available - extrapolate from previous frame duration
377
2/2
✓ Branch 0 taken 33445 times.
✓ Branch 1 taken 96496 times.
129941 if (frame->pts == AV_NOPTS_VALUE)
378
2/2
✓ Branch 0 taken 32902 times.
✓ Branch 1 taken 543 times.
66347 frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
379 32902 dp->last_frame_pts + dp->last_frame_duration_est;
380
381 // update timestamp history
382 129941 dp->last_frame_duration_est = video_duration_estimate(dp, frame);
383 129941 dp->last_frame_pts = frame->pts;
384 129941 dp->last_frame_tb = frame->time_base;
385
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129941 times.
129941 if (debug_ts) {
387 av_log(dp, AV_LOG_INFO,
388 "decoder -> pts:%s pts_time:%s "
389 "pkt_dts:%s pkt_dts_time:%s "
390 "duration:%s duration_time:%s "
391 "keyframe:%d frame_type:%d time_base:%d/%d\n",
392 av_ts2str(frame->pts),
393 av_ts2timestr(frame->pts, &frame->time_base),
394 av_ts2str(frame->pkt_dts),
395 av_ts2timestr(frame->pkt_dts, &frame->time_base),
396 av_ts2str(frame->duration),
397 av_ts2timestr(frame->duration, &frame->time_base),
398 !!(frame->flags & AV_FRAME_FLAG_KEY), frame->pict_type,
399 frame->time_base.num, frame->time_base.den);
400 }
401
402
2/2
✓ Branch 0 taken 13510 times.
✓ Branch 1 taken 116431 times.
129941 if (dp->sar_override.num)
403 13510 frame->sample_aspect_ratio = dp->sar_override;
404
405
1/2
✓ Branch 0 taken 129941 times.
✗ Branch 1 not taken.
129941 if (dp->apply_cropping) {
406 // lavfi does not require aligned frame data
407 129941 int ret = av_frame_apply_cropping(frame, AV_FRAME_CROP_UNALIGNED);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129941 times.
129941 if (ret < 0) {
409 av_log(dp, AV_LOG_ERROR, "Error applying decoder cropping\n");
410 return ret;
411 }
412 }
413
414 129941 return 0;
415 }
416
417 1 static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
418 {
419 1 int ret = AVERROR_BUG;
420 1 AVSubtitle tmp = {
421 1 .format = src->format,
422 1 .start_display_time = src->start_display_time,
423 1 .end_display_time = src->end_display_time,
424 .num_rects = 0,
425 .rects = NULL,
426 1 .pts = src->pts
427 };
428
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!src->num_rects)
430 goto success;
431
432
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
433 return AVERROR(ENOMEM);
434
435
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int i = 0; i < src->num_rects; i++) {
436 1 AVSubtitleRect *src_rect = src->rects[i];
437 AVSubtitleRect *dst_rect;
438
439
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
440 ret = AVERROR(ENOMEM);
441 goto cleanup;
442 }
443
444 1 tmp.num_rects++;
445
446 1 dst_rect->type = src_rect->type;
447 1 dst_rect->flags = src_rect->flags;
448
449 1 dst_rect->x = src_rect->x;
450 1 dst_rect->y = src_rect->y;
451 1 dst_rect->w = src_rect->w;
452 1 dst_rect->h = src_rect->h;
453 1 dst_rect->nb_colors = src_rect->nb_colors;
454
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (src_rect->text)
456 if (!(dst_rect->text = av_strdup(src_rect->text))) {
457 ret = AVERROR(ENOMEM);
458 goto cleanup;
459 }
460
461
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (src_rect->ass)
462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
463 ret = AVERROR(ENOMEM);
464 goto cleanup;
465 }
466
467
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int j = 0; j < 4; j++) {
468 // SUBTITLE_BITMAP images are special in the sense that they
469 // are like PAL8 images. first pointer to data, second to
470 // palette. This makes the size calculation match this.
471 size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 AVPALETTE_SIZE :
473 4 src_rect->h * src_rect->linesize[j];
474
475
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!src_rect->data[j])
476 4 continue;
477
478 if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
479 ret = AVERROR(ENOMEM);
480 goto cleanup;
481 }
482 dst_rect->linesize[j] = src_rect->linesize[j];
483 }
484 }
485
486 1 success:
487 1 *dst = tmp;
488
489 1 return 0;
490
491 cleanup:
492 avsubtitle_free(&tmp);
493
494 return ret;
495 }
496
497 834 static void subtitle_free(void *opaque, uint8_t *data)
498 {
499 834 AVSubtitle *sub = (AVSubtitle*)data;
500 834 avsubtitle_free(sub);
501 834 av_free(sub);
502 834 }
503
504 834 static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
505 {
506 AVBufferRef *buf;
507 AVSubtitle *sub;
508 int ret;
509
510
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 833 times.
834 if (copy) {
511 1 sub = av_mallocz(sizeof(*sub));
512
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
514 av_freep(&sub);
515 return ret;
516 }
517 } else {
518 833 sub = av_memdup(subtitle, sizeof(*subtitle));
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
833 if (!sub)
520 return AVERROR(ENOMEM);
521 833 memset(subtitle, 0, sizeof(*subtitle));
522 }
523
524 834 buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
525 subtitle_free, NULL, 0);
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834 times.
834 if (!buf) {
527 avsubtitle_free(sub);
528 av_freep(&sub);
529 return AVERROR(ENOMEM);
530 }
531
532 834 frame->buf[0] = buf;
533
534 834 return 0;
535 }
536
537 834 static int process_subtitle(DecoderPriv *dp, AVFrame *frame)
538 {
539 834 const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
540 834 int ret = 0;
541
542
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 817 times.
834 if (dp->flags & DECODER_FLAG_FIX_SUB_DURATION) {
543 34 AVSubtitle *sub_prev = dp->sub_prev[0]->buf[0] ?
544
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
545 17 int end = 1;
546
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 if (sub_prev) {
547 15 end = av_rescale(subtitle->pts - sub_prev->pts,
548 1000, AV_TIME_BASE);
549
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (end < sub_prev->end_display_time) {
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_log(dp, AV_LOG_DEBUG,
551 "Subtitle duration reduced from %"PRId32" to %d%s\n",
552 sub_prev->end_display_time, end,
553 end <= 0 ? ", dropping it" : "");
554 15 sub_prev->end_display_time = end;
555 }
556 }
557
558 17 av_frame_unref(dp->sub_prev[1]);
559 17 av_frame_move_ref(dp->sub_prev[1], frame);
560
561 17 frame = dp->sub_prev[0];
562
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL;
563
564 17 FFSWAP(AVFrame*, dp->sub_prev[0], dp->sub_prev[1]);
565
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (end <= 0)
567 return 0;
568 }
569
570
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 832 times.
834 if (!subtitle)
571 2 return 0;
572
573 832 ret = sch_dec_send(dp->sch, dp->sch_idx, frame);
574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 832 times.
832 if (ret < 0)
575 av_frame_unref(frame);
576
577
1/2
✓ Branch 0 taken 832 times.
✗ Branch 1 not taken.
832 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
578 }
579
580 4 static int fix_sub_duration_heartbeat(DecoderPriv *dp, int64_t signal_pts)
581 {
582 4 int ret = AVERROR_BUG;
583 8 AVSubtitle *prev_subtitle = dp->sub_prev[0]->buf[0] ?
584
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
585 AVSubtitle *subtitle;
586
587
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 ||
588
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)
589 3 return 0;
590
591 1 av_frame_unref(dp->sub_heartbeat);
592 1 ret = subtitle_wrap_frame(dp->sub_heartbeat, prev_subtitle, 1);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
594 return ret;
595
596 1 subtitle = (AVSubtitle*)dp->sub_heartbeat->buf[0]->data;
597 1 subtitle->pts = signal_pts;
598
599 1 return process_subtitle(dp, dp->sub_heartbeat);
600 }
601
602 2654 static int transcode_subtitles(DecoderPriv *dp, const AVPacket *pkt,
603 AVFrame *frame)
604 {
605 2654 AVPacket *flush_pkt = NULL;
606 AVSubtitle subtitle;
607 int got_output;
608 int ret;
609
610
4/4
✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 1666 times.
2654 if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT) {
611 948 frame->pts = pkt->pts;
612 948 frame->time_base = pkt->time_base;
613 948 frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT;
614
615 948 ret = sch_dec_send(dp->sch, dp->sch_idx, frame);
616
1/2
✓ Branch 0 taken 948 times.
✗ Branch 1 not taken.
948 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
617
4/4
✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1662 times.
1706 } else if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION) {
618 4 return fix_sub_duration_heartbeat(dp, av_rescale_q(pkt->pts, pkt->time_base,
619 4 AV_TIME_BASE_Q));
620 }
621
622
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1662 times.
1702 if (!pkt) {
623 40 flush_pkt = av_packet_alloc();
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!flush_pkt)
625 return AVERROR(ENOMEM);
626 }
627
628
2/2
✓ Branch 0 taken 1662 times.
✓ Branch 1 taken 40 times.
1702 ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output,
629 pkt ? pkt : flush_pkt);
630 1702 av_packet_free(&flush_pkt);
631
632
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1623 times.
1702 if (ret < 0) {
633 79 av_log(dp, AV_LOG_ERROR, "Error decoding subtitles: %s\n",
634 79 av_err2str(ret));
635 79 dp->dec.decode_errors++;
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 return exit_on_error ? ret : 0;
637 }
638
639
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 833 times.
1623 if (!got_output)
640
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 40 times.
790 return pkt ? 0 : AVERROR_EOF;
641
642 833 dp->dec.frames_decoded++;
643
644 // XXX the queue for transferring data to consumers runs
645 // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
646 // inside the frame
647 // eventually, subtitles should be switched to use AVFrames natively
648 833 ret = subtitle_wrap_frame(frame, &subtitle, 0);
649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
833 if (ret < 0) {
650 avsubtitle_free(&subtitle);
651 return ret;
652 }
653
654 833 frame->width = dp->dec_ctx->width;
655 833 frame->height = dp->dec_ctx->height;
656
657 833 return process_subtitle(dp, frame);
658 }
659
660 364373 static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame)
661 {
662 364373 AVCodecContext *dec = dp->dec_ctx;
663 364373 const char *type_desc = av_get_media_type_string(dec->codec_type);
664 int ret;
665
666
2/2
✓ Branch 0 taken 2654 times.
✓ Branch 1 taken 361719 times.
364373 if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
667 2654 return transcode_subtitles(dp, pkt, frame);
668
669 // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
670 // reason. This seems like a semi-critical bug. Don't trigger EOF, and
671 // skip the packet.
672
4/4
✓ Branch 0 taken 358493 times.
✓ Branch 1 taken 3226 times.
✓ Branch 2 taken 1480 times.
✓ Branch 3 taken 357013 times.
361719 if (pkt && pkt->size == 0)
673 1480 return 0;
674
675
4/4
✓ Branch 0 taken 357013 times.
✓ Branch 1 taken 3226 times.
✓ Branch 2 taken 58138 times.
✓ Branch 3 taken 298875 times.
360239 if (pkt && (dp->flags & DECODER_FLAG_TS_UNRELIABLE)) {
676 58138 pkt->pts = AV_NOPTS_VALUE;
677 58138 pkt->dts = AV_NOPTS_VALUE;
678 }
679
680
2/2
✓ Branch 0 taken 357013 times.
✓ Branch 1 taken 3226 times.
360239 if (pkt) {
681 357013 FrameData *fd = packet_data(pkt);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 357013 times.
357013 if (!fd)
683 return AVERROR(ENOMEM);
684 357013 fd->wallclock[LATENCY_PROBE_DEC_PRE] = av_gettime_relative();
685 }
686
687 360239 ret = avcodec_send_packet(dec, pkt);
688
3/6
✓ Branch 0 taken 359841 times.
✓ Branch 1 taken 398 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 398 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
360239 if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) {
689 // In particular, we don't expect AVERROR(EAGAIN), because we read all
690 // decoded frames with avcodec_receive_frame() until done.
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398 times.
398 if (ret == AVERROR(EAGAIN)) {
692 av_log(dp, AV_LOG_FATAL, "A decoder returned an unexpected error code. "
693 "This is a bug, please report it.\n");
694 return AVERROR_BUG;
695 }
696
1/2
✓ Branch 0 taken 398 times.
✗ Branch 1 not taken.
398 av_log(dp, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
697 398 pkt ? "packet" : "EOF", av_err2str(ret));
698
699
1/2
✓ Branch 0 taken 398 times.
✗ Branch 1 not taken.
398 if (ret != AVERROR_EOF) {
700 398 dp->dec.decode_errors++;
701
1/2
✓ Branch 0 taken 398 times.
✗ Branch 1 not taken.
398 if (!exit_on_error)
702 398 ret = 0;
703 }
704
705 398 return ret;
706 }
707
708 377346 while (1) {
709 FrameData *fd;
710
711 737187 av_frame_unref(frame);
712
713 737187 update_benchmark(NULL);
714 737187 ret = avcodec_receive_frame(dec, frame);
715 737187 update_benchmark("decode_%s %s", type_desc, dp->parent_name);
716
717
2/2
✓ Branch 0 taken 353484 times.
✓ Branch 1 taken 383703 times.
737187 if (ret == AVERROR(EAGAIN)) {
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353484 times.
353484 av_assert0(pkt); // should never happen during flushing
719 353484 return 0;
720
2/2
✓ Branch 0 taken 3225 times.
✓ Branch 1 taken 380478 times.
383703 } else if (ret == AVERROR_EOF) {
721 3225 return ret;
722
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 380472 times.
380478 } else if (ret < 0) {
723 6 av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
724 6 dp->dec.decode_errors++;
725
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (exit_on_error)
727 return ret;
728
729 6 continue;
730 }
731
732
3/4
✓ Branch 0 taken 380435 times.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 380435 times.
380472 if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) {
733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 av_log(dp, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
734 "corrupt decoded frame\n");
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (exit_on_error)
736 return AVERROR_INVALIDDATA;
737 }
738
739 380472 fd = frame_data(frame);
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380472 times.
380472 if (!fd) {
741 av_frame_unref(frame);
742 return AVERROR(ENOMEM);
743 }
744 380472 fd->dec.pts = frame->pts;
745 380472 fd->dec.tb = dec->pkt_timebase;
746 380472 fd->dec.frame_num = dec->frame_num - 1;
747 380472 fd->bits_per_raw_sample = dec->bits_per_raw_sample;
748
749 380472 fd->wallclock[LATENCY_PROBE_DEC_POST] = av_gettime_relative();
750
751 380472 frame->time_base = dec->pkt_timebase;
752
753
2/2
✓ Branch 0 taken 250531 times.
✓ Branch 1 taken 129941 times.
380472 if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
754 250531 dp->dec.samples_decoded += frame->nb_samples;
755
756 250531 audio_ts_process(dp, frame);
757 } else {
758 129941 ret = video_frame_process(dp, frame);
759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129941 times.
129941 if (ret < 0) {
760 av_log(dp, AV_LOG_FATAL,
761 "Error while processing the decoded data\n");
762 return ret;
763 }
764 }
765
766 380472 dp->dec.frames_decoded++;
767
768 380472 ret = sch_dec_send(dp->sch, dp->sch_idx, frame);
769
2/2
✓ Branch 0 taken 3132 times.
✓ Branch 1 taken 377340 times.
380472 if (ret < 0) {
770 3132 av_frame_unref(frame);
771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3132 times.
3132 return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
772 }
773 }
774 }
775
776 static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
777 const DecoderOpts *o, AVFrame *param_out);
778
779 1 static int dec_standalone_open(DecoderPriv *dp, const AVPacket *pkt)
780 {
781 DecoderOpts o;
782 const FrameData *fd;
783 char name[16];
784
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pkt->opaque_ref)
786 return AVERROR_BUG;
787 1 fd = (FrameData *)pkt->opaque_ref->data;
788
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fd->par_enc)
790 return AVERROR_BUG;
791
792 1 memset(&o, 0, sizeof(o));
793
794 1 o.par = fd->par_enc;
795 1 o.time_base = pkt->time_base;
796
797 1 o.codec = dp->standalone_init.codec;
798
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!o.codec)
799 1 o.codec = avcodec_find_decoder(o.par->codec_id);
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!o.codec) {
801 const AVCodecDescriptor *desc = avcodec_descriptor_get(o.par->codec_id);
802
803 av_log(dp, AV_LOG_ERROR, "Cannot find a decoder for codec ID '%s'\n",
804 desc ? desc->name : "?");
805 return AVERROR_DECODER_NOT_FOUND;
806 }
807
808 1 snprintf(name, sizeof(name), "dec%d", dp->index);
809 1 o.name = name;
810
811 1 return dec_open(dp, &dp->standalone_init.opts, &o, NULL);
812 }
813
814 6391 static void dec_thread_set_name(const DecoderPriv *dp)
815 {
816 6391 char name[16] = "dec";
817
818
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6390 times.
6391 if (dp->index >= 0)
819 1 av_strlcatf(name, sizeof(name), "%d", dp->index);
820
1/2
✓ Branch 0 taken 6390 times.
✗ Branch 1 not taken.
6390 else if (dp->parent_name)
821 6390 av_strlcat(name, dp->parent_name, sizeof(name));
822
823
2/2
✓ Branch 0 taken 6390 times.
✓ Branch 1 taken 1 times.
6391 if (dp->dec_ctx)
824 6390 av_strlcatf(name, sizeof(name), ":%s", dp->dec_ctx->codec->name);
825
826 6391 ff_thread_setname(name);
827 6391 }
828
829 6391 static void dec_thread_uninit(DecThreadContext *dt)
830 {
831 6391 av_packet_free(&dt->pkt);
832 6391 av_frame_free(&dt->frame);
833
834 6391 memset(dt, 0, sizeof(*dt));
835 6391 }
836
837 6391 static int dec_thread_init(DecThreadContext *dt)
838 {
839 6391 memset(dt, 0, sizeof(*dt));
840
841 6391 dt->frame = av_frame_alloc();
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dt->frame)
843 goto fail;
844
845 6391 dt->pkt = av_packet_alloc();
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dt->pkt)
847 goto fail;
848
849 6391 return 0;
850
851 fail:
852 dec_thread_uninit(dt);
853 return AVERROR(ENOMEM);
854 }
855
856 6391 static int decoder_thread(void *arg)
857 {
858 6391 DecoderPriv *dp = arg;
859 DecThreadContext dt;
860 6391 int ret = 0, input_status = 0;
861
862 6391 ret = dec_thread_init(&dt);
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret < 0)
864 goto finish;
865
866 6391 dec_thread_set_name(dp);
867
868
1/2
✓ Branch 0 taken 364373 times.
✗ Branch 1 not taken.
364373 while (!input_status) {
869 int flush_buffers, have_data;
870
871 364373 input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt);
872
2/2
✓ Branch 0 taken 361113 times.
✓ Branch 1 taken 3260 times.
725486 have_data = input_status >= 0 &&
873
3/4
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 360155 times.
✓ Branch 2 taken 958 times.
✗ Branch 3 not taken.
361113 (dt.pkt->buf || dt.pkt->side_data_elems ||
874
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 948 times.
958 (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
875
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
876
4/4
✓ Branch 0 taken 361113 times.
✓ Branch 1 taken 3260 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 361107 times.
364373 flush_buffers = input_status >= 0 && !have_data;
877
2/2
✓ Branch 0 taken 3266 times.
✓ Branch 1 taken 361107 times.
364373 if (!have_data)
878
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3260 times.
3266 av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
879 flush_buffers ? "flush" : "EOF");
880
881 // this is a standalone decoder that has not been initialized yet
882
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 364372 times.
364373 if (!dp->dec_ctx) {
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (flush_buffers)
884 continue;
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (input_status < 0) {
886 av_log(dp, AV_LOG_ERROR,
887 "Cannot initialize a standalone decoder\n");
888 ret = input_status;
889 goto finish;
890 }
891
892 1 ret = dec_standalone_open(dp, dt.pkt);
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
894 goto finish;
895 }
896
897
2/2
✓ Branch 0 taken 361107 times.
✓ Branch 1 taken 3266 times.
364373 ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame);
898
899 364373 av_packet_unref(dt.pkt);
900 364373 av_frame_unref(dt.frame);
901
902 // AVERROR_EOF - EOF from the decoder
903 // AVERROR_EXIT - EOF from the scheduler
904 // we treat them differently when flushing
905
2/2
✓ Branch 0 taken 3132 times.
✓ Branch 1 taken 361241 times.
364373 if (ret == AVERROR_EXIT) {
906 3132 ret = AVERROR_EOF;
907 3132 flush_buffers = 0;
908 }
909
910
2/2
✓ Branch 0 taken 6397 times.
✓ Branch 1 taken 357976 times.
364373 if (ret == AVERROR_EOF) {
911
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6391 times.
6397 av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
912 flush_buffers ? "resetting" : "finishing");
913
914
2/2
✓ Branch 0 taken 6391 times.
✓ Branch 1 taken 6 times.
6397 if (!flush_buffers)
915 6391 break;
916
917 /* report last frame duration to the scheduler */
918
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
919 3 dt.pkt->pts = dp->last_frame_pts + dp->last_frame_duration_est;
920 3 dt.pkt->time_base = dp->last_frame_tb;
921 }
922
923 6 avcodec_flush_buffers(dp->dec_ctx);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 357976 times.
357976 } else if (ret < 0) {
925 av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
926 av_err2str(ret));
927 break;
928 }
929 }
930
931 // EOF is normal thread termination
932
1/2
✓ Branch 0 taken 6391 times.
✗ Branch 1 not taken.
6391 if (ret == AVERROR_EOF)
933 6391 ret = 0;
934
935 // on success send EOF timestamp to our downstreams
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret >= 0) {
937 float err_rate;
938
939 6391 av_frame_unref(dt.frame);
940
941 6391 dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
942
2/2
✓ Branch 0 taken 6348 times.
✓ Branch 1 taken 43 times.
6391 dt.frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
943 6348 dp->last_frame_pts + dp->last_frame_duration_est;
944 6391 dt.frame->time_base = dp->last_frame_tb;
945
946 6391 ret = sch_dec_send(dp->sch, dp->sch_idx, dt.frame);
947
3/4
✓ Branch 0 taken 3140 times.
✓ Branch 1 taken 3251 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3140 times.
6391 if (ret < 0 && ret != AVERROR_EOF) {
948 av_log(dp, AV_LOG_FATAL,
949 "Error signalling EOF timestamp: %s\n", av_err2str(ret));
950 goto finish;
951 }
952 6391 ret = 0;
953
954
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ?
955
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6388 times.
6394 dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f;
956
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6390 times.
6391 if (err_rate > max_error_rate) {
957 1 av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
958 err_rate, max_error_rate);
959 1 ret = FFMPEG_ERROR_RATE_EXCEEDED;
960
2/2
✓ Branch 0 taken 6389 times.
✓ Branch 1 taken 1 times.
6390 } else if (err_rate)
961 1 av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
962 }
963
964 6389 finish:
965 6391 dec_thread_uninit(&dt);
966
967 6391 return ret;
968 }
969
970 951 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
971 {
972 951 DecoderPriv *dp = s->opaque;
973 const enum AVPixelFormat *p;
974
975
1/2
✓ Branch 0 taken 2608 times.
✗ Branch 1 not taken.
2608 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
976 2608 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
977 2608 const AVCodecHWConfig *config = NULL;
978
979
2/2
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 1657 times.
2608 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
980 951 break;
981
982
1/2
✓ Branch 0 taken 1657 times.
✗ Branch 1 not taken.
1657 if (dp->hwaccel_id == HWACCEL_GENERIC ||
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1657 times.
1657 dp->hwaccel_id == HWACCEL_AUTO) {
984 for (int i = 0;; i++) {
985 config = avcodec_get_hw_config(s->codec, i);
986 if (!config)
987 break;
988 if (!(config->methods &
989 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
990 continue;
991 if (config->pix_fmt == *p)
992 break;
993 }
994 }
995
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1657 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1657 if (config && config->device_type == dp->hwaccel_device_type) {
996 dp->hwaccel_pix_fmt = *p;
997 break;
998 }
999 }
1000
1001 951 return *p;
1002 }
1003
1004 6391 static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
1005 {
1006 const AVCodecHWConfig *config;
1007 HWDevice *dev;
1008 6391 for (int i = 0;; i++) {
1009 8044 config = avcodec_get_hw_config(codec, i);
1010
2/2
✓ Branch 0 taken 6391 times.
✓ Branch 1 taken 1653 times.
8044 if (!config)
1011 6391 return NULL;
1012
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1653 times.
1653 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1013 continue;
1014 1653 dev = hw_device_get_by_type(config->device_type);
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1653 times.
1653 if (dev)
1016 return dev;
1017 }
1018 }
1019
1020 6391 static int hw_device_setup_for_decode(DecoderPriv *dp,
1021 const AVCodec *codec,
1022 const char *hwaccel_device)
1023 {
1024 const AVCodecHWConfig *config;
1025 enum AVHWDeviceType type;
1026 6391 HWDevice *dev = NULL;
1027 6391 int err, auto_device = 0;
1028
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (hwaccel_device) {
1030 dev = hw_device_get_by_name(hwaccel_device);
1031 if (!dev) {
1032 if (dp->hwaccel_id == HWACCEL_AUTO) {
1033 auto_device = 1;
1034 } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
1035 type = dp->hwaccel_device_type;
1036 err = hw_device_init_from_type(type, hwaccel_device,
1037 &dev);
1038 } else {
1039 // This will be dealt with by API-specific initialisation
1040 // (using hwaccel_device), so nothing further needed here.
1041 return 0;
1042 }
1043 } else {
1044 if (dp->hwaccel_id == HWACCEL_AUTO) {
1045 dp->hwaccel_device_type = dev->type;
1046 } else if (dp->hwaccel_device_type != dev->type) {
1047 av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device "
1048 "specified for decoder: device %s of type %s is not "
1049 "usable with hwaccel %s.\n", dev->name,
1050 av_hwdevice_get_type_name(dev->type),
1051 av_hwdevice_get_type_name(dp->hwaccel_device_type));
1052 return AVERROR(EINVAL);
1053 }
1054 }
1055 } else {
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (dp->hwaccel_id == HWACCEL_AUTO) {
1057 auto_device = 1;
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
1059 type = dp->hwaccel_device_type;
1060 dev = hw_device_get_by_type(type);
1061
1062 // When "-qsv_device device" is used, an internal QSV device named
1063 // as "__qsv_device" is created. Another QSV device is created too
1064 // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
1065 // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
1066 // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
1067 // To keep back-compatibility with the removed ad-hoc libmfx setup code,
1068 // call hw_device_get_by_name("__qsv_device") to select the internal QSV
1069 // device.
1070 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
1071 dev = hw_device_get_by_name("__qsv_device");
1072
1073 if (!dev)
1074 err = hw_device_init_from_type(type, NULL, &dev);
1075 } else {
1076 6391 dev = hw_device_match_by_codec(codec);
1077
1/2
✓ Branch 0 taken 6391 times.
✗ Branch 1 not taken.
6391 if (!dev) {
1078 // No device for this codec, but not using generic hwaccel
1079 // and therefore may well not need one - ignore.
1080 6391 return 0;
1081 }
1082 }
1083 }
1084
1085 if (auto_device) {
1086 if (!avcodec_get_hw_config(codec, 0)) {
1087 // Decoder does not support any hardware devices.
1088 return 0;
1089 }
1090 for (int i = 0; !dev; i++) {
1091 config = avcodec_get_hw_config(codec, i);
1092 if (!config)
1093 break;
1094 type = config->device_type;
1095 dev = hw_device_get_by_type(type);
1096 if (dev) {
1097 av_log(dp, AV_LOG_INFO, "Using auto "
1098 "hwaccel type %s with existing device %s.\n",
1099 av_hwdevice_get_type_name(type), dev->name);
1100 }
1101 }
1102 for (int i = 0; !dev; i++) {
1103 config = avcodec_get_hw_config(codec, i);
1104 if (!config)
1105 break;
1106 type = config->device_type;
1107 // Try to make a new device of this type.
1108 err = hw_device_init_from_type(type, hwaccel_device,
1109 &dev);
1110 if (err < 0) {
1111 // Can't make a device of this type.
1112 continue;
1113 }
1114 if (hwaccel_device) {
1115 av_log(dp, AV_LOG_INFO, "Using auto "
1116 "hwaccel type %s with new device created "
1117 "from %s.\n", av_hwdevice_get_type_name(type),
1118 hwaccel_device);
1119 } else {
1120 av_log(dp, AV_LOG_INFO, "Using auto "
1121 "hwaccel type %s with new default device.\n",
1122 av_hwdevice_get_type_name(type));
1123 }
1124 }
1125 if (dev) {
1126 dp->hwaccel_device_type = type;
1127 } else {
1128 av_log(dp, AV_LOG_INFO, "Auto hwaccel "
1129 "disabled: no device found.\n");
1130 dp->hwaccel_id = HWACCEL_NONE;
1131 return 0;
1132 }
1133 }
1134
1135 if (!dev) {
1136 av_log(dp, AV_LOG_ERROR, "No device available "
1137 "for decoder: device type %s needed for codec %s.\n",
1138 av_hwdevice_get_type_name(type), codec->name);
1139 return err;
1140 }
1141
1142 dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
1143 if (!dp->dec_ctx->hw_device_ctx)
1144 return AVERROR(ENOMEM);
1145
1146 return 0;
1147 }
1148
1149 6391 static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
1150 const DecoderOpts *o, AVFrame *param_out)
1151 {
1152 6391 const AVCodec *codec = o->codec;
1153 int ret;
1154
1155 6391 dp->flags = o->flags;
1156 6391 dp->log_parent = o->log_parent;
1157
1158 6391 dp->dec.type = codec->type;
1159 6391 dp->framerate_in = o->framerate;
1160
1161 6391 dp->hwaccel_id = o->hwaccel_id;
1162 6391 dp->hwaccel_device_type = o->hwaccel_device_type;
1163 6391 dp->hwaccel_output_format = o->hwaccel_output_format;
1164
1165 6391 snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
1166
1167
1/2
✓ Branch 0 taken 6391 times.
✗ Branch 1 not taken.
6391 dp->parent_name = av_strdup(o->name ? o->name : "");
1168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dp->parent_name)
1169 return AVERROR(ENOMEM);
1170
1171
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 6351 times.
6391 if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
1172
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
40 (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) {
1173
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++) {
1174 4 dp->sub_prev[i] = av_frame_alloc();
1175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!dp->sub_prev[i])
1176 return AVERROR(ENOMEM);
1177 }
1178 2 dp->sub_heartbeat = av_frame_alloc();
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!dp->sub_heartbeat)
1180 return AVERROR(ENOMEM);
1181 }
1182
1183 6391 dp->sar_override = o->par->sample_aspect_ratio;
1184
1185 6391 dp->dec_ctx = avcodec_alloc_context3(codec);
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (!dp->dec_ctx)
1187 return AVERROR(ENOMEM);
1188
1189 6391 ret = avcodec_parameters_to_context(dp->dec_ctx, o->par);
1190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret < 0) {
1191 av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1192 return ret;
1193 }
1194
1195 6391 dp->dec_ctx->opaque = dp;
1196 6391 dp->dec_ctx->get_format = get_format;
1197 6391 dp->dec_ctx->pkt_timebase = o->time_base;
1198
1199
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 6334 times.
6391 if (!av_dict_get(*dec_opts, "threads", NULL, 0))
1200 57 av_dict_set(dec_opts, "threads", "auto", 0);
1201
1202 6391 ret = hw_device_setup_for_decode(dp, codec, o->hwaccel_device);
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret < 0) {
1204 av_log(dp, AV_LOG_ERROR,
1205 "Hardware device setup failed for decoder: %s\n",
1206 av_err2str(ret));
1207 return ret;
1208 }
1209
1210 6391 ret = av_opt_set_dict2(dp->dec_ctx, dec_opts, AV_OPT_SEARCH_CHILDREN);
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret < 0) {
1212 av_log(dp, AV_LOG_ERROR, "Error applying decoder options: %s\n",
1213 av_err2str(ret));
1214 return ret;
1215 }
1216 6391 ret = check_avoptions(*dec_opts);
1217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (ret < 0)
1218 return ret;
1219
1220 6391 dp->dec_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
1221
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 6313 times.
6391 if (o->flags & DECODER_FLAG_BITEXACT)
1222 78 dp->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1223
1224 // we apply cropping outselves
1225 6391 dp->apply_cropping = dp->dec_ctx->apply_cropping;
1226 6391 dp->dec_ctx->apply_cropping = 0;
1227
1228
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6391 times.
6391 if ((ret = avcodec_open2(dp->dec_ctx, codec, NULL)) < 0) {
1229 av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
1230 av_err2str(ret));
1231 return ret;
1232 }
1233
1234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6391 times.
6391 if (dp->dec_ctx->hw_device_ctx) {
1235 // Update decoder extra_hw_frames option to account for the
1236 // frames held in queues inside the ffmpeg utility. This is
1237 // called after avcodec_open2() because the user-set value of
1238 // extra_hw_frames becomes valid in there, and we need to add
1239 // this on top of it.
1240 int extra_frames = DEFAULT_FRAME_THREAD_QUEUE_SIZE;
1241 if (dp->dec_ctx->extra_hw_frames >= 0)
1242 dp->dec_ctx->extra_hw_frames += extra_frames;
1243 else
1244 dp->dec_ctx->extra_hw_frames = extra_frames;
1245 }
1246
1247 6391 dp->dec.subtitle_header = dp->dec_ctx->subtitle_header;
1248 6391 dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
1249
1250
2/2
✓ Branch 0 taken 6390 times.
✓ Branch 1 taken 1 times.
6391 if (param_out) {
1251
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 5190 times.
6390 if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1252 1200 param_out->format = dp->dec_ctx->sample_fmt;
1253 1200 param_out->sample_rate = dp->dec_ctx->sample_rate;
1254
1255 1200 ret = av_channel_layout_copy(&param_out->ch_layout, &dp->dec_ctx->ch_layout);
1256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1200 times.
1200 if (ret < 0)
1257 return ret;
1258
2/2
✓ Branch 0 taken 5150 times.
✓ Branch 1 taken 40 times.
5190 } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1259 5150 param_out->format = dp->dec_ctx->pix_fmt;
1260 5150 param_out->width = dp->dec_ctx->width;
1261 5150 param_out->height = dp->dec_ctx->height;
1262 5150 param_out->sample_aspect_ratio = dp->dec_ctx->sample_aspect_ratio;
1263 5150 param_out->colorspace = dp->dec_ctx->colorspace;
1264 5150 param_out->color_range = dp->dec_ctx->color_range;
1265 }
1266
1267 6390 param_out->time_base = dp->dec_ctx->pkt_timebase;
1268 }
1269
1270 6391 return 0;
1271 }
1272
1273 6390 int dec_init(Decoder **pdec, Scheduler *sch,
1274 AVDictionary **dec_opts, const DecoderOpts *o,
1275 AVFrame *param_out)
1276 {
1277 DecoderPriv *dp;
1278 int ret;
1279
1280 6390 *pdec = NULL;
1281
1282 6390 ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
1283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
6390 if (ret < 0)
1284 return ret;
1285
1286 6390 ret = dec_open(dp, dec_opts, o, param_out);
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6390 times.
6390 if (ret < 0)
1288 goto fail;
1289
1290 6390 *pdec = &dp->dec;
1291
1292 6390 return dp->sch_idx;
1293 fail:
1294 dec_free((Decoder**)&dp);
1295 return ret;
1296 }
1297
1298 1 int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch)
1299 {
1300 DecoderPriv *dp;
1301
1302 OutputFile *of;
1303 OutputStream *ost;
1304 int of_index, ost_index;
1305 char *p;
1306
1307 unsigned enc_idx;
1308 int ret;
1309
1310 1 ret = dec_alloc(&dp, sch, 0);
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1312 return ret;
1313
1314 1 dp->index = nb_decoders;
1315
1316 1 ret = GROW_ARRAY(decoders, nb_decoders);
1317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
1318 dec_free((Decoder **)&dp);
1319 return ret;
1320 }
1321
1322 1 decoders[nb_decoders - 1] = (Decoder *)dp;
1323
1324 1 of_index = strtol(arg, &p, 0);
1325
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) {
1326 av_log(dp, AV_LOG_ERROR, "Invalid output file index '%d' in %s\n", of_index, arg);
1327 return AVERROR(EINVAL);
1328 }
1329 1 of = output_files[of_index];
1330
1331 1 ost_index = strtol(p + 1, NULL, 0);
1332
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) {
1333 av_log(dp, AV_LOG_ERROR, "Invalid output stream index '%d' in %s\n", ost_index, arg);
1334 return AVERROR(EINVAL);
1335 }
1336 1 ost = of->streams[ost_index];
1337
1338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ost->enc) {
1339 av_log(dp, AV_LOG_ERROR, "Output stream %s has no encoder\n", arg);
1340 return AVERROR(EINVAL);
1341 }
1342
1343 1 dp->dec.type = ost->type;
1344
1345 1 ret = enc_loopback(ost->enc);
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1347 return ret;
1348 1 enc_idx = ret;
1349
1350 1 ret = sch_connect(sch, SCH_ENC(enc_idx), SCH_DEC(dp->sch_idx));
1351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1352 return ret;
1353
1354 1 ret = av_dict_copy(&dp->standalone_init.opts, o->g->codec_opts, 0);
1355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1356 return ret;
1357
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (o->codec_names.nb_opt) {
1359 const char *name = o->codec_names.opt[o->codec_names.nb_opt - 1].u.str;
1360 dp->standalone_init.codec = avcodec_find_decoder_by_name(name);
1361 if (!dp->standalone_init.codec) {
1362 av_log(dp, AV_LOG_ERROR, "No such decoder: %s\n", name);
1363 return AVERROR_DECODER_NOT_FOUND;
1364 }
1365 }
1366
1367 1 return 0;
1368 }
1369
1370 1 int dec_filter_add(Decoder *d, InputFilter *ifilter, InputFilterOptions *opts)
1371 {
1372 1 DecoderPriv *dp = dp_from_dec(d);
1373 char name[16];
1374
1375 1 snprintf(name, sizeof(name), "dec%d", dp->index);
1376 1 opts->name = av_strdup(name);
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!opts->name)
1378 return AVERROR(ENOMEM);
1379
1380 1 return dp->sch_idx;
1381 }
1382