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