y4minput.c 39.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 *
11 12
 * Based on code from the OggTheora software codec source code,
 * Copyright (C) 2002-2010 The Xiph.Org Foundation and contributors.
13
 */
14
#include <errno.h>
15 16
#include <stdlib.h>
#include <string.h>
17

18
#include "aom/aom_integer.h"
19 20
#include "y4minput.h"

21 22 23 24 25
// Reads 'size' bytes from 'file' into 'buf' with some fault tolerance.
// Returns true on success.
static int file_read(void *buf, size_t size, FILE *file) {
  const int kMaxRetries = 5;
  int retry_count = 0;
26
  int file_error;
27 28
  size_t len = 0;
  do {
clang-format's avatar
clang-format committed
29
    const size_t n = fread((uint8_t *)buf + len, 1, size - len, file);
30
    len += n;
31 32
    file_error = ferror(file);
    if (file_error) {
33 34 35 36 37 38 39 40 41
      if (errno == EINTR || errno == EAGAIN) {
        clearerr(file);
        continue;
      } else {
        fprintf(stderr, "Error reading file: %u of %u bytes read, %d: %s\n",
                (uint32_t)len, (uint32_t)size, errno, strerror(errno));
        return 0;
      }
    }
42 43 44
  } while (!feof(file) && len < size && ++retry_count < kMaxRetries);

  if (!feof(file) && len != size) {
clang-format's avatar
clang-format committed
45 46 47 48 49
    fprintf(stderr,
            "Error reading file: %u of %u bytes read,"
            " error: %d, retries: %d, %d: %s\n",
            (uint32_t)len, (uint32_t)size, file_error, retry_count, errno,
            strerror(errno));
50
  }
51 52 53
  return len == size;
}

John Koleszar's avatar
John Koleszar committed
54
static int y4m_parse_tags(y4m_input *_y4m, char *_tags) {
clang-format's avatar
clang-format committed
55 56 57 58 59 60
  int got_w;
  int got_h;
  int got_fps;
  int got_interlace;
  int got_par;
  int got_chroma;
61 62
  char *p;
  char *q;
John Koleszar's avatar
John Koleszar committed
63 64
  got_w = got_h = got_fps = got_interlace = got_par = got_chroma = 0;
  for (p = _tags;; p = q) {
65
    /*Skip any leading spaces.*/
66
    while (*p == ' ') p++;
67
    /*If that's all we have, stop.*/
68
    if (p[0] == '\0') break;
69
    /*Find the end of this tag.*/
70 71
    for (q = p + 1; *q != '\0' && *q != ' '; q++) {
    }
72
    /*Process the tag.*/
John Koleszar's avatar
John Koleszar committed
73 74
    switch (p[0]) {
      case 'W': {
clang-format's avatar
clang-format committed
75
        if (sscanf(p + 1, "%d", &_y4m->pic_w) != 1) return -1;
John Koleszar's avatar
John Koleszar committed
76
        got_w = 1;
clang-format's avatar
clang-format committed
77
      } break;
John Koleszar's avatar
John Koleszar committed
78
      case 'H': {
clang-format's avatar
clang-format committed
79
        if (sscanf(p + 1, "%d", &_y4m->pic_h) != 1) return -1;
John Koleszar's avatar
John Koleszar committed
80
        got_h = 1;
clang-format's avatar
clang-format committed
81
      } break;
John Koleszar's avatar
John Koleszar committed
82 83
      case 'F': {
        if (sscanf(p + 1, "%d:%d", &_y4m->fps_n, &_y4m->fps_d) != 2) {
84 85
          return -1;
        }
John Koleszar's avatar
John Koleszar committed
86
        got_fps = 1;
clang-format's avatar
clang-format committed
87
      } break;
John Koleszar's avatar
John Koleszar committed
88 89 90
      case 'I': {
        _y4m->interlace = p[1];
        got_interlace = 1;
clang-format's avatar
clang-format committed
91
      } break;
John Koleszar's avatar
John Koleszar committed
92 93
      case 'A': {
        if (sscanf(p + 1, "%d:%d", &_y4m->par_n, &_y4m->par_d) != 2) {
94 95
          return -1;
        }
John Koleszar's avatar
John Koleszar committed
96
        got_par = 1;
clang-format's avatar
clang-format committed
97
      } break;
John Koleszar's avatar
John Koleszar committed
98
      case 'C': {
clang-format's avatar
clang-format committed
99
        if (q - p > 16) return -1;
John Koleszar's avatar
John Koleszar committed
100 101 102
        memcpy(_y4m->chroma_type, p + 1, q - p - 1);
        _y4m->chroma_type[q - p - 1] = '\0';
        got_chroma = 1;
clang-format's avatar
clang-format committed
103 104
      } break;
        /*Ignore unknown tags.*/
105 106
    }
  }
clang-format's avatar
clang-format committed
107 108 109
  if (!got_w || !got_h || !got_fps) return -1;
  if (!got_interlace) _y4m->interlace = '?';
  if (!got_par) _y4m->par_n = _y4m->par_d = 0;
110 111
  /*Chroma-type is not specified in older files, e.g., those generated by
     mplayer.*/
clang-format's avatar
clang-format committed
112
  if (!got_chroma) strcpy(_y4m->chroma_type, "420");
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  return 0;
}

/*All anti-aliasing filters in the following conversion functions are based on
   one of two window functions:
  The 6-tap Lanczos window (for down-sampling and shifts):
   sinc(\pi*t)*sinc(\pi*t/3), |t|<3  (sinc(t)==sin(t)/t)
   0,                         |t|>=3
  The 4-tap Mitchell window (for up-sampling):
   7|t|^3-12|t|^2+16/3,             |t|<1
   -(7/3)|x|^3+12|x|^2-20|x|+32/3,  |t|<2
   0,                               |t|>=2
  The number of taps is intentionally kept small to reduce computational
   overhead and limit ringing.

  The taps from these filters are scaled so that their sum is 1, and the result
   is scaled by 128 and rounded to integers to create a filter whose
   intermediate values fit inside 16 bits.
  Coefficients are rounded in such a way as to ensure their sum is still 128,
   which is usually equivalent to normal rounding.

  Conversions which require both horizontal and vertical filtering could
   have these steps pipelined, for less memory consumption and better cache
   performance, but we do them separately for simplicity.*/

