Cleanup for jpegyuv and yuvjpeg

This commit is contained in:
Josh Aas
2016-05-25 13:36:12 +02:00
parent a53303b82e
commit 5e01695e2d
2 changed files with 22 additions and 22 deletions

View File

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

View File

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