FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2025-12-07 22:02:57
Exec Total Coverage
Lines: 875 1283 68.2%
Functions: 71 74 95.9%
Branches: 521 897 58.1%

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