clang-format's avatar
clang-format committed
138 139 140
#define OC_MINI(_a, _b) ((_a) > (_b) ? (_b) : (_a))
#define OC_MAXI(_a, _b) ((_a) < (_b) ? (_b) : (_a))
#define OC_CLAMPI(_a, _b, _c) (OC_MAXI(_a, OC_MINI(_b, _c)))
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

/*420jpeg chroma samples are sited like:
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  420mpeg2 chroma samples are sited like:
  Y-------Y-------Y-------Y-------
  |       |       |       |
  BR      |       BR      |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  BR      |       BR      |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  We use a resampling filter to shift the site locations one quarter pixel (at
   the chroma plane's resolution) to the right.
  The 4:2:2 modes look exactly the same, except there are twice as many chroma
   lines, and they are vertically co-sited with the luma samples in both the
   mpeg2 and jpeg cases (thus requiring no vertical resampling).*/
static void y4m_42xmpeg2_42xjpeg_helper(unsigned char *_dst,
clang-format's avatar
clang-format committed
184 185
                                        const unsigned char *_src, int _c_w,
                                        int _c_h) {
186 187
  int y;
  int x;
John Koleszar's avatar
John Koleszar committed
188
  for (y = 0; y < _c_h; y++) {
189 190
    /*Filter: [4 -17 114 35 -9 1]/128, derived from a 6-tap Lanczos
       window.*/
John Koleszar's avatar
John Koleszar committed
191
    for (x = 0; x < OC_MINI(_c_w, 2); x++) {
clang-format's avatar
clang-format committed
192
      _dst[x] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
193 194 195 196 197 198
          0,
          (4 * _src[0] - 17 * _src[OC_MAXI(x - 1, 0)] + 114 * _src[x] +
           35 * _src[OC_MINI(x + 1, _c_w - 1)] -
           9 * _src[OC_MINI(x + 2, _c_w - 1)] + _src[OC_MINI(x + 3, _c_w - 1)] +
           64) >>
              7,
clang-format's avatar
clang-format committed
199
          255);
200
    }
John Koleszar's avatar
John Koleszar committed
201
    for (; x < _c_w - 3; x++) {
clang-format's avatar
clang-format committed
202
      _dst[x] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
203 204 205 206
          0,
          (4 * _src[x - 2] - 17 * _src[x - 1] + 114 * _src[x] +
           35 * _src[x + 1] - 9 * _src[x + 2] + _src[x + 3] + 64) >>
              7,
clang-format's avatar
clang-format committed
207
          255);
208
    }
John Koleszar's avatar
John Koleszar committed
209
    for (; x < _c_w; x++) {
clang-format's avatar
clang-format committed
210
      _dst[x] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
211 212 213 214 215
          0,
          (4 * _src[x - 2] - 17 * _src[x - 1] + 114 * _src[x] +
           35 * _src[OC_MINI(x + 1, _c_w - 1)] -
           9 * _src[OC_MINI(x + 2, _c_w - 1)] + _src[_c_w - 1] + 64) >>
              7,
clang-format's avatar
clang-format committed
216
          255);
217
    }
John Koleszar's avatar
John Koleszar committed
218 219
    _dst += _c_w;
    _src += _c_w;
220 221 222 223
  }
}

/*Handles both 422 and 420mpeg2 to 422jpeg and 420jpeg, respectively.*/
John Koleszar's avatar
John Koleszar committed
224 225
static void y4m_convert_42xmpeg2_42xjpeg(y4m_input *_y4m, unsigned char *_dst,
                                         unsigned char *_aux) {
226 227 228 229 230
  int c_w;
  int c_h;
  int c_sz;
  int pli;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
231
  _dst += _y4m->pic_w * _y4m->pic_h;
232
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
233 234 235 236 237 238 239
  c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h;
  c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v;
  c_sz = c_w * c_h;
  for (pli = 1; pli < 3; pli++) {
    y4m_42xmpeg2_42xjpeg_helper(_dst, _aux, c_w, c_h);
    _dst += c_sz;
    _aux += c_sz;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  }
}

/*This format is only used for interlaced content, but is included for
   completeness.

  420jpeg chroma samples are sited like:
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  420paldv chroma samples are sited like:
  YR------Y-------YR------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YB------Y-------YB------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YR------Y-------YR------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YB------Y-------YB------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  We use a resampling filter to shift the site locations one quarter pixel (at
   the chroma plane's resolution) to the right.
  Then we use another filter to move the C_r location down one quarter pixel,
   and the C_b location up one quarter pixel.*/
John Koleszar's avatar
John Koleszar committed
286 287
static void y4m_convert_42xpaldv_42xjpeg(y4m_input *_y4m, unsigned char *_dst,
                                         unsigned char *_aux) {
288
  unsigned char *tmp;
clang-format's avatar
clang-format committed
289 290 291 292 293 294
  int c_w;
  int c_h;
  int c_sz;
  int pli;
  int y;
  int x;
295
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
296
  _dst += _y4m->pic_w * _y4m->pic_h;
297
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
298 299 300 301 302
  c_w = (_y4m->pic_w + 1) / 2;
  c_h = (_y4m->pic_h + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h;
  c_sz = c_w * c_h;
  tmp = _aux + 2 * c_sz;
  for (pli = 1; pli < 3; pli++) {
303 304 305
    /*First do the horizontal re-sampling.
      This is the same as the mpeg2 case, except that after the horizontal
       case, we need to apply a second vertical filter.*/
John Koleszar's avatar
John Koleszar committed
306 307 308 309
    y4m_42xmpeg2_42xjpeg_helper(tmp, _aux, c_w, c_h);
    _aux += c_sz;
    switch (pli) {
      case 1: {
310 311
        /*Slide C_b up a quarter-pel.
          This is the same filter used above, but in the other order.*/
John Koleszar's avatar
John Koleszar committed
312 313
        for (x = 0; x < c_w; x++) {
          for (y = 0; y < OC_MINI(c_h, 3); y++) {
clang-format's avatar
clang-format committed
314
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
315 316 317 318 319 320
                0,
                (tmp[0] - 9 * tmp[OC_MAXI(y - 2, 0) * c_w] +
                 35 * tmp[OC_MAXI(y - 1, 0) * c_w] + 114 * tmp[y * c_w] -
                 17 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] +
                 4 * tmp[OC_MINI(y + 2, c_h - 1) * c_w] + 64) >>
                    7,
clang-format's avatar
clang-format committed
321
                255);
322
          }
John Koleszar's avatar
John Koleszar committed
323
          for (; y < c_h - 2; y++) {
clang-format's avatar
clang-format committed
324
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
325 326 327 328 329
                0,
                (tmp[(y - 3) * c_w] - 9 * tmp[(y - 2) * c_w] +
                 35 * tmp[(y - 1) * c_w] + 114 * tmp[y * c_w] -
                 17 * tmp[(y + 1) * c_w] + 4 * tmp[(y + 2) * c_w] + 64) >>
                    7,
clang-format's avatar
clang-format committed
330
                255);
331
          }
John Koleszar's avatar
John Koleszar committed
332
          for (; y < c_h; y++) {
clang-format's avatar
clang-format committed
333
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
334 335 336 337 338 339
                0,
                (tmp[(y - 3) * c_w] - 9 * tmp[(y - 2) * c_w] +
                 35 * tmp[(y - 1) * c_w] + 114 * tmp[y * c_w] -
                 17 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] +
                 4 * tmp[(c_h - 1) * c_w] + 64) >>
                    7,
clang-format's avatar
clang-format committed
340
                255);
341 342 343 344
          }
          _dst++;
          tmp++;
        }
