H264 parameter setting based on V4L2 MFC video codec

Source: Internet
Author: User

Last Post hardware video encoding, after the default parameters are set, encode 1000 frames 640x480 H264 file size is about 180m, very large, you must set the parameters

After the test discovery is Enablembratecontrol, the encoded data begins to shrink significantly:

Here the first 54 control parameters are all initialized-1, in these 54 parameters, including h264,h263 and MPEG video format settings, for each video format only select corresponding to his parameters. Just test the H264 format here.

All control parameters are as follows:

struct {int num;int id;char *name;int value;} ctrls[] = {{0, v4l2_cid_mpeg_mfc51_video_force_frame_type, ' force_frame ',- 1},{1, V4l2_cid_mpeg_mfc51_video_frame_skip_mode, "Frame_skip_mode", -1},{2, v4l2_cid_mpeg_mfc51_video_h264_ Adaptive_rc_activity, "h264_arc_activity", -1},{3, V4l2_cid_mpeg_mfc51_video_h264_adaptive_rc_dark, "H264_arc_dark ", -1},{4, V4l2_cid_mpeg_mfc51_video_h264_adaptive_rc_smooth," H264_arc_smooth ", -1},{5, V4L2_CID_MPEG_MFC51_VIDEO_ H264_adaptive_rc_static, "H264_arc_static", -1},{6, V4l2_cid_mpeg_mfc51_video_h264_num_ref_pic_for_p, "h264_ref_ Pic ", -1},{7, V4l2_cid_mpeg_mfc51_video_padding_yuv," Padding_yuv ", -1},{8, V4l2_cid_mpeg_mfc51_video_padding," Padding "},{9, V4l2_cid_mpeg_mfc51_video_rc_fixed_target_bit," Rc_fixed_target_bit ", -1},{10, V4L2_CID_MPEG_MFC51_ Video_rc_reaction_coeff, "Rc_react_coeff", -1},{11, V4l2_cid_mpeg_video_b_frames, "B_frames", -1},{12, V4L2_CID_MPEG _video_bitrate, "bitrate", -1},{13, V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, "Intra_rEfresh_mb ", -1},{14, V4l2_cid_mpeg_video_frame_rc_enable," rc_enable ", -1},{15, V4l2_cid_mpeg_video_gop_closure," Gop_closure ", -1},{16, V4l2_cid_mpeg_video_gop_size," Gop_size ", -1},{17, V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP," h263 _b_qp ", -1},{18, V4l2_cid_mpeg_video_h263_i_frame_qp," H263_i_qp ", -1},{19, V4L2_CID_MPEG_VIDEO_H263_MAX_QP," h263_ Max_qp ", -1},{20, V4l2_cid_mpeg_video_h263_min_qp," H263_min_qp ", -1},{21, V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP," H263_p_qp "},{22, V4l2_cid_mpeg_video_h264_8x8_transform," H264_8x8_transform ", -1},{23, V4L2_CID_MPEG_VIDEO_H264_B _FRAME_QP, "H264_b_qp", -1},{24, V4l2_cid_mpeg_video_h264_cpb_size, "H264_cpb_size", -1},{25, V4L2_CID_MPEG_VIDEO_ H264_entropy_mode, "H264_entropy", -1},{26, V4l2_cid_mpeg_video_h264_i_frame_qp, "H264_i_qp", -1},{27, V4L2_CID_MPEG _video_h264_i_period, "H264_i_period", -1},{28, V4l2_cid_mpeg_video_h264_level, "H264_level", -1},{29, V4L2_CID_MPEG _video_h264_loop_filter_alpha, "H264_loop_filter_alpha", -1},{30, V4l2_cid_mpeg_viDeo_h264_loop_filter_beta, "H264_loop_filter_beta", -1},{31, V4l2_cid_mpeg_video_h264_loop_filter_mode, "H264_LOOP _filter_mode ", -1},{32, V4l2_cid_mpeg_video_h264_max_qp," H264_max_qp ", -1},{33, V4L2_CID_MPEG_VIDEO_H264_MIN_QP," H264_min_qp ", -1},{34, V4l2_cid_mpeg_video_h264_p_frame_qp," H264_p_qp ", -1},{35, V4l2_cid_mpeg_video_h264_profile, "H264_profile", -1},{36, V4l2_cid_mpeg_video_h264_vui_ext_sar_height, "H264_vui_sar_height", -1},{37, V4L2_CID_MPEG _video_h264_vui_ext_sar_width, "H264_vui_sar_width", -1},{38, V4l2_cid_mpeg_video_h264_vui_sar_enable, "h264_vui_ Sar ", -1},{39, V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC," H264_VUI_SAR_IDC ", -1},{40, V4l2_cid_mpeg_video_header_mode," Header_mode ", -1},{41, V4l2_cid_mpeg_video_mb_rc_enable," MB_RC ", -1},{42, V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP," Mpeg4_b_qp ", -1},{43, V4l2_cid_mpeg_video_mpeg4_i_frame_qp," Mpeg4_i_qp ", -1},{44, V4l2_cid_mpeg_video_mpeg4_level, "Mpeg4_level", -1},{45, V4l2_cid_mpeg_video_mpeg4_max_qp, "Mpeg4_max_qp", -1},{46, V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP, "Mpeg4_min_qp", -1},{47, V4l2_cid_mpeg_video_mpeg4_p_frame_qp, "Mpeg4_p_qp",-1} , {v4l2_cid_mpeg_video_mpeg4_profile, "mpeg4_profile", -1},{49, V4l2_cid_mpeg_video_mpeg4_qpel, "MPEG4_QPEL",-1} , {v4l2_cid_mpeg_video_multi_slice_max_bytes, "slice_max_bytes", -1},{51, V4l2_cid_mpeg_video_multi_slice_max_ MB, "Slice_max_mb", -1},{52, V4l2_cid_mpeg_video_multi_slice_mode, "Slice_mode", -1},{53, V4l2_cid_mpeg_video_vbv_ SIZE, "Vbv_size",-1},};


