FFmpeg coverage


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