intattribute_align_argavcodec_decode_video2(AVCodecContext*avctx,AVFrame*picture,int*got_picture_ptr,constAVPacket*avpkt){...//检测输入参数if(!avctx->codec)returnAVERROR(EINVAL);if(avctx->codec->type!=AVMEDIA_TYPE_VIDEO){av_log(avctx,AV_LOG_ERROR,"Invalid media type for video\n");returnAVERROR(EINVAL);}*got_picture_ptr=0;if((avctx->coded_width||avctx->coded_height)&&av_image_check_size(avctx->coded_width,avctx->coded_height,0,avctx))returnAVERROR(EINVAL);...//真正的解码ret=avctx->codec->decode(avctx,picture,got_picture_ptr,&tmp);...//设置参数if(!(avctx->codec->capabilities&AV_CODEC_CAP_DR1)){if(!picture->sample_aspect_ratio.num)picture->sample_aspect_ratio=avctx->sample_aspect_ratio;if(!picture->width)picture->width=avctx->width;if(!picture->height)picture->height=avctx->height;if(picture->format==AV_PIX_FMT_NONE)picture->format=avctx->pix_fmt;}...}
以H.265解码器为例,解码示例如下:
ff_hevc_decoder
12345678910111213
AVCodecff_hevc_decoder={.name="hevc",.long_name=NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),.type=AVMEDIA_TYPE_VIDEO,.id=AV_CODEC_ID_HEVC,.priv_data_size=sizeof(HEVCContext),.priv_class=&hevc_decoder_class,.init=hevc_decode_init,.close=hevc_decode_free,.decode=hevc_decode_frame,.flush=hevc_decode_flush,...};
staticinthevc_decode_frame(AVCodecContext*avctx,void*data,int*got_output,AVPacket*avpkt){intret;HEVCContext*s=avctx->priv_data;if(!avpkt->size){ret=ff_hevc_output_frame(s,data,1);if(ret<0)returnret;*got_output=ret;return0;}s->ref=NULL;ret=decode_nal_units(s,avpkt->data,avpkt->size);if(ret<0)returnret;if(avctx->hwaccel){if(s->ref&&(ret=avctx->hwaccel->end_frame(avctx))<0){av_log(avctx,AV_LOG_ERROR,"hardware accelerator failed to decode picture\n");ff_hevc_unref_frame(s,s->ref,~0);returnret;}}else{/* verify the SEI checksum */if(avctx->err_recognition&AV_EF_CRCCHECK&&s->is_decoded&&s->is_md5){ret=verify_md5(s,s->ref->frame);if(ret<0&&avctx->err_recognition&AV_EF_EXPLODE){ff_hevc_unref_frame(s,s->ref,~0);returnret;}}}s->is_md5=0;if(s->is_decoded){av_log(avctx,AV_LOG_DEBUG,"Decoded frame with POC %d.\n",s->poc);s->is_decoded=0;}if(s->output_frame->buf[0]){av_frame_move_ref(data,s->output_frame);*got_output=1;}returnavpkt->size;}