FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2026-09-17 21:53:11
Exec Total Coverage
Lines: 889 1291 68.9%
Functions: 69 72 95.8%
Branches: 538 907 59.3%

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