]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/decode.c
hevc: 16x16 NEON idct: Use the right element size for loads/stores
[ffmpeg] / libavcodec / decode.c
index b02278d3a68bc3959e9d1810c4fae8872dbf723b..8aa27095b63b931cff0643073e30364248a3fd7f 100644 (file)
 #include "config.h"
 
 #include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
 #include "libavutil/common.h"
 #include "libavutil/frame.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/intmath.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
+#include "decode.h"
 #include "internal.h"
 #include "thread.h"
 
@@ -152,269 +155,529 @@ static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
     return 0;
 }
 
-static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
+static int bsfs_init(AVCodecContext *avctx)
 {
-    int got_frame;
+    AVCodecInternal *avci = avctx->internal;
+    DecodeFilterContext *s = &avci->filter;
+    const char *bsfs_str;
     int ret;
 
-    av_assert0(!avctx->internal->buffer_frame->buf[0]);
+    if (s->nb_bsfs)
+        return 0;
+
+    bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
+    while (bsfs_str && *bsfs_str) {
+        AVBSFContext **tmp;
+        const AVBitStreamFilter *filter;
+        char *bsf;
 
-    if (!pkt)
-        pkt = avctx->internal->buffer_pkt;
+        bsf = av_get_token(&bsfs_str, ",");
+        if (!bsf) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
 
-    // This is the lesser evil. The field is for compatibility with legacy users
-    // of the legacy API, and users using the new API should not be forced to
-    // even know about this field.
-    avctx->refcounted_frames = 1;
+        filter = av_bsf_get_by_name(bsf);
+        if (!filter) {
+            av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
+                   "requested by a decoder. This is a bug, please report it.\n",
+                   bsf);
+            ret = AVERROR_BUG;
+            av_freep(&bsf);
+            goto fail;
+        }
+        av_freep(&bsf);
+
+        tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
+        if (!tmp) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+        s->bsfs = tmp;
+        s->nb_bsfs++;
+
+        ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
+        if (ret < 0)
+            goto fail;
+
+        if (s->nb_bsfs == 1) {
+            /* We do not currently have an API for passing the input timebase into decoders,
+             * but no filters used here should actually need it.
+             * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
+            s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
+            ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
+                                                  avctx);
+        } else {
+            s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
+            ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
+                                          s->bsfs[s->nb_bsfs - 2]->par_out);
+        }
+        if (ret < 0)
+            goto fail;
+
+        ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
+        if (ret < 0)
+            goto fail;
+    }
+
+    return 0;
+fail:
+    ff_decode_bsfs_uninit(avctx);
+    return ret;
+}
+
+/* try to get one output packet from the filter chain */
+static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
+{
+    DecodeFilterContext *s = &avctx->internal->filter;
+    int idx, ret;
+
+    /* start with the last filter in the chain */
+    idx = s->nb_bsfs - 1;
+    while (idx >= 0) {
+        /* request a packet from the currently selected filter */
+        ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
+        if (ret == AVERROR(EAGAIN)) {
+            /* no packets available, try the next filter up the chain */
+            ret = 0;
+            idx--;
+            continue;
+        } else if (ret < 0 && ret != AVERROR_EOF) {
+            return ret;
+        }
+
+        /* got a packet or EOF -- pass it to the caller or to the next filter
+         * down the chain */
+        if (idx == s->nb_bsfs - 1) {
+            return ret;
+        } else {
+            idx++;
+            ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
+            if (ret < 0) {
+                av_log(avctx, AV_LOG_ERROR,
+                       "Error pre-processing a packet before decoding\n");
+                av_packet_unref(pkt);
+                return ret;
+            }
+        }
+    }
+
+    return AVERROR(EAGAIN);
+}
+
+int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
+{
+    AVCodecInternal *avci = avctx->internal;
+    int ret;
+
+    if (avci->draining)
+        return AVERROR_EOF;
+
+    ret = bsfs_poll(avctx, pkt);
+    if (ret == AVERROR_EOF)
+        avci->draining = 1;
+    if (ret < 0)
+        return ret;
+
+    ret = extract_packet_props(avctx->internal, pkt);
+    if (ret < 0)
+        goto finish;
+
+    ret = apply_param_change(avctx, pkt);
+    if (ret < 0)
+        goto finish;
+
+    if (avctx->codec->receive_frame)
+        avci->compat_decode_consumed += pkt->size;
+
+    return 0;
+finish:
+    av_packet_unref(pkt);
+    return ret;
+}
+
+/*
+ * The core of the receive_frame_wrapper for the decoders implementing
+ * the simple API. Certain decoders might consume partial packets without
+ * returning any output, so this function needs to be called in a loop until it
+ * returns EAGAIN.
+ **/
+static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
+{
+    AVCodecInternal   *avci = avctx->internal;
+    DecodeSimpleContext *ds = &avci->ds;
+    AVPacket           *pkt = ds->in_pkt;
+    int got_frame;
+    int ret;
+
+    if (!pkt->data && !avci->draining) {
+        av_packet_unref(pkt);
+        ret = ff_decode_get_packet(avctx, pkt);
+        if (ret < 0 && ret != AVERROR_EOF)
+            return ret;
+    }
 
     // Some codecs (at least wma lossless) will crash when feeding drain packets
     // after EOF was signaled.
-    if (avctx->internal->draining_done)
+    if (avci->draining_done)
         return AVERROR_EOF;
 
-    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
-        ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
-                                    &got_frame, pkt);
-        if (ret >= 0)
-            ret = pkt->size;
-    } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
-        ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
-                                    &got_frame, pkt);
+    if (!pkt->data &&
+        !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
+          avctx->active_thread_type & FF_THREAD_FRAME))
+        return AVERROR_EOF;
+
+    got_frame = 0;
+
+    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
+        ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
     } else {
-        ret = AVERROR(EINVAL);
+        ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
+
+        if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
+            frame->pkt_dts = pkt->dts;
+        /* get_buffer is supposed to set frame parameters */
+        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
+            frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
+            frame->width               = avctx->width;
+            frame->height              = avctx->height;
+            frame->format              = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
+                                         avctx->pix_fmt : avctx->sample_fmt;
+        }
     }
 
