blob: 49f46e39ad58c1cfbec806c732312485cef38bc8 [file] [log] [blame]
John Koleszarc377bf02010-11-02 09:11:57 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszarc377bf02010-11-02 09:11:57 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
John Koleszarc377bf02010-11-02 09:11:57 -040010 */
Tom Finegan00a35aa2013-11-14 12:37:42 -080011
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -080012#include <math.h>
Tom Finegan03848f52013-11-05 10:02:18 -080013#include <stdarg.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080014#include <stdio.h>
Tom Finegan03848f52013-11-05 10:02:18 -080015#include <stdlib.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080016#include <string.h>
Tom Finegan03848f52013-11-05 10:02:18 -080017
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -080018#include "./tools_common.h"
19
Yaowu Xuf883b422016-08-30 14:01:10 -070020#if CONFIG_AV1_ENCODER
21#include "aom/aomcx.h"
Dmitry Kovalev70d96642014-02-11 21:12:23 -080022#endif
23
Yaowu Xuf883b422016-08-30 14:01:10 -070024#if CONFIG_AV1_DECODER
25#include "aom/aomdx.h"
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080026#endif
27
John Koleszar82b1a342012-11-06 12:08:05 -080028#if defined(_WIN32) || defined(__OS2__)
John Koleszarc377bf02010-11-02 09:11:57 -040029#include <io.h>
30#include <fcntl.h>
John Koleszar82b1a342012-11-06 12:08:05 -080031
32#ifdef __OS2__
clang-format6c4d83e2016-08-08 19:03:30 -070033#define _setmode setmode
34#define _fileno fileno
35#define _O_BINARY O_BINARY
John Koleszar82b1a342012-11-06 12:08:05 -080036#endif
John Koleszarc377bf02010-11-02 09:11:57 -040037#endif
38
clang-format6c4d83e2016-08-08 19:03:30 -070039#define LOG_ERROR(label) \
40 do { \
41 const char *l = label; \
42 va_list ap; \
43 va_start(ap, fmt); \
44 if (l) fprintf(stderr, "%s: ", l); \
45 vfprintf(stderr, fmt, ap); \
46 fprintf(stderr, "\n"); \
47 va_end(ap); \
48 } while (0)
Tom Finegan03848f52013-11-05 10:02:18 -080049
John Koleszarc6b90392012-07-13 15:21:29 -070050FILE *set_binary_mode(FILE *stream) {
51 (void)stream;
John Koleszar82b1a342012-11-06 12:08:05 -080052#if defined(_WIN32) || defined(__OS2__)
John Koleszarc6b90392012-07-13 15:21:29 -070053 _setmode(_fileno(stream), _O_BINARY);
John Koleszarc377bf02010-11-02 09:11:57 -040054#endif
John Koleszarc6b90392012-07-13 15:21:29 -070055 return stream;
John Koleszarc377bf02010-11-02 09:11:57 -040056}
Tom Finegan03848f52013-11-05 10:02:18 -080057
58void die(const char *fmt, ...) {
59 LOG_ERROR(NULL);
60 usage_exit();
61}
62
63void fatal(const char *fmt, ...) {
64 LOG_ERROR("Fatal");
65 exit(EXIT_FAILURE);
66}
67
clang-format6c4d83e2016-08-08 19:03:30 -070068void warn(const char *fmt, ...) { LOG_ERROR("Warning"); }
Tom Finegan00a35aa2013-11-14 12:37:42 -080069
Yaowu Xuf883b422016-08-30 14:01:10 -070070void die_codec(aom_codec_ctx_t *ctx, const char *s) {
71 const char *detail = aom_codec_error_detail(ctx);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080072
Yaowu Xuf883b422016-08-30 14:01:10 -070073 printf("%s: %s\n", s, aom_codec_error(ctx));
clang-format6c4d83e2016-08-08 19:03:30 -070074 if (detail) printf(" %s\n", detail);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080075 exit(EXIT_FAILURE);
76}
77
Yaowu Xuf883b422016-08-30 14:01:10 -070078int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame) {
Tom Finegan00a35aa2013-11-14 12:37:42 -080079 FILE *f = input_ctx->file;
80 struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
81 int plane = 0;
82 int shortread = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070083 const int bytespp = (yuv_frame->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
Tom Finegan00a35aa2013-11-14 12:37:42 -080084
85 for (plane = 0; plane < 3; ++plane) {
86 uint8_t *ptr;
Yaowu Xuf883b422016-08-30 14:01:10 -070087 const int w = aom_img_plane_width(yuv_frame, plane);
88 const int h = aom_img_plane_height(yuv_frame, plane);
Tom Finegan00a35aa2013-11-14 12:37:42 -080089 int r;
90
91 /* Determine the correct plane based on the image format. The for-loop
92 * always counts in Y,U,V order, but this may not match the order of
93 * the data on disk.
94 */
95 switch (plane) {
96 case 1:
clang-format6c4d83e2016-08-08 19:03:30 -070097 ptr =
Yaowu Xuf883b422016-08-30 14:01:10 -070098 yuv_frame->planes[yuv_frame->fmt == AOM_IMG_FMT_YV12 ? AOM_PLANE_V
99 : AOM_PLANE_U];
Tom Finegan00a35aa2013-11-14 12:37:42 -0800100 break;
101 case 2:
clang-format6c4d83e2016-08-08 19:03:30 -0700102 ptr =
Yaowu Xuf883b422016-08-30 14:01:10 -0700103 yuv_frame->planes[yuv_frame->fmt == AOM_IMG_FMT_YV12 ? AOM_PLANE_U
104 : AOM_PLANE_V];
Tom Finegan00a35aa2013-11-14 12:37:42 -0800105 break;
clang-format6c4d83e2016-08-08 19:03:30 -0700106 default: ptr = yuv_frame->planes[plane];
Tom Finegan00a35aa2013-11-14 12:37:42 -0800107 }
108
109 for (r = 0; r < h; ++r) {
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700110 size_t needed = w * bytespp;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800111 size_t buf_position = 0;
112 const size_t left = detect->buf_read - detect->position;
113 if (left > 0) {
114 const size_t more = (left < needed) ? left : needed;
115 memcpy(ptr, detect->buf + detect->position, more);
116 buf_position = more;
117 needed -= more;
118 detect->position += more;
119 }
120 if (needed > 0) {
121 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
122 }
123
124 ptr += yuv_frame->stride[plane];
125 }
126 }
127
128 return shortread;
129}
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800130
Yaowu Xuf883b422016-08-30 14:01:10 -0700131#if CONFIG_AV1_ENCODER
Tom Fineganba02c242017-05-16 15:01:54 -0700132static const AvxInterface aom_encoders[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 { "av1", AV1_FOURCC, &aom_codec_av1_cx },
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800134};
135
Yaowu Xuf883b422016-08-30 14:01:10 -0700136int get_aom_encoder_count(void) {
137 return sizeof(aom_encoders) / sizeof(aom_encoders[0]);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800138}
139
Yaowu Xuf883b422016-08-30 14:01:10 -0700140const AvxInterface *get_aom_encoder_by_index(int i) { return &aom_encoders[i]; }
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800141
Yaowu Xuf883b422016-08-30 14:01:10 -0700142const AvxInterface *get_aom_encoder_by_name(const char *name) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800143 int i;
144
Yaowu Xuf883b422016-08-30 14:01:10 -0700145 for (i = 0; i < get_aom_encoder_count(); ++i) {
146 const AvxInterface *encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700147 if (strcmp(encoder->name, name) == 0) return encoder;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800148 }
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800149
150 return NULL;
151}
Tom Fineganba02c242017-05-16 15:01:54 -0700152#endif // CONFIG_AV1_ENCODER
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800153
Yaowu Xuf883b422016-08-30 14:01:10 -0700154#if CONFIG_AV1_DECODER
Tom Fineganba02c242017-05-16 15:01:54 -0700155static const AvxInterface aom_decoders[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700156 { "av1", AV1_FOURCC, &aom_codec_av1_dx },
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800157};
158
Yaowu Xuf883b422016-08-30 14:01:10 -0700159int get_aom_decoder_count(void) {
160 return sizeof(aom_decoders) / sizeof(aom_decoders[0]);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800161}
162
Yaowu Xuf883b422016-08-30 14:01:10 -0700163const AvxInterface *get_aom_decoder_by_index(int i) { return &aom_decoders[i]; }
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800164
Yaowu Xuf883b422016-08-30 14:01:10 -0700165const AvxInterface *get_aom_decoder_by_name(const char *name) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800166 int i;
167
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 for (i = 0; i < get_aom_decoder_count(); ++i) {
169 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700170 if (strcmp(decoder->name, name) == 0) return decoder;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800171 }
172
173 return NULL;
174}
175
Yaowu Xuf883b422016-08-30 14:01:10 -0700176const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800177 int i;
178
Yaowu Xuf883b422016-08-30 14:01:10 -0700179 for (i = 0; i < get_aom_decoder_count(); ++i) {
180 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700181 if (decoder->fourcc == fourcc) return decoder;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800182 }
183
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800184 return NULL;
185}
Tom Fineganba02c242017-05-16 15:01:54 -0700186#endif // CONFIG_AV1_DECODER
James Zern8465c932015-08-10 16:45:49 -0700187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188void aom_img_write(const aom_image_t *img, FILE *file) {
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800189 int plane;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800190
191 for (plane = 0; plane < 3; ++plane) {
192 const unsigned char *buf = img->planes[plane];
193 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700194 const int w = aom_img_plane_width(img, plane) *
195 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
196 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800197 int y;
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800198
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800199 for (y = 0; y < h; ++y) {
200 fwrite(buf, 1, w, file);
201 buf += stride;
202 }
203 }
204}
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800205
Yaowu Xuf883b422016-08-30 14:01:10 -0700206int aom_img_read(aom_image_t *img, FILE *file) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800207 int plane;
208
209 for (plane = 0; plane < 3; ++plane) {
210 unsigned char *buf = img->planes[plane];
211 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700212 const int w = aom_img_plane_width(img, plane) *
213 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
214 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800215 int y;
216
217 for (y = 0; y < h; ++y) {
clang-format6c4d83e2016-08-08 19:03:30 -0700218 if (fread(buf, 1, w, file) != (size_t)w) return 0;
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800219 buf += stride;
220 }
221 }
222
223 return 1;
224}
225
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -0800226// TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
227double sse_to_psnr(double samples, double peak, double sse) {
228 static const double kMaxPSNR = 100.0;
229
230 if (sse > 0.0) {
231 const double psnr = 10.0 * log10(samples * peak * peak / sse);
232 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
233 } else {
234 return kMaxPSNR;
235 }
236}
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700237
238// TODO(debargha): Consolidate the functions below into a separate file.
Yaowu Xuf883b422016-08-30 14:01:10 -0700239static void highbd_img_upshift(aom_image_t *dst, aom_image_t *src,
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700240 int input_shift) {
241 // Note the offset is 1 less than half.
242 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
243 int plane;
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700244 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700245 dst->x_chroma_shift != src->x_chroma_shift ||
clang-format6c4d83e2016-08-08 19:03:30 -0700246 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
247 input_shift < 0) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700248 fatal("Unsupported image conversion");
249 }
250 switch (src->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700251 case AOM_IMG_FMT_I42016:
252 case AOM_IMG_FMT_I42216:
253 case AOM_IMG_FMT_I44416:
254 case AOM_IMG_FMT_I44016: break;
clang-format6c4d83e2016-08-08 19:03:30 -0700255 default: fatal("Unsupported image conversion"); break;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700256 }
257 for (plane = 0; plane < 3; plane++) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700258 int w = src->d_w;
259 int h = src->d_h;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700260 int x, y;
261 if (plane) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700262 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
263 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700264 }
265 for (y = 0; y < h; y++) {
266 uint16_t *p_src =
267 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
268 uint16_t *p_dst =
269 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
clang-format6c4d83e2016-08-08 19:03:30 -0700270 for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700271 }
272 }
273}
274
Yaowu Xuf883b422016-08-30 14:01:10 -0700275static void lowbd_img_upshift(aom_image_t *dst, aom_image_t *src,
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700276 int input_shift) {
277 // Note the offset is 1 less than half.
278 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
279 int plane;
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700280 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700281 dst->x_chroma_shift != src->x_chroma_shift ||
282 dst->y_chroma_shift != src->y_chroma_shift ||
Yaowu Xuf883b422016-08-30 14:01:10 -0700283 dst->fmt != src->fmt + AOM_IMG_FMT_HIGHBITDEPTH || input_shift < 0) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700284 fatal("Unsupported image conversion");
285 }
286 switch (src->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700287 case AOM_IMG_FMT_I420:
288 case AOM_IMG_FMT_I422:
289 case AOM_IMG_FMT_I444:
290 case AOM_IMG_FMT_I440: break;
clang-format6c4d83e2016-08-08 19:03:30 -0700291 default: fatal("Unsupported image conversion"); break;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700292 }
293 for (plane = 0; plane < 3; plane++) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700294 int w = src->d_w;
295 int h = src->d_h;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700296 int x, y;
297 if (plane) {
298 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
299 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
300 }
301 for (y = 0; y < h; y++) {
302 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
303 uint16_t *p_dst =
304 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
305 for (x = 0; x < w; x++) {
306 *p_dst++ = (*p_src++ << input_shift) + offset;
307 }
308 }
309 }
310}
311
Yaowu Xuf883b422016-08-30 14:01:10 -0700312void aom_img_upshift(aom_image_t *dst, aom_image_t *src, int input_shift) {
313 if (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700314 highbd_img_upshift(dst, src, input_shift);
315 } else {
316 lowbd_img_upshift(dst, src, input_shift);
317 }
318}
319
Yaowu Xuf883b422016-08-30 14:01:10 -0700320void aom_img_truncate_16_to_8(aom_image_t *dst, aom_image_t *src) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700321 int plane;
Yaowu Xuf883b422016-08-30 14:01:10 -0700322 if (dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w ||
clang-format6c4d83e2016-08-08 19:03:30 -0700323 dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift ||
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700324 dst->y_chroma_shift != src->y_chroma_shift) {
325 fatal("Unsupported image conversion");
326 }
327 switch (dst->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700328 case AOM_IMG_FMT_I420:
329 case AOM_IMG_FMT_I422:
330 case AOM_IMG_FMT_I444:
331 case AOM_IMG_FMT_I440: break;
clang-format6c4d83e2016-08-08 19:03:30 -0700332 default: fatal("Unsupported image conversion"); break;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700333 }
334 for (plane = 0; plane < 3; plane++) {
335 int w = src->d_w;
336 int h = src->d_h;
337 int x, y;
338 if (plane) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700339 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
340 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700341 }
342 for (y = 0; y < h; y++) {
343 uint16_t *p_src =
344 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
345 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
346 for (x = 0; x < w; x++) {
Yaowu Xuc369daf2015-07-07 14:22:07 -0700347 *p_dst++ = (uint8_t)(*p_src++);
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700348 }
349 }
350 }
351}
352
Yaowu Xuf883b422016-08-30 14:01:10 -0700353static void highbd_img_downshift(aom_image_t *dst, aom_image_t *src,
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700354 int down_shift) {
355 int plane;
356 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
357 dst->x_chroma_shift != src->x_chroma_shift ||
clang-format6c4d83e2016-08-08 19:03:30 -0700358 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
359 down_shift < 0) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700360 fatal("Unsupported image conversion");
361 }
362 switch (src->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700363 case AOM_IMG_FMT_I42016:
364 case AOM_IMG_FMT_I42216:
365 case AOM_IMG_FMT_I44416:
366 case AOM_IMG_FMT_I44016: break;
clang-format6c4d83e2016-08-08 19:03:30 -0700367 default: fatal("Unsupported image conversion"); break;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700368 }
369 for (plane = 0; plane < 3; plane++) {
370 int w = src->d_w;
371 int h = src->d_h;
372 int x, y;
373 if (plane) {
374 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
375 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
376 }
377 for (y = 0; y < h; y++) {
378 uint16_t *p_src =
379 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
380 uint16_t *p_dst =
381 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
clang-format6c4d83e2016-08-08 19:03:30 -0700382 for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700383 }
384 }
385}
386
Yaowu Xuf883b422016-08-30 14:01:10 -0700387static void lowbd_img_downshift(aom_image_t *dst, aom_image_t *src,
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700388 int down_shift) {
389 int plane;
390 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
391 dst->x_chroma_shift != src->x_chroma_shift ||
392 dst->y_chroma_shift != src->y_chroma_shift ||
Yaowu Xuf883b422016-08-30 14:01:10 -0700393 src->fmt != dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH || down_shift < 0) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700394 fatal("Unsupported image conversion");
395 }
396 switch (dst->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700397 case AOM_IMG_FMT_I420:
398 case AOM_IMG_FMT_I422:
399 case AOM_IMG_FMT_I444:
400 case AOM_IMG_FMT_I440: break;
clang-format6c4d83e2016-08-08 19:03:30 -0700401 default: fatal("Unsupported image conversion"); break;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700402 }
403 for (plane = 0; plane < 3; plane++) {
404 int w = src->d_w;
405 int h = src->d_h;
406 int x, y;
407 if (plane) {
408 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
409 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
410 }
411 for (y = 0; y < h; y++) {
412 uint16_t *p_src =
413 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
414 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
415 for (x = 0; x < w; x++) {
416 *p_dst++ = *p_src++ >> down_shift;
417 }
418 }
419 }
420}
421
Yaowu Xuf883b422016-08-30 14:01:10 -0700422void aom_img_downshift(aom_image_t *dst, aom_image_t *src, int down_shift) {
423 if (dst->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700424 highbd_img_downshift(dst, src, down_shift);
425 } else {
426 lowbd_img_downshift(dst, src, down_shift);
427 }
428}