downsample.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*Daala video codec
  2. Copyright (c) 2002-2015 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #ifdef HAVE_CONFIG_H
  21. # include "config.h"
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "vidinput.h"
  27. #if defined(_WIN32)
  28. # include <io.h>
  29. # include <fcntl.h>
  30. #endif
  31. #include "getopt.h"
  32. static void usage(char **_argv) {
  33. fprintf(stderr, "Usage: %s [options] <input> <output>\n"
  34. " <input> must be a YUV4MPEG file.\n\n"
  35. " Options:\n\n"
  36. " --ds-algo <n> Downsampling algorithm:\n"
  37. " 0 (default) => 2x2 box average,\n"
  38. " 1 => discard every other row and every other column.\n",
  39. _argv[0]);
  40. }
  41. static const char *CHROMA_TAGS[4] = { " C420jpeg", "", " C422jpeg", " C444" };
  42. enum downsamplers {
  43. BOX_DOWNSAMPLER = 0,
  44. DISCARD_DOWNSAMPLER,
  45. DOWNSAMPLERS_MAX
  46. };
  47. int main(int _argc, char **_argv) {
  48. const char *optstring = "";
  49. const struct option long_options[] = {
  50. { "ds-algo", required_argument, NULL, 0 },
  51. { NULL, 0, NULL, 0 }
  52. };
  53. FILE *fin;
  54. FILE *fout;
  55. video_input vid;
  56. video_input_info info;
  57. int frameno;
  58. int pli;
  59. unsigned char *buf[3];
  60. int buf_stride[3];
  61. int xdec[3];
  62. int ydec[3];
  63. int w[3];
  64. int h[3];
  65. int long_option_index;
  66. int c;
  67. int ds_algo;
  68. ds_algo = BOX_DOWNSAMPLER;
  69. while ((c = getopt_long(_argc, _argv, optstring, long_options,
  70. &long_option_index)) != EOF) {
  71. switch (c) {
  72. case 0: {
  73. if (strcmp(long_options[long_option_index].name,"ds-algo") == 0) {
  74. ds_algo = atoi(optarg);
  75. if (ds_algo < 0 || ds_algo >= DOWNSAMPLERS_MAX) {
  76. fprintf(stderr, "Invalid value for --ds-algo");
  77. exit(1);
  78. }
  79. }
  80. break;
  81. }
  82. default: {
  83. usage(_argv);
  84. exit(EXIT_FAILURE);
  85. }
  86. }
  87. }
  88. if (optind+2 != _argc) {
  89. usage(_argv);
  90. exit(EXIT_FAILURE);
  91. }
  92. fin = strcmp(_argv[optind], "-") == 0 ? stdin : fopen(_argv[optind], "rb");
  93. if (fin == NULL) {
  94. fprintf(stderr, "Unable to open '%s' for extraction.\n", _argv[optind]);
  95. exit(EXIT_FAILURE);
  96. }
  97. fprintf(stderr, "Opening %s as input...\n", _argv[optind]);
  98. if (video_input_open(&vid, fin) < 0) exit(EXIT_FAILURE);
  99. video_input_get_info(&vid, &info);
  100. for (pli = 0; pli < 3; pli++) {
  101. xdec[pli] = pli && !(info.pixel_fmt&1);
  102. ydec[pli] = pli && !(info.pixel_fmt&2);
  103. h[pli] = info.pic_h >> ydec[pli];
  104. w[pli] = info.pic_w >> xdec[pli];
  105. if (h[pli] & 1) {
  106. fprintf(stderr, "Plane %d has odd height: %d\n", pli, h[pli]);
  107. return 1;
  108. }
  109. if (w[pli] & 1) {
  110. fprintf(stderr, "Plane %d has odd width: %d\n", pli, w[pli]);
  111. return 1;
  112. }
  113. buf[pli]=malloc(w[pli]/2*h[pli]/2*sizeof(**buf));
  114. buf_stride[pli]=w[pli]/2;
  115. }
  116. fout = strcmp(_argv[optind+1], "-") == 0 ? stdout : fopen(_argv[optind+1],
  117. "wb");
  118. if (fout == NULL) {
  119. fprintf(stderr, "Error opening output file \"%s\".\n", _argv[optind+1]);
  120. return 1;
  121. }
  122. fprintf(fout, "YUV4MPEG2 W%i H%i F%i:%i Ip A%i:%i%s\n",
  123. info.pic_w/2, info.pic_h/2, (unsigned)info.fps_n,
  124. (unsigned)info.fps_d, info.par_n, info.par_d,
  125. CHROMA_TAGS[ydec[1] ? xdec[1] ? 0 : 2 : 3]);
  126. for (frameno = 0;; frameno++) {
  127. video_input_ycbcr in;
  128. int ret = 0;
  129. char tag[5];
  130. int x, y;
  131. ret = video_input_fetch_frame(&vid, in, tag);
  132. if (ret == 0) break;
  133. for (pli = 0; pli < 3; pli++) {
  134. unsigned char *src = in[pli].data;
  135. int src_stride = in[pli].stride;
  136. for (y = 0; y < h[pli]; y += 2) {
  137. for (x = 0; x < w[pli]; x += 2) {
  138. int cy = y + (info.pic_y >> ydec[pli]);
  139. int cx = x + (info.pic_x >> xdec[pli]);
  140. if (ds_algo == BOX_DOWNSAMPLER) {
  141. buf[pli][y/2*buf_stride[pli] + x/2] = (src[cy*src_stride + cx] +
  142. src[cy*src_stride + cx + 1] + src[(cy + 1)*src_stride + cx] +
  143. src[(cy + 1)*src_stride + cx + 1])/4;
  144. } else {
  145. buf[pli][y/2*buf_stride[pli] + x/2] = src[cy*src_stride + cx];
  146. }
  147. }
  148. }
  149. }
  150. fprintf(fout, "FRAME\n");
  151. for (pli = 0; pli < 3; pli++) {
  152. if (fwrite(buf[pli], w[pli]/2*h[pli]/2, 1, fout) < 1) {
  153. fprintf(stderr, "Error writing to output.\n");
  154. return EXIT_FAILURE;
  155. }
  156. }
  157. fprintf(stderr, "Completed frame %d.\n", frameno);
  158. }
  159. video_input_close(&vid);
  160. if (fout != stdout) fclose(fout);
  161. return EXIT_SUCCESS;
  162. }