yuvjpeg.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Written by Josh Aas and Tim Terriberry
  3. * Copyright (c) 2013, Mozilla Corporation
  4. * All rights reserved.
  5. *
  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. /* Expects 4:2:0 YCbCr */
  31. /* gcc -std=c99 yuvjpeg.c -I/opt/local/include/ -L/opt/local/lib/ -ljpeg -o yuvjpeg */
  32. #include <errno.h>
  33. #include <stdio.h>
  34. #include <inttypes.h>
  35. #include <sys/stat.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include "jpeglib.h"
  39. void extend_edge(JSAMPLE *image, int width, int height, unsigned char *yuv,
  40. int luma_width, int luma_height, int chroma_width, int chroma_height) {
  41. int x;
  42. int y;
  43. for (y = 0; y < luma_height; y++) {
  44. for (x = 0; x < luma_width; x++) {
  45. image[width*y + x] = yuv[luma_width*y + x];
  46. }
  47. }
  48. for (y = 0; y < chroma_height; y++) {
  49. for (x = 0; x < chroma_width; x++) {
  50. image[width*height + (width/2)*y + x] =
  51. yuv[luma_width*luma_height + chroma_width*y + x];
  52. image[width*height + (width/2)*((height/2) + y) + x] =
  53. yuv[luma_width*luma_height + chroma_width*(chroma_height + y) + x];
  54. }
  55. }
  56. /* Perform right edge extension. */
  57. for (y = 0; y < luma_height; y++) {
  58. for (x = luma_width; x < width; x++) {
  59. image[width*y + x] = image[width*y + (x - 1)];
  60. }
  61. }
  62. for (y = 0; y < chroma_height; y++) {
  63. for (x = chroma_width; x < width/2; x++) {
  64. image[width*height + (width/2)*y + x] =
  65. image[width*height + (width/2)*y + (x - 1)];
  66. image[width*height + (width/2)*((height/2) + y) + x] =
  67. image[width*height + (width/2)*((height/2) + y) + (x - 1)];
  68. }
  69. }
  70. /* Perform bottom edge extension. */
  71. for (x = 0; x < width; x++) {
  72. for (y = luma_height; y < height; y++) {
  73. image[width*y + x] = image[width*(y - 1) + x];
  74. }
  75. }
  76. for (x = 0; x < width/2; x++) {
  77. for (y = chroma_height; y < height/2; y++) {
  78. image[width*height + (width/2)*y + x] =
  79. image[width*height + (width/2)*(y - 1) + x];
  80. image[width*height + (width/2)*((height/2) + y) + x] =
  81. image[width*height + (width/2)*((height/2) + (y - 1)) + x];
  82. }
  83. }
  84. }
  85. int main(int argc, char *argv[]) {
  86. long quality;
  87. const char *size;
  88. const char *x;
  89. int luma_width;
  90. int luma_height;
  91. int chroma_width;
  92. int chroma_height;
  93. int frame_width;
  94. int frame_height;
  95. const char *yuv_path;
  96. const char *jpg_path;
  97. FILE *yuv_fd;
  98. size_t yuv_size;
  99. unsigned char *yuv_buffer;
  100. JSAMPLE *image_buffer;
  101. struct jpeg_compress_struct cinfo;
  102. struct jpeg_error_mgr jerr;
  103. FILE *jpg_fd;
  104. JSAMPROW yrow_pointer[16];
  105. JSAMPROW cbrow_pointer[8];
  106. JSAMPROW crrow_pointer[8];
  107. JSAMPROW *plane_pointer[3];
  108. int y;
  109. if (argc != 5) {
  110. fprintf(stderr, "Required arguments:\n");
  111. fprintf(stderr, "1. JPEG quality value, 0-100\n");
  112. fprintf(stderr, "2. Image size (e.g. '512x512')\n");
  113. fprintf(stderr, "3. Path to YUV input file\n");
  114. fprintf(stderr, "4. Path to JPG output file\n");
  115. return 1;
  116. }
  117. errno = 0;
  118. quality = strtol(argv[1], NULL, 10);
  119. if (errno != 0 || quality < 0 || quality > 100) {
  120. fprintf(stderr, "Invalid JPEG quality value!\n");
  121. return 1;
  122. }
  123. size = argv[2];
  124. x = strchr(size, 'x');
  125. if (!x && x != size && x != (x + strlen(x) - 1)) {
  126. fprintf(stderr, "Invalid image size input!\n");
  127. return 1;
  128. }
  129. luma_width = (int)strtol(size, NULL, 10);
  130. if (errno != 0) {
  131. fprintf(stderr, "Invalid image size input!\n");
  132. return 1;
  133. }
  134. luma_height = (int)strtol(x + 1, NULL, 10);
  135. if (errno != 0) {
  136. fprintf(stderr, "Invalid image size input!\n");
  137. return 1;
  138. }
  139. if (luma_width <= 0 || luma_height <= 0) {
  140. fprintf(stderr, "Invalid image size input!\n");
  141. return 1;
  142. }
  143. chroma_width = (luma_width + 1) >> 1;
  144. chroma_height = (luma_height + 1) >> 1;
  145. /* Will check these for validity when opening via 'fopen'. */
  146. yuv_path = argv[3];
  147. jpg_path = argv[4];
  148. yuv_fd = fopen(yuv_path, "r");
  149. if (!yuv_fd) {
  150. fprintf(stderr, "Invalid path to YUV file!\n");
  151. return 1;
  152. }
  153. fseek(yuv_fd, 0, SEEK_END);
  154. yuv_size = ftell(yuv_fd);
  155. fseek(yuv_fd, 0, SEEK_SET);
  156. /* Check that the file size matches 4:2:0 yuv. */
  157. if (yuv_size !=
  158. (size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {
  159. fprintf(stderr, "Unexpected input format!\n");
  160. return 1;
  161. }
  162. yuv_buffer = (unsigned char *)malloc(yuv_size);
  163. if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
  164. fprintf(stderr, "Error reading yuv file\n");
  165. };
  166. fclose(yuv_fd);
  167. frame_width = (luma_width + (16 - 1)) & ~(16 - 1);
  168. frame_height = (luma_height + (16 - 1)) & ~(16 - 1);
  169. image_buffer = (JSAMPLE *)malloc(frame_width*frame_height
  170. + 2*(frame_width/2)*(frame_height/2));
  171. extend_edge(image_buffer, frame_width, frame_height,
  172. yuv_buffer, luma_width, luma_height, chroma_width, chroma_height);
  173. free(yuv_buffer);
  174. cinfo.err = jpeg_std_error(&jerr);
  175. jpeg_create_compress(&cinfo);
  176. jpg_fd = fopen(jpg_path, "wb");
  177. if (!jpg_fd) {
  178. fprintf(stderr, "Invalid path to JPEG file!\n");
  179. free(image_buffer);
  180. return 1;
  181. }
  182. jpeg_stdio_dest(&cinfo, jpg_fd);
  183. cinfo.image_width = luma_width;
  184. cinfo.image_height = luma_height;
  185. cinfo.input_components = 3;
  186. jpeg_set_defaults(&cinfo);
  187. cinfo.raw_data_in = TRUE;
  188. #if JPEG_LIB_VERSION >= 70
  189. cinfo.do_fancy_downsampling = FALSE;
  190. #endif
  191. cinfo.in_color_space = JCS_YCbCr;
  192. cinfo.comp_info[0].h_samp_factor = 2;
  193. cinfo.comp_info[0].v_samp_factor = 2;
  194. cinfo.comp_info[0].dc_tbl_no = 0;
  195. cinfo.comp_info[0].ac_tbl_no = 0;
  196. cinfo.comp_info[0].quant_tbl_no = 0;
  197. cinfo.comp_info[1].h_samp_factor = 1;
  198. cinfo.comp_info[1].v_samp_factor = 1;
  199. cinfo.comp_info[1].dc_tbl_no = 1;
  200. cinfo.comp_info[1].ac_tbl_no = 1;
  201. cinfo.comp_info[1].quant_tbl_no = 1;
  202. cinfo.comp_info[2].h_samp_factor = 1;
  203. cinfo.comp_info[2].v_samp_factor = 1;
  204. cinfo.comp_info[2].dc_tbl_no = 1;
  205. cinfo.comp_info[2].ac_tbl_no = 1;
  206. cinfo.comp_info[2].quant_tbl_no = 1;
  207. jpeg_set_quality(&cinfo, quality, TRUE);
  208. cinfo.optimize_coding = TRUE;
  209. jpeg_simple_progression(&cinfo);
  210. jpeg_start_compress(&cinfo, TRUE);
  211. plane_pointer[0] = yrow_pointer;
  212. plane_pointer[1] = cbrow_pointer;
  213. plane_pointer[2] = crrow_pointer;
  214. while (cinfo.next_scanline < cinfo.image_height) {
  215. int scanline;
  216. scanline = cinfo.next_scanline;
  217. for (y = 0; y < 16; y++) {
  218. yrow_pointer[y] = &image_buffer[frame_width*(scanline + y)];
  219. }
  220. for (y = 0; y < 8; y++) {
  221. cbrow_pointer[y] = &image_buffer[frame_width*frame_height +
  222. (frame_width/2)*((scanline/2) + y)];
  223. crrow_pointer[y] = &image_buffer[frame_width*frame_height +
  224. (frame_width/2)*(frame_height/2) + (frame_width/2)*((scanline/2) + y)];
  225. }
  226. jpeg_write_raw_data(&cinfo, plane_pointer, 16);
  227. }
  228. jpeg_finish_compress(&cinfo);
  229. jpeg_destroy_compress(&cinfo);
  230. free(image_buffer);
  231. fclose(jpg_fd);
  232. return 0;
  233. }