@@ -491,7 +491,7 @@ impl<'a, F: PixelFormat> From<OImage<F>> for CowImage<'a, F> {
491491 }
492492}
493493
494- impl < ' a , F : PixelFormat > Stride for CowImage < ' a , F > {
494+ impl < F : PixelFormat > Stride for CowImage < ' _ , F > {
495495 fn stride ( & self ) -> usize {
496496 match self {
497497 CowImage :: Borrowed ( im) => im. stride ( ) ,
@@ -500,7 +500,7 @@ impl<'a, F: PixelFormat> Stride for CowImage<'a, F> {
500500 }
501501}
502502
503- impl < ' a , F : PixelFormat > ImageData < F > for CowImage < ' a , F > {
503+ impl < F : PixelFormat > ImageData < F > for CowImage < ' _ , F > {
504504 fn width ( & self ) -> u32 {
505505 match self {
506506 CowImage :: Borrowed ( im) => im. width ( ) ,
@@ -783,10 +783,9 @@ where
783783 let width: usize = source. width ( ) . try_into ( ) . unwrap ( ) ;
784784 let height: usize = source. height ( ) . try_into ( ) . unwrap ( ) ;
785785 let stride = width * 3 ;
786- let mut rgb_buf = & mut vec ! [ 0u8 ; stride * height] ;
786+ let rgb_buf = & mut vec ! [ 0u8 ; stride * height] ;
787787 let mut tmp_rgb =
788- ImageRefMut :: new ( source. width ( ) , source. height ( ) , stride, & mut rgb_buf)
789- . unwrap ( ) ;
788+ ImageRefMut :: new ( source. width ( ) , source. height ( ) , stride, rgb_buf) . unwrap ( ) ;
790789 // The bayer code requires no padding in the input image.
791790 let exact_stride = remove_padding ( source) ?;
792791 bayer_into_rgb ( & exact_stride, & mut tmp_rgb) ?;
@@ -825,7 +824,7 @@ enum SupportedEncoding<'a> {
825824 Mono ( Box < dyn HasRowChunksExact < formats:: pixel_format:: Mono8 > + ' a > ) ,
826825}
827826
828- impl < ' a > SupportedEncoding < ' a > {
827+ impl SupportedEncoding < ' _ > {
829828 #[ inline]
830829 fn width ( & self ) -> u32 {
831830 match self {
@@ -989,6 +988,89 @@ where
989988 Ok ( result)
990989}
991990
991+ fn encode_into_nv12_inner < FMT > (
992+ frame : & dyn HasRowChunksExact < FMT > ,
993+ dest : & mut ImageBufferMutRef < NV12 > ,
994+ dest_stride : usize ,
995+ ) -> Result < ( ) >
996+ where
997+ FMT : PixelFormat ,
998+ {
999+ use itertools:: izip;
1000+
1001+ let frame = to_rgb8_or_mono8 ( frame) ?;
1002+
1003+ let luma_size = frame. height ( ) as usize * dest_stride;
1004+
1005+ let ( nv12_luma, nv12_chroma) = dest. data . split_at_mut ( luma_size) ;
1006+
1007+ match & frame {
1008+ SupportedEncoding :: Mono ( frame) => {
1009+ // ported from convertYUVpitchtoNV12 in NvEncoder.cpp
1010+ let w = frame. width ( ) as usize ;
1011+ for y in 0 ..frame. height ( ) as usize {
1012+ let start = dest_stride * y;
1013+ let src = frame. stride ( ) * y;
1014+ nv12_luma[ start..( start + w) ] . copy_from_slice ( & frame. image_data ( ) [ src..( src + w) ] ) ;
1015+ }
1016+
1017+ for y in 0 ..( frame. height ( ) as usize / 2 ) {
1018+ let start = dest_stride * y;
1019+ for x in ( 0 ..frame. width ( ) as usize ) . step_by ( 2 ) {
1020+ nv12_chroma[ start + x] = 128u8 ;
1021+ nv12_chroma[ start + ( x + 1 ) ] = 128u8 ;
1022+ }
1023+ }
1024+ }
1025+ SupportedEncoding :: Rgb ( frame) => {
1026+ let w = frame. width ( ) as usize ;
1027+
1028+ // Allocate temporary storage for full-res chroma planes.
1029+ // TODO: eliminate this or make it much smaller (e.g. two rows).
1030+ let mut u_plane_full: Vec < u8 > = vec ! [ 0 ; nv12_luma. len( ) ] ;
1031+ let mut v_plane_full: Vec < u8 > = vec ! [ 0 ; nv12_luma. len( ) ] ;
1032+
1033+ for ( src_row, dest_row, udest_row, vdest_row) in izip ! [
1034+ frame. image_data( ) . chunks_exact( frame. stride( ) ) ,
1035+ nv12_luma. chunks_exact_mut( dest_stride) ,
1036+ u_plane_full. chunks_exact_mut( dest_stride) ,
1037+ v_plane_full. chunks_exact_mut( dest_stride) ,
1038+ ] {
1039+ let yuv_iter = src_row[ ..w * 3 ]
1040+ . chunks_exact ( 3 )
1041+ . map ( |rgb| RGB888toYUV444_bt601_full_swing ( rgb[ 0 ] , rgb[ 1 ] , rgb[ 2 ] ) ) ;
1042+
1043+ let dest_iter = dest_row[ 0 ..w] . iter_mut ( ) ;
1044+
1045+ for ( ydest, udest, vdest, yuv) in izip ! [ dest_iter, udest_row, vdest_row, yuv_iter] {
1046+ * ydest = yuv. Y ;
1047+ * udest = yuv. U ;
1048+ * vdest = yuv. V ;
1049+ }
1050+ }
1051+
1052+ // Now downsample the full-res chroma planes.
1053+ let half_stride = dest_stride; // This is not half because the two channels are interleaved.
1054+ for y in 0 ..( frame. height ( ) as usize / 2 ) {
1055+ for x in 0 ..( frame. width ( ) as usize / 2 ) {
1056+ let u_sum: u16 = u_plane_full[ dest_stride * 2 * y + 2 * x] as u16
1057+ + u_plane_full[ dest_stride * 2 * y + 2 * x + 1 ] as u16
1058+ + u_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x] as u16
1059+ + u_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x + 1 ] as u16 ;
1060+ let v_sum: u16 = v_plane_full[ dest_stride * 2 * y + 2 * x] as u16
1061+ + v_plane_full[ dest_stride * 2 * y + 2 * x + 1 ] as u16
1062+ + v_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x] as u16
1063+ + v_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x + 1 ] as u16 ;
1064+
1065+ nv12_chroma[ ( half_stride * y) + 2 * x] = ( u_sum / 4 ) as u8 ;
1066+ nv12_chroma[ ( half_stride * y) + 2 * x + 1 ] = ( v_sum / 4 ) as u8 ;
1067+ }
1068+ }
1069+ }
1070+ }
1071+ Ok ( ( ) )
1072+ }
1073+
9921074#[ cfg( test) ]
9931075mod tests {
9941076 use crate :: * ;
@@ -1032,13 +1114,13 @@ mod tests {
10321114 }
10331115 }
10341116
1035- impl < ' a , F > Stride for RoiImage < ' a , F > {
1117+ impl < F > Stride for RoiImage < ' _ , F > {
10361118 fn stride ( & self ) -> usize {
10371119 self . stride
10381120 }
10391121 }
10401122
1041- impl < ' a , F > ImageData < F > for RoiImage < ' a , F > {
1123+ impl < F > ImageData < F > for RoiImage < ' _ , F > {
10421124 fn width ( & self ) -> u32 {
10431125 self . w
10441126 }
@@ -1326,86 +1408,3 @@ mod tests {
13261408 Ok ( ( ) )
13271409 }
13281410}
1329-
1330- fn encode_into_nv12_inner < FMT > (
1331- frame : & dyn HasRowChunksExact < FMT > ,
1332- dest : & mut ImageBufferMutRef < NV12 > ,
1333- dest_stride : usize ,
1334- ) -> Result < ( ) >
1335- where
1336- FMT : PixelFormat ,
1337- {
1338- use itertools:: izip;
1339-
1340- let frame = to_rgb8_or_mono8 ( frame) ?;
1341-
1342- let luma_size = frame. height ( ) as usize * dest_stride;
1343-
1344- let ( nv12_luma, nv12_chroma) = dest. data . split_at_mut ( luma_size) ;
1345-
1346- match & frame {
1347- SupportedEncoding :: Mono ( frame) => {
1348- // ported from convertYUVpitchtoNV12 in NvEncoder.cpp
1349- let w = frame. width ( ) as usize ;
1350- for y in 0 ..frame. height ( ) as usize {
1351- let start = dest_stride * y;
1352- let src = frame. stride ( ) * y;
1353- nv12_luma[ start..( start + w) ] . copy_from_slice ( & frame. image_data ( ) [ src..( src + w) ] ) ;
1354- }
1355-
1356- for y in 0 ..( frame. height ( ) as usize / 2 ) {
1357- let start = dest_stride * y;
1358- for x in ( 0 ..frame. width ( ) as usize ) . step_by ( 2 ) {
1359- nv12_chroma[ start + x] = 128u8 ;
1360- nv12_chroma[ start + ( x + 1 ) ] = 128u8 ;
1361- }
1362- }
1363- }
1364- SupportedEncoding :: Rgb ( frame) => {
1365- let w = frame. width ( ) as usize ;
1366-
1367- // Allocate temporary storage for full-res chroma planes.
1368- // TODO: eliminate this or make it much smaller (e.g. two rows).
1369- let mut u_plane_full: Vec < u8 > = vec ! [ 0 ; nv12_luma. len( ) ] ;
1370- let mut v_plane_full: Vec < u8 > = vec ! [ 0 ; nv12_luma. len( ) ] ;
1371-
1372- for ( src_row, dest_row, udest_row, vdest_row) in izip ! [
1373- frame. image_data( ) . chunks_exact( frame. stride( ) ) ,
1374- nv12_luma. chunks_exact_mut( dest_stride) ,
1375- u_plane_full. chunks_exact_mut( dest_stride) ,
1376- v_plane_full. chunks_exact_mut( dest_stride) ,
1377- ] {
1378- let yuv_iter = src_row[ ..w * 3 ]
1379- . chunks_exact ( 3 )
1380- . map ( |rgb| RGB888toYUV444_bt601_full_swing ( rgb[ 0 ] , rgb[ 1 ] , rgb[ 2 ] ) ) ;
1381-
1382- let dest_iter = dest_row[ 0 ..w] . iter_mut ( ) ;
1383-
1384- for ( ydest, udest, vdest, yuv) in izip ! [ dest_iter, udest_row, vdest_row, yuv_iter] {
1385- * ydest = yuv. Y ;
1386- * udest = yuv. U ;
1387- * vdest = yuv. V ;
1388- }
1389- }
1390-
1391- // Now downsample the full-res chroma planes.
1392- let half_stride = dest_stride; // This is not half because the two channels are interleaved.
1393- for y in 0 ..( frame. height ( ) as usize / 2 ) {
1394- for x in 0 ..( frame. width ( ) as usize / 2 ) {
1395- let u_sum: u16 = u_plane_full[ dest_stride * 2 * y + 2 * x] as u16
1396- + u_plane_full[ dest_stride * 2 * y + 2 * x + 1 ] as u16
1397- + u_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x] as u16
1398- + u_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x + 1 ] as u16 ;
1399- let v_sum: u16 = v_plane_full[ dest_stride * 2 * y + 2 * x] as u16
1400- + v_plane_full[ dest_stride * 2 * y + 2 * x + 1 ] as u16
1401- + v_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x] as u16
1402- + v_plane_full[ dest_stride * ( 2 * y + 1 ) + 2 * x + 1 ] as u16 ;
1403-
1404- nv12_chroma[ ( half_stride * y) + 2 * x] = ( u_sum / 4 ) as u8 ;
1405- nv12_chroma[ ( half_stride * y) + 2 * x + 1 ] = ( v_sum / 4 ) as u8 ;
1406- }
1407- }
1408- }
1409- }
1410- Ok ( ( ) )
1411- }
0 commit comments