FFmpeg 源碼分析 - avcodec_send_packet 和 avcodec_receive_frame

使用

從 FFmpeg 3.x 開始,avcodec_decode_video2 就被廢棄了,取而代之的是 avcodec_send_packet 和 avcodec_receive_frame。使用方法很簡單,能夠查看 ffmpeg 源碼文件夾 ffmpeg-/doc/example/decode_video.c 下的代碼,這裏摘抄關鍵部分以下:緩存

static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
                   const char *filename)
{
    char buf[1024];
    int ret;

    ret = avcodec_send_packet(dec_ctx, pkt);
    if (ret < 0) {
        fprintf(stderr, "Error sending a packet for decoding\n");
        exit(1);
    }

    while (ret >= 0) {
        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr, "Error during decoding\n");
            exit(1);
        }

        printf("saving frame %3d\n", dec_ctx->frame_number);
    }
}
複製代碼

avcodec_decode_video2

在開始分析 avcodec_send_packet 和 avcodec_receive_frame 以前,先看一下 avcodec_decode_video2 的源碼實現:bash

int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
                                              int *got_picture_ptr,
                                              const AVPacket *avpkt)
{
    return compat_decode(avctx, picture, got_picture_ptr, avpkt);
}
複製代碼
static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
                         int *got_frame, const AVPacket *pkt)
{
    AVCodecInternal *avci = avctx->internal;
    int ret = 0;

    av_assert0(avci->compat_decode_consumed == 0);

    if (avci->draining_done && pkt && pkt->size != 0) {
        av_log(avctx, AV_LOG_WARNING, "Got unexpected packet after EOF\n");
        avcodec_flush_buffers(avctx);
    }

    *got_frame = 0;
    avci->compat_decode = 1;

    ... // 容錯處理

    if (!avci->compat_decode_partial_size) {
        ret = avcodec_send_packet(avctx, pkt);
        ...
    }

    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) {
                ret = unrefcount_frame(avci, frame);
                if (ret < 0)
                    goto finish;
            }

            *got_frame = 1;
            frame = avci->compat_decode_frame;
        } else {
            ...
        }

        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;
}
複製代碼

能夠看到,avcodec_decode_video2 這個過期的函數,在新版本中最終仍是經過 avcodec_send_packet() 和 avcodec_receive_frame() 完成的。app

avcodec_send_packet

下面看 avcodec_send_packet,關鍵的地方寫了註釋異步

int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
{
    AVCodecInternal *avci = avctx->internal;
    int ret;

    // 檢查 AVCodecContext 是否已打開,而且 AVCodec 是否爲解碼器
    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 && avpkt->data)
        return AVERROR(EINVAL);

    // 初始化 avci 的 DecodeFilterContext 等成員
    ret = bsfs_init(avctx);
    if (ret < 0)
        return ret;

    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;
    }

    // 把 AVPacket 的數據傳給 avci->filter.bsfs[0]
    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;
    }

    return 0;
}
複製代碼

能夠看到,avcodec_send_packet 的關鍵是 decode_receive_frame_internal 這個函數。DecodeFilterContext、av_bsf_send_packet 等結構體或函數主要用於存儲 AVPacket。ide

av_bsf_send_packet

av_bsf_send_packet 的邏輯很簡單,定義以下:函數

int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
{
    if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
        ctx->internal->eof = 1;
        return 0;
    }

    if (ctx->internal->eof) {
        av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
        return AVERROR(EINVAL);
    }

    if (ctx->internal->buffer_pkt->data ||
        ctx->internal->buffer_pkt->side_data_elems)
        return AVERROR(EAGAIN);

    av_packet_move_ref(ctx->internal->buffer_pkt, pkt);

    return 0;
}
複製代碼
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
{
    *dst = *src;
    av_init_packet(src);
    src->data = NULL;
    src->size = 0;
}
複製代碼

能夠看到,av_bsf_send_packet 的做用是把傳過來的 AVPacket 放到指定的 AVBSFContext 內部,並將該 AVPacket 置爲空。ui

decode_receive_frame_internal

