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.

1931 lines
74KB

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