Bug Summary

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