y4minput.c 34.6 KB
Newer Older
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
10
11
12
13
14
15
16
 *
 *  Based on code from the OggTheora software codec source code,
 *  Copyright (C) 2002-2010 The Xiph.Org Foundation and contributors.
 */
#include <stdlib.h>
#include <string.h>
#include "y4minput.h"

John Koleszar's avatar
John Koleszar committed
17
static int y4m_parse_tags(y4m_input *_y4m, char *_tags) {
18
19
20
21
22
23
24
25
  int   got_w;
  int   got_h;
  int   got_fps;
  int   got_interlace;
  int   got_par;
  int   got_chroma;
  char *p;
  char *q;
John Koleszar's avatar
John Koleszar committed
26
27
  got_w = got_h = got_fps = got_interlace = got_par = got_chroma = 0;
  for (p = _tags;; p = q) {
28
    /*Skip any leading spaces.*/
John Koleszar's avatar
John Koleszar committed
29
    while (*p == ' ')p++;
30
    /*If that's all we have, stop.*/
John Koleszar's avatar
John Koleszar committed
31
    if (p[0] == '\0')break;
32
    /*Find the end of this tag.*/
John Koleszar's avatar
John Koleszar committed
33
    for (q = p + 1; *q != '\0' && *q != ' '; q++);
34
    /*Process the tag.*/
John Koleszar's avatar
John Koleszar committed
35
36
37
38
39
40
41
42
43
44
45
46
47
    switch (p[0]) {
      case 'W': {
        if (sscanf(p + 1, "%d", &_y4m->pic_w) != 1)return -1;
        got_w = 1;
      }
      break;
      case 'H': {
        if (sscanf(p + 1, "%d", &_y4m->pic_h) != 1)return -1;
        got_h = 1;
      }
      break;
      case 'F': {
        if (sscanf(p + 1, "%d:%d", &_y4m->fps_n, &_y4m->fps_d) != 2) {
48
49
          return -1;
        }
John Koleszar's avatar
John Koleszar committed
50
51
52
53
54
55
56
57
58
59
        got_fps = 1;
      }
      break;
      case 'I': {
        _y4m->interlace = p[1];
        got_interlace = 1;
      }
      break;
      case 'A': {
        if (sscanf(p + 1, "%d:%d", &_y4m->par_n, &_y4m->par_d) != 2) {
60
61
          return -1;
        }
John Koleszar's avatar
John Koleszar committed
62
63
64
65
66
67
68
69
70
71
        got_par = 1;
      }
      break;
      case 'C': {
        if (q - p > 16)return -1;
        memcpy(_y4m->chroma_type, p + 1, q - p - 1);
        _y4m->chroma_type[q - p - 1] = '\0';
        got_chroma = 1;
      }
      break;
72
73
74
      /*Ignore unknown tags.*/
    }
  }
John Koleszar's avatar
John Koleszar committed
75
76
77
  if (!got_w || !got_h || !got_fps)return -1;
  if (!got_interlace)_y4m->interlace = '?';
  if (!got_par)_y4m->par_n = _y4m->par_d = 0;
78
79
  /*Chroma-type is not specified in older files, e.g., those generated by
     mplayer.*/
John Koleszar's avatar
John Koleszar committed
80
  if (!got_chroma)strcpy(_y4m->chroma_type, "420");
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  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.*/

#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)))

/*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,
John Koleszar's avatar
John Koleszar committed
154
                                        const unsigned char *_src, int _c_w, int _c_h) {
155
156
  int y;
  int x;
John Koleszar's avatar
John Koleszar committed
157
  for (y = 0; y < _c_h; y++) {
158
159
    /*Filter: [4 -17 114 35 -9 1]/128, derived from a 6-tap Lanczos
       window.*/
John Koleszar's avatar
John Koleszar committed
160
161
162
163
    for (x = 0; x < OC_MINI(_c_w, 2); x++) {
      _dst[x] = (unsigned char)OC_CLAMPI(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, 255);
164
    }
John Koleszar's avatar
John Koleszar committed
165
166
167
    for (; x < _c_w - 3; x++) {
      _dst[x] = (unsigned char)OC_CLAMPI(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, 255);
168
    }
John Koleszar's avatar
John Koleszar committed
169
170
171
172
    for (; x < _c_w; x++) {
      _dst[x] = (unsigned char)OC_CLAMPI(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, 255);
173
    }
John Koleszar's avatar
John Koleszar committed
174
175
    _dst += _c_w;
    _src += _c_w;
176
177
178
179
  }
}

