Bug Summary

File:root/firefox-clang/media/ffvpx/libavcodec/decode.c
Warning:line 1787, column 14
Although the value stored to 'ret' is used in the enclosing expression, the value is never actually read from 'ret'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name decode.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/media/ffvpx/libavcodec -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/media/ffvpx/libavcodec -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -include libavutil_visibility.h -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D HAVE_AV_CONFIG_H -D ASSERT_LEVEL=2 -I /root/firefox-clang/media/ffvpx/libavcodec -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/media/ffvpx/libavcodec -I /root/firefox-clang/modules/fdlibm/inexact-math-override -I /root/firefox-clang/third_party/khronos/vulkan-headers/include -I /root/firefox-clang/media/mozva -I /root/firefox-clang/media/libopus/include -I /root/firefox-clang/media/libvorbis -I /root/firefox-clang/media/libvpx -I /root/firefox-clang/media/ffvpx -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=tautological-type-limit-compare -Wno-range-loop-analysis -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-unknown-warning-option -Wno-character-conversion -Wno-parentheses -Wno-pointer-sign -Wno-sign-compare -Wno-switch -Wno-type-limits -Wno-unused-function -Wno-deprecated-declarations -Wno-absolute-value -Wno-incompatible-pointer-types -Wno-string-conversion -Wno-visibility -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-10-17-231001-351276-1 -x c /root/firefox-clang/media/ffvpx/libavcodec/decode.c
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 <assert.h>
22#include <stdint.h>
23#include <stdbool.h>
24#include <string.h>
25
26#include "config.h"
27
28#if CONFIG_ICONV0
29# include <iconv.h>
30#endif
31
32#include "libavutil/avassert.h"
33#include "libavutil/channel_layout.h"
34#include "libavutil/common.h"
35#include "libavutil/emms.h"
36#include "libavutil/frame.h"
37#include "libavutil/hwcontext.h"
38#include "libavutil/imgutils.h"
39#include "libavutil/internal.h"
40#include "libavutil/mastering_display_metadata.h"
41#include "libavutil/mem.h"
42#include "libavutil/stereo3d.h"
43
44#include "avcodec.h"
45#include "avcodec_internal.h"
46#include "bytestream.h"
47#include "bsf.h"
48#include "codec_desc.h"
49#include "codec_internal.h"
50#include "decode.h"
51#include "exif.h"
52#include "exif_internal.h"
53#include "hwaccel_internal.h"
54#include "hwconfig.h"
55#include "internal.h"
56#include "lcevcdec.h"
57#include "packet_internal.h"
58#include "progressframe.h"
59#include "libavutil/refstruct.h"
60#include "thread.h"
61#include "threadprogress.h"
62
63typedef struct DecodeContext {
64 AVCodecInternal avci;
65
66 /**
67 * This is set to AV_FRAME_FLAG_KEY for decoders of intra-only formats
68 * (those whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set)
69 * to set the flag generically.
70 */
71 int intra_only_flag;
72
73 /**
74 * This is set to AV_PICTURE_TYPE_I for intra only video decoders
75 * and to AV_PICTURE_TYPE_NONE for other decoders. It is used to set
76 * the AVFrame's pict_type before the decoder receives it.
77 */
78 enum AVPictureType initial_pict_type;
79
80 /* to prevent infinite loop on errors when draining */
81 int nb_draining_errors;
82
83 /**
84 * The caller has submitted a NULL packet on input.
85 */
86 int draining_started;
87
88 int64_t pts_correction_num_faulty_pts; /// Number of incorrect PTS values so far
89 int64_t pts_correction_num_faulty_dts; /// Number of incorrect DTS values so far
90 int64_t pts_correction_last_pts; /// PTS of the last frame
91 int64_t pts_correction_last_dts; /// DTS of the last frame
92
93 /**
94 * Bitmask indicating for which side data types we prefer user-supplied
95 * (global or attached to packets) side data over bytestream.
96 */
97 uint64_t side_data_pref_mask;
98
99#if CONFIG_LIBLCEVC_DEC0
100 struct {
101 FFLCEVCContext *ctx;
102 int frame;
103 enum AVPixelFormat format;
104 int base_width;
105 int base_height;
106 int width;
107 int height;
108 } lcevc;
109#endif
110} DecodeContext;
111
112static DecodeContext *decode_ctx(AVCodecInternal *avci)
113{
114 return (DecodeContext *)avci;
115}
116
117static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
118{
119 int ret;
120 size_t size;
121 const uint8_t *data;
122 uint32_t flags;
123 int64_t val;
124
125 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
126 if (!data)
127 return 0;
128
129 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE(1 << 14))) {
130 av_log(avctx, AV_LOG_ERROR16, "This decoder does not support parameter "
131 "changes, but PARAM_CHANGE side data was sent to it.\n");
132 ret = AVERROR(EINVAL)(-(22));
133 goto fail2;
134 }
135
136 if (size < 4)
137 goto fail;
138
139 flags = bytestream_get_le32(&data);
140 size -= 4;
141
142 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
143 if (size < 4)
144 goto fail;
145 val = bytestream_get_le32(&data);
146 if (val <= 0 || val > INT_MAX2147483647) {
147 av_log(avctx, AV_LOG_ERROR16, "Invalid sample rate");
148 ret = AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
149 goto fail2;
150 }
151 avctx->sample_rate = val;
152 size -= 4;
153 }
154 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
155 if (size < 8)
156 goto fail;
157 avctx->width = bytestream_get_le32(&data);
158 avctx->height = bytestream_get_le32(&data);
159 size -= 8;
160 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
161 if (ret < 0)
162 goto fail2;
163 }
164
165 return 0;
166fail:
167 av_log(avctx, AV_LOG_ERROR16, "PARAM_CHANGE side data too small.\n");
168 ret = AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
169fail2:
170 if (ret < 0) {
171 av_log(avctx, AV_LOG_ERROR16, "Error applying parameter changes.\n");
172 if (avctx->err_recognition & AV_EF_EXPLODE(1<<3))
173 return ret;
174 }
175 return 0;
176}
177
178static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
179{
180 int ret = 0;
181
182 av_packet_unref(avci->last_pkt_props);
183 if (pkt) {
184 ret = av_packet_copy_props(avci->last_pkt_props, pkt);
185 }
186 return ret;
187}
188
189static int decode_bsfs_init(AVCodecContext *avctx)
190{
191 AVCodecInternal *avci = avctx->internal;
192 const FFCodec *const codec = ffcodec(avctx->codec);
193 int ret;
194
195 if (avci->bsf)
196 return 0;
197
198 ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
199 if (ret < 0) {
200 av_log(avctx, AV_LOG_ERROR16, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret)av_make_error_string((char[64]){0}, 64, ret));
201 if (ret != AVERROR(ENOMEM)(-(12)))
202 ret = AVERROR_BUG(-(int)(('B') | (('U') << 8) | (('G') << 16) | ((
unsigned)('!') << 24)))
;
203 goto fail;
204 }
205
206 /* We do not currently have an API for passing the input timebase into decoders,
207 * but no filters used here should actually need it.
208 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
209 avci->bsf->time_base_in = (AVRational){ 1, 90000 };
210 ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
211 if (ret < 0)
212 goto fail;
213
214 ret = av_bsf_init(avci->bsf);
215 if (ret < 0)
216 goto fail;
217
218 return 0;
219fail:
220 av_bsf_free(&avci->bsf);
221 return ret;
222}
223
224#if !HAVE_THREADS1
225#define ff_thread_get_packet(avctx, pkt) (AVERROR_BUG(-(int)(('B') | (('U') << 8) | (('G') << 16) | ((
unsigned)('!') << 24)))
)
226#define ff_thread_receive_frame(avctx, frame, flags) (AVERROR_BUG(-(int)(('B') | (('U') << 8) | (('G') << 16) | ((
unsigned)('!') << 24)))
)
227#endif
228
229static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
230{
231 AVCodecInternal *avci = avctx->internal;
232 int ret;
233
234 ret = av_bsf_receive_packet(avci->bsf, pkt);
235 if (ret < 0)
236 return ret;
237
238 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS(1 << 8))) {
239 ret = extract_packet_props(avctx->internal, pkt);
240 if (ret < 0)
241 goto finish;
242 }
243
244 ret = apply_param_change(avctx, pkt);
245 if (ret < 0)
246 goto finish;
247
248 return 0;
249finish:
250 av_packet_unref(pkt);
251 return ret;
252}
253
254int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
255{
256 AVCodecInternal *avci = avctx->internal;
257 DecodeContext *dc = decode_ctx(avci);
258
259 if (avci->draining)
260 return AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
;
261
262 /* If we are a worker thread, get the next packet from the threading
263 * context. Otherwise we are the main (user-facing) context, so we get the
264 * next packet from the input filterchain.
265 */
266 if (avctx->internal->is_frame_mt)
267 return ff_thread_get_packet(avctx, pkt);
268
269 while (1) {
270 int ret = decode_get_packet(avctx, pkt);
271 if (ret == AVERROR(EAGAIN)(-(11)) &&
272 (!AVPACKET_IS_EMPTY(avci->buffer_pkt)(!(avci->buffer_pkt)->data && !(avci->buffer_pkt
)->side_data_elems)
|| dc->draining_started)) {
273 ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
274 if (ret >= 0)
275 continue;
276
277 av_packet_unref(avci->buffer_pkt);
278 }
279
280 if (ret == AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
)
281 avci->draining = 1;
282 return ret;
283 }
284}
285
286/**
287 * Attempt to guess proper monotonic timestamps for decoded video frames
288 * which might have incorrect times. Input timestamps may wrap around, in
289 * which case the output will as well.
290 *
291 * @param pts the pts field of the decoded AVPacket, as passed through
292 * AVFrame.pts
293 * @param dts the dts field of the decoded AVPacket
294 * @return one of the input values, may be AV_NOPTS_VALUE
295 */
296static int64_t guess_correct_pts(DecodeContext *dc,
297 int64_t reordered_pts, int64_t dts)
298{
299 int64_t pts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
300
301 if (dts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL)) {
302 dc->pts_correction_num_faulty_dts += dts <= dc->pts_correction_last_dts;
303 dc->pts_correction_last_dts = dts;
304 } else if (reordered_pts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
305 dc->pts_correction_last_dts = reordered_pts;
306
307 if (reordered_pts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL)) {
308 dc->pts_correction_num_faulty_pts += reordered_pts <= dc->pts_correction_last_pts;
309 dc->pts_correction_last_pts = reordered_pts;
310 } else if(dts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
311 dc->pts_correction_last_pts = dts;
312
313 if ((dc->pts_correction_num_faulty_pts<=dc->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
314 && reordered_pts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
315 pts = reordered_pts;
316 else
317 pts = dts;
318
319 return pts;
320}
321
322static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
323{
324 AVCodecInternal *avci = avctx->internal;
325 AVFrameSideData *side;
326 uint32_t discard_padding = 0;
327 uint8_t skip_reason = 0;
328 uint8_t discard_reason = 0;
329
330 side = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
331 if (side && side->size >= 10) {
332 int skip_samples = AV_RL32(side->data)(((const union unaligned_32 *) (side->data))->l);
333 if (skip_samples)
334 avci->skip_samples = skip_samples;
335 avci->skip_samples = FFMAX(0, avci->skip_samples)((0) > (avci->skip_samples) ? (0) : (avci->skip_samples
))
;
336 discard_padding = AV_RL32(side->data + 4)(((const union unaligned_32 *) (side->data + 4))->l);
337 av_log(avctx, AV_LOG_DEBUG48, "skip %d / discard %d samples due to side data\n",
338 avci->skip_samples, (int)discard_padding);
339 skip_reason = AV_RL8(side->data + 8)(((const uint8_t*)(side->data + 8))[0]);
340 discard_reason = AV_RL8(side->data + 9)(((const uint8_t*)(side->data + 9))[0]);
341 }
342
343 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL(1 << 29))) {
344 if (!side && (avci->skip_samples || discard_padding))
345 side = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
346 if (side && (avci->skip_samples || discard_padding)) {
347 AV_WL32(side->data, avci->skip_samples)((((union unaligned_32 *) (side->data))->l) = (avci->
skip_samples))
;
348 AV_WL32(side->data + 4, discard_padding)((((union unaligned_32 *) (side->data + 4))->l) = (discard_padding
))
;
349 AV_WL8(side->data + 8, skip_reason)do { ((uint8_t*)(side->data + 8))[0] = (skip_reason); } while
(0)
;
350 AV_WL8(side->data + 9, discard_reason)do { ((uint8_t*)(side->data + 9))[0] = (discard_reason); }
while(0)
;
351 avci->skip_samples = 0;
352 }
353 return 0;
354 }
355 av_frame_remove_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
356
357 if ((frame->flags & AV_FRAME_FLAG_DISCARD(1 << 2))) {
358 avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples)((0) > (avci->skip_samples - frame->nb_samples) ? (0
) : (avci->skip_samples - frame->nb_samples))
;
359 *discarded_samples += frame->nb_samples;
360 return AVERROR(EAGAIN)(-(11));
361 }
362
363 if (avci->skip_samples > 0) {
364 if (frame->nb_samples <= avci->skip_samples){
365 *discarded_samples += frame->nb_samples;
366 avci->skip_samples -= frame->nb_samples;
367 av_log(avctx, AV_LOG_DEBUG48, "skip whole frame, skip left: %d\n",
368 avci->skip_samples);
369 return AVERROR(EAGAIN)(-(11));
370 } else {
371 av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
372 frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
373 if (avctx->pkt_timebase.num && avctx->sample_rate) {
374 int64_t diff_ts = av_rescale_q(avci->skip_samples,
375 (AVRational){1, avctx->sample_rate},
376 avctx->pkt_timebase);
377 if (diff_ts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL)) {
378 if (frame->pts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
379 frame->pts = av_sat_add64av_sat_add64_c(frame->pts, diff_ts);
380 if (frame->pkt_dts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
381 frame->pkt_dts = av_sat_add64av_sat_add64_c(frame->pkt_dts, diff_ts);
382 if (frame->duration >= diff_ts)
383 frame->duration = av_sat_sub64av_sat_sub64_c(frame->duration, diff_ts);
384 } else {
385 frame->pts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
386 frame->pkt_dts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
387 frame->duration = 0;
388 }
389 } else
390 av_log(avctx, AV_LOG_WARNING24, "Could not update timestamps for skipped samples.\n");
391
392 av_log(avctx, AV_LOG_DEBUG48, "skip %d/%d samples\n",
393 avci->skip_samples, frame->nb_samples);
394 *discarded_samples += avci->skip_samples;
395 frame->nb_samples -= avci->skip_samples;
396 avci->skip_samples = 0;
397 }
398 }
399
400 if (discard_padding > 0 && discard_padding <= frame->nb_samples) {
401 if (discard_padding == frame->nb_samples) {
402 *discarded_samples += frame->nb_samples;
403 return AVERROR(EAGAIN)(-(11));
404 } else {
405 if (avctx->pkt_timebase.num && avctx->sample_rate) {
406 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
407 (AVRational){1, avctx->sample_rate},
408 avctx->pkt_timebase);
409 frame->duration = diff_ts == AV_NOPTS_VALUE((int64_t)0x8000000000000000UL) ? 0 : diff_ts;
410 } else
411 av_log(avctx, AV_LOG_WARNING24, "Could not update timestamps for discarded samples.\n");
412
413 av_log(avctx, AV_LOG_DEBUG48, "discard %d/%d samples\n",
414 (int)discard_padding, frame->nb_samples);
415 frame->nb_samples -= discard_padding;
416 }
417 }
418
419 return 0;
420}
421
422/*
423 * The core of the receive_frame_wrapper for the decoders implementing
424 * the simple API. Certain decoders might consume partial packets without
425 * returning any output, so this function needs to be called in a loop until it
426 * returns EAGAIN.
427 **/
428static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
429{
430 AVCodecInternal *avci = avctx->internal;
431 DecodeContext *dc = decode_ctx(avci);
432 AVPacket *const pkt = avci->in_pkt;
433 const FFCodec *const codec = ffcodec(avctx->codec);
434 int got_frame, consumed;
435 int ret;
436
437 if (!pkt->data && !avci->draining) {
438 av_packet_unref(pkt);
439 ret = ff_decode_get_packet(avctx, pkt);
440 if (ret < 0 && ret != AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
)
441 return ret;
442 }
443
444 // Some codecs (at least wma lossless) will crash when feeding drain packets
445 // after EOF was signaled.
446 if (avci->draining_done)
447 return AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
;
448
449 if (!pkt->data &&
450 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY(1 << 5)))
451 return AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
;
452
453 got_frame = 0;
454
455 frame->pict_type = dc->initial_pict_type;
456 frame->flags |= dc->intra_only_flag;
457 consumed = codec->cb.decode(avctx, frame, &got_frame, pkt);
458
459 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS(1 << 2)))
460 frame->pkt_dts = pkt->dts;
461 emms_cemms_c();
462
463 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
464 ret = (!got_frame || frame->flags & AV_FRAME_FLAG_DISCARD(1 << 2))
465 ? AVERROR(EAGAIN)(-(11))
466 : 0;
467 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
468 ret = !got_frame ? AVERROR(EAGAIN)(-(11))
469 : discard_samples(avctx, frame, discarded_samples);
470 } else
471 av_assert0(0)do { if (!(0)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "0", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c",
471); abort(); } } while (0)
;
472
473 if (ret == AVERROR(EAGAIN)(-(11)))
474 av_frame_unref(frame);
475
476 // FF_CODEC_CB_TYPE_DECODE decoders must not return AVERROR EAGAIN
477 // code later will add AVERROR(EAGAIN) to a pointer
478 av_assert0(consumed != AVERROR(EAGAIN))do { if (!(consumed != (-(11)))) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "consumed != (-(11))", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 478); abort(); } } while (0)
;
479 if (consumed < 0)
480 ret = consumed;
481 if (consumed >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
482 consumed = pkt->size;
483
484 if (!ret)
485 av_assert0(frame->buf[0])do { if (!(frame->buf[0])) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "frame->buf[0]", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 485); abort(); } } while (0)
;
486 if (ret == AVERROR(EAGAIN)(-(11)))
487 ret = 0;
488
489 /* do not stop draining when got_frame != 0 or ret < 0 */
490 if (avci->draining && !got_frame) {
491 if (ret < 0) {
492 /* prevent infinite loop if a decoder wrongly always return error on draining */
493 /* reasonable nb_errors_max = maximum b frames + thread count */
494 int nb_errors_max = 20 + (HAVE_THREADS1 && avctx->active_thread_type & FF_THREAD_FRAME1 ?
495 avctx->thread_count : 1);
496
497 if (decode_ctx(avci)->nb_draining_errors++ >= nb_errors_max) {
498 av_log(avctx, AV_LOG_ERROR16, "Too many errors when draining, this is a bug. "
499 "Stop draining and force EOF.\n");
500 avci->draining_done = 1;
501 ret = AVERROR_BUG(-(int)(('B') | (('U') << 8) | (('G') << 16) | ((
unsigned)('!') << 24)))
;
502 }
503 } else {
504 avci->draining_done = 1;
505 }
506 }
507
508 if (consumed >= pkt->size || ret < 0) {
509 av_packet_unref(pkt);
510 } else {
511 pkt->data += consumed;
512 pkt->size -= consumed;
513 pkt->pts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
514 pkt->dts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
515 if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS(1 << 8))) {
516 avci->last_pkt_props->pts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
517 avci->last_pkt_props->dts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
518 }
519 }
520
521 return ret;
522}
523
524#if CONFIG_LCMS20
525static int detect_colorspace(AVCodecContext *avctx, AVFrame *frame)
526{
527 AVCodecInternal *avci = avctx->internal;
528 enum AVColorTransferCharacteristic trc;
529 AVColorPrimariesDesc coeffs;
530 enum AVColorPrimaries prim;
531 cmsHPROFILE profile;
532 AVFrameSideData *sd;
533 int ret;
534 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES(1U << 31)))
535 return 0;
536
537 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
538 if (!sd || !sd->size)
539 return 0;
540
541 if (!avci->icc.avctx) {
542 ret = ff_icc_context_init(&avci->icc, avctx);
543 if (ret < 0)
544 return ret;
545 }
546
547 profile = cmsOpenProfileFromMemTHR(avci->icc.ctx, sd->data, sd->size);
548 if (!profile)
549 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
550
551 ret = ff_icc_profile_sanitize(&avci->icc, profile);
552 if (!ret)
553 ret = ff_icc_profile_read_primaries(&avci->icc, profile, &coeffs);
554 if (!ret)
555 ret = ff_icc_profile_detect_transfer(&avci->icc, profile, &trc);
556 cmsCloseProfile(profile);
557 if (ret < 0)
558 return ret;
559
560 prim = av_csp_primaries_id_from_desc(&coeffs);
561 if (prim != AVCOL_PRI_UNSPECIFIED)
562 frame->color_primaries = prim;
563 if (trc != AVCOL_TRC_UNSPECIFIED)
564 frame->color_trc = trc;
565 return 0;
566}
567#else /* !CONFIG_LCMS2 */
568static int detect_colorspace(av_unused__attribute__((unused)) AVCodecContext *c, av_unused__attribute__((unused)) AVFrame *f)
569{
570 return 0;
571}
572#endif
573
574static int fill_frame_props(const AVCodecContext *avctx, AVFrame *frame)
575{
576 int ret;
577
578 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
579 frame->color_primaries = avctx->color_primaries;
580 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
581 frame->color_trc = avctx->color_trc;
582 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
583 frame->colorspace = avctx->colorspace;
584 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
585 frame->color_range = avctx->color_range;
586 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
587 frame->chroma_location = avctx->chroma_sample_location;
588 if (frame->alpha_mode == AVALPHA_MODE_UNSPECIFIED)
589 frame->alpha_mode = avctx->alpha_mode;
590
591 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
592 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
593 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
594 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
595 if (frame->format == AV_SAMPLE_FMT_NONE)
596 frame->format = avctx->sample_fmt;
597 if (!frame->ch_layout.nb_channels) {
598 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
599 if (ret < 0)
600 return ret;
601 }
602 if (!frame->sample_rate)
603 frame->sample_rate = avctx->sample_rate;
604 }
605
606 return 0;
607}
608
609static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
610{
611 int ret;
612 int64_t discarded_samples = 0;
613
614 while (!frame->buf[0]) {
615 if (discarded_samples > avctx->max_samples)
616 return AVERROR(EAGAIN)(-(11));
617 ret = decode_simple_internal(avctx, frame, &discarded_samples);
618 if (ret < 0)
619 return ret;
620 }
621
622 return 0;
623}
624
625int ff_decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
626{
627 AVCodecInternal *avci = avctx->internal;
628 DecodeContext *dc = decode_ctx(avci);
629 const FFCodec *const codec = ffcodec(avctx->codec);
630 int ret;
631
632 av_assert0(!frame->buf[0])do { if (!(!frame->buf[0])) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "!frame->buf[0]", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 632); abort(); } } while (0)
;
633
634 if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) {
635 while (1) {
636 frame->pict_type = dc->initial_pict_type;
637 frame->flags |= dc->intra_only_flag;
638 ret = codec->cb.receive_frame(avctx, frame);
639 emms_cemms_c();
640 if (!ret) {
641 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
642 int64_t discarded_samples = 0;
643 ret = discard_samples(avctx, frame, &discarded_samples);
644 }
645 if (ret == AVERROR(EAGAIN)(-(11)) || (frame->flags & AV_FRAME_FLAG_DISCARD(1 << 2))) {
646 av_frame_unref(frame);
647 continue;
648 }
649 }
650 break;
651 }
652 } else
653 ret = decode_simple_receive_frame(avctx, frame);
654
655 if (ret == AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
)
656 avci->draining_done = 1;
657
658 return ret;
659}
660
661static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame,
662 unsigned flags)
663{
664 AVCodecInternal *avci = avctx->internal;
665 DecodeContext *dc = decode_ctx(avci);
666 int ret, ok;
667
668 if (avctx->active_thread_type & FF_THREAD_FRAME1)
669 ret = ff_thread_receive_frame(avctx, frame, flags);
670 else
671 ret = ff_decode_receive_frame_internal(avctx, frame);
672
673 /* preserve ret */
674 ok = detect_colorspace(avctx, frame);
675 if (ok < 0) {
676 av_frame_unref(frame);
677 return ok;
678 }
679
680 if (!ret) {
681 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
682 if (!frame->width)
683 frame->width = avctx->width;
684 if (!frame->height)
685 frame->height = avctx->height;
686 }
687
688 ret = fill_frame_props(avctx, frame);
689 if (ret < 0) {
690 av_frame_unref(frame);
691 return ret;
692 }
693
694 frame->best_effort_timestamp = guess_correct_pts(dc,
695 frame->pts,
696 frame->pkt_dts);
697
698 /* the only case where decode data is not set should be decoders
699 * that do not call ff_get_buffer() */
700 av_assert0(frame->private_ref ||do { if (!(frame->private_ref || !(avctx->codec->capabilities
& (1 << 1)))) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "frame->private_ref || !(avctx->codec->capabilities & (1 << 1))"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 701)
; abort(); } } while (0)
701 !(avctx->codec->capabilities & AV_CODEC_CAP_DR1))do { if (!(frame->private_ref || !(avctx->codec->capabilities
& (1 << 1)))) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "frame->private_ref || !(avctx->codec->capabilities & (1 << 1))"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 701)
; abort(); } } while (0)
;
702
703 if (frame->private_ref) {
704 FrameDecodeData *fdd = frame->private_ref;
705
706 if (fdd->hwaccel_priv_post_process) {
707 ret = fdd->hwaccel_priv_post_process(avctx, frame);
708 if (ret < 0) {
709 av_frame_unref(frame);
710 return ret;
711 }
712 }
713
714 if (fdd->post_process) {
715 ret = fdd->post_process(avctx, frame);
716 if (ret < 0) {
717 av_frame_unref(frame);
718 return ret;
719 }
720 }
721 }
722 }
723
724 /* free the per-frame decode data */
725 av_refstruct_unref(&frame->private_ref);
726
727 return ret;
728}
729
730int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
731{
732 AVCodecInternal *avci = avctx->internal;
733 DecodeContext *dc = decode_ctx(avci);
734 int ret;
735
736 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
737 return AVERROR(EINVAL)(-(22));
738
739 if (dc->draining_started)
740 return AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
;
741
742 if (avpkt && !avpkt->size && avpkt->data)
743 return AVERROR(EINVAL)(-(22));
744
745 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
746 if (!AVPACKET_IS_EMPTY(avci->buffer_pkt)(!(avci->buffer_pkt)->data && !(avci->buffer_pkt
)->side_data_elems)
)
747 return AVERROR(EAGAIN)(-(11));
748 ret = av_packet_ref(avci->buffer_pkt, avpkt);
749 if (ret < 0)
750 return ret;
751 } else
752 dc->draining_started = 1;
753
754 if (!avci->buffer_frame->buf[0] && !dc->draining_started) {
755 ret = decode_receive_frame_internal(avctx, avci->buffer_frame, 0);
756 if (ret < 0 && ret != AVERROR(EAGAIN)(-(11)) && ret != AVERROR_EOF(-(int)(('E') | (('O') << 8) | (('F') << 16) | ((
unsigned)(' ') << 24)))
)
757 return ret;
758 }
759
760 return 0;
761}
762
763static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
764{
765 /* make sure we are noisy about decoders returning invalid cropping data */
766 if (frame->crop_left >= INT_MAX2147483647 - frame->crop_right ||
767 frame->crop_top >= INT_MAX2147483647 - frame->crop_bottom ||
768 (frame->crop_left + frame->crop_right) >= frame->width ||
769 (frame->crop_top + frame->crop_bottom) >= frame->height) {
770 av_log(avctx, AV_LOG_WARNING24,
771 "Invalid cropping information set by a decoder: "
772 "%zu/%zu/%zu/%zu (frame size %dx%d). "
773 "This is a bug, please report it\n",
774 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
775 frame->width, frame->height);
776 frame->crop_left = 0;
777 frame->crop_right = 0;
778 frame->crop_top = 0;
779 frame->crop_bottom = 0;
780 return 0;
781 }
782
783 if (!avctx->apply_cropping)
784 return 0;
785
786 return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED(1 << 0) ?
787 AV_FRAME_CROP_UNALIGNED : 0);
788}
789
790// make sure frames returned to the caller are valid
791static int frame_validate(AVCodecContext *avctx, AVFrame *frame)
792{
793 if (!frame->buf[0] || frame->format < 0)
794 goto fail;
795
796 switch (avctx->codec_type) {
797 case AVMEDIA_TYPE_VIDEO:
798 if (frame->width <= 0 || frame->height <= 0)
799 goto fail;
800 break;
801 case AVMEDIA_TYPE_AUDIO:
802 if (!av_channel_layout_check(&frame->ch_layout) ||
803 frame->sample_rate <= 0)
804 goto fail;
805
806 break;
807 default: av_assert0(0)do { if (!(0)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "0", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c",
807); abort(); } } while (0)
;
808 }
809
810 return 0;
811fail:
812 av_log(avctx, AV_LOG_ERROR16, "An invalid frame was output by a decoder. "
813 "This is a bug, please report it.\n");
814 return AVERROR_BUG(-(int)(('B') | (('U') << 8) | (('G') << 16) | ((
unsigned)('!') << 24)))
;
815}
816
817int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame, unsigned flags)
818{
819 AVCodecInternal *avci = avctx->internal;
820 int ret;
821
822 if (avci->buffer_frame->buf[0]) {
823 av_frame_move_ref(frame, avci->buffer_frame);
824 } else {
825 ret = decode_receive_frame_internal(avctx, frame, flags);
826 if (ret < 0)
827 return ret;
828 }
829
830 ret = frame_validate(avctx, frame);
831 if (ret < 0)
832 goto fail;
833
834 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
835 ret = apply_cropping(avctx, frame);
836 if (ret < 0)
837 goto fail;
838 }
839
840 avctx->frame_num++;
841
842 return 0;
843fail:
844 av_frame_unref(frame);
845 return ret;
846}
847
848static void get_subtitle_defaults(AVSubtitle *sub)
849{
850 memset(sub, 0, sizeof(*sub));
851 sub->pts = AV_NOPTS_VALUE((int64_t)0x8000000000000000UL);
852}
853
854#define UTF8_MAX_BYTES4 4 /* 5 and 6 bytes sequences should not be used */
855static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt,
856 const AVPacket *inpkt, AVPacket *buf_pkt)
857{
858#if CONFIG_ICONV0
859 iconv_t cd = (iconv_t)-1;
860 int ret = 0;
861 char *inb, *outb;
862 size_t inl, outl;
863#endif
864
865 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER1 || inpkt->size == 0) {
866 *outpkt = inpkt;
867 return 0;
868 }
869
870#if CONFIG_ICONV0
871 inb = inpkt->data;
872 inl = inpkt->size;
873
874 if (inl >= INT_MAX2147483647 / UTF8_MAX_BYTES4 - AV_INPUT_BUFFER_PADDING_SIZE64) {
875 av_log(avctx, AV_LOG_ERROR16, "Subtitles packet is too big for recoding\n");
876 return AVERROR(ERANGE)(-(34));
877 }
878
879 cd = iconv_open("UTF-8", avctx->sub_charenc);
880 av_assert0(cd != (iconv_t)-1)do { if (!(cd != (iconv_t)-1)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "cd != (iconv_t)-1", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 880); abort(); } } while (0)
;
881
882 ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES4);
883 if (ret < 0)
884 goto end;
885 ret = av_packet_copy_props(buf_pkt, inpkt);
886 if (ret < 0)
887 goto end;
888 outb = buf_pkt->data;
889 outl = buf_pkt->size;
890
891 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
892 iconv(cd, NULL((void*)0), NULL((void*)0), &outb, &outl) == (size_t)-1 ||
893 outl >= buf_pkt->size || inl != 0) {
894 ret = FFMIN(AVERROR(errno), -1)(((-((*__errno_location ())))) > (-1) ? (-1) : ((-((*__errno_location
())))))
;
895 av_log(avctx, AV_LOG_ERROR16, "Unable to recode subtitle event \"%s\" "
896 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
897 goto end;
898 }
899 buf_pkt->size -= outl;
900 memset(buf_pkt->data + buf_pkt->size, 0, outl);
901 *outpkt = buf_pkt;
902
903 ret = 0;
904end:
905 if (ret < 0)
906 av_packet_unref(buf_pkt);
907 if (cd != (iconv_t)-1)
908 iconv_close(cd);
909 return ret;
910#else
911 av_log(avctx, AV_LOG_ERROR16, "requesting subtitles recoding without iconv");
912 return AVERROR(EINVAL)(-(22));
913#endif
914}
915
916static int utf8_check(const uint8_t *str)
917{
918 const uint8_t *byte;
919 uint32_t codepoint, min;
920
921 while (*str) {
922 byte = str;
923 GET_UTF8(codepoint, *(byte++), return 0;)codepoint= (uint8_t)(*(byte++)); { uint32_t top = (codepoint &
128) >> 1; if ((codepoint & 0xc0) == 0x80 || codepoint
>= 0xFE) {return 0;} while (codepoint & top) { unsigned
int tmp = (uint8_t)(*(byte++)) - 128; if(tmp>>6) {return
0;} codepoint= (codepoint<<6) + tmp; top <<= 5; }
codepoint &= (top << 1) - 1; }
;
924 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
925 1 << (5 * (byte - str) - 4);
926 if (codepoint < min || codepoint >= 0x110000 ||
927 codepoint == 0xFFFE /* BOM */ ||
928 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
929 return 0;
930 str = byte;
931 }
932 return 1;
933}
934
935int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
936 int *got_sub_ptr, const AVPacket *avpkt)
937{
938 int ret = 0;
939
940 if (!avpkt->data && avpkt->size) {
941 av_log(avctx, AV_LOG_ERROR16, "invalid packet: NULL data, size != 0\n");
942 return AVERROR(EINVAL)(-(22));
943 }
944 if (!avctx->codec)
945 return AVERROR(EINVAL)(-(22));
946 if (ffcodec(avctx->codec)->cb_type != FF_CODEC_CB_TYPE_DECODE_SUB) {
947 av_log(avctx, AV_LOG_ERROR16, "Codec not subtitle decoder\n");
948 return AVERROR(EINVAL)(-(22));
949 }
950
951 *got_sub_ptr = 0;
952 get_subtitle_defaults(sub);
953
954 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY(1 << 5)) || avpkt->size) {
955 AVCodecInternal *avci = avctx->internal;
956 const AVPacket *pkt;
957
958 ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
959 if (ret < 0)
960 return ret;
961
962 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE((int64_t)0x8000000000000000UL))
963 sub->pts = av_rescale_q(avpkt->pts,
964 avctx->pkt_timebase, AV_TIME_BASE_Q(AVRational){1, 1000000});
965 ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
966 if (pkt == avci->buffer_pkt) // did we recode?
967 av_packet_unref(avci->buffer_pkt);
968 if (ret < 0) {
969 *got_sub_ptr = 0;
970 avsubtitle_free(sub);
971 return ret;
972 }
973 av_assert1(!sub->num_rects || *got_sub_ptr)do { if (!(!sub->num_rects || *got_sub_ptr)) { av_log(((void
*)0), 0, "Assertion %s failed at %s:%d\n", "!sub->num_rects || *got_sub_ptr"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 973)
; abort(); } } while (0)
;
974
975 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
976 avctx->pkt_timebase.num) {
977 AVRational ms = { 1, 1000 };
978 sub->end_display_time = av_rescale_q(avpkt->duration,
979 avctx->pkt_timebase, ms);
980 }
981
982 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB(1 << 16))
983 sub->format = 0;
984 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB(1 << 17))
985 sub->format = 1;
986
987 for (unsigned i = 0; i < sub->num_rects; i++) {
988 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE2 &&
989 sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
990 av_log(avctx, AV_LOG_ERROR16,
991 "Invalid UTF-8 in decoded subtitles text; "
992 "maybe missing -sub_charenc option\n");
993 avsubtitle_free(sub);
994 *got_sub_ptr = 0;
995 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
996 }
997 }
998
999 if (*got_sub_ptr)
1000 avctx->frame_num++;
1001 }
1002
1003 return ret;
1004}
1005
1006enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
1007 const enum AVPixelFormat *fmt)
1008{
1009 const AVCodecHWConfig *config;
1010 int i, n;
1011
1012 // If a device was supplied when the codec was opened, assume that the
1013 // user wants to use it.
1014 if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
1015 AVHWDeviceContext *device_ctx =
1016 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1017 for (i = 0;; i++) {
1018 config = &ffcodec(avctx->codec)->hw_configs[i]->public;
1019 if (!config)
1020 break;
1021 if (!(config->methods &
1022 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
1023 continue;
1024 if (device_ctx->type != config->device_type)
1025 continue;
1026 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1027 if (config->pix_fmt == fmt[n])
1028 return fmt[n];
1029 }
1030 }
1031 }
1032 // No device or other setup, so we have to choose from things which
1033 // don't any other external information.
1034
1035 // Choose the first software format
1036 // (this should be best software format if any exist).
1037 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1038 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt[n]);
1039 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL(1 << 3)))
1040 return fmt[n];
1041 }
1042
1043 // Finally, traverse the list in order and choose the first entry
1044 // with no external dependencies (if there is no hardware configuration
1045 // information available then this just picks the first entry).
1046 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
1047 for (i = 0;; i++) {
1048 config = avcodec_get_hw_config(avctx->codec, i);
1049 if (!config)
1050 break;
1051 if (config->pix_fmt == fmt[n])
1052 break;
1053 }
1054 if (!config) {
1055 // No specific config available, so the decoder must be able
1056 // to handle this format without any additional setup.
1057 return fmt[n];
1058 }
1059 if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1060 // Usable with only internal setup.
1061 return fmt[n];
1062 }
1063 }
1064
1065 // Nothing is usable, give up.
1066 return AV_PIX_FMT_NONE;
1067}
1068
1069int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
1070 enum AVHWDeviceType dev_type)
1071{
1072 AVHWDeviceContext *device_ctx;
1073 AVHWFramesContext *frames_ctx;
1074 int ret;
1075
1076 if (!avctx->hwaccel)
1077 return AVERROR(ENOSYS)(-(38));
1078
1079 if (avctx->hw_frames_ctx)
1080 return 0;
1081 if (!avctx->hw_device_ctx) {
1082 av_log(avctx, AV_LOG_ERROR16, "A hardware frames or device context is "
1083 "required for hardware accelerated decoding.\n");
1084 return AVERROR(EINVAL)(-(22));
1085 }
1086
1087 device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
1088 if (device_ctx->type != dev_type) {
1089 av_log(avctx, AV_LOG_ERROR16, "Device type %s expected for hardware "
1090 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
1091 av_hwdevice_get_type_name(device_ctx->type));
1092 return AVERROR(EINVAL)(-(22));
1093 }
1094
1095 ret = avcodec_get_hw_frames_parameters(avctx,
1096 avctx->hw_device_ctx,
1097 avctx->hwaccel->pix_fmt,
1098 &avctx->hw_frames_ctx);
1099 if (ret < 0)
1100 return ret;
1101
1102 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1103
1104
1105 if (frames_ctx->initial_pool_size) {
1106 // We guarantee 4 base work surfaces. The function above guarantees 1
1107 // (the absolute minimum), so add the missing count.
1108 frames_ctx->initial_pool_size += 3;
1109 }
1110
1111 ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
1112 if (ret < 0) {
1113 av_buffer_unref(&avctx->hw_frames_ctx);
1114 return ret;
1115 }
1116
1117 return 0;
1118}
1119
1120int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
1121 AVBufferRef *device_ref,
1122 enum AVPixelFormat hw_pix_fmt,
1123 AVBufferRef **out_frames_ref)
1124{
1125 AVBufferRef *frames_ref = NULL((void*)0);
1126 const AVCodecHWConfigInternal *hw_config;
1127 const FFHWAccel *hwa;
1128 int i, ret;
1129 bool_Bool clean_priv_data = false0;
1130
1131 for (i = 0;; i++) {
1132 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1133 if (!hw_config)
1134 return AVERROR(ENOENT)(-(2));
1135 if (hw_config->public.pix_fmt == hw_pix_fmt)
1136 break;
1137 }
1138
1139 hwa = hw_config->hwaccel;
1140 if (!hwa || !hwa->frame_params)
1141 return AVERROR(ENOENT)(-(2));
1142
1143 frames_ref = av_hwframe_ctx_alloc(device_ref);
1144 if (!frames_ref)
1145 return AVERROR(ENOMEM)(-(12));
1146
1147 if (!avctx->internal->hwaccel_priv_data) {
1148 avctx->internal->hwaccel_priv_data =
1149 av_mallocz(hwa->priv_data_size);
1150 if (!avctx->internal->hwaccel_priv_data) {
1151 av_buffer_unref(&frames_ref);
1152 return AVERROR(ENOMEM)(-(12));
1153 }
1154 clean_priv_data = true1;
1155 }
1156
1157 ret = hwa->frame_params(avctx, frames_ref);
1158 if (ret >= 0) {
1159 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
1160
1161 if (frames_ctx->initial_pool_size) {
1162 // If the user has requested that extra output surfaces be
1163 // available then add them here.
1164 if (avctx->extra_hw_frames > 0)
1165 frames_ctx->initial_pool_size += avctx->extra_hw_frames;
1166
1167 // If frame threading is enabled then an extra surface per thread
1168 // is also required.
1169 if (avctx->active_thread_type & FF_THREAD_FRAME1)
1170 frames_ctx->initial_pool_size += avctx->thread_count;
1171 }
1172
1173 *out_frames_ref = frames_ref;
1174 } else {
1175 if (clean_priv_data)
1176 av_freep(&avctx->internal->hwaccel_priv_data);
1177 av_buffer_unref(&frames_ref);
1178 }
1179 return ret;
1180}
1181
1182static int hwaccel_init(AVCodecContext *avctx,
1183 const FFHWAccel *hwaccel)
1184{
1185 int err;
1186
1187 if (hwaccel->p.capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL0x0200 &&
1188 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL-2) {
1189 av_log(avctx, AV_LOG_WARNING24, "Ignoring experimental hwaccel: %s\n",
1190 hwaccel->p.name);
1191 return AVERROR_PATCHWELCOME(-(int)(('P') | (('A') << 8) | (('W') << 16) | ((
unsigned)('E') << 24)))
;
1192 }
1193
1194 if (!avctx->internal->hwaccel_priv_data && hwaccel->priv_data_size) {
1195 avctx->internal->hwaccel_priv_data =
1196 av_mallocz(hwaccel->priv_data_size);
1197 if (!avctx->internal->hwaccel_priv_data)
1198 return AVERROR(ENOMEM)(-(12));
1199 }
1200
1201 avctx->hwaccel = &hwaccel->p;
1202 if (hwaccel->init) {
1203 err = hwaccel->init(avctx);
1204 if (err < 0) {
1205 av_log(avctx, AV_LOG_ERROR16, "Failed setup for format %s: "
1206 "hwaccel initialisation returned error.\n",
1207 av_get_pix_fmt_name(hwaccel->p.pix_fmt));
1208 av_freep(&avctx->internal->hwaccel_priv_data);
1209 avctx->hwaccel = NULL((void*)0);
1210 return err;
1211 }
1212 }
1213
1214 return 0;
1215}
1216
1217void ff_hwaccel_uninit(AVCodecContext *avctx)
1218{
1219 if (FF_HW_HAS_CB(avctx, uninit)((avctx)->hwaccel && ffhwaccel((avctx)->hwaccel
)->uninit)
)
1220 FF_HW_SIMPLE_CALL(avctx, uninit)(ffhwaccel((avctx)->hwaccel)->uninit(avctx));
1221
1222 av_freep(&avctx->internal->hwaccel_priv_data);
1223
1224 avctx->hwaccel = NULL((void*)0);
1225
1226 av_buffer_unref(&avctx->hw_frames_ctx);
1227}
1228
1229int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1230{
1231 const AVPixFmtDescriptor *desc;
1232 enum AVPixelFormat *choices;
1233 enum AVPixelFormat ret, user_choice;
1234 const AVCodecHWConfigInternal *hw_config;
1235 const AVCodecHWConfig *config;
1236 int i, n, err;
1237
1238 // Find end of list.
1239 for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
1240 // Must contain at least one entry.
1241 av_assert0(n >= 1)do { if (!(n >= 1)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "n >= 1", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 1241); abort(); } } while (0)
;
1242 // If a software format is available, it must be the last entry.
1243 desc = av_pix_fmt_desc_get(fmt[n - 1]);
1244 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL(1 << 3)) {
1245 // No software format is available.
1246 } else {
1247 avctx->sw_pix_fmt = fmt[n - 1];
1248 }
1249
1250 choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
1251 if (!choices)
1252 return AV_PIX_FMT_NONE;
1253
1254 for (;;) {
1255 // Remove the previous hwaccel, if there was one.
1256 ff_hwaccel_uninit(avctx);
1257
1258 user_choice = avctx->get_format(avctx, choices);
1259 if (user_choice == AV_PIX_FMT_NONE) {
1260 // Explicitly chose nothing, give up.
1261 ret = AV_PIX_FMT_NONE;
1262 break;
1263 }
1264
1265 desc = av_pix_fmt_desc_get(user_choice);
1266 if (!desc) {
1267 av_log(avctx, AV_LOG_ERROR16, "Invalid format returned by "
1268 "get_format() callback.\n");
1269 ret = AV_PIX_FMT_NONE;
1270 break;
1271 }
1272 av_log(avctx, AV_LOG_DEBUG48, "Format %s chosen by get_format().\n",
1273 desc->name);
1274
1275 for (i = 0; i < n; i++) {
1276 if (choices[i] == user_choice)
1277 break;
1278 }
1279 if (i == n) {
1280 av_log(avctx, AV_LOG_ERROR16, "Invalid return from get_format(): "
1281 "%s not in possible list.\n", desc->name);
1282 ret = AV_PIX_FMT_NONE;
1283 break;
1284 }
1285
1286 if (ffcodec(avctx->codec)->hw_configs) {
1287 for (i = 0;; i++) {
1288 hw_config = ffcodec(avctx->codec)->hw_configs[i];
1289 if (!hw_config)
1290 break;
1291 if (hw_config->public.pix_fmt == user_choice)
1292 break;
1293 }
1294 } else {
1295 hw_config = NULL((void*)0);
1296 }
1297
1298 if (!hw_config) {
1299 // No config available, so no extra setup required.
1300 ret = user_choice;
1301 break;
1302 }
1303 config = &hw_config->public;
1304
1305 if (config->methods &
1306 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
1307 avctx->hw_frames_ctx) {
1308 const AVHWFramesContext *frames_ctx =
1309 (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1310 if (frames_ctx->format != user_choice) {
1311 av_log(avctx, AV_LOG_ERROR16, "Invalid setup for format %s: "
1312 "does not match the format of the provided frames "
1313 "context.\n", desc->name);
1314 goto try_again;
1315 }
1316 } else if (config->methods &
1317 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
1318 avctx->hw_device_ctx) {
1319 const AVHWDeviceContext *device_ctx =
1320 (AVHWDeviceContext*)avctx->hw_device_ctx->data;
1321 if (device_ctx->type != config->device_type) {
1322 av_log(avctx, AV_LOG_ERROR16, "Invalid setup for format %s: "
1323 "does not match the type of the provided device "
1324 "context.\n", desc->name);
1325 goto try_again;
1326 }
1327 } else if (config->methods &
1328 AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
1329 // Internal-only setup, no additional configuration.
1330 } else if (config->methods &
1331 AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
1332 // Some ad-hoc configuration we can't see and can't check.
1333 } else {
1334 av_log(avctx, AV_LOG_ERROR16, "Invalid setup for format %s: "
1335 "missing configuration.\n", desc->name);
1336 goto try_again;
1337 }
1338 if (hw_config->hwaccel) {
1339 av_log(avctx, AV_LOG_DEBUG48, "Format %s requires hwaccel %s "
1340 "initialisation.\n", desc->name, hw_config->hwaccel->p.name);
1341 err = hwaccel_init(avctx, hw_config->hwaccel);
1342 if (err < 0)
1343 goto try_again;
1344 }
1345 ret = user_choice;
1346 break;
1347
1348 try_again:
1349 av_log(avctx, AV_LOG_DEBUG48, "Format %s not usable, retrying "
1350 "get_format() without it.\n", desc->name);
1351 for (i = 0; i < n; i++) {
1352 if (choices[i] == user_choice)
1353 break;
1354 }
1355 for (; i + 1 < n; i++)
1356 choices[i] = choices[i + 1];
1357 --n;
1358 }
1359
1360 if (ret < 0)
1361 ff_hwaccel_uninit(avctx);
1362
1363 av_freep(&choices);
1364 return ret;
1365}
1366
1367static const AVPacketSideData*
1368packet_side_data_get(const AVPacketSideData *sd, int nb_sd,
1369 enum AVPacketSideDataType type)
1370{
1371 for (int i = 0; i < nb_sd; i++)
1372 if (sd[i].type == type)
1373 return &sd[i];
1374
1375 return NULL((void*)0);
1376}
1377
1378const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
1379 enum AVPacketSideDataType type)
1380{
1381 return packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data, type);
1382}
1383
1384static int side_data_stereo3d_merge(AVFrameSideData *sd_frame,
1385 const AVPacketSideData *sd_pkt)
1386{
1387 const AVStereo3D *src;
1388 AVStereo3D *dst;
1389 int ret;
1390
1391 ret = av_buffer_make_writable(&sd_frame->buf);
1392 if (ret < 0)
1393 return ret;
1394 sd_frame->data = sd_frame->buf->data;
1395
1396 dst = ( AVStereo3D*)sd_frame->data;
1397 src = (const AVStereo3D*)sd_pkt->data;
1398
1399 if (dst->type == AV_STEREO3D_UNSPEC)
1400 dst->type = src->type;
1401
1402 if (dst->view == AV_STEREO3D_VIEW_UNSPEC)
1403 dst->view = src->view;
1404
1405 if (dst->primary_eye == AV_PRIMARY_EYE_NONE)
1406 dst->primary_eye = src->primary_eye;
1407
1408 if (!dst->baseline)
1409 dst->baseline = src->baseline;
1410
1411 if (!dst->horizontal_disparity_adjustment.num)
1412 dst->horizontal_disparity_adjustment = src->horizontal_disparity_adjustment;
1413
1414 if (!dst->horizontal_field_of_view.num)
1415 dst->horizontal_field_of_view = src->horizontal_field_of_view;
1416
1417 return 0;
1418}
1419
1420static int side_data_exif_parse(AVFrame *dst, const AVPacketSideData *sd_pkt)
1421{
1422 AVExifMetadata ifd = { 0 };
1423 AVExifEntry *entry = NULL((void*)0);
1424 AVBufferRef *buf = NULL((void*)0);
1425 AVFrameSideData *sd_frame;
1426 int ret;
1427
1428 ret = av_exif_parse_buffer(NULL((void*)0), sd_pkt->data, sd_pkt->size, &ifd,
1429 AV_EXIF_TIFF_HEADER);
1430 if (ret < 0)
1431 return ret;
1432
1433 ret = av_exif_get_entry(NULL((void*)0), &ifd, av_exif_get_tag_id("Orientation"), 0, &entry);
1434 if (ret < 0)
1435 goto end;
1436
1437 if (!entry) {
1438 ret = av_exif_ifd_to_dict(NULL((void*)0), &ifd, &dst->metadata);
1439 if (ret < 0)
1440 goto end;
1441
1442 sd_frame = av_frame_side_data_new(&dst->side_data, &dst->nb_side_data, AV_FRAME_DATA_EXIF,
1443 sd_pkt->size, 0);
1444 if (sd_frame)
1445 memcpy(sd_frame->data, sd_pkt->data, sd_pkt->size);
1446 ret = sd_frame ? 0 : AVERROR(ENOMEM)(-(12));
1447
1448 goto end;
1449 } else if (entry->count <= 0 || entry->type != AV_TIFF_SHORT) {
1450 ret = AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
1451 goto end;
1452 }
1453
1454 // If a display matrix already exists in the frame, give it priority
1455 if (av_frame_side_data_get(dst->side_data, dst->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX))
1456 goto finish;
1457
1458 sd_frame = av_frame_side_data_new(&dst->side_data, &dst->nb_side_data, AV_FRAME_DATA_DISPLAYMATRIX,
1459 sizeof(int32_t) * 9, 0);
1460 if (!sd_frame) {
1461 ret = AVERROR(ENOMEM)(-(12));
1462 goto end;
1463 }
1464
1465 ret = av_exif_orientation_to_matrix((int32_t *)sd_frame->data, entry->value.uint[0]);
1466 if (ret < 0)
1467 goto end;
1468
1469finish:
1470 av_exif_remove_entry(NULL((void*)0), &ifd, entry->id, 0);
1471
1472 ret = av_exif_ifd_to_dict(NULL((void*)0), &ifd, &dst->metadata);
1473 if (ret < 0)
1474 goto end;
1475
1476 ret = av_exif_write(NULL((void*)0), &ifd, &buf, AV_EXIF_TIFF_HEADER);
1477 if (ret < 0)
1478 goto end;
1479
1480 if (!av_frame_side_data_add(&dst->side_data, &dst->nb_side_data, AV_FRAME_DATA_EXIF, &buf, 0)) {
1481 ret = AVERROR(ENOMEM)(-(12));
1482 goto end;
1483 }
1484
1485 ret = 0;
1486end:
1487 av_buffer_unref(&buf);
1488 av_exif_free(&ifd);
1489 return ret;
1490}
1491
1492static int side_data_map(AVFrame *dst,
1493 const AVPacketSideData *sd_src, int nb_sd_src,
1494 const SideDataMap *map)
1495
1496{
1497 for (int i = 0; map[i].packet < AV_PKT_DATA_NB; i++) {
1498 const enum AVPacketSideDataType type_pkt = map[i].packet;
1499 const enum AVFrameSideDataType type_frame = map[i].frame;
1500 const AVPacketSideData *sd_pkt;
1501 AVFrameSideData *sd_frame;
1502
1503 sd_pkt = packet_side_data_get(sd_src, nb_sd_src, type_pkt);
1504 if (!sd_pkt)
1505 continue;
1506
1507 sd_frame = av_frame_get_side_data(dst, type_frame);
1508 if (sd_frame) {
1509 if (type_frame == AV_FRAME_DATA_STEREO3D) {
1510 int ret = side_data_stereo3d_merge(sd_frame, sd_pkt);
1511 if (ret < 0)
1512 return ret;
1513 }
1514
1515 continue;
1516 }
1517
1518 switch (type_pkt) {
1519 case AV_PKT_DATA_EXIF: {
1520 int ret = side_data_exif_parse(dst, sd_pkt);
1521 if (ret < 0)
1522 return ret;
1523 break;
1524 }
1525 default:
1526 sd_frame = av_frame_new_side_data(dst, type_frame, sd_pkt->size);
1527 if (!sd_frame)
1528 return AVERROR(ENOMEM)(-(12));
1529
1530 memcpy(sd_frame->data, sd_pkt->data, sd_pkt->size);
1531 break;
1532 }
1533 }
1534
1535 return 0;
1536}
1537
1538static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1539{
1540 size_t size;
1541 const uint8_t *side_metadata;
1542
1543 AVDictionary **frame_md = &frame->metadata;
1544
1545 side_metadata = av_packet_get_side_data(avpkt,
1546 AV_PKT_DATA_STRINGS_METADATA, &size);
1547 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1548}
1549
1550int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx,
1551 AVFrame *frame, const AVPacket *pkt)
1552{
1553 static const SideDataMap sd[] = {
1554 { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
1555 { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD },
1556 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
1557 { AV_PKT_DATA_DYNAMIC_HDR_SMPTE_2094_APP5, AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5 },
1558 { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
1559 { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES },
1560 { AV_PKT_DATA_LCEVC, AV_FRAME_DATA_LCEVC },
1561 { AV_PKT_DATA_IAMF_MIX_GAIN_PARAM, AV_FRAME_DATA_IAMF_MIX_GAIN_PARAM },
1562 { AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM, AV_FRAME_DATA_IAMF_DEMIXING_INFO_PARAM },
1563 { AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM, AV_FRAME_DATA_IAMF_RECON_GAIN_INFO_PARAM },
1564 { AV_PKT_DATA_NB }
1565 };
1566
1567 int ret = 0;
1568
1569 frame->pts = pkt->pts;
1570 frame->duration = pkt->duration;
1571
1572 if (pkt->side_data_elems) {
1573 ret = side_data_map(frame, pkt->side_data, pkt->side_data_elems, ff_sd_global_map);
1574 if (ret < 0)
1575 return ret;
1576
1577 ret = side_data_map(frame, pkt->side_data, pkt->side_data_elems, sd);
1578 if (ret < 0)
1579 return ret;
1580
1581 add_metadata_from_side_data(pkt, frame);
1582 }
1583
1584 if (pkt->flags & AV_PKT_FLAG_DISCARD0x0004) {
1585 frame->flags |= AV_FRAME_FLAG_DISCARD(1 << 2);
1586 }
1587
1588 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE(1 << 7)) {
1589 ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref);
1590 if (ret < 0)
1591 return ret;
1592 frame->opaque = pkt->opaque;
1593 }
1594
1595 return 0;
1596}
1597
1598int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1599{
1600 int ret;
1601
1602 ret = side_data_map(frame, avctx->coded_side_data, avctx->nb_coded_side_data,
1603 ff_sd_global_map);
1604 if (ret < 0)
1605 return ret;
1606
1607 for (int i = 0; i < avctx->nb_decoded_side_data; i++) {
1608 const AVFrameSideData *src = avctx->decoded_side_data[i];
1609 if (av_frame_get_side_data(frame, src->type))
1610 continue;
1611 ret = av_frame_side_data_clone(&frame->side_data, &frame->nb_side_data, src, 0);
1612 if (ret < 0)
1613 return ret;
1614 }
1615
1616 if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS(1 << 8))) {
1617 const AVPacket *pkt = avctx->internal->last_pkt_props;
1618
1619 ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt);
1620 if (ret < 0)
1621 return ret;
1622 }
1623
1624 ret = fill_frame_props(avctx, frame);
1625 if (ret < 0)
1626 return ret;
1627
1628 switch (avctx->codec->type) {
1629 case AVMEDIA_TYPE_VIDEO:
1630 if (frame->width && frame->height &&
1631 av_image_check_sar(frame->width, frame->height,
1632 frame->sample_aspect_ratio) < 0) {
1633 av_log(avctx, AV_LOG_WARNING24, "ignoring invalid SAR: %u/%u\n",
1634 frame->sample_aspect_ratio.num,
1635 frame->sample_aspect_ratio.den);
1636 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1637 }
1638 break;
1639 }
1640
1641#if CONFIG_LIBLCEVC_DEC0
1642 AVCodecInternal *avci = avctx->internal;
1643 DecodeContext *dc = decode_ctx(avci);
1644
1645 dc->lcevc.frame = dc->lcevc.ctx &&
1646 av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC);
1647
1648 if (dc->lcevc.frame) {
1649 ret = ff_lcevc_parse_frame(dc->lcevc.ctx, frame, &dc->lcevc.format,
1650 &dc->lcevc.width, &dc->lcevc.height);
1651 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE(1<<3)))
1652 return ret;
1653
1654 // force get_buffer2() to allocate the base frame using the same dimensions
1655 // as the final enhanced frame, in order to prevent reinitializing the buffer
1656 // pools unnecessarely
1657 if (!ret && dc->lcevc.width && dc->lcevc.height) {
1658 dc->lcevc.base_width = frame->width;
1659 dc->lcevc.base_height = frame->height;
1660 frame->width = dc->lcevc.width;
1661 frame->height = dc->lcevc.height;
1662 } else
1663 dc->lcevc.frame = 0;
1664 }
1665#endif
1666
1667 return 0;
1668}
1669
1670static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1671{
1672 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1673 int i;
1674 int num_planes = av_pix_fmt_count_planes(frame->format);
1675 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1676 int flags = desc ? desc->flags : 0;
1677 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL(1 << 1)))
1678 num_planes = 2;
1679 for (i = 0; i < num_planes; i++) {
1680 av_assert0(frame->data[i])do { if (!(frame->data[i])) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "frame->data[i]", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 1680); abort(); } } while (0)
;
1681 }
1682 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1683 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data)(sizeof(frame->data) / sizeof((frame->data)[0])); i++) {
1684 if (frame->data[i])
1685 av_log(avctx, AV_LOG_ERROR16, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1686 frame->data[i] = NULL((void*)0);
1687 }
1688 }
1689}
1690
1691static void decode_data_free(AVRefStructOpaque unused, void *obj)
1692{
1693 FrameDecodeData *fdd = obj;
1694
1695 if (CONFIG_LIBLCEVC_DEC0)
1696 av_refstruct_unref(&fdd->post_process_opaque);
1697 else
1698 av_assert1(!fdd->post_process_opaque)do { if (!(!fdd->post_process_opaque)) { av_log(((void*)0)
, 0, "Assertion %s failed at %s:%d\n", "!fdd->post_process_opaque"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1698
); abort(); } } while (0)
;
1699
1700 if (fdd->hwaccel_priv_free)
1701 fdd->hwaccel_priv_free(fdd->hwaccel_priv);
1702}
1703
1704int ff_attach_decode_data(AVCodecContext *avctx, AVFrame *frame)
1705{
1706 FrameDecodeData *fdd;
1707
1708 av_assert1(!frame->private_ref)do { if (!(!frame->private_ref)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "!frame->private_ref", "/root/firefox-clang/media/ffvpx/libavcodec/decode.c"
, 1708); abort(); } } while (0)
;
1709 av_refstruct_unref(&frame->private_ref);
1710
1711 fdd = av_refstruct_alloc_ext(sizeof(*fdd), 0, NULL((void*)0), decode_data_free);
1712 if (!fdd)
1713 return AVERROR(ENOMEM)(-(12));
1714
1715 frame->private_ref = fdd;
1716
1717#if CONFIG_LIBLCEVC_DEC0
1718 AVCodecInternal *avci = avctx->internal;
1719 DecodeContext *dc = decode_ctx(avci);
1720
1721 if (!dc->lcevc.frame) {
1722 dc->lcevc.frame = dc->lcevc.ctx &&
1723 av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC);
1724
1725 if (dc->lcevc.frame) {
1726 int ret = ff_lcevc_parse_frame(dc->lcevc.ctx, frame, &dc->lcevc.format,
1727 &dc->lcevc.width, &dc->lcevc.height);
1728 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE(1<<3)))
1729 return ret;
1730
1731 if (!ret && dc->lcevc.width && dc->lcevc.height) {
1732 dc->lcevc.base_width = frame->width;
1733 dc->lcevc.base_height = frame->height;
1734 } else
1735 dc->lcevc.frame = 0;
1736 }
1737 }
1738 if (dc->lcevc.frame) {
1739 FFLCEVCFrame *frame_ctx;
1740 int ret;
1741
1742 if (fdd->post_process || !dc->lcevc.width || !dc->lcevc.height) {
1743 dc->lcevc.frame = 0;
1744 return 0;
1745 }
1746
1747 frame_ctx = av_refstruct_pool_get(dc->lcevc.ctx->frame_pool);
1748 if (!frame_ctx)
1749 return AVERROR(ENOMEM)(-(12));
1750
1751 frame_ctx->lcevc = av_refstruct_ref(dc->lcevc.ctx);
1752 frame_ctx->frame->width = dc->lcevc.width;
1753 frame_ctx->frame->height = dc->lcevc.height;
1754 frame_ctx->frame->format = dc->lcevc.format;
1755 avctx->bits_per_raw_sample = av_pix_fmt_desc_get(dc->lcevc.format)->comp[0].depth;
1756
1757 frame->width = dc->lcevc.base_width;
1758 frame->height = dc->lcevc.base_height;
1759
1760 ret = avctx->get_buffer2(avctx, frame_ctx->frame, 0);
1761 if (ret < 0) {
1762 av_refstruct_unref(&frame_ctx);
1763 return ret;
1764 }
1765
1766 validate_avframe_allocation(avctx, frame_ctx->frame);
1767
1768 fdd->post_process_opaque = frame_ctx;
1769 fdd->post_process = ff_lcevc_process;
1770 }
1771 dc->lcevc.frame = 0;
1772#endif
1773
1774 return 0;
1775}
1776
1777int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1778{
1779 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1780 int override_dimensions = 1;
1781 int ret;
1782
1783 av_assert0(ff_codec_is_decoder(avctx->codec))do { if (!(ff_codec_is_decoder(avctx->codec))) { av_log(((
void*)0), 0, "Assertion %s failed at %s:%d\n", "ff_codec_is_decoder(avctx->codec)"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1783
); abort(); } } while (0)
;
1784
1785 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1786 if ((unsigned)avctx->width > INT_MAX2147483647 - STRIDE_ALIGN64 ||
1787 (ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN)(((avctx->width)+(64)-1)&~((64)-1)), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
Although the value stored to 'ret' is used in the enclosing expression, the value is never actually read from 'ret'
1788 av_log(avctx, AV_LOG_ERROR16, "video_get_buffer: image parameters invalid\n");
1789 ret = AVERROR(EINVAL)(-(22));
1790 goto fail;
1791 }
1792
1793 if (frame->width <= 0 || frame->height <= 0) {
1794 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres))((avctx->width) > ((!__builtin_constant_p(avctx->lowres
) ? -((-(avctx->coded_width)) >> (avctx->lowres))
: ((avctx->coded_width) + (1<<(avctx->lowres)) -
1) >> (avctx->lowres))) ? (avctx->width) : ((!__builtin_constant_p
(avctx->lowres) ? -((-(avctx->coded_width)) >> (avctx
->lowres)) : ((avctx->coded_width) + (1<<(avctx->
lowres)) - 1) >> (avctx->lowres))))
;
1795 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres))((avctx->height) > ((!__builtin_constant_p(avctx->lowres
) ? -((-(avctx->coded_height)) >> (avctx->lowres)
) : ((avctx->coded_height) + (1<<(avctx->lowres))
- 1) >> (avctx->lowres))) ? (avctx->height) : ((
!__builtin_constant_p(avctx->lowres) ? -((-(avctx->coded_height
)) >> (avctx->lowres)) : ((avctx->coded_height) +
(1<<(avctx->lowres)) - 1) >> (avctx->lowres
))))
;
1796 override_dimensions = 0;
1797 }
1798
1799 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1800 av_log(avctx, AV_LOG_ERROR16, "pic->data[*]!=NULL in get_buffer_internal\n");
1801 ret = AVERROR(EINVAL)(-(22));
1802 goto fail;
1803 }
1804 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1805 if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
1806 av_log(avctx, AV_LOG_ERROR16, "samples per frame %d, exceeds max_samples %"PRId64"l" "d""\n", frame->nb_samples, avctx->max_samples);
1807 ret = AVERROR(EINVAL)(-(22));
1808 goto fail;
1809 }
1810 }
1811 ret = ff_decode_frame_props(avctx, frame);
1812 if (ret < 0)
1813 goto fail;
1814
1815 if (hwaccel) {
1816 if (hwaccel->alloc_frame) {
1817 ret = hwaccel->alloc_frame(avctx, frame);
1818 goto end;
1819 }
1820 } else {
1821 avctx->sw_pix_fmt = avctx->pix_fmt;
1822 }
1823
1824 ret = avctx->get_buffer2(avctx, frame, flags);
1825 if (ret < 0)
1826 goto fail;
1827
1828 validate_avframe_allocation(avctx, frame);
1829
1830 ret = ff_attach_decode_data(avctx, frame);
1831 if (ret < 0)
1832 goto fail;
1833
1834end:
1835 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1836 !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING(1 << 4))) {
1837 frame->width = avctx->width;
1838 frame->height = avctx->height;
1839 }
1840
1841fail:
1842 if (ret < 0) {
1843 av_log(avctx, AV_LOG_ERROR16, "get_buffer() failed\n");
1844 av_frame_unref(frame);
1845 }
1846
1847 return ret;
1848}
1849
1850static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1851{
1852 int ret;
1853
1854 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO)do { if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO)) { av_log
(((void*)0), 0, "Assertion %s failed at %s:%d\n", "avctx->codec_type == AVMEDIA_TYPE_VIDEO"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1854
); abort(); } } while (0)
;
1855
1856 // make sure the discard flag does not persist
1857 frame->flags &= ~AV_FRAME_FLAG_DISCARD(1 << 2);
1858
1859 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1860 av_log(avctx, AV_LOG_WARNING24, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1861 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1862 av_frame_unref(frame);
1863 }
1864
1865 if (!frame->data[0])
1866 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF(1 << 0));
1867
1868 av_frame_side_data_free(&frame->side_data, &frame->nb_side_data);
1869
1870 if ((flags & FF_REGET_BUFFER_FLAG_READONLY1) || av_frame_is_writable(frame))
1871 return ff_decode_frame_props(avctx, frame);
1872
1873 uint8_t *data[AV_VIDEO_MAX_PLANES4];
1874 AVBufferRef *buf[AV_VIDEO_MAX_PLANES4];
1875 int linesize[AV_VIDEO_MAX_PLANES4];
1876
1877 static_assert_Static_assert(AV_VIDEO_MAX_PLANES4 <= FF_ARRAY_ELEMS(frame->data)(sizeof(frame->data) / sizeof((frame->data)[0])) &&
1878 AV_VIDEO_MAX_PLANES4 <= FF_ARRAY_ELEMS(frame->buf)(sizeof(frame->buf) / sizeof((frame->buf)[0])) &&
1879 AV_VIDEO_MAX_PLANES4 <= FF_ARRAY_ELEMS(frame->linesize)(sizeof(frame->linesize) / sizeof((frame->linesize)[0])
)
,
1880 "Copying code needs to be adjusted");
1881 static_assert_Static_assert(sizeof(frame->linesize[0]) == sizeof(linesize[0]),
1882 "linesize needs to be switched to ptrdiff_t");
1883
1884 for (int i = 0; i < AV_VIDEO_MAX_PLANES4; ++i) {
1885 data[i] = frame->data[i];
1886 linesize[i] = frame->linesize[i];
1887 buf[i] = frame->buf[i];
1888 frame->buf[i] = NULL((void*)0);
1889 }
1890 av_assert1(!frame->buf[AV_VIDEO_MAX_PLANES] && !frame->extended_buf)do { if (!(!frame->buf[4] && !frame->extended_buf
)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n", "!frame->buf[4] && !frame->extended_buf"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1890
); abort(); } } while (0)
;
1891
1892 av_frame_unref(frame);
1893
1894 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF(1 << 0));
1895 if (ret >= 0) {
1896 av_image_copy2(frame->data, frame->linesize,
1897 data, linesize,
1898 frame->format, frame->width, frame->height);
1899 }
1900 for (int i = 0; i < AV_VIDEO_MAX_PLANES4; ++i)
1901 av_buffer_unref(&buf[i]);
1902
1903 return ret;
1904}
1905
1906int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1907{
1908 int ret = reget_buffer_internal(avctx, frame, flags);
1909 if (ret < 0)
1910 av_log(avctx, AV_LOG_ERROR16, "reget_buffer() failed\n");
1911 return ret;
1912}
1913
1914typedef struct ProgressInternal {
1915 ThreadProgress progress;
1916 struct AVFrame *f;
1917} ProgressInternal;
1918
1919static void check_progress_consistency(const ProgressFrame *f)
1920{
1921 av_assert1(!!f->f == !!f->progress)do { if (!(!!f->f == !!f->progress)) { av_log(((void*)0
), 0, "Assertion %s failed at %s:%d\n", "!!f->f == !!f->progress"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1921
); abort(); } } while (0)
;
1922 av_assert1(!f->progress || f->progress->f == f->f)do { if (!(!f->progress || f->progress->f == f->f
)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n", "!f->progress || f->progress->f == f->f"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1922
); abort(); } } while (0)
;
1923}
1924
1925int ff_progress_frame_alloc(AVCodecContext *avctx, ProgressFrame *f)
1926{
1927 AVRefStructPool *pool = avctx->internal->progress_frame_pool;
1928
1929 av_assert1(!f->f && !f->progress)do { if (!(!f->f && !f->progress)) { av_log(((void
*)0), 0, "Assertion %s failed at %s:%d\n", "!f->f && !f->progress"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1929
); abort(); } } while (0)
;
1930
1931 f->progress = av_refstruct_pool_get(pool);
1932 if (!f->progress)
1933 return AVERROR(ENOMEM)(-(12));
1934
1935 f->f = f->progress->f;
1936 return 0;
1937}
1938
1939int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
1940{
1941 int ret = ff_progress_frame_alloc(avctx, f);
1942 if (ret < 0)
1943 return ret;
1944
1945 ret = ff_thread_get_buffer(avctx, f->progress->f, flags);
1946 if (ret < 0) {
1947 f->f = NULL((void*)0);
1948 av_refstruct_unref(&f->progress);
1949 return ret;
1950 }
1951 return 0;
1952}
1953
1954void ff_progress_frame_ref(ProgressFrame *dst, const ProgressFrame *src)
1955{
1956 av_assert1(src->progress && src->f && src->f == src->progress->f)do { if (!(src->progress && src->f && src
->f == src->progress->f)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n"
, "src->progress && src->f && src->f == src->progress->f"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1956
); abort(); } } while (0)
;
1957 av_assert1(!dst->f && !dst->progress)do { if (!(!dst->f && !dst->progress)) { av_log
(((void*)0), 0, "Assertion %s failed at %s:%d\n", "!dst->f && !dst->progress"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 1957
); abort(); } } while (0)
;
1958 dst->f = src->f;
1959 dst->progress = av_refstruct_ref(src->progress);
1960}
1961
1962void ff_progress_frame_unref(ProgressFrame *f)
1963{
1964 check_progress_consistency(f);
1965 f->f = NULL((void*)0);
1966 av_refstruct_unref(&f->progress);
1967}
1968
1969void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
1970{
1971 if (dst == src)
1972 return;
1973 ff_progress_frame_unref(dst);
1974 check_progress_consistency(src);
1975 if (src->f)
1976 ff_progress_frame_ref(dst, src);
1977}
1978
1979void ff_progress_frame_report(ProgressFrame *f, int n)
1980{
1981 ff_thread_progress_report(&f->progress->progress, n);
1982}
1983
1984void ff_progress_frame_await(const ProgressFrame *f, int n)
1985{
1986 ff_thread_progress_await(&f->progress->progress, n);
1987}
1988
1989#if !HAVE_THREADS1
1990enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1991{
1992 return FF_THREAD_NO_FRAME_THREADING;
1993}
1994#endif /* !HAVE_THREADS */
1995
1996static av_cold__attribute__((cold)) int progress_frame_pool_init_cb(AVRefStructOpaque opaque, void *obj)
1997{
1998 const AVCodecContext *avctx = opaque.nc;
1999 ProgressInternal *progress = obj;
2000 int ret;
2001
2002 ret = ff_thread_progress_init(&progress->progress, avctx->active_thread_type & FF_THREAD_FRAME1);
2003 if (ret < 0)
2004 return ret;
2005
2006 progress->f = av_frame_alloc();
2007 if (!progress->f)
2008 return AVERROR(ENOMEM)(-(12));
2009
2010 return 0;
2011}
2012
2013static void progress_frame_pool_reset_cb(AVRefStructOpaque unused, void *obj)
2014{
2015 ProgressInternal *progress = obj;
2016
2017 ff_thread_progress_reset(&progress->progress);
2018 av_frame_unref(progress->f);
2019}
2020
2021static av_cold__attribute__((cold)) void progress_frame_pool_free_entry_cb(AVRefStructOpaque opaque, void *obj)
2022{
2023 ProgressInternal *progress = obj;
2024
2025 ff_thread_progress_destroy(&progress->progress);
2026 av_frame_free(&progress->f);
2027}
2028
2029av_cold__attribute__((cold)) int ff_decode_preinit(AVCodecContext *avctx)
2030{
2031 AVCodecInternal *avci = avctx->internal;
2032 DecodeContext *dc = decode_ctx(avci);
2033 int ret = 0;
2034
2035 dc->initial_pict_type = AV_PICTURE_TYPE_NONE;
2036 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY(1 << 0)) {
2037 dc->intra_only_flag = AV_FRAME_FLAG_KEY(1 << 1);
2038 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
2039 dc->initial_pict_type = AV_PICTURE_TYPE_I;
2040 }
2041
2042 /* if the decoder init function was already called previously,
2043 * free the already allocated subtitle_header before overwriting it */
2044 av_freep(&avctx->subtitle_header);
2045
2046 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
2047 av_log(avctx, AV_LOG_WARNING24, "The maximum value for lowres supported by the decoder is %d\n",
2048 avctx->codec->max_lowres);
2049 avctx->lowres = avctx->codec->max_lowres;
2050 }
2051 if (avctx->sub_charenc) {
2052 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
2053 av_log(avctx, AV_LOG_ERROR16, "Character encoding is only "
2054 "supported with subtitles codecs\n");
2055 return AVERROR(EINVAL)(-(22));
2056 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB(1 << 16)) {
2057 av_log(avctx, AV_LOG_WARNING24, "Codec '%s' is bitmap-based, "
2058 "subtitles character encoding will be ignored\n",
2059 avctx->codec_descriptor->name);
2060 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING-1;
2061 } else {
2062 /* input character encoding is set for a text based subtitle
2063 * codec at this point */
2064 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC0)
2065 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER1;
2066
2067 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER1) {
2068#if CONFIG_ICONV0
2069 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
2070 if (cd == (iconv_t)-1) {
2071 ret = AVERROR(errno)(-((*__errno_location ())));
2072 av_log(avctx, AV_LOG_ERROR16, "Unable to open iconv context "
2073 "with input character encoding \"%s\"\n", avctx->sub_charenc);
2074 return ret;
2075 }
2076 iconv_close(cd);
2077#else
2078 av_log(avctx, AV_LOG_ERROR16, "Character encoding subtitles "
2079 "conversion needs a libavcodec built with iconv support "
2080 "for this codec\n");
2081 return AVERROR(ENOSYS)(-(38));
2082#endif
2083 }
2084 }
2085 }
2086
2087 dc->pts_correction_num_faulty_pts =
2088 dc->pts_correction_num_faulty_dts = 0;
2089 dc->pts_correction_last_pts =
2090 dc->pts_correction_last_dts = INT64_MIN(-9223372036854775807L -1);
2091
2092 if ( !CONFIG_GRAY0 && avctx->flags & AV_CODEC_FLAG_GRAY(1 << 13)
2093 && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
2094 av_log(avctx, AV_LOG_WARNING24,
2095 "gray decoding requested but not enabled at configuration time\n");
2096 if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS(1 << 28)) {
2097 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS(1 << 0);
2098 }
2099
2100 if (avctx->nb_side_data_prefer_packet == 1 &&
2101 avctx->side_data_prefer_packet[0] == -1)
2102 dc->side_data_pref_mask = ~0ULL;
2103 else {
2104 for (unsigned i = 0; i < avctx->nb_side_data_prefer_packet; i++) {
2105 int val = avctx->side_data_prefer_packet[i];
2106
2107 if (val < 0 || val >= AV_PKT_DATA_NB) {
2108 av_log(avctx, AV_LOG_ERROR16, "Invalid side data type: %d\n", val);
2109 return AVERROR(EINVAL)(-(22));
2110 }
2111
2112 for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
2113 if (ff_sd_global_map[j].packet == val) {
2114 val = ff_sd_global_map[j].frame;
2115
2116 // this code will need to be changed when we have more than
2117 // 64 frame side data types
2118 if (val >= 64) {
2119 av_log(avctx, AV_LOG_ERROR16, "Side data type too big\n");
2120 return AVERROR_BUG(-(int)(('B') | (('U') << 8) | (('G') << 16) | ((
unsigned)('!') << 24)))
;
2121 }
2122
2123 dc->side_data_pref_mask |= 1ULL << val;
2124 }
2125 }
2126 }
2127 }
2128
2129 avci->in_pkt = av_packet_alloc();
2130 avci->last_pkt_props = av_packet_alloc();
2131 if (!avci->in_pkt || !avci->last_pkt_props)
2132 return AVERROR(ENOMEM)(-(12));
2133
2134 if (ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_USES_PROGRESSFRAMES(1 << 6)) {
2135 avci->progress_frame_pool =
2136 av_refstruct_pool_alloc_ext(sizeof(ProgressInternal),
2137 AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR(1 << 17),
2138 avctx, progress_frame_pool_init_cb,
2139 progress_frame_pool_reset_cb,
2140 progress_frame_pool_free_entry_cb, NULL((void*)0));
2141 if (!avci->progress_frame_pool)
2142 return AVERROR(ENOMEM)(-(12));
2143 }
2144 ret = decode_bsfs_init(avctx);
2145 if (ret < 0)
2146 return ret;
2147
2148 if (!(avctx->export_side_data & AV_CODEC_EXPORT_DATA_ENHANCEMENTS(1 << 4))) {
2149 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2150#if CONFIG_LIBLCEVC_DEC0
2151 ret = ff_lcevc_alloc(&dc->lcevc.ctx, av_log_get_level() + avctx->log_level_offset);
2152 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE(1<<3)))
2153 return ret;
2154#endif
2155 }
2156 }
2157
2158 return 0;
2159}
2160
2161/**
2162 * Check side data preference and clear existing side data from frame
2163 * if needed.
2164 *
2165 * @retval 0 side data of this type can be added to frame
2166 * @retval 1 side data of this type should not be added to frame
2167 */
2168static int side_data_pref(const AVCodecContext *avctx, AVFrameSideData ***sd,
2169 int *nb_sd, enum AVFrameSideDataType type)
2170{
2171 DecodeContext *dc = decode_ctx(avctx->internal);
2172
2173 // Note: could be skipped for `type` without corresponding packet sd
2174 if (av_frame_side_data_get(*sd, *nb_sd, type)) {
2175 if (dc->side_data_pref_mask & (1ULL << type))
2176 return 1;
2177 av_frame_side_data_remove(sd, nb_sd, type);
2178 }
2179
2180 return 0;
2181}
2182
2183
2184int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame,
2185 enum AVFrameSideDataType type, size_t size,
2186 AVFrameSideData **psd)
2187{
2188 AVFrameSideData *sd;
2189
2190 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data, type)) {
2191 if (psd)
2192 *psd = NULL((void*)0);
2193 return 0;
2194 }
2195
2196 sd = av_frame_new_side_data(frame, type, size);
2197 if (psd)
2198 *psd = sd;
2199
2200 return sd ? 0 : AVERROR(ENOMEM)(-(12));
2201}
2202
2203int ff_frame_new_side_data_from_buf_ext(const AVCodecContext *avctx,
2204 AVFrameSideData ***sd, int *nb_sd,
2205 enum AVFrameSideDataType type,
2206 AVBufferRef **buf)
2207{
2208 int ret = 0;
2209
2210 if (side_data_pref(avctx, sd, nb_sd, type))
2211 goto finish;
2212
2213 if (!av_frame_side_data_add(sd, nb_sd, type, buf, 0))
2214 ret = AVERROR(ENOMEM)(-(12));
2215
2216finish:
2217 av_buffer_unref(buf);
2218
2219 return ret;
2220}
2221
2222int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx,
2223 AVFrame *frame, enum AVFrameSideDataType type,
2224 AVBufferRef **buf)
2225{
2226 return ff_frame_new_side_data_from_buf_ext(avctx,
2227 &frame->side_data, &frame->nb_side_data,
2228 type, buf);
2229}
2230
2231int ff_decode_mastering_display_new_ext(const AVCodecContext *avctx,
2232 AVFrameSideData ***sd, int *nb_sd,
2233 struct AVMasteringDisplayMetadata **mdm)
2234{
2235 AVBufferRef *buf;
2236 size_t size;
2237
2238 if (side_data_pref(avctx, sd, nb_sd, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
2239 *mdm = NULL((void*)0);
2240 return 0;
2241 }
2242
2243 *mdm = av_mastering_display_metadata_alloc_size(&size);
2244 if (!*mdm)
2245 return AVERROR(ENOMEM)(-(12));
2246
2247 buf = av_buffer_create((uint8_t *)*mdm, size, NULL((void*)0), NULL((void*)0), 0);
2248 if (!buf) {
2249 av_freep(mdm);
2250 return AVERROR(ENOMEM)(-(12));
2251 }
2252
2253 if (!av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA,
2254 &buf, 0)) {
2255 *mdm = NULL((void*)0);
2256 av_buffer_unref(&buf);
2257 return AVERROR(ENOMEM)(-(12));
2258 }
2259
2260 return 0;
2261}
2262
2263int ff_decode_mastering_display_new(const AVCodecContext *avctx, AVFrame *frame,
2264 AVMasteringDisplayMetadata **mdm)
2265{
2266 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2267 AV_FRAME_DATA_MASTERING_DISPLAY_METADATA)) {
2268 *mdm = NULL((void*)0);
2269 return 0;
2270 }
2271
2272 *mdm = av_mastering_display_metadata_create_side_data(frame);
2273 return *mdm ? 0 : AVERROR(ENOMEM)(-(12));
2274}
2275
2276int ff_decode_content_light_new_ext(const AVCodecContext *avctx,
2277 AVFrameSideData ***sd, int *nb_sd,
2278 AVContentLightMetadata **clm)
2279{
2280 AVBufferRef *buf;
2281 size_t size;
2282
2283 if (side_data_pref(avctx, sd, nb_sd, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2284 *clm = NULL((void*)0);
2285 return 0;
2286 }
2287
2288 *clm = av_content_light_metadata_alloc(&size);
2289 if (!*clm)
2290 return AVERROR(ENOMEM)(-(12));
2291
2292 buf = av_buffer_create((uint8_t *)*clm, size, NULL((void*)0), NULL((void*)0), 0);
2293 if (!buf) {
2294 av_freep(clm);
2295 return AVERROR(ENOMEM)(-(12));
2296 }
2297
2298 if (!av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL,
2299 &buf, 0)) {
2300 *clm = NULL((void*)0);
2301 av_buffer_unref(&buf);
2302 return AVERROR(ENOMEM)(-(12));
2303 }
2304
2305 return 0;
2306}
2307
2308int ff_decode_content_light_new(const AVCodecContext *avctx, AVFrame *frame,
2309 AVContentLightMetadata **clm)
2310{
2311 if (side_data_pref(avctx, &frame->side_data, &frame->nb_side_data,
2312 AV_FRAME_DATA_CONTENT_LIGHT_LEVEL)) {
2313 *clm = NULL((void*)0);
2314 return 0;
2315 }
2316
2317 *clm = av_content_light_metadata_create_side_data(frame);
2318 return *clm ? 0 : AVERROR(ENOMEM)(-(12));
2319}
2320
2321int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
2322{
2323 size_t size;
2324 const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
2325
2326 if (pal && size == AVPALETTE_SIZE1024) {
2327 memcpy(dst, pal, AVPALETTE_SIZE1024);
2328 return 1;
2329 } else if (pal) {
2330 av_log(logctx, AV_LOG_ERROR16,
2331 "Palette size %zu is wrong\n", size);
2332 }
2333 return 0;
2334}
2335
2336int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
2337{
2338 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
2339
2340 if (!hwaccel || !hwaccel->frame_priv_data_size)
2341 return 0;
2342
2343 av_assert0(!*hwaccel_picture_private)do { if (!(!*hwaccel_picture_private)) { av_log(((void*)0), 0
, "Assertion %s failed at %s:%d\n", "!*hwaccel_picture_private"
, "/root/firefox-clang/media/ffvpx/libavcodec/decode.c", 2343
); abort(); } } while (0)
;
2344
2345 if (hwaccel->free_frame_priv) {
2346 AVHWFramesContext *frames_ctx;
2347
2348 if (!avctx->hw_frames_ctx)
2349 return AVERROR(EINVAL)(-(22));
2350
2351 frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
2352 *hwaccel_picture_private = av_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
2353 frames_ctx->device_ctx,
2354 hwaccel->free_frame_priv);
2355 } else {
2356 *hwaccel_picture_private = av_refstruct_allocz(hwaccel->frame_priv_data_size);
2357 }
2358
2359 if (!*hwaccel_picture_private)
2360 return AVERROR(ENOMEM)(-(12));
2361
2362 return 0;
2363}
2364
2365av_cold__attribute__((cold)) void ff_decode_flush_buffers(AVCodecContext *avctx)
2366{
2367 AVCodecInternal *avci = avctx->internal;
2368 DecodeContext *dc = decode_ctx(avci);
2369
2370 av_packet_unref(avci->last_pkt_props);
2371 av_packet_unref(avci->in_pkt);
2372
2373 dc->pts_correction_num_faulty_pts =
2374 dc->pts_correction_num_faulty_dts = 0;
2375 dc->pts_correction_last_pts =
2376 dc->pts_correction_last_dts = INT64_MIN(-9223372036854775807L -1);
2377
2378 if (avci->bsf)
2379 av_bsf_flush(avci->bsf);
2380
2381 dc->nb_draining_errors = 0;
2382 dc->draining_started = 0;
2383}
2384
2385av_cold__attribute__((cold)) AVCodecInternal *ff_decode_internal_alloc(void)
2386{
2387 return av_mallocz(sizeof(DecodeContext));
2388}
2389
2390av_cold__attribute__((cold)) void ff_decode_internal_sync(AVCodecContext *dst, const AVCodecContext *src)
2391{
2392 const DecodeContext *src_dc = decode_ctx(src->internal);
2393 DecodeContext *dst_dc = decode_ctx(dst->internal);
2394
2395 dst_dc->initial_pict_type = src_dc->initial_pict_type;
2396 dst_dc->intra_only_flag = src_dc->intra_only_flag;
2397 dst_dc->side_data_pref_mask = src_dc->side_data_pref_mask;
2398#if CONFIG_LIBLCEVC_DEC0
2399 av_refstruct_replace(&dst_dc->lcevc.ctx, src_dc->lcevc.ctx);
2400 dst_dc->lcevc.width = src_dc->lcevc.width;
2401 dst_dc->lcevc.height = src_dc->lcevc.height;
2402 dst_dc->lcevc.format = src_dc->lcevc.format;
2403#endif
2404}
2405
2406av_cold__attribute__((cold)) void ff_decode_internal_uninit(AVCodecContext *avctx)
2407{
2408#if CONFIG_LIBLCEVC_DEC0
2409 AVCodecInternal *avci = avctx->internal;
2410 DecodeContext *dc = decode_ctx(avci);
2411
2412 av_refstruct_unref(&dc->lcevc.ctx);
2413#endif
2414}
2415
2416static int attach_displaymatrix(AVCodecContext *avctx, AVFrame *frame, int orientation)
2417{
2418 AVFrameSideData *sd = NULL((void*)0);
2419 int32_t *matrix;
2420 int ret;
2421 /* invalid orientation */
2422 if (orientation < 1 || orientation > 8)
2423 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
2424 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9, &sd);
2425 if (ret < 0) {
2426 av_log(avctx, AV_LOG_ERROR16, "Could not allocate frame side data: %s\n", av_err2str(ret)av_make_error_string((char[64]){0}, 64, ret));
2427 return ret;
2428 }
2429 if (sd) {
2430 matrix = (int32_t *) sd->data;
2431 ret = av_exif_orientation_to_matrix(matrix, orientation);
2432 }
2433
2434 return ret;
2435}
2436
2437static int exif_attach_ifd(AVCodecContext *avctx, AVFrame *frame, const AVExifMetadata *ifd, AVBufferRef **pbuf)
2438{
2439 const AVExifEntry *orient = NULL((void*)0);
2440 AVExifMetadata *cloned = NULL((void*)0);
2441 int ret;
2442
2443 for (size_t i = 0; i < ifd->count; i++) {
2444 const AVExifEntry *entry = &ifd->entries[i];
2445 if (entry->id == av_exif_get_tag_id("Orientation") &&
2446 entry->count > 0 && entry->type == AV_TIFF_SHORT) {
2447 orient = entry;
2448 break;
2449 }
2450 }
2451
2452 if (orient) {
2453 av_log(avctx, AV_LOG_DEBUG48, "found EXIF orientation: %" PRIu64"l" "u" "\n", orient->value.uint[0]);
2454 ret = attach_displaymatrix(avctx, frame, orient->value.uint[0]);
2455 if (ret < 0) {
2456 av_log(avctx, AV_LOG_WARNING24, "unable to attach displaymatrix from EXIF\n");
2457 } else {
2458 cloned = av_exif_clone_ifd(ifd);
2459 if (!cloned) {
2460 ret = AVERROR(ENOMEM)(-(12));
2461 goto end;
2462 }
2463 av_exif_remove_entry(avctx, cloned, orient->id, 0);
2464 ifd = cloned;
2465 }
2466 }
2467
2468 ret = av_exif_ifd_to_dict(avctx, ifd, &frame->metadata);
2469 if (ret < 0)
2470 goto end;
2471
2472 if (cloned || !*pbuf) {
2473 av_buffer_unref(pbuf);
2474 ret = av_exif_write(avctx, ifd, pbuf, AV_EXIF_TIFF_HEADER);
2475 if (ret < 0)
2476 goto end;
2477 }
2478
2479 ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_EXIF, pbuf);
2480 if (ret < 0)
2481 goto end;
2482
2483 ret = 0;
2484
2485end:
2486 av_buffer_unref(pbuf);
2487 av_exif_free(cloned);
2488 av_free(cloned);
2489 return ret;
2490}
2491
2492int ff_decode_exif_attach_ifd(AVCodecContext *avctx, AVFrame *frame, const AVExifMetadata *ifd)
2493{
2494 AVBufferRef *dummy = NULL((void*)0);
2495 return exif_attach_ifd(avctx, frame, ifd, &dummy);
2496}
2497
2498int ff_decode_exif_attach_buffer(AVCodecContext *avctx, AVFrame *frame, AVBufferRef **pbuf,
2499 enum AVExifHeaderMode header_mode)
2500{
2501 int ret;
2502 AVBufferRef *data = *pbuf;
2503 AVExifMetadata ifd = { 0 };
2504
2505 ret = av_exif_parse_buffer(avctx, data->data, data->size, &ifd, header_mode);
2506 if (ret < 0)
2507 goto end;
2508
2509 ret = exif_attach_ifd(avctx, frame, &ifd, pbuf);
2510
2511end:
2512 av_buffer_unref(pbuf);
2513 av_exif_free(&ifd);
2514 return ret;
2515}