FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2024-09-13 17:16:59
Exec Total Coverage
Lines: 755 1157 65.3%
Functions: 59 62 95.2%
Branches: 479 847 56.6%

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