FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 729 1139 64.0%
Functions: 56 61 91.8%
Branches: 469 841 55.8%

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 <string.h>
23
24 #include "config.h"
25
26 #if CONFIG_ICONV
27 # include <iconv.h>
28 #endif
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/common.h"
33 #include "libavutil/emms.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/hwcontext.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mastering_display_metadata.h"
39 #include "libavutil/mem.h"
40
41 #include "avcodec.h"
42 #include "avcodec_internal.h"
43 #include "bytestream.h"
44 #include "bsf.h"
45 #include "codec_desc.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 #include "hwaccel_internal.h"
49 #include "hwconfig.h"
50 #include "internal.h"
51 #include "packet_internal.h"
52 #include "progressframe.h"
53 #include "refstruct.h"
54 #include "thread.h"
55 #include "threadprogress.h"
56
57 typedef struct DecodeContext {
58 AVCodecInternal avci;
59
60 /* to prevent infinite loop on errors when draining */
61 int nb_draining_errors;
62
63 /**
64 * The caller has submitted a NULL packet on input.
65 */
66 int draining_started;
67
68 int64_t pts_correction_num_faulty_pts; /// Number of incorrect PTS values so far
69 int64_t pts_correction_num_faulty_dts; /// Number of incorrect DTS values so far
70 int64_t pts_correction_last_pts; /// PTS of the last frame
71 int64_t pts_correction_last_dts; /// DTS of the last frame
72
73 /**
74 * Bitmask indicating for which side data types we prefer user-supplied
75 * (global or attached to packets) side data over bytestream.
76 */
77 uint64_t side_data_pref_mask;
78 } DecodeContext;
79
80 1921089 static DecodeContext *decode_ctx(AVCodecInternal *avci)
81 {
82 1921089 return (DecodeContext *)avci;
83 }
84
85 376030 static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
86 {
87 int ret;
88 size_t size;
89 const uint8_t *data;
90 uint32_t flags;
91 int64_t val;
92
93 376030 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
94
2/2
✓ Branch 0 taken 376028 times.
✓ Branch 1 taken 2 times.
376030 if (!data)
95 376028 return 0;
96
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
98 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
99 "changes, but PARAM_CHANGE side data was sent to it.\n");
100 ret = AVERROR(EINVAL);
101 goto fail2;
102 }
103
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 4)
105 goto fail;
106
107 2 flags = bytestream_get_le32(&data);
108 2 size -= 4;
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
111 if (size < 4)
112 goto fail;
113 val = bytestream_get_le32(&data);
114 if (val <= 0 || val > INT_MAX) {
115 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
116 ret = AVERROR_INVALIDDATA;
117 goto fail2;
118 }
119 avctx->sample_rate = val;
120 size -= 4;
121 }
122
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 8)
124 goto fail;
125 2 avctx->width = bytestream_get_le32(&data);
126 2 avctx->height = bytestream_get_le32(&data);
127 2 size -= 8;
128 2 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
130 goto fail2;
131 }
132
133 2 return 0;
134 fail:
135 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
136 ret = AVERROR_INVALIDDATA;
137 fail2:
138 if (ret < 0) {
139 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
140 if (avctx->err_recognition & AV_EF_EXPLODE)
141 return ret;
142 }
143 return 0;
144 }
145
146 376030 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
147 {
148 376030 int ret = 0;
149
150 376030 av_packet_unref(avci->last_pkt_props);
151
1/2
✓ Branch 0 taken 376030 times.
✗ Branch 1 not taken.
376030 if (pkt) {
152 376030 ret = av_packet_copy_props(avci->last_pkt_props, pkt);
153 #if FF_API_FRAME_PKT
154
1/2
✓ Branch 0 taken 376030 times.
✗ Branch 1 not taken.
376030 if (!ret)
155 376030 avci->last_pkt_props->stream_index = pkt->size; // Needed for ff_decode_frame_props().
156 #endif
157 }
158 376030 return ret;
159 }
160
161 14480 static int decode_bsfs_init(AVCodecContext *avctx)
162 {
163 14480 AVCodecInternal *avci = avctx->internal;
164 14480 const FFCodec *const codec = ffcodec(avctx->codec);
165 int ret;
166
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (avci->bsf)
168 return 0;
169
170 14480 ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (ret < 0) {
172 av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret));
173 if (ret != AVERROR(ENOMEM))
174 ret = AVERROR_BUG;
175 goto fail;
176 }
177
178 /* We do not currently have an API for passing the input timebase into decoders,
179 * but no filters used here should actually need it.
180 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
181 14480 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
182 14480 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (ret < 0)
184 goto fail;
185
186 14480 ret = av_bsf_init(avci->bsf);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (ret < 0)
188 goto fail;
189
190 14480 return 0;
191 fail:
192 av_bsf_free(&avci->bsf);
193 return ret;
194 }
195
196 1130585 static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
197 {
198 1130585 AVCodecInternal *avci = avctx->internal;
199 int ret;
200
201 1130585 ret = av_bsf_receive_packet(avci->bsf, pkt);
202
2/2
✓ Branch 0 taken 3346 times.
✓ Branch 1 taken 1127239 times.
1130585 if (ret == AVERROR_EOF)
203 3346 avci->draining = 1;
204
2/2
✓ Branch 0 taken 754555 times.
✓ Branch 1 taken 376030 times.
1130585 if (ret < 0)
205 754555 return ret;
206
207
1/2
✓ Branch 1 taken 376030 times.
✗ Branch 2 not taken.
376030 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
208 376030 ret = extract_packet_props(avctx->internal, pkt);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 376030 times.
376030 if (ret < 0)
210 goto finish;
211 }
212
213 376030 ret = apply_param_change(avctx, pkt);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 376030 times.
376030 if (ret < 0)
215 goto finish;
216
217 376030 return 0;
218 finish:
219 av_packet_unref(pkt);
220 return ret;
221 }
222
223 751290 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
224 {
225 751290 AVCodecInternal *avci = avctx->internal;
226 751290 DecodeContext *dc = decode_ctx(avci);
227
228
2/2
✓ Branch 0 taken 751273 times.
✓ Branch 1 taken 17 times.
751290 if (avci->draining)
229 17 return AVERROR_EOF;
230
231 379312 while (1) {
232 1130585 int ret = decode_get_packet(avctx, pkt);
233
2/2
✓ Branch 0 taken 751209 times.
✓ Branch 1 taken 379376 times.
1130585 if (ret == AVERROR(EAGAIN) &&
234
5/6
✓ Branch 0 taken 375243 times.
✓ Branch 1 taken 375966 times.
✓ Branch 2 taken 375243 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3346 times.
✓ Branch 5 taken 371897 times.
751209 (!AVPACKET_IS_EMPTY(avci->buffer_pkt) || dc->draining_started)) {
235 379312 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 379312 times.
379312 if (ret < 0) {
237 av_packet_unref(avci->buffer_pkt);
238 return ret;
239 }
240
241 379312 continue;
242 }
243
244 751273 return ret;
245 }
246 }
247
248 /**
249 * Attempt to guess proper monotonic timestamps for decoded video frames
250 * which might have incorrect times. Input timestamps may wrap around, in
251 * which case the output will as well.
252 *
253 * @param pts the pts field of the decoded AVPacket, as passed through
254 * AVFrame.pts
255 * @param dts the dts field of the decoded AVPacket
256 * @return one of the input values, may be AV_NOPTS_VALUE
257 */
258 394370 static int64_t guess_correct_pts(DecodeContext *dc,
259 int64_t reordered_pts, int64_t dts)
260 {
261 394370 int64_t pts = AV_NOPTS_VALUE;
262
263
2/2
✓ Branch 0 taken 305020 times.
✓ Branch 1 taken 89350 times.
394370 if (dts != AV_NOPTS_VALUE) {
264 305020 dc->pts_correction_num_faulty_dts += dts <= dc->pts_correction_last_dts;
265 305020 dc->pts_correction_last_dts = dts;
266
2/2
✓ Branch 0 taken 353 times.
✓ Branch 1 taken 88997 times.
89350 } else if (reordered_pts != AV_NOPTS_VALUE)
267 353 dc->pts_correction_last_dts = reordered_pts;
268
269
2/2
✓ Branch 0 taken 304678 times.
✓ Branch 1 taken 89692 times.
394370 if (reordered_pts != AV_NOPTS_VALUE) {
270 304678 dc->pts_correction_num_faulty_pts += reordered_pts <= dc->pts_correction_last_pts;
271 304678 dc->pts_correction_last_pts = reordered_pts;
272
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 88997 times.
89692 } else if(dts != AV_NOPTS_VALUE)
273 695 dc->pts_correction_last_pts = dts;
274
275
4/4
✓ Branch 0 taken 566 times.
✓ Branch 1 taken 393804 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 557 times.
394370 if ((dc->pts_correction_num_faulty_pts<=dc->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
276
2/2
✓ Branch 0 taken 304121 times.
✓ Branch 1 taken 89692 times.
393813 && reordered_pts != AV_NOPTS_VALUE)
277 304121 pts = reordered_pts;
278 else
279 90249 pts = dts;
280
281 394370 return pts;
282 }
283
284 257322 static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
285 {
286 257322 AVCodecInternal *avci = avctx->internal;
287 AVFrameSideData *side;
288 257322 uint32_t discard_padding = 0;
289 257322 uint8_t skip_reason = 0;
290 257322 uint8_t discard_reason = 0;
291
292 257322 side = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
293
3/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 257210 times.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
257322 if (side && side->size >= 10) {
294 112 avci->skip_samples = AV_RL32(side->data);
295 112 avci->skip_samples = FFMAX(0, avci->skip_samples);
296 112 discard_padding = AV_RL32(side->data + 4);
297 112 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
298 avci->skip_samples, (int)discard_padding);
299 112 skip_reason = AV_RL8(side->data + 8);
300 112 discard_reason = AV_RL8(side->data + 9);
301 }
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257322 times.
257322 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
304 if (!side && (avci->skip_samples || discard_padding))
305 side = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
306 if (side && (avci->skip_samples || discard_padding)) {
307 AV_WL32(side->data, avci->skip_samples);
308 AV_WL32(side->data + 4, discard_padding);
309 AV_WL8(side->data + 8, skip_reason);
310 AV_WL8(side->data + 9, discard_reason);
311 avci->skip_samples = 0;
312 }
313 return 0;
314 }
315 257322 av_frame_remove_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
316
317
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 257302 times.
257322 if ((frame->flags & AV_FRAME_FLAG_DISCARD)) {
318 20 avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
319 20 *discarded_samples += frame->nb_samples;
320 20 return AVERROR(EAGAIN);
321 }
322
323
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 257089 times.
257302 if (avci->skip_samples > 0) {
324
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 63 times.
213 if (frame->nb_samples <= avci->skip_samples){
325 150 *discarded_samples += frame->nb_samples;
326 150 avci->skip_samples -= frame->nb_samples;
327 150 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
328 avci->skip_samples);
329 150 return AVERROR(EAGAIN);
330 } else {
331 63 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
332 63 frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
333
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
126 if (avctx->pkt_timebase.num && avctx->sample_rate) {
334 63 int64_t diff_ts = av_rescale_q(avci->skip_samples,
335 63 (AVRational){1, avctx->sample_rate},
336 avctx->pkt_timebase);
337
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (frame->pts != AV_NOPTS_VALUE)
338 63 frame->pts += diff_ts;
339
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (frame->pkt_dts != AV_NOPTS_VALUE)
340 63 frame->pkt_dts += diff_ts;
341
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (frame->duration >= diff_ts)
342 63 frame->duration -= diff_ts;
343 } else
344 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
345
346 63 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
347 avci->skip_samples, frame->nb_samples);
348 63 *discarded_samples += avci->skip_samples;
349 63 frame->nb_samples -= avci->skip_samples;
350 63 avci->skip_samples = 0;
351 }
352 }
353
354
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 257145 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
257152 if (discard_padding > 0 && discard_padding <= frame->nb_samples) {
355
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (discard_padding == frame->nb_samples) {
356 2 *discarded_samples += frame->nb_samples;
357 2 return AVERROR(EAGAIN);
358 } else {
359
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (avctx->pkt_timebase.num && avctx->sample_rate) {
360 5 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
361 5 (AVRational){1, avctx->sample_rate},
362 avctx->pkt_timebase);
363 5 frame->duration = diff_ts;
364 } else
365 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
366
367 5 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
368 (int)discard_padding, frame->nb_samples);
369 5 frame->nb_samples -= discard_padding;
370 }
371 }
372
373 257150 return 0;
374 }
375
376 /*
377 * The core of the receive_frame_wrapper for the decoders implementing
378 * the simple API. Certain decoders might consume partial packets without
379 * returning any output, so this function needs to be called in a loop until it
380 * returns EAGAIN.
381 **/
382 779274 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
383 {
384 779274 AVCodecInternal *avci = avctx->internal;
385 779274 AVPacket *const pkt = avci->in_pkt;
386 779274 const FFCodec *const codec = ffcodec(avctx->codec);
387 int got_frame, consumed;
388 int ret;
389
390
4/4
✓ Branch 0 taken 750655 times.
✓ Branch 1 taken 28619 times.
✓ Branch 2 taken 748952 times.
✓ Branch 3 taken 1703 times.
779274 if (!pkt->data && !avci->draining) {
391 748952 av_packet_unref(pkt);
392 748952 ret = ff_decode_get_packet(avctx, pkt);
393
4/4
✓ Branch 0 taken 374065 times.
✓ Branch 1 taken 374887 times.
✓ Branch 2 taken 370723 times.
✓ Branch 3 taken 3342 times.
748952 if (ret < 0 && ret != AVERROR_EOF)
394 370723 return ret;
395 }
396
397 // Some codecs (at least wma lossless) will crash when feeding drain packets
398 // after EOF was signaled.
399
2/2
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 407839 times.
408551 if (avci->draining_done)
400 712 return AVERROR_EOF;
401
402
2/2
✓ Branch 0 taken 4333 times.
✓ Branch 1 taken 403506 times.
407839 if (!pkt->data &&
403
2/2
✓ Branch 0 taken 2670 times.
✓ Branch 1 taken 1663 times.
4333 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
404
2/2
✓ Branch 0 taken 2629 times.
✓ Branch 1 taken 41 times.
2670 avctx->active_thread_type & FF_THREAD_FRAME))
405 2629 return AVERROR_EOF;
406
407 405210 got_frame = 0;
408
409
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 404600 times.
405210 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
410 610 consumed = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
411 } else {
412 404600 consumed = codec->cb.decode(avctx, frame, &got_frame, pkt);
413
414
1/2
✓ Branch 0 taken 404600 times.
✗ Branch 1 not taken.
404600 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
415 404600 frame->pkt_dts = pkt->dts;
416
2/2
✓ Branch 0 taken 148153 times.
✓ Branch 1 taken 256447 times.
404600 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
417 #if FF_API_FRAME_PKT
418 FF_DISABLE_DEPRECATION_WARNINGS
419
2/2
✓ Branch 0 taken 112884 times.
✓ Branch 1 taken 35269 times.
148153 if(!avctx->has_b_frames)
420 112884 frame->pkt_pos = pkt->pos;
421 FF_ENABLE_DEPRECATION_WARNINGS
422 #endif
423 }
424 }
425 405210 emms_c();
426
427
2/2
✓ Branch 0 taken 148291 times.
✓ Branch 1 taken 256919 times.
405210 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
428
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 137207 times.
285678 ret = (!got_frame || frame->flags & AV_FRAME_FLAG_DISCARD)
429 ? AVERROR(EAGAIN)
430
2/2
✓ Branch 0 taken 137387 times.
✓ Branch 1 taken 10904 times.
285678 : 0;
431
1/2
✓ Branch 0 taken 256919 times.
✗ Branch 1 not taken.
256919 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
432 256919 ret = !got_frame ? AVERROR(EAGAIN)
433
2/2
✓ Branch 0 taken 255513 times.
✓ Branch 1 taken 1406 times.
256919 : discard_samples(avctx, frame, discarded_samples);
434 }
435
436
2/2
✓ Branch 0 taken 12662 times.
✓ Branch 1 taken 392548 times.
405210 if (ret == AVERROR(EAGAIN))
437 12662 av_frame_unref(frame);
438
439 // FF_CODEC_CB_TYPE_DECODE decoders must not return AVERROR EAGAIN
440 // code later will add AVERROR(EAGAIN) to a pointer
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405210 times.
405210 av_assert0(consumed != AVERROR(EAGAIN));
442
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 404536 times.
405210 if (consumed < 0)
443 674 ret = consumed;
444
4/4
✓ Branch 0 taken 404536 times.
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 147654 times.
✓ Branch 3 taken 256882 times.
405210 if (consumed >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
445 147654 consumed = pkt->size;
446
447
2/2
✓ Branch 0 taken 392548 times.
✓ Branch 1 taken 12662 times.
405210 if (!ret)
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392548 times.
392548 av_assert0(frame->buf[0]);
449
2/2
✓ Branch 0 taken 11988 times.
✓ Branch 1 taken 393222 times.
405210 if (ret == AVERROR(EAGAIN))
450 11988 ret = 0;
451
452 /* do not stop draining when got_frame != 0 or ret < 0 */
453
4/4
✓ Branch 0 taken 1704 times.
✓ Branch 1 taken 403506 times.
✓ Branch 2 taken 713 times.
✓ Branch 3 taken 991 times.
405210 if (avci->draining && !got_frame) {
454
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 712 times.
713 if (ret < 0) {
455 /* prevent infinite loop if a decoder wrongly always return error on draining */
456 /* reasonable nb_errors_max = maximum b frames + thread count */
457
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 ?
458 avctx->thread_count : 1);
459
460
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (decode_ctx(avci)->nb_draining_errors++ >= nb_errors_max) {
461 av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
462 "Stop draining and force EOF.\n");
463 avci->draining_done = 1;
464 ret = AVERROR_BUG;
465 }
466 } else {
467 712 avci->draining_done = 1;
468 }
469 }
470
471
4/4
✓ Branch 0 taken 29307 times.
✓ Branch 1 taken 375903 times.
✓ Branch 2 taken 674 times.
✓ Branch 3 taken 28633 times.
405210 if (consumed >= pkt->size || ret < 0) {
472 376577 av_packet_unref(pkt);
473 } else {
474 28633 pkt->data += consumed;
475 28633 pkt->size -= consumed;
476 28633 pkt->pts = AV_NOPTS_VALUE;
477 28633 pkt->dts = AV_NOPTS_VALUE;
478
1/2
✓ Branch 0 taken 28633 times.
✗ Branch 1 not taken.
28633 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
479 #if FF_API_FRAME_PKT
480 // See extract_packet_props() comment.
481 28633 avci->last_pkt_props->stream_index = avci->last_pkt_props->stream_index - consumed;
482 #endif
483 28633 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
484 28633 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
485 }
486 }
487
488 405210 return ret;
489 }
490
491 #if CONFIG_LCMS2
492 static int detect_colorspace(AVCodecContext *avctx, AVFrame *frame)
493 {
494 AVCodecInternal *avci = avctx->internal;
495 enum AVColorTransferCharacteristic trc;
496 AVColorPrimariesDesc coeffs;
497 enum AVColorPrimaries prim;
498 cmsHPROFILE profile;
499 AVFrameSideData *sd;
500 int ret;
501 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
502 return 0;
503
504 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
505 if (!sd || !sd->size)
506 return 0;
507
508 if (!avci->icc.avctx) {
509 ret = ff_icc_context_init(&avci->icc, avctx);
510 if (ret < 0)
511 return ret;
512 }
513
514 profile = cmsOpenProfileFromMemTHR(avci->icc.ctx, sd->data, sd->size);
515 if (!profile)
516 return AVERROR_INVALIDDATA;
517
518 ret = ff_icc_profile_sanitize(&avci->icc, profile);
519 if (!ret)
520 ret = ff_icc_profile_read_primaries(&avci->icc, profile, &coeffs);
521 if (!ret)
522 ret = ff_icc_profile_detect_transfer(&avci->icc, profile, &trc);
523 cmsCloseProfile(profile);
524 if (ret < 0)
525 return ret;
526
527 prim = av_csp_primaries_id_from_desc(&coeffs);
528 if (prim != AVCOL_PRI_UNSPECIFIED)
529 frame->color_primaries = prim;
530 if (trc != AVCOL_TRC_UNSPECIFIED)
531 frame->color_trc = trc;
532 return 0;
533 }
534 #else /* !CONFIG_LCMS2 */
535 770286 static int detect_colorspace(av_unused AVCodecContext *c, av_unused AVFrame *f)
536 {
537 770286 return 0;
538 }
539 #endif
540
541 791062 static int fill_frame_props(const AVCodecContext *avctx, AVFrame *frame)
542 {
543 int ret;
544
545
2/2
✓ Branch 0 taken 533105 times.
✓ Branch 1 taken 257957 times.
791062 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
546 533105 frame->color_primaries = avctx->color_primaries;
547
2/2
✓ Branch 0 taken 532744 times.
✓ Branch 1 taken 258318 times.
791062 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
548 532744 frame->color_trc = avctx->color_trc;
549
2/2
✓ Branch 0 taken 526853 times.
✓ Branch 1 taken 264209 times.
791062 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
550 526853 frame->colorspace = avctx->colorspace;
551
2/2
✓ Branch 0 taken 731673 times.
✓ Branch 1 taken 59389 times.
791062 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
552 731673 frame->color_range = avctx->color_range;
553
2/2
✓ Branch 0 taken 763545 times.
✓ Branch 1 taken 27517 times.
791062 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
554 763545 frame->chroma_location = avctx->chroma_sample_location;
555
556
2/2
✓ Branch 0 taken 276531 times.
✓ Branch 1 taken 514531 times.
791062 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
557
2/2
✓ Branch 0 taken 260553 times.
✓ Branch 1 taken 15978 times.
276531 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
558
2/2
✓ Branch 0 taken 137376 times.
✓ Branch 1 taken 139155 times.
276531 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
559
1/2
✓ Branch 0 taken 514531 times.
✗ Branch 1 not taken.
514531 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
560
2/2
✓ Branch 0 taken 257355 times.
✓ Branch 1 taken 257176 times.
514531 if (frame->format == AV_SAMPLE_FMT_NONE)
561 257355 frame->format = avctx->sample_fmt;
562
2/2
✓ Branch 0 taken 257355 times.
✓ Branch 1 taken 257176 times.
514531 if (!frame->ch_layout.nb_channels) {
563 257355 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257355 times.
257355 if (ret < 0)
565 return ret;
566 }
567
2/2
✓ Branch 0 taken 257215 times.
✓ Branch 1 taken 257316 times.
514531 if (!frame->sample_rate)
568 257215 frame->sample_rate = avctx->sample_rate;
569 }
570
571 791062 return 0;
572 }
573
574 767286 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
575 {
576 int ret;
577 767286 int64_t discarded_samples = 0;
578
579
2/2
✓ Branch 0 taken 779274 times.
✓ Branch 1 taken 392548 times.
1171822 while (!frame->buf[0]) {
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 779274 times.
779274 if (discarded_samples > avctx->max_samples)
581 return AVERROR(EAGAIN);
582 779274 ret = decode_simple_internal(avctx, frame, &discarded_samples);
583
2/2
✓ Branch 0 taken 374738 times.
✓ Branch 1 taken 404536 times.
779274 if (ret < 0)
584 374738 return ret;
585 }
586
587 392548 return 0;
588 }
589
590 770286 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
591 {
592 770286 AVCodecInternal *avci = avctx->internal;
593 770286 DecodeContext *dc = decode_ctx(avci);
594 770286 const FFCodec *const codec = ffcodec(avctx->codec);
595 int ret, ok;
596
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770286 times.
770286 av_assert0(!frame->buf[0]);
598
599
2/2
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 767286 times.
770286 if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) {
600 3000 ret = codec->cb.receive_frame(avctx, frame);
601 3000 emms_c();
602
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 1178 times.
3000 if (!ret) {
603
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1809 times.
1822 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO)
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 ret = (frame->flags & AV_FRAME_FLAG_DISCARD) ? AVERROR(EAGAIN) : 0;
605
1/2
✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
1809 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
606 1809 int64_t discarded_samples = 0;
607 1809 ret = discard_samples(avctx, frame, &discarded_samples);
608 }
609 }
610 } else
611 767286 ret = decode_simple_receive_frame(avctx, frame);
612
613
2/2
✓ Branch 0 taken 3345 times.
✓ Branch 1 taken 766941 times.
770286 if (ret == AVERROR_EOF)
614 3345 avci->draining_done = 1;
615
616 /* preserve ret */
617 770286 ok = detect_colorspace(avctx, frame);
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770286 times.
770286 if (ok < 0) {
619 av_frame_unref(frame);
620 return ok;
621 }
622
623
2/2
✓ Branch 0 taken 394370 times.
✓ Branch 1 taken 375916 times.
770286 if (!ret) {
624
2/2
✓ Branch 0 taken 137220 times.
✓ Branch 1 taken 257150 times.
394370 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
625
2/2
✓ Branch 0 taken 23781 times.
✓ Branch 1 taken 113439 times.
137220 if (!frame->width)
626 23781 frame->width = avctx->width;
627
2/2
✓ Branch 0 taken 23781 times.
✓ Branch 1 taken 113439 times.
137220 if (!frame->height)
628 23781 frame->height = avctx->height;
629 } else
630 257150 frame->flags |= AV_FRAME_FLAG_KEY;
631
632 394370 ret = fill_frame_props(avctx, frame);
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394370 times.
394370 if (ret < 0) {
634 av_frame_unref(frame);
635 return ret;
636 }
637
638 #if FF_API_FRAME_KEY
639 FF_DISABLE_DEPRECATION_WARNINGS
640 394370 frame->key_frame = !!(frame->flags & AV_FRAME_FLAG_KEY);
641 FF_ENABLE_DEPRECATION_WARNINGS
642 #endif
643 #if FF_API_INTERLACED_FRAME
644 FF_DISABLE_DEPRECATION_WARNINGS
645 394370 frame->interlaced_frame = !!(frame->flags & AV_FRAME_FLAG_INTERLACED);
646 394370 frame->top_field_first = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
647 FF_ENABLE_DEPRECATION_WARNINGS
648 #endif
649 394370 frame->best_effort_timestamp = guess_correct_pts(dc,
650 frame->pts,
651 frame->pkt_dts);
652
653 /* the only case where decode data is not set should be decoders
654 * that do not call ff_get_buffer() */
655
4/6
✓ Branch 0 taken 369871 times.
✓ Branch 1 taken 24499 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 369871 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24499 times.
394370 av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
656 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
657
658
2/2
✓ Branch 0 taken 369871 times.
✓ Branch 1 taken 24499 times.
394370 if (frame->private_ref) {
659 369871 FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
660
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369871 times.
369871 if (fdd->post_process) {
662 ret = fdd->post_process(avctx, frame);
663 if (ret < 0) {
664 av_frame_unref(frame);
665 return ret;
666 }
667 }
668 }
669 }
670
671 /* free the per-frame decode data */
672 770286 av_buffer_unref(&frame->private_ref);
673
674 770286 return ret;
675 }
676
677 379342 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
678 {
679 379342 AVCodecInternal *avci = avctx->internal;
680 379342 DecodeContext *dc = decode_ctx(avci);
681 int ret;
682
683
2/4
✓ Branch 1 taken 379342 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 379342 times.
379342 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
684 return AVERROR(EINVAL);
685
686
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 379312 times.
379342 if (dc->draining_started)
687 30 return AVERROR_EOF;
688
689
5/6
✓ Branch 0 taken 376059 times.
✓ Branch 1 taken 3253 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 375966 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 93 times.
379312 if (avpkt && !avpkt->size && avpkt->data)
690 return AVERROR(EINVAL);
691
692
5/6
✓ Branch 0 taken 376059 times.
✓ Branch 1 taken 3253 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 375966 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 93 times.
379312 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
693
2/4
✓ Branch 0 taken 375966 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 375966 times.
375966 if (!AVPACKET_IS_EMPTY(avci->buffer_pkt))
694 return AVERROR(EAGAIN);
695 375966 ret = av_packet_ref(avci->buffer_pkt, avpkt);
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375966 times.
375966 if (ret < 0)
697 return ret;
698 } else
699 3346 dc->draining_started = 1;
700
701
3/4
✓ Branch 0 taken 379312 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 375966 times.
✓ Branch 3 taken 3346 times.
379312 if (!avci->buffer_frame->buf[0] && !dc->draining_started) {
702 375966 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
703
5/6
✓ Branch 0 taken 10674 times.
✓ Branch 1 taken 365292 times.
✓ Branch 2 taken 668 times.
✓ Branch 3 taken 10006 times.
✓ Branch 4 taken 668 times.
✗ Branch 5 not taken.
375966 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
704 668 return ret;
705 }
706
707 378644 return 0;
708 }
709
710 137220 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
711 {
712 /* make sure we are noisy about decoders returning invalid cropping data */
713
1/2
✓ Branch 0 taken 137220 times.
✗ Branch 1 not taken.
137220 if (frame->crop_left >= INT_MAX - frame->crop_right ||
714
1/2
✓ Branch 0 taken 137220 times.
✗ Branch 1 not taken.
137220 frame->crop_top >= INT_MAX - frame->crop_bottom ||
715
1/2
✓ Branch 0 taken 137220 times.
✗ Branch 1 not taken.
137220 (frame->crop_left + frame->crop_right) >= frame->width ||
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137220 times.
137220 (frame->crop_top + frame->crop_bottom) >= frame->height) {
717 av_log(avctx, AV_LOG_WARNING,
718 "Invalid cropping information set by a decoder: "
719 "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
720 "(frame size %dx%d). This is a bug, please report it\n",
721 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
722 frame->width, frame->height);
723 frame->crop_left = 0;
724 frame->crop_right = 0;
725 frame->crop_top = 0;
726 frame->crop_bottom = 0;
727 return 0;
728 }
729
730
2/2
✓ Branch 0 taken 129903 times.
✓ Branch 1 taken 7317 times.
137220 if (!avctx->apply_cropping)
731 129903 return 0;
732
733 7317 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
734 AV_FRAME_CROP_UNALIGNED : 0);
735 }
736
737 // make sure frames returned to the caller are valid
738 394370 static int frame_validate(AVCodecContext *avctx, AVFrame *frame)
739 {
740
2/4
✓ Branch 0 taken 394370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 394370 times.
394370 if (!frame->buf[0] || frame->format < 0)
741 goto fail;
742
743
2/3
✓ Branch 0 taken 137220 times.
✓ Branch 1 taken 257150 times.
✗ Branch 2 not taken.
394370 switch (avctx->codec_type) {
744 137220 case AVMEDIA_TYPE_VIDEO:
745
2/4
✓ Branch 0 taken 137220 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 137220 times.
137220 if (frame->width <= 0 || frame->height <= 0)
746 goto fail;
747 137220 break;
748 257150 case AVMEDIA_TYPE_AUDIO:
749
1/2
✓ Branch 1 taken 257150 times.
✗ Branch 2 not taken.
257150 if (!av_channel_layout_check(&frame->ch_layout) ||
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257150 times.
257150 frame->sample_rate <= 0)
751 goto fail;
752
753 257150 break;
754 default: av_assert0(0);
755 }
756
757 394370 return 0;
758 fail:
759 av_log(avctx, AV_LOG_ERROR, "An invalid frame was output by a decoder. "
760 "This is a bug, please report it.\n");
761 return AVERROR_BUG;
762 }
763
764 759612 int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
765 {
766 759612 AVCodecInternal *avci = avctx->internal;
767 int ret;
768
769
2/4
✓ Branch 1 taken 759612 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 759612 times.
759612 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
770 return AVERROR(EINVAL);
771
772
2/2
✓ Branch 0 taken 365292 times.
✓ Branch 1 taken 394320 times.
759612 if (avci->buffer_frame->buf[0]) {
773 365292 av_frame_move_ref(frame, avci->buffer_frame);
774 } else {
775 394320 ret = decode_receive_frame_internal(avctx, frame);
776
2/2
✓ Branch 0 taken 365242 times.
✓ Branch 1 taken 29078 times.
394320 if (ret < 0)
777 365242 return ret;
778 }
779
780 394370 ret = frame_validate(avctx, frame);
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394370 times.
394370 if (ret < 0)
782 goto fail;
783
784
2/2
✓ Branch 0 taken 137220 times.
✓ Branch 1 taken 257150 times.
394370 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
785 137220 ret = apply_cropping(avctx, frame);
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137220 times.
137220 if (ret < 0)
787 goto fail;
788 }
789
790 394370 avctx->frame_num++;
791
792 #if FF_API_DROPCHANGED
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394370 times.
394370 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
794
795 if (avctx->frame_num == 1) {
796 avci->initial_format = frame->format;
797 switch(avctx->codec_type) {
798 case AVMEDIA_TYPE_VIDEO:
799 avci->initial_width = frame->width;
800 avci->initial_height = frame->height;
801 break;
802 case AVMEDIA_TYPE_AUDIO:
803 avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
804 avctx->sample_rate;
805 ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout);
806 if (ret < 0)
807 goto fail;
808 break;
809 }
810 }
811
812 if (avctx->frame_num > 1) {
813 int changed = avci->initial_format != frame->format;
814
815 switch(avctx->codec_type) {
816 case AVMEDIA_TYPE_VIDEO:
817 changed |= avci->initial_width != frame->width ||
818 avci->initial_height != frame->height;
819 break;
820 case AVMEDIA_TYPE_AUDIO:
821 changed |= avci->initial_sample_rate != frame->sample_rate ||
822 avci->initial_sample_rate != avctx->sample_rate ||
823 av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout);
824 break;
825 }
826
827 if (changed) {
828 avci->changed_frames_dropped++;
829 av_log(avctx, AV_LOG_INFO, "dropped changed frame #%"PRId64" pts %"PRId64
830 " drop count: %d \n",
831 avctx->frame_num, frame->pts,
832 avci->changed_frames_dropped);
833 ret = AVERROR_INPUT_CHANGED;
834 goto fail;
835 }
836 }
837 }
838 #endif
839 394370 return 0;
840 fail:
841 av_frame_unref(frame);
842 return ret;
843 }
844
845 1709 static void get_subtitle_defaults(AVSubtitle *sub)
846 {
847 1709 memset(sub, 0, sizeof(*sub));
848 1709 sub->pts = AV_NOPTS_VALUE;
849 1709 }
850
851 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
852 1675 static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt,
853 const AVPacket *inpkt, AVPacket *buf_pkt)
854 {
855 #if CONFIG_ICONV
856 1675 iconv_t cd = (iconv_t)-1;
857 1675 int ret = 0;
858 char *inb, *outb;
859 size_t inl, outl;
860 #endif
861
862
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
1675 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) {
863 1582 *outpkt = inpkt;
864 1582 return 0;
865 }
866
867 #if CONFIG_ICONV
868 93 inb = inpkt->data;
869 93 inl = inpkt->size;
870
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
872 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
873 return AVERROR(ERANGE);
874 }
875
876 93 cd = iconv_open("UTF-8", avctx->sub_charenc);
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 av_assert0(cd != (iconv_t)-1);
878
879 93 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
881 goto end;
882 93 ret = av_packet_copy_props(buf_pkt, inpkt);
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
884 goto end;
885 93 outb = buf_pkt->data;
886 93 outl = buf_pkt->size;
887
888
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 ||
889 93 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
890
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) {
891 ret = FFMIN(AVERROR(errno), -1);
892 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
893 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
894 goto end;
895 }
896 93 buf_pkt->size -= outl;
897 93 memset(buf_pkt->data + buf_pkt->size, 0, outl);
898 93 *outpkt = buf_pkt;
899
900 93 ret = 0;
901 93 end:
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
903 av_packet_unref(buf_pkt);
904
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (cd != (iconv_t)-1)
905 93 iconv_close(cd);
906 93 return ret;
907 #else
908 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
909 return AVERROR(EINVAL);
910 #endif
911 }
912
913 700 static int utf8_check(const uint8_t *str)
914 {
915 const uint8_t *byte;
916 uint32_t codepoint, min;
917
918
2/2
✓ Branch 0 taken 61070 times.
✓ Branch 1 taken 700 times.
61770 while (*str) {
919 61070 byte = str;
920
5/8
✓ Branch 0 taken 61070 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61070 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2798 times.
✓ Branch 6 taken 2798 times.
✓ Branch 7 taken 61070 times.
63868 GET_UTF8(codepoint, *(byte++), return 0;);
921
4/4
✓ Branch 0 taken 2091 times.
✓ Branch 1 taken 58979 times.
✓ Branch 2 taken 707 times.
✓ Branch 3 taken 1384 times.
61777 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
922 707 1 << (5 * (byte - str) - 4);
923
3/6
✓ Branch 0 taken 61070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61070 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61070 times.
✗ Branch 5 not taken.
61070 if (codepoint < min || codepoint >= 0x110000 ||
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61070 times.
61070 codepoint == 0xFFFE /* BOM */ ||
925 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
926 return 0;
927 61070 str = byte;
928 }
929 700 return 1;
930 }
931
932 1709 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
933 int *got_sub_ptr, const AVPacket *avpkt)
934 {
935 1709 int ret = 0;
936
937
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1669 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
1709 if (!avpkt->data && avpkt->size) {
938 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
939 return AVERROR(EINVAL);
940 }
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1709 times.
1709 if (!avctx->codec)
942 return AVERROR(EINVAL);
943
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1709 times.
1709 if (ffcodec(avctx->codec)->cb_type != FF_CODEC_CB_TYPE_DECODE_SUB) {
944 av_log(avctx, AV_LOG_ERROR, "Codec not subtitle decoder\n");
945 return AVERROR(EINVAL);
946 }
947
948 1709 *got_sub_ptr = 0;
949 1709 get_subtitle_defaults(sub);
950
951
4/4
✓ Branch 0 taken 857 times.
✓ Branch 1 taken 852 times.
✓ Branch 2 taken 823 times.
✓ Branch 3 taken 34 times.
1709 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
952 1675 AVCodecInternal *avci = avctx->internal;
953 const AVPacket *pkt;
954
955 1675 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1675 times.
1675 if (ret < 0)
957 79 return ret;
958
959
3/4
✓ Branch 0 taken 1675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
✓ Branch 3 taken 10 times.
1675 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
960 1665 sub->pts = av_rescale_q(avpkt->pts,
961 1665 avctx->pkt_timebase, AV_TIME_BASE_Q);
962 1675 ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
963
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
1675 if (pkt == avci->buffer_pkt) // did we recode?
964 93 av_packet_unref(avci->buffer_pkt);
965
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1596 times.
1675 if (ret < 0) {
966 79 *got_sub_ptr = 0;
967 79 avsubtitle_free(sub);
968 79 return ret;
969 }
970 av_assert1(!sub->num_rects || *got_sub_ptr);
971
972
6/6
✓ Branch 0 taken 826 times.
✓ Branch 1 taken 770 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 246 times.
✓ Branch 4 taken 528 times.
✓ Branch 5 taken 52 times.
1596 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
973
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 avctx->pkt_timebase.num) {
974 528 AVRational ms = { 1, 1000 };
975 528 sub->end_display_time = av_rescale_q(avpkt->duration,
976 avctx->pkt_timebase, ms);
977 }
978
979
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1440 times.
1596 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
980 156 sub->format = 0;
981
1/2
✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
1440 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
982 1440 sub->format = 1;
983
984
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 1596 times.
2455 for (unsigned i = 0; i < sub->num_rects; i++) {
985
1/2
✓ Branch 0 taken 859 times.
✗ Branch 1 not taken.
859 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
986
3/4
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 700 times.
859 sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
987 av_log(avctx, AV_LOG_ERROR,
988 "Invalid UTF-8 in decoded subtitles text; "
989 "maybe missing -sub_charenc option\n");
990 avsubtitle_free(sub);
991 *got_sub_ptr = 0;
992 return AVERROR_INVALIDDATA;
993 }
994 }
995
996
2/2
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 761 times.
1596 if (*got_sub_ptr)
997 835 avctx->frame_num++;
998 }
999
1000 1630 return ret;
1001 }
1002
1003 1481 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
1004 const enum AVPixelFormat *fmt)
1005 {
1006 const AVPixFmtDescriptor *desc;
1007 const AVCodecHWConfig *config;
1008 int i, n;
1009
1010 // If a device was supplied when the codec was opened, assume that the
1011 // user wants to use it.
1012
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1481 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1481 if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
1013 AVHWDeviceContext *device_ctx =
1014 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1015 for (i = 0;; i++) {
1016 config = &ffcodec(avctx->codec)->hw_configs[i]->public;
1017 if (!config)
1018 break;
1019 if (!(config->methods &
1020 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1021 continue;
1022 if (device_ctx->type != config->device_type)
1023 continue;
1024 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1025 if (config->pix_fmt == fmt[n])
1026 return fmt[n];
1027 }
1028 }
1029 }
1030 // No device or other setup, so we have to choose from things which
1031 // don't any other external information.
1032
1033 // If the last element of the list is a software format, choose it
1034 // (this should be best software format if any exist).
1035
2/2
✓ Branch 0 taken 3832 times.
✓ Branch 1 taken 1481 times.
5313 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1036 1481 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1037
1/2
✓ Branch 0 taken 1481 times.
✗ Branch 1 not taken.
1481 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1038 1481 return fmt[n - 1];
1039
1040 // Finally, traverse the list in order and choose the first entry
1041 // with no external dependencies (if there is no hardware configuration
1042 // information available then this just picks the first entry).
1043 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1044 for (i = 0;; i++) {
1045 config = avcodec_get_hw_config(avctx->codec, i);
1046 if (!config)
1047 break;
1048 if (config->pix_fmt == fmt[n])
1049 break;
1050 }
1051 if (!config) {
1052 // No specific config available, so the decoder must be able
1053 // to handle this format without any additional setup.
1054 return fmt[n];
1055 }
1056 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1057 // Usable with only internal setup.
1058 return fmt[n];
1059 }
1060 }
1061
1062 // Nothing is usable, give up.
1063 return AV_PIX_FMT_NONE;
1064 }
1065
1066 int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
1067 enum AVHWDeviceType dev_type)
1068 {
1069 AVHWDeviceContext *device_ctx;
1070 AVHWFramesContext *frames_ctx;
1071 int ret;
1072
1073 if (!avctx->hwaccel)
1074 return AVERROR(ENOSYS);
1075
1076 if (avctx->hw_frames_ctx)
1077 return 0;
1078 if (!avctx->hw_device_ctx) {
1079 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
1080 "required for hardware accelerated decoding.\n");
1081 return AVERROR(EINVAL);
1082 }
1083
1084 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
1085 if (device_ctx->type != dev_type) {
1086 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
1087 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
1088 av_hwdevice_get_type_name(device_ctx->type));
1089 return AVERROR(EINVAL);
1090 }
1091
1092 ret = avcodec_get_hw_frames_parameters(avctx,
1093 avctx->hw_device_ctx,
1094 avctx->hwaccel->pix_fmt,
1095 &avctx->hw_frames_ctx);
1096 if (ret < 0)
1097 return ret;
1098
1099 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1100
1101
1102 if (frames_ctx->initial_pool_size) {
1103 // We guarantee 4 base work surfaces. The function above guarantees 1
1104 // (the absolute minimum), so add the missing count.
1105 frames_ctx->initial_pool_size += 3;
1106 }
1107
1108 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1109 if (ret < 0) {
1110 av_buffer_unref(&avctx->hw_frames_ctx);
1111 return ret;
1112 }
1113
1114 return 0;
1115 }
1116
1117 int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
1118 AVBufferRef *device_ref,
1119 enum AVPixelFormat hw_pix_fmt,
1120 AVBufferRef **out_frames_ref)
1121 {
1122 AVBufferRef *frames_ref = NULL;
1123 const AVCodecHWConfigInternal *hw_config;
1124 const FFHWAccel *hwa;
1125 int i, ret;
1126
1127 for (i = 0;; i++) {
1128 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1129 if (!hw_config)
1130 return AVERROR(ENOENT);
1131 if (hw_config->public.pix_fmt == hw_pix_fmt)
1132 break;
1133 }
1134
1135 hwa = hw_config->hwaccel;
1136 if (!hwa || !hwa->frame_params)
1137 return AVERROR(ENOENT);
1138
1139 frames_ref = av_hwframe_ctx_alloc(device_ref);
1140 if (!frames_ref)
1141 return AVERROR(ENOMEM);
1142
1143 if (!avctx->internal->hwaccel_priv_data) {
1144 avctx->internal->hwaccel_priv_data =
1145 av_mallocz(hwa->priv_data_size);
1146 if (!avctx->internal->hwaccel_priv_data) {
1147 av_buffer_unref(&frames_ref);
1148 return AVERROR(ENOMEM);
1149 }
1150 }
1151
1152 ret = hwa->frame_params(avctx, frames_ref);
1153 if (ret >= 0) {
1154 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1155
1156 if (frames_ctx->initial_pool_size) {
1157 // If the user has requested that extra output surfaces be
1158 // available then add them here.
1159 if (avctx->extra_hw_frames > 0)
1160 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1161
1162 // If frame threading is enabled then an extra surface per thread
1163 // is also required.
1164 if (avctx->active_thread_type & FF_THREAD_FRAME)
1165 frames_ctx->initial_pool_size += avctx->thread_count;
1166 }
1167
1168 *out_frames_ref = frames_ref;
1169 } else {
1170 av_buffer_unref(&frames_ref);
1171 }
1172 return ret;
1173 }
1174
1175 static int hwaccel_init(AVCodecContext *avctx,
1176 const FFHWAccel *hwaccel)
1177 {
1178 int err;
1179
1180 if (hwaccel->p.capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1181 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1182 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1183 hwaccel->p.name);
1184 return AVERROR_PATCHWELCOME;
1185 }
1186
1187 if (!avctx->internal->hwaccel_priv_data && hwaccel->priv_data_size) {
1188 avctx->internal->hwaccel_priv_data =
1189 av_mallocz(hwaccel->priv_data_size);
1190 if (!avctx->internal->hwaccel_priv_data)
1191 return AVERROR(ENOMEM);
1192 }
1193
1194 avctx->hwaccel = &hwaccel->p;
1195 if (hwaccel->init) {
1196 err = hwaccel->init(avctx);
1197 if (err < 0) {
1198 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
1199 "hwaccel initialisation returned error.\n",
1200 av_get_pix_fmt_name(hwaccel->p.pix_fmt));
1201 av_freep(&avctx->internal->hwaccel_priv_data);
1202 avctx->hwaccel = NULL;
1203 return err;
1204 }
1205 }
1206
1207 return 0;
1208 }
1209
1210 36278 void ff_hwaccel_uninit(AVCodecContext *avctx)
1211 {
1212
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 36278 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
36278 if (FF_HW_HAS_CB(avctx, uninit))
1213 FF_HW_SIMPLE_CALL(avctx, uninit);
1214
1215 36278 av_freep(&avctx->internal->hwaccel_priv_data);
1216
1217 36278 avctx->hwaccel = NULL;
1218
1219 36278 av_buffer_unref(&avctx->hw_frames_ctx);
1220 36278 }
1221
1222 2432 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1223 {
1224 const AVPixFmtDescriptor *desc;
1225 enum AVPixelFormat *choices;
1226 enum AVPixelFormat ret, user_choice;
1227 const AVCodecHWConfigInternal *hw_config;
1228 const AVCodecHWConfig *config;
1229 int i, n, err;
1230
1231 // Find end of list.
1232
2/2
✓ Branch 0 taken 6440 times.
✓ Branch 1 taken 2432 times.
8872 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1233 // Must contain at least one entry.
1234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2432 times.
2432 av_assert0(n >= 1);
1235 // If a software format is available, it must be the last entry.
1236 2432 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1237
1/2
✓ Branch 0 taken 2432 times.
✗ Branch 1 not taken.
2432 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1238 // No software format is available.
1239 } else {
1240 2432 avctx->sw_pix_fmt = fmt[n - 1];
1241 }
1242
1243 2432 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1244
1/2
✓ Branch 0 taken 2432 times.
✗ Branch 1 not taken.
2432 if (!choices)
1245 return AV_PIX_FMT_NONE;
1246
1247 for (;;) {
1248 // Remove the previous hwaccel, if there was one.
1249 2432 ff_hwaccel_uninit(avctx);
1250
1251 2432 user_choice = avctx->get_format(avctx, choices);
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2432 times.
2432 if (user_choice == AV_PIX_FMT_NONE) {
1253 // Explicitly chose nothing, give up.
1254 ret = AV_PIX_FMT_NONE;
1255 break;
1256 }
1257
1258 2432 desc = av_pix_fmt_desc_get(user_choice);
1259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2432 times.
2432 if (!desc) {
1260 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
1261 "get_format() callback.\n");
1262 ret = AV_PIX_FMT_NONE;
1263 break;
1264 }
1265 2432 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
1266 2432 desc->name);
1267
1268
1/2
✓ Branch 0 taken 6440 times.
✗ Branch 1 not taken.
6440 for (i = 0; i < n; i++) {
1269
2/2
✓ Branch 0 taken 2432 times.
✓ Branch 1 taken 4008 times.
6440 if (choices[i] == user_choice)
1270 2432 break;
1271 }
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2432 times.
2432 if (i == n) {
1273 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
1274 "%s not in possible list.\n", desc->name);
1275 ret = AV_PIX_FMT_NONE;
1276 break;
1277 }
1278
1279
2/2
✓ Branch 1 taken 2385 times.
✓ Branch 2 taken 47 times.
2432 if (ffcodec(avctx->codec)->hw_configs) {
1280 2385 for (i = 0;; i++) {
1281 6585 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1282
2/2
✓ Branch 0 taken 2385 times.
✓ Branch 1 taken 4200 times.
6585 if (!hw_config)
1283 2385 break;
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4200 times.
4200 if (hw_config->public.pix_fmt == user_choice)
1285 break;
1286 }
1287 } else {
1288 47 hw_config = NULL;
1289 }
1290
1291
1/2
✓ Branch 0 taken 2432 times.
✗ Branch 1 not taken.
2432 if (!hw_config) {
1292 // No config available, so no extra setup required.
1293 2432 ret = user_choice;
1294 2432 break;
1295 }
1296 config = &hw_config->public;
1297
1298 if (config->methods &
1299 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1300 avctx->hw_frames_ctx) {
1301 const AVHWFramesContext *frames_ctx =
1302 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1303 if (frames_ctx->format != user_choice) {
1304 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1305 "does not match the format of the provided frames "
1306 "context.\n", desc->name);
1307 goto try_again;
1308 }
1309 } else if (config->methods &
1310 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1311 avctx->hw_device_ctx) {
1312 const AVHWDeviceContext *device_ctx =
1313 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1314 if (device_ctx->type != config->device_type) {
1315 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1316 "does not match the type of the provided device "
1317 "context.\n", desc->name);
1318 goto try_again;
1319 }
1320 } else if (config->methods &
1321 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1322 // Internal-only setup, no additional configuration.
1323 } else if (config->methods &
1324 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1325 // Some ad-hoc configuration we can't see and can't check.
1326 } else {
1327 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1328 "missing configuration.\n", desc->name);
1329 goto try_again;
1330 }
1331 if (hw_config->hwaccel) {
1332 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel %s "
1333 "initialisation.\n", desc->name, hw_config->hwaccel->p.name);
1334 err = hwaccel_init(avctx, hw_config->hwaccel);
1335 if (err < 0)
1336 goto try_again;
1337 }
1338 ret = user_choice;
1339 break;
1340
1341 try_again:
1342 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
1343 "get_format() without it.\n", desc->name);
1344 for (i = 0; i < n; i++) {
1345 if (choices[i] == user_choice)
1346 break;
1347 }
1348 for (; i + 1 < n; i++)
1349 choices[i] = choices[i + 1];
1350 --n;
1351 }
1352
1353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2432 times.
2432 if (ret < 0)
1354 ff_hwaccel_uninit(avctx);
1355
1356 2432 av_freep(&choices);
1357 2432 return ret;
1358 }
1359
1360 3570680 const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
1361 enum AVPacketSideDataType type)
1362 {
1363
2/2
✓ Branch 0 taken 55905 times.
✓ Branch 1 taken 3568317 times.
3624222 for (int i = 0; i < avctx->nb_coded_side_data; i++)
1364
2/2
✓ Branch 0 taken 2363 times.
✓ Branch 1 taken 53542 times.
55905 if (avctx->coded_side_data[i].type == type)
1365 2363 return &avctx->coded_side_data[i];
1366
1367 3568317 return NULL;
1368 }
1369
1370 396692 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1371 {
1372 size_t size;
1373 const uint8_t *side_metadata;
1374
1375 396692 AVDictionary **frame_md = &frame->metadata;
1376
1377 396692 side_metadata = av_packet_get_side_data(avpkt,
1378 AV_PKT_DATA_STRINGS_METADATA, &size);
1379 396692 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1380 }
1381
1382 396692 int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx,
1383 AVFrame *frame, const AVPacket *pkt)
1384 {
1385 static const struct {
1386 enum AVPacketSideDataType packet;
1387 enum AVFrameSideDataType frame;
1388 } sd[] = {
1389 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1390 { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD },
1391 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1392 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1393 { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES },
1394 };
1395
1396 396692 frame->pts = pkt->pts;
1397 396692 frame->duration = pkt->duration;
1398 #if FF_API_FRAME_PKT
1399 FF_DISABLE_DEPRECATION_WARNINGS
1400 396692 frame->pkt_pos = pkt->pos;
1401 396692 frame->pkt_size = pkt->size;
1402 FF_ENABLE_DEPRECATION_WARNINGS
1403 #endif
1404
1405
2/2
✓ Branch 0 taken 3570228 times.
✓ Branch 1 taken 396692 times.
3966920 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
1406 size_t size;
1407 3570228 const uint8_t *packet_sd = av_packet_get_side_data(pkt, ff_sd_global_map[i].packet, &size);
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3570228 times.
3570228 if (packet_sd) {
1409 AVFrameSideData *frame_sd;
1410
1411 frame_sd = av_frame_new_side_data(frame, ff_sd_global_map[i].frame, size);
1412 if (!frame_sd)
1413 return AVERROR(ENOMEM);
1414 memcpy(frame_sd->data, packet_sd, size);
1415 }
1416 }
1417
2/2
✓ Branch 0 taken 1983460 times.
✓ Branch 1 taken 396692 times.
2380152 for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1418 size_t size;
1419 1983460 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1420
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 1983342 times.
1983460 if (packet_sd) {
1421 118 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1422 118 sd[i].frame,
1423 size);
1424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (!frame_sd)
1425 return AVERROR(ENOMEM);
1426
1427 118 memcpy(frame_sd->data, packet_sd, size);
1428 }
1429 }
1430 396692 add_metadata_from_side_data(pkt, frame);
1431
1432
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 396490 times.
396692 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1433 202 frame->flags |= AV_FRAME_FLAG_DISCARD;
1434 } else {
1435 396490 frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
1436 }
1437
1438
2/2
✓ Branch 0 taken 385222 times.
✓ Branch 1 taken 11470 times.
396692 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
1439 385222 int ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref);
1440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 385222 times.
385222 if (ret < 0)
1441 return ret;
1442 385222 frame->opaque = pkt->opaque;
1443 }
1444
1445 396692 return 0;
1446 }
1447
1448 396692 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1449 {
1450 int ret;
1451
1452
2/2
✓ Branch 0 taken 3570228 times.
✓ Branch 1 taken 396692 times.
3966920 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
1453 3570228 const AVPacketSideData *packet_sd = ff_get_coded_side_data(avctx,
1454 3570228 ff_sd_global_map[i].packet);
1455
2/2
✓ Branch 0 taken 2352 times.
✓ Branch 1 taken 3567876 times.
3570228 if (packet_sd) {
1456 2352 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1457 2352 ff_sd_global_map[i].frame,
1458 2352 packet_sd->size);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2352 times.
2352 if (!frame_sd)
1460 return AVERROR(ENOMEM);
1461
1462 2352 memcpy(frame_sd->data, packet_sd->data, packet_sd->size);
1463 }
1464 }
1465
1466
1/2
✓ Branch 1 taken 396692 times.
✗ Branch 2 not taken.
396692 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
1467 396692 const AVPacket *pkt = avctx->internal->last_pkt_props;
1468
1469 396692 ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt);
1470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396692 times.
396692 if (ret < 0)
1471 return ret;
1472 #if FF_API_FRAME_PKT
1473 FF_DISABLE_DEPRECATION_WARNINGS
1474 396692 frame->pkt_size = pkt->stream_index;
1475 FF_ENABLE_DEPRECATION_WARNINGS
1476 #endif
1477 }
1478
1479 396692 ret = fill_frame_props(avctx, frame);
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396692 times.
396692 if (ret < 0)
1481 return ret;
1482
1483
2/2
✓ Branch 0 taken 139311 times.
✓ Branch 1 taken 257381 times.
396692 switch (avctx->codec->type) {
1484 139311 case AVMEDIA_TYPE_VIDEO:
1485
4/6
✓ Branch 0 taken 115530 times.
✓ Branch 1 taken 23781 times.
✓ Branch 2 taken 115530 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 115530 times.
254841 if (frame->width && frame->height &&
1486 115530 av_image_check_sar(frame->width, frame->height,
1487 frame->sample_aspect_ratio) < 0) {
1488 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1489 frame->sample_aspect_ratio.num,
1490 frame->sample_aspect_ratio.den);
1491 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1492 }
1493 139311 break;
1494 }
1495 396692 return 0;
1496 }
1497
1498 370986 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1499 {
1500
2/2
✓ Branch 0 taken 113605 times.
✓ Branch 1 taken 257381 times.
370986 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1501 int i;
1502 113605 int num_planes = av_pix_fmt_count_planes(frame->format);
1503 113605 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1504
1/2
✓ Branch 0 taken 113605 times.
✗ Branch 1 not taken.
113605 int flags = desc ? desc->flags : 0;
1505
4/4
✓ Branch 0 taken 13242 times.
✓ Branch 1 taken 100363 times.
✓ Branch 2 taken 4048 times.
✓ Branch 3 taken 9194 times.
113605 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1506 4048 num_planes = 2;
1507
2/2
✓ Branch 0 taken 319270 times.
✓ Branch 1 taken 113605 times.
432875 for (i = 0; i < num_planes; i++) {
1508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319270 times.
319270 av_assert0(frame->data[i]);
1509 }
1510 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1511
3/4
✓ Branch 0 taken 703175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 589570 times.
✓ Branch 3 taken 113605 times.
703175 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 589570 times.
589570 if (frame->data[i])
1513 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1514 589570 frame->data[i] = NULL;
1515 }
1516 }
1517 370986 }
1518
1519 370986 static void decode_data_free(void *opaque, uint8_t *data)
1520 {
1521 370986 FrameDecodeData *fdd = (FrameDecodeData*)data;
1522
1523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (fdd->post_process_opaque_free)
1524 fdd->post_process_opaque_free(fdd->post_process_opaque);
1525
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (fdd->hwaccel_priv_free)
1527 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1528
1529 370986 av_freep(&fdd);
1530 370986 }
1531
1532 370986 int ff_attach_decode_data(AVFrame *frame)
1533 {
1534 AVBufferRef *fdd_buf;
1535 FrameDecodeData *fdd;
1536
1537 av_assert1(!frame->private_ref);
1538 370986 av_buffer_unref(&frame->private_ref);
1539
1540 370986 fdd = av_mallocz(sizeof(*fdd));
1541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (!fdd)
1542 return AVERROR(ENOMEM);
1543
1544 370986 fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
1545 NULL, AV_BUFFER_FLAG_READONLY);
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (!fdd_buf) {
1547 av_freep(&fdd);
1548 return AVERROR(ENOMEM);
1549 }
1550
1551 370986 frame->private_ref = fdd_buf;
1552
1553 370986 return 0;
1554 }
1555
1556 370986 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1557 {
1558 370986 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1559 370986 int override_dimensions = 1;
1560 int ret;
1561
1562
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 370986 times.
370986 av_assert0(av_codec_is_decoder(avctx->codec));
1563
1564
2/2
✓ Branch 0 taken 113605 times.
✓ Branch 1 taken 257381 times.
370986 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1565
2/4
✓ Branch 0 taken 113605 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113605 times.
✗ Branch 3 not taken.
227210 if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
1566
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 113605 times.
227210 (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) {
1567 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1568 ret = AVERROR(EINVAL);
1569 goto fail;
1570 }
1571
1572
3/4
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 112874 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 731 times.
113605 if (frame->width <= 0 || frame->height <= 0) {
1573 112874 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1574 112874 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1575 112874 override_dimensions = 0;
1576 }
1577
1578
4/8
✓ Branch 0 taken 113605 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113605 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 113605 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 113605 times.
113605 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1579 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1580 ret = AVERROR(EINVAL);
1581 goto fail;
1582 }
1583
1/2
✓ Branch 0 taken 257381 times.
✗ Branch 1 not taken.
257381 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257381 times.
257381 if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
1585 av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
1586 ret = AVERROR(EINVAL);
1587 goto fail;
1588 }
1589 }
1590 370986 ret = ff_decode_frame_props(avctx, frame);
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (ret < 0)
1592 goto fail;
1593
1594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (hwaccel) {
1595 if (hwaccel->alloc_frame) {
1596 ret = hwaccel->alloc_frame(avctx, frame);
1597 goto end;
1598 }
1599 } else
1600 370986 avctx->sw_pix_fmt = avctx->pix_fmt;
1601
1602 370986 ret = avctx->get_buffer2(avctx, frame, flags);
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (ret < 0)
1604 goto fail;
1605
1606 370986 validate_avframe_allocation(avctx, frame);
1607
1608 370986 ret = ff_attach_decode_data(frame);
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (ret < 0)
1610 goto fail;
1611
1612 370986 end:
1613
4/4
✓ Branch 0 taken 257381 times.
✓ Branch 1 taken 113605 times.
✓ Branch 2 taken 731 times.
✓ Branch 3 taken 112874 times.
370986 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1614
2/2
✓ Branch 1 taken 36125 times.
✓ Branch 2 taken 76749 times.
112874 !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1615 76749 frame->width = avctx->width;
1616 76749 frame->height = avctx->height;
1617 }
1618
1619 294237 fail:
1620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370986 times.
370986 if (ret < 0) {
1621 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1622 av_frame_unref(frame);
1623 }
1624
1625 370986 return ret;
1626 }
1627
1628 7750 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1629 {
1630 AVFrame *tmp;
1631 int ret;
1632
1633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7750 times.
7750 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1634
1635
5/8
✓ Branch 0 taken 7601 times.
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 7601 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7601 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7601 times.
7750 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1636 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1637 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1638 av_frame_unref(frame);
1639 }
1640
1641
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 7601 times.
7750 if (!frame->data[0])
1642 149 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1643
1644
4/4
✓ Branch 0 taken 7600 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1097 times.
✓ Branch 4 taken 6503 times.
7601 if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
1645 1098 return ff_decode_frame_props(avctx, frame);
1646
1647 6503 tmp = av_frame_alloc();
1648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6503 times.
6503 if (!tmp)
1649 return AVERROR(ENOMEM);
1650
1651 6503 av_frame_move_ref(tmp, frame);
1652
1653 6503 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6503 times.
6503 if (ret < 0) {
1655 av_frame_free(&tmp);
1656 return ret;
1657 }
1658
1659 6503 av_frame_copy(frame, tmp);
1660 6503 av_frame_free(&tmp);
1661
1662 6503 return 0;
1663 }
1664
1665 7750 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1666 {
1667 7750 int ret = reget_buffer_internal(avctx, frame, flags);
1668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7750 times.
7750 if (ret < 0)
1669 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1670 7750 return ret;
1671 }
1672
1673 typedef struct ProgressInternal {
1674 ThreadProgress progress;
1675 struct AVFrame *f;
1676 } ProgressInternal;
1677
1678 385878 static void check_progress_consistency(const ProgressFrame *f)
1679 {
1680 av_assert1(!!f->f == !!f->progress);
1681 av_assert1(!f->progress || f->progress->f == f->f);
1682 385878 }
1683
1684 15658 static int progress_frame_get(AVCodecContext *avctx, ProgressFrame *f)
1685 {
1686 15658 FFRefStructPool *pool = avctx->internal->progress_frame_pool;
1687
1688 av_assert1(!f->f && !f->progress);
1689
1690 15658 f->progress = ff_refstruct_pool_get(pool);
1691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15658 times.
15658 if (!f->progress)
1692 return AVERROR(ENOMEM);
1693
1694 15658 f->f = f->progress->f;
1695 15658 return 0;
1696 }
1697
1698 15658 int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
1699 {
1700 int ret;
1701
1702 15658 ret = progress_frame_get(avctx, f);
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15658 times.
15658 if (ret < 0)
1704 return ret;
1705
1706 15658 ret = ff_thread_get_buffer(avctx, f->progress->f, flags);
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15658 times.
15658 if (ret < 0) {
1708 f->f = NULL;
1709 ff_refstruct_unref(&f->progress);
1710 return ret;
1711 }
1712 15658 return 0;
1713 }
1714
1715 38946 void ff_progress_frame_ref(ProgressFrame *dst, const ProgressFrame *src)
1716 {
1717 av_assert1(src->progress && src->f && src->f == src->progress->f);
1718 av_assert1(!dst->f && !dst->progress);
1719 38946 dst->f = src->f;
1720 38946 dst->progress = ff_refstruct_ref(src->progress);
1721 38946 }
1722
1723 345724 void ff_progress_frame_unref(ProgressFrame *f)
1724 {
1725 345724 check_progress_consistency(f);
1726 345724 f->f = NULL;
1727 345724 ff_refstruct_unref(&f->progress);
1728 345724 }
1729
1730 40154 void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
1731 {
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40154 times.
40154 if (dst == src)
1733 return;
1734 40154 ff_progress_frame_unref(dst);
1735 40154 check_progress_consistency(src);
1736
2/2
✓ Branch 0 taken 38879 times.
✓ Branch 1 taken 1275 times.
40154 if (src->f)
1737 38879 ff_progress_frame_ref(dst, src);
1738 }
1739
1740 25254 void ff_progress_frame_report(ProgressFrame *f, int n)
1741 {
1742 25254 ff_thread_progress_report(&f->progress->progress, n);
1743 25254 }
1744
1745 2592454 void ff_progress_frame_await(const ProgressFrame *f, int n)
1746 {
1747 2592454 ff_thread_progress_await(&f->progress->progress, n);
1748 2592454 }
1749
1750 #if !HAVE_THREADS
1751 enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1752 {
1753 return FF_THREAD_NO_FRAME_THREADING;
1754 }
1755 #endif /* !HAVE_THREADS */
1756
1757 2517 static av_cold int progress_frame_pool_init_cb(FFRefStructOpaque opaque, void *obj)
1758 {
1759 2517 const AVCodecContext *avctx = opaque.nc;
1760 2517 ProgressInternal *progress = obj;
1761 int ret;
1762
1763 2517 ret = ff_thread_progress_init(&progress->progress, avctx->active_thread_type & FF_THREAD_FRAME);
1764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2517 times.
2517 if (ret < 0)
1765 return ret;
1766
1767 2517 progress->f = av_frame_alloc();
1768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2517 times.
2517 if (!progress->f)
1769 return AVERROR(ENOMEM);
1770
1771 2517 return 0;
1772 }
1773
1774 15658 static void progress_frame_pool_reset_cb(FFRefStructOpaque unused, void *obj)
1775 {
1776 15658 ProgressInternal *progress = obj;
1777
1778 15658 ff_thread_progress_reset(&progress->progress);
1779 15658 av_frame_unref(progress->f);
1780 15658 }
1781
1782 2517 static av_cold void progress_frame_pool_free_entry_cb(FFRefStructOpaque opaque, void *obj)
1783 {
1784 2517 ProgressInternal *progress = obj;
1785
1786 2517 ff_thread_progress_destroy(&progress->progress);
1787 2517 av_frame_free(&progress->f);
1788 2517 }
1789
1790 14480 int ff_decode_preinit(AVCodecContext *avctx)
1791 {
1792 14480 AVCodecInternal *avci = avctx->internal;
1793 14480 DecodeContext *dc = decode_ctx(avci);
1794 14480 int ret = 0;
1795
1796 /* if the decoder init function was already called previously,
1797 * free the already allocated subtitle_header before overwriting it */
1798 14480 av_freep(&avctx->subtitle_header);
1799
1800
2/4
✓ Branch 0 taken 14480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14480 times.
14480 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1801 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
1802 avctx->codec->max_lowres);
1803 avctx->lowres = avctx->codec->max_lowres;
1804 }
1805
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14474 times.
14480 if (avctx->sub_charenc) {
1806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1807 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1808 "supported with subtitles codecs\n");
1809 return AVERROR(EINVAL);
1810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1811 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1812 "subtitles character encoding will be ignored\n",
1813 avctx->codec_descriptor->name);
1814 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1815 } else {
1816 /* input character encoding is set for a text based subtitle
1817 * codec at this point */
1818
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1819 6 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1820
1821
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1822 #if CONFIG_ICONV
1823 6 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cd == (iconv_t)-1) {
1825 ret = AVERROR(errno);
1826 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1827 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1828 return ret;
1829 }
1830 6 iconv_close(cd);
1831 #else
1832 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1833 "conversion needs a libavcodec built with iconv support "
1834 "for this codec\n");
1835 return AVERROR(ENOSYS);
1836 #endif
1837 }
1838 }
1839 }
1840
1841 14480 dc->pts_correction_num_faulty_pts =
1842 14480 dc->pts_correction_num_faulty_dts = 0;
1843 14480 dc->pts_correction_last_pts =
1844 14480 dc->pts_correction_last_dts = INT64_MIN;
1845
1846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1847 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1848 av_log(avctx, AV_LOG_WARNING,
1849 "gray decoding requested but not enabled at configuration time\n");
1850
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14475 times.
14480 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
1851 5 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
1852 }
1853
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (avctx->nb_side_data_prefer_packet == 1 &&
1855 avctx->side_data_prefer_packet[0] == -1)
1856 dc->side_data_pref_mask = ~0ULL;
1857 else {
1858
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14480 times.
14484 for (unsigned i = 0; i < avctx->nb_side_data_prefer_packet; i++) {
1859 4 int val = avctx->side_data_prefer_packet[i];
1860
1861
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (val < 0 || val >= AV_PKT_DATA_NB) {
1862 av_log(avctx, AV_LOG_ERROR, "Invalid side data type: %d\n", val);
1863 return AVERROR(EINVAL);
1864 }
1865
1866
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
40 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
1867
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 if (ff_sd_global_map[j].packet == val) {
1868 4 val = ff_sd_global_map[j].frame;
1869
1870 // this code will need to be changed when we have more than
1871 // 64 frame side data types
1872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (val >= 64) {
1873 av_log(avctx, AV_LOG_ERROR, "Side data type too big\n");
1874 return AVERROR_BUG;
1875 }
1876
1877 4 dc->side_data_pref_mask |= 1ULL << val;
1878 }
1879 }
1880 }
1881 }
1882
1883 14480 avci->in_pkt = av_packet_alloc();
1884 14480 avci->last_pkt_props = av_packet_alloc();
1885
2/4
✓ Branch 0 taken 14480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14480 times.
14480 if (!avci->in_pkt || !avci->last_pkt_props)
1886 return AVERROR(ENOMEM);
1887
1888
2/2
✓ Branch 1 taken 1270 times.
✓ Branch 2 taken 13210 times.
14480 if (ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_USES_PROGRESSFRAMES) {
1889 1270 avci->progress_frame_pool =
1890 1270 ff_refstruct_pool_alloc_ext(sizeof(ProgressInternal),
1891 FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
1892 avctx, progress_frame_pool_init_cb,
1893 progress_frame_pool_reset_cb,
1894 progress_frame_pool_free_entry_cb, NULL);
1895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1270 times.
1270 if (!avci->progress_frame_pool)
1896 return AVERROR(ENOMEM);
1897 }
1898 14480 ret = decode_bsfs_init(avctx);
1899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (ret < 0)
1900 return ret;
1901
1902 #if FF_API_DROPCHANGED
1903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14480 times.
14480 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED)
1904 av_log(avctx, AV_LOG_WARNING, "The dropchanged flag is deprecated.\n");
1905 #endif
1906
1907 14480 return 0;
1908 }
1909
1910 /**
1911 * Check side data preference and clear existing side data from frame
1912 * if needed.
1913 *
1914 * @retval 0 side data of this type can be added to frame
1915 * @retval 1 side data of this type should not be added to frame
1916 */
1917 5588 static int side_data_pref(const AVCodecContext *avctx, AVFrameSideData ***sd,
1918 int *nb_sd, enum AVFrameSideDataType type)
1919 {
1920 5588 DecodeContext *dc = decode_ctx(avctx->internal);
1921
1922 // Note: could be skipped for `type` without corresponding packet sd
1923
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 5568 times.
5588 if (av_frame_side_data_get(*sd, *nb_sd, type)) {
1924
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (dc->side_data_pref_mask & (1ULL << type))
1925 10 return 1;
1926 10 av_frame_side_data_remove(sd, nb_sd, type);
1927 }
1928
1929 5578 return 0;
1930 }
1931
1932
1933 4799 int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame,
1934 enum AVFrameSideDataType type, size_t size,
1935 AVFrameSideData **psd)
1936 {
1937 AVFrameSideData *sd;
1938
1939
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4799 times.
4799 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data, type)) {
1940 if (psd)
1941 *psd = NULL;
1942 return 0;
1943 }
1944
1945 4799 sd = av_frame_new_side_data(frame, type, size);
1946
1/2
✓ Branch 0 taken 4799 times.
✗ Branch 1 not taken.
4799 if (psd)
1947 4799 *psd = sd;
1948
1949
1/2
✓ Branch 0 taken 4799 times.
✗ Branch 1 not taken.
4799 return sd ? 0 : AVERROR(ENOMEM);
1950 }
1951
1952 754 int ff_frame_new_side_data_from_buf_ext(const AVCodecContext *avctx,
1953 AVFrameSideData ***sd, int *nb_sd,
1954 enum AVFrameSideDataType type,
1955 AVBufferRef **buf)
1956 {
1957 754 int ret = 0;
1958
1959
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 754 times.
754 if (side_data_pref(avctx, sd, nb_sd, type))
1960 goto finish;
1961
1962
1/2
✓ Branch 1 taken 754 times.
✗ Branch 2 not taken.
754 if (!av_frame_side_data_add(sd, nb_sd, type, buf, 0))
1963 ret = AVERROR(ENOMEM);
1964
1965 754 finish:
1966 754 av_buffer_unref(buf);
1967
1968 754 return ret;
1969 }
1970
1971 744 int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx,
1972 AVFrame *frame, enum AVFrameSideDataType type,
1973 AVBufferRef **buf, AVFrameSideData **psd)
1974 {
1975 744 return ff_frame_new_side_data_from_buf_ext(avctx,
1976 &frame->side_data, &frame->nb_side_data,
1977 type, buf);
1978 }
1979
1980 31 int ff_decode_mastering_display_new_ext(const AVCodecContext *avctx,
1981 AVFrameSideData ***sd, int *nb_sd,
1982 struct AVMasteringDisplayMetadata **mdm)
1983 {
1984 AVBufferRef *buf;
1985 size_t size;
1986
1987
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21 times.
31 if (side_data_pref(avctx, sd, nb_sd, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
1988 10 *mdm = NULL;
1989 10 return 0;
1990 }
1991
1992 21 *mdm = av_mastering_display_metadata_alloc_size(&size);
1993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!*mdm)
1994 return AVERROR(ENOMEM);
1995
1996 21 buf = av_buffer_create((uint8_t *)*mdm, size, NULL, NULL, 0);
1997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!buf) {
1998 av_freep(mdm);
1999 return AVERROR(ENOMEM);
2000 }
2001
2002
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (!av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA,
2003 &buf, 0)) {
2004 *mdm = NULL;
2005 av_buffer_unref(&buf);
2006 return AVERROR(ENOMEM);
2007 }
2008
2009 21 return 0;
2010 }
2011
2012 int ff_decode_mastering_display_new(const AVCodecContext *avctx, AVFrame *frame,
2013 AVMasteringDisplayMetadata **mdm)
2014 {
2015 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2016 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
2017 *mdm = NULL;
2018 return 0;
2019 }
2020
2021 *mdm = av_mastering_display_metadata_create_side_data(frame);
2022 return *mdm ? 0 : AVERROR(ENOMEM);
2023 }
2024
2025 4 int ff_decode_content_light_new_ext(const AVCodecContext *avctx,
2026 AVFrameSideData ***sd, int *nb_sd,
2027 AVContentLightMetadata **clm)
2028 {
2029 AVBufferRef *buf;
2030 size_t size;
2031
2032
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (side_data_pref(avctx, sd, nb_sd, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2033 *clm = NULL;
2034 return 0;
2035 }
2036
2037 4 *clm = av_content_light_metadata_alloc(&size);
2038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!*clm)
2039 return AVERROR(ENOMEM);
2040
2041 4 buf = av_buffer_create((uint8_t *)*clm, size, NULL, NULL, 0);
2042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!buf) {
2043 av_freep(clm);
2044 return AVERROR(ENOMEM);
2045 }
2046
2047
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL,
2048 &buf, 0)) {
2049 *clm = NULL;
2050 av_buffer_unref(&buf);
2051 return AVERROR(ENOMEM);
2052 }
2053
2054 4 return 0;
2055 }
2056
2057 int ff_decode_content_light_new(const AVCodecContext *avctx, AVFrame *frame,
2058 AVContentLightMetadata **clm)
2059 {
2060 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2061 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2062 *clm = NULL;
2063 return 0;
2064 }
2065
2066 *clm = av_content_light_metadata_create_side_data(frame);
2067 return *clm ? 0 : AVERROR(ENOMEM);
2068 }
2069
2070 1673 int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
2071 {
2072 size_t size;
2073 1673 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
2074
2075
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1641 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
1673 if (pal && size == AVPALETTE_SIZE) {
2076 32 memcpy(dst, pal, AVPALETTE_SIZE);
2077 32 return 1;
2078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1641 times.
1641 } else if (pal) {
2079 av_log(logctx, AV_LOG_ERROR,
2080 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
2081 }
2082 1641 return 0;
2083 }
2084
2085 49891 int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
2086 {
2087 49891 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
2088
2089
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49891 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49891 if (!hwaccel || !hwaccel->frame_priv_data_size)
2090 49891 return 0;
2091
2092 av_assert0(!*hwaccel_picture_private);
2093
2094 if (hwaccel->free_frame_priv) {
2095 AVHWFramesContext *frames_ctx;
2096
2097 if (!avctx->hw_frames_ctx)
2098 return AVERROR(EINVAL);
2099
2100 frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
2101 *hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
2102 frames_ctx->device_ctx,
2103 hwaccel->free_frame_priv);
2104 } else {
2105 *hwaccel_picture_private = ff_refstruct_allocz(hwaccel->frame_priv_data_size);
2106 }
2107
2108 if (!*hwaccel_picture_private)
2109 return AVERROR(ENOMEM);
2110
2111 return 0;
2112 }
2113
2114 102 void ff_decode_flush_buffers(AVCodecContext *avctx)
2115 {
2116 102 AVCodecInternal *avci = avctx->internal;
2117 102 DecodeContext *dc = decode_ctx(avci);
2118
2119 102 av_packet_unref(avci->last_pkt_props);
2120 102 av_packet_unref(avci->in_pkt);
2121
2122 102 dc->pts_correction_last_pts =
2123 102 dc->pts_correction_last_dts = INT64_MIN;
2124
2125 102 av_bsf_flush(avci->bsf);
2126
2127 102 dc->nb_draining_errors = 0;
2128 102 dc->draining_started = 0;
2129 102 }
2130
2131 14608 AVCodecInternal *ff_decode_internal_alloc(void)
2132 {
2133 14608 return av_mallocz(sizeof(DecodeContext));
2134 }
2135