2 * generic decoding-related code
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/common.h"
29 #include "libavutil/frame.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/imgutils.h"
34 #include "bytestream.h"
39 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
45 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
49 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
50 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
51 "changes, but PARAM_CHANGE side data was sent to it.\n");
52 ret = AVERROR(EINVAL);
59 flags = bytestream_get_le32(&data);
62 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
65 avctx->channels = bytestream_get_le32(&data);
68 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
71 avctx->channel_layout = bytestream_get_le64(&data);
74 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
77 avctx->sample_rate = bytestream_get_le32(&data);
80 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
83 avctx->width = bytestream_get_le32(&data);
84 avctx->height = bytestream_get_le32(&data);
86 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
93 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
94 ret = AVERROR_INVALIDDATA;
97 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
98 if (avctx->err_recognition & AV_EF_EXPLODE)
104 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
106 av_packet_unref(avci->last_pkt_props);
108 return av_packet_copy_props(avci->last_pkt_props, pkt);
112 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
116 /* move the original frame to our backup */
117 av_frame_unref(avci->to_free);
118 av_frame_move_ref(avci->to_free, frame);
120 /* now copy everything except the AVBufferRefs back
121 * note that we make a COPY of the side data, so calling av_frame_free() on
122 * the caller's frame will work properly */
123 ret = av_frame_copy_props(frame, avci->to_free);
127 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
128 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
129 if (avci->to_free->extended_data != avci->to_free->data) {
130 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
131 int size = planes * sizeof(*frame->extended_data);
134 av_frame_unref(frame);
138 frame->extended_data = av_malloc(size);
139 if (!frame->extended_data) {
140 av_frame_unref(frame);
141 return AVERROR(ENOMEM);
143 memcpy(frame->extended_data, avci->to_free->extended_data,
146 frame->extended_data = frame->data;
148 frame->format = avci->to_free->format;
149 frame->width = avci->to_free->width;
150 frame->height = avci->to_free->height;
151 frame->channel_layout = avci->to_free->channel_layout;
152 frame->nb_samples = avci->to_free->nb_samples;
157 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
159 AVCodecInternal *avci = avctx->internal;
165 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data_elems)
166 return AVERROR(EAGAIN);
168 av_packet_move_ref(pkt, avci->buffer_pkt);
170 ret = extract_packet_props(avctx->internal, pkt);
174 ret = apply_param_change(avctx, pkt);
178 if (avctx->codec->receive_frame)
179 avci->compat_decode_consumed += pkt->size;
183 av_packet_unref(pkt);
188 * The core of the receive_frame_wrapper for the decoders implementing
189 * the simple API. Certain decoders might consume partial packets without
190 * returning any output, so this function needs to be called in a loop until it
193 static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
195 AVCodecInternal *avci = avctx->internal;
196 DecodeSimpleContext *ds = &avci->ds;
197 AVPacket *pkt = ds->in_pkt;
201 if (!pkt->data && !avci->draining) {
202 av_packet_unref(pkt);
203 ret = ff_decode_get_packet(avctx, pkt);
204 if (ret < 0 && ret != AVERROR_EOF)
208 // Some codecs (at least wma lossless) will crash when feeding drain packets
209 // after EOF was signaled.
210 if (avci->draining_done)
214 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
215 avctx->active_thread_type & FF_THREAD_FRAME))
220 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
221 ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
223 ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
225 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
226 frame->pkt_dts = pkt->dts;
227 /* get_buffer is supposed to set frame parameters */
228 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
229 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
230 frame->width = avctx->width;
231 frame->height = avctx->height;
232 frame->format = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
233 avctx->pix_fmt : avctx->sample_fmt;
240 av_frame_unref(frame);
242 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
245 #if FF_API_AVCTX_TIMEBASE
246 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
247 avctx->time_base = av_inv_q(avctx->framerate);
250 if (avctx->internal->draining && !got_frame)
251 avci->draining_done = 1;
253 avci->compat_decode_consumed += ret;
255 if (ret >= pkt->size || ret < 0) {
256 av_packet_unref(pkt);
260 pkt->data += consumed;
261 pkt->size -= consumed;
262 pkt->pts = AV_NOPTS_VALUE;
263 pkt->dts = AV_NOPTS_VALUE;
264 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
265 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
269 av_assert0(frame->buf[0]);
271 return ret < 0 ? ret : 0;
274 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
278 while (!frame->buf[0]) {
279 ret = decode_simple_internal(avctx, frame);
287 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
289 AVCodecInternal *avci = avctx->internal;
292 av_assert0(!frame->buf[0]);
294 if (avctx->codec->receive_frame)
295 ret = avctx->codec->receive_frame(avctx, frame);
297 ret = decode_simple_receive_frame(avctx, frame);
299 if (ret == AVERROR_EOF)
300 avci->draining_done = 1;
305 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
307 AVCodecInternal *avci = avctx->internal;
310 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
311 return AVERROR(EINVAL);
313 if (avctx->internal->draining)
316 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems)
317 return AVERROR(EAGAIN);
319 if (!avpkt || !avpkt->size) {
320 avctx->internal->draining = 1;
322 ret = av_packet_ref(avci->buffer_pkt, avpkt);
327 if (!avci->buffer_frame->buf[0]) {
328 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
329 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
336 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
338 AVCodecInternal *avci = avctx->internal;
341 av_frame_unref(frame);
343 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
344 return AVERROR(EINVAL);
346 if (avci->buffer_frame->buf[0]) {
347 av_frame_move_ref(frame, avci->buffer_frame);
349 ret = decode_receive_frame_internal(avctx, frame);
354 avctx->frame_number++;
359 static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
360 int *got_frame, AVPacket *pkt)
362 AVCodecInternal *avci = avctx->internal;
365 av_assert0(avci->compat_decode_consumed == 0);
368 avci->compat_decode = 1;
370 if (avci->compat_decode_partial_size > 0 &&
371 avci->compat_decode_partial_size != pkt->size) {
372 av_log(avctx, AV_LOG_ERROR,
373 "Got unexpected packet size after a partial decode\n");
374 ret = AVERROR(EINVAL);
378 if (!avci->compat_decode_partial_size) {
379 ret = avcodec_send_packet(avctx, pkt);
380 if (ret == AVERROR_EOF)
382 else if (ret == AVERROR(EAGAIN)) {
383 /* we fully drain all the output in each decode call, so this should not
392 ret = avcodec_receive_frame(avctx, frame);
394 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
399 if (frame != avci->compat_decode_frame) {
400 if (!avctx->refcounted_frames) {
401 ret = unrefcount_frame(avci, frame);
407 frame = avci->compat_decode_frame;
409 if (!avci->compat_decode_warned) {
410 av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
411 "API cannot return all the frames for this decoder. "
412 "Some frames will be dropped. Update your code to the "
413 "new decoding API to fix this.\n");
414 avci->compat_decode_warned = 1;
418 if (avci->draining || avci->compat_decode_consumed < pkt->size)
424 ret = FFMIN(avci->compat_decode_consumed, pkt->size);
425 avci->compat_decode_consumed = 0;
426 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
431 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
432 int *got_picture_ptr,
435 return compat_decode(avctx, picture, got_picture_ptr, avpkt);
438 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
443 return compat_decode(avctx, frame, got_frame_ptr, avpkt);
446 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
452 ret = extract_packet_props(avctx->internal, avpkt);
457 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
459 avctx->frame_number++;
463 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
465 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
466 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
469 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
471 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
476 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
477 enum AVPixelFormat pix_fmt)
479 AVHWAccel *hwaccel = NULL;
481 while ((hwaccel = av_hwaccel_next(hwaccel)))
482 if (hwaccel->id == codec_id
483 && hwaccel->pix_fmt == pix_fmt)
488 static int setup_hwaccel(AVCodecContext *avctx,
489 const enum AVPixelFormat fmt,
492 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
496 av_log(avctx, AV_LOG_ERROR,
497 "Could not find an AVHWAccel for the pixel format: %s",
499 return AVERROR(ENOENT);
502 if (hwa->priv_data_size) {
503 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
504 if (!avctx->internal->hwaccel_priv_data)
505 return AVERROR(ENOMEM);
509 ret = hwa->init(avctx);
511 av_freep(&avctx->internal->hwaccel_priv_data);
516 avctx->hwaccel = hwa;
521 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
523 const AVPixFmtDescriptor *desc;
524 enum AVPixelFormat *choices;
525 enum AVPixelFormat ret;
528 while (fmt[n] != AV_PIX_FMT_NONE)
532 avctx->sw_pix_fmt = fmt[n - 1];
533 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
535 choices = av_malloc_array(n + 1, sizeof(*choices));
537 return AV_PIX_FMT_NONE;
539 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
542 if (avctx->hwaccel && avctx->hwaccel->uninit)
543 avctx->hwaccel->uninit(avctx);
544 av_freep(&avctx->internal->hwaccel_priv_data);
545 avctx->hwaccel = NULL;
547 av_buffer_unref(&avctx->hw_frames_ctx);
549 ret = avctx->get_format(avctx, choices);
551 desc = av_pix_fmt_desc_get(ret);
553 ret = AV_PIX_FMT_NONE;
557 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
560 if (avctx->hw_frames_ctx) {
561 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
562 if (hw_frames_ctx->format != ret) {
563 av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
564 "does not match the format of provided AVHWFramesContext\n");
565 ret = AV_PIX_FMT_NONE;
570 if (!setup_hwaccel(avctx, ret, desc->name))
573 /* Remove failed hwaccel from choices */
574 for (n = 0; choices[n] != ret; n++)
575 av_assert0(choices[n] != AV_PIX_FMT_NONE);
578 choices[n] = choices[n + 1];
579 while (choices[n++] != AV_PIX_FMT_NONE);
586 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
588 FramePool *pool = avctx->internal->pool;
591 switch (avctx->codec_type) {
592 case AVMEDIA_TYPE_VIDEO: {
596 int w = frame->width;
597 int h = frame->height;
598 int tmpsize, unaligned;
600 if (pool->format == frame->format &&
601 pool->width == frame->width && pool->height == frame->height)
604 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
607 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
608 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
609 av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
610 // increase alignment of w for next try (rhs gives the lowest bit set in w)
614 for (i = 0; i < 4; i++)
615 unaligned |= linesize[i] % pool->stride_align[i];
618 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
623 for (i = 0; i < 3 && data[i + 1]; i++)
624 size[i] = data[i + 1] - data[i];
625 size[i] = tmpsize - (data[i] - data[0]);
627 for (i = 0; i < 4; i++) {
628 av_buffer_pool_uninit(&pool->pools[i]);
629 pool->linesize[i] = linesize[i];
631 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
632 if (!pool->pools[i]) {
633 ret = AVERROR(ENOMEM);
638 pool->format = frame->format;
639 pool->width = frame->width;
640 pool->height = frame->height;
644 case AVMEDIA_TYPE_AUDIO: {
645 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
646 int planar = av_sample_fmt_is_planar(frame->format);
647 int planes = planar ? ch : 1;
649 if (pool->format == frame->format && pool->planes == planes &&
650 pool->channels == ch && frame->nb_samples == pool->samples)
653 av_buffer_pool_uninit(&pool->pools[0]);
654 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
655 frame->nb_samples, frame->format, 0);
659 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
660 if (!pool->pools[0]) {
661 ret = AVERROR(ENOMEM);
665 pool->format = frame->format;
666 pool->planes = planes;
668 pool->samples = frame->nb_samples;
671 default: av_assert0(0);
675 for (i = 0; i < 4; i++)
676 av_buffer_pool_uninit(&pool->pools[i]);
678 pool->planes = pool->channels = pool->samples = 0;
679 pool->width = pool->height = 0;
683 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
685 FramePool *pool = avctx->internal->pool;
686 int planes = pool->planes;
689 frame->linesize[0] = pool->linesize[0];
691 if (planes > AV_NUM_DATA_POINTERS) {
692 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
693 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
694 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
695 sizeof(*frame->extended_buf));
696 if (!frame->extended_data || !frame->extended_buf) {
697 av_freep(&frame->extended_data);
698 av_freep(&frame->extended_buf);
699 return AVERROR(ENOMEM);
702 frame->extended_data = frame->data;
704 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
705 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
708 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
710 for (i = 0; i < frame->nb_extended_buf; i++) {
711 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
712 if (!frame->extended_buf[i])
714 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
717 if (avctx->debug & FF_DEBUG_BUFFERS)
718 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
722 av_frame_unref(frame);
723 return AVERROR(ENOMEM);
726 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
728 FramePool *pool = s->internal->pool;
732 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
736 memset(pic->data, 0, sizeof(pic->data));
737 pic->extended_data = pic->data;
739 for (i = 0; i < 4 && pool->pools[i]; i++) {
740 pic->linesize[i] = pool->linesize[i];
742 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
746 pic->data[i] = pic->buf[i]->data;
748 for (; i < AV_NUM_DATA_POINTERS; i++) {
750 pic->linesize[i] = 0;
752 if (pic->data[1] && !pic->data[2])
753 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
755 if (s->debug & FF_DEBUG_BUFFERS)
756 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
761 return AVERROR(ENOMEM);
764 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
768 if (avctx->hw_frames_ctx)
769 return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
771 if ((ret = update_frame_pool(avctx, frame)) < 0)
774 switch (avctx->codec_type) {
775 case AVMEDIA_TYPE_VIDEO:
776 return video_get_buffer(avctx, frame);
777 case AVMEDIA_TYPE_AUDIO:
778 return audio_get_buffer(avctx, frame);
784 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
786 AVPacket *pkt = avctx->internal->last_pkt_props;
789 enum AVPacketSideDataType packet;
790 enum AVFrameSideDataType frame;
792 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
793 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
794 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
795 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
796 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
799 frame->color_primaries = avctx->color_primaries;
800 frame->color_trc = avctx->color_trc;
801 frame->colorspace = avctx->colorspace;
802 frame->color_range = avctx->color_range;
803 frame->chroma_location = avctx->chroma_sample_location;
805 frame->reordered_opaque = avctx->reordered_opaque;
808 FF_DISABLE_DEPRECATION_WARNINGS
809 frame->pkt_pts = pkt->pts;
810 FF_ENABLE_DEPRECATION_WARNINGS
812 frame->pts = pkt->pts;
814 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
816 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
818 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
822 return AVERROR(ENOMEM);
824 memcpy(frame_sd->data, packet_sd, size);
831 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
833 const AVHWAccel *hwaccel = avctx->hwaccel;
834 int override_dimensions = 1;
837 switch (avctx->codec_type) {
838 case AVMEDIA_TYPE_VIDEO:
839 if (frame->width <= 0 || frame->height <= 0) {
840 frame->width = FFMAX(avctx->width, avctx->coded_width);
841 frame->height = FFMAX(avctx->height, avctx->coded_height);
842 override_dimensions = 0;
844 if (frame->format < 0)
845 frame->format = avctx->pix_fmt;
846 if (!frame->sample_aspect_ratio.num)
847 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
849 if (av_image_check_sar(frame->width, frame->height,
850 frame->sample_aspect_ratio) < 0) {
851 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
852 frame->sample_aspect_ratio.num,
853 frame->sample_aspect_ratio.den);
854 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
857 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
860 case AVMEDIA_TYPE_AUDIO:
861 if (!frame->sample_rate)
862 frame->sample_rate = avctx->sample_rate;
863 if (frame->format < 0)
864 frame->format = avctx->sample_fmt;
865 if (!frame->channel_layout) {
866 if (avctx->channel_layout) {
867 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
869 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
871 return AVERROR(EINVAL);
874 frame->channel_layout = avctx->channel_layout;
876 if (avctx->channels > FF_SANE_NB_CHANNELS) {
877 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
879 return AVERROR(ENOSYS);
882 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
883 if (!frame->channel_layout)
884 frame->channel_layout = (1ULL << avctx->channels) - 1;
888 default: return AVERROR(EINVAL);
891 ret = ff_decode_frame_props(avctx, frame);
896 if (hwaccel->alloc_frame) {
897 ret = hwaccel->alloc_frame(avctx, frame);
901 avctx->sw_pix_fmt = avctx->pix_fmt;
903 ret = avctx->get_buffer2(avctx, frame, flags);
906 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
907 frame->width = avctx->width;
908 frame->height = avctx->height;
914 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
919 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
922 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
924 if (av_frame_is_writable(frame))
925 return ff_decode_frame_props(avctx, frame);
927 tmp = av_frame_alloc();
929 return AVERROR(ENOMEM);
931 av_frame_move_ref(tmp, frame);
933 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
939 av_frame_copy(frame, tmp);
945 void avcodec_flush_buffers(AVCodecContext *avctx)
947 avctx->internal->draining = 0;
948 avctx->internal->draining_done = 0;
949 av_frame_unref(avctx->internal->buffer_frame);
950 av_frame_unref(avctx->internal->compat_decode_frame);
951 av_packet_unref(avctx->internal->buffer_pkt);
952 avctx->internal->buffer_pkt_valid = 0;
954 av_packet_unref(avctx->internal->ds.in_pkt);
956 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
957 ff_thread_flush(avctx);
958 else if (avctx->codec->flush)
959 avctx->codec->flush(avctx);
961 if (!avctx->refcounted_frames)
962 av_frame_unref(avctx->internal->to_free);