FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 566 920 61.5%
Branches: 422 751 56.2%

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/avstring.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/common.h"
35 #include "libavutil/fifo.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/hwcontext.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/internal.h"
40 #include "libavutil/intmath.h"
41 #include "libavutil/opt.h"
42
43 #include "avcodec.h"
44 #include "bytestream.h"
45 #include "bsf.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 #include "hwconfig.h"
49 #include "internal.h"
50 #include "thread.h"
51
52 383435 static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
53 {
54 int ret;
55 size_t size;
56 const uint8_t *data;
57 uint32_t flags;
58 int64_t val;
59
60 383435 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
61
2/2
✓ Branch 0 taken 383433 times.
✓ Branch 1 taken 2 times.
383435 if (!data)
62 383433 return 0;
63
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
65 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
66 "changes, but PARAM_CHANGE side data was sent to it.\n");
67 ret = AVERROR(EINVAL);
68 goto fail2;
69 }
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 4)
72 goto fail;
73
74 2 flags = bytestream_get_le32(&data);
75 2 size -= 4;
76
77 #if FF_API_OLD_CHANNEL_LAYOUT
78 FF_DISABLE_DEPRECATION_WARNINGS
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
80 if (size < 4)
81 goto fail;
82 val = bytestream_get_le32(&data);
83 if (val <= 0 || val > INT_MAX) {
84 av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
85 ret = AVERROR_INVALIDDATA;
86 goto fail2;
87 }
88 avctx->channels = val;
89 size -= 4;
90 }
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
92 if (size < 8)
93 goto fail;
94 avctx->channel_layout = bytestream_get_le64(&data);
95 size -= 8;
96 }
97 FF_ENABLE_DEPRECATION_WARNINGS
98 #endif
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
100 if (size < 4)
101 goto fail;
102 val = bytestream_get_le32(&data);
103 if (val <= 0 || val > INT_MAX) {
104 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
105 ret = AVERROR_INVALIDDATA;
106 goto fail2;
107 }
108 avctx->sample_rate = val;
109 size -= 4;
110 }
111
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 8)
113 goto fail;
114 2 avctx->width = bytestream_get_le32(&data);
115 2 avctx->height = bytestream_get_le32(&data);
116 2 size -= 8;
117 2 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
119 goto fail2;
120 }
121
122 2 return 0;
123 fail:
124 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
125 ret = AVERROR_INVALIDDATA;
126 fail2:
127 if (ret < 0) {
128 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
129 if (avctx->err_recognition & AV_EF_EXPLODE)
130 return ret;
131 }
132 return 0;
133 }
134
135 #define IS_EMPTY(pkt) (!(pkt)->data)
136
137 383435 static int copy_packet_props(AVPacket *dst, const AVPacket *src)
138 {
139 383435 int ret = av_packet_copy_props(dst, src);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383435 times.
383435 if (ret < 0)
141 return ret;
142
143 383435 dst->size = src->size; // HACK: Needed for ff_decode_frame_props().
144 383435 dst->data = (void*)1; // HACK: Needed for IS_EMPTY().
145
146 383435 return 0;
147 }
148
149 383435 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
150 {
151 383435 AVPacket tmp = { 0 };
152 383435 int ret = 0;
153
154
1/2
✓ Branch 0 taken 383435 times.
✗ Branch 1 not taken.
383435 if (IS_EMPTY(avci->last_pkt_props)) {
155
1/2
✓ Branch 1 taken 383435 times.
✗ Branch 2 not taken.
383435 if (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) < 0)
156 383435 return copy_packet_props(avci->last_pkt_props, pkt);
157 }
158
159 ret = copy_packet_props(&tmp, pkt);
160 if (ret < 0)
161 return ret;
162
163 ret = av_fifo_write(avci->pkt_props, &tmp, 1);
164 if (ret < 0)
165 av_packet_unref(&tmp);
166
167 return ret;
168 }
169
170 13383 static int decode_bsfs_init(AVCodecContext *avctx)
171 {
172 13383 AVCodecInternal *avci = avctx->internal;
173 13383 const FFCodec *const codec = ffcodec(avctx->codec);
174 int ret;
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 if (avci->bsf)
177 return 0;
178
179 13383 ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 if (ret < 0) {
181 av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret));
182 if (ret != AVERROR(ENOMEM))
183 ret = AVERROR_BUG;
184 goto fail;
185 }
186
187 /* We do not currently have an API for passing the input timebase into decoders,
188 * but no filters used here should actually need it.
189 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
190 13383 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
191 13383 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 if (ret < 0)
193 goto fail;
194
195 13383 ret = av_bsf_init(avci->bsf);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 if (ret < 0)
197 goto fail;
198
199 13383 return 0;
200 fail:
201 av_bsf_free(&avci->bsf);
202 return ret;
203 }
204
205 769690 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
206 {
207 769690 AVCodecInternal *avci = avctx->internal;
208 int ret;
209
210
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 769626 times.
769690 if (avci->draining)
211 64 return AVERROR_EOF;
212
213 769626 ret = av_bsf_receive_packet(avci->bsf, pkt);
214
2/2
✓ Branch 0 taken 6101 times.
✓ Branch 1 taken 763525 times.
769626 if (ret == AVERROR_EOF)
215 6101 avci->draining = 1;
216
2/2
✓ Branch 0 taken 386191 times.
✓ Branch 1 taken 383435 times.
769626 if (ret < 0)
217 386191 return ret;
218
219
1/2
✓ Branch 1 taken 383435 times.
✗ Branch 2 not taken.
383435 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
220 383435 ret = extract_packet_props(avctx->internal, pkt);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383435 times.
383435 if (ret < 0)
222 goto finish;
223 }
224
225 383435 ret = apply_param_change(avctx, pkt);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383435 times.
383435 if (ret < 0)
227 goto finish;
228
229 383435 return 0;
230 finish:
231 av_packet_unref(pkt);
232 return ret;
233 }
234
235 /**
236 * Attempt to guess proper monotonic timestamps for decoded video frames
237 * which might have incorrect times. Input timestamps may wrap around, in
238 * which case the output will as well.
239 *
240 * @param pts the pts field of the decoded AVPacket, as passed through
241 * AVFrame.pts
242 * @param dts the dts field of the decoded AVPacket
243 * @return one of the input values, may be AV_NOPTS_VALUE
244 */
245 404058 static int64_t guess_correct_pts(AVCodecContext *ctx,
246 int64_t reordered_pts, int64_t dts)
247 {
248 404058 int64_t pts = AV_NOPTS_VALUE;
249
250
2/2
✓ Branch 0 taken 369210 times.
✓ Branch 1 taken 34848 times.
404058 if (dts != AV_NOPTS_VALUE) {
251 369210 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
252 369210 ctx->pts_correction_last_dts = dts;
253
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 34535 times.
34848 } else if (reordered_pts != AV_NOPTS_VALUE)
254 313 ctx->pts_correction_last_dts = reordered_pts;
255
256
2/2
✓ Branch 0 taken 340593 times.
✓ Branch 1 taken 63465 times.
404058 if (reordered_pts != AV_NOPTS_VALUE) {
257 340593 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
258 340593 ctx->pts_correction_last_pts = reordered_pts;
259
2/2
✓ Branch 0 taken 28930 times.
✓ Branch 1 taken 34535 times.
63465 } else if(dts != AV_NOPTS_VALUE)
260 28930 ctx->pts_correction_last_pts = dts;
261
262
4/4
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 403497 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 556 times.
404058 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
263
2/2
✓ Branch 0 taken 340037 times.
✓ Branch 1 taken 63465 times.
403502 && reordered_pts != AV_NOPTS_VALUE)
264 340037 pts = reordered_pts;
265 else
266 64021 pts = dts;
267
268 404058 return pts;
269 }
270
271 /*
272 * The core of the receive_frame_wrapper for the decoders implementing
273 * the simple API. Certain decoders might consume partial packets without
274 * returning any output, so this function needs to be called in a loop until it
275 * returns EAGAIN.
276 **/
277 800287 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
278 {
279 800287 AVCodecInternal *avci = avctx->internal;
280 800287 AVPacket *const pkt = avci->in_pkt;
281 800287 const FFCodec *const codec = ffcodec(avctx->codec);
282 int got_frame, actual_got_frame;
283 int ret;
284
285
4/4
✓ Branch 0 taken 771627 times.
✓ Branch 1 taken 28660 times.
✓ Branch 2 taken 764557 times.
✓ Branch 3 taken 7070 times.
800287 if (!pkt->data && !avci->draining) {
286 764557 av_packet_unref(pkt);
287 764557 ret = ff_decode_get_packet(avctx, pkt);
288
4/4
✓ Branch 0 taken 383771 times.
✓ Branch 1 taken 380786 times.
✓ Branch 2 taken 377734 times.
✓ Branch 3 taken 6037 times.
764557 if (ret < 0 && ret != AVERROR_EOF)
289 377734 return ret;
290 }
291
292 // Some codecs (at least wma lossless) will crash when feeding drain packets
293 // after EOF was signaled.
294
2/2
✓ Branch 0 taken 6341 times.
✓ Branch 1 taken 416212 times.
422553 if (avci->draining_done)
295 6341 return AVERROR_EOF;
296
297
2/2
✓ Branch 0 taken 6766 times.
✓ Branch 1 taken 409446 times.
416212 if (!pkt->data &&
298
2/2
✓ Branch 0 taken 5393 times.
✓ Branch 1 taken 1373 times.
6766 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
299
2/2
✓ Branch 0 taken 5352 times.
✓ Branch 1 taken 41 times.
5393 avctx->active_thread_type & FF_THREAD_FRAME))
300 5352 return AVERROR_EOF;
301
302 410860 got_frame = 0;
303
304
2/2
✓ Branch 0 taken 570 times.
✓ Branch 1 taken 410290 times.
410860 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
305 570 ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
306 } else {
307 410290 ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
308
309
2/2
✓ Branch 0 taken 409960 times.
✓ Branch 1 taken 330 times.
410290 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
310 409960 frame->pkt_dts = pkt->dts;
311
2/2
✓ Branch 0 taken 115687 times.
✓ Branch 1 taken 294603 times.
410290 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
312
2/2
✓ Branch 0 taken 83351 times.
✓ Branch 1 taken 32336 times.
115687 if(!avctx->has_b_frames)
313 83351 frame->pkt_pos = pkt->pos;
314 //FIXME these should be under if(!avctx->has_b_frames)
315 /* get_buffer is supposed to set frame parameters */
316
2/2
✓ Branch 0 taken 21906 times.
✓ Branch 1 taken 93781 times.
115687 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
317
2/2
✓ Branch 0 taken 21350 times.
✓ Branch 1 taken 556 times.
21906 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
318
2/2
✓ Branch 0 taken 21900 times.
✓ Branch 1 taken 6 times.
21906 if (!frame->width) frame->width = avctx->width;
319
2/2
✓ Branch 0 taken 21900 times.
✓ Branch 1 taken 6 times.
21906 if (!frame->height) frame->height = avctx->height;
320
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21904 times.
21906 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
321 }
322 }
323 }
324 410860 emms_c();
325 410860 actual_got_frame = got_frame;
326
327
2/2
✓ Branch 0 taken 115785 times.
✓ Branch 1 taken 295075 times.
410860 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
328
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 115605 times.
115785 if (frame->flags & AV_FRAME_FLAG_DISCARD)
329 180 got_frame = 0;
330
1/2
✓ Branch 0 taken 295075 times.
✗ Branch 1 not taken.
295075 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
331 uint8_t *side;
332 size_t side_size;
333 295075 uint32_t discard_padding = 0;
334 295075 uint8_t skip_reason = 0;
335 295075 uint8_t discard_reason = 0;
336
337
4/4
✓ Branch 0 taken 295058 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 293629 times.
✓ Branch 3 taken 1429 times.
295075 if (ret >= 0 && got_frame) {
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293629 times.
293629 if (frame->format == AV_SAMPLE_FMT_NONE)
339 frame->format = avctx->sample_fmt;
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293629 times.
293629 if (!frame->ch_layout.nb_channels) {
341 int ret2 = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
342 if (ret2 < 0) {
343 ret = ret2;
344 got_frame = 0;
345 }
346 }
347 #if FF_API_OLD_CHANNEL_LAYOUT
348 FF_DISABLE_DEPRECATION_WARNINGS
349
2/2
✓ Branch 0 taken 1113 times.
✓ Branch 1 taken 292516 times.
293629 if (!frame->channel_layout)
350 1113 frame->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1113 times.
1113 avctx->ch_layout.u.mask : 0;
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293629 times.
293629 if (!frame->channels)
353 frame->channels = avctx->ch_layout.nb_channels;
354 FF_ENABLE_DEPRECATION_WARNINGS
355 #endif
356
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 293626 times.
293629 if (!frame->sample_rate)
357 3 frame->sample_rate = avctx->sample_rate;
358 }
359
360 295075 side= av_packet_get_side_data(avci->last_pkt_props, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
361
3/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 294965 times.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
295075 if(side && side_size>=10) {
362 110 avci->skip_samples = AV_RL32(side) * avci->skip_samples_multiplier;
363 110 avci->skip_samples = FFMAX(0, avci->skip_samples);
364 110 discard_padding = AV_RL32(side + 4);
365 110 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
366 avci->skip_samples, (int)discard_padding);
367 110 skip_reason = AV_RL8(side + 8);
368 110 discard_reason = AV_RL8(side + 9);
369 }
370
371
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 295058 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
295075 if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame &&
372
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
373 17 avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
374 17 got_frame = 0;
375 17 *discarded_samples += frame->nb_samples;
376 }
377
378
4/4
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 294903 times.
✓ Branch 2 taken 165 times.
✓ Branch 3 taken 7 times.
295075 if (avci->skip_samples > 0 && got_frame &&
379
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
380
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 56 times.
165 if(frame->nb_samples <= avci->skip_samples){
381 109 got_frame = 0;
382 109 *discarded_samples += frame->nb_samples;
383 109 avci->skip_samples -= frame->nb_samples;
384 109 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
385 avci->skip_samples);
386 } else {
387 56 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
388 56 frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
389
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
112 if(avctx->pkt_timebase.num && avctx->sample_rate) {
390 56 int64_t diff_ts = av_rescale_q(avci->skip_samples,
391 56 (AVRational){1, avctx->sample_rate},
392 avctx->pkt_timebase);
393
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if(frame->pts!=AV_NOPTS_VALUE)
394 56 frame->pts += diff_ts;
395
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if(frame->pkt_dts!=AV_NOPTS_VALUE)
396 56 frame->pkt_dts += diff_ts;
397
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (frame->pkt_duration >= diff_ts)
398 56 frame->pkt_duration -= diff_ts;
399 } else {
400 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
401 }
402 56 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
403 avci->skip_samples, frame->nb_samples);
404 56 *discarded_samples += avci->skip_samples;
405 56 frame->nb_samples -= avci->skip_samples;
406 56 avci->skip_samples = 0;
407 }
408 }
409
410
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 295068 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
295075 if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame &&
411
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
412
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (discard_padding == frame->nb_samples) {
413 2 *discarded_samples += frame->nb_samples;
414 2 got_frame = 0;
415 } else {
416
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) {
417 5 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
418 5 (AVRational){1, avctx->sample_rate},
419 avctx->pkt_timebase);
420 5 frame->pkt_duration = diff_ts;
421 } else {
422 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
423 }
424 5 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
425 (int)discard_padding, frame->nb_samples);
426 5 frame->nb_samples -= discard_padding;
427 }
428 }
429
430
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 295075 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
295075 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) {
431 AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
432 if (fside) {
433 AV_WL32(fside->data, avci->skip_samples);
434 AV_WL32(fside->data + 4, discard_padding);
435 AV_WL8(fside->data + 8, skip_reason);
436 AV_WL8(fside->data + 9, discard_reason);
437 avci->skip_samples = 0;
438 }
439 }
440 }
441
442
2/2
✓ Branch 0 taken 295075 times.
✓ Branch 1 taken 115785 times.
410860 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO &&
443
4/4
✓ Branch 0 taken 269752 times.
✓ Branch 1 taken 25323 times.
✓ Branch 2 taken 269737 times.
✓ Branch 3 taken 15 times.
295075 !avci->showed_multi_packet_warning &&
444
4/4
✓ Branch 0 taken 5265 times.
✓ Branch 1 taken 264472 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 5249 times.
269737 ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
445 16 av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
446 16 avci->showed_multi_packet_warning = 1;
447 }
448
449
2/2
✓ Branch 0 taken 9465 times.
✓ Branch 1 taken 401395 times.
410860 if (!got_frame)
450 9465 av_frame_unref(frame);
451
452 #if FF_API_FLAG_TRUNCATED
453
5/6
✓ Branch 0 taken 409863 times.
✓ Branch 1 taken 997 times.
✓ Branch 2 taken 114805 times.
✓ Branch 3 taken 295058 times.
✓ Branch 4 taken 114805 times.
✗ Branch 5 not taken.
410860 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED))
454 #else
455 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
456 #endif
457 114805 ret = pkt->size;
458
459 #if FF_API_AVCTX_TIMEBASE
460
3/4
✓ Branch 0 taken 84473 times.
✓ Branch 1 taken 326387 times.
✓ Branch 2 taken 84473 times.
✗ Branch 3 not taken.
410860 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
461 84473 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
462 #endif
463
464 /* do not stop draining when actual_got_frame != 0 or ret < 0 */
465 /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
466
4/4
✓ Branch 0 taken 1414 times.
✓ Branch 1 taken 409446 times.
✓ Branch 2 taken 671 times.
✓ Branch 3 taken 743 times.
410860 if (avci->draining && !actual_got_frame) {
467
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 670 times.
671 if (ret < 0) {
468 /* prevent infinite loop if a decoder wrongly always return error on draining */
469 /* reasonable nb_errors_max = maximum b frames + thread count */
470
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 ?
471 avctx->thread_count : 1);
472
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avci->nb_draining_errors++ >= nb_errors_max) {
474 av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
475 "Stop draining and force EOF.\n");
476 avci->draining_done = 1;
477 ret = AVERROR_BUG;
478 }
479 } else {
480 670 avci->draining_done = 1;
481 }
482 }
483
484
4/4
✓ Branch 0 taken 29666 times.
✓ Branch 1 taken 381194 times.
✓ Branch 2 taken 997 times.
✓ Branch 3 taken 28669 times.
410860 if (ret >= pkt->size || ret < 0) {
485 382191 av_packet_unref(pkt);
486 382191 av_packet_unref(avci->last_pkt_props);
487 } else {
488 28669 int consumed = ret;
489
490 28669 pkt->data += consumed;
491 28669 pkt->size -= consumed;
492 28669 pkt->pts = AV_NOPTS_VALUE;
493 28669 pkt->dts = AV_NOPTS_VALUE;
494
1/2
✓ Branch 0 taken 28669 times.
✗ Branch 1 not taken.
28669 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
495 28669 avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
496 28669 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
497 28669 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
498 }
499 }
500
501
2/2
✓ Branch 0 taken 401395 times.
✓ Branch 1 taken 9465 times.
410860 if (got_frame)
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 401395 times.
401395 av_assert0(frame->buf[0]);
503
504 410860 return ret < 0 ? ret : 0;
505 }
506
507 791819 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
508 {
509 int ret;
510 791819 int64_t discarded_samples = 0;
511
512
2/2
✓ Branch 0 taken 800287 times.
✓ Branch 1 taken 401395 times.
1201682 while (!frame->buf[0]) {
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800287 times.
800287 if (discarded_samples > avctx->max_samples)
514 return AVERROR(EAGAIN);
515 800287 ret = decode_simple_internal(avctx, frame, &discarded_samples);
516
2/2
✓ Branch 0 taken 390424 times.
✓ Branch 1 taken 409863 times.
800287 if (ret < 0)
517 390424 return ret;
518 }
519
520 401395 return 0;
521 }
522
523 797042 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
524 {
525 797042 AVCodecInternal *avci = avctx->internal;
526 797042 const FFCodec *const codec = ffcodec(avctx->codec);
527 int ret;
528
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 797042 times.
797042 av_assert0(!frame->buf[0]);
530
531
2/2
✓ Branch 0 taken 5223 times.
✓ Branch 1 taken 791819 times.
797042 if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) {
532 5223 ret = codec->cb.receive_frame(avctx, frame);
533
2/2
✓ Branch 0 taken 2792 times.
✓ Branch 1 taken 2431 times.
5223 if (ret != AVERROR(EAGAIN))
534 2792 av_packet_unref(avci->last_pkt_props);
535 } else
536 791819 ret = decode_simple_receive_frame(avctx, frame);
537
538
2/2
✓ Branch 0 taken 11821 times.
✓ Branch 1 taken 785221 times.
797042 if (ret == AVERROR_EOF)
539 11821 avci->draining_done = 1;
540
541
1/2
✓ Branch 0 taken 797042 times.
✗ Branch 1 not taken.
797042 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS) &&
542
2/2
✓ Branch 0 taken 768267 times.
✓ Branch 1 taken 28775 times.
797042 IS_EMPTY(avci->last_pkt_props)) {
543 // May fail if the FIFO is empty.
544 768267 av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
545 }
546
547
2/2
✓ Branch 0 taken 404058 times.
✓ Branch 1 taken 392984 times.
797042 if (!ret) {
548 404058 frame->best_effort_timestamp = guess_correct_pts(avctx,
549 frame->pts,
550 frame->pkt_dts);
551
552 /* the only case where decode data is not set should be decoders
553 * that do not call ff_get_buffer() */
554
4/6
✓ Branch 0 taken 382160 times.
✓ Branch 1 taken 21898 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 382160 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 21898 times.
404058 av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
555 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
556
557
2/2
✓ Branch 0 taken 382160 times.
✓ Branch 1 taken 21898 times.
404058 if (frame->private_ref) {
558 382160 FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
559
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382160 times.
382160 if (fdd->post_process) {
561 ret = fdd->post_process(avctx, frame);
562 if (ret < 0) {
563 av_frame_unref(frame);
564 return ret;
565 }
566 }
567 }
568 }
569
570 /* free the per-frame decode data */
571 797042 av_buffer_unref(&frame->private_ref);
572
573 797042 return ret;
574 }
575
576 390151 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
577 {
578 390151 AVCodecInternal *avci = avctx->internal;
579 int ret;
580
581
2/4
✓ Branch 1 taken 390151 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 390151 times.
390151 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
582 return AVERROR(EINVAL);
583
584
2/2
✓ Branch 0 taken 679 times.
✓ Branch 1 taken 389472 times.
390151 if (avctx->internal->draining)
585 679 return AVERROR_EOF;
586
587
5/6
✓ Branch 0 taken 389455 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 6084 times.
✓ Branch 3 taken 383371 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6084 times.
389472 if (avpkt && !avpkt->size && avpkt->data)
588 return AVERROR(EINVAL);
589
590 389472 av_packet_unref(avci->buffer_pkt);
591
5/6
✓ Branch 0 taken 389455 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 6084 times.
✓ Branch 3 taken 383371 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6084 times.
389472 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
592 383371 ret = av_packet_ref(avci->buffer_pkt, avpkt);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383371 times.
383371 if (ret < 0)
594 return ret;
595 }
596
597 389472 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
598
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 389405 times.
389472 if (ret < 0) {
599 67 av_packet_unref(avci->buffer_pkt);
600 67 return ret;
601 }
602
603
1/2
✓ Branch 0 taken 389405 times.
✗ Branch 1 not taken.
389405 if (!avci->buffer_frame->buf[0]) {
604 389405 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
605
6/6
✓ Branch 0 taken 13228 times.
✓ Branch 1 taken 376177 times.
✓ Branch 2 taken 6659 times.
✓ Branch 3 taken 6569 times.
✓ Branch 4 taken 926 times.
✓ Branch 5 taken 5733 times.
389405 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
606 926 return ret;
607 }
608
609 388479 return 0;
610 }
611
612 110385 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
613 {
614 /* make sure we are noisy about decoders returning invalid cropping data */
615
1/2
✓ Branch 0 taken 110385 times.
✗ Branch 1 not taken.
110385 if (frame->crop_left >= INT_MAX - frame->crop_right ||
616
1/2
✓ Branch 0 taken 110385 times.
✗ Branch 1 not taken.
110385 frame->crop_top >= INT_MAX - frame->crop_bottom ||
617
1/2
✓ Branch 0 taken 110385 times.
✗ Branch 1 not taken.
110385 (frame->crop_left + frame->crop_right) >= frame->width ||
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110385 times.
110385 (frame->crop_top + frame->crop_bottom) >= frame->height) {
619 av_log(avctx, AV_LOG_WARNING,
620 "Invalid cropping information set by a decoder: "
621 "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
622 "(frame size %dx%d). This is a bug, please report it\n",
623 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
624 frame->width, frame->height);
625 frame->crop_left = 0;
626 frame->crop_right = 0;
627 frame->crop_top = 0;
628 frame->crop_bottom = 0;
629 return 0;
630 }
631
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110385 times.
110385 if (!avctx->apply_cropping)
633 return 0;
634
635 110385 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
636 AV_FRAME_CROP_UNALIGNED : 0);
637 }
638
639 783814 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
640 {
641 783814 AVCodecInternal *avci = avctx->internal;
642 int ret, changed;
643
644 783814 av_frame_unref(frame);
645
646
2/4
✓ Branch 1 taken 783814 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 783814 times.
783814 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
647 return AVERROR(EINVAL);
648
649
2/2
✓ Branch 0 taken 376177 times.
✓ Branch 1 taken 407637 times.
783814 if (avci->buffer_frame->buf[0]) {
650 376177 av_frame_move_ref(frame, avci->buffer_frame);
651 } else {
652 407637 ret = decode_receive_frame_internal(avctx, frame);
653
2/2
✓ Branch 0 taken 379756 times.
✓ Branch 1 taken 27881 times.
407637 if (ret < 0)
654 379756 return ret;
655 }
656
657
2/2
✓ Branch 0 taken 110385 times.
✓ Branch 1 taken 293673 times.
404058 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
658 110385 ret = apply_cropping(avctx, frame);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110385 times.
110385 if (ret < 0) {
660 av_frame_unref(frame);
661 return ret;
662 }
663 }
664
665 404058 avctx->frame_number++;
666
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404058 times.
404058 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
668
669 if (avctx->frame_number == 1) {
670 avci->initial_format = frame->format;
671 switch(avctx->codec_type) {
672 case AVMEDIA_TYPE_VIDEO:
673 avci->initial_width = frame->width;
674 avci->initial_height = frame->height;
675 break;
676 case AVMEDIA_TYPE_AUDIO:
677 avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
678 avctx->sample_rate;
679 #if FF_API_OLD_CHANNEL_LAYOUT
680 FF_DISABLE_DEPRECATION_WARNINGS
681 avci->initial_channels = frame->ch_layout.nb_channels;
682 avci->initial_channel_layout = frame->channel_layout;
683 FF_ENABLE_DEPRECATION_WARNINGS
684 #endif
685 ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout);
686 if (ret < 0) {
687 av_frame_unref(frame);
688 return ret;
689 }
690 break;
691 }
692 }
693
694 if (avctx->frame_number > 1) {
695 changed = avci->initial_format != frame->format;
696
697 switch(avctx->codec_type) {
698 case AVMEDIA_TYPE_VIDEO:
699 changed |= avci->initial_width != frame->width ||
700 avci->initial_height != frame->height;
701 break;
702 case AVMEDIA_TYPE_AUDIO:
703 FF_DISABLE_DEPRECATION_WARNINGS
704 changed |= avci->initial_sample_rate != frame->sample_rate ||
705 avci->initial_sample_rate != avctx->sample_rate ||
706 #if FF_API_OLD_CHANNEL_LAYOUT
707 avci->initial_channels != frame->channels ||
708 avci->initial_channel_layout != frame->channel_layout ||
709 #endif
710 av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout);
711 FF_ENABLE_DEPRECATION_WARNINGS
712 break;
713 }
714
715 if (changed) {
716 avci->changed_frames_dropped++;
717 av_log(avctx, AV_LOG_INFO, "dropped changed frame #%d pts %"PRId64
718 " drop count: %d \n",
719 avctx->frame_number, frame->pts,
720 avci->changed_frames_dropped);
721 av_frame_unref(frame);
722 return AVERROR_INPUT_CHANGED;
723 }
724 }
725 }
726 404058 return 0;
727 }
728
729 1571 static void get_subtitle_defaults(AVSubtitle *sub)
730 {
731 1571 memset(sub, 0, sizeof(*sub));
732 1571 sub->pts = AV_NOPTS_VALUE;
733 1571 }
734
735 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
736 1537 static int recode_subtitle(AVCodecContext *avctx, AVPacket **outpkt,
737 AVPacket *inpkt, AVPacket *buf_pkt)
738 {
739 #if CONFIG_ICONV
740 1537 iconv_t cd = (iconv_t)-1;
741 1537 int ret = 0;
742 char *inb, *outb;
743 size_t inl, outl;
744 #endif
745
746
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1444 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
1537 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) {
747 1444 *outpkt = inpkt;
748 1444 return 0;
749 }
750
751 #if CONFIG_ICONV
752 93 inb = inpkt->data;
753 93 inl = inpkt->size;
754
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
756 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
757 return AVERROR(ERANGE);
758 }
759
760 93 cd = iconv_open("UTF-8", avctx->sub_charenc);
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 av_assert0(cd != (iconv_t)-1);
762
763 93 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
765 goto end;
766 93 ret = av_packet_copy_props(buf_pkt, inpkt);
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
768 goto end;
769 93 outb = buf_pkt->data;
770 93 outl = buf_pkt->size;
771
772
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 ||
773 93 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
774
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) {
775 ret = FFMIN(AVERROR(errno), -1);
776 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
777 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
778 goto end;
779 }
780 93 buf_pkt->size -= outl;
781 93 memset(buf_pkt->data + buf_pkt->size, 0, outl);
782 93 *outpkt = buf_pkt;
783
784 93 ret = 0;
785 93 end:
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
787 av_packet_unref(buf_pkt);
788
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (cd != (iconv_t)-1)
789 93 iconv_close(cd);
790 93 return ret;
791 #else
792 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
793 return AVERROR(EINVAL);
794 #endif
795 }
796
797 692 static int utf8_check(const uint8_t *str)
798 {
799 const uint8_t *byte;
800 uint32_t codepoint, min;
801
802
2/2
✓ Branch 0 taken 60369 times.
✓ Branch 1 taken 692 times.
61061 while (*str) {
803 60369 byte = str;
804
5/8
✓ Branch 0 taken 60369 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60369 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2794 times.
✓ Branch 6 taken 2794 times.
✓ Branch 7 taken 60369 times.
63163 GET_UTF8(codepoint, *(byte++), return 0;);
805
4/4
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 58280 times.
✓ Branch 2 taken 705 times.
✓ Branch 3 taken 1384 times.
61074 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
806 705 1 << (5 * (byte - str) - 4);
807
3/6
✓ Branch 0 taken 60369 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60369 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60369 times.
✗ Branch 5 not taken.
60369 if (codepoint < min || codepoint >= 0x110000 ||
808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60369 times.
60369 codepoint == 0xFFFE /* BOM */ ||
809 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
810 return 0;
811 60369 str = byte;
812 }
813 692 return 1;
814 }
815
816 1571 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
817 int *got_sub_ptr,
818 AVPacket *avpkt)
819 {
820 1571 int ret = 0;
821
822
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1532 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
1571 if (!avpkt->data && avpkt->size) {
823 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
824 return AVERROR(EINVAL);
825 }
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1571 times.
1571 if (!avctx->codec)
827 return AVERROR(EINVAL);
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1571 times.
1571 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
829 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
830 return AVERROR(EINVAL);
831 }
832
833 1571 *got_sub_ptr = 0;
834 1571 get_subtitle_defaults(sub);
835
836
4/4
✓ Branch 0 taken 855 times.
✓ Branch 1 taken 716 times.
✓ Branch 2 taken 821 times.
✓ Branch 3 taken 34 times.
1571 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
837 1537 AVCodecInternal *avci = avctx->internal;
838 AVPacket *pkt;
839
840 1537 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1537 times.
1537 if (ret < 0)
842 79 return ret;
843
844
3/4
✓ Branch 0 taken 1537 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1529 times.
✓ Branch 3 taken 8 times.
1537 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
845 1529 sub->pts = av_rescale_q(avpkt->pts,
846 1529 avctx->pkt_timebase, AV_TIME_BASE_Q);
847 1537 ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
848
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1444 times.
1537 if (pkt == avci->buffer_pkt) // did we recode?
849 93 av_packet_unref(avci->buffer_pkt);
850
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1458 times.
1537 if (ret < 0) {
851 79 *got_sub_ptr = 0;
852 79 avsubtitle_free(sub);
853 79 return ret;
854 }
855 av_assert1(!sub->num_rects || *got_sub_ptr);
856
857
6/6
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 640 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 238 times.
✓ Branch 4 taken 528 times.
✓ Branch 5 taken 52 times.
1458 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
858
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 avctx->pkt_timebase.num) {
859 528 AVRational ms = { 1, 1000 };
860 528 sub->end_display_time = av_rescale_q(avpkt->duration,
861 avctx->pkt_timebase, ms);
862 }
863
864
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 1304 times.
1458 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
865 154 sub->format = 0;
866
1/2
✓ Branch 0 taken 1304 times.
✗ Branch 1 not taken.
1304 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
867 1304 sub->format = 1;
868
869
2/2
✓ Branch 0 taken 851 times.
✓ Branch 1 taken 1458 times.
2309 for (unsigned i = 0; i < sub->num_rects; i++) {
870
1/2
✓ Branch 0 taken 851 times.
✗ Branch 1 not taken.
851 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
871
3/4
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 692 times.
851 sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
872 av_log(avctx, AV_LOG_ERROR,
873 "Invalid UTF-8 in decoded subtitles text; "
874 "maybe missing -sub_charenc option\n");
875 avsubtitle_free(sub);
876 *got_sub_ptr = 0;
877 return AVERROR_INVALIDDATA;
878 }
879 }
880
881
2/2
✓ Branch 0 taken 827 times.
✓ Branch 1 taken 631 times.
1458 if (*got_sub_ptr)
882 827 avctx->frame_number++;
883 }
884
885 1492 return ret;
886 }
887
888 1568 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
889 const enum AVPixelFormat *fmt)
890 {
891 const AVPixFmtDescriptor *desc;
892 const AVCodecHWConfig *config;
893 int i, n;
894
895 // If a device was supplied when the codec was opened, assume that the
896 // user wants to use it.
897
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1568 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1568 if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
898 AVHWDeviceContext *device_ctx =
899 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
900 for (i = 0;; i++) {
901 config = &ffcodec(avctx->codec)->hw_configs[i]->public;
902 if (!config)
903 break;
904 if (!(config->methods &
905 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
906 continue;
907 if (device_ctx->type != config->device_type)
908 continue;
909 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
910 if (config->pix_fmt == fmt[n])
911 return fmt[n];
912 }
913 }
914 }
915 // No device or other setup, so we have to choose from things which
916 // don't any other external information.
917
918 // If the last element of the list is a software format, choose it
919 // (this should be best software format if any exist).
920
2/2
✓ Branch 0 taken 3903 times.
✓ Branch 1 taken 1568 times.
5471 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
921 1568 desc = av_pix_fmt_desc_get(fmt[n - 1]);
922
1/2
✓ Branch 0 taken 1568 times.
✗ Branch 1 not taken.
1568 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
923 1568 return fmt[n - 1];
924
925 // Finally, traverse the list in order and choose the first entry
926 // with no external dependencies (if there is no hardware configuration
927 // information available then this just picks the first entry).
928 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
929 for (i = 0;; i++) {
930 config = avcodec_get_hw_config(avctx->codec, i);
931 if (!config)
932 break;
933 if (config->pix_fmt == fmt[n])
934 break;
935 }
936 if (!config) {
937 // No specific config available, so the decoder must be able
938 // to handle this format without any additional setup.
939 return fmt[n];
940 }
941 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
942 // Usable with only internal setup.
943 return fmt[n];
944 }
945 }
946
947 // Nothing is usable, give up.
948 return AV_PIX_FMT_NONE;
949 }
950
951 int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
952 enum AVHWDeviceType dev_type)
953 {
954 AVHWDeviceContext *device_ctx;
955 AVHWFramesContext *frames_ctx;
956 int ret;
957
958 if (!avctx->hwaccel)
959 return AVERROR(ENOSYS);
960
961 if (avctx->hw_frames_ctx)
962 return 0;
963 if (!avctx->hw_device_ctx) {
964 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
965 "required for hardware accelerated decoding.\n");
966 return AVERROR(EINVAL);
967 }
968
969 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
970 if (device_ctx->type != dev_type) {
971 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
972 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
973 av_hwdevice_get_type_name(device_ctx->type));
974 return AVERROR(EINVAL);
975 }
976
977 ret = avcodec_get_hw_frames_parameters(avctx,
978 avctx->hw_device_ctx,
979 avctx->hwaccel->pix_fmt,
980 &avctx->hw_frames_ctx);
981 if (ret < 0)
982 return ret;
983
984 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
985
986
987 if (frames_ctx->initial_pool_size) {
988 // We guarantee 4 base work surfaces. The function above guarantees 1
989 // (the absolute minimum), so add the missing count.
990 frames_ctx->initial_pool_size += 3;
991 }
992
993 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
994 if (ret < 0) {
995 av_buffer_unref(&avctx->hw_frames_ctx);
996 return ret;
997 }
998
999 return 0;
1000 }
1001
1002 int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
1003 AVBufferRef *device_ref,
1004 enum AVPixelFormat hw_pix_fmt,
1005 AVBufferRef **out_frames_ref)
1006 {
1007 AVBufferRef *frames_ref = NULL;
1008 const AVCodecHWConfigInternal *hw_config;
1009 const AVHWAccel *hwa;
1010 int i, ret;
1011
1012 for (i = 0;; i++) {
1013 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1014 if (!hw_config)
1015 return AVERROR(ENOENT);
1016 if (hw_config->public.pix_fmt == hw_pix_fmt)
1017 break;
1018 }
1019
1020 hwa = hw_config->hwaccel;
1021 if (!hwa || !hwa->frame_params)
1022 return AVERROR(ENOENT);
1023
1024 frames_ref = av_hwframe_ctx_alloc(device_ref);
1025 if (!frames_ref)
1026 return AVERROR(ENOMEM);
1027
1028 ret = hwa->frame_params(avctx, frames_ref);
1029 if (ret >= 0) {
1030 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1031
1032 if (frames_ctx->initial_pool_size) {
1033 // If the user has requested that extra output surfaces be
1034 // available then add them here.
1035 if (avctx->extra_hw_frames > 0)
1036 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1037
1038 // If frame threading is enabled then an extra surface per thread
1039 // is also required.
1040 if (avctx->active_thread_type & FF_THREAD_FRAME)
1041 frames_ctx->initial_pool_size += avctx->thread_count;
1042 }
1043
1044 *out_frames_ref = frames_ref;
1045 } else {
1046 av_buffer_unref(&frames_ref);
1047 }
1048 return ret;
1049 }
1050
1051 static int hwaccel_init(AVCodecContext *avctx,
1052 const AVCodecHWConfigInternal *hw_config)
1053 {
1054 const AVHWAccel *hwaccel;
1055 int err;
1056
1057 hwaccel = hw_config->hwaccel;
1058 if (hwaccel->capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1059 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1060 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1061 hwaccel->name);
1062 return AVERROR_PATCHWELCOME;
1063 }
1064
1065 if (hwaccel->priv_data_size) {
1066 avctx->internal->hwaccel_priv_data =
1067 av_mallocz(hwaccel->priv_data_size);
1068 if (!avctx->internal->hwaccel_priv_data)
1069 return AVERROR(ENOMEM);
1070 }
1071
1072 avctx->hwaccel = hwaccel;
1073 if (hwaccel->init) {
1074 err = hwaccel->init(avctx);
1075 if (err < 0) {
1076 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
1077 "hwaccel initialisation returned error.\n",
1078 av_get_pix_fmt_name(hw_config->public.pix_fmt));
1079 av_freep(&avctx->internal->hwaccel_priv_data);
1080 avctx->hwaccel = NULL;
1081 return err;
1082 }
1083 }
1084
1085 return 0;
1086 }
1087
1088 2535 static void hwaccel_uninit(AVCodecContext *avctx)
1089 {
1090
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2535 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2535 if (avctx->hwaccel && avctx->hwaccel->uninit)
1091 avctx->hwaccel->uninit(avctx);
1092
1093 2535 av_freep(&avctx->internal->hwaccel_priv_data);
1094
1095 2535 avctx->hwaccel = NULL;
1096
1097 2535 av_buffer_unref(&avctx->hw_frames_ctx);
1098 2535 }
1099
1100 2535 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1101 {
1102 const AVPixFmtDescriptor *desc;
1103 enum AVPixelFormat *choices;
1104 enum AVPixelFormat ret, user_choice;
1105 const AVCodecHWConfigInternal *hw_config;
1106 const AVCodecHWConfig *config;
1107 int i, n, err;
1108
1109 // Find end of list.
1110
2/2
✓ Branch 0 taken 6498 times.
✓ Branch 1 taken 2535 times.
9033 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1111 // Must contain at least one entry.
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2535 times.
2535 av_assert0(n >= 1);
1113 // If a software format is available, it must be the last entry.
1114 2535 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1115
1/2
✓ Branch 0 taken 2535 times.
✗ Branch 1 not taken.
2535 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1116 // No software format is available.
1117 } else {
1118 2535 avctx->sw_pix_fmt = fmt[n - 1];
1119 }
1120
1121 2535 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2535 times.
2535 if (!choices)
1123 return AV_PIX_FMT_NONE;
1124
1125 for (;;) {
1126 // Remove the previous hwaccel, if there was one.
1127 2535 hwaccel_uninit(avctx);
1128
1129 2535 user_choice = avctx->get_format(avctx, choices);
1130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2535 times.
2535 if (user_choice == AV_PIX_FMT_NONE) {
1131 // Explicitly chose nothing, give up.
1132 ret = AV_PIX_FMT_NONE;
1133 break;
1134 }
1135
1136 2535 desc = av_pix_fmt_desc_get(user_choice);
1137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2535 times.
2535 if (!desc) {
1138 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
1139 "get_format() callback.\n");
1140 ret = AV_PIX_FMT_NONE;
1141 break;
1142 }
1143 2535 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
1144 2535 desc->name);
1145
1146
1/2
✓ Branch 0 taken 6498 times.
✗ Branch 1 not taken.
6498 for (i = 0; i < n; i++) {
1147
2/2
✓ Branch 0 taken 2535 times.
✓ Branch 1 taken 3963 times.
6498 if (choices[i] == user_choice)
1148 2535 break;
1149 }
1150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2535 times.
2535 if (i == n) {
1151 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
1152 "%s not in possible list.\n", desc->name);
1153 ret = AV_PIX_FMT_NONE;
1154 break;
1155 }
1156
1157
2/2
✓ Branch 1 taken 2411 times.
✓ Branch 2 taken 124 times.
2535 if (ffcodec(avctx->codec)->hw_configs) {
1158 6588 for (i = 0;; i++) {
1159 6588 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1160
2/2
✓ Branch 0 taken 2411 times.
✓ Branch 1 taken 4177 times.
6588 if (!hw_config)
1161 2411 break;
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4177 times.
4177 if (hw_config->public.pix_fmt == user_choice)
1163 break;
1164 }
1165 } else {
1166 124 hw_config = NULL;
1167 }
1168
1169
1/2
✓ Branch 0 taken 2535 times.
✗ Branch 1 not taken.
2535 if (!hw_config) {
1170 // No config available, so no extra setup required.
1171 2535 ret = user_choice;
1172 2535 break;
1173 }
1174 config = &hw_config->public;
1175
1176 if (config->methods &
1177 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1178 avctx->hw_frames_ctx) {
1179 const AVHWFramesContext *frames_ctx =
1180 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1181 if (frames_ctx->format != user_choice) {
1182 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1183 "does not match the format of the provided frames "
1184 "context.\n", desc->name);
1185 goto try_again;
1186 }
1187 } else if (config->methods &
1188 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1189 avctx->hw_device_ctx) {
1190 const AVHWDeviceContext *device_ctx =
1191 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1192 if (device_ctx->type != config->device_type) {
1193 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1194 "does not match the type of the provided device "
1195 "context.\n", desc->name);
1196 goto try_again;
1197 }
1198 } else if (config->methods &
1199 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1200 // Internal-only setup, no additional configuration.
1201 } else if (config->methods &
1202 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1203 // Some ad-hoc configuration we can't see and can't check.
1204 } else {
1205 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1206 "missing configuration.\n", desc->name);
1207 goto try_again;
1208 }
1209 if (hw_config->hwaccel) {
1210 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
1211 "initialisation.\n", desc->name);
1212 err = hwaccel_init(avctx, hw_config);
1213 if (err < 0)
1214 goto try_again;
1215 }
1216 ret = user_choice;
1217 break;
1218
1219 try_again:
1220 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
1221 "get_format() without it.\n", desc->name);
1222 for (i = 0; i < n; i++) {
1223 if (choices[i] == user_choice)
1224 break;
1225 }
1226 for (; i + 1 < n; i++)
1227 choices[i] = choices[i + 1];
1228 --n;
1229 }
1230
1231 2535 av_freep(&choices);
1232 2535 return ret;
1233 }
1234
1235 406079 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1236 {
1237 size_t size;
1238 const uint8_t *side_metadata;
1239
1240 406079 AVDictionary **frame_md = &frame->metadata;
1241
1242 406079 side_metadata = av_packet_get_side_data(avpkt,
1243 AV_PKT_DATA_STRINGS_METADATA, &size);
1244 406079 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1245 }
1246
1247 406079 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1248 {
1249 406079 AVPacket *pkt = avctx->internal->last_pkt_props;
1250 static const struct {
1251 enum AVPacketSideDataType packet;
1252 enum AVFrameSideDataType frame;
1253 } sd[] = {
1254 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
1255 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
1256 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
1257 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
1258 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
1259 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
1260 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
1261 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1262 { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
1263 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1264 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1265 };
1266
1267
1/2
✓ Branch 1 taken 406079 times.
✗ Branch 2 not taken.
406079 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
1268 406079 frame->pts = pkt->pts;
1269 406079 frame->pkt_pos = pkt->pos;
1270 406079 frame->pkt_duration = pkt->duration;
1271 406079 frame->pkt_size = pkt->size;
1272
1273
2/2
✓ Branch 0 taken 4466869 times.
✓ Branch 1 taken 406079 times.
4872948 for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1274 size_t size;
1275 4466869 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1276
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 4466658 times.
4466869 if (packet_sd) {
1277 211 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1278 211 sd[i].frame,
1279 size);
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
211 if (!frame_sd)
1281 return AVERROR(ENOMEM);
1282
1283 211 memcpy(frame_sd->data, packet_sd, size);
1284 }
1285 }
1286 406079 add_metadata_from_side_data(pkt, frame);
1287
1288
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 405877 times.
406079 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1289 202 frame->flags |= AV_FRAME_FLAG_DISCARD;
1290 } else {
1291 405877 frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
1292 }
1293 }
1294 406079 frame->reordered_opaque = avctx->reordered_opaque;
1295
1296
1/2
✓ Branch 0 taken 406079 times.
✗ Branch 1 not taken.
406079 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
1297 406079 frame->color_primaries = avctx->color_primaries;
1298
1/2
✓ Branch 0 taken 406079 times.
✗ Branch 1 not taken.
406079 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
1299 406079 frame->color_trc = avctx->color_trc;
1300
2/2
✓ Branch 0 taken 405644 times.
✓ Branch 1 taken 435 times.
406079 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
1301 405644 frame->colorspace = avctx->colorspace;
1302
2/2
✓ Branch 0 taken 405868 times.
✓ Branch 1 taken 211 times.
406079 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
1303 405868 frame->color_range = avctx->color_range;
1304
1/2
✓ Branch 0 taken 406079 times.
✗ Branch 1 not taken.
406079 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
1305 406079 frame->chroma_location = avctx->chroma_sample_location;
1306
1307
2/3
✓ Branch 0 taken 112179 times.
✓ Branch 1 taken 293900 times.
✗ Branch 2 not taken.
406079 switch (avctx->codec->type) {
1308 112179 case AVMEDIA_TYPE_VIDEO:
1309 112179 frame->format = avctx->pix_fmt;
1310
2/2
✓ Branch 0 taken 112060 times.
✓ Branch 1 taken 119 times.
112179 if (!frame->sample_aspect_ratio.num)
1311 112060 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
1312
1313
4/6
✓ Branch 0 taken 90281 times.
✓ Branch 1 taken 21898 times.
✓ Branch 2 taken 90281 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 90281 times.
202460 if (frame->width && frame->height &&
1314 90281 av_image_check_sar(frame->width, frame->height,
1315 frame->sample_aspect_ratio) < 0) {
1316 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1317 frame->sample_aspect_ratio.num,
1318 frame->sample_aspect_ratio.den);
1319 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1320 }
1321
1322 112179 break;
1323 293900 case AVMEDIA_TYPE_AUDIO:
1324
1/2
✓ Branch 0 taken 293900 times.
✗ Branch 1 not taken.
293900 if (!frame->sample_rate)
1325 293900 frame->sample_rate = avctx->sample_rate;
1326
1/2
✓ Branch 0 taken 293900 times.
✗ Branch 1 not taken.
293900 if (frame->format < 0)
1327 293900 frame->format = avctx->sample_fmt;
1328
1/2
✓ Branch 0 taken 293900 times.
✗ Branch 1 not taken.
293900 if (!frame->ch_layout.nb_channels) {
1329 293900 int ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
1330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293900 times.
293900 if (ret < 0)
1331 return ret;
1332 }
1333 #if FF_API_OLD_CHANNEL_LAYOUT
1334 FF_DISABLE_DEPRECATION_WARNINGS
1335 293900 frame->channels = frame->ch_layout.nb_channels;
1336 587800 frame->channel_layout = frame->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
1337
2/2
✓ Branch 0 taken 292787 times.
✓ Branch 1 taken 1113 times.
293900 frame->ch_layout.u.mask : 0;
1338 FF_ENABLE_DEPRECATION_WARNINGS
1339 #endif
1340 293900 break;
1341 }
1342 406079 return 0;
1343 }
1344
1345 378740 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1346 {
1347
2/2
✓ Branch 0 taken 84840 times.
✓ Branch 1 taken 293900 times.
378740 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1348 int i;
1349 84840 int num_planes = av_pix_fmt_count_planes(frame->format);
1350 84840 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1351
1/2
✓ Branch 0 taken 84840 times.
✗ Branch 1 not taken.
84840 int flags = desc ? desc->flags : 0;
1352
4/4
✓ Branch 0 taken 7941 times.
✓ Branch 1 taken 76899 times.
✓ Branch 2 taken 2015 times.
✓ Branch 3 taken 5926 times.
84840 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1353 2015 num_planes = 2;
1354
2/2
✓ Branch 0 taken 241366 times.
✓ Branch 1 taken 84840 times.
326206 for (i = 0; i < num_planes; i++) {
1355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 241366 times.
241366 av_assert0(frame->data[i]);
1356 }
1357 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1358
3/4
✓ Branch 0 taken 522194 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 437354 times.
✓ Branch 3 taken 84840 times.
522194 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 437354 times.
437354 if (frame->data[i])
1360 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1361 437354 frame->data[i] = NULL;
1362 }
1363 }
1364 378740 }
1365
1366 378740 static void decode_data_free(void *opaque, uint8_t *data)
1367 {
1368 378740 FrameDecodeData *fdd = (FrameDecodeData*)data;
1369
1370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (fdd->post_process_opaque_free)
1371 fdd->post_process_opaque_free(fdd->post_process_opaque);
1372
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (fdd->hwaccel_priv_free)
1374 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1375
1376 378740 av_freep(&fdd);
1377 378740 }
1378
1379 378740 int ff_attach_decode_data(AVFrame *frame)
1380 {
1381 AVBufferRef *fdd_buf;
1382 FrameDecodeData *fdd;
1383
1384 av_assert1(!frame->private_ref);
1385 378740 av_buffer_unref(&frame->private_ref);
1386
1387 378740 fdd = av_mallocz(sizeof(*fdd));
1388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (!fdd)
1389 return AVERROR(ENOMEM);
1390
1391 378740 fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
1392 NULL, AV_BUFFER_FLAG_READONLY);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (!fdd_buf) {
1394 av_freep(&fdd);
1395 return AVERROR(ENOMEM);
1396 }
1397
1398 378740 frame->private_ref = fdd_buf;
1399
1400 378740 return 0;
1401 }
1402
1403 378740 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1404 {
1405 378740 const AVHWAccel *hwaccel = avctx->hwaccel;
1406 378740 int override_dimensions = 1;
1407 int ret;
1408
1409
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 378740 times.
378740 av_assert0(av_codec_is_decoder(avctx->codec));
1410
1411
2/2
✓ Branch 0 taken 84840 times.
✓ Branch 1 taken 293900 times.
378740 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1412
2/4
✓ Branch 0 taken 84840 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84840 times.
✗ Branch 3 not taken.
169680 if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
1413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84840 times.
169680 (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) {
1414 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1415 ret = AVERROR(EINVAL);
1416 goto fail;
1417 }
1418
1419
3/4
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 84111 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729 times.
84840 if (frame->width <= 0 || frame->height <= 0) {
1420 84111 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1421 84111 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1422 84111 override_dimensions = 0;
1423 }
1424
1425
4/8
✓ Branch 0 taken 84840 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84840 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84840 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 84840 times.
84840 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1426 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1427 ret = AVERROR(EINVAL);
1428 goto fail;
1429 }
1430
1/2
✓ Branch 0 taken 293900 times.
✗ Branch 1 not taken.
293900 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1431 #if FF_API_OLD_CHANNEL_LAYOUT
1432 FF_DISABLE_DEPRECATION_WARNINGS
1433 /* compat layer for old-style get_buffer() implementations */
1434 293900 avctx->channels = avctx->ch_layout.nb_channels;
1435 587800 avctx->channel_layout = (avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) ?
1436
2/2
✓ Branch 0 taken 292787 times.
✓ Branch 1 taken 1113 times.
293900 avctx->ch_layout.u.mask : 0;
1437 FF_ENABLE_DEPRECATION_WARNINGS
1438 #endif
1439
1440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293900 times.
293900 if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
1441 av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
1442 ret = AVERROR(EINVAL);
1443 goto fail;
1444 }
1445 }
1446 378740 ret = ff_decode_frame_props(avctx, frame);
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (ret < 0)
1448 goto fail;
1449
1450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (hwaccel) {
1451 if (hwaccel->alloc_frame) {
1452 ret = hwaccel->alloc_frame(avctx, frame);
1453 goto end;
1454 }
1455 } else
1456 378740 avctx->sw_pix_fmt = avctx->pix_fmt;
1457
1458 378740 ret = avctx->get_buffer2(avctx, frame, flags);
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (ret < 0)
1460 goto fail;
1461
1462 378740 validate_avframe_allocation(avctx, frame);
1463
1464 378740 ret = ff_attach_decode_data(frame);
1465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (ret < 0)
1466 goto fail;
1467
1468 378740 end:
1469
4/4
✓ Branch 0 taken 293900 times.
✓ Branch 1 taken 84840 times.
✓ Branch 2 taken 729 times.
✓ Branch 3 taken 84111 times.
378740 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1470
2/2
✓ Branch 1 taken 33224 times.
✓ Branch 2 taken 50887 times.
84111 !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1471 50887 frame->width = avctx->width;
1472 50887 frame->height = avctx->height;
1473 }
1474
1475 327853 fail:
1476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378740 times.
378740 if (ret < 0) {
1477 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1478 av_frame_unref(frame);
1479 }
1480
1481 378740 return ret;
1482 }
1483
1484 7012 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1485 {
1486 AVFrame *tmp;
1487 int ret;
1488
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7012 times.
7012 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1490
1491
5/8
✓ Branch 0 taken 6890 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 6890 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6890 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6890 times.
7012 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1492 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1493 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1494 av_frame_unref(frame);
1495 }
1496
1497
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 6890 times.
7012 if (!frame->data[0])
1498 122 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1499
1500
4/4
✓ Branch 0 taken 6889 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 5331 times.
✓ Branch 4 taken 1558 times.
6890 if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
1501 5332 return ff_decode_frame_props(avctx, frame);
1502
1503 1558 tmp = av_frame_alloc();
1504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1558 times.
1558 if (!tmp)
1505 return AVERROR(ENOMEM);
1506
1507 1558 av_frame_move_ref(tmp, frame);
1508
1509 1558 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1558 times.
1558 if (ret < 0) {
1511 av_frame_free(&tmp);
1512 return ret;
1513 }
1514
1515 1558 av_frame_copy(frame, tmp);
1516 1558 av_frame_free(&tmp);
1517
1518 1558 return 0;
1519 }
1520
1521 7012 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1522 {
1523 7012 int ret = reget_buffer_internal(avctx, frame, flags);
1524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7012 times.
7012 if (ret < 0)
1525 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1526 7012 return ret;
1527 }
1528
1529 13383 int ff_decode_preinit(AVCodecContext *avctx)
1530 {
1531 13383 AVCodecInternal *avci = avctx->internal;
1532 13383 int ret = 0;
1533
1534 /* if the decoder init function was already called previously,
1535 * free the already allocated subtitle_header before overwriting it */
1536 13383 av_freep(&avctx->subtitle_header);
1537
1538 #if FF_API_THREAD_SAFE_CALLBACKS
1539 FF_DISABLE_DEPRECATION_WARNINGS
1540
2/2
✓ Branch 0 taken 13338 times.
✓ Branch 1 taken 45 times.
13383 if ((avctx->thread_type & FF_THREAD_FRAME) &&
1541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13338 times.
13338 avctx->get_buffer2 != avcodec_default_get_buffer2 &&
1542 !avctx->thread_safe_callbacks) {
1543 av_log(avctx, AV_LOG_WARNING, "Requested frame threading with a "
1544 "custom get_buffer2() implementation which is not marked as "
1545 "thread safe. This is not supported anymore, make your "
1546 "callback thread-safe.\n");
1547 }
1548 FF_ENABLE_DEPRECATION_WARNINGS
1549 #endif
1550
1551
4/4
✓ Branch 0 taken 2934 times.
✓ Branch 1 taken 10449 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 2734 times.
13383 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && avctx->ch_layout.nb_channels == 0 &&
1552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 !(avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
1553 av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
1554 return AVERROR(EINVAL);
1555 }
1556
2/4
✓ Branch 0 taken 13383 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13383 times.
13383 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1557 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
1558 avctx->codec->max_lowres);
1559 avctx->lowres = avctx->codec->max_lowres;
1560 }
1561
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13377 times.
13383 if (avctx->sub_charenc) {
1562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1563 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1564 "supported with subtitles codecs\n");
1565 return AVERROR(EINVAL);
1566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1567 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1568 "subtitles character encoding will be ignored\n",
1569 avctx->codec_descriptor->name);
1570 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1571 } else {
1572 /* input character encoding is set for a text based subtitle
1573 * codec at this point */
1574
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1575 6 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1576
1577
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1578 #if CONFIG_ICONV
1579 6 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cd == (iconv_t)-1) {
1581 ret = AVERROR(errno);
1582 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1583 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1584 return ret;
1585 }
1586 6 iconv_close(cd);
1587 #else
1588 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1589 "conversion needs a libavcodec built with iconv support "
1590 "for this codec\n");
1591 return AVERROR(ENOSYS);
1592 #endif
1593 }
1594 }
1595 }
1596
1597 13383 avctx->pts_correction_num_faulty_pts =
1598 13383 avctx->pts_correction_num_faulty_dts = 0;
1599 13383 avctx->pts_correction_last_pts =
1600 13383 avctx->pts_correction_last_dts = INT64_MIN;
1601
1602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1603 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1604 av_log(avctx, AV_LOG_WARNING,
1605 "gray decoding requested but not enabled at configuration time\n");
1606
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13378 times.
13383 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
1607 5 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
1608 }
1609
1610 13383 avci->in_pkt = av_packet_alloc();
1611 13383 avci->last_pkt_props = av_packet_alloc();
1612 13383 avci->pkt_props = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
1613 AV_FIFO_FLAG_AUTO_GROW);
1614
3/6
✓ Branch 0 taken 13383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13383 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 13383 times.
13383 if (!avci->in_pkt || !avci->last_pkt_props || !avci->pkt_props)
1615 return AVERROR(ENOMEM);
1616
1617 13383 ret = decode_bsfs_init(avctx);
1618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 if (ret < 0)
1619 return ret;
1620
1621 13383 return 0;
1622 }
1623
1624 1263 int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
1625 {
1626 size_t size;
1627 1263 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
1628
1629
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1239 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
1263 if (pal && size == AVPALETTE_SIZE) {
1630 24 memcpy(dst, pal, AVPALETTE_SIZE);
1631 24 return 1;
1632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 } else if (pal) {
1633 av_log(logctx, AV_LOG_ERROR,
1634 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
1635 }
1636 1239 return 0;
1637 }
1638