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.

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