y4menc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2016, Alliance for Open Media. All rights reserved
  3. *
  4. * This source code is subject to the terms of the BSD 2 Clause License and
  5. * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
  6. * was not distributed with this source code in the LICENSE file, you can
  7. * obtain it at www.aomedia.org/license/software. If the Alliance for Open
  8. * Media Patent License 1.0 was not distributed with this source code in the
  9. * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  10. */
  11. #include <assert.h>
  12. #include "common/rawenc.h"
  13. #include "common/y4menc.h"
  14. // Returns the Y4M name associated with the monochrome colorspace.
  15. static const char *monochrome_colorspace(unsigned int bit_depth) {
  16. switch (bit_depth) {
  17. case 8: return "Cmono";
  18. case 9: return "Cmono9";
  19. case 10: return "Cmono10";
  20. case 12: return "Cmono12";
  21. case 16: return "Cmono16";
  22. default: assert(0); return NULL;
  23. }
  24. }
  25. // Return the Y4M name of the 8-bit colorspace, given the chroma position and
  26. // image format.
  27. static const char *colorspace8(aom_chroma_sample_position_t csp,
  28. aom_img_fmt_t fmt) {
  29. switch (fmt) {
  30. case AOM_IMG_FMT_I444: return "C444";
  31. case AOM_IMG_FMT_I422: return "C422";
  32. default:
  33. if (csp == AOM_CSP_VERTICAL) {
  34. return "C420mpeg2 XYSCSS=420MPEG2";
  35. } else if (csp == AOM_CSP_COLOCATED) {
  36. // Note that Y4M does not have a dedicated header for colocated chroma,
  37. // and that FFMPEG interprets C420 as C420jpeg.
  38. return "C420";
  39. } else {
  40. return "C420jpeg";
  41. }
  42. }
  43. }
  44. // Return the Y4M name of the colorspace, given the bit depth and image format.
  45. static const char *colorspace(unsigned int bit_depth,
  46. aom_chroma_sample_position_t csp,
  47. aom_img_fmt_t fmt) {
  48. switch (bit_depth) {
  49. case 8: return colorspace8(csp, fmt);
  50. case 9:
  51. return fmt == AOM_IMG_FMT_I44416 ? "C444p9 XYSCSS=444P9"
  52. : fmt == AOM_IMG_FMT_I42216 ? "C422p9 XYSCSS=422P9"
  53. : "C420p9 XYSCSS=420P9";
  54. case 10:
  55. return fmt == AOM_IMG_FMT_I44416 ? "C444p10 XYSCSS=444P10"
  56. : fmt == AOM_IMG_FMT_I42216 ? "C422p10 XYSCSS=422P10"
  57. : "C420p10 XYSCSS=420P10";
  58. case 12:
  59. return fmt == AOM_IMG_FMT_I44416 ? "C444p12 XYSCSS=444P12"
  60. : fmt == AOM_IMG_FMT_I42216 ? "C422p12 XYSCSS=422P12"
  61. : "C420p12 XYSCSS=420P12";
  62. case 14:
  63. return fmt == AOM_IMG_FMT_I44416 ? "C444p14 XYSCSS=444P14"
  64. : fmt == AOM_IMG_FMT_I42216 ? "C422p14 XYSCSS=422P14"
  65. : "C420p14 XYSCSS=420P14";
  66. case 16:
  67. return fmt == AOM_IMG_FMT_I44416 ? "C444p16 XYSCSS=444P16"
  68. : fmt == AOM_IMG_FMT_I42216 ? "C422p16 XYSCSS=422P16"
  69. : "C420p16 XYSCSS=420P16";
  70. default: assert(0); return NULL;
  71. }
  72. }
  73. int y4m_write_file_header(char *buf, size_t len, int width, int height,
  74. const struct AvxRational *framerate, int monochrome,
  75. aom_chroma_sample_position_t csp, aom_img_fmt_t fmt,
  76. unsigned int bit_depth, aom_color_range_t range) {
  77. const char *color = monochrome ? monochrome_colorspace(bit_depth)
  78. : colorspace(bit_depth, csp, fmt);
  79. const char *color_range = ""; // Default assumption is studio range.
  80. if (range == AOM_CR_FULL_RANGE) {
  81. color_range = " XCOLORRANGE=FULL";
  82. }
  83. return snprintf(buf, len, "YUV4MPEG2 W%d H%d F%d:%d Ip %s%s\n", width, height,
  84. framerate->numerator, framerate->denominator, color,
  85. color_range);
  86. }
  87. int y4m_write_frame_header(char *buf, size_t len) {
  88. return snprintf(buf, len, "FRAME\n");
  89. }
  90. void y4m_write_image_file(const aom_image_t *img, const int *planes,
  91. FILE *file) {
  92. int num_planes = img->monochrome ? 1 : 3;
  93. raw_write_image_file(img, planes, num_planes, file);
  94. }
  95. void y4m_update_image_md5(const aom_image_t *img, const int *planes,
  96. MD5Context *md5) {
  97. int num_planes = img->monochrome ? 1 : 3;
  98. raw_update_image_md5(img, planes, num_planes, md5);
  99. }