2525use std:: rc:: Rc ;
2626
2727use cros_codecs:: libva:: {
28- self , BufferType , Context , Display , EncCodedBuffer , EncMiscParameter , EncMiscParameterFrameRate ,
29- EncMiscParameterRateControl , EncPictureParameter , EncPictureParameterBufferH264 ,
30- EncSequenceParameter , EncSequenceParameterBufferH264 , EncSliceParameter ,
31- EncSliceParameterBufferH264 , H264EncFrameCropOffsets , H264EncPicFields , H264EncSeqFields ,
32- H264VuiFields , MappedCodedBuffer , Picture , PictureH264 , RcFlags , Surface , UsageHint ,
33- VAEntrypoint , VAProfile , VA_INVALID_ID , VA_PICTURE_H264_INVALID ,
28+ self , BufferType , Context , Display , EncCodedBuffer , EncMiscParameter ,
29+ EncMiscParameterFrameRate , EncMiscParameterRateControl , EncPictureParameter ,
30+ EncPictureParameterBufferH264 , EncSequenceParameter , EncSequenceParameterBufferH264 ,
31+ EncSliceParameter , EncSliceParameterBufferH264 , H264EncFrameCropOffsets , H264EncPicFields ,
32+ H264EncSeqFields , H264VuiFields , MappedCodedBuffer , Picture , PictureH264 , RcFlags , Surface ,
33+ UsageHint , VAEntrypoint , VAProfile , VA_INVALID_ID , VA_PICTURE_H264_INVALID ,
3434 VA_PICTURE_H264_SHORT_TERM_REFERENCE , VA_RT_FORMAT_YUV420 ,
3535} ;
3636
@@ -254,11 +254,9 @@ impl VaH264Encoder {
254254 force_keyframe : bool ,
255255 ) -> Result < Vec < u8 > , String > {
256256 if frame. pixel_format == PixelFormat :: Rgba8 {
257- return Err (
258- "VA-API H.264 encoder requires NV12 or I420 input; \
257+ return Err ( "VA-API H.264 encoder requires NV12 or I420 input; \
259258 insert a video::pixel_convert node upstream"
260- . into ( ) ,
261- ) ;
259+ . into ( ) ) ;
262260 }
263261
264262 // Determine whether this frame is an IDR.
@@ -287,9 +285,8 @@ impl VaH264Encoder {
287285 vec ! [ ( ) ] ,
288286 )
289287 . map_err ( |e| format ! ( "failed to create input surface: {e}" ) ) ?;
290- let input_surface = input_surfaces
291- . pop ( )
292- . ok_or_else ( || "create_surfaces returned empty vec" . to_string ( ) ) ?;
288+ let input_surface =
289+ input_surfaces. pop ( ) . ok_or_else ( || "create_surfaces returned empty vec" . to_string ( ) ) ?;
293290
294291 write_nv12_to_va_surface ( & self . display , & input_surface, frame) ?;
295292
@@ -308,15 +305,12 @@ impl VaH264Encoder {
308305 self . build_pic_param ( is_idr, & recon_surface, & coded_buf, frame_num as u16 , poc) ;
309306 let slice_param = self . build_slice_param ( is_i_frame, is_idr, poc, frame_num) ;
310307 let rc_param = self . build_rc_param ( ) ;
311- let framerate_param =
312- BufferType :: EncMiscParameter ( EncMiscParameter :: FrameRate ( EncMiscParameterFrameRate :: new (
313- self . framerate ,
314- 0 ,
315- ) ) ) ;
308+ let framerate_param = BufferType :: EncMiscParameter ( EncMiscParameter :: FrameRate (
309+ EncMiscParameterFrameRate :: new ( self . framerate , 0 ) ,
310+ ) ) ;
316311
317312 // Create picture, attach buffers, and submit.
318- let mut picture =
319- Picture :: new ( self . frame_count , Rc :: clone ( & self . context ) , input_surface) ;
313+ let mut picture = Picture :: new ( self . frame_count , Rc :: clone ( & self . context ) , input_surface) ;
320314
321315 picture. add_buffer (
322316 self . context
@@ -344,15 +338,9 @@ impl VaH264Encoder {
344338 . map_err ( |e| format ! ( "failed to create framerate param buffer: {e}" ) ) ?,
345339 ) ;
346340
347- let picture = picture
348- . begin ( )
349- . map_err ( |e| format ! ( "vaBeginPicture failed: {e}" ) ) ?;
350- let picture = picture
351- . render ( )
352- . map_err ( |e| format ! ( "vaRenderPicture failed: {e}" ) ) ?;
353- let picture = picture
354- . end ( )
355- . map_err ( |e| format ! ( "vaEndPicture failed: {e}" ) ) ?;
341+ let picture = picture. begin ( ) . map_err ( |e| format ! ( "vaBeginPicture failed: {e}" ) ) ?;
342+ let picture = picture. render ( ) . map_err ( |e| format ! ( "vaRenderPicture failed: {e}" ) ) ?;
343+ let picture = picture. end ( ) . map_err ( |e| format ! ( "vaEndPicture failed: {e}" ) ) ?;
356344
357345 // Sync and read coded output.
358346 let _synced = picture. sync ( ) . map_err ( |( e, _) | format ! ( "vaSyncSurface failed: {e}" ) ) ?;
@@ -366,11 +354,7 @@ impl VaH264Encoder {
366354 }
367355
368356 // Update reference frame.
369- self . reference = Some ( RefPic {
370- surface : recon_surface,
371- poc,
372- frame_num,
373- } ) ;
357+ self . reference = Some ( RefPic { surface : recon_surface, poc, frame_num } ) ;
374358
375359 self . frame_count += 1 ;
376360
@@ -408,9 +392,7 @@ impl VaH264Encoder {
408392 . map_err ( |e| format ! ( "failed to replenish scratch surfaces: {e}" ) ) ?;
409393 self . scratch_surfaces = new_surfaces;
410394 }
411- self . scratch_surfaces
412- . pop ( )
413- . ok_or_else ( || "scratch surface pool exhausted" . to_string ( ) )
395+ self . scratch_surfaces . pop ( ) . ok_or_else ( || "scratch surface pool exhausted" . to_string ( ) )
414396 }
415397
416398 /// Build the sequence parameter buffer (SPS-derived fields).
@@ -440,15 +422,15 @@ impl VaH264Encoder {
440422
441423 BufferType :: EncSequenceParameter ( EncSequenceParameter :: H264 (
442424 EncSequenceParameterBufferH264 :: new (
443- 0 , // seq_parameter_set_id
444- 41 , // level_idc (Level 4.1)
445- self . idr_period , // intra_period
446- self . idr_period , // intra_idr_period
447- 0 , // ip_period (no B frames)
448- 0 , // bits_per_second (CQP mode)
449- 1 , // max_num_ref_frames
450- self . width_in_mbs , // picture_width_in_mbs
451- self . height_in_mbs , // picture_height_in_mbs
425+ 0 , // seq_parameter_set_id
426+ 41 , // level_idc (Level 4.1)
427+ self . idr_period , // intra_period
428+ self . idr_period , // intra_idr_period
429+ 0 , // ip_period (no B frames)
430+ 0 , // bits_per_second (CQP mode)
431+ 1 , // max_num_ref_frames
432+ self . width_in_mbs , // picture_width_in_mbs
433+ self . height_in_mbs , // picture_height_in_mbs
452434 & seq_fields,
453435 0 , // bit_depth_luma_minus8
454436 0 , // bit_depth_chroma_minus8
@@ -488,8 +470,7 @@ impl VaH264Encoder {
488470 ) ;
489471
490472 // Reference frames array (up to 16 slots).
491- let mut reference_frames: [ PictureH264 ; 16 ] =
492- std:: array:: from_fn ( |_| build_invalid_pic ( ) ) ;
473+ let mut reference_frames: [ PictureH264 ; 16 ] = std:: array:: from_fn ( |_| build_invalid_pic ( ) ) ;
493474
494475 if let Some ( ref ref_pic) = self . reference {
495476 reference_frames[ 0 ] = PictureH264 :: new (
@@ -502,33 +483,33 @@ impl VaH264Encoder {
502483 }
503484
504485 let pic_fields = H264EncPicFields :: new (
505- u32:: from ( is_idr) , // idr_pic_flag
486+ u32:: from ( is_idr) , // idr_pic_flag
506487 u32:: from ( is_reference) , // reference_pic_flag
507- 0 , // entropy_coding_mode_flag (CAVLC)
508- 0 , // weighted_pred_flag
509- 0 , // weighted_bipred_idc
510- 0 , // constrained_intra_pred_flag
511- 0 , // transform_8x8_mode_flag
512- 1 , // deblocking_filter_control_present_flag
513- 0 , // redundant_pic_cnt_present_flag
514- 0 , // pic_order_present_flag
515- 0 , // pic_scaling_matrix_present_flag
488+ 0 , // entropy_coding_mode_flag (CAVLC)
489+ 0 , // weighted_pred_flag
490+ 0 , // weighted_bipred_idc
491+ 0 , // constrained_intra_pred_flag
492+ 0 , // transform_8x8_mode_flag
493+ 1 , // deblocking_filter_control_present_flag
494+ 0 , // redundant_pic_cnt_present_flag
495+ 0 , // pic_order_present_flag
496+ 0 , // pic_scaling_matrix_present_flag
516497 ) ;
517498
518499 BufferType :: EncPictureParameter ( EncPictureParameter :: H264 (
519500 EncPictureParameterBufferH264 :: new (
520501 curr_pic,
521502 reference_frames,
522503 coded_buf. id ( ) ,
523- 0 , // pic_parameter_set_id
524- 0 , // seq_parameter_set_id
525- 0 , // last_picture (not EOS)
526- frame_num, // frame_num
527- self . qp as u8 , // pic_init_qp
528- 0 , // num_ref_idx_l0_active_minus1
529- 0 , // num_ref_idx_l1_active_minus1
530- 0 , // chroma_qp_index_offset
531- 0 , // second_chroma_qp_index_offset
504+ 0 , // pic_parameter_set_id
505+ 0 , // seq_parameter_set_id
506+ 0 , // last_picture (not EOS)
507+ frame_num, // frame_num
508+ self . qp as u8 , // pic_init_qp
509+ 0 , // num_ref_idx_l0_active_minus1
510+ 0 , // num_ref_idx_l1_active_minus1
511+ 0 , // chroma_qp_index_offset
512+ 0 , // second_chroma_qp_index_offset
532513 & pic_fields,
533514 ) ,
534515 ) )
@@ -545,8 +526,7 @@ impl VaH264Encoder {
545526 let slice_type = if is_i_frame { SLICE_TYPE_I } else { SLICE_TYPE_P } ;
546527
547528 // Reference picture lists.
548- let mut ref_pic_list_0: [ PictureH264 ; 32 ] =
549- std:: array:: from_fn ( |_| build_invalid_pic ( ) ) ;
529+ let mut ref_pic_list_0: [ PictureH264 ; 32 ] = std:: array:: from_fn ( |_| build_invalid_pic ( ) ) ;
550530 let mut num_ref_idx_l0_active_minus1: u8 = 0 ;
551531 let mut num_ref_idx_active_override_flag: u8 = 0 ;
552532
@@ -564,57 +544,51 @@ impl VaH264Encoder {
564544 }
565545 }
566546
567- let ref_pic_list_1: [ PictureH264 ; 32 ] =
568- std:: array:: from_fn ( |_| build_invalid_pic ( ) ) ;
547+ let ref_pic_list_1: [ PictureH264 ; 32 ] = std:: array:: from_fn ( |_| build_invalid_pic ( ) ) ;
569548
570- let idr_pic_id = if is_idr {
571- ( self . frame_count / u64:: from ( self . idr_period ) ) as u16
572- } else {
573- 0
574- } ;
549+ let idr_pic_id =
550+ if is_idr { ( self . frame_count / u64:: from ( self . idr_period ) ) as u16 } else { 0 } ;
575551
576552 // Compute slice_qp_delta so that pic_init_qp + slice_qp_delta = target QP.
577553 // Since we set pic_init_qp = self.qp, slice_qp_delta = 0.
578554 let slice_qp_delta: i8 = 0 ;
579555
580- BufferType :: EncSliceParameter ( EncSliceParameter :: H264 (
581- EncSliceParameterBufferH264 :: new (
582- 0 , // macroblock_address (start of slice)
583- self . num_mbs , // num_macroblocks
584- VA_INVALID_ID , // macroblock_info (unused)
585- slice_type,
586- 0 , // pic_parameter_set_id
587- idr_pic_id,
588- poc, // pic_order_cnt_lsb
589- 0 , // delta_pic_order_cnt_bottom
590- [ 0i32 ; 2 ] , // delta_pic_order_cnt
591- 0 , // direct_spatial_mv_pred_flag
592- num_ref_idx_active_override_flag,
593- num_ref_idx_l0_active_minus1,
594- 0 , // num_ref_idx_l1_active_minus1
595- ref_pic_list_0,
596- ref_pic_list_1,
597- 0 , // luma_log2_weight_denom
598- 0 , // chroma_log2_weight_denom
599- 0 , // luma_weight_l0_flag
600- [ 0i16 ; 32 ] , // luma_weight_l0
601- [ 0i16 ; 32 ] , // luma_offset_l0
602- 0 , // chroma_weight_l0_flag
603- [ [ 0i16 ; 2 ] ; 32 ] , // chroma_weight_l0
604- [ [ 0i16 ; 2 ] ; 32 ] , // chroma_offset_l0
605- 0 , // luma_weight_l1_flag
606- [ 0i16 ; 32 ] , // luma_weight_l1
607- [ 0i16 ; 32 ] , // luma_offset_l1
608- 0 , // chroma_weight_l1_flag
609- [ [ 0i16 ; 2 ] ; 32 ] , // chroma_weight_l1
610- [ [ 0i16 ; 2 ] ; 32 ] , // chroma_offset_l1
611- 0 , // cabac_init_idc (CAVLC)
612- slice_qp_delta,
613- 0 , // disable_deblocking_filter_idc (enabled)
614- 0 , // slice_alpha_c0_offset_div2
615- 0 , // slice_beta_offset_div2
616- ) ,
617- ) )
556+ BufferType :: EncSliceParameter ( EncSliceParameter :: H264 ( EncSliceParameterBufferH264 :: new (
557+ 0 , // macroblock_address (start of slice)
558+ self . num_mbs , // num_macroblocks
559+ VA_INVALID_ID , // macroblock_info (unused)
560+ slice_type,
561+ 0 , // pic_parameter_set_id
562+ idr_pic_id,
563+ poc, // pic_order_cnt_lsb
564+ 0 , // delta_pic_order_cnt_bottom
565+ [ 0i32 ; 2 ] , // delta_pic_order_cnt
566+ 0 , // direct_spatial_mv_pred_flag
567+ num_ref_idx_active_override_flag,
568+ num_ref_idx_l0_active_minus1,
569+ 0 , // num_ref_idx_l1_active_minus1
570+ ref_pic_list_0,
571+ ref_pic_list_1,
572+ 0 , // luma_log2_weight_denom
573+ 0 , // chroma_log2_weight_denom
574+ 0 , // luma_weight_l0_flag
575+ [ 0i16 ; 32 ] , // luma_weight_l0
576+ [ 0i16 ; 32 ] , // luma_offset_l0
577+ 0 , // chroma_weight_l0_flag
578+ [ [ 0i16 ; 2 ] ; 32 ] , // chroma_weight_l0
579+ [ [ 0i16 ; 2 ] ; 32 ] , // chroma_offset_l0
580+ 0 , // luma_weight_l1_flag
581+ [ 0i16 ; 32 ] , // luma_weight_l1
582+ [ 0i16 ; 32 ] , // luma_offset_l1
583+ 0 , // chroma_weight_l1_flag
584+ [ [ 0i16 ; 2 ] ; 32 ] , // chroma_weight_l1
585+ [ [ 0i16 ; 2 ] ; 32 ] , // chroma_offset_l1
586+ 0 , // cabac_init_idc (CAVLC)
587+ slice_qp_delta,
588+ 0 , // disable_deblocking_filter_idc (enabled)
589+ 0 , // slice_alpha_c0_offset_div2
590+ 0 , // slice_beta_offset_div2
591+ ) ) )
618592 }
619593
620594 /// Build the rate-control miscellaneous parameter buffer (CQP mode).
@@ -633,17 +607,16 @@ impl VaH264Encoder {
633607
634608 BufferType :: EncMiscParameter ( EncMiscParameter :: RateControl (
635609 EncMiscParameterRateControl :: new (
636- 0 , // bits_per_second (CQP → 0)
637- 100 , // target_percentage
638- 1500 , // window_size (ms)
639- self . qp , // initial_qp
640- MIN_QP , // min_qp
641- 0 , // basic_unit_size
642- rc_flags,
643- 0 , // icq_quality_factor
644- MAX_QP , // max_qp
645- 0 , // quality_factor
646- 0 , // target_frame_size
610+ 0 , // bits_per_second (CQP → 0)
611+ 100 , // target_percentage
612+ 1500 , // window_size (ms)
613+ self . qp , // initial_qp
614+ MIN_QP , // min_qp
615+ 0 , // basic_unit_size
616+ rc_flags, 0 , // icq_quality_factor
617+ MAX_QP , // max_qp
618+ 0 , // quality_factor
619+ 0 , // target_frame_size
647620 ) ,
648621 ) )
649622 }
@@ -700,15 +673,13 @@ fn resolve_low_power(display: &Display, requested: bool) -> Result<bool, String>
700673 let has_full = entrypoints. contains ( & VAEntrypoint :: VAEntrypointEncSlice ) ;
701674
702675 if !has_lp && !has_full {
703- return Err (
704- "VA-API driver does not support H.264 encoding (no EncSlice entrypoint)" . into ( ) ,
705- ) ;
676+ return Err ( "VA-API driver does not support H.264 encoding (no EncSlice entrypoint)" . into ( ) ) ;
706677 }
707678
708679 if requested {
709680 if !has_lp {
710681 return Err (
711- "low_power=true requested but VAEntrypointEncSliceLP is not supported" . into ( ) ,
682+ "low_power=true requested but VAEntrypointEncSliceLP is not supported" . into ( )
712683 ) ;
713684 }
714685 Ok ( true )
0 commit comments