You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2069 lines
79KB

  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intmath.h"
  21. #include "libavutil/libm.h"
  22. #include "libavutil/log.h"
  23. #include "libavutil/opt.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. #include "snow_dwt.h"
  27. #include "snow.h"
  28. #include "rangecoder.h"
  29. #include "mathops.h"
  30. #include "mpegvideo.h"
  31. #include "h263.h"
  32. #define FF_ME_ITER 50
  33. static av_cold int encode_init(AVCodecContext *avctx)
  34. {
  35. SnowContext *s = avctx->priv_data;
  36. int plane_index, ret;
  37. int i;
  38. if(avctx->prediction_method == DWT_97
  39. && (avctx->flags & AV_CODEC_FLAG_QSCALE)
  40. && avctx->global_quality == 0){
  41. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  42. return -1;
  43. }
  44. #if FF_API_MOTION_EST
  45. FF_DISABLE_DEPRECATION_WARNINGS
  46. if (avctx->me_method == ME_ITER)
  47. s->motion_est = FF_ME_ITER;
  48. FF_ENABLE_DEPRECATION_WARNINGS
  49. #endif
  50. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  51. s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
  52. s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
  53. for(plane_index=0; plane_index<3; plane_index++){
  54. s->plane[plane_index].diag_mc= 1;
  55. s->plane[plane_index].htaps= 6;
  56. s->plane[plane_index].hcoeff[0]= 40;
  57. s->plane[plane_index].hcoeff[1]= -10;
  58. s->plane[plane_index].hcoeff[2]= 2;
  59. s->plane[plane_index].fast_mc= 1;
  60. }
  61. if ((ret = ff_snow_common_init(avctx)) < 0) {
  62. return ret;
  63. }
  64. ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
  65. ff_snow_alloc_blocks(s);
  66. s->version=0;
  67. s->m.avctx = avctx;
  68. s->m.bit_rate= avctx->bit_rate;
  69. s->m.me.temp =
  70. s->m.me.scratchpad= av_mallocz_array((avctx->width+64), 2*16*2*sizeof(uint8_t));
  71. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  72. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  73. s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  74. if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
  75. return AVERROR(ENOMEM);
  76. ff_h263_encode_init(&s->m); //mv_penalty
  77. s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
  78. if(avctx->flags&AV_CODEC_FLAG_PASS1){
  79. if(!avctx->stats_out)
  80. avctx->stats_out = av_mallocz(256);
  81. if (!avctx->stats_out)
  82. return AVERROR(ENOMEM);
  83. }
  84. if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  85. if(ff_rate_control_init(&s->m) < 0)
  86. return -1;
  87. }
  88. s->pass1_rc= !(avctx->flags & (AV_CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  89. switch(avctx->pix_fmt){
  90. case AV_PIX_FMT_YUV444P:
  91. // case AV_PIX_FMT_YUV422P:
  92. case AV_PIX_FMT_YUV420P:
  93. // case AV_PIX_FMT_YUV411P:
  94. case AV_PIX_FMT_YUV410P:
  95. s->nb_planes = 3;
  96. s->colorspace_type= 0;
  97. break;
  98. case AV_PIX_FMT_GRAY8:
  99. s->nb_planes = 1;
  100. s->colorspace_type = 1;
  101. break;
  102. /* case AV_PIX_FMT_RGB32:
  103. s->colorspace= 1;
  104. break;*/
  105. default:
  106. av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
  107. return -1;
  108. }
  109. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  110. ff_set_cmp(&s->mecc, s->mecc.me_cmp, s->avctx->me_cmp);
  111. ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
  112. s->input_picture = av_frame_alloc();
  113. if (!s->input_picture)
  114. return AVERROR(ENOMEM);
  115. if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
  116. return ret;
  117. if(s->motion_est == FF_ME_ITER){
  118. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  119. for(i=0; i<s->max_ref_frames; i++){
  120. s->ref_mvs[i]= av_mallocz_array(size, sizeof(int16_t[2]));
  121. s->ref_scores[i]= av_mallocz_array(size, sizeof(uint32_t));
  122. if (!s->ref_mvs[i] || !s->ref_scores[i])
  123. return AVERROR(ENOMEM);
  124. }
  125. }
  126. return 0;
  127. }
  128. //near copy & paste from dsputil, FIXME
  129. static int pix_sum(uint8_t * pix, int line_size, int w, int h)
  130. {
  131. int s, i, j;
  132. s = 0;
  133. for (i = 0; i < h; i++) {
  134. for (j = 0; j < w; j++) {
  135. s += pix[0];
  136. pix ++;
  137. }
  138. pix += line_size - w;
  139. }
  140. return s;
  141. }
  142. //near copy & paste from dsputil, FIXME
  143. static int pix_norm1(uint8_t * pix, int line_size, int w)
  144. {
  145. int s, i, j;
  146. uint32_t *sq = ff_square_tab + 256;
  147. s = 0;
  148. for (i = 0; i < w; i++) {
  149. for (j = 0; j < w; j ++) {
  150. s += sq[pix[0]];
  151. pix ++;
  152. }
  153. pix += line_size - w;
  154. }
  155. return s;
  156. }
  157. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  158. switch(type&0xFF){
  159. default:
  160. case FF_CMP_SAD:
  161. return lambda>>FF_LAMBDA_SHIFT;
  162. case FF_CMP_DCT:
  163. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  164. case FF_CMP_W53:
  165. return (4*lambda)>>(FF_LAMBDA_SHIFT);
  166. case FF_CMP_W97:
  167. return (2*lambda)>>(FF_LAMBDA_SHIFT);
  168. case FF_CMP_SATD:
  169. case FF_CMP_DCT264:
  170. return (2*lambda)>>FF_LAMBDA_SHIFT;
  171. case FF_CMP_RD:
  172. case FF_CMP_PSNR:
  173. case FF_CMP_SSE:
  174. case FF_CMP_NSSE:
  175. return lambda2>>FF_LAMBDA_SHIFT;
  176. case FF_CMP_BIT:
  177. return 1;
  178. }
  179. }
  180. //FIXME copy&paste
  181. #define P_LEFT P[1]
  182. #define P_TOP P[2]
  183. #define P_TOPRIGHT P[3]
  184. #define P_MEDIAN P[4]
  185. #define P_MV1 P[9]
  186. #define FLAG_QPEL 1 //must be 1
  187. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  188. uint8_t p_buffer[1024];
  189. uint8_t i_buffer[1024];
  190. uint8_t p_state[sizeof(s->block_state)];
  191. uint8_t i_state[sizeof(s->block_state)];
  192. RangeCoder pc, ic;
  193. uint8_t *pbbak= s->c.bytestream;
  194. uint8_t *pbbak_start= s->c.bytestream_start;
  195. int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  196. const int w= s->b_width << s->block_max_depth;
  197. const int h= s->b_height << s->block_max_depth;
  198. const int rem_depth= s->block_max_depth - level;
  199. const int index= (x + y*w) << rem_depth;
  200. const int block_w= 1<<(LOG2_MB_SIZE - level);
  201. int trx= (x+1)<<rem_depth;
  202. int try= (y+1)<<rem_depth;
  203. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  204. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  205. const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  206. const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  207. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  208. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  209. int pl = left->color[0];
  210. int pcb= left->color[1];
  211. int pcr= left->color[2];
  212. int pmx, pmy;
  213. int mx=0, my=0;
  214. int l,cr,cb;
  215. const int stride= s->current_picture->linesize[0];
  216. const int uvstride= s->current_picture->linesize[1];
  217. uint8_t *current_data[3]= { s->input_picture->data[0] + (x + y* stride)*block_w,
  218. s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
  219. s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
  220. int P[10][2];
  221. int16_t last_mv[3][2];
  222. int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
  223. const int shift= 1+qpel;
  224. MotionEstContext *c= &s->m.me;
  225. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  226. int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  227. int my_context= av_log2(2*FFABS(left->my - top->my));
  228. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  229. int ref, best_ref, ref_score, ref_mx, ref_my;
  230. av_assert0(sizeof(s->block_state) >= 256);
  231. if(s->keyframe){
  232. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  233. return 0;
  234. }
  235. // clip predictors / edge ?
  236. P_LEFT[0]= left->mx;
  237. P_LEFT[1]= left->my;
  238. P_TOP [0]= top->mx;
  239. P_TOP [1]= top->my;
  240. P_TOPRIGHT[0]= tr->mx;
  241. P_TOPRIGHT[1]= tr->my;
  242. last_mv[0][0]= s->block[index].mx;
  243. last_mv[0][1]= s->block[index].my;
  244. last_mv[1][0]= right->mx;
  245. last_mv[1][1]= right->my;
  246. last_mv[2][0]= bottom->mx;
  247. last_mv[2][1]= bottom->my;
  248. s->m.mb_stride=2;
  249. s->m.mb_x=
  250. s->m.mb_y= 0;
  251. c->skip= 0;
  252. av_assert1(c-> stride == stride);
  253. av_assert1(c->uvstride == uvstride);
  254. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  255. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  256. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  257. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_DMV;
  258. c->xmin = - x*block_w - 16+3;
  259. c->ymin = - y*block_w - 16+3;
  260. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  261. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  262. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  263. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  264. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  265. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  266. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  267. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  268. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  269. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  270. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  271. if (!y) {
  272. c->pred_x= P_LEFT[0];
  273. c->pred_y= P_LEFT[1];
  274. } else {
  275. c->pred_x = P_MEDIAN[0];
  276. c->pred_y = P_MEDIAN[1];
  277. }
  278. score= INT_MAX;
  279. best_ref= 0;
  280. for(ref=0; ref<s->ref_frames; ref++){
  281. init_ref(c, current_data, s->last_picture[ref]->data, NULL, block_w*x, block_w*y, 0);
  282. ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  283. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  284. av_assert2(ref_mx >= c->xmin);
  285. av_assert2(ref_mx <= c->xmax);
  286. av_assert2(ref_my >= c->ymin);
  287. av_assert2(ref_my <= c->ymax);
  288. ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  289. ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  290. ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  291. if(s->ref_mvs[ref]){
  292. s->ref_mvs[ref][index][0]= ref_mx;
  293. s->ref_mvs[ref][index][1]= ref_my;
  294. s->ref_scores[ref][index]= ref_score;
  295. }
  296. if(score > ref_score){
  297. score= ref_score;
  298. best_ref= ref;
  299. mx= ref_mx;
  300. my= ref_my;
  301. }
  302. }
  303. //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  304. // subpel search
  305. base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  306. pc= s->c;
  307. pc.bytestream_start=
  308. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  309. memcpy(p_state, s->block_state, sizeof(s->block_state));
  310. if(level!=s->block_max_depth)
  311. put_rac(&pc, &p_state[4 + s_context], 1);
  312. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  313. if(s->ref_frames > 1)
  314. put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  315. pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  316. put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  317. put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  318. p_len= pc.bytestream - pc.bytestream_start;
  319. score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  320. block_s= block_w*block_w;
  321. sum = pix_sum(current_data[0], stride, block_w, block_w);
  322. l= (sum + block_s/2)/block_s;
  323. iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  324. if (s->nb_planes > 2) {
  325. block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
  326. sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
  327. cb= (sum + block_s/2)/block_s;
  328. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  329. sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
  330. cr= (sum + block_s/2)/block_s;
  331. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  332. }else
  333. cb = cr = 0;
  334. ic= s->c;
  335. ic.bytestream_start=
  336. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  337. memcpy(i_state, s->block_state, sizeof(s->block_state));
  338. if(level!=s->block_max_depth)
  339. put_rac(&ic, &i_state[4 + s_context], 1);
  340. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  341. put_symbol(&ic, &i_state[32], l-pl , 1);
  342. if (s->nb_planes > 2) {
  343. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  344. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  345. }
  346. i_len= ic.bytestream - ic.bytestream_start;
  347. iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  348. av_assert1(iscore < 255*255*256 + s->lambda2*10);
  349. av_assert1(iscore >= 0);
  350. av_assert1(l>=0 && l<=255);
  351. av_assert1(pl>=0 && pl<=255);
  352. if(level==0){
  353. int varc= iscore >> 8;
  354. int vard= score >> 8;
  355. if (vard <= 64 || vard < varc)
  356. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  357. else
  358. c->scene_change_score+= s->m.qscale;
  359. }
  360. if(level!=s->block_max_depth){
  361. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  362. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  363. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  364. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  365. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  366. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  367. if(score2 < score && score2 < iscore)
  368. return score2;
  369. }
  370. if(iscore < score){
  371. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  372. memcpy(pbbak, i_buffer, i_len);
  373. s->c= ic;
  374. s->c.bytestream_start= pbbak_start;
  375. s->c.bytestream= pbbak + i_len;
  376. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  377. memcpy(s->block_state, i_state, sizeof(s->block_state));
  378. return iscore;
  379. }else{
  380. memcpy(pbbak, p_buffer, p_len);
  381. s->c= pc;
  382. s->c.bytestream_start= pbbak_start;
  383. s->c.bytestream= pbbak + p_len;
  384. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  385. memcpy(s->block_state, p_state, sizeof(s->block_state));
  386. return score;
  387. }
  388. }
  389. static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  390. const int w= s->b_width << s->block_max_depth;
  391. const int rem_depth= s->block_max_depth - level;
  392. const int index= (x + y*w) << rem_depth;
  393. int trx= (x+1)<<rem_depth;
  394. BlockNode *b= &s->block[index];
  395. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  396. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  397. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  398. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  399. int pl = left->color[0];
  400. int pcb= left->color[1];
  401. int pcr= left->color[2];
  402. int pmx, pmy;
  403. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  404. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  405. int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  406. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  407. if(s->keyframe){
  408. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  409. return;
  410. }
  411. if(level!=s->block_max_depth){
  412. if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  413. put_rac(&s->c, &s->block_state[4 + s_context], 1);
  414. }else{
  415. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  416. encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  417. encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  418. encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  419. encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  420. return;
  421. }
  422. }
  423. if(b->type & BLOCK_INTRA){
  424. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  425. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  426. put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  427. if (s->nb_planes > 2) {
  428. put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  429. put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  430. }
  431. set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  432. }else{
  433. pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  434. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  435. if(s->ref_frames > 1)
  436. put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  437. put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  438. put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  439. set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  440. }
  441. }
  442. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  443. int i, x2, y2;
  444. Plane *p= &s->plane[plane_index];
  445. const int block_size = MB_SIZE >> s->block_max_depth;
  446. const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  447. const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  448. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  449. const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  450. const int ref_stride= s->current_picture->linesize[plane_index];
  451. uint8_t *src= s-> input_picture->data[plane_index];
  452. IDWTELEM *dst= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  453. const int b_stride = s->b_width << s->block_max_depth;
  454. const int w= p->width;
  455. const int h= p->height;
  456. int index= mb_x + mb_y*b_stride;
  457. BlockNode *b= &s->block[index];
  458. BlockNode backup= *b;
  459. int ab=0;
  460. int aa=0;
  461. av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
  462. b->type|= BLOCK_INTRA;
  463. b->color[plane_index]= 0;
  464. memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  465. for(i=0; i<4; i++){
  466. int mb_x2= mb_x + (i &1) - 1;
  467. int mb_y2= mb_y + (i>>1) - 1;
  468. int x= block_w*mb_x2 + block_w/2;
  469. int y= block_h*mb_y2 + block_h/2;
  470. add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
  471. x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  472. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
  473. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  474. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
  475. int obmc_v= obmc[index];
  476. int d;
  477. if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
  478. if(x<0) obmc_v += obmc[index + block_w];
  479. if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
  480. if(x+block_w>w) obmc_v += obmc[index - block_w];
  481. //FIXME precalculate this or simplify it somehow else
  482. d = -dst[index] + (1<<(FRAC_BITS-1));
  483. dst[index] = d;
  484. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  485. aa += obmc_v * obmc_v; //FIXME precalculate this
  486. }
  487. }
  488. }
  489. *b= backup;
  490. return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
  491. }
  492. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  493. const int b_stride = s->b_width << s->block_max_depth;
  494. const int b_height = s->b_height<< s->block_max_depth;
  495. int index= x + y*b_stride;
  496. const BlockNode *b = &s->block[index];
  497. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  498. const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  499. const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  500. const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  501. int dmx, dmy;
  502. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  503. // int my_context= av_log2(2*FFABS(left->my - top->my));
  504. if(x<0 || x>=b_stride || y>=b_height)
  505. return 0;
  506. /*
  507. 1 0 0
  508. 01X 1-2 1
  509. 001XX 3-6 2-3
  510. 0001XXX 7-14 4-7
  511. 00001XXXX 15-30 8-15
  512. */
  513. //FIXME try accurate rate
  514. //FIXME intra and inter predictors if surrounding blocks are not the same type
  515. if(b->type & BLOCK_INTRA){
  516. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  517. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  518. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  519. }else{
  520. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  521. dmx-= b->mx;
  522. dmy-= b->my;
  523. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  524. + av_log2(2*FFABS(dmy))
  525. + av_log2(2*b->ref));
  526. }
  527. }
  528. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2]){
  529. Plane *p= &s->plane[plane_index];
  530. const int block_size = MB_SIZE >> s->block_max_depth;
  531. const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  532. const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  533. const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  534. const int ref_stride= s->current_picture->linesize[plane_index];
  535. uint8_t *dst= s->current_picture->data[plane_index];
  536. uint8_t *src= s-> input_picture->data[plane_index];
  537. IDWTELEM *pred= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
  538. uint8_t *cur = s->scratchbuf;
  539. uint8_t *tmp = s->emu_edge_buffer;
  540. const int b_stride = s->b_width << s->block_max_depth;
  541. const int b_height = s->b_height<< s->block_max_depth;
  542. const int w= p->width;
  543. const int h= p->height;
  544. int distortion;
  545. int rate= 0;
  546. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  547. int sx= block_w*mb_x - block_w/2;
  548. int sy= block_h*mb_y - block_h/2;
  549. int x0= FFMAX(0,-sx);
  550. int y0= FFMAX(0,-sy);
  551. int x1= FFMIN(block_w*2, w-sx);
  552. int y1= FFMIN(block_h*2, h-sy);
  553. int i,x,y;
  554. av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
  555. ff_snow_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_h*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
  556. for(y=y0; y<y1; y++){
  557. const uint8_t *obmc1= obmc_edged[y];
  558. const IDWTELEM *pred1 = pred + y*obmc_stride;
  559. uint8_t *cur1 = cur + y*ref_stride;
  560. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  561. for(x=x0; x<x1; x++){
  562. #if FRAC_BITS >= LOG2_OBMC_MAX
  563. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  564. #else
  565. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  566. #endif
  567. v = (v + pred1[x]) >> FRAC_BITS;
  568. if(v&(~255)) v= ~(v>>31);
  569. dst1[x] = v;
  570. }
  571. }
  572. /* copy the regions where obmc[] = (uint8_t)256 */
  573. if(LOG2_OBMC_MAX == 8
  574. && (mb_x == 0 || mb_x == b_stride-1)
  575. && (mb_y == 0 || mb_y == b_height-1)){
  576. if(mb_x == 0)
  577. x1 = block_w;
  578. else
  579. x0 = block_w;
  580. if(mb_y == 0)
  581. y1 = block_h;
  582. else
  583. y0 = block_h;
  584. for(y=y0; y<y1; y++)
  585. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  586. }
  587. if(block_w==16){
  588. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  589. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  590. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  591. * So improving the score of one block is not strictly guaranteed
  592. * to improve the score of the whole frame, thus iterative motion
  593. * estimation does not always converge. */
  594. if(s->avctx->me_cmp == FF_CMP_W97)
  595. distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  596. else if(s->avctx->me_cmp == FF_CMP_W53)
  597. distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  598. else{
  599. distortion = 0;
  600. for(i=0; i<4; i++){
  601. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  602. distortion += s->mecc.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  603. }
  604. }
  605. }else{
  606. av_assert2(block_w==8);
  607. distortion = s->mecc.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  608. }
  609. if(plane_index==0){
  610. for(i=0; i<4; i++){
  611. /* ..RRr
  612. * .RXx.
  613. * rxx..
  614. */
  615. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  616. }
  617. if(mb_x == b_stride-2)
  618. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  619. }
  620. return distortion + rate*penalty_factor;
  621. }
  622. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  623. int i, y2;
  624. Plane *p= &s->plane[plane_index];
  625. const int block_size = MB_SIZE >> s->block_max_depth;
  626. const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  627. const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  628. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  629. const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  630. const int ref_stride= s->current_picture->linesize[plane_index];
  631. uint8_t *dst= s->current_picture->data[plane_index];
  632. uint8_t *src= s-> input_picture->data[plane_index];
  633. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  634. // const has only been removed from zero_dst to suppress a warning
  635. static IDWTELEM zero_dst[4096]; //FIXME
  636. const int b_stride = s->b_width << s->block_max_depth;
  637. const int w= p->width;
  638. const int h= p->height;
  639. int distortion= 0;
  640. int rate= 0;
  641. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  642. av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
  643. for(i=0; i<9; i++){
  644. int mb_x2= mb_x + (i%3) - 1;
  645. int mb_y2= mb_y + (i/3) - 1;
  646. int x= block_w*mb_x2 + block_w/2;
  647. int y= block_h*mb_y2 + block_h/2;
  648. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  649. x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  650. //FIXME find a cleaner/simpler way to skip the outside stuff
  651. for(y2= y; y2<0; y2++)
  652. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  653. for(y2= h; y2<y+block_h; y2++)
  654. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  655. if(x<0){
  656. for(y2= y; y2<y+block_h; y2++)
  657. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  658. }
  659. if(x+block_w > w){
  660. for(y2= y; y2<y+block_h; y2++)
  661. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  662. }
  663. av_assert1(block_w== 8 || block_w==16);
  664. distortion += s->mecc.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
  665. }
  666. if(plane_index==0){
  667. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  668. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  669. /* ..RRRr
  670. * .RXXx.
  671. * .RXXx.
  672. * rxxx.
  673. */
  674. if(merged)
  675. rate = get_block_bits(s, mb_x, mb_y, 2);
  676. for(i=merged?4:0; i<9; i++){
  677. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  678. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  679. }
  680. }
  681. return distortion + rate*penalty_factor;
  682. }
  683. static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
  684. const int w= b->width;
  685. const int h= b->height;
  686. int x, y;
  687. if(1){
  688. int run=0;
  689. int *runs = s->run_buffer;
  690. int run_index=0;
  691. int max_index;
  692. for(y=0; y<h; y++){
  693. for(x=0; x<w; x++){
  694. int v, p=0;
  695. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  696. v= src[x + y*stride];
  697. if(y){
  698. t= src[x + (y-1)*stride];
  699. if(x){
  700. lt= src[x - 1 + (y-1)*stride];
  701. }
  702. if(x + 1 < w){
  703. rt= src[x + 1 + (y-1)*stride];
  704. }
  705. }
  706. if(x){
  707. l= src[x - 1 + y*stride];
  708. /*if(x > 1){
  709. if(orientation==1) ll= src[y + (x-2)*stride];
  710. else ll= src[x - 2 + y*stride];
  711. }*/
  712. }
  713. if(parent){
  714. int px= x>>1;
  715. int py= y>>1;
  716. if(px<b->parent->width && py<b->parent->height)
  717. p= parent[px + py*2*stride];
  718. }
  719. if(!(/*ll|*/l|lt|t|rt|p)){
  720. if(v){
  721. runs[run_index++]= run;
  722. run=0;
  723. }else{
  724. run++;
  725. }
  726. }
  727. }
  728. }
  729. max_index= run_index;
  730. runs[run_index++]= run;
  731. run_index=0;
  732. run= runs[run_index++];
  733. put_symbol2(&s->c, b->state[30], max_index, 0);
  734. if(run_index <= max_index)
  735. put_symbol2(&s->c, b->state[1], run, 3);
  736. for(y=0; y<h; y++){
  737. if(s->c.bytestream_end - s->c.bytestream < w*40){
  738. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  739. return -1;
  740. }
  741. for(x=0; x<w; x++){
  742. int v, p=0;
  743. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  744. v= src[x + y*stride];
  745. if(y){
  746. t= src[x + (y-1)*stride];
  747. if(x){
  748. lt= src[x - 1 + (y-1)*stride];
  749. }
  750. if(x + 1 < w){
  751. rt= src[x + 1 + (y-1)*stride];
  752. }
  753. }
  754. if(x){
  755. l= src[x - 1 + y*stride];
  756. /*if(x > 1){
  757. if(orientation==1) ll= src[y + (x-2)*stride];
  758. else ll= src[x - 2 + y*stride];
  759. }*/
  760. }
  761. if(parent){
  762. int px= x>>1;
  763. int py= y>>1;
  764. if(px<b->parent->width && py<b->parent->height)
  765. p= parent[px + py*2*stride];
  766. }
  767. if(/*ll|*/l|lt|t|rt|p){
  768. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  769. put_rac(&s->c, &b->state[0][context], !!v);
  770. }else{
  771. if(!run){
  772. run= runs[run_index++];
  773. if(run_index <= max_index)
  774. put_symbol2(&s->c, b->state[1], run, 3);
  775. av_assert2(v);
  776. }else{
  777. run--;
  778. av_assert2(!v);
  779. }
  780. }
  781. if(v){
  782. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  783. int l2= 2*FFABS(l) + (l<0);
  784. int t2= 2*FFABS(t) + (t<0);
  785. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  786. put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
  787. }
  788. }
  789. }
  790. }
  791. return 0;
  792. }
  793. static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
  794. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  795. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  796. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  797. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  798. }
  799. static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
  800. const int b_stride= s->b_width << s->block_max_depth;
  801. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  802. BlockNode backup= *block;
  803. unsigned value;
  804. int rd, index;
  805. av_assert2(mb_x>=0 && mb_y>=0);
  806. av_assert2(mb_x<b_stride);
  807. if(intra){
  808. block->color[0] = p[0];
  809. block->color[1] = p[1];
  810. block->color[2] = p[2];
  811. block->type |= BLOCK_INTRA;
  812. }else{
  813. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  814. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  815. if(s->me_cache[index] == value)
  816. return 0;
  817. s->me_cache[index]= value;
  818. block->mx= p[0];
  819. block->my= p[1];
  820. block->type &= ~BLOCK_INTRA;
  821. }
  822. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged) + s->intra_penalty * !!intra;
  823. //FIXME chroma
  824. if(rd < *best_rd){
  825. *best_rd= rd;
  826. return 1;
  827. }else{
  828. *block= backup;
  829. return 0;
  830. }
  831. }
  832. /* special case for int[2] args we discard afterwards,
  833. * fixes compilation problem with gcc 2.95 */
  834. static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
  835. int p[2] = {p0, p1};
  836. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  837. }
  838. static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
  839. const int b_stride= s->b_width << s->block_max_depth;
  840. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  841. BlockNode backup[4];
  842. unsigned value;
  843. int rd, index;
  844. /* We don't initialize backup[] during variable declaration, because
  845. * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
  846. * 'int16_t'". */
  847. backup[0] = block[0];
  848. backup[1] = block[1];
  849. backup[2] = block[b_stride];
  850. backup[3] = block[b_stride + 1];
  851. av_assert2(mb_x>=0 && mb_y>=0);
  852. av_assert2(mb_x<b_stride);
  853. av_assert2(((mb_x|mb_y)&1) == 0);
  854. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  855. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  856. if(s->me_cache[index] == value)
  857. return 0;
  858. s->me_cache[index]= value;
  859. block->mx= p0;
  860. block->my= p1;
  861. block->ref= ref;
  862. block->type &= ~BLOCK_INTRA;
  863. block[1]= block[b_stride]= block[b_stride+1]= *block;
  864. rd= get_4block_rd(s, mb_x, mb_y, 0);
  865. //FIXME chroma
  866. if(rd < *best_rd){
  867. *best_rd= rd;
  868. return 1;
  869. }else{
  870. block[0]= backup[0];
  871. block[1]= backup[1];
  872. block[b_stride]= backup[2];
  873. block[b_stride+1]= backup[3];
  874. return 0;
  875. }
  876. }
  877. static void iterative_me(SnowContext *s){
  878. int pass, mb_x, mb_y;
  879. const int b_width = s->b_width << s->block_max_depth;
  880. const int b_height= s->b_height << s->block_max_depth;
  881. const int b_stride= b_width;
  882. int color[3];
  883. {
  884. RangeCoder r = s->c;
  885. uint8_t state[sizeof(s->block_state)];
  886. memcpy(state, s->block_state, sizeof(s->block_state));
  887. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  888. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  889. encode_q_branch(s, 0, mb_x, mb_y);
  890. s->c = r;
  891. memcpy(s->block_state, state, sizeof(s->block_state));
  892. }
  893. for(pass=0; pass<25; pass++){
  894. int change= 0;
  895. for(mb_y= 0; mb_y<b_height; mb_y++){
  896. for(mb_x= 0; mb_x<b_width; mb_x++){
  897. int dia_change, i, j, ref;
  898. int best_rd= INT_MAX, ref_rd;
  899. BlockNode backup, ref_b;
  900. const int index= mb_x + mb_y * b_stride;
  901. BlockNode *block= &s->block[index];
  902. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  903. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  904. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  905. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  906. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  907. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  908. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  909. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  910. const int b_w= (MB_SIZE >> s->block_max_depth);
  911. uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
  912. if(pass && (block->type & BLOCK_OPT))
  913. continue;
  914. block->type |= BLOCK_OPT;
  915. backup= *block;
  916. if(!s->me_cache_generation)
  917. memset(s->me_cache, 0, sizeof(s->me_cache));
  918. s->me_cache_generation += 1<<22;
  919. //FIXME precalculate
  920. {
  921. int x, y;
  922. for (y = 0; y < b_w * 2; y++)
  923. memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
  924. if(mb_x==0)
  925. for(y=0; y<b_w*2; y++)
  926. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  927. if(mb_x==b_stride-1)
  928. for(y=0; y<b_w*2; y++)
  929. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  930. if(mb_y==0){
  931. for(x=0; x<b_w*2; x++)
  932. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  933. for(y=1; y<b_w; y++)
  934. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  935. }
  936. if(mb_y==b_height-1){
  937. for(x=0; x<b_w*2; x++)
  938. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  939. for(y=b_w; y<b_w*2-1; y++)
  940. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  941. }
  942. }
  943. //skip stuff outside the picture
  944. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  945. uint8_t *src= s-> input_picture->data[0];
  946. uint8_t *dst= s->current_picture->data[0];
  947. const int stride= s->current_picture->linesize[0];
  948. const int block_w= MB_SIZE >> s->block_max_depth;
  949. const int block_h= MB_SIZE >> s->block_max_depth;
  950. const int sx= block_w*mb_x - block_w/2;
  951. const int sy= block_h*mb_y - block_h/2;
  952. const int w= s->plane[0].width;
  953. const int h= s->plane[0].height;
  954. int y;
  955. for(y=sy; y<0; y++)
  956. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  957. for(y=h; y<sy+block_h*2; y++)
  958. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  959. if(sx<0){
  960. for(y=sy; y<sy+block_h*2; y++)
  961. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  962. }
  963. if(sx+block_w*2 > w){
  964. for(y=sy; y<sy+block_h*2; y++)
  965. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  966. }
  967. }
  968. // intra(black) = neighbors' contribution to the current block
  969. for(i=0; i < s->nb_planes; i++)
  970. color[i]= get_dc(s, mb_x, mb_y, i);
  971. // get previous score (cannot be cached due to OBMC)
  972. if(pass > 0 && (block->type&BLOCK_INTRA)){
  973. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  974. check_block(s, mb_x, mb_y, color0, 1, obmc_edged, &best_rd);
  975. }else
  976. check_block_inter(s, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
  977. ref_b= *block;
  978. ref_rd= best_rd;
  979. for(ref=0; ref < s->ref_frames; ref++){
  980. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  981. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  982. continue;
  983. block->ref= ref;
  984. best_rd= INT_MAX;
  985. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
  986. check_block_inter(s, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
  987. if(tb)
  988. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
  989. if(lb)
  990. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
  991. if(rb)
  992. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
  993. if(bb)
  994. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
  995. /* fullpel ME */
  996. //FIXME avoid subpel interpolation / round to nearest integer
  997. do{
  998. int newx = block->mx;
  999. int newy = block->my;
  1000. int dia_size = s->iterative_dia_size ? s->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
  1001. dia_change=0;
  1002. for(i=0; i < dia_size; i++){
  1003. for(j=0; j<i; j++){
  1004. dia_change |= check_block_inter(s, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
  1005. dia_change |= check_block_inter(s, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
  1006. dia_change |= check_block_inter(s, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
  1007. dia_change |= check_block_inter(s, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
  1008. }
  1009. }
  1010. }while(dia_change);
  1011. /* subpel ME */
  1012. do{
  1013. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  1014. dia_change=0;
  1015. for(i=0; i<8; i++)
  1016. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
  1017. }while(dia_change);
  1018. //FIXME or try the standard 2 pass qpel or similar
  1019. mvr[0][0]= block->mx;
  1020. mvr[0][1]= block->my;
  1021. if(ref_rd > best_rd){
  1022. ref_rd= best_rd;
  1023. ref_b= *block;
  1024. }
  1025. }
  1026. best_rd= ref_rd;
  1027. *block= ref_b;
  1028. check_block(s, mb_x, mb_y, color, 1, obmc_edged, &best_rd);
  1029. //FIXME RD style color selection
  1030. if(!same_block(block, &backup)){
  1031. if(tb ) tb ->type &= ~BLOCK_OPT;
  1032. if(lb ) lb ->type &= ~BLOCK_OPT;
  1033. if(rb ) rb ->type &= ~BLOCK_OPT;
  1034. if(bb ) bb ->type &= ~BLOCK_OPT;
  1035. if(tlb) tlb->type &= ~BLOCK_OPT;
  1036. if(trb) trb->type &= ~BLOCK_OPT;
  1037. if(blb) blb->type &= ~BLOCK_OPT;
  1038. if(brb) brb->type &= ~BLOCK_OPT;
  1039. change ++;
  1040. }
  1041. }
  1042. }
  1043. av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
  1044. if(!change)
  1045. break;
  1046. }
  1047. if(s->block_max_depth == 1){
  1048. int change= 0;
  1049. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  1050. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  1051. int i;
  1052. int best_rd, init_rd;
  1053. const int index= mb_x + mb_y * b_stride;
  1054. BlockNode *b[4];
  1055. b[0]= &s->block[index];
  1056. b[1]= b[0]+1;
  1057. b[2]= b[0]+b_stride;
  1058. b[3]= b[2]+1;
  1059. if(same_block(b[0], b[1]) &&
  1060. same_block(b[0], b[2]) &&
  1061. same_block(b[0], b[3]))
  1062. continue;
  1063. if(!s->me_cache_generation)
  1064. memset(s->me_cache, 0, sizeof(s->me_cache));
  1065. s->me_cache_generation += 1<<22;
  1066. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  1067. //FIXME more multiref search?
  1068. check_4block_inter(s, mb_x, mb_y,
  1069. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  1070. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  1071. for(i=0; i<4; i++)
  1072. if(!(b[i]->type&BLOCK_INTRA))
  1073. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  1074. if(init_rd != best_rd)
  1075. change++;
  1076. }
  1077. }
  1078. av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  1079. }
  1080. }
  1081. static void encode_blocks(SnowContext *s, int search){
  1082. int x, y;
  1083. int w= s->b_width;
  1084. int h= s->b_height;
  1085. if(s->motion_est == FF_ME_ITER && !s->keyframe && search)
  1086. iterative_me(s);
  1087. for(y=0; y<h; y++){
  1088. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  1089. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1090. return;
  1091. }
  1092. for(x=0; x<w; x++){
  1093. if(s->motion_est == FF_ME_ITER || !search)
  1094. encode_q_branch2(s, 0, x, y);
  1095. else
  1096. encode_q_branch (s, 0, x, y);
  1097. }
  1098. }
  1099. }
  1100. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  1101. const int w= b->width;
  1102. const int h= b->height;
  1103. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1104. const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  1105. int x,y, thres1, thres2;
  1106. if(s->qlog == LOSSLESS_QLOG){
  1107. for(y=0; y<h; y++)
  1108. for(x=0; x<w; x++)
  1109. dst[x + y*stride]= src[x + y*stride];
  1110. return;
  1111. }
  1112. bias= bias ? 0 : (3*qmul)>>3;
  1113. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  1114. thres2= 2*thres1;
  1115. if(!bias){
  1116. for(y=0; y<h; y++){
  1117. for(x=0; x<w; x++){
  1118. int i= src[x + y*stride];
  1119. if((unsigned)(i+thres1) > thres2){
  1120. if(i>=0){
  1121. i<<= QEXPSHIFT;
  1122. i/= qmul; //FIXME optimize
  1123. dst[x + y*stride]= i;
  1124. }else{
  1125. i= -i;
  1126. i<<= QEXPSHIFT;
  1127. i/= qmul; //FIXME optimize
  1128. dst[x + y*stride]= -i;
  1129. }
  1130. }else
  1131. dst[x + y*stride]= 0;
  1132. }
  1133. }
  1134. }else{
  1135. for(y=0; y<h; y++){
  1136. for(x=0; x<w; x++){
  1137. int i= src[x + y*stride];
  1138. if((unsigned)(i+thres1) > thres2){
  1139. if(i>=0){
  1140. i<<= QEXPSHIFT;
  1141. i= (i + bias) / qmul; //FIXME optimize
  1142. dst[x + y*stride]= i;
  1143. }else{
  1144. i= -i;
  1145. i<<= QEXPSHIFT;
  1146. i= (i + bias) / qmul; //FIXME optimize
  1147. dst[x + y*stride]= -i;
  1148. }
  1149. }else
  1150. dst[x + y*stride]= 0;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  1156. const int w= b->width;
  1157. const int h= b->height;
  1158. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1159. const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1160. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1161. int x,y;
  1162. if(s->qlog == LOSSLESS_QLOG) return;
  1163. for(y=0; y<h; y++){
  1164. for(x=0; x<w; x++){
  1165. int i= src[x + y*stride];
  1166. if(i<0){
  1167. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  1168. }else if(i>0){
  1169. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  1170. }
  1171. }
  1172. }
  1173. }
  1174. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  1175. const int w= b->width;
  1176. const int h= b->height;
  1177. int x,y;
  1178. for(y=h-1; y>=0; y--){
  1179. for(x=w-1; x>=0; x--){
  1180. int i= x + y*stride;
  1181. if(x){
  1182. if(use_median){
  1183. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  1184. else src[i] -= src[i - 1];
  1185. }else{
  1186. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  1187. else src[i] -= src[i - 1];
  1188. }
  1189. }else{
  1190. if(y) src[i] -= src[i - stride];
  1191. }
  1192. }
  1193. }
  1194. }
  1195. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  1196. const int w= b->width;
  1197. const int h= b->height;
  1198. int x,y;
  1199. for(y=0; y<h; y++){
  1200. for(x=0; x<w; x++){
  1201. int i= x + y*stride;
  1202. if(x){
  1203. if(use_median){
  1204. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  1205. else src[i] += src[i - 1];
  1206. }else{
  1207. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  1208. else src[i] += src[i - 1];
  1209. }
  1210. }else{
  1211. if(y) src[i] += src[i - stride];
  1212. }
  1213. }
  1214. }
  1215. }
  1216. static void encode_qlogs(SnowContext *s){
  1217. int plane_index, level, orientation;
  1218. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  1219. for(level=0; level<s->spatial_decomposition_count; level++){
  1220. for(orientation=level ? 1:0; orientation<4; orientation++){
  1221. if(orientation==2) continue;
  1222. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  1223. }
  1224. }
  1225. }
  1226. }
  1227. static void encode_header(SnowContext *s){
  1228. int plane_index, i;
  1229. uint8_t kstate[32];
  1230. memset(kstate, MID_STATE, sizeof(kstate));
  1231. put_rac(&s->c, kstate, s->keyframe);
  1232. if(s->keyframe || s->always_reset){
  1233. ff_snow_reset_contexts(s);
  1234. s->last_spatial_decomposition_type=
  1235. s->last_qlog=
  1236. s->last_qbias=
  1237. s->last_mv_scale=
  1238. s->last_block_max_depth= 0;
  1239. for(plane_index=0; plane_index<2; plane_index++){
  1240. Plane *p= &s->plane[plane_index];
  1241. p->last_htaps=0;
  1242. p->last_diag_mc=0;
  1243. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  1244. }
  1245. }
  1246. if(s->keyframe){
  1247. put_symbol(&s->c, s->header_state, s->version, 0);
  1248. put_rac(&s->c, s->header_state, s->always_reset);
  1249. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  1250. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  1251. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  1252. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  1253. if (s->nb_planes > 2) {
  1254. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  1255. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  1256. }
  1257. put_rac(&s->c, s->header_state, s->spatial_scalability);
  1258. // put_rac(&s->c, s->header_state, s->rate_scalability);
  1259. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  1260. encode_qlogs(s);
  1261. }
  1262. if(!s->keyframe){
  1263. int update_mc=0;
  1264. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  1265. Plane *p= &s->plane[plane_index];
  1266. update_mc |= p->last_htaps != p->htaps;
  1267. update_mc |= p->last_diag_mc != p->diag_mc;
  1268. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  1269. }
  1270. put_rac(&s->c, s->header_state, update_mc);
  1271. if(update_mc){
  1272. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  1273. Plane *p= &s->plane[plane_index];
  1274. put_rac(&s->c, s->header_state, p->diag_mc);
  1275. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  1276. for(i= p->htaps/2; i; i--)
  1277. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  1278. }
  1279. }
  1280. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  1281. put_rac(&s->c, s->header_state, 1);
  1282. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  1283. encode_qlogs(s);
  1284. }else
  1285. put_rac(&s->c, s->header_state, 0);
  1286. }
  1287. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  1288. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  1289. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  1290. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  1291. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  1292. }
  1293. static void update_last_header_values(SnowContext *s){
  1294. int plane_index;
  1295. if(!s->keyframe){
  1296. for(plane_index=0; plane_index<2; plane_index++){
  1297. Plane *p= &s->plane[plane_index];
  1298. p->last_diag_mc= p->diag_mc;
  1299. p->last_htaps = p->htaps;
  1300. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  1301. }
  1302. }
  1303. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  1304. s->last_qlog = s->qlog;
  1305. s->last_qbias = s->qbias;
  1306. s->last_mv_scale = s->mv_scale;
  1307. s->last_block_max_depth = s->block_max_depth;
  1308. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  1309. }
  1310. static int qscale2qlog(int qscale){
  1311. return lrint(QROOT*log2(qscale / (float)FF_QP2LAMBDA))
  1312. + 61*QROOT/8; ///< 64 > 60
  1313. }
  1314. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  1315. {
  1316. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  1317. * FIXME we know exact mv bits at this point,
  1318. * but ratecontrol isn't set up to include them. */
  1319. uint32_t coef_sum= 0;
  1320. int level, orientation, delta_qlog;
  1321. for(level=0; level<s->spatial_decomposition_count; level++){
  1322. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1323. SubBand *b= &s->plane[0].band[level][orientation];
  1324. IDWTELEM *buf= b->ibuf;
  1325. const int w= b->width;
  1326. const int h= b->height;
  1327. const int stride= b->stride;
  1328. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  1329. const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1330. const int qdiv= (1<<16)/qmul;
  1331. int x, y;
  1332. //FIXME this is ugly
  1333. for(y=0; y<h; y++)
  1334. for(x=0; x<w; x++)
  1335. buf[x+y*stride]= b->buf[x+y*stride];
  1336. if(orientation==0)
  1337. decorrelate(s, b, buf, stride, 1, 0);
  1338. for(y=0; y<h; y++)
  1339. for(x=0; x<w; x++)
  1340. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  1341. }
  1342. }
  1343. /* ugly, ratecontrol just takes a sqrt again */
  1344. av_assert0(coef_sum < INT_MAX);
  1345. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  1346. if(pict->pict_type == AV_PICTURE_TYPE_I){
  1347. s->m.current_picture.mb_var_sum= coef_sum;
  1348. s->m.current_picture.mc_mb_var_sum= 0;
  1349. }else{
  1350. s->m.current_picture.mc_mb_var_sum= coef_sum;
  1351. s->m.current_picture.mb_var_sum= 0;
  1352. }
  1353. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  1354. if (pict->quality < 0)
  1355. return INT_MIN;
  1356. s->lambda= pict->quality * 3/2;
  1357. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  1358. s->qlog+= delta_qlog;
  1359. return delta_qlog;
  1360. }
  1361. static void calculate_visual_weight(SnowContext *s, Plane *p){
  1362. int width = p->width;
  1363. int height= p->height;
  1364. int level, orientation, x, y;
  1365. for(level=0; level<s->spatial_decomposition_count; level++){
  1366. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1367. SubBand *b= &p->band[level][orientation];
  1368. IDWTELEM *ibuf= b->ibuf;
  1369. int64_t error=0;
  1370. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  1371. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  1372. ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1373. for(y=0; y<height; y++){
  1374. for(x=0; x<width; x++){
  1375. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  1376. error += d*d;
  1377. }
  1378. }
  1379. b->qlog= (int)(QROOT * log2(352256.0/sqrt(error)) + 0.5);
  1380. }
  1381. }
  1382. }
  1383. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1384. const AVFrame *pict, int *got_packet)
  1385. {
  1386. SnowContext *s = avctx->priv_data;
  1387. RangeCoder * const c= &s->c;
  1388. AVFrame *pic;
  1389. const int width= s->avctx->width;
  1390. const int height= s->avctx->height;
  1391. int level, orientation, plane_index, i, y, ret;
  1392. uint8_t rc_header_bak[sizeof(s->header_state)];
  1393. uint8_t rc_block_bak[sizeof(s->block_state)];
  1394. if ((ret = ff_alloc_packet2(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  1395. return ret;
  1396. ff_init_range_encoder(c, pkt->data, pkt->size);
  1397. ff_build_rac_states(c, (1LL<<32)/20, 256-8);
  1398. for(i=0; i < s->nb_planes; i++){
  1399. int hshift= i ? s->chroma_h_shift : 0;
  1400. int vshift= i ? s->chroma_v_shift : 0;
  1401. for(y=0; y<AV_CEIL_RSHIFT(height, vshift); y++)
  1402. memcpy(&s->input_picture->data[i][y * s->input_picture->linesize[i]],
  1403. &pict->data[i][y * pict->linesize[i]],
  1404. AV_CEIL_RSHIFT(width, hshift));
  1405. s->mpvencdsp.draw_edges(s->input_picture->data[i], s->input_picture->linesize[i],
  1406. AV_CEIL_RSHIFT(width, hshift), AV_CEIL_RSHIFT(height, vshift),
  1407. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1408. EDGE_TOP | EDGE_BOTTOM);
  1409. }
  1410. emms_c();
  1411. pic = s->input_picture;
  1412. pic->pict_type = pict->pict_type;
  1413. pic->quality = pict->quality;
  1414. s->m.picture_number= avctx->frame_number;
  1415. if(avctx->flags&AV_CODEC_FLAG_PASS2){
  1416. s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  1417. s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
  1418. if(!(avctx->flags&AV_CODEC_FLAG_QSCALE)) {
  1419. pic->quality = ff_rate_estimate_qscale(&s->m, 0);
  1420. if (pic->quality < 0)
  1421. return -1;
  1422. }
  1423. }else{
  1424. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  1425. s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1426. }
  1427. if(s->pass1_rc && avctx->frame_number == 0)
  1428. pic->quality = 2*FF_QP2LAMBDA;
  1429. if (pic->quality) {
  1430. s->qlog = qscale2qlog(pic->quality);
  1431. s->lambda = pic->quality * 3/2;
  1432. }
  1433. if (s->qlog < 0 || (!pic->quality && (avctx->flags & AV_CODEC_FLAG_QSCALE))) {
  1434. s->qlog= LOSSLESS_QLOG;
  1435. s->lambda = 0;
  1436. }//else keep previous frame's qlog until after motion estimation
  1437. if (s->current_picture->data[0]
  1438. #if FF_API_EMU_EDGE
  1439. && !(s->avctx->flags&CODEC_FLAG_EMU_EDGE)
  1440. #endif
  1441. ) {
  1442. int w = s->avctx->width;
  1443. int h = s->avctx->height;
  1444. s->mpvencdsp.draw_edges(s->current_picture->data[0],
  1445. s->current_picture->linesize[0], w , h ,
  1446. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1447. if (s->current_picture->data[2]) {
  1448. s->mpvencdsp.draw_edges(s->current_picture->data[1],
  1449. s->current_picture->linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
  1450. EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
  1451. s->mpvencdsp.draw_edges(s->current_picture->data[2],
  1452. s->current_picture->linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
  1453. EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
  1454. }
  1455. }
  1456. ff_snow_frame_start(s);
  1457. av_frame_unref(avctx->coded_frame);
  1458. ret = av_frame_ref(avctx->coded_frame, s->current_picture);
  1459. if (ret < 0)
  1460. return ret;
  1461. s->m.current_picture_ptr= &s->m.current_picture;
  1462. s->m.current_picture.f = s->current_picture;
  1463. s->m.current_picture.f->pts = pict->pts;
  1464. if(pic->pict_type == AV_PICTURE_TYPE_P){
  1465. int block_width = (width +15)>>4;
  1466. int block_height= (height+15)>>4;
  1467. int stride= s->current_picture->linesize[0];
  1468. av_assert0(s->current_picture->data[0]);
  1469. av_assert0(s->last_picture[0]->data[0]);
  1470. s->m.avctx= s->avctx;
  1471. s->m. last_picture.f = s->last_picture[0];
  1472. s->m. new_picture.f = s->input_picture;
  1473. s->m. last_picture_ptr= &s->m. last_picture;
  1474. s->m.linesize = stride;
  1475. s->m.uvlinesize= s->current_picture->linesize[1];
  1476. s->m.width = width;
  1477. s->m.height= height;
  1478. s->m.mb_width = block_width;
  1479. s->m.mb_height= block_height;
  1480. s->m.mb_stride= s->m.mb_width+1;
  1481. s->m.b8_stride= 2*s->m.mb_width+1;
  1482. s->m.f_code=1;
  1483. s->m.pict_type = pic->pict_type;
  1484. #if FF_API_MOTION_EST
  1485. s->m.me_method= s->avctx->me_method;
  1486. #endif
  1487. s->m.motion_est= s->motion_est;
  1488. s->m.me.scene_change_score=0;
  1489. s->m.me.dia_size = avctx->dia_size;
  1490. s->m.quarter_sample= (s->avctx->flags & AV_CODEC_FLAG_QPEL)!=0;
  1491. s->m.out_format= FMT_H263;
  1492. s->m.unrestricted_mv= 1;
  1493. s->m.lambda = s->lambda;
  1494. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  1495. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  1496. s->m.mecc= s->mecc; //move
  1497. s->m.qdsp= s->qdsp; //move
  1498. s->m.hdsp = s->hdsp;
  1499. ff_init_me(&s->m);
  1500. s->hdsp = s->m.hdsp;
  1501. s->mecc= s->m.mecc;
  1502. }
  1503. if(s->pass1_rc){
  1504. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  1505. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  1506. }
  1507. redo_frame:
  1508. s->spatial_decomposition_count= 5;
  1509. while( !(width >>(s->chroma_h_shift + s->spatial_decomposition_count))
  1510. || !(height>>(s->chroma_v_shift + s->spatial_decomposition_count)))
  1511. s->spatial_decomposition_count--;
  1512. if (s->spatial_decomposition_count <= 0) {
  1513. av_log(avctx, AV_LOG_ERROR, "Resolution too low\n");
  1514. return AVERROR(EINVAL);
  1515. }
  1516. s->m.pict_type = pic->pict_type;
  1517. s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
  1518. ff_snow_common_init_after_header(avctx);
  1519. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  1520. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  1521. calculate_visual_weight(s, &s->plane[plane_index]);
  1522. }
  1523. }
  1524. encode_header(s);
  1525. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  1526. encode_blocks(s, 1);
  1527. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  1528. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  1529. Plane *p= &s->plane[plane_index];
  1530. int w= p->width;
  1531. int h= p->height;
  1532. int x, y;
  1533. // int bits= put_bits_count(&s->c.pb);
  1534. if (!s->memc_only) {
  1535. //FIXME optimize
  1536. if(pict->data[plane_index]) //FIXME gray hack
  1537. for(y=0; y<h; y++){
  1538. for(x=0; x<w; x++){
  1539. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  1540. }
  1541. }
  1542. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  1543. if( plane_index==0
  1544. && pic->pict_type == AV_PICTURE_TYPE_P
  1545. && !(avctx->flags&AV_CODEC_FLAG_PASS2)
  1546. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  1547. ff_init_range_encoder(c, pkt->data, pkt->size);
  1548. ff_build_rac_states(c, (1LL<<32)/20, 256-8);
  1549. pic->pict_type= AV_PICTURE_TYPE_I;
  1550. s->keyframe=1;
  1551. s->current_picture->key_frame=1;
  1552. goto redo_frame;
  1553. }
  1554. if(s->qlog == LOSSLESS_QLOG){
  1555. for(y=0; y<h; y++){
  1556. for(x=0; x<w; x++){
  1557. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  1558. }
  1559. }
  1560. }else{
  1561. for(y=0; y<h; y++){
  1562. for(x=0; x<w; x++){
  1563. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  1564. }
  1565. }
  1566. }
  1567. ff_spatial_dwt(s->spatial_dwt_buffer, s->temp_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1568. if(s->pass1_rc && plane_index==0){
  1569. int delta_qlog = ratecontrol_1pass(s, pic);
  1570. if (delta_qlog <= INT_MIN)
  1571. return -1;
  1572. if(delta_qlog){
  1573. //reordering qlog in the bitstream would eliminate this reset
  1574. ff_init_range_encoder(c, pkt->data, pkt->size);
  1575. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  1576. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  1577. encode_header(s);
  1578. encode_blocks(s, 0);
  1579. }
  1580. }
  1581. for(level=0; level<s->spatial_decomposition_count; level++){
  1582. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1583. SubBand *b= &p->band[level][orientation];
  1584. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  1585. if(orientation==0)
  1586. decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
  1587. if (!s->no_bitstream)
  1588. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  1589. av_assert0(b->parent==NULL || b->parent->stride == b->stride*2);
  1590. if(orientation==0)
  1591. correlate(s, b, b->ibuf, b->stride, 1, 0);
  1592. }
  1593. }
  1594. for(level=0; level<s->spatial_decomposition_count; level++){
  1595. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1596. SubBand *b= &p->band[level][orientation];
  1597. dequantize(s, b, b->ibuf, b->stride);
  1598. }
  1599. }
  1600. ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1601. if(s->qlog == LOSSLESS_QLOG){
  1602. for(y=0; y<h; y++){
  1603. for(x=0; x<w; x++){
  1604. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  1605. }
  1606. }
  1607. }
  1608. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1609. }else{
  1610. //ME/MC only
  1611. if(pic->pict_type == AV_PICTURE_TYPE_I){
  1612. for(y=0; y<h; y++){
  1613. for(x=0; x<w; x++){
  1614. s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x]=
  1615. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  1616. }
  1617. }
  1618. }else{
  1619. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  1620. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1621. }
  1622. }
  1623. if(s->avctx->flags&AV_CODEC_FLAG_PSNR){
  1624. int64_t error= 0;
  1625. if(pict->data[plane_index]) //FIXME gray hack
  1626. for(y=0; y<h; y++){
  1627. for(x=0; x<w; x++){
  1628. int d= s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
  1629. error += d*d;
  1630. }
  1631. }
  1632. s->avctx->error[plane_index] += error;
  1633. s->encoding_error[plane_index] = error;
  1634. }
  1635. }
  1636. update_last_header_values(s);
  1637. ff_snow_release_buffer(avctx);
  1638. s->current_picture->coded_picture_number = avctx->frame_number;
  1639. s->current_picture->pict_type = pic->pict_type;
  1640. s->current_picture->quality = pic->quality;
  1641. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  1642. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  1643. s->m.current_picture.f->display_picture_number =
  1644. s->m.current_picture.f->coded_picture_number = avctx->frame_number;
  1645. s->m.current_picture.f->quality = pic->quality;
  1646. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  1647. if(s->pass1_rc)
  1648. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  1649. return -1;
  1650. if(avctx->flags&AV_CODEC_FLAG_PASS1)
  1651. ff_write_pass1_stats(&s->m);
  1652. s->m.last_pict_type = s->m.pict_type;
  1653. avctx->frame_bits = s->m.frame_bits;
  1654. avctx->mv_bits = s->m.mv_bits;
  1655. avctx->misc_bits = s->m.misc_bits;
  1656. avctx->p_tex_bits = s->m.p_tex_bits;
  1657. emms_c();
  1658. ff_side_data_set_encoder_stats(pkt, s->current_picture->quality,
  1659. s->encoding_error,
  1660. (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
  1661. s->current_picture->pict_type);
  1662. #if FF_API_ERROR_FRAME
  1663. FF_DISABLE_DEPRECATION_WARNINGS
  1664. memcpy(s->current_picture->error, s->encoding_error, sizeof(s->encoding_error));
  1665. FF_ENABLE_DEPRECATION_WARNINGS
  1666. #endif
  1667. pkt->size = ff_rac_terminate(c);
  1668. if (s->current_picture->key_frame)
  1669. pkt->flags |= AV_PKT_FLAG_KEY;
  1670. *got_packet = 1;
  1671. return 0;
  1672. }
  1673. static av_cold int encode_end(AVCodecContext *avctx)
  1674. {
  1675. SnowContext *s = avctx->priv_data;
  1676. ff_snow_common_end(s);
  1677. ff_rate_control_uninit(&s->m);
  1678. av_frame_free(&s->input_picture);
  1679. av_freep(&avctx->stats_out);
  1680. return 0;
  1681. }
  1682. #define OFFSET(x) offsetof(SnowContext, x)
  1683. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1684. static const AVOption options[] = {
  1685. FF_MPV_COMMON_OPTS
  1686. { "iter", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ITER }, 0, 0, FF_MPV_OPT_FLAGS, "motion_est" },
  1687. { "memc_only", "Only do ME/MC (I frames -> ref, P frame -> ME+MC).", OFFSET(memc_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  1688. { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  1689. { "intra_penalty", "Penalty for intra blocks in block decission", OFFSET(intra_penalty), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  1690. { "iterative_dia_size", "Dia size for the iterative ME", OFFSET(iterative_dia_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  1691. { NULL },
  1692. };
  1693. static const AVClass snowenc_class = {
  1694. .class_name = "snow encoder",
  1695. .item_name = av_default_item_name,
  1696. .option = options,
  1697. .version = LIBAVUTIL_VERSION_INT,
  1698. };
  1699. AVCodec ff_snow_encoder = {
  1700. .name = "snow",
  1701. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  1702. .type = AVMEDIA_TYPE_VIDEO,
  1703. .id = AV_CODEC_ID_SNOW,
  1704. .priv_data_size = sizeof(SnowContext),
  1705. .init = encode_init,
  1706. .encode2 = encode_frame,
  1707. .close = encode_end,
  1708. .pix_fmts = (const enum AVPixelFormat[]){
  1709. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV444P,
  1710. AV_PIX_FMT_GRAY8,
  1711. AV_PIX_FMT_NONE
  1712. },
  1713. .priv_class = &snowenc_class,
  1714. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  1715. FF_CODEC_CAP_INIT_CLEANUP,
  1716. };
  1717. #ifdef TEST
  1718. #undef malloc
  1719. #undef free
  1720. #undef printf
  1721. #include "libavutil/lfg.h"
  1722. #include "libavutil/mathematics.h"
  1723. int main(void){
  1724. #define width 256
  1725. #define height 256
  1726. int buffer[2][width*height];
  1727. SnowContext s;
  1728. int i;
  1729. AVLFG prng;
  1730. s.spatial_decomposition_count=6;
  1731. s.spatial_decomposition_type=1;
  1732. s.temp_dwt_buffer = av_mallocz_array(width, sizeof(DWTELEM));
  1733. s.temp_idwt_buffer = av_mallocz_array(width, sizeof(IDWTELEM));
  1734. if (!s.temp_dwt_buffer || !s.temp_idwt_buffer) {
  1735. fprintf(stderr, "Failed to allocate memory\n");
  1736. return 1;
  1737. }
  1738. av_lfg_init(&prng, 1);
  1739. printf("testing 5/3 DWT\n");
  1740. for(i=0; i<width*height; i++)
  1741. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  1742. ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1743. ff_spatial_idwt((IDWTELEM*)buffer[0], s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1744. for(i=0; i<width*height; i++)
  1745. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  1746. printf("testing 9/7 DWT\n");
  1747. s.spatial_decomposition_type=0;
  1748. for(i=0; i<width*height; i++)
  1749. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  1750. ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1751. ff_spatial_idwt((IDWTELEM*)buffer[0], s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1752. for(i=0; i<width*height; i++)
  1753. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  1754. {
  1755. int level, orientation, x, y;
  1756. int64_t errors[8][4];
  1757. int64_t g=0;
  1758. memset(errors, 0, sizeof(errors));
  1759. s.spatial_decomposition_count=3;
  1760. s.spatial_decomposition_type=0;
  1761. for(level=0; level<s.spatial_decomposition_count; level++){
  1762. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1763. int w= width >> (s.spatial_decomposition_count-level);
  1764. int h= height >> (s.spatial_decomposition_count-level);
  1765. int stride= width << (s.spatial_decomposition_count-level);
  1766. DWTELEM *buf= buffer[0];
  1767. int64_t error=0;
  1768. if(orientation&1) buf+=w;
  1769. if(orientation>1) buf+=stride>>1;
  1770. memset(buffer[0], 0, sizeof(int)*width*height);
  1771. buf[w/2 + h/2*stride]= 256*256;
  1772. ff_spatial_idwt((IDWTELEM*)buffer[0], s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1773. for(y=0; y<height; y++){
  1774. for(x=0; x<width; x++){
  1775. int64_t d= buffer[0][x + y*width];
  1776. error += d*d;
  1777. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  1778. }
  1779. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  1780. }
  1781. error= (int)(sqrt(error)+0.5);
  1782. errors[level][orientation]= error;
  1783. if(g) g=av_gcd(g, error);
  1784. else g= error;
  1785. }
  1786. }
  1787. printf("static int const visual_weight[][4]={\n");
  1788. for(level=0; level<s.spatial_decomposition_count; level++){
  1789. printf(" {");
  1790. for(orientation=0; orientation<4; orientation++){
  1791. printf("%8"PRId64",", errors[level][orientation]/g);
  1792. }
  1793. printf("},\n");
  1794. }
  1795. printf("};\n");
  1796. {
  1797. int level=2;
  1798. int w= width >> (s.spatial_decomposition_count-level);
  1799. //int h= height >> (s.spatial_decomposition_count-level);
  1800. int stride= width << (s.spatial_decomposition_count-level);
  1801. DWTELEM *buf= buffer[0];
  1802. int64_t error=0;
  1803. buf+=w;
  1804. buf+=stride>>1;
  1805. memset(buffer[0], 0, sizeof(int)*width*height);
  1806. for(y=0; y<height; y++){
  1807. for(x=0; x<width; x++){
  1808. int tab[4]={0,2,3,1};
  1809. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  1810. }
  1811. }
  1812. ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1813. for(y=0; y<height; y++){
  1814. for(x=0; x<width; x++){
  1815. int64_t d= buffer[0][x + y*width];
  1816. error += d*d;
  1817. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  1818. }
  1819. if(FFABS(height/2-y)<9) printf("\n");
  1820. }
  1821. }
  1822. }
  1823. return 0;
  1824. }
  1825. #endif /* TEST */