avcodec_receive_frame() 函数的主要功能是从解码队列中取出一帧
avcodec_receive_frame()的声明位于ffmpeg/libavcodec/avcodec.h , 如下:
/**
* Return decoded output data from a decoder.
*
* @param avctx codec context
* @param frame This will be set to a reference-counted video or audio
* frame (depending on the decoder type) allocated by the
* decoder. Note that the function will always call
* av_frame_unref(frame) before doing anything else.
*
* @return
* 0: success, a frame was returned
* AVERROR(EAGAIN): output is not available in this state - user must try
* to send new input
* AVERROR_EOF: the decoder has been fully flushed, and there will be
* no more output frames
* AVERROR(EINVAL): codec not opened, or it is an encoder
* AVERROR_INPUT_CHANGED: current decoded frame has changed parameters
* with respect to first decoded frame. Applicable
* when flag AV_CODEC_FLAG_DROPCHANGED is set.
* other negative values: legitimate decoding errors
*/
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);
返回错误码:
0 : success, 返回一帧
AVERROR(EAGAIN):输出在此状态下不可用, 用户必须重试发送新的输入
AVERROR_EOF: 解码器已完全刷新,不会有新的输出帧
AVERROR(EINVAL): 解码器没有被打开,或者是一个编码器AVERROR_INPUT_CHANGED: 当前解码帧已更改参数
avcodec_receive_frame()的定义位于ffmpeg/libavcodec/decode.c, 如下:
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
AVCodecInternal *avci = avctx->internal;
int ret, changed;
// 初始化输出帧
av_frame_unref(frame);
/解码器是否打开
//是否为解码器, 非0:是; 0:不是
if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
return AVERROR(EINVAL);
//如果解码器解码出了一帧,则会调用av_frame_move_ref输出这一帧,否则继续调用decode_receive_frame_internal继续进行解码
// 也就是说,如果存在已经解码的数据,则直接取出使用,否则就要调用decode_receive_frame_internal 自行解码
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;
}
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
ret = apply_cropping(avctx, frame);
if (ret < 0) {
av_frame_unref(frame);
return ret;
}
}
avctx->frame_number++;
if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
if (avctx->frame_number == 1) {
avci->initial_format = frame->format;
switch(avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO:
avci->initial_width = frame->width;
avci->initial_height = frame->height;
break;
case AVMEDIA_TYPE_AUDIO:
avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
avctx->sample_rate;
avci->initial_channels = frame->channels;
avci->initial_channel_layout = frame->channel_layout;
break;
}
}
if (avctx->frame_number > 1) {
changed = avci->initial_format != frame->format;
switch(avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO:
changed |= avci->initial_width != frame->width ||
avci->initial_height != frame->height;
break;
case AVMEDIA_TYPE_AUDIO:
changed |= avci->initial_sample_rate != frame->sample_rate ||
avci->initial_sample_rate != avctx->sample_rate ||
avci->initial_channels != frame->channels ||
avci->initial_channel_layout != frame->channel_layout;
break;
}
if (changed) {
avci->changed_frames_dropped++;
av_log(avctx, AV_LOG_INFO, "dropped changed frame #%d pts %"PRId64
" drop count: %d \n",
avctx->frame_number, frame->pts,
avci->changed_frames_dropped);
av_frame_unref(frame);
return AVERROR_INPUT_CHANGED;
}
}
}
return 0;
}