John Koleszar's avatar
John Koleszar committed
345 346
        _dst += c_sz - c_w;
        tmp -= c_w;
clang-format's avatar
clang-format committed
347
      } break;
John Koleszar's avatar
John Koleszar committed
348
      case 2: {
349 350
        /*Slide C_r down a quarter-pel.
          This is the same as the horizontal filter.*/
John Koleszar's avatar
John Koleszar committed
351 352
        for (x = 0; x < c_w; x++) {
          for (y = 0; y < OC_MINI(c_h, 2); y++) {
clang-format's avatar
clang-format committed
353 354 355 356 357 358 359 360
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(
                0,
                (4 * tmp[0] - 17 * tmp[OC_MAXI(y - 1, 0) * c_w] +
                 114 * tmp[y * c_w] + 35 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] -
                 9 * tmp[OC_MINI(y + 2, c_h - 1) * c_w] +
                 tmp[OC_MINI(y + 3, c_h - 1) * c_w] + 64) >>
                    7,
                255);
361
          }
John Koleszar's avatar
John Koleszar committed
362
          for (; y < c_h - 3; y++) {
clang-format's avatar
clang-format committed
363
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
364 365 366 367 368
                0,
                (4 * tmp[(y - 2) * c_w] - 17 * tmp[(y - 1) * c_w] +
                 114 * tmp[y * c_w] + 35 * tmp[(y + 1) * c_w] -
                 9 * tmp[(y + 2) * c_w] + tmp[(y + 3) * c_w] + 64) >>
                    7,
clang-format's avatar
clang-format committed
369
                255);
370
          }
John Koleszar's avatar
John Koleszar committed
371
          for (; y < c_h; y++) {
clang-format's avatar
clang-format committed
372 373 374 375 376 377 378 379
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(
                0,
                (4 * tmp[(y - 2) * c_w] - 17 * tmp[(y - 1) * c_w] +
                 114 * tmp[y * c_w] + 35 * tmp[OC_MINI(y + 1, c_h - 1) * c_w] -
                 9 * tmp[OC_MINI(y + 2, c_h - 1) * c_w] + tmp[(c_h - 1) * c_w] +
                 64) >>
                    7,
                255);
380 381 382 383
          }
          _dst++;
          tmp++;
        }
clang-format's avatar
clang-format committed
384
      } break;
385 386 387 388 389 390 391 392 393 394 395 396 397 398
    }
    /*For actual interlaced material, this would have to be done separately on
       each field, and the shift amounts would be different.
      C_r moves down 1/8, C_b up 3/8 in the top field, and C_r moves down 3/8,
       C_b up 1/8 in the bottom field.
      The corresponding filters would be:
       Down 1/8 (reverse order for up): [3 -11 125 15 -4 0]/128
       Down 3/8 (reverse order for up): [4 -19 98 56 -13 2]/128*/
  }
}

/*Perform vertical filtering to reduce a single plane from 4:2:2 to 4:2:0.
  This is used as a helper by several converation routines.*/
static void y4m_422jpeg_420jpeg_helper(unsigned char *_dst,
clang-format's avatar
clang-format committed
399 400
                                       const unsigned char *_src, int _c_w,
                                       int _c_h) {
401 402 403
  int y;
  int x;
  /*Filter: [3 -17 78 78 -17 3]/128, derived from a 6-tap Lanczos window.*/
John Koleszar's avatar
John Koleszar committed
404 405
  for (x = 0; x < _c_w; x++) {
    for (y = 0; y < OC_MINI(_c_h, 2); y += 2) {
clang-format's avatar
clang-format committed
406
      _dst[(y >> 1) * _c_w] =
clang-format's avatar
clang-format committed
407 408 409 410 411
          OC_CLAMPI(0,
                    (64 * _src[0] + 78 * _src[OC_MINI(1, _c_h - 1) * _c_w] -
                     17 * _src[OC_MINI(2, _c_h - 1) * _c_w] +
                     3 * _src[OC_MINI(3, _c_h - 1) * _c_w] + 64) >>
                        7,
clang-format's avatar
clang-format committed
412
                    255);
413
    }
John Koleszar's avatar
John Koleszar committed
414
    for (; y < _c_h - 3; y += 2) {
clang-format's avatar
clang-format committed
415
      _dst[(y >> 1) * _c_w] =
clang-format's avatar
clang-format committed
416 417 418 419 420
          OC_CLAMPI(0,
                    (3 * (_src[(y - 2) * _c_w] + _src[(y + 3) * _c_w]) -
                     17 * (_src[(y - 1) * _c_w] + _src[(y + 2) * _c_w]) +
                     78 * (_src[y * _c_w] + _src[(y + 1) * _c_w]) + 64) >>
                        7,
clang-format's avatar
clang-format committed
421
                    255);
422
    }
John Koleszar's avatar
John Koleszar committed
423
    for (; y < _c_h; y += 2) {
clang-format's avatar
clang-format committed
424 425 426 427 428 429 430 431
      _dst[(y >> 1) * _c_w] = OC_CLAMPI(
          0,
          (3 * (_src[(y - 2) * _c_w] + _src[(_c_h - 1) * _c_w]) -
           17 * (_src[(y - 1) * _c_w] + _src[OC_MINI(y + 2, _c_h - 1) * _c_w]) +
           78 * (_src[y * _c_w] + _src[OC_MINI(y + 1, _c_h - 1) * _c_w]) +
           64) >>
              7,
          255);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    }
    _src++;
    _dst++;
  }
}