decode_receive_frame_internal 是解碼的關鍵函數,定義以下:編碼

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;
}
複製代碼

能夠看到,decode_receive_frame_internal 首先會判斷 AVCodec 是否存在 receive_frame 這個函數指針,若是存在,就使用該指針對應的函數實現進行解碼,不然調用 decode_simple_receive_frame 解碼。spa

以 H264 格式爲例,對應的解碼器爲 ff_h264_decoder:3d

AVCodec ff_h264_decoder = {
    .name                  = "h264",
    .long_name             = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
    .type                  = AVMEDIA_TYPE_VIDEO,
    .id                    = AV_CODEC_ID_H264,
    .priv_data_size        = sizeof(H264Context),
    .init                  = h264_decode_init,
    .close                 = h264_decode_end,
    .decode                = h264_decode_frame,
    .capabilities          = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
                             AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
                             AV_CODEC_CAP_FRAME_THREADS,
    .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
    .flush                 = flush_dpb,
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
    .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
    .profiles              = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
    .priv_class            = &h264_class,
};
複製代碼

沒有發現函數指針 receive_frame,所以這裏直接看 decode_simple_receive_frame:

static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
    int ret;

    // 某些解碼器可能會消耗部分數據包而不返回任何輸出,所以須要在循環中調用此函數
    // 直到它返回EAGAIN
    while (!frame->buf[0]) {
        ret = decode_simple_internal(avctx, frame);
        if (ret < 0)
            return ret;
    }

    return 0;
}
複製代碼

能夠看到,它直接調用 decode_simple_internal 這個函數:

/*
* The core of the receive_frame_wrapper for the decoders implementing
* the simple API.
* 某些解碼器可能會消耗部分數據包而不返回任何輸出,所以須要在循環中調用此函數,直到它返回EAGAIN
**/
static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
{
    AVCodecInternal   *avci = avctx->internal;
    DecodeSimpleContext *ds = &avci->ds;
    AVPacket           *pkt = ds->in_pkt;
    // copy to ensure we do not change pkt
    AVPacket tmp;
    int got_frame, actual_got_frame, did_split;
    int ret;

    if (!pkt->data && !avci->draining) {
        av_packet_unref(pkt);
        // 獲取在執行 av_bsf_send_packet 時緩存的  AVPacket
        ret = ff_decode_get_packet(avctx, pkt);
        if (ret < 0 && ret != AVERROR_EOF)
            return ret;
    }

    ...

    got_frame = 0;

    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
        // 獲取異步解碼緩存的 AVFrame
        ret = ff_thread_decode_frame(avctx, frame, &got_frame, &tmp);
    } else {
        // 解碼的關鍵函數
        ret = avctx->codec->decode(avctx, frame, &got_frame, &tmp);

        ...
    }
    
    ... // 設置 AVFrame 的成員變量,如 best_effort_timestamp、format、channel 等

    avci->compat_decode_consumed += ret;

    // 若是這個 AVPacket 已被消耗完,則釋放內存,不然調整指針、成員變量
    if (ret >= pkt->size || ret < 0) {
        av_packet_unref(pkt);
    } else {
        int consumed = ret;

        pkt->data                += consumed;
        pkt->size                -= consumed;
        avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
        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(frame->buf[0]);

    return ret < 0 ? ret : 0;
}
複製代碼

這個函數其實很長,但最關鍵的只有 avctx->codec->decode 這一句代碼,decode 是 結構體 AVCodec 的函數指針,不一樣的編碼格式對應不一樣的函數實現,以 H264 爲例,對應的解碼器爲 ff_h264_decoder (上面把它的聲明貼出來了),函數實現是 h264_decode_frame。

h264_decode_frame

這裏簡單看一下 h264_decode_frame:

