FFmpeg coverage


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