Skip to content

Commit e4e091a

Browse files
committed
Merge pull request #207 from mozilla/jpg-yuv-cleanup
Cleanup for jpegyuv and yuvjpeg
2 parents 730f65f + 5e01695 commit e4e091a

2 files changed

Lines changed: 22 additions & 22 deletions

File tree

jpegyuv.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(int argc, char *argv[]) {
5050
int chroma_height;
5151
int frame_width;
5252
int yuv_size;
53-
JSAMPLE *image_buffer;
53+
JSAMPLE *jpg_buffer;
5454
JSAMPROW yrow_pointer[16];
5555
JSAMPROW cbrow_pointer[8];
5656
JSAMPROW crrow_pointer[8];
@@ -98,14 +98,16 @@ int main(int argc, char *argv[]) {
9898
yuv_size = luma_width*luma_height + 2*chroma_width*chroma_height;
9999
yuv_buffer = malloc(yuv_size);
100100
if (!yuv_buffer) {
101+
fclose(jpg_fd);
101102
fprintf(stderr, "Memory allocation failure!\n");
102103
return 1;
103104
}
104105

105106
frame_width = (cinfo.output_width + (16 - 1)) & ~(16 - 1);
106107

107-
image_buffer = malloc(frame_width*16 + 2*(frame_width/2)*8);
108-
if (!image_buffer) {
108+
jpg_buffer = malloc(frame_width*16 + 2*(frame_width/2)*8);
109+
if (!jpg_buffer) {
110+
fclose(jpg_fd);
109111
free(yuv_buffer);
110112
fprintf(stderr, "Memory allocation failure!\n");
111113
return 1;
@@ -116,11 +118,11 @@ int main(int argc, char *argv[]) {
116118
plane_pointer[2] = crrow_pointer;
117119

118120
for (y = 0; y < 16; y++) {
119-
yrow_pointer[y] = &image_buffer[frame_width*y];
121+
yrow_pointer[y] = &jpg_buffer[frame_width*y];
120122
}
121123
for (y = 0; y < 8; y++) {
122-
cbrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*y];
123-
crrow_pointer[y] = &image_buffer[frame_width*16 + (frame_width/2)*(8 + y)];
124+
cbrow_pointer[y] = &jpg_buffer[frame_width*16 + (frame_width/2)*y];
125+
crrow_pointer[y] = &jpg_buffer[frame_width*16 + (frame_width/2)*(8 + y)];
124126
}
125127

126128
while (cinfo.output_scanline < cinfo.output_height) {
@@ -148,12 +150,10 @@ int main(int argc, char *argv[]) {
148150
}
149151

150152
jpeg_finish_decompress(&cinfo);
151-
152153
jpeg_destroy_decompress(&cinfo);
153154

154155
fclose(jpg_fd);
155-
156-
free(image_buffer);
156+
free(jpg_buffer);
157157

158158
yuv_fd = fopen(yuv_path, "wb");
159159
if (!yuv_fd) {
@@ -164,8 +164,8 @@ int main(int argc, char *argv[]) {
164164
if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
165165
fprintf(stderr, "Error writing yuv file\n");
166166
}
167-
fclose(yuv_fd);
168167

168+
fclose(yuv_fd);
169169
free(yuv_buffer);
170170

171171
return 0;

yuvjpeg.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
104104
FILE *yuv_fd;
105105
size_t yuv_size;
106106
unsigned char *yuv_buffer;
107-
JSAMPLE *image_buffer;
107+
JSAMPLE *jpg_buffer;
108108
struct jpeg_compress_struct cinfo;
109109
struct jpeg_error_mgr jerr;
110110
FILE *jpg_fd;
@@ -172,12 +172,14 @@ int main(int argc, char *argv[]) {
172172
/* Check that the file size matches 4:2:0 yuv. */
173173
if (yuv_size !=
174174
(size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {
175+
fclose(yuv_fd);
175176
fprintf(stderr, "Unexpected input format!\n");
176177
return 1;
177178
}
178179

179180
yuv_buffer = malloc(yuv_size);
180181
if (!yuv_buffer) {
182+
fclose(yuv_fd);
181183
fprintf(stderr, "Memory allocation failure!\n");
182184
return 1;
183185
}
@@ -191,15 +193,15 @@ int main(int argc, char *argv[]) {
191193
frame_width = (luma_width + (16 - 1)) & ~(16 - 1);
192194
frame_height = (luma_height + (16 - 1)) & ~(16 - 1);
193195

194-
image_buffer =
196+
jpg_buffer =
195197
malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2));
196-
if (!image_buffer) {
198+
if (!jpg_buffer) {
197199
free(yuv_buffer);
198200
fprintf(stderr, "Memory allocation failure!\n");
199201
return 1;
200202
}
201203

202-
extend_edge(image_buffer, frame_width, frame_height,
204+
extend_edge(jpg_buffer, frame_width, frame_height,
203205
yuv_buffer, luma_width, luma_height, chroma_width, chroma_height);
204206

205207
free(yuv_buffer);
@@ -208,9 +210,9 @@ int main(int argc, char *argv[]) {
208210
jpeg_create_compress(&cinfo);
209211

210212
jpg_fd = fopen(jpg_path, "wb");
211-
if (!jpg_fd) {
213+
if (!jpg_fd) {
214+
free(jpg_buffer);
212215
fprintf(stderr, "Invalid path to JPEG file!\n");
213-
free(image_buffer);
214216
return 1;
215217
}
216218

@@ -257,23 +259,21 @@ int main(int argc, char *argv[]) {
257259
scanline = cinfo.next_scanline;
258260

259261
for (y = 0; y < 16; y++) {
260-
yrow_pointer[y] = &image_buffer[frame_width*(scanline + y)];
262+
yrow_pointer[y] = &jpg_buffer[frame_width*(scanline + y)];
261263
}
262264
for (y = 0; y < 8; y++) {
263-
cbrow_pointer[y] = &image_buffer[frame_width*frame_height +
265+
cbrow_pointer[y] = &jpg_buffer[frame_width*frame_height +
264266
(frame_width/2)*((scanline/2) + y)];
265-
crrow_pointer[y] = &image_buffer[frame_width*frame_height +
267+
crrow_pointer[y] = &jpg_buffer[frame_width*frame_height +
266268
(frame_width/2)*(frame_height/2) + (frame_width/2)*((scanline/2) + y)];
267269
}
268270
jpeg_write_raw_data(&cinfo, plane_pointer, 16);
269271
}
270272

271273
jpeg_finish_compress(&cinfo);
272-
273274
jpeg_destroy_compress(&cinfo);
274275

275-
free(image_buffer);
276-
276+
free(jpg_buffer);
277277
fclose(jpg_fd);
278278

279279
return 0;

0 commit comments

Comments
 (0)