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"
32 #include "libavutil/intmath.h"
35 #include "bytestream.h"
40 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
46 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
50 if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
51 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
52 "changes, but PARAM_CHANGE side data was sent to it.\n");
53 ret = AVERROR(EINVAL);
60 flags = bytestream_get_le32(&data);
63 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
66 avctx->channels = bytestream_get_le32(&data);
69 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
72 avctx->channel_layout = bytestream_get_le64(&data);
75 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
78 avctx->sample_rate = bytestream_get_le32(&data);
81 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
84 avctx->width = bytestream_get_le32(&data);
85 avctx->height = bytestream_get_le32(&data);
87 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
94 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
95 ret = AVERROR_INVALIDDATA;
98 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
99 if (avctx->err_recognition & AV_EF_EXPLODE)
105 static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
107 av_packet_unref(avci->last_pkt_props);
109 return av_packet_copy_props(avci->last_pkt_props, pkt);
113 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
117 /* move the original frame to our backup */
118 av_frame_unref(avci->to_free);
119 av_frame_move_ref(avci->to_free, frame);
121 /* now copy everything except the AVBufferRefs back
122 * note that we make a COPY of the side data, so calling av_frame_free() on
123 * the caller's frame will work properly */
124 ret = av_frame_copy_props(frame, avci->to_free);
128 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
129 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
130 if (avci->to_free->extended_data != avci->to_free->data) {
131 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
132 int size = planes * sizeof(*frame->extended_data);
135 av_frame_unref(frame);
139 frame->extended_data = av_malloc(size);
140 if (!frame->extended_data) {
141 av_frame_unref(frame);
142 return AVERROR(ENOMEM);
144 memcpy(frame->extended_data, avci->to_free->extended_data,
147 frame->extended_data = frame->data;
149 frame->format = avci->to_free->format;
150 frame->width = avci->to_free->width;
151 frame->height = avci->to_free->height;
152 frame->channel_layout = avci->to_free->channel_layout;
153 frame->nb_samples = avci->to_free->nb_samples;
158 static int bsfs_init(AVCodecContext *avctx)
160 AVCodecInternal *avci = avctx->internal;
161 DecodeFilterContext *s = &avci->filter;
162 const char *bsfs_str;
168 bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
169 while (bsfs_str && *bsfs_str) {
171 const AVBitStreamFilter *filter;
174 bsf = av_get_token(&bsfs_str, ",");
176 ret = AVERROR(ENOMEM);
180 filter = av_bsf_get_by_name(bsf);
182 av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
183 "requested by a decoder. This is a bug, please report it.\n",
191 tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
193 ret = AVERROR(ENOMEM);
199 ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
203 if (s->nb_bsfs == 1) {
204 /* We do not currently have an API for passing the input timebase into decoders,
205 * but no filters used here should actually need it.
206 * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
207 s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
208 ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
211 s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
212 ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
213 s->bsfs[s->nb_bsfs - 2]->par_out);
218 ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
225 ff_decode_bsfs_uninit(avctx);
229 /* try to get one output packet from the filter chain */
230 static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
232 DecodeFilterContext *s = &avctx->internal->filter;
235 /* start with the last filter in the chain */
236 idx = s->nb_bsfs - 1;
238 /* request a packet from the currently selected filter */
239 ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
240 if (ret == AVERROR(EAGAIN)) {
241 /* no packets available, try the next filter up the chain */
245 } else if (ret < 0 && ret != AVERROR_EOF) {
249 /* got a packet or EOF -- pass it to the caller or to the next filter
251 if (idx == s->nb_bsfs - 1) {
255 ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
257 av_log(avctx, AV_LOG_ERROR,
258 "Error pre-processing a packet before decoding\n");
259 av_packet_unref(pkt);
265 return AVERROR(EAGAIN);
268 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
270 AVCodecInternal *avci = avctx->internal;
276 ret = bsfs_poll(avctx, pkt);
277 if (ret == AVERROR_EOF)
282 ret = extract_packet_props(avctx->internal, pkt);
286 ret = apply_param_change(avctx, pkt);
290 if (avctx->codec->receive_frame)
291 avci->compat_decode_consumed += pkt->size;
295 av_packet_unref(pkt);
300 * The core of the receive_frame_wrapper for the decoders implementing
301 * the simple API. Certain decoders might consume partial packets without
302 * returning any output, so this function needs to be called in a loop until it
305 static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
307 AVCodecInternal *avci = avctx->internal;
308 DecodeSimpleContext *ds = &avci->ds;
309 AVPacket *pkt = ds->in_pkt;
313 if (!pkt->data && !avci->draining) {
314 av_packet_unref(pkt);
315 ret = ff_decode_get_packet(avctx, pkt);
316 if (ret < 0 && ret != AVERROR_EOF)
320 // Some codecs (at least wma lossless) will crash when feeding drain packets
321 // after EOF was signaled.
322 if (avci->draining_done)
326 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
327 avctx->active_thread_type & FF_THREAD_FRAME))
332 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
333 ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
335 ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
337 if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
338 frame->pkt_dts = pkt->dts;
339 /* get_buffer is supposed to set frame parameters */
340 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
341 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
342 frame->width = avctx->width;
343 frame->height = avctx->height;
344 frame->format = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
345 avctx->pix_fmt : avctx->sample_fmt;
352 av_frame_unref(frame);
354 if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
357 if (avctx->internal->draining && !got_frame)
358 avci->draining_done = 1;
360 avci->compat_decode_consumed += ret;
362 if (ret >= pkt->size || ret < 0) {
363 av_packet_unref(pkt);
367 pkt->data += consumed;
368 pkt->size -= consumed;
369 pkt->pts = AV_NOPTS_VALUE;
370 pkt->dts = AV_NOPTS_VALUE;
371 avci->last_pkt_props->pts = AV_NOPTS_VALUE;
372 avci->last_pkt_props->dts = AV_NOPTS_VALUE;
376 av_assert0(frame->buf[0]);
378 return ret < 0 ? ret : 0;
381 static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
385 while (!frame->buf[0]) {
386 ret = decode_simple_internal(avctx, frame);
394 static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
396 AVCodecInternal *avci = avctx->internal;
399 av_assert0(!frame->buf[0]);
401 if (avctx->codec->receive_frame)
402 ret = avctx->codec->receive_frame(avctx, frame);
404 ret = decode_simple_receive_frame(avctx, frame);
406 if (ret == AVERROR_EOF)
407 avci->draining_done = 1;
412 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
414 AVCodecInternal *avci = avctx->internal;
417 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
418 return AVERROR(EINVAL);
420 if (avctx->internal->draining)
423 ret = bsfs_init(avctx);
427 av_packet_unref(avci->buffer_pkt);
428 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
429 ret = av_packet_ref(avci->buffer_pkt, avpkt);
434 ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
436 av_packet_unref(avci->buffer_pkt);
440 if (!avci->buffer_frame->buf[0]) {
441 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
442 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
449 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
450 const AVPixFmtDescriptor *desc)
454 for (i = 0; frame->data[i]; i++) {
455 const AVComponentDescriptor *comp = NULL;
456 int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
457 int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
459 if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
464 /* find any component descriptor for this plane */
465 for (j = 0; j < desc->nb_components; j++) {
466 if (desc->comp[j].plane == i) {
467 comp = &desc->comp[j];
474 offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
475 (frame->crop_left >> shift_x) * comp->step;
481 static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
483 const AVPixFmtDescriptor *desc;
487 /* make sure we are noisy about decoders returning invalid cropping data */
488 if (frame->crop_left >= INT_MAX - frame->crop_right ||
489 frame->crop_top >= INT_MAX - frame->crop_bottom ||
490 (frame->crop_left + frame->crop_right) >= frame->width ||
491 (frame->crop_top + frame->crop_bottom) >= frame->height) {
492 av_log(avctx, AV_LOG_WARNING,
493 "Invalid cropping information set by a decoder: %zu/%zu/%zu/%zu "
494 "(frame size %dx%d). This is a bug, please report it\n",
495 frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
496 frame->width, frame->height);
497 frame->crop_left = 0;
498 frame->crop_right = 0;
500 frame->crop_bottom = 0;
504 if (!avctx->apply_cropping)
507 desc = av_pix_fmt_desc_get(frame->format);
511 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
512 * formats cannot be easily handled here either (and corresponding decoders
513 * should not export any cropping anyway), so do the same for those as well.
515 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
516 frame->width -= frame->crop_right;
517 frame->height -= frame->crop_bottom;
518 frame->crop_right = 0;
519 frame->crop_bottom = 0;
523 /* calculate the offsets for each plane */
524 calc_cropping_offsets(offsets, frame, desc);
526 /* adjust the offsets to avoid breaking alignment */
527 if (!(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
528 int log2_crop_align = frame->crop_left ? av_ctz(frame->crop_left) : INT_MAX;
529 int min_log2_align = INT_MAX;
531 for (i = 0; frame->data[i]; i++) {
532 int log2_align = offsets[i] ? av_ctz(offsets[i]) : INT_MAX;
533 min_log2_align = FFMIN(log2_align, min_log2_align);
536 /* we assume, and it should always be true, that the data alignment is
537 * related to the cropping alignment by a constant power-of-2 factor */
538 if (log2_crop_align < min_log2_align)
541 if (min_log2_align < 5) {
542 frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
543 calc_cropping_offsets(offsets, frame, desc);
547 for (i = 0; frame->data[i]; i++)
548 frame->data[i] += offsets[i];
550 frame->width -= (frame->crop_left + frame->crop_right);
551 frame->height -= (frame->crop_top + frame->crop_bottom);
552 frame->crop_left = 0;
553 frame->crop_right = 0;
555 frame->crop_bottom = 0;
560 int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
562 AVCodecInternal *avci = avctx->internal;
565 av_frame_unref(frame);
567 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
568 return AVERROR(EINVAL);
570 ret = bsfs_init(avctx);
574 if (avci->buffer_frame->buf[0]) {
575 av_frame_move_ref(frame, avci->buffer_frame);
577 ret = decode_receive_frame_internal(avctx, frame);
582 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
583 ret = apply_cropping(avctx, frame);
585 av_frame_unref(frame);
590 avctx->frame_number++;
595 static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
596 int *got_frame, AVPacket *pkt)
598 AVCodecInternal *avci = avctx->internal;
601 av_assert0(avci->compat_decode_consumed == 0);
604 avci->compat_decode = 1;
606 if (avci->compat_decode_partial_size > 0 &&
607 avci->compat_decode_partial_size != pkt->size) {
608 av_log(avctx, AV_LOG_ERROR,
609 "Got unexpected packet size after a partial decode\n");
610 ret = AVERROR(EINVAL);
614 if (!avci->compat_decode_partial_size) {
615 ret = avcodec_send_packet(avctx, pkt);
616 if (ret == AVERROR_EOF)
618 else if (ret == AVERROR(EAGAIN)) {
619 /* we fully drain all the output in each decode call, so this should not
628 ret = avcodec_receive_frame(avctx, frame);
630 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
635 if (frame != avci->compat_decode_frame) {
636 if (!avctx->refcounted_frames) {
637 ret = unrefcount_frame(avci, frame);
643 frame = avci->compat_decode_frame;
645 if (!avci->compat_decode_warned) {
646 av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
647 "API cannot return all the frames for this decoder. "
648 "Some frames will be dropped. Update your code to the "
649 "new decoding API to fix this.\n");
650 avci->compat_decode_warned = 1;
654 if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
660 /* if there are any bsfs then assume full packet is always consumed */
661 if (avctx->codec->bsfs)
664 ret = FFMIN(avci->compat_decode_consumed, pkt->size);
666 avci->compat_decode_consumed = 0;
667 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
672 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
673 int *got_picture_ptr,
676 return compat_decode(avctx, picture, got_picture_ptr, avpkt);
679 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
684 return compat_decode(avctx, frame, got_frame_ptr, avpkt);
687 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
693 ret = extract_packet_props(avctx->internal, avpkt);
698 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
700 avctx->frame_number++;
704 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
706 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
707 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
710 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
712 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
717 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
718 enum AVPixelFormat pix_fmt)
720 AVHWAccel *hwaccel = NULL;
722 while ((hwaccel = av_hwaccel_next(hwaccel)))
723 if (hwaccel->id == codec_id
724 && hwaccel->pix_fmt == pix_fmt)
729 static int setup_hwaccel(AVCodecContext *avctx,
730 const enum AVPixelFormat fmt,
733 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
737 av_log(avctx, AV_LOG_ERROR,
738 "Could not find an AVHWAccel for the pixel format: %s",
740 return AVERROR(ENOENT);
743 if (hwa->priv_data_size) {
744 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
745 if (!avctx->internal->hwaccel_priv_data)
746 return AVERROR(ENOMEM);
749 avctx->hwaccel = hwa;
751 ret = hwa->init(avctx);
753 av_freep(&avctx->internal->hwaccel_priv_data);
754 avctx->hwaccel = NULL;
762 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
764 const AVPixFmtDescriptor *desc;
765 enum AVPixelFormat *choices;
766 enum AVPixelFormat ret;
769 while (fmt[n] != AV_PIX_FMT_NONE)
773 avctx->sw_pix_fmt = fmt[n - 1];
774 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
776 choices = av_malloc_array(n + 1, sizeof(*choices));
778 return AV_PIX_FMT_NONE;
780 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
783 if (avctx->hwaccel && avctx->hwaccel->uninit)
784 avctx->hwaccel->uninit(avctx);
785 av_freep(&avctx->internal->hwaccel_priv_data);
786 avctx->hwaccel = NULL;
788 av_buffer_unref(&avctx->hw_frames_ctx);
790 ret = avctx->get_format(avctx, choices);
792 desc = av_pix_fmt_desc_get(ret);
794 ret = AV_PIX_FMT_NONE;
798 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
801 if (avctx->hw_frames_ctx) {
802 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
803 if (hw_frames_ctx->format != ret) {
804 av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
805 "does not match the format of provided AVHWFramesContext\n");
806 ret = AV_PIX_FMT_NONE;
811 if (!setup_hwaccel(avctx, ret, desc->name))
814 /* Remove failed hwaccel from choices */
815 for (n = 0; choices[n] != ret; n++)
816 av_assert0(choices[n] != AV_PIX_FMT_NONE);
819 choices[n] = choices[n + 1];
820 while (choices[n++] != AV_PIX_FMT_NONE);
827 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
829 FramePool *pool = avctx->internal->pool;
832 switch (avctx->codec_type) {
833 case AVMEDIA_TYPE_VIDEO: {
837 int w = frame->width;
838 int h = frame->height;
839 int tmpsize, unaligned;
841 if (pool->format == frame->format &&
842 pool->width == frame->width && pool->height == frame->height)
845 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
848 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
849 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
850 av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
851 // increase alignment of w for next try (rhs gives the lowest bit set in w)
855 for (i = 0; i < 4; i++)
856 unaligned |= linesize[i] % pool->stride_align[i];
859 tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
864 for (i = 0; i < 3 && data[i + 1]; i++)
865 size[i] = data[i + 1] - data[i];
866 size[i] = tmpsize - (data[i] - data[0]);
868 for (i = 0; i < 4; i++) {
869 av_buffer_pool_uninit(&pool->pools[i]);
870 pool->linesize[i] = linesize[i];
872 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
873 if (!pool->pools[i]) {
874 ret = AVERROR(ENOMEM);
879 pool->format = frame->format;
880 pool->width = frame->width;
881 pool->height = frame->height;
885 case AVMEDIA_TYPE_AUDIO: {
886 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
887 int planar = av_sample_fmt_is_planar(frame->format);
888 int planes = planar ? ch : 1;
890 if (pool->format == frame->format && pool->planes == planes &&
891 pool->channels == ch && frame->nb_samples == pool->samples)
894 av_buffer_pool_uninit(&pool->pools[0]);
895 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
896 frame->nb_samples, frame->format, 0);
900 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
901 if (!pool->pools[0]) {
902 ret = AVERROR(ENOMEM);
906 pool->format = frame->format;
907 pool->planes = planes;
909 pool->samples = frame->nb_samples;
912 default: av_assert0(0);
916 for (i = 0; i < 4; i++)
917 av_buffer_pool_uninit(&pool->pools[i]);
919 pool->planes = pool->channels = pool->samples = 0;
920 pool->width = pool->height = 0;
924 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
926 FramePool *pool = avctx->internal->pool;
927 int planes = pool->planes;
930 frame->linesize[0] = pool->linesize[0];
932 if (planes > AV_NUM_DATA_POINTERS) {
933 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
934 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
935 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
936 sizeof(*frame->extended_buf));
937 if (!frame->extended_data || !frame->extended_buf) {
938 av_freep(&frame->extended_data);
939 av_freep(&frame->extended_buf);
940 return AVERROR(ENOMEM);
943 frame->extended_data = frame->data;
945 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
946 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
949 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
951 for (i = 0; i < frame->nb_extended_buf; i++) {
952 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
953 if (!frame->extended_buf[i])
955 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
958 if (avctx->debug & FF_DEBUG_BUFFERS)
959 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
963 av_frame_unref(frame);
964 return AVERROR(ENOMEM);
967 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
969 FramePool *pool = s->internal->pool;
973 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
977 memset(pic->data, 0, sizeof(pic->data));
978 pic->extended_data = pic->data;
980 for (i = 0; i < 4 && pool->pools[i]; i++) {
981 pic->linesize[i] = pool->linesize[i];
983 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
987 pic->data[i] = pic->buf[i]->data;
989 for (; i < AV_NUM_DATA_POINTERS; i++) {
991 pic->linesize[i] = 0;
993 if (pic->data[1] && !pic->data[2])
994 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
996 if (s->debug & FF_DEBUG_BUFFERS)
997 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
1001 av_frame_unref(pic);
1002 return AVERROR(ENOMEM);
1005 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
1009 if (avctx->hw_frames_ctx)
1010 return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
1012 if ((ret = update_frame_pool(avctx, frame)) < 0)
1015 switch (avctx->codec_type) {
1016 case AVMEDIA_TYPE_VIDEO:
1017 return video_get_buffer(avctx, frame);
1018 case AVMEDIA_TYPE_AUDIO:
1019 return audio_get_buffer(avctx, frame);
1025 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
1027 AVPacket *pkt = avctx->internal->last_pkt_props;
1030 enum AVPacketSideDataType packet;
1031 enum AVFrameSideDataType frame;
1033 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
1034 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
1035 { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
1036 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
1037 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
1040 frame->color_primaries = avctx->color_primaries;
1041 frame->color_trc = avctx->color_trc;
1042 frame->colorspace = avctx->colorspace;
1043 frame->color_range = avctx->color_range;
1044 frame->chroma_location = avctx->chroma_sample_location;
1046 frame->reordered_opaque = avctx->reordered_opaque;
1049 FF_DISABLE_DEPRECATION_WARNINGS
1050 frame->pkt_pts = pkt->pts;
1051 FF_ENABLE_DEPRECATION_WARNINGS
1053 frame->pts = pkt->pts;
1055 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
1057 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
1059 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
1063 return AVERROR(ENOMEM);
1065 memcpy(frame_sd->data, packet_sd, size);
1072 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1074 const AVHWAccel *hwaccel = avctx->hwaccel;
1075 int override_dimensions = 1;
1078 switch (avctx->codec_type) {
1079 case AVMEDIA_TYPE_VIDEO:
1080 if (frame->width <= 0 || frame->height <= 0) {
1081 frame->width = FFMAX(avctx->width, avctx->coded_width);
1082 frame->height = FFMAX(avctx->height, avctx->coded_height);
1083 override_dimensions = 0;
1085 if (frame->format < 0)
1086 frame->format = avctx->pix_fmt;
1087 if (!frame->sample_aspect_ratio.num)
1088 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
1090 if (av_image_check_sar(frame->width, frame->height,
1091 frame->sample_aspect_ratio) < 0) {
1092 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1093 frame->sample_aspect_ratio.num,
1094 frame->sample_aspect_ratio.den);
1095 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
1098 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
1101 case AVMEDIA_TYPE_AUDIO:
1102 if (!frame->sample_rate)
1103 frame->sample_rate = avctx->sample_rate;
1104 if (frame->format < 0)
1105 frame->format = avctx->sample_fmt;
1106 if (!frame->channel_layout) {
1107 if (avctx->channel_layout) {
1108 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
1110 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
1111 "configuration.\n");
1112 return AVERROR(EINVAL);
1115 frame->channel_layout = avctx->channel_layout;
1117 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1118 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
1120 return AVERROR(ENOSYS);
1123 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
1124 if (!frame->channel_layout)
1125 frame->channel_layout = (1ULL << avctx->channels) - 1;
1129 default: return AVERROR(EINVAL);
1132 ret = ff_decode_frame_props(avctx, frame);
1137 if (hwaccel->alloc_frame) {
1138 ret = hwaccel->alloc_frame(avctx, frame);
1142 avctx->sw_pix_fmt = avctx->pix_fmt;
1144 ret = avctx->get_buffer2(avctx, frame, flags);
1147 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
1148 !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
1149 frame->width = avctx->width;
1150 frame->height = avctx->height;
1156 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1161 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1163 if (!frame->data[0])
1164 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1166 if (av_frame_is_writable(frame))
1167 return ff_decode_frame_props(avctx, frame);
1169 tmp = av_frame_alloc();
1171 return AVERROR(ENOMEM);
1173 av_frame_move_ref(tmp, frame);
1175 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1177 av_frame_free(&tmp);
1181 av_frame_copy(frame, tmp);
1182 av_frame_free(&tmp);
1187 void avcodec_flush_buffers(AVCodecContext *avctx)
1189 avctx->internal->draining = 0;
1190 avctx->internal->draining_done = 0;
1191 av_frame_unref(avctx->internal->buffer_frame);
1192 av_frame_unref(avctx->internal->compat_decode_frame);
1193 av_packet_unref(avctx->internal->buffer_pkt);
1194 avctx->internal->buffer_pkt_valid = 0;
1196 av_packet_unref(avctx->internal->ds.in_pkt);
1198 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1199 ff_thread_flush(avctx);
1200 else if (avctx->codec->flush)
1201 avctx->codec->flush(avctx);
1203 ff_decode_bsfs_uninit(avctx);
1205 if (!avctx->refcounted_frames)
1206 av_frame_unref(avctx->internal->to_free);
1209 void ff_decode_bsfs_uninit(AVCodecContext *avctx)
1211 DecodeFilterContext *s = &avctx->internal->filter;
1214 for (i = 0; i < s->nb_bsfs; i++)
1215 av_bsf_free(&s->bsfs[i]);