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