/*Handles both 422 and 420mpeg2 to 422jpeg and 420jpeg, respectively.*/
John Koleszar's avatar
John Koleszar committed
180
181
static void y4m_convert_42xmpeg2_42xjpeg(y4m_input *_y4m, unsigned char *_dst,
                                         unsigned char *_aux) {
182
183
184
185
186
  int c_w;
  int c_h;
  int c_sz;
  int pli;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
187
  _dst += _y4m->pic_w * _y4m->pic_h;
188
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
189
190
191
192
193
194
195
  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;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  }
}

/*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
242
243
static void y4m_convert_42xpaldv_42xjpeg(y4m_input *_y4m, unsigned char *_dst,
                                         unsigned char *_aux) {
244
245
246
247
248
249
250
251
  unsigned char *tmp;
  int            c_w;
  int            c_h;
  int            c_sz;
  int            pli;
  int            y;
  int            x;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
252
  _dst += _y4m->pic_w * _y4m->pic_h;
253
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
254
255
256
257
258
  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++) {
259
260
261
    /*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
262
263
264
265
    y4m_42xmpeg2_42xjpeg_helper(tmp, _aux, c_w, c_h);
    _aux += c_sz;
    switch (pli) {
      case 1: {
266
267
        /*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
268
269
270
271
272
273
        for (x = 0; x < c_w; x++) {
          for (y = 0; y < OC_MINI(c_h, 3); y++) {
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(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, 255);
274
          }
John Koleszar's avatar
John Koleszar committed
275
276
277
278
          for (; y < c_h - 2; y++) {
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(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, 255);
279
          }
John Koleszar's avatar
John Koleszar committed
280
281
282
283
          for (; y < c_h; y++) {
            _dst[y * c_w] = (unsigned char)OC_CLAMPI(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, 255);
284
285
286
287
          }
          _dst++;
          tmp++;
        }
John Koleszar's avatar
John Koleszar committed
288
289
290
291
292
        _dst += c_sz - c_w;
        tmp -= c_w;
      }
      break;
      case 2: {
293
294
        /*Slide C_r down a quarter-pel.
          This is the same as the horizontal filter.*/
John Koleszar's avatar
John Koleszar committed
295
296
297
298
299
300
        for (x = 0; x < c_w; x++) {
          for (y = 0; y < OC_MINI(c_h, 2); y++) {
            _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);
301
          }
John Koleszar's avatar
John Koleszar committed
302
303
304
305
          for (; y < c_h - 3; y++) {
            _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[(y + 1) * c_w]
                                                         - 9 * tmp[(y + 2) * c_w] + tmp[(y + 3) * c_w] + 64) >> 7, 255);
306
          }
John Koleszar's avatar
John Koleszar committed
307
308
309
310
          for (; y < c_h; y++) {
            _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);
311
312
313
314
          }
          _dst++;
          tmp++;
        }
