2
0

y4minput.c 40 KB

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