FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/decode.c
Date: 2023-10-02 11:06:47
Exec Total Coverage
Lines: 619 1001 61.8%
Functions: 38 41 92.7%
Branches: 429 775 55.4%

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