John Koleszar's avatar
John Koleszar committed
315
316
      }
      break;
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    }
    /*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,
John Koleszar's avatar
John Koleszar committed
331
                                       const unsigned char *_src, int _c_w, int _c_h) {
332
333
334
  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
335
336
337
338
339
340
  for (x = 0; x < _c_w; x++) {
    for (y = 0; y < OC_MINI(_c_h, 2); y += 2) {
      _dst[(y >> 1)*_c_w] = 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, 255);
341
    }
John Koleszar's avatar
John Koleszar committed
342
343
344
345
    for (; y < _c_h - 3; y += 2) {
      _dst[(y >> 1)*_c_w] = 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, 255);
346
    }
John Koleszar's avatar
John Koleszar committed
347
348
349
350
351
    for (; y < _c_h; y += 2) {
      _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);
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
    }
    _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
396
397
static void y4m_convert_422jpeg_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                        unsigned char *_aux) {
398
399
400
401
402
403
404
405
  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
406
  _dst += _y4m->pic_w * _y4m->pic_h;
407
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
408
409
410
411
412
413
414
415
416
417
  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;
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
  }
}

/*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
461
462
static void y4m_convert_422_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                    unsigned char *_aux) {
463
464
465
466
467
468
469
470
  unsigned char *tmp;
  int            c_w;
  int            c_h;
  int            c_sz;
  int            dst_c_h;
  int            dst_c_sz;
  int            pli;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
471
  _dst += _y4m->pic_w * _y4m->pic_h;
472
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
473
474
475
476
477
478
479
  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++) {
480
481
482
483
    /*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
484
    y4m_42xmpeg2_42xjpeg_helper(tmp, _aux, c_w, c_h);
485
    /*Now do the vertical filtering.*/
John Koleszar's avatar
John Koleszar committed
486
487
488
    y4m_422jpeg_420jpeg_helper(_dst, tmp, c_w, c_h);
    _aux += c_sz;
    _dst += dst_c_sz;
489
490
491
492
493
494
495
496
497
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
  }
}

/*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
533
534
static void y4m_convert_411_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                    unsigned char *_aux) {
535
536
537
538
539
540
541
542
543
544
545
546
  unsigned char *tmp;
  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;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
547
  _dst += _y4m->pic_w * _y4m->pic_h;
548
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
549
550
551
552
553
554
555
556
557
  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++) {
558
559
560
561
    /*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
562
    for (y = 0; y < c_h; y++) {
563
564
      /*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
565
566
567
568
569
      for (x = 0; x < OC_MINI(c_w, 1); x++) {
        tmp[x << 1] = (unsigned char)OC_CLAMPI(0, (111 * _aux[0]
                                                   + 18 * _aux[OC_MINI(1, c_w - 1)] - _aux[OC_MINI(2, c_w - 1)] + 64) >> 7, 255);
        tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI(0, (47 * _aux[0]
                                                       + 86 * _aux[OC_MINI(1, c_w - 1)] - 5 * _aux[OC_MINI(2, c_w - 1)] + 64) >> 7, 255);
570
      }
John Koleszar's avatar
John Koleszar committed
571
572
573
574
575
      for (; x < c_w - 2; x++) {
        tmp[x << 1] = (unsigned char)OC_CLAMPI(0, (_aux[x - 1] + 110 * _aux[x]
                                                   + 18 * _aux[x + 1] - _aux[x + 2] + 64) >> 7, 255);
        tmp[x << 1 | 1] = (unsigned char)OC_CLAMPI(0, (-3 * _aux[x - 1] + 50 * _aux[x]
                                                       + 86 * _aux[x + 1] - 5 * _aux[x + 2] + 64) >> 7, 255);
576
      }
John Koleszar's avatar
John Koleszar committed
577
578
579
580
581
582
      for (; x < c_w; x++) {
        tmp[x << 1] = (unsigned char)OC_CLAMPI(0, (_aux[x - 1] + 110 * _aux[x]
                                                   + 18 * _aux[OC_MINI(x + 1, c_w - 1)] - _aux[c_w - 1] + 64) >> 7, 255);
        if ((x << 1 | 1) < dst_c_w) {
          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);
583
584
        }
      }
John Koleszar's avatar
John Koleszar committed
585
586
      tmp += dst_c_w;
      _aux += c_w;
587
    }
John Koleszar's avatar
John Koleszar committed
588
    tmp -= tmp_sz;
589
    /*Now do the vertical filtering.*/
John Koleszar's avatar
John Koleszar committed
590
591
    y4m_422jpeg_420jpeg_helper(_dst, tmp, dst_c_w, c_h);
    _dst += dst_c_sz;
592
593
594
595
  }
}