-    if (ret < 0)
-        return ret;
+    emms_c();
+
+    if (!got_frame)
+        av_frame_unref(frame);
+
+    if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
+        ret = pkt->size;
 
     if (avctx->internal->draining && !got_frame)
-        avctx->internal->draining_done = 1;
+        avci->draining_done = 1;
 
-    if (ret >= pkt->size) {
-        av_packet_unref(avctx->internal->buffer_pkt);
+    avci->compat_decode_consumed += ret;
+
+    if (ret >= pkt->size || ret < 0) {
+        av_packet_unref(pkt);
     } else {
         int consumed = ret;
 
-        if (pkt != avctx->internal->buffer_pkt) {
-            av_packet_unref(avctx->internal->buffer_pkt);
-            if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
-                return ret;
-        }
-
-        avctx->internal->buffer_pkt->data += consumed;
-        avctx->internal->buffer_pkt->size -= consumed;
-        avctx->internal->buffer_pkt->pts   = AV_NOPTS_VALUE;
-        avctx->internal->buffer_pkt->dts   = AV_NOPTS_VALUE;
+        pkt->data                += consumed;
+        pkt->size                -= consumed;
+        pkt->pts                  = AV_NOPTS_VALUE;
+        pkt->dts                  = AV_NOPTS_VALUE;
+        avci->last_pkt_props->pts = AV_NOPTS_VALUE;
+        avci->last_pkt_props->dts = AV_NOPTS_VALUE;
     }
 
     if (got_frame)
-        av_assert0(avctx->internal->buffer_frame->buf[0]);
+        av_assert0(frame->buf[0]);
+
+    return ret < 0 ? ret : 0;
+}
+
+static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+{
+    int ret;
+
+    while (!frame->buf[0]) {
+        ret = decode_simple_internal(avctx, frame);
+        if (ret < 0)
+            return ret;
+    }
 
     return 0;
 }
 