/*420jpeg chroma samples are sited like:
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  422jpeg chroma samples are sited like:
  Y---BR--Y-------Y---BR--Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y---BR--Y-------Y---BR--Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y---BR--Y-------Y---BR--Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y---BR--Y-------Y---BR--Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  We use a resampling filter to decimate the chroma planes by two in the
   vertical direction.*/
John Koleszar's avatar
John Koleszar committed
476 477
static void y4m_convert_422jpeg_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                        unsigned char *_aux) {
478 479 480 481 482 483 484 485
  int c_w;
  int c_h;
  int c_sz;
  int dst_c_w;
  int dst_c_h;
  int dst_c_sz;
  int pli;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
486
  _dst += _y4m->pic_w * _y4m->pic_h;
487
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
488 489 490 491 492 493 494 495 496 497
  c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h;
  c_h = _y4m->pic_h;
  dst_c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h;
  dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v;
  c_sz = c_w * c_h;
  dst_c_sz = dst_c_w * dst_c_h;
  for (pli = 1; pli < 3; pli++) {
    y4m_422jpeg_420jpeg_helper(_dst, _aux, c_w, c_h);
    _aux += c_sz;
    _dst += dst_c_sz;
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
  }
}

/*420jpeg chroma samples are sited like:
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  422 chroma samples are sited like:
  YBR-----Y-------YBR-----Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YBR-----Y-------YBR-----Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YBR-----Y-------YBR-----Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YBR-----Y-------YBR-----Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  We use a resampling filter to shift the original site locations one quarter
   pixel (at the original chroma resolution) to the right.
  Then we use a second resampling filter to decimate the chroma planes by two
   in the vertical direction.*/
John Koleszar's avatar
John Koleszar committed
541 542
static void y4m_convert_422_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                    unsigned char *_aux) {
543
  unsigned char *tmp;
clang-format's avatar
clang-format committed
544 545 546 547 548 549
  int c_w;
  int c_h;
  int c_sz;
  int dst_c_h;
  int dst_c_sz;
  int pli;
550
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
551
  _dst += _y4m->pic_w * _y4m->pic_h;
552
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
553 554 555 556 557 558 559
  c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h;
  c_h = _y4m->pic_h;
  dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v;
  c_sz = c_w * c_h;
  dst_c_sz = c_w * dst_c_h;
  tmp = _aux + 2 * c_sz;
  for (pli = 1; pli < 3; pli++) {
560 561 562 563
    /*In reality, the horizontal and vertical steps could be pipelined, for
       less memory consumption and better cache performance, but we do them
       separately for simplicity.*/
    /*First do horizontal filtering (convert to 422jpeg)*/
John Koleszar's avatar
John Koleszar committed
564
    y4m_42xmpeg2_42xjpeg_helper(tmp, _aux, c_w, c_h);
565
    /*Now do the vertical filtering.*/
John Koleszar's avatar
John Koleszar committed
566 567 568
    y4m_422jpeg_420jpeg_helper(_dst, tmp, c_w, c_h);
    _aux += c_sz;
    _dst += dst_c_sz;
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
  }
}

/*420jpeg chroma samples are sited like:
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |   BR  |       |   BR  |
  |       |       |       |
  Y-------Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  411 chroma samples are sited like:
  YBR-----Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YBR-----Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YBR-----Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |
  YBR-----Y-------Y-------Y-------
  |       |       |       |
  |       |       |       |
  |       |       |       |

  We use a filter to resample at site locations one eighth pixel (at the source
   chroma plane's horizontal resolution) and five eighths of a pixel to the
   right.
  Then we use another filter to decimate the planes by 2 in the vertical
   direction.*/
John Koleszar's avatar
John Koleszar committed
613 614
static void y4m_convert_411_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                    unsigned char *_aux) {
615
  unsigned char *tmp;
clang-format's avatar
clang-format committed
616 617 618 619 620 621 622 623 624 625
  int c_w;
  int c_h;
  int c_sz;
  int dst_c_w;
  int dst_c_h;
  int dst_c_sz;
  int tmp_sz;
  int pli;
  int y;
  int x;
626
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
627
  _dst += _y4m->pic_w * _y4m->pic_h;
628
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
629 630 631 632 633 634 635 636 637
  c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h;
  c_h = _y4m->pic_h;
  dst_c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h;
  dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v;
  c_sz = c_w * c_h;
  dst_c_sz = dst_c_w * dst_c_h;
  tmp_sz = dst_c_w * c_h;
  tmp = _aux + 2 * c_sz;
  for (pli = 1; pli < 3; pli++) {
638 639 640 641
    /*In reality, the horizontal and vertical steps could be pipelined, for
       less memory consumption and better cache performance, but we do them
       separately for simplicity.*/
    /*First do horizontal filtering (convert to 422jpeg)*/
John Koleszar's avatar
John Koleszar committed
642
    for (y = 0; y < c_h; y++) {
643 644
      /*Filters: [1 110 18 -1]/128 and [-3 50 86 -5]/128, both derived from a
         4-tap Mitchell window.*/
John Koleszar's avatar
John Koleszar committed
645
      for (x = 0; x < OC_MINI(c_w, 1); x++) {
clang-format's avatar
clang-format committed
646
        tmp[x << 1] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
647 648 649 650
            0,
            (111 * _aux[0] + 18 * _aux[OC_MINI(1, c_w - 1)] -
             _aux[OC_MINI(2, c_w - 1)] + 64) >>
                7,
clang-format's avatar
clang-format committed
651 652
            255);
        tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
653 654 655 656
            0,
            (47 * _aux[0] + 86 * _aux[OC_MINI(1, c_w - 1)] -
             5 * _aux[OC_MINI(2, c_w - 1)] + 64) >>
                7,
clang-format's avatar
clang-format committed
657
            255);
658
      }
John Koleszar's avatar
John Koleszar committed
659
      for (; x < c_w - 2; x++) {
clang-format's avatar
clang-format committed
660
        tmp[x << 1] =
clang-format's avatar
clang-format committed
661 662 663 664
            (unsigned char)OC_CLAMPI(0,
                                     (_aux[x - 1] + 110 * _aux[x] +
                                      18 * _aux[x + 1] - _aux[x + 2] + 64) >>
                                         7,
clang-format's avatar
clang-format committed
665 666
                                     255);
        tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
667 668 669 670
            0,
            (-3 * _aux[x - 1] + 50 * _aux[x] + 86 * _aux[x + 1] -
             5 * _aux[x + 2] + 64) >>
                7,
clang-format's avatar
clang-format committed
671
            255);
672
      }
