Merge pull request #62 from negge/master

Updating yuvjpeg and jpegyuv to match Daala tools.
This commit is contained in:
Josh Aas
2014-07-07 13:07:07 -05:00
2 changed files with 153 additions and 44 deletions

View File

@@ -48,14 +48,20 @@ int main(int argc, char *argv[]) {
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *jpg_fd;
int width;
int height;
int luma_width;
int luma_height;
int chroma_width;
int chroma_height;
int frame_width;
int frame_height;
int yuv_size;
JSAMPLE *image_buffer;
JSAMPROW yrow_pointer[16];
JSAMPROW cbrow_pointer[8];
JSAMPROW crrow_pointer[8];
JSAMPROW *plane_pointer[3];
unsigned char *yuv_buffer;
int x;
int y;
FILE *yuv_fd;
@@ -90,32 +96,54 @@ int main(int argc, char *argv[]) {
jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
/* Right now we only support dimensions that are multiples of 16. */
if ((width % 16) != 0 || (height % 16) != 0) {
fprintf(stderr, "Image dimensions must be multiples of 16!\n");
return 1;
}
luma_width = cinfo.output_width;
luma_height = cinfo.output_height;
yuv_size = (width * height) + (((width >> 1) * (height >> 1)) * 2);
image_buffer = malloc(yuv_size);
chroma_width = (luma_width + 1) >> 1;
chroma_height = (luma_height + 1) >> 1;
yuv_size = luma_width*luma_height + 2*chroma_width*chroma_height;
yuv_buffer = malloc(yuv_size);
frame_width = (cinfo.output_width + (16 - 1)) & ~(16 - 1);
frame_height = (cinfo.output_height + (16 - 1)) & ~(16 - 1);
image_buffer = malloc(frame_width*16 + 2*(frame_width/2)*8);
plane_pointer[0] = yrow_pointer;
plane_pointer[1] = cbrow_pointer;
plane_pointer[2] = crrow_pointer;
for (y = 0; y < 16; y++) {
yrow_pointer[y] = &image_buffer[frame_width*y];
}
for (y = 0; y < 8; y++) {
cbrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*y];
crrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*(8 + y)];
}
while (cinfo.output_scanline < cinfo.output_height) {
for (y = 0; y < 16; y++) {
yrow_pointer[y] = image_buffer + cinfo.image_width * (cinfo.output_scanline + y);
}
for (y = 0; y < 8; y++) {
cbrow_pointer[y] = image_buffer + width * height +
((width + 1) >> 1) * ((cinfo.output_scanline >> 1) + y);
crrow_pointer[y] = image_buffer + width * height +
((width + 1) >> 1) * ((height + 1) >> 1) +
((width + 1) >> 1) * ((cinfo.output_scanline >> 1) + y);
}
int luma_scanline;
int chroma_scanline;
luma_scanline = cinfo.output_scanline;
chroma_scanline = (luma_scanline + 1) >> 1;
jpeg_read_raw_data(&cinfo, plane_pointer, 16);
for (y = 0; y < 16 && luma_scanline + y < luma_height; y++) {
for (x = 0; x < luma_width; x++) {
yuv_buffer[luma_width*(luma_scanline + y) + x] = yrow_pointer[y][x];
}
}
for (y = 0; y < 8 && chroma_scanline + y < chroma_height; y++) {
for (x = 0; x < chroma_width; x++) {
yuv_buffer[luma_width*luma_height +
chroma_width*(chroma_scanline + y) + x] = cbrow_pointer[y][x];
yuv_buffer[luma_width*luma_height + chroma_width*chroma_height +
chroma_width*(chroma_scanline + y) + x] = crrow_pointer[y][x];
}
}
}
jpeg_finish_decompress(&cinfo);
@@ -124,15 +152,19 @@ int main(int argc, char *argv[]) {
fclose(jpg_fd);
free(image_buffer);
yuv_fd = fopen(yuv_path, "wb");
if (!yuv_fd) {
fprintf(stderr, "Invalid path to YUV file!");
return 1;
}
if (fwrite(image_buffer, yuv_size, 1, yuv_fd)!=1) {
if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
fprintf(stderr, "Error writing yuv file\n");
}
fclose(yuv_fd);
free(yuv_buffer);
return 0;
}

121
yuvjpeg.c
View File

