Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 10 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 11 | * Based on code from the OggTheora software codec source code, |
| 12 | * Copyright (C) 2002-2010 The Xiph.Org Foundation and contributors. |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 13 | */ |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 14 | #include <errno.h> |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 17 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "aom/aom_integer.h" |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 19 | #include "y4minput.h" |
| 20 | |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 21 | // Reads 'size' bytes from 'file' into 'buf' with some fault tolerance. |
| 22 | // Returns true on success. |
| 23 | static int file_read(void *buf, size_t size, FILE *file) { |
| 24 | const int kMaxRetries = 5; |
| 25 | int retry_count = 0; |
James Zern | d586698 | 2014-03-15 12:35:44 -0700 | [diff] [blame] | 26 | int file_error; |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 27 | size_t len = 0; |
| 28 | do { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 29 | const size_t n = fread((uint8_t *)buf + len, 1, size - len, file); |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 30 | len += n; |
James Zern | d586698 | 2014-03-15 12:35:44 -0700 | [diff] [blame] | 31 | file_error = ferror(file); |
| 32 | if (file_error) { |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 33 | if (errno == EINTR || errno == EAGAIN) { |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 34 | clearerr(file); |
| 35 | continue; |
| 36 | } else { |
| 37 | fprintf(stderr, "Error reading file: %u of %u bytes read, %d: %s\n", |
| 38 | (uint32_t)len, (uint32_t)size, errno, strerror(errno)); |
| 39 | return 0; |
| 40 | } |
| 41 | } |
James Zern | d586698 | 2014-03-15 12:35:44 -0700 | [diff] [blame] | 42 | } while (!feof(file) && len < size && ++retry_count < kMaxRetries); |
| 43 | |
| 44 | if (!feof(file) && len != size) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 45 | fprintf(stderr, |
| 46 | "Error reading file: %u of %u bytes read," |
| 47 | " error: %d, retries: %d, %d: %s\n", |
| 48 | (uint32_t)len, (uint32_t)size, file_error, retry_count, errno, |
| 49 | strerror(errno)); |
James Zern | d586698 | 2014-03-15 12:35:44 -0700 | [diff] [blame] | 50 | } |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 51 | return len == size; |
| 52 | } |
| 53 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 54 | static int y4m_parse_tags(y4m_input *_y4m, char *_tags) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 55 | int got_w; |
| 56 | int got_h; |
| 57 | int got_fps; |
| 58 | int got_interlace; |
| 59 | int got_par; |
| 60 | int got_chroma; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 61 | char *p; |
| 62 | char *q; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 63 | got_w = got_h = got_fps = got_interlace = got_par = got_chroma = 0; |
| 64 | for (p = _tags;; p = q) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 65 | /*Skip any leading spaces.*/ |
James Zern | e0c265e | 2016-07-19 20:46:39 -0700 | [diff] [blame] | 66 | while (*p == ' ') p++; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 67 | /*If that's all we have, stop.*/ |
James Zern | e0c265e | 2016-07-19 20:46:39 -0700 | [diff] [blame] | 68 | if (p[0] == '\0') break; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 69 | /*Find the end of this tag.*/ |
James Zern | e0c265e | 2016-07-19 20:46:39 -0700 | [diff] [blame] | 70 | for (q = p + 1; *q != '\0' && *q != ' '; q++) { |
| 71 | } |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 72 | /*Process the tag.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 73 | switch (p[0]) { |
| 74 | case 'W': { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 75 | if (sscanf(p + 1, "%d", &_y4m->pic_w) != 1) return -1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 76 | got_w = 1; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 77 | } break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 78 | case 'H': { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 79 | if (sscanf(p + 1, "%d", &_y4m->pic_h) != 1) return -1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 80 | got_h = 1; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 81 | } break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 82 | case 'F': { |
| 83 | if (sscanf(p + 1, "%d:%d", &_y4m->fps_n, &_y4m->fps_d) != 2) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 84 | return -1; |
| 85 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 86 | got_fps = 1; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 87 | } break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 88 | case 'I': { |
| 89 | _y4m->interlace = p[1]; |
| 90 | got_interlace = 1; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 91 | } break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 92 | case 'A': { |
| 93 | if (sscanf(p + 1, "%d:%d", &_y4m->par_n, &_y4m->par_d) != 2) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 94 | return -1; |
| 95 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 96 | got_par = 1; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 97 | } break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 98 | case 'C': { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 99 | if (q - p > 16) return -1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 100 | memcpy(_y4m->chroma_type, p + 1, q - p - 1); |
| 101 | _y4m->chroma_type[q - p - 1] = '\0'; |
| 102 | got_chroma = 1; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 103 | } break; |
| 104 | /*Ignore unknown tags.*/ |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 105 | } |
| 106 | } |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 107 | if (!got_w || !got_h || !got_fps) return -1; |
| 108 | if (!got_interlace) _y4m->interlace = '?'; |
| 109 | if (!got_par) _y4m->par_n = _y4m->par_d = 0; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 110 | /*Chroma-type is not specified in older files, e.g., those generated by |
| 111 | mplayer.*/ |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 112 | if (!got_chroma) strcpy(_y4m->chroma_type, "420"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 116 | /*All anti-aliasing filters in the following conversion functions are based on |
| 117 | one of two window functions: |
| 118 | The 6-tap Lanczos window (for down-sampling and shifts): |
| 119 | sinc(\pi*t)*sinc(\pi*t/3), |t|<3 (sinc(t)==sin(t)/t) |
| 120 | 0, |t|>=3 |
| 121 | The 4-tap Mitchell window (for up-sampling): |
| 122 | 7|t|^3-12|t|^2+16/3, |t|<1 |
| 123 | -(7/3)|x|^3+12|x|^2-20|x|+32/3, |t|<2 |
| 124 | 0, |t|>=2 |
| 125 | The number of taps is intentionally kept small to reduce computational |
| 126 | overhead and limit ringing. |
| 127 | |
| 128 | The taps from these filters are scaled so that their sum is 1, and the result |
| 129 | is scaled by 128 and rounded to integers to create a filter whose |
| 130 | intermediate values fit inside 16 bits. |
| 131 | Coefficients are rounded in such a way as to ensure their sum is still 128, |
| 132 | which is usually equivalent to normal rounding. |
| 133 | |
| 134 | Conversions which require both horizontal and vertical filtering could |
| 135 | have these steps pipelined, for less memory consumption and better cache |
| 136 | performance, but we do them separately for simplicity.*/ |
| 137 | |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 138 | #define OC_MINI(_a, _b) ((_a) > (_b) ? (_b) : (_a)) |
| 139 | #define OC_MAXI(_a, _b) ((_a) < (_b) ? (_b) : (_a)) |
| 140 | #define OC_CLAMPI(_a, _b, _c) (OC_MAXI(_a, OC_MINI(_b, _c))) |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 141 | |
| 142 | /*420jpeg chroma samples are sited like: |
| 143 | Y-------Y-------Y-------Y------- |
| 144 | | | | | |
| 145 | | BR | | BR | |
| 146 | | | | | |
| 147 | Y-------Y-------Y-------Y------- |
| 148 | | | | | |
| 149 | | | | | |
| 150 | | | | | |
| 151 | Y-------Y-------Y-------Y------- |
| 152 | | | | | |
| 153 | | BR | | BR | |
| 154 | | | | | |
| 155 | Y-------Y-------Y-------Y------- |
| 156 | | | | | |
| 157 | | | | | |
| 158 | | | | | |
| 159 | |
| 160 | 420mpeg2 chroma samples are sited like: |
| 161 | Y-------Y-------Y-------Y------- |
| 162 | | | | | |
| 163 | BR | BR | |
| 164 | | | | | |
| 165 | Y-------Y-------Y-------Y------- |
| 166 | | | | | |
| 167 | | | | | |
| 168 | | | | | |
| 169 | Y-------Y-------Y-------Y------- |
| 170 | | | | | |
| 171 | BR | BR | |
| 172 | | | | | |
| 173 | Y-------Y-------Y-------Y------- |
| 174 | | | | | |
| 175 | | | | | |
| 176 | | | | | |
| 177 | |
| 178 | We use a resampling filter to shift the site locations one quarter pixel (at |
| 179 | the chroma plane's resolution) to the right. |
| 180 | The 4:2:2 modes look exactly the same, except there are twice as many chroma |
| 181 | lines, and they are vertically co-sited with the luma samples in both the |
| 182 | mpeg2 and jpeg cases (thus requiring no vertical resampling).*/ |
| 183 | static void y4m_42xmpeg2_42xjpeg_helper(unsigned char *_dst, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 184 | const unsigned char *_src, int _c_w, |
| 185 | int _c_h) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 186 | int y; |
| 187 | int x; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 188 | for (y = 0; y < _c_h; y++) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 189 | /*Filter: [4 -17 114 35 -9 1]/128, derived from a 6-tap Lanczos |
| 190 | window.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 191 | for (x = 0; x < OC_MINI(_c_w, 2); x++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 192 | _dst[x] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 193 | 0, |
| 194 | (4 * _src[0] - 17 * _src[OC_MAXI(x - 1, 0)] + 114 * _src[x] + |
| 195 | 35 * _src[OC_MINI(x + 1, _c_w - 1)] - |
| 196 | 9 * _src[OC_MINI(x + 2, _c_w - 1)] + _src[OC_MINI(x + 3, _c_w - 1)] + |
| 197 | 64) >> |
| 198 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 199 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 200 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 201 | for (; x < _c_w - 3; x++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 202 | _dst[x] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 203 | 0, |
| 204 | (4 * _src[x - 2] - 17 * _src[x - 1] + 114 * _src[x] + |
| 205 | 35 * _src[x + 1] - 9 * _src[x + 2] + _src[x + 3] + 64) >> |
| 206 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 207 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 208 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 209 | for (; x < _c_w; x++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 210 | _dst[x] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 211 | 0, |
| 212 | (4 * _src[x - 2] - 17 * _src[x - 1] + 114 * _src[x] + |
| 213 | 35 * _src[OC_MINI(x + 1, _c_w - 1)] - |
| 214 | 9 * _src[OC_MINI(x + 2, _c_w - 1)] + _src[_c_w - 1] + 64) >> |
| 215 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 216 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 217 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 218 | _dst += _c_w; |
| 219 | _src += _c_w; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | /*Handles both 422 and 420mpeg2 to 422jpeg and 420jpeg, respectively.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 224 | static void y4m_convert_42xmpeg2_42xjpeg(y4m_input *_y4m, unsigned char *_dst, |
| 225 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 226 | int c_w; |
| 227 | int c_h; |
| 228 | int c_sz; |
| 229 | int pli; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 230 | /*Skip past the luma data.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 231 | _dst += _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 232 | /*Compute the size of each chroma plane.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 233 | c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h; |
| 234 | c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
| 235 | c_sz = c_w * c_h; |
| 236 | for (pli = 1; pli < 3; pli++) { |
| 237 | y4m_42xmpeg2_42xjpeg_helper(_dst, _aux, c_w, c_h); |
| 238 | _dst += c_sz; |
| 239 | _aux += c_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | /*This format is only used for interlaced content, but is included for |
| 244 | completeness. |
| 245 | |
| 246 | 420jpeg chroma samples are sited like: |
| 247 | Y-------Y-------Y-------Y------- |
| 248 | | | | | |
| 249 | | BR | | BR | |
| 250 | | | | | |
| 251 | Y-------Y-------Y-------Y------- |
| 252 | | | | | |
| 253 | | | | | |
| 254 | | | | | |
| 255 | Y-------Y-------Y-------Y------- |
| 256 | | | | | |
| 257 | | BR | | BR | |
| 258 | | | | | |
| 259 | Y-------Y-------Y-------Y------- |
| 260 | | | | | |
| 261 | | | | | |
| 262 | | | | | |
| 263 | |
| 264 | 420paldv chroma samples are sited like: |
| 265 | YR------Y-------YR------Y------- |
| 266 | | | | | |
| 267 | | | | | |
| 268 | | | | | |
| 269 | YB------Y-------YB------Y------- |
| 270 | | | | | |
| 271 | | | | | |
| 272 | | | | | |
| 273 | YR------Y-------YR------Y------- |
| 274 | | | | | |
| 275 | | | | | |
| 276 | | | | | |
| 277 | YB------Y-------YB------Y------- |
| 278 | | | | | |
| 279 | | | | | |
| 280 | | | | | |
| 281 | |
| 282 | We use a resampling filter to shift the site locations one quarter pixel (at |
| 283 | the chroma plane's resolution) to the right. |
| 284 | Then we use another filter to move the C_r location down one quarter pixel, |
| 285 | and the C_b location up one quarter pixel.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 286 | static void y4m_convert_42xpaldv_42xjpeg(y4m_input *_y4m, unsigned char *_dst, |
| 287 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 288 | unsigned char *tmp; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 289 | int c_w; |
| 290 | int c_h; |
| 291 | int c_sz; |
| 292 | int pli; |
| 293 | int y; |
| 294 | int x; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 295 | /*Skip past the luma data.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 296 | _dst += _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 297 | /*Compute the size of each chroma plane.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 298 | c_w = (_y4m->pic_w + 1) / 2; |
| 299 | c_h = (_y4m->pic_h + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h; |
| 300 | c_sz = c_w * c_h; |
| 301 | tmp = _aux + 2 * c_sz; |
| 302 | for (pli = 1; pli < 3; pli++) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 303 | /*First do the horizontal re-sampling. |
| 304 | This is the same as the mpeg2 case, except that after the horizontal |
| 305 | case, we need to apply a second vertical filter.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 306 | y4m_42xmpeg2_42xjpeg_helper(tmp, _aux, c_w, c_h); |
| 307 | _aux += c_sz; |
| 308 | switch (pli) { |
| 309 | case 1: { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 310 | /*Slide C_b up a quarter-pel. |
| 311 | This is the same filter used above, but in the other order.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 312 | for (x = 0; x < c_w; x++) { |
| 313 | for (y = 0; y < OC_MINI(c_h, 3); y++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 314 | _dst[y * c_w] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 315 | 0, |
| 316 | (tmp[0] - 9 * tmp[OC_MAXI(y - 2, 0) * c_w] + |
| 317 | 35 * tmp[OC_MAXI(y - 1, 0) * c_w] + 114 * tmp[y * c_w] - |
| 318 | 17 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] + |
| 319 | 4 * tmp[OC_MINI(y + 2, c_h - 1) * c_w] + 64) >> |
| 320 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 321 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 322 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 323 | for (; y < c_h - 2; y++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 324 | _dst[y * c_w] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 325 | 0, |
| 326 | (tmp[(y - 3) * c_w] - 9 * tmp[(y - 2) * c_w] + |
| 327 | 35 * tmp[(y - 1) * c_w] + 114 * tmp[y * c_w] - |
| 328 | 17 * tmp[(y + 1) * c_w] + 4 * tmp[(y + 2) * c_w] + 64) >> |
| 329 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 330 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 331 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 332 | for (; y < c_h; y++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 333 | _dst[y * c_w] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 334 | 0, |
| 335 | (tmp[(y - 3) * c_w] - 9 * tmp[(y - 2) * c_w] + |
| 336 | 35 * tmp[(y - 1) * c_w] + 114 * tmp[y * c_w] - |
| 337 | 17 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] + |
| 338 | 4 * tmp[(c_h - 1) * c_w] + 64) >> |
| 339 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 340 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 341 | } |
| 342 | _dst++; |
| 343 | tmp++; |
| 344 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 345 | _dst += c_sz - c_w; |
| 346 | tmp -= c_w; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 347 | } break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 348 | case 2: { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 349 | /*Slide C_r down a quarter-pel. |
| 350 | This is the same as the horizontal filter.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 351 | for (x = 0; x < c_w; x++) { |
| 352 | for (y = 0; y < OC_MINI(c_h, 2); y++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 353 | _dst[y * c_w] = (unsigned char)OC_CLAMPI( |
| 354 | 0, |
| 355 | (4 * tmp[0] - 17 * tmp[OC_MAXI(y - 1, 0) * c_w] + |
| 356 | 114 * tmp[y * c_w] + 35 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] - |
| 357 | 9 * tmp[OC_MINI(y + 2, c_h - 1) * c_w] + |
| 358 | tmp[OC_MINI(y + 3, c_h - 1) * c_w] + 64) >> |
| 359 | 7, |
| 360 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 361 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 362 | for (; y < c_h - 3; y++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 363 | _dst[y * c_w] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 364 | 0, |
| 365 | (4 * tmp[(y - 2) * c_w] - 17 * tmp[(y - 1) * c_w] + |
| 366 | 114 * tmp[y * c_w] + 35 * tmp[(y + 1) * c_w] - |
| 367 | 9 * tmp[(y + 2) * c_w] + tmp[(y + 3) * c_w] + 64) >> |
| 368 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 369 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 370 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 371 | for (; y < c_h; y++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 372 | _dst[y * c_w] = (unsigned char)OC_CLAMPI( |
| 373 | 0, |
| 374 | (4 * tmp[(y - 2) * c_w] - 17 * tmp[(y - 1) * c_w] + |
| 375 | 114 * tmp[y * c_w] + 35 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] - |
| 376 | 9 * tmp[OC_MINI(y + 2, c_h - 1) * c_w] + tmp[(c_h - 1) * c_w] + |
| 377 | 64) >> |
| 378 | 7, |
| 379 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 380 | } |
| 381 | _dst++; |
| 382 | tmp++; |
| 383 | } |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 384 | } break; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 385 | } |
| 386 | /*For actual interlaced material, this would have to be done separately on |
| 387 | each field, and the shift amounts would be different. |
| 388 | C_r moves down 1/8, C_b up 3/8 in the top field, and C_r moves down 3/8, |
| 389 | C_b up 1/8 in the bottom field. |
| 390 | The corresponding filters would be: |
| 391 | Down 1/8 (reverse order for up): [3 -11 125 15 -4 0]/128 |
| 392 | Down 3/8 (reverse order for up): [4 -19 98 56 -13 2]/128*/ |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /*Perform vertical filtering to reduce a single plane from 4:2:2 to 4:2:0. |
| 397 | This is used as a helper by several converation routines.*/ |
| 398 | static void y4m_422jpeg_420jpeg_helper(unsigned char *_dst, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 399 | const unsigned char *_src, int _c_w, |
| 400 | int _c_h) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 401 | int y; |
| 402 | int x; |
| 403 | /*Filter: [3 -17 78 78 -17 3]/128, derived from a 6-tap Lanczos window.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 404 | for (x = 0; x < _c_w; x++) { |
| 405 | for (y = 0; y < OC_MINI(_c_h, 2); y += 2) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 406 | _dst[(y >> 1) * _c_w] = |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 407 | OC_CLAMPI(0, |
| 408 | (64 * _src[0] + 78 * _src[OC_MINI(1, _c_h - 1) * _c_w] - |
| 409 | 17 * _src[OC_MINI(2, _c_h - 1) * _c_w] + |
| 410 | 3 * _src[OC_MINI(3, _c_h - 1) * _c_w] + 64) >> |
| 411 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 412 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 413 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 414 | for (; y < _c_h - 3; y += 2) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 415 | _dst[(y >> 1) * _c_w] = |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 416 | OC_CLAMPI(0, |
| 417 | (3 * (_src[(y - 2) * _c_w] + _src[(y + 3) * _c_w]) - |
| 418 | 17 * (_src[(y - 1) * _c_w] + _src[(y + 2) * _c_w]) + |
| 419 | 78 * (_src[y * _c_w] + _src[(y + 1) * _c_w]) + 64) >> |
| 420 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 421 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 422 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 423 | for (; y < _c_h; y += 2) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 424 | _dst[(y >> 1) * _c_w] = OC_CLAMPI( |
| 425 | 0, |
| 426 | (3 * (_src[(y - 2) * _c_w] + _src[(_c_h - 1) * _c_w]) - |
| 427 | 17 * (_src[(y - 1) * _c_w] + _src[OC_MINI(y + 2, _c_h - 1) * _c_w]) + |
| 428 | 78 * (_src[y * _c_w] + _src[OC_MINI(y + 1, _c_h - 1) * _c_w]) + |
| 429 | 64) >> |
| 430 | 7, |
| 431 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 432 | } |
| 433 | _src++; |
| 434 | _dst++; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /*420jpeg chroma samples are sited like: |
| 439 | Y-------Y-------Y-------Y------- |
| 440 | | | | | |
| 441 | | BR | | BR | |
| 442 | | | | | |
| 443 | Y-------Y-------Y-------Y------- |
| 444 | | | | | |
| 445 | | | | | |
| 446 | | | | | |
| 447 | Y-------Y-------Y-------Y------- |
| 448 | | | | | |
| 449 | | BR | | BR | |
| 450 | | | | | |
| 451 | Y-------Y-------Y-------Y------- |
| 452 | | | | | |
| 453 | | | | | |
| 454 | | | | | |
| 455 | |
| 456 | 422jpeg chroma samples are sited like: |
| 457 | Y---BR--Y-------Y---BR--Y------- |
| 458 | | | | | |
| 459 | | | | | |
| 460 | | | | | |
| 461 | Y---BR--Y-------Y---BR--Y------- |
| 462 | | | | | |
| 463 | | | | | |
| 464 | | | | | |
| 465 | Y---BR--Y-------Y---BR--Y------- |
| 466 | | | | | |
| 467 | | | | | |
| 468 | | | | | |
| 469 | Y---BR--Y-------Y---BR--Y------- |
| 470 | | | | | |
| 471 | | | | | |
| 472 | | | | | |
| 473 | |
| 474 | We use a resampling filter to decimate the chroma planes by two in the |
| 475 | vertical direction.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 476 | static void y4m_convert_422jpeg_420jpeg(y4m_input *_y4m, unsigned char *_dst, |
| 477 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 478 | int c_w; |
| 479 | int c_h; |
| 480 | int c_sz; |
| 481 | int dst_c_w; |
| 482 | int dst_c_h; |
| 483 | int dst_c_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 484 | int pli; |
| 485 | /*Skip past the luma data.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 486 | _dst += _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 487 | /*Compute the size of each chroma plane.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 488 | c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h; |
| 489 | c_h = _y4m->pic_h; |
| 490 | dst_c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h; |
| 491 | dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
| 492 | c_sz = c_w * c_h; |
| 493 | dst_c_sz = dst_c_w * dst_c_h; |
| 494 | for (pli = 1; pli < 3; pli++) { |
| 495 | y4m_422jpeg_420jpeg_helper(_dst, _aux, c_w, c_h); |
| 496 | _aux += c_sz; |
| 497 | _dst += dst_c_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | /*420jpeg chroma samples are sited like: |
| 502 | Y-------Y-------Y-------Y------- |
| 503 | | | | | |
| 504 | | BR | | BR | |
| 505 | | | | | |
| 506 | Y-------Y-------Y-------Y------- |
| 507 | | | | | |
| 508 | | | | | |
| 509 | | | | | |
| 510 | Y-------Y-------Y-------Y------- |
| 511 | | | | | |
| 512 | | BR | | BR | |
| 513 | | | | | |
| 514 | Y-------Y-------Y-------Y------- |
| 515 | | | | | |
| 516 | | | | | |
| 517 | | | | | |
| 518 | |
| 519 | 422 chroma samples are sited like: |
| 520 | YBR-----Y-------YBR-----Y------- |
| 521 | | | | | |
| 522 | | | | | |
| 523 | | | | | |
| 524 | YBR-----Y-------YBR-----Y------- |
| 525 | | | | | |
| 526 | | | | | |
| 527 | | | | | |
| 528 | YBR-----Y-------YBR-----Y------- |
| 529 | | | | | |
| 530 | | | | | |
| 531 | | | | | |
| 532 | YBR-----Y-------YBR-----Y------- |
| 533 | | | | | |
| 534 | | | | | |
| 535 | | | | | |
| 536 | |
| 537 | We use a resampling filter to shift the original site locations one quarter |
| 538 | pixel (at the original chroma resolution) to the right. |
| 539 | Then we use a second resampling filter to decimate the chroma planes by two |
| 540 | in the vertical direction.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 541 | static void y4m_convert_422_420jpeg(y4m_input *_y4m, unsigned char *_dst, |
| 542 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 543 | unsigned char *tmp; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 544 | int c_w; |
| 545 | int c_h; |
| 546 | int c_sz; |
| 547 | int dst_c_h; |
| 548 | int dst_c_sz; |
| 549 | int pli; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 550 | /*Skip past the luma data.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 551 | _dst += _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 552 | /*Compute the size of each chroma plane.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 553 | c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h; |
| 554 | c_h = _y4m->pic_h; |
| 555 | dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
| 556 | c_sz = c_w * c_h; |
| 557 | dst_c_sz = c_w * dst_c_h; |
| 558 | tmp = _aux + 2 * c_sz; |
| 559 | for (pli = 1; pli < 3; pli++) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 560 | /*In reality, the horizontal and vertical steps could be pipelined, for |
| 561 | less memory consumption and better cache performance, but we do them |
| 562 | separately for simplicity.*/ |
| 563 | /*First do horizontal filtering (convert to 422jpeg)*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 564 | y4m_42xmpeg2_42xjpeg_helper(tmp, _aux, c_w, c_h); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 565 | /*Now do the vertical filtering.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 566 | y4m_422jpeg_420jpeg_helper(_dst, tmp, c_w, c_h); |
| 567 | _aux += c_sz; |
| 568 | _dst += dst_c_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | /*420jpeg chroma samples are sited like: |
| 573 | Y-------Y-------Y-------Y------- |
| 574 | | | | | |
| 575 | | BR | | BR | |
| 576 | | | | | |
| 577 | Y-------Y-------Y-------Y------- |
| 578 | | | | | |
| 579 | | | | | |
| 580 | | | | | |
| 581 | Y-------Y-------Y-------Y------- |
| 582 | | | | | |
| 583 | | BR | | BR | |
| 584 | | | | | |
| 585 | Y-------Y-------Y-------Y------- |
| 586 | | | | | |
| 587 | | | | | |
| 588 | | | | | |
| 589 | |
| 590 | 411 chroma samples are sited like: |
| 591 | YBR-----Y-------Y-------Y------- |
| 592 | | | | | |
| 593 | | | | | |
| 594 | | | | | |
| 595 | YBR-----Y-------Y-------Y------- |
| 596 | | | | | |
| 597 | | | | | |
| 598 | | | | | |
| 599 | YBR-----Y-------Y-------Y------- |
| 600 | | | | | |
| 601 | | | | | |
| 602 | | | | | |
| 603 | YBR-----Y-------Y-------Y------- |
| 604 | | | | | |
| 605 | | | | | |
| 606 | | | | | |
| 607 | |
| 608 | We use a filter to resample at site locations one eighth pixel (at the source |
| 609 | chroma plane's horizontal resolution) and five eighths of a pixel to the |
| 610 | right. |
| 611 | Then we use another filter to decimate the planes by 2 in the vertical |
| 612 | direction.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 613 | static void y4m_convert_411_420jpeg(y4m_input *_y4m, unsigned char *_dst, |
| 614 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 615 | unsigned char *tmp; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 616 | int c_w; |
| 617 | int c_h; |
| 618 | int c_sz; |
| 619 | int dst_c_w; |
| 620 | int dst_c_h; |
| 621 | int dst_c_sz; |
| 622 | int tmp_sz; |
| 623 | int pli; |
| 624 | int y; |
| 625 | int x; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 626 | /*Skip past the luma data.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 627 | _dst += _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 628 | /*Compute the size of each chroma plane.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 629 | c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h; |
| 630 | c_h = _y4m->pic_h; |
| 631 | dst_c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h; |
| 632 | dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
| 633 | c_sz = c_w * c_h; |
| 634 | dst_c_sz = dst_c_w * dst_c_h; |
| 635 | tmp_sz = dst_c_w * c_h; |
| 636 | tmp = _aux + 2 * c_sz; |
| 637 | for (pli = 1; pli < 3; pli++) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 638 | /*In reality, the horizontal and vertical steps could be pipelined, for |
| 639 | less memory consumption and better cache performance, but we do them |
| 640 | separately for simplicity.*/ |
| 641 | /*First do horizontal filtering (convert to 422jpeg)*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 642 | for (y = 0; y < c_h; y++) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 643 | /*Filters: [1 110 18 -1]/128 and [-3 50 86 -5]/128, both derived from a |
| 644 | 4-tap Mitchell window.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 645 | for (x = 0; x < OC_MINI(c_w, 1); x++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 646 | tmp[x << 1] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 647 | 0, |
| 648 | (111 * _aux[0] + 18 * _aux[OC_MINI(1, c_w - 1)] - |
| 649 | _aux[OC_MINI(2, c_w - 1)] + 64) >> |
| 650 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 651 | 255); |
| 652 | tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 653 | 0, |
| 654 | (47 * _aux[0] + 86 * _aux[OC_MINI(1, c_w - 1)] - |
| 655 | 5 * _aux[OC_MINI(2, c_w - 1)] + 64) >> |
| 656 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 657 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 658 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 659 | for (; x < c_w - 2; x++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 660 | tmp[x << 1] = |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 661 | (unsigned char)OC_CLAMPI(0, |
| 662 | (_aux[x - 1] + 110 * _aux[x] + |
| 663 | 18 * _aux[x + 1] - _aux[x + 2] + 64) >> |
| 664 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 665 | 255); |
| 666 | tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 667 | 0, |
| 668 | (-3 * _aux[x - 1] + 50 * _aux[x] + 86 * _aux[x + 1] - |
| 669 | 5 * _aux[x + 2] + 64) >> |
| 670 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 671 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 672 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 673 | for (; x < c_w; x++) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 674 | tmp[x << 1] = (unsigned char)OC_CLAMPI( |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 675 | 0, |
| 676 | (_aux[x - 1] + 110 * _aux[x] + 18 * _aux[OC_MINI(x + 1, c_w - 1)] - |
| 677 | _aux[c_w - 1] + 64) >> |
| 678 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 679 | 255); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 680 | if ((x << 1 | 1) < dst_c_w) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 681 | tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI( |
| 682 | 0, |
| 683 | (-3 * _aux[x - 1] + 50 * _aux[x] + |
| 684 | 86 * _aux[OC_MINI(x + 1, c_w - 1)] - 5 * _aux[c_w - 1] + 64) >> |
| 685 | 7, |
| 686 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 687 | } |
| 688 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 689 | tmp += dst_c_w; |
| 690 | _aux += c_w; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 691 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 692 | tmp -= tmp_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 693 | /*Now do the vertical filtering.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 694 | y4m_422jpeg_420jpeg_helper(_dst, tmp, dst_c_w, c_h); |
| 695 | _dst += dst_c_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | /*Convert 444 to 420jpeg.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 700 | static void y4m_convert_444_420jpeg(y4m_input *_y4m, unsigned char *_dst, |
| 701 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 702 | unsigned char *tmp; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 703 | int c_w; |
| 704 | int c_h; |
| 705 | int c_sz; |
| 706 | int dst_c_w; |
| 707 | int dst_c_h; |
| 708 | int dst_c_sz; |
| 709 | int tmp_sz; |
| 710 | int pli; |
| 711 | int y; |
| 712 | int x; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 713 | /*Skip past the luma data.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 714 | _dst += _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 715 | /*Compute the size of each chroma plane.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 716 | c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h; |
| 717 | c_h = _y4m->pic_h; |
| 718 | dst_c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h; |
| 719 | dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
| 720 | c_sz = c_w * c_h; |
| 721 | dst_c_sz = dst_c_w * dst_c_h; |
| 722 | tmp_sz = dst_c_w * c_h; |
| 723 | tmp = _aux + 2 * c_sz; |
| 724 | for (pli = 1; pli < 3; pli++) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 725 | /*Filter: [3 -17 78 78 -17 3]/128, derived from a 6-tap Lanczos window.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 726 | for (y = 0; y < c_h; y++) { |
| 727 | for (x = 0; x < OC_MINI(c_w, 2); x += 2) { |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 728 | tmp[x >> 1] = OC_CLAMPI(0, |
| 729 | (64 * _aux[0] + 78 * _aux[OC_MINI(1, c_w - 1)] - |
| 730 | 17 * _aux[OC_MINI(2, c_w - 1)] + |
| 731 | 3 * _aux[OC_MINI(3, c_w - 1)] + 64) >> |
| 732 | 7, |
| 733 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 734 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 735 | for (; x < c_w - 3; x += 2) { |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 736 | tmp[x >> 1] = OC_CLAMPI(0, |
| 737 | (3 * (_aux[x - 2] + _aux[x + 3]) - |
| 738 | 17 * (_aux[x - 1] + _aux[x + 2]) + |
| 739 | 78 * (_aux[x] + _aux[x + 1]) + 64) >> |
| 740 | 7, |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 741 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 742 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 743 | for (; x < c_w; x += 2) { |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 744 | tmp[x >> 1] = |
| 745 | OC_CLAMPI(0, |
| 746 | (3 * (_aux[x - 2] + _aux[c_w - 1]) - |
| 747 | 17 * (_aux[x - 1] + _aux[OC_MINI(x + 2, c_w - 1)]) + |
| 748 | 78 * (_aux[x] + _aux[OC_MINI(x + 1, c_w - 1)]) + 64) >> |
| 749 | 7, |
| 750 | 255); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 751 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 752 | tmp += dst_c_w; |
| 753 | _aux += c_w; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 754 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 755 | tmp -= tmp_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 756 | /*Now do the vertical filtering.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 757 | y4m_422jpeg_420jpeg_helper(_dst, tmp, dst_c_w, c_h); |
| 758 | _dst += dst_c_sz; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | /*The image is padded with empty chroma components at 4:2:0.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 763 | static void y4m_convert_mono_420jpeg(y4m_input *_y4m, unsigned char *_dst, |
| 764 | unsigned char *_aux) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 765 | int c_sz; |
Yaowu Xu | 618e7ef | 2014-07-11 16:27:21 -0700 | [diff] [blame] | 766 | (void)_aux; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 767 | _dst += _y4m->pic_w * _y4m->pic_h; |
| 768 | c_sz = ((_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h) * |
| 769 | ((_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v); |
| 770 | memset(_dst, 128, c_sz * 2); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /*No conversion function needed.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 774 | static void y4m_convert_null(y4m_input *_y4m, unsigned char *_dst, |
| 775 | unsigned char *_aux) { |
Yaowu Xu | 618e7ef | 2014-07-11 16:27:21 -0700 | [diff] [blame] | 776 | (void)_y4m; |
| 777 | (void)_dst; |
| 778 | (void)_aux; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 779 | } |
| 780 | |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 781 | int y4m_input_open(y4m_input *_y4m, FILE *_fin, char *_skip, int _nskip, |
| 782 | int only_420) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 783 | char buffer[80] = { 0 }; |
| 784 | int ret; |
| 785 | int i; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 786 | /*Read until newline, or 80 cols, whichever happens first.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 787 | for (i = 0; i < 79; i++) { |
| 788 | if (_nskip > 0) { |
| 789 | buffer[i] = *_skip++; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 790 | _nskip--; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 791 | } else { |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 792 | if (!file_read(buffer + i, 1, _fin)) return -1; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 793 | } |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 794 | if (buffer[i] == '\n') break; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 795 | } |
| 796 | /*We skipped too much header data.*/ |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 797 | if (_nskip > 0) return -1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 798 | if (i == 79) { |
| 799 | fprintf(stderr, "Error parsing header; not a YUV2MPEG2 file?\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 800 | return -1; |
| 801 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 802 | buffer[i] = '\0'; |
| 803 | if (memcmp(buffer, "YUV4MPEG", 8)) { |
| 804 | fprintf(stderr, "Incomplete magic for YUV4MPEG file.\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 805 | return -1; |
| 806 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 807 | if (buffer[8] != '2') { |
| 808 | fprintf(stderr, "Incorrect YUV input file version; YUV4MPEG2 required.\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 809 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 810 | ret = y4m_parse_tags(_y4m, buffer + 5); |
| 811 | if (ret < 0) { |
| 812 | fprintf(stderr, "Error parsing YUV4MPEG2 header.\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 813 | return ret; |
| 814 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 815 | if (_y4m->interlace == '?') { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 816 | fprintf(stderr, |
| 817 | "Warning: Input video interlacing format unknown; " |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 818 | "assuming progressive scan.\n"); |
| 819 | } else if (_y4m->interlace != 'p') { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 820 | fprintf(stderr, |
| 821 | "Input video is interlaced; " |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 822 | "Only progressive scan handled.\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 823 | return -1; |
| 824 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 825 | _y4m->aom_fmt = AOM_IMG_FMT_I420; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 826 | _y4m->bps = 12; |
| 827 | _y4m->bit_depth = 8; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 828 | if (strcmp(_y4m->chroma_type, "420") == 0 || |
| 829 | strcmp(_y4m->chroma_type, "420jpeg") == 0) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 830 | _y4m->src_c_dec_h = _y4m->dst_c_dec_h = _y4m->src_c_dec_v = |
| 831 | _y4m->dst_c_dec_v = 2; |
| 832 | _y4m->dst_buf_read_sz = |
| 833 | _y4m->pic_w * _y4m->pic_h + |
| 834 | 2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2); |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 835 | /* Natively supported: no conversion required. */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 836 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 837 | _y4m->convert = y4m_convert_null; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 838 | } else if (strcmp(_y4m->chroma_type, "420p10") == 0) { |
| 839 | _y4m->src_c_dec_h = 2; |
| 840 | _y4m->dst_c_dec_h = 2; |
| 841 | _y4m->src_c_dec_v = 2; |
| 842 | _y4m->dst_c_dec_v = 2; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 843 | _y4m->dst_buf_read_sz = |
| 844 | 2 * (_y4m->pic_w * _y4m->pic_h + |
| 845 | 2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2)); |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 846 | /* Natively supported: no conversion required. */ |
| 847 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 848 | _y4m->convert = y4m_convert_null; |
| 849 | _y4m->bit_depth = 10; |
| 850 | _y4m->bps = 15; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 851 | _y4m->aom_fmt = AOM_IMG_FMT_I42016; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 852 | if (only_420) { |
| 853 | fprintf(stderr, "Unsupported conversion from 420p10 to 420jpeg\n"); |
| 854 | return -1; |
| 855 | } |
| 856 | } else if (strcmp(_y4m->chroma_type, "420p12") == 0) { |
| 857 | _y4m->src_c_dec_h = 2; |
| 858 | _y4m->dst_c_dec_h = 2; |
| 859 | _y4m->src_c_dec_v = 2; |
| 860 | _y4m->dst_c_dec_v = 2; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 861 | _y4m->dst_buf_read_sz = |
| 862 | 2 * (_y4m->pic_w * _y4m->pic_h + |
| 863 | 2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2)); |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 864 | /* Natively supported: no conversion required. */ |
| 865 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 866 | _y4m->convert = y4m_convert_null; |
| 867 | _y4m->bit_depth = 12; |
| 868 | _y4m->bps = 18; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 869 | _y4m->aom_fmt = AOM_IMG_FMT_I42016; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 870 | if (only_420) { |
| 871 | fprintf(stderr, "Unsupported conversion from 420p12 to 420jpeg\n"); |
| 872 | return -1; |
| 873 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 874 | } else if (strcmp(_y4m->chroma_type, "420mpeg2") == 0) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 875 | _y4m->src_c_dec_h = _y4m->dst_c_dec_h = _y4m->src_c_dec_v = |
| 876 | _y4m->dst_c_dec_v = 2; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 877 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 878 | /*Chroma filter required: read into the aux buf first.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 879 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 880 | 2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 881 | _y4m->convert = y4m_convert_42xmpeg2_42xjpeg; |
| 882 | } else if (strcmp(_y4m->chroma_type, "420paldv") == 0) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 883 | _y4m->src_c_dec_h = _y4m->dst_c_dec_h = _y4m->src_c_dec_v = |
| 884 | _y4m->dst_c_dec_v = 2; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 885 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 886 | /*Chroma filter required: read into the aux buf first. |
| 887 | We need to make two filter passes, so we need some extra space in the |
| 888 | aux buffer.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 889 | _y4m->aux_buf_sz = 3 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2); |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 890 | _y4m->aux_buf_read_sz = |
| 891 | 2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 892 | _y4m->convert = y4m_convert_42xpaldv_42xjpeg; |
| 893 | } else if (strcmp(_y4m->chroma_type, "422jpeg") == 0) { |
| 894 | _y4m->src_c_dec_h = _y4m->dst_c_dec_h = 2; |
| 895 | _y4m->src_c_dec_v = 1; |
| 896 | _y4m->dst_c_dec_v = 2; |
| 897 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 898 | /*Chroma filter required: read into the aux buf first.*/ |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 899 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = |
| 900 | 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 901 | _y4m->convert = y4m_convert_422jpeg_420jpeg; |
| 902 | } else if (strcmp(_y4m->chroma_type, "422") == 0) { |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 903 | _y4m->src_c_dec_h = 2; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 904 | _y4m->src_c_dec_v = 1; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 905 | if (only_420) { |
| 906 | _y4m->dst_c_dec_h = 2; |
| 907 | _y4m->dst_c_dec_v = 2; |
| 908 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
| 909 | /*Chroma filter required: read into the aux buf first. |
| 910 | We need to make two filter passes, so we need some extra space in the |
| 911 | aux buffer.*/ |
| 912 | _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 913 | _y4m->aux_buf_sz = |
| 914 | _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 915 | _y4m->convert = y4m_convert_422_420jpeg; |
| 916 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 917 | _y4m->aom_fmt = AOM_IMG_FMT_I422; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 918 | _y4m->bps = 16; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 919 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 920 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 921 | _y4m->dst_buf_read_sz = |
| 922 | _y4m->pic_w * _y4m->pic_h + 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 923 | /*Natively supported: no conversion required.*/ |
| 924 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 925 | _y4m->convert = y4m_convert_null; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 926 | } |
| 927 | } else if (strcmp(_y4m->chroma_type, "422p10") == 0) { |
| 928 | _y4m->src_c_dec_h = 2; |
| 929 | _y4m->src_c_dec_v = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 930 | _y4m->aom_fmt = AOM_IMG_FMT_I42216; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 931 | _y4m->bps = 20; |
| 932 | _y4m->bit_depth = 10; |
| 933 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 934 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
| 935 | _y4m->dst_buf_read_sz = 2 * (_y4m->pic_w * _y4m->pic_h + |
| 936 | 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h); |
| 937 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 938 | _y4m->convert = y4m_convert_null; |
| 939 | if (only_420) { |
| 940 | fprintf(stderr, "Unsupported conversion from 422p10 to 420jpeg\n"); |
| 941 | return -1; |
| 942 | } |
| 943 | } else if (strcmp(_y4m->chroma_type, "422p12") == 0) { |
| 944 | _y4m->src_c_dec_h = 2; |
| 945 | _y4m->src_c_dec_v = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 946 | _y4m->aom_fmt = AOM_IMG_FMT_I42216; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 947 | _y4m->bps = 24; |
| 948 | _y4m->bit_depth = 12; |
| 949 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 950 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
| 951 | _y4m->dst_buf_read_sz = 2 * (_y4m->pic_w * _y4m->pic_h + |
| 952 | 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h); |
| 953 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 954 | _y4m->convert = y4m_convert_null; |
| 955 | if (only_420) { |
| 956 | fprintf(stderr, "Unsupported conversion from 422p12 to 420jpeg\n"); |
| 957 | return -1; |
| 958 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 959 | } else if (strcmp(_y4m->chroma_type, "411") == 0) { |
| 960 | _y4m->src_c_dec_h = 4; |
| 961 | _y4m->dst_c_dec_h = 2; |
| 962 | _y4m->src_c_dec_v = 1; |
| 963 | _y4m->dst_c_dec_v = 2; |
| 964 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 965 | /*Chroma filter required: read into the aux buf first. |
| 966 | We need to make two filter passes, so we need some extra space in the |
| 967 | aux buffer.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 968 | _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 3) / 4) * _y4m->pic_h; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 969 | _y4m->aux_buf_sz = |
| 970 | _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 971 | _y4m->convert = y4m_convert_411_420jpeg; |
| 972 | } else if (strcmp(_y4m->chroma_type, "444") == 0) { |
| 973 | _y4m->src_c_dec_h = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 974 | _y4m->src_c_dec_v = 1; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 975 | if (only_420) { |
| 976 | _y4m->dst_c_dec_h = 2; |
| 977 | _y4m->dst_c_dec_v = 2; |
| 978 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
| 979 | /*Chroma filter required: read into the aux buf first. |
| 980 | We need to make two filter passes, so we need some extra space in the |
| 981 | aux buffer.*/ |
| 982 | _y4m->aux_buf_read_sz = 2 * _y4m->pic_w * _y4m->pic_h; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 983 | _y4m->aux_buf_sz = |
| 984 | _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 985 | _y4m->convert = y4m_convert_444_420jpeg; |
| 986 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 987 | _y4m->aom_fmt = AOM_IMG_FMT_I444; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 988 | _y4m->bps = 24; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 989 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 990 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
| 991 | _y4m->dst_buf_read_sz = 3 * _y4m->pic_w * _y4m->pic_h; |
| 992 | /*Natively supported: no conversion required.*/ |
| 993 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 994 | _y4m->convert = y4m_convert_null; |
| 995 | } |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 996 | } else if (strcmp(_y4m->chroma_type, "444p10") == 0) { |
| 997 | _y4m->src_c_dec_h = 1; |
| 998 | _y4m->src_c_dec_v = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 999 | _y4m->aom_fmt = AOM_IMG_FMT_I44416; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1000 | _y4m->bps = 30; |
| 1001 | _y4m->bit_depth = 10; |
| 1002 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 1003 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
| 1004 | _y4m->dst_buf_read_sz = 2 * 3 * _y4m->pic_w * _y4m->pic_h; |
| 1005 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 1006 | _y4m->convert = y4m_convert_null; |
| 1007 | if (only_420) { |
| 1008 | fprintf(stderr, "Unsupported conversion from 444p10 to 420jpeg\n"); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | } else if (strcmp(_y4m->chroma_type, "444p12") == 0) { |
| 1012 | _y4m->src_c_dec_h = 1; |
| 1013 | _y4m->src_c_dec_v = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1014 | _y4m->aom_fmt = AOM_IMG_FMT_I44416; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1015 | _y4m->bps = 36; |
| 1016 | _y4m->bit_depth = 12; |
| 1017 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 1018 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
| 1019 | _y4m->dst_buf_read_sz = 2 * 3 * _y4m->pic_w * _y4m->pic_h; |
| 1020 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 1021 | _y4m->convert = y4m_convert_null; |
| 1022 | if (only_420) { |
| 1023 | fprintf(stderr, "Unsupported conversion from 444p12 to 420jpeg\n"); |
| 1024 | return -1; |
| 1025 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1026 | } else if (strcmp(_y4m->chroma_type, "444alpha") == 0) { |
| 1027 | _y4m->src_c_dec_h = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1028 | _y4m->src_c_dec_v = 1; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1029 | if (only_420) { |
| 1030 | _y4m->dst_c_dec_h = 2; |
| 1031 | _y4m->dst_c_dec_v = 2; |
| 1032 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
| 1033 | /*Chroma filter required: read into the aux buf first. |
| 1034 | We need to make two filter passes, so we need some extra space in the |
| 1035 | aux buffer. |
| 1036 | The extra plane also gets read into the aux buf. |
| 1037 | It will be discarded.*/ |
| 1038 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 3 * _y4m->pic_w * _y4m->pic_h; |
| 1039 | _y4m->convert = y4m_convert_444_420jpeg; |
| 1040 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1041 | _y4m->aom_fmt = AOM_IMG_FMT_444A; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1042 | _y4m->bps = 32; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1043 | _y4m->dst_c_dec_h = _y4m->src_c_dec_h; |
| 1044 | _y4m->dst_c_dec_v = _y4m->src_c_dec_v; |
| 1045 | _y4m->dst_buf_read_sz = 4 * _y4m->pic_w * _y4m->pic_h; |
| 1046 | /*Natively supported: no conversion required.*/ |
| 1047 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 1048 | _y4m->convert = y4m_convert_null; |
| 1049 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1050 | } else if (strcmp(_y4m->chroma_type, "mono") == 0) { |
| 1051 | _y4m->src_c_dec_h = _y4m->src_c_dec_v = 0; |
| 1052 | _y4m->dst_c_dec_h = _y4m->dst_c_dec_v = 2; |
| 1053 | _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1054 | /*No extra space required, but we need to clear the chroma planes.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1055 | _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0; |
| 1056 | _y4m->convert = y4m_convert_mono_420jpeg; |
| 1057 | } else { |
| 1058 | fprintf(stderr, "Unknown chroma sampling type: %s\n", _y4m->chroma_type); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1059 | return -1; |
| 1060 | } |
| 1061 | /*The size of the final frame buffers is always computed from the |
| 1062 | destination chroma decimation type.*/ |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 1063 | _y4m->dst_buf_sz = |
| 1064 | _y4m->pic_w * _y4m->pic_h + |
| 1065 | 2 * ((_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h) * |
| 1066 | ((_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v); |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1067 | if (_y4m->bit_depth == 8) |
Yaowu Xu | a0af416 | 2014-10-08 08:38:15 -0700 | [diff] [blame] | 1068 | _y4m->dst_buf = (unsigned char *)malloc(_y4m->dst_buf_sz); |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1069 | else |
Yaowu Xu | a0af416 | 2014-10-08 08:38:15 -0700 | [diff] [blame] | 1070 | _y4m->dst_buf = (unsigned char *)malloc(2 * _y4m->dst_buf_sz); |
Jim Bankoski | e35c54e | 2014-08-19 09:00:44 -0700 | [diff] [blame] | 1071 | |
| 1072 | if (_y4m->aux_buf_sz > 0) |
| 1073 | _y4m->aux_buf = (unsigned char *)malloc(_y4m->aux_buf_sz); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1077 | void y4m_input_close(y4m_input *_y4m) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1078 | free(_y4m->dst_buf); |
| 1079 | free(_y4m->aux_buf); |
| 1080 | } |
| 1081 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1082 | int y4m_input_fetch_frame(y4m_input *_y4m, FILE *_fin, aom_image_t *_img) { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1083 | char frame[6]; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 1084 | int pic_sz; |
| 1085 | int c_w; |
| 1086 | int c_h; |
| 1087 | int c_sz; |
| 1088 | int bytes_per_sample = _y4m->bit_depth > 8 ? 2 : 1; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1089 | /*Read and skip the frame header.*/ |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 1090 | if (!file_read(frame, 6, _fin)) return 0; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1091 | if (memcmp(frame, "FRAME", 5)) { |
| 1092 | fprintf(stderr, "Loss of framing in Y4M input data\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1093 | return -1; |
| 1094 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1095 | if (frame[5] != '\n') { |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1096 | char c; |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 1097 | int j; |
| 1098 | for (j = 0; j < 79 && file_read(&c, 1, _fin) && c != '\n'; j++) { |
| 1099 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1100 | if (j == 79) { |
| 1101 | fprintf(stderr, "Error parsing Y4M frame header\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1102 | return -1; |
| 1103 | } |
| 1104 | } |
| 1105 | /*Read the frame data that needs no conversion.*/ |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 1106 | if (!file_read(_y4m->dst_buf, _y4m->dst_buf_read_sz, _fin)) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1107 | fprintf(stderr, "Error reading Y4M frame data.\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1108 | return -1; |
| 1109 | } |
| 1110 | /*Read the frame data that does need conversion.*/ |
James Zern | df0829f | 2014-02-12 16:30:43 -0800 | [diff] [blame] | 1111 | if (!file_read(_y4m->aux_buf, _y4m->aux_buf_read_sz, _fin)) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1112 | fprintf(stderr, "Error reading Y4M frame data.\n"); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1113 | return -1; |
| 1114 | } |
| 1115 | /*Now convert the just read frame.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1116 | (*_y4m->convert)(_y4m, _y4m->dst_buf, _y4m->aux_buf); |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1117 | /*Fill in the frame buffer pointers. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1118 | We don't use aom_img_wrap() because it forces padding for odd picture |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1119 | sizes, which would require a separate fread call for every row.*/ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1120 | memset(_img, 0, sizeof(*_img)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1121 | /*Y4M has the planes in Y'CbCr order, which libaom calls Y, U, and V.*/ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1122 | _img->fmt = _y4m->aom_fmt; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1123 | _img->w = _img->d_w = _y4m->pic_w; |
| 1124 | _img->h = _img->d_h = _y4m->pic_h; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1125 | _img->x_chroma_shift = _y4m->dst_c_dec_h >> 1; |
| 1126 | _img->y_chroma_shift = _y4m->dst_c_dec_v >> 1; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1127 | _img->bps = _y4m->bps; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1128 | |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1129 | /*Set up the buffer pointers.*/ |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1130 | pic_sz = _y4m->pic_w * _y4m->pic_h * bytes_per_sample; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1131 | c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1132 | c_w *= bytes_per_sample; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1133 | c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
| 1134 | c_sz = c_w * c_h; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1135 | _img->stride[AOM_PLANE_Y] = _img->stride[AOM_PLANE_ALPHA] = |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 1136 | _y4m->pic_w * bytes_per_sample; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1137 | _img->stride[AOM_PLANE_U] = _img->stride[AOM_PLANE_V] = c_w; |
| 1138 | _img->planes[AOM_PLANE_Y] = _y4m->dst_buf; |
| 1139 | _img->planes[AOM_PLANE_U] = _y4m->dst_buf + pic_sz; |
| 1140 | _img->planes[AOM_PLANE_V] = _y4m->dst_buf + pic_sz + c_sz; |
| 1141 | _img->planes[AOM_PLANE_ALPHA] = _y4m->dst_buf + pic_sz + 2 * c_sz; |
Timothy B. Terriberry | c4d7e5e | 2010-10-27 16:04:02 -0700 | [diff] [blame] | 1142 | return 1; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1143 | } |