@@ -461,6 +461,37 @@ gpujpeg_preprocessor_encoder_copy_planar_data(struct gpujpeg_encoder * encoder)
461461 return 0 ;
462462}
463463
464+ static __global__ void vertical_flip_kernel (uint32_t *data,
465+ int width, // image linesize/4
466+ int height // image height in pixels
467+ ) {
468+ int x = blockIdx .x * blockDim .x + threadIdx .x ; // column index
469+ int y = blockIdx .y * blockDim .y + threadIdx .y ; // row index
470+
471+ if (x < width) {
472+ // Flipped row index
473+ int flipped_y = height - 1 - y;
474+ uint32_t tmp = data[y * width + x];
475+ data[y * width + x] = data[flipped_y * width + x];
476+ data[flipped_y * width + x] = tmp;
477+ }
478+ }
479+
480+ static int
481+ flip_lines (struct gpujpeg_encoder * encoder)
482+ {
483+ struct gpujpeg_coder * coder = &encoder->coder ;
484+ for ( int i = 0 ; i < coder->param .comp_count ; ++i ) {
485+ dim3 block (256 , 1 );
486+ size_t width = coder->component [i].data_width / 4 ;
487+ int height = coder->component [i].data_height ;
488+ dim3 grid ((width + block.x - 1 ) / block.x , height / 2 ); // only half of height
489+ vertical_flip_kernel<<<grid, block>>> ((uint32_t *)coder->component [i].d_data , width, height);
490+ }
491+ gpujpeg_cuda_check_error (" Preprocessor flip failed" , return -1 );
492+ return 0 ;
493+ }
494+
464495/* Documented at declaration */
465496int
466497gpujpeg_preprocessor_encode (struct gpujpeg_encoder * encoder)
@@ -470,11 +501,15 @@ gpujpeg_preprocessor_encode(struct gpujpeg_encoder * encoder)
470501 assert (!coder->param_image .width_padding ||
471502 (coder->param_image .pixel_format == GPUJPEG_444_U8_P012 && coder->preprocessor .kernel != nullptr ));
472503
473- if (coder->preprocessor .kernel != nullptr ) {
474- return gpujpeg_preprocessor_encode_interlaced (encoder);
475- } else {
476- return gpujpeg_preprocessor_encoder_copy_planar_data (encoder);
504+ int ret = coder->preprocessor .kernel != nullptr ? gpujpeg_preprocessor_encode_interlaced (encoder)
505+ : gpujpeg_preprocessor_encoder_copy_planar_data (encoder);
506+ if (ret != 0 ) {
507+ return ret;
508+ }
509+ if (coder->preprocessor .input_flipped ) {
510+ return flip_lines (encoder);
477511 }
512+ return ret;
478513}
479514
480515/* vi: set expandtab sw=4: */
0 commit comments