John Koleszar's avatar
John Koleszar committed
673
      for (; x < c_w; x++) {
clang-format's avatar
clang-format committed
674
        tmp[x << 1] = (unsigned char)OC_CLAMPI(
clang-format's avatar
clang-format committed
675 676 677 678
            0,
            (_aux[x - 1] + 110 * _aux[x] + 18 * _aux[OC_MINI(x + 1, c_w - 1)] -
             _aux[c_w - 1] + 64) >>
                7,
clang-format's avatar
clang-format committed
679
            255);
John Koleszar's avatar
John Koleszar committed
680
        if ((x << 1 | 1) < dst_c_w) {
clang-format's avatar
clang-format committed
681 682 683 684 685 686
          tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI(
              0,
              (-3 * _aux[x - 1] + 50 * _aux[x] +
               86 * _aux[OC_MINI(x + 1, c_w - 1)] - 5 * _aux[c_w - 1] + 64) >>
                  7,
              255);
687 688
        }
      }
John Koleszar's avatar
John Koleszar committed
689 690
      tmp += dst_c_w;
      _aux += c_w;
691
    }
John Koleszar's avatar
John Koleszar committed
692
    tmp -= tmp_sz;
693
    /*Now do the vertical filtering.*/
John Koleszar's avatar
John Koleszar committed
694 695
    y4m_422jpeg_420jpeg_helper(_dst, tmp, dst_c_w, c_h);
    _dst += dst_c_sz;
696 697 698 699
  }
}

/*Convert 444 to 420jpeg.*/
John Koleszar's avatar
John Koleszar committed
700 701
static void y4m_convert_444_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                    unsigned char *_aux) {
702
  unsigned char *tmp;
clang-format's avatar
clang-format committed
703 704 705 706 707 708 709 710 711 712
  int c_w;
  int c_h;
  int c_sz;
  int dst_c_w;
  int dst_c_h;
  int dst_c_sz;
  int tmp_sz;
  int pli;
  int y;
  int x;
713
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
714
  _dst += _y4m->pic_w * _y4m->pic_h;
715
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
716 717 718 719 720 721 722 723 724
  c_w = (_y4m->pic_w + _y4m->src_c_dec_h - 1) / _y4m->src_c_dec_h;
  c_h = _y4m->pic_h;
  dst_c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h;
  dst_c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v;
  c_sz = c_w * c_h;
  dst_c_sz = dst_c_w * dst_c_h;
  tmp_sz = dst_c_w * c_h;
  tmp = _aux + 2 * c_sz;
  for (pli = 1; pli < 3; pli++) {
725
    /*Filter: [3 -17 78 78 -17 3]/128, derived from a 6-tap Lanczos window.*/
John Koleszar's avatar
John Koleszar committed
726 727
    for (y = 0; y < c_h; y++) {
      for (x = 0; x < OC_MINI(c_w, 2); x += 2) {
clang-format's avatar
clang-format committed
728 729 730 731 732 733
        tmp[x >> 1] = OC_CLAMPI(0,
                                (64 * _aux[0] + 78 * _aux[OC_MINI(1, c_w - 1)] -
                                 17 * _aux[OC_MINI(2, c_w - 1)] +
                                 3 * _aux[OC_MINI(3, c_w - 1)] + 64) >>
                                    7,
                                255);
734
      }
John Koleszar's avatar
John Koleszar committed
735
      for (; x < c_w - 3; x += 2) {
clang-format's avatar
clang-format committed
736 737 738 739 740
        tmp[x >> 1] = OC_CLAMPI(0,
                                (3 * (_aux[x - 2] + _aux[x + 3]) -
                                 17 * (_aux[x - 1] + _aux[x + 2]) +
                                 78 * (_aux[x] + _aux[x + 1]) + 64) >>
                                    7,
clang-format's avatar
clang-format committed
741
                                255);
742
      }
John Koleszar's avatar
John Koleszar committed
743
      for (; x < c_w; x += 2) {
clang-format's avatar
clang-format committed
744 745 746 747 748 749 750
        tmp[x >> 1] =
            OC_CLAMPI(0,
                      (3 * (_aux[x - 2] + _aux[c_w - 1]) -
                       17 * (_aux[x - 1] + _aux[OC_MINI(x + 2, c_w - 1)]) +
                       78 * (_aux[x] + _aux[OC_MINI(x + 1, c_w - 1)]) + 64) >>
                          7,
                      255);
751
      }
John Koleszar's avatar
John Koleszar committed
752 753
      tmp += dst_c_w;
      _aux += c_w;
754
    }
John Koleszar's avatar
John Koleszar committed
755
    tmp -= tmp_sz;
756
    /*Now do the vertical filtering.*/
John Koleszar's avatar
John Koleszar committed
757 758
    y4m_422jpeg_420jpeg_helper(_dst, tmp, dst_c_w, c_h);
    _dst += dst_c_sz;
759 760 761 762
  }
}

