FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 738 1149 64.2%
Functions: 56 61 91.8%
Branches: 473 845 56.0%

Line Branch Exec Source
1 /*
2 * generic decoding-related code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include "config.h"
25
26 #if CONFIG_ICONV
27 # include <iconv.h>
28 #endif
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/common.h"
33 #include "libavutil/emms.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/hwcontext.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mastering_display_metadata.h"
39 #include "libavutil/mem.h"
40
41 #include "avcodec.h"
42 #include "avcodec_internal.h"
43 #include "bytestream.h"
44 #include "bsf.h"
45 #include "codec_desc.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 #include "hwaccel_internal.h"
49 #include "hwconfig.h"
50 #include "internal.h"
51 #include "packet_internal.h"
52 #include "progressframe.h"
53 #include "refstruct.h"
54 #include "thread.h"
55 #include "threadprogress.h"
56
57 typedef struct DecodeContext {
58 AVCodecInternal avci;
59
60 /**
61 * This is set to AV_FRAME_FLAG_KEY for decoders of intra-only formats
62 * (those whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set)
63 * to set the flag generically.
64 */
65 int intra_only_flag;
66
67 /**
68 * This is set to AV_PICTURE_TYPE_I for intra only video decoders
69 * and to AV_PICTURE_TYPE_NONE for other decoders. It is used to set
70 * the AVFrame's pict_type before the decoder receives it.
71 */
72 enum AVPictureType initial_pict_type;
73
74 /* to prevent infinite loop on errors when draining */
75 int nb_draining_errors;
76
77 /**
78 * The caller has submitted a NULL packet on input.
79 */
80 int draining_started;
81
82 int64_t pts_correction_num_faulty_pts; /// Number of incorrect PTS values so far
83 int64_t pts_correction_num_faulty_dts; /// Number of incorrect DTS values so far
84 int64_t pts_correction_last_pts; /// PTS of the last frame
85 int64_t pts_correction_last_dts; /// DTS of the last frame
86
87 /**
88 * Bitmask indicating for which side data types we prefer user-supplied
89 * (global or attached to packets) side data over bytestream.
90 */
91 uint64_t side_data_pref_mask;
92 } DecodeContext;
93
94 2730135 static DecodeContext *decode_ctx(AVCodecInternal *avci)
95 {
96 2730135 return (DecodeContext *)avci;
97 }
98
99 380276 static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
100 {
101 int ret;
102 size_t size;
103 const uint8_t *data;
104 uint32_t flags;
105 int64_t val;
106
107 380276 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
108
2/2
✓ Branch 0 taken 380274 times.
✓ Branch 1 taken 2 times.
380276 if (!data)
109 380274 return 0;
110
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
112 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
113 "changes, but PARAM_CHANGE side data was sent to it.\n");
114 ret = AVERROR(EINVAL);
115 goto fail2;
116 }
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 4)
119 goto fail;
120
121 2 flags = bytestream_get_le32(&data);
122 2 size -= 4;
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
125 if (size < 4)
126 goto fail;
127 val = bytestream_get_le32(&data);
128 if (val <= 0 || val > INT_MAX) {
129 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
130 ret = AVERROR_INVALIDDATA;
131 goto fail2;
132 }
133 avctx->sample_rate = val;
134 size -= 4;
135 }
136
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 8)
138 goto fail;
139 2 avctx->width = bytestream_get_le32(&data);
140 2 avctx->height = bytestream_get_le32(&data);
141 2 size -= 8;
142 2 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
144 goto fail2;
145 }
146
147 2 return 0;
148 fail:
149 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
150 ret = AVERROR_INVALIDDATA;
151 fail2:
152 if (ret < 0) {
153 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
154 if (avctx->err_recognition & AV_EF_EXPLODE)
155 return ret;
156 }
157 return 0;
158 }
159
160 380276 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
161 {
162 380276 int ret = 0;
163
164 380276 av_packet_unref(avci->last_pkt_props);
165
1/2
✓ Branch 0 taken 380276 times.
✗ Branch 1 not taken.
380276 if (pkt) {
166 380276 ret = av_packet_copy_props(avci->last_pkt_props, pkt);
167 #if FF_API_FRAME_PKT
168
1/2
✓ Branch 0 taken 380276 times.
✗ Branch 1 not taken.
380276 if (!ret)
169 380276 avci->last_pkt_props->stream_index = pkt->size; // Needed for ff_decode_frame_props().
170 #endif
171 }
172 380276 return ret;
173 }
174
175 14518 static int decode_bsfs_init(AVCodecContext *avctx)
176 {
177 14518 AVCodecInternal *avci = avctx->internal;
178 14518 const FFCodec *const codec = ffcodec(avctx->codec);
179 int ret;
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (avci->bsf)
182 return 0;
183
184 14518 ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (ret < 0) {
186 av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret));
187 if (ret != AVERROR(ENOMEM))
188 ret = AVERROR_BUG;
189 goto fail;
190 }
191
192 /* We do not currently have an API for passing the input timebase into decoders,
193 * but no filters used here should actually need it.
194 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
195 14518 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
196 14518 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (ret < 0)
198 goto fail;
199
200 14518 ret = av_bsf_init(avci->bsf);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (ret < 0)
202 goto fail;
203
204 14518 return 0;
205 fail:
206 av_bsf_free(&avci->bsf);
207 return ret;
208 }
209
210 1143335 static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
211 {
212 1143335 AVCodecInternal *avci = avctx->internal;
213 int ret;
214
215 1143335 ret = av_bsf_receive_packet(avci->bsf, pkt);
216
2/2
✓ Branch 0 taken 3349 times.
✓ Branch 1 taken 1139986 times.
1143335 if (ret == AVERROR_EOF)
217 3349 avci->draining = 1;
218
2/2
✓ Branch 0 taken 763059 times.
✓ Branch 1 taken 380276 times.
1143335 if (ret < 0)
219 763059 return ret;
220
221
1/2
✓ Branch 1 taken 380276 times.
✗ Branch 2 not taken.
380276 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
222 380276 ret = extract_packet_props(avctx->internal, pkt);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380276 times.
380276 if (ret < 0)
224 goto finish;
225 }
226
227 380276 ret = apply_param_change(avctx, pkt);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380276 times.
380276 if (ret < 0)
229 goto finish;
230
231 380276 return 0;
232 finish:
233 av_packet_unref(pkt);
234 return ret;
235 }
236
237 759791 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
238 {
239 759791 AVCodecInternal *avci = avctx->internal;
240 759791 DecodeContext *dc = decode_ctx(avci);
241
242
2/2
✓ Branch 0 taken 759774 times.
✓ Branch 1 taken 17 times.
759791 if (avci->draining)
243 17 return AVERROR_EOF;
244
245 383561 while (1) {
246 1143335 int ret = decode_get_packet(avctx, pkt);
247
2/2
✓ Branch 0 taken 759710 times.
✓ Branch 1 taken 383625 times.
1143335 if (ret == AVERROR(EAGAIN) &&
248
5/6
✓ Branch 0 taken 379498 times.
✓ Branch 1 taken 380212 times.
✓ Branch 2 taken 379498 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3349 times.
✓ Branch 5 taken 376149 times.
759710 (!AVPACKET_IS_EMPTY(avci->buffer_pkt) || dc->draining_started)) {
249 383561 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383561 times.
383561 if (ret < 0) {
251 av_packet_unref(avci->buffer_pkt);
252 return ret;
253 }
254
255 383561 continue;
256 }
257
258 759774 return ret;
259 }
260 }
261
262 /**
263 * Attempt to guess proper monotonic timestamps for decoded video frames
264 * which might have incorrect times. Input timestamps may wrap around, in
265 * which case the output will as well.
266 *
267 * @param pts the pts field of the decoded AVPacket, as passed through
268 * AVFrame.pts
269 * @param dts the dts field of the decoded AVPacket
270 * @return one of the input values, may be AV_NOPTS_VALUE
271 */
272 398608 static int64_t guess_correct_pts(DecodeContext *dc,
273 int64_t reordered_pts, int64_t dts)
274 {
275 398608 int64_t pts = AV_NOPTS_VALUE;
276
277
2/2
✓ Branch 0 taken 309253 times.
✓ Branch 1 taken 89355 times.
398608 if (dts != AV_NOPTS_VALUE) {
278 309253 dc->pts_correction_num_faulty_dts += dts <= dc->pts_correction_last_dts;
279 309253 dc->pts_correction_last_dts = dts;
280
2/2
✓ Branch 0 taken 353 times.
✓ Branch 1 taken 89002 times.
89355 } else if (reordered_pts != AV_NOPTS_VALUE)
281 353 dc->pts_correction_last_dts = reordered_pts;
282
283
2/2
✓ Branch 0 taken 308912 times.
✓ Branch 1 taken 89696 times.
398608 if (reordered_pts != AV_NOPTS_VALUE) {
284 308912 dc->pts_correction_num_faulty_pts += reordered_pts <= dc->pts_correction_last_pts;
285 308912 dc->pts_correction_last_pts = reordered_pts;
286
2/2
✓ Branch 0 taken 694 times.
✓ Branch 1 taken 89002 times.
89696 } else if(dts != AV_NOPTS_VALUE)
287 694 dc->pts_correction_last_pts = dts;
288
289
4/4
✓ Branch 0 taken 566 times.
✓ Branch 1 taken 398042 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 557 times.
398608 if ((dc->pts_correction_num_faulty_pts<=dc->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
290
2/2
✓ Branch 0 taken 308355 times.
✓ Branch 1 taken 89696 times.
398051 && reordered_pts != AV_NOPTS_VALUE)
291 308355 pts = reordered_pts;
292 else
293 90253 pts = dts;
294
295 398608 return pts;
296 }
297
298 261334 static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
299 {
300 261334 AVCodecInternal *avci = avctx->internal;
301 AVFrameSideData *side;
302 261334 uint32_t discard_padding = 0;
303 261334 uint8_t skip_reason = 0;
304 261334 uint8_t discard_reason = 0;
305
306 261334 side = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
307
3/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 261218 times.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
261334 if (side && side->size >= 10) {
308 116 avci->skip_samples = AV_RL32(side->data);
309 116 avci->skip_samples = FFMAX(0, avci->skip_samples);
310 116 discard_padding = AV_RL32(side->data + 4);
311 116 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
312 avci->skip_samples, (int)discard_padding);
313 116 skip_reason = AV_RL8(side->data + 8);
314 116 discard_reason = AV_RL8(side->data + 9);
315 }
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261334 times.
261334 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
318 if (!side && (avci->skip_samples || discard_padding))
319 side = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
320 if (side && (avci->skip_samples || discard_padding)) {
321 AV_WL32(side->data, avci->skip_samples);
322 AV_WL32(side->data + 4, discard_padding);
323 AV_WL8(side->data + 8, skip_reason);
324 AV_WL8(side->data + 9, discard_reason);
325 avci->skip_samples = 0;
326 }
327 return 0;
328 }
329 261334 av_frame_remove_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
330
331
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 261314 times.
261334 if ((frame->flags & AV_FRAME_FLAG_DISCARD)) {
332 20 avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
333 20 *discarded_samples += frame->nb_samples;
334 20 return AVERROR(EAGAIN);
335 }
336
337
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 261093 times.
261314 if (avci->skip_samples > 0) {
338
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 65 times.
221 if (frame->nb_samples <= avci->skip_samples){
339 156 *discarded_samples += frame->nb_samples;
340 156 avci->skip_samples -= frame->nb_samples;
341 156 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
342 avci->skip_samples);
343 156 return AVERROR(EAGAIN);
344 } else {
345 65 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
346 65 frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
347
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
130 if (avctx->pkt_timebase.num && avctx->sample_rate) {
348 65 int64_t diff_ts = av_rescale_q(avci->skip_samples,
349 65 (AVRational){1, avctx->sample_rate},
350 avctx->pkt_timebase);
351
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (frame->pts != AV_NOPTS_VALUE)
352 65 frame->pts += diff_ts;
353
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (frame->pkt_dts != AV_NOPTS_VALUE)
354 65 frame->pkt_dts += diff_ts;
355
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (frame->duration >= diff_ts)
356 65 frame->duration -= diff_ts;
357 } else
358 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
359
360 65 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
361 avci->skip_samples, frame->nb_samples);
362 65 *discarded_samples += avci->skip_samples;
363 65 frame->nb_samples -= avci->skip_samples;
364 65 avci->skip_samples = 0;
365 }
366 }
367
368
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 261151 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
261158 if (discard_padding > 0 && discard_padding <= frame->nb_samples) {
369
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (discard_padding == frame->nb_samples) {
370 2 *discarded_samples += frame->nb_samples;
371 2 return AVERROR(EAGAIN);
372 } else {
373
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (avctx->pkt_timebase.num && avctx->sample_rate) {
374 5 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
375 5 (AVRational){1, avctx->sample_rate},
376 avctx->pkt_timebase);
377 5 frame->duration = diff_ts;
378 } else
379 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
380
381 5 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
382 (int)discard_padding, frame->nb_samples);
383 5 frame->nb_samples -= discard_padding;
384 }
385 }
386
387 261156 return 0;
388 }
389
390 /*
391 * The core of the receive_frame_wrapper for the decoders implementing
392 * the simple API. Certain decoders might consume partial packets without
393 * returning any output, so this function needs to be called in a loop until it
394 * returns EAGAIN.
395 **/
396 787780 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
397 {
398 787780 AVCodecInternal *avci = avctx->internal;
399 787780 DecodeContext *dc = decode_ctx(avci);
400 787780 AVPacket *const pkt = avci->in_pkt;
401 787780 const FFCodec *const codec = ffcodec(avctx->codec);
402 int got_frame, consumed;
403 int ret;
404
405
4/4
✓ Branch 0 taken 759162 times.
✓ Branch 1 taken 28618 times.
✓ Branch 2 taken 757453 times.
✓ Branch 3 taken 1709 times.
787780 if (!pkt->data && !avci->draining) {
406 757453 av_packet_unref(pkt);
407 757453 ret = ff_decode_get_packet(avctx, pkt);
408
4/4
✓ Branch 0 taken 378320 times.
✓ Branch 1 taken 379133 times.
✓ Branch 2 taken 374975 times.
✓ Branch 3 taken 3345 times.
757453 if (ret < 0 && ret != AVERROR_EOF)
409 374975 return ret;
410 }
411
412 // Some codecs (at least wma lossless) will crash when feeding drain packets
413 // after EOF was signaled.
414
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 412091 times.
412805 if (avci->draining_done)
415 714 return AVERROR_EOF;
416
417
2/2
✓ Branch 0 taken 4340 times.
✓ Branch 1 taken 407751 times.
412091 if (!pkt->data &&
418
2/2
✓ Branch 0 taken 2671 times.
✓ Branch 1 taken 1669 times.
4340 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
419
2/2
✓ Branch 0 taken 2630 times.
✓ Branch 1 taken 41 times.
2671 avctx->active_thread_type & FF_THREAD_FRAME))
420 2630 return AVERROR_EOF;
421
422 409461 got_frame = 0;
423
424
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 408851 times.
409461 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
425 610 consumed = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
426 } else {
427 408851 frame->pict_type = dc->initial_pict_type;
428 408851 frame->flags |= dc->intra_only_flag;
429 408851 consumed = codec->cb.decode(avctx, frame, &got_frame, pkt);
430
431
1/2
✓ Branch 0 taken 408851 times.
✗ Branch 1 not taken.
408851 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
432 408851 frame->pkt_dts = pkt->dts;
433
2/2
✓ Branch 0 taken 148394 times.
✓ Branch 1 taken 260457 times.
408851 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
434 #if FF_API_FRAME_PKT
435 FF_DISABLE_DEPRECATION_WARNINGS
436
2/2
✓ Branch 0 taken 113125 times.
✓ Branch 1 taken 35269 times.
148394 if(!avctx->has_b_frames)
437 113125 frame->pkt_pos = pkt->pos;
438 FF_ENABLE_DEPRECATION_WARNINGS
439 #endif
440 }
441 }
442 409461 emms_c();
443
444
2/2
✓ Branch 0 taken 148532 times.
✓ Branch 1 taken 260929 times.
409461 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
445
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 137439 times.
286151 ret = (!got_frame || frame->flags & AV_FRAME_FLAG_DISCARD)
446 ? AVERROR(EAGAIN)
447
2/2
✓ Branch 0 taken 137619 times.
✓ Branch 1 taken 10913 times.
286151 : 0;
448
1/2
✓ Branch 0 taken 260929 times.
✗ Branch 1 not taken.
260929 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
449 260929 ret = !got_frame ? AVERROR(EAGAIN)
450
2/2
✓ Branch 0 taken 259525 times.
✓ Branch 1 taken 1404 times.
260929 : discard_samples(avctx, frame, discarded_samples);
451 } else
452 av_assert0(0);
453
454
2/2
✓ Branch 0 taken 12675 times.
✓ Branch 1 taken 396786 times.
409461 if (ret == AVERROR(EAGAIN))
455 12675 av_frame_unref(frame);
456
457 // FF_CODEC_CB_TYPE_DECODE decoders must not return AVERROR EAGAIN
458 // code later will add AVERROR(EAGAIN) to a pointer
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409461 times.
409461 av_assert0(consumed != AVERROR(EAGAIN));
460
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 408789 times.
409461 if (consumed < 0)
461 672 ret = consumed;
462
4/4
✓ Branch 0 taken 408789 times.
✓ Branch 1 taken 672 times.
✓ Branch 2 taken 147896 times.
✓ Branch 3 taken 260893 times.
409461 if (consumed >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
463 147896 consumed = pkt->size;
464
465
2/2
✓ Branch 0 taken 396786 times.
✓ Branch 1 taken 12675 times.
409461 if (!ret)
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396786 times.
396786 av_assert0(frame->buf[0]);
467
2/2
✓ Branch 0 taken 12003 times.
✓ Branch 1 taken 397458 times.
409461 if (ret == AVERROR(EAGAIN))
468 12003 ret = 0;
469
470 /* do not stop draining when got_frame != 0 or ret < 0 */
471
4/4
✓ Branch 0 taken 1710 times.
✓ Branch 1 taken 407751 times.
✓ Branch 2 taken 715 times.
✓ Branch 3 taken 995 times.
409461 if (avci->draining && !got_frame) {
472
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 714 times.
715 if (ret < 0) {
473 /* prevent infinite loop if a decoder wrongly always return error on draining */
474 /* reasonable nb_errors_max = maximum b frames + thread count */
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ?
476 avctx->thread_count : 1);
477
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (decode_ctx(avci)->nb_draining_errors++ >= nb_errors_max) {
479 av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
480 "Stop draining and force EOF.\n");
481 avci->draining_done = 1;
482 ret = AVERROR_BUG;
483 }
484 } else {
485 714 avci->draining_done = 1;
486 }
487 }
488
489
4/4
✓ Branch 0 taken 29306 times.
✓ Branch 1 taken 380155 times.
✓ Branch 2 taken 672 times.
✓ Branch 3 taken 28634 times.
409461 if (consumed >= pkt->size || ret < 0) {
490 380827 av_packet_unref(pkt);
491 } else {
492 28634 pkt->data += consumed;
493 28634 pkt->size -= consumed;
494 28634 pkt->pts = AV_NOPTS_VALUE;
495 28634 pkt->dts = AV_NOPTS_VALUE;
496
1/2
✓ Branch 0 taken 28634 times.
✗ Branch 1 not taken.
28634 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
497 #if FF_API_FRAME_PKT
498 // See extract_packet_props() comment.
499 28634 avci->last_pkt_props->stream_index = avci->last_pkt_props->stream_index - consumed;
500 #endif
501 28634 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
502 28634 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
503 }
504 }
505
506 409461 return ret;
507 }
508
509 #if CONFIG_LCMS2
510 static int detect_colorspace(AVCodecContext *avctx, AVFrame *frame)
511 {
512 AVCodecInternal *avci = avctx->internal;
513 enum AVColorTransferCharacteristic trc;
514 AVColorPrimariesDesc coeffs;
515 enum AVColorPrimaries prim;
516 cmsHPROFILE profile;
517 AVFrameSideData *sd;
518 int ret;
519 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
520 return 0;
521
522 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
523 if (!sd || !sd->size)
524 return 0;
525
526 if (!avci->icc.avctx) {
527 ret = ff_icc_context_init(&avci->icc, avctx);
528 if (ret < 0)
529 return ret;
530 }
531
532 profile = cmsOpenProfileFromMemTHR(avci->icc.ctx, sd->data, sd->size);
533 if (!profile)
534 return AVERROR_INVALIDDATA;
535
536 ret = ff_icc_profile_sanitize(&avci->icc, profile);
537 if (!ret)
538 ret = ff_icc_profile_read_primaries(&avci->icc, profile, &coeffs);
539 if (!ret)
540 ret = ff_icc_profile_detect_transfer(&avci->icc, profile, &trc);
541 cmsCloseProfile(profile);
542 if (ret < 0)
543 return ret;
544
545 prim = av_csp_primaries_id_from_desc(&coeffs);
546 if (prim != AVCOL_PRI_UNSPECIFIED)
547 frame->color_primaries = prim;
548 if (trc != AVCOL_TRC_UNSPECIFIED)
549 frame->color_trc = trc;
550 return 0;
551 }
552 #else /* !CONFIG_LCMS2 */
553 778777 static int detect_colorspace(av_unused AVCodecContext *c, av_unused AVFrame *f)
554 {
555 778777 return 0;
556 }
557 #endif
558
559 799558 static int fill_frame_props(const AVCodecContext *avctx, AVFrame *frame)
560 {
561 int ret;
562
563
2/2
✓ Branch 0 taken 537592 times.
✓ Branch 1 taken 261966 times.
799558 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
564 537592 frame->color_primaries = avctx->color_primaries;
565
2/2
✓ Branch 0 taken 537231 times.
✓ Branch 1 taken 262327 times.
799558 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
566 537231 frame->color_trc = avctx->color_trc;
567
2/2
✓ Branch 0 taken 531337 times.
✓ Branch 1 taken 268221 times.
799558 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
568 531337 frame->colorspace = avctx->colorspace;
569
2/2
✓ Branch 0 taken 739945 times.
✓ Branch 1 taken 59613 times.
799558 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
570 739945 frame->color_range = avctx->color_range;
571
2/2
✓ Branch 0 taken 772029 times.
✓ Branch 1 taken 27529 times.
799558 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
572 772029 frame->chroma_location = avctx->chroma_sample_location;
573
574
2/2
✓ Branch 0 taken 277011 times.
✓ Branch 1 taken 522547 times.
799558 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
575
2/2
✓ Branch 0 taken 260952 times.
✓ Branch 1 taken 16059 times.
277011 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
576
2/2
✓ Branch 0 taken 137703 times.
✓ Branch 1 taken 139308 times.
277011 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
577
1/2
✓ Branch 0 taken 522547 times.
✗ Branch 1 not taken.
522547 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
578
2/2
✓ Branch 0 taken 261365 times.
✓ Branch 1 taken 261182 times.
522547 if (frame->format == AV_SAMPLE_FMT_NONE)
579 261365 frame->format = avctx->sample_fmt;
580
2/2
✓ Branch 0 taken 261365 times.
✓ Branch 1 taken 261182 times.
522547 if (!frame->ch_layout.nb_channels) {
581 261365 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261365 times.
261365 if (ret < 0)
583 return ret;
584 }
585
2/2
✓ Branch 0 taken 261225 times.
✓ Branch 1 taken 261322 times.
522547 if (!frame->sample_rate)
586 261225 frame->sample_rate = avctx->sample_rate;
587 }
588
589 799558 return 0;
590 }
591
592 775777 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
593 {
594 int ret;
595 775777 int64_t discarded_samples = 0;
596
597
2/2
✓ Branch 0 taken 787780 times.
✓ Branch 1 taken 396786 times.
1184566 while (!frame->buf[0]) {
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 787780 times.
787780 if (discarded_samples > avctx->max_samples)
599 return AVERROR(EAGAIN);
600 787780 ret = decode_simple_internal(avctx, frame, &discarded_samples);
601
2/2
✓ Branch 0 taken 378991 times.
✓ Branch 1 taken 408789 times.
787780 if (ret < 0)
602 378991 return ret;
603 }
604
605 396786 return 0;
606 }
607
608 778777 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
609 {
610 778777 AVCodecInternal *avci = avctx->internal;
611 778777 DecodeContext *dc = decode_ctx(avci);
612 778777 const FFCodec *const codec = ffcodec(avctx->codec);
613 int ret, ok;
614
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 778777 times.
778777 av_assert0(!frame->buf[0]);
616
617
2/2
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 775777 times.
778777 if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) {
618 3000 frame->pict_type = dc->initial_pict_type;
619 3000 frame->flags |= dc->intra_only_flag;
620 3000 ret = codec->cb.receive_frame(avctx, frame);
621 3000 emms_c();
622
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 1178 times.
3000 if (!ret) {
623
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1809 times.
1822 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO)
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 ret = (frame->flags & AV_FRAME_FLAG_DISCARD) ? AVERROR(EAGAIN) : 0;
625
1/2
✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
1809 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
626 1809 int64_t discarded_samples = 0;
627 1809 ret = discard_samples(avctx, frame, &discarded_samples);
628 }
629 }
630 } else
631 775777 ret = decode_simple_receive_frame(avctx, frame);
632
633
2/2
✓ Branch 0 taken 3348 times.
✓ Branch 1 taken 775429 times.
778777 if (ret == AVERROR_EOF)
634 3348 avci->draining_done = 1;
635
636 /* preserve ret */
637 778777 ok = detect_colorspace(avctx, frame);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 778777 times.
778777 if (ok < 0) {
639 av_frame_unref(frame);
640 return ok;
641 }
642
643
2/2
✓ Branch 0 taken 398608 times.
✓ Branch 1 taken 380169 times.
778777 if (!ret) {
644
2/2
✓ Branch 0 taken 137452 times.
✓ Branch 1 taken 261156 times.
398608 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
645
2/2
✓ Branch 0 taken 23790 times.
✓ Branch 1 taken 113662 times.
137452 if (!frame->width)
646 23790 frame->width = avctx->width;
647
2/2
✓ Branch 0 taken 23790 times.
✓ Branch 1 taken 113662 times.
137452 if (!frame->height)
648 23790 frame->height = avctx->height;
649 }
650
651 398608 ret = fill_frame_props(avctx, frame);
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398608 times.
398608 if (ret < 0) {
653 av_frame_unref(frame);
654 return ret;
655 }
656
657 #if FF_API_FRAME_KEY
658 FF_DISABLE_DEPRECATION_WARNINGS
659 398608 frame->key_frame = !!(frame->flags & AV_FRAME_FLAG_KEY);
660 FF_ENABLE_DEPRECATION_WARNINGS
661 #endif
662 #if FF_API_INTERLACED_FRAME
663 FF_DISABLE_DEPRECATION_WARNINGS
664 398608 frame->interlaced_frame = !!(frame->flags & AV_FRAME_FLAG_INTERLACED);
665 398608 frame->top_field_first = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
666 FF_ENABLE_DEPRECATION_WARNINGS
667 #endif
668 398608 frame->best_effort_timestamp = guess_correct_pts(dc,
669 frame->pts,
670 frame->pkt_dts);
671
672 /* the only case where decode data is not set should be decoders
673 * that do not call ff_get_buffer() */
674
4/6
✓ Branch 0 taken 374100 times.
✓ Branch 1 taken 24508 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 374100 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24508 times.
398608 av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
675 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
676
677
2/2
✓ Branch 0 taken 374100 times.
✓ Branch 1 taken 24508 times.
398608 if (frame->private_ref) {
678 374100 FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
679
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374100 times.
374100 if (fdd->post_process) {
681 ret = fdd->post_process(avctx, frame);
682 if (ret < 0) {
683 av_frame_unref(frame);
684 return ret;
685 }
686 }
687 }
688 }
689
690 /* free the per-frame decode data */
691 778777 av_buffer_unref(&frame->private_ref);
692
693 778777 return ret;
694 }
695
696 383591 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
697 {
698 383591 AVCodecInternal *avci = avctx->internal;
699 383591 DecodeContext *dc = decode_ctx(avci);
700 int ret;
701
702
2/4
✓ Branch 1 taken 383591 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 383591 times.
383591 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
703 return AVERROR(EINVAL);
704
705
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 383561 times.
383591 if (dc->draining_started)
706 30 return AVERROR_EOF;
707
708
5/6
✓ Branch 0 taken 380305 times.
✓ Branch 1 taken 3256 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 380212 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 93 times.
383561 if (avpkt && !avpkt->size && avpkt->data)
709 return AVERROR(EINVAL);
710
711
5/6
✓ Branch 0 taken 380305 times.
✓ Branch 1 taken 3256 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 380212 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 93 times.
383561 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
712
2/4
✓ Branch 0 taken 380212 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 380212 times.
380212 if (!AVPACKET_IS_EMPTY(avci->buffer_pkt))
713 return AVERROR(EAGAIN);
714 380212 ret = av_packet_ref(avci->buffer_pkt, avpkt);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380212 times.
380212 if (ret < 0)
716 return ret;
717 } else
718 3349 dc->draining_started = 1;
719
720
3/4
✓ Branch 0 taken 383561 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 380212 times.
✓ Branch 3 taken 3349 times.
383561 if (!avci->buffer_frame->buf[0] && !dc->draining_started) {
721 380212 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
722
5/6
✓ Branch 0 taken 10686 times.
✓ Branch 1 taken 369526 times.
✓ Branch 2 taken 666 times.
✓ Branch 3 taken 10020 times.
✓ Branch 4 taken 666 times.
✗ Branch 5 not taken.
380212 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
723 666 return ret;
724 }
725
726 382895 return 0;
727 }
728
729 137452 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
730 {
731 /* make sure we are noisy about decoders returning invalid cropping data */
732
1/2
✓ Branch 0 taken 137452 times.
✗ Branch 1 not taken.
137452 if (frame->crop_left >= INT_MAX - frame->crop_right ||
733
1/2
✓ Branch 0 taken 137452 times.
✗ Branch 1 not taken.
137452 frame->crop_top >= INT_MAX - frame->crop_bottom ||
734
1/2
✓ Branch 0 taken 137452 times.
✗ Branch 1 not taken.
137452 (frame->crop_left + frame->crop_right) >= frame->width ||
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137452 times.
137452 (frame->crop_top + frame->crop_bottom) >= frame->height) {
736 av_log(avctx, AV_LOG_WARNING,
737 "Invalid cropping information set by a decoder: "
738 "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
739 "(frame size %dx%d). This is a bug, please report it\n",
740 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
741 frame->width, frame->height);
742 frame->crop_left = 0;
743 frame->crop_right = 0;
744 frame->crop_top = 0;
745 frame->crop_bottom = 0;
746 return 0;
747 }
748
749
2/2
✓ Branch 0 taken 130131 times.
✓ Branch 1 taken 7321 times.
137452 if (!avctx->apply_cropping)
750 130131 return 0;
751
752 7321 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
753 AV_FRAME_CROP_UNALIGNED : 0);
754 }
755
756 // make sure frames returned to the caller are valid
757 398608 static int frame_validate(AVCodecContext *avctx, AVFrame *frame)
758 {
759
2/4
✓ Branch 0 taken 398608 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 398608 times.
398608 if (!frame->buf[0] || frame->format < 0)
760 goto fail;
761
762
2/3
✓ Branch 0 taken 137452 times.
✓ Branch 1 taken 261156 times.
✗ Branch 2 not taken.
398608 switch (avctx->codec_type) {
763 137452 case AVMEDIA_TYPE_VIDEO:
764
2/4
✓ Branch 0 taken 137452 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 137452 times.
137452 if (frame->width <= 0 || frame->height <= 0)
765 goto fail;
766 137452 break;
767 261156 case AVMEDIA_TYPE_AUDIO:
768
1/2
✓ Branch 1 taken 261156 times.
✗ Branch 2 not taken.
261156 if (!av_channel_layout_check(&frame->ch_layout) ||
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261156 times.
261156 frame->sample_rate <= 0)
770 goto fail;
771
772 261156 break;
773 default: av_assert0(0);
774 }
775
776 398608 return 0;
777 fail:
778 av_log(avctx, AV_LOG_ERROR, "An invalid frame was output by a decoder. "
779 "This is a bug, please report it.\n");
780 return AVERROR_BUG;
781 }
782
783 768091 int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
784 {
785 768091 AVCodecInternal *avci = avctx->internal;
786 int ret;
787
788
2/4
✓ Branch 1 taken 768091 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 768091 times.
768091 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
789 return AVERROR(EINVAL);
790
791
2/2
✓ Branch 0 taken 369526 times.
✓ Branch 1 taken 398565 times.
768091 if (avci->buffer_frame->buf[0]) {
792 369526 av_frame_move_ref(frame, avci->buffer_frame);
793 } else {
794 398565 ret = decode_receive_frame_internal(avctx, frame);
795
2/2
✓ Branch 0 taken 369483 times.
✓ Branch 1 taken 29082 times.
398565 if (ret < 0)
796 369483 return ret;
797 }
798
799 398608 ret = frame_validate(avctx, frame);
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398608 times.
398608 if (ret < 0)
801 goto fail;
802
803
2/2
✓ Branch 0 taken 137452 times.
✓ Branch 1 taken 261156 times.
398608 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
804 137452 ret = apply_cropping(avctx, frame);
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137452 times.
137452 if (ret < 0)
806 goto fail;
807 }
808
809 398608 avctx->frame_num++;
810
811 #if FF_API_DROPCHANGED
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398608 times.
398608 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
813
814 if (avctx->frame_num == 1) {
815 avci->initial_format = frame->format;
816 switch(avctx->codec_type) {
817 case AVMEDIA_TYPE_VIDEO:
818 avci->initial_width = frame->width;
819 avci->initial_height = frame->height;
820 break;
821 case AVMEDIA_TYPE_AUDIO:
822 avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
823 avctx->sample_rate;
824 ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout);
825 if (ret < 0)
826 goto fail;
827 break;
828 }
829 }
830
831 if (avctx->frame_num > 1) {
832 int changed = avci->initial_format != frame->format;
833
834 switch(avctx->codec_type) {
835 case AVMEDIA_TYPE_VIDEO:
836 changed |= avci->initial_width != frame->width ||
837 avci->initial_height != frame->height;
838 break;
839 case AVMEDIA_TYPE_AUDIO:
840 changed |= avci->initial_sample_rate != frame->sample_rate ||
841 avci->initial_sample_rate != avctx->sample_rate ||
842 av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout);
843 break;
844 }
845
846 if (changed) {
847 avci->changed_frames_dropped++;
848 av_log(avctx, AV_LOG_INFO, "dropped changed frame #%"PRId64" pts %"PRId64
849 " drop count: %d \n",
850 avctx->frame_num, frame->pts,
851 avci->changed_frames_dropped);
852 ret = AVERROR_INPUT_CHANGED;
853 goto fail;
854 }
855 }
856 }
857 #endif
858 398608 return 0;
859 fail:
860 av_frame_unref(frame);
861 return ret;
862 }
863
864 1709 static void get_subtitle_defaults(AVSubtitle *sub)
865 {
866 1709 memset(sub, 0, sizeof(*sub));
867 1709 sub->pts = AV_NOPTS_VALUE;
868 1709 }
869
870 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
871 1675 static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt,
872 const AVPacket *inpkt, AVPacket *buf_pkt)
873 {
874 #if CONFIG_ICONV
875 1675 iconv_t cd = (iconv_t)-1;
876 1675 int ret = 0;
877 char *inb, *outb;
878 size_t inl, outl;
879 #endif
880
881
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
1675 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) {
882 1582 *outpkt = inpkt;
883 1582 return 0;
884 }
885
886 #if CONFIG_ICONV
887 93 inb = inpkt->data;
888 93 inl = inpkt->size;
889
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
891 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
892 return AVERROR(ERANGE);
893 }
894
895 93 cd = iconv_open("UTF-8", avctx->sub_charenc);
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 av_assert0(cd != (iconv_t)-1);
897
898 93 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
900 goto end;
901 93 ret = av_packet_copy_props(buf_pkt, inpkt);
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
903 goto end;
904 93 outb = buf_pkt->data;
905 93 outl = buf_pkt->size;
906
907
2/4
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
✗ Branch 4 not taken.
186 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
908 93 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
909
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
93 outl >= buf_pkt->size || inl != 0) {
910 ret = FFMIN(AVERROR(errno), -1);
911 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
912 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
913 goto end;
914 }
915 93 buf_pkt->size -= outl;
916 93 memset(buf_pkt->data + buf_pkt->size, 0, outl);
917 93 *outpkt = buf_pkt;
918
919 93 ret = 0;
920 93 end:
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
922 av_packet_unref(buf_pkt);
923
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (cd != (iconv_t)-1)
924 93 iconv_close(cd);
925 93 return ret;
926 #else
927 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
928 return AVERROR(EINVAL);
929 #endif
930 }
931
932 700 static int utf8_check(const uint8_t *str)
933 {
934 const uint8_t *byte;
935 uint32_t codepoint, min;
936
937
2/2
✓ Branch 0 taken 61070 times.
✓ Branch 1 taken 700 times.
61770 while (*str) {
938 61070 byte = str;
939
5/8
✓ Branch 0 taken 61070 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61070 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2798 times.
✓ Branch 6 taken 2798 times.
✓ Branch 7 taken 61070 times.
63868 GET_UTF8(codepoint, *(byte++), return 0;);
940
4/4
✓ Branch 0 taken 2091 times.
✓ Branch 1 taken 58979 times.
✓ Branch 2 taken 707 times.
✓ Branch 3 taken 1384 times.
61777 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
941 707 1 << (5 * (byte - str) - 4);
942
3/6
✓ Branch 0 taken 61070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61070 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61070 times.
✗ Branch 5 not taken.
61070 if (codepoint < min || codepoint >= 0x110000 ||
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61070 times.
61070 codepoint == 0xFFFE /* BOM */ ||
944 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
945 return 0;
946 61070 str = byte;
947 }
948 700 return 1;
949 }
950
951 1709 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
952 int *got_sub_ptr, const AVPacket *avpkt)
953 {
954 1709 int ret = 0;
955
956
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1669 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
1709 if (!avpkt->data && avpkt->size) {
957 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
958 return AVERROR(EINVAL);
959 }
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1709 times.
1709 if (!avctx->codec)
961 return AVERROR(EINVAL);
962
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1709 times.
1709 if (ffcodec(avctx->codec)->cb_type != FF_CODEC_CB_TYPE_DECODE_SUB) {
963 av_log(avctx, AV_LOG_ERROR, "Codec not subtitle decoder\n");
964 return AVERROR(EINVAL);
965 }
966
967 1709 *got_sub_ptr = 0;
968 1709 get_subtitle_defaults(sub);
969
970
4/4
✓ Branch 0 taken 857 times.
✓ Branch 1 taken 852 times.
✓ Branch 2 taken 823 times.
✓ Branch 3 taken 34 times.
1709 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
971 1675 AVCodecInternal *avci = avctx->internal;
972 const AVPacket *pkt;
973
974 1675 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1675 times.
1675 if (ret < 0)
976 79 return ret;
977
978
3/4
✓ Branch 0 taken 1675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
✓ Branch 3 taken 10 times.
1675 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
979 1665 sub->pts = av_rescale_q(avpkt->pts,
980 1665 avctx->pkt_timebase, AV_TIME_BASE_Q);
981 1675 ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
982
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
1675 if (pkt == avci->buffer_pkt) // did we recode?
983 93 av_packet_unref(avci->buffer_pkt);
984
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1596 times.
1675 if (ret < 0) {
985 79 *got_sub_ptr = 0;
986 79 avsubtitle_free(sub);
987 79 return ret;
988 }
989 av_assert1(!sub->num_rects || *got_sub_ptr);
990
991
6/6
✓ Branch 0 taken 826 times.
✓ Branch 1 taken 770 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 246 times.
✓ Branch 4 taken 528 times.
✓ Branch 5 taken 52 times.
1596 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
992
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 avctx->pkt_timebase.num) {
993 528 AVRational ms = { 1, 1000 };
994 528 sub->end_display_time = av_rescale_q(avpkt->duration,
995 avctx->pkt_timebase, ms);
996 }
997
998
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1440 times.
1596 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
999 156 sub->format = 0;
1000
1/2
✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
1440 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
1001 1440 sub->format = 1;
1002
1003
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 1596 times.
2455 for (unsigned i = 0; i < sub->num_rects; i++) {
1004
1/2
✓ Branch 0 taken 859 times.
✗ Branch 1 not taken.
859 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
1005
3/4
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 700 times.
859 sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
1006 av_log(avctx, AV_LOG_ERROR,
1007 "Invalid UTF-8 in decoded subtitles text; "
1008 "maybe missing -sub_charenc option\n");
1009 avsubtitle_free(sub);
1010 *got_sub_ptr = 0;
1011 return AVERROR_INVALIDDATA;
1012 }
1013 }
1014
1015
2/2
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 761 times.
1596 if (*got_sub_ptr)
1016 835 avctx->frame_num++;
1017 }
1018
1019 1630 return ret;
1020 }
1021
1022 1484 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
1023 const enum AVPixelFormat *fmt)
1024 {
1025 const AVPixFmtDescriptor *desc;
1026 const AVCodecHWConfig *config;
1027 int i, n;
1028
1029 // If a device was supplied when the codec was opened, assume that the
1030 // user wants to use it.
1031
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1484 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1484 if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
1032 AVHWDeviceContext *device_ctx =
1033 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1034 for (i = 0;; i++) {
1035 config = &ffcodec(avctx->codec)->hw_configs[i]->public;
1036 if (!config)
1037 break;
1038 if (!(config->methods &
1039 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1040 continue;
1041 if (device_ctx->type != config->device_type)
1042 continue;
1043 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1044 if (config->pix_fmt == fmt[n])
1045 return fmt[n];
1046 }
1047 }
1048 }
1049 // No device or other setup, so we have to choose from things which
1050 // don't any other external information.
1051
1052 // If the last element of the list is a software format, choose it
1053 // (this should be best software format if any exist).
1054
2/2
✓ Branch 0 taken 3842 times.
✓ Branch 1 taken 1484 times.
5326 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1055 1484 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1056
1/2
✓ Branch 0 taken 1484 times.
✗ Branch 1 not taken.
1484 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1057 1484 return fmt[n - 1];
1058
1059 // Finally, traverse the list in order and choose the first entry
1060 // with no external dependencies (if there is no hardware configuration
1061 // information available then this just picks the first entry).
1062 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1063 for (i = 0;; i++) {
1064 config = avcodec_get_hw_config(avctx->codec, i);
1065 if (!config)
1066 break;
1067 if (config->pix_fmt == fmt[n])
1068 break;
1069 }
1070 if (!config) {
1071 // No specific config available, so the decoder must be able
1072 // to handle this format without any additional setup.
1073 return fmt[n];
1074 }
1075 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1076 // Usable with only internal setup.
1077 return fmt[n];
1078 }
1079 }
1080
1081 // Nothing is usable, give up.
1082 return AV_PIX_FMT_NONE;
1083 }
1084
1085 int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
1086 enum AVHWDeviceType dev_type)
1087 {
1088 AVHWDeviceContext *device_ctx;
1089 AVHWFramesContext *frames_ctx;
1090 int ret;
1091
1092 if (!avctx->hwaccel)
1093 return AVERROR(ENOSYS);
1094
1095 if (avctx->hw_frames_ctx)
1096 return 0;
1097 if (!avctx->hw_device_ctx) {
1098 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
1099 "required for hardware accelerated decoding.\n");
1100 return AVERROR(EINVAL);
1101 }
1102
1103 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
1104 if (device_ctx->type != dev_type) {
1105 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
1106 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
1107 av_hwdevice_get_type_name(device_ctx->type));
1108 return AVERROR(EINVAL);
1109 }
1110
1111 ret = avcodec_get_hw_frames_parameters(avctx,
1112 avctx->hw_device_ctx,
1113 avctx->hwaccel->pix_fmt,
1114 &avctx->hw_frames_ctx);
1115 if (ret < 0)
1116 return ret;
1117
1118 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1119
1120
1121 if (frames_ctx->initial_pool_size) {
1122 // We guarantee 4 base work surfaces. The function above guarantees 1
1123 // (the absolute minimum), so add the missing count.
1124 frames_ctx->initial_pool_size += 3;
1125 }
1126
1127 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1128 if (ret < 0) {
1129 av_buffer_unref(&avctx->hw_frames_ctx);
1130 return ret;
1131 }
1132
1133 return 0;
1134 }
1135
1136 int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
1137 AVBufferRef *device_ref,
1138 enum AVPixelFormat hw_pix_fmt,
1139 AVBufferRef **out_frames_ref)
1140 {
1141 AVBufferRef *frames_ref = NULL;
1142 const AVCodecHWConfigInternal *hw_config;
1143 const FFHWAccel *hwa;
1144 int i, ret;
1145
1146 for (i = 0;; i++) {
1147 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1148 if (!hw_config)
1149 return AVERROR(ENOENT);
1150 if (hw_config->public.pix_fmt == hw_pix_fmt)
1151 break;
1152 }
1153
1154 hwa = hw_config->hwaccel;
1155 if (!hwa || !hwa->frame_params)
1156 return AVERROR(ENOENT);
1157
1158 frames_ref = av_hwframe_ctx_alloc(device_ref);
1159 if (!frames_ref)
1160 return AVERROR(ENOMEM);
1161
1162 if (!avctx->internal->hwaccel_priv_data) {
1163 avctx->internal->hwaccel_priv_data =
1164 av_mallocz(hwa->priv_data_size);
1165 if (!avctx->internal->hwaccel_priv_data) {
1166 av_buffer_unref(&frames_ref);
1167 return AVERROR(ENOMEM);
1168 }
1169 }
1170
1171 ret = hwa->frame_params(avctx, frames_ref);
1172 if (ret >= 0) {
1173 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1174
1175 if (frames_ctx->initial_pool_size) {
1176 // If the user has requested that extra output surfaces be
1177 // available then add them here.
1178 if (avctx->extra_hw_frames > 0)
1179 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1180
1181 // If frame threading is enabled then an extra surface per thread
1182 // is also required.
1183 if (avctx->active_thread_type & FF_THREAD_FRAME)
1184 frames_ctx->initial_pool_size += avctx->thread_count;
1185 }
1186
1187 *out_frames_ref = frames_ref;
1188 } else {
1189 av_buffer_unref(&frames_ref);
1190 }
1191 return ret;
1192 }
1193
1194 static int hwaccel_init(AVCodecContext *avctx,
1195 const FFHWAccel *hwaccel)
1196 {
1197 int err;
1198
1199 if (hwaccel->p.capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1200 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1201 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1202 hwaccel->p.name);
1203 return AVERROR_PATCHWELCOME;
1204 }
1205
1206 if (!avctx->internal->hwaccel_priv_data && hwaccel->priv_data_size) {
1207 avctx->internal->hwaccel_priv_data =
1208 av_mallocz(hwaccel->priv_data_size);
1209 if (!avctx->internal->hwaccel_priv_data)
1210 return AVERROR(ENOMEM);
1211 }
1212
1213 avctx->hwaccel = &hwaccel->p;
1214 if (hwaccel->init) {
1215 err = hwaccel->init(avctx);
1216 if (err < 0) {
1217 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
1218 "hwaccel initialisation returned error.\n",
1219 av_get_pix_fmt_name(hwaccel->p.pix_fmt));
1220 av_freep(&avctx->internal->hwaccel_priv_data);
1221 avctx->hwaccel = NULL;
1222 return err;
1223 }
1224 }
1225
1226 return 0;
1227 }
1228
1229 36393 void ff_hwaccel_uninit(AVCodecContext *avctx)
1230 {
1231
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 36393 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
36393 if (FF_HW_HAS_CB(avctx, uninit))
1232 FF_HW_SIMPLE_CALL(avctx, uninit);
1233
1234 36393 av_freep(&avctx->internal->hwaccel_priv_data);
1235
1236 36393 avctx->hwaccel = NULL;
1237
1238 36393 av_buffer_unref(&avctx->hw_frames_ctx);
1239 36393 }
1240
1241 2436 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1242 {
1243 const AVPixFmtDescriptor *desc;
1244 enum AVPixelFormat *choices;
1245 enum AVPixelFormat ret, user_choice;
1246 const AVCodecHWConfigInternal *hw_config;
1247 const AVCodecHWConfig *config;
1248 int i, n, err;
1249
1250 // Find end of list.
1251
2/2
✓ Branch 0 taken 6453 times.
✓ Branch 1 taken 2436 times.
8889 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1252 // Must contain at least one entry.
1253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
2436 av_assert0(n >= 1);
1254 // If a software format is available, it must be the last entry.
1255 2436 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1256
1/2
✓ Branch 0 taken 2436 times.
✗ Branch 1 not taken.
2436 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1257 // No software format is available.
1258 } else {
1259 2436 avctx->sw_pix_fmt = fmt[n - 1];
1260 }
1261
1262 2436 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1263
1/2
✓ Branch 0 taken 2436 times.
✗ Branch 1 not taken.
2436 if (!choices)
1264 return AV_PIX_FMT_NONE;
1265
1266 for (;;) {
1267 // Remove the previous hwaccel, if there was one.
1268 2436 ff_hwaccel_uninit(avctx);
1269
1270 2436 user_choice = avctx->get_format(avctx, choices);
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
2436 if (user_choice == AV_PIX_FMT_NONE) {
1272 // Explicitly chose nothing, give up.
1273 ret = AV_PIX_FMT_NONE;
1274 break;
1275 }
1276
1277 2436 desc = av_pix_fmt_desc_get(user_choice);
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
2436 if (!desc) {
1279 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
1280 "get_format() callback.\n");
1281 ret = AV_PIX_FMT_NONE;
1282 break;
1283 }
1284 2436 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
1285 2436 desc->name);
1286
1287
1/2
✓ Branch 0 taken 6453 times.
✗ Branch 1 not taken.
6453 for (i = 0; i < n; i++) {
1288
2/2
✓ Branch 0 taken 2436 times.
✓ Branch 1 taken 4017 times.
6453 if (choices[i] == user_choice)
1289 2436 break;
1290 }
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
2436 if (i == n) {
1292 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
1293 "%s not in possible list.\n", desc->name);
1294 ret = AV_PIX_FMT_NONE;
1295 break;
1296 }
1297
1298
2/2
✓ Branch 1 taken 2389 times.
✓ Branch 2 taken 47 times.
2436 if (ffcodec(avctx->codec)->hw_configs) {
1299 2389 for (i = 0;; i++) {
1300 6599 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1301
2/2
✓ Branch 0 taken 2389 times.
✓ Branch 1 taken 4210 times.
6599 if (!hw_config)
1302 2389 break;
1303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4210 times.
4210 if (hw_config->public.pix_fmt == user_choice)
1304 break;
1305 }
1306 } else {
1307 47 hw_config = NULL;
1308 }
1309
1310
1/2
✓ Branch 0 taken 2436 times.
✗ Branch 1 not taken.
2436 if (!hw_config) {
1311 // No config available, so no extra setup required.
1312 2436 ret = user_choice;
1313 2436 break;
1314 }
1315 config = &hw_config->public;
1316
1317 if (config->methods &
1318 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1319 avctx->hw_frames_ctx) {
1320 const AVHWFramesContext *frames_ctx =
1321 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1322 if (frames_ctx->format != user_choice) {
1323 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1324 "does not match the format of the provided frames "
1325 "context.\n", desc->name);
1326 goto try_again;
1327 }
1328 } else if (config->methods &
1329 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1330 avctx->hw_device_ctx) {
1331 const AVHWDeviceContext *device_ctx =
1332 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1333 if (device_ctx->type != config->device_type) {
1334 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1335 "does not match the type of the provided device "
1336 "context.\n", desc->name);
1337 goto try_again;
1338 }
1339 } else if (config->methods &
1340 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1341 // Internal-only setup, no additional configuration.
1342 } else if (config->methods &
1343 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1344 // Some ad-hoc configuration we can't see and can't check.
1345 } else {
1346 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1347 "missing configuration.\n", desc->name);
1348 goto try_again;
1349 }
1350 if (hw_config->hwaccel) {
1351 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel %s "
1352 "initialisation.\n", desc->name, hw_config->hwaccel->p.name);
1353 err = hwaccel_init(avctx, hw_config->hwaccel);
1354 if (err < 0)
1355 goto try_again;
1356 }
1357 ret = user_choice;
1358 break;
1359
1360 try_again:
1361 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
1362 "get_format() without it.\n", desc->name);
1363 for (i = 0; i < n; i++) {
1364 if (choices[i] == user_choice)
1365 break;
1366 }
1367 for (; i + 1 < n; i++)
1368 choices[i] = choices[i + 1];
1369 --n;
1370 }
1371
1372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
2436 if (ret < 0)
1373 ff_hwaccel_uninit(avctx);
1374
1375 2436 av_freep(&choices);
1376 2436 return ret;
1377 }
1378
1379 3609004 const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
1380 enum AVPacketSideDataType type)
1381 {
1382
2/2
✓ Branch 0 taken 57768 times.
✓ Branch 1 taken 3606641 times.
3664409 for (int i = 0; i < avctx->nb_coded_side_data; i++)
1383
2/2
✓ Branch 0 taken 2363 times.
✓ Branch 1 taken 55405 times.
57768 if (avctx->coded_side_data[i].type == type)
1384 2363 return &avctx->coded_side_data[i];
1385
1386 3606641 return NULL;
1387 }
1388
1389 400950 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1390 {
1391 size_t size;
1392 const uint8_t *side_metadata;
1393
1394 400950 AVDictionary **frame_md = &frame->metadata;
1395
1396 400950 side_metadata = av_packet_get_side_data(avpkt,
1397 AV_PKT_DATA_STRINGS_METADATA, &size);
1398 400950 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1399 }
1400
1401 400950 int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx,
1402 AVFrame *frame, const AVPacket *pkt)
1403 {
1404 static const struct {
1405 enum AVPacketSideDataType packet;
1406 enum AVFrameSideDataType frame;
1407 } sd[] = {
1408 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1409 { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD },
1410 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1411 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1412 { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES },
1413 };
1414
1415 400950 frame->pts = pkt->pts;
1416 400950 frame->duration = pkt->duration;
1417 #if FF_API_FRAME_PKT
1418 FF_DISABLE_DEPRECATION_WARNINGS
1419 400950 frame->pkt_pos = pkt->pos;
1420 400950 frame->pkt_size = pkt->size;
1421 FF_ENABLE_DEPRECATION_WARNINGS
1422 #endif
1423
1424
2/2
✓ Branch 0 taken 3608550 times.
✓ Branch 1 taken 400950 times.
4009500 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
1425 size_t size;
1426 3608550 const uint8_t *packet_sd = av_packet_get_side_data(pkt, ff_sd_global_map[i].packet, &size);
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3608550 times.
3608550 if (packet_sd) {
1428 AVFrameSideData *frame_sd;
1429
1430 frame_sd = av_frame_new_side_data(frame, ff_sd_global_map[i].frame, size);
1431 if (!frame_sd)
1432 return AVERROR(ENOMEM);
1433 memcpy(frame_sd->data, packet_sd, size);
1434 }
1435 }
1436
2/2
✓ Branch 0 taken 2004750 times.
✓ Branch 1 taken 400950 times.
2405700 for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1437 size_t size;
1438 2004750 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1439
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 2004628 times.
2004750 if (packet_sd) {
1440 122 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1441 122 sd[i].frame,
1442 size);
1443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 if (!frame_sd)
1444 return AVERROR(ENOMEM);
1445
1446 122 memcpy(frame_sd->data, packet_sd, size);
1447 }
1448 }
1449 400950 add_metadata_from_side_data(pkt, frame);
1450
1451
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 400742 times.
400950 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1452 208 frame->flags |= AV_FRAME_FLAG_DISCARD;
1453 } else {
1454 400742 frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
1455 }
1456
1457
2/2
✓ Branch 0 taken 389476 times.
✓ Branch 1 taken 11474 times.
400950 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
1458 389476 int ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389476 times.
389476 if (ret < 0)
1460 return ret;
1461 389476 frame->opaque = pkt->opaque;
1462 }
1463
1464 400950 return 0;
1465 }
1466
1467 400950 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1468 {
1469 int ret;
1470
1471
2/2
✓ Branch 0 taken 3608550 times.
✓ Branch 1 taken 400950 times.
4009500 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
1472 3608550 const AVPacketSideData *packet_sd = ff_get_coded_side_data(avctx,
1473 3608550 ff_sd_global_map[i].packet);
1474
2/2
✓ Branch 0 taken 2352 times.
✓ Branch 1 taken 3606198 times.
3608550 if (packet_sd) {
1475 2352 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1476 2352 ff_sd_global_map[i].frame,
1477 2352 packet_sd->size);
1478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2352 times.
2352 if (!frame_sd)
1479 return AVERROR(ENOMEM);
1480
1481 2352 memcpy(frame_sd->data, packet_sd->data, packet_sd->size);
1482 }
1483 }
1484
1485
1/2
✓ Branch 1 taken 400950 times.
✗ Branch 2 not taken.
400950 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
1486 400950 const AVPacket *pkt = avctx->internal->last_pkt_props;
1487
1488 400950 ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt);
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400950 times.
400950 if (ret < 0)
1490 return ret;
1491 #if FF_API_FRAME_PKT
1492 FF_DISABLE_DEPRECATION_WARNINGS
1493 400950 frame->pkt_size = pkt->stream_index;
1494 FF_ENABLE_DEPRECATION_WARNINGS
1495 #endif
1496 }
1497
1498 400950 ret = fill_frame_props(avctx, frame);
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400950 times.
400950 if (ret < 0)
1500 return ret;
1501
1502
2/2
✓ Branch 0 taken 139559 times.
✓ Branch 1 taken 261391 times.
400950 switch (avctx->codec->type) {
1503 139559 case AVMEDIA_TYPE_VIDEO:
1504
4/6
✓ Branch 0 taken 115769 times.
✓ Branch 1 taken 23790 times.
✓ Branch 2 taken 115769 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 115769 times.
255328 if (frame->width && frame->height &&
1505 115769 av_image_check_sar(frame->width, frame->height,
1506 frame->sample_aspect_ratio) < 0) {
1507 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1508 frame->sample_aspect_ratio.num,
1509 frame->sample_aspect_ratio.den);
1510 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1511 }
1512 139559 break;
1513 }
1514 400950 return 0;
1515 }
1516
1517 375314 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1518 {
1519
2/2
✓ Branch 0 taken 113923 times.
✓ Branch 1 taken 261391 times.
375314 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1520 int i;
1521 113923 int num_planes = av_pix_fmt_count_planes(frame->format);
1522 113923 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1523
1/2
✓ Branch 0 taken 113923 times.
✗ Branch 1 not taken.
113923 int flags = desc ? desc->flags : 0;
1524
4/4
✓ Branch 0 taken 13323 times.
✓ Branch 1 taken 100600 times.
✓ Branch 2 taken 4135 times.
✓ Branch 3 taken 9188 times.
113923 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1525 4135 num_planes = 2;
1526
2/2
✓ Branch 0 taken 320149 times.
✓ Branch 1 taken 113923 times.
434072 for (i = 0; i < num_planes; i++) {
1527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320149 times.
320149 av_assert0(frame->data[i]);
1528 }
1529 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1530
3/4
✓ Branch 0 taken 705158 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 591235 times.
✓ Branch 3 taken 113923 times.
705158 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 591235 times.
591235 if (frame->data[i])
1532 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1533 591235 frame->data[i] = NULL;
1534 }
1535 }
1536 375314 }
1537
1538 375314 static void decode_data_free(void *opaque, uint8_t *data)
1539 {
1540 375314 FrameDecodeData *fdd = (FrameDecodeData*)data;
1541
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (fdd->post_process_opaque_free)
1543 fdd->post_process_opaque_free(fdd->post_process_opaque);
1544
1545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (fdd->hwaccel_priv_free)
1546 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1547
1548 375314 av_freep(&fdd);
1549 375314 }
1550
1551 375314 int ff_attach_decode_data(AVFrame *frame)
1552 {
1553 AVBufferRef *fdd_buf;
1554 FrameDecodeData *fdd;
1555
1556 av_assert1(!frame->private_ref);
1557 375314 av_buffer_unref(&frame->private_ref);
1558
1559 375314 fdd = av_mallocz(sizeof(*fdd));
1560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (!fdd)
1561 return AVERROR(ENOMEM);
1562
1563 375314 fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
1564 NULL, AV_BUFFER_FLAG_READONLY);
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (!fdd_buf) {
1566 av_freep(&fdd);
1567 return AVERROR(ENOMEM);
1568 }
1569
1570 375314 frame->private_ref = fdd_buf;
1571
1572 375314 return 0;
1573 }
1574
1575 375314 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1576 {
1577 375314 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1578 375314 int override_dimensions = 1;
1579 int ret;
1580
1581
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 375314 times.
375314 av_assert0(av_codec_is_decoder(avctx->codec));
1582
1583
2/2
✓ Branch 0 taken 113923 times.
✓ Branch 1 taken 261391 times.
375314 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1584
2/4
✓ Branch 0 taken 113923 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113923 times.
✗ Branch 3 not taken.
227846 if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
1585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 113923 times.
227846 (ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
1586 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1587 ret = AVERROR(EINVAL);
1588 goto fail;
1589 }
1590
1591
3/4
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 113192 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 731 times.
113923 if (frame->width <= 0 || frame->height <= 0) {
1592 113192 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1593 113192 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1594 113192 override_dimensions = 0;
1595 }
1596
1597
4/8
✓ Branch 0 taken 113923 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113923 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 113923 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 113923 times.
113923 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1598 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1599 ret = AVERROR(EINVAL);
1600 goto fail;
1601 }
1602
1/2
✓ Branch 0 taken 261391 times.
✗ Branch 1 not taken.
261391 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261391 times.
261391 if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
1604 av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
1605 ret = AVERROR(EINVAL);
1606 goto fail;
1607 }
1608 }
1609 375314 ret = ff_decode_frame_props(avctx, frame);
1610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (ret < 0)
1611 goto fail;
1612
1613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (hwaccel) {
1614 if (hwaccel->alloc_frame) {
1615 ret = hwaccel->alloc_frame(avctx, frame);
1616 goto end;
1617 }
1618 } else
1619 375314 avctx->sw_pix_fmt = avctx->pix_fmt;
1620
1621 375314 ret = avctx->get_buffer2(avctx, frame, flags);
1622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (ret < 0)
1623 goto fail;
1624
1625 375314 validate_avframe_allocation(avctx, frame);
1626
1627 375314 ret = ff_attach_decode_data(frame);
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (ret < 0)
1629 goto fail;
1630
1631 375314 end:
1632
4/4
✓ Branch 0 taken 261391 times.
✓ Branch 1 taken 113923 times.
✓ Branch 2 taken 731 times.
✓ Branch 3 taken 113192 times.
375314 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1633
2/2
✓ Branch 1 taken 36134 times.
✓ Branch 2 taken 77058 times.
113192 !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1634 77058 frame->width = avctx->width;
1635 77058 frame->height = avctx->height;
1636 }
1637
1638 298256 fail:
1639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375314 times.
375314 if (ret < 0) {
1640 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1641 av_frame_unref(frame);
1642 }
1643
1644 375314 return ret;
1645 }
1646
1647 7739 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1648 {
1649 AVFrame *tmp;
1650 int ret;
1651
1652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7739 times.
7739 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1653
1654
5/8
✓ Branch 0 taken 7590 times.
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 7590 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7590 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7590 times.
7739 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1655 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1656 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1657 av_frame_unref(frame);
1658 }
1659
1660
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 7590 times.
7739 if (!frame->data[0])
1661 149 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1662
1663
4/4
✓ Branch 0 taken 7589 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1018 times.
✓ Branch 4 taken 6571 times.
7590 if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
1664 1019 return ff_decode_frame_props(avctx, frame);
1665
1666 6571 tmp = av_frame_alloc();
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6571 times.
6571 if (!tmp)
1668 return AVERROR(ENOMEM);
1669
1670 6571 av_frame_move_ref(tmp, frame);
1671
1672 6571 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6571 times.
6571 if (ret < 0) {
1674 av_frame_free(&tmp);
1675 return ret;
1676 }
1677
1678 6571 av_frame_copy(frame, tmp);
1679 6571 av_frame_free(&tmp);
1680
1681 6571 return 0;
1682 }
1683
1684 7739 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1685 {
1686 7739 int ret = reget_buffer_internal(avctx, frame, flags);
1687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7739 times.
7739 if (ret < 0)
1688 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1689 7739 return ret;
1690 }
1691
1692 typedef struct ProgressInternal {
1693 ThreadProgress progress;
1694 struct AVFrame *f;
1695 } ProgressInternal;
1696
1697 386251 static void check_progress_consistency(const ProgressFrame *f)
1698 {
1699 av_assert1(!!f->f == !!f->progress);
1700 av_assert1(!f->progress || f->progress->f == f->f);
1701 386251 }
1702
1703 15661 static int progress_frame_get(AVCodecContext *avctx, ProgressFrame *f)
1704 {
1705 15661 FFRefStructPool *pool = avctx->internal->progress_frame_pool;
1706
1707 av_assert1(!f->f && !f->progress);
1708
1709 15661 f->progress = ff_refstruct_pool_get(pool);
1710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15661 times.
15661 if (!f->progress)
1711 return AVERROR(ENOMEM);
1712
1713 15661 f->f = f->progress->f;
1714 15661 return 0;
1715 }
1716
1717 15661 int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
1718 {
1719 int ret;
1720
1721 15661 ret = progress_frame_get(avctx, f);
1722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15661 times.
15661 if (ret < 0)
1723 return ret;
1724
1725 15661 ret = ff_thread_get_buffer(avctx, f->progress->f, flags);
1726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15661 times.
15661 if (ret < 0) {
1727 f->f = NULL;
1728 ff_refstruct_unref(&f->progress);
1729 return ret;
1730 }
1731 15661 return 0;
1732 }
1733
1734 38946 void ff_progress_frame_ref(ProgressFrame *dst, const ProgressFrame *src)
1735 {
1736 av_assert1(src->progress && src->f && src->f == src->progress->f);
1737 av_assert1(!dst->f && !dst->progress);
1738 38946 dst->f = src->f;
1739 38946 dst->progress = ff_refstruct_ref(src->progress);
1740 38946 }
1741
1742 346097 void ff_progress_frame_unref(ProgressFrame *f)
1743 {
1744 346097 check_progress_consistency(f);
1745 346097 f->f = NULL;
1746 346097 ff_refstruct_unref(&f->progress);
1747 346097 }
1748
1749 40154 void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
1750 {
1751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40154 times.
40154 if (dst == src)
1752 return;
1753 40154 ff_progress_frame_unref(dst);
1754 40154 check_progress_consistency(src);
1755
2/2
✓ Branch 0 taken 38879 times.
✓ Branch 1 taken 1275 times.
40154 if (src->f)
1756 38879 ff_progress_frame_ref(dst, src);
1757 }
1758
1759 25253 void ff_progress_frame_report(ProgressFrame *f, int n)
1760 {
1761 25253 ff_thread_progress_report(&f->progress->progress, n);
1762 25253 }
1763
1764 2591886 void ff_progress_frame_await(const ProgressFrame *f, int n)
1765 {
1766 2591886 ff_thread_progress_await(&f->progress->progress, n);
1767 2591886 }
1768
1769 #if !HAVE_THREADS
1770 enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1771 {
1772 return FF_THREAD_NO_FRAME_THREADING;
1773 }
1774 #endif /* !HAVE_THREADS */
1775
1776 2518 static av_cold int progress_frame_pool_init_cb(FFRefStructOpaque opaque, void *obj)
1777 {
1778 2518 const AVCodecContext *avctx = opaque.nc;
1779 2518 ProgressInternal *progress = obj;
1780 int ret;
1781
1782 2518 ret = ff_thread_progress_init(&progress->progress, avctx->active_thread_type & FF_THREAD_FRAME);
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2518 times.
2518 if (ret < 0)
1784 return ret;
1785
1786 2518 progress->f = av_frame_alloc();
1787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2518 times.
2518 if (!progress->f)
1788 return AVERROR(ENOMEM);
1789
1790 2518 return 0;
1791 }
1792
1793 15661 static void progress_frame_pool_reset_cb(FFRefStructOpaque unused, void *obj)
1794 {
1795 15661 ProgressInternal *progress = obj;
1796
1797 15661 ff_thread_progress_reset(&progress->progress);
1798 15661 av_frame_unref(progress->f);
1799 15661 }
1800
1801 2518 static av_cold void progress_frame_pool_free_entry_cb(FFRefStructOpaque opaque, void *obj)
1802 {
1803 2518 ProgressInternal *progress = obj;
1804
1805 2518 ff_thread_progress_destroy(&progress->progress);
1806 2518 av_frame_free(&progress->f);
1807 2518 }
1808
1809 14518 int ff_decode_preinit(AVCodecContext *avctx)
1810 {
1811 14518 AVCodecInternal *avci = avctx->internal;
1812 14518 DecodeContext *dc = decode_ctx(avci);
1813 14518 int ret = 0;
1814
1815 14518 dc->initial_pict_type = AV_PICTURE_TYPE_NONE;
1816
2/2
✓ Branch 0 taken 11278 times.
✓ Branch 1 taken 3240 times.
14518 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY) {
1817 11278 dc->intra_only_flag = AV_FRAME_FLAG_KEY;
1818
2/2
✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 3238 times.
11278 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
1819 8040 dc->initial_pict_type = AV_PICTURE_TYPE_I;
1820 }
1821
1822 /* if the decoder init function was already called previously,
1823 * free the already allocated subtitle_header before overwriting it */
1824 14518 av_freep(&avctx->subtitle_header);
1825
1826
2/4
✓ Branch 0 taken 14518 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14518 times.
14518 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1827 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
1828 avctx->codec->max_lowres);
1829 avctx->lowres = avctx->codec->max_lowres;
1830 }
1831
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14512 times.
14518 if (avctx->sub_charenc) {
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1833 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1834 "supported with subtitles codecs\n");
1835 return AVERROR(EINVAL);
1836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1837 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1838 "subtitles character encoding will be ignored\n",
1839 avctx->codec_descriptor->name);
1840 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1841 } else {
1842 /* input character encoding is set for a text based subtitle
1843 * codec at this point */
1844
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1845 6 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1846
1847
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1848 #if CONFIG_ICONV
1849 6 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cd == (iconv_t)-1) {
1851 ret = AVERROR(errno);
1852 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1853 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1854 return ret;
1855 }
1856 6 iconv_close(cd);
1857 #else
1858 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1859 "conversion needs a libavcodec built with iconv support "
1860 "for this codec\n");
1861 return AVERROR(ENOSYS);
1862 #endif
1863 }
1864 }
1865 }
1866
1867 14518 dc->pts_correction_num_faulty_pts =
1868 14518 dc->pts_correction_num_faulty_dts = 0;
1869 14518 dc->pts_correction_last_pts =
1870 14518 dc->pts_correction_last_dts = INT64_MIN;
1871
1872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1873 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1874 av_log(avctx, AV_LOG_WARNING,
1875 "gray decoding requested but not enabled at configuration time\n");
1876
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14513 times.
14518 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
1877 5 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
1878 }
1879
1880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (avctx->nb_side_data_prefer_packet == 1 &&
1881 avctx->side_data_prefer_packet[0] == -1)
1882 dc->side_data_pref_mask = ~0ULL;
1883 else {
1884
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14518 times.
14522 for (unsigned i = 0; i < avctx->nb_side_data_prefer_packet; i++) {
1885 4 int val = avctx->side_data_prefer_packet[i];
1886
1887
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (val < 0 || val >= AV_PKT_DATA_NB) {
1888 av_log(avctx, AV_LOG_ERROR, "Invalid side data type: %d\n", val);
1889 return AVERROR(EINVAL);
1890 }
1891
1892
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
40 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
1893
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 if (ff_sd_global_map[j].packet == val) {
1894 4 val = ff_sd_global_map[j].frame;
1895
1896 // this code will need to be changed when we have more than
1897 // 64 frame side data types
1898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (val >= 64) {
1899 av_log(avctx, AV_LOG_ERROR, "Side data type too big\n");
1900 return AVERROR_BUG;
1901 }
1902
1903 4 dc->side_data_pref_mask |= 1ULL << val;
1904 }
1905 }
1906 }
1907 }
1908
1909 14518 avci->in_pkt = av_packet_alloc();
1910 14518 avci->last_pkt_props = av_packet_alloc();
1911
2/4
✓ Branch 0 taken 14518 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14518 times.
14518 if (!avci->in_pkt || !avci->last_pkt_props)
1912 return AVERROR(ENOMEM);
1913
1914
2/2
✓ Branch 1 taken 1272 times.
✓ Branch 2 taken 13246 times.
14518 if (ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_USES_PROGRESSFRAMES) {
1915 1272 avci->progress_frame_pool =
1916 1272 ff_refstruct_pool_alloc_ext(sizeof(ProgressInternal),
1917 FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
1918 avctx, progress_frame_pool_init_cb,
1919 progress_frame_pool_reset_cb,
1920 progress_frame_pool_free_entry_cb, NULL);
1921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1272 times.
1272 if (!avci->progress_frame_pool)
1922 return AVERROR(ENOMEM);
1923 }
1924 14518 ret = decode_bsfs_init(avctx);
1925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (ret < 0)
1926 return ret;
1927
1928 #if FF_API_DROPCHANGED
1929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14518 times.
14518 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED)
1930 av_log(avctx, AV_LOG_WARNING, "The dropchanged flag is deprecated.\n");
1931 #endif
1932
1933 14518 return 0;
1934 }
1935
1936 /**
1937 * Check side data preference and clear existing side data from frame
1938 * if needed.
1939 *
1940 * @retval 0 side data of this type can be added to frame
1941 * @retval 1 side data of this type should not be added to frame
1942 */
1943 5575 static int side_data_pref(const AVCodecContext *avctx, AVFrameSideData ***sd,
1944 int *nb_sd, enum AVFrameSideDataType type)
1945 {
1946 5575 DecodeContext *dc = decode_ctx(avctx->internal);
1947
1948 // Note: could be skipped for `type` without corresponding packet sd
1949
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 5555 times.
5575 if (av_frame_side_data_get(*sd, *nb_sd, type)) {
1950
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (dc->side_data_pref_mask & (1ULL << type))
1951 10 return 1;
1952 10 av_frame_side_data_remove(sd, nb_sd, type);
1953 }
1954
1955 5565 return 0;
1956 }
1957
1958
1959 4786 int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame,
1960 enum AVFrameSideDataType type, size_t size,
1961 AVFrameSideData **psd)
1962 {
1963 AVFrameSideData *sd;
1964
1965
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4786 times.
4786 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data, type)) {
1966 if (psd)
1967 *psd = NULL;
1968 return 0;
1969 }
1970
1971 4786 sd = av_frame_new_side_data(frame, type, size);
1972
1/2
✓ Branch 0 taken 4786 times.
✗ Branch 1 not taken.
4786 if (psd)
1973 4786 *psd = sd;
1974
1975
1/2
✓ Branch 0 taken 4786 times.
✗ Branch 1 not taken.
4786 return sd ? 0 : AVERROR(ENOMEM);
1976 }
1977
1978 754 int ff_frame_new_side_data_from_buf_ext(const AVCodecContext *avctx,
1979 AVFrameSideData ***sd, int *nb_sd,
1980 enum AVFrameSideDataType type,
1981 AVBufferRef **buf)
1982 {
1983 754 int ret = 0;
1984
1985
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 754 times.
754 if (side_data_pref(avctx, sd, nb_sd, type))
1986 goto finish;
1987
1988
1/2
✓ Branch 1 taken 754 times.
✗ Branch 2 not taken.
754 if (!av_frame_side_data_add(sd, nb_sd, type, buf, 0))
1989 ret = AVERROR(ENOMEM);
1990
1991 754 finish:
1992 754 av_buffer_unref(buf);
1993
1994 754 return ret;
1995 }
1996
1997 744 int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx,
1998 AVFrame *frame, enum AVFrameSideDataType type,
1999 AVBufferRef **buf, AVFrameSideData **psd)
2000 {
2001 744 return ff_frame_new_side_data_from_buf_ext(avctx,
2002 &frame->side_data, &frame->nb_side_data,
2003 type, buf);
2004 }
2005
2006 31 int ff_decode_mastering_display_new_ext(const AVCodecContext *avctx,
2007 AVFrameSideData ***sd, int *nb_sd,
2008 struct AVMasteringDisplayMetadata **mdm)
2009 {
2010 AVBufferRef *buf;
2011 size_t size;
2012
2013
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21 times.
31 if (side_data_pref(avctx, sd, nb_sd, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
2014 10 *mdm = NULL;
2015 10 return 0;
2016 }
2017
2018 21 *mdm = av_mastering_display_metadata_alloc_size(&size);
2019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!*mdm)
2020 return AVERROR(ENOMEM);
2021
2022 21 buf = av_buffer_create((uint8_t *)*mdm, size, NULL, NULL, 0);
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!buf) {
2024 av_freep(mdm);
2025 return AVERROR(ENOMEM);
2026 }
2027
2028
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (!av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA,
2029 &buf, 0)) {
2030 *mdm = NULL;
2031 av_buffer_unref(&buf);
2032 return AVERROR(ENOMEM);
2033 }
2034
2035 21 return 0;
2036 }
2037
2038 int ff_decode_mastering_display_new(const AVCodecContext *avctx, AVFrame *frame,
2039 AVMasteringDisplayMetadata **mdm)
2040 {
2041 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2042 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
2043 *mdm = NULL;
2044 return 0;
2045 }
2046
2047 *mdm = av_mastering_display_metadata_create_side_data(frame);
2048 return *mdm ? 0 : AVERROR(ENOMEM);
2049 }
2050
2051 4 int ff_decode_content_light_new_ext(const AVCodecContext *avctx,
2052 AVFrameSideData ***sd, int *nb_sd,
2053 AVContentLightMetadata **clm)
2054 {
2055 AVBufferRef *buf;
2056 size_t size;
2057
2058
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (side_data_pref(avctx, sd, nb_sd, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2059 *clm = NULL;
2060 return 0;
2061 }
2062
2063 4 *clm = av_content_light_metadata_alloc(&size);
2064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!*clm)
2065 return AVERROR(ENOMEM);
2066
2067 4 buf = av_buffer_create((uint8_t *)*clm, size, NULL, NULL, 0);
2068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!buf) {
2069 av_freep(clm);
2070 return AVERROR(ENOMEM);
2071 }
2072
2073
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL,
2074 &buf, 0)) {
2075 *clm = NULL;
2076 av_buffer_unref(&buf);
2077 return AVERROR(ENOMEM);
2078 }
2079
2080 4 return 0;
2081 }
2082
2083 int ff_decode_content_light_new(const AVCodecContext *avctx, AVFrame *frame,
2084 AVContentLightMetadata **clm)
2085 {
2086 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2087 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2088 *clm = NULL;
2089 return 0;
2090 }
2091
2092 *clm = av_content_light_metadata_create_side_data(frame);
2093 return *clm ? 0 : AVERROR(ENOMEM);
2094 }
2095
2096 1673 int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
2097 {
2098 size_t size;
2099 1673 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
2100
2101
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1641 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
1673 if (pal && size == AVPALETTE_SIZE) {
2102 32 memcpy(dst, pal, AVPALETTE_SIZE);
2103 32 return 1;
2104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1641 times.
1641 } else if (pal) {
2105 av_log(logctx, AV_LOG_ERROR,
2106 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
2107 }
2108 1641 return 0;
2109 }
2110
2111 49914 int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
2112 {
2113 49914 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
2114
2115
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49914 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49914 if (!hwaccel || !hwaccel->frame_priv_data_size)
2116 49914 return 0;
2117
2118 av_assert0(!*hwaccel_picture_private);
2119
2120 if (hwaccel->free_frame_priv) {
2121 AVHWFramesContext *frames_ctx;
2122
2123 if (!avctx->hw_frames_ctx)
2124 return AVERROR(EINVAL);
2125
2126 frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
2127 *hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
2128 frames_ctx->device_ctx,
2129 hwaccel->free_frame_priv);
2130 } else {
2131 *hwaccel_picture_private = ff_refstruct_allocz(hwaccel->frame_priv_data_size);
2132 }
2133
2134 if (!*hwaccel_picture_private)
2135 return AVERROR(ENOMEM);
2136
2137 return 0;
2138 }
2139
2140 102 void ff_decode_flush_buffers(AVCodecContext *avctx)
2141 {
2142 102 AVCodecInternal *avci = avctx->internal;
2143 102 DecodeContext *dc = decode_ctx(avci);
2144
2145 102 av_packet_unref(avci->last_pkt_props);
2146 102 av_packet_unref(avci->in_pkt);
2147
2148 102 dc->pts_correction_last_pts =
2149 102 dc->pts_correction_last_dts = INT64_MIN;
2150
2151 102 av_bsf_flush(avci->bsf);
2152
2153 102 dc->nb_draining_errors = 0;
2154 102 dc->draining_started = 0;
2155 102 }
2156
2157 14650 AVCodecInternal *ff_decode_internal_alloc(void)
2158 {
2159 14650 return av_mallocz(sizeof(DecodeContext));
2160 }
2161