FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2026-07-22 00:55:30
Exec Total Coverage
Lines: 887 1289 68.8%
Functions: 69 72 95.8%
Branches: 538 907 59.3%

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