xref: /aosp_15_r20/external/libaom/common/rawenc.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 <stdbool.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include "common/rawenc.h"
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker // Number of bytes to write per batch in write_greyscale.
16*77c1e3ccSAndroid Build Coastguard Worker #define BATCH_SIZE 8
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker // Interface to writing to either a file or MD5Context. Takes a pointer to
19*77c1e3ccSAndroid Build Coastguard Worker // either the file or MD5Context, the buffer, the size of each element, and
20*77c1e3ccSAndroid Build Coastguard Worker // number of elements to write. Note that size and nmemb (last two args) must
21*77c1e3ccSAndroid Build Coastguard Worker // be unsigned int, as the interface to MD5Update requires that.
22*77c1e3ccSAndroid Build Coastguard Worker typedef void (*WRITER)(void *, const uint8_t *, unsigned int, unsigned int);
23*77c1e3ccSAndroid Build Coastguard Worker 
write_file(void * fp,const uint8_t * buffer,unsigned int size,unsigned int nmemb)24*77c1e3ccSAndroid Build Coastguard Worker static void write_file(void *fp, const uint8_t *buffer, unsigned int size,
25*77c1e3ccSAndroid Build Coastguard Worker                        unsigned int nmemb) {
26*77c1e3ccSAndroid Build Coastguard Worker   fwrite(buffer, size, nmemb, (FILE *)fp);
27*77c1e3ccSAndroid Build Coastguard Worker }
28*77c1e3ccSAndroid Build Coastguard Worker 
write_md5(void * md5,const uint8_t * buffer,unsigned int size,unsigned int nmemb)29*77c1e3ccSAndroid Build Coastguard Worker static void write_md5(void *md5, const uint8_t *buffer, unsigned int size,
30*77c1e3ccSAndroid Build Coastguard Worker                       unsigned int nmemb) {
31*77c1e3ccSAndroid Build Coastguard Worker   MD5Update((MD5Context *)md5, buffer, size * nmemb);
32*77c1e3ccSAndroid Build Coastguard Worker }
33*77c1e3ccSAndroid Build Coastguard Worker 
34*77c1e3ccSAndroid Build Coastguard Worker // Writes out n neutral chroma samples (for greyscale).
write_greyscale(const aom_image_t * img,int n,WRITER writer_func,void * file_or_md5)35*77c1e3ccSAndroid Build Coastguard Worker static void write_greyscale(const aom_image_t *img, int n, WRITER writer_func,
36*77c1e3ccSAndroid Build Coastguard Worker                             void *file_or_md5) {
37*77c1e3ccSAndroid Build Coastguard Worker   // Batch 8 writes for low bit-depth, 4 writes for high bit-depth.
38*77c1e3ccSAndroid Build Coastguard Worker   int bytes_per_sample;
39*77c1e3ccSAndroid Build Coastguard Worker   union {
40*77c1e3ccSAndroid Build Coastguard Worker     uint8_t u8[BATCH_SIZE];
41*77c1e3ccSAndroid Build Coastguard Worker     uint16_t u16[BATCH_SIZE / 2];
42*77c1e3ccSAndroid Build Coastguard Worker   } batched;
43*77c1e3ccSAndroid Build Coastguard Worker   if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
44*77c1e3ccSAndroid Build Coastguard Worker     bytes_per_sample = 2;
45*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < BATCH_SIZE / 2; ++i) {
46*77c1e3ccSAndroid Build Coastguard Worker       batched.u16[i] = 1 << (img->bit_depth - 1);
47*77c1e3ccSAndroid Build Coastguard Worker     }
48*77c1e3ccSAndroid Build Coastguard Worker   } else {
49*77c1e3ccSAndroid Build Coastguard Worker     bytes_per_sample = 1;
50*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < BATCH_SIZE; ++i) {
51*77c1e3ccSAndroid Build Coastguard Worker       batched.u8[i] = 0x80;
52*77c1e3ccSAndroid Build Coastguard Worker     }
53*77c1e3ccSAndroid Build Coastguard Worker   }
54*77c1e3ccSAndroid Build Coastguard Worker   const int samples_per_batch = BATCH_SIZE / bytes_per_sample;
55*77c1e3ccSAndroid Build Coastguard Worker   const int num_batched_writes = n / samples_per_batch;
56*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_batched_writes; ++i) {
57*77c1e3ccSAndroid Build Coastguard Worker     writer_func(file_or_md5, batched.u8, sizeof(uint8_t), BATCH_SIZE);
58*77c1e3ccSAndroid Build Coastguard Worker   }
59*77c1e3ccSAndroid Build Coastguard Worker   const int remaining = n % samples_per_batch;
60*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < remaining; ++i) {
61*77c1e3ccSAndroid Build Coastguard Worker     writer_func(file_or_md5, batched.u8, sizeof(uint8_t), bytes_per_sample);
62*77c1e3ccSAndroid Build Coastguard Worker   }
63*77c1e3ccSAndroid Build Coastguard Worker }
64*77c1e3ccSAndroid Build Coastguard Worker 
65*77c1e3ccSAndroid Build Coastguard Worker // Encapsulates the logic for writing raw data to either an image file or
66*77c1e3ccSAndroid Build Coastguard Worker // to an MD5 context.
raw_write_image_file_or_md5(const aom_image_t * img,const int * planes,const int num_planes,void * file_or_md5,WRITER writer_func)67*77c1e3ccSAndroid Build Coastguard Worker static void raw_write_image_file_or_md5(const aom_image_t *img,
68*77c1e3ccSAndroid Build Coastguard Worker                                         const int *planes, const int num_planes,
69*77c1e3ccSAndroid Build Coastguard Worker                                         void *file_or_md5, WRITER writer_func) {
70*77c1e3ccSAndroid Build Coastguard Worker   const bool high_bitdepth = img->fmt & AOM_IMG_FMT_HIGHBITDEPTH;
71*77c1e3ccSAndroid Build Coastguard Worker   const int bytes_per_sample = high_bitdepth ? 2 : 1;
72*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_planes; ++i) {
73*77c1e3ccSAndroid Build Coastguard Worker     const int plane = planes[i];
74*77c1e3ccSAndroid Build Coastguard Worker     const int w = aom_img_plane_width(img, plane);
75*77c1e3ccSAndroid Build Coastguard Worker     const int h = aom_img_plane_height(img, plane);
76*77c1e3ccSAndroid Build Coastguard Worker     // If we're on a color plane and the output is monochrome, write a greyscale
77*77c1e3ccSAndroid Build Coastguard Worker     // value. Since there are only YUV planes, compare against Y.
78*77c1e3ccSAndroid Build Coastguard Worker     if (img->monochrome && plane != AOM_PLANE_Y) {
79*77c1e3ccSAndroid Build Coastguard Worker       write_greyscale(img, w * h, writer_func, file_or_md5);
80*77c1e3ccSAndroid Build Coastguard Worker       continue;
81*77c1e3ccSAndroid Build Coastguard Worker     }
82*77c1e3ccSAndroid Build Coastguard Worker     const unsigned char *buf = img->planes[plane];
83*77c1e3ccSAndroid Build Coastguard Worker     const int stride = img->stride[plane];
84*77c1e3ccSAndroid Build Coastguard Worker     for (int y = 0; y < h; ++y) {
85*77c1e3ccSAndroid Build Coastguard Worker       writer_func(file_or_md5, buf, bytes_per_sample, w);
86*77c1e3ccSAndroid Build Coastguard Worker       buf += stride;
87*77c1e3ccSAndroid Build Coastguard Worker     }
88*77c1e3ccSAndroid Build Coastguard Worker   }
89*77c1e3ccSAndroid Build Coastguard Worker }
90*77c1e3ccSAndroid Build Coastguard Worker 
raw_write_image_file(const aom_image_t * img,const int * planes,const int num_planes,FILE * file)91*77c1e3ccSAndroid Build Coastguard Worker void raw_write_image_file(const aom_image_t *img, const int *planes,
92*77c1e3ccSAndroid Build Coastguard Worker                           const int num_planes, FILE *file) {
93*77c1e3ccSAndroid Build Coastguard Worker   raw_write_image_file_or_md5(img, planes, num_planes, file, write_file);
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker 
raw_update_image_md5(const aom_image_t * img,const int * planes,const int num_planes,MD5Context * md5)96*77c1e3ccSAndroid Build Coastguard Worker void raw_update_image_md5(const aom_image_t *img, const int *planes,
97*77c1e3ccSAndroid Build Coastguard Worker                           const int num_planes, MD5Context *md5) {
98*77c1e3ccSAndroid Build Coastguard Worker   raw_write_image_file_or_md5(img, planes, num_planes, md5, write_md5);
99*77c1e3ccSAndroid Build Coastguard Worker }
100