FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 815 1236 65.9%
Functions: 66 69 95.7%
Branches: 498 887 56.1%

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