FFmpeg coverage


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