FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2026-09-28 04:46:15
Exec Total Coverage
Lines: 889 1291 68.9%
Functions: 69 72 95.8%
Branches: 540 909 59.4%

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