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/common.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/hwcontext.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/internal.h"
40 #include "bytestream.h"
45 static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
52 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
56 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
57 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
58 "changes, but PARAM_CHANGE side data was sent to it.\n");
59 ret = AVERROR(EINVAL);
66 flags = bytestream_get_le32(&data);
69 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
72 val = bytestream_get_le32(&data);
73 if (val <= 0 || val > INT_MAX) {
74 av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
75 ret = AVERROR_INVALIDDATA;
78 avctx->channels = val;
81 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
84 avctx->channel_layout = bytestream_get_le64(&data);
87 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
90 val = bytestream_get_le32(&data);
91 if (val <= 0 || val > INT_MAX) {
92 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
93 ret = AVERROR_INVALIDDATA;
96 avctx->sample_rate = val;
99 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
102 avctx->width = bytestream_get_le32(&data);
103 avctx->height = bytestream_get_le32(&data);
105 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
112 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
113 ret = AVERROR_INVALIDDATA;
116 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
117 if (avctx->err_recognition & AV_EF_EXPLODE)
123 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
127 av_packet_unref(avci->last_pkt_props);
129 ret = av_packet_copy_props(avci->last_pkt_props, pkt);
131 avci->last_pkt_props->size = pkt->size; // HACK: Needed for ff_init_buffer_info().
136 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
140 /* move the original frame to our backup */
141 av_frame_unref(avci->to_free);
142 av_frame_move_ref(avci->to_free, frame);
144 /* now copy everything except the AVBufferRefs back
145 * note that we make a COPY of the side data, so calling av_frame_free() on
146 * the caller's frame will work properly */
147 ret = av_frame_copy_props(frame, avci->to_free);
151 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
152 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
153 if (avci->to_free->extended_data != avci->to_free->data) {
154 int planes = avci->to_free->channels;
155 int size = planes * sizeof(*frame->extended_data);
158 av_frame_unref(frame);
162 frame->extended_data = av_malloc(size);
163 if (!frame->extended_data) {
164 av_frame_unref(frame);
165 return AVERROR(ENOMEM);
167 memcpy(frame->extended_data, avci->to_free->extended_data,
170 frame->extended_data = frame->data;
172 frame->format = avci->to_free->format;
173 frame->width = avci->to_free->width;
174 frame->height = avci->to_free->height;
175 frame->channel_layout = avci->to_free->channel_layout;
176 frame->nb_samples = avci->to_free->nb_samples;
177 frame->channels = avci->to_free->channels;
182 static int bsfs_init(AVCodecContext *avctx)
184 AVCodecInternal *avci = avctx->internal;
185 DecodeFilterContext *s = &avci->filter;
186 const char *bsfs_str;
192 bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
193 while (bsfs_str && *bsfs_str) {
195 const AVBitStreamFilter *filter;
198 bsf = av_get_token(&bsfs_str, ",");
200 ret = AVERROR(ENOMEM);
204 filter = av_bsf_get_by_name(bsf);
206 av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
207 "requested by a decoder. This is a bug, please report it.\n",
215 tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
217 ret = AVERROR(ENOMEM);
223 ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
227 if (s->nb_bsfs == 1) {
228 /* We do not currently have an API for passing the input timebase into decoders,
229 * but no filters used here should actually need it.
230 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
231 s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
232 ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
235 s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
236 ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
237 s->bsfs[s->nb_bsfs - 2]->par_out);
242 ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
249 ff_decode_bsfs_uninit(avctx);
253 /* try to get one output packet from the filter chain */
254 static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
256 DecodeFilterContext *s = &avctx->internal->filter;
259 /* start with the last filter in the chain */
260 idx = s->nb_bsfs - 1;
262 /* request a packet from the currently selected filter */
263 ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
264 if (ret == AVERROR(EAGAIN)) {
265 /* no packets available, try the next filter up the chain */
269 } else if (ret < 0 && ret != AVERROR_EOF) {
273 /* got a packet or EOF -- pass it to the caller or to the next filter
275 if (idx == s->nb_bsfs - 1) {
279 ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
281 av_log(avctx, AV_LOG_ERROR,
282 "Error pre-processing a packet before decoding\n");
283 av_packet_unref(pkt);
289 return AVERROR(EAGAIN);
292 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
294 AVCodecInternal *avci = avctx->internal;
300 ret = bsfs_poll(avctx, pkt);
301 if (ret == AVERROR_EOF)
306 ret = extract_packet_props(avctx->internal, pkt);
310 ret = apply_param_change(avctx, pkt);
314 if (avctx->codec->receive_frame)
315 avci->compat_decode_consumed += pkt->size;
319 av_packet_unref(pkt);
324 * Attempt to guess proper monotonic timestamps for decoded video frames
325 * which might have incorrect times. Input timestamps may wrap around, in
326 * which case the output will as well.
328 * @param pts the pts field of the decoded AVPacket, as passed through
330 * @param dts the dts field of the decoded AVPacket
331 * @return one of the input values, may be AV_NOPTS_VALUE
333 static int64_t guess_correct_pts(AVCodecContext *ctx,
334 int64_t reordered_pts, int64_t dts)
336 int64_t pts = AV_NOPTS_VALUE;
338 if (dts != AV_NOPTS_VALUE) {
339 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
340 ctx->pts_correction_last_dts = dts;
341 } else if (reordered_pts != AV_NOPTS_VALUE)
342 ctx->pts_correction_last_dts = reordered_pts;
344 if (reordered_pts != AV_NOPTS_VALUE) {
345 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
346 ctx->pts_correction_last_pts = reordered_pts;
347 } else if(dts != AV_NOPTS_VALUE)
348 ctx->pts_correction_last_pts = dts;
350 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
351 && reordered_pts != AV_NOPTS_VALUE)
360 * The core of the receive_frame_wrapper for the decoders implementing
361 * the simple API. Certain decoders might consume partial packets without
362 * returning any output, so this function needs to be called in a loop until it
365 static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
367 AVCodecInternal *avci = avctx->internal;
368 DecodeSimpleContext *ds = &avci->ds;
369 AVPacket *pkt = ds->in_pkt;
370 // copy to ensure we do not change pkt
372 int got_frame, actual_got_frame, did_split;
375 if (!pkt->data && !avci->draining) {
376 av_packet_unref(pkt);
377 ret = ff_decode_get_packet(avctx, pkt);
378 if (ret < 0 && ret != AVERROR_EOF)
382 // Some codecs (at least wma lossless) will crash when feeding drain packets
383 // after EOF was signaled.
384 if (avci->draining_done)
388 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
389 avctx->active_thread_type & FF_THREAD_FRAME))
394 FF_DISABLE_DEPRECATION_WARNINGS
395 did_split = av_packet_split_side_data(&tmp);
398 ret = extract_packet_props(avctx->internal, &tmp);
402 ret = apply_param_change(avctx, &tmp);
406 FF_ENABLE_DEPRECATION_WARNINGS
411 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
412 ret = ff_thread_decode_frame(avctx, frame, &got_frame, &tmp);
414 ret = avctx->codec->decode(avctx, frame, &got_frame, &tmp);
416 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
417 frame->pkt_dts = pkt->dts;
418 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
419 if(!avctx->has_b_frames)
420 frame->pkt_pos = pkt->pos;
421 //FIXME these should be under if(!avctx->has_b_frames)
422 /* get_buffer is supposed to set frame parameters */
423 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
424 if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
425 if (!frame->width) frame->width = avctx->width;
426 if (!frame->height) frame->height = avctx->height;
427 if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
432 actual_got_frame = got_frame;
434 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
435 if (frame->flags & AV_FRAME_FLAG_DISCARD)
438 frame->best_effort_timestamp = guess_correct_pts(avctx,
441 } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
444 uint32_t discard_padding = 0;
445 uint8_t skip_reason = 0;
446 uint8_t discard_reason = 0;
448 if (ret >= 0 && got_frame) {
449 frame->best_effort_timestamp = guess_correct_pts(avctx,
452 if (frame->format == AV_SAMPLE_FMT_NONE)
453 frame->format = avctx->sample_fmt;
454 if (!frame->channel_layout)
455 frame->channel_layout = avctx->channel_layout;
456 if (!frame->channels)
457 frame->channels = avctx->channels;
458 if (!frame->sample_rate)
459 frame->sample_rate = avctx->sample_rate;
462 side= av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
463 if(side && side_size>=10) {
464 avctx->internal->skip_samples = AV_RL32(side) * avctx->internal->skip_samples_multiplier;
465 discard_padding = AV_RL32(side + 4);
466 av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
467 avctx->internal->skip_samples, (int)discard_padding);
468 skip_reason = AV_RL8(side + 8);
469 discard_reason = AV_RL8(side + 9);
472 if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame &&
473 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
474 avctx->internal->skip_samples = FFMAX(0, avctx->internal->skip_samples - frame->nb_samples);
478 if (avctx->internal->skip_samples > 0 && got_frame &&
479 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
480 if(frame->nb_samples <= avctx->internal->skip_samples){
482 avctx->internal->skip_samples -= frame->nb_samples;
483 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
484 avctx->internal->skip_samples);
486 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
487 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
488 if(avctx->pkt_timebase.num && avctx->sample_rate) {
489 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
490 (AVRational){1, avctx->sample_rate},
491 avctx->pkt_timebase);
492 if(frame->pts!=AV_NOPTS_VALUE)
493 frame->pts += diff_ts;
495 FF_DISABLE_DEPRECATION_WARNINGS
496 if(frame->pkt_pts!=AV_NOPTS_VALUE)
497 frame->pkt_pts += diff_ts;
498 FF_ENABLE_DEPRECATION_WARNINGS
500 if(frame->pkt_dts!=AV_NOPTS_VALUE)
501 frame->pkt_dts += diff_ts;
502 if (frame->pkt_duration >= diff_ts)
503 frame->pkt_duration -= diff_ts;
505 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
507 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
508 avctx->internal->skip_samples, frame->nb_samples);
509 frame->nb_samples -= avctx->internal->skip_samples;
510 avctx->internal->skip_samples = 0;
514 if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame &&
515 !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
516 if (discard_padding == frame->nb_samples) {
519 if(avctx->pkt_timebase.num && avctx->sample_rate) {
520 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
521 (AVRational){1, avctx->sample_rate},
522 avctx->pkt_timebase);
523 frame->pkt_duration = diff_ts;
525 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
527 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
528 (int)discard_padding, frame->nb_samples);
529 frame->nb_samples -= discard_padding;
533 if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) {
534 AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
536 AV_WL32(fside->data, avctx->internal->skip_samples);
537 AV_WL32(fside->data + 4, discard_padding);
538 AV_WL8(fside->data + 8, skip_reason);
539 AV_WL8(fside->data + 9, discard_reason);
540 avctx->internal->skip_samples = 0;
546 av_packet_free_side_data(&tmp);
552 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO &&
553 !avci->showed_multi_packet_warning &&
554 ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
555 av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
556 avci->showed_multi_packet_warning = 1;
560 av_frame_unref(frame);
562 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED))
565 #if FF_API_AVCTX_TIMEBASE
566 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
567 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
570 /* do not stop draining when actual_got_frame != 0 or ret < 0 */
571 /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
572 if (avctx->internal->draining && !actual_got_frame) {
574 /* prevent infinite loop if a decoder wrongly always return error on draining */
575 /* reasonable nb_errors_max = maximum b frames + thread count */
576 int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ?
577 avctx->thread_count : 1);
579 if (avci->nb_draining_errors++ >= nb_errors_max) {
580 av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
581 "Stop draining and force EOF.\n");
582 avci->draining_done = 1;
586 avci->draining_done = 1;
590 avci->compat_decode_consumed += ret;
592 if (ret >= pkt->size || ret < 0) {
593 av_packet_unref(pkt);
597 pkt->data += consumed;
598 pkt->size -= consumed;
599 avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
600 pkt->pts = AV_NOPTS_VALUE;
601 pkt->dts = AV_NOPTS_VALUE;
602 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
603 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
607 av_assert0(frame->buf[0]);
609 return ret < 0 ? ret : 0;
612 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
616 while (!frame->buf[0]) {
617 ret = decode_simple_internal(avctx, frame);
625 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
627 AVCodecInternal *avci = avctx->internal;
630 av_assert0(!frame->buf[0]);
632 if (avctx->codec->receive_frame)
633 ret = avctx->codec->receive_frame(avctx, frame);
635 ret = decode_simple_receive_frame(avctx, frame);
637 if (ret == AVERROR_EOF)
638 avci->draining_done = 1;
643 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
645 AVCodecInternal *avci = avctx->internal;
648 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
649 return AVERROR(EINVAL);
651 if (avctx->internal->draining)
654 if (avpkt && !avpkt->size && avpkt->data)
655 return AVERROR(EINVAL);
657 ret = bsfs_init(avctx);
661 av_packet_unref(avci->buffer_pkt);
662 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
663 ret = av_packet_ref(avci->buffer_pkt, avpkt);
668 ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
670 av_packet_unref(avci->buffer_pkt);
674 if (!avci->buffer_frame->buf[0]) {
675 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
676 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
683 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
685 AVCodecInternal *avci = avctx->internal;
688 av_frame_unref(frame);
690 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
691 return AVERROR(EINVAL);
693 ret = bsfs_init(avctx);
697 if (avci->buffer_frame->buf[0]) {
698 av_frame_move_ref(frame, avci->buffer_frame);
700 ret = decode_receive_frame_internal(avctx, frame);
705 avctx->frame_number++;
710 static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
711 int *got_frame, const AVPacket *pkt)
713 AVCodecInternal *avci = avctx->internal;
716 av_assert0(avci->compat_decode_consumed == 0);
719 avci->compat_decode = 1;
721 if (avci->compat_decode_partial_size > 0 &&
722 avci->compat_decode_partial_size != pkt->size) {
723 av_log(avctx, AV_LOG_ERROR,
724 "Got unexpected packet size after a partial decode\n");
725 ret = AVERROR(EINVAL);
729 if (!avci->compat_decode_partial_size) {
730 ret = avcodec_send_packet(avctx, pkt);
731 if (ret == AVERROR_EOF)
733 else if (ret == AVERROR(EAGAIN)) {
734 /* we fully drain all the output in each decode call, so this should not
743 ret = avcodec_receive_frame(avctx, frame);
745 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
750 if (frame != avci->compat_decode_frame) {
751 if (!avctx->refcounted_frames) {
752 ret = unrefcount_frame(avci, frame);
758 frame = avci->compat_decode_frame;
760 if (!avci->compat_decode_warned) {
761 av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
762 "API cannot return all the frames for this decoder. "
763 "Some frames will be dropped. Update your code to the "
764 "new decoding API to fix this.\n");
765 avci->compat_decode_warned = 1;
769 if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
775 /* if there are any bsfs then assume full packet is always consumed */
776 if (avctx->codec->bsfs)
779 ret = FFMIN(avci->compat_decode_consumed, pkt->size);
781 avci->compat_decode_consumed = 0;
782 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
787 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
788 int *got_picture_ptr,
789 const AVPacket *avpkt)
791 return compat_decode(avctx, picture, got_picture_ptr, avpkt);
794 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
797 const AVPacket *avpkt)
799 return compat_decode(avctx, frame, got_frame_ptr, avpkt);
802 static void get_subtitle_defaults(AVSubtitle *sub)
804 memset(sub, 0, sizeof(*sub));
805 sub->pts = AV_NOPTS_VALUE;
808 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
809 static int recode_subtitle(AVCodecContext *avctx,
810 AVPacket *outpkt, const AVPacket *inpkt)
813 iconv_t cd = (iconv_t)-1;
820 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
824 cd = iconv_open("UTF-8", avctx->sub_charenc);
825 av_assert0(cd != (iconv_t)-1);
830 if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
831 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
832 ret = AVERROR(ENOMEM);
836 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
839 outpkt->buf = tmp.buf;
840 outpkt->data = tmp.data;
841 outpkt->size = tmp.size;
845 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
846 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
847 outl >= outpkt->size || inl != 0) {
848 ret = FFMIN(AVERROR(errno), -1);
849 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
850 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
851 av_packet_unref(&tmp);
854 outpkt->size -= outl;
855 memset(outpkt->data + outpkt->size, 0, outl);
858 if (cd != (iconv_t)-1)
862 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
863 return AVERROR(EINVAL);
867 static int utf8_check(const uint8_t *str)
870 uint32_t codepoint, min;
874 GET_UTF8(codepoint, *(byte++), return 0;);
875 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
876 1 << (5 * (byte - str) - 4);
877 if (codepoint < min || codepoint >= 0x110000 ||
878 codepoint == 0xFFFE /* BOM */ ||
879 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
886 #if FF_API_ASS_TIMING
887 static void insert_ts(AVBPrint *buf, int ts)
890 av_bprintf(buf, "9:59:59.99,");
894 h = ts/360000; ts -= 360000*h;
895 m = ts/ 6000; ts -= 6000*m;
896 s = ts/ 100; ts -= 100*s;
897 av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
901 static int convert_sub_to_old_ass_form(AVSubtitle *sub, const AVPacket *pkt, AVRational tb)
906 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
908 for (i = 0; i < sub->num_rects; i++) {
911 AVSubtitleRect *rect = sub->rects[i];
912 int ts_start, ts_duration = -1;
915 if (rect->type != SUBTITLE_ASS || !strncmp(rect->ass, "Dialogue: ", 10))
918 av_bprint_clear(&buf);
921 dialog = strchr(rect->ass, ',');
926 /* extract Layer or Marked */
927 layer = strtol(dialog, (char**)&dialog, 10);
932 /* rescale timing to ASS time base (ms) */
933 ts_start = av_rescale_q(pkt->pts, tb, av_make_q(1, 100));
934 if (pkt->duration != -1)
935 ts_duration = av_rescale_q(pkt->duration, tb, av_make_q(1, 100));
936 sub->end_display_time = FFMAX(sub->end_display_time, 10 * ts_duration);
938 /* construct ASS (standalone file form with timestamps) string */
939 av_bprintf(&buf, "Dialogue: %ld,", layer);
940 insert_ts(&buf, ts_start);
941 insert_ts(&buf, ts_duration == -1 ? -1 : ts_start + ts_duration);
942 av_bprintf(&buf, "%s\r\n", dialog);
944 final_dialog = av_strdup(buf.str);
945 if (!av_bprint_is_complete(&buf) || !final_dialog) {
946 av_freep(&final_dialog);
947 av_bprint_finalize(&buf, NULL);
948 return AVERROR(ENOMEM);
950 av_freep(&rect->ass);
951 rect->ass = final_dialog;
954 av_bprint_finalize(&buf, NULL);
959 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
965 if (!avpkt->data && avpkt->size) {
966 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
967 return AVERROR(EINVAL);
970 return AVERROR(EINVAL);
971 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
972 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
973 return AVERROR(EINVAL);
977 get_subtitle_defaults(sub);
979 if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
980 AVPacket pkt_recoded;
981 AVPacket tmp = *avpkt;
983 FF_DISABLE_DEPRECATION_WARNINGS
984 int did_split = av_packet_split_side_data(&tmp);
985 //apply_param_change(avctx, &tmp);
988 /* FFMIN() prevents overflow in case the packet wasn't allocated with
990 * If the side data is smaller than the buffer padding size, the
991 * remaining bytes should have already been filled with zeros by the
992 * original packet allocation anyway. */
993 memset(tmp.data + tmp.size, 0,
994 FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
996 FF_ENABLE_DEPRECATION_WARNINGS
1000 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
1004 ret = extract_packet_props(avctx->internal, &pkt_recoded);
1008 if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
1009 sub->pts = av_rescale_q(avpkt->pts,
1010 avctx->pkt_timebase, AV_TIME_BASE_Q);
1011 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
1012 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
1013 !!*got_sub_ptr >= !!sub->num_rects);
1015 #if FF_API_ASS_TIMING
1016 if (avctx->sub_text_format == FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS
1017 && *got_sub_ptr && sub->num_rects) {
1018 const AVRational tb = avctx->pkt_timebase.num ? avctx->pkt_timebase
1020 int err = convert_sub_to_old_ass_form(sub, avpkt, tb);
1026 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
1027 avctx->pkt_timebase.num) {
1028 AVRational ms = { 1, 1000 };
1029 sub->end_display_time = av_rescale_q(avpkt->duration,
1030 avctx->pkt_timebase, ms);
1033 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
1035 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
1038 for (i = 0; i < sub->num_rects; i++) {
1039 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
1040 av_log(avctx, AV_LOG_ERROR,
1041 "Invalid UTF-8 in decoded subtitles text; "
1042 "maybe missing -sub_charenc option\n");
1043 avsubtitle_free(sub);
1044 ret = AVERROR_INVALIDDATA;
1049 if (tmp.data != pkt_recoded.data) { // did we recode?
1050 /* prevent from destroying side data from original packet */
1051 pkt_recoded.side_data = NULL;
1052 pkt_recoded.side_data_elems = 0;
1054 av_packet_unref(&pkt_recoded);
1060 av_packet_free_side_data(&tmp);
1067 avctx->frame_number++;
1073 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1075 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1076 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1079 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1081 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1086 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
1087 enum AVPixelFormat pix_fmt)
1089 AVHWAccel *hwaccel = NULL;
1091 while ((hwaccel = av_hwaccel_next(hwaccel)))
1092 if (hwaccel->id == codec_id
1093 && hwaccel->pix_fmt == pix_fmt)
1098 static int setup_hwaccel(AVCodecContext *avctx,
1099 const enum AVPixelFormat fmt,
1102 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
1106 av_log(avctx, AV_LOG_ERROR,
1107 "Could not find an AVHWAccel for the pixel format: %s",
1109 return AVERROR(ENOENT);
1112 if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
1113 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1114 av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
1116 return AVERROR_PATCHWELCOME;
1119 if (hwa->priv_data_size) {
1120 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
1121 if (!avctx->internal->hwaccel_priv_data)
1122 return AVERROR(ENOMEM);
1126 ret = hwa->init(avctx);
1128 av_freep(&avctx->internal->hwaccel_priv_data);
1133 avctx->hwaccel = hwa;
1138 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1140 const AVPixFmtDescriptor *desc;
1141 enum AVPixelFormat *choices;
1142 enum AVPixelFormat ret;
1145 while (fmt[n] != AV_PIX_FMT_NONE)
1149 avctx->sw_pix_fmt = fmt[n - 1];
1150 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
1152 choices = av_malloc_array(n + 1, sizeof(*choices));
1154 return AV_PIX_FMT_NONE;
1156 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
1159 if (avctx->hwaccel && avctx->hwaccel->uninit)
1160 avctx->hwaccel->uninit(avctx);
1161 av_freep(&avctx->internal->hwaccel_priv_data);
1162 avctx->hwaccel = NULL;
1164 av_buffer_unref(&avctx->hw_frames_ctx);
1166 ret = avctx->get_format(avctx, choices);
1168 desc = av_pix_fmt_desc_get(ret);
1170 ret = AV_PIX_FMT_NONE;
1174 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1176 #if FF_API_CAP_VDPAU
1177 if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
1181 if (avctx->hw_frames_ctx) {
1182 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1183 if (hw_frames_ctx->format != ret) {
1184 av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
1185 "does not match the format of provided AVHWFramesContext\n");
1186 ret = AV_PIX_FMT_NONE;
1191 if (!setup_hwaccel(avctx, ret, desc->name))
1194 /* Remove failed hwaccel from choices */
1195 for (n = 0; choices[n] != ret; n++)
1196 av_assert0(choices[n] != AV_PIX_FMT_NONE);
1199 choices[n] = choices[n + 1];
1200 while (choices[n++] != AV_PIX_FMT_NONE);
1207 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
1209 FramePool *pool = avctx->internal->pool;
1212 switch (avctx->codec_type) {
1213 case AVMEDIA_TYPE_VIDEO: {
1216 int size[4] = { 0 };
1217 int w = frame->width;
1218 int h = frame->height;
1219 int tmpsize, unaligned;
1221 if (pool->format == frame->format &&
1222 pool->width == frame->width && pool->height == frame->height)
1225 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
1228 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
1229 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
1230 ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
1233 // increase alignment of w for next try (rhs gives the lowest bit set in w)
1237 for (i = 0; i < 4; i++)
1238 unaligned |= linesize[i] % pool->stride_align[i];
1239 } while (unaligned);
1241 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
1246 for (i = 0; i < 3 && data[i + 1]; i++)
1247 size[i] = data[i + 1] - data[i];
1248 size[i] = tmpsize - (data[i] - data[0]);
1250 for (i = 0; i < 4; i++) {
1251 av_buffer_pool_uninit(&pool->pools[i]);
1252 pool->linesize[i] = linesize[i];
1254 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
1255 CONFIG_MEMORY_POISONING ?
1258 if (!pool->pools[i]) {
1259 ret = AVERROR(ENOMEM);
1264 pool->format = frame->format;
1265 pool->width = frame->width;
1266 pool->height = frame->height;
1270 case AVMEDIA_TYPE_AUDIO: {
1271 int ch = frame->channels; //av_get_channel_layout_nb_channels(frame->channel_layout);
1272 int planar = av_sample_fmt_is_planar(frame->format);
1273 int planes = planar ? ch : 1;
1275 if (pool->format == frame->format && pool->planes == planes &&
1276 pool->channels == ch && frame->nb_samples == pool->samples)
1279 av_buffer_pool_uninit(&pool->pools[0]);
1280 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
1281 frame->nb_samples, frame->format, 0);
1285 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
1286 if (!pool->pools[0]) {
1287 ret = AVERROR(ENOMEM);
1291 pool->format = frame->format;
1292 pool->planes = planes;
1293 pool->channels = ch;
1294 pool->samples = frame->nb_samples;
1297 default: av_assert0(0);
1301 for (i = 0; i < 4; i++)
1302 av_buffer_pool_uninit(&pool->pools[i]);
1304 pool->planes = pool->channels = pool->samples = 0;
1305 pool->width = pool->height = 0;
1309 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
1311 FramePool *pool = avctx->internal->pool;
1312 int planes = pool->planes;
1315 frame->linesize[0] = pool->linesize[0];
1317 if (planes > AV_NUM_DATA_POINTERS) {
1318 frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
1319 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
1320 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
1321 sizeof(*frame->extended_buf));
1322 if (!frame->extended_data || !frame->extended_buf) {
1323 av_freep(&frame->extended_data);
1324 av_freep(&frame->extended_buf);
1325 return AVERROR(ENOMEM);
1328 frame->extended_data = frame->data;
1329 av_assert0(frame->nb_extended_buf == 0);
1332 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
1333 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
1336 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
1338 for (i = 0; i < frame->nb_extended_buf; i++) {
1339 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
1340 if (!frame->extended_buf[i])
1342 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
1345 if (avctx->debug & FF_DEBUG_BUFFERS)
1346 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
1350 av_frame_unref(frame);
1351 return AVERROR(ENOMEM);
1354 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
1356 FramePool *pool = s->internal->pool;
1357 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
1360 if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
1361 av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
1366 av_log(s, AV_LOG_ERROR,
1367 "Unable to get pixel format descriptor for format %s\n",
1368 av_get_pix_fmt_name(pic->format));
1369 return AVERROR(EINVAL);
1372 memset(pic->data, 0, sizeof(pic->data));
1373 pic->extended_data = pic->data;
1375 for (i = 0; i < 4 && pool->pools[i]; i++) {
1376 pic->linesize[i] = pool->linesize[i];
1378 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
1382 pic->data[i] = pic->buf[i]->data;
1384 for (; i < AV_NUM_DATA_POINTERS; i++) {
1385 pic->data[i] = NULL;
1386 pic->linesize[i] = 0;
1388 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
1389 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
1390 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
1392 if (s->debug & FF_DEBUG_BUFFERS)
1393 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
1397 av_frame_unref(pic);
1398 return AVERROR(ENOMEM);
1401 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
1405 if (avctx->hw_frames_ctx)
1406 return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
1408 if ((ret = update_frame_pool(avctx, frame)) < 0)
1411 switch (avctx->codec_type) {
1412 case AVMEDIA_TYPE_VIDEO:
1413 return video_get_buffer(avctx, frame);
1414 case AVMEDIA_TYPE_AUDIO:
1415 return audio_get_buffer(avctx, frame);
1421 static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
1424 const uint8_t *side_metadata;
1426 AVDictionary **frame_md = &frame->metadata;
1428 side_metadata = av_packet_get_side_data(avpkt,
1429 AV_PKT_DATA_STRINGS_METADATA, &size);
1430 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
1433 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
1435 const AVPacket *pkt = avctx->internal->last_pkt_props;
1437 static const struct {
1438 enum AVPacketSideDataType packet;
1439 enum AVFrameSideDataType frame;
1441 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
1442 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
1443 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
1444 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
1445 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
1446 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
1447 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
1451 frame->pts = pkt->pts;
1453 FF_DISABLE_DEPRECATION_WARNINGS
1454 frame->pkt_pts = pkt->pts;
1455 FF_ENABLE_DEPRECATION_WARNINGS
1457 frame->pkt_pos = pkt->pos;
1458 frame->pkt_duration = pkt->duration;
1459 frame->pkt_size = pkt->size;
1461 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1463 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1465 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1469 return AVERROR(ENOMEM);
1471 memcpy(frame_sd->data, packet_sd, size);
1474 add_metadata_from_side_data(pkt, frame);
1476 if (pkt->flags & AV_PKT_FLAG_DISCARD) {
1477 frame->flags |= AV_FRAME_FLAG_DISCARD;
1479 frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
1482 frame->reordered_opaque = avctx->reordered_opaque;
1484 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
1485 frame->color_primaries = avctx->color_primaries;
1486 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
1487 frame->color_trc = avctx->color_trc;
1488 if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
1489 frame->colorspace = avctx->colorspace;
1490 if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
1491 frame->color_range = avctx->color_range;
1492 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
1493 frame->chroma_location = avctx->chroma_sample_location;
1495 switch (avctx->codec->type) {
1496 case AVMEDIA_TYPE_VIDEO:
1497 frame->format = avctx->pix_fmt;
1498 if (!frame->sample_aspect_ratio.num)
1499 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
1501 if (frame->width && frame->height &&
1502 av_image_check_sar(frame->width, frame->height,
1503 frame->sample_aspect_ratio) < 0) {
1504 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1505 frame->sample_aspect_ratio.num,
1506 frame->sample_aspect_ratio.den);
1507 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1511 case AVMEDIA_TYPE_AUDIO:
1512 if (!frame->sample_rate)
1513 frame->sample_rate = avctx->sample_rate;
1514 if (frame->format < 0)
1515 frame->format = avctx->sample_fmt;
1516 if (!frame->channel_layout) {
1517 if (avctx->channel_layout) {
1518 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
1520 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
1521 "configuration.\n");
1522 return AVERROR(EINVAL);
1525 frame->channel_layout = avctx->channel_layout;
1527 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1528 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
1530 return AVERROR(ENOSYS);
1534 frame->channels = avctx->channels;
1540 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1542 return ff_init_buffer_info(avctx, frame);
1545 static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
1547 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1549 int num_planes = av_pix_fmt_count_planes(frame->format);
1550 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1551 int flags = desc ? desc->flags : 0;
1552 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
1554 for (i = 0; i < num_planes; i++) {
1555 av_assert0(frame->data[i]);
1557 // For now do not enforce anything for palette of pseudopal formats
1558 if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PSEUDOPAL))
1560 // For formats without data like hwaccel allow unused pointers to be non-NULL.
1561 for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
1563 av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
1564 frame->data[i] = NULL;
1569 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
1571 const AVHWAccel *hwaccel = avctx->hwaccel;
1572 int override_dimensions = 1;
1575 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1576 if ((ret = av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
1577 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
1578 return AVERROR(EINVAL);
1581 if (frame->width <= 0 || frame->height <= 0) {
1582 frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
1583 frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
1584 override_dimensions = 0;
1587 if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
1588 av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
1589 return AVERROR(EINVAL);
1592 ret = ff_decode_frame_props(avctx, frame);
1597 if (hwaccel->alloc_frame) {
1598 ret = hwaccel->alloc_frame(avctx, frame);
1602 avctx->sw_pix_fmt = avctx->pix_fmt;
1604 ret = avctx->get_buffer2(avctx, frame, flags);
1606 validate_avframe_allocation(avctx, frame);
1609 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
1610 frame->width = avctx->width;
1611 frame->height = avctx->height;
1617 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1619 int ret = get_buffer_internal(avctx, frame, flags);
1621 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1622 frame->width = frame->height = 0;
1627 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
1632 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1634 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1635 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1636 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1637 av_frame_unref(frame);
1640 ff_init_buffer_info(avctx, frame);
1642 if (!frame->data[0])
1643 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1645 if (av_frame_is_writable(frame))
1646 return ff_decode_frame_props(avctx, frame);
1648 tmp = av_frame_alloc();
1650 return AVERROR(ENOMEM);
1652 av_frame_move_ref(tmp, frame);
1654 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1656 av_frame_free(&tmp);
1660 av_frame_copy(frame, tmp);
1661 av_frame_free(&tmp);
1666 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1668 int ret = reget_buffer_internal(avctx, frame);
1670 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1674 void avcodec_flush_buffers(AVCodecContext *avctx)
1676 avctx->internal->draining = 0;
1677 avctx->internal->draining_done = 0;
1678 avctx->internal->nb_draining_errors = 0;
1679 av_frame_unref(avctx->internal->buffer_frame);
1680 av_frame_unref(avctx->internal->compat_decode_frame);
1681 av_packet_unref(avctx->internal->buffer_pkt);
1682 avctx->internal->buffer_pkt_valid = 0;
1684 av_packet_unref(avctx->internal->ds.in_pkt);
1686 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1687 ff_thread_flush(avctx);
1688 else if (avctx->codec->flush)
1689 avctx->codec->flush(avctx);
1691 avctx->pts_correction_last_pts =
1692 avctx->pts_correction_last_dts = INT64_MIN;
1694 ff_decode_bsfs_uninit(avctx);
1696 if (!avctx->refcounted_frames)
1697 av_frame_unref(avctx->internal->to_free);
1700 void ff_decode_bsfs_uninit(AVCodecContext *avctx)
1702 DecodeFilterContext *s = &avctx->internal->filter;
1705 for (i = 0; i < s->nb_bsfs; i++)
1706 av_bsf_free(&s->bsfs[i]);