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