/*Convert 444 to 420jpeg.*/
John Koleszar's avatar
John Koleszar committed
596
597
static void y4m_convert_444_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                    unsigned char *_aux) {
598
599
600
601
602
603
604
605
606
607
608
609
  unsigned char *tmp;
  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;
  /*Skip past the luma data.*/
John Koleszar's avatar
John Koleszar committed
610
  _dst += _y4m->pic_w * _y4m->pic_h;
611
  /*Compute the size of each chroma plane.*/
John Koleszar's avatar
John Koleszar committed
612
613
614
615
616
617
618
619
620
  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++) {
621
    /*Filter: [3 -17 78 78 -17 3]/128, derived from a 6-tap Lanczos window.*/
John Koleszar's avatar
John Koleszar committed
622
623
624
625
626
    for (y = 0; y < c_h; y++) {
      for (x = 0; x < OC_MINI(c_w, 2); x += 2) {
        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);
627
      }
John Koleszar's avatar
John Koleszar committed
628
629
630
      for (; x < c_w - 3; x += 2) {
        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, 255);
631
      }
John Koleszar's avatar
John Koleszar committed
632
633
634
635
      for (; x < c_w; x += 2) {
        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);
636
      }
John Koleszar's avatar
John Koleszar committed
637
638
      tmp += dst_c_w;
      _aux += c_w;
639
    }
John Koleszar's avatar
John Koleszar committed
640
    tmp -= tmp_sz;
641
    /*Now do the vertical filtering.*/
John Koleszar's avatar
John Koleszar committed
642
643
    y4m_422jpeg_420jpeg_helper(_dst, tmp, dst_c_w, c_h);
    _dst += dst_c_sz;
644
645
646
647
  }
}

/*The image is padded with empty chroma components at 4:2:0.*/
John Koleszar's avatar
John Koleszar committed
648
649
static void y4m_convert_mono_420jpeg(y4m_input *_y4m, unsigned char *_dst,
                                     unsigned char *_aux) {
650
  int c_sz;
John Koleszar's avatar
John Koleszar committed
651
652
653
654
  _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);
655
656
657
}

/*No conversion function needed.*/
John Koleszar's avatar
John Koleszar committed
658
659
static void y4m_convert_null(y4m_input *_y4m, unsigned char *_dst,
                             unsigned char *_aux) {
660
661
}

