to_monochrome.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. " --output-mono-y4m Output as a 4:0:0 YUV4MPEG instead of keeping\n"
  37. " the original subsampling and blanking the\n"
  38. " chroma planes.\n",
  39. _argv[0]);
  40. }
  41. static const char *CHROMA_TAGS[4] = { " C420jpeg", "", " C422jpeg", " C444" };
  42. int main(int _argc, char **_argv) {
  43. const char *optstring = "";
  44. const struct option long_options[] = {
  45. { "output-mono-y4m", no_argument, NULL, 0 },
  46. { NULL, 0, NULL, 0 }
  47. };
  48. FILE *fin;
  49. FILE *fout;
  50. video_input vid;
  51. video_input_info info;
  52. int frameno;
  53. int pli;
  54. int w[3];
  55. int h[3];
  56. int xdec[3];
  57. int ydec[3];
  58. unsigned char *blank;
  59. int long_option_index;
  60. int c;
  61. int output_mono_y4m;
  62. output_mono_y4m = 0;
  63. while ((c = getopt_long(_argc, _argv, optstring, long_options,
  64. &long_option_index)) != EOF) {
  65. switch (c) {
  66. case 0: {
  67. if (strcmp(long_options[long_option_index].name,
  68. "output-mono-y4m") == 0) {
  69. output_mono_y4m = 1;
  70. }
  71. break;
  72. }
  73. default: {
  74. usage(_argv);
  75. exit(EXIT_FAILURE);
  76. }
  77. }
  78. }
  79. if (optind+2 != _argc) {
  80. usage(_argv);
  81. exit(EXIT_FAILURE);
  82. }
  83. fin = strcmp(_argv[optind], "-") == 0 ? stdin : fopen(_argv[optind], "rb");
  84. if (fin == NULL) {
  85. fprintf(stderr, "Unable to open '%s' for extraction.\n", _argv[optind]);
  86. exit(EXIT_FAILURE);
  87. }
  88. fprintf(stderr, "Opening %s as input...\n", _argv[optind]);
  89. if (video_input_open(&vid, fin) < 0) exit(EXIT_FAILURE);
  90. video_input_get_info(&vid, &info);
  91. fout = strcmp(_argv[optind+1], "-") == 0 ? stdout : fopen(_argv[optind+1],
  92. "wb");
  93. if (fout == NULL) {
  94. fprintf(stderr, "Error opening output file \"%s\".\n", _argv[optind+1]);
  95. return 1;
  96. }
  97. for (pli = 0; pli < 3; pli++) {
  98. xdec[pli] = pli && !(info.pixel_fmt&1);
  99. ydec[pli] = pli && !(info.pixel_fmt&2);
  100. w[pli] = info.pic_w >> xdec[pli];
  101. h[pli] = info.pic_h >> ydec[pli];
  102. }
  103. blank = malloc(w[0]*h[0]);
  104. memset(blank, 128, w[0]*h[0]);
  105. fprintf(fout, "YUV4MPEG2 W%i H%i F%i:%i Ip A%i:%i%s\n",
  106. info.pic_w, info.pic_h, (unsigned)info.fps_n,
  107. (unsigned)info.fps_d, info.par_n, info.par_d,
  108. output_mono_y4m ? " Cmono" : CHROMA_TAGS[ydec[1] ? xdec[1] ? 0 : 2 : 3]);
  109. for (frameno = 0;; frameno++) {
  110. video_input_ycbcr in;
  111. int ret = 0;
  112. char tag[5];
  113. unsigned char *src;
  114. int src_stride;
  115. int y;
  116. ret = video_input_fetch_frame(&vid, in, tag);
  117. if (ret == 0) break;
  118. src = in[0].data;
  119. src_stride = in[0].stride;
  120. fprintf(fout, "FRAME\n");
  121. for (y = 0; y < h[0]; y++) {
  122. if (fwrite(src + (y + info.pic_y)*src_stride + info.pic_x, w[0], 1,
  123. fout) < 1) {
  124. fprintf(stderr, "Error writing to output.\n");
  125. return EXIT_FAILURE;
  126. }
  127. }
  128. if (!output_mono_y4m) {
  129. for (pli = 1; pli < 3; pli++) {
  130. if (fwrite(blank, w[pli]*h[pli], 1, fout) < 1) {
  131. fprintf(stderr, "Error writing to output.\n");
  132. return EXIT_FAILURE;
  133. }
  134. }
  135. }
  136. fprintf(stderr, "Completed frame %d.\n", frameno);
  137. }
  138. video_input_close(&vid);
  139. if (fout != stdout) fclose(fout);
  140. return EXIT_SUCCESS;
  141. }