2 * generic decoding-related code
4 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/common.h"
35 #include "libavutil/fifo.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/hwcontext.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/internal.h"
40 #include "libavutil/intmath.h"
41 #include "libavutil/opt.h"
44 #include "bytestream.h"
46 #include "codec_internal.h"
52 static int apply_param_change(AVCodecContext
*avctx
, const AVPacket
*avpkt
)
60 data
= av_packet_get_side_data(avpkt
, AV_PKT_DATA_PARAM_CHANGE
, &size
);
64 if (!(avctx
->codec
->capabilities
& AV_CODEC_CAP_PARAM_CHANGE
)) {
65 av_log(avctx
, AV_LOG_ERROR
, "This decoder does not support parameter "
66 "changes, but PARAM_CHANGE side data was sent to it.\n");
67 ret
= AVERROR(EINVAL
);
74 flags
= bytestream_get_le32(&data
);
77 #if FF_API_OLD_CHANNEL_LAYOUT
78 FF_DISABLE_DEPRECATION_WARNINGS
79 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT
) {
82 val
= bytestream_get_le32(&data
);
83 if (val
<= 0 || val
> INT_MAX
) {
84 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel count");
85 ret
= AVERROR_INVALIDDATA
;
88 avctx
->channels
= val
;
91 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT
) {
94 avctx
->channel_layout
= bytestream_get_le64(&data
);
97 FF_ENABLE_DEPRECATION_WARNINGS
99 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
) {
102 val
= bytestream_get_le32(&data
);
103 if (val
<= 0 || val
> INT_MAX
) {
104 av_log(avctx
, AV_LOG_ERROR
, "Invalid sample rate");
105 ret
= AVERROR_INVALIDDATA
;
108 avctx
->sample_rate
= val
;
111 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
) {
114 avctx
->width
= bytestream_get_le32(&data
);
115 avctx
->height
= bytestream_get_le32(&data
);
117 ret
= ff_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
124 av_log(avctx
, AV_LOG_ERROR
, "PARAM_CHANGE side data too small.\n");
125 ret
= AVERROR_INVALIDDATA
;
128 av_log(avctx
, AV_LOG_ERROR
, "Error applying parameter changes.\n");
129 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
135 #define IS_EMPTY(pkt) (!(pkt)->data)
137 static int copy_packet_props(AVPacket
*dst
, const AVPacket
*src
)
139 int ret
= av_packet_copy_props(dst
, src
);
143 dst
->size
= src
->size
; // HACK: Needed for ff_decode_frame_props().
144 dst
->data
= (void*)1; // HACK: Needed for IS_EMPTY().
149 static int extract_packet_props(AVCodecInternal
*avci
, const AVPacket
*pkt
)
151 AVPacket tmp
= { 0 };
154 if (IS_EMPTY(avci
->last_pkt_props
)) {
155 if (av_fifo_read(avci
->pkt_props
, avci
->last_pkt_props
, 1) < 0)
156 return copy_packet_props(avci
->last_pkt_props
, pkt
);
159 ret
= copy_packet_props(&tmp
, pkt
);
163 ret
= av_fifo_write(avci
->pkt_props
, &tmp
, 1);
165 av_packet_unref(&tmp
);
170 static int decode_bsfs_init(AVCodecContext
*avctx
)
172 AVCodecInternal
*avci
= avctx
->internal
;
173 const FFCodec
*const codec
= ffcodec(avctx
->codec
);
179 ret
= av_bsf_list_parse_str(codec
->bsfs
, &avci
->bsf
);
181 av_log(avctx
, AV_LOG_ERROR
, "Error parsing decoder bitstream filters '%s': %s\n", codec
->bsfs
, av_err2str(ret
));
182 if (ret
!= AVERROR(ENOMEM
))
187 /* We do not currently have an API for passing the input timebase into decoders,
188 * but no filters used here should actually need it.
189 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
190 avci
->bsf
->time_base_in
= (AVRational
){ 1, 90000 };
191 ret
= avcodec_parameters_from_context(avci
->bsf
->par_in
, avctx
);
195 ret
= av_bsf_init(avci
->bsf
);
201 av_bsf_free(&avci
->bsf
);
205 int ff_decode_get_packet(AVCodecContext
*avctx
, AVPacket
*pkt
)
207 AVCodecInternal
*avci
= avctx
->internal
;
213 ret
= av_bsf_receive_packet(avci
->bsf
, pkt
);
214 if (ret
== AVERROR_EOF
)
219 if (!(ffcodec(avctx
->codec
)->caps_internal
& FF_CODEC_CAP_SETS_FRAME_PROPS
)) {
220 ret
= extract_packet_props(avctx
->internal
, pkt
);
225 ret
= apply_param_change(avctx
, pkt
);
231 av_packet_unref(pkt
);
236 * Attempt to guess proper monotonic timestamps for decoded video frames
237 * which might have incorrect times. Input timestamps may wrap around, in
238 * which case the output will as well.
240 * @param pts the pts field of the decoded AVPacket, as passed through
242 * @param dts the dts field of the decoded AVPacket
243 * @return one of the input values, may be AV_NOPTS_VALUE
245 static int64_t guess_correct_pts(AVCodecContext
*ctx
,
246 int64_t reordered_pts
, int64_t dts
)
248 int64_t pts
= AV_NOPTS_VALUE
;
250 if (dts
!= AV_NOPTS_VALUE
) {
251 ctx
->pts_correction_num_faulty_dts
+= dts
<= ctx
->pts_correction_last_dts
;
252 ctx
->pts_correction_last_dts
= dts
;
253 } else if (reordered_pts
!= AV_NOPTS_VALUE
)
254 ctx
->pts_correction_last_dts
= reordered_pts
;
256 if (reordered_pts
!= AV_NOPTS_VALUE
) {
257 ctx
->pts_correction_num_faulty_pts
+= reordered_pts
<= ctx
->pts_correction_last_pts
;
258 ctx
->pts_correction_last_pts
= reordered_pts
;
259 } else if(dts
!= AV_NOPTS_VALUE
)
260 ctx
->pts_correction_last_pts
= dts
;
262 if ((ctx
->pts_correction_num_faulty_pts
<=ctx
->pts_correction_num_faulty_dts
|| dts
== AV_NOPTS_VALUE
)
263 && reordered_pts
!= AV_NOPTS_VALUE
)
272 * The core of the receive_frame_wrapper for the decoders implementing
273 * the simple API. Certain decoders might consume partial packets without
274 * returning any output, so this function needs to be called in a loop until it
277 static inline int decode_simple_internal(AVCodecContext
*avctx
, AVFrame
*frame
, int64_t *discarded_samples
)
279 AVCodecInternal
*avci
= avctx
->internal
;
280 AVPacket
*const pkt
= avci
->in_pkt
;
281 const FFCodec
*const codec
= ffcodec(avctx
->codec
);
282 int got_frame
, actual_got_frame
;
285 if (!pkt
->data
&& !avci
->draining
) {
286 av_packet_unref(pkt
);
287 ret
= ff_decode_get_packet(avctx
, pkt
);
288 if (ret
< 0 && ret
!= AVERROR_EOF
)
292 // Some codecs (at least wma lossless) will crash when feeding drain packets
293 // after EOF was signaled.
294 if (avci
->draining_done
)
298 !(avctx
->codec
->capabilities
& AV_CODEC_CAP_DELAY
||
299 avctx
->active_thread_type
& FF_THREAD_FRAME
))
304 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
) {
305 ret
= ff_thread_decode_frame(avctx
, frame
, &got_frame
, pkt
);
307 ret
= codec
->cb
.decode(avctx
, frame
, &got_frame
, pkt
);
309 if (!(codec
->caps_internal
& FF_CODEC_CAP_SETS_PKT_DTS
))
310 frame
->pkt_dts
= pkt
->dts
;
311 if (avctx
->codec
->type
== AVMEDIA_TYPE_VIDEO
) {
312 if(!avctx
->has_b_frames
)
313 frame
->pkt_pos
= pkt
->pos
;
314 //FIXME these should be under if(!avctx->has_b_frames)
315 /* get_buffer is supposed to set frame parameters */
316 if (!(avctx
->codec
->capabilities
& AV_CODEC_CAP_DR1
)) {
317 if (!frame
->sample_aspect_ratio
.num
) frame
->sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
318 if (!frame
->width
) frame
->width
= avctx
->width
;
319 if (!frame
->height
) frame
->height
= avctx
->height
;
320 if (frame
->format
== AV_PIX_FMT_NONE
) frame
->format
= avctx
->pix_fmt
;
325 actual_got_frame
= got_frame
;
327 if (avctx
->codec
->type
== AVMEDIA_TYPE_VIDEO
) {
328 if (frame
->flags
& AV_FRAME_FLAG_DISCARD
)
330 } else if (avctx
->codec
->type
== AVMEDIA_TYPE_AUDIO
) {
333 uint32_t discard_padding
= 0;
334 uint8_t skip_reason
= 0;
335 uint8_t discard_reason
= 0;
337 if (ret
>= 0 && got_frame
) {
338 if (frame
->format
== AV_SAMPLE_FMT_NONE
)
339 frame
->format
= avctx
->sample_fmt
;
340 if (!frame
->ch_layout
.nb_channels
) {
341 int ret2
= av_channel_layout_copy(&frame
->ch_layout
, &avctx
->ch_layout
);
347 #if FF_API_OLD_CHANNEL_LAYOUT
348 FF_DISABLE_DEPRECATION_WARNINGS
349 if (!frame
->channel_layout
)
350 frame
->channel_layout
= avctx
->ch_layout
.order
== AV_CHANNEL_ORDER_NATIVE
?
351 avctx
->ch_layout
.u
.mask
: 0;
352 if (!frame
->channels
)
353 frame
->channels
= avctx
->ch_layout
.nb_channels
;
354 FF_ENABLE_DEPRECATION_WARNINGS
356 if (!frame
->sample_rate
)
357 frame
->sample_rate
= avctx
->sample_rate
;
360 side
= av_packet_get_side_data(avci
->last_pkt_props
, AV_PKT_DATA_SKIP_SAMPLES
, &side_size
);
361 if(side
&& side_size
>=10) {
362 avci
->skip_samples
= AV_RL32(side
) * avci
->skip_samples_multiplier
;
363 avci
->skip_samples
= FFMAX(0, avci
->skip_samples
);
364 discard_padding
= AV_RL32(side
+ 4);
365 av_log(avctx
, AV_LOG_DEBUG
, "skip %d / discard %d samples due to side data\n",
366 avci
->skip_samples
, (int)discard_padding
);
367 skip_reason
= AV_RL8(side
+ 8);
368 discard_reason
= AV_RL8(side
+ 9);
371 if ((frame
->flags
& AV_FRAME_FLAG_DISCARD
) && got_frame
&&
372 !(avctx
->flags2
& AV_CODEC_FLAG2_SKIP_MANUAL
)) {
373 avci
->skip_samples
= FFMAX(0, avci
->skip_samples
- frame
->nb_samples
);
375 *discarded_samples
+= frame
->nb_samples
;
378 if (avci
->skip_samples
> 0 && got_frame
&&
379 !(avctx
->flags2
& AV_CODEC_FLAG2_SKIP_MANUAL
)) {
380 if(frame
->nb_samples
<= avci
->skip_samples
){
382 *discarded_samples
+= frame
->nb_samples
;
383 avci
->skip_samples
-= frame
->nb_samples
;
384 av_log(avctx
, AV_LOG_DEBUG
, "skip whole frame, skip left: %d\n",
387 av_samples_copy(frame
->extended_data
, frame
->extended_data
, 0, avci
->skip_samples
,
388 frame
->nb_samples
- avci
->skip_samples
, avctx
->ch_layout
.nb_channels
, frame
->format
);
389 if(avctx
->pkt_timebase
.num
&& avctx
->sample_rate
) {
390 int64_t diff_ts
= av_rescale_q(avci
->skip_samples
,
391 (AVRational
){1, avctx
->sample_rate
},
392 avctx
->pkt_timebase
);
393 if(frame
->pts
!=AV_NOPTS_VALUE
)
394 frame
->pts
+= diff_ts
;
395 if(frame
->pkt_dts
!=AV_NOPTS_VALUE
)
396 frame
->pkt_dts
+= diff_ts
;
397 if (frame
->pkt_duration
>= diff_ts
)
398 frame
->pkt_duration
-= diff_ts
;
400 av_log(avctx
, AV_LOG_WARNING
, "Could not update timestamps for skipped samples.\n");
402 av_log(avctx
, AV_LOG_DEBUG
, "skip %d/%d samples\n",
403 avci
->skip_samples
, frame
->nb_samples
);
404 *discarded_samples
+= avci
->skip_samples
;
405 frame
->nb_samples
-= avci
->skip_samples
;
406 avci
->skip_samples
= 0;
410 if (discard_padding
> 0 && discard_padding
<= frame
->nb_samples
&& got_frame
&&
411 !(avctx
->flags2
& AV_CODEC_FLAG2_SKIP_MANUAL
)) {
412 if (discard_padding
== frame
->nb_samples
) {
413 *discarded_samples
+= frame
->nb_samples
;
416 if(avctx
->pkt_timebase
.num
&& avctx
->sample_rate
) {
417 int64_t diff_ts
= av_rescale_q(frame
->nb_samples
- discard_padding
,
418 (AVRational
){1, avctx
->sample_rate
},
419 avctx
->pkt_timebase
);
420 frame
->pkt_duration
= diff_ts
;
422 av_log(avctx
, AV_LOG_WARNING
, "Could not update timestamps for discarded samples.\n");
424 av_log(avctx
, AV_LOG_DEBUG
, "discard %d/%d samples\n",
425 (int)discard_padding
, frame
->nb_samples
);
426 frame
->nb_samples
-= discard_padding
;
430 if ((avctx
->flags2
& AV_CODEC_FLAG2_SKIP_MANUAL
) && got_frame
) {
431 AVFrameSideData
*fside
= av_frame_new_side_data(frame
, AV_FRAME_DATA_SKIP_SAMPLES
, 10);
433 AV_WL32(fside
->data
, avci
->skip_samples
);
434 AV_WL32(fside
->data
+ 4, discard_padding
);
435 AV_WL8(fside
->data
+ 8, skip_reason
);
436 AV_WL8(fside
->data
+ 9, discard_reason
);
437 avci
->skip_samples
= 0;
442 if (avctx
->codec
->type
== AVMEDIA_TYPE_AUDIO
&&
443 !avci
->showed_multi_packet_warning
&&
444 ret
>= 0 && ret
!= pkt
->size
&& !(avctx
->codec
->capabilities
& AV_CODEC_CAP_SUBFRAMES
)) {
445 av_log(avctx
, AV_LOG_WARNING
, "Multiple frames in a packet.\n");
446 avci
->showed_multi_packet_warning
= 1;
450 av_frame_unref(frame
);
452 #if FF_API_FLAG_TRUNCATED
453 if (ret
>= 0 && avctx
->codec
->type
== AVMEDIA_TYPE_VIDEO
&& !(avctx
->flags
& AV_CODEC_FLAG_TRUNCATED
))
455 if (ret
>= 0 && avctx
->codec
->type
== AVMEDIA_TYPE_VIDEO
)
459 #if FF_API_AVCTX_TIMEBASE
460 if (avctx
->framerate
.num
> 0 && avctx
->framerate
.den
> 0)
461 avctx
->time_base
= av_inv_q(av_mul_q(avctx
->framerate
, (AVRational
){avctx
->ticks_per_frame
, 1}));
464 /* do not stop draining when actual_got_frame != 0 or ret < 0 */
465 /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
466 if (avci
->draining
&& !actual_got_frame
) {
468 /* prevent infinite loop if a decoder wrongly always return error on draining */
469 /* reasonable nb_errors_max = maximum b frames + thread count */
470 int nb_errors_max
= 20 + (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
?
471 avctx
->thread_count
: 1);
473 if (avci
->nb_draining_errors
++ >= nb_errors_max
) {
474 av_log(avctx
, AV_LOG_ERROR
, "Too many errors when draining, this is a bug. "
475 "Stop draining and force EOF.\n");
476 avci
->draining_done
= 1;
480 avci
->draining_done
= 1;
484 if (ret
>= pkt
->size
|| ret
< 0) {
485 av_packet_unref(pkt
);
486 av_packet_unref(avci
->last_pkt_props
);
490 pkt
->data
+= consumed
;
491 pkt
->size
-= consumed
;
492 pkt
->pts
= AV_NOPTS_VALUE
;
493 pkt
->dts
= AV_NOPTS_VALUE
;
494 if (!(codec
->caps_internal
& FF_CODEC_CAP_SETS_FRAME_PROPS
)) {
495 avci
->last_pkt_props
->size
-= consumed
; // See extract_packet_props() comment.
496 avci
->last_pkt_props
->pts
= AV_NOPTS_VALUE
;
497 avci
->last_pkt_props
->dts
= AV_NOPTS_VALUE
;
502 av_assert0(frame
->buf
[0]);
504 return ret
< 0 ? ret
: 0;
507 static int decode_simple_receive_frame(AVCodecContext
*avctx
, AVFrame
*frame
)
510 int64_t discarded_samples
= 0;
512 while (!frame
->buf
[0]) {
513 if (discarded_samples
> avctx
->max_samples
)
514 return AVERROR(EAGAIN
);
515 ret
= decode_simple_internal(avctx
, frame
, &discarded_samples
);
523 static int decode_receive_frame_internal(AVCodecContext
*avctx
, AVFrame
*frame
)
525 AVCodecInternal
*avci
= avctx
->internal
;
526 const FFCodec
*const codec
= ffcodec(avctx
->codec
);
529 av_assert0(!frame
->buf
[0]);
531 if (codec
->cb_type
== FF_CODEC_CB_TYPE_RECEIVE_FRAME
) {
532 ret
= codec
->cb
.receive_frame(avctx
, frame
);
533 if (ret
!= AVERROR(EAGAIN
))
534 av_packet_unref(avci
->last_pkt_props
);
536 ret
= decode_simple_receive_frame(avctx
, frame
);
538 if (ret
== AVERROR_EOF
)
539 avci
->draining_done
= 1;
541 if (!(codec
->caps_internal
& FF_CODEC_CAP_SETS_FRAME_PROPS
) &&
542 IS_EMPTY(avci
->last_pkt_props
)) {
543 // May fail if the FIFO is empty.
544 av_fifo_read(avci
->pkt_props
, avci
->last_pkt_props
, 1);
548 frame
->best_effort_timestamp
= guess_correct_pts(avctx
,
552 /* the only case where decode data is not set should be decoders
553 * that do not call ff_get_buffer() */
554 av_assert0((frame
->private_ref
&& frame
->private_ref
->size
== sizeof(FrameDecodeData
)) ||
555 !(avctx
->codec
->capabilities
& AV_CODEC_CAP_DR1
));
557 if (frame
->private_ref
) {
558 FrameDecodeData
*fdd
= (FrameDecodeData
*)frame
->private_ref
->data
;
560 if (fdd
->post_process
) {
561 ret
= fdd
->post_process(avctx
, frame
);
563 av_frame_unref(frame
);
570 /* free the per-frame decode data */
571 av_buffer_unref(&frame
->private_ref
);
576 int attribute_align_arg
avcodec_send_packet(AVCodecContext
*avctx
, const AVPacket
*avpkt
)
578 AVCodecInternal
*avci
= avctx
->internal
;
581 if (!avcodec_is_open(avctx
) || !av_codec_is_decoder(avctx
->codec
))
582 return AVERROR(EINVAL
);
584 if (avctx
->internal
->draining
)
587 if (avpkt
&& !avpkt
->size
&& avpkt
->data
)
588 return AVERROR(EINVAL
);
590 av_packet_unref(avci
->buffer_pkt
);
591 if (avpkt
&& (avpkt
->data
|| avpkt
->side_data_elems
)) {
592 ret
= av_packet_ref(avci
->buffer_pkt
, avpkt
);
597 ret
= av_bsf_send_packet(avci
->bsf
, avci
->buffer_pkt
);
599 av_packet_unref(avci
->buffer_pkt
);
603 if (!avci
->buffer_frame
->buf
[0]) {
604 ret
= decode_receive_frame_internal(avctx
, avci
->buffer_frame
);
605 if (ret
< 0 && ret
!= AVERROR(EAGAIN
) && ret
!= AVERROR_EOF
)
612 static int apply_cropping(AVCodecContext
*avctx
, AVFrame
*frame
)
614 /* make sure we are noisy about decoders returning invalid cropping data */
615 if (frame
->crop_left
>= INT_MAX
- frame
->crop_right
||
616 frame
->crop_top
>= INT_MAX
- frame
->crop_bottom
||
617 (frame
->crop_left
+ frame
->crop_right
) >= frame
->width
||
618 (frame
->crop_top
+ frame
->crop_bottom
) >= frame
->height
) {
619 av_log(avctx
, AV_LOG_WARNING
,
620 "Invalid cropping information set by a decoder: "
621 "%"SIZE_SPECIFIER
"/%"SIZE_SPECIFIER
"/%"SIZE_SPECIFIER
"/%"SIZE_SPECIFIER
" "
622 "(frame size %dx%d). This is a bug, please report it\n",
623 frame
->crop_left
, frame
->crop_right
, frame
->crop_top
, frame
->crop_bottom
,
624 frame
->width
, frame
->height
);
625 frame
->crop_left
= 0;
626 frame
->crop_right
= 0;
628 frame
->crop_bottom
= 0;
632 if (!avctx
->apply_cropping
)
635 return av_frame_apply_cropping(frame
, avctx
->flags
& AV_CODEC_FLAG_UNALIGNED
?
636 AV_FRAME_CROP_UNALIGNED
: 0);
639 int attribute_align_arg
avcodec_receive_frame(AVCodecContext
*avctx
, AVFrame
*frame
)
641 AVCodecInternal
*avci
= avctx
->internal
;
644 av_frame_unref(frame
);
646 if (!avcodec_is_open(avctx
) || !av_codec_is_decoder(avctx
->codec
))
647 return AVERROR(EINVAL
);
649 if (avci
->buffer_frame
->buf
[0]) {
650 av_frame_move_ref(frame
, avci
->buffer_frame
);
652 ret
= decode_receive_frame_internal(avctx
, frame
);
657 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
658 ret
= apply_cropping(avctx
, frame
);
660 av_frame_unref(frame
);
665 avctx
->frame_number
++;
667 if (avctx
->flags
& AV_CODEC_FLAG_DROPCHANGED
) {
669 if (avctx
->frame_number
== 1) {
670 avci
->initial_format
= frame
->format
;
671 switch(avctx
->codec_type
) {
672 case AVMEDIA_TYPE_VIDEO
:
673 avci
->initial_width
= frame
->width
;
674 avci
->initial_height
= frame
->height
;
676 case AVMEDIA_TYPE_AUDIO
:
677 avci
->initial_sample_rate
= frame
->sample_rate
? frame
->sample_rate
:
679 #if FF_API_OLD_CHANNEL_LAYOUT
680 FF_DISABLE_DEPRECATION_WARNINGS
681 avci
->initial_channels
= frame
->ch_layout
.nb_channels
;
682 avci
->initial_channel_layout
= frame
->channel_layout
;
683 FF_ENABLE_DEPRECATION_WARNINGS
685 ret
= av_channel_layout_copy(&avci
->initial_ch_layout
, &frame
->ch_layout
);
687 av_frame_unref(frame
);
694 if (avctx
->frame_number
> 1) {
695 changed
= avci
->initial_format
!= frame
->format
;
697 switch(avctx
->codec_type
) {
698 case AVMEDIA_TYPE_VIDEO
:
699 changed
|= avci
->initial_width
!= frame
->width
||
700 avci
->initial_height
!= frame
->height
;
702 case AVMEDIA_TYPE_AUDIO
:
703 FF_DISABLE_DEPRECATION_WARNINGS
704 changed
|= avci
->initial_sample_rate
!= frame
->sample_rate
||
705 avci
->initial_sample_rate
!= avctx
->sample_rate
||
706 #if FF_API_OLD_CHANNEL_LAYOUT
707 avci
->initial_channels
!= frame
->channels
||
708 avci
->initial_channel_layout
!= frame
->channel_layout
||
710 av_channel_layout_compare(&avci
->initial_ch_layout
, &frame
->ch_layout
);
711 FF_ENABLE_DEPRECATION_WARNINGS
716 avci
->changed_frames_dropped
++;
717 av_log(avctx
, AV_LOG_INFO
, "dropped changed frame #%d pts %"PRId64
718 " drop count: %d \n",
719 avctx
->frame_number
, frame
->pts
,
720 avci
->changed_frames_dropped
);
721 av_frame_unref(frame
);
722 return AVERROR_INPUT_CHANGED
;
729 static void get_subtitle_defaults(AVSubtitle
*sub
)
731 memset(sub
, 0, sizeof(*sub
));
732 sub
->pts
= AV_NOPTS_VALUE
;
735 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
736 static int recode_subtitle(AVCodecContext
*avctx
, AVPacket
**outpkt
,
737 AVPacket
*inpkt
, AVPacket
*buf_pkt
)
740 iconv_t cd
= (iconv_t
)-1;
746 if (avctx
->sub_charenc_mode
!= FF_SUB_CHARENC_MODE_PRE_DECODER
|| inpkt
->size
== 0) {
755 if (inl
>= INT_MAX
/ UTF8_MAX_BYTES
- AV_INPUT_BUFFER_PADDING_SIZE
) {
756 av_log(avctx
, AV_LOG_ERROR
, "Subtitles packet is too big for recoding\n");
757 return AVERROR(ERANGE
);
760 cd
= iconv_open("UTF-8", avctx
->sub_charenc
);
761 av_assert0(cd
!= (iconv_t
)-1);
763 ret
= av_new_packet(buf_pkt
, inl
* UTF8_MAX_BYTES
);
766 ret
= av_packet_copy_props(buf_pkt
, inpkt
);
769 outb
= buf_pkt
->data
;
770 outl
= buf_pkt
->size
;
772 if (iconv(cd
, &inb
, &inl
, &outb
, &outl
) == (size_t)-1 ||
773 iconv(cd
, NULL
, NULL
, &outb
, &outl
) == (size_t)-1 ||
774 outl
>= buf_pkt
->size
|| inl
!= 0) {
775 ret
= FFMIN(AVERROR(errno
), -1);
776 av_log(avctx
, AV_LOG_ERROR
, "Unable to recode subtitle event \"%s\" "
777 "from %s to UTF-8\n", inpkt
->data
, avctx
->sub_charenc
);
780 buf_pkt
->size
-= outl
;
781 memset(buf_pkt
->data
+ buf_pkt
->size
, 0, outl
);
787 av_packet_unref(buf_pkt
);
788 if (cd
!= (iconv_t
)-1)
792 av_log(avctx
, AV_LOG_ERROR
, "requesting subtitles recoding without iconv");
793 return AVERROR(EINVAL
);
797 static int utf8_check(const uint8_t *str
)
800 uint32_t codepoint
, min
;
804 GET_UTF8(codepoint
, *(byte
++), return 0;);
805 min
= byte
- str
== 1 ? 0 : byte
- str
== 2 ? 0x80 :
806 1 << (5 * (byte
- str
) - 4);
807 if (codepoint
< min
|| codepoint
>= 0x110000 ||
808 codepoint
== 0xFFFE /* BOM */ ||
809 codepoint
>= 0xD800 && codepoint
<= 0xDFFF /* surrogates */)
816 int avcodec_decode_subtitle2(AVCodecContext
*avctx
, AVSubtitle
*sub
,
822 if (!avpkt
->data
&& avpkt
->size
) {
823 av_log(avctx
, AV_LOG_ERROR
, "invalid packet: NULL data, size != 0\n");
824 return AVERROR(EINVAL
);
827 return AVERROR(EINVAL
);
828 if (avctx
->codec
->type
!= AVMEDIA_TYPE_SUBTITLE
) {
829 av_log(avctx
, AV_LOG_ERROR
, "Invalid media type for subtitles\n");
830 return AVERROR(EINVAL
);
834 get_subtitle_defaults(sub
);
836 if ((avctx
->codec
->capabilities
& AV_CODEC_CAP_DELAY
) || avpkt
->size
) {
837 AVCodecInternal
*avci
= avctx
->internal
;
840 ret
= recode_subtitle(avctx
, &pkt
, avpkt
, avci
->buffer_pkt
);
844 if (avctx
->pkt_timebase
.num
&& avpkt
->pts
!= AV_NOPTS_VALUE
)
845 sub
->pts
= av_rescale_q(avpkt
->pts
,
846 avctx
->pkt_timebase
, AV_TIME_BASE_Q
);
847 ret
= ffcodec(avctx
->codec
)->cb
.decode_sub(avctx
, sub
, got_sub_ptr
, pkt
);
848 if (pkt
== avci
->buffer_pkt
) // did we recode?
849 av_packet_unref(avci
->buffer_pkt
);
852 avsubtitle_free(sub
);
855 av_assert1(!sub
->num_rects
|| *got_sub_ptr
);
857 if (sub
->num_rects
&& !sub
->end_display_time
&& avpkt
->duration
&&
858 avctx
->pkt_timebase
.num
) {
859 AVRational ms
= { 1, 1000 };
860 sub
->end_display_time
= av_rescale_q(avpkt
->duration
,
861 avctx
->pkt_timebase
, ms
);
864 if (avctx
->codec_descriptor
->props
& AV_CODEC_PROP_BITMAP_SUB
)
866 else if (avctx
->codec_descriptor
->props
& AV_CODEC_PROP_TEXT_SUB
)
869 for (unsigned i
= 0; i
< sub
->num_rects
; i
++) {
870 if (avctx
->sub_charenc_mode
!= FF_SUB_CHARENC_MODE_IGNORE
&&
871 sub
->rects
[i
]->ass
&& !utf8_check(sub
->rects
[i
]->ass
)) {
872 av_log(avctx
, AV_LOG_ERROR
,
873 "Invalid UTF-8 in decoded subtitles text; "
874 "maybe missing -sub_charenc option\n");
875 avsubtitle_free(sub
);
877 return AVERROR_INVALIDDATA
;
882 avctx
->frame_number
++;
888 enum AVPixelFormat
avcodec_default_get_format(struct AVCodecContext
*avctx
,
889 const enum AVPixelFormat
*fmt
)
891 const AVPixFmtDescriptor
*desc
;
892 const AVCodecHWConfig
*config
;
895 // If a device was supplied when the codec was opened, assume that the
896 // user wants to use it.
897 if (avctx
->hw_device_ctx
&& ffcodec(avctx
->codec
)->hw_configs
) {
898 AVHWDeviceContext
*device_ctx
=
899 (AVHWDeviceContext
*)avctx
->hw_device_ctx
->data
;
901 config
= &ffcodec(avctx
->codec
)->hw_configs
[i
]->public;
904 if (!(config
->methods
&
905 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
))
907 if (device_ctx
->type
!= config
->device_type
)
909 for (n
= 0; fmt
[n
] != AV_PIX_FMT_NONE
; n
++) {
910 if (config
->pix_fmt
== fmt
[n
])
915 // No device or other setup, so we have to choose from things which
916 // don't any other external information.
918 // If the last element of the list is a software format, choose it
919 // (this should be best software format if any exist).
920 for (n
= 0; fmt
[n
] != AV_PIX_FMT_NONE
; n
++);
921 desc
= av_pix_fmt_desc_get(fmt
[n
- 1]);
922 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
))
925 // Finally, traverse the list in order and choose the first entry
926 // with no external dependencies (if there is no hardware configuration
927 // information available then this just picks the first entry).
928 for (n
= 0; fmt
[n
] != AV_PIX_FMT_NONE
; n
++) {
930 config
= avcodec_get_hw_config(avctx
->codec
, i
);
933 if (config
->pix_fmt
== fmt
[n
])
937 // No specific config available, so the decoder must be able
938 // to handle this format without any additional setup.
941 if (config
->methods
& AV_CODEC_HW_CONFIG_METHOD_INTERNAL
) {
942 // Usable with only internal setup.
947 // Nothing is usable, give up.
948 return AV_PIX_FMT_NONE
;
951 int ff_decode_get_hw_frames_ctx(AVCodecContext
*avctx
,
952 enum AVHWDeviceType dev_type
)
954 AVHWDeviceContext
*device_ctx
;
955 AVHWFramesContext
*frames_ctx
;
959 return AVERROR(ENOSYS
);
961 if (avctx
->hw_frames_ctx
)
963 if (!avctx
->hw_device_ctx
) {
964 av_log(avctx
, AV_LOG_ERROR
, "A hardware frames or device context is "
965 "required for hardware accelerated decoding.\n");
966 return AVERROR(EINVAL
);
969 device_ctx
= (AVHWDeviceContext
*)avctx
->hw_device_ctx
->data
;
970 if (device_ctx
->type
!= dev_type
) {
971 av_log(avctx
, AV_LOG_ERROR
, "Device type %s expected for hardware "
972 "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type
),
973 av_hwdevice_get_type_name(device_ctx
->type
));
974 return AVERROR(EINVAL
);
977 ret
= avcodec_get_hw_frames_parameters(avctx
,
978 avctx
->hw_device_ctx
,
979 avctx
->hwaccel
->pix_fmt
,
980 &avctx
->hw_frames_ctx
);
984 frames_ctx
= (AVHWFramesContext
*)avctx
->hw_frames_ctx
->data
;
987 if (frames_ctx
->initial_pool_size
) {
988 // We guarantee 4 base work surfaces. The function above guarantees 1
989 // (the absolute minimum), so add the missing count.
990 frames_ctx
->initial_pool_size
+= 3;
993 ret
= av_hwframe_ctx_init(avctx
->hw_frames_ctx
);
995 av_buffer_unref(&avctx
->hw_frames_ctx
);
1002 int avcodec_get_hw_frames_parameters(AVCodecContext
*avctx
,
1003 AVBufferRef
*device_ref
,
1004 enum AVPixelFormat hw_pix_fmt
,
1005 AVBufferRef
**out_frames_ref
)
1007 AVBufferRef
*frames_ref
= NULL
;
1008 const AVCodecHWConfigInternal
*hw_config
;
1009 const AVHWAccel
*hwa
;
1013 hw_config
= ffcodec(avctx
->codec
)->hw_configs
[i
];
1015 return AVERROR(ENOENT
);
1016 if (hw_config
->public.pix_fmt
== hw_pix_fmt
)
1020 hwa
= hw_config
->hwaccel
;
1021 if (!hwa
|| !hwa
->frame_params
)
1022 return AVERROR(ENOENT
);
1024 frames_ref
= av_hwframe_ctx_alloc(device_ref
);
1026 return AVERROR(ENOMEM
);
1028 ret
= hwa
->frame_params(avctx
, frames_ref
);
1030 AVHWFramesContext
*frames_ctx
= (AVHWFramesContext
*)frames_ref
->data
;
1032 if (frames_ctx
->initial_pool_size
) {
1033 // If the user has requested that extra output surfaces be
1034 // available then add them here.
1035 if (avctx
->extra_hw_frames
> 0)
1036 frames_ctx
->initial_pool_size
+= avctx
->extra_hw_frames
;
1038 // If frame threading is enabled then an extra surface per thread
1039 // is also required.
1040 if (avctx
->active_thread_type
& FF_THREAD_FRAME
)
1041 frames_ctx
->initial_pool_size
+= avctx
->thread_count
;
1044 *out_frames_ref
= frames_ref
;
1046 av_buffer_unref(&frames_ref
);
1051 static int hwaccel_init(AVCodecContext
*avctx
,
1052 const AVCodecHWConfigInternal
*hw_config
)
1054 const AVHWAccel
*hwaccel
;
1057 hwaccel
= hw_config
->hwaccel
;
1058 if (hwaccel
->capabilities
& AV_HWACCEL_CODEC_CAP_EXPERIMENTAL
&&
1059 avctx
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
1060 av_log(avctx
, AV_LOG_WARNING
, "Ignoring experimental hwaccel: %s\n",
1062 return AVERROR_PATCHWELCOME
;
1065 if (hwaccel
->priv_data_size
) {
1066 avctx
->internal
->hwaccel_priv_data
=
1067 av_mallocz(hwaccel
->priv_data_size
);
1068 if (!avctx
->internal
->hwaccel_priv_data
)
1069 return AVERROR(ENOMEM
);
1072 avctx
->hwaccel
= hwaccel
;
1073 if (hwaccel
->init
) {
1074 err
= hwaccel
->init(avctx
);
1076 av_log(avctx
, AV_LOG_ERROR
, "Failed setup for format %s: "
1077 "hwaccel initialisation returned error.\n",
1078 av_get_pix_fmt_name(hw_config
->public.pix_fmt
));
1079 av_freep(&avctx
->internal
->hwaccel_priv_data
);
1080 avctx
->hwaccel
= NULL
;
1088 static void hwaccel_uninit(AVCodecContext
*avctx
)
1090 if (avctx
->hwaccel
&& avctx
->hwaccel
->uninit
)
1091 avctx
->hwaccel
->uninit(avctx
);
1093 av_freep(&avctx
->internal
->hwaccel_priv_data
);
1095 avctx
->hwaccel
= NULL
;
1097 av_buffer_unref(&avctx
->hw_frames_ctx
);
1100 int ff_get_format(AVCodecContext
*avctx
, const enum AVPixelFormat
*fmt
)
1102 const AVPixFmtDescriptor
*desc
;
1103 enum AVPixelFormat
*choices
;
1104 enum AVPixelFormat ret
, user_choice
;
1105 const AVCodecHWConfigInternal
*hw_config
;
1106 const AVCodecHWConfig
*config
;
1109 // Find end of list.
1110 for (n
= 0; fmt
[n
] != AV_PIX_FMT_NONE
; n
++);
1111 // Must contain at least one entry.
1113 // If a software format is available, it must be the last entry.
1114 desc
= av_pix_fmt_desc_get(fmt
[n
- 1]);
1115 if (desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
) {
1116 // No software format is available.
1118 avctx
->sw_pix_fmt
= fmt
[n
- 1];
1121 choices
= av_memdup(fmt
, (n
+ 1) * sizeof(*choices
));
1123 return AV_PIX_FMT_NONE
;
1126 // Remove the previous hwaccel, if there was one.
1127 hwaccel_uninit(avctx
);
1129 user_choice
= avctx
->get_format(avctx
, choices
);
1130 if (user_choice
== AV_PIX_FMT_NONE
) {
1131 // Explicitly chose nothing, give up.
1132 ret
= AV_PIX_FMT_NONE
;
1136 desc
= av_pix_fmt_desc_get(user_choice
);
1138 av_log(avctx
, AV_LOG_ERROR
, "Invalid format returned by "
1139 "get_format() callback.\n");
1140 ret
= AV_PIX_FMT_NONE
;
1143 av_log(avctx
, AV_LOG_DEBUG
, "Format %s chosen by get_format().\n",
1146 for (i
= 0; i
< n
; i
++) {
1147 if (choices
[i
] == user_choice
)
1151 av_log(avctx
, AV_LOG_ERROR
, "Invalid return from get_format(): "
1152 "%s not in possible list.\n", desc
->name
);
1153 ret
= AV_PIX_FMT_NONE
;
1157 if (ffcodec(avctx
->codec
)->hw_configs
) {
1159 hw_config
= ffcodec(avctx
->codec
)->hw_configs
[i
];
1162 if (hw_config
->public.pix_fmt
== user_choice
)
1170 // No config available, so no extra setup required.
1174 config
= &hw_config
->public;
1176 if (config
->methods
&
1177 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
&&
1178 avctx
->hw_frames_ctx
) {
1179 const AVHWFramesContext
*frames_ctx
=
1180 (AVHWFramesContext
*)avctx
->hw_frames_ctx
->data
;
1181 if (frames_ctx
->format
!= user_choice
) {
1182 av_log(avctx
, AV_LOG_ERROR
, "Invalid setup for format %s: "
1183 "does not match the format of the provided frames "
1184 "context.\n", desc
->name
);
1187 } else if (config
->methods
&
1188 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
&&
1189 avctx
->hw_device_ctx
) {
1190 const AVHWDeviceContext
*device_ctx
=
1191 (AVHWDeviceContext
*)avctx
->hw_device_ctx
->data
;
1192 if (device_ctx
->type
!= config
->device_type
) {
1193 av_log(avctx
, AV_LOG_ERROR
, "Invalid setup for format %s: "
1194 "does not match the type of the provided device "
1195 "context.\n", desc
->name
);
1198 } else if (config
->methods
&
1199 AV_CODEC_HW_CONFIG_METHOD_INTERNAL
) {
1200 // Internal-only setup, no additional configuration.
1201 } else if (config
->methods
&
1202 AV_CODEC_HW_CONFIG_METHOD_AD_HOC
) {
1203 // Some ad-hoc configuration we can't see and can't check.
1205 av_log(avctx
, AV_LOG_ERROR
, "Invalid setup for format %s: "
1206 "missing configuration.\n", desc
->name
);
1209 if (hw_config
->hwaccel
) {
1210 av_log(avctx
, AV_LOG_DEBUG
, "Format %s requires hwaccel "
1211 "initialisation.\n", desc
->name
);
1212 err
= hwaccel_init(avctx
, hw_config
);
1220 av_log(avctx
, AV_LOG_DEBUG
, "Format %s not usable, retrying "
1221 "get_format() without it.\n", desc
->name
);
1222 for (i
= 0; i
< n
; i
++) {
1223 if (choices
[i
] == user_choice
)
1226 for (; i
+ 1 < n
; i
++)
1227 choices
[i
] = choices
[i
+ 1];
1235 static int add_metadata_from_side_data(const AVPacket
*avpkt
, AVFrame
*frame
)
1238 const uint8_t *side_metadata
;
1240 AVDictionary
**frame_md
= &frame
->metadata
;
1242 side_metadata
= av_packet_get_side_data(avpkt
,
1243 AV_PKT_DATA_STRINGS_METADATA
, &size
);
1244 return av_packet_unpack_dictionary(side_metadata
, size
, frame_md
);
1247 int ff_decode_frame_props(AVCodecContext
*avctx
, AVFrame
*frame
)
1249 AVPacket
*pkt
= avctx
->internal
->last_pkt_props
;
1250 static const struct {
1251 enum AVPacketSideDataType packet
;
1252 enum AVFrameSideDataType frame
;
1254 { AV_PKT_DATA_REPLAYGAIN
, AV_FRAME_DATA_REPLAYGAIN
},
1255 { AV_PKT_DATA_DISPLAYMATRIX
, AV_FRAME_DATA_DISPLAYMATRIX
},
1256 { AV_PKT_DATA_SPHERICAL
, AV_FRAME_DATA_SPHERICAL
},
1257 { AV_PKT_DATA_STEREO3D
, AV_FRAME_DATA_STEREO3D
},
1258 { AV_PKT_DATA_AUDIO_SERVICE_TYPE
, AV_FRAME_DATA_AUDIO_SERVICE_TYPE
},
1259 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA
, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
},
1260 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL
, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
},
1261 { AV_PKT_DATA_A53_CC
, AV_FRAME_DATA_A53_CC
},
1262 { AV_PKT_DATA_ICC_PROFILE
, AV_FRAME_DATA_ICC_PROFILE
},
1263 { AV_PKT_DATA_S12M_TIMECODE
, AV_FRAME_DATA_S12M_TIMECODE
},
1264 { AV_PKT_DATA_DYNAMIC_HDR10_PLUS
, AV_FRAME_DATA_DYNAMIC_HDR_PLUS
},
1267 if (!(ffcodec(avctx
->codec
)->caps_internal
& FF_CODEC_CAP_SETS_FRAME_PROPS
)) {
1268 frame
->pts
= pkt
->pts
;
1269 frame
->pkt_pos
= pkt
->pos
;
1270 frame
->pkt_duration
= pkt
->duration
;
1271 frame
->pkt_size
= pkt
->size
;
1273 for (int i
= 0; i
< FF_ARRAY_ELEMS(sd
); i
++) {
1275 uint8_t *packet_sd
= av_packet_get_side_data(pkt
, sd
[i
].packet
, &size
);
1277 AVFrameSideData
*frame_sd
= av_frame_new_side_data(frame
,
1281 return AVERROR(ENOMEM
);
1283 memcpy(frame_sd
->data
, packet_sd
, size
);
1286 add_metadata_from_side_data(pkt
, frame
);
1288 if (pkt
->flags
& AV_PKT_FLAG_DISCARD
) {
1289 frame
->flags
|= AV_FRAME_FLAG_DISCARD
;
1291 frame
->flags
= (frame
->flags
& ~AV_FRAME_FLAG_DISCARD
);
1294 frame
->reordered_opaque
= avctx
->reordered_opaque
;
1296 if (frame
->color_primaries
== AVCOL_PRI_UNSPECIFIED
)
1297 frame
->color_primaries
= avctx
->color_primaries
;
1298 if (frame
->color_trc
== AVCOL_TRC_UNSPECIFIED
)
1299 frame
->color_trc
= avctx
->color_trc
;
1300 if (frame
->colorspace
== AVCOL_SPC_UNSPECIFIED
)
1301 frame
->colorspace
= avctx
->colorspace
;
1302 if (frame
->color_range
== AVCOL_RANGE_UNSPECIFIED
)
1303 frame
->color_range
= avctx
->color_range
;
1304 if (frame
->chroma_location
== AVCHROMA_LOC_UNSPECIFIED
)
1305 frame
->chroma_location
= avctx
->chroma_sample_location
;
1307 switch (avctx
->codec
->type
) {
1308 case AVMEDIA_TYPE_VIDEO
:
1309 frame
->format
= avctx
->pix_fmt
;
1310 if (!frame
->sample_aspect_ratio
.num
)
1311 frame
->sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
1313 if (frame
->width
&& frame
->height
&&
1314 av_image_check_sar(frame
->width
, frame
->height
,
1315 frame
->sample_aspect_ratio
) < 0) {
1316 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid SAR: %u/%u\n",
1317 frame
->sample_aspect_ratio
.num
,
1318 frame
->sample_aspect_ratio
.den
);
1319 frame
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
1323 case AVMEDIA_TYPE_AUDIO
:
1324 if (!frame
->sample_rate
)
1325 frame
->sample_rate
= avctx
->sample_rate
;
1326 if (frame
->format
< 0)
1327 frame
->format
= avctx
->sample_fmt
;
1328 if (!frame
->ch_layout
.nb_channels
) {
1329 int ret
= av_channel_layout_copy(&frame
->ch_layout
, &avctx
->ch_layout
);
1333 #if FF_API_OLD_CHANNEL_LAYOUT
1334 FF_DISABLE_DEPRECATION_WARNINGS
1335 frame
->channels
= frame
->ch_layout
.nb_channels
;
1336 frame
->channel_layout
= frame
->ch_layout
.order
== AV_CHANNEL_ORDER_NATIVE
?
1337 frame
->ch_layout
.u
.mask
: 0;
1338 FF_ENABLE_DEPRECATION_WARNINGS
1345 static void validate_avframe_allocation(AVCodecContext
*avctx
, AVFrame
*frame
)
1347 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1349 int num_planes
= av_pix_fmt_count_planes(frame
->format
);
1350 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(frame
->format
);
1351 int flags
= desc
? desc
->flags
: 0;
1352 if (num_planes
== 1 && (flags
& AV_PIX_FMT_FLAG_PAL
))
1354 for (i
= 0; i
< num_planes
; i
++) {
1355 av_assert0(frame
->data
[i
]);
1357 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1358 for (i
= num_planes
; num_planes
> 0 && i
< FF_ARRAY_ELEMS(frame
->data
); i
++) {
1360 av_log(avctx
, AV_LOG_ERROR
, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1361 frame
->data
[i
] = NULL
;
1366 static void decode_data_free(void *opaque
, uint8_t *data
)
1368 FrameDecodeData
*fdd
= (FrameDecodeData
*)data
;
1370 if (fdd
->post_process_opaque_free
)
1371 fdd
->post_process_opaque_free(fdd
->post_process_opaque
);
1373 if (fdd
->hwaccel_priv_free
)
1374 fdd
->hwaccel_priv_free(fdd
->hwaccel_priv
);
1379 int ff_attach_decode_data(AVFrame
*frame
)
1381 AVBufferRef
*fdd_buf
;
1382 FrameDecodeData
*fdd
;
1384 av_assert1(!frame
->private_ref
);
1385 av_buffer_unref(&frame
->private_ref
);
1387 fdd
= av_mallocz(sizeof(*fdd
));
1389 return AVERROR(ENOMEM
);
1391 fdd_buf
= av_buffer_create((uint8_t*)fdd
, sizeof(*fdd
), decode_data_free
,
1392 NULL
, AV_BUFFER_FLAG_READONLY
);
1395 return AVERROR(ENOMEM
);
1398 frame
->private_ref
= fdd_buf
;
1403 int ff_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
1405 const AVHWAccel
*hwaccel
= avctx
->hwaccel
;
1406 int override_dimensions
= 1;
1409 av_assert0(av_codec_is_decoder(avctx
->codec
));
1411 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1412 if ((unsigned)avctx
->width
> INT_MAX
- STRIDE_ALIGN
||
1413 (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) {
1414 av_log(avctx
, AV_LOG_ERROR
, "video_get_buffer: image parameters invalid\n");
1415 ret
= AVERROR(EINVAL
);
1419 if (frame
->width
<= 0 || frame
->height
<= 0) {
1420 frame
->width
= FFMAX(avctx
->width
, AV_CEIL_RSHIFT(avctx
->coded_width
, avctx
->lowres
));
1421 frame
->height
= FFMAX(avctx
->height
, AV_CEIL_RSHIFT(avctx
->coded_height
, avctx
->lowres
));
1422 override_dimensions
= 0;
1425 if (frame
->data
[0] || frame
->data
[1] || frame
->data
[2] || frame
->data
[3]) {
1426 av_log(avctx
, AV_LOG_ERROR
, "pic->data[*]!=NULL in get_buffer_internal\n");
1427 ret
= AVERROR(EINVAL
);
1430 } else if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
1431 #if FF_API_OLD_CHANNEL_LAYOUT
1432 FF_DISABLE_DEPRECATION_WARNINGS
1433 /* compat layer for old-style get_buffer() implementations */
1434 avctx
->channels
= avctx
->ch_layout
.nb_channels
;
1435 avctx
->channel_layout
= (avctx
->ch_layout
.order
== AV_CHANNEL_ORDER_NATIVE
) ?
1436 avctx
->ch_layout
.u
.mask
: 0;
1437 FF_ENABLE_DEPRECATION_WARNINGS
1440 if (frame
->nb_samples
* (int64_t)avctx
->ch_layout
.nb_channels
> avctx
->max_samples
) {
1441 av_log(avctx
, AV_LOG_ERROR
, "samples per frame %d, exceeds max_samples %"PRId64
"\n", frame
->nb_samples
, avctx
->max_samples
);
1442 ret
= AVERROR(EINVAL
);
1446 ret
= ff_decode_frame_props(avctx
, frame
);
1451 if (hwaccel
->alloc_frame
) {
1452 ret
= hwaccel
->alloc_frame(avctx
, frame
);
1456 avctx
->sw_pix_fmt
= avctx
->pix_fmt
;
1458 ret
= avctx
->get_buffer2(avctx
, frame
, flags
);
1462 validate_avframe_allocation(avctx
, frame
);
1464 ret
= ff_attach_decode_data(frame
);
1469 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
&& !override_dimensions
&&
1470 !(ffcodec(avctx
->codec
)->caps_internal
& FF_CODEC_CAP_EXPORTS_CROPPING
)) {
1471 frame
->width
= avctx
->width
;
1472 frame
->height
= avctx
->height
;
1477 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
1478 av_frame_unref(frame
);
1484 static int reget_buffer_internal(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
1489 av_assert0(avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
);
1491 if (frame
->data
[0] && (frame
->width
!= avctx
->width
|| frame
->height
!= avctx
->height
|| frame
->format
!= avctx
->pix_fmt
)) {
1492 av_log(avctx
, AV_LOG_WARNING
, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1493 frame
->width
, frame
->height
, av_get_pix_fmt_name(frame
->format
), avctx
->width
, avctx
->height
, av_get_pix_fmt_name(avctx
->pix_fmt
));
1494 av_frame_unref(frame
);
1497 if (!frame
->data
[0])
1498 return ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
);
1500 if ((flags
& FF_REGET_BUFFER_FLAG_READONLY
) || av_frame_is_writable(frame
))
1501 return ff_decode_frame_props(avctx
, frame
);
1503 tmp
= av_frame_alloc();
1505 return AVERROR(ENOMEM
);
1507 av_frame_move_ref(tmp
, frame
);
1509 ret
= ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
);
1511 av_frame_free(&tmp
);
1515 av_frame_copy(frame
, tmp
);
1516 av_frame_free(&tmp
);
1521 int ff_reget_buffer(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
1523 int ret
= reget_buffer_internal(avctx
, frame
, flags
);
1525 av_log(avctx
, AV_LOG_ERROR
, "reget_buffer() failed\n");
1529 int ff_decode_preinit(AVCodecContext
*avctx
)
1531 AVCodecInternal
*avci
= avctx
->internal
;
1534 /* if the decoder init function was already called previously,
1535 * free the already allocated subtitle_header before overwriting it */
1536 av_freep(&avctx
->subtitle_header
);
1538 #if FF_API_THREAD_SAFE_CALLBACKS
1539 FF_DISABLE_DEPRECATION_WARNINGS
1540 if ((avctx
->thread_type
& FF_THREAD_FRAME
) &&
1541 avctx
->get_buffer2
!= avcodec_default_get_buffer2
&&
1542 !avctx
->thread_safe_callbacks
) {
1543 av_log(avctx
, AV_LOG_WARNING
, "Requested frame threading with a "
1544 "custom get_buffer2() implementation which is not marked as "
1545 "thread safe. This is not supported anymore, make your "
1546 "callback thread-safe.\n");
1548 FF_ENABLE_DEPRECATION_WARNINGS
1551 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&& avctx
->ch_layout
.nb_channels
== 0 &&
1552 !(avctx
->codec
->capabilities
& AV_CODEC_CAP_CHANNEL_CONF
)) {
1553 av_log(avctx
, AV_LOG_ERROR
, "Decoder requires channel count but channels not set\n");
1554 return AVERROR(EINVAL
);
1556 if (avctx
->codec
->max_lowres
< avctx
->lowres
|| avctx
->lowres
< 0) {
1557 av_log(avctx
, AV_LOG_WARNING
, "The maximum value for lowres supported by the decoder is %d\n",
1558 avctx
->codec
->max_lowres
);
1559 avctx
->lowres
= avctx
->codec
->max_lowres
;
1561 if (avctx
->sub_charenc
) {
1562 if (avctx
->codec_type
!= AVMEDIA_TYPE_SUBTITLE
) {
1563 av_log(avctx
, AV_LOG_ERROR
, "Character encoding is only "
1564 "supported with subtitles codecs\n");
1565 return AVERROR(EINVAL
);
1566 } else if (avctx
->codec_descriptor
->props
& AV_CODEC_PROP_BITMAP_SUB
) {
1567 av_log(avctx
, AV_LOG_WARNING
, "Codec '%s' is bitmap-based, "
1568 "subtitles character encoding will be ignored\n",
1569 avctx
->codec_descriptor
->name
);
1570 avctx
->sub_charenc_mode
= FF_SUB_CHARENC_MODE_DO_NOTHING
;
1572 /* input character encoding is set for a text based subtitle
1573 * codec at this point */
1574 if (avctx
->sub_charenc_mode
== FF_SUB_CHARENC_MODE_AUTOMATIC
)
1575 avctx
->sub_charenc_mode
= FF_SUB_CHARENC_MODE_PRE_DECODER
;
1577 if (avctx
->sub_charenc_mode
== FF_SUB_CHARENC_MODE_PRE_DECODER
) {
1579 iconv_t cd
= iconv_open("UTF-8", avctx
->sub_charenc
);
1580 if (cd
== (iconv_t
)-1) {
1581 ret
= AVERROR(errno
);
1582 av_log(avctx
, AV_LOG_ERROR
, "Unable to open iconv context "
1583 "with input character encoding \"%s\"\n", avctx
->sub_charenc
);
1588 av_log(avctx
, AV_LOG_ERROR
, "Character encoding subtitles "
1589 "conversion needs a libavcodec built with iconv support "
1590 "for this codec\n");
1591 return AVERROR(ENOSYS
);
1597 avctx
->pts_correction_num_faulty_pts
=
1598 avctx
->pts_correction_num_faulty_dts
= 0;
1599 avctx
->pts_correction_last_pts
=
1600 avctx
->pts_correction_last_dts
= INT64_MIN
;
1602 if ( !CONFIG_GRAY
&& avctx
->flags
& AV_CODEC_FLAG_GRAY
1603 && avctx
->codec_descriptor
->type
== AVMEDIA_TYPE_VIDEO
)
1604 av_log(avctx
, AV_LOG_WARNING
,
1605 "gray decoding requested but not enabled at configuration time\n");
1606 if (avctx
->flags2
& AV_CODEC_FLAG2_EXPORT_MVS
) {
1607 avctx
->export_side_data
|= AV_CODEC_EXPORT_DATA_MVS
;
1610 avci
->in_pkt
= av_packet_alloc();
1611 avci
->last_pkt_props
= av_packet_alloc();
1612 avci
->pkt_props
= av_fifo_alloc2(1, sizeof(*avci
->last_pkt_props
),
1613 AV_FIFO_FLAG_AUTO_GROW
);
1614 if (!avci
->in_pkt
|| !avci
->last_pkt_props
|| !avci
->pkt_props
)
1615 return AVERROR(ENOMEM
);
1617 ret
= decode_bsfs_init(avctx
);
1624 int ff_copy_palette(void *dst
, const AVPacket
*src
, void *logctx
)
1627 const void *pal
= av_packet_get_side_data(src
, AV_PKT_DATA_PALETTE
, &size
);
1629 if (pal
&& size
== AVPALETTE_SIZE
) {
1630 memcpy(dst
, pal
, AVPALETTE_SIZE
);
1633 av_log(logctx
, AV_LOG_ERROR
,
1634 "Palette size %"SIZE_SPECIFIER
" is wrong\n", size
);