-int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
+static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 {
+    AVCodecInternal *avci = avctx->internal;
     int ret;
 
+    av_assert0(!frame->buf[0]);
+
+    if (avctx->codec->receive_frame)
+        ret = avctx->codec->receive_frame(avctx, frame);
+    else
+        ret = decode_simple_receive_frame(avctx, frame);
+
+    if (ret == AVERROR_EOF)
+        avci->draining_done = 1;
+
+    return ret;
+}
+
+int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
+{
+    AVCodecInternal *avci = avctx->internal;
+    int ret = 0;
+
     if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
         return AVERROR(EINVAL);
 
     if (avctx->internal->draining)
         return AVERROR_EOF;
 
-    if (!avpkt || !avpkt->size) {
-        avctx->internal->draining = 1;
-        avpkt = NULL;
+    ret = bsfs_init(avctx);
+    if (ret < 0)
+        return ret;
 
-        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
-            return 0;
+    av_packet_unref(avci->buffer_pkt);
+    if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
+        ret = av_packet_ref(avci->buffer_pkt, avpkt);
+        if (ret < 0)
+            return ret;
     }
 
-    if (avctx->codec->send_packet) {
-        if (avpkt) {
-            ret = apply_param_change(avctx, (AVPacket *)avpkt);
-            if (ret < 0)
-                return ret;
-        }
-        return avctx->codec->send_packet(avctx, avpkt);
+    ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
+    if (ret < 0) {
+        av_packet_unref(avci->buffer_pkt);
+        return ret;
+    }
+
+    if (!avci->buffer_frame->buf[0]) {
+        ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
+        if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
+            return ret;
     }
 
-    // Emulation via old API. Assume avpkt is likely not refcounted, while
-    // decoder output is always refcounted, and avoid copying.
+    return 0;
+}
 
-    if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
-        return AVERROR(EAGAIN);
+static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
+                                 const AVPixFmtDescriptor *desc)
+{
+    int i, j;
 
-    // The goal is decoding the first frame of the packet without using memcpy,
-    // because the common case is having only 1 frame per packet (especially
-    // with video, but audio too). In other cases, it can't be avoided, unless
-    // the user is feeding refcounted packets.
-    return do_decode(avctx, (AVPacket *)avpkt);
+    for (i = 0; frame->data[i]; i++) {
+        const AVComponentDescriptor *comp = NULL;
+        int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
+        int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
+
+        if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
+            offsets[i] = 0;
+            break;
+        }
+
+        /* find any component descriptor for this plane */
+        for (j = 0; j < desc->nb_components; j++) {
+            if (desc->comp[j].plane == i) {
+                comp = &desc->comp[j];
+                break;
+            }
+        }
+        if (!comp)
+            return AVERROR_BUG;
+
+        offsets[i] = (frame->crop_top  >> shift_y) * frame->linesize[i] +
+                     (frame->crop_left >> shift_x) * comp->step;
+    }
+
+    return 0;
 }
 
