Merge pull request #207 from mozilla/jpg-yuv-cleanup

Cleanup for jpegyuv and yuvjpeg
This commit is contained in:
Kornel
2016-05-25 17:30:36 +01:00
2 changed files with 22 additions and 22 deletions

View File

@@ -50,7 +50,7 @@ int main(int argc, char *argv[]) {
int chroma_height; int chroma_height;
int frame_width; int frame_width;
int yuv_size; int yuv_size;
JSAMPLE *image_buffer; JSAMPLE *jpg_buffer;
JSAMPROW yrow_pointer[16]; JSAMPROW yrow_pointer[16];
JSAMPROW cbrow_pointer[8]; JSAMPROW cbrow_pointer[8];
JSAMPROW crrow_pointer[8]; JSAMPROW crrow_pointer[8];
@@ -98,14 +98,16 @@ int main(int argc, char *argv[]) {
yuv_size = luma_width*luma_height + 2*chroma_width*chroma_height; yuv_size = luma_width*luma_height + 2*chroma_width*chroma_height;
yuv_buffer = malloc(yuv_size); yuv_buffer = malloc(yuv_size);
if (!yuv_buffer) { if (!yuv_buffer) {
fclose(jpg_fd);
fprintf(stderr, "Memory allocation failure!\n"); fprintf(stderr, "Memory allocation failure!\n");
return 1; return 1;
} }
frame_width = (cinfo.output_width + (16 - 1)) & ~(16 - 1); frame_width = (cinfo.output_width + (16 - 1)) & ~(16 - 1);
image_buffer = malloc(frame_width*16 + 2*(frame_width/2)*8); jpg_buffer = malloc(frame_width*16 + 2*(frame_width/2)*8);
if (!image_buffer) { if (!jpg_buffer) {
fclose(jpg_fd);
free(yuv_buffer); free(yuv_buffer);
fprintf(stderr, "Memory allocation failure!\n"); fprintf(stderr, "Memory allocation failure!\n");
return 1; return 1;
@@ -116,11 +118,11 @@ int main(int argc, char *argv[]) {
plane_pointer[2] = crrow_pointer; plane_pointer[2] = crrow_pointer;
for (y = 0; y < 16; y++) { for (y = 0; y < 16; y++) {
yrow_pointer[y] = &image_buffer[frame_width*y]; yrow_pointer[y] = &jpg_buffer[frame_width*y];
} }
for (y = 0; y < 8; y++) { for (y = 0; y < 8; y++) {
cbrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*y]; cbrow_pointer[y] = &jpg_buffer[frame_width*16 + (frame_width/2)*y];
crrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*(8 + y)]; crrow_pointer[y] = &jpg_buffer[frame_width*16 + (frame_width/2)*(8 + y)];
} }
while (cinfo.output_scanline < cinfo.output_height) { while (cinfo.output_scanline < cinfo.output_height) {
@@ -148,12 +150,10 @@ int main(int argc, char *argv[]) {
} }
jpeg_finish_decompress(&cinfo); jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
fclose(jpg_fd); fclose(jpg_fd);
free(jpg_buffer);
free(image_buffer);
yuv_fd = fopen(yuv_path, "wb"); yuv_fd = fopen(yuv_path, "wb");
if (!yuv_fd) { if (!yuv_fd) {
@@ -164,8 +164,8 @@ int main(int argc, char *argv[]) {
if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) { if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
fprintf(stderr, "Error writing yuv file\n"); fprintf(stderr, "Error writing yuv file\n");
} }
fclose(yuv_fd);
fclose(yuv_fd);
free(yuv_buffer); free(yuv_buffer);
return 0; return 0;

View File

@@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
FILE *yuv_fd; FILE *yuv_fd;
size_t yuv_size; size_t yuv_size;
unsigned char *yuv_buffer; unsigned char *yuv_buffer;
JSAMPLE *image_buffer; JSAMPLE *jpg_buffer;
struct jpeg_compress_struct cinfo; struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr; struct jpeg_error_mgr jerr;
FILE *jpg_fd; FILE *jpg_fd;
@@ -172,12 +172,14 @@ int main(int argc, char *argv[]) {
/* Check that the file size matches 4:2:0 yuv. */ /* Check that the file size matches 4:2:0 yuv. */
if (yuv_size != if (yuv_size !=
(size_t)luma_width*luma_height + 2*chroma_width*chroma_height) { (size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {
fclose(yuv_fd);
fprintf(stderr, "Unexpected input format!\n"); fprintf(stderr, "Unexpected input format!\n");
return 1; return 1;
} }
yuv_buffer = malloc(yuv_size); yuv_buffer = malloc(yuv_size);
if (!yuv_buffer) { if (!yuv_buffer) {
fclose(yuv_fd);
fprintf(stderr, "Memory allocation failure!\n"); fprintf(stderr, "Memory allocation failure!\n");
return 1; return 1;
} }
@@ -191,15 +193,15 @@ int main(int argc, char *argv[]) {
frame_width = (luma_width + (16 - 1)) & ~(16 - 1); frame_width = (luma_width + (16 - 1)) & ~(16 - 1);
frame_height = (luma_height + (16 - 1)) & ~(16 - 1); frame_height = (luma_height + (16 - 1)) & ~(16 - 1);
image_buffer = jpg_buffer =
malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2)); malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2));
if (!image_buffer) { if (!jpg_buffer) {
free(yuv_buffer); free(yuv_buffer);
fprintf(stderr, "Memory allocation failure!\n"); fprintf(stderr, "Memory allocation failure!\n");
return 1; return 1;
} }
extend_edge(image_buffer, frame_width, frame_height, extend_edge(jpg_buffer, frame_width, frame_height,
yuv_buffer, luma_width, luma_height, chroma_width, chroma_height); yuv_buffer, luma_width, luma_height, chroma_width, chroma_height);
free(yuv_buffer); free(yuv_buffer);
@@ -208,9 +210,9 @@ int main(int argc, char *argv[]) {
jpeg_create_compress(&cinfo); jpeg_create_compress(&cinfo);
jpg_fd = fopen(jpg_path, "wb"); jpg_fd = fopen(jpg_path, "wb");
if (!jpg_fd) { if (!jpg_fd) {
free(jpg_buffer);
fprintf(stderr, "Invalid path to JPEG file!\n"); fprintf(stderr, "Invalid path to JPEG file!\n");
free(image_buffer);
return 1; return 1;
} }
@@ -257,23 +259,21 @@ int main(int argc, char *argv[]) {
scanline = cinfo.next_scanline; scanline = cinfo.next_scanline;
for (y = 0; y < 16; y++) { for (y = 0; y < 16; y++) {
yrow_pointer[y] = &image_buffer[frame_width*(scanline + y)]; yrow_pointer[y] = &jpg_buffer[frame_width*(scanline + y)];
} }
for (y = 0; y < 8; y++) { for (y = 0; y < 8; y++) {
cbrow_pointer[y] = &image_buffer[frame_width*frame_height + cbrow_pointer[y] = &jpg_buffer[frame_width*frame_height +
(frame_width/2)*((scanline/2) + y)]; (frame_width/2)*((scanline/2) + y)];
crrow_pointer[y] = &image_buffer[frame_width*frame_height + crrow_pointer[y] = &jpg_buffer[frame_width*frame_height +
(frame_width/2)*(frame_height/2) + (frame_width/2)*((scanline/2) + y)]; (frame_width/2)*(frame_height/2) + (frame_width/2)*((scanline/2) + y)];
} }
jpeg_write_raw_data(&cinfo, plane_pointer, 16); jpeg_write_raw_data(&cinfo, plane_pointer, 16);
} }
jpeg_finish_compress(&cinfo); jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo); jpeg_destroy_compress(&cinfo);
free(image_buffer); free(jpg_buffer);
fclose(jpg_fd); fclose(jpg_fd);
return 0; return 0;