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.

2082 lines
80KB

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