yuvjpeg.c 7.9 KB

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