662
663
int y4m_input_open(y4m_input *_y4m, FILE *_fin, char *_skip, int _nskip,
                   int only_420) {
664
665
666
667
  char buffer[80];
  int  ret;
  int  i;
  /*Read until newline, or 80 cols, whichever happens first.*/
John Koleszar's avatar
John Koleszar committed
668
669
670
  for (i = 0; i < 79; i++) {
    if (_nskip > 0) {
      buffer[i] = *_skip++;
671
      _nskip--;
John Koleszar's avatar
John Koleszar committed
672
    } else {
John Koleszar's avatar
John Koleszar committed
673
      ret = (int)fread(buffer + i, 1, 1, _fin);
John Koleszar's avatar
John Koleszar committed
674
      if (ret < 1)return -1;
675
    }
John Koleszar's avatar
John Koleszar committed
676
    if (buffer[i] == '\n')break;
677
678
  }
  /*We skipped too much header data.*/
John Koleszar's avatar
John Koleszar committed
679
680
681
  if (_nskip > 0)return -1;
  if (i == 79) {
    fprintf(stderr, "Error parsing header; not a YUV2MPEG2 file?\n");
682
683
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
684
685
686
  buffer[i] = '\0';
  if (memcmp(buffer, "YUV4MPEG", 8)) {
    fprintf(stderr, "Incomplete magic for YUV4MPEG file.\n");
687
688
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
689
690
  if (buffer[8] != '2') {
    fprintf(stderr, "Incorrect YUV input file version; YUV4MPEG2 required.\n");
691
  }
John Koleszar's avatar
John Koleszar committed
692
693
694
  ret = y4m_parse_tags(_y4m, buffer + 5);
  if (ret < 0) {
    fprintf(stderr, "Error parsing YUV4MPEG2 header.\n");
695
696
    return ret;
  }
John Koleszar's avatar
John Koleszar committed
697
698
699
700
701
702
  if (_y4m->interlace == '?') {
    fprintf(stderr, "Warning: Input video interlacing format unknown; "
            "assuming progressive scan.\n");
  } else if (_y4m->interlace != 'p') {
    fprintf(stderr, "Input video is interlaced; "
            "Only progressive scan handled.\n");
703
704
    return -1;
  }
705
706
  _y4m->vpx_fmt = VPX_IMG_FMT_I420;
  _y4m->vpx_bps = 12;
John Koleszar's avatar
John Koleszar committed
707
708
709
710
711
  if (strcmp(_y4m->chroma_type, "420") == 0 ||
      strcmp(_y4m->chroma_type, "420jpeg") == 0) {
    _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);
712
    /*Natively supported: no conversion required.*/
John Koleszar's avatar
John Koleszar committed
713
714
715
716
717
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 0;
    _y4m->convert = y4m_convert_null;
  } else if (strcmp(_y4m->chroma_type, "420mpeg2") == 0) {
    _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;
718
    /*Chroma filter required: read into the aux buf first.*/
John Koleszar's avatar
John Koleszar committed
719
720
721
722
723
724
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz =
                         2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
    _y4m->convert = y4m_convert_42xmpeg2_42xjpeg;
  } else if (strcmp(_y4m->chroma_type, "420paldv") == 0) {
    _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;
725
726
727
    /*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
728
729
730
731
732
733
734
735
    _y4m->aux_buf_sz = 3 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
    _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 1) / 2) * ((_y4m->pic_h + 1) / 2);
    _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;
736
    /*Chroma filter required: read into the aux buf first.*/
John Koleszar's avatar
John Koleszar committed
737
738
739
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
    _y4m->convert = y4m_convert_422jpeg_420jpeg;
  } else if (strcmp(_y4m->chroma_type, "422") == 0) {
740
    _y4m->src_c_dec_h = 2;
John Koleszar's avatar
John Koleszar committed
741
    _y4m->src_c_dec_v = 1;
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
    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;
      _y4m->aux_buf_sz = _y4m->aux_buf_read_sz +
          ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
      _y4m->convert = y4m_convert_422_420jpeg;
    } else {
      _y4m->vpx_fmt = VPX_IMG_FMT_I422;
      _y4m->vpx_bps = 16;
      _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 = _y4m->pic_w * _y4m->pic_h
                              + 2 * ((_y4m->pic_w + 1) / 2) * _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
764
765
766
767
768
769
  } 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;
770
771
772
    /*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
773
774
775
776
777
778
    _y4m->aux_buf_read_sz = 2 * ((_y4m->pic_w + 3) / 4) * _y4m->pic_h;
    _y4m->aux_buf_sz = _y4m->aux_buf_read_sz + ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
    _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;
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
    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;
      _y4m->aux_buf_sz = _y4m->aux_buf_read_sz +
          ((_y4m->pic_w + 1) / 2) * _y4m->pic_h;
      _y4m->convert = y4m_convert_444_420jpeg;
    } else {
      _y4m->vpx_fmt = VPX_IMG_FMT_I444;
      _y4m->vpx_bps = 24;
      _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;
    }
John Koleszar's avatar
John Koleszar committed
800
801
802
  } else if (strcmp(_y4m->chroma_type, "444alpha") == 0) {
    _y4m->src_c_dec_h = 1;
    _y4m->src_c_dec_v = 1;
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
    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 {
      _y4m->vpx_fmt = VPX_IMG_FMT_444A;
      _y4m->vpx_bps = 32;
      _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
824
825
826
827
  } 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;
828
    /*No extra space required, but we need to clear the chroma planes.*/
John Koleszar's avatar
John Koleszar committed
829
830
831
832
    _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);
833
834
835
836
    return -1;
  }
  /*The size of the final frame buffers is always computed from the
     destination chroma decimation type.*/
John Koleszar's avatar
John Koleszar committed
837
838
839
840
841
  _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);
  _y4m->dst_buf = (unsigned char *)malloc(_y4m->dst_buf_sz);
  _y4m->aux_buf = (unsigned char *)malloc(_y4m->aux_buf_sz);
