【FFmpeg程式設計進階】(15)FLV 編碼器Codec初始化 ff_mpv_encode_init() 程式碼分析 -- 待更新
【FFmpeg程式設計進階】(15)FLV 編碼器Codec初始化 ff_mpv_encode_init 程式碼分析
在前面分析《【FFmpeg程式設計進階】(11)avcodec_open2() 函式程式碼分析 - 開啟Codec解碼器》時,
我們涉及到了Codec 編碼器初始化,由於程式碼比較長,我們再分一章來分析。
主要目的,還是以 FLV 編碼器為例 ,來看下編碼器初始化主要做了啥
先記錄下TODO LIST,後面再更新。
# libavcodec\flvenc.c
AVCodec ff_flv_encoder = {
.name = "flv",
.long_name = NULL_IF_CONFIG_SMALL("FLV / Sorenson Spark / Sorenson H.263 (Flash Video)"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_FLV1,
.priv_data_size = sizeof(MpegEncContext),
.init = ff_mpv_encode_init,
.encode2 = ff_mpv_encode_picture,
.close = ff_mpv_encode_end,
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
AV_PIX_FMT_NONE},
.priv_class = &flv_class,
};
一、ff_mpv_encode_init()
# libavcodec\mpegvideo_enc.c
/* init video encoder */
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
AVCPBProperties *cpb_props;
int i, ret, format_supported;
mpv_encode_defaults(s);
switch (avctx->codec_id) {
case AV_CODEC_ID_MPEG2VIDEO:
if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
av_log(avctx, AV_LOG_ERROR,
"only YUV420 and YUV422 are supported\n");
return AVERROR(EINVAL);
}
break;
case AV_CODEC_ID_MJPEG:
case AV_CODEC_ID_AMV:
format_supported = 0;
/* JPEG color space */
if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
(avctx->color_range == AVCOL_RANGE_JPEG &&
(avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
avctx->pix_fmt == AV_PIX_FMT_YUV444P)))
format_supported = 1;
/* MPEG color space */
else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
(avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
avctx->pix_fmt == AV_PIX_FMT_YUV444P))
format_supported = 1;
if (!format_supported) {
av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
return AVERROR(EINVAL);
}
break;
default:
if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
return AVERROR(EINVAL);
}
}
switch (avctx->pix_fmt) {
case AV_PIX_FMT_YUVJ444P:
case AV_PIX_FMT_YUV444P:
s->chroma_format = CHROMA_444;
break;
case AV_PIX_FMT_YUVJ422P:
case AV_PIX_FMT_YUV422P:
s->chroma_format = CHROMA_422;
break;
case AV_PIX_FMT_YUVJ420P:
case AV_PIX_FMT_YUV420P:
default:
s->chroma_format = CHROMA_420;
break;
}
avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->rtp_payload_size)
s->rtp_payload_size = avctx->rtp_payload_size;
if (avctx->me_penalty_compensation)
s->me_penalty_compensation = avctx->me_penalty_compensation;
if (avctx->pre_me)
s->me_pre = avctx->pre_me;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
s->bit_rate = avctx->bit_rate;
s->width = avctx->width;
s->height = avctx->height;
if (avctx->gop_size > 600 &&
avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
av_log(avctx, AV_LOG_WARNING,
"keyframe interval too large!, reducing it from %d to %d\n",
avctx->gop_size, 600);
avctx->gop_size = 600;
}
s->gop_size = avctx->gop_size;
s->avctx = avctx;
if (avctx->max_b_frames > MAX_B_FRAMES) {
av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
"is %d.\n", MAX_B_FRAMES);
avctx->max_b_frames = MAX_B_FRAMES;
}
s->max_b_frames = avctx->max_b_frames;
s->codec_id = avctx->codec->id;
s->strict_std_compliance = avctx->strict_std_compliance;
s->quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
s->rtp_mode = !!s->rtp_payload_size;
s->intra_dc_precision = avctx->intra_dc_precision;
// workaround some differences between how applications specify dc precision
if (s->intra_dc_precision < 0) {
s->intra_dc_precision += 8;
} else if (s->intra_dc_precision >= 8)
s->intra_dc_precision -= 8;
if (s->intra_dc_precision < 0) {
av_log(avctx, AV_LOG_ERROR,
"intra dc precision must be positive, note some applications use"
" 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
return AVERROR(EINVAL);
}
if (avctx->codec_id == AV_CODEC_ID_AMV || (avctx->active_thread_type & FF_THREAD_SLICE))
s->huffman = 0;
if (s->intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
return AVERROR(EINVAL);
}
s->user_specified_pts = AV_NOPTS_VALUE;
if (s->gop_size <= 1) {
s->intra_only = 1;
s->gop_size = 12;
} else {
s->intra_only = 0;
}
/* Fixed QSCALE */
s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
s->adaptive_quant = (avctx->lumi_masking ||
avctx->dark_masking ||
avctx->temporal_cplx_masking ||
avctx->spatial_cplx_masking ||
avctx->p_masking ||
s->border_masking ||
(s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
!s->fixed_qscale;
s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
switch(avctx->codec_id) {
case AV_CODEC_ID_MPEG1VIDEO:
case AV_CODEC_ID_MPEG2VIDEO:
avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
break;
case AV_CODEC_ID_MPEG4:
case AV_CODEC_ID_MSMPEG4V1:
case AV_CODEC_ID_MSMPEG4V2:
case AV_CODEC_ID_MSMPEG4V3:
if (avctx->rc_max_rate >= 15000000) {
avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
} else if(avctx->rc_max_rate >= 2000000) {
avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
} else if(avctx->rc_max_rate >= 384000) {
avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
} else
avctx->rc_buffer_size = 40;
avctx->rc_buffer_size *= 16384;
break;
}
if (avctx->rc_buffer_size) {
av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
}
}
if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
return AVERROR(EINVAL);
}
if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
av_log(avctx, AV_LOG_INFO,
"Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
}
if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
return AVERROR(EINVAL);
}
if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
return AVERROR(EINVAL);
}
if (avctx->rc_max_rate &&
avctx->rc_max_rate == avctx->bit_rate &&
avctx->rc_max_rate != avctx->rc_min_rate) {
av_log(avctx, AV_LOG_INFO,
"impossible bitrate constraints, this will fail\n");
}
if (avctx->rc_buffer_size &&
avctx->bit_rate * (int64_t)avctx->time_base.num >
avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
return AVERROR(EINVAL);
}
if (!s->fixed_qscale &&
avctx->bit_rate * av_q2d(avctx->time_base) >
avctx->bit_rate_tolerance) {
av_log(avctx, AV_LOG_WARNING,
"bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
avctx->bit_rate_tolerance = 5 * avctx->bit_rate * av_q2d(avctx->time_base);
}
if (avctx->rc_max_rate &&
avctx->rc_min_rate == avctx->rc_max_rate &&
(s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
90000LL * (avctx->rc_buffer_size - 1) >
avctx->rc_max_rate * 0xFFFFLL) {
av_log(avctx, AV_LOG_INFO,
"Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
"specified vbv buffer is too large for the given bitrate!\n");
}
if ((avctx->flags & AV_CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
s->codec_id != AV_CODEC_ID_FLV1) {
av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
return AVERROR(EINVAL);
}
if (s->obmc && avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
av_log(avctx, AV_LOG_ERROR,
"OBMC is only supported with simple mb decision\n");
return AVERROR(EINVAL);
}
if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
return AVERROR(EINVAL);
}
if (s->max_b_frames &&
s->codec_id != AV_CODEC_ID_MPEG4 &&
s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
return AVERROR(EINVAL);
}
if (s->max_b_frames < 0) {
av_log(avctx, AV_LOG_ERROR,
"max b frames must be 0 or positive for mpegvideo based encoders\n");
return AVERROR(EINVAL);
}
if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
s->codec_id == AV_CODEC_ID_H263 ||
s->codec_id == AV_CODEC_ID_H263P) &&
(avctx->sample_aspect_ratio.num > 255 ||
avctx->sample_aspect_ratio.den > 255)) {
av_log(avctx, AV_LOG_WARNING,
"Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
}
if ((s->codec_id == AV_CODEC_ID_H263 ||
s->codec_id == AV_CODEC_ID_H263P) &&
(avctx->width > 2048 ||
avctx->height > 1152 )) {
av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
return AVERROR(EINVAL);
}
if ((s->codec_id == AV_CODEC_ID_H263 ||
s->codec_id == AV_CODEC_ID_H263P) &&
((avctx->width &3) ||
(avctx->height&3) )) {
av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
return AVERROR(EINVAL);
}
if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
(avctx->width > 4095 ||
avctx->height > 4095 )) {
av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
return AVERROR(EINVAL);
}
if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
(avctx->width > 16383 ||
avctx->height > 16383 )) {
av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
return AVERROR(EINVAL);
}
if (s->codec_id == AV_CODEC_ID_RV10 &&
(avctx->width &15 ||
avctx->height&15 )) {
av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
return AVERROR(EINVAL);
}
if (s->codec_id == AV_CODEC_ID_RV20 &&
(avctx->width &3 ||
avctx->height&3 )) {
av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
return AVERROR(EINVAL);
}
if ((s->codec_id == AV_CODEC_ID_WMV1 ||
s->codec_id == AV_CODEC_ID_WMV2) &&
avctx->width & 1) {
av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
return AVERROR(EINVAL);
}
if ((avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
return AVERROR(EINVAL);
}
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->mpeg_quant)
s->mpeg_quant = avctx->mpeg_quant;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
// FIXME mpeg2 uses that too
if (s->mpeg_quant && ( s->codec_id != AV_CODEC_ID_MPEG4
&& s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
av_log(avctx, AV_LOG_ERROR,
"mpeg2 style quantization not supported by codec\n");
return AVERROR(EINVAL);
}
if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
return AVERROR(EINVAL);
}
if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
avctx->mb_decision != FF_MB_DECISION_RD) {
av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
return AVERROR(EINVAL);
}
if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
(s->codec_id == AV_CODEC_ID_AMV ||
s->codec_id == AV_CODEC_ID_MJPEG)) {
// Used to produce garbage with MJPEG.
av_log(avctx, AV_LOG_ERROR,
"QP RD is no longer compatible with MJPEG or AMV\n");
return AVERROR(EINVAL);
}
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->scenechange_threshold)
s->scenechange_threshold = avctx->scenechange_threshold;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
if (s->scenechange_threshold < 1000000000 &&
(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
av_log(avctx, AV_LOG_ERROR,
"closed gop with scene change detection are not supported yet, "
"set threshold to 1000000000\n");
return AVERROR_PATCHWELCOME;
}
if (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
av_log(avctx, AV_LOG_ERROR,
"low delay forcing is only available for mpeg2, "
"set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
return AVERROR(EINVAL);
}
if (s->max_b_frames != 0) {
av_log(avctx, AV_LOG_ERROR,
"B-frames cannot be used with low delay\n");
return AVERROR(EINVAL);
}
}
if (s->q_scale_type == 1) {
if (avctx->qmax > 28) {
av_log(avctx, AV_LOG_ERROR,
"non linear quant only supports qmax <= 28 currently\n");
return AVERROR_PATCHWELCOME;
}
}
if (avctx->slices > 1 &&
(avctx->codec_id == AV_CODEC_ID_FLV1 || avctx->codec_id == AV_CODEC_ID_H261)) {
av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
return AVERROR(EINVAL);
}
if (avctx->thread_count > 1 &&
s->codec_id != AV_CODEC_ID_MPEG4 &&
s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
s->codec_id != AV_CODEC_ID_MJPEG &&
(s->codec_id != AV_CODEC_ID_H263P)) {
av_log(avctx, AV_LOG_ERROR,
"multi threaded encoding not supported by codec\n");
return AVERROR_PATCHWELCOME;
}
if (avctx->thread_count < 1) {
av_log(avctx, AV_LOG_ERROR,
"automatic thread number detection not supported by codec, "
"patch welcome\n");
return AVERROR_PATCHWELCOME;
}
if (!avctx->time_base.den || !avctx->time_base.num) {
av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
return AVERROR(EINVAL);
}
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->b_frame_strategy)
s->b_frame_strategy = avctx->b_frame_strategy;
if (avctx->b_sensitivity != 40)
s->b_sensitivity = avctx->b_sensitivity;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
av_log(avctx, AV_LOG_INFO,
"notice: b_frame_strategy only affects the first pass\n");
s->b_frame_strategy = 0;
}
i = av_gcd(avctx->time_base.den, avctx->time_base.num);
if (i > 1) {
av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
avctx->time_base.den /= i;
avctx->time_base.num /= i;
//return -1;
}
if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG || s->codec_id==AV_CODEC_ID_AMV) {
// (a + x * 3 / 8) / x
s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
s->inter_quant_bias = 0;
} else {
s->intra_quant_bias = 0;
// (a - x / 4) / x
s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
}
if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
return AVERROR(EINVAL);
}
av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
avctx->time_base.den > (1 << 16) - 1) {
av_log(avctx, AV_LOG_ERROR,
"timebase %d/%d not supported by MPEG 4 standard, "
"the maximum admitted value for the timebase denominator "
"is %d\n", avctx->time_base.num, avctx->time_base.den,
(1 << 16) - 1);
return AVERROR(EINVAL);
}
s->time_increment_bits = av_log2(avctx->time_base.den - 1) + 1;
switch (avctx->codec->id) {
case AV_CODEC_ID_MPEG1VIDEO:
s->out_format = FMT_MPEG1;
s->low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
break;
case AV_CODEC_ID_MPEG2VIDEO:
s->out_format = FMT_MPEG1;
s->low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
s->rtp_mode = 1;
break;
case AV_CODEC_ID_MJPEG:
case AV_CODEC_ID_AMV:
s->out_format = FMT_MJPEG;
s->intra_only = 1; /* force intra only for jpeg */
if (!CONFIG_MJPEG_ENCODER)
return AVERROR_ENCODER_NOT_FOUND;
if ((ret = ff_mjpeg_encode_init(s)) < 0)
return ret;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_H261:
if (!CONFIG_H261_ENCODER)
return AVERROR_ENCODER_NOT_FOUND;
if (ff_h261_get_picture_format(s->width, s->height) < 0) {
av_log(avctx, AV_LOG_ERROR,
"The specified picture size of %dx%d is not valid for the "
"H.261 codec.\nValid sizes are 176x144, 352x288\n",
s->width, s->height);
return AVERROR(EINVAL);
}
s->out_format = FMT_H261;
avctx->delay = 0;
s->low_delay = 1;
s->rtp_mode = 0; /* Sliced encoding not supported */
break;
case AV_CODEC_ID_H263:
if (!CONFIG_H263_ENCODER)
return AVERROR_ENCODER_NOT_FOUND;
if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
s->width, s->height) == 8) {
av_log(avctx, AV_LOG_ERROR,
"The specified picture size of %dx%d is not valid for "
"the H.263 codec.\nValid sizes are 128x96, 176x144, "
"352x288, 704x576, and 1408x1152. "
"Try H.263+.\n", s->width, s->height);
return AVERROR(EINVAL);
}
s->out_format = FMT_H263;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_H263P:
s->out_format = FMT_H263;
s->h263_plus = 1;
/* Fx */
s->h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
s->modified_quant = s->h263_aic;
s->loop_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
/* /Fx */
/* These are just to be sure */
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_FLV1:
s->out_format = FMT_H263;
s->h263_flv = 2; /* format = 1; 11-bit codes */
s->unrestricted_mv = 1;
s->rtp_mode = 0; /* don't allow GOB */
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_RV10:
s->out_format = FMT_H263;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_RV20:
s->out_format = FMT_H263;
avctx->delay = 0;
s->low_delay = 1;
s->modified_quant = 1;
s->h263_aic = 1;
s->h263_plus = 1;
s->loop_filter = 1;
s->unrestricted_mv = 0;
break;
case AV_CODEC_ID_MPEG4:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->low_delay = s->max_b_frames ? 0 : 1;
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
break;
case AV_CODEC_ID_MSMPEG4V2:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 2;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_MSMPEG4V3:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 3;
s->flipflop_rounding = 1;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_WMV1:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 4;
s->flipflop_rounding = 1;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_WMV2:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 5;
s->flipflop_rounding = 1;
avctx->delay = 0;
s->low_delay = 1;
break;
default:
return AVERROR(EINVAL);
}
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->noise_reduction)
s->noise_reduction = avctx->noise_reduction;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
avctx->has_b_frames = !s->low_delay;
s->encoding = 1;
s->progressive_frame =
s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
AV_CODEC_FLAG_INTERLACED_ME) ||
s->alternate_scan);
/* init */
ff_mpv_idct_init(s);
if ((ret = ff_mpv_common_init(s)) < 0)
return ret;
ff_fdctdsp_init(&s->fdsp, avctx);
ff_me_cmp_init(&s->mecc, avctx);
ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
ff_pixblockdsp_init(&s->pdsp, avctx);
ff_qpeldsp_init(&s->qdsp);
if (s->msmpeg4_version) {
int ac_stats_size = 2 * 2 * (MAX_LEVEL + 1) * (MAX_RUN + 1) * 2 * sizeof(int);
if (!(s->ac_stats = av_mallocz(ac_stats_size)))
return AVERROR(ENOMEM);
}
if (!(avctx->stats_out = av_mallocz(256)) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix, 32) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix, 32) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16, 32) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16, 32) ||
!FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_PICTURE_COUNT) ||
!FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_PICTURE_COUNT))
return AVERROR(ENOMEM);
if (s->noise_reduction) {
if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2))
return AVERROR(ENOMEM);
}
ff_dct_encode_init(s);
if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
s->chroma_qscale_table = ff_h263_chroma_qscale_table;
if (s->slice_context_count > 1) {
s->rtp_mode = 1;
if (avctx->codec_id == AV_CODEC_ID_H263P)
s->h263_slice_structured = 1;
}
s->quant_precision = 5;
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->frame_skip_threshold)
s->frame_skip_threshold = avctx->frame_skip_threshold;
if (avctx->frame_skip_factor)
s->frame_skip_factor = avctx->frame_skip_factor;
if (avctx->frame_skip_exp)
s->frame_skip_exp = avctx->frame_skip_exp;
if (avctx->frame_skip_cmp != FF_CMP_DCTMAX)
s->frame_skip_cmp = avctx->frame_skip_cmp;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, avctx->ildct_cmp);
ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
ff_h261_encode_init(s);
if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
ff_h263_encode_init(s);
if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
if ((ret = ff_msmpeg4_encode_init(s)) < 0)
return ret;
if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
&& s->out_format == FMT_MPEG1)
ff_mpeg1_encode_init(s);
/* init q matrix */
for (i = 0; i < 64; i++) {
int j = s->idsp.idct_permutation[i];
if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
s->mpeg_quant) {
s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
} else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
s->intra_matrix[j] =
s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
} else {
/* MPEG-1/2 */
s->chroma_intra_matrix[j] =
s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
}
if (avctx->intra_matrix)
s->intra_matrix[j] = avctx->intra_matrix[i];
if (avctx->inter_matrix)
s->inter_matrix[j] = avctx->inter_matrix[i];
}
/* precompute matrix */
/* for mjpeg, we do include qscale in the matrix */
if (s->out_format != FMT_MJPEG) {
ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
s->intra_matrix, s->intra_quant_bias, avctx->qmin,
31, 1);
ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
s->inter_matrix, s->inter_quant_bias, avctx->qmin,
31, 0);
}
if ((ret = ff_rate_control_init(s)) < 0)
return ret;
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
if (avctx->brd_scale)
s->brd_scale = avctx->brd_scale;
if (avctx->prediction_method)
s->pred = avctx->prediction_method + 1;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
if (s->b_frame_strategy == 2) {
for (i = 0; i < s->max_b_frames + 2; i++) {
s->tmp_frames[i] = av_frame_alloc();
if (!s->tmp_frames[i])
return AVERROR(ENOMEM);
s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
s->tmp_frames[i]->width = s->width >> s->brd_scale;
s->tmp_frames[i]->height = s->height >> s->brd_scale;
ret = av_frame_get_buffer(s->tmp_frames[i], 0);
if (ret < 0)
return ret;
}
}
cpb_props = ff_add_cpb_side_data(avctx);
if (!cpb_props)
return AVERROR(ENOMEM);
cpb_props->max_bitrate = avctx->rc_max_rate;
cpb_props->min_bitrate = avctx->rc_min_rate;
cpb_props->avg_bitrate = avctx->bit_rate;
cpb_props->buffer_size = avctx->rc_buffer_size;
return 0;
}
相關文章
- 前端進階-編寫測試程式碼前端
- 15. 程式程式設計進階:函式的返回值程式設計函式
- shell程式設計進階程式設計
- 程式設計師 vs 編碼員程式設計師
- 程式設計師都應學習程式碼編譯器知識程式設計師編譯
- 好程式設計師web前端分享css初始化程式碼程式設計師Web前端CSS
- 高階bash指令碼程式設計(1)指令碼程式設計
- 程式設計師常用的五種輕量級程式碼編輯器程式設計師
- 併發程式設計進階程式設計
- Python 高階程式設計:深入探索高階程式碼實踐Python程式設計
- 程式設計入門之字元編碼與亂碼程式設計字元
- JavaScript簡單計算器程式碼分析JavaScript
- 15Java進階 程式Java
- Cursor,程式設計師的 AI 程式碼編輯助手程式設計師AI
- 高階bash/shell指令碼程式設計指南指令碼程式設計
- 13.程式程式設計進階:函式程式設計函式
- 7個最受Linux程式設計師歡迎的程式碼編輯器Linux程式設計師
- 5個最受Linux程式設計師歡迎的程式碼編輯器Linux程式設計師
- [譯]編碼,編碼,編碼!提高程式設計技能的唯一途徑程式設計
- 《UNIX環境高階程式設計》apue原始碼Ubuntu下的編譯程式設計原始碼Ubuntu編譯
- 面向研發的低程式碼、程式碼視覺化設計編排視覺化
- 併發程式設計—— FutureTask 原始碼分析程式設計原始碼
- 無程式碼程式設計程式設計
- 程式設計師的進階之路程式設計師
- 【程式設計素質】Java編碼約定程式設計Java
- JavaCV FFmpeg AAC編碼Java
- 中國程式設計師與美國程式設計師寫程式碼的區別分析程式設計師
- 為物料管理進行公司程式碼初始化
- php中使用 ffmpeg(部分程式碼)PHP
- 實時程式碼編輯器
- 利用Pycharm進行程式碼比較更新PyCharm行程
- python 安全編碼&程式碼審計Python
- Rust 程式設計影片教程(進階)——015_2 弱引用Rust程式設計
- 程式碼行數能衡量編碼進度嗎?
- sublime text 4 for Mac中文漢化教程高階程式碼編輯器Mac
- iOS逆向之旅(進階篇) — 程式碼注入iOS
- 併發程式設計之 Exchanger 原始碼分析程式設計原始碼
- java 併發程式設計-AQS原始碼分析Java程式設計AQS原始碼