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.

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