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