static int h264_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame, AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size       = avpkt->size;
    H264Context *h     = avctx->priv_data;
    AVFrame *pict      = data;
    int buf_index;
    int ret;

    h->flags = avctx->flags;
    h->setup_finished = 0;
    h->nb_slice_ctx_queued = 0;

    ff_h264_unref_picture(h, &h->last_pic_for_ec);

    /* end of stream, output what is still in the buffers */
    // 直接返回依然在緩存中的數據
    if (buf_size == 0)
        return send_next_delayed_frame(h, pict, got_frame, 0);

    if (h->is_avc && av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
        int side_size;
        uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
        // 解碼 SPS、PPS
        if (is_extra(side, side_size))
            ff_h264_decode_extradata(side, side_size,
                                     &h->ps, &h->is_avc, &h->nal_length_size,
                                     avctx->err_recognition, avctx);
    }
    if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
        if (is_extra(buf, buf_size))
            return ff_h264_decode_extradata(buf, buf_size,
                                            &h->ps, &h->is_avc, &h->nal_length_size,
                                            avctx->err_recognition, avctx);
    }

	// 解碼 NAL Unit
    buf_index = decode_nal_units(h, buf, buf_size);
    if (buf_index < 0)
        return AVERROR_INVALIDDATA;

    ... 

    av_assert0(pict->buf[0] || !*got_frame);

    ff_h264_unref_picture(h, &h->last_pic_for_ec);

    return get_consumed_bytes(buf_index, buf_size);
}
複製代碼

能夠看到,這個函數大體能夠分爲 2 個步驟:

  1. 判斷當前是否已到達文件的末尾,若是是,則返回解碼後依然存在於緩存中的數據
  2. 不然根據 NAL 的類型進行解碼,若是是 SPS、PPS 數據,則調用 ff_h264_decode_extradata,不然調用 decode_nal_units

其中函數 is_extra 用於判斷數據類型是否爲 SPS、PPS:

static int is_extra(const uint8_t *buf, int buf_size)
{
    int cnt= buf[5]&0x1f;
    const uint8_t *p= buf+6;
    while(cnt--){
        int nalsize= AV_RB16(p) + 2;
        if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
            return 0;
        p += nalsize;
    }
    cnt = *(p++);
    if(!cnt)
        return 0;
    while(cnt--){
        int nalsize= AV_RB16(p) + 2;
        if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
            return 0;
        p += nalsize;
    }
    return 1;
}
複製代碼

從代碼中能夠看出,這個函數判在進行斷的時候,有兩個很關鍵的數字爲 7 和 8,它們分別表示該 NAL Unit 的類型爲 SPS、PPS。

ff_h264_decode_extradata 和 decode_nal_units 這兩個函數繼續分析下去能夠發現不少 H264 相關的知識,這裏就不繼續了,有興趣的能夠自行研究。

avcodec_receive_frame

int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
    AVCodecInternal *avci = avctx->internal;
    int ret;

    av_frame_unref(frame);

    if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
        return AVERROR(EINVAL);

    ret = bsfs_init(avctx);
    if (ret < 0)
        return ret;

    // 若是存在緩存,則直接獲取
    // 這個條件判斷在函數 avcodec_send_packet 裏也有
    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;
    }

    ...

    avctx->frame_number++;

    return 0;
}
複製代碼

能夠看到,這個函數的邏輯很簡單,它首先會嘗試從緩存中直接獲取 AVFrame,若是不存在,則調用 decode_receive_frame_internal 進行解碼。

總結

函數 avcodec_decode_video2 已通過時了,如今推薦使用的是 avcodec_send_packet() 和 avcodec_receive_frame(),而在新版本中,avcodec_decode_video2 也是經過這兩個新的 API 完成解碼功能的。

avcodec_send_packet 和 avcodec_receive_frame 內部的關鍵實現都是 decode_receive_frame_internal,最終調用的是 AVCodec 的兩個函數指針之一:receive_frame 或 decode,但目前 ff_h264_decoder、ff_aac_decoder 等解碼器依然只實現了 decode 這個函數。即 avcodec_send_packet 和 avcodec_receive_frame 最終調用的是 AVCodec 的函數指針 decode,對應不一樣的編碼格式,decode 有不一樣的實現,以 ff_h264_decoder 爲例,decode 對應的是 h264_decode_frame,它會根據 NAL Unit 的類型進行解碼。

相關文章
相關標籤/搜索