/*The image is padded with empty chroma components at 4:2:0.*/
John Koleszar's avatar
John Koleszar committed
763 764
static void y4m_convert_mono_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                     unsigned char *_aux) {
765
  int c_sz;
Yaowu Xu's avatar
Yaowu Xu committed
766
  (void)_aux;
John Koleszar's avatar
John Koleszar committed
767 768 769 770
  _dst += _y4m->pic_w * _y4m->pic_h;
  c_sz = ((_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h) *
         ((_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v);
  memset(_dst, 128, c_sz * 2);
771 772 773
}

/*No conversion function needed.*/
John Koleszar's avatar
John Koleszar committed
774 775
static void y4m_convert_null(y4m_input *_y4m, unsigned char *_dst,
                             unsigned char *_aux) {
Yaowu Xu's avatar
Yaowu Xu committed
776 777 778
  (void)_y4m;
  (void)_dst;
  (void)_aux;
779 780
}

781 782
int y4m_input_open(y4m_input *_y4m, FILE *_fin, char *_skip, int _nskip,
                   int only_420) {
clang-format's avatar
clang-format committed
783 784 785
  char buffer[80] = { 0 };
  int ret;
  int i;
786
  /*Read until newline, or 80 cols, whichever happens first.*/
John Koleszar's avatar
John Koleszar committed
787 788 789
  for (i = 0; i < 79; i++) {
    if (_nskip > 0) {
      buffer[i] = *_skip++;
790
      _nskip--;
John Koleszar's avatar
John Koleszar committed
791
    } else {
792
      if (!file_read(buffer + i, 1, _fin)) return -1;
793
    }
clang-format's avatar
clang-format committed
794
    if (buffer[i] == '\n') break;
795 796
  }
  /*We skipped too much header data.*/
clang-format's avatar
clang-format committed
797
  if (_nskip > 0) return -1;
John Koleszar's avatar
John Koleszar committed
798 799
  if (i == 79) {
    fprintf(stderr, "Error parsing header; not a YUV2MPEG2 file?\n");
800 801
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
802 803 804
  buffer[i] = '\0';
  if (memcmp(buffer, "YUV4MPEG", 8)) {
    fprintf(stderr, "Incomplete magic for YUV4MPEG file.\n");
805 806
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
807 808
  if (buffer[8] != '2') {
    fprintf(stderr, "Incorrect YUV input file version; YUV4MPEG2 required.\n");
809
  }
John Koleszar's avatar
John Koleszar committed
810 811 812
  ret = y4m_parse_tags(_y4m, buffer + 5);
  if (ret < 0) {
    fprintf(stderr, "Error parsing YUV4MPEG2 header.\n");
813 814
    return ret;
  }
John Koleszar's avatar
John Koleszar committed
815
  if (_y4m->interlace == '?') {
clang-format's avatar
clang-format committed
816 817
    fprintf(stderr,
            "Warning: Input video interlacing format unknown; "
John Koleszar's avatar
John Koleszar committed
818 819
            "assuming progressive scan.\n");
  } else if (_y4m->interlace != 'p') {
clang-format's avatar
clang-format committed
820 821
    fprintf(stderr,
            "Input video is interlaced; "
John Koleszar's avatar
John Koleszar committed
822
            "Only progressive scan handled.\n");
823 824
    return -1;
  }
825
  _y4m->aom_fmt = AOM_IMG_FMT_I420;
826 827
  _y4m->bps = 12;
  _y4m->bit_depth = 8;
John Koleszar's avatar
John Koleszar committed
828 829
  if (strcmp(_y4m->chroma_type, "420") == 0 ||
      strcmp(_y4m->chroma_type, "420jpeg") == 0) {
clang-format's avatar
clang-format committed
830 831 832 833 834
    _y4m->src_c_dec_h = _y4m->dst_c_dec_h = _y4m->src_c_dec_v =
        _y4m->dst_c_dec_v = 2;
    _y4m->dst_buf_read_sz =
        _y4m->pic_w * _y4m->pic_h +
        2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
835
    /* Natively supported: no conversion required. */
John Koleszar's avatar
John Koleszar committed
836 837
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
838 839 840 841 842
  } else if (strcmp(_y4m->chroma_type, "420p10") == 0) {
    _y4m->src_c_dec_h = 2;
    _y4m->dst_c_dec_h = 2;
    _y4m->src_c_dec_v = 2;
    _y4m->dst_c_dec_v = 2;
clang-format's avatar
clang-format committed
843 844 845
    _y4m->dst_buf_read_sz =
        2 * (_y4m->pic_w * _y4m->pic_h +
             2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2));
846 847 848 849 850
    /* Natively supported: no conversion required. */
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
    _y4m->bit_depth = 10;
    _y4m->bps = 15;
851
    _y4m->aom_fmt = AOM_IMG_FMT_I42016;
852 853 854 855 856 857 858 859 860
    if (only_420) {
      fprintf(stderr, "Unsupported conversion from 420p10 to 420jpeg\n");
      return -1;
    }
  } else if (strcmp(_y4m->chroma_type, "420p12") == 0) {
    _y4m->src_c_dec_h = 2;
    _y4m->dst_c_dec_h = 2;
    _y4m->src_c_dec_v = 2;
    _y4m->dst_c_dec_v = 2;
clang-format's avatar
clang-format committed
861 862 863
    _y4m->dst_buf_read_sz =
        2 * (_y4m->pic_w * _y4m->pic_h +
             2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2));
864 865 866 867 868
    /* Natively supported: no conversion required. */
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
    _y4m->bit_depth = 12;
    _y4m->bps = 18;
869
    _y4m->aom_fmt = AOM_IMG_FMT_I42016;
870 871 872 873
    if (only_420) {
      fprintf(stderr, "Unsupported conversion from 420p12 to 420jpeg\n");
      return -1;
    }
John Koleszar's avatar
John Koleszar committed
874
  } else if (strcmp(_y4m->chroma_type, "420mpeg2") == 0) {
clang-format's avatar
clang-format committed
875 876
    _y4m->src_c_dec_h = _y4m->dst_c_dec_h = _y4m->src_c_dec_v =
        _y4m->dst_c_dec_v = 2;
John Koleszar's avatar
John Koleszar committed
877
    _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
878
    /*Chroma filter required: read into the aux buf first.*/
John Koleszar's avatar
John Koleszar committed
879
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz =
clang-format's avatar
clang-format committed
880
        2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
John Koleszar's avatar
John Koleszar committed
881 882
    _y4m->convert = y4m_convert_42xmpeg2_42xjpeg;
  } else if (strcmp(_y4m->chroma_type, "420paldv") == 0) {
clang-format's avatar
clang-format committed
883 884
    _y4m->src_c_dec_h = _y4m->dst_c_dec_h = _y4m->src_c_dec_v =
        _y4m->dst_c_dec_v = 2;
John Koleszar's avatar
John Koleszar committed
885
    _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
886 887 888
    /*Chroma filter required: read into the aux buf first.
      We need to make two filter passes, so we need some extra space in the
       aux buffer.*/
John Koleszar's avatar
John Koleszar committed
889
    _y4m->aux_buf_sz = 3 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
clang-format's avatar
clang-format committed
890 891
    _y4m->aux_buf_read_sz =
        2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
John Koleszar's avatar
John Koleszar committed
892 893 894 895 896 897
    _y4m->convert = y4m_convert_42xpaldv_42xjpeg;
  } else if (strcmp(_y4m->chroma_type, "422jpeg") == 0) {
    _y4m->src_c_dec_h = _y4m->dst_c_dec_h = 2;
    _y4m->src_c_dec_v = 1;
    _y4m->dst_c_dec_v = 2;
    _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
898
    /*Chroma filter required: read into the aux buf first.*/
clang-format's avatar
clang-format committed
899 900
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz =
        2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
John Koleszar's avatar
John Koleszar committed
901 902
    _y4m->convert = y4m_convert_422jpeg_420jpeg;
  } else if (strcmp(_y4m->chroma_type, "422") == 0) {
903
    _y4m->src_c_dec_h = 2;
John Koleszar's avatar
John Koleszar committed
904
    _y4m->src_c_dec_v = 1;
905 906 907 908 909 910 911 912
    if (only_420) {
      _y4m->dst_c_dec_h = 2;
      _y4m->dst_c_dec_v = 2;
      _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
      /*Chroma filter required: read into the aux buf first.
        We need to make two filter passes, so we need some extra space in the
         aux buffer.*/
      _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
clang-format's avatar
clang-format committed
913 914
      _y4m->aux_buf_sz =
          _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
915 916
      _y4m->convert = y4m_convert_422_420jpeg;
    } else {
917
      _y4m->aom_fmt = AOM_IMG_FMT_I422;
918
      _y4m->bps = 16;
919 920
      _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
      _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
clang-format's avatar
clang-format committed
921 922
      _y4m->dst_buf_read_sz =
          _y4m->pic_w * _y4m->pic_h + 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
923 924 925
      /*Natively supported: no conversion required.*/
      _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
      _y4m->convert = y4m_convert_null;
926 927 928 929
    }
  } else if (strcmp(_y4m->chroma_type, "422p10") == 0) {
    _y4m->src_c_dec_h = 2;
    _y4m->src_c_dec_v = 1;
930
    _y4m->aom_fmt = AOM_IMG_FMT_I42216;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    _y4m->bps = 20;
    _y4m->bit_depth = 10;
    _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
    _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
    _y4m->dst_buf_read_sz = 2 * (_y4m->pic_w * _y4m->pic_h +
                                 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h);
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
    if (only_420) {
      fprintf(stderr, "Unsupported conversion from 422p10 to 420jpeg\n");
      return -1;
    }
  } else if (strcmp(_y4m->chroma_type, "422p12") == 0) {
    _y4m->src_c_dec_h = 2;
    _y4m->src_c_dec_v = 1;
946
    _y4m->aom_fmt = AOM_IMG_FMT_I42216;
947 948 949 950 951 952 953 954 955 956 957 958
    _y4m->bps = 24;
    _y4m->bit_depth = 12;
    _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
    _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
    _y4m->dst_buf_read_sz = 2 * (_y4m->pic_w * _y4m->pic_h +
                                 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h);
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
    if (only_420) {
      fprintf(stderr, "Unsupported conversion from 422p12 to 420jpeg\n");
      return -1;
    }
John Koleszar's avatar
John Koleszar committed
959 960 961 962 963 964
  } else if (strcmp(_y4m->chroma_type, "411") == 0) {
    _y4m->src_c_dec_h = 4;
    _y4m->dst_c_dec_h = 2;
    _y4m->src_c_dec_v = 1;
    _y4m->dst_c_dec_v = 2;
    _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
965 966 967
    /*Chroma filter required: read into the aux buf first.
      We need to make two filter passes, so we need some extra space in the
       aux buffer.*/
John Koleszar's avatar
John Koleszar committed
968
    _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 3) / 4) * _y4m->pic_h;
clang-format's avatar
clang-format committed
969 970
    _y4m->aux_buf_sz =
        _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
John Koleszar's avatar
John Koleszar committed
971 972 973 974
    _y4m->convert = y4m_convert_411_420jpeg;
  } else if (strcmp(_y4m->chroma_type, "444") == 0) {
    _y4m->src_c_dec_h = 1;
    _y4m->src_c_dec_v = 1;
975 976 977 978 979 980 981 982
    if (only_420) {
      _y4m->dst_c_dec_h = 2;
      _y4m->dst_c_dec_v = 2;
      _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
      /*Chroma filter required: read into the aux buf first.
        We need to make two filter passes, so we need some extra space in the
         aux buffer.*/
      _y4m->aux_buf_read_sz = 2 * _y4m->pic_w * _y4m->pic_h;
clang-format's avatar
clang-format committed
983 984
      _y4m->aux_buf_sz =
          _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
985 986
      _y4m->convert = y4m_convert_444_420jpeg;
    } else {
987
      _y4m->aom_fmt = AOM_IMG_FMT_I444;
988
      _y4m->bps = 24;
989 990 991 992 993 994 995
      _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
      _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
      _y4m->dst_buf_read_sz = 3 * _y4m->pic_w * _y4m->pic_h;
      /*Natively supported: no conversion required.*/
      _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
      _y4m->convert = y4m_convert_null;
    }
996 997 998
  } else if (strcmp(_y4m->chroma_type, "444p10") == 0) {
    _y4m->src_c_dec_h = 1;
    _y4m->src_c_dec_v = 1;
999
    _y4m->aom_fmt = AOM_IMG_FMT_I44416;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    _y4m->bps = 30;
    _y4m->bit_depth = 10;
    _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
    _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
    _y4m->dst_buf_read_sz = 2 * 3 * _y4m->pic_w * _y4m->pic_h;
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
    if (only_420) {
      fprintf(stderr, "Unsupported conversion from 444p10 to 420jpeg\n");
      return -1;
    }
  } else if (strcmp(_y4m->chroma_type, "444p12") == 0) {
    _y4m->src_c_dec_h = 1;
    _y4m->src_c_dec_v = 1;
1014
    _y4m->aom_fmt = AOM_IMG_FMT_I44416;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    _y4m->bps = 36;
    _y4m->bit_depth = 12;
    _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
    _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
    _y4m->dst_buf_read_sz = 2 * 3 * _y4m->pic_w * _y4m->pic_h;
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
    if (only_420) {
      fprintf(stderr, "Unsupported conversion from 444p12 to 420jpeg\n");
      return -1;
    }
John Koleszar's avatar
John Koleszar committed
1026 1027 1028
  } else if (strcmp(_y4m->chroma_type, "444alpha") == 0) {
    _y4m->src_c_dec_h = 1;
    _y4m->src_c_dec_v = 1;
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    if (only_420) {
      _y4m->dst_c_dec_h = 2;
      _y4m->dst_c_dec_v = 2;
      _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
      /*Chroma filter required: read into the aux buf first.
        We need to make two filter passes, so we need some extra space in the
         aux buffer.
        The extra plane also gets read into the aux buf.
        It will be discarded.*/
      _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 3 * _y4m->pic_w * _y4m->pic_h;
      _y4m->convert = y4m_convert_444_420jpeg;
    } else {
1041
      _y4m->aom_fmt = AOM_IMG_FMT_444A;
1042
      _y4m->bps = 32;
1043 1044 1045 1046 1047 1048 1049
      _y4m->dst_c_dec_h = _y4m->src_c_dec_h;
      _y4m->dst_c_dec_v = _y4m->src_c_dec_v;
      _y4m->dst_buf_read_sz = 4 * _y4m->pic_w * _y4m->pic_h;
      /*Natively supported: no conversion required.*/
      _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
      _y4m->convert = y4m_convert_null;
    }
John Koleszar's avatar
John Koleszar committed
1050 1051 1052 1053
  } else if (strcmp(_y4m->chroma_type, "mono") == 0) {
    _y4m->src_c_dec_h = _y4m->src_c_dec_v = 0;
    _y4m->dst_c_dec_h = _y4m->dst_c_dec_v = 2;
    _y4m->dst_buf_read_sz = _y4m->pic_w * _y4m->pic_h;
1054
    /*No extra space required, but we need to clear the chroma planes.*/
John Koleszar's avatar
John Koleszar committed
1055 1056 1057 1058
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_mono_420jpeg;
  } else {
    fprintf(stderr, "Unknown chroma sampling type: %s\n", _y4m->chroma_type);
1059 1060 1061 1062
    return -1;
  }
  /*The size of the final frame buffers is always computed from the
     destination chroma decimation type.*/
clang-format's avatar
clang-format committed
1063 1064 1065 1066
  _y4m->dst_buf_sz =
      _y4m->pic_w * _y4m->pic_h +
      2 * ((_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h) *
          ((_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v);
1067
  if (_y4m->bit_depth == 8)
1068
    _y4m->dst_buf = (unsigned char *)malloc(_y4m->dst_buf_sz);
1069
  else
1070
    _y4m->dst_buf = (unsigned char *)malloc(2 * _y4m->dst_buf_sz);
1071 1072 1073

  if (_y4m->aux_buf_sz > 0)
    _y4m->aux_buf = (unsigned char *)malloc(_y4m->aux_buf_sz);
1074 1075 1076
  return 0;
}

John Koleszar's avatar
John Koleszar committed
1077
void y4m_input_close(y4m_input *_y4m) {
1078 1079 1080 1081
  free(_y4m->dst_buf);
  free(_y4m->aux_buf);
}

1082
int y4m_input_fetch_frame(y4m_input *_y4m, FILE *_fin, aom_image_t *_img) {
1083
  char frame[6];
clang-format's avatar
clang-format committed
1084 1085 1086 1087 1088
  int pic_sz;
  int c_w;
  int c_h;
  int c_sz;
  int bytes_per_sample = _y4m->bit_depth > 8 ? 2 : 1;
1089
  /*Read and skip the frame header.*/
1090
  if (!file_read(frame, 6, _fin)) return 0;
John Koleszar's avatar
John Koleszar committed
1091 1092
  if (memcmp(frame, "FRAME", 5)) {
    fprintf(stderr, "Loss of framing in Y4M input data\n");
1093 1094
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
1095
  if (frame[5] != '\n') {
1096
    char c;
clang-format's avatar
clang-format committed
1097 1098 1099
    int j;
    for (j = 0; j < 79 && file_read(&c, 1, _fin) && c != '\n'; j++) {
    }
John Koleszar's avatar
John Koleszar committed
1100 1101
    if (j == 79) {
      fprintf(stderr, "Error parsing Y4M frame header\n");
1102 1103 1104 1105
      return -1;
    }
  }
  /*Read the frame data that needs no conversion.*/
1106
  if (!file_read(_y4m->dst_buf, _y4m->dst_buf_read_sz, _fin)) {
John Koleszar's avatar
John Koleszar committed
1107
    fprintf(stderr, "Error reading Y4M frame data.\n");
1108 1109 1110
    return -1;
  }
  /*Read the frame data that does need conversion.*/
1111
  if (!file_read(_y4m->aux_buf, _y4m->aux_buf_read_sz, _fin)) {
John Koleszar's avatar
John Koleszar committed
1112
    fprintf(stderr, "Error reading Y4M frame data.\n");
1113 1114 1115
    return -1;
  }
  /*Now convert the just read frame.*/
John Koleszar's avatar
John Koleszar committed
1116
  (*_y4m->convert)(_y4m, _y4m->dst_buf, _y4m->aux_buf);
1117
  /*Fill in the frame buffer pointers.
1118
    We don't use aom_img_wrap() because it forces padding for odd picture
1119
     sizes, which would require a separate fread call for every row.*/
John Koleszar's avatar
John Koleszar committed
1120
  memset(_img, 0, sizeof(*_img));
1121
  /*Y4M has the planes in Y'CbCr order, which libaom calls Y, U, and V.*/
1122
  _img->fmt = _y4m->aom_fmt;
John Koleszar's avatar
John Koleszar committed
1123 1124
  _img->w = _img->d_w = _y4m->pic_w;
  _img->h = _img->d_h = _y4m->pic_h;
1125 1126
  _img->x_chroma_shift = _y4m->dst_c_dec_h >> 1;
  _img->y_chroma_shift = _y4m->dst_c_dec_v >> 1;
1127
  _img->bps = _y4m->bps;
1128

1129
  /*Set up the buffer pointers.*/
1130
  pic_sz = _y4m->pic_w * _y4m->pic_h * bytes_per_sample;
John Koleszar's avatar
John Koleszar committed
1131
  c_w = (_y4m->pic_w + _y4m->dst_c_dec_h - 1) / _y4m->dst_c_dec_h;
1132
  c_w *= bytes_per_sample;
John Koleszar's avatar
John Koleszar committed
1133 1134
  c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v;
  c_sz = c_w * c_h;
1135
  _img->stride[AOM_PLANE_Y] = _img->stride[AOM_PLANE_ALPHA] =
1136
      _y4m->pic_w * bytes_per_sample;
1137 1138 1139 1140 1141
  _img->stride[AOM_PLANE_U] = _img->stride[AOM_PLANE_V] = c_w;
  _img->planes[AOM_PLANE_Y] = _y4m->dst_buf;
  _img->planes[AOM_PLANE_U] = _y4m->dst_buf + pic_sz;
  _img->planes[AOM_PLANE_V] = _y4m->dst_buf + pic_sz + c_sz;
  _img->planes[AOM_PLANE_ALPHA] = _y4m->dst_buf + pic_sz + 2 * c_sz;
1142
  return 1;
1143
}