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.

2106 lines
81KB

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