FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2022-01-22 23:30:28
Exec Total Coverage
Lines: 664 1061 62.6%
Branches: 488 839 58.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/frame.h"
36 #include "libavutil/hwcontext.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/intmath.h"
40 #include "libavutil/opt.h"
41
42 #include "avcodec.h"
43 #include "bytestream.h"
44 #include "bsf.h"
45 #include "decode.h"
46 #include "hwconfig.h"
47 #include "internal.h"
48 #include "thread.h"
49
50 typedef struct FramePool {
51 /**
52 * Pools for each data plane. For audio all the planes have the same size,
53 * so only pools[0] is used.
54 */
55 AVBufferPool *pools[4];
56
57 /*
58 * Pool parameters
59 */
60 int format;
61 int width, height;
62 int stride_align[AV_NUM_DATA_POINTERS];
63 int linesize[4];
64 int planes;
65 int channels;
66 int samples;
67 } FramePool;
68
69 382103 static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
70 {
71 int ret;
72 size_t size;
73 const uint8_t *data;
74 uint32_t flags;
75 int64_t val;
76
77 382103 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
78
2/2
✓ Branch 0 taken 382101 times.
✓ Branch 1 taken 2 times.
382103 if (!data)
79 382101 return 0;
80
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
82 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
83 "changes, but PARAM_CHANGE side data was sent to it.\n");
84 ret = AVERROR(EINVAL);
85 goto fail2;
86 }
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 4)
89 goto fail;
90
91 2 flags = bytestream_get_le32(&data);
92 2 size -= 4;
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
95 if (size < 4)
96 goto fail;
97 val = bytestream_get_le32(&data);
98 if (val <= 0 || val > INT_MAX) {
99 av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
100 ret = AVERROR_INVALIDDATA;
101 goto fail2;
102 }
103 avctx->channels = val;
104 size -= 4;
105 }
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
107 if (size < 8)
108 goto fail;
109 avctx->channel_layout = bytestream_get_le64(&data);
110 size -= 8;
111 }
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
113 if (size < 4)
114 goto fail;
115 val = bytestream_get_le32(&data);
116 if (val <= 0 || val > INT_MAX) {
117 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
118 ret = AVERROR_INVALIDDATA;
119 goto fail2;
120 }
121 avctx->sample_rate = val;
122 size -= 4;
123 }
124
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 8)
126 goto fail;
127 2 avctx->width = bytestream_get_le32(&data);
128 2 avctx->height = bytestream_get_le32(&data);
129 2 size -= 8;
130 2 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
132 goto fail2;
133 }
134
135 2 return 0;
136 fail:
137 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
138 ret = AVERROR_INVALIDDATA;
139 fail2:
140 if (ret < 0) {
141 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
142 if (avctx->err_recognition & AV_EF_EXPLODE)
143 return ret;
144 }
145 return 0;
146 }
147
148 #define IS_EMPTY(pkt) (!(pkt)->data)
149
150 382103 static int copy_packet_props(AVPacket *dst, const AVPacket *src)
151 {
152 382103 int ret = av_packet_copy_props(dst, src);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382103 times.
382103 if (ret < 0)
154 return ret;
155
156 382103 dst->size = src->size; // HACK: Needed for ff_decode_frame_props().
157 382103 dst->data = (void*)1; // HACK: Needed for IS_EMPTY().
158
159 382103 return 0;
160 }
161
162 382103 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
163 {
164 382103 AVPacket tmp = { 0 };
165 382103 int ret = 0;
166
167
1/2
✓ Branch 0 taken 382103 times.
✗ Branch 1 not taken.
382103 if (IS_EMPTY(avci->last_pkt_props)) {
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 382103 times.
382103 if (av_fifo_size(avci->pkt_props) >= sizeof(*pkt)) {
169 av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
170 sizeof(*avci->last_pkt_props), NULL);
171 } else
172 382103 return copy_packet_props(avci->last_pkt_props, pkt);
173 }
174
175 if (av_fifo_space(avci->pkt_props) < sizeof(*pkt)) {
176 ret = av_fifo_grow(avci->pkt_props, sizeof(*pkt));
177 if (ret < 0)
178 return ret;
179 }
180
181 ret = copy_packet_props(&tmp, pkt);
182 if (ret < 0)
183 return ret;
184
185 av_fifo_generic_write(avci->pkt_props, &tmp, sizeof(tmp), NULL);
186
187 return 0;
188 }
189
190 13210 static int decode_bsfs_init(AVCodecContext *avctx)
191 {
192 13210 AVCodecInternal *avci = avctx->internal;
193 int ret;
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13210 times.
13210 if (avci->bsf)
196 return 0;
197
198 13210 ret = av_bsf_list_parse_str(avctx->codec->bsfs, &avci->bsf);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13210 times.
13210 if (ret < 0) {
200 av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", avctx->codec->bsfs, av_err2str(ret));
201 if (ret != AVERROR(ENOMEM))
202 ret = AVERROR_BUG;
203 goto fail;
204 }
205
206 /* We do not currently have an API for passing the input timebase into decoders,
207 * but no filters used here should actually need it.
208 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
209 13210 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
210 13210 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13210 times.
13210 if (ret < 0)
212 goto fail;
213
214 13210 ret = av_bsf_init(avci->bsf);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13210 times.
13210 if (ret < 0)
216 goto fail;
217
218 13210 return 0;
219 fail:
220 av_bsf_free(&avci->bsf);
221 return ret;
222 }
223
224 766976 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
225 {
226 766976 AVCodecInternal *avci = avctx->internal;
227 int ret;
228
229
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 766914 times.
766976 if (avci->draining)
230 62 return AVERROR_EOF;
231
232 766914 ret = av_bsf_receive_packet(avci->bsf, pkt);
233
2/2
✓ Branch 0 taken 6030 times.
✓ Branch 1 taken 760884 times.
766914 if (ret == AVERROR_EOF)
234 6030 avci->draining = 1;
235
2/2
✓ Branch 0 taken 384811 times.
✓ Branch 1 taken 382103 times.
766914 if (ret < 0)
236 384811 return ret;
237
238
1/2
✓ Branch 0 taken 382103 times.
✗ Branch 1 not taken.
382103 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
239 382103 ret = extract_packet_props(avctx->internal, pkt);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382103 times.
382103 if (ret < 0)
241 goto finish;
242 }
243
244 382103 ret = apply_param_change(avctx, pkt);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382103 times.
382103 if (ret < 0)
246 goto finish;
247
248 382103 return 0;
249 finish:
250 av_packet_unref(pkt);
251 return ret;
252 }
253
254 /**
255 * Attempt to guess proper monotonic timestamps for decoded video frames
256 * which might have incorrect times. Input timestamps may wrap around, in
257 * which case the output will as well.
258 *
259 * @param pts the pts field of the decoded AVPacket, as passed through
260 * AVFrame.pts
261 * @param dts the dts field of the decoded AVPacket
262 * @return one of the input values, may be AV_NOPTS_VALUE
263 */
264 402746 static int64_t guess_correct_pts(AVCodecContext *ctx,
265 int64_t reordered_pts, int64_t dts)
266 {
267 402746 int64_t pts = AV_NOPTS_VALUE;
268
269
2/2
✓ Branch 0 taken 367900 times.
✓ Branch 1 taken 34846 times.
402746 if (dts != AV_NOPTS_VALUE) {
270 367900 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
271 367900 ctx->pts_correction_last_dts = dts;
272
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 34533 times.
34846 } else if (reordered_pts != AV_NOPTS_VALUE)
273 313 ctx->pts_correction_last_dts = reordered_pts;
274
275
2/2
✓ Branch 0 taken 339328 times.
✓ Branch 1 taken 63418 times.
402746 if (reordered_pts != AV_NOPTS_VALUE) {
276 339328 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
277 339328 ctx->pts_correction_last_pts = reordered_pts;
278
2/2
✓ Branch 0 taken 28885 times.
✓ Branch 1 taken 34533 times.
63418 } else if(dts != AV_NOPTS_VALUE)
279 28885 ctx->pts_correction_last_pts = dts;
280
281
4/4
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 402185 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 556 times.
402746 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
282
2/2
✓ Branch 0 taken 338772 times.
✓ Branch 1 taken 63418 times.
402190 && reordered_pts != AV_NOPTS_VALUE)
283 338772 pts = reordered_pts;
284 else
285 63974 pts = dts;
286
287 402746 return pts;
288 }
289
290 /*
291 * The core of the receive_frame_wrapper for the decoders implementing
292 * the simple API. Certain decoders might consume partial packets without
293 * returning any output, so this function needs to be called in a loop until it
294 * returns EAGAIN.
295 **/
296 797517 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
297 {
298 797517 AVCodecInternal *avci = avctx->internal;
299 797517 AVPacket *const pkt = avci->in_pkt;
300 int got_frame, actual_got_frame;
301 int ret;
302
303
4/4
✓ Branch 0 taken 768857 times.
✓ Branch 1 taken 28660 times.
✓ Branch 2 taken 761855 times.
✓ Branch 3 taken 7002 times.
797517 if (!pkt->data && !avci->draining) {
304 761855 av_packet_unref(pkt);
305 761855 ret = ff_decode_get_packet(avctx, pkt);
306
4/4
✓ Branch 0 taken 382397 times.
✓ Branch 1 taken 379458 times.
✓ Branch 2 taken 376429 times.
✓ Branch 3 taken 5968 times.
761855 if (ret < 0 && ret != AVERROR_EOF)
307 376429 return ret;
308 }
309
310 // Some codecs (at least wma lossless) will crash when feeding drain packets
311 // after EOF was signaled.
312
2/2
✓ Branch 0 taken 6271 times.
✓ Branch 1 taken 414817 times.
421088 if (avci->draining_done)
313 6271 return AVERROR_EOF;
314
315
2/2
✓ Branch 0 taken 6699 times.
✓ Branch 1 taken 408118 times.
414817 if (!pkt->data &&
316
2/2
✓ Branch 0 taken 5331 times.
✓ Branch 1 taken 1368 times.
6699 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
317
2/2
✓ Branch 0 taken 5282 times.
✓ Branch 1 taken 49 times.
5331 avctx->active_thread_type & FF_THREAD_FRAME))
318 5282 return AVERROR_EOF;
319
320 409535 got_frame = 0;
321
322
2/2
✓ Branch 0 taken 582 times.
✓ Branch 1 taken 408953 times.
409535 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
323 582 ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
324 } else {
325 408953 ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
326
327
2/2
✓ Branch 0 taken 408619 times.
✓ Branch 1 taken 334 times.
408953 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
328 408619 frame->pkt_dts = pkt->dts;
329
2/2
✓ Branch 0 taken 114418 times.
✓ Branch 1 taken 294535 times.
408953 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
330
2/2
✓ Branch 0 taken 82226 times.
✓ Branch 1 taken 32192 times.
114418 if(!avctx->has_b_frames)
331 82226 frame->pkt_pos = pkt->pos;
332 //FIXME these should be under if(!avctx->has_b_frames)
333 /* get_buffer is supposed to set frame parameters */
334
2/2
✓ Branch 0 taken 21705 times.
✓ Branch 1 taken 92713 times.
114418 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
335
2/2
✓ Branch 0 taken 21149 times.
✓ Branch 1 taken 556 times.
21705 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
336
2/2
✓ Branch 0 taken 21498 times.
✓ Branch 1 taken 207 times.
21705 if (!frame->width) frame->width = avctx->width;
337
2/2
✓ Branch 0 taken 21498 times.
✓ Branch 1 taken 207 times.
21705 if (!frame->height) frame->height = avctx->height;
338
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21703 times.
21705 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
339 }
340 }
341 }
342 409535 emms_c();
343 409535 actual_got_frame = got_frame;
344
345
2/2
✓ Branch 0 taken 114528 times.
✓ Branch 1 taken 295007 times.
409535 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
346
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 114348 times.
114528 if (frame->flags & AV_FRAME_FLAG_DISCARD)
347 180 got_frame = 0;
348
1/2
✓ Branch 0 taken 295007 times.
✗ Branch 1 not taken.
295007 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
349 uint8_t *side;
350 size_t side_size;
351 295007 uint32_t discard_padding = 0;
352 295007 uint8_t skip_reason = 0;
353 295007 uint8_t discard_reason = 0;
354
355
4/4
✓ Branch 0 taken 294990 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 293564 times.
✓ Branch 3 taken 1426 times.
295007 if (ret >= 0 && got_frame) {
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293564 times.
293564 if (frame->format == AV_SAMPLE_FMT_NONE)
357 frame->format = avctx->sample_fmt;
358
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 292476 times.
293564 if (!frame->channel_layout)
359 1088 frame->channel_layout = avctx->channel_layout;
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293564 times.
293564 if (!frame->channels)
361 frame->channels = avctx->channels;
362
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 293561 times.
293564 if (!frame->sample_rate)
363 3 frame->sample_rate = avctx->sample_rate;
364 }
365
366 295007 side= av_packet_get_side_data(avci->last_pkt_props, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
367
3/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 294897 times.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
295007 if(side && side_size>=10) {
368 110 avci->skip_samples = AV_RL32(side) * avci->skip_samples_multiplier;
369 110 discard_padding = AV_RL32(side + 4);
370 110 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
371 avci->skip_samples, (int)discard_padding);
372 110 skip_reason = AV_RL8(side + 8);
373 110 discard_reason = AV_RL8(side + 9);
374 }
375
376
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 294990 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
295007 if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame &&
377
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
378 17 avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
379 17 got_frame = 0;
380 17 *discarded_samples += frame->nb_samples;
381 }
382
383
4/4
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 294835 times.
✓ Branch 2 taken 165 times.
✓ Branch 3 taken 7 times.
295007 if (avci->skip_samples > 0 && got_frame &&
384
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
385
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 56 times.
165 if(frame->nb_samples <= avci->skip_samples){
386 109 got_frame = 0;
387 109 *discarded_samples += frame->nb_samples;
388 109 avci->skip_samples -= frame->nb_samples;
389 109 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
390 avci->skip_samples);
391 } else {
392 56 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
393 56 frame->nb_samples - avci->skip_samples, avctx->channels, frame->format);
394
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) {
395 56 int64_t diff_ts = av_rescale_q(avci->skip_samples,
396 56 (AVRational){1, avctx->sample_rate},
397 avctx->pkt_timebase);
398
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if(frame->pts!=AV_NOPTS_VALUE)
399 56 frame->pts += diff_ts;
400
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if(frame->pkt_dts!=AV_NOPTS_VALUE)
401 56 frame->pkt_dts += diff_ts;
402
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (frame->pkt_duration >= diff_ts)
403 56 frame->pkt_duration -= diff_ts;
404 } else {
405 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
406 }
407 56 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
408 avci->skip_samples, frame->nb_samples);
409 56 *discarded_samples += avci->skip_samples;
410 56 frame->nb_samples -= avci->skip_samples;
411 56 avci->skip_samples = 0;
412 }
413 }
414
415
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 295000 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
295007 if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame &&
416
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
417
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (discard_padding == frame->nb_samples) {
418 2 *discarded_samples += frame->nb_samples;
419 2 got_frame = 0;
420 } else {
421
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) {
422 5 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
423 5 (AVRational){1, avctx->sample_rate},
424 avctx->pkt_timebase);
425 5 frame->pkt_duration = diff_ts;
426 } else {
427 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
428 }
429 5 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
430 (int)discard_padding, frame->nb_samples);
431 5 frame->nb_samples -= discard_padding;
432 }
433 }
434
435
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 295007 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
295007 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) {
436 AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
437 if (fside) {
438 AV_WL32(fside->data, avci->skip_samples);
439 AV_WL32(fside->data + 4, discard_padding);
440 AV_WL8(fside->data + 8, skip_reason);
441 AV_WL8(fside->data + 9, discard_reason);
442 avci->skip_samples = 0;
443 }
444 }
445 }
446
447
2/2
✓ Branch 0 taken 295007 times.
✓ Branch 1 taken 114528 times.
409535 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO &&
448
4/4
✓ Branch 0 taken 269684 times.
✓ Branch 1 taken 25323 times.
✓ Branch 2 taken 269669 times.
✓ Branch 3 taken 15 times.
295007 !avci->showed_multi_packet_warning &&
449
4/4
✓ Branch 0 taken 5265 times.
✓ Branch 1 taken 264404 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 5249 times.
269669 ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
450 16 av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
451 16 avci->showed_multi_packet_warning = 1;
452 }
453
454
2/2
✓ Branch 0 taken 9450 times.
✓ Branch 1 taken 400085 times.
409535 if (!got_frame)
455 9450 av_frame_unref(frame);
456
457 #if FF_API_FLAG_TRUNCATED
458
5/6
✓ Branch 0 taken 408538 times.
✓ Branch 1 taken 997 times.
✓ Branch 2 taken 113548 times.
✓ Branch 3 taken 294990 times.
✓ Branch 4 taken 113548 times.
✗ Branch 5 not taken.
409535 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED))
459 #else
460 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
461 #endif
462 113548 ret = pkt->size;
463
464 #if FF_API_AVCTX_TIMEBASE
465
3/4
✓ Branch 0 taken 83663 times.
✓ Branch 1 taken 325872 times.
✓ Branch 2 taken 83663 times.
✗ Branch 3 not taken.
409535 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
466 83663 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
467 #endif
468
469 /* do not stop draining when actual_got_frame != 0 or ret < 0 */
470 /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
471
4/4
✓ Branch 0 taken 1417 times.
✓ Branch 1 taken 408118 times.
✓ Branch 2 taken 672 times.
✓ Branch 3 taken 745 times.
409535 if (avci->draining && !actual_got_frame) {
472
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 671 times.
672 if (ret < 0) {
473 /* prevent infinite loop if a decoder wrongly always return error on draining */
474 /* reasonable nb_errors_max = maximum b frames + thread count */
475
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 ?
476 avctx->thread_count : 1);
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avci->nb_draining_errors++ >= nb_errors_max) {
479 av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
480 "Stop draining and force EOF.\n");
481 avci->draining_done = 1;
482 ret = AVERROR_BUG;
483 }
484 } else {
485 671 avci->draining_done = 1;
486 }
487 }
488
489
4/4
✓ Branch 0 taken 29666 times.
✓ Branch 1 taken 379869 times.
✓ Branch 2 taken 997 times.
✓ Branch 3 taken 28669 times.
409535 if (ret >= pkt->size || ret < 0) {
490 380866 av_packet_unref(pkt);
491 380866 av_packet_unref(avci->last_pkt_props);
492 } else {
493 28669 int consumed = ret;
494
495 28669 pkt->data += consumed;
496 28669 pkt->size -= consumed;
497 28669 pkt->pts = AV_NOPTS_VALUE;
498 28669 pkt->dts = AV_NOPTS_VALUE;
499
1/2
✓ Branch 0 taken 28669 times.
✗ Branch 1 not taken.
28669 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
500 28669 avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
501 28669 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
502 28669 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
503 }
504 }
505
506
2/2
✓ Branch 0 taken 400085 times.
✓ Branch 1 taken 9450 times.
409535 if (got_frame)
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400085 times.
400085 av_assert0(frame->buf[0]);
508
509 409535 return ret < 0 ? ret : 0;
510 }
511
512 789064 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
513 {
514 int ret;
515 789064 int64_t discarded_samples = 0;
516
517
2/2
✓ Branch 0 taken 797517 times.
✓ Branch 1 taken 400085 times.
1197602 while (!frame->buf[0]) {
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 797517 times.
797517 if (discarded_samples > avctx->max_samples)
519 return AVERROR(EAGAIN);
520 797517 ret = decode_simple_internal(avctx, frame, &discarded_samples);
521
2/2
✓ Branch 0 taken 388979 times.
✓ Branch 1 taken 408538 times.
797517 if (ret < 0)
522 388979 return ret;
523 }
524
525 400085 return 0;
526 }
527
528 794275 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
529 {
530 794275 AVCodecInternal *avci = avctx->internal;
531 int ret;
532
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 794275 times.
794275 av_assert0(!frame->buf[0]);
534
535
2/2
✓ Branch 0 taken 5211 times.
✓ Branch 1 taken 789064 times.
794275 if (avctx->codec->receive_frame) {
536 5211 ret = avctx->codec->receive_frame(avctx, frame);
537
2/2
✓ Branch 0 taken 2786 times.
✓ Branch 1 taken 2425 times.
5211 if (ret != AVERROR(EAGAIN))
538 2786 av_packet_unref(avci->last_pkt_props);
539 } else
540 789064 ret = decode_simple_receive_frame(avctx, frame);
541
542
2/2
✓ Branch 0 taken 11677 times.
✓ Branch 1 taken 782598 times.
794275 if (ret == AVERROR_EOF)
543 11677 avci->draining_done = 1;
544
545
1/2
✓ Branch 0 taken 794275 times.
✗ Branch 1 not taken.
794275 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS) &&
546
3/4
✓ Branch 0 taken 765504 times.
✓ Branch 1 taken 28771 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 765504 times.
794275 IS_EMPTY(avci->last_pkt_props) && av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props))
547 av_fifo_generic_read(avci->pkt_props,
548 avci->last_pkt_props, sizeof(*avci->last_pkt_props), NULL);
549
550
2/2
✓ Branch 0 taken 402746 times.
✓ Branch 1 taken 391529 times.
794275 if (!ret) {
551 402746 frame->best_effort_timestamp = guess_correct_pts(avctx,
552 frame->pts,
553 frame->pkt_dts);
554
555 /* the only case where decode data is not set should be decoders
556 * that do not call ff_get_buffer() */
557
4/6
✓ Branch 0 taken 381250 times.
✓ Branch 1 taken 21496 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 381250 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 21496 times.
402746 av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
558 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
559
560
2/2
✓ Branch 0 taken 381250 times.
✓ Branch 1 taken 21496 times.
402746 if (frame->private_ref) {
561 381250 FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
562
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 381250 times.
381250 if (fdd->post_process) {
564 ret = fdd->post_process(avctx, frame);
565 if (ret < 0) {
566 av_frame_unref(frame);
567 return ret;
568 }
569 }
570 }
571 }
572
573 /* free the per-frame decode data */
574 794275 av_buffer_unref(&frame->private_ref);
575
576 794275 return ret;
577 }
578
579 388751 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
580 {
581 388751 AVCodecInternal *avci = avctx->internal;
582 int ret;
583
584
2/4
✓ Branch 1 taken 388751 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 388751 times.
388751 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
585 return AVERROR(EINVAL);
586
587
2/2
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 388069 times.
388751 if (avctx->internal->draining)
588 682 return AVERROR_EOF;
589
590
5/6
✓ Branch 0 taken 388053 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 6014 times.
✓ Branch 3 taken 382039 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6014 times.
388069 if (avpkt && !avpkt->size && avpkt->data)
591 return AVERROR(EINVAL);
592
593 388069 av_packet_unref(avci->buffer_pkt);
594
5/6
✓ Branch 0 taken 388053 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 6014 times.
✓ Branch 3 taken 382039 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6014 times.
388069 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
595 382039 ret = av_packet_ref(avci->buffer_pkt, avpkt);
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382039 times.
382039 if (ret < 0)
597 return ret;
598 }
599
600 388069 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
601
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 388000 times.
388069 if (ret < 0) {
602 69 av_packet_unref(avci->buffer_pkt);
603 69 return ret;
604 }
605
606
1/2
✓ Branch 0 taken 388000 times.
✗ Branch 1 not taken.
388000 if (!avci->buffer_frame->buf[0]) {
607 388000 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
608
6/6
✓ Branch 0 taken 13135 times.
✓ Branch 1 taken 374865 times.
✓ Branch 2 taken 6588 times.
✓ Branch 3 taken 6547 times.
✓ Branch 4 taken 928 times.
✓ Branch 5 taken 5660 times.
388000 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
609 928 return ret;
610 }
611
612 387072 return 0;
613 }
614
615 109138 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
616 {
617 /* make sure we are noisy about decoders returning invalid cropping data */
618
1/2
✓ Branch 0 taken 109138 times.
✗ Branch 1 not taken.
109138 if (frame->crop_left >= INT_MAX - frame->crop_right ||
619
1/2
✓ Branch 0 taken 109138 times.
✗ Branch 1 not taken.
109138 frame->crop_top >= INT_MAX - frame->crop_bottom ||
620
1/2
✓ Branch 0 taken 109138 times.
✗ Branch 1 not taken.
109138 (frame->crop_left + frame->crop_right) >= frame->width ||
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109138 times.
109138 (frame->crop_top + frame->crop_bottom) >= frame->height) {
622 av_log(avctx, AV_LOG_WARNING,
623 "Invalid cropping information set by a decoder: "
624 "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
625 "(frame size %dx%d). This is a bug, please report it\n",
626 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
627 frame->width, frame->height);
628 frame->crop_left = 0;
629 frame->crop_right = 0;
630 frame->crop_top = 0;
631 frame->crop_bottom = 0;
632 return 0;
633 }
634
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109138 times.
109138 if (!avctx->apply_cropping)
636 return 0;
637
638 109138 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
639 AV_FRAME_CROP_UNALIGNED : 0);
640 }
641
642 781140 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
643 {
644 781140 AVCodecInternal *avci = avctx->internal;
645 int ret, changed;
646
647 781140 av_frame_unref(frame);
648
649
2/4
✓ Branch 1 taken 781140 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 781140 times.
781140 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
650 return AVERROR(EINVAL);
651
652
2/2
✓ Branch 0 taken 374865 times.
✓ Branch 1 taken 406275 times.
781140 if (avci->buffer_frame->buf[0]) {
653 374865 av_frame_move_ref(frame, avci->buffer_frame);
654 } else {
655 406275 ret = decode_receive_frame_internal(avctx, frame);
656
2/2
✓ Branch 0 taken 378394 times.
✓ Branch 1 taken 27881 times.
406275 if (ret < 0)
657 378394 return ret;
658 }
659
660
2/2
✓ Branch 0 taken 109138 times.
✓ Branch 1 taken 293608 times.
402746 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
661 109138 ret = apply_cropping(avctx, frame);
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109138 times.
109138 if (ret < 0) {
663 av_frame_unref(frame);
664 return ret;
665 }
666 }
667
668 402746 avctx->frame_number++;
669
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402746 times.
402746 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
671
672 if (avctx->frame_number == 1) {
673 avci->initial_format = frame->format;
674 switch(avctx->codec_type) {
675 case AVMEDIA_TYPE_VIDEO:
676 avci->initial_width = frame->width;
677 avci->initial_height = frame->height;
678 break;
679 case AVMEDIA_TYPE_AUDIO:
680 avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
681 avctx->sample_rate;
682 avci->initial_channels = frame->channels;
683 avci->initial_channel_layout = frame->channel_layout;
684 break;
685 }
686 }
687
688 if (avctx->frame_number > 1) {
689 changed = avci->initial_format != frame->format;
690
691 switch(avctx->codec_type) {
692 case AVMEDIA_TYPE_VIDEO:
693 changed |= avci->initial_width != frame->width ||
694 avci->initial_height != frame->height;
695 break;
696 case AVMEDIA_TYPE_AUDIO:
697 changed |= avci->initial_sample_rate != frame->sample_rate ||
698 avci->initial_sample_rate != avctx->sample_rate ||
699 avci->initial_channels != frame->channels ||
700 avci->initial_channel_layout != frame->channel_layout;
701 break;
702 }
703
704 if (changed) {
705 avci->changed_frames_dropped++;
706 av_log(avctx, AV_LOG_INFO, "dropped changed frame #%d pts %"PRId64
707 " drop count: %d \n",
708 avctx->frame_number, frame->pts,
709 avci->changed_frames_dropped);
710 av_frame_unref(frame);
711 return AVERROR_INPUT_CHANGED;
712 }
713 }
714 }
715 402746 return 0;
716 }
717
718 1431 static void get_subtitle_defaults(AVSubtitle *sub)
719 {
720 1431 memset(sub, 0, sizeof(*sub));
721 1431 sub->pts = AV_NOPTS_VALUE;
722 1431 }
723
724 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
725 1397 static int recode_subtitle(AVCodecContext *avctx, AVPacket **outpkt,
726 AVPacket *inpkt, AVPacket *buf_pkt)
727 {
728 #if CONFIG_ICONV
729 1397 iconv_t cd = (iconv_t)-1;
730 1397 int ret = 0;
731 char *inb, *outb;
732 size_t inl, outl;
733 #endif
734
735
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1304 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
1397 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) {
736 1304 *outpkt = inpkt;
737 1304 return 0;
738 }
739
740 #if CONFIG_ICONV
741 93 inb = inpkt->data;
742 93 inl = inpkt->size;
743
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
745 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
746 return AVERROR(ERANGE);
747 }
748
749 93 cd = iconv_open("UTF-8", avctx->sub_charenc);
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 av_assert0(cd != (iconv_t)-1);
751
752 93 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
754 goto end;
755 93 ret = av_packet_copy_props(buf_pkt, inpkt);
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
757 goto end;
758 93 outb = buf_pkt->data;
759 93 outl = buf_pkt->size;
760
761
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 ||
762 93 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
763
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) {
764 ret = FFMIN(AVERROR(errno), -1);
765 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
766 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
767 goto end;
768 }
769 93 buf_pkt->size -= outl;
770 93 memset(buf_pkt->data + buf_pkt->size, 0, outl);
771 93 *outpkt = buf_pkt;
772
773 93 ret = 0;
774 93 end:
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
776 av_packet_unref(buf_pkt);
777
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (cd != (iconv_t)-1)
778 93 iconv_close(cd);
779 93 return ret;
780 #else
781 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
782 return AVERROR(EINVAL);
783 #endif
784 }
785
786 684 static int utf8_check(const uint8_t *str)
787 {
788 const uint8_t *byte;
789 uint32_t codepoint, min;
790
791
2/2
✓ Branch 0 taken 59671 times.
✓ Branch 1 taken 684 times.
60355 while (*str) {
792 59671 byte = str;
793
5/8
✓ Branch 0 taken 59671 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 59671 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2794 times.
✓ Branch 6 taken 2794 times.
✓ Branch 7 taken 59671 times.
62465 GET_UTF8(codepoint, *(byte++), return 0;);
794
4/4
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 57582 times.
✓ Branch 2 taken 705 times.
✓ Branch 3 taken 1384 times.
60376 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
795 705 1 << (5 * (byte - str) - 4);
796
3/6
✓ Branch 0 taken 59671 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 59671 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 59671 times.
✗ Branch 5 not taken.
59671 if (codepoint < min || codepoint >= 0x110000 ||
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59671 times.
59671 codepoint == 0xFFFE /* BOM */ ||
798 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
799 return 0;
800 59671 str = byte;
801 }
802 684 return 1;
803 }
804
805 1431 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
806 int *got_sub_ptr,
807 AVPacket *avpkt)
808 {
809 1431 int ret = 0;
810
811
3/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1393 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
1431 if (!avpkt->data && avpkt->size) {
812 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
813 return AVERROR(EINVAL);
814 }
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1431 times.
1431 if (!avctx->codec)
816 return AVERROR(EINVAL);
817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1431 times.
1431 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
818 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
819 return AVERROR(EINVAL);
820 }
821
822 1431 *got_sub_ptr = 0;
823 1431 get_subtitle_defaults(sub);
824
825
4/4
✓ Branch 0 taken 851 times.
✓ Branch 1 taken 580 times.
✓ Branch 2 taken 817 times.
✓ Branch 3 taken 34 times.
1431 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
826 1397 AVCodecInternal *avci = avctx->internal;
827 AVPacket *pkt;
828
829 1397 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1397 times.
1397 if (ret < 0)
831 79 return ret;
832
833
3/4
✓ Branch 0 taken 1397 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1391 times.
✓ Branch 3 taken 6 times.
1397 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
834 1391 sub->pts = av_rescale_q(avpkt->pts,
835 1391 avctx->pkt_timebase, AV_TIME_BASE_Q);
836 1397 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, pkt);
837
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1304 times.
1397 if (pkt == avci->buffer_pkt) // did we recode?
838 93 av_packet_unref(avci->buffer_pkt);
839
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1318 times.
1397 if (ret < 0) {
840 79 *got_sub_ptr = 0;
841 79 avsubtitle_free(sub);
842 79 return ret;
843 }
844 av_assert1(!sub->num_rects || *got_sub_ptr);
845
846
6/6
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 510 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 228 times.
✓ Branch 4 taken 528 times.
✓ Branch 5 taken 52 times.
1318 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
847
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 avctx->pkt_timebase.num) {
848 528 AVRational ms = { 1, 1000 };
849 528 sub->end_display_time = av_rescale_q(avpkt->duration,
850 avctx->pkt_timebase, ms);
851 }
852
853
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 1168 times.
1318 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
854 150 sub->format = 0;
855
1/2
✓ Branch 0 taken 1168 times.
✗ Branch 1 not taken.
1168 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
856 1168 sub->format = 1;
857
858
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 1318 times.
2157 for (unsigned i = 0; i < sub->num_rects; i++) {
859
1/2
✓ Branch 0 taken 839 times.
✗ Branch 1 not taken.
839 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
860
3/4
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 155 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 684 times.
839 sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
861 av_log(avctx, AV_LOG_ERROR,
862 "Invalid UTF-8 in decoded subtitles text; "
863 "maybe missing -sub_charenc option\n");
864 avsubtitle_free(sub);
865 *got_sub_ptr = 0;
866 return AVERROR_INVALIDDATA;
867 }
868 }
869
870
2/2
✓ Branch 0 taken 817 times.
✓ Branch 1 taken 501 times.
1318 if (*got_sub_ptr)
871 817 avctx->frame_number++;
872 }
873
874 1352 return ret;
875 }
876
877 1477 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
878 const enum AVPixelFormat *fmt)
879 {
880 const AVPixFmtDescriptor *desc;
881 const AVCodecHWConfig *config;
882 int i, n;
883
884 // If a device was supplied when the codec was opened, assume that the
885 // user wants to use it.
886
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1477 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1477 if (avctx->hw_device_ctx && avctx->codec->hw_configs) {
887 AVHWDeviceContext *device_ctx =
888 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
889 for (i = 0;; i++) {
890 config = &avctx->codec->hw_configs[i]->public;
891 if (!config)
892 break;
893 if (!(config->methods &
894 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
895 continue;
896 if (device_ctx->type != config->device_type)
897 continue;
898 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
899 if (config->pix_fmt == fmt[n])
900 return fmt[n];
901 }
902 }
903 }
904 // No device or other setup, so we have to choose from things which
905 // don't any other external information.
906
907 // If the last element of the list is a software format, choose it
908 // (this should be best software format if any exist).
909
2/2
✓ Branch 0 taken 3720 times.
✓ Branch 1 taken 1477 times.
5197 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
910 1477 desc = av_pix_fmt_desc_get(fmt[n - 1]);
911
1/2
✓ Branch 0 taken 1477 times.
✗ Branch 1 not taken.
1477 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
912 1477 return fmt[n - 1];
913
914 // Finally, traverse the list in order and choose the first entry
915 // with no external dependencies (if there is no hardware configuration
916 // information available then this just picks the first entry).
917 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
918 for (i = 0;; i++) {
919 config = avcodec_get_hw_config(avctx->codec, i);
920 if (!config)
921 break;
922 if (config->pix_fmt == fmt[n])
923 break;
924 }
925 if (!config) {
926 // No specific config available, so the decoder must be able
927 // to handle this format without any additional setup.
928 return fmt[n];
929 }
930 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
931 // Usable with only internal setup.
932 return fmt[n];
933 }
934 }
935
936 // Nothing is usable, give up.
937 return AV_PIX_FMT_NONE;
938 }
939
940 int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
941 enum AVHWDeviceType dev_type)
942 {
943 AVHWDeviceContext *device_ctx;
944 AVHWFramesContext *frames_ctx;
945 int ret;
946
947 if (!avctx->hwaccel)
948 return AVERROR(ENOSYS);
949
950 if (avctx->hw_frames_ctx)
951 return 0;
952 if (!avctx->hw_device_ctx) {
953 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
954 "required for hardware accelerated decoding.\n");
955 return AVERROR(EINVAL);
956 }
957
958 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
959 if (device_ctx->type != dev_type) {
960 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
961 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
962 av_hwdevice_get_type_name(device_ctx->type));
963 return AVERROR(EINVAL);
964 }
965
966 ret = avcodec_get_hw_frames_parameters(avctx,
967 avctx->hw_device_ctx,
968 avctx->hwaccel->pix_fmt,
969 &avctx->hw_frames_ctx);
970 if (ret < 0)
971 return ret;
972
973 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
974
975
976 if (frames_ctx->initial_pool_size) {
977 // We guarantee 4 base work surfaces. The function above guarantees 1
978 // (the absolute minimum), so add the missing count.
979 frames_ctx->initial_pool_size += 3;
980 }
981
982 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
983 if (ret < 0) {
984 av_buffer_unref(&avctx->hw_frames_ctx);
985 return ret;
986 }
987
988 return 0;
989 }
990
991 int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
992 AVBufferRef *device_ref,
993 enum AVPixelFormat hw_pix_fmt,
994 AVBufferRef **out_frames_ref)
995 {
996 AVBufferRef *frames_ref = NULL;
997 const AVCodecHWConfigInternal *hw_config;
998 const AVHWAccel *hwa;
999 int i, ret;
1000
1001 for (i = 0;; i++) {
1002 hw_config = avctx->codec->hw_configs[i];
1003 if (!hw_config)
1004 return AVERROR(ENOENT);
1005 if (hw_config->public.pix_fmt == hw_pix_fmt)
1006 break;
1007 }
1008
1009 hwa = hw_config->hwaccel;
1010 if (!hwa || !hwa->frame_params)
1011 return AVERROR(ENOENT);
1012
1013 frames_ref = av_hwframe_ctx_alloc(device_ref);
1014 if (!frames_ref)
1015 return AVERROR(ENOMEM);
1016
1017 ret = hwa->frame_params(avctx, frames_ref);
1018 if (ret >= 0) {
1019 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1020
1021 if (frames_ctx->initial_pool_size) {
1022 // If the user has requested that extra output surfaces be
1023 // available then add them here.
1024 if (avctx->extra_hw_frames > 0)
1025 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1026
1027 // If frame threading is enabled then an extra surface per thread
1028 // is also required.
1029 if (avctx->active_thread_type & FF_THREAD_FRAME)
1030 frames_ctx->initial_pool_size += avctx->thread_count;
1031 }
1032
1033 *out_frames_ref = frames_ref;
1034 } else {
1035 av_buffer_unref(&frames_ref);
1036 }
1037 return ret;
1038 }
1039
1040 static int hwaccel_init(AVCodecContext *avctx,
1041 const AVCodecHWConfigInternal *hw_config)
1042 {
1043 const AVHWAccel *hwaccel;
1044 int err;
1045
1046 hwaccel = hw_config->hwaccel;
1047 if (hwaccel->capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1048 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1049 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1050 hwaccel->name);
1051 return AVERROR_PATCHWELCOME;
1052 }
1053
1054 if (hwaccel->priv_data_size) {
1055 avctx->internal->hwaccel_priv_data =
1056 av_mallocz(hwaccel->priv_data_size);
1057 if (!avctx->internal->hwaccel_priv_data)
1058 return AVERROR(ENOMEM);
1059 }
1060
1061 avctx->hwaccel = hwaccel;
1062 if (hwaccel->init) {
1063 err = hwaccel->init(avctx);
1064 if (err < 0) {
1065 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
1066 "hwaccel initialisation returned error.\n",
1067 av_get_pix_fmt_name(hw_config->public.pix_fmt));
1068 av_freep(&avctx->internal->hwaccel_priv_data);
1069 avctx->hwaccel = NULL;
1070 return err;
1071 }
1072 }
1073
1074 return 0;
1075 }
1076
1077 2443 static void hwaccel_uninit(AVCodecContext *avctx)
1078 {
1079
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2443 if (avctx->hwaccel && avctx->hwaccel->uninit)
1080 avctx->hwaccel->uninit(avctx);
1081
1082 2443 av_freep(&avctx->internal->hwaccel_priv_data);
1083
1084 2443 avctx->hwaccel = NULL;
1085
1086 2443 av_buffer_unref(&avctx->hw_frames_ctx);
1087 2443 }
1088
1089 2443 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1090 {
1091 const AVPixFmtDescriptor *desc;
1092 enum AVPixelFormat *choices;
1093 enum AVPixelFormat ret, user_choice;
1094 const AVCodecHWConfigInternal *hw_config;
1095 const AVCodecHWConfig *config;
1096 int i, n, err;
1097
1098 // Find end of list.
1099
2/2
✓ Branch 0 taken 6313 times.
✓ Branch 1 taken 2443 times.
8756 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1100 // Must contain at least one entry.
1101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
2443 av_assert0(n >= 1);
1102 // If a software format is available, it must be the last entry.
1103 2443 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1104
1/2
✓ Branch 0 taken 2443 times.
✗ Branch 1 not taken.
2443 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1105 // No software format is available.
1106 } else {
1107 2443 avctx->sw_pix_fmt = fmt[n - 1];
1108 }
1109
1110 2443 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
2443 if (!choices)
1112 return AV_PIX_FMT_NONE;
1113
1114 for (;;) {
1115 // Remove the previous hwaccel, if there was one.
1116 2443 hwaccel_uninit(avctx);
1117
1118 2443 user_choice = avctx->get_format(avctx, choices);
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
2443 if (user_choice == AV_PIX_FMT_NONE) {
1120 // Explicitly chose nothing, give up.
1121 ret = AV_PIX_FMT_NONE;
1122 break;
1123 }
1124
1125 2443 desc = av_pix_fmt_desc_get(user_choice);
1126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
2443 if (!desc) {
1127 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
1128 "get_format() callback.\n");
1129 ret = AV_PIX_FMT_NONE;
1130 break;
1131 }
1132 2443 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
1133 2443 desc->name);
1134
1135
1/2
✓ Branch 0 taken 6313 times.
✗ Branch 1 not taken.
6313 for (i = 0; i < n; i++) {
1136
2/2
✓ Branch 0 taken 2443 times.
✓ Branch 1 taken 3870 times.
6313 if (choices[i] == user_choice)
1137 2443 break;
1138 }
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2443 times.
2443 if (i == n) {
1140 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
1141 "%s not in possible list.\n", desc->name);
1142 ret = AV_PIX_FMT_NONE;
1143 break;
1144 }
1145
1146
2/2
✓ Branch 0 taken 2322 times.
✓ Branch 1 taken 121 times.
2443 if (avctx->codec->hw_configs) {
1147 6407 for (i = 0;; i++) {
1148 6407 hw_config = avctx->codec->hw_configs[i];
1149
2/2
✓ Branch 0 taken 2322 times.
✓ Branch 1 taken 4085 times.
6407 if (!hw_config)
1150 2322 break;
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4085 times.
4085 if (hw_config->public.pix_fmt == user_choice)
1152 break;
1153 }
1154 } else {
1155 121 hw_config = NULL;
1156 }
1157
1158
1/2
✓ Branch 0 taken 2443 times.
✗ Branch 1 not taken.
2443 if (!hw_config) {
1159 // No config available, so no extra setup required.
1160 2443 ret = user_choice;
1161 2443 break;
1162 }
1163 config = &hw_config->public;
1164
1165 if (config->methods &
1166 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1167 avctx->hw_frames_ctx) {
1168 const AVHWFramesContext *frames_ctx =
1169 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1170 if (frames_ctx->format != user_choice) {
1171 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1172 "does not match the format of the provided frames "
1173 "context.\n", desc->name);
1174 goto try_again;
1175 }
1176 } else if (config->methods &
1177 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1178 avctx->hw_device_ctx) {
1179 const AVHWDeviceContext *device_ctx =
1180 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1181 if (device_ctx->type != config->device_type) {
1182 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1183 "does not match the type of the provided device "
1184 "context.\n", desc->name);
1185 goto try_again;
1186 }
1187 } else if (config->methods &
1188 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1189 // Internal-only setup, no additional configuration.
1190 } else if (config->methods &
1191 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1192 // Some ad-hoc configuration we can't see and can't check.
1193 } else {
1194 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1195 "missing configuration.\n", desc->name);
1196 goto try_again;
1197 }
1198 if (hw_config->hwaccel) {
1199 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
1200 "initialisation.\n", desc->name);
1201 err = hwaccel_init(avctx, hw_config);
1202 if (err < 0)
1203 goto try_again;
1204 }
1205 ret = user_choice;
1206 break;
1207
1208 try_again:
1209 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
1210 "get_format() without it.\n", desc->name);
1211 for (i = 0; i < n; i++) {
1212 if (choices[i] == user_choice)
1213 break;
1214 }
1215 for (; i + 1 < n; i++)
1216 choices[i] = choices[i + 1];
1217 --n;
1218 }
1219
1220 2443 av_freep(&choices);
1221 2443 return ret;
1222 }
1223
1224 16918 static void frame_pool_free(void *opaque, uint8_t *data)
1225 {
1226 16918 FramePool *pool = (FramePool*)data;
1227 int i;
1228
1229
2/2
✓ Branch 0 taken 67672 times.
✓ Branch 1 taken 16918 times.
84590 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1230 67672 av_buffer_pool_uninit(&pool->pools[i]);
1231
1232 16918 av_freep(&data);
1233 16918 }
1234
1235 16918 static AVBufferRef *frame_pool_alloc(void)
1236 {
1237 16918 FramePool *pool = av_mallocz(sizeof(*pool));
1238 AVBufferRef *buf;
1239
1240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16918 times.
16918 if (!pool)
1241 return NULL;
1242
1243 16918 buf = av_buffer_create((uint8_t*)pool, sizeof(*pool),
1244 frame_pool_free, NULL, 0);
1245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16918 times.
16918 if (!buf) {
1246 av_freep(&pool);
1247 return NULL;
1248 }
1249
1250 16918 return buf;
1251 }
1252
1253 386987 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
1254 {
1255 773974 FramePool *pool = avctx->internal->pool ?
1256
2/2
✓ Branch 0 taken 376891 times.
✓ Branch 1 taken 10096 times.
386987 (FramePool*)avctx->internal->pool->data : NULL;
1257 AVBufferRef *pool_buf;
1258 int i, ret, ch, planes;
1259
1260
2/2
✓ Branch 0 taken 293832 times.
✓ Branch 1 taken 93155 times.
386987 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1261 293832 int planar = av_sample_fmt_is_planar(frame->format);
1262 293832 ch = frame->channels;
1263
2/2
✓ Branch 0 taken 160461 times.
✓ Branch 1 taken 133371 times.
293832 planes = planar ? ch : 1;
1264 }
1265
1266
4/4
✓ Branch 0 taken 376891 times.
✓ Branch 1 taken 10096 times.
✓ Branch 2 taken 376716 times.
✓ Branch 3 taken 175 times.
386987 if (pool && pool->format == frame->format) {
1267
2/2
✓ Branch 0 taken 84658 times.
✓ Branch 1 taken 292058 times.
376716 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1268
4/4
✓ Branch 0 taken 84418 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 84407 times.
✓ Branch 3 taken 11 times.
84658 pool->width == frame->width && pool->height == frame->height)
1269 84407 return 0;
1270
4/4
✓ Branch 0 taken 292058 times.
✓ Branch 1 taken 251 times.
✓ Branch 2 taken 292045 times.
✓ Branch 3 taken 13 times.
292309 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && pool->planes == planes &&
1271
3/4
✓ Branch 0 taken 292045 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 285662 times.
✓ Branch 3 taken 6383 times.
292045 pool->channels == ch && frame->nb_samples == pool->samples)
1272 285662 return 0;
1273 }
1274
1275 16918 pool_buf = frame_pool_alloc();
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16918 times.
16918 if (!pool_buf)
1277 return AVERROR(ENOMEM);
1278 16918 pool = (FramePool*)pool_buf->data;
1279
1280
2/3
✓ Branch 0 taken 8748 times.
✓ Branch 1 taken 8170 times.
✗ Branch 2 not taken.
16918 switch (avctx->codec_type) {
1281 8748 case AVMEDIA_TYPE_VIDEO: {
1282 int linesize[4];
1283 8748 int w = frame->width;
1284 8748 int h = frame->height;
1285 int unaligned;
1286 ptrdiff_t linesize1[4];
1287 size_t size[4];
1288
1289 8748 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
1290
1291 do {
1292 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
1293 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
1294 17616 ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
1295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17616 times.
17616 if (ret < 0)
1296 goto fail;
1297 // increase alignment of w for next try (rhs gives the lowest bit set in w)
1298 17616 w += w & ~(w - 1);
1299
1300 17616 unaligned = 0;
1301
2/2
✓ Branch 0 taken 70464 times.
✓ Branch 1 taken 17616 times.
88080 for (i = 0; i < 4; i++)
1302 70464 unaligned |= linesize[i] % pool->stride_align[i];
1303
2/2
✓ Branch 0 taken 8868 times.
✓ Branch 1 taken 8748 times.
17616 } while (unaligned);
1304
1305
2/2
✓ Branch 0 taken 34992 times.
✓ Branch 1 taken 8748 times.
43740 for (i = 0; i < 4; i++)
1306 34992 linesize1[i] = linesize[i];
1307 8748 ret = av_image_fill_plane_sizes(size, avctx->pix_fmt, h, linesize1);
1308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8748 times.
8748 if (ret < 0)
1309 goto fail;
1310
1311
2/2
✓ Branch 0 taken 34992 times.
✓ Branch 1 taken 8748 times.
43740 for (i = 0; i < 4; i++) {
1312 34992 pool->linesize[i] = linesize[i];
1313
2/2
✓ Branch 0 taken 24759 times.
✓ Branch 1 taken 10233 times.
34992 if (size[i]) {
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24759 times.
24759 if (size[i] > INT_MAX - (16 + STRIDE_ALIGN - 1)) {
1315 ret = AVERROR(EINVAL);
1316 goto fail;
1317 }
1318 24759 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
1319 CONFIG_MEMORY_POISONING ?
1320 NULL :
1321 av_buffer_allocz);
1322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24759 times.
24759 if (!pool->pools[i]) {
1323 ret = AVERROR(ENOMEM);
1324 goto fail;
1325 }
1326 }
1327 }
1328 8748 pool->format = frame->format;
1329 8748 pool->width = frame->width;
1330 8748 pool->height = frame->height;
1331
1332 8748 break;
1333 }
1334 8170 case AVMEDIA_TYPE_AUDIO: {
1335 8170 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
1336 8170 frame->nb_samples, frame->format, 0);
1337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8170 times.
8170 if (ret < 0)
1338 goto fail;
1339
1340 8170 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8170 times.
8170 if (!pool->pools[0]) {
1342 ret = AVERROR(ENOMEM);
1343 goto fail;
1344 }
1345
1346 8170 pool->format = frame->format;
1347 8170 pool->planes = planes;
1348 8170 pool->channels = ch;
1349 8170 pool->samples = frame->nb_samples;
1350 8170 break;
1351 }
1352 default: av_assert0(0);
1353 }
1354
1355 16918 av_buffer_unref(&avctx->internal->pool);
1356 16918 avctx->internal->pool = pool_buf;
1357
1358 16918 return 0;
1359 fail:
1360 av_buffer_unref(&pool_buf);
1361 return ret;
1362 }
1363
1364 293832 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
1365 {
1366 293832 FramePool *pool = (FramePool*)avctx->internal->pool->data;
1367 293832 int planes = pool->planes;
1368 int i;
1369
1370 293832 frame->linesize[0] = pool->linesize[0];
1371
1372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293832 times.
293832 if (planes > AV_NUM_DATA_POINTERS) {
1373 frame->extended_data = av_calloc(planes, sizeof(*frame->extended_data));
1374 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
1375 frame->extended_buf = av_calloc(frame->nb_extended_buf,
1376 sizeof(*frame->extended_buf));
1377 if (!frame->extended_data || !frame->extended_buf) {
1378 av_freep(&frame->extended_data);
1379 av_freep(&frame->extended_buf);
1380 return AVERROR(ENOMEM);
1381 }
1382 } else {
1383 293832 frame->extended_data = frame->data;
1384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293832 times.
293832 av_assert0(frame->nb_extended_buf == 0);
1385 }
1386
1387
2/2
✓ Branch 0 taken 436321 times.
✓ Branch 1 taken 293832 times.
730153 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
1388 436321 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 436321 times.
436321 if (!frame->buf[i])
1390 goto fail;
1391 436321 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
1392 }
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293832 times.
293832 for (i = 0; i < frame->nb_extended_buf; i++) {
1394 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
1395 if (!frame->extended_buf[i])
1396 goto fail;
1397 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
1398 }
1399
1400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293832 times.
293832 if (avctx->debug & FF_DEBUG_BUFFERS)
1401 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
1402
1403 293832 return 0;
1404 fail:
1405 av_frame_unref(frame);
1406 return AVERROR(ENOMEM);
1407 }
1408
1409 93155 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
1410 {
1411 93155 FramePool *pool = (FramePool*)s->internal->pool->data;
1412 93155 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
1413 int i;
1414
1415
4/8
✓ Branch 0 taken 93155 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93155 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 93155 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 93155 times.
93155 if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
1416 av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
1417 return -1;
1418 }
1419
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93155 times.
93155 if (!desc) {
1421 av_log(s, AV_LOG_ERROR,
1422 "Unable to get pixel format descriptor for format %s\n",
1423 av_get_pix_fmt_name(pic->format));
1424 return AVERROR(EINVAL);
1425 }
1426
1427 93155 memset(pic->data, 0, sizeof(pic->data));
1428 93155 pic->extended_data = pic->data;
1429
1430
4/4
✓ Branch 0 taken 359068 times.
✓ Branch 1 taken 657 times.
✓ Branch 2 taken 266570 times.
✓ Branch 3 taken 92498 times.
359725 for (i = 0; i < 4 && pool->pools[i]; i++) {
1431 266570 pic->linesize[i] = pool->linesize[i];
1432
1433 266570 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266570 times.
266570 if (!pic->buf[i])
1435 goto fail;
1436
1437 266570 pic->data[i] = pic->buf[i]->data;
1438 }
1439
2/2
✓ Branch 0 taken 478670 times.
✓ Branch 1 taken 93155 times.
571825 for (; i < AV_NUM_DATA_POINTERS; i++) {
1440 478670 pic->data[i] = NULL;
1441 478670 pic->linesize[i] = 0;
1442 }
1443
1444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93155 times.
93155 if (s->debug & FF_DEBUG_BUFFERS)
1445 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
1446
1447 93155 return 0;
1448 fail:
1449 av_frame_unref(pic);
1450 return AVERROR(ENOMEM);
1451 }
1452
1453 386987 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
1454 {
1455 int ret;
1456
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386987 times.
386987 if (avctx->hw_frames_ctx) {
1458 ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
1459 frame->width = avctx->coded_width;
1460 frame->height = avctx->coded_height;
1461 return ret;
1462 }
1463
1464
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 386987 times.
386987 if ((ret = update_frame_pool(avctx, frame)) < 0)
1465 return ret;
1466
1467
2/3
✓ Branch 0 taken 93155 times.
✓ Branch 1 taken 293832 times.
✗ Branch 2 not taken.
386987 switch (avctx->codec_type) {
1468 93155 case AVMEDIA_TYPE_VIDEO:
1469 93155 return video_get_buffer(avctx, frame);
1470 293832 case AVMEDIA_TYPE_AUDIO:
1471 293832 return audio_get_buffer(avctx, frame);
1472 default:
1473 return -1;
1474 }
1475 }
1476
1477 413846 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1478 {
1479 size_t size;
1480 const uint8_t *side_metadata;
1481
1482 413846 AVDictionary **frame_md = &frame->metadata;
1483
1484 413846 side_metadata = av_packet_get_side_data(avpkt,
1485 AV_PKT_DATA_STRINGS_METADATA, &size);
1486 413846 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1487 }
1488
1489 413846 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1490 {
1491 413846 AVPacket *pkt = avctx->internal->last_pkt_props;
1492 static const struct {
1493 enum AVPacketSideDataType packet;
1494 enum AVFrameSideDataType frame;
1495 } sd[] = {
1496 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
1497 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
1498 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
1499 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
1500 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
1501 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
1502 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
1503 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1504 { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
1505 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1506 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1507 };
1508
1509
1/2
✓ Branch 0 taken 413846 times.
✗ Branch 1 not taken.
413846 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
1510 413846 frame->pts = pkt->pts;
1511 413846 frame->pkt_pos = pkt->pos;
1512 413846 frame->pkt_duration = pkt->duration;
1513 413846 frame->pkt_size = pkt->size;
1514
1515
2/2
✓ Branch 0 taken 4552306 times.
✓ Branch 1 taken 413846 times.
4966152 for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1516 size_t size;
1517 4552306 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1518
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 4552095 times.
4552306 if (packet_sd) {
1519 211 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1520 211 sd[i].frame,
1521 size);
1522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
211 if (!frame_sd)
1523 return AVERROR(ENOMEM);
1524
1525 211 memcpy(frame_sd->data, packet_sd, size);
1526 }
1527 }
1528 413846 add_metadata_from_side_data(pkt, frame);
1529
1530
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 413646 times.
413846 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1531 200 frame->flags |= AV_FRAME_FLAG_DISCARD;
1532 } else {
1533 413646 frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
1534 }
1535 }
1536 413846 frame->reordered_opaque = avctx->reordered_opaque;
1537
1538
1/2
✓ Branch 0 taken 413846 times.
✗ Branch 1 not taken.
413846 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
1539 413846 frame->color_primaries = avctx->color_primaries;
1540
1/2
✓ Branch 0 taken 413846 times.
✗ Branch 1 not taken.
413846 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
1541 413846 frame->color_trc = avctx->color_trc;
1542
2/2
✓ Branch 0 taken 413411 times.
✓ Branch 1 taken 435 times.
413846 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
1543 413411 frame->colorspace = avctx->colorspace;
1544
2/2
✓ Branch 0 taken 413634 times.
✓ Branch 1 taken 212 times.
413846 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
1545 413634 frame->color_range = avctx->color_range;
1546
1/2
✓ Branch 0 taken 413846 times.
✗ Branch 1 not taken.
413846 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
1547 413846 frame->chroma_location = avctx->chroma_sample_location;
1548
1549
2/3
✓ Branch 0 taken 120014 times.
✓ Branch 1 taken 293832 times.
✗ Branch 2 not taken.
413846 switch (avctx->codec->type) {
1550 120014 case AVMEDIA_TYPE_VIDEO:
1551 120014 frame->format = avctx->pix_fmt;
1552
2/2
✓ Branch 0 taken 119895 times.
✓ Branch 1 taken 119 times.
120014 if (!frame->sample_aspect_ratio.num)
1553 119895 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
1554
1555
4/6
✓ Branch 0 taken 98518 times.
✓ Branch 1 taken 21496 times.
✓ Branch 2 taken 98518 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 98518 times.
218532 if (frame->width && frame->height &&
1556 98518 av_image_check_sar(frame->width, frame->height,
1557 frame->sample_aspect_ratio) < 0) {
1558 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1559 frame->sample_aspect_ratio.num,
1560 frame->sample_aspect_ratio.den);
1561 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1562 }
1563
1564 120014 break;
1565 293832 case AVMEDIA_TYPE_AUDIO:
1566
1/2
✓ Branch 0 taken 293832 times.
✗ Branch 1 not taken.
293832 if (!frame->sample_rate)
1567 293832 frame->sample_rate = avctx->sample_rate;
1568
1/2
✓ Branch 0 taken 293832 times.
✗ Branch 1 not taken.
293832 if (frame->format < 0)
1569 293832 frame->format = avctx->sample_fmt;
1570
1/2
✓ Branch 0 taken 293832 times.
✗ Branch 1 not taken.
293832 if (!frame->channel_layout) {
1571
2/2
✓ Branch 0 taken 292744 times.
✓ Branch 1 taken 1088 times.
293832 if (avctx->channel_layout) {
1572 292744 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
1573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 292744 times.
292744 avctx->channels) {
1574 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
1575 "configuration.\n");
1576 return AVERROR(EINVAL);
1577 }
1578
1579 292744 frame->channel_layout = avctx->channel_layout;
1580 } else {
1581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1088 times.
1088 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1582 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
1583 avctx->channels);
1584 return AVERROR(ENOSYS);
1585 }
1586 }
1587 }
1588 293832 frame->channels = avctx->channels;
1589 293832 break;
1590 }
1591 413846 return 0;
1592 }
1593
1594 386908 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1595 {
1596
2/2
✓ Branch 0 taken 93076 times.
✓ Branch 1 taken 293832 times.
386908 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1597 int i;
1598 93076 int num_planes = av_pix_fmt_count_planes(frame->format);
1599 93076 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1600
1/2
✓ Branch 0 taken 93076 times.
✗ Branch 1 not taken.
93076 int flags = desc ? desc->flags : 0;
1601
4/4
✓ Branch 0 taken 7804 times.
✓ Branch 1 taken 85272 times.
✓ Branch 2 taken 2056 times.
✓ Branch 3 taken 5748 times.
93076 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1602 2056 num_planes = 2;
1603
2/2
✓ Branch 0 taken 266333 times.
✓ Branch 1 taken 93076 times.
359409 for (i = 0; i < num_planes; i++) {
1604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266333 times.
266333 av_assert0(frame->data[i]);
1605 }
1606 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1607
3/4
✓ Branch 0 taken 571351 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 478275 times.
✓ Branch 3 taken 93076 times.
571351 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 478275 times.
478275 if (frame->data[i])
1609 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1610 478275 frame->data[i] = NULL;
1611 }
1612 }
1613 386908 }
1614
1615 386908 static void decode_data_free(void *opaque, uint8_t *data)
1616 {
1617 386908 FrameDecodeData *fdd = (FrameDecodeData*)data;
1618
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (fdd->post_process_opaque_free)
1620 fdd->post_process_opaque_free(fdd->post_process_opaque);
1621
1622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (fdd->hwaccel_priv_free)
1623 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1624
1625 386908 av_freep(&fdd);
1626 386908 }
1627
1628 386908 int ff_attach_decode_data(AVFrame *frame)
1629 {
1630 AVBufferRef *fdd_buf;
1631 FrameDecodeData *fdd;
1632
1633 av_assert1(!frame->private_ref);
1634 386908 av_buffer_unref(&frame->private_ref);
1635
1636 386908 fdd = av_mallocz(sizeof(*fdd));
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (!fdd)
1638 return AVERROR(ENOMEM);
1639
1640 386908 fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
1641 NULL, AV_BUFFER_FLAG_READONLY);
1642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (!fdd_buf) {
1643 av_freep(&fdd);
1644 return AVERROR(ENOMEM);
1645 }
1646
1647 386908 frame->private_ref = fdd_buf;
1648
1649 386908 return 0;
1650 }
1651
1652 387007 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1653 {
1654 387007 const AVHWAccel *hwaccel = avctx->hwaccel;
1655 387007 int override_dimensions = 1;
1656 int ret;
1657
1658
2/2
✓ Branch 0 taken 93175 times.
✓ Branch 1 taken 293832 times.
387007 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1659
2/4
✓ Branch 0 taken 93175 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93175 times.
✗ Branch 3 not taken.
186350 if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
1660
2/2
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 93076 times.
186350 (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) {
1661 99 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1662 99 ret = AVERROR(EINVAL);
1663 99 goto fail;
1664 }
1665
1666
3/4
✓ Branch 0 taken 9801 times.
✓ Branch 1 taken 83275 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9801 times.
93076 if (frame->width <= 0 || frame->height <= 0) {
1667 83275 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1668 83275 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1669 83275 override_dimensions = 0;
1670 }
1671
1672
4/8
✓ Branch 0 taken 93076 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93076 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 93076 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 93076 times.
93076 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1673 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1674 ret = AVERROR(EINVAL);
1675 goto fail;
1676 }
1677
1/2
✓ Branch 0 taken 293832 times.
✗ Branch 1 not taken.
293832 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293832 times.
293832 if (frame->nb_samples * (int64_t)avctx->channels > avctx->max_samples) {
1679 av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
1680 ret = AVERROR(EINVAL);
1681 goto fail;
1682 }
1683 }
1684 386908 ret = ff_decode_frame_props(avctx, frame);
1685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (ret < 0)
1686 goto fail;
1687
1688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (hwaccel) {
1689 if (hwaccel->alloc_frame) {
1690 ret = hwaccel->alloc_frame(avctx, frame);
1691 goto end;
1692 }
1693 } else
1694 386908 avctx->sw_pix_fmt = avctx->pix_fmt;
1695
1696 386908 ret = avctx->get_buffer2(avctx, frame, flags);
1697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (ret < 0)
1698 goto fail;
1699
1700 386908 validate_avframe_allocation(avctx, frame);
1701
1702 386908 ret = ff_attach_decode_data(frame);
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386908 times.
386908 if (ret < 0)
1704 goto fail;
1705
1706 386908 end:
1707
4/4
✓ Branch 0 taken 293832 times.
✓ Branch 1 taken 93076 times.
✓ Branch 2 taken 9801 times.
✓ Branch 3 taken 83275 times.
386908 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1708
2/2
✓ Branch 0 taken 33222 times.
✓ Branch 1 taken 50053 times.
83275 !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1709 50053 frame->width = avctx->width;
1710 50053 frame->height = avctx->height;
1711 }
1712
1713 336855 fail:
1714
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 386908 times.
387007 if (ret < 0) {
1715 99 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1716 99 av_frame_unref(frame);
1717 }
1718
1719 387007 return ret;
1720 }
1721
1722 7012 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1723 {
1724 AVFrame *tmp;
1725 int ret;
1726
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7012 times.
7012 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1728
1729
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)) {
1730 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1731 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1732 av_frame_unref(frame);
1733 }
1734
1735
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 6890 times.
7012 if (!frame->data[0])
1736 122 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1737
1738
4/4
✓ Branch 0 taken 6889 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 5332 times.
✓ Branch 4 taken 1557 times.
6890 if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
1739 5333 return ff_decode_frame_props(avctx, frame);
1740
1741 1557 tmp = av_frame_alloc();
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1557 times.
1557 if (!tmp)
1743 return AVERROR(ENOMEM);
1744
1745 1557 av_frame_move_ref(tmp, frame);
1746
1747 1557 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1557 times.
1557 if (ret < 0) {
1749 av_frame_free(&tmp);
1750 return ret;
1751 }
1752
1753 1557 av_frame_copy(frame, tmp);
1754 1557 av_frame_free(&tmp);
1755
1756 1557 return 0;
1757 }
1758
1759 7012 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1760 {
1761 7012 int ret = reget_buffer_internal(avctx, frame, flags);
1762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7012 times.
7012 if (ret < 0)
1763 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1764 7012 return ret;
1765 }
1766
1767 13210 int ff_decode_preinit(AVCodecContext *avctx)
1768 {
1769 13210 int ret = 0;
1770
1771 /* if the decoder init function was already called previously,
1772 * free the already allocated subtitle_header before overwriting it */
1773 13210 av_freep(&avctx->subtitle_header);
1774
1775 #if FF_API_THREAD_SAFE_CALLBACKS
1776 FF_DISABLE_DEPRECATION_WARNINGS
1777
2/2
✓ Branch 0 taken 13166 times.
✓ Branch 1 taken 44 times.
13210 if ((avctx->thread_type & FF_THREAD_FRAME) &&
1778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13166 times.
13166 avctx->get_buffer2 != avcodec_default_get_buffer2 &&
1779 !avctx->thread_safe_callbacks) {
1780 av_log(avctx, AV_LOG_WARNING, "Requested frame threading with a "
1781 "custom get_buffer2() implementation which is not marked as "
1782 "thread safe. This is not supported anymore, make your "
1783 "callback thread-safe.\n");
1784 }
1785 FF_ENABLE_DEPRECATION_WARNINGS
1786 #endif
1787
1788
4/4
✓ Branch 0 taken 2919 times.
✓ Branch 1 taken 10291 times.
✓ Branch 2 taken 215 times.
✓ Branch 3 taken 2704 times.
13210 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && avctx->channels == 0 &&
1789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 !(avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
1790 av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
1791 return AVERROR(EINVAL);
1792 }
1793
2/4
✓ Branch 0 taken 13210 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13210 times.
13210 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1794 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
1795 avctx->codec->max_lowres);
1796 avctx->lowres = avctx->codec->max_lowres;
1797 }
1798
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13204 times.
13210 if (avctx->sub_charenc) {
1799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1800 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1801 "supported with subtitles codecs\n");
1802 return AVERROR(EINVAL);
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1804 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1805 "subtitles character encoding will be ignored\n",
1806 avctx->codec_descriptor->name);
1807 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1808 } else {
1809 /* input character encoding is set for a text based subtitle
1810 * codec at this point */
1811
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1812 6 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1813
1814
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1815 #if CONFIG_ICONV
1816 6 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cd == (iconv_t)-1) {
1818 ret = AVERROR(errno);
1819 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1820 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1821 return ret;
1822 }
1823 6 iconv_close(cd);
1824 #else
1825 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1826 "conversion needs a libavcodec built with iconv support "
1827 "for this codec\n");
1828 return AVERROR(ENOSYS);
1829 #endif
1830 }
1831 }
1832 }
1833
1834 13210 avctx->pts_correction_num_faulty_pts =
1835 13210 avctx->pts_correction_num_faulty_dts = 0;
1836 13210 avctx->pts_correction_last_pts =
1837 13210 avctx->pts_correction_last_dts = INT64_MIN;
1838
1839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13210 times.
13210 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1840 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1841 av_log(avctx, AV_LOG_WARNING,
1842 "gray decoding requested but not enabled at configuration time\n");
1843
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13205 times.
13210 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
1844 5 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
1845 }
1846
1847 13210 ret = decode_bsfs_init(avctx);
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13210 times.
13210 if (ret < 0)
1849 return ret;
1850
1851 13210 return 0;
1852 }
1853
1854 1263 int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
1855 {
1856 size_t size;
1857 1263 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
1858
1859
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) {
1860 24 memcpy(dst, pal, AVPALETTE_SIZE);
1861 24 return 1;
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 } else if (pal) {
1863 av_log(logctx, AV_LOG_ERROR,
1864 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
1865 }
1866 1239 return 0;
1867 }
1868