@@ -41,16 +41,71 @@
#include "jpeglib.h"
void extend_edge(JSAMPLE *image, int width, int height, unsigned char *yuv,
int luma_width, int luma_height, int chroma_width, int chroma_height) {
int x;
int y;
for (y = 0; y < luma_height; y++) {
for (x = 0; x < luma_width; x++) {
image[width*y + x] = yuv[luma_width*y + x];
}
}
for (y = 0; y < chroma_height; y++) {
for (x = 0; x < chroma_width; x++) {
image[width*height + (width/2)*y + x] =
yuv[luma_width*luma_height + chroma_width*y + x];
image[width*height + (width/2)*((height/2) + y) + x] =
yuv[luma_width*luma_height + chroma_width*(chroma_height + y) + x];
}
}
/* Perform right edge extension. */
for (y = 0; y < luma_height; y++) {
for (x = luma_width; x < width; x++) {
image[width*y + x] = image[width*y + (x - 1)];
}
}
for (y = 0; y < chroma_height; y++) {
for (x = chroma_width; x < width/2; x++) {
image[width*height + (width/2)*y + x] =
image[width*height + (width/2)*y + (x - 1)];
image[width*height + (width/2)*((height/2) + y) + x] =
image[width*height + (width/2)*((height/2) + y) + (x - 1)];
}
}
/* Perform bottom edge extension. */
for (x = 0; x < width; x++) {
for (y = luma_height; y < height; y++) {
image[width*y + x] = image[width*(y - 1) + x];
}
}
for (x = 0; x < width/2; x++) {
for (y = chroma_height; y < height/2; y++) {
image[width*height + (width/2)*y + x] =
image[width*height + (width/2)*(y - 1) + x];
image[width*height + (width/2)*((height/2) + y) + x] =
image[width*height + (width/2)*((height/2) + (y - 1)) + x];
}
}
}
int main(int argc, char *argv[]) {
long quality;
const char *size;
char *x;
long width;
long height;
int luma_width;
int luma_height;
int chroma_width;
int chroma_height;
int frame_width;
int frame_height;
const char *yuv_path;
const char *jpg_path;
FILE *yuv_fd;
size_t yuv_size;
unsigned char *yuv_buffer;
JSAMPLE *image_buffer;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
@@ -64,7 +119,7 @@ int main(int argc, char *argv[]) {
if (argc != 5) {
fprintf(stderr, "Required arguments:\n");
fprintf(stderr, "1. JPEG quality value, 0-100\n");
fprintf(stderr, "2. Image size (e.g. '512x512'\n");
fprintf(stderr, "2. Image size (e.g. '512x512')\n");
fprintf(stderr, "3. Path to YUV input file\n");
fprintf(stderr, "4. Path to JPG output file\n");
return 1;
@@ -84,22 +139,24 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Invalid image size input!\n");
return 1;
}
width = strtol(size, NULL, 10);
luma_width = (int)strtol(size, NULL, 10);
if (errno != 0) {
fprintf(stderr, "Invalid image size input!\n");
return 1;
}
height = strtol(x + 1, NULL, 10);
luma_height = (int)strtol(x + 1, NULL, 10);
if (errno != 0) {
fprintf(stderr, "Invalid image size input!\n");
return 1;
}
/* Right now we only support dimensions that are multiples of 16. */
if ((width % 16) != 0 || (height % 16) != 0) {
fprintf(stderr, "Image dimensions must be multiples of 16!\n");
if (luma_width <= 0 || luma_height <= 0) {
fprintf(stderr, "Invalid image size input!\n");
return 1;
}
chroma_width = (luma_width + 1) >> 1;
chroma_height = (luma_height + 1) >> 1;
/* Will check these for validity when opening via 'fopen'. */
yuv_path = argv[3];
jpg_path = argv[4];
@@ -113,13 +170,32 @@ int main(int argc, char *argv[]) {
fseek(yuv_fd, 0, SEEK_END);
yuv_size = ftell(yuv_fd);
fseek(yuv_fd, 0, SEEK_SET);
image_buffer = malloc(yuv_size);
if (fread(image_buffer, yuv_size, 1, yuv_fd)!=1) {
/* Check that the file size matches 4:2:0 yuv. */
if (yuv_size !=
(size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {
fprintf(stderr, "Unexpected input format!\n");
return 1;
}
yuv_buffer = malloc(yuv_size);
if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
fprintf(stderr, "Error reading yuv file\n");
};
fclose(yuv_fd);
frame_width = (luma_width + (16 - 1)) & ~(16 - 1);
frame_height = (luma_height + (16 - 1)) & ~(16 - 1);
image_buffer =
malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2));
extend_edge(image_buffer, frame_width, frame_height,
yuv_buffer, luma_width, luma_height, chroma_width, chroma_height);
free(yuv_buffer);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
@@ -131,18 +207,16 @@ int main(int argc, char *argv[]) {
jpeg_stdio_dest(&cinfo, jpg_fd);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.use_moz_defaults = TRUE;
cinfo.image_width = luma_width;
cinfo.image_height = luma_height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;
cinfo.use_moz_defaults = TRUE;
jpeg_set_defaults(&cinfo);
cinfo.raw_data_in = TRUE;
#if JPEG_LIB_VERSION >= 70
cinfo.do_fancy_downsampling = FALSE;
#endif
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
@@ -170,16 +244,19 @@ int main(int argc, char *argv[]) {
plane_pointer[0] = yrow_pointer;
plane_pointer[1] = cbrow_pointer;
plane_pointer[2] = crrow_pointer;
while (cinfo.next_scanline < cinfo.image_height) {
int scanline;
scanline = cinfo.next_scanline;
for (y = 0; y < 16; y++) {
yrow_pointer[y] = image_buffer + cinfo.image_width * (cinfo.next_scanline + y);
yrow_pointer[y] = &image_buffer[frame_width*(scanline + y)];
}
for (y = 0; y < 8; y++) {
cbrow_pointer[y] = image_buffer + width * height +
((width + 1) >> 1) * ((cinfo.next_scanline >> 1) + y);
crrow_pointer[y] = image_buffer + width * height +
((width + 1) >> 1) * ((height + 1) >> 1) +
((width + 1) >> 1) * ((cinfo.next_scanline >> 1) + y);
cbrow_pointer[y] = &image_buffer[frame_width*frame_height +
(frame_width/2)*((scanline/2) + y)];
crrow_pointer[y] = &image_buffer[frame_width*frame_height +
(frame_width/2)*(frame_height/2) + (frame_width/2)*((scanline/2) + y)];
}
jpeg_write_raw_data(&cinfo, plane_pointer, 16);
}