FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 639 1021 62.6%
Functions: 39 42 92.9%
Branches: 443 793 55.9%

Line Branch Exec Source
1 /*
2 * generic decoding-related code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include "config.h"
25
26 #if CONFIG_ICONV
27 # include <iconv.h>
28 #endif
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/common.h"
33 #include "libavutil/emms.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/hwcontext.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/internal.h"
38
39 #include "avcodec.h"
40 #include "avcodec_internal.h"
41 #include "bytestream.h"
42 #include "bsf.h"
43 #include "codec_desc.h"
44 #include "codec_internal.h"
45 #include "decode.h"
46 #include "hwaccel_internal.h"
47 #include "hwconfig.h"
48 #include "internal.h"
49 #include "packet_internal.h"
50 #include "refstruct.h"
51 #include "thread.h"
52
53 typedef struct DecodeContext {
54 AVCodecInternal avci;
55
56 /* to prevent infinite loop on errors when draining */
57 int nb_draining_errors;
58
59 /**
60 * The caller has submitted a NULL packet on input.
61 */
62 int draining_started;
63 } DecodeContext;
64
65 1283283 static DecodeContext *decode_ctx(AVCodecInternal *avci)
66 {
67 1283283 return (DecodeContext *)avci;
68 }
69
70 426952 static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
71 {
72 int ret;
73 size_t size;
74 const uint8_t *data;
75 uint32_t flags;
76 int64_t val;
77
78 426952 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
79
2/2
✓ Branch 0 taken 426950 times.
✓ Branch 1 taken 2 times.
426952 if (!data)
80 426950 return 0;
81
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
83 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
84 "changes, but PARAM_CHANGE side data was sent to it.\n");
85 ret = AVERROR(EINVAL);
86 goto fail2;
87 }
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 4)
90 goto fail;
91
92 2 flags = bytestream_get_le32(&data);
93 2 size -= 4;
94
95 #if FF_API_OLD_CHANNEL_LAYOUT
96 FF_DISABLE_DEPRECATION_WARNINGS
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
98 if (size < 4)
99 goto fail;
100 val = bytestream_get_le32(&data);
101 if (val <= 0 || val > INT_MAX) {
102 av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
103 ret = AVERROR_INVALIDDATA;
104 goto fail2;
105 }
106 av_channel_layout_uninit(&avctx->ch_layout);
107 avctx->ch_layout.nb_channels = val;
108 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
109 size -= 4;
110 }
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
112 if (size < 8)
113 goto fail;
114 av_channel_layout_uninit(&avctx->ch_layout);
115 ret = av_channel_layout_from_mask(&avctx->ch_layout, bytestream_get_le64(&data));
116 if (ret < 0)
117 goto fail2;
118 size -= 8;
119 }
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & (AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT |
121 AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT)) {
122 avctx->channels = avctx->ch_layout.nb_channels;
123 avctx->channel_layout = (avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) ?
124 avctx->ch_layout.u.mask : 0;
125 }
126 FF_ENABLE_DEPRECATION_WARNINGS
127 #endif
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
129 if (size < 4)
130 goto fail;
131 val = bytestream_get_le32(&data);
132 if (val <= 0 || val > INT_MAX) {
133 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
134 ret = AVERROR_INVALIDDATA;
135 goto fail2;
136 }
137 avctx->sample_rate = val;
138 size -= 4;
139 }
140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 8)
142 goto fail;
143 2 avctx->width = bytestream_get_le32(&data);
144 2 avctx->height = bytestream_get_le32(&data);
145 2 size -= 8;
146 2 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
148 goto fail2;
149 }
150
151 2 return 0;
152 fail:
153 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
154 ret = AVERROR_INVALIDDATA;
155 fail2:
156 if (ret < 0) {
157 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
158 if (avctx->err_recognition & AV_EF_EXPLODE)
159 return ret;
160 }
161 return 0;
162 }
163
164 426952 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
165 {
166 426952 int ret = 0;
167
168 426952 av_packet_unref(avci->last_pkt_props);
169
1/2
✓ Branch 0 taken 426952 times.
✗ Branch 1 not taken.
426952 if (pkt) {
170 426952 ret = av_packet_copy_props(avci->last_pkt_props, pkt);
171 #if FF_API_FRAME_PKT
172
1/2
✓ Branch 0 taken 426952 times.
✗ Branch 1 not taken.
426952 if (!ret)
173 426952 avci->last_pkt_props->stream_index = pkt->size; // Needed for ff_decode_frame_props().
174 #endif
175 }
176 426952 return ret;
177 }
178
179 14361 static int decode_bsfs_init(AVCodecContext *avctx)
180 {
181 14361 AVCodecInternal *avci = avctx->internal;
182 14361 const FFCodec *const codec = ffcodec(avctx->codec);
183 int ret;
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if (avci->bsf)
186 return 0;
187
188 14361 ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if (ret < 0) {
190 av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret));
191 if (ret != AVERROR(ENOMEM))
192 ret = AVERROR_BUG;
193 goto fail;
194 }
195
196 /* We do not currently have an API for passing the input timebase into decoders,
197 * but no filters used here should actually need it.
198 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
199 14361 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
200 14361 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if (ret < 0)
202 goto fail;
203
204 14361 ret = av_bsf_init(avci->bsf);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if (ret < 0)
206 goto fail;
207
208 14361 return 0;
209 fail:
210 av_bsf_free(&avci->bsf);
211 return ret;
212 }
213
214 1283139 static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
215 {
216 1283139 AVCodecInternal *avci = avctx->internal;
217 int ret;
218
219 1283139 ret = av_bsf_receive_packet(avci->bsf, pkt);
220
2/2
✓ Branch 0 taken 3403 times.
✓ Branch 1 taken 1279736 times.
1283139 if (ret == AVERROR_EOF)
221 3403 avci->draining = 1;
222
2/2
✓ Branch 0 taken 856187 times.
✓ Branch 1 taken 426952 times.
1283139 if (ret < 0)
223 856187 return ret;
224
225
1/2
✓ Branch 1 taken 426952 times.
✗ Branch 2 not taken.
426952 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
226 426952 ret = extract_packet_props(avctx->internal, pkt);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 426952 times.
426952 if (ret < 0)
228 goto finish;
229 }
230
231 426952 ret = apply_param_change(avctx, pkt);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 426952 times.
426952 if (ret < 0)
233 goto finish;
234
235 426952 return 0;
236 finish:
237 av_packet_unref(pkt);
238 return ret;
239 }
240
241 852865 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
242 {
243 852865 AVCodecInternal *avci = avctx->internal;
244 852865 DecodeContext *dc = decode_ctx(avci);
245
246
2/2
✓ Branch 0 taken 852848 times.
✓ Branch 1 taken 17 times.
852865 if (avci->draining)
247 17 return AVERROR_EOF;
248
249 430291 while (1) {
250 1283139 int ret = decode_get_packet(avctx, pkt);
251
2/2
✓ Branch 0 taken 852784 times.
✓ Branch 1 taken 430355 times.
1283139 if (ret == AVERROR(EAGAIN) &&
252
5/6
✓ Branch 0 taken 425896 times.
✓ Branch 1 taken 426888 times.
✓ Branch 2 taken 425896 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3403 times.
✓ Branch 5 taken 422493 times.
852784 (!AVPACKET_IS_EMPTY(avci->buffer_pkt) || dc->draining_started)) {
253 430291 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430291 times.
430291 if (ret < 0) {
255 av_packet_unref(avci->buffer_pkt);
256 return ret;
257 }
258
259 430291 continue;
260 }
261
262 852848 return ret;
263 }
264 }
265
266 /**
267 * Attempt to guess proper monotonic timestamps for decoded video frames
268 * which might have incorrect times. Input timestamps may wrap around, in
269 * which case the output will as well.
270 *
271 * @param pts the pts field of the decoded AVPacket, as passed through
272 * AVFrame.pts
273 * @param dts the dts field of the decoded AVPacket
274 * @return one of the input values, may be AV_NOPTS_VALUE
275 */
276 445096 static int64_t guess_correct_pts(AVCodecContext *ctx,
277 int64_t reordered_pts, int64_t dts)
278 {
279 445096 int64_t pts = AV_NOPTS_VALUE;
280
281
2/2
✓ Branch 0 taken 355626 times.
✓ Branch 1 taken 89470 times.
445096 if (dts != AV_NOPTS_VALUE) {
282 355626 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
283 355626 ctx->pts_correction_last_dts = dts;
284
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 89122 times.
89470 } else if (reordered_pts != AV_NOPTS_VALUE)
285 348 ctx->pts_correction_last_dts = reordered_pts;
286
287
2/2
✓ Branch 0 taken 355277 times.
✓ Branch 1 taken 89819 times.
445096 if (reordered_pts != AV_NOPTS_VALUE) {
288 355277 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
289 355277 ctx->pts_correction_last_pts = reordered_pts;
290
2/2
✓ Branch 0 taken 697 times.
✓ Branch 1 taken 89122 times.
89819 } else if(dts != AV_NOPTS_VALUE)
291 697 ctx->pts_correction_last_pts = dts;
292
293
4/4
✓ Branch 0 taken 566 times.
✓ Branch 1 taken 444530 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 557 times.
445096 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
294
2/2
✓ Branch 0 taken 354720 times.
✓ Branch 1 taken 89819 times.
444539 && reordered_pts != AV_NOPTS_VALUE)
295 354720 pts = reordered_pts;
296 else
297 90376 pts = dts;
298
299 445096 return pts;
300 }
301
302 308252 static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
303 {
304 308252 AVCodecInternal *avci = avctx->internal;
305 AVFrameSideData *side;
306 308252 uint32_t discard_padding = 0;
307 308252 uint8_t skip_reason = 0;
308 308252 uint8_t discard_reason = 0;
309
310 308252 side = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
311
3/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 308140 times.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
308252 if (side && side->size >= 10) {
312 112 avci->skip_samples = AV_RL32(side->data);
313 112 avci->skip_samples = FFMAX(0, avci->skip_samples);
314 112 discard_padding = AV_RL32(side->data + 4);
315 112 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
316 avci->skip_samples, (int)discard_padding);
317 112 skip_reason = AV_RL8(side->data + 8);
318 112 discard_reason = AV_RL8(side->data + 9);
319 }
320
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308252 times.
308252 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
322 if (!side && (avci->skip_samples || discard_padding))
323 side = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
324 if (side && (avci->skip_samples || discard_padding)) {
325 AV_WL32(side->data, avci->skip_samples);
326 AV_WL32(side->data + 4, discard_padding);
327 AV_WL8(side->data + 8, skip_reason);
328 AV_WL8(side->data + 9, discard_reason);
329 avci->skip_samples = 0;
330 }
331 return 0;
332 }
333 308252 av_frame_remove_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
334
335
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 308232 times.
308252 if ((frame->flags & AV_FRAME_FLAG_DISCARD)) {
336 20 avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
337 20 *discarded_samples += frame->nb_samples;
338 20 return AVERROR(EAGAIN);
339 }
340
341
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 308019 times.
308232 if (avci->skip_samples > 0) {
342
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 63 times.
213 if (frame->nb_samples <= avci->skip_samples){
343 150 *discarded_samples += frame->nb_samples;
344 150 avci->skip_samples -= frame->nb_samples;
345 150 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
346 avci->skip_samples);
347 150 return AVERROR(EAGAIN);
348 } else {
349 63 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
350 63 frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
351
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) {
352 63 int64_t diff_ts = av_rescale_q(avci->skip_samples,
353 63 (AVRational){1, avctx->sample_rate},
354 avctx->pkt_timebase);
355
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (frame->pts != AV_NOPTS_VALUE)
356 63 frame->pts += diff_ts;
357
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (frame->pkt_dts != AV_NOPTS_VALUE)
358 63 frame->pkt_dts += diff_ts;
359
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (frame->duration >= diff_ts)
360 63 frame->duration -= diff_ts;
361 } else
362 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
363
364 63 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
365 avci->skip_samples, frame->nb_samples);
366 63 *discarded_samples += avci->skip_samples;
367 63 frame->nb_samples -= avci->skip_samples;
368 63 avci->skip_samples = 0;
369 }
370 }
371
372
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 308075 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
308082 if (discard_padding > 0 && discard_padding <= frame->nb_samples) {
373
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (discard_padding == frame->nb_samples) {
374 2 *discarded_samples += frame->nb_samples;
375 2 return AVERROR(EAGAIN);
376 } else {
377
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) {
378 5 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
379 5 (AVRational){1, avctx->sample_rate},
380 avctx->pkt_timebase);
381 5 frame->duration = diff_ts;
382 } else
383 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
384
385 5 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
386 (int)discard_padding, frame->nb_samples);
387 5 frame->nb_samples -= discard_padding;
388 }
389 }
390
391 308080 return 0;
392 }
393
394 /*
395 * The core of the receive_frame_wrapper for the decoders implementing
396 * the simple API. Certain decoders might consume partial packets without
397 * returning any output, so this function needs to be called in a loop until it
398 * returns EAGAIN.
399 **/
400 880726 static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
401 {
402 880726 AVCodecInternal *avci = avctx->internal;
403 880726 AVPacket *const pkt = avci->in_pkt;
404 880726 const FFCodec *const codec = ffcodec(avctx->codec);
405 int got_frame, consumed;
406 int ret;
407
408
4/4
✓ Branch 0 taken 851886 times.
✓ Branch 1 taken 28840 times.
✓ Branch 2 taken 850182 times.
✓ Branch 3 taken 1704 times.
880726 if (!pkt->data && !avci->draining) {
409 850182 av_packet_unref(pkt);
410 850182 ret = ff_decode_get_packet(avctx, pkt);
411
4/4
✓ Branch 0 taken 424740 times.
✓ Branch 1 taken 425442 times.
✓ Branch 2 taken 421363 times.
✓ Branch 3 taken 3377 times.
850182 if (ret < 0 && ret != AVERROR_EOF)
412 421363 return ret;
413 }
414
415 // Some codecs (at least wma lossless) will crash when feeding drain packets
416 // after EOF was signaled.
417
2/2
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 458654 times.
459363 if (avci->draining_done)
418 709 return AVERROR_EOF;
419
420
2/2
✓ Branch 0 taken 4372 times.
✓ Branch 1 taken 454282 times.
458654 if (!pkt->data &&
421
2/2
✓ Branch 0 taken 2708 times.
✓ Branch 1 taken 1664 times.
4372 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
422
2/2
✓ Branch 0 taken 2667 times.
✓ Branch 1 taken 41 times.
2708 avctx->active_thread_type & FF_THREAD_FRAME))
423 2667 return AVERROR_EOF;
424
425 455987 got_frame = 0;
426
427
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 455377 times.
455987 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
428 610 consumed = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
429 } else {
430 455377 consumed = codec->cb.decode(avctx, frame, &got_frame, pkt);
431
432
1/2
✓ Branch 0 taken 455377 times.
✗ Branch 1 not taken.
455377 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
433 455377 frame->pkt_dts = pkt->dts;
434
2/2
✓ Branch 0 taken 148001 times.
✓ Branch 1 taken 307376 times.
455377 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
435 #if FF_API_FRAME_PKT
436 FF_DISABLE_DEPRECATION_WARNINGS
437
2/2
✓ Branch 0 taken 112805 times.
✓ Branch 1 taken 35196 times.
148001 if(!avctx->has_b_frames)
438 112805 frame->pkt_pos = pkt->pos;
439 FF_ENABLE_DEPRECATION_WARNINGS
440 #endif
441 }
442 }
443 455987 emms_c();
444
445
2/2
✓ Branch 0 taken 148139 times.
✓ Branch 1 taken 307848 times.
455987 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
446
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 137003 times.
285322 ret = (!got_frame || frame->flags & AV_FRAME_FLAG_DISCARD)
447 ? AVERROR(EAGAIN)
448
2/2
✓ Branch 0 taken 137183 times.
✓ Branch 1 taken 10956 times.
285322 : 0;
449
1/2
✓ Branch 0 taken 307848 times.
✗ Branch 1 not taken.
307848 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
450 307848 ret = !got_frame ? AVERROR(EAGAIN)
451
2/2
✓ Branch 0 taken 306443 times.
✓ Branch 1 taken 1405 times.
307848 : discard_samples(avctx, frame, discarded_samples);
452 }
453
454
2/2
✓ Branch 0 taken 12713 times.
✓ Branch 1 taken 443274 times.
455987 if (ret == AVERROR(EAGAIN))
455 12713 av_frame_unref(frame);
456
457 // FF_CODEC_CB_TYPE_DECODE decoders must not return AVERROR EAGAIN
458 // code later will add AVERROR(EAGAIN) to a pointer
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 455987 times.
455987 av_assert0(consumed != AVERROR(EAGAIN));
460
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 455288 times.
455987 if (consumed < 0)
461 699 ret = consumed;
462
4/4
✓ Branch 0 taken 455288 times.
✓ Branch 1 taken 699 times.
✓ Branch 2 taken 147477 times.
✓ Branch 3 taken 307811 times.
455987 if (consumed >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
463 147477 consumed = pkt->size;
464
465
2/2
✓ Branch 0 taken 443274 times.
✓ Branch 1 taken 12713 times.
455987 if (!ret)
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 443274 times.
443274 av_assert0(frame->buf[0]);
467
2/2
✓ Branch 0 taken 12014 times.
✓ Branch 1 taken 443973 times.
455987 if (ret == AVERROR(EAGAIN))
468 12014 ret = 0;
469
470 /* do not stop draining when got_frame != 0 or ret < 0 */
471
4/4
✓ Branch 0 taken 1705 times.
✓ Branch 1 taken 454282 times.
✓ Branch 2 taken 710 times.
✓ Branch 3 taken 995 times.
455987 if (avci->draining && !got_frame) {
472
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 709 times.
710 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 1 not taken.
✓ Branch 2 taken 1 times.
1 if (decode_ctx(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 709 avci->draining_done = 1;
486 }
487 }
488
489
4/4
✓ Branch 0 taken 29555 times.
✓ Branch 1 taken 426432 times.
✓ Branch 2 taken 699 times.
✓ Branch 3 taken 28856 times.
455987 if (consumed >= pkt->size || ret < 0) {
490 427131 av_packet_unref(pkt);
491 } else {
492 28856 pkt->data += consumed;
493 28856 pkt->size -= consumed;
494 28856 pkt->pts = AV_NOPTS_VALUE;
495 28856 pkt->dts = AV_NOPTS_VALUE;
496
1/2
✓ Branch 0 taken 28856 times.
✗ Branch 1 not taken.
28856 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
497 #if FF_API_FRAME_PKT
498 // See extract_packet_props() comment.
499 28856 avci->last_pkt_props->stream_index = avci->last_pkt_props->stream_index - consumed;
500 #endif
501 28856 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
502 28856 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
503 }
504 }
505
506 455987 return ret;
507 }
508
509 #if CONFIG_LCMS2
510 static int detect_colorspace(AVCodecContext *avctx, AVFrame *frame)
511 {
512 AVCodecInternal *avci = avctx->internal;
513 enum AVColorTransferCharacteristic trc;
514 AVColorPrimariesDesc coeffs;
515 enum AVColorPrimaries prim;
516 cmsHPROFILE profile;
517 AVFrameSideData *sd;
518 int ret;
519 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
520 return 0;
521
522 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
523 if (!sd || !sd->size)
524 return 0;
525
526 if (!avci->icc.avctx) {
527 ret = ff_icc_context_init(&avci->icc, avctx);
528 if (ret < 0)
529 return ret;
530 }
531
532 profile = cmsOpenProfileFromMemTHR(avci->icc.ctx, sd->data, sd->size);
533 if (!profile)
534 return AVERROR_INVALIDDATA;
535
536 ret = ff_icc_profile_sanitize(&avci->icc, profile);
537 if (!ret)
538 ret = ff_icc_profile_read_primaries(&avci->icc, profile, &coeffs);
539 if (!ret)
540 ret = ff_icc_profile_detect_transfer(&avci->icc, profile, &trc);
541 cmsCloseProfile(profile);
542 if (ret < 0)
543 return ret;
544
545 prim = av_csp_primaries_id_from_desc(&coeffs);
546 if (prim != AVCOL_PRI_UNSPECIFIED)
547 frame->color_primaries = prim;
548 if (trc != AVCOL_TRC_UNSPECIFIED)
549 frame->color_trc = trc;
550 return 0;
551 }
552 #else /* !CONFIG_LCMS2 */
553 872079 static int detect_colorspace(av_unused AVCodecContext *c, av_unused AVFrame *f)
554 {
555 872079 return 0;
556 }
557 #endif
558
559 892624 static int fill_frame_props(const AVCodecContext *avctx, AVFrame *frame)
560 {
561 int ret;
562
563
2/2
✓ Branch 0 taken 583720 times.
✓ Branch 1 taken 308904 times.
892624 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
564 583720 frame->color_primaries = avctx->color_primaries;
565
2/2
✓ Branch 0 taken 583359 times.
✓ Branch 1 taken 309265 times.
892624 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
566 583359 frame->color_trc = avctx->color_trc;
567
2/2
✓ Branch 0 taken 577467 times.
✓ Branch 1 taken 315157 times.
892624 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
568 577467 frame->colorspace = avctx->colorspace;
569
2/2
✓ Branch 0 taken 833325 times.
✓ Branch 1 taken 59299 times.
892624 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
570 833325 frame->color_range = avctx->color_range;
571
2/2
✓ Branch 0 taken 865172 times.
✓ Branch 1 taken 27452 times.
892624 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
572 865172 frame->chroma_location = avctx->chroma_sample_location;
573
574
2/2
✓ Branch 0 taken 276235 times.
✓ Branch 1 taken 616389 times.
892624 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
575
2/2
✓ Branch 0 taken 260280 times.
✓ Branch 1 taken 15955 times.
276235 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
576
2/2
✓ Branch 0 taken 137034 times.
✓ Branch 1 taken 139201 times.
276235 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
577
1/2
✓ Branch 0 taken 616389 times.
✗ Branch 1 not taken.
616389 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
578
2/2
✓ Branch 0 taken 308283 times.
✓ Branch 1 taken 308106 times.
616389 if (frame->format == AV_SAMPLE_FMT_NONE)
579 308283 frame->format = avctx->sample_fmt;
580
2/2
✓ Branch 0 taken 308283 times.
✓ Branch 1 taken 308106 times.
616389 if (!frame->ch_layout.nb_channels) {
581 308283 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308283 times.
308283 if (ret < 0)
583 return ret;
584 }
585 #if FF_API_OLD_CHANNEL_LAYOUT
586 FF_DISABLE_DEPRECATION_WARNINGS
587
2/2
✓ Branch 0 taken 311106 times.
✓ Branch 1 taken 305283 times.
616389 if (!frame->channel_layout)
588 311106 frame->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
589
2/2
✓ Branch 0 taken 305510 times.
✓ Branch 1 taken 5596 times.
311106 avctx->ch_layout.u.mask : 0;
590
2/2
✓ Branch 0 taken 308309 times.
✓ Branch 1 taken 308080 times.
616389 if (!frame->channels)
591 308309 frame->channels = avctx->ch_layout.nb_channels;
592 FF_ENABLE_DEPRECATION_WARNINGS
593 #endif
594
2/2
✓ Branch 0 taken 308143 times.
✓ Branch 1 taken 308246 times.
616389 if (!frame->sample_rate)
595 308143 frame->sample_rate = avctx->sample_rate;
596 }
597
598 892624 return 0;
599 }
600
601 868712 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
602 {
603 int ret;
604 868712 int64_t discarded_samples = 0;
605
606
2/2
✓ Branch 0 taken 880726 times.
✓ Branch 1 taken 443274 times.
1324000 while (!frame->buf[0]) {
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 880726 times.
880726 if (discarded_samples > avctx->max_samples)
608 return AVERROR(EAGAIN);
609 880726 ret = decode_simple_internal(avctx, frame, &discarded_samples);
610
2/2
✓ Branch 0 taken 425438 times.
✓ Branch 1 taken 455288 times.
880726 if (ret < 0)
611 425438 return ret;
612 }
613
614 443274 return 0;
615 }
616
617 872079 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
618 {
619 872079 AVCodecInternal *avci = avctx->internal;
620 872079 const FFCodec *const codec = ffcodec(avctx->codec);
621 int ret, ok;
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 872079 times.
872079 av_assert0(!frame->buf[0]);
624
625
2/2
✓ Branch 0 taken 3367 times.
✓ Branch 1 taken 868712 times.
872079 if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) {
626 3367 ret = codec->cb.receive_frame(avctx, frame);
627 3367 emms_c();
628
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 1545 times.
3367 if (!ret) {
629
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1809 times.
1822 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO)
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 ret = (frame->flags & AV_FRAME_FLAG_DISCARD) ? AVERROR(EAGAIN) : 0;
631
1/2
✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
1809 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
632 1809 int64_t discarded_samples = 0;
633 1809 ret = discard_samples(avctx, frame, &discarded_samples);
634 }
635 }
636 } else
637 868712 ret = decode_simple_receive_frame(avctx, frame);
638
639
2/2
✓ Branch 0 taken 3402 times.
✓ Branch 1 taken 868677 times.
872079 if (ret == AVERROR_EOF)
640 3402 avci->draining_done = 1;
641
642 /* preserve ret */
643 872079 ok = detect_colorspace(avctx, frame);
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 872079 times.
872079 if (ok < 0) {
645 av_frame_unref(frame);
646 return ok;
647 }
648
649
2/2
✓ Branch 0 taken 445096 times.
✓ Branch 1 taken 426983 times.
872079 if (!ret) {
650
2/2
✓ Branch 0 taken 137016 times.
✓ Branch 1 taken 308080 times.
445096 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
651
2/2
✓ Branch 0 taken 23720 times.
✓ Branch 1 taken 113296 times.
137016 if (!frame->width)
652 23720 frame->width = avctx->width;
653
2/2
✓ Branch 0 taken 23720 times.
✓ Branch 1 taken 113296 times.
137016 if (!frame->height)
654 23720 frame->height = avctx->height;
655 } else
656 308080 frame->flags |= AV_FRAME_FLAG_KEY;
657
658 445096 ret = fill_frame_props(avctx, frame);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445096 times.
445096 if (ret < 0) {
660 av_frame_unref(frame);
661 return ret;
662 }
663
664 #if FF_API_FRAME_KEY
665 FF_DISABLE_DEPRECATION_WARNINGS
666 445096 frame->key_frame = !!(frame->flags & AV_FRAME_FLAG_KEY);
667 FF_ENABLE_DEPRECATION_WARNINGS
668 #endif
669 #if FF_API_INTERLACED_FRAME
670 FF_DISABLE_DEPRECATION_WARNINGS
671 445096 frame->interlaced_frame = !!(frame->flags & AV_FRAME_FLAG_INTERLACED);
672 445096 frame->top_field_first = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
673 FF_ENABLE_DEPRECATION_WARNINGS
674 #endif
675 445096 frame->best_effort_timestamp = guess_correct_pts(avctx,
676 frame->pts,
677 frame->pkt_dts);
678
679 #if FF_API_PKT_DURATION
680 FF_DISABLE_DEPRECATION_WARNINGS
681 445096 frame->pkt_duration = frame->duration;
682 FF_ENABLE_DEPRECATION_WARNINGS
683 #endif
684
685 /* the only case where decode data is not set should be decoders
686 * that do not call ff_get_buffer() */
687
4/6
✓ Branch 0 taken 420658 times.
✓ Branch 1 taken 24438 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 420658 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24438 times.
445096 av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
688 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
689
690
2/2
✓ Branch 0 taken 420658 times.
✓ Branch 1 taken 24438 times.
445096 if (frame->private_ref) {
691 420658 FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
692
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 420658 times.
420658 if (fdd->post_process) {
694 ret = fdd->post_process(avctx, frame);
695 if (ret < 0) {
696 av_frame_unref(frame);
697 return ret;
698 }
699 }
700 }
701 }
702
703 /* free the per-frame decode data */
704 872079 av_buffer_unref(&frame->private_ref);
705
706 872079 return ret;
707 }
708
709 430317 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
710 {
711 430317 AVCodecInternal *avci = avctx->internal;
712 430317 DecodeContext *dc = decode_ctx(avci);
713 int ret;
714
715
2/4
✓ Branch 1 taken 430317 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 430317 times.
430317 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
716 return AVERROR(EINVAL);
717
718
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 430291 times.
430317 if (dc->draining_started)
719 26 return AVERROR_EOF;
720
721
5/6
✓ Branch 0 taken 427001 times.
✓ Branch 1 taken 3290 times.
✓ Branch 2 taken 113 times.
✓ Branch 3 taken 426888 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 113 times.
430291 if (avpkt && !avpkt->size && avpkt->data)
722 return AVERROR(EINVAL);
723
724
5/6
✓ Branch 0 taken 427001 times.
✓ Branch 1 taken 3290 times.
✓ Branch 2 taken 113 times.
✓ Branch 3 taken 426888 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 113 times.
430291 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
725
2/4
✓ Branch 0 taken 426888 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 426888 times.
426888 if (!AVPACKET_IS_EMPTY(avci->buffer_pkt))
726 return AVERROR(EAGAIN);
727 426888 ret = av_packet_ref(avci->buffer_pkt, avpkt);
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 426888 times.
426888 if (ret < 0)
729 return ret;
730 } else
731 3403 dc->draining_started = 1;
732
733
3/4
✓ Branch 0 taken 430291 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 426888 times.
✓ Branch 3 taken 3403 times.
430291 if (!avci->buffer_frame->buf[0] && !dc->draining_started) {
734 426888 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
735
5/6
✓ Branch 0 taken 11094 times.
✓ Branch 1 taken 415794 times.
✓ Branch 2 taken 1081 times.
✓ Branch 3 taken 10013 times.
✓ Branch 4 taken 1081 times.
✗ Branch 5 not taken.
426888 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
736 1081 return ret;
737 }
738
739 429210 return 0;
740 }
741
742 137016 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
743 {
744 /* make sure we are noisy about decoders returning invalid cropping data */
745
1/2
✓ Branch 0 taken 137016 times.
✗ Branch 1 not taken.
137016 if (frame->crop_left >= INT_MAX - frame->crop_right ||
746
1/2
✓ Branch 0 taken 137016 times.
✗ Branch 1 not taken.
137016 frame->crop_top >= INT_MAX - frame->crop_bottom ||
747
1/2
✓ Branch 0 taken 137016 times.
✗ Branch 1 not taken.
137016 (frame->crop_left + frame->crop_right) >= frame->width ||
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137016 times.
137016 (frame->crop_top + frame->crop_bottom) >= frame->height) {
749 av_log(avctx, AV_LOG_WARNING,
750 "Invalid cropping information set by a decoder: "
751 "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
752 "(frame size %dx%d). This is a bug, please report it\n",
753 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
754 frame->width, frame->height);
755 frame->crop_left = 0;
756 frame->crop_right = 0;
757 frame->crop_top = 0;
758 frame->crop_bottom = 0;
759 return 0;
760 }
761
762
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 136962 times.
137016 if (!avctx->apply_cropping)
763 54 return 0;
764
765 136962 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
766 AV_FRAME_CROP_UNALIGNED : 0);
767 }
768
769 // make sure frames returned to the caller are valid
770 445096 static int frame_validate(AVCodecContext *avctx, AVFrame *frame)
771 {
772
2/4
✓ Branch 0 taken 445096 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 445096 times.
445096 if (!frame->buf[0] || frame->format < 0)
773 goto fail;
774
775
2/3
✓ Branch 0 taken 137016 times.
✓ Branch 1 taken 308080 times.
✗ Branch 2 not taken.
445096 switch (avctx->codec_type) {
776 137016 case AVMEDIA_TYPE_VIDEO:
777
2/4
✓ Branch 0 taken 137016 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 137016 times.
137016 if (frame->width <= 0 || frame->height <= 0)
778 goto fail;
779 137016 break;
780 308080 case AVMEDIA_TYPE_AUDIO:
781
1/2
✓ Branch 1 taken 308080 times.
✗ Branch 2 not taken.
308080 if (!av_channel_layout_check(&frame->ch_layout) ||
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308080 times.
308080 frame->sample_rate <= 0)
783 goto fail;
784
785 308080 break;
786 default: av_assert0(0);
787 }
788
789 445096 return 0;
790 fail:
791 av_log(avctx, AV_LOG_ERROR, "An invalid frame was output by a decoder. "
792 "This is a bug, please report it.\n");
793 return AVERROR_BUG;
794 }
795
796 860985 int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
797 {
798 860985 AVCodecInternal *avci = avctx->internal;
799 int ret;
800
801
2/4
✓ Branch 1 taken 860985 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 860985 times.
860985 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
802 return AVERROR(EINVAL);
803
804
2/2
✓ Branch 0 taken 415794 times.
✓ Branch 1 taken 445191 times.
860985 if (avci->buffer_frame->buf[0]) {
805 415794 av_frame_move_ref(frame, avci->buffer_frame);
806 } else {
807 445191 ret = decode_receive_frame_internal(avctx, frame);
808
2/2
✓ Branch 0 taken 415889 times.
✓ Branch 1 taken 29302 times.
445191 if (ret < 0)
809 415889 return ret;
810 }
811
812 445096 ret = frame_validate(avctx, frame);
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445096 times.
445096 if (ret < 0)
814 goto fail;
815
816
2/2
✓ Branch 0 taken 137016 times.
✓ Branch 1 taken 308080 times.
445096 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
817 137016 ret = apply_cropping(avctx, frame);
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137016 times.
137016 if (ret < 0)
819 goto fail;
820 }
821
822 445096 avctx->frame_num++;
823 #if FF_API_AVCTX_FRAME_NUMBER
824 FF_DISABLE_DEPRECATION_WARNINGS
825 445096 avctx->frame_number = avctx->frame_num;
826 FF_ENABLE_DEPRECATION_WARNINGS
827 #endif
828
829 #if FF_API_DROPCHANGED
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445096 times.
445096 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
831
832 if (avctx->frame_num == 1) {
833 avci->initial_format = frame->format;
834 switch(avctx->codec_type) {
835 case AVMEDIA_TYPE_VIDEO:
836 avci->initial_width = frame->width;
837 avci->initial_height = frame->height;
838 break;
839 case AVMEDIA_TYPE_AUDIO:
840 avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
841 avctx->sample_rate;
842 ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout);
843 if (ret < 0)
844 goto fail;
845 break;
846 }
847 }
848
849 if (avctx->frame_num > 1) {
850 int changed = avci->initial_format != frame->format;
851
852 switch(avctx->codec_type) {
853 case AVMEDIA_TYPE_VIDEO:
854 changed |= avci->initial_width != frame->width ||
855 avci->initial_height != frame->height;
856 break;
857 case AVMEDIA_TYPE_AUDIO:
858 changed |= avci->initial_sample_rate != frame->sample_rate ||
859 avci->initial_sample_rate != avctx->sample_rate ||
860 av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout);
861 break;
862 }
863
864 if (changed) {
865 avci->changed_frames_dropped++;
866 av_log(avctx, AV_LOG_INFO, "dropped changed frame #%"PRId64" pts %"PRId64
867 " drop count: %d \n",
868 avctx->frame_num, frame->pts,
869 avci->changed_frames_dropped);
870 ret = AVERROR_INPUT_CHANGED;
871 goto fail;
872 }
873 }
874 }
875 #endif
876 445096 return 0;
877 fail:
878 av_frame_unref(frame);
879 return ret;
880 }
881
882 1709 static void get_subtitle_defaults(AVSubtitle *sub)
883 {
884 1709 memset(sub, 0, sizeof(*sub));
885 1709 sub->pts = AV_NOPTS_VALUE;
886 1709 }
887
888 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
889 1675 static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt,
890 const AVPacket *inpkt, AVPacket *buf_pkt)
891 {
892 #if CONFIG_ICONV
893 1675 iconv_t cd = (iconv_t)-1;
894 1675 int ret = 0;
895 char *inb, *outb;
896 size_t inl, outl;
897 #endif
898
899
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
1675 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) {
900 1582 *outpkt = inpkt;
901 1582 return 0;
902 }
903
904 #if CONFIG_ICONV
905 93 inb = inpkt->data;
906 93 inl = inpkt->size;
907
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
909 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
910 return AVERROR(ERANGE);
911 }
912
913 93 cd = iconv_open("UTF-8", avctx->sub_charenc);
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 av_assert0(cd != (iconv_t)-1);
915
916 93 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
918 goto end;
919 93 ret = av_packet_copy_props(buf_pkt, inpkt);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
921 goto end;
922 93 outb = buf_pkt->data;
923 93 outl = buf_pkt->size;
924
925
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 ||
926 93 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
927
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) {
928 ret = FFMIN(AVERROR(errno), -1);
929 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
930 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
931 goto end;
932 }
933 93 buf_pkt->size -= outl;
934 93 memset(buf_pkt->data + buf_pkt->size, 0, outl);
935 93 *outpkt = buf_pkt;
936
937 93 ret = 0;
938 93 end:
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
940 av_packet_unref(buf_pkt);
941
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (cd != (iconv_t)-1)
942 93 iconv_close(cd);
943 93 return ret;
944 #else
945 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
946 return AVERROR(EINVAL);
947 #endif
948 }
949
950 700 static int utf8_check(const uint8_t *str)
951 {
952 const uint8_t *byte;
953 uint32_t codepoint, min;
954
955
2/2
✓ Branch 0 taken 61067 times.
✓ Branch 1 taken 700 times.
61767 while (*str) {
956 61067 byte = str;
957
5/8
✓ Branch 0 taken 61067 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61067 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2794 times.
✓ Branch 6 taken 2794 times.
✓ Branch 7 taken 61067 times.
63861 GET_UTF8(codepoint, *(byte++), return 0;);
958
4/4
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 58978 times.
✓ Branch 2 taken 705 times.
✓ Branch 3 taken 1384 times.
61772 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
959 705 1 << (5 * (byte - str) - 4);
960
3/6
✓ Branch 0 taken 61067 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61067 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61067 times.
✗ Branch 5 not taken.
61067 if (codepoint < min || codepoint >= 0x110000 ||
961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61067 times.
61067 codepoint == 0xFFFE /* BOM */ ||
962 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
963 return 0;
964 61067 str = byte;
965 }
966 700 return 1;
967 }
968
969 1709 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
970 int *got_sub_ptr, const AVPacket *avpkt)
971 {
972 1709 int ret = 0;
973
974
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1669 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
1709 if (!avpkt->data && avpkt->size) {
975 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
976 return AVERROR(EINVAL);
977 }
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1709 times.
1709 if (!avctx->codec)
979 return AVERROR(EINVAL);
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1709 times.
1709 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
981 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
982 return AVERROR(EINVAL);
983 }
984
985 1709 *got_sub_ptr = 0;
986 1709 get_subtitle_defaults(sub);
987
988
4/4
✓ Branch 0 taken 857 times.
✓ Branch 1 taken 852 times.
✓ Branch 2 taken 823 times.
✓ Branch 3 taken 34 times.
1709 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
989 1675 AVCodecInternal *avci = avctx->internal;
990 const AVPacket *pkt;
991
992 1675 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1675 times.
1675 if (ret < 0)
994 79 return ret;
995
996
3/4
✓ Branch 0 taken 1675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
✓ Branch 3 taken 10 times.
1675 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
997 1665 sub->pts = av_rescale_q(avpkt->pts,
998 1665 avctx->pkt_timebase, AV_TIME_BASE_Q);
999 1675 ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
1000
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
1675 if (pkt == avci->buffer_pkt) // did we recode?
1001 93 av_packet_unref(avci->buffer_pkt);
1002
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1596 times.
1675 if (ret < 0) {
1003 79 *got_sub_ptr = 0;
1004 79 avsubtitle_free(sub);
1005 79 return ret;
1006 }
1007 av_assert1(!sub->num_rects || *got_sub_ptr);
1008
1009
6/6
✓ Branch 0 taken 826 times.
✓ Branch 1 taken 770 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 246 times.
✓ Branch 4 taken 528 times.
✓ Branch 5 taken 52 times.
1596 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
1010
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 avctx->pkt_timebase.num) {
1011 528 AVRational ms = { 1, 1000 };
1012 528 sub->end_display_time = av_rescale_q(avpkt->duration,
1013 avctx->pkt_timebase, ms);
1014 }
1015
1016
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1440 times.
1596 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
1017 156 sub->format = 0;
1018
1/2
✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
1440 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
1019 1440 sub->format = 1;
1020
1021
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 1596 times.
2455 for (unsigned i = 0; i < sub->num_rects; i++) {
1022
1/2
✓ Branch 0 taken 859 times.
✗ Branch 1 not taken.
859 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
1023
3/4
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 700 times.
859 sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
1024 av_log(avctx, AV_LOG_ERROR,
1025 "Invalid UTF-8 in decoded subtitles text; "
1026 "maybe missing -sub_charenc option\n");
1027 avsubtitle_free(sub);
1028 *got_sub_ptr = 0;
1029 return AVERROR_INVALIDDATA;
1030 }
1031 }
1032
1033
2/2
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 761 times.
1596 if (*got_sub_ptr)
1034 835 avctx->frame_num++;
1035 #if FF_API_AVCTX_FRAME_NUMBER
1036 FF_DISABLE_DEPRECATION_WARNINGS
1037 1596 avctx->frame_number = avctx->frame_num;
1038 FF_ENABLE_DEPRECATION_WARNINGS
1039 #endif
1040 }
1041
1042 1630 return ret;
1043 }
1044
1045 1645 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
1046 const enum AVPixelFormat *fmt)
1047 {
1048 const AVPixFmtDescriptor *desc;
1049 const AVCodecHWConfig *config;
1050 int i, n;
1051
1052 // If a device was supplied when the codec was opened, assume that the
1053 // user wants to use it.
1054
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1645 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1645 if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
1055 AVHWDeviceContext *device_ctx =
1056 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1057 for (i = 0;; i++) {
1058 config = &ffcodec(avctx->codec)->hw_configs[i]->public;
1059 if (!config)
1060 break;
1061 if (!(config->methods &
1062 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1063 continue;
1064 if (device_ctx->type != config->device_type)
1065 continue;
1066 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1067 if (config->pix_fmt == fmt[n])
1068 return fmt[n];
1069 }
1070 }
1071 }
1072 // No device or other setup, so we have to choose from things which
1073 // don't any other external information.
1074
1075 // If the last element of the list is a software format, choose it
1076 // (this should be best software format if any exist).
1077
2/2
✓ Branch 0 taken 4324 times.
✓ Branch 1 taken 1645 times.
5969 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1078 1645 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1079
1/2
✓ Branch 0 taken 1645 times.
✗ Branch 1 not taken.
1645 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1080 1645 return fmt[n - 1];
1081
1082 // Finally, traverse the list in order and choose the first entry
1083 // with no external dependencies (if there is no hardware configuration
1084 // information available then this just picks the first entry).
1085 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1086 for (i = 0;; i++) {
1087 config = avcodec_get_hw_config(avctx->codec, i);
1088 if (!config)
1089 break;
1090 if (config->pix_fmt == fmt[n])
1091 break;
1092 }
1093 if (!config) {
1094 // No specific config available, so the decoder must be able
1095 // to handle this format without any additional setup.
1096 return fmt[n];
1097 }
1098 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1099 // Usable with only internal setup.
1100 return fmt[n];
1101 }
1102 }
1103
1104 // Nothing is usable, give up.
1105 return AV_PIX_FMT_NONE;
1106 }
1107
1108 int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
1109 enum AVHWDeviceType dev_type)
1110 {
1111 AVHWDeviceContext *device_ctx;
1112 AVHWFramesContext *frames_ctx;
1113 int ret;
1114
1115 if (!avctx->hwaccel)
1116 return AVERROR(ENOSYS);
1117
1118 if (avctx->hw_frames_ctx)
1119 return 0;
1120 if (!avctx->hw_device_ctx) {
1121 av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
1122 "required for hardware accelerated decoding.\n");
1123 return AVERROR(EINVAL);
1124 }
1125
1126 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
1127 if (device_ctx->type != dev_type) {
1128 av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
1129 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
1130 av_hwdevice_get_type_name(device_ctx->type));
1131 return AVERROR(EINVAL);
1132 }
1133
1134 ret = avcodec_get_hw_frames_parameters(avctx,
1135 avctx->hw_device_ctx,
1136 avctx->hwaccel->pix_fmt,
1137 &avctx->hw_frames_ctx);
1138 if (ret < 0)
1139 return ret;
1140
1141 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1142
1143
1144 if (frames_ctx->initial_pool_size) {
1145 // We guarantee 4 base work surfaces. The function above guarantees 1
1146 // (the absolute minimum), so add the missing count.
1147 frames_ctx->initial_pool_size += 3;
1148 }
1149
1150 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1151 if (ret < 0) {
1152 av_buffer_unref(&avctx->hw_frames_ctx);
1153 return ret;
1154 }
1155
1156 return 0;
1157 }
1158
1159 int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
1160 AVBufferRef *device_ref,
1161 enum AVPixelFormat hw_pix_fmt,
1162 AVBufferRef **out_frames_ref)
1163 {
1164 AVBufferRef *frames_ref = NULL;
1165 const AVCodecHWConfigInternal *hw_config;
1166 const FFHWAccel *hwa;
1167 int i, ret;
1168
1169 for (i = 0;; i++) {
1170 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1171 if (!hw_config)
1172 return AVERROR(ENOENT);
1173 if (hw_config->public.pix_fmt == hw_pix_fmt)
1174 break;
1175 }
1176
1177 hwa = hw_config->hwaccel;
1178 if (!hwa || !hwa->frame_params)
1179 return AVERROR(ENOENT);
1180
1181 frames_ref = av_hwframe_ctx_alloc(device_ref);
1182 if (!frames_ref)
1183 return AVERROR(ENOMEM);
1184
1185 if (!avctx->internal->hwaccel_priv_data) {
1186 avctx->internal->hwaccel_priv_data =
1187 av_mallocz(hwa->priv_data_size);
1188 if (!avctx->internal->hwaccel_priv_data) {
1189 av_buffer_unref(&frames_ref);
1190 return AVERROR(ENOMEM);
1191 }
1192 }
1193
1194 ret = hwa->frame_params(avctx, frames_ref);
1195 if (ret >= 0) {
1196 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1197
1198 if (frames_ctx->initial_pool_size) {
1199 // If the user has requested that extra output surfaces be
1200 // available then add them here.
1201 if (avctx->extra_hw_frames > 0)
1202 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1203
1204 // If frame threading is enabled then an extra surface per thread
1205 // is also required.
1206 if (avctx->active_thread_type & FF_THREAD_FRAME)
1207 frames_ctx->initial_pool_size += avctx->thread_count;
1208 }
1209
1210 *out_frames_ref = frames_ref;
1211 } else {
1212 av_buffer_unref(&frames_ref);
1213 }
1214 return ret;
1215 }
1216
1217 static int hwaccel_init(AVCodecContext *avctx,
1218 const FFHWAccel *hwaccel)
1219 {
1220 int err;
1221
1222 if (hwaccel->p.capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1223 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1224 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1225 hwaccel->p.name);
1226 return AVERROR_PATCHWELCOME;
1227 }
1228
1229 if (!avctx->internal->hwaccel_priv_data && hwaccel->priv_data_size) {
1230 avctx->internal->hwaccel_priv_data =
1231 av_mallocz(hwaccel->priv_data_size);
1232 if (!avctx->internal->hwaccel_priv_data)
1233 return AVERROR(ENOMEM);
1234 }
1235
1236 avctx->hwaccel = &hwaccel->p;
1237 if (hwaccel->init) {
1238 err = hwaccel->init(avctx);
1239 if (err < 0) {
1240 av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
1241 "hwaccel initialisation returned error.\n",
1242 av_get_pix_fmt_name(hwaccel->p.pix_fmt));
1243 av_freep(&avctx->internal->hwaccel_priv_data);
1244 avctx->hwaccel = NULL;
1245 return err;
1246 }
1247 }
1248
1249 return 0;
1250 }
1251
1252 36245 void ff_hwaccel_uninit(AVCodecContext *avctx)
1253 {
1254
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 36245 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
36245 if (FF_HW_HAS_CB(avctx, uninit))
1255 FF_HW_SIMPLE_CALL(avctx, uninit);
1256
1257 36245 av_freep(&avctx->internal->hwaccel_priv_data);
1258
1259 36245 avctx->hwaccel = NULL;
1260
1261 36245 av_buffer_unref(&avctx->hw_frames_ctx);
1262 36245 }
1263
1264 2595 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1265 {
1266 const AVPixFmtDescriptor *desc;
1267 enum AVPixelFormat *choices;
1268 enum AVPixelFormat ret, user_choice;
1269 const AVCodecHWConfigInternal *hw_config;
1270 const AVCodecHWConfig *config;
1271 int i, n, err;
1272
1273 // Find end of list.
1274
2/2
✓ Branch 0 taken 6929 times.
✓ Branch 1 taken 2595 times.
9524 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1275 // Must contain at least one entry.
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2595 times.
2595 av_assert0(n >= 1);
1277 // If a software format is available, it must be the last entry.
1278 2595 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1279
1/2
✓ Branch 0 taken 2595 times.
✗ Branch 1 not taken.
2595 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
1280 // No software format is available.
1281 } else {
1282 2595 avctx->sw_pix_fmt = fmt[n - 1];
1283 }
1284
1285 2595 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1286
1/2
✓ Branch 0 taken 2595 times.
✗ Branch 1 not taken.
2595 if (!choices)
1287 return AV_PIX_FMT_NONE;
1288
1289 for (;;) {
1290 // Remove the previous hwaccel, if there was one.
1291 2595 ff_hwaccel_uninit(avctx);
1292
1293 2595 user_choice = avctx->get_format(avctx, choices);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2595 times.
2595 if (user_choice == AV_PIX_FMT_NONE) {
1295 // Explicitly chose nothing, give up.
1296 ret = AV_PIX_FMT_NONE;
1297 break;
1298 }
1299
1300 2595 desc = av_pix_fmt_desc_get(user_choice);
1301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2595 times.
2595 if (!desc) {
1302 av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
1303 "get_format() callback.\n");
1304 ret = AV_PIX_FMT_NONE;
1305 break;
1306 }
1307 2595 av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
1308 2595 desc->name);
1309
1310
1/2
✓ Branch 0 taken 6929 times.
✗ Branch 1 not taken.
6929 for (i = 0; i < n; i++) {
1311
2/2
✓ Branch 0 taken 2595 times.
✓ Branch 1 taken 4334 times.
6929 if (choices[i] == user_choice)
1312 2595 break;
1313 }
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2595 times.
2595 if (i == n) {
1315 av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
1316 "%s not in possible list.\n", desc->name);
1317 ret = AV_PIX_FMT_NONE;
1318 break;
1319 }
1320
1321
2/2
✓ Branch 1 taken 2548 times.
✓ Branch 2 taken 47 times.
2595 if (ffcodec(avctx->codec)->hw_configs) {
1322 2548 for (i = 0;; i++) {
1323 7074 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1324
2/2
✓ Branch 0 taken 2548 times.
✓ Branch 1 taken 4526 times.
7074 if (!hw_config)
1325 2548 break;
1326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4526 times.
4526 if (hw_config->public.pix_fmt == user_choice)
1327 break;
1328 }
1329 } else {
1330 47 hw_config = NULL;
1331 }
1332
1333
1/2
✓ Branch 0 taken 2595 times.
✗ Branch 1 not taken.
2595 if (!hw_config) {
1334 // No config available, so no extra setup required.
1335 2595 ret = user_choice;
1336 2595 break;
1337 }
1338 config = &hw_config->public;
1339
1340 if (config->methods &
1341 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1342 avctx->hw_frames_ctx) {
1343 const AVHWFramesContext *frames_ctx =
1344 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1345 if (frames_ctx->format != user_choice) {
1346 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1347 "does not match the format of the provided frames "
1348 "context.\n", desc->name);
1349 goto try_again;
1350 }
1351 } else if (config->methods &
1352 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1353 avctx->hw_device_ctx) {
1354 const AVHWDeviceContext *device_ctx =
1355 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1356 if (device_ctx->type != config->device_type) {
1357 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1358 "does not match the type of the provided device "
1359 "context.\n", desc->name);
1360 goto try_again;
1361 }
1362 } else if (config->methods &
1363 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1364 // Internal-only setup, no additional configuration.
1365 } else if (config->methods &
1366 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1367 // Some ad-hoc configuration we can't see and can't check.
1368 } else {
1369 av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
1370 "missing configuration.\n", desc->name);
1371 goto try_again;
1372 }
1373 if (hw_config->hwaccel) {
1374 av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
1375 "initialisation.\n", desc->name);
1376 err = hwaccel_init(avctx, hw_config->hwaccel);
1377 if (err < 0)
1378 goto try_again;
1379 }
1380 ret = user_choice;
1381 break;
1382
1383 try_again:
1384 av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
1385 "get_format() without it.\n", desc->name);
1386 for (i = 0; i < n; i++) {
1387 if (choices[i] == user_choice)
1388 break;
1389 }
1390 for (; i + 1 < n; i++)
1391 choices[i] = choices[i + 1];
1392 --n;
1393 }
1394
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2595 times.
2595 if (ret < 0)
1396 ff_hwaccel_uninit(avctx);
1397
1398 2595 av_freep(&choices);
1399 2595 return ret;
1400 }
1401
1402 4475679 const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
1403 enum AVPacketSideDataType type)
1404 {
1405
2/2
✓ Branch 0 taken 61055 times.
✓ Branch 1 taken 4473376 times.
4534431 for (int i = 0; i < avctx->nb_coded_side_data; i++)
1406
2/2
✓ Branch 0 taken 2303 times.
✓ Branch 1 taken 58752 times.
61055 if (avctx->coded_side_data[i].type == type)
1407 2303 return &avctx->coded_side_data[i];
1408
1409 4473376 return NULL;
1410 }
1411
1412 447528 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1413 {
1414 size_t size;
1415 const uint8_t *side_metadata;
1416
1417 447528 AVDictionary **frame_md = &frame->metadata;
1418
1419 447528 side_metadata = av_packet_get_side_data(avpkt,
1420 AV_PKT_DATA_STRINGS_METADATA, &size);
1421 447528 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1422 }
1423
1424 static const struct {
1425 enum AVPacketSideDataType packet;
1426 enum AVFrameSideDataType frame;
1427 } sd_global_map[] = {
1428 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
1429 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
1430 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
1431 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
1432 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
1433 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
1434 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
1435 { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
1436 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1437 { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT },
1438 };
1439
1440 447528 int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx,
1441 AVFrame *frame, const AVPacket *pkt)
1442 {
1443 static const struct {
1444 enum AVPacketSideDataType packet;
1445 enum AVFrameSideDataType frame;
1446 } sd[] = {
1447 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1448 { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD },
1449 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1450 { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES },
1451 };
1452
1453 447528 frame->pts = pkt->pts;
1454 447528 frame->duration = pkt->duration;
1455 #if FF_API_FRAME_PKT
1456 FF_DISABLE_DEPRECATION_WARNINGS
1457 447528 frame->pkt_pos = pkt->pos;
1458 447528 frame->pkt_size = pkt->size;
1459 FF_ENABLE_DEPRECATION_WARNINGS
1460 #endif
1461
1462
2/2
✓ Branch 0 taken 4475280 times.
✓ Branch 1 taken 447528 times.
4922808 for (int i = 0; i < FF_ARRAY_ELEMS(sd_global_map); i++) {
1463 size_t size;
1464 4475280 const uint8_t *packet_sd = av_packet_get_side_data(pkt, sd_global_map[i].packet, &size);
1465
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4475274 times.
4475280 if (packet_sd) {
1466 AVFrameSideData *frame_sd;
1467
1468 6 frame_sd = av_frame_new_side_data(frame, sd_global_map[i].frame, size);
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!frame_sd)
1470 return AVERROR(ENOMEM);
1471 6 memcpy(frame_sd->data, packet_sd, size);
1472 }
1473 }
1474
2/2
✓ Branch 0 taken 1790112 times.
✓ Branch 1 taken 447528 times.
2237640 for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1475 size_t size;
1476 1790112 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1477
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 1790000 times.
1790112 if (packet_sd) {
1478 112 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1479 112 sd[i].frame,
1480 size);
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (!frame_sd)
1482 return AVERROR(ENOMEM);
1483
1484 112 memcpy(frame_sd->data, packet_sd, size);
1485 }
1486 }
1487 447528 add_metadata_from_side_data(pkt, frame);
1488
1489
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 447326 times.
447528 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1490 202 frame->flags |= AV_FRAME_FLAG_DISCARD;
1491 } else {
1492 447326 frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
1493 }
1494
1495
2/2
✓ Branch 0 taken 436071 times.
✓ Branch 1 taken 11457 times.
447528 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
1496 436071 int ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref);
1497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 436071 times.
436071 if (ret < 0)
1498 return ret;
1499 436071 frame->opaque = pkt->opaque;
1500 }
1501
1502 447528 return 0;
1503 }
1504
1505 447528 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1506 {
1507 int ret;
1508
1509
2/2
✓ Branch 0 taken 4475280 times.
✓ Branch 1 taken 447528 times.
4922808 for (int i = 0; i < FF_ARRAY_ELEMS(sd_global_map); i++) {
1510 4475280 const AVPacketSideData *packet_sd = ff_get_coded_side_data(avctx,
1511 4475280 sd_global_map[i].packet);
1512
2/2
✓ Branch 0 taken 2292 times.
✓ Branch 1 taken 4472988 times.
4475280 if (packet_sd) {
1513 2292 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1514 2292 sd_global_map[i].frame,
1515 2292 packet_sd->size);
1516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2292 times.
2292 if (!frame_sd)
1517 return AVERROR(ENOMEM);
1518
1519 2292 memcpy(frame_sd->data, packet_sd->data, packet_sd->size);
1520 }
1521 }
1522
1523
1/2
✓ Branch 1 taken 447528 times.
✗ Branch 2 not taken.
447528 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
1524 447528 const AVPacket *pkt = avctx->internal->last_pkt_props;
1525
1526 447528 ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt);
1527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 447528 times.
447528 if (ret < 0)
1528 return ret;
1529 #if FF_API_FRAME_PKT
1530 FF_DISABLE_DEPRECATION_WARNINGS
1531 447528 frame->pkt_size = pkt->stream_index;
1532 FF_ENABLE_DEPRECATION_WARNINGS
1533 #endif
1534 }
1535 #if FF_API_REORDERED_OPAQUE
1536 FF_DISABLE_DEPRECATION_WARNINGS
1537 447528 frame->reordered_opaque = avctx->reordered_opaque;
1538 FF_ENABLE_DEPRECATION_WARNINGS
1539 #endif
1540
1541 447528 ret = fill_frame_props(avctx, frame);
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 447528 times.
447528 if (ret < 0)
1543 return ret;
1544
1545
2/2
✓ Branch 0 taken 139219 times.
✓ Branch 1 taken 308309 times.
447528 switch (avctx->codec->type) {
1546 139219 case AVMEDIA_TYPE_VIDEO:
1547
4/6
✓ Branch 0 taken 115499 times.
✓ Branch 1 taken 23720 times.
✓ Branch 2 taken 115499 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 115499 times.
254718 if (frame->width && frame->height &&
1548 115499 av_image_check_sar(frame->width, frame->height,
1549 frame->sample_aspect_ratio) < 0) {
1550 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1551 frame->sample_aspect_ratio.num,
1552 frame->sample_aspect_ratio.den);
1553 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1554 }
1555 139219 break;
1556 }
1557 447528 return 0;
1558 }
1559
1560 421631 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1561 {
1562
2/2
✓ Branch 0 taken 113322 times.
✓ Branch 1 taken 308309 times.
421631 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1563 int i;
1564 113322 int num_planes = av_pix_fmt_count_planes(frame->format);
1565 113322 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1566
1/2
✓ Branch 0 taken 113322 times.
✗ Branch 1 not taken.
113322 int flags = desc ? desc->flags : 0;
1567
4/4
✓ Branch 0 taken 13013 times.
✓ Branch 1 taken 100309 times.
✓ Branch 2 taken 3835 times.
✓ Branch 3 taken 9178 times.
113322 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1568 3835 num_planes = 2;
1569
2/2
✓ Branch 0 taken 318666 times.
✓ Branch 1 taken 113322 times.
431988 for (i = 0; i < num_planes; i++) {
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318666 times.
318666 av_assert0(frame->data[i]);
1571 }
1572 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1573
3/4
✓ Branch 0 taken 701232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 587910 times.
✓ Branch 3 taken 113322 times.
701232 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587910 times.
587910 if (frame->data[i])
1575 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1576 587910 frame->data[i] = NULL;
1577 }
1578 }
1579 421631 }
1580
1581 421631 static void decode_data_free(void *opaque, uint8_t *data)
1582 {
1583 421631 FrameDecodeData *fdd = (FrameDecodeData*)data;
1584
1585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (fdd->post_process_opaque_free)
1586 fdd->post_process_opaque_free(fdd->post_process_opaque);
1587
1588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (fdd->hwaccel_priv_free)
1589 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1590
1591 421631 av_freep(&fdd);
1592 421631 }
1593
1594 421631 int ff_attach_decode_data(AVFrame *frame)
1595 {
1596 AVBufferRef *fdd_buf;
1597 FrameDecodeData *fdd;
1598
1599 av_assert1(!frame->private_ref);
1600 421631 av_buffer_unref(&frame->private_ref);
1601
1602 421631 fdd = av_mallocz(sizeof(*fdd));
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (!fdd)
1604 return AVERROR(ENOMEM);
1605
1606 421631 fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
1607 NULL, AV_BUFFER_FLAG_READONLY);
1608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (!fdd_buf) {
1609 av_freep(&fdd);
1610 return AVERROR(ENOMEM);
1611 }
1612
1613 421631 frame->private_ref = fdd_buf;
1614
1615 421631 return 0;
1616 }
1617
1618 421631 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1619 {
1620 421631 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1621 421631 int override_dimensions = 1;
1622 int ret;
1623
1624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421631 times.
421631 av_assert0(av_codec_is_decoder(avctx->codec));
1625
1626
2/2
✓ Branch 0 taken 113322 times.
✓ Branch 1 taken 308309 times.
421631 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1627
2/4
✓ Branch 0 taken 113322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113322 times.
✗ Branch 3 not taken.
226644 if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
1628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 113322 times.
226644 (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) {
1629 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1630 ret = AVERROR(EINVAL);
1631 goto fail;
1632 }
1633
1634
3/4
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 112593 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729 times.
113322 if (frame->width <= 0 || frame->height <= 0) {
1635 112593 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1636 112593 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1637 112593 override_dimensions = 0;
1638 }
1639
1640
4/8
✓ Branch 0 taken 113322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113322 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 113322 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 113322 times.
113322 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1641 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1642 ret = AVERROR(EINVAL);
1643 goto fail;
1644 }
1645
1/2
✓ Branch 0 taken 308309 times.
✗ Branch 1 not taken.
308309 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1646 #if FF_API_OLD_CHANNEL_LAYOUT
1647 FF_DISABLE_DEPRECATION_WARNINGS
1648 /* compat layer for old-style get_buffer() implementations */
1649 308309 avctx->channels = avctx->ch_layout.nb_channels;
1650 616618 avctx->channel_layout = (avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) ?
1651
2/2
✓ Branch 0 taken 305510 times.
✓ Branch 1 taken 2799 times.
308309 avctx->ch_layout.u.mask : 0;
1652 FF_ENABLE_DEPRECATION_WARNINGS
1653 #endif
1654
1655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308309 times.
308309 if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
1656 av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
1657 ret = AVERROR(EINVAL);
1658 goto fail;
1659 }
1660 }
1661 421631 ret = ff_decode_frame_props(avctx, frame);
1662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (ret < 0)
1663 goto fail;
1664
1665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (hwaccel) {
1666 if (hwaccel->alloc_frame) {
1667 ret = hwaccel->alloc_frame(avctx, frame);
1668 goto end;
1669 }
1670 } else
1671 421631 avctx->sw_pix_fmt = avctx->pix_fmt;
1672
1673 421631 ret = avctx->get_buffer2(avctx, frame, flags);
1674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (ret < 0)
1675 goto fail;
1676
1677 421631 validate_avframe_allocation(avctx, frame);
1678
1679 421631 ret = ff_attach_decode_data(frame);
1680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (ret < 0)
1681 goto fail;
1682
1683 421631 end:
1684
4/4
✓ Branch 0 taken 308309 times.
✓ Branch 1 taken 113322 times.
✓ Branch 2 taken 729 times.
✓ Branch 3 taken 112593 times.
421631 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1685
2/2
✓ Branch 1 taken 36169 times.
✓ Branch 2 taken 76424 times.
112593 !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1686 76424 frame->width = avctx->width;
1687 76424 frame->height = avctx->height;
1688 }
1689
1690 345207 fail:
1691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421631 times.
421631 if (ret < 0) {
1692 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1693 av_frame_unref(frame);
1694 }
1695
1696 421631 return ret;
1697 }
1698
1699 7754 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1700 {
1701 AVFrame *tmp;
1702 int ret;
1703
1704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7754 times.
7754 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1705
1706
5/8
✓ Branch 0 taken 7605 times.
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 7605 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7605 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7605 times.
7754 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1707 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1708 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1709 av_frame_unref(frame);
1710 }
1711
1712
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 7605 times.
7754 if (!frame->data[0])
1713 149 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1714
1715
4/4
✓ Branch 0 taken 7604 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1349 times.
✓ Branch 4 taken 6255 times.
7605 if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
1716 1350 return ff_decode_frame_props(avctx, frame);
1717
1718 6255 tmp = av_frame_alloc();
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6255 times.
6255 if (!tmp)
1720 return AVERROR(ENOMEM);
1721
1722 6255 av_frame_move_ref(tmp, frame);
1723
1724 6255 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6255 times.
6255 if (ret < 0) {
1726 av_frame_free(&tmp);
1727 return ret;
1728 }
1729
1730 6255 av_frame_copy(frame, tmp);
1731 6255 av_frame_free(&tmp);
1732
1733 6255 return 0;
1734 }
1735
1736 7754 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1737 {
1738 7754 int ret = reget_buffer_internal(avctx, frame, flags);
1739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7754 times.
7754 if (ret < 0)
1740 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1741 7754 return ret;
1742 }
1743
1744 14361 int ff_decode_preinit(AVCodecContext *avctx)
1745 {
1746 14361 AVCodecInternal *avci = avctx->internal;
1747 14361 int ret = 0;
1748
1749 /* if the decoder init function was already called previously,
1750 * free the already allocated subtitle_header before overwriting it */
1751 14361 av_freep(&avctx->subtitle_header);
1752
1753
2/4
✓ Branch 0 taken 14361 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14361 times.
14361 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1754 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
1755 avctx->codec->max_lowres);
1756 avctx->lowres = avctx->codec->max_lowres;
1757 }
1758
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14355 times.
14361 if (avctx->sub_charenc) {
1759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1760 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1761 "supported with subtitles codecs\n");
1762 return AVERROR(EINVAL);
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1764 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1765 "subtitles character encoding will be ignored\n",
1766 avctx->codec_descriptor->name);
1767 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1768 } else {
1769 /* input character encoding is set for a text based subtitle
1770 * codec at this point */
1771
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1772 6 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1773
1774
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1775 #if CONFIG_ICONV
1776 6 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cd == (iconv_t)-1) {
1778 ret = AVERROR(errno);
1779 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1780 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1781 return ret;
1782 }
1783 6 iconv_close(cd);
1784 #else
1785 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1786 "conversion needs a libavcodec built with iconv support "
1787 "for this codec\n");
1788 return AVERROR(ENOSYS);
1789 #endif
1790 }
1791 }
1792 }
1793
1794 14361 avctx->pts_correction_num_faulty_pts =
1795 14361 avctx->pts_correction_num_faulty_dts = 0;
1796 14361 avctx->pts_correction_last_pts =
1797 14361 avctx->pts_correction_last_dts = INT64_MIN;
1798
1799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
1800 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
1801 av_log(avctx, AV_LOG_WARNING,
1802 "gray decoding requested but not enabled at configuration time\n");
1803
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14356 times.
14361 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
1804 5 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
1805 }
1806
1807 14361 avci->in_pkt = av_packet_alloc();
1808 14361 avci->last_pkt_props = av_packet_alloc();
1809
2/4
✓ Branch 0 taken 14361 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14361 times.
14361 if (!avci->in_pkt || !avci->last_pkt_props)
1810 return AVERROR(ENOMEM);
1811
1812 14361 ret = decode_bsfs_init(avctx);
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if (ret < 0)
1814 return ret;
1815
1816 #if FF_API_DROPCHANGED
1817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
14361 if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED)
1818 av_log(avctx, AV_LOG_WARNING, "The dropchanged flag is deprecated.\n");
1819 #endif
1820
1821 14361 return 0;
1822 }
1823
1824 1674 int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
1825 {
1826 size_t size;
1827 1674 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
1828
1829
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1642 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
1674 if (pal && size == AVPALETTE_SIZE) {
1830 32 memcpy(dst, pal, AVPALETTE_SIZE);
1831 32 return 1;
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1642 times.
1642 } else if (pal) {
1833 av_log(logctx, AV_LOG_ERROR,
1834 "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
1835 }
1836 1642 return 0;
1837 }
1838
1839 49823 int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
1840 {
1841 49823 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1842
1843
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49823 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49823 if (!hwaccel || !hwaccel->frame_priv_data_size)
1844 49823 return 0;
1845
1846 av_assert0(!*hwaccel_picture_private);
1847
1848 if (hwaccel->free_frame_priv) {
1849 AVHWFramesContext *frames_ctx;
1850
1851 if (!avctx->hw_frames_ctx)
1852 return AVERROR(EINVAL);
1853
1854 frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
1855 *hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
1856 frames_ctx->device_ctx,
1857 hwaccel->free_frame_priv);
1858 } else {
1859 *hwaccel_picture_private = ff_refstruct_allocz(hwaccel->frame_priv_data_size);
1860 }
1861
1862 if (!*hwaccel_picture_private)
1863 return AVERROR(ENOMEM);
1864
1865 return 0;
1866 }
1867
1868 100 void ff_decode_flush_buffers(AVCodecContext *avctx)
1869 {
1870 100 AVCodecInternal *avci = avctx->internal;
1871 100 DecodeContext *dc = decode_ctx(avci);
1872
1873 100 av_packet_unref(avci->last_pkt_props);
1874 100 av_packet_unref(avci->in_pkt);
1875
1876 100 avctx->pts_correction_last_pts =
1877 100 avctx->pts_correction_last_dts = INT64_MIN;
1878
1879 100 av_bsf_flush(avci->bsf);
1880
1881 100 dc->nb_draining_errors = 0;
1882 100 dc->draining_started = 0;
1883 100 }
1884
1885 14437 AVCodecInternal *ff_decode_internal_alloc(void)
1886 {
1887 14437 return av_mallocz(sizeof(DecodeContext));
1888 }
1889