Dmitry Kovalev | 5ab63583 | 2014-01-17 17:02:37 -0800 | [diff] [blame] | 1 | /* |
James Zern | b7c05bd | 2024-06-11 19:15:10 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved. |
Dmitry Kovalev | 5ab63583 | 2014-01-17 17:02:37 -0800 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Dmitry Kovalev | 5ab63583 | 2014-01-17 17:02:37 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 12 | #include <assert.h> |
Tom Finegan | dd3e2a5 | 2018-05-23 14:33:09 -0700 | [diff] [blame] | 13 | |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 14 | #include "common/rawenc.h" |
Tom Finegan | dd3e2a5 | 2018-05-23 14:33:09 -0700 | [diff] [blame] | 15 | #include "common/y4menc.h" |
Dmitry Kovalev | 5ab63583 | 2014-01-17 17:02:37 -0800 | [diff] [blame] | 16 | |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 17 | // Returns the Y4M name associated with the monochrome colorspace. |
Wan-Teh Chang | dc6e2c8 | 2018-09-21 13:49:49 -0700 | [diff] [blame] | 18 | static const char *monochrome_colorspace(unsigned int bit_depth) { |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 19 | switch (bit_depth) { |
| 20 | case 8: return "Cmono"; |
| 21 | case 9: return "Cmono9"; |
| 22 | case 10: return "Cmono10"; |
| 23 | case 12: return "Cmono12"; |
| 24 | case 16: return "Cmono16"; |
| 25 | default: assert(0); return NULL; |
| 26 | } |
| 27 | } |
| 28 | |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 29 | // Return the Y4M name of the 8-bit colorspace, given the chroma position and |
| 30 | // image format. |
James Zern | c743cce | 2023-04-12 12:29:31 -0700 | [diff] [blame] | 31 | static const char *colorspace8(aom_chroma_sample_position_t csp, |
| 32 | aom_img_fmt_t fmt) { |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 33 | switch (fmt) { |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 34 | case AOM_IMG_FMT_I444: return "C444"; |
| 35 | case AOM_IMG_FMT_I422: return "C422"; |
| 36 | default: |
| 37 | if (csp == AOM_CSP_VERTICAL) { |
| 38 | return "C420mpeg2 XYSCSS=420MPEG2"; |
elliottk | 6dbf774 | 2018-10-30 17:26:38 -0700 | [diff] [blame] | 39 | } else if (csp == AOM_CSP_COLOCATED) { |
| 40 | // Note that Y4M does not have a dedicated header for colocated chroma, |
| 41 | // and that FFMPEG interprets C420 as C420jpeg. |
| 42 | return "C420"; |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 43 | } else { |
| 44 | return "C420jpeg"; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 49 | // Return the Y4M name of the colorspace, given the bit depth and image format. |
Wan-Teh Chang | dc6e2c8 | 2018-09-21 13:49:49 -0700 | [diff] [blame] | 50 | static const char *colorspace(unsigned int bit_depth, |
| 51 | aom_chroma_sample_position_t csp, |
| 52 | aom_img_fmt_t fmt) { |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 53 | switch (bit_depth) { |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 54 | case 8: return colorspace8(csp, fmt); |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 55 | case 9: |
clang-format | 64f68f0 | 2022-08-25 17:42:36 -0700 | [diff] [blame] | 56 | return fmt == AOM_IMG_FMT_I44416 ? "C444p9 XYSCSS=444P9" |
| 57 | : fmt == AOM_IMG_FMT_I42216 ? "C422p9 XYSCSS=422P9" |
| 58 | : "C420p9 XYSCSS=420P9"; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 59 | case 10: |
clang-format | 64f68f0 | 2022-08-25 17:42:36 -0700 | [diff] [blame] | 60 | return fmt == AOM_IMG_FMT_I44416 ? "C444p10 XYSCSS=444P10" |
| 61 | : fmt == AOM_IMG_FMT_I42216 ? "C422p10 XYSCSS=422P10" |
| 62 | : "C420p10 XYSCSS=420P10"; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 63 | case 12: |
clang-format | 64f68f0 | 2022-08-25 17:42:36 -0700 | [diff] [blame] | 64 | return fmt == AOM_IMG_FMT_I44416 ? "C444p12 XYSCSS=444P12" |
| 65 | : fmt == AOM_IMG_FMT_I42216 ? "C422p12 XYSCSS=422P12" |
| 66 | : "C420p12 XYSCSS=420P12"; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 67 | case 14: |
clang-format | 64f68f0 | 2022-08-25 17:42:36 -0700 | [diff] [blame] | 68 | return fmt == AOM_IMG_FMT_I44416 ? "C444p14 XYSCSS=444P14" |
| 69 | : fmt == AOM_IMG_FMT_I42216 ? "C422p14 XYSCSS=422P14" |
| 70 | : "C420p14 XYSCSS=420P14"; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 71 | case 16: |
clang-format | 64f68f0 | 2022-08-25 17:42:36 -0700 | [diff] [blame] | 72 | return fmt == AOM_IMG_FMT_I44416 ? "C444p16 XYSCSS=444P16" |
| 73 | : fmt == AOM_IMG_FMT_I42216 ? "C422p16 XYSCSS=422P16" |
| 74 | : "C420p16 XYSCSS=420P16"; |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 75 | default: assert(0); return NULL; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 76 | } |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | int y4m_write_file_header(char *buf, size_t len, int width, int height, |
| 80 | const struct AvxRational *framerate, int monochrome, |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 81 | aom_chroma_sample_position_t csp, aom_img_fmt_t fmt, |
Elliott Karpilovsky | b2d8258 | 2021-02-02 22:49:56 -0800 | [diff] [blame] | 82 | unsigned int bit_depth, aom_color_range_t range) { |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 83 | const char *color = monochrome ? monochrome_colorspace(bit_depth) |
elliottk | 27d46e3 | 2018-09-13 12:38:08 -0700 | [diff] [blame] | 84 | : colorspace(bit_depth, csp, fmt); |
Elliott Karpilovsky | b2d8258 | 2021-02-02 22:49:56 -0800 | [diff] [blame] | 85 | const char *color_range = ""; // Default assumption is studio range. |
| 86 | if (range == AOM_CR_FULL_RANGE) { |
| 87 | color_range = " XCOLORRANGE=FULL"; |
| 88 | } |
| 89 | return snprintf(buf, len, "YUV4MPEG2 W%d H%d F%d:%d Ip %s%s\n", width, height, |
| 90 | framerate->numerator, framerate->denominator, color, |
| 91 | color_range); |
Dmitry Kovalev | 5ab63583 | 2014-01-17 17:02:37 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Dmitry Kovalev | 5260b39 | 2014-01-28 11:12:58 -0800 | [diff] [blame] | 94 | int y4m_write_frame_header(char *buf, size_t len) { |
| 95 | return snprintf(buf, len, "FRAME\n"); |
Dmitry Kovalev | 5ab63583 | 2014-01-17 17:02:37 -0800 | [diff] [blame] | 96 | } |
elliottk | 6e2aac2 | 2018-08-31 11:08:44 -0700 | [diff] [blame] | 97 | |
| 98 | void y4m_write_image_file(const aom_image_t *img, const int *planes, |
| 99 | FILE *file) { |
| 100 | int num_planes = img->monochrome ? 1 : 3; |
| 101 | raw_write_image_file(img, planes, num_planes, file); |
| 102 | } |
| 103 | |
| 104 | void y4m_update_image_md5(const aom_image_t *img, const int *planes, |
| 105 | MD5Context *md5) { |
| 106 | int num_planes = img->monochrome ? 1 : 3; |
| 107 | raw_update_image_md5(img, planes, num_planes, md5); |
| 108 | } |