| 6 |
| * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of the Mozilla Corporation nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from this |
| 16 | * software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 |
| 31 | /* Expects 4:2:0 YCbCr */ |
| 32 |
| 33 | /* gcc -std=c99 yuvjpeg.c -I/opt/local/include/ -L/opt/local/lib/ -ljpeg -o yuvjpeg */ |
| 34 |
| 35 | #include <errno.h> |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | #include <stdlib.h> |
| 39 |
| 40 | #include "jpeglib.h" |
| 41 |
| 42 | void extend_edge(JSAMPLE *image, int width, int height, unsigned char *yuv, |
| 43 | int luma_width, int luma_height, int chroma_width, int chroma_height) { |
| 44 | int x; |
| 45 | int y; |
| 46 |
| 47 | for (y = 0; y < luma_height; y++) { |
| 48 | for (x = 0; x < luma_width; x++) { |
| 49 | image[width*y + x] = yuv[luma_width*y + x]; |
| 50 | } |
| 51 | } |
| 52 | for (y = 0; y < chroma_height; y++) { |
| 53 | for (x = 0; x < chroma_width; x++) { |
| 54 | image[width*height + (width/2)*y + x] = |
| 55 | yuv[luma_width*luma_height + chroma_width*y + x]; |
| 56 | image[width*height + (width/2)*((height/2) + y) + x] = |
| 57 | yuv[luma_width*luma_height + chroma_width*(chroma_height + y) + x]; |
| 58 | } |
| 59 | } |
| 60 |
| 61 | /* Perform right edge extension. */ |
| 62 | for (y = 0; y < luma_height; y++) { |
| 63 | for (x = luma_width; x < width; x++) { |
| 64 | image[width*y + x] = image[width*y + (x - 1)]; |
| 65 | } |
| 66 | } |
| 67 | for (y = 0; y < chroma_height; y++) { |
| 68 | for (x = chroma_width; x < width/2; x++) { |
| 69 | image[width*height + (width/2)*y + x] = |
| 70 | image[width*height + (width/2)*y + (x - 1)]; |
| 71 | image[width*height + (width/2)*((height/2) + y) + x] = |
| 72 | image[width*height + (width/2)*((height/2) + y) + (x - 1)]; |
| 73 | } |
| 74 | } |
| 75 |
| 76 | /* Perform bottom edge extension. */ |
| 77 | for (x = 0; x < width; x++) { |
| 78 | for (y = luma_height; y < height; y++) { |
| 79 | image[width*y + x] = image[width*(y - 1) + x]; |
| 80 | } |
| 81 | } |
| 82 | for (x = 0; x < width/2; x++) { |
| 83 | for (y = chroma_height; y < height/2; y++) { |
| 84 | image[width*height + (width/2)*y + x] = |
| 85 | image[width*height + (width/2)*(y - 1) + x]; |
| 86 | image[width*height + (width/2)*((height/2) + y) + x] = |
| 87 | image[width*height + (width/2)*((height/2) + (y - 1)) + x]; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 |
| 92 | int main(int argc, char *argv[]) { |
| 93 | long quality; |
| 94 | int matches; |
| 95 | int luma_width; |
| 96 | int luma_height; |
| 97 | int chroma_width; |
| 98 | int chroma_height; |
| 99 | int frame_width; |
| 100 | int frame_height; |
| 101 | const char *yuv_path; |
| 102 | const char *jpg_path; |
| 103 | FILE *yuv_fd; |
| 104 | size_t yuv_size; |
| 105 | unsigned char *yuv_buffer; |
| 106 | JSAMPLE *jpg_buffer; |
| 107 | struct jpeg_compress_struct cinfo; |
| 108 | struct jpeg_error_mgr jerr; |
| 109 | FILE *jpg_fd; |
| 110 | JSAMPROW yrow_pointer[16]; |
| 111 | JSAMPROW cbrow_pointer[8]; |
| 112 | JSAMPROW crrow_pointer[8]; |
| 113 | JSAMPROW *plane_pointer[3]; |
| 114 | int y; |
| 115 |
| 116 | if (argc != 5) { |
| 117 | fprintf(stderr, "Required arguments:\n"); |
| 118 | fprintf(stderr, "1. JPEG quality value, 0-100\n"); |
| 119 | fprintf(stderr, "2. Image size (e.g. '512x512')\n"); |
| 120 | fprintf(stderr, "3. Path to YUV input file\n"); |
| 121 | fprintf(stderr, "4. Path to JPG output file\n"); |
| 122 | return 1; |
| 123 | } |
| 124 |
| 125 | errno = 0; |
| 126 |
| 127 | quality = strtol(argv[1], NULL, 10); |
| 128 | if (errno != 0 || quality < 0 || quality > 100) { |
| 129 | fprintf(stderr, "Invalid JPEG quality value!\n"); |
| 130 | return 1; |
| 131 | } |
| 132 |
| 133 | matches = sscanf(argv[2], "%dx%d", &luma_width, &luma_height); |
| 134 | if (matches != 2) { |
| 135 | fprintf(stderr, "Invalid image size input!\n"); |
| 136 | return 1; |
| 137 | } |
| 138 | if (luma_width <= 0 || luma_height <= 0) { |
| 139 | fprintf(stderr, "Invalid image size input!\n"); |
| 140 | return 1; |
| 141 | } |
| 142 |
| 143 | chroma_width = (luma_width + 1) >> 1; |
| 144 | chroma_height = (luma_height + 1) >> 1; |
| 145 |
| 146 | /* Will check these for validity when opening via 'fopen'. */ |
| 147 | yuv_path = argv[3]; |
| 148 | jpg_path = argv[4]; |
| 149 |
| 150 | yuv_fd = fopen(yuv_path, "r"); |
| 151 | if (!yuv_fd) { |
| 152 | fprintf(stderr, "Invalid path to YUV file!\n"); |
| 153 | return 1; |
| 154 | } |
| 155 |
| 156 | fseek(yuv_fd, 0, SEEK_END); |
| 157 | yuv_size = ftell(yuv_fd); |
| 158 | fseek(yuv_fd, 0, SEEK_SET); |
| 159 |
| 160 | /* Check that the file size matches 4:2:0 yuv. */ |
| 161 | if (yuv_size != |
| 162 | (size_t)luma_width*luma_height + 2*chroma_width*chroma_height) { |
| 163 | fclose(yuv_fd); |
| 164 | fprintf(stderr, "Unexpected input format!\n"); |
| 165 | return 1; |
| 166 | } |
| 167 |
| 168 | yuv_buffer = malloc(yuv_size); |
| 169 | if (!yuv_buffer) { |
| 170 | fclose(yuv_fd); |
| 171 | fprintf(stderr, "Memory allocation failure!\n"); |
| 172 | return 1; |
| 173 | } |
| 174 |
| 175 | if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) { |
| 176 | fprintf(stderr, "Error reading yuv file\n"); |
| 177 | }; |
| 178 |
| 179 | fclose(yuv_fd); |
| 180 |
| 181 | frame_width = (luma_width + (16 - 1)) & ~(16 - 1); |
| 182 | frame_height = (luma_height + (16 - 1)) & ~(16 - 1); |
| 183 |
| 184 | jpg_buffer = |
| 185 | malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2)); |
| 186 | if (!jpg_buffer) { |
| 187 | free(yuv_buffer); |
| 188 | fprintf(stderr, "Memory allocation failure!\n"); |
| 189 | return 1; |
| 190 | } |
| 191 |
| 192 | extend_edge(jpg_buffer, frame_width, frame_height, |
| 193 | yuv_buffer, luma_width, luma_height, chroma_width, chroma_height); |
| 194 |
| 195 | free(yuv_buffer); |
| 196 |
| 197 | cinfo.err = jpeg_std_error(&jerr); |
| 198 | jpeg_create_compress(&cinfo); |
| 199 |
| 200 | jpg_fd = fopen(jpg_path, "wb"); |
| 201 | if (!jpg_fd) { |
| 202 | free(jpg_buffer); |
| 203 | fprintf(stderr, "Invalid path to JPEG file!\n"); |
| 204 | return 1; |
| 205 | } |
| 206 |
| 207 | jpeg_stdio_dest(&cinfo, jpg_fd); |
| 208 |
| 209 | cinfo.image_width = luma_width; |
| 210 | cinfo.image_height = luma_height; |
| 211 | cinfo.input_components = 3; |
| 212 |
| 213 | cinfo.in_color_space = JCS_YCbCr; |
| 214 | jpeg_set_defaults(&cinfo); |
| 215 |
| 216 | cinfo.raw_data_in = TRUE; |
| 217 |
| 218 | cinfo.comp_info[0].h_samp_factor = 2; |
| 219 | cinfo.comp_info[0].v_samp_factor = 2; |
| 220 | cinfo.comp_info[0].dc_tbl_no = 0; |
| 221 | cinfo.comp_info[0].ac_tbl_no = 0; |
| 222 | cinfo.comp_info[0].quant_tbl_no = 0; |
| 223 |
| 224 | cinfo.comp_info[1].h_samp_factor = 1; |
| 225 | cinfo.comp_info[1].v_samp_factor = 1; |
| 226 | cinfo.comp_info[1].dc_tbl_no = 1; |
| 227 | cinfo.comp_info[1].ac_tbl_no = 1; |
| 228 | cinfo.comp_info[1].quant_tbl_no = 1; |
| 229 |
| 230 | cinfo.comp_info[2].h_samp_factor = 1; |
| 231 | cinfo.comp_info[2].v_samp_factor = 1; |
| 232 | cinfo.comp_info[2].dc_tbl_no = 1; |
| 233 | cinfo.comp_info[2].ac_tbl_no = 1; |
| 234 | cinfo.comp_info[2].quant_tbl_no = 1; |
| 235 |
| 236 | jpeg_set_quality(&cinfo, quality, TRUE); |
| 237 | cinfo.optimize_coding = TRUE; |
| 238 |
| 239 | jpeg_start_compress(&cinfo, TRUE); |
| 240 |
| 241 | plane_pointer[0] = yrow_pointer; |
| 242 | plane_pointer[1] = cbrow_pointer; |
| 243 | plane_pointer[2] = crrow_pointer; |
| 244 |
| 245 | while (cinfo.next_scanline < cinfo.image_height) { |
| 246 | int scanline; |
| 247 | scanline = cinfo.next_scanline; |
| 248 |
| 249 | for (y = 0; y < 16; y++) { |
| 250 | yrow_pointer[y] = &jpg_buffer[frame_width*(scanline + y)]; |
| 251 | } |
| 252 | for (y = 0; y < 8; y++) { |
| 253 | cbrow_pointer[y] = &jpg_buffer[frame_width*frame_height + |
| 254 | (frame_width/2)*((scanline/2) + y)]; |
| 255 | crrow_pointer[y] = &jpg_buffer[frame_width*frame_height + |
| 256 | (frame_width/2)*(frame_height/2) + (frame_width/2)*((scanline/2) + y)]; |
| 257 | } |
| 258 | jpeg_write_raw_data(&cinfo, plane_pointer, 16); |
| 259 | } |
| 260 |
| 261 | jpeg_finish_compress(&cinfo); |
| 262 | jpeg_destroy_compress(&cinfo); |
| 263 |
| 264 | free(jpg_buffer); |
| 265 | fclose(jpg_fd); |
| 266 |
| 267 | return 0; |
| 268 | } |
| 269 |