The parameter setting function is as follows:

static int Mfc_set_control (struct io_dev *dev, int id, int value) {struct V4l2_ext_control ctrl;struct v4l2_ext_controls CT Rl_s;int ret;ctrl.id = Id;ctrl.value = Value;ctrl_s.ctrl_class = V4l2_ctrl_class_mpeg;ctrl_s.count = 1;ctrl_s.controls =  &ctrl;ret = IOCTL (DEV->FD, Vidioc_s_ext_ctrls, &ctrl_s), if (Ret < 0) Err ("Cannot set control%d to%d on%d", ID, value, DEV->FD); return ret;}

The H264 initialization parameters are as follows:

int h264_param_init (struct Io_dev *dev) {int i;ctrls[2].value = 1;//h264_arc_activity, v4l2_cid_mpeg_mfc51_video_h264_ adaptive_rc_activity, Activitydisablectrls[3].value = 1;//h264_arc_dark, v4l2_cid_mpeg_mfc51_video_h264_adaptive_ Rc_dark, Darkdisablectrls[4].value = 1;//h264_arc_smooth, V4l2_cid_mpeg_mfc51_video_h264_adaptive_rc_smooth, Smoothdisablectrls[5].value = 1;//h264_arc_static, v4l2_cid_mpeg_mfc51_video_h264_adaptive_rc_static, Staticdisablectrls[6].value = 2; H264_ref_pic, v4l2_cid_mpeg_mfc51_video_h264_num_ref_pic_for_p, Numberrefforpframesctrls[7].value = 0;//padding_ YUV, V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV, Lumapadval, cbpadval, Crpadval,ctrls[8].value = 1;//padding, V4L2_CID_ mpeg_mfc51_video_padding, Padcontrolonctrls[10].value = 120;//rc_react_coeff, v4l2_cid_mpeg_mfc51_video_rc_ Reaction_coeff, cbrperiodrfctrls[11].value = 2; B_frames, V4l2_cid_mpeg_video_b_frames, Numberbframes (0~2) Ctrls[12].value = 128000;//bitrate, V4L2_CID_MPEG_VIDEO _bitrate, bitratectrls[13].Value = 0;//INTRA_REFRESH_MB, v4l2_cid_mpeg_video_cyclic_intra_refresh_mb, randomintrambrefreshctrls[14].value = 0; Rc_enable, v4l2_cid_mpeg_video_frame_rc_enable, Numberreferenceframesctrls[22].value = 0;//h264_8x8_transform, V4l2_cid_mpeg_video_h264_8x8_transform, Transform8x8modectrls[26].value = 20;//H264_I_QP, V4L2_CID_MPEG_VIDEO_H264 _I_FRAME_QP, Frameqpctrls[21].value = 20;//h263_p_qp, V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP, FrameQp_Pctrls[23].value = 20;//h264_b_qp, v4l2_cid_mpeg_video_h264_b_frame_qp, frameqp_bctrls[25].value = 1; H264_entropy, v4l2_cid_mpeg_video_h264_entropy_mode, symbolmodectrls[27].value = 100; H264_i_period, v4l2_cid_mpeg_video_h264_i_period, idrperiodctrls[28].value = 11; H264_level, v4l2_cid_mpeg_video_h264_level, 40levelidcctrls[29].value = 0; H264_loop_filter_alpha, v4l2_cid_mpeg_video_h264_loop_filter_alpha, loopfilteralphac0offsetctrls[30].value = 0; H264_loop_filter_beta, V4l2_cid_mpeg_video_h264_loop_filter_beta, loopfilterbetaoffsetctrls[31].value = 1; H264_loop_filter_mode, v4l2_cid_mpeg_video_h264_loop_filter_mode, Loopfilterdisablectrls[32].value = 51;//h264_ MAX_QP, v4l2_cid_mpeg_video_h264_max_qp, Qscodemaxctrls[33].value = 10;//H264_MIN_QP, V4L2_CID_MPEG_VIDEO_H264_MIN_ QP, qscodeminctrls[35].value = 4; H264_profile, v4l2_cid_mpeg_video_h264_profile, profileidcctrls[41].value = 1; MB_RC, v4l2_cid_mpeg_video_mb_rc_enable, enablembratecontrolctrls[50].value = 1900; Slice_max_bytes, V4l2_cid_mpeg_video_multi_slice_max_bytes,????? Ctrls[51].value = 1; SLICE_MAX_MB, v4l2_cid_mpeg_video_multi_slice_max_mb, sliceargumentctrls[52].value = 0; Slice_mode, V4l2_cid_mpeg_video_multi_slice_mode, slicemodefor (i = 0; i < Array_len (ctrls); ++i) {if (Ctrls[i]. Value! =-1) if (Mfc_set_control (Dev, ctrls[i].id, ctrls[i].value) < 0) {err ("param:%s error", ctrls[i].name); return-1 ;}} return 0;}


After testing

640x480 H264 1000-frame encoding after generating file 4.6M

1920x1080 H264 1000-frame encoding after generating file 29.6M


H264 parameter setting based on V4L2 MFC video codec

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.