Skip to content

Commit f14594e

Browse files
committed
tmp
1 parent 6a79a22 commit f14594e

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

libgpujpeg/gpujpeg_encoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ gpujpeg_encoder_suggest_restart_interval(const struct gpujpeg_image_parameters*
231231
#define GPUJPEG_ENC_HDR_VAL_SPIFF "SPIFF"
232232
/// @}
233233

234+
/// input image is vertically flipped (bottom-up): values @ref GPUJPEG_VAL_TRUE or @ref GPUJPEG_VAL_FALSE
235+
#define GPUJPEG_ENC_OPT_FLIPPED_BOOL "enc_opt_flipped"
236+
234237
/**
235238
* sets encoder option
236239
* @retval GPUJPEG_NOERR option was sucessfully set

src/gpujpeg_encoder.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,19 @@ gpujpeg_encoder_set_option(struct gpujpeg_encoder* encoder, const char *opt, con
688688
}
689689
return GPUJPEG_NOERR;
690690
}
691+
if ( strcmp(opt, GPUJPEG_ENC_OPT_FLIPPED_BOOL) == 0 ) {
692+
if ( strcasecmp(val, GPUJPEG_VAL_TRUE) == 0 ) {
693+
encoder->coder.preprocessor.input_flipped = true;
694+
}
695+
else if ( strcasecmp(val, GPUJPEG_VAL_FALSE) == 0 ) {
696+
encoder->coder.preprocessor.input_flipped = false;
697+
}
698+
else {
699+
ERROR_MSG("Unknown option %s for " GPUJPEG_ENC_OPT_FLIPPED_BOOL "\n", val);
700+
return GPUJPEG_ERROR;
701+
}
702+
return GPUJPEG_NOERR;
703+
}
691704
ERROR_MSG("Invalid encoder option: %s!\n", opt);
692705
return GPUJPEG_ERROR;
693706
}

src/gpujpeg_preprocessor.cu

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */
465496
int
466497
gpujpeg_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: */

src/gpujpeg_preprocessor_common.cuh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "../libgpujpeg/gpujpeg_common.h"
3434
#include "../libgpujpeg/gpujpeg_type.h"
3535

36+
#ifndef __cplusplus
37+
#include <stdbool.h>
38+
#endif
39+
3640
#define RGB_8BIT_THREADS 256
3741

3842
/**
@@ -55,6 +59,7 @@ struct gpujpeg_preprocessor_data
5559

5660
struct gpujpeg_preprocessor {
5761
void* kernel; // function poitner
62+
bool input_flipped; // [preprocess only] input is flipped
5863
struct gpujpeg_preprocessor_data data;
5964
};
6065

0 commit comments

Comments
 (0)