block_size_analysis.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*Daala video codec
  2. Copyright (c) 2002-2013 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "../src/odintrin.h"
  24. #include "vidinput.h"
  25. #include "../src/filter.h"
  26. #include "../src/dct.h"
  27. #include "../src/pvq.h"
  28. #if defined(_WIN32)
  29. #include <io.h>
  30. #include <fcntl.h>
  31. #endif
  32. #include "getopt.h"
  33. #include "../src/block_size.h"
  34. #include "../src/block_size_enc.h"
  35. #include <math.h>
  36. static void usage(char **_argv){
  37. fprintf(stderr,"Usage: %s [options] <input> <output>\n"
  38. " <reference> and <input> may be either YUV4MPEG or Ogg Theora files.\n\n"
  39. " Options:\n"
  40. " --intra Intraframes only.\n"
  41. " --limit N Only read N frames from input.\n"
  42. " --ext N Encode the final frame N more times.\n"
  43. " --fps N Override output fps.\n"
  44. " --ref N Reference stream.\n"
  45. " --pvqk N PVQ K parameter.\n",_argv[0]);
  46. }
  47. static const char *CHROMA_TAGS[4]={" C420jpeg",""," C422jpeg"," C444"};
  48. /* Warning, this will fail for images larger than 2024 x 2024 */
  49. #define MAX_VAR_BLOCKS 1024
  50. #define SQUARE(x) ((int)(x)*(int)(x))
  51. /* Actual 2D coding gains of lapped transforms (the 32x32 one is made-up). We divide by 6 to get bits. */
  52. #define CG4 (15.943/6)
  53. #define CG8 (16.7836/6)
  54. #define CG16 (16.9986/6)
  55. #define CG32 (17.1/6)
  56. #define OFF8 (1)
  57. #define OFF16 (2)
  58. #define OFF16_8 (1)
  59. #define OFF32_8 (1)
  60. #define COUNT8 (3+2*OFF8)
  61. #define COUNT16 (7+2*OFF16)
  62. #define COUNT32 (15+2*OFF32)
  63. #define COUNT16_8 (3+2*OFF16_8)
  64. #define COUNT32_8 (7+2*OFF32_8)
  65. #define PSY_LAMBDA .65
  66. int switch_decision(unsigned char *img, int w, int h, int stride, int ow, int oh)
  67. {
  68. int i,j;
  69. int h8,w8,h32,w32;
  70. static char dec8[MAX_VAR_BLOCKS>>2][MAX_VAR_BLOCKS>>2];
  71. #if 0
  72. int h4,w4,h16,w16;
  73. static int Sx[MAX_VAR_BLOCKS][MAX_VAR_BLOCKS];
  74. static int Sxx[MAX_VAR_BLOCKS][MAX_VAR_BLOCKS];
  75. static int Sx4[MAX_VAR_BLOCKS>>1][MAX_VAR_BLOCKS>>1];
  76. static int Sxx4[MAX_VAR_BLOCKS>>1][MAX_VAR_BLOCKS>>1];
  77. static int var[MAX_VAR_BLOCKS][MAX_VAR_BLOCKS];
  78. static int var_1[MAX_VAR_BLOCKS][MAX_VAR_BLOCKS];
  79. static int var8[MAX_VAR_BLOCKS>>1][MAX_VAR_BLOCKS>>1];
  80. static int var8_1[MAX_VAR_BLOCKS>>1][MAX_VAR_BLOCKS>>1];
  81. static float dummy[MAX_VAR_BLOCKS][MAX_VAR_BLOCKS];
  82. static float dummy8[MAX_VAR_BLOCKS][MAX_VAR_BLOCKS];
  83. static float nmr4[MAX_VAR_BLOCKS>>1][MAX_VAR_BLOCKS>>1];
  84. static float nmr8[MAX_VAR_BLOCKS>>2][MAX_VAR_BLOCKS>>2];
  85. static float cg8[MAX_VAR_BLOCKS>>2][MAX_VAR_BLOCKS>>2];
  86. static float nmr16[MAX_VAR_BLOCKS>>3][MAX_VAR_BLOCKS>>3];
  87. static float cg16[MAX_VAR_BLOCKS>>3][MAX_VAR_BLOCKS>>3];
  88. static float nmr32[MAX_VAR_BLOCKS>>4][MAX_VAR_BLOCKS>>4];
  89. static float cg32[MAX_VAR_BLOCKS>>4][MAX_VAR_BLOCKS>>4];
  90. const unsigned char *x;
  91. #endif
  92. (void)ow;
  93. (void)oh;
  94. w>>=1;
  95. h>>=1;
  96. w8 = w>>2;
  97. h8 = h>>2;
  98. w32 = w>>4;
  99. h32 = h>>4;
  100. #if 0
  101. w4 = w>>1;
  102. h4 = h>>1;
  103. w16 = w>>3;
  104. h16 = h>>3;
  105. x = img;
  106. for(i=0;i<h;i++){
  107. for(j=0;j<w;j++){
  108. Sx[i][j]=x[2*j]+x[2*j+1]+x[stride+2*j]+x[stride+2*j+1];
  109. Sxx[i][j]=SQUARE(x[2*j])+SQUARE(x[2*j+1])+SQUARE(x[stride+2*j])+SQUARE(x[stride+2*j+1]);
  110. }
  111. x+=2*stride;
  112. }
  113. for(i=0;i<h4;i++){
  114. for(j=0;j<w4;j++){
  115. Sxx4[i][j] = Sxx[2*i][2*j] + Sxx[2*i][2*j+1] + Sxx[2*i+1][2*j] + Sxx[2*i+1][2*j+1];
  116. Sx4[i][j] = Sx [2*i][2*j] + Sx [2*i][2*j+1] + Sx [2*i+1][2*j] + Sx [2*i+1][2*j+1];
  117. }
  118. }
  119. for(i=0;i<h-1;i++){
  120. for(j=0;j<w-1;j++){
  121. int sum_x;
  122. int sum_xx;
  123. int var_floor;
  124. sum_x=Sx[i][j]+Sx[i][j+1]+Sx[i+1][j]+Sx[i+1][j+1];
  125. sum_xx=Sxx[i][j]+Sxx[i][j+1]+Sxx[i+1][j]+Sxx[i+1][j+1];
  126. var[i][j]=(sum_xx-(SQUARE(sum_x)>>4))>>5;
  127. var_floor = 4+(sum_x>>8);
  128. if (var[i][j]<var_floor)var[i][j]=var_floor;
  129. /*printf("%d ", var[i][j]);*/
  130. var_1[i][j] = 16384/var[i][j];
  131. }
  132. /*printf("\n");*/
  133. }
  134. for(i=0;i<h4-1;i++){
  135. for(j=0;j<w4-1;j++){
  136. int sum_x;
  137. int sum_xx;
  138. int var_floor;
  139. sum_x =Sx4 [i][j]+Sx4 [i][j+1]+Sx4 [i+1][j]+Sx4 [i+1][j+1];
  140. sum_xx=Sxx4[i][j]+Sxx4[i][j+1]+Sxx4[i+1][j]+Sxx4[i+1][j+1];
  141. var8[i][j]=(sum_xx-(SQUARE(sum_x)>>6))>>5;
  142. var_floor = 4+(sum_x>>8);
  143. if (var8[i][j]<var_floor)var8[i][j]=var_floor;
  144. /*printf("%d ", var8[i][j]);*/
  145. var8_1[i][j] = 16384/var8[i][j];
  146. }
  147. /*printf("\n");*/
  148. }
  149. for(i=1;i<h4-1;i++){
  150. for(j=1;j<w4-1;j++){
  151. int k,m;
  152. int sum_var=0;
  153. int sum_var_1=0;
  154. float psy=0;
  155. float noise;
  156. for(k=0;k<3;k++){
  157. for(m=0;m<3;m++){
  158. sum_var+=var[2*i-1+k][2*j-1+m];
  159. }
  160. }
  161. noise = sum_var/(3*3);
  162. for(k=0;k<3;k++){
  163. for(m=0;m<3;m++){
  164. psy += OD_LOG2(1+noise*var_1[2*i-1+k][2*j-1+m]/16384.);
  165. }
  166. }
  167. psy /= (3*3);
  168. psy -= 1;
  169. nmr4[i][j] = psy;
  170. /*printf("%f ", nmr4[i][j]);*/
  171. }
  172. /*printf("\n");*/
  173. }
  174. for(i=1;i<h8-1;i++){
  175. for(j=1;j<w8-1;j++){
  176. int k,m;
  177. int sum_var=0;
  178. int sum_var_1=0;
  179. float nmr4_avg;
  180. float cgl, cgs;
  181. float noise;
  182. float psy;
  183. for(k=0;k<COUNT8;k++){
  184. for(m=0;m<COUNT8;m++){
  185. sum_var +=var[4*i-OFF8+k][4*j-OFF8+m];
  186. }
  187. }
  188. noise = sum_var/(COUNT8*COUNT8);
  189. psy=0;
  190. for(k=0;k<COUNT8;k++){
  191. for(m=0;m<COUNT8;m++){
  192. psy += OD_LOG2(1+noise*var_1[4*i-OFF8+k][4*j-OFF8+m]/16384.);
  193. }
  194. }
  195. psy /= (COUNT8*COUNT8);
  196. psy -= 1;
  197. nmr8[i][j] = psy;
  198. nmr4_avg = .25f*(nmr4[2*i][2*j]+nmr4[2*i][2*j+1]+nmr4[2*i+1][2*j]+nmr4[2*i+1][2*j+1]);
  199. cgs = CG4 - PSY_LAMBDA*(nmr4_avg);
  200. cgl = CG8 - PSY_LAMBDA*(nmr8[i][j]);
  201. if (cgl>=cgs)
  202. {
  203. dec8[i][j] = 1;
  204. cg8[i][j] = CG8;
  205. } else {
  206. nmr8[i][j] = nmr4_avg;
  207. dec8[i][j] = 0;
  208. cg8[i][j] = CG4;
  209. }
  210. /*printf("%d ", dec8[i][j]);*/
  211. }
  212. /*printf("\n");*/
  213. }
  214. for(i=1;i<h16-1;i++){
  215. for(j=1;j<w16-1;j++){
  216. int k,m;
  217. int sum_var=0;
  218. int sum_var8=0;
  219. int sum_var_1=0;
  220. float nmr8_avg;
  221. float cgl,cgs;
  222. float noise;
  223. float noise8;
  224. float psy;
  225. float psy8;
  226. for(k=0;k<COUNT16;k++){
  227. for(m=0;m<COUNT16;m++){
  228. sum_var+=var[8*i-OFF16+k][8*j-OFF16+m];
  229. }
  230. }
  231. noise = sum_var/(float)(COUNT16*COUNT16);
  232. for(k=0;k<COUNT16_8;k++){
  233. for(m=0;m<COUNT16_8;m++){
  234. sum_var8+=var8[4*i-OFF16_8+k][4*j-OFF16_8+m];
  235. }
  236. }
  237. noise8 = sum_var8/(float)(COUNT16_8*COUNT16_8);
  238. psy=0;
  239. for(k=0;k<COUNT16;k++){
  240. for(m=0;m<COUNT16;m++){
  241. psy += OD_LOG2(1+noise*var_1[8*i-OFF16+k][8*j-OFF16+m]/16384.);
  242. }
  243. }
  244. psy /= (COUNT16*COUNT16);
  245. psy -= 1;
  246. psy8=0;
  247. for(k=0;k<COUNT16_8;k++){
  248. for(m=0;m<COUNT16_8;m++){
  249. psy8 += OD_LOG2(1+noise8*var8_1[4*i-OFF16_8+k][4*j-OFF16_8+m]/16384.);
  250. }
  251. }
  252. psy8 /= (COUNT16_8*COUNT16_8);
  253. psy8 -= 1;
  254. psy = OD_MAXF(psy, .25*psy8);
  255. /*psy = .5*(psy+psy8);*/
  256. nmr16[i][j] = psy;
  257. nmr8_avg = .25f*(nmr8[2*i][2*j]+nmr8[2*i][2*j+1]+nmr8[2*i+1][2*j]+nmr8[2*i+1][2*j+1]);
  258. cg16[i][j] = .25*(cg8[2*i][2*j] + cg8[2*i][2*j+1] + cg8[2*i+1][2*j] + cg8[2*i+1][2*j+1]);
  259. cgs = cg16[i][j] - PSY_LAMBDA*(nmr8_avg);
  260. cgl = CG16 - PSY_LAMBDA*(nmr16[i][j]);
  261. /*printf("%f ", psy);*/
  262. if (cgl>=cgs)
  263. {
  264. dec8[2*i][2*j] = 2;
  265. dec8[2*i][2*j+1] = 2;
  266. dec8[2*i+1][2*j] = 2;
  267. dec8[2*i+1][2*j+1] = 2;
  268. cg16[i][j] = CG16;
  269. } else {
  270. nmr16[i][j] = nmr8_avg;
  271. }
  272. }
  273. /*printf("\n");*/
  274. }
  275. #if 1
  276. for(i=1;i<h32-1;i++){
  277. for(j=1;j<w32-1;j++){
  278. int k,m;
  279. int sum_var=0;
  280. int sum_var_1=0;
  281. int sum_var8=0;
  282. float nmr16_avg;
  283. float cgl,cgs;
  284. float noise, psy;
  285. float noise8, psy8;
  286. for(k=0;k<COUNT32;k++){
  287. for(m=0;m<COUNT32;m++){
  288. sum_var +=var[16*i-OFF32+k][16*j-OFF32+m];
  289. }
  290. }
  291. noise = sum_var/(float)(COUNT32*COUNT32);
  292. for(k=0;k<COUNT32_8;k++){
  293. for(m=0;m<COUNT32_8;m++){
  294. sum_var8+=var8[8*i-OFF32_8+k][8*j-OFF32_8+m];
  295. }
  296. }
  297. noise8 = sum_var8/(float)(COUNT32_8*COUNT32_8);
  298. psy=0;
  299. for(k=0;k<COUNT32;k++){
  300. for(m=0;m<COUNT32;m++){
  301. psy += OD_LOG2(1.+noise*var_1[16*i-OFF32+k][16*j-OFF32+m]/16384.);
  302. }
  303. }
  304. psy /= (COUNT32*COUNT32);
  305. psy -= 1;
  306. psy8=0;
  307. for(k=0;k<COUNT32_8;k++){
  308. for(m=0;m<COUNT32_8;m++){
  309. psy8 += OD_LOG2(1+noise8*var8_1[8*i-OFF32_8+k][8*j-OFF32_8+m]/16384.);
  310. }
  311. }
  312. psy8 /= (COUNT32_8*COUNT32_8);
  313. psy8 -= 1;
  314. psy = OD_MAXF(psy, .25*psy8);
  315. /*psy = .5*(psy+psy8);*/
  316. /*psy += psy8;*/
  317. nmr32[i][j] = psy;
  318. nmr16_avg = .25f*(nmr16[2*i][2*j]+nmr16[2*i][2*j+1]+nmr16[2*i+1][2*j]+nmr16[2*i+1][2*j+1]);
  319. cg32[i][j] = .25*(cg16[2*i][2*j] + cg16[2*i][2*j+1] + cg16[2*i+1][2*j] + cg16[2*i+1][2*j+1]);
  320. cgs = cg32[i][j] - PSY_LAMBDA*(nmr16_avg);
  321. cgl = CG32 - PSY_LAMBDA*(nmr32[i][j]);
  322. /*printf("%f ", psy8);*/
  323. if (cgl>=cgs)
  324. {
  325. for(k=0;k<4;k++){
  326. for(m=0;m<4;m++){
  327. dec8[4*i+k][4*j+m]=3;
  328. }
  329. }
  330. cg32[i][j] = CG32;
  331. } else {
  332. nmr32[i][j] = nmr16_avg;
  333. }
  334. }
  335. /*printf("\n");*/
  336. }
  337. #endif
  338. #endif
  339. /* Replace decision with the one from process_block_size32() */
  340. if (1)
  341. {
  342. BlockSizeComp bs;
  343. for(i=1;i<h32-1;i++){
  344. for(j=1;j<w32-1;j++){
  345. int k,m;
  346. int dec[4][4];
  347. process_block_size32(&bs, img+32*stride*i+32*j, img+32*stride*i+32*j, stride, dec);
  348. for(k=0;k<4;k++)
  349. for(m=0;m<4;m++)
  350. dec8[4*i+k][4*j+m]=dec[k][m];
  351. #if 0
  352. for(k=0;k<16;k++)
  353. {
  354. for(m=0;m<16;m++)
  355. {
  356. var[16*i+k][16*j+m]=bs.img_stats.Var4[k+3][m+3];
  357. var_1[16*i+k][16*j+m]=bs.img_stats.invVar4[k+3][m+3];
  358. }
  359. }
  360. for(k=0;k<8;k++)
  361. {
  362. for(m=0;m<8;m++)
  363. {
  364. dummy[8*i+k][8*j+m]=bs.psy4[k][m];
  365. }
  366. }
  367. for(k=0;k<4;k++)
  368. {
  369. for(m=0;m<4;m++)
  370. {
  371. dummy8[4*i+k][4*j+m]=bs.psy8[k][m];
  372. }
  373. }
  374. for(k=0;k<8;k++)
  375. {
  376. for(m=0;m<8;m++)
  377. {
  378. var8[8*i+k][8*j+m]=bs.img_stats.Var8[k+2][m+2];
  379. var8_1[8*i+k][8*j+m]=bs.img_stats.invVar8[k+2][m+2];
  380. }
  381. }
  382. #endif
  383. }
  384. }
  385. if (0) {
  386. int count32[28] = {0};
  387. int count16[144] = {0};
  388. int stats32[28] = {0};
  389. int stats16[144] = {0};
  390. int stats8[144][16] = {{0}};
  391. for (i = 2; i < h32 - 2; i++) {
  392. for (j = 2; j < w32 - 2; j++) {
  393. int k;
  394. int m;
  395. char *bsize;
  396. int stride;
  397. int id32;
  398. bsize = &dec8[4*i][4*j];
  399. stride = sizeof(dec8[0])/sizeof(dec8[0][0]);
  400. id32 = od_block_size_prob32(bsize, stride);
  401. count32[id32]++;
  402. if (bsize[0] == 3) stats32[id32]++;
  403. else for (k = 0; k < 2; k++) for (m = 0; m < 2; m++) {
  404. int id16;
  405. id16 = od_block_size_cdf16_id(&bsize[2*k*stride + 2*m], stride);
  406. count16[id16]++;
  407. OD_ASSERT(bsize[2*k*stride + 2*m]<=2);
  408. stats16[id16] += bsize[2*k*stride + 2*m] == 2;
  409. if (bsize[2*k*stride + 2*m] != 2) {
  410. int id8;
  411. OD_ASSERT(bsize[2*k*stride + 2*m]<=1);
  412. OD_ASSERT(bsize[2*k*stride + 2*m + 1]<=1);
  413. OD_ASSERT(bsize[2*k*stride + stride + 2*m]<=1);
  414. OD_ASSERT(bsize[2*k*stride + stride + 2*m + 1]<=1);
  415. id8 = 8*bsize[2*k*stride + 2*m] + 4*bsize[2*k*stride + 2*m + 1]
  416. + 2*bsize[2*k*stride + stride + 2*m]
  417. + bsize[2*k*stride + stride + 2*m + 1];
  418. stats8[id16][id8]++;
  419. }
  420. }
  421. }
  422. }
  423. for (i = 0; i < 28; i++) printf("%d ", count32[i]);
  424. for (i = 0; i < 28; i++) printf("%d ", stats32[i]);
  425. for (i = 0; i < 144; i++) printf("%d ", count16[i]);
  426. for (i = 0; i < 144; i++) printf("%d ", stats16[i]);
  427. for (i = 0; i < 144; i++) for (j = 0; j < 16; j++) printf("%d ", stats8[i][j]);
  428. printf("\n");
  429. }
  430. }
  431. #if 0
  432. for(i=0;i<h;i++){
  433. for(j=0;j<w;j++){
  434. printf("%d ", var[i][j]);
  435. }
  436. printf("\n");
  437. }
  438. #endif
  439. #if 0
  440. for(i=8;i<h4-8;i++){
  441. for(j=8;j<w4-8;j++){
  442. printf("%f ", dummy[i][j]);
  443. }
  444. printf("\n");
  445. }
  446. #endif
  447. #if 0
  448. for(i=4;i<h8-4;i++){
  449. for(j=4;j<w8-4;j++){
  450. printf("%f ", dummy8[i][j]);
  451. }
  452. printf("\n");
  453. }
  454. #endif
  455. #if 0
  456. for(i=4;i<h8-4;i++){
  457. for(j=4;j<w8-4;j++){
  458. printf("%d ", dec8[i][j]);
  459. }
  460. printf("\n");
  461. }
  462. #endif
  463. #if 0
  464. fprintf(stderr, "size : %dx%d\n", (w<<1), (h<<1));
  465. for(i=0;i<(h<<1);i++){
  466. for(j=0;j<1296;j++){
  467. putc(dec8[i>>3][j>>3], stdout);
  468. }
  469. }
  470. #endif
  471. #if 0
  472. {
  473. /*Raw mode data with offsets to match the 4x8 training tool's padding.*/
  474. int wn=(ow-16)>>2;
  475. int hn=(oh-16)>>2;
  476. fprintf(stderr, "size : %dx%d\n", wn, hn);
  477. for(i=0;i<hn;i++){
  478. for(j=0;j<wn;j++){
  479. int posi=(i>>1)+1;
  480. int posj=(j>>1)+1;
  481. if(posi>=4 && posi<(h8-4) &&posj>=4 && posj<(w8-4))putc(dec8[posi][posj], stdout);
  482. else putc(0, stdout);
  483. }
  484. }
  485. }
  486. #endif
  487. #if 1
  488. for(i=4;i<h8-4;i++){
  489. for(j=4;j<w8-4;j++){
  490. if ((i&3)==0 && (j&3)==0){
  491. int k;
  492. for(k=0;k<32;k++)
  493. img[i*stride*8+j*8+k] = 0;
  494. for(k=0;k<32;k++)
  495. img[(8*i+k)*stride+j*8] = 0;
  496. }
  497. if ((i&1)==0 && (j&1)==0 && dec8[i][j]==2){
  498. int k;
  499. for(k=0;k<16;k++)
  500. img[i*stride*8+j*8+k] = 0;
  501. for(k=0;k<16;k++)
  502. img[(8*i+k)*stride+j*8] = 0;
  503. }
  504. if (dec8[i][j]<=1){
  505. int k;
  506. for(k=0;k<8;k++)
  507. img[i*stride*8+j*8+k] = 0;
  508. for(k=0;k<8;k++)
  509. img[(8*i+k)*stride+j*8] = 0;
  510. if (dec8[i][j]==0){
  511. img[(8*i+4)*stride+j*8+3] = 0;
  512. img[(8*i+4)*stride+j*8+4] = 0;
  513. img[(8*i+4)*stride+j*8+5] = 0;
  514. img[(8*i+3)*stride+j*8+4] = 0;
  515. img[(8*i+5)*stride+j*8+4] = 0;
  516. }
  517. }
  518. }
  519. }
  520. for (i=32;i<(w32-1)*32;i++)
  521. img[(h32-1)*32*stride+i]=0;
  522. for (i=32;i<(h32-1)*32;i++)
  523. img[i*stride+(w32-1)*32]=0;
  524. #endif
  525. #if 0 /* 32x32 decision data */
  526. for(i=2;i<h32-2;i++){
  527. for(j=2;j<w32-2;j++){
  528. int i8, j8;
  529. int k;
  530. i8 = 4*i-1;
  531. j8 = 4*j-1;
  532. for(k=0;k<5;k++)
  533. printf("%d ", dec8[i8+k][j8]);
  534. for(k=1;k<5;k++)
  535. printf("%d ", dec8[i8][j8+k]);
  536. printf("%d\n", dec8[i8+1][j8+1]);
  537. }
  538. }
  539. #endif
  540. #if 0 /* 16x16 decision data */
  541. for(i=4;i<h16-4;i++){
  542. for(j=4;j<w16-4;j++){
  543. int i8, j8;
  544. int k;
  545. i8 = 2*i-1;
  546. j8 = 2*j-1;
  547. if (dec8[i8+1][j8+1]==3)
  548. continue;
  549. if (1)
  550. {
  551. int sum=0;
  552. /*for(k=0;k<3;k++)
  553. printf("%d ", dec8[i8+k][j8]);
  554. for(k=1;k<3;k++)
  555. printf("%d ", dec8[i8][j8+k]);*/
  556. for(k=0;k<3;k++)
  557. sum += dec8[i8+k][j8];
  558. for(k=1;k<3;k++)
  559. sum += dec8[i8][j8+k];
  560. printf("%d ", sum);
  561. } else {
  562. int up, left;
  563. up = (dec8[i8][j8+1]>=2) ? 2+dec8[i8][j8+1] : dec8[i8][j8+1]*2+dec8[i8][j8+2];
  564. left = (dec8[i8+1][j8]>=2) ? 2+dec8[i8+1][j8] : dec8[i8+1][j8]*2+dec8[i8+2][j8];
  565. printf("%d ", dec8[i8][j8]*36+up*6+left);
  566. }
  567. if (dec8[i8+1][j8+1]==2)
  568. printf("16\n");
  569. else
  570. printf("%d\n", 8*dec8[i8+1][j8+1]+4*dec8[i8+1][j8+2]+2*dec8[i8+2][j8+1]+dec8[i8+2][j8+2]);
  571. }
  572. printf("\n");
  573. }
  574. #endif
  575. #if 0 /* 8x8 decision data */
  576. for(i=8;i<h8-8;i++){
  577. for(j=8;j<w8-8;j++){
  578. if (dec8[i][j]<=1)
  579. {
  580. printf("%d %d %d %d\n", dec8[i-1][j-1], dec8[i-1][j], dec8[i][j-1], dec8[i][j]);
  581. }
  582. }
  583. }
  584. #endif
  585. return 0;
  586. }
  587. /*Applies vert then horiz prefilters of size _n.*/
  588. void prefilter_image(od_coeff *_img, int _w, int _h, int _n){
  589. int x,y,j;
  590. /*Pre-filter*/
  591. switch(_n){
  592. case 4:
  593. for(y=0;y<_h;y++){
  594. for(x=2;x<_w-2;x+=4){
  595. od_pre_filter4(&_img[y*_w+x],&_img[y*_w+x]);
  596. }
  597. }
  598. for(y=2;y<_h-2;y+=4){
  599. for(x=0;x<_w;x++){
  600. od_coeff tmp[4];
  601. for(j=0;j<4;j++)tmp[j]=_img[(y+j)*_w+x];
  602. od_pre_filter4(tmp,tmp);
  603. for(j=0;j<4;j++)_img[(y+j)*_w+x]=tmp[j];
  604. }
  605. }
  606. break;
  607. case 8:
  608. for(y=0;y<_h;y++){
  609. for(x=4;x<_w-4;x+=8){
  610. od_pre_filter8(&_img[y*_w+x],&_img[y*_w+x]);
  611. }
  612. }
  613. for(y=4;y<_h-4;y+=8){
  614. for(x=0;x<_w;x++){
  615. od_coeff tmp[16];
  616. for(j=0;j<8;j++)tmp[j]=_img[(y+j)*_w+x];
  617. od_pre_filter8(tmp,tmp);
  618. for(j=0;j<8;j++)_img[(y+j)*_w+x]=tmp[j];
  619. }
  620. }
  621. break;
  622. case 16:
  623. for(y=0;y<_h;y++){
  624. for(x=8;x<_w-8;x+=16){
  625. od_pre_filter16(&_img[y*_w+x],&_img[y*_w+x]);
  626. }
  627. }
  628. for(y=8;y<_h-8;y+=16){
  629. for(x=0;x<_w;x++){
  630. od_coeff tmp[16];
  631. for(j=0;j<16;j++)tmp[j]=_img[(y+j)*_w+x];
  632. od_pre_filter16(tmp,tmp);
  633. for(j=0;j<16;j++)_img[(y+j)*_w+x]=tmp[j];
  634. }
  635. }
  636. break;
  637. default:
  638. break;
  639. }
  640. }
  641. /*Applies horiz then vert postfilters of size _n.*/
  642. void postfilter_image(od_coeff *_img, int _w, int _h, int _n){
  643. int x,y,j;
  644. /*Pre-filter*/
  645. switch(_n){
  646. case 4:
  647. for(y=2;y<_h-2;y+=4){
  648. for(x=0;x<_w;x++){
  649. od_coeff tmp[4];
  650. for(j=0;j<4;j++)tmp[j]=_img[(y+j)*_w+x];
  651. od_post_filter4(tmp,tmp);
  652. for(j=0;j<4;j++)_img[(y+j)*_w+x]=tmp[j];
  653. }
  654. }
  655. for(y=0;y<_h;y++){
  656. for(x=2;x<_w-2;x+=4){
  657. od_post_filter4(&_img[y*_w+x],&_img[y*_w+x]);
  658. }
  659. }
  660. break;
  661. case 8:
  662. for(y=4;y<_h-4;y+=8){
  663. for(x=0;x<_w;x++){
  664. od_coeff tmp[16];
  665. for(j=0;j<8;j++)tmp[j]=_img[(y+j)*_w+x];
  666. od_post_filter8(tmp,tmp);
  667. for(j=0;j<8;j++)_img[(y+j)*_w+x]=tmp[j];
  668. }
  669. }
  670. for(y=0;y<_h;y++){
  671. for(x=4;x<_w-4;x+=8){
  672. od_post_filter8(&_img[y*_w+x],&_img[y*_w+x]);
  673. }
  674. }
  675. break;
  676. case 16:
  677. for(y=8;y<_h-8;y+=16){
  678. for(x=0;x<_w;x++){
  679. od_coeff tmp[16];
  680. for(j=0;j<16;j++)tmp[j]=_img[(y+j)*_w+x];
  681. od_post_filter16(tmp,tmp);
  682. for(j=0;j<16;j++)_img[(y+j)*_w+x]=tmp[j];
  683. }
  684. }
  685. for(y=0;y<_h;y++){
  686. for(x=8;x<_w-8;x+=16){
  687. od_post_filter16(&_img[y*_w+x],&_img[y*_w+x]);
  688. }
  689. }
  690. break;
  691. default:
  692. break;
  693. }
  694. }
  695. /*static const int fzig16[256] = {0,16,1,2,17,32,48,33,18,3,4,19,34,49,64,80,65,50,35,20,5,6,21,36,51,66,81,96,112,97,82,67,52,37,22,7,8,23,38,53,68,83,98,113,128,144,129,114,99,84,69,54,39,24,9,10,25,40,55,70,85,100,115,130,145,160,176,161,146,131,116,101,86,71,56,41,26,11,12,27,42,57,72,87,102,117,132,147,162,177,192,208,193,178,163,148,133,118,103,88,73,58,43,28,13,14,29,44,59,74,89,104,119,134,149,164,179,194,209,224,240,225,210,195,180,165,150,135,120,105,90,75,60,45,30,15,31,46,61,76,91,106,121,136,151,166,181,196,211,226,241,242,227,212,197,182,167,152,137,122,107,92,77,62,47,63,78,93,108,123,138,153,168,183,198,213,228,243,244,229,214,199,184,169,154,139,124,109,94,79,95,110,125,140,155,170,185,200,215,230,245,246,231,216,201,186,171,156,141,126,111,127,142,157,172,187,202,217,232,247,248,233,218,203,188,173,158,143,159,174,189,204,219,234,249,250,235,220,205,190,175,191,206,221,236,251,252,237,222,207,223,238,253,254,239,255};
  696. static const int izig16[256] = {0,2,3,9,10,20,21,35,36,54,55,77,78,104,105,135,1,4,8,11,19,22,34,37,53,56,76,79,103,106,134,136,5,7,12,18,23,33,38,52,57,75,80,102,107,133,137,164,6,13,17,24,32,39,51,58,74,81,101,108,132,138,163,165,14,16,25,31,40,50,59,73,82,100,109,131,139,162,166,189,15,26,30,41,49,60,72,83,99,110,130,140,161,167,188,190,27,29,42,48,61,71,84,98,111,129,141,160,168,187,191,210,28,43,47,62,70,85,97,112,128,142,159,169,186,192,209,211,44,46,63,69,86,96,113,127,143,158,170,185,193,208,212,227,45,64,68,87,95,114,126,144,157,171,184,194,207,213,226,228,65,67,88,94,115,125,145,156,172,183,195,206,214,225,229,240,66,89,93,116,124,146,155,173,182,196,205,215,224,230,239,241,90,92,117,123,147,154,174,181,197,204,216,223,231,238,242,249,91,118,122,148,153,175,180,198,203,217,222,232,237,243,248,250,119,121,149,152,176,179,199,202,218,221,233,236,244,247,251,254,120,150,151,177,178,200,201,219,220,234,235,245,246,252,253,255};
  697. */
  698. #define ROUNDUP_32(x) (((x)+31)&~31)
  699. #define MAXB 16
  700. #define SQUARE(x) ((int)(x)*(int)(x))
  701. int oc_ilog32(unsigned _v){
  702. int ret;
  703. static const unsigned char OC_DEBRUIJN_IDX32[32]={
  704. 0, 1,28, 2,29,14,24, 3,30,22,20,15,25,17, 4, 8,
  705. 31,27,13,23,21,19,16, 7,26,12,18, 6,11, 5,10, 9};
  706. _v|=_v>>1;
  707. _v|=_v>>2;
  708. _v|=_v>>4;
  709. _v|=_v>>8;
  710. _v|=_v>>16;
  711. ret=_v&1;
  712. _v=(_v>>1)+1;
  713. ret+=OC_DEBRUIJN_IDX32[_v*0x77CB531U>>27&0x1F];
  714. return ret;
  715. }
  716. void quant_scalar_gain(ogg_int32_t *_x,ogg_int16_t *_scale,int *y,int N,int Q){
  717. float gain0, gain1;
  718. float Q_1;
  719. int i;
  720. (void)_scale;
  721. Q*=15;
  722. Q_1 = 1.f/Q;
  723. gain0=0;
  724. gain1=0;
  725. for (i=0;i<N;i++)
  726. {
  727. int qx;
  728. float s = _x[i]*Q_1;
  729. float bias = s>0?-.49:.49;
  730. gain0 += s*s;
  731. qx = (int)floor(.5+s+bias);
  732. y[i] = qx;
  733. gain1 += qx*qx;
  734. _x[i] = Q*qx;
  735. }
  736. gain0 = sqrt(gain0/(1e-15+gain1));
  737. for (i=0;i<N;i++)
  738. _x[i] *= gain0;
  739. }
  740. static void process_plane(od_coeff *_img, od_coeff *_refi, int _w, int _h, int _pli, int _pvq_k){
  741. int x;
  742. int y;
  743. int j;
  744. int i;
  745. int free_ref;
  746. static int count=0;
  747. (void)_pvq_k;
  748. _w = ROUNDUP_32(_w);
  749. _h = ROUNDUP_32(_h);
  750. if(!_refi){
  751. _refi=calloc(ROUNDUP_32(_w)*ROUNDUP_32(_h),sizeof(od_coeff));
  752. free_ref=1;
  753. }else free_ref=0;
  754. prefilter_image(_img,_w,_h,16);
  755. /*for (i=0;i<1000000;i++){
  756. int tmp[16];
  757. for(j=0;j<16;j++)
  758. tmp[j] = rand()%255-127;
  759. od_bin_idct16(tmp, tmp);
  760. for(j=0;j<16;j++)printf("%d ", tmp[j]);
  761. printf("\n");
  762. }
  763. exit(0);*/
  764. /*Block processing.*/
  765. for(y=0;y<_h;y+=16){
  766. for(x=0;x<_w;x+=16){
  767. od_coeff coeffs[256];
  768. ogg_int32_t zi[256];
  769. /* int out[256];*/
  770. /* ogg_int16_t scale[256];*/
  771. /* for(j=0;j<256;j++)scale[j]=1;*/
  772. od_bin_fdct16x16(coeffs,16,&_img[(y)*_w+x],_w);
  773. for(i=0;i<16;i++){
  774. for(j=0;j<16;j++){
  775. zi[16*i+j]=floor(.5+coeffs[16*i+j]);
  776. }
  777. }
  778. if (_pli==-1){
  779. #if 0
  780. int foo[256];
  781. ogg_int32_t x[256];
  782. /*quant_scalar_gain(&zi[1],NULL,foo,255,200);*/
  783. extract(&zi[4], x, 2, 4, 16);
  784. quant_scalar_gain(x,NULL,foo,8,200);
  785. interleave(x, &zi[4], 2, 4, 16);
  786. extract(&zi[64], x, 4, 2, 16);
  787. quant_scalar_gain(x,NULL,foo,8,200);
  788. interleave(x, &zi[64], 4, 2, 16);
  789. extract(&zi[64+2], x, 4, 6, 16);
  790. extract(&zi[32+4], x+24, 2, 4, 16);
  791. quant_scalar_gain(x,NULL,foo,32,600);
  792. interleave(x, &zi[64+2], 4, 6, 16);
  793. interleave(x+24, &zi[32+4], 2, 4, 16);
  794. #endif
  795. #if 0
  796. extract(&zi[8], x, 4, 8, 16);
  797. /*quant_pvq(x, r, scale, out, 32, 1200./f, &qg);*/
  798. quant_scalar_gain(x,NULL,foo,32,600);
  799. interleave(x, &zi[8], 4, 8, 16);
  800. extract(&zi[128], x, 8, 4, 16);
  801. /*quant_pvq(x, r, scale, out, 32, 1200./f, &qg);*/
  802. quant_scalar_gain(x,NULL,foo,32,600);
  803. interleave(x, &zi[128], 8, 4, 16);
  804. extract(&zi[128+4], x, 8, 12, 16);
  805. extract(&zi[64+8], x+96, 4, 8, 16);
  806. /*quant_pvq(x, r, scale, out, 128, 1200./f, &qg);*/
  807. quant_scalar_gain(x,NULL,foo,128,1200);
  808. interleave(x, &zi[128+4], 8, 12, 16);
  809. interleave(x+96, &zi[64+8], 4, 8, 16);
  810. #endif
  811. }
  812. /*for(j=0;j<256;j++)coeffs[j]=zi[j];*/
  813. for(i=0;i<16;i++){
  814. for(j=0;j<16;j++){
  815. coeffs[16*i+j]=floor(.5+zi[16*i+j]);
  816. }
  817. }
  818. od_bin_idct16x16(&_img[(y)*_w+x],_w,coeffs,16);
  819. }
  820. /*printf("\n");*/
  821. }
  822. postfilter_image(_img,_w,_h,16);
  823. if(free_ref)free(_refi);
  824. count++;
  825. }
  826. int main(int _argc,char **_argv){
  827. const char *optstring = "hv?";
  828. const struct option long_options[]={
  829. {"ref",required_argument,NULL,0},
  830. {"limit",required_argument,NULL,0},
  831. {"fps",required_argument,NULL,0},
  832. {"ext",required_argument,NULL,0},
  833. {"pvqk",required_argument,NULL,0},
  834. {"intra",no_argument,NULL,0},
  835. {NULL,0,NULL,0}
  836. };
  837. FILE *fin;
  838. FILE *fout;
  839. video_input vid1;
  840. th_info ti1;
  841. video_input vid2;
  842. th_info ti2;
  843. int frameno;
  844. int pli;
  845. char *outline;
  846. od_coeff *refi[3];
  847. od_coeff *iimg[3];
  848. int xdec[3];
  849. int ydec[3];
  850. int w[3];
  851. int h[3];
  852. int pvq_k;
  853. int fps;
  854. int extend;
  855. int limit;
  856. int intra;
  857. char refname[1024];
  858. int ref_in;
  859. int long_option_index;
  860. int c;
  861. pvq_k=32;
  862. fps=-1;
  863. ref_in=0;
  864. limit=0;
  865. extend=0;
  866. intra=0;
  867. while((c=getopt_long(_argc,_argv,optstring,long_options,&long_option_index))!=EOF){
  868. switch(c){
  869. case 0:
  870. if(strcmp(long_options[long_option_index].name,"ref")==0){
  871. ref_in=1;
  872. strncpy(refname,optarg,1023);
  873. } else if (strcmp(long_options[long_option_index].name,"pvqk")==0){
  874. pvq_k=atoi(optarg);
  875. } else if (strcmp(long_options[long_option_index].name,"limit")==0){
  876. limit=atoi(optarg);
  877. } else if (strcmp(long_options[long_option_index].name,"ext")==0){
  878. extend=atoi(optarg);
  879. } else if (strcmp(long_options[long_option_index].name,"intra")==0){
  880. intra=1;
  881. } else if (strcmp(long_options[long_option_index].name,"fps")==0){
  882. fps=atoi(optarg);
  883. }
  884. break;
  885. case 'v':
  886. case '?':
  887. case 'h':
  888. default:{
  889. usage(_argv);
  890. exit(EXIT_FAILURE);
  891. }break;
  892. }
  893. }
  894. if(optind+2!=_argc){
  895. usage(_argv);
  896. exit(EXIT_FAILURE);
  897. }
  898. fin=strcmp(_argv[optind],"-")==0?stdin:fopen(_argv[optind],"rb");
  899. if(fin==NULL){
  900. fprintf(stderr,"Unable to open '%s' for extraction.\n",_argv[optind]);
  901. exit(EXIT_FAILURE);
  902. }
  903. fprintf(stderr,"Opening %s as input%s...\n",_argv[optind],ref_in?"":" and reference");
  904. if(video_input_open(&vid1,fin)<0)exit(EXIT_FAILURE);
  905. video_input_get_info(&vid1,&ti1);
  906. if(ref_in){
  907. fin=fopen(refname,"rb");
  908. if(fin==NULL){
  909. fprintf(stderr,"Unable to open '%s' for extraction.\n",refname);
  910. exit(EXIT_FAILURE);
  911. }
  912. fprintf(stderr,"Opening %s as reference...\n",refname);
  913. if(video_input_open(&vid2,fin)<0)exit(EXIT_FAILURE);
  914. video_input_get_info(&vid2,&ti2);
  915. /*Check to make sure these videos are compatible.*/
  916. if(ti1.pic_width!=ti2.pic_width||ti1.pic_height!=ti2.pic_height){
  917. fprintf(stderr,"Video resolution does not match.\n");
  918. exit(EXIT_FAILURE);
  919. }
  920. if(ti1.pixel_fmt!=ti2.pixel_fmt){
  921. fprintf(stderr,"Pixel formats do not match.\n");
  922. exit(EXIT_FAILURE);
  923. }
  924. if((ti1.pic_x&!(ti1.pixel_fmt&1))!=(ti2.pic_x&!(ti2.pixel_fmt&1))||
  925. (ti1.pic_y&!(ti1.pixel_fmt&2))!=(ti2.pic_y&!(ti2.pixel_fmt&2))){
  926. fprintf(stderr,"Chroma subsampling offsets do not match.\n");
  927. exit(EXIT_FAILURE);
  928. }
  929. if(ti1.fps_numerator*(ogg_int64_t)ti2.fps_denominator!=
  930. ti2.fps_numerator*(ogg_int64_t)ti1.fps_denominator){
  931. fprintf(stderr,"Warning: framerates do not match.\n");
  932. }
  933. if(ti1.aspect_numerator*(ogg_int64_t)ti2.aspect_denominator!=
  934. ti2.aspect_numerator*(ogg_int64_t)ti1.aspect_denominator){
  935. fprintf(stderr,"Warning: aspect ratios do not match.\n");
  936. }
  937. }
  938. for(pli=0;pli<3;pli++){
  939. /*Planes padded up to a multiple of 32px*/
  940. xdec[pli]=pli&&!(ti1.pixel_fmt&1);
  941. ydec[pli]=pli&&!(ti1.pixel_fmt&2);
  942. h[pli]=ROUNDUP_32(ti1.pic_height>>ydec[pli]);
  943. w[pli]=ROUNDUP_32( ti1.pic_width>>xdec[pli]);
  944. refi[pli]=malloc(w[pli]*h[pli]*sizeof(od_coeff));
  945. iimg[pli]=malloc(w[pli]*h[pli]*sizeof(od_coeff));
  946. }
  947. outline=malloc(ti1.pic_width*sizeof(char));
  948. fout=strcmp(_argv[optind+1],"-")==0?stdout:fopen(_argv[optind+1],"wb");
  949. if(fout==NULL){
  950. fprintf(stderr,"Error opening output file \"%s\".\n",_argv[optind+1]);
  951. return 1;
  952. }
  953. fprintf(fout,"YUV4MPEG2 W%i H%i F%i:%i Ip A%i:%i%s\n",
  954. ti1.pic_width,ti1.pic_height,fps>0?(unsigned)fps:ti1.fps_numerator,fps>0?1U:ti1.fps_denominator,
  955. ti1.aspect_numerator,ti1.aspect_denominator,CHROMA_TAGS[ydec[1]?xdec[1]?0:2:3]);
  956. for(frameno=0;;frameno++){
  957. th_ycbcr_buffer in;
  958. th_ycbcr_buffer ref;
  959. int ret1=0;
  960. int ret2=0;
  961. char tag1[5];
  962. char tag2[5];
  963. if(!limit||frameno<limit){
  964. ret1=video_input_fetch_frame(&vid1,in,tag1);
  965. if(ref_in)ret2=video_input_fetch_frame(&vid2,ref,tag2);
  966. }
  967. if(ret1==0){
  968. if(extend<1)break;
  969. extend--;
  970. /*If we're extending, keep feeding back the output to the reference input.*/
  971. for(pli=0;pli<3;pli++){
  972. int x;
  973. int y;
  974. for(y=0;y<h[pli];y++){
  975. for(x=0;x<w[pli];x++){
  976. refi[pli][y*w[pli]+x]=iimg[pli][y*w[pli]+x];
  977. }
  978. }
  979. }
  980. }
  981. if(ref_in&&ret1!=0&&ret2==0){
  982. fprintf(stderr,"Warning: Reference ended before input!\n");
  983. break;
  984. }
  985. for(pli=0;pli<3;pli++){
  986. int x;
  987. int y;
  988. if (pli==0)
  989. switch_decision(in[pli].data, w[pli], h[pli], in[pli].stride, ti1.pic_width, ti1.pic_height);
  990. for(y=0;y<h[pli];y++){
  991. for(x=0;x<w[pli];x++){
  992. int cy=OD_MINI(y+(int)(ti1.pic_y>>ydec[pli]),(int)ti1.pic_height>>ydec[pli]);
  993. int cx=OD_MINI(x+(int)(ti1.pic_x>>xdec[pli]),(int)ti1.pic_width>>xdec[pli]);
  994. iimg[pli][y*w[pli]+x]=128*(in[pli].data[cy*in[pli].stride+cx]-128);
  995. }
  996. }
  997. if(ref_in&&ret2!=0){
  998. for(y=0;y<h[pli];y++){
  999. for(x=0;x<w[pli];x++){
  1000. int cy=OD_MINI(y+(int)(ti1.pic_y>>ydec[pli]),(int)ti1.pic_height>>ydec[pli]);
  1001. int cx=OD_MINI(x+(int)(ti1.pic_x>>xdec[pli]),(int)ti1.pic_width>>xdec[pli]);
  1002. refi[pli][y*w[pli]+x]=128*(ref[pli].data[cy*in[pli].stride+cx]-128);
  1003. }
  1004. }
  1005. }
  1006. process_plane(iimg[pli],(ref_in||frameno>0)&&!intra?refi[pli]:NULL,ti1.pic_width>>xdec[pli],ti1.pic_height>>ydec[pli],pli,pvq_k);
  1007. if(!ref_in){
  1008. for(y=0;y<h[pli];y++){
  1009. for(x=0;x<w[pli];x++){
  1010. refi[pli][y*w[pli]+x]=iimg[pli][y*w[pli]+x];
  1011. }
  1012. }
  1013. }
  1014. }
  1015. fprintf(fout,"FRAME\n");
  1016. for(pli=0;pli<3;pli++){
  1017. int x;
  1018. int y;
  1019. for(y=0;y<(int)ti1.pic_height>>ydec[pli];y++){
  1020. for(x=0;x<(int)ti1.pic_width>>xdec[pli];x++)outline[x]=OD_CLAMP255((int)floor(.5+(1./128)*iimg[pli][y*w[pli]+x])+128);
  1021. if(fwrite(outline,
  1022. (ti1.pic_width>>xdec[pli]),1,fout)<1){
  1023. fprintf(stderr,"Error writing to output.\n");
  1024. return EXIT_FAILURE;
  1025. }
  1026. }
  1027. }
  1028. fprintf(stderr, "Completed frame %d.\n",frameno);
  1029. }
  1030. video_input_close(&vid1);
  1031. if(ref_in)video_input_close(&vid2);
  1032. if(fout!=stdout)fclose(fout);
  1033. free(outline);
  1034. for(pli=0;pli<3;pli++)free(refi[pli]);
  1035. for(pli=0;pli<3;pli++)free(iimg[pli]);
  1036. return EXIT_SUCCESS;
  1037. }