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.

2050 lines
78KB

  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, 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, 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, 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. s->chroma_h_shift= 1;
  203. s->chroma_v_shift= 1;
  204. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  205. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  206. s->avctx->get_buffer(s->avctx, &s->input_picture);
  207. if(s->avctx->me_method == ME_ITER){
  208. int i;
  209. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  210. for(i=0; i<s->max_ref_frames; i++){
  211. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  212. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  213. }
  214. }
  215. s->runs = av_malloc(avctx->width * avctx->height * sizeof(*s->runs));
  216. return 0;
  217. }
  218. //near copy & paste from dsputil, FIXME
  219. static int pix_sum(uint8_t * pix, int line_size, int w)
  220. {
  221. int s, i, j;
  222. s = 0;
  223. for (i = 0; i < w; 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 + y*uvstride)*block_w/2,
  286. s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
  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);
  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>>2;
  392. sum = pix_sum(current_data[1], uvstride, block_w>>1);
  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>>1);
  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/2 : block_size;
  508. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+1] : ff_obmc_tab[s->block_max_depth];
  509. const int obmc_stride= plane_index ? block_size : 2*block_size;
  510. const int ref_stride= s->current_picture.linesize[plane_index];
  511. uint8_t *src= s-> input_picture.data[plane_index];
  512. IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  513. const int b_stride = s->b_width << s->block_max_depth;
  514. const int w= p->width;
  515. const int h= p->height;
  516. int index= mb_x + mb_y*b_stride;
  517. BlockNode *b= &s->block[index];
  518. BlockNode backup= *b;
  519. int ab=0;
  520. int aa=0;
  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_w*mb_y2 + block_w/2;
  529. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  530. x, y, block_w, block_w, 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_w); 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_w*mb_y - block_w/2))*obmc_stride;
  534. int obmc_v= obmc[index];
  535. int d;
  536. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  537. if(x<0) obmc_v += obmc[index + block_w];
  538. if(y+block_w>h) obmc_v += obmc[index - block_w*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/2 : block_size;
  591. const int obmc_stride= plane_index ? block_size : 2*block_size;
  592. const int ref_stride= s->current_picture.linesize[plane_index];
  593. uint8_t *dst= s->current_picture.data[plane_index];
  594. uint8_t *src= s-> input_picture.data[plane_index];
  595. IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  596. uint8_t *cur = s->scratchbuf;
  597. uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
  598. const int b_stride = s->b_width << s->block_max_depth;
  599. const int b_height = s->b_height<< s->block_max_depth;
  600. const int w= p->width;
  601. const int h= p->height;
  602. int distortion;
  603. int rate= 0;
  604. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  605. int sx= block_w*mb_x - block_w/2;
  606. int sy= block_w*mb_y - block_w/2;
  607. int x0= FFMAX(0,-sx);
  608. int y0= FFMAX(0,-sy);
  609. int x1= FFMIN(block_w*2, w-sx);
  610. int y1= FFMIN(block_w*2, h-sy);
  611. int i,x,y;
  612. ff_snow_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
  613. for(y=y0; y<y1; y++){
  614. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  615. const IDWTELEM *pred1 = pred + y*obmc_stride;
  616. uint8_t *cur1 = cur + y*ref_stride;
  617. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  618. for(x=x0; x<x1; x++){
  619. #if FRAC_BITS >= LOG2_OBMC_MAX
  620. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  621. #else
  622. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  623. #endif
  624. v = (v + pred1[x]) >> FRAC_BITS;
  625. if(v&(~255)) v= ~(v>>31);
  626. dst1[x] = v;
  627. }
  628. }
  629. /* copy the regions where obmc[] = (uint8_t)256 */
  630. if(LOG2_OBMC_MAX == 8
  631. && (mb_x == 0 || mb_x == b_stride-1)
  632. && (mb_y == 0 || mb_y == b_height-1)){
  633. if(mb_x == 0)
  634. x1 = block_w;
  635. else
  636. x0 = block_w;
  637. if(mb_y == 0)
  638. y1 = block_w;
  639. else
  640. y0 = block_w;
  641. for(y=y0; y<y1; y++)
  642. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  643. }
  644. if(block_w==16){
  645. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  646. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  647. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  648. * So improving the score of one block is not strictly guaranteed
  649. * to improve the score of the whole frame, thus iterative motion
  650. * estimation does not always converge. */
  651. if(s->avctx->me_cmp == FF_CMP_W97)
  652. distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  653. else if(s->avctx->me_cmp == FF_CMP_W53)
  654. distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  655. else{
  656. distortion = 0;
  657. for(i=0; i<4; i++){
  658. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  659. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  660. }
  661. }
  662. }else{
  663. assert(block_w==8);
  664. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  665. }
  666. if(plane_index==0){
  667. for(i=0; i<4; i++){
  668. /* ..RRr
  669. * .RXx.
  670. * rxx..
  671. */
  672. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  673. }
  674. if(mb_x == b_stride-2)
  675. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  676. }
  677. return distortion + rate*penalty_factor;
  678. }
  679. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  680. int i, y2;
  681. Plane *p= &s->plane[plane_index];
  682. const int block_size = MB_SIZE >> s->block_max_depth;
  683. const int block_w = plane_index ? block_size/2 : block_size;
  684. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+1] : ff_obmc_tab[s->block_max_depth];
  685. const int obmc_stride= plane_index ? block_size : 2*block_size;
  686. const int ref_stride= s->current_picture.linesize[plane_index];
  687. uint8_t *dst= s->current_picture.data[plane_index];
  688. uint8_t *src= s-> input_picture.data[plane_index];
  689. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  690. // const has only been removed from zero_dst to suppress a warning
  691. static IDWTELEM zero_dst[4096]; //FIXME
  692. const int b_stride = s->b_width << s->block_max_depth;
  693. const int w= p->width;
  694. const int h= p->height;
  695. int distortion= 0;
  696. int rate= 0;
  697. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  698. for(i=0; i<9; i++){
  699. int mb_x2= mb_x + (i%3) - 1;
  700. int mb_y2= mb_y + (i/3) - 1;
  701. int x= block_w*mb_x2 + block_w/2;
  702. int y= block_w*mb_y2 + block_w/2;
  703. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  704. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  705. //FIXME find a cleaner/simpler way to skip the outside stuff
  706. for(y2= y; y2<0; y2++)
  707. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  708. for(y2= h; y2<y+block_w; y2++)
  709. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  710. if(x<0){
  711. for(y2= y; y2<y+block_w; y2++)
  712. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  713. }
  714. if(x+block_w > w){
  715. for(y2= y; y2<y+block_w; y2++)
  716. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  717. }
  718. assert(block_w== 8 || block_w==16);
  719. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  720. }
  721. if(plane_index==0){
  722. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  723. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  724. /* ..RRRr
  725. * .RXXx.
  726. * .RXXx.
  727. * rxxx.
  728. */
  729. if(merged)
  730. rate = get_block_bits(s, mb_x, mb_y, 2);
  731. for(i=merged?4:0; i<9; i++){
  732. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  733. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  734. }
  735. }
  736. return distortion + rate*penalty_factor;
  737. }
  738. static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
  739. const int w= b->width;
  740. const int h= b->height;
  741. int x, y;
  742. if(1){
  743. int run=0;
  744. int run_index=0;
  745. int max_index;
  746. for(y=0; y<h; y++){
  747. for(x=0; x<w; x++){
  748. int v, p=0;
  749. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  750. v= src[x + y*stride];
  751. if(y){
  752. t= src[x + (y-1)*stride];
  753. if(x){
  754. lt= src[x - 1 + (y-1)*stride];
  755. }
  756. if(x + 1 < w){
  757. rt= src[x + 1 + (y-1)*stride];
  758. }
  759. }
  760. if(x){
  761. l= src[x - 1 + y*stride];
  762. /*if(x > 1){
  763. if(orientation==1) ll= src[y + (x-2)*stride];
  764. else ll= src[x - 2 + y*stride];
  765. }*/
  766. }
  767. if(parent){
  768. int px= x>>1;
  769. int py= y>>1;
  770. if(px<b->parent->width && py<b->parent->height)
  771. p= parent[px + py*2*stride];
  772. }
  773. if(!(/*ll|*/l|lt|t|rt|p)){
  774. if(v){
  775. s->runs[run_index++]= run;
  776. run=0;
  777. }else{
  778. run++;
  779. }
  780. }
  781. }
  782. }
  783. max_index= run_index;
  784. s->runs[run_index++]= run;
  785. run_index=0;
  786. run= s->runs[run_index++];
  787. put_symbol2(&s->c, b->state[30], max_index, 0);
  788. if(run_index <= max_index)
  789. put_symbol2(&s->c, b->state[1], run, 3);
  790. for(y=0; y<h; y++){
  791. if(s->c.bytestream_end - s->c.bytestream < w*40){
  792. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  793. return -1;
  794. }
  795. for(x=0; x<w; x++){
  796. int v, p=0;
  797. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  798. v= src[x + y*stride];
  799. if(y){
  800. t= src[x + (y-1)*stride];
  801. if(x){
  802. lt= src[x - 1 + (y-1)*stride];
  803. }
  804. if(x + 1 < w){
  805. rt= src[x + 1 + (y-1)*stride];
  806. }
  807. }
  808. if(x){
  809. l= src[x - 1 + y*stride];
  810. /*if(x > 1){
  811. if(orientation==1) ll= src[y + (x-2)*stride];
  812. else ll= src[x - 2 + y*stride];
  813. }*/
  814. }
  815. if(parent){
  816. int px= x>>1;
  817. int py= y>>1;
  818. if(px<b->parent->width && py<b->parent->height)
  819. p= parent[px + py*2*stride];
  820. }
  821. if(/*ll|*/l|lt|t|rt|p){
  822. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  823. put_rac(&s->c, &b->state[0][context], !!v);
  824. }else{
  825. if(!run){
  826. run= s->runs[run_index++];
  827. if(run_index <= max_index)
  828. put_symbol2(&s->c, b->state[1], run, 3);
  829. assert(v);
  830. }else{
  831. run--;
  832. assert(!v);
  833. }
  834. }
  835. if(v){
  836. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  837. int l2= 2*FFABS(l) + (l<0);
  838. int t2= 2*FFABS(t) + (t<0);
  839. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  840. put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
  841. }
  842. }
  843. }
  844. }
  845. return 0;
  846. }
  847. static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
  848. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  849. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  850. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  851. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  852. }
  853. 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){
  854. const int b_stride= s->b_width << s->block_max_depth;
  855. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  856. BlockNode backup= *block;
  857. unsigned value;
  858. int rd, index;
  859. assert(mb_x>=0 && mb_y>=0);
  860. assert(mb_x<b_stride);
  861. if(intra){
  862. block->color[0] = p[0];
  863. block->color[1] = p[1];
  864. block->color[2] = p[2];
  865. block->type |= BLOCK_INTRA;
  866. }else{
  867. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  868. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  869. if(s->me_cache[index] == value)
  870. return 0;
  871. s->me_cache[index]= value;
  872. block->mx= p[0];
  873. block->my= p[1];
  874. block->type &= ~BLOCK_INTRA;
  875. }
  876. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  877. //FIXME chroma
  878. if(rd < *best_rd){
  879. *best_rd= rd;
  880. return 1;
  881. }else{
  882. *block= backup;
  883. return 0;
  884. }
  885. }
  886. /* special case for int[2] args we discard afterwards,
  887. * fixes compilation problem with gcc 2.95 */
  888. 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){
  889. int p[2] = {p0, p1};
  890. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  891. }
  892. 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){
  893. const int b_stride= s->b_width << s->block_max_depth;
  894. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  895. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  896. unsigned value;
  897. int rd, index;
  898. assert(mb_x>=0 && mb_y>=0);
  899. assert(mb_x<b_stride);
  900. assert(((mb_x|mb_y)&1) == 0);
  901. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  902. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  903. if(s->me_cache[index] == value)
  904. return 0;
  905. s->me_cache[index]= value;
  906. block->mx= p0;
  907. block->my= p1;
  908. block->ref= ref;
  909. block->type &= ~BLOCK_INTRA;
  910. block[1]= block[b_stride]= block[b_stride+1]= *block;
  911. rd= get_4block_rd(s, mb_x, mb_y, 0);
  912. //FIXME chroma
  913. if(rd < *best_rd){
  914. *best_rd= rd;
  915. return 1;
  916. }else{
  917. block[0]= backup[0];
  918. block[1]= backup[1];
  919. block[b_stride]= backup[2];
  920. block[b_stride+1]= backup[3];
  921. return 0;
  922. }
  923. }
  924. static void iterative_me(SnowContext *s){
  925. int pass, mb_x, mb_y;
  926. const int b_width = s->b_width << s->block_max_depth;
  927. const int b_height= s->b_height << s->block_max_depth;
  928. const int b_stride= b_width;
  929. int color[3];
  930. {
  931. RangeCoder r = s->c;
  932. uint8_t state[sizeof(s->block_state)];
  933. memcpy(state, s->block_state, sizeof(s->block_state));
  934. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  935. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  936. encode_q_branch(s, 0, mb_x, mb_y);
  937. s->c = r;
  938. memcpy(s->block_state, state, sizeof(s->block_state));
  939. }
  940. for(pass=0; pass<25; pass++){
  941. int change= 0;
  942. for(mb_y= 0; mb_y<b_height; mb_y++){
  943. for(mb_x= 0; mb_x<b_width; mb_x++){
  944. int dia_change, i, j, ref;
  945. int best_rd= INT_MAX, ref_rd;
  946. BlockNode backup, ref_b;
  947. const int index= mb_x + mb_y * b_stride;
  948. BlockNode *block= &s->block[index];
  949. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  950. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  951. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  952. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  953. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  954. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  955. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  956. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  957. const int b_w= (MB_SIZE >> s->block_max_depth);
  958. uint8_t obmc_edged[b_w*2][b_w*2];
  959. if(pass && (block->type & BLOCK_OPT))
  960. continue;
  961. block->type |= BLOCK_OPT;
  962. backup= *block;
  963. if(!s->me_cache_generation)
  964. memset(s->me_cache, 0, sizeof(s->me_cache));
  965. s->me_cache_generation += 1<<22;
  966. //FIXME precalculate
  967. {
  968. int x, y;
  969. memcpy(obmc_edged, ff_obmc_tab[s->block_max_depth], b_w*b_w*4);
  970. if(mb_x==0)
  971. for(y=0; y<b_w*2; y++)
  972. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  973. if(mb_x==b_stride-1)
  974. for(y=0; y<b_w*2; y++)
  975. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  976. if(mb_y==0){
  977. for(x=0; x<b_w*2; x++)
  978. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  979. for(y=1; y<b_w; y++)
  980. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  981. }
  982. if(mb_y==b_height-1){
  983. for(x=0; x<b_w*2; x++)
  984. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  985. for(y=b_w; y<b_w*2-1; y++)
  986. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  987. }
  988. }
  989. //skip stuff outside the picture
  990. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  991. uint8_t *src= s-> input_picture.data[0];
  992. uint8_t *dst= s->current_picture.data[0];
  993. const int stride= s->current_picture.linesize[0];
  994. const int block_w= MB_SIZE >> s->block_max_depth;
  995. const int sx= block_w*mb_x - block_w/2;
  996. const int sy= block_w*mb_y - block_w/2;
  997. const int w= s->plane[0].width;
  998. const int h= s->plane[0].height;
  999. int y;
  1000. for(y=sy; y<0; y++)
  1001. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  1002. for(y=h; y<sy+block_w*2; y++)
  1003. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  1004. if(sx<0){
  1005. for(y=sy; y<sy+block_w*2; y++)
  1006. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  1007. }
  1008. if(sx+block_w*2 > w){
  1009. for(y=sy; y<sy+block_w*2; y++)
  1010. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  1011. }
  1012. }
  1013. // intra(black) = neighbors' contribution to the current block
  1014. for(i=0; i<3; i++)
  1015. color[i]= get_dc(s, mb_x, mb_y, i);
  1016. // get previous score (cannot be cached due to OBMC)
  1017. if(pass > 0 && (block->type&BLOCK_INTRA)){
  1018. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  1019. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  1020. }else
  1021. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  1022. ref_b= *block;
  1023. ref_rd= best_rd;
  1024. for(ref=0; ref < s->ref_frames; ref++){
  1025. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  1026. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  1027. continue;
  1028. block->ref= ref;
  1029. best_rd= INT_MAX;
  1030. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  1031. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  1032. if(tb)
  1033. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  1034. if(lb)
  1035. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  1036. if(rb)
  1037. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  1038. if(bb)
  1039. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  1040. /* fullpel ME */
  1041. //FIXME avoid subpel interpolation / round to nearest integer
  1042. do{
  1043. dia_change=0;
  1044. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  1045. for(j=0; j<i; j++){
  1046. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  1047. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  1048. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  1049. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  1050. }
  1051. }
  1052. }while(dia_change);
  1053. /* subpel ME */
  1054. do{
  1055. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  1056. dia_change=0;
  1057. for(i=0; i<8; i++)
  1058. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  1059. }while(dia_change);
  1060. //FIXME or try the standard 2 pass qpel or similar
  1061. mvr[0][0]= block->mx;
  1062. mvr[0][1]= block->my;
  1063. if(ref_rd > best_rd){
  1064. ref_rd= best_rd;
  1065. ref_b= *block;
  1066. }
  1067. }
  1068. best_rd= ref_rd;
  1069. *block= ref_b;
  1070. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  1071. //FIXME RD style color selection
  1072. if(!same_block(block, &backup)){
  1073. if(tb ) tb ->type &= ~BLOCK_OPT;
  1074. if(lb ) lb ->type &= ~BLOCK_OPT;
  1075. if(rb ) rb ->type &= ~BLOCK_OPT;
  1076. if(bb ) bb ->type &= ~BLOCK_OPT;
  1077. if(tlb) tlb->type &= ~BLOCK_OPT;
  1078. if(trb) trb->type &= ~BLOCK_OPT;
  1079. if(blb) blb->type &= ~BLOCK_OPT;
  1080. if(brb) brb->type &= ~BLOCK_OPT;
  1081. change ++;
  1082. }
  1083. }
  1084. }
  1085. av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  1086. if(!change)
  1087. break;
  1088. }
  1089. if(s->block_max_depth == 1){
  1090. int change= 0;
  1091. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  1092. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  1093. int i;
  1094. int best_rd, init_rd;
  1095. const int index= mb_x + mb_y * b_stride;
  1096. BlockNode *b[4];
  1097. b[0]= &s->block[index];
  1098. b[1]= b[0]+1;
  1099. b[2]= b[0]+b_stride;
  1100. b[3]= b[2]+1;
  1101. if(same_block(b[0], b[1]) &&
  1102. same_block(b[0], b[2]) &&
  1103. same_block(b[0], b[3]))
  1104. continue;
  1105. if(!s->me_cache_generation)
  1106. memset(s->me_cache, 0, sizeof(s->me_cache));
  1107. s->me_cache_generation += 1<<22;
  1108. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  1109. //FIXME more multiref search?
  1110. check_4block_inter(s, mb_x, mb_y,
  1111. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  1112. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  1113. for(i=0; i<4; i++)
  1114. if(!(b[i]->type&BLOCK_INTRA))
  1115. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  1116. if(init_rd != best_rd)
  1117. change++;
  1118. }
  1119. }
  1120. av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  1121. }
  1122. }
  1123. static void encode_blocks(SnowContext *s, int search){
  1124. int x, y;
  1125. int w= s->b_width;
  1126. int h= s->b_height;
  1127. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  1128. iterative_me(s);
  1129. for(y=0; y<h; y++){
  1130. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  1131. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1132. return;
  1133. }
  1134. for(x=0; x<w; x++){
  1135. if(s->avctx->me_method == ME_ITER || !search)
  1136. encode_q_branch2(s, 0, x, y);
  1137. else
  1138. encode_q_branch (s, 0, x, y);
  1139. }
  1140. }
  1141. }
  1142. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  1143. const int w= b->width;
  1144. const int h= b->height;
  1145. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1146. const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  1147. int x,y, thres1, thres2;
  1148. if(s->qlog == LOSSLESS_QLOG){
  1149. for(y=0; y<h; y++)
  1150. for(x=0; x<w; x++)
  1151. dst[x + y*stride]= src[x + y*stride];
  1152. return;
  1153. }
  1154. bias= bias ? 0 : (3*qmul)>>3;
  1155. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  1156. thres2= 2*thres1;
  1157. if(!bias){
  1158. for(y=0; y<h; y++){
  1159. for(x=0; x<w; x++){
  1160. int i= src[x + y*stride];
  1161. if((unsigned)(i+thres1) > thres2){
  1162. if(i>=0){
  1163. i<<= QEXPSHIFT;
  1164. i/= qmul; //FIXME optimize
  1165. dst[x + y*stride]= i;
  1166. }else{
  1167. i= -i;
  1168. i<<= QEXPSHIFT;
  1169. i/= qmul; //FIXME optimize
  1170. dst[x + y*stride]= -i;
  1171. }
  1172. }else
  1173. dst[x + y*stride]= 0;
  1174. }
  1175. }
  1176. }else{
  1177. for(y=0; y<h; y++){
  1178. for(x=0; x<w; x++){
  1179. int i= src[x + y*stride];
  1180. if((unsigned)(i+thres1) > thres2){
  1181. if(i>=0){
  1182. i<<= QEXPSHIFT;
  1183. i= (i + bias) / qmul; //FIXME optimize
  1184. dst[x + y*stride]= i;
  1185. }else{
  1186. i= -i;
  1187. i<<= QEXPSHIFT;
  1188. i= (i + bias) / qmul; //FIXME optimize
  1189. dst[x + y*stride]= -i;
  1190. }
  1191. }else
  1192. dst[x + y*stride]= 0;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  1198. const int w= b->width;
  1199. const int h= b->height;
  1200. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1201. const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1202. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1203. int x,y;
  1204. if(s->qlog == LOSSLESS_QLOG) return;
  1205. for(y=0; y<h; y++){
  1206. for(x=0; x<w; x++){
  1207. int i= src[x + y*stride];
  1208. if(i<0){
  1209. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  1210. }else if(i>0){
  1211. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  1212. }
  1213. }
  1214. }
  1215. }
  1216. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  1217. const int w= b->width;
  1218. const int h= b->height;
  1219. int x,y;
  1220. for(y=h-1; y>=0; y--){
  1221. for(x=w-1; x>=0; x--){
  1222. int i= x + y*stride;
  1223. if(x){
  1224. if(use_median){
  1225. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  1226. else src[i] -= src[i - 1];
  1227. }else{
  1228. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  1229. else src[i] -= src[i - 1];
  1230. }
  1231. }else{
  1232. if(y) src[i] -= src[i - stride];
  1233. }
  1234. }
  1235. }
  1236. }
  1237. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  1238. const int w= b->width;
  1239. const int h= b->height;
  1240. int x,y;
  1241. for(y=0; y<h; y++){
  1242. for(x=0; x<w; x++){
  1243. int i= x + y*stride;
  1244. if(x){
  1245. if(use_median){
  1246. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  1247. else src[i] += src[i - 1];
  1248. }else{
  1249. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  1250. else src[i] += src[i - 1];
  1251. }
  1252. }else{
  1253. if(y) src[i] += src[i - stride];
  1254. }
  1255. }
  1256. }
  1257. }
  1258. static void encode_qlogs(SnowContext *s){
  1259. int plane_index, level, orientation;
  1260. for(plane_index=0; plane_index<2; plane_index++){
  1261. for(level=0; level<s->spatial_decomposition_count; level++){
  1262. for(orientation=level ? 1:0; orientation<4; orientation++){
  1263. if(orientation==2) continue;
  1264. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  1265. }
  1266. }
  1267. }
  1268. }
  1269. static void encode_header(SnowContext *s){
  1270. int plane_index, i;
  1271. uint8_t kstate[32];
  1272. memset(kstate, MID_STATE, sizeof(kstate));
  1273. put_rac(&s->c, kstate, s->keyframe);
  1274. if(s->keyframe || s->always_reset){
  1275. ff_snow_reset_contexts(s);
  1276. s->last_spatial_decomposition_type=
  1277. s->last_qlog=
  1278. s->last_qbias=
  1279. s->last_mv_scale=
  1280. s->last_block_max_depth= 0;
  1281. for(plane_index=0; plane_index<2; plane_index++){
  1282. Plane *p= &s->plane[plane_index];
  1283. p->last_htaps=0;
  1284. p->last_diag_mc=0;
  1285. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  1286. }
  1287. }
  1288. if(s->keyframe){
  1289. put_symbol(&s->c, s->header_state, s->version, 0);
  1290. put_rac(&s->c, s->header_state, s->always_reset);
  1291. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  1292. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  1293. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  1294. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  1295. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  1296. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  1297. put_rac(&s->c, s->header_state, s->spatial_scalability);
  1298. // put_rac(&s->c, s->header_state, s->rate_scalability);
  1299. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  1300. encode_qlogs(s);
  1301. }
  1302. if(!s->keyframe){
  1303. int update_mc=0;
  1304. for(plane_index=0; plane_index<2; plane_index++){
  1305. Plane *p= &s->plane[plane_index];
  1306. update_mc |= p->last_htaps != p->htaps;
  1307. update_mc |= p->last_diag_mc != p->diag_mc;
  1308. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  1309. }
  1310. put_rac(&s->c, s->header_state, update_mc);
  1311. if(update_mc){
  1312. for(plane_index=0; plane_index<2; plane_index++){
  1313. Plane *p= &s->plane[plane_index];
  1314. put_rac(&s->c, s->header_state, p->diag_mc);
  1315. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  1316. for(i= p->htaps/2; i; i--)
  1317. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  1318. }
  1319. }
  1320. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  1321. put_rac(&s->c, s->header_state, 1);
  1322. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  1323. encode_qlogs(s);
  1324. }else
  1325. put_rac(&s->c, s->header_state, 0);
  1326. }
  1327. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  1328. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  1329. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  1330. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  1331. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  1332. }
  1333. static void update_last_header_values(SnowContext *s){
  1334. int plane_index;
  1335. if(!s->keyframe){
  1336. for(plane_index=0; plane_index<2; plane_index++){
  1337. Plane *p= &s->plane[plane_index];
  1338. p->last_diag_mc= p->diag_mc;
  1339. p->last_htaps = p->htaps;
  1340. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  1341. }
  1342. }
  1343. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  1344. s->last_qlog = s->qlog;
  1345. s->last_qbias = s->qbias;
  1346. s->last_mv_scale = s->mv_scale;
  1347. s->last_block_max_depth = s->block_max_depth;
  1348. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  1349. }
  1350. static int qscale2qlog(int qscale){
  1351. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  1352. + 61*QROOT/8; ///< 64 > 60
  1353. }
  1354. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  1355. {
  1356. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  1357. * FIXME we know exact mv bits at this point,
  1358. * but ratecontrol isn't set up to include them. */
  1359. uint32_t coef_sum= 0;
  1360. int level, orientation, delta_qlog;
  1361. for(level=0; level<s->spatial_decomposition_count; level++){
  1362. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1363. SubBand *b= &s->plane[0].band[level][orientation];
  1364. IDWTELEM *buf= b->ibuf;
  1365. const int w= b->width;
  1366. const int h= b->height;
  1367. const int stride= b->stride;
  1368. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  1369. const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1370. const int qdiv= (1<<16)/qmul;
  1371. int x, y;
  1372. //FIXME this is ugly
  1373. for(y=0; y<h; y++)
  1374. for(x=0; x<w; x++)
  1375. buf[x+y*stride]= b->buf[x+y*stride];
  1376. if(orientation==0)
  1377. decorrelate(s, b, buf, stride, 1, 0);
  1378. for(y=0; y<h; y++)
  1379. for(x=0; x<w; x++)
  1380. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  1381. }
  1382. }
  1383. /* ugly, ratecontrol just takes a sqrt again */
  1384. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  1385. assert(coef_sum < INT_MAX);
  1386. if(pict->pict_type == AV_PICTURE_TYPE_I){
  1387. s->m.current_picture.mb_var_sum= coef_sum;
  1388. s->m.current_picture.mc_mb_var_sum= 0;
  1389. }else{
  1390. s->m.current_picture.mc_mb_var_sum= coef_sum;
  1391. s->m.current_picture.mb_var_sum= 0;
  1392. }
  1393. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  1394. if (pict->quality < 0)
  1395. return INT_MIN;
  1396. s->lambda= pict->quality * 3/2;
  1397. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  1398. s->qlog+= delta_qlog;
  1399. return delta_qlog;
  1400. }
  1401. static void calculate_visual_weight(SnowContext *s, Plane *p){
  1402. int width = p->width;
  1403. int height= p->height;
  1404. int level, orientation, x, y;
  1405. for(level=0; level<s->spatial_decomposition_count; level++){
  1406. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1407. SubBand *b= &p->band[level][orientation];
  1408. IDWTELEM *ibuf= b->ibuf;
  1409. int64_t error=0;
  1410. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  1411. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  1412. ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1413. for(y=0; y<height; y++){
  1414. for(x=0; x<width; x++){
  1415. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  1416. error += d*d;
  1417. }
  1418. }
  1419. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  1420. }
  1421. }
  1422. }
  1423. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1424. const AVFrame *pict, int *got_packet)
  1425. {
  1426. SnowContext *s = avctx->priv_data;
  1427. RangeCoder * const c= &s->c;
  1428. AVFrame *pic = &s->new_picture;
  1429. const int width= s->avctx->width;
  1430. const int height= s->avctx->height;
  1431. int level, orientation, plane_index, i, y, ret;
  1432. uint8_t rc_header_bak[sizeof(s->header_state)];
  1433. uint8_t rc_block_bak[sizeof(s->block_state)];
  1434. if ((ret = ff_alloc_packet2(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + FF_MIN_BUFFER_SIZE)) < 0)
  1435. return ret;
  1436. ff_init_range_encoder(c, pkt->data, pkt->size);
  1437. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1438. for(i=0; i<3; i++){
  1439. int shift= !!i;
  1440. for(y=0; y<(height>>shift); y++)
  1441. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  1442. &pict->data[i][y * pict->linesize[i]],
  1443. width>>shift);
  1444. }
  1445. s->new_picture = *pict;
  1446. s->m.picture_number= avctx->frame_number;
  1447. if(avctx->flags&CODEC_FLAG_PASS2){
  1448. s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  1449. s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
  1450. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  1451. pic->quality = ff_rate_estimate_qscale(&s->m, 0);
  1452. if (pic->quality < 0)
  1453. return -1;
  1454. }
  1455. }else{
  1456. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  1457. s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1458. }
  1459. if(s->pass1_rc && avctx->frame_number == 0)
  1460. pic->quality = 2*FF_QP2LAMBDA;
  1461. if (pic->quality) {
  1462. s->qlog = qscale2qlog(pic->quality);
  1463. s->lambda = pic->quality * 3/2;
  1464. }
  1465. if (s->qlog < 0 || (!pic->quality && (avctx->flags & CODEC_FLAG_QSCALE))) {
  1466. s->qlog= LOSSLESS_QLOG;
  1467. s->lambda = 0;
  1468. }//else keep previous frame's qlog until after motion estimation
  1469. ff_snow_frame_start(s);
  1470. s->m.current_picture_ptr= &s->m.current_picture;
  1471. s->m.last_picture.f.pts = s->m.current_picture.f.pts;
  1472. s->m.current_picture.f.pts = pict->pts;
  1473. if(pic->pict_type == AV_PICTURE_TYPE_P){
  1474. int block_width = (width +15)>>4;
  1475. int block_height= (height+15)>>4;
  1476. int stride= s->current_picture.linesize[0];
  1477. assert(s->current_picture.data[0]);
  1478. assert(s->last_picture[0].data[0]);
  1479. s->m.avctx= s->avctx;
  1480. s->m.current_picture.f.data[0] = s->current_picture.data[0];
  1481. s->m. last_picture.f.data[0] = s->last_picture[0].data[0];
  1482. s->m. new_picture.f.data[0] = s-> input_picture.data[0];
  1483. s->m. last_picture_ptr= &s->m. last_picture;
  1484. s->m.linesize=
  1485. s->m. last_picture.f.linesize[0] =
  1486. s->m. new_picture.f.linesize[0] =
  1487. s->m.current_picture.f.linesize[0] = stride;
  1488. s->m.uvlinesize= s->current_picture.linesize[1];
  1489. s->m.width = width;
  1490. s->m.height= height;
  1491. s->m.mb_width = block_width;
  1492. s->m.mb_height= block_height;
  1493. s->m.mb_stride= s->m.mb_width+1;
  1494. s->m.b8_stride= 2*s->m.mb_width+1;
  1495. s->m.f_code=1;
  1496. s->m.pict_type = pic->pict_type;
  1497. s->m.me_method= s->avctx->me_method;
  1498. s->m.me.scene_change_score=0;
  1499. s->m.flags= s->avctx->flags;
  1500. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  1501. s->m.out_format= FMT_H263;
  1502. s->m.unrestricted_mv= 1;
  1503. s->m.lambda = s->lambda;
  1504. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  1505. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  1506. s->m.dsp= s->dsp; //move
  1507. ff_init_me(&s->m);
  1508. s->dsp= s->m.dsp;
  1509. }
  1510. if(s->pass1_rc){
  1511. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  1512. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  1513. }
  1514. redo_frame:
  1515. if (pic->pict_type == AV_PICTURE_TYPE_I)
  1516. s->spatial_decomposition_count= 5;
  1517. else
  1518. s->spatial_decomposition_count= 5;
  1519. s->m.pict_type = pic->pict_type;
  1520. s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
  1521. ff_snow_common_init_after_header(avctx);
  1522. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  1523. for(plane_index=0; plane_index<3; plane_index++){
  1524. calculate_visual_weight(s, &s->plane[plane_index]);
  1525. }
  1526. }
  1527. encode_header(s);
  1528. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  1529. encode_blocks(s, 1);
  1530. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  1531. for(plane_index=0; plane_index<3; plane_index++){
  1532. Plane *p= &s->plane[plane_index];
  1533. int w= p->width;
  1534. int h= p->height;
  1535. int x, y;
  1536. // int bits= put_bits_count(&s->c.pb);
  1537. if (!s->memc_only) {
  1538. //FIXME optimize
  1539. if(pict->data[plane_index]) //FIXME gray hack
  1540. for(y=0; y<h; y++){
  1541. for(x=0; x<w; x++){
  1542. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  1543. }
  1544. }
  1545. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  1546. if( plane_index==0
  1547. && pic->pict_type == AV_PICTURE_TYPE_P
  1548. && !(avctx->flags&CODEC_FLAG_PASS2)
  1549. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  1550. ff_init_range_encoder(c, pkt->data, pkt->size);
  1551. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1552. pic->pict_type= AV_PICTURE_TYPE_I;
  1553. s->keyframe=1;
  1554. s->current_picture.key_frame=1;
  1555. goto redo_frame;
  1556. }
  1557. if(s->qlog == LOSSLESS_QLOG){
  1558. for(y=0; y<h; y++){
  1559. for(x=0; x<w; x++){
  1560. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  1561. }
  1562. }
  1563. }else{
  1564. for(y=0; y<h; y++){
  1565. for(x=0; x<w; x++){
  1566. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  1567. }
  1568. }
  1569. }
  1570. /* if(QUANTIZE2)
  1571. dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
  1572. else*/
  1573. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1574. if(s->pass1_rc && plane_index==0){
  1575. int delta_qlog = ratecontrol_1pass(s, pic);
  1576. if (delta_qlog <= INT_MIN)
  1577. return -1;
  1578. if(delta_qlog){
  1579. //reordering qlog in the bitstream would eliminate this reset
  1580. ff_init_range_encoder(c, pkt->data, pkt->size);
  1581. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  1582. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  1583. encode_header(s);
  1584. encode_blocks(s, 0);
  1585. }
  1586. }
  1587. for(level=0; level<s->spatial_decomposition_count; level++){
  1588. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1589. SubBand *b= &p->band[level][orientation];
  1590. if(!QUANTIZE2)
  1591. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  1592. if(orientation==0)
  1593. decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
  1594. if (!s->no_bitstream)
  1595. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  1596. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  1597. if(orientation==0)
  1598. correlate(s, b, b->ibuf, b->stride, 1, 0);
  1599. }
  1600. }
  1601. for(level=0; level<s->spatial_decomposition_count; level++){
  1602. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1603. SubBand *b= &p->band[level][orientation];
  1604. dequantize(s, b, b->ibuf, b->stride);
  1605. }
  1606. }
  1607. ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1608. if(s->qlog == LOSSLESS_QLOG){
  1609. for(y=0; y<h; y++){
  1610. for(x=0; x<w; x++){
  1611. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  1612. }
  1613. }
  1614. }
  1615. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1616. }else{
  1617. //ME/MC only
  1618. if(pic->pict_type == AV_PICTURE_TYPE_I){
  1619. for(y=0; y<h; y++){
  1620. for(x=0; x<w; x++){
  1621. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  1622. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  1623. }
  1624. }
  1625. }else{
  1626. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  1627. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1628. }
  1629. }
  1630. if(s->avctx->flags&CODEC_FLAG_PSNR){
  1631. int64_t error= 0;
  1632. if(pict->data[plane_index]) //FIXME gray hack
  1633. for(y=0; y<h; y++){
  1634. for(x=0; x<w; x++){
  1635. 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];
  1636. error += d*d;
  1637. }
  1638. }
  1639. s->avctx->error[plane_index] += error;
  1640. s->current_picture.error[plane_index] = error;
  1641. }
  1642. }
  1643. update_last_header_values(s);
  1644. ff_snow_release_buffer(avctx);
  1645. s->current_picture.coded_picture_number = avctx->frame_number;
  1646. s->current_picture.pict_type = pict->pict_type;
  1647. s->current_picture.quality = pict->quality;
  1648. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  1649. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  1650. s->m.current_picture.f.display_picture_number =
  1651. s->m.current_picture.f.coded_picture_number = avctx->frame_number;
  1652. s->m.current_picture.f.quality = pic->quality;
  1653. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  1654. if(s->pass1_rc)
  1655. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  1656. return -1;
  1657. if(avctx->flags&CODEC_FLAG_PASS1)
  1658. ff_write_pass1_stats(&s->m);
  1659. s->m.last_pict_type = s->m.pict_type;
  1660. avctx->frame_bits = s->m.frame_bits;
  1661. avctx->mv_bits = s->m.mv_bits;
  1662. avctx->misc_bits = s->m.misc_bits;
  1663. avctx->p_tex_bits = s->m.p_tex_bits;
  1664. emms_c();
  1665. pkt->size = ff_rac_terminate(c);
  1666. if (avctx->coded_frame->key_frame)
  1667. pkt->flags |= AV_PKT_FLAG_KEY;
  1668. *got_packet = 1;
  1669. return 0;
  1670. }
  1671. static av_cold int encode_end(AVCodecContext *avctx)
  1672. {
  1673. SnowContext *s = avctx->priv_data;
  1674. ff_snow_common_end(s);
  1675. if (s->input_picture.data[0])
  1676. avctx->release_buffer(avctx, &s->input_picture);
  1677. av_free(avctx->stats_out);
  1678. av_freep(&s->runs);
  1679. return 0;
  1680. }
  1681. #define OFFSET(x) offsetof(SnowContext, x)
  1682. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1683. static const AVOption options[] = {
  1684. { "memc_only", "Only do ME/MC (I frames -> ref, P frame -> ME+MC).", OFFSET(memc_only), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
  1685. { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
  1686. { NULL },
  1687. };
  1688. static const AVClass snowenc_class = {
  1689. .class_name = "snow encoder",
  1690. .item_name = av_default_item_name,
  1691. .option = options,
  1692. .version = LIBAVUTIL_VERSION_INT,
  1693. };
  1694. AVCodec ff_snow_encoder = {
  1695. .name = "snow",
  1696. .type = AVMEDIA_TYPE_VIDEO,
  1697. .id = CODEC_ID_SNOW,
  1698. .priv_data_size = sizeof(SnowContext),
  1699. .init = encode_init,
  1700. .encode2 = encode_frame,
  1701. .close = encode_end,
  1702. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  1703. .priv_class = &snowenc_class,
  1704. };
  1705. #ifdef TEST
  1706. #undef malloc
  1707. #undef free
  1708. #undef printf
  1709. #include "libavutil/lfg.h"
  1710. #include "libavutil/mathematics.h"
  1711. int main(void){
  1712. int width=256;
  1713. int height=256;
  1714. int buffer[2][width*height];
  1715. SnowContext s;
  1716. int i;
  1717. AVLFG prng;
  1718. s.spatial_decomposition_count=6;
  1719. s.spatial_decomposition_type=1;
  1720. av_lfg_init(&prng, 1);
  1721. printf("testing 5/3 DWT\n");
  1722. for(i=0; i<width*height; i++)
  1723. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  1724. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1725. ff_spatial_idwt((IDWTELEM*)buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1726. for(i=0; i<width*height; i++)
  1727. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  1728. printf("testing 9/7 DWT\n");
  1729. s.spatial_decomposition_type=0;
  1730. for(i=0; i<width*height; i++)
  1731. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  1732. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1733. ff_spatial_idwt((IDWTELEM*)buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1734. for(i=0; i<width*height; i++)
  1735. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  1736. {
  1737. int level, orientation, x, y;
  1738. int64_t errors[8][4];
  1739. int64_t g=0;
  1740. memset(errors, 0, sizeof(errors));
  1741. s.spatial_decomposition_count=3;
  1742. s.spatial_decomposition_type=0;
  1743. for(level=0; level<s.spatial_decomposition_count; level++){
  1744. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1745. int w= width >> (s.spatial_decomposition_count-level);
  1746. int h= height >> (s.spatial_decomposition_count-level);
  1747. int stride= width << (s.spatial_decomposition_count-level);
  1748. DWTELEM *buf= buffer[0];
  1749. int64_t error=0;
  1750. if(orientation&1) buf+=w;
  1751. if(orientation>1) buf+=stride>>1;
  1752. memset(buffer[0], 0, sizeof(int)*width*height);
  1753. buf[w/2 + h/2*stride]= 256*256;
  1754. ff_spatial_idwt((IDWTELEM*)buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1755. for(y=0; y<height; y++){
  1756. for(x=0; x<width; x++){
  1757. int64_t d= buffer[0][x + y*width];
  1758. error += d*d;
  1759. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  1760. }
  1761. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  1762. }
  1763. error= (int)(sqrt(error)+0.5);
  1764. errors[level][orientation]= error;
  1765. if(g) g=av_gcd(g, error);
  1766. else g= error;
  1767. }
  1768. }
  1769. printf("static int const visual_weight[][4]={\n");
  1770. for(level=0; level<s.spatial_decomposition_count; level++){
  1771. printf(" {");
  1772. for(orientation=0; orientation<4; orientation++){
  1773. printf("%8"PRId64",", errors[level][orientation]/g);
  1774. }
  1775. printf("},\n");
  1776. }
  1777. printf("};\n");
  1778. {
  1779. int level=2;
  1780. int w= width >> (s.spatial_decomposition_count-level);
  1781. //int h= height >> (s.spatial_decomposition_count-level);
  1782. int stride= width << (s.spatial_decomposition_count-level);
  1783. DWTELEM *buf= buffer[0];
  1784. int64_t error=0;
  1785. buf+=w;
  1786. buf+=stride>>1;
  1787. memset(buffer[0], 0, sizeof(int)*width*height);
  1788. for(y=0; y<height; y++){
  1789. for(x=0; x<width; x++){
  1790. int tab[4]={0,2,3,1};
  1791. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  1792. }
  1793. }
  1794. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  1795. for(y=0; y<height; y++){
  1796. for(x=0; x<width; x++){
  1797. int64_t d= buffer[0][x + y*width];
  1798. error += d*d;
  1799. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  1800. }
  1801. if(FFABS(height/2-y)<9) printf("\n");
  1802. }
  1803. }
  1804. }
  1805. return 0;
  1806. }
  1807. #endif /* TEST */