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 的原始碼實現:
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() 完成的。
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。
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 置為空。
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 解碼。
以 H264 格式為例,對應的解碼器為 ff_h264_decoder:
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 個步驟:
- 判斷當前是否已到達檔案的末尾,如果是,則返回解碼後依然存在於快取中的資料
- 否則根據 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 的型別進行解碼。