842
843
844
  return 0;
}

John Koleszar's avatar
John Koleszar committed
845
void y4m_input_close(y4m_input *_y4m) {
846
847
848
849
  free(_y4m->dst_buf);
  free(_y4m->aux_buf);
}

John Koleszar's avatar
John Koleszar committed
850
int y4m_input_fetch_frame(y4m_input *_y4m, FILE *_fin, vpx_image_t *_img) {
851
852
853
854
855
856
857
  char frame[6];
  int  pic_sz;
  int  c_w;
  int  c_h;
  int  c_sz;
  int  ret;
  /*Read and skip the frame header.*/
John Koleszar's avatar
John Koleszar committed
858
  ret = (int)fread(frame, 1, 6, _fin);
John Koleszar's avatar
John Koleszar committed
859
860
861
  if (ret < 6)return 0;
  if (memcmp(frame, "FRAME", 5)) {
    fprintf(stderr, "Loss of framing in Y4M input data\n");
862
863
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
864
  if (frame[5] != '\n') {
865
866
    char c;
    int  j;
John Koleszar's avatar
John Koleszar committed
867
868
869
    for (j = 0; j < 79 && fread(&c, 1, 1, _fin) && c != '\n'; j++);
    if (j == 79) {
      fprintf(stderr, "Error parsing Y4M frame header\n");
870
871
872
873
      return -1;
    }
  }
  /*Read the frame data that needs no conversion.*/
John Koleszar's avatar
John Koleszar committed
874
875
  if (fread(_y4m->dst_buf, 1, _y4m->dst_buf_read_sz, _fin) != _y4m->dst_buf_read_sz) {
    fprintf(stderr, "Error reading Y4M frame data.\n");
876
877
878
    return -1;
  }
  /*Read the frame data that does need conversion.*/
John Koleszar's avatar
John Koleszar committed
879
880
  if (fread(_y4m->aux_buf, 1, _y4m->aux_buf_read_sz, _fin) != _y4m->aux_buf_read_sz) {
    fprintf(stderr, "Error reading Y4M frame data.\n");
881
882
883
    return -1;
  }
  /*Now convert the just read frame.*/
John Koleszar's avatar
John Koleszar committed
884
  (*_y4m->convert)(_y4m, _y4m->dst_buf, _y4m->aux_buf);
885
886
887
  /*Fill in the frame buffer pointers.
    We don't use vpx_img_wrap() because it forces padding for odd picture
     sizes, which would require a separate fread call for every row.*/
John Koleszar's avatar
John Koleszar committed
888
  memset(_img, 0, sizeof(*_img));
889
  /*Y4M has the planes in Y'CbCr order, which libvpx calls Y, U, and V.*/
890
  _img->fmt = _y4m->vpx_fmt;
John Koleszar's avatar
John Koleszar committed
891
892
  _img->w = _img->d_w = _y4m->pic_w;
  _img->h = _img->d_h = _y4m->pic_h;
893
894
895
896
  _img->x_chroma_shift = _y4m->dst_c_dec_h >> 1;
  _img->y_chroma_shift = _y4m->dst_c_dec_v >> 1;
  _img->bps = _y4m->vpx_bps;

897
  /*Set up the buffer pointers.*/
John Koleszar's avatar
John Koleszar committed
898
899
900
901
  pic_sz = _y4m->pic_w * _y4m->pic_h;
  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;
902
  _img->stride[PLANE_Y] = _img->stride[PLANE_ALPHA] = _y4m->pic_w;
John Koleszar's avatar
John Koleszar committed
903
904
905
906
  _img->stride[PLANE_U] = _img->stride[PLANE_V] = c_w;
  _img->planes[PLANE_Y] = _y4m->dst_buf;
  _img->planes[PLANE_U] = _y4m->dst_buf + pic_sz;
  _img->planes[PLANE_V] = _y4m->dst_buf + pic_sz + c_sz;
907
  _img->planes[PLANE_ALPHA] = _y4m->dst_buf + pic_sz + 2 * c_sz;
908
  return 1;
909
}