-int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
 {
-    int ret;
+    const AVPixFmtDescriptor *desc;
+    size_t offsets[4];
+    int i;
 
-    av_frame_unref(frame);
+    /* make sure we are noisy about decoders returning invalid cropping data */
+    if (frame->crop_left >= INT_MAX - frame->crop_right        ||
+        frame->crop_top  >= INT_MAX - frame->crop_bottom       ||
+        (frame->crop_left + frame->crop_right) >= frame->width ||
+        (frame->crop_top + frame->crop_bottom) >= frame->height) {
+        av_log(avctx, AV_LOG_WARNING,
+               "Invalid cropping information set by a decoder: %zu/%zu/%zu/%zu "
+               "(frame size %dx%d). This is a bug, please report it\n",
+               frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
+               frame->width, frame->height);
+        frame->crop_left   = 0;
+        frame->crop_right  = 0;
+        frame->crop_top    = 0;
+        frame->crop_bottom = 0;
+        return 0;
+    }
 
-    if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
-        return AVERROR(EINVAL);
+    if (!avctx->apply_cropping)
+        return 0;
 
-    if (avctx->codec->receive_frame) {
-        if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
-            return AVERROR_EOF;
-        return avctx->codec->receive_frame(avctx, frame);
+    desc = av_pix_fmt_desc_get(frame->format);
+    if (!desc)
+        return AVERROR_BUG;
+
+    /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
+     * formats cannot be easily handled here either (and corresponding decoders
+     * should not export any cropping anyway), so do the same for those as well.
+     * */
+    if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
+        frame->width      -= frame->crop_right;
+        frame->height     -= frame->crop_bottom;
+        frame->crop_right  = 0;
+        frame->crop_bottom = 0;
+        return 0;
     }
 
-    // Emulation via old API.
+    /* calculate the offsets for each plane */
+    calc_cropping_offsets(offsets, frame, desc);
 
-    if (!avctx->internal->buffer_frame->buf[0]) {
-        if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
-            return AVERROR(EAGAIN);
+    /* adjust the offsets to avoid breaking alignment */
+    if (!(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
+        int min_log2_align = INT_MAX;
 
-        while (1) {
-            if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
-                av_packet_unref(avctx->internal->buffer_pkt);
-                return ret;
-            }
-            // Some audio decoders may consume partial data without returning
-            // a frame (fate-wmapro-2ch). There is no way to make the caller
-            // call avcodec_receive_frame() again without returning a frame,
-            // so try to decode more in these cases.
-            if (avctx->internal->buffer_frame->buf[0] ||
-                !avctx->internal->buffer_pkt->size)
-                break;
+        for (i = 0; frame->data[i]; i++) {
+            int log2_align = offsets[i] ? av_ctz(offsets[i]) : INT_MAX;
+            min_log2_align = FFMIN(log2_align, min_log2_align);
+        }
+
+        if (min_log2_align < 5) {
+            frame->crop_left &= ~((1 << min_log2_align) - 1);
+            calc_cropping_offsets(offsets, frame, desc);
         }
     }
 
-    if (!avctx->internal->buffer_frame->buf[0])
-        return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
+    for (i = 0; frame->data[i]; i++)
+        frame->data[i] += offsets[i];
+
+    frame->width      -= (frame->crop_left + frame->crop_right);
+    frame->height     -= (frame->crop_top  + frame->crop_bottom);
+    frame->crop_left   = 0;
+    frame->crop_right  = 0;
+    frame->crop_top    = 0;
+    frame->crop_bottom = 0;
 
-    av_frame_move_ref(frame, avctx->internal->buffer_frame);
     return 0;
 }
 
-int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
-                                              int *got_picture_ptr,
-                                              AVPacket *avpkt)
+int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 {
     AVCodecInternal *avci = avctx->internal;
     int ret;
 
-    *got_picture_ptr = 0;
-    if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
-        return -1;
+    av_frame_unref(frame);
 
-    if (!avctx->codec->decode) {
-        av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
-        return AVERROR(ENOSYS);
-    }
+    if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
+        return AVERROR(EINVAL);
 
-    ret = extract_packet_props(avci, avpkt);
+    ret = bsfs_init(avctx);
     if (ret < 0)
         return ret;
 
-    ret = apply_param_change(avctx, avpkt);
-    if (ret < 0)
-        return ret;
+    if (avci->buffer_frame->buf[0]) {
+        av_frame_move_ref(frame, avci->buffer_frame);
+    } else {
+        ret = decode_receive_frame_internal(avctx, frame);
+        if (ret < 0)
+            return ret;
+    }
 
-    av_frame_unref(picture);
-
-    if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
-        (avctx->active_thread_type & FF_THREAD_FRAME)) {
-        if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
-            ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
-                                         avpkt);
-        else {
-            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
-                                       avpkt);
-            if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
-                picture->pkt_dts = avpkt->dts;
-            /* get_buffer is supposed to set frame parameters */
-            if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
-                picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
-                picture->width               = avctx->width;
-                picture->height              = avctx->height;
-                picture->format              = avctx->pix_fmt;
-            }
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        ret = apply_cropping(avctx, frame);
+        if (ret < 0) {
+            av_frame_unref(frame);
+            return ret;
         }
+    }
 
-        emms_c(); //needed to avoid an emms_c() call before every return;
-
-        if (*got_picture_ptr) {
-            if (!avctx->refcounted_frames) {
-                int err = unrefcount_frame(avci, picture);
-                if (err < 0)
-                    return err;
-            }
-
-            avctx->frame_number++;
-        } else
-            av_frame_unref(picture);
-    } else
-        ret = 0;
-
-#if FF_API_AVCTX_TIMEBASE
-    if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
-        avctx->time_base = av_inv_q(avctx->framerate);
-#endif
+    avctx->frame_number++;
 
-    return ret;
+    return 0;
 }
 
