FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2025-11-30 00:22:21
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 3514863 static DecodeContext *decode_ctx(AVCodecInternal *avci)
108 {
109 3514863 return (DecodeContext *)avci;
110 }
111
112 382595 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 382595 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
121
2/2
✓ Branch 0 taken 382593 times.
✓ Branch 1 taken 2 times.
382595 if (!data)
122 382593 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 382595 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
174 {
175 382595 int ret = 0;
176
177 382595 av_packet_unref(avci->last_pkt_props);
178
1/2
✓ Branch 0 taken 382595 times.
✗ Branch 1 not taken.
382595 if (pkt) {
179 382595 ret = av_packet_copy_props(avci->last_pkt_props, pkt);
180 }
181 382595 return ret;
182 }
183
184 15827 static int decode_bsfs_init(AVCodecContext *avctx)
185 {
186 15827 AVCodecInternal *avci = avctx->internal;
187 15827 const FFCodec *const codec = ffcodec(avctx->codec);
188 int ret;
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15827 times.
15827 if (avci->bsf)
191 return 0;
192
193 15827 ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15827 times.
15827 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 15827 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
205 15827 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15827 times.
15827 if (ret < 0)
207 goto fail;
208
209 15827 ret = av_bsf_init(avci->bsf);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15827 times.
15827 if (ret < 0)
211 goto fail;
212
213 15827 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) (AVERROR_BUG)
222 #endif
223
224 1150538 static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
225 {
226 1150538 AVCodecInternal *avci = avctx->internal;
227 int ret;
228
229 1150538 ret = av_bsf_receive_packet(avci->bsf, pkt);
230
2/2
✓ Branch 0 taken 767943 times.
✓ Branch 1 taken 382595 times.
1150538 if (ret < 0)
231 767943 return ret;
232
233
1/2
✓ Branch 1 taken 382595 times.
✗ Branch 2 not taken.
382595 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
234 382595 ret = extract_packet_props(avctx->internal, pkt);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382595 times.
382595 if (ret < 0)
236 goto finish;
237 }
238
239 382595 ret = apply_param_change(avctx, pkt);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382595 times.
382595 if (ret < 0)
241 goto finish;
242
243 382595 return 0;
244 finish:
245 av_packet_unref(pkt);
246 return ret;
247 }
248
249 765801 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
250 {
251 765801 AVCodecInternal *avci = avctx->internal;
252 765801 DecodeContext *dc = decode_ctx(avci);
253
254
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 765608 times.
765801 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 764532 times.
✓ Branch 1 taken 1076 times.
765608 if (avctx->internal->is_frame_mt)
262 1076 return ff_thread_get_packet(avctx, pkt);
263
264 386006 while (1) {
265 1150538 int ret = decode_get_packet(avctx, pkt);
266
2/2
✓ Branch 0 taken 764468 times.
✓ Branch 1 taken 386070 times.
1150538 if (ret == AVERROR(EAGAIN) &&
267
5/6
✓ Branch 0 taken 381937 times.
✓ Branch 1 taken 382531 times.
✓ Branch 2 taken 381937 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3475 times.
✓ Branch 5 taken 378462 times.
764468 (!AVPACKET_IS_EMPTY(avci->buffer_pkt) || dc->draining_started)) {
268 386006 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
269
1/2
✓ Branch 0 taken 386006 times.
✗ Branch 1 not taken.
386006 if (ret >= 0)
270 386006 continue;
271
272 av_packet_unref(avci->buffer_pkt);
273 }
274
275
2/2
✓ Branch 0 taken 3475 times.
✓ Branch 1 taken 761057 times.
764532 if (ret == AVERROR_EOF)
276 3475 avci->draining = 1;
277 764532 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 400550 static int64_t guess_correct_pts(DecodeContext *dc,
292 int64_t reordered_pts, int64_t dts)
293 {
294 400550 int64_t pts = AV_NOPTS_VALUE;
295
296
2/2
✓ Branch 0 taken 310252 times.
✓ Branch 1 taken 90298 times.
400550 if (dts != AV_NOPTS_VALUE) {
297 310252 dc->pts_correction_num_faulty_dts += dts <= dc->pts_correction_last_dts;
298 310252 dc->pts_correction_last_dts = dts;
299
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 89938 times.
90298 } else if (reordered_pts != AV_NOPTS_VALUE)
300 360 dc->pts_correction_last_dts = reordered_pts;
301
302
2/2
✓ Branch 0 taken 309872 times.
✓ Branch 1 taken 90678 times.
400550 if (reordered_pts != AV_NOPTS_VALUE) {
303 309872 dc->pts_correction_num_faulty_pts += reordered_pts <= dc->pts_correction_last_pts;
304 309872 dc->pts_correction_last_pts = reordered_pts;
305
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 89938 times.
90678 } 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 400098 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 440 times.
400550 if ((dc->pts_correction_num_faulty_pts<=dc->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
309
2/2
✓ Branch 0 taken 309432 times.
✓ Branch 1 taken 90678 times.
400110 && reordered_pts != AV_NOPTS_VALUE)
310 309432 pts = reordered_pts;
311 else
312 91118 pts = dts;
313
314 400550 return pts;
315 }
316
317 267235 static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
318 {
319 267235 AVCodecInternal *avci = avctx->internal;
320 AVFrameSideData *side;
321 267235 uint32_t discard_padding = 0;
322 267235 uint8_t skip_reason = 0;
323 267235 uint8_t discard_reason = 0;
324
325 267235 side = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
326
3/4
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 267112 times.
✓ Branch 2 taken 123 times.
✗ Branch 3 not taken.
267235 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 267235 times.
267235 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 267235 av_frame_remove_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
349
350
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 267215 times.
267235 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 266986 times.
267215 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 267046 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
267057 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 267055 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 772015 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
416 {
417 772015 AVCodecInternal *avci = avctx->internal;
418 772015 DecodeContext *dc = decode_ctx(avci);
419 772015 AVPacket *const pkt = avci->in_pkt;
420 772015 const FFCodec *const codec = ffcodec(avctx->codec);
421 int got_frame, consumed;
422 int ret;
423
424
4/4
✓ Branch 0 taken 743398 times.
✓ Branch 1 taken 28617 times.
✓ Branch 2 taken 742067 times.
✓ Branch 3 taken 1331 times.
772015 if (!pkt->data && !avci->draining) {
425 742067 av_packet_unref(pkt);
426 742067 ret = ff_decode_get_packet(avctx, pkt);
427
4/4
✓ Branch 0 taken 370741 times.
✓ Branch 1 taken 371326 times.
✓ Branch 2 taken 367486 times.
✓ Branch 3 taken 3255 times.
742067 if (ret < 0 && ret != AVERROR_EOF)
428 367486 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 570 times.
✓ Branch 1 taken 403959 times.
404529 if (avci->draining_done)
434 570 return AVERROR_EOF;
435
436
2/2
✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 399943 times.
403959 if (!pkt->data &&
437
2/2
✓ Branch 0 taken 2753 times.
✓ Branch 1 taken 1263 times.
4016 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
438 2753 return AVERROR_EOF;
439
440 401206 got_frame = 0;
441
442 401206 frame->pict_type = dc->initial_pict_type;
443 401206 frame->flags |= dc->intra_only_flag;
444 401206 consumed = codec->cb.decode(avctx, frame, &got_frame, pkt);
445
446
1/2
✓ Branch 0 taken 401206 times.
✗ Branch 1 not taken.
401206 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
447 401206 frame->pkt_dts = pkt->dts;
448 401206 emms_c();
449
450
2/2
✓ Branch 0 taken 134400 times.
✓ Branch 1 taken 266806 times.
401206 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
451
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 123339 times.
257917 ret = (!got_frame || frame->flags & AV_FRAME_FLAG_DISCARD)
452 ? AVERROR(EAGAIN)
453
2/2
✓ Branch 0 taken 123517 times.
✓ Branch 1 taken 10883 times.
257917 : 0;
454
1/2
✓ Branch 0 taken 266806 times.
✗ Branch 1 not taken.
266806 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
455 266806 ret = !got_frame ? AVERROR(EAGAIN)
456
2/2
✓ Branch 0 taken 265426 times.
✓ Branch 1 taken 1380 times.
266806 : discard_samples(avctx, frame, discarded_samples);
457 } else
458 av_assert0(0);
459
460
2/2
✓ Branch 0 taken 12621 times.
✓ Branch 1 taken 388585 times.
401206 if (ret == AVERROR(EAGAIN))
461 12621 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 401206 times.
401206 av_assert0(consumed != AVERROR(EAGAIN));
466
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 400558 times.
401206 if (consumed < 0)
467 648 ret = consumed;
468
4/4
✓ Branch 0 taken 400558 times.
✓ Branch 1 taken 648 times.
✓ Branch 2 taken 133790 times.
✓ Branch 3 taken 266768 times.
401206 if (consumed >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
469 133790 consumed = pkt->size;
470
471
2/2
✓ Branch 0 taken 388585 times.
✓ Branch 1 taken 12621 times.
401206 if (!ret)
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388585 times.
388585 av_assert0(frame->buf[0]);
473
2/2
✓ Branch 0 taken 11973 times.
✓ Branch 1 taken 389233 times.
401206 if (ret == AVERROR(EAGAIN))
474 11973 ret = 0;
475
476 /* do not stop draining when got_frame != 0 or ret < 0 */
477
4/4
✓ Branch 0 taken 1263 times.
✓ Branch 1 taken 399943 times.
✓ Branch 2 taken 571 times.
✓ Branch 3 taken 692 times.
401206 if (avci->draining && !got_frame) {
478
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 570 times.
571 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 570 avci->draining_done = 1;
492 }
493 }
494
495
4/4
✓ Branch 0 taken 29279 times.
✓ Branch 1 taken 371927 times.
✓ Branch 2 taken 648 times.
✓ Branch 3 taken 28631 times.
401206 if (consumed >= pkt->size || ret < 0) {
496 372575 av_packet_unref(pkt);
497 } else {
498 28631 pkt->data += consumed;
499 28631 pkt->size -= consumed;
500 28631 pkt->pts = AV_NOPTS_VALUE;
501 28631 pkt->dts = AV_NOPTS_VALUE;
502
1/2
✓ Branch 0 taken 28631 times.
✗ Branch 1 not taken.
28631 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
503 28631 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
504 28631 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
505 }
506 }
507
508 401206 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 784174 static int detect_colorspace(av_unused AVCodecContext *c, av_unused AVFrame *f)
556 {
557 784174 return 0;
558 }
559 #endif
560
561 803625 static int fill_frame_props(const AVCodecContext *avctx, AVFrame *frame)
562 {
563 int ret;
564
565
2/2
✓ Branch 0 taken 535708 times.
✓ Branch 1 taken 267917 times.
803625 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
566 535708 frame->color_primaries = avctx->color_primaries;
567
2/2
✓ Branch 0 taken 535431 times.
✓ Branch 1 taken 268194 times.
803625 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
568 535431 frame->color_trc = avctx->color_trc;
569
2/2
✓ Branch 0 taken 526075 times.
✓ Branch 1 taken 277550 times.
803625 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
570 526075 frame->colorspace = avctx->colorspace;
571
2/2
✓ Branch 0 taken 726597 times.
✓ Branch 1 taken 77028 times.
803625 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
572 726597 frame->color_range = avctx->color_range;
573
2/2
✓ Branch 0 taken 775267 times.
✓ Branch 1 taken 28358 times.
803625 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
574 775267 frame->chroma_location = avctx->chroma_sample_location;
575
2/2
✓ Branch 0 taken 803466 times.
✓ Branch 1 taken 159 times.
803625 if (frame->alpha_mode == AVALPHA_MODE_UNSPECIFIED)
576 803466 frame->alpha_mode = avctx->alpha_mode;
577
578
2/2
✓ Branch 0 taken 269274 times.
✓ Branch 1 taken 534351 times.
803625 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
579
2/2
✓ Branch 0 taken 249544 times.
✓ Branch 1 taken 19730 times.
269274 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
580
2/2
✓ Branch 0 taken 132369 times.
✓ Branch 1 taken 136905 times.
269274 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
581
1/2
✓ Branch 0 taken 534351 times.
✗ Branch 1 not taken.
534351 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
582
2/2
✓ Branch 0 taken 267270 times.
✓ Branch 1 taken 267081 times.
534351 if (frame->format == AV_SAMPLE_FMT_NONE)
583 267270 frame->format = avctx->sample_fmt;
584
2/2
✓ Branch 0 taken 267270 times.
✓ Branch 1 taken 267081 times.
534351 if (!frame->ch_layout.nb_channels) {
585 267270 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 267270 times.
267270 if (ret < 0)
587 return ret;
588 }
589
2/2
✓ Branch 0 taken 267129 times.
✓ Branch 1 taken 267222 times.
534351 if (!frame->sample_rate)
590 267129 frame->sample_rate = avctx->sample_rate;
591 }
592
593 803625 return 0;
594 }
595
596 760042 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
597 {
598 int ret;
599 760042 int64_t discarded_samples = 0;
600
601
2/2
✓ Branch 0 taken 772015 times.
✓ Branch 1 taken 388585 times.
1160600 while (!frame->buf[0]) {
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 772015 times.
772015 if (discarded_samples > avctx->max_samples)
603 return AVERROR(EAGAIN);
604 772015 ret = decode_simple_internal(avctx, frame, &discarded_samples);
605
2/2
✓ Branch 0 taken 371457 times.
✓ Branch 1 taken 400558 times.
772015 if (ret < 0)
606 371457 return ret;
607 }
608
609 388585 return 0;
610 }
611
612 784486 int ff_decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
613 {
614 784486 AVCodecInternal *avci = avctx->internal;
615 784486 DecodeContext *dc = decode_ctx(avci);
616 784486 const FFCodec *const codec = ffcodec(avctx->codec);
617 int ret;
618
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784486 times.
784486 av_assert0(!frame->buf[0]);
620
621
2/2
✓ Branch 0 taken 24444 times.
✓ Branch 1 taken 760042 times.
784486 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 760042 ret = decode_simple_receive_frame(avctx, frame);
641
642
2/2
✓ Branch 0 taken 3534 times.
✓ Branch 1 taken 780952 times.
784486 if (ret == AVERROR_EOF)
643 3534 avci->draining_done = 1;
644
645 784486 return ret;
646 }
647
648 784174 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
649 {
650 784174 AVCodecInternal *avci = avctx->internal;
651 784174 DecodeContext *dc = decode_ctx(avci);
652 int ret, ok;
653
654
2/2
✓ Branch 0 taken 832 times.
✓ Branch 1 taken 783342 times.
784174 if (avctx->active_thread_type & FF_THREAD_FRAME)
655 832 ret = ff_thread_receive_frame(avctx, frame);
656 else
657 783342 ret = ff_decode_receive_frame_internal(avctx, frame);
658
659 /* preserve ret */
660 784174 ok = detect_colorspace(avctx, frame);
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 784174 times.
784174 if (ok < 0) {
662 av_frame_unref(frame);
663 return ok;
664 }
665
666
2/2
✓ Branch 0 taken 400550 times.
✓ Branch 1 taken 383624 times.
784174 if (!ret) {
667
2/2
✓ Branch 0 taken 133495 times.
✓ Branch 1 taken 267055 times.
400550 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
668
2/2
✓ Branch 0 taken 24951 times.
✓ Branch 1 taken 108544 times.
133495 if (!frame->width)
669 24951 frame->width = avctx->width;
670
2/2
✓ Branch 0 taken 24951 times.
✓ Branch 1 taken 108544 times.
133495 if (!frame->height)
671 24951 frame->height = avctx->height;
672 }
673
674 400550 ret = fill_frame_props(avctx, frame);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400550 times.
400550 if (ret < 0) {
676 av_frame_unref(frame);
677 return ret;
678 }
679
680 400550 frame->best_effort_timestamp = guess_correct_pts(dc,
681 frame->pts,
682 frame->pkt_dts);
683
684 /* the only case where decode data is not set should be decoders
685 * that do not call ff_get_buffer() */
686
3/4
✓ Branch 0 taken 27365 times.
✓ Branch 1 taken 373185 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27365 times.
400550 av_assert0(frame->private_ref ||
687 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
688
689
2/2
✓ Branch 0 taken 373185 times.
✓ Branch 1 taken 27365 times.
400550 if (frame->private_ref) {
690 373185 FrameDecodeData *fdd = frame->private_ref;
691
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 373185 times.
373185 if (fdd->post_process) {
693 ret = fdd->post_process(avctx, frame);
694 if (ret < 0) {
695 av_frame_unref(frame);
696 return ret;
697 }
698 }
699 }
700 }
701
702 /* free the per-frame decode data */
703 784174 av_refstruct_unref(&frame->private_ref);
704
705 784174 return ret;
706 }
707
708 386036 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
709 {
710 386036 AVCodecInternal *avci = avctx->internal;
711 386036 DecodeContext *dc = decode_ctx(avci);
712 int ret;
713
714
2/4
✓ Branch 1 taken 386036 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 386036 times.
386036 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
715 return AVERROR(EINVAL);
716
717
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 386006 times.
386036 if (dc->draining_started)
718 30 return AVERROR_EOF;
719
720
5/6
✓ Branch 0 taken 382635 times.
✓ Branch 1 taken 3371 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 382531 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 104 times.
386006 if (avpkt && !avpkt->size && avpkt->data)
721 return AVERROR(EINVAL);
722
723
5/6
✓ Branch 0 taken 382635 times.
✓ Branch 1 taken 3371 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 382531 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 104 times.
386006 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
724
2/4
✓ Branch 0 taken 382531 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 382531 times.
382531 if (!AVPACKET_IS_EMPTY(avci->buffer_pkt))
725 return AVERROR(EAGAIN);
726 382531 ret = av_packet_ref(avci->buffer_pkt, avpkt);
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382531 times.
382531 if (ret < 0)
728 return ret;
729 } else
730 3475 dc->draining_started = 1;
731
732
3/4
✓ Branch 0 taken 386006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 382531 times.
✓ Branch 3 taken 3475 times.
386006 if (!avci->buffer_frame->buf[0] && !dc->draining_started) {
733 382531 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
734
5/6
✓ Branch 0 taken 11881 times.
✓ Branch 1 taken 370650 times.
✓ Branch 2 taken 642 times.
✓ Branch 3 taken 11239 times.
✓ Branch 4 taken 642 times.
✗ Branch 5 not taken.
382531 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
735 642 return ret;
736 }
737
738 385364 return 0;
739 }
740
741 133495 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
742 {
743 /* make sure we are noisy about decoders returning invalid cropping data */
744
1/2
✓ Branch 0 taken 133495 times.
✗ Branch 1 not taken.
133495 if (frame->crop_left >= INT_MAX - frame->crop_right ||
745
1/2
✓ Branch 0 taken 133495 times.
✗ Branch 1 not taken.
133495 frame->crop_top >= INT_MAX - frame->crop_bottom ||
746
1/2
✓ Branch 0 taken 133495 times.
✗ Branch 1 not taken.
133495 (frame->crop_left + frame->crop_right) >= frame->width ||
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133495 times.
133495 (frame->crop_top + frame->crop_bottom) >= frame->height) {
748 av_log(avctx, AV_LOG_WARNING,
749 "Invalid cropping information set by a decoder: "
750 "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
751 "(frame size %dx%d). This is a bug, please report it\n",
752 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
753 frame->width, frame->height);
754 frame->crop_left = 0;
755 frame->crop_right = 0;
756 frame->crop_top = 0;
757 frame->crop_bottom = 0;
758 return 0;
759 }
760
761
2/2
✓ Branch 0 taken 126014 times.
✓ Branch 1 taken 7481 times.
133495 if (!avctx->apply_cropping)
762 126014 return 0;
763
764 7481 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
765 AV_FRAME_CROP_UNALIGNED : 0);
766 }
767
768 // make sure frames returned to the caller are valid
769 400550 static int frame_validate(AVCodecContext *avctx, AVFrame *frame)
770 {
771
2/4
✓ Branch 0 taken 400550 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 400550 times.
400550 if (!frame->buf[0] || frame->format < 0)
772 goto fail;
773
774
2/3
✓ Branch 0 taken 133495 times.
✓ Branch 1 taken 267055 times.
✗ Branch 2 not taken.
400550 switch (avctx->codec_type) {
775 133495 case AVMEDIA_TYPE_VIDEO:
776
2/4
✓ Branch 0 taken 133495 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 133495 times.
133495 if (frame->width <= 0 || frame->height <= 0)
777 goto fail;
778 133495 break;
779 267055 case AVMEDIA_TYPE_AUDIO:
780
1/2
✓ Branch 1 taken 267055 times.
✗ Branch 2 not taken.
267055 if (!av_channel_layout_check(&frame->ch_layout) ||
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 267055 times.
267055 frame->sample_rate <= 0)
782 goto fail;
783
784 267055 break;
785 default: av_assert0(0);
786 }
787
788 400550 return 0;
789 fail:
790 av_log(avctx, AV_LOG_ERROR, "An invalid frame was output by a decoder. "
791 "This is a bug, please report it.\n");
792 return AVERROR_BUG;
793 }
794
795 772293 int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
796 {
797 772293 AVCodecInternal *avci = avctx->internal;
798 int ret;
799
800
2/2
✓ Branch 0 taken 370650 times.
✓ Branch 1 taken 401643 times.
772293 if (avci->buffer_frame->buf[0]) {
801 370650 av_frame_move_ref(frame, avci->buffer_frame);
802 } else {
803 401643 ret = decode_receive_frame_internal(avctx, frame);
804
2/2
✓ Branch 0 taken 371743 times.
✓ Branch 1 taken 29900 times.
401643 if (ret < 0)
805 371743 return ret;
806 }
807
808 400550 ret = frame_validate(avctx, frame);
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400550 times.
400550 if (ret < 0)
810 goto fail;
811
812
2/2
✓ Branch 0 taken 133495 times.
✓ Branch 1 taken 267055 times.
400550 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
813 133495 ret = apply_cropping(avctx, frame);
814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133495 times.
133495 if (ret < 0)
815 goto fail;
816 }
817
818 400550 avctx->frame_num++;
819
820 400550 return 0;
821 fail:
822 av_frame_unref(frame);
823 return ret;
824 }
825
826 1873 static void get_subtitle_defaults(AVSubtitle *sub)
827 {
828 1873 memset(sub, 0, sizeof(*sub));
829 1873 sub->pts = AV_NOPTS_VALUE;
830 1873 }
831
832 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
833 1835 static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt,
834 const AVPacket *inpkt, AVPacket *buf_pkt)
835 {
836 #if CONFIG_ICONV
837 1835 iconv_t cd = (iconv_t)-1;
838 1835 int ret = 0;
839 char *inb, *outb;
840 size_t inl, outl;
841 #endif
842
843
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) {
844 1742 *outpkt = inpkt;
845 1742 return 0;
846 }
847
848 #if CONFIG_ICONV
849 93 inb = inpkt->data;
850 93 inl = inpkt->size;
851
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
853 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
854 return AVERROR(ERANGE);
855 }
856
857 93 cd = iconv_open("UTF-8", avctx->sub_charenc);
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 av_assert0(cd != (iconv_t)-1);
859
860 93 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
862 goto end;
863 93 ret = av_packet_copy_props(buf_pkt, inpkt);
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
865 goto end;
866 93 outb = buf_pkt->data;
867 93 outl = buf_pkt->size;
868
869
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 ||
870 93 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
871
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) {
872 ret = FFMIN(AVERROR(errno), -1);
873 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
874 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
875 goto end;
876 }
877 93 buf_pkt->size -= outl;
878 93 memset(buf_pkt->data + buf_pkt->size, 0, outl);
879 93 *outpkt = buf_pkt;
880
881 93 ret = 0;
882 93 end:
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
884 av_packet_unref(buf_pkt);
885
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (cd != (iconv_t)-1)
886 93 iconv_close(cd);
887 93 return ret;
888 #else
889 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
890 return AVERROR(EINVAL);
891 #endif
892 }
893
894 860 static int utf8_check(const uint8_t *str)
895 {
896 const uint8_t *byte;
897 uint32_t codepoint, min;
898
899
2/2
✓ Branch 0 taken 74680 times.
✓ Branch 1 taken 860 times.
75540 while (*str) {
900 74680 byte = str;
901
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;);
902
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 :
903 713 1 << (5 * (byte - str) - 4);
904
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 ||
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74680 times.
74680 codepoint == 0xFFFE /* BOM */ ||
906 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
907 return 0;
908 74680 str = byte;
909 }
910 860 return 1;
911 }
912
913 1873 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
914 int *got_sub_ptr, const AVPacket *avpkt)
915 {
916 1873 int ret = 0;
917
918
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) {
919 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
920 return AVERROR(EINVAL);
921 }
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1873 times.
1873 if (!avctx->codec)
923 return AVERROR(EINVAL);
924
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1873 times.
1873 if (ffcodec(avctx->codec)->cb_type != FF_CODEC_CB_TYPE_DECODE_SUB) {
925 av_log(avctx, AV_LOG_ERROR, "Codec not subtitle decoder\n");
926 return AVERROR(EINVAL);
927 }
928
929 1873 *got_sub_ptr = 0;
930 1873 get_subtitle_defaults(sub);
931
932
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) {
933 1835 AVCodecInternal *avci = avctx->internal;
934 const AVPacket *pkt;
935
936 1835 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1835 times.
1835 if (ret < 0)
938 79 return ret;
939
940
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)
941 1825 sub->pts = av_rescale_q(avpkt->pts,
942 1825 avctx->pkt_timebase, AV_TIME_BASE_Q);
943 1835 ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
944
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1742 times.
1835 if (pkt == avci->buffer_pkt) // did we recode?
945 93 av_packet_unref(avci->buffer_pkt);
946
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1756 times.
1835 if (ret < 0) {
947 79 *got_sub_ptr = 0;
948 79 avsubtitle_free(sub);
949 79 return ret;
950 }
951 av_assert1(!sub->num_rects || *got_sub_ptr);
952
953
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 &&
954
1/2
✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
684 avctx->pkt_timebase.num) {
955 684 AVRational ms = { 1, 1000 };
956 684 sub->end_display_time = av_rescale_q(avpkt->duration,
957 avctx->pkt_timebase, ms);
958 }
959
960
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1600 times.
1756 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
961 156 sub->format = 0;
962
1/2
✓ Branch 0 taken 1600 times.
✗ Branch 1 not taken.
1600 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
963 1600 sub->format = 1;
964
965
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 1756 times.
2775 for (unsigned i = 0; i < sub->num_rects; i++) {
966
1/2
✓ Branch 0 taken 1019 times.
✗ Branch 1 not taken.
1019 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
967
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)) {
968 av_log(avctx, AV_LOG_ERROR,
969 "Invalid UTF-8 in decoded subtitles text; "
970 "maybe missing -sub_charenc option\n");
971 avsubtitle_free(sub);
972 *got_sub_ptr = 0;
973 return AVERROR_INVALIDDATA;
974 }
975 }
976
977
2/2
✓ Branch 0 taken 995 times.
✓ Branch 1 taken 761 times.
1756 if (*got_sub_ptr)
978 995 avctx->frame_num++;
979 }
980
981 1794 return ret;
982 }
983
984 1617 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
985 const enum AVPixelFormat *fmt)
986 {
987 const AVPixFmtDescriptor *desc;
988 const AVCodecHWConfig *config;
989 int i, n;
990
991 // If a device was supplied when the codec was opened, assume that the
992 // user wants to use it.
993
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1617 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1617 if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
994 AVHWDeviceContext *device_ctx =
995 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
996 for (i = 0;; i++) {
997 config = &ffcodec(avctx->codec)->hw_configs[i]->public;
998 if (!config)
999 break;
1000 if (!(config->methods &
1001 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1002 continue;
1003 if (device_ctx->type != config->device_type)
1004 continue;
1005 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1006 if (config->pix_fmt == fmt[n])
1007 return fmt[n];
1008 }
1009 }
1010 }
1011 // No device or other setup, so we have to choose from things which
1012 // don't any other external information.
1013
1014 // If the last element of the list is a software format, choose it
1015 // (this should be best software format if any exist).
1016
2/2
✓ Branch 0 taken 4131 times.
✓ Branch 1 taken 1617 times.
5748 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1017 1617 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1018
1/2
✓ Branch 0 taken 1617 times.
✗ Branch 1 not taken.
1617 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1019 1617 return fmt[n - 1];
1020
1021 // Finally, traverse the list in order and choose the first entry
1022 // with no external dependencies (if there is no hardware configuration
1023 // information available then this just picks the first entry).
1024 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1025 for (i = 0;; i++) {
1026 config = avcodec_get_hw_config(avctx->codec, i);
1027 if (!config)
1028 break;
1029 if (config->pix_fmt == fmt[n])
1030 break;
1031 }
1032 if (!config) {
1033 // No specific config available, so the decoder must be able
1034 // to handle this format without any additional setup.
1035 return fmt[n];
1036 }
1037 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1038 // Usable with only internal setup.
1039 return fmt[n];
1040 }
1041 }
1042
1043 // Nothing is usable, give up.
1044 return AV_PIX_FMT_NONE;
1045 }
1046
1047 int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
1048 enum AVHWDeviceType dev_type)
1049 {
1050 AVHWDeviceContext *device_ctx;
1051 AVHWFramesContext *frames_ctx;
1052 int ret;
1053
1054 if (!avctx->hwaccel)
1055 return AVERROR(ENOSYS);
1056
1057 if (avctx->hw_frames_ctx)
1058 return 0;
1059 if (!avctx->hw_device_ctx) {
1060 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
1061 "required for hardware accelerated decoding.\n");
1062 return AVERROR(EINVAL);
1063 }
1064
1065 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
1066 if (device_ctx->type != dev_type) {
1067 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
1068 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
1069 av_hwdevice_get_type_name(device_ctx->type));
1070 return AVERROR(EINVAL);
1071 }
1072
1073 ret = avcodec_get_hw_frames_parameters(avctx,
1074 avctx->hw_device_ctx,
1075 avctx->hwaccel->pix_fmt,
1076 &avctx->hw_frames_ctx);
1077 if (ret < 0)
1078 return ret;
1079
1080 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1081
1082
1083 if (frames_ctx->initial_pool_size) {
1084 // We guarantee 4 base work surfaces. The function above guarantees 1
1085 // (the absolute minimum), so add the missing count.
1086 frames_ctx->initial_pool_size += 3;
1087 }
1088
1089 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1090 if (ret < 0) {
1091 av_buffer_unref(&avctx->hw_frames_ctx);
1092 return ret;
1093 }
1094
1095 return 0;
1096 }
1097
1098 int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
1099 AVBufferRef *device_ref,
1100 enum AVPixelFormat hw_pix_fmt,
1101 AVBufferRef **out_frames_ref)
1102 {
1103 AVBufferRef *frames_ref = NULL;
1104 const AVCodecHWConfigInternal *hw_config;
1105 const FFHWAccel *hwa;
1106 int i, ret;
1107 bool clean_priv_data = false;
1108
1109 for (i = 0;; i++) {
1110 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1111 if (!hw_config)
1112 return AVERROR(ENOENT);
1113 if (hw_config->public.pix_fmt == hw_pix_fmt)
1114 break;
1115 }
1116
1117 hwa = hw_config->hwaccel;
1118 if (!hwa || !hwa->frame_params)
1119 return AVERROR(ENOENT);
1120
1121 frames_ref = av_hwframe_ctx_alloc(device_ref);
1122 if (!frames_ref)
1123 return AVERROR(ENOMEM);
1124
1125 if (!avctx->internal->hwaccel_priv_data) {
1126 avctx->internal->hwaccel_priv_data =
1127 av_mallocz(hwa->priv_data_size);
1128 if (!avctx->internal->hwaccel_priv_data) {
1129 av_buffer_unref(&frames_ref);
1130 return AVERROR(ENOMEM);
1131 }
1132 clean_priv_data = true;
1133 }
1134
1135 ret = hwa->frame_params(avctx, frames_ref);
1136 if (ret >= 0) {
1137 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1138
1139 if (frames_ctx->initial_pool_size) {
1140 // If the user has requested that extra output surfaces be
1141 // available then add them here.
1142 if (avctx->extra_hw_frames > 0)
1143 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1144
1145 // If frame threading is enabled then an extra surface per thread
1146 // is also required.
1147 if (avctx->active_thread_type & FF_THREAD_FRAME)
1148 frames_ctx->initial_pool_size += avctx->thread_count;
1149 }
1150
1151 *out_frames_ref = frames_ref;
1152 } else {
1153 if (clean_priv_data)
1154 av_freep(&avctx->internal->hwaccel_priv_data);
1155 av_buffer_unref(&frames_ref);
1156 }
1157 return ret;
1158 }
1159
1160 static int hwaccel_init(AVCodecContext *avctx,
1161 const FFHWAccel *hwaccel)
1162 {
1163 int err;
1164
1165 if (hwaccel->p.capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1166 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1167 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1168 hwaccel->p.name);
1169 return AVERROR_PATCHWELCOME;
1170 }
1171
1172 if (!avctx->internal->hwaccel_priv_data && hwaccel->priv_data_size) {
1173 avctx->internal->hwaccel_priv_data =
1174 av_mallocz(hwaccel->priv_data_size);
1175 if (!avctx->internal->hwaccel_priv_data)
1176 return AVERROR(ENOMEM);
1177 }
1178
1179 avctx->hwaccel = &hwaccel->p;
1180 if (hwaccel->init) {
1181 err = hwaccel->init(avctx);
1182 if (err < 0) {
1183 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
1184 "hwaccel initialisation returned error.\n",
1185 av_get_pix_fmt_name(hwaccel->p.pix_fmt));
1186 av_freep(&avctx->internal->hwaccel_priv_data);
1187 avctx->hwaccel = NULL;
1188 return err;
1189 }
1190 }
1191
1192 return 0;
1193 }
1194
1195 40058 void ff_hwaccel_uninit(AVCodecContext *avctx)
1196 {
1197
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40058 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
40058 if (FF_HW_HAS_CB(avctx, uninit))
1198 FF_HW_SIMPLE_CALL(avctx, uninit);
1199
1200 40058 av_freep(&avctx->internal->hwaccel_priv_data);
1201
1202 40058 avctx->hwaccel = NULL;
1203
1204 40058 av_buffer_unref(&avctx->hw_frames_ctx);
1205 40058 }
1206
1207 2656 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1208 {
1209 const AVPixFmtDescriptor *desc;
1210 enum AVPixelFormat *choices;
1211 enum AVPixelFormat ret, user_choice;
1212 const AVCodecHWConfigInternal *hw_config;
1213 const AVCodecHWConfig *config;
1214 int i, n, err;
1215
1216 // Find end of list.
1217
2/2
✓ Branch 0 taken 6922 times.
✓ Branch 1 taken 2656 times.
9578 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1218 // Must contain at least one entry.
1219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2656 times.
2656 av_assert0(n >= 1);
1220 // If a software format is available, it must be the last entry.
1221 2656 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1222
1/2
✓ Branch 0 taken 2656 times.
✗ Branch 1 not taken.
2656 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1223 // No software format is available.
1224 } else {
1225 2656 avctx->sw_pix_fmt = fmt[n - 1];
1226 }
1227
1228 2656 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1229
1/2
✓ Branch 0 taken 2656 times.
✗ Branch 1 not taken.
2656 if (!choices)
1230 return AV_PIX_FMT_NONE;
1231
1232 for (;;) {
1233 // Remove the previous hwaccel, if there was one.
1234 2656 ff_hwaccel_uninit(avctx);
1235
1236 2656 user_choice = avctx->get_format(avctx, choices);
1237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2656 times.
2656 if (user_choice == AV_PIX_FMT_NONE) {
1238 // Explicitly chose nothing, give up.
1239 ret = AV_PIX_FMT_NONE;
1240 break;
1241 }
1242
1243 2656 desc = av_pix_fmt_desc_get(user_choice);
1244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2656 times.
2656 if (!desc) {
1245 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
1246 "get_format() callback.\n");
1247 ret = AV_PIX_FMT_NONE;
1248 break;
1249 }
1250 2656 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
1251 2656 desc->name);
1252
1253
1/2
✓ Branch 0 taken 6921 times.
✗ Branch 1 not taken.
6921 for (i = 0; i < n; i++) {
1254
2/2
✓ Branch 0 taken 2656 times.
✓ Branch 1 taken 4265 times.
6921 if (choices[i] == user_choice)
1255 2656 break;
1256 }
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2656 times.
2656 if (i == n) {
1258 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
1259 "%s not in possible list.\n", desc->name);
1260 ret = AV_PIX_FMT_NONE;
1261 break;
1262 }
1263
1264
2/2
✓ Branch 1 taken 2604 times.
✓ Branch 2 taken 52 times.
2656 if (ffcodec(avctx->codec)->hw_configs) {
1265 2604 for (i = 0;; i++) {
1266 7067 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1267
2/2
✓ Branch 0 taken 2604 times.
✓ Branch 1 taken 4463 times.
7067 if (!hw_config)
1268 2604 break;
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4463 times.
4463 if (hw_config->public.pix_fmt == user_choice)
1270 break;
1271 }
1272 } else {
1273 52 hw_config = NULL;
1274 }
1275
1276
1/2
✓ Branch 0 taken 2656 times.
✗ Branch 1 not taken.
2656 if (!hw_config) {
1277 // No config available, so no extra setup required.
1278 2656 ret = user_choice;
1279 2656 break;
1280 }
1281 config = &hw_config->public;
1282
1283 if (config->methods &
1284 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1285 avctx->hw_frames_ctx) {
1286 const AVHWFramesContext *frames_ctx =
1287 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1288 if (frames_ctx->format != user_choice) {
1289 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1290 "does not match the format of the provided frames "
1291 "context.\n", desc->name);
1292 goto try_again;
1293 }
1294 } else if (config->methods &
1295 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1296 avctx->hw_device_ctx) {
1297 const AVHWDeviceContext *device_ctx =
1298 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1299 if (device_ctx->type != config->device_type) {
1300 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1301 "does not match the type of the provided device "
1302 "context.\n", desc->name);
1303 goto try_again;
1304 }
1305 } else if (config->methods &
1306 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1307 // Internal-only setup, no additional configuration.
1308 } else if (config->methods &
1309 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1310 // Some ad-hoc configuration we can't see and can't check.
1311 } else {
1312 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1313 "missing configuration.\n", desc->name);
1314 goto try_again;
1315 }
1316 if (hw_config->hwaccel) {
1317 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel %s "
1318 "initialisation.\n", desc->name, hw_config->hwaccel->p.name);
1319 err = hwaccel_init(avctx, hw_config->hwaccel);
1320 if (err < 0)
1321 goto try_again;
1322 }
1323 ret = user_choice;
1324 break;
1325
1326 try_again:
1327 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
1328 "get_format() without it.\n", desc->name);
1329 for (i = 0; i < n; i++) {
1330 if (choices[i] == user_choice)
1331 break;
1332 }
1333 for (; i + 1 < n; i++)
1334 choices[i] = choices[i + 1];
1335 --n;
1336 }
1337
1338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2656 times.
2656 if (ret < 0)
1339 ff_hwaccel_uninit(avctx);
1340
1341 2656 av_freep(&choices);
1342 2656 return ret;
1343 }
1344
1345 static const AVPacketSideData*
1346 11286644 packet_side_data_get(const AVPacketSideData *sd, int nb_sd,
1347 enum AVPacketSideDataType type)
1348 {
1349
2/2
✓ Branch 0 taken 163714 times.
✓ Branch 1 taken 11284119 times.
11447833 for (int i = 0; i < nb_sd; i++)
1350
2/2
✓ Branch 0 taken 2525 times.
✓ Branch 1 taken 161189 times.
163714 if (sd[i].type == type)
1351 2525 return &sd[i];
1352
1353 11284119 return NULL;
1354 }
1355
1356 544 const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
1357 enum AVPacketSideDataType type)
1358 {
1359 544 return packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data, type);
1360 }
1361
1362 22 static int side_data_stereo3d_merge(AVFrameSideData *sd_frame,
1363 const AVPacketSideData *sd_pkt)
1364 {
1365 const AVStereo3D *src;
1366 AVStereo3D *dst;
1367 int ret;
1368
1369 22 ret = av_buffer_make_writable(&sd_frame->buf);
1370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
1371 return ret;
1372 22 sd_frame->data = sd_frame->buf->data;
1373
1374 22 dst = ( AVStereo3D*)sd_frame->data;
1375 22 src = (const AVStereo3D*)sd_pkt->data;
1376
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (dst->type == AV_STEREO3D_UNSPEC)
1378 dst->type = src->type;
1379
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (dst->view == AV_STEREO3D_VIEW_UNSPEC)
1381 dst->view = src->view;
1382
1383
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (dst->primary_eye == AV_PRIMARY_EYE_NONE)
1384 22 dst->primary_eye = src->primary_eye;
1385
1386
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!dst->baseline)
1387 22 dst->baseline = src->baseline;
1388
1389
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!dst->horizontal_disparity_adjustment.num)
1390 22 dst->horizontal_disparity_adjustment = src->horizontal_disparity_adjustment;
1391
1392
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!dst->horizontal_field_of_view.num)
1393 22 dst->horizontal_field_of_view = src->horizontal_field_of_view;
1394
1395 22 return 0;
1396 }
1397
1398 1 static int side_data_exif_parse(AVFrame *dst, const AVPacketSideData *sd_pkt)
1399 {
1400 1 AVExifMetadata ifd = { 0 };
1401 1 AVExifEntry *entry = NULL;
1402 1 AVBufferRef *buf = NULL;
1403 AVFrameSideData *sd_frame;
1404 int ret;
1405
1406 1 ret = av_exif_parse_buffer(NULL, sd_pkt->data, sd_pkt->size, &ifd,
1407 AV_EXIF_TIFF_HEADER);
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1409 return ret;
1410
1411 1 ret = av_exif_get_entry(NULL, &ifd, av_exif_get_tag_id("Orientation"), 0, &entry);
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1413 goto end;
1414
1415
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!entry) {
1416 1 ret = av_exif_ifd_to_dict(NULL, &ifd, &dst->metadata);
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1418 goto end;
1419
1420 1 sd_frame = av_frame_side_data_new(&dst->side_data, &dst->nb_side_data, AV_FRAME_DATA_EXIF,
1421 1 sd_pkt->size, 0);
1422
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sd_frame)
1423 1 memcpy(sd_frame->data, sd_pkt->data, sd_pkt->size);
1424
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ret = sd_frame ? 0 : AVERROR(ENOMEM);
1425
1426 1 goto end;
1427 } else if (entry->count <= 0 || entry->type != AV_TIFF_SHORT) {
1428 ret = AVERROR_INVALIDDATA;
1429 goto end;
1430 }
1431
1432 // If a display matrix already exists in the frame, give it priority
1433 if (av_frame_side_data_get(dst->side_data, dst->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX))
1434 goto finish;
1435
1436 sd_frame = av_frame_side_data_new(&dst->side_data, &dst->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX,
1437 sizeof(int32_t) * 9, 0);
1438 if (!sd_frame) {
1439 ret = AVERROR(ENOMEM);
1440 goto end;
1441 }
1442
1443 ret = av_exif_orientation_to_matrix((int32_t *)sd_frame->data, entry->value.uint[0]);
1444 if (ret < 0)
1445 goto end;
1446
1447 finish:
1448 av_exif_remove_entry(NULL, &ifd, entry->id, 0);
1449
1450 ret = av_exif_ifd_to_dict(NULL, &ifd, &dst->metadata);
1451 if (ret < 0)
1452 goto end;
1453
1454 ret = av_exif_write(NULL, &ifd, &buf, AV_EXIF_TIFF_HEADER);
1455 if (ret < 0)
1456 goto end;
1457
1458 if (!av_frame_side_data_add(&dst->side_data, &dst->nb_side_data, AV_FRAME_DATA_EXIF, &buf, 0)) {
1459 ret = AVERROR(ENOMEM);
1460 goto end;
1461 }
1462
1463 ret = 0;
1464 1 end:
1465 1 av_buffer_unref(&buf);
1466 1 av_exif_free(&ifd);
1467 1 return ret;
1468 }
1469
1470 1209225 static int side_data_map(AVFrame *dst,
1471 const AVPacketSideData *sd_src, int nb_sd_src,
1472 const SideDataMap *map)
1473
1474 {
1475
2/2
✓ Branch 0 taken 11286100 times.
✓ Branch 1 taken 1209225 times.
12495325 for (int i = 0; map[i].packet < AV_PKT_DATA_NB; i++) {
1476 11286100 const enum AVPacketSideDataType type_pkt = map[i].packet;
1477 11286100 const enum AVFrameSideDataType type_frame = map[i].frame;
1478 const AVPacketSideData *sd_pkt;
1479 AVFrameSideData *sd_frame;
1480
1481 11286100 sd_pkt = packet_side_data_get(sd_src, nb_sd_src, type_pkt);
1482
2/2
✓ Branch 0 taken 11283586 times.
✓ Branch 1 taken 2514 times.
11286100 if (!sd_pkt)
1483 11283586 continue;
1484
1485 2514 sd_frame = av_frame_get_side_data(dst, type_frame);
1486
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2492 times.
2514 if (sd_frame) {
1487
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (type_frame == AV_FRAME_DATA_STEREO3D) {
1488 22 int ret = side_data_stereo3d_merge(sd_frame, sd_pkt);
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
1490 return ret;
1491 }
1492
1493 22 continue;
1494 }
1495
1496
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2491 times.
2492 switch (type_pkt) {
1497 1 case AV_PKT_DATA_EXIF: {
1498 1 int ret = side_data_exif_parse(dst, sd_pkt);
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1500 return ret;
1501 1 break;
1502 }
1503 2491 default:
1504 2491 sd_frame = av_frame_new_side_data(dst, type_frame, sd_pkt->size);
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2491 times.
2491 if (!sd_frame)
1506 return AVERROR(ENOMEM);
1507
1508 2491 memcpy(sd_frame->data, sd_pkt->data, sd_pkt->size);
1509 2491 break;
1510 }
1511 }
1512
1513 1209225 return 0;
1514 }
1515
1516 403075 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1517 {
1518 size_t size;
1519 const uint8_t *side_metadata;
1520
1521 403075 AVDictionary **frame_md = &frame->metadata;
1522
1523 403075 side_metadata = av_packet_get_side_data(avpkt,
1524 AV_PKT_DATA_STRINGS_METADATA, &size);
1525 403075 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1526 }
1527
1528 403075 int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx,
1529 AVFrame *frame, const AVPacket *pkt)
1530 {
1531 static const SideDataMap sd[] = {
1532 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1533 { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD },
1534 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1535 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1536 { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES },
1537 { AV_PKT_DATA_LCEVC, AV_FRAME_DATA_LCEVC },
1538 { AV_PKT_DATA_NB }
1539 };
1540
1541 403075 int ret = 0;
1542
1543 403075 frame->pts = pkt->pts;
1544 403075 frame->duration = pkt->duration;
1545
1546 403075 ret = side_data_map(frame, pkt->side_data, pkt->side_data_elems, ff_sd_global_map);
1547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403075 times.
403075 if (ret < 0)
1548 return ret;
1549
1550 403075 ret = side_data_map(frame, pkt->side_data, pkt->side_data_elems, sd);
1551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403075 times.
403075 if (ret < 0)
1552 return ret;
1553
1554 403075 add_metadata_from_side_data(pkt, frame);
1555
1556
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 402866 times.
403075 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1557 209 frame->flags |= AV_FRAME_FLAG_DISCARD;
1558 }
1559
1560
2/2
✓ Branch 0 taken 391312 times.
✓ Branch 1 taken 11763 times.
403075 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
1561 391312 int ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref);
1562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391312 times.
391312 if (ret < 0)
1563 return ret;
1564 391312 frame->opaque = pkt->opaque;
1565 }
1566
1567 403075 return 0;
1568 }
1569
1570 403075 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1571 {
1572 int ret;
1573
1574 403075 ret = side_data_map(frame, avctx->coded_side_data, avctx->nb_coded_side_data,
1575 ff_sd_global_map);
1576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403075 times.
403075 if (ret < 0)
1577 return ret;
1578
1579
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 403075 times.
403169 for (int i = 0; i < avctx->nb_decoded_side_data; i++) {
1580 94 const AVFrameSideData *src = avctx->decoded_side_data[i];
1581
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 74 times.
94 if (av_frame_get_side_data(frame, src->type))
1582 20 continue;
1583 74 ret = av_frame_side_data_clone(&frame->side_data, &frame->nb_side_data, src, 0);
1584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (ret < 0)
1585 return ret;
1586 }
1587
1588
1/2
✓ Branch 1 taken 403075 times.
✗ Branch 2 not taken.
403075 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
1589 403075 const AVPacket *pkt = avctx->internal->last_pkt_props;
1590
1591 403075 ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt);
1592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403075 times.
403075 if (ret < 0)
1593 return ret;
1594 }
1595
1596 403075 ret = fill_frame_props(avctx, frame);
1597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403075 times.
403075 if (ret < 0)
1598 return ret;
1599
1600
2/2
✓ Branch 0 taken 135779 times.
✓ Branch 1 taken 267296 times.
403075 switch (avctx->codec->type) {
1601 135779 case AVMEDIA_TYPE_VIDEO:
1602
4/6
✓ Branch 0 taken 110828 times.
✓ Branch 1 taken 24951 times.
✓ Branch 2 taken 110828 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 110828 times.
246607 if (frame->width && frame->height &&
1603 110828 av_image_check_sar(frame->width, frame->height,
1604 frame->sample_aspect_ratio) < 0) {
1605 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1606 frame->sample_aspect_ratio.num,
1607 frame->sample_aspect_ratio.den);
1608 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1609 }
1610 135779 break;
1611 }
1612 403075 return 0;
1613 }
1614
1615 374724 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1616 {
1617
2/2
✓ Branch 0 taken 107428 times.
✓ Branch 1 taken 267296 times.
374724 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1618 int i;
1619 107428 int num_planes = av_pix_fmt_count_planes(frame->format);
1620 107428 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1621
1/2
✓ Branch 0 taken 107428 times.
✗ Branch 1 not taken.
107428 int flags = desc ? desc->flags : 0;
1622
4/4
✓ Branch 0 taken 13494 times.
✓ Branch 1 taken 93934 times.
✓ Branch 2 taken 4124 times.
✓ Branch 3 taken 9370 times.
107428 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1623 4124 num_planes = 2;
1624
2/2
✓ Branch 0 taken 300208 times.
✓ Branch 1 taken 107428 times.
407636 for (i = 0; i < num_planes; i++) {
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 300208 times.
300208 av_assert0(frame->data[i]);
1626 }
1627 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1628
3/4
✓ Branch 0 taken 666644 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 559216 times.
✓ Branch 3 taken 107428 times.
666644 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 559216 times.
559216 if (frame->data[i])
1630 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1631 559216 frame->data[i] = NULL;
1632 }
1633 }
1634 374724 }
1635
1636 374724 static void decode_data_free(AVRefStructOpaque unused, void *obj)
1637 {
1638 374724 FrameDecodeData *fdd = obj;
1639
1640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (fdd->post_process_opaque_free)
1641 fdd->post_process_opaque_free(fdd->post_process_opaque);
1642
1643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (fdd->hwaccel_priv_free)
1644 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1645 374724 }
1646
1647 374724 int ff_attach_decode_data(AVFrame *frame)
1648 {
1649 FrameDecodeData *fdd;
1650
1651 av_assert1(!frame->private_ref);
1652 374724 av_refstruct_unref(&frame->private_ref);
1653
1654 374724 fdd = av_refstruct_alloc_ext(sizeof(*fdd), 0, NULL, decode_data_free);
1655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (!fdd)
1656 return AVERROR(ENOMEM);
1657
1658 374724 frame->private_ref = fdd;
1659
1660 374724 return 0;
1661 }
1662
1663 374724 static void update_frame_props(AVCodecContext *avctx, AVFrame *frame)
1664 {
1665 #if CONFIG_LIBLCEVC_DEC
1666 AVCodecInternal *avci = avctx->internal;
1667 DecodeContext *dc = decode_ctx(avci);
1668
1669 dc->lcevc.frame = dc->lcevc.ctx && avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1670 av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC);
1671
1672 if (dc->lcevc.frame) {
1673 dc->lcevc.width = frame->width;
1674 dc->lcevc.height = frame->height;
1675 frame->width = frame->width * 2 / FFMAX(frame->sample_aspect_ratio.den, 1);
1676 frame->height = frame->height * 2 / FFMAX(frame->sample_aspect_ratio.num, 1);
1677 }
1678 #endif
1679 374724 }
1680
1681 374724 static int attach_post_process_data(AVCodecContext *avctx, AVFrame *frame)
1682 {
1683 #if CONFIG_LIBLCEVC_DEC
1684 AVCodecInternal *avci = avctx->internal;
1685 DecodeContext *dc = decode_ctx(avci);
1686
1687 if (dc->lcevc.frame) {
1688 FrameDecodeData *fdd = frame->private_ref;
1689 FFLCEVCFrame *frame_ctx;
1690 int ret;
1691
1692 frame_ctx = av_mallocz(sizeof(*frame_ctx));
1693 if (!frame_ctx)
1694 return AVERROR(ENOMEM);
1695
1696 frame_ctx->frame = av_frame_alloc();
1697 if (!frame_ctx->frame) {
1698 av_free(frame_ctx);
1699 return AVERROR(ENOMEM);
1700 }
1701
1702 frame_ctx->lcevc = av_refstruct_ref(dc->lcevc.ctx);
1703 frame_ctx->frame->width = frame->width;
1704 frame_ctx->frame->height = frame->height;
1705 frame_ctx->frame->format = frame->format;
1706
1707 frame->width = dc->lcevc.width;
1708 frame->height = dc->lcevc.height;
1709
1710 ret = avctx->get_buffer2(avctx, frame_ctx->frame, 0);
1711 if (ret < 0) {
1712 ff_lcevc_unref(frame_ctx);
1713 return ret;
1714 }
1715
1716 validate_avframe_allocation(avctx, frame_ctx->frame);
1717
1718 fdd->post_process_opaque = frame_ctx;
1719 fdd->post_process_opaque_free = ff_lcevc_unref;
1720 fdd->post_process = ff_lcevc_process;
1721 }
1722 dc->lcevc.frame = 0;
1723 #endif
1724
1725 374724 return 0;
1726 }
1727
1728 374724 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1729 {
1730 374724 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1731 374724 int override_dimensions = 1;
1732 int ret;
1733
1734
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 374724 times.
374724 av_assert0(ff_codec_is_decoder(avctx->codec));
1735
1736
2/2
✓ Branch 0 taken 107428 times.
✓ Branch 1 taken 267296 times.
374724 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1737
2/4
✓ Branch 0 taken 107428 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 107428 times.
✗ Branch 3 not taken.
214856 if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
1738
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107428 times.
214856 (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) {
1739 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1740 ret = AVERROR(EINVAL);
1741 goto fail;
1742 }
1743
1744
3/4
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 106697 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 731 times.
107428 if (frame->width <= 0 || frame->height <= 0) {
1745 106697 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1746 106697 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1747 106697 override_dimensions = 0;
1748 }
1749
1750
4/8
✓ Branch 0 taken 107428 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 107428 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 107428 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 107428 times.
107428 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1751 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1752 ret = AVERROR(EINVAL);
1753 goto fail;
1754 }
1755
1/2
✓ Branch 0 taken 267296 times.
✗ Branch 1 not taken.
267296 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 267296 times.
267296 if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
1757 av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
1758 ret = AVERROR(EINVAL);
1759 goto fail;
1760 }
1761 }
1762 374724 ret = ff_decode_frame_props(avctx, frame);
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (ret < 0)
1764 goto fail;
1765
1766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (hwaccel) {
1767 if (hwaccel->alloc_frame) {
1768 ret = hwaccel->alloc_frame(avctx, frame);
1769 goto end;
1770 }
1771 } else {
1772 374724 avctx->sw_pix_fmt = avctx->pix_fmt;
1773 374724 update_frame_props(avctx, frame);
1774 }
1775
1776 374724 ret = avctx->get_buffer2(avctx, frame, flags);
1777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (ret < 0)
1778 goto fail;
1779
1780 374724 validate_avframe_allocation(avctx, frame);
1781
1782 374724 ret = ff_attach_decode_data(frame);
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (ret < 0)
1784 goto fail;
1785
1786 374724 ret = attach_post_process_data(avctx, frame);
1787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (ret < 0)
1788 goto fail;
1789
1790 374724 end:
1791
4/4
✓ Branch 0 taken 267296 times.
✓ Branch 1 taken 107428 times.
✓ Branch 2 taken 731 times.
✓ Branch 3 taken 106697 times.
374724 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1792
2/2
✓ Branch 1 taken 37285 times.
✓ Branch 2 taken 69412 times.
106697 !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1793 69412 frame->width = avctx->width;
1794 69412 frame->height = avctx->height;
1795 }
1796
1797 305312 fail:
1798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374724 times.
374724 if (ret < 0) {
1799 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1800 av_frame_unref(frame);
1801 }
1802
1803 374724 return ret;
1804 }
1805
1806 7841 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1807 {
1808 AVFrame *tmp;
1809 int ret;
1810
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7841 times.
7841 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1812
1813 // make sure the discard flag does not persist
1814 7841 frame->flags &= ~AV_FRAME_FLAG_DISCARD;
1815
1816
5/8
✓ Branch 0 taken 7691 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 7691 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7691 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7691 times.
7841 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1817 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1818 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1819 av_frame_unref(frame);
1820 }
1821
1822
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 7691 times.
7841 if (!frame->data[0])
1823 150 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1824
1825 7691 av_frame_side_data_free(&frame->side_data, &frame->nb_side_data);
1826
1827
4/4
✓ Branch 0 taken 7494 times.
✓ Branch 1 taken 197 times.
✓ Branch 3 taken 680 times.
✓ Branch 4 taken 6814 times.
7691 if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
1828 877 return ff_decode_frame_props(avctx, frame);
1829
1830 6814 tmp = av_frame_alloc();
1831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6814 times.
6814 if (!tmp)
1832 return AVERROR(ENOMEM);
1833
1834 6814 av_frame_move_ref(tmp, frame);
1835
1836 6814 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6814 times.
6814 if (ret < 0) {
1838 av_frame_free(&tmp);
1839 return ret;
1840 }
1841
1842 6814 av_frame_copy(frame, tmp);
1843 6814 av_frame_free(&tmp);
1844
1845 6814 return 0;
1846 }
1847
1848 7841 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1849 {
1850 7841 int ret = reget_buffer_internal(avctx, frame, flags);
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7841 times.
7841 if (ret < 0)
1852 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1853 7841 return ret;
1854 }
1855
1856 typedef struct ProgressInternal {
1857 ThreadProgress progress;
1858 struct AVFrame *f;
1859 } ProgressInternal;
1860
1861 453172 static void check_progress_consistency(const ProgressFrame *f)
1862 {
1863 av_assert1(!!f->f == !!f->progress);
1864 av_assert1(!f->progress || f->progress->f == f->f);
1865 453172 }
1866
1867 16916 int ff_progress_frame_alloc(AVCodecContext *avctx, ProgressFrame *f)
1868 {
1869 16916 AVRefStructPool *pool = avctx->internal->progress_frame_pool;
1870
1871 av_assert1(!f->f && !f->progress);
1872
1873 16916 f->progress = av_refstruct_pool_get(pool);
1874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16916 times.
16916 if (!f->progress)
1875 return AVERROR(ENOMEM);
1876
1877 16916 f->f = f->progress->f;
1878 16916 return 0;
1879 }
1880
1881 6427 int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
1882 {
1883 6427 int ret = ff_progress_frame_alloc(avctx, f);
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6427 times.
6427 if (ret < 0)
1885 return ret;
1886
1887 6427 ret = ff_thread_get_buffer(avctx, f->progress->f, flags);
1888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6427 times.
6427 if (ret < 0) {
1889 f->f = NULL;
1890 av_refstruct_unref(&f->progress);
1891 return ret;
1892 }
1893 6427 return 0;
1894 }
1895
1896 38994 void ff_progress_frame_ref(ProgressFrame *dst, const ProgressFrame *src)
1897 {
1898 av_assert1(src->progress && src->f && src->f == src->progress->f);
1899 av_assert1(!dst->f && !dst->progress);
1900 38994 dst->f = src->f;
1901 38994 dst->progress = av_refstruct_ref(src->progress);
1902 38994 }
1903
1904 412964 void ff_progress_frame_unref(ProgressFrame *f)
1905 {
1906 412964 check_progress_consistency(f);
1907 412964 f->f = NULL;
1908 412964 av_refstruct_unref(&f->progress);
1909 412964 }
1910
1911 40208 void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
1912 {
1913
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40208 times.
40208 if (dst == src)
1914 return;
1915 40208 ff_progress_frame_unref(dst);
1916 40208 check_progress_consistency(src);
1917
2/2
✓ Branch 0 taken 38927 times.
✓ Branch 1 taken 1281 times.
40208 if (src->f)
1918 38927 ff_progress_frame_ref(dst, src);
1919 }
1920
1921 19586 void ff_progress_frame_report(ProgressFrame *f, int n)
1922 {
1923 19586 ff_thread_progress_report(&f->progress->progress, n);
1924 19586 }
1925
1926 2597786 void ff_progress_frame_await(const ProgressFrame *f, int n)
1927 {
1928 2597786 ff_thread_progress_await(&f->progress->progress, n);
1929 2597786 }
1930
1931 #if !HAVE_THREADS
1932 enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1933 {
1934 return FF_THREAD_NO_FRAME_THREADING;
1935 }
1936 #endif /* !HAVE_THREADS */
1937
1938 2575 static av_cold int progress_frame_pool_init_cb(AVRefStructOpaque opaque, void *obj)
1939 {
1940 2575 const AVCodecContext *avctx = opaque.nc;
1941 2575 ProgressInternal *progress = obj;
1942 int ret;
1943
1944 2575 ret = ff_thread_progress_init(&progress->progress, avctx->active_thread_type & FF_THREAD_FRAME);
1945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2575 times.
2575 if (ret < 0)
1946 return ret;
1947
1948 2575 progress->f = av_frame_alloc();
1949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2575 times.
2575 if (!progress->f)
1950 return AVERROR(ENOMEM);
1951
1952 2575 return 0;
1953 }
1954
1955 16916 static void progress_frame_pool_reset_cb(AVRefStructOpaque unused, void *obj)
1956 {
1957 16916 ProgressInternal *progress = obj;
1958
1959 16916 ff_thread_progress_reset(&progress->progress);
1960 16916 av_frame_unref(progress->f);
1961 16916 }
1962
1963 2575 static av_cold void progress_frame_pool_free_entry_cb(AVRefStructOpaque opaque, void *obj)
1964 {
1965 2575 ProgressInternal *progress = obj;
1966
1967 2575 ff_thread_progress_destroy(&progress->progress);
1968 2575 av_frame_free(&progress->f);
1969 2575 }
1970
1971 15827 av_cold int ff_decode_preinit(AVCodecContext *avctx)
1972 {
1973 15827 AVCodecInternal *avci = avctx->internal;
1974 15827 DecodeContext *dc = decode_ctx(avci);
1975 15827 int ret = 0;
1976
1977 15827 dc->initial_pict_type = AV_PICTURE_TYPE_NONE;
1978
2/2
✓ Branch 0 taken 12055 times.
✓ Branch 1 taken 3772 times.
15827 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY) {
1979 12055 dc->intra_only_flag = AV_FRAME_FLAG_KEY;
1980
2/2
✓ Branch 0 taken 8996 times.
✓ Branch 1 taken 3059 times.
12055 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
1981 8996 dc->initial_pict_type = AV_PICTURE_TYPE_I;
1982 }
1983
1984 /* if the decoder init function was already called previously,
1985 * free the already allocated subtitle_header before overwriting it */
1986 15827 av_freep(&avctx->subtitle_header);
1987
1988
2/4
✓ Branch 0 taken 15827 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15827 times.
15827 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1989 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
1990 avctx->codec->max_lowres);
1991 avctx->lowres = avctx->codec->max_lowres;
1992 }
1993
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 15821 times.
15827 if (avctx->sub_charenc) {
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1995 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1996 "supported with subtitles codecs\n");
1997 return AVERROR(EINVAL);
1998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1999 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
2000 "subtitles character encoding will be ignored\n",
2001 avctx->codec_descriptor->name);
2002 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
2003 } else {
2004 /* input character encoding is set for a text based subtitle
2005 * codec at this point */
2006
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
2007 6 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
2008
2009
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
2010 #if CONFIG_ICONV
2011 6 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
2012
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cd == (iconv_t)-1) {
2013 ret = AVERROR(errno);
2014 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
2015 "with input character encoding \"%s\"\n", avctx->sub_charenc);
2016 return ret;
2017 }
2018 6 iconv_close(cd);
2019 #else
2020 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
2021 "conversion needs a libavcodec built with iconv support "
2022 "for this codec\n");
2023 return AVERROR(ENOSYS);
2024 #endif
2025 }
2026 }
2027 }
2028
2029 15827 dc->pts_correction_num_faulty_pts =
2030 15827 dc->pts_correction_num_faulty_dts = 0;
2031 15827 dc->pts_correction_last_pts =
2032 15827 dc->pts_correction_last_dts = INT64_MIN;
2033
2034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15827 times.
15827 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
2035 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
2036 av_log(avctx, AV_LOG_WARNING,
2037 "gray decoding requested but not enabled at configuration time\n");
2038
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15822 times.
15827 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
2039 5 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
2040 }
2041
2042
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15826 times.
15827 if (avctx->nb_side_data_prefer_packet == 1 &&
2043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 avctx->side_data_prefer_packet[0] == -1)
2044 dc->side_data_pref_mask = ~0ULL;
2045 else {
2046
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15827 times.
15832 for (unsigned i = 0; i < avctx->nb_side_data_prefer_packet; i++) {
2047 5 int val = avctx->side_data_prefer_packet[i];
2048
2049
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) {
2050 av_log(avctx, AV_LOG_ERROR, "Invalid side data type: %d\n", val);
2051 return AVERROR(EINVAL);
2052 }
2053
2054
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++) {
2055
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 49 times.
55 if (ff_sd_global_map[j].packet == val) {
2056 6 val = ff_sd_global_map[j].frame;
2057
2058 // this code will need to be changed when we have more than
2059 // 64 frame side data types
2060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (val >= 64) {
2061 av_log(avctx, AV_LOG_ERROR, "Side data type too big\n");
2062 return AVERROR_BUG;
2063 }
2064
2065 6 dc->side_data_pref_mask |= 1ULL << val;
2066 }
2067 }
2068 }
2069 }
2070
2071 15827 avci->in_pkt = av_packet_alloc();
2072 15827 avci->last_pkt_props = av_packet_alloc();
2073
2/4
✓ Branch 0 taken 15827 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15827 times.
15827 if (!avci->in_pkt || !avci->last_pkt_props)
2074 return AVERROR(ENOMEM);
2075
2076
2/2
✓ Branch 1 taken 1391 times.
✓ Branch 2 taken 14436 times.
15827 if (ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_USES_PROGRESSFRAMES) {
2077 1391 avci->progress_frame_pool =
2078 1391 av_refstruct_pool_alloc_ext(sizeof(ProgressInternal),
2079 AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
2080 avctx, progress_frame_pool_init_cb,
2081 progress_frame_pool_reset_cb,
2082 progress_frame_pool_free_entry_cb, NULL);
2083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1391 times.
1391 if (!avci->progress_frame_pool)
2084 return AVERROR(ENOMEM);
2085 }
2086 15827 ret = decode_bsfs_init(avctx);
2087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15827 times.
15827 if (ret < 0)
2088 return ret;
2089
2090
1/2
✓ Branch 0 taken 15827 times.
✗ Branch 1 not taken.
15827 if (!(avctx->export_side_data & AV_CODEC_EXPORT_DATA_ENHANCEMENTS)) {
2091 15827 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2092 #if CONFIG_LIBLCEVC_DEC
2093 ret = ff_lcevc_alloc(&dc->lcevc.ctx);
2094 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2095 return ret;
2096 #endif
2097 }
2098 }
2099
2100 15827 return 0;
2101 }
2102
2103 /**
2104 * Check side data preference and clear existing side data from frame
2105 * if needed.
2106 *
2107 * @retval 0 side data of this type can be added to frame
2108 * @retval 1 side data of this type should not be added to frame
2109 */
2110 6260 static int side_data_pref(const AVCodecContext *avctx, AVFrameSideData ***sd,
2111 int *nb_sd, enum AVFrameSideDataType type)
2112 {
2113 6260 DecodeContext *dc = decode_ctx(avctx->internal);
2114
2115 // Note: could be skipped for `type` without corresponding packet sd
2116
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 6237 times.
6260 if (av_frame_side_data_get(*sd, *nb_sd, type)) {
2117
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 12 times.
23 if (dc->side_data_pref_mask & (1ULL << type))
2118 11 return 1;
2119 12 av_frame_side_data_remove(sd, nb_sd, type);
2120 }
2121
2122 6249 return 0;
2123 }
2124
2125
2126 5212 int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame,
2127 enum AVFrameSideDataType type, size_t size,
2128 AVFrameSideData **psd)
2129 {
2130 AVFrameSideData *sd;
2131
2132
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5211 times.
5212 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data, type)) {
2133
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (psd)
2134 1 *psd = NULL;
2135 1 return 0;
2136 }
2137
2138 5211 sd = av_frame_new_side_data(frame, type, size);
2139
1/2
✓ Branch 0 taken 5211 times.
✗ Branch 1 not taken.
5211 if (psd)
2140 5211 *psd = sd;
2141
2142
1/2
✓ Branch 0 taken 5211 times.
✗ Branch 1 not taken.
5211 return sd ? 0 : AVERROR(ENOMEM);
2143 }
2144
2145 1001 int ff_frame_new_side_data_from_buf_ext(const AVCodecContext *avctx,
2146 AVFrameSideData ***sd, int *nb_sd,
2147 enum AVFrameSideDataType type,
2148 AVBufferRef **buf)
2149 {
2150 1001 int ret = 0;
2151
2152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1001 times.
1001 if (side_data_pref(avctx, sd, nb_sd, type))
2153 goto finish;
2154
2155
1/2
✓ Branch 1 taken 1001 times.
✗ Branch 2 not taken.
1001 if (!av_frame_side_data_add(sd, nb_sd, type, buf, 0))
2156 ret = AVERROR(ENOMEM);
2157
2158 1001 finish:
2159 1001 av_buffer_unref(buf);
2160
2161 1001 return ret;
2162 }
2163
2164 987 int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx,
2165 AVFrame *frame, enum AVFrameSideDataType type,
2166 AVBufferRef **buf)
2167 {
2168 987 return ff_frame_new_side_data_from_buf_ext(avctx,
2169 &frame->side_data, &frame->nb_side_data,
2170 type, buf);
2171 }
2172
2173 35 int ff_decode_mastering_display_new_ext(const AVCodecContext *avctx,
2174 AVFrameSideData ***sd, int *nb_sd,
2175 struct AVMasteringDisplayMetadata **mdm)
2176 {
2177 AVBufferRef *buf;
2178 size_t size;
2179
2180
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)) {
2181 10 *mdm = NULL;
2182 10 return 0;
2183 }
2184
2185 25 *mdm = av_mastering_display_metadata_alloc_size(&size);
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!*mdm)
2187 return AVERROR(ENOMEM);
2188
2189 25 buf = av_buffer_create((uint8_t *)*mdm, size, NULL, NULL, 0);
2190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!buf) {
2191 av_freep(mdm);
2192 return AVERROR(ENOMEM);
2193 }
2194
2195
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,
2196 &buf, 0)) {
2197 *mdm = NULL;
2198 av_buffer_unref(&buf);
2199 return AVERROR(ENOMEM);
2200 }
2201
2202 25 return 0;
2203 }
2204
2205 2 int ff_decode_mastering_display_new(const AVCodecContext *avctx, AVFrame *frame,
2206 AVMasteringDisplayMetadata **mdm)
2207 {
2208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2209 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
2210 *mdm = NULL;
2211 return 0;
2212 }
2213
2214 2 *mdm = av_mastering_display_metadata_create_side_data(frame);
2215
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return *mdm ? 0 : AVERROR(ENOMEM);
2216 }
2217
2218 8 int ff_decode_content_light_new_ext(const AVCodecContext *avctx,
2219 AVFrameSideData ***sd, int *nb_sd,
2220 AVContentLightMetadata **clm)
2221 {
2222 AVBufferRef *buf;
2223 size_t size;
2224
2225
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)) {
2226 *clm = NULL;
2227 return 0;
2228 }
2229
2230 8 *clm = av_content_light_metadata_alloc(&size);
2231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!*clm)
2232 return AVERROR(ENOMEM);
2233
2234 8 buf = av_buffer_create((uint8_t *)*clm, size, NULL, NULL, 0);
2235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!buf) {
2236 av_freep(clm);
2237 return AVERROR(ENOMEM);
2238 }
2239
2240
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,
2241 &buf, 0)) {
2242 *clm = NULL;
2243 av_buffer_unref(&buf);
2244 return AVERROR(ENOMEM);
2245 }
2246
2247 8 return 0;
2248 }
2249
2250 2 int ff_decode_content_light_new(const AVCodecContext *avctx, AVFrame *frame,
2251 AVContentLightMetadata **clm)
2252 {
2253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2254 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2255 *clm = NULL;
2256 return 0;
2257 }
2258
2259 2 *clm = av_content_light_metadata_create_side_data(frame);
2260
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return *clm ? 0 : AVERROR(ENOMEM);
2261 }
2262
2263 1667 int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
2264 {
2265 size_t size;
2266 1667 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
2267
2268
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1635 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
1667 if (pal && size == AVPALETTE_SIZE) {
2269 32 memcpy(dst, pal, AVPALETTE_SIZE);
2270 32 return 1;
2271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1635 times.
1635 } else if (pal) {
2272 av_log(logctx, AV_LOG_ERROR,
2273 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
2274 }
2275 1635 return 0;
2276 }
2277
2278 55236 int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
2279 {
2280 55236 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
2281
2282
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 55236 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
55236 if (!hwaccel || !hwaccel->frame_priv_data_size)
2283 55236 return 0;
2284
2285 av_assert0(!*hwaccel_picture_private);
2286
2287 if (hwaccel->free_frame_priv) {
2288 AVHWFramesContext *frames_ctx;
2289
2290 if (!avctx->hw_frames_ctx)
2291 return AVERROR(EINVAL);
2292
2293 frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
2294 *hwaccel_picture_private = av_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
2295 frames_ctx->device_ctx,
2296 hwaccel->free_frame_priv);
2297 } else {
2298 *hwaccel_picture_private = av_refstruct_allocz(hwaccel->frame_priv_data_size);
2299 }
2300
2301 if (!*hwaccel_picture_private)
2302 return AVERROR(ENOMEM);
2303
2304 return 0;
2305 }
2306
2307 111 av_cold void ff_decode_flush_buffers(AVCodecContext *avctx)
2308 {
2309 111 AVCodecInternal *avci = avctx->internal;
2310 111 DecodeContext *dc = decode_ctx(avci);
2311
2312 111 av_packet_unref(avci->last_pkt_props);
2313 111 av_packet_unref(avci->in_pkt);
2314
2315 111 dc->pts_correction_last_pts =
2316 111 dc->pts_correction_last_dts = INT64_MIN;
2317
2318
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 if (avci->bsf)
2319 111 av_bsf_flush(avci->bsf);
2320
2321 111 dc->nb_draining_errors = 0;
2322 111 dc->draining_started = 0;
2323 111 }
2324
2325 15903 av_cold AVCodecInternal *ff_decode_internal_alloc(void)
2326 {
2327 15903 return av_mallocz(sizeof(DecodeContext));
2328 }
2329
2330 76 av_cold void ff_decode_internal_sync(AVCodecContext *dst, const AVCodecContext *src)
2331 {
2332 76 const DecodeContext *src_dc = decode_ctx(src->internal);
2333 76 DecodeContext *dst_dc = decode_ctx(dst->internal);
2334
2335 76 dst_dc->initial_pict_type = src_dc->initial_pict_type;
2336 76 dst_dc->intra_only_flag = src_dc->intra_only_flag;
2337 76 dst_dc->side_data_pref_mask = src_dc->side_data_pref_mask;
2338 #if CONFIG_LIBLCEVC_DEC
2339 av_refstruct_replace(&dst_dc->lcevc.ctx, src_dc->lcevc.ctx);
2340 #endif
2341 76 }
2342
2343 15903 av_cold void ff_decode_internal_uninit(AVCodecContext *avctx)
2344 {
2345 #if CONFIG_LIBLCEVC_DEC
2346 AVCodecInternal *avci = avctx->internal;
2347 DecodeContext *dc = decode_ctx(avci);
2348
2349 av_refstruct_unref(&dc->lcevc.ctx);
2350 #endif
2351 15903 }
2352
2353 19 static int attach_displaymatrix(AVCodecContext *avctx, AVFrame *frame, int orientation)
2354 {
2355 19 AVFrameSideData *sd = NULL;
2356 int32_t *matrix;
2357 int ret;
2358 /* invalid orientation */
2359
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)
2360 return AVERROR_INVALIDDATA;
2361 19 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9, &sd);
2362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0) {
2363 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame side data: %s\n", av_err2str(ret));
2364 return ret;
2365 }
2366
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 if (sd) {
2367 18 matrix = (int32_t *) sd->data;
2368 18 ret = av_exif_orientation_to_matrix(matrix, orientation);
2369 }
2370
2371 19 return ret;
2372 }
2373
2374 37 static int exif_attach_ifd(AVCodecContext *avctx, AVFrame *frame, const AVExifMetadata *ifd, AVBufferRef **pbuf)
2375 {
2376 37 const AVExifEntry *orient = NULL;
2377 37 AVExifMetadata *cloned = NULL;
2378 int ret;
2379
2380
2/2
✓ Branch 0 taken 322 times.
✓ Branch 1 taken 18 times.
340 for (size_t i = 0; i < ifd->count; i++) {
2381 322 const AVExifEntry *entry = &ifd->entries[i];
2382
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 303 times.
322 if (entry->id == av_exif_get_tag_id("Orientation") &&
2383
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) {
2384 19 orient = entry;
2385 19 break;
2386 }
2387 }
2388
2389
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 18 times.
37 if (orient) {
2390 19 av_log(avctx, AV_LOG_DEBUG, "found EXIF orientation: %" PRIu64 "\n", orient->value.uint[0]);
2391 19 ret = attach_displaymatrix(avctx, frame, orient->value.uint[0]);
2392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0) {
2393 av_log(avctx, AV_LOG_WARNING, "unable to attach displaymatrix from EXIF\n");
2394 } else {
2395 19 cloned = av_exif_clone_ifd(ifd);
2396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!cloned) {
2397 ret = AVERROR(ENOMEM);
2398 goto end;
2399 }
2400 19 av_exif_remove_entry(avctx, cloned, orient->id, 0);
2401 19 ifd = cloned;
2402 }
2403 }
2404
2405 37 ret = av_exif_ifd_to_dict(avctx, ifd, &frame->metadata);
2406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
2407 goto end;
2408
2409
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) {
2410 37 av_buffer_unref(pbuf);
2411 37 ret = av_exif_write(avctx, ifd, pbuf, AV_EXIF_TIFF_HEADER);
2412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
2413 goto end;
2414 }
2415
2416 37 ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_EXIF, pbuf);
2417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
2418 goto end;
2419
2420 37 ret = 0;
2421
2422 37 end:
2423 37 av_buffer_unref(pbuf);
2424 37 av_exif_free(cloned);
2425 37 av_free(cloned);
2426 37 return ret;
2427 }
2428
2429 33 int ff_decode_exif_attach_ifd(AVCodecContext *avctx, AVFrame *frame, const AVExifMetadata *ifd)
2430 {
2431 33 AVBufferRef *dummy = NULL;
2432 33 return exif_attach_ifd(avctx, frame, ifd, &dummy);
2433 }
2434
2435 4 int ff_decode_exif_attach_buffer(AVCodecContext *avctx, AVFrame *frame, AVBufferRef **pbuf,
2436 enum AVExifHeaderMode header_mode)
2437 {
2438 int ret;
2439 4 AVBufferRef *data = *pbuf;
2440 4 AVExifMetadata ifd = { 0 };
2441
2442 4 ret = av_exif_parse_buffer(avctx, data->data, data->size, &ifd, header_mode);
2443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
2444 goto end;
2445
2446 4 ret = exif_attach_ifd(avctx, frame, &ifd, pbuf);
2447
2448 4 end:
2449 4 av_buffer_unref(pbuf);
2450 4 av_exif_free(&ifd);
2451 4 return ret;
2452 }
2453