FFmpeg coverage


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