-int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
-                                              AVFrame *frame,
-                                              int *got_frame_ptr,
-                                              AVPacket *avpkt)
+static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
+                         int *got_frame, AVPacket *pkt)
 {
     AVCodecInternal *avci = avctx->internal;
     int ret = 0;
 
-    *got_frame_ptr = 0;
+    av_assert0(avci->compat_decode_consumed == 0);
 
-    if (!avctx->codec->decode) {
-        av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
-        return AVERROR(ENOSYS);
-    }
-
-    ret = extract_packet_props(avci, avpkt);
-    if (ret < 0)
-        return ret;
+    *got_frame = 0;
+    avci->compat_decode = 1;
 
-    if (!avpkt->data && avpkt->size) {
-        av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
-        return AVERROR(EINVAL);
+    if (avci->compat_decode_partial_size > 0 &&
+        avci->compat_decode_partial_size != pkt->size) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Got unexpected packet size after a partial decode\n");
+        ret = AVERROR(EINVAL);
+        goto finish;
     }
 
-    ret = apply_param_change(avctx, avpkt);
-    if (ret < 0)
-        return ret;
-
-    av_frame_unref(frame);
+    if (!avci->compat_decode_partial_size) {
+        ret = avcodec_send_packet(avctx, pkt);
+        if (ret == AVERROR_EOF)
+            ret = 0;
+        else if (ret == AVERROR(EAGAIN)) {
+            /* we fully drain all the output in each decode call, so this should not
+             * ever happen */
+            ret = AVERROR_BUG;
+            goto finish;
+        } else if (ret < 0)
+            goto finish;
+    }
 
-    if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
-        ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
-        if (ret >= 0 && *got_frame_ptr) {
-            avctx->frame_number++;
-            frame->pkt_dts = avpkt->dts;
-            if (frame->format == AV_SAMPLE_FMT_NONE)
-                frame->format = avctx->sample_fmt;
+    while (ret >= 0) {
+        ret = avcodec_receive_frame(avctx, frame);
+        if (ret < 0) {
+            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+                ret = 0;
+            goto finish;
+        }
 
+        if (frame != avci->compat_decode_frame) {
             if (!avctx->refcounted_frames) {
-                int err = unrefcount_frame(avci, frame);
-                if (err < 0)
-                    return err;
+                ret = unrefcount_frame(avci, frame);
+                if (ret < 0)
+                    goto finish;
             }
-        } else
-            av_frame_unref(frame);
+
+            *got_frame = 1;
+            frame = avci->compat_decode_frame;
+        } else {
+            if (!avci->compat_decode_warned) {
+                av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
+                       "API cannot return all the frames for this decoder. "
+                       "Some frames will be dropped. Update your code to the "
+                       "new decoding API to fix this.\n");
+                avci->compat_decode_warned = 1;
+            }
+        }
+
+        if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
+            break;
     }
 
+finish:
+    if (ret == 0) {
+        /* if there are any bsfs then assume full packet is always consumed */
+        if (avctx->codec->bsfs)
+            ret = pkt->size;
+        else
+            ret = FFMIN(avci->compat_decode_consumed, pkt->size);
+    }
+    avci->compat_decode_consumed = 0;
+    avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
 
     return ret;
 }
 
+int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
+                                              int *got_picture_ptr,
+                                              AVPacket *avpkt)
+{
+    return compat_decode(avctx, picture, got_picture_ptr, avpkt);
+}
+
+int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
+                                              AVFrame *frame,
+                                              int *got_frame_ptr,
+                                              AVPacket *avpkt)
+{
+    return compat_decode(avctx, frame, got_frame_ptr, avpkt);
+}
+
 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
                              int *got_sub_ptr,
                              AVPacket *avpkt)
@@ -875,7 +1138,8 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
     ret = avctx->get_buffer2(avctx, frame, flags);
 
 end:
-    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
+        !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
         frame->width  = avctx->width;
         frame->height = avctx->height;
     }
@@ -919,14 +1183,30 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
     avctx->internal->draining      = 0;
     avctx->internal->draining_done = 0;
     av_frame_unref(avctx->internal->buffer_frame);
+    av_frame_unref(avctx->internal->compat_decode_frame);
     av_packet_unref(avctx->internal->buffer_pkt);
     avctx->internal->buffer_pkt_valid = 0;
 
+    av_packet_unref(avctx->internal->ds.in_pkt);
+
     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
         ff_thread_flush(avctx);
     else if (avctx->codec->flush)
         avctx->codec->flush(avctx);
 
+    ff_decode_bsfs_uninit(avctx);
+
     if (!avctx->refcounted_frames)
         av_frame_unref(avctx->internal->to_free);
 }
+
+void ff_decode_bsfs_uninit(AVCodecContext *avctx)
+{
+    DecodeFilterContext *s = &avctx->internal->filter;
+    int i;
+
+    for (i = 0; i < s->nb_bsfs; i++)
+        av_bsf_free(&s->bsfs[i]);
+    av_freep(&s->bsfs);
+    s->nb_bsfs = 0;
+}