| John Koleszar | c377bf0 | 2010-11-02 13:11:57 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 10 | |
| Dmitry Kovalev | 2dad0e1 | 2014-02-27 22:00:41 | [diff] [blame] | 11 | #include <math.h> |
| Tom Finegan | 03848f5 | 2013-11-05 18:02:18 | [diff] [blame] | 12 | #include <stdarg.h> |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 13 | #include <stdio.h> |
| Tom Finegan | 03848f5 | 2013-11-05 18:02:18 | [diff] [blame] | 14 | #include <stdlib.h> |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 15 | #include <string.h> |
| Tom Finegan | 03848f5 | 2013-11-05 18:02:18 | [diff] [blame] | 16 | |
| Dmitry Kovalev | 2dad0e1 | 2014-02-27 22:00:41 | [diff] [blame] | 17 | #include "./tools_common.h" |
| 18 | |
| Jim Bankoski | c96ecc2 | 2016-01-20 18:15:20 | [diff] [blame] | 19 | #if CONFIG_VP10_ENCODER |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 20 | #include "vpx/vp8cx.h" |
| 21 | #endif |
| 22 | |
| Jim Bankoski | c96ecc2 | 2016-01-20 18:15:20 | [diff] [blame] | 23 | #if CONFIG_VP10_DECODER |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 24 | #include "vpx/vp8dx.h" |
| 25 | #endif |
| 26 | |
| John Koleszar | 82b1a34 | 2012-11-06 20:08:05 | [diff] [blame] | 27 | #if defined(_WIN32) || defined(__OS2__) |
| John Koleszar | c377bf0 | 2010-11-02 13:11:57 | [diff] [blame] | 28 | #include <io.h> |
| 29 | #include <fcntl.h> |
| John Koleszar | 82b1a34 | 2012-11-06 20:08:05 | [diff] [blame] | 30 | |
| 31 | #ifdef __OS2__ |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 32 | #define _setmode setmode |
| 33 | #define _fileno fileno |
| 34 | #define _O_BINARY O_BINARY |
| John Koleszar | 82b1a34 | 2012-11-06 20:08:05 | [diff] [blame] | 35 | #endif |
| John Koleszar | c377bf0 | 2010-11-02 13:11:57 | [diff] [blame] | 36 | #endif |
| 37 | |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 38 | #define LOG_ERROR(label) \ |
| 39 | do { \ |
| 40 | const char *l = label; \ |
| 41 | va_list ap; \ |
| 42 | va_start(ap, fmt); \ |
| 43 | if (l) fprintf(stderr, "%s: ", l); \ |
| 44 | vfprintf(stderr, fmt, ap); \ |
| 45 | fprintf(stderr, "\n"); \ |
| 46 | va_end(ap); \ |
| 47 | } while (0) |
| Tom Finegan | 03848f5 | 2013-11-05 18:02:18 | [diff] [blame] | 48 | |
| John Koleszar | c6b9039 | 2012-07-13 22:21:29 | [diff] [blame] | 49 | FILE *set_binary_mode(FILE *stream) { |
| 50 | (void)stream; |
| John Koleszar | 82b1a34 | 2012-11-06 20:08:05 | [diff] [blame] | 51 | #if defined(_WIN32) || defined(__OS2__) |
| John Koleszar | c6b9039 | 2012-07-13 22:21:29 | [diff] [blame] | 52 | _setmode(_fileno(stream), _O_BINARY); |
| John Koleszar | c377bf0 | 2010-11-02 13:11:57 | [diff] [blame] | 53 | #endif |
| John Koleszar | c6b9039 | 2012-07-13 22:21:29 | [diff] [blame] | 54 | return stream; |
| John Koleszar | c377bf0 | 2010-11-02 13:11:57 | [diff] [blame] | 55 | } |
| Tom Finegan | 03848f5 | 2013-11-05 18:02:18 | [diff] [blame] | 56 | |
| 57 | void die(const char *fmt, ...) { |
| 58 | LOG_ERROR(NULL); |
| 59 | usage_exit(); |
| 60 | } |
| 61 | |
| 62 | void fatal(const char *fmt, ...) { |
| 63 | LOG_ERROR("Fatal"); |
| 64 | exit(EXIT_FAILURE); |
| 65 | } |
| 66 | |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 67 | void warn(const char *fmt, ...) { LOG_ERROR("Warning"); } |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 68 | |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 69 | void die_codec(vpx_codec_ctx_t *ctx, const char *s) { |
| 70 | const char *detail = vpx_codec_error_detail(ctx); |
| 71 | |
| 72 | printf("%s: %s\n", s, vpx_codec_error(ctx)); |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 73 | if (detail) printf(" %s\n", detail); |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 74 | exit(EXIT_FAILURE); |
| 75 | } |
| 76 | |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 77 | int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) { |
| 78 | FILE *f = input_ctx->file; |
| 79 | struct FileTypeDetectionBuffer *detect = &input_ctx->detect; |
| 80 | int plane = 0; |
| 81 | int shortread = 0; |
| Deb Mukherjee | 5acfafb | 2014-08-26 19:35:15 | [diff] [blame] | 82 | const int bytespp = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1; |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 83 | |
| 84 | for (plane = 0; plane < 3; ++plane) { |
| 85 | uint8_t *ptr; |
| Deb Mukherjee | 449e5f2 | 2014-07-11 18:43:31 | [diff] [blame] | 86 | const int w = vpx_img_plane_width(yuv_frame, plane); |
| 87 | const int h = vpx_img_plane_height(yuv_frame, plane); |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 88 | int r; |
| 89 | |
| 90 | /* Determine the correct plane based on the image format. The for-loop |
| 91 | * always counts in Y,U,V order, but this may not match the order of |
| 92 | * the data on disk. |
| 93 | */ |
| 94 | switch (plane) { |
| 95 | case 1: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 96 | ptr = |
| 97 | yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V |
| 98 | : VPX_PLANE_U]; |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 99 | break; |
| 100 | case 2: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 101 | ptr = |
| 102 | yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U |
| 103 | : VPX_PLANE_V]; |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 104 | break; |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 105 | default: ptr = yuv_frame->planes[plane]; |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | for (r = 0; r < h; ++r) { |
| Deb Mukherjee | 449e5f2 | 2014-07-11 18:43:31 | [diff] [blame] | 109 | size_t needed = w * bytespp; |
| Tom Finegan | 00a35aa | 2013-11-14 20:37:42 | [diff] [blame] | 110 | size_t buf_position = 0; |
| 111 | const size_t left = detect->buf_read - detect->position; |
| 112 | if (left > 0) { |
| 113 | const size_t more = (left < needed) ? left : needed; |
| 114 | memcpy(ptr, detect->buf + detect->position, more); |
| 115 | buf_position = more; |
| 116 | needed -= more; |
| 117 | detect->position += more; |
| 118 | } |
| 119 | if (needed > 0) { |
| 120 | shortread |= (fread(ptr + buf_position, 1, needed, f) < needed); |
| 121 | } |
| 122 | |
| 123 | ptr += yuv_frame->stride[plane]; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return shortread; |
| 128 | } |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 129 | |
| James Zern | 8465c93 | 2015-08-10 23:45:49 | [diff] [blame] | 130 | #if CONFIG_ENCODERS |
| 131 | |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 132 | static const VpxInterface vpx_encoders[] = { |
| Jingning Han | 5dccce5 | 2015-08-15 22:57:54 | [diff] [blame] | 133 | #if CONFIG_VP10_ENCODER |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 134 | { "vp10", VP10_FOURCC, &vpx_codec_vp10_cx }, |
| Jingning Han | 5dccce5 | 2015-08-15 22:57:54 | [diff] [blame] | 135 | #endif |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 136 | }; |
| 137 | |
| James Zern | 5a73bbd | 2015-05-09 17:40:51 | [diff] [blame] | 138 | int get_vpx_encoder_count(void) { |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 139 | return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]); |
| 140 | } |
| 141 | |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 142 | const VpxInterface *get_vpx_encoder_by_index(int i) { return &vpx_encoders[i]; } |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 143 | |
| 144 | const VpxInterface *get_vpx_encoder_by_name(const char *name) { |
| 145 | int i; |
| 146 | |
| 147 | for (i = 0; i < get_vpx_encoder_count(); ++i) { |
| 148 | const VpxInterface *encoder = get_vpx_encoder_by_index(i); |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 149 | if (strcmp(encoder->name, name) == 0) return encoder; |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 150 | } |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 151 | |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| James Zern | 8465c93 | 2015-08-10 23:45:49 | [diff] [blame] | 155 | #endif // CONFIG_ENCODERS |
| 156 | |
| 157 | #if CONFIG_DECODERS |
| 158 | |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 159 | static const VpxInterface vpx_decoders[] = { |
| Jingning Han | 3ee6db6 | 2015-08-06 02:00:31 | [diff] [blame] | 160 | #if CONFIG_VP10_DECODER |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 161 | { "vp10", VP10_FOURCC, &vpx_codec_vp10_dx }, |
| Jingning Han | 3ee6db6 | 2015-08-06 02:00:31 | [diff] [blame] | 162 | #endif |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 163 | }; |
| 164 | |
| James Zern | d1999cb | 2015-05-09 17:41:54 | [diff] [blame] | 165 | int get_vpx_decoder_count(void) { |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 166 | return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]); |
| 167 | } |
| 168 | |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 169 | const VpxInterface *get_vpx_decoder_by_index(int i) { return &vpx_decoders[i]; } |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 170 | |
| 171 | const VpxInterface *get_vpx_decoder_by_name(const char *name) { |
| 172 | int i; |
| 173 | |
| 174 | for (i = 0; i < get_vpx_decoder_count(); ++i) { |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 175 | const VpxInterface *const decoder = get_vpx_decoder_by_index(i); |
| 176 | if (strcmp(decoder->name, name) == 0) return decoder; |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) { |
| 183 | int i; |
| 184 | |
| 185 | for (i = 0; i < get_vpx_decoder_count(); ++i) { |
| 186 | const VpxInterface *const decoder = get_vpx_decoder_by_index(i); |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 187 | if (decoder->fourcc == fourcc) return decoder; |
| Dmitry Kovalev | 70d9664 | 2014-02-12 05:12:23 | [diff] [blame] | 188 | } |
| 189 | |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | |
| James Zern | 8465c93 | 2015-08-10 23:45:49 | [diff] [blame] | 193 | #endif // CONFIG_DECODERS |
| 194 | |
| Dmitry Kovalev | 2bdd43d | 2014-02-13 02:36:36 | [diff] [blame] | 195 | // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part |
| 196 | // of vpx_image_t support |
| 197 | int vpx_img_plane_width(const vpx_image_t *img, int plane) { |
| 198 | if (plane > 0 && img->x_chroma_shift > 0) |
| 199 | return (img->d_w + 1) >> img->x_chroma_shift; |
| 200 | else |
| 201 | return img->d_w; |
| 202 | } |
| 203 | |
| 204 | int vpx_img_plane_height(const vpx_image_t *img, int plane) { |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 205 | if (plane > 0 && img->y_chroma_shift > 0) |
| Dmitry Kovalev | 2bdd43d | 2014-02-13 02:36:36 | [diff] [blame] | 206 | return (img->d_h + 1) >> img->y_chroma_shift; |
| 207 | else |
| 208 | return img->d_h; |
| 209 | } |
| 210 | |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 211 | void vpx_img_write(const vpx_image_t *img, FILE *file) { |
| Dmitry Kovalev | 2bdd43d | 2014-02-13 02:36:36 | [diff] [blame] | 212 | int plane; |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 213 | |
| 214 | for (plane = 0; plane < 3; ++plane) { |
| 215 | const unsigned char *buf = img->planes[plane]; |
| 216 | const int stride = img->stride[plane]; |
| Deb Mukherjee | 9ed23de | 2014-09-25 22:46:50 | [diff] [blame] | 217 | const int w = vpx_img_plane_width(img, plane) * |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 218 | ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); |
| Dmitry Kovalev | 2bdd43d | 2014-02-13 02:36:36 | [diff] [blame] | 219 | const int h = vpx_img_plane_height(img, plane); |
| 220 | int y; |
| Dmitry Kovalev | 592936b | 2014-02-07 19:37:39 | [diff] [blame] | 221 | |
| Dmitry Kovalev | 7ec2769 | 2014-01-27 21:40:29 | [diff] [blame] | 222 | for (y = 0; y < h; ++y) { |
| 223 | fwrite(buf, 1, w, file); |
| 224 | buf += stride; |
| 225 | } |
| 226 | } |
| 227 | } |
| Dmitry Kovalev | 592936b | 2014-02-07 19:37:39 | [diff] [blame] | 228 | |
| 229 | int vpx_img_read(vpx_image_t *img, FILE *file) { |
| 230 | int plane; |
| 231 | |
| 232 | for (plane = 0; plane < 3; ++plane) { |
| 233 | unsigned char *buf = img->planes[plane]; |
| 234 | const int stride = img->stride[plane]; |
| Deb Mukherjee | 5acfafb | 2014-08-26 19:35:15 | [diff] [blame] | 235 | const int w = vpx_img_plane_width(img, plane) * |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 236 | ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); |
| Dmitry Kovalev | 2bdd43d | 2014-02-13 02:36:36 | [diff] [blame] | 237 | const int h = vpx_img_plane_height(img, plane); |
| Dmitry Kovalev | 592936b | 2014-02-07 19:37:39 | [diff] [blame] | 238 | int y; |
| 239 | |
| 240 | for (y = 0; y < h; ++y) { |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 241 | if (fread(buf, 1, w, file) != (size_t)w) return 0; |
| Dmitry Kovalev | 592936b | 2014-02-07 19:37:39 | [diff] [blame] | 242 | buf += stride; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return 1; |
| 247 | } |
| 248 | |
| Dmitry Kovalev | 2dad0e1 | 2014-02-27 22:00:41 | [diff] [blame] | 249 | // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t |
| 250 | double sse_to_psnr(double samples, double peak, double sse) { |
| 251 | static const double kMaxPSNR = 100.0; |
| 252 | |
| 253 | if (sse > 0.0) { |
| 254 | const double psnr = 10.0 * log10(samples * peak * peak / sse); |
| 255 | return psnr > kMaxPSNR ? kMaxPSNR : psnr; |
| 256 | } else { |
| 257 | return kMaxPSNR; |
| 258 | } |
| 259 | } |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 260 | |
| 261 | // TODO(debargha): Consolidate the functions below into a separate file. |
| Yaowu Xu | 3246fc0 | 2016-01-21 00:13:04 | [diff] [blame] | 262 | #if CONFIG_VPX_HIGHBITDEPTH |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 263 | static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src, |
| 264 | int input_shift) { |
| 265 | // Note the offset is 1 less than half. |
| 266 | const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0; |
| 267 | int plane; |
| Deb Mukherjee | 23fc1f7 | 2014-10-15 17:01:34 | [diff] [blame] | 268 | if (dst->d_w != src->d_w || dst->d_h != src->d_h || |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 269 | dst->x_chroma_shift != src->x_chroma_shift || |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 270 | dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt || |
| 271 | input_shift < 0) { |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 272 | fatal("Unsupported image conversion"); |
| 273 | } |
| 274 | switch (src->fmt) { |
| 275 | case VPX_IMG_FMT_I42016: |
| 276 | case VPX_IMG_FMT_I42216: |
| 277 | case VPX_IMG_FMT_I44416: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 278 | case VPX_IMG_FMT_I44016: break; |
| 279 | default: fatal("Unsupported image conversion"); break; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 280 | } |
| 281 | for (plane = 0; plane < 3; plane++) { |
| Deb Mukherjee | 23fc1f7 | 2014-10-15 17:01:34 | [diff] [blame] | 282 | int w = src->d_w; |
| 283 | int h = src->d_h; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 284 | int x, y; |
| 285 | if (plane) { |
| Deb Mukherjee | 23fc1f7 | 2014-10-15 17:01:34 | [diff] [blame] | 286 | w = (w + src->x_chroma_shift) >> src->x_chroma_shift; |
| 287 | h = (h + src->y_chroma_shift) >> src->y_chroma_shift; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 288 | } |
| 289 | for (y = 0; y < h; y++) { |
| 290 | uint16_t *p_src = |
| 291 | (uint16_t *)(src->planes[plane] + y * src->stride[plane]); |
| 292 | uint16_t *p_dst = |
| 293 | (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]); |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 294 | for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src, |
| 300 | int input_shift) { |
| 301 | // Note the offset is 1 less than half. |
| 302 | const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0; |
| 303 | int plane; |
| Deb Mukherjee | 23fc1f7 | 2014-10-15 17:01:34 | [diff] [blame] | 304 | if (dst->d_w != src->d_w || dst->d_h != src->d_h || |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 305 | dst->x_chroma_shift != src->x_chroma_shift || |
| 306 | dst->y_chroma_shift != src->y_chroma_shift || |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 307 | dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH || input_shift < 0) { |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 308 | fatal("Unsupported image conversion"); |
| 309 | } |
| 310 | switch (src->fmt) { |
| 311 | case VPX_IMG_FMT_I420: |
| 312 | case VPX_IMG_FMT_I422: |
| 313 | case VPX_IMG_FMT_I444: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 314 | case VPX_IMG_FMT_I440: break; |
| 315 | default: fatal("Unsupported image conversion"); break; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 316 | } |
| 317 | for (plane = 0; plane < 3; plane++) { |
| Deb Mukherjee | 23fc1f7 | 2014-10-15 17:01:34 | [diff] [blame] | 318 | int w = src->d_w; |
| 319 | int h = src->d_h; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 320 | int x, y; |
| 321 | if (plane) { |
| 322 | w = (w + src->x_chroma_shift) >> src->x_chroma_shift; |
| 323 | h = (h + src->y_chroma_shift) >> src->y_chroma_shift; |
| 324 | } |
| 325 | for (y = 0; y < h; y++) { |
| 326 | uint8_t *p_src = src->planes[plane] + y * src->stride[plane]; |
| 327 | uint16_t *p_dst = |
| 328 | (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]); |
| 329 | for (x = 0; x < w; x++) { |
| 330 | *p_dst++ = (*p_src++ << input_shift) + offset; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 336 | void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src, int input_shift) { |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 337 | if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { |
| 338 | highbd_img_upshift(dst, src, input_shift); |
| 339 | } else { |
| 340 | lowbd_img_upshift(dst, src, input_shift); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) { |
| 345 | int plane; |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 346 | if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w || |
| 347 | dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift || |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 348 | dst->y_chroma_shift != src->y_chroma_shift) { |
| 349 | fatal("Unsupported image conversion"); |
| 350 | } |
| 351 | switch (dst->fmt) { |
| 352 | case VPX_IMG_FMT_I420: |
| 353 | case VPX_IMG_FMT_I422: |
| 354 | case VPX_IMG_FMT_I444: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 355 | case VPX_IMG_FMT_I440: break; |
| 356 | default: fatal("Unsupported image conversion"); break; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 357 | } |
| 358 | for (plane = 0; plane < 3; plane++) { |
| 359 | int w = src->d_w; |
| 360 | int h = src->d_h; |
| 361 | int x, y; |
| 362 | if (plane) { |
| Deb Mukherjee | 23fc1f7 | 2014-10-15 17:01:34 | [diff] [blame] | 363 | w = (w + src->x_chroma_shift) >> src->x_chroma_shift; |
| 364 | h = (h + src->y_chroma_shift) >> src->y_chroma_shift; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 365 | } |
| 366 | for (y = 0; y < h; y++) { |
| 367 | uint16_t *p_src = |
| 368 | (uint16_t *)(src->planes[plane] + y * src->stride[plane]); |
| 369 | uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane]; |
| 370 | for (x = 0; x < w; x++) { |
| Yaowu Xu | c369daf | 2015-07-07 21:22:07 | [diff] [blame] | 371 | *p_dst++ = (uint8_t)(*p_src++); |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src, |
| 378 | int down_shift) { |
| 379 | int plane; |
| 380 | if (dst->d_w != src->d_w || dst->d_h != src->d_h || |
| 381 | dst->x_chroma_shift != src->x_chroma_shift || |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 382 | dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt || |
| 383 | down_shift < 0) { |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 384 | fatal("Unsupported image conversion"); |
| 385 | } |
| 386 | switch (src->fmt) { |
| 387 | case VPX_IMG_FMT_I42016: |
| 388 | case VPX_IMG_FMT_I42216: |
| 389 | case VPX_IMG_FMT_I44416: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 390 | case VPX_IMG_FMT_I44016: break; |
| 391 | default: fatal("Unsupported image conversion"); break; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 392 | } |
| 393 | for (plane = 0; plane < 3; plane++) { |
| 394 | int w = src->d_w; |
| 395 | int h = src->d_h; |
| 396 | int x, y; |
| 397 | if (plane) { |
| 398 | w = (w + src->x_chroma_shift) >> src->x_chroma_shift; |
| 399 | h = (h + src->y_chroma_shift) >> src->y_chroma_shift; |
| 400 | } |
| 401 | for (y = 0; y < h; y++) { |
| 402 | uint16_t *p_src = |
| 403 | (uint16_t *)(src->planes[plane] + y * src->stride[plane]); |
| 404 | uint16_t *p_dst = |
| 405 | (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]); |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 406 | for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src, |
| 412 | int down_shift) { |
| 413 | int plane; |
| 414 | if (dst->d_w != src->d_w || dst->d_h != src->d_h || |
| 415 | dst->x_chroma_shift != src->x_chroma_shift || |
| 416 | dst->y_chroma_shift != src->y_chroma_shift || |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 417 | src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH || down_shift < 0) { |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 418 | fatal("Unsupported image conversion"); |
| 419 | } |
| 420 | switch (dst->fmt) { |
| 421 | case VPX_IMG_FMT_I420: |
| 422 | case VPX_IMG_FMT_I422: |
| 423 | case VPX_IMG_FMT_I444: |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 424 | case VPX_IMG_FMT_I440: break; |
| 425 | default: fatal("Unsupported image conversion"); break; |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 426 | } |
| 427 | for (plane = 0; plane < 3; plane++) { |
| 428 | int w = src->d_w; |
| 429 | int h = src->d_h; |
| 430 | int x, y; |
| 431 | if (plane) { |
| 432 | w = (w + src->x_chroma_shift) >> src->x_chroma_shift; |
| 433 | h = (h + src->y_chroma_shift) >> src->y_chroma_shift; |
| 434 | } |
| 435 | for (y = 0; y < h; y++) { |
| 436 | uint16_t *p_src = |
| 437 | (uint16_t *)(src->planes[plane] + y * src->stride[plane]); |
| 438 | uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane]; |
| 439 | for (x = 0; x < w; x++) { |
| 440 | *p_dst++ = *p_src++ >> down_shift; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| clang-format | 99e28b8 | 2016-01-27 20:42:45 | [diff] [blame] | 446 | void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src, int down_shift) { |
| Deb Mukherjee | 7a2a611 | 2014-10-07 03:46:11 | [diff] [blame] | 447 | if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { |
| 448 | highbd_img_downshift(dst, src, down_shift); |
| 449 | } else { |
| 450 | lowbd_img_downshift(dst, src, down_shift); |
| 451 | } |
| 452 | } |
| Yaowu Xu | 3246fc0 | 2016-01-21 00:13:04 | [diff] [blame] | 453 | #endif // CONFIG_VPX_HIGHBITDEPTH |