FFmpeg
snowenc.c
Go to the documentation of this file.
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 
21 #include "libavutil/intmath.h"
22 #include "libavutil/libm.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avcodec.h"
27 #include "encode.h"
28 #include "internal.h"
29 #include "packet_internal.h"
30 #include "snow_dwt.h"
31 #include "snow.h"
32 
33 #include "rangecoder.h"
34 #include "mathops.h"
35 
36 #include "mpegvideo.h"
37 #include "h263.h"
38 
40 {
41  SnowContext *s = avctx->priv_data;
42  int plane_index, ret;
43  int i;
44 
45  if(s->pred == DWT_97
46  && (avctx->flags & AV_CODEC_FLAG_QSCALE)
47  && avctx->global_quality == 0){
48  av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
49  return AVERROR(EINVAL);
50  }
51 
52  s->spatial_decomposition_type= s->pred; //FIXME add decorrelator type r transform_type
53 
54  s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
55  s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
56 
57  for(plane_index=0; plane_index<3; plane_index++){
58  s->plane[plane_index].diag_mc= 1;
59  s->plane[plane_index].htaps= 6;
60  s->plane[plane_index].hcoeff[0]= 40;
61  s->plane[plane_index].hcoeff[1]= -10;
62  s->plane[plane_index].hcoeff[2]= 2;
63  s->plane[plane_index].fast_mc= 1;
64  }
65 
66  if ((ret = ff_snow_common_init(avctx)) < 0) {
67  return ret;
68  }
69  ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
70 
72 
73  s->version=0;
74 
75  s->m.avctx = avctx;
76  s->m.bit_rate= avctx->bit_rate;
77  s->m.lmin = avctx->mb_lmin;
78  s->m.lmax = avctx->mb_lmax;
79  s->m.mb_num = (avctx->width * avctx->height + 255) / 256; // For ratecontrol
80 
81  s->m.me.temp =
82  s->m.me.scratchpad= av_mallocz_array((avctx->width+64), 2*16*2*sizeof(uint8_t));
83  s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
84  s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
85  s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
86  if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
87  return AVERROR(ENOMEM);
88 
89  ff_h263_encode_init(&s->m); //mv_penalty
90 
91  s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
92 
93  if(avctx->flags&AV_CODEC_FLAG_PASS1){
94  if(!avctx->stats_out)
95  avctx->stats_out = av_mallocz(256);
96 
97  if (!avctx->stats_out)
98  return AVERROR(ENOMEM);
99  }
100  if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&AV_CODEC_FLAG_QSCALE)){
101  ret = ff_rate_control_init(&s->m);
102  if(ret < 0)
103  return ret;
104  }
105  s->pass1_rc= !(avctx->flags & (AV_CODEC_FLAG_QSCALE|AV_CODEC_FLAG_PASS2));
106 
107  switch(avctx->pix_fmt){
108  case AV_PIX_FMT_YUV444P:
109 // case AV_PIX_FMT_YUV422P:
110  case AV_PIX_FMT_YUV420P:
111 // case AV_PIX_FMT_YUV411P:
112  case AV_PIX_FMT_YUV410P:
113  s->nb_planes = 3;
114  s->colorspace_type= 0;
115  break;
116  case AV_PIX_FMT_GRAY8:
117  s->nb_planes = 1;
118  s->colorspace_type = 1;
119  break;
120 /* case AV_PIX_FMT_RGB32:
121  s->colorspace= 1;
122  break;*/
123  default:
124  av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
125  return AVERROR_PATCHWELCOME;
126  }
127 
128  ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
129  &s->chroma_v_shift);
130  if (ret) {
131  av_log(avctx, AV_LOG_ERROR, "pixel format invalid or unknown\n");
132  return ret;
133  }
134 
135  ff_set_cmp(&s->mecc, s->mecc.me_cmp, s->avctx->me_cmp);
136  ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
137 
138  s->input_picture = av_frame_alloc();
139  if (!s->input_picture)
140  return AVERROR(ENOMEM);
141 
142  if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
143  return ret;
144 
145  if(s->motion_est == FF_ME_ITER){
146  int size= s->b_width * s->b_height << 2*s->block_max_depth;
147  for(i=0; i<s->max_ref_frames; i++){
148  s->ref_mvs[i]= av_mallocz_array(size, sizeof(int16_t[2]));
149  s->ref_scores[i]= av_mallocz_array(size, sizeof(uint32_t));
150  if (!s->ref_mvs[i] || !s->ref_scores[i])
151  return AVERROR(ENOMEM);
152  }
153  }
154 
155  return 0;
156 }
157 
158 //near copy & paste from dsputil, FIXME
159 static int pix_sum(uint8_t * pix, int line_size, int w, int h)
160 {
161  int s, i, j;
162 
163  s = 0;
164  for (i = 0; i < h; i++) {
165  for (j = 0; j < w; j++) {
166  s += pix[0];
167  pix ++;
168  }
169  pix += line_size - w;
170  }
171  return s;
172 }
173 
174 //near copy & paste from dsputil, FIXME
175 static int pix_norm1(uint8_t * pix, int line_size, int w)
176 {
177  int s, i, j;
178  const uint32_t *sq = ff_square_tab + 256;
179 
180  s = 0;
181  for (i = 0; i < w; i++) {
182  for (j = 0; j < w; j ++) {
183  s += sq[pix[0]];
184  pix ++;
185  }
186  pix += line_size - w;
187  }
188  return s;
189 }
190 
191 static inline int get_penalty_factor(int lambda, int lambda2, int type){
192  switch(type&0xFF){
193  default:
194  case FF_CMP_SAD:
195  return lambda>>FF_LAMBDA_SHIFT;
196  case FF_CMP_DCT:
197  return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
198  case FF_CMP_W53:
199  return (4*lambda)>>(FF_LAMBDA_SHIFT);
200  case FF_CMP_W97:
201  return (2*lambda)>>(FF_LAMBDA_SHIFT);
202  case FF_CMP_SATD:
203  case FF_CMP_DCT264:
204  return (2*lambda)>>FF_LAMBDA_SHIFT;
205  case FF_CMP_RD:
206  case FF_CMP_PSNR:
207  case FF_CMP_SSE:
208  case FF_CMP_NSSE:
209  return lambda2>>FF_LAMBDA_SHIFT;
210  case FF_CMP_BIT:
211  return 1;
212  }
213 }
214 
215 //FIXME copy&paste
216 #define P_LEFT P[1]
217 #define P_TOP P[2]
218 #define P_TOPRIGHT P[3]
219 #define P_MEDIAN P[4]
220 #define P_MV1 P[9]
221 #define FLAG_QPEL 1 //must be 1
222 
223 static int encode_q_branch(SnowContext *s, int level, int x, int y){
224  uint8_t p_buffer[1024];
225  uint8_t i_buffer[1024];
226  uint8_t p_state[sizeof(s->block_state)];
227  uint8_t i_state[sizeof(s->block_state)];
228  RangeCoder pc, ic;
229  uint8_t *pbbak= s->c.bytestream;
230  uint8_t *pbbak_start= s->c.bytestream_start;
231  int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
232  const int w= s->b_width << s->block_max_depth;
233  const int h= s->b_height << s->block_max_depth;
234  const int rem_depth= s->block_max_depth - level;
235  const int index= (x + y*w) << rem_depth;
236  const int block_w= 1<<(LOG2_MB_SIZE - level);
237  int trx= (x+1)<<rem_depth;
238  int try= (y+1)<<rem_depth;
239  const BlockNode *left = x ? &s->block[index-1] : &null_block;
240  const BlockNode *top = y ? &s->block[index-w] : &null_block;
241  const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
242  const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
243  const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
244  const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
245  int pl = left->color[0];
246  int pcb= left->color[1];
247  int pcr= left->color[2];
248  int pmx, pmy;
249  int mx=0, my=0;
250  int l,cr,cb;
251  const int stride= s->current_picture->linesize[0];
252  const int uvstride= s->current_picture->linesize[1];
253  uint8_t *current_data[3]= { s->input_picture->data[0] + (x + y* stride)*block_w,
254  s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
255  s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
256  int P[10][2];
257  int16_t last_mv[3][2];
258  int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
259  const int shift= 1+qpel;
260  MotionEstContext *c= &s->m.me;
261  int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
262  int mx_context= av_log2(2*FFABS(left->mx - top->mx));
263  int my_context= av_log2(2*FFABS(left->my - top->my));
264  int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
265  int ref, best_ref, ref_score, ref_mx, ref_my;
266 
267  av_assert0(sizeof(s->block_state) >= 256);
268  if(s->keyframe){
269  set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
270  return 0;
271  }
272 
273 // clip predictors / edge ?
274 
275  P_LEFT[0]= left->mx;
276  P_LEFT[1]= left->my;
277  P_TOP [0]= top->mx;
278  P_TOP [1]= top->my;
279  P_TOPRIGHT[0]= tr->mx;
280  P_TOPRIGHT[1]= tr->my;
281 
282  last_mv[0][0]= s->block[index].mx;
283  last_mv[0][1]= s->block[index].my;
284  last_mv[1][0]= right->mx;
285  last_mv[1][1]= right->my;
286  last_mv[2][0]= bottom->mx;
287  last_mv[2][1]= bottom->my;
288 
289  s->m.mb_stride=2;
290  s->m.mb_x=
291  s->m.mb_y= 0;
292  c->skip= 0;
293 
294  av_assert1(c-> stride == stride);
295  av_assert1(c->uvstride == uvstride);
296 
297  c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
298  c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
299  c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
300  c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_DMV;
301 
302  c->xmin = - x*block_w - 16+3;
303  c->ymin = - y*block_w - 16+3;
304  c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
305  c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
306 
307  if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
308  if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
309  if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
310  if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
311  if(P_TOPRIGHT[0] < (c->xmin * (1<<shift))) P_TOPRIGHT[0]= (c->xmin * (1<<shift));
312  if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
313  if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
314 
315  P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
316  P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
317 
318  if (!y) {
319  c->pred_x= P_LEFT[0];
320  c->pred_y= P_LEFT[1];
321  } else {
322  c->pred_x = P_MEDIAN[0];
323  c->pred_y = P_MEDIAN[1];
324  }
325 
326  score= INT_MAX;
327  best_ref= 0;
328  for(ref=0; ref<s->ref_frames; ref++){
329  init_ref(c, current_data, s->last_picture[ref]->data, NULL, block_w*x, block_w*y, 0);
330 
331  ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
332  (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
333 
334  av_assert2(ref_mx >= c->xmin);
335  av_assert2(ref_mx <= c->xmax);
336  av_assert2(ref_my >= c->ymin);
337  av_assert2(ref_my <= c->ymax);
338 
339  ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
340  ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
341  ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
342  if(s->ref_mvs[ref]){
343  s->ref_mvs[ref][index][0]= ref_mx;
344  s->ref_mvs[ref][index][1]= ref_my;
345  s->ref_scores[ref][index]= ref_score;
346  }
347  if(score > ref_score){
348  score= ref_score;
349  best_ref= ref;
350  mx= ref_mx;
351  my= ref_my;
352  }
353  }
354  //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
355 
356  // subpel search
357  base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
358  pc= s->c;
359  pc.bytestream_start=
360  pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
361  memcpy(p_state, s->block_state, sizeof(s->block_state));
362 
363  if(level!=s->block_max_depth)
364  put_rac(&pc, &p_state[4 + s_context], 1);
365  put_rac(&pc, &p_state[1 + left->type + top->type], 0);
366  if(s->ref_frames > 1)
367  put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
368  pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
369  put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
370  put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
371  p_len= pc.bytestream - pc.bytestream_start;
372  score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
373 
374  block_s= block_w*block_w;
375  sum = pix_sum(current_data[0], stride, block_w, block_w);
376  l= (sum + block_s/2)/block_s;
377  iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
378 
379  if (s->nb_planes > 2) {
380  block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
381  sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
382  cb= (sum + block_s/2)/block_s;
383  // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
384  sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
385  cr= (sum + block_s/2)/block_s;
386  // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
387  }else
388  cb = cr = 0;
389 
390  ic= s->c;
391  ic.bytestream_start=
392  ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
393  memcpy(i_state, s->block_state, sizeof(s->block_state));
394  if(level!=s->block_max_depth)
395  put_rac(&ic, &i_state[4 + s_context], 1);
396  put_rac(&ic, &i_state[1 + left->type + top->type], 1);
397  put_symbol(&ic, &i_state[32], l-pl , 1);
398  if (s->nb_planes > 2) {
399  put_symbol(&ic, &i_state[64], cb-pcb, 1);
400  put_symbol(&ic, &i_state[96], cr-pcr, 1);
401  }
402  i_len= ic.bytestream - ic.bytestream_start;
403  iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
404 
405  av_assert1(iscore < 255*255*256 + s->lambda2*10);
406  av_assert1(iscore >= 0);
407  av_assert1(l>=0 && l<=255);
408  av_assert1(pl>=0 && pl<=255);
409 
410  if(level==0){
411  int varc= iscore >> 8;
412  int vard= score >> 8;
413  if (vard <= 64 || vard < varc)
414  c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
415  else
416  c->scene_change_score+= s->m.qscale;
417  }
418 
419  if(level!=s->block_max_depth){
420  put_rac(&s->c, &s->block_state[4 + s_context], 0);
421  score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
422  score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
423  score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
424  score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
425  score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
426 
427  if(score2 < score && score2 < iscore)
428  return score2;
429  }
430 
431  if(iscore < score){
432  pred_mv(s, &pmx, &pmy, 0, left, top, tr);
433  memcpy(pbbak, i_buffer, i_len);
434  s->c= ic;
435  s->c.bytestream_start= pbbak_start;
436  s->c.bytestream= pbbak + i_len;
437  set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
438  memcpy(s->block_state, i_state, sizeof(s->block_state));
439  return iscore;
440  }else{
441  memcpy(pbbak, p_buffer, p_len);
442  s->c= pc;
443  s->c.bytestream_start= pbbak_start;
444  s->c.bytestream= pbbak + p_len;
445  set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
446  memcpy(s->block_state, p_state, sizeof(s->block_state));
447  return score;
448  }
449 }
450 
451 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
452  const int w= s->b_width << s->block_max_depth;
453  const int rem_depth= s->block_max_depth - level;
454  const int index= (x + y*w) << rem_depth;
455  int trx= (x+1)<<rem_depth;
456  BlockNode *b= &s->block[index];
457  const BlockNode *left = x ? &s->block[index-1] : &null_block;
458  const BlockNode *top = y ? &s->block[index-w] : &null_block;
459  const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
460  const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
461  int pl = left->color[0];
462  int pcb= left->color[1];
463  int pcr= left->color[2];
464  int pmx, pmy;
465  int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
466  int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
467  int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
468  int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
469 
470  if(s->keyframe){
471  set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
472  return;
473  }
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  if (s->nb_planes > 2) {
492  put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
493  put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
494  }
495  set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
496  }else{
497  pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
498  put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
499  if(s->ref_frames > 1)
500  put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
501  put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
502  put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
503  set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
504  }
505 }
506 
507 static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
508  int i, x2, y2;
509  Plane *p= &s->plane[plane_index];
510  const int block_size = MB_SIZE >> s->block_max_depth;
511  const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
512  const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
513  const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
514  const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
515  const int ref_stride= s->current_picture->linesize[plane_index];
516  uint8_t *src= s-> input_picture->data[plane_index];
517  IDWTELEM *dst= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
518  const int b_stride = s->b_width << s->block_max_depth;
519  const int w= p->width;
520  const int h= p->height;
521  int index= mb_x + mb_y*b_stride;
522  BlockNode *b= &s->block[index];
523  BlockNode backup= *b;
524  int ab=0;
525  int aa=0;
526 
527  av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
528 
529  b->type|= BLOCK_INTRA;
530  b->color[plane_index]= 0;
531  memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
532 
533  for(i=0; i<4; i++){
534  int mb_x2= mb_x + (i &1) - 1;
535  int mb_y2= mb_y + (i>>1) - 1;
536  int x= block_w*mb_x2 + block_w/2;
537  int y= block_h*mb_y2 + block_h/2;
538 
539  add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
540  x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
541 
542  for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
543  for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
544  int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
545  int obmc_v= obmc[index];
546  int d;
547  if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
548  if(x<0) obmc_v += obmc[index + block_w];
549  if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
550  if(x+block_w>w) obmc_v += obmc[index - block_w];
551  //FIXME precalculate this or simplify it somehow else
552 
553  d = -dst[index] + (1<<(FRAC_BITS-1));
554  dst[index] = d;
555  ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
556  aa += obmc_v * obmc_v; //FIXME precalculate this
557  }
558  }
559  }
560  *b= backup;
561 
562  return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
563 }
564 
565 static inline int get_block_bits(SnowContext *s, int x, int y, int w){
566  const int b_stride = s->b_width << s->block_max_depth;
567  const int b_height = s->b_height<< s->block_max_depth;
568  int index= x + y*b_stride;
569  const BlockNode *b = &s->block[index];
570  const BlockNode *left = x ? &s->block[index-1] : &null_block;
571  const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
572  const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
573  const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
574  int dmx, dmy;
575 // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
576 // int my_context= av_log2(2*FFABS(left->my - top->my));
577 
578  if(x<0 || x>=b_stride || y>=b_height)
579  return 0;
580 /*
581 1 0 0
582 01X 1-2 1
583 001XX 3-6 2-3
584 0001XXX 7-14 4-7
585 00001XXXX 15-30 8-15
586 */
587 //FIXME try accurate rate
588 //FIXME intra and inter predictors if surrounding blocks are not the same type
589  if(b->type & BLOCK_INTRA){
590  return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
591  + av_log2(2*FFABS(left->color[1] - b->color[1]))
592  + av_log2(2*FFABS(left->color[2] - b->color[2])));
593  }else{
594  pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
595  dmx-= b->mx;
596  dmy-= b->my;
597  return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
598  + av_log2(2*FFABS(dmy))
599  + av_log2(2*b->ref));
600  }
601 }
602 
603 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2]){
604  Plane *p= &s->plane[plane_index];
605  const int block_size = MB_SIZE >> s->block_max_depth;
606  const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
607  const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
608  const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
609  const int ref_stride= s->current_picture->linesize[plane_index];
610  uint8_t *dst= s->current_picture->data[plane_index];
611  uint8_t *src= s-> input_picture->data[plane_index];
612  IDWTELEM *pred= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
613  uint8_t *cur = s->scratchbuf;
614  uint8_t *tmp = s->emu_edge_buffer;
615  const int b_stride = s->b_width << s->block_max_depth;
616  const int b_height = s->b_height<< s->block_max_depth;
617  const int w= p->width;
618  const int h= p->height;
619  int distortion;
620  int rate= 0;
621  const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
622  int sx= block_w*mb_x - block_w/2;
623  int sy= block_h*mb_y - block_h/2;
624  int x0= FFMAX(0,-sx);
625  int y0= FFMAX(0,-sy);
626  int x1= FFMIN(block_w*2, w-sx);
627  int y1= FFMIN(block_h*2, h-sy);
628  int i,x,y;
629 
630  av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
631 
632  ff_snow_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_h*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
633 
634  for(y=y0; y<y1; y++){
635  const uint8_t *obmc1= obmc_edged[y];
636  const IDWTELEM *pred1 = pred + y*obmc_stride;
637  uint8_t *cur1 = cur + y*ref_stride;
638  uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
639  for(x=x0; x<x1; x++){
640 #if FRAC_BITS >= LOG2_OBMC_MAX
641  int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
642 #else
643  int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
644 #endif
645  v = (v + pred1[x]) >> FRAC_BITS;
646  if(v&(~255)) v= ~(v>>31);
647  dst1[x] = v;
648  }
649  }
650 
651  /* copy the regions where obmc[] = (uint8_t)256 */
652  if(LOG2_OBMC_MAX == 8
653  && (mb_x == 0 || mb_x == b_stride-1)
654  && (mb_y == 0 || mb_y == b_height-1)){
655  if(mb_x == 0)
656  x1 = block_w;
657  else
658  x0 = block_w;
659  if(mb_y == 0)
660  y1 = block_h;
661  else
662  y0 = block_h;
663  for(y=y0; y<y1; y++)
664  memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
665  }
666 
667  if(block_w==16){
668  /* FIXME rearrange dsputil to fit 32x32 cmp functions */
669  /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
670  /* FIXME cmps overlap but do not cover the wavelet's whole support.
671  * So improving the score of one block is not strictly guaranteed
672  * to improve the score of the whole frame, thus iterative motion
673  * estimation does not always converge. */
674  if(s->avctx->me_cmp == FF_CMP_W97)
675  distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
676  else if(s->avctx->me_cmp == FF_CMP_W53)
677  distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
678  else{
679  distortion = 0;
680  for(i=0; i<4; i++){
681  int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
682  distortion += s->mecc.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
683  }
684  }
685  }else{
686  av_assert2(block_w==8);
687  distortion = s->mecc.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
688  }
689 
690  if(plane_index==0){
691  for(i=0; i<4; i++){
692 /* ..RRr
693  * .RXx.
694  * rxx..
695  */
696  rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
697  }
698  if(mb_x == b_stride-2)
699  rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
700  }
701  return distortion + rate*penalty_factor;
702 }
703 
704 static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
705  int i, y2;
706  Plane *p= &s->plane[plane_index];
707  const int block_size = MB_SIZE >> s->block_max_depth;
708  const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
709  const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
710  const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
711  const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
712  const int ref_stride= s->current_picture->linesize[plane_index];
713  uint8_t *dst= s->current_picture->data[plane_index];
714  uint8_t *src= s-> input_picture->data[plane_index];
715  //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
716  // const has only been removed from zero_dst to suppress a warning
717  static IDWTELEM zero_dst[4096]; //FIXME
718  const int b_stride = s->b_width << s->block_max_depth;
719  const int w= p->width;
720  const int h= p->height;
721  int distortion= 0;
722  int rate= 0;
723  const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
724 
725  av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
726 
727  for(i=0; i<9; i++){
728  int mb_x2= mb_x + (i%3) - 1;
729  int mb_y2= mb_y + (i/3) - 1;
730  int x= block_w*mb_x2 + block_w/2;
731  int y= block_h*mb_y2 + block_h/2;
732 
733  add_yblock(s, 0, NULL, zero_dst, dst, obmc,
734  x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
735 
736  //FIXME find a cleaner/simpler way to skip the outside stuff
737  for(y2= y; y2<0; y2++)
738  memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
739  for(y2= h; y2<y+block_h; y2++)
740  memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
741  if(x<0){
742  for(y2= y; y2<y+block_h; y2++)
743  memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
744  }
745  if(x+block_w > w){
746  for(y2= y; y2<y+block_h; y2++)
747  memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
748  }
749 
750  av_assert1(block_w== 8 || block_w==16);
751  distortion += s->mecc.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
752  }
753 
754  if(plane_index==0){
755  BlockNode *b= &s->block[mb_x+mb_y*b_stride];
756  int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
757 
758 /* ..RRRr
759  * .RXXx.
760  * .RXXx.
761  * rxxx.
762  */
763  if(merged)
764  rate = get_block_bits(s, mb_x, mb_y, 2);
765  for(i=merged?4:0; i<9; i++){
766  static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
767  rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
768  }
769  }
770  return distortion + rate*penalty_factor;
771 }
772 
773 static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
774  const int w= b->width;
775  const int h= b->height;
776  int x, y;
777 
778  if(1){
779  int run=0;
780  int *runs = s->run_buffer;
781  int run_index=0;
782  int max_index;
783 
784  for(y=0; y<h; y++){
785  for(x=0; x<w; x++){
786  int v, p=0;
787  int /*ll=0, */l=0, lt=0, t=0, rt=0;
788  v= src[x + y*stride];
789 
790  if(y){
791  t= src[x + (y-1)*stride];
792  if(x){
793  lt= src[x - 1 + (y-1)*stride];
794  }
795  if(x + 1 < w){
796  rt= src[x + 1 + (y-1)*stride];
797  }
798  }
799  if(x){
800  l= src[x - 1 + y*stride];
801  /*if(x > 1){
802  if(orientation==1) ll= src[y + (x-2)*stride];
803  else ll= src[x - 2 + y*stride];
804  }*/
805  }
806  if(parent){
807  int px= x>>1;
808  int py= y>>1;
809  if(px<b->parent->width && py<b->parent->height)
810  p= parent[px + py*2*stride];
811  }
812  if(!(/*ll|*/l|lt|t|rt|p)){
813  if(v){
814  runs[run_index++]= run;
815  run=0;
816  }else{
817  run++;
818  }
819  }
820  }
821  }
822  max_index= run_index;
823  runs[run_index++]= run;
824  run_index=0;
825  run= runs[run_index++];
826 
827  put_symbol2(&s->c, b->state[30], max_index, 0);
828  if(run_index <= max_index)
829  put_symbol2(&s->c, b->state[1], run, 3);
830 
831  for(y=0; y<h; y++){
832  if(s->c.bytestream_end - s->c.bytestream < w*40){
833  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
834  return AVERROR(ENOMEM);
835  }
836  for(x=0; x<w; x++){
837  int v, p=0;
838  int /*ll=0, */l=0, lt=0, t=0, rt=0;
839  v= src[x + y*stride];
840 
841  if(y){
842  t= src[x + (y-1)*stride];
843  if(x){
844  lt= src[x - 1 + (y-1)*stride];
845  }
846  if(x + 1 < w){
847  rt= src[x + 1 + (y-1)*stride];
848  }
849  }
850  if(x){
851  l= src[x - 1 + y*stride];
852  /*if(x > 1){
853  if(orientation==1) ll= src[y + (x-2)*stride];
854  else ll= src[x - 2 + y*stride];
855  }*/
856  }
857  if(parent){
858  int px= x>>1;
859  int py= y>>1;
860  if(px<b->parent->width && py<b->parent->height)
861  p= parent[px + py*2*stride];
862  }
863  if(/*ll|*/l|lt|t|rt|p){
864  int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
865 
866  put_rac(&s->c, &b->state[0][context], !!v);
867  }else{
868  if(!run){
869  run= runs[run_index++];
870 
871  if(run_index <= max_index)
872  put_symbol2(&s->c, b->state[1], run, 3);
873  av_assert2(v);
874  }else{
875  run--;
876  av_assert2(!v);
877  }
878  }
879  if(v){
880  int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
881  int l2= 2*FFABS(l) + (l<0);
882  int t2= 2*FFABS(t) + (t<0);
883 
884  put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
885  put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
886  }
887  }
888  }
889  }
890  return 0;
891 }
892 
893 static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
894 // encode_subband_qtree(s, b, src, parent, stride, orientation);
895 // encode_subband_z0run(s, b, src, parent, stride, orientation);
896  return encode_subband_c0run(s, b, src, parent, stride, orientation);
897 // encode_subband_dzr(s, b, src, parent, stride, orientation);
898 }
899 
900 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){
901  const int b_stride= s->b_width << s->block_max_depth;
902  BlockNode *block= &s->block[mb_x + mb_y * b_stride];
903  BlockNode backup= *block;
904  unsigned value;
905  int rd, index;
906 
907  av_assert2(mb_x>=0 && mb_y>=0);
908  av_assert2(mb_x<b_stride);
909 
910  if(intra){
911  block->color[0] = p[0];
912  block->color[1] = p[1];
913  block->color[2] = p[2];
914  block->type |= BLOCK_INTRA;
915  }else{
916  index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
917  value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
918  if(s->me_cache[index] == value)
919  return 0;
920  s->me_cache[index]= value;
921 
922  block->mx= p[0];
923  block->my= p[1];
924  block->type &= ~BLOCK_INTRA;
925  }
926 
927  rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged) + s->intra_penalty * !!intra;
928 
929 //FIXME chroma
930  if(rd < *best_rd){
931  *best_rd= rd;
932  return 1;
933  }else{
934  *block= backup;
935  return 0;
936  }
937 }
938 
939 /* special case for int[2] args we discard afterwards,
940  * fixes compilation problem with gcc 2.95 */
941 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){
942  int p[2] = {p0, p1};
943  return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
944 }
945 
946 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){
947  const int b_stride= s->b_width << s->block_max_depth;
948  BlockNode *block= &s->block[mb_x + mb_y * b_stride];
949  BlockNode backup[4];
950  unsigned value;
951  int rd, index;
952 
953  /* We don't initialize backup[] during variable declaration, because
954  * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
955  * 'int16_t'". */
956  backup[0] = block[0];
957  backup[1] = block[1];
958  backup[2] = block[b_stride];
959  backup[3] = block[b_stride + 1];
960 
961  av_assert2(mb_x>=0 && mb_y>=0);
962  av_assert2(mb_x<b_stride);
963  av_assert2(((mb_x|mb_y)&1) == 0);
964 
965  index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
966  value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
967  if(s->me_cache[index] == value)
968  return 0;
969  s->me_cache[index]= value;
970 
971  block->mx= p0;
972  block->my= p1;
973  block->ref= ref;
974  block->type &= ~BLOCK_INTRA;
975  block[1]= block[b_stride]= block[b_stride+1]= *block;
976 
977  rd= get_4block_rd(s, mb_x, mb_y, 0);
978 
979 //FIXME chroma
980  if(rd < *best_rd){
981  *best_rd= rd;
982  return 1;
983  }else{
984  block[0]= backup[0];
985  block[1]= backup[1];
986  block[b_stride]= backup[2];
987  block[b_stride+1]= backup[3];
988  return 0;
989  }
990 }
991 
992 static void iterative_me(SnowContext *s){
993  int pass, mb_x, mb_y;
994  const int b_width = s->b_width << s->block_max_depth;
995  const int b_height= s->b_height << s->block_max_depth;
996  const int b_stride= b_width;
997  int color[3];
998 
999  {
1000  RangeCoder r = s->c;
1001  uint8_t state[sizeof(s->block_state)];
1002  memcpy(state, s->block_state, sizeof(s->block_state));
1003  for(mb_y= 0; mb_y<s->b_height; mb_y++)
1004  for(mb_x= 0; mb_x<s->b_width; mb_x++)
1005  encode_q_branch(s, 0, mb_x, mb_y);
1006  s->c = r;
1007  memcpy(s->block_state, state, sizeof(s->block_state));
1008  }
1009 
1010  for(pass=0; pass<25; pass++){
1011  int change= 0;
1012 
1013  for(mb_y= 0; mb_y<b_height; mb_y++){
1014  for(mb_x= 0; mb_x<b_width; mb_x++){
1015  int dia_change, i, j, ref;
1016  int best_rd= INT_MAX, ref_rd;
1017  BlockNode backup, ref_b;
1018  const int index= mb_x + mb_y * b_stride;
1019  BlockNode *block= &s->block[index];
1020  BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
1021  BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
1022  BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
1023  BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
1024  BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
1025  BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
1026  BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
1027  BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
1028  const int b_w= (MB_SIZE >> s->block_max_depth);
1029  uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
1030 
1031  if(pass && (block->type & BLOCK_OPT))
1032  continue;
1033  block->type |= BLOCK_OPT;
1034 
1035  backup= *block;
1036 
1037  if(!s->me_cache_generation)
1038  memset(s->me_cache, 0, sizeof(s->me_cache));
1039  s->me_cache_generation += 1<<22;
1040 
1041  //FIXME precalculate
1042  {
1043  int x, y;
1044  for (y = 0; y < b_w * 2; y++)
1045  memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
1046  if(mb_x==0)
1047  for(y=0; y<b_w*2; y++)
1048  memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
1049  if(mb_x==b_stride-1)
1050  for(y=0; y<b_w*2; y++)
1051  memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
1052  if(mb_y==0){
1053  for(x=0; x<b_w*2; x++)
1054  obmc_edged[0][x] += obmc_edged[b_w-1][x];
1055  for(y=1; y<b_w; y++)
1056  memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
1057  }
1058  if(mb_y==b_height-1){
1059  for(x=0; x<b_w*2; x++)
1060  obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
1061  for(y=b_w; y<b_w*2-1; y++)
1062  memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
1063  }
1064  }
1065 
1066  //skip stuff outside the picture
1067  if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
1068  uint8_t *src= s-> input_picture->data[0];
1069  uint8_t *dst= s->current_picture->data[0];
1070  const int stride= s->current_picture->linesize[0];
1071  const int block_w= MB_SIZE >> s->block_max_depth;
1072  const int block_h= MB_SIZE >> s->block_max_depth;
1073  const int sx= block_w*mb_x - block_w/2;
1074  const int sy= block_h*mb_y - block_h/2;
1075  const int w= s->plane[0].width;
1076  const int h= s->plane[0].height;
1077  int y;
1078 
1079  for(y=sy; y<0; y++)
1080  memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1081  for(y=h; y<sy+block_h*2; y++)
1082  memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1083  if(sx<0){
1084  for(y=sy; y<sy+block_h*2; y++)
1085  memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
1086  }
1087  if(sx+block_w*2 > w){
1088  for(y=sy; y<sy+block_h*2; y++)
1089  memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
1090  }
1091  }
1092 
1093  // intra(black) = neighbors' contribution to the current block
1094  for(i=0; i < s->nb_planes; i++)
1095  color[i]= get_dc(s, mb_x, mb_y, i);
1096 
1097  // get previous score (cannot be cached due to OBMC)
1098  if(pass > 0 && (block->type&BLOCK_INTRA)){
1099  int color0[3]= {block->color[0], block->color[1], block->color[2]};
1100  check_block(s, mb_x, mb_y, color0, 1, obmc_edged, &best_rd);
1101  }else
1102  check_block_inter(s, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
1103 
1104  ref_b= *block;
1105  ref_rd= best_rd;
1106  for(ref=0; ref < s->ref_frames; ref++){
1107  int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
1108  if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
1109  continue;
1110  block->ref= ref;
1111  best_rd= INT_MAX;
1112 
1113  check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
1114  check_block_inter(s, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
1115  if(tb)
1116  check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
1117  if(lb)
1118  check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
1119  if(rb)
1120  check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
1121  if(bb)
1122  check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
1123 
1124  /* fullpel ME */
1125  //FIXME avoid subpel interpolation / round to nearest integer
1126  do{
1127  int newx = block->mx;
1128  int newy = block->my;
1129  int dia_size = s->iterative_dia_size ? s->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
1130  dia_change=0;
1131  for(i=0; i < dia_size; i++){
1132  for(j=0; j<i; j++){
1133  dia_change |= check_block_inter(s, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
1134  dia_change |= check_block_inter(s, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
1135  dia_change |= check_block_inter(s, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
1136  dia_change |= check_block_inter(s, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
1137  }
1138  }
1139  }while(dia_change);
1140  /* subpel ME */
1141  do{
1142  static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
1143  dia_change=0;
1144  for(i=0; i<8; i++)
1145  dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
1146  }while(dia_change);
1147  //FIXME or try the standard 2 pass qpel or similar
1148 
1149  mvr[0][0]= block->mx;
1150  mvr[0][1]= block->my;
1151  if(ref_rd > best_rd){
1152  ref_rd= best_rd;
1153  ref_b= *block;
1154  }
1155  }
1156  best_rd= ref_rd;
1157  *block= ref_b;
1158  check_block(s, mb_x, mb_y, color, 1, obmc_edged, &best_rd);
1159  //FIXME RD style color selection
1160  if(!same_block(block, &backup)){
1161  if(tb ) tb ->type &= ~BLOCK_OPT;
1162  if(lb ) lb ->type &= ~BLOCK_OPT;
1163  if(rb ) rb ->type &= ~BLOCK_OPT;
1164  if(bb ) bb ->type &= ~BLOCK_OPT;
1165  if(tlb) tlb->type &= ~BLOCK_OPT;
1166  if(trb) trb->type &= ~BLOCK_OPT;
1167  if(blb) blb->type &= ~BLOCK_OPT;
1168  if(brb) brb->type &= ~BLOCK_OPT;
1169  change ++;
1170  }
1171  }
1172  }
1173  av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
1174  if(!change)
1175  break;
1176  }
1177 
1178  if(s->block_max_depth == 1){
1179  int change= 0;
1180  for(mb_y= 0; mb_y<b_height; mb_y+=2){
1181  for(mb_x= 0; mb_x<b_width; mb_x+=2){
1182  int i;
1183  int best_rd, init_rd;
1184  const int index= mb_x + mb_y * b_stride;
1185  BlockNode *b[4];
1186 
1187  b[0]= &s->block[index];
1188  b[1]= b[0]+1;
1189  b[2]= b[0]+b_stride;
1190  b[3]= b[2]+1;
1191  if(same_block(b[0], b[1]) &&
1192  same_block(b[0], b[2]) &&
1193  same_block(b[0], b[3]))
1194  continue;
1195 
1196  if(!s->me_cache_generation)
1197  memset(s->me_cache, 0, sizeof(s->me_cache));
1198  s->me_cache_generation += 1<<22;
1199 
1200  init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
1201 
1202  //FIXME more multiref search?
1203  check_4block_inter(s, mb_x, mb_y,
1204  (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
1205  (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
1206 
1207  for(i=0; i<4; i++)
1208  if(!(b[i]->type&BLOCK_INTRA))
1209  check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
1210 
1211  if(init_rd != best_rd)
1212  change++;
1213  }
1214  }
1215  av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
1216  }
1217 }
1218 
1219 static void encode_blocks(SnowContext *s, int search){
1220  int x, y;
1221  int w= s->b_width;
1222  int h= s->b_height;
1223 
1224  if(s->motion_est == FF_ME_ITER && !s->keyframe && search)
1225  iterative_me(s);
1226 
1227  for(y=0; y<h; y++){
1228  if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
1229  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1230  return;
1231  }
1232  for(x=0; x<w; x++){
1233  if(s->motion_est == FF_ME_ITER || !search)
1234  encode_q_branch2(s, 0, x, y);
1235  else
1236  encode_q_branch (s, 0, x, y);
1237  }
1238  }
1239 }
1240 
1241 static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
1242  const int w= b->width;
1243  const int h= b->height;
1244  const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1245  const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
1246  int x,y, thres1, thres2;
1247 
1248  if(s->qlog == LOSSLESS_QLOG){
1249  for(y=0; y<h; y++)
1250  for(x=0; x<w; x++)
1251  dst[x + y*stride]= src[x + y*stride];
1252  return;
1253  }
1254 
1255  bias= bias ? 0 : (3*qmul)>>3;
1256  thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
1257  thres2= 2*thres1;
1258 
1259  if(!bias){
1260  for(y=0; y<h; y++){
1261  for(x=0; x<w; x++){
1262  int i= src[x + y*stride];
1263 
1264  if((unsigned)(i+thres1) > thres2){
1265  if(i>=0){
1266  i<<= QEXPSHIFT;
1267  i/= qmul; //FIXME optimize
1268  dst[x + y*stride]= i;
1269  }else{
1270  i= -i;
1271  i<<= QEXPSHIFT;
1272  i/= qmul; //FIXME optimize
1273  dst[x + y*stride]= -i;
1274  }
1275  }else
1276  dst[x + y*stride]= 0;
1277  }
1278  }
1279  }else{
1280  for(y=0; y<h; y++){
1281  for(x=0; x<w; x++){
1282  int i= src[x + y*stride];
1283 
1284  if((unsigned)(i+thres1) > thres2){
1285  if(i>=0){
1286  i<<= QEXPSHIFT;
1287  i= (i + bias) / qmul; //FIXME optimize
1288  dst[x + y*stride]= i;
1289  }else{
1290  i= -i;
1291  i<<= QEXPSHIFT;
1292  i= (i + bias) / qmul; //FIXME optimize
1293  dst[x + y*stride]= -i;
1294  }
1295  }else
1296  dst[x + y*stride]= 0;
1297  }
1298  }
1299  }
1300 }
1301 
1303  const int w= b->width;
1304  const int h= b->height;
1305  const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1306  const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1307  const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1308  int x,y;
1309 
1310  if(s->qlog == LOSSLESS_QLOG) return;
1311 
1312  for(y=0; y<h; y++){
1313  for(x=0; x<w; x++){
1314  int i= src[x + y*stride];
1315  if(i<0){
1316  src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
1317  }else if(i>0){
1318  src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
1319  }
1320  }
1321  }
1322 }
1323 
1324 static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1325  const int w= b->width;
1326  const int h= b->height;
1327  int x,y;
1328 
1329  for(y=h-1; y>=0; y--){
1330  for(x=w-1; x>=0; x--){
1331  int i= x + y*stride;
1332 
1333  if(x){
1334  if(use_median){
1335  if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1336  else src[i] -= src[i - 1];
1337  }else{
1338  if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
1339  else src[i] -= src[i - 1];
1340  }
1341  }else{
1342  if(y) src[i] -= src[i - stride];
1343  }
1344  }
1345  }
1346 }
1347 
1348 static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1349  const int w= b->width;
1350  const int h= b->height;
1351  int x,y;
1352 
1353  for(y=0; y<h; y++){
1354  for(x=0; x<w; x++){
1355  int i= x + y*stride;
1356 
1357  if(x){
1358  if(use_median){
1359  if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1360  else src[i] += src[i - 1];
1361  }else{
1362  if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
1363  else src[i] += src[i - 1];
1364  }
1365  }else{
1366  if(y) src[i] += src[i - stride];
1367  }
1368  }
1369  }
1370 }
1371 
1373  int plane_index, level, orientation;
1374 
1375  for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1376  for(level=0; level<s->spatial_decomposition_count; level++){
1377  for(orientation=level ? 1:0; orientation<4; orientation++){
1378  if(orientation==2) continue;
1379  put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
1380  }
1381  }
1382  }
1383 }
1384 
1386  int plane_index, i;
1387  uint8_t kstate[32];
1388 
1389  memset(kstate, MID_STATE, sizeof(kstate));
1390 
1391  put_rac(&s->c, kstate, s->keyframe);
1392  if(s->keyframe || s->always_reset){
1394  s->last_spatial_decomposition_type=
1395  s->last_qlog=
1396  s->last_qbias=
1397  s->last_mv_scale=
1398  s->last_block_max_depth= 0;
1399  for(plane_index=0; plane_index<2; plane_index++){
1400  Plane *p= &s->plane[plane_index];
1401  p->last_htaps=0;
1402  p->last_diag_mc=0;
1403  memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
1404  }
1405  }
1406  if(s->keyframe){
1407  put_symbol(&s->c, s->header_state, s->version, 0);
1408  put_rac(&s->c, s->header_state, s->always_reset);
1409  put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
1410  put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
1411  put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1412  put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
1413  if (s->nb_planes > 2) {
1414  put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
1415  put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
1416  }
1417  put_rac(&s->c, s->header_state, s->spatial_scalability);
1418 // put_rac(&s->c, s->header_state, s->rate_scalability);
1419  put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
1420 
1421  encode_qlogs(s);
1422  }
1423 
1424  if(!s->keyframe){
1425  int update_mc=0;
1426  for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1427  Plane *p= &s->plane[plane_index];
1428  update_mc |= p->last_htaps != p->htaps;
1429  update_mc |= p->last_diag_mc != p->diag_mc;
1430  update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1431  }
1432  put_rac(&s->c, s->header_state, update_mc);
1433  if(update_mc){
1434  for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1435  Plane *p= &s->plane[plane_index];
1436  put_rac(&s->c, s->header_state, p->diag_mc);
1437  put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
1438  for(i= p->htaps/2; i; i--)
1439  put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
1440  }
1441  }
1442  if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1443  put_rac(&s->c, s->header_state, 1);
1444  put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1445  encode_qlogs(s);
1446  }else
1447  put_rac(&s->c, s->header_state, 0);
1448  }
1449 
1450  put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
1451  put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
1452  put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
1453  put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
1454  put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
1455 
1456 }
1457 
1459  int plane_index;
1460 
1461  if(!s->keyframe){
1462  for(plane_index=0; plane_index<2; plane_index++){
1463  Plane *p= &s->plane[plane_index];
1464  p->last_diag_mc= p->diag_mc;
1465  p->last_htaps = p->htaps;
1466  memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1467  }
1468  }
1469 
1470  s->last_spatial_decomposition_type = s->spatial_decomposition_type;
1471  s->last_qlog = s->qlog;
1472  s->last_qbias = s->qbias;
1473  s->last_mv_scale = s->mv_scale;
1474  s->last_block_max_depth = s->block_max_depth;
1475  s->last_spatial_decomposition_count = s->spatial_decomposition_count;
1476 }
1477 
1478 static int qscale2qlog(int qscale){
1479  return lrint(QROOT*log2(qscale / (float)FF_QP2LAMBDA))
1480  + 61*QROOT/8; ///< 64 > 60
1481 }
1482 
1484 {
1485  /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
1486  * FIXME we know exact mv bits at this point,
1487  * but ratecontrol isn't set up to include them. */
1488  uint32_t coef_sum= 0;
1489  int level, orientation, delta_qlog;
1490 
1491  for(level=0; level<s->spatial_decomposition_count; level++){
1492  for(orientation=level ? 1 : 0; orientation<4; orientation++){
1493  SubBand *b= &s->plane[0].band[level][orientation];
1494  IDWTELEM *buf= b->ibuf;
1495  const int w= b->width;
1496  const int h= b->height;
1497  const int stride= b->stride;
1498  const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
1499  const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1500  const int qdiv= (1<<16)/qmul;
1501  int x, y;
1502  //FIXME this is ugly
1503  for(y=0; y<h; y++)
1504  for(x=0; x<w; x++)
1505  buf[x+y*stride]= b->buf[x+y*stride];
1506  if(orientation==0)
1507  decorrelate(s, b, buf, stride, 1, 0);
1508  for(y=0; y<h; y++)
1509  for(x=0; x<w; x++)
1510  coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
1511  }
1512  }
1513 
1514  /* ugly, ratecontrol just takes a sqrt again */
1515  av_assert0(coef_sum < INT_MAX);
1516  coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
1517 
1518  if(pict->pict_type == AV_PICTURE_TYPE_I){
1519  s->m.current_picture.mb_var_sum= coef_sum;
1520  s->m.current_picture.mc_mb_var_sum= 0;
1521  }else{
1522  s->m.current_picture.mc_mb_var_sum= coef_sum;
1523  s->m.current_picture.mb_var_sum= 0;
1524  }
1525 
1526  pict->quality= ff_rate_estimate_qscale(&s->m, 1);
1527  if (pict->quality < 0)
1528  return INT_MIN;
1529  s->lambda= pict->quality * 3/2;
1530  delta_qlog= qscale2qlog(pict->quality) - s->qlog;
1531  s->qlog+= delta_qlog;
1532  return delta_qlog;
1533 }
1534 
1536  int width = p->width;
1537  int height= p->height;
1538  int level, orientation, x, y;
1539 
1540  for(level=0; level<s->spatial_decomposition_count; level++){
1541  for(orientation=level ? 1 : 0; orientation<4; orientation++){
1542  SubBand *b= &p->band[level][orientation];
1543  IDWTELEM *ibuf= b->ibuf;
1544  int64_t error=0;
1545 
1546  memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
1547  ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
1548  ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
1549  for(y=0; y<height; y++){
1550  for(x=0; x<width; x++){
1551  int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
1552  error += d*d;
1553  }
1554  }
1555 
1556  b->qlog= (int)(QROOT * log2(352256.0/sqrt(error)) + 0.5);
1557  }
1558  }
1559 }
1560 
1562  const AVFrame *pict, int *got_packet)
1563 {
1564  SnowContext *s = avctx->priv_data;
1565  RangeCoder * const c= &s->c;
1566  AVFrame *pic;
1567  const int width= s->avctx->width;
1568  const int height= s->avctx->height;
1569  int level, orientation, plane_index, i, y, ret;
1570  uint8_t rc_header_bak[sizeof(s->header_state)];
1571  uint8_t rc_block_bak[sizeof(s->block_state)];
1572 
1573  if ((ret = ff_alloc_packet(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
1574  return ret;
1575 
1577  ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1578 
1579  for(i=0; i < s->nb_planes; i++){
1580  int hshift= i ? s->chroma_h_shift : 0;
1581  int vshift= i ? s->chroma_v_shift : 0;
1582  for(y=0; y<AV_CEIL_RSHIFT(height, vshift); y++)
1583  memcpy(&s->input_picture->data[i][y * s->input_picture->linesize[i]],
1584  &pict->data[i][y * pict->linesize[i]],
1585  AV_CEIL_RSHIFT(width, hshift));
1586  s->mpvencdsp.draw_edges(s->input_picture->data[i], s->input_picture->linesize[i],
1587  AV_CEIL_RSHIFT(width, hshift), AV_CEIL_RSHIFT(height, vshift),
1588  EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1589  EDGE_TOP | EDGE_BOTTOM);
1590 
1591  }
1592  emms_c();
1593  pic = s->input_picture;
1594  pic->pict_type = pict->pict_type;
1595  pic->quality = pict->quality;
1596 
1597  s->m.picture_number= avctx->frame_number;
1598  if(avctx->flags&AV_CODEC_FLAG_PASS2){
1599  s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
1600  s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
1601  if(!(avctx->flags&AV_CODEC_FLAG_QSCALE)) {
1602  pic->quality = ff_rate_estimate_qscale(&s->m, 0);
1603  if (pic->quality < 0)
1604  return -1;
1605  }
1606  }else{
1607  s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
1608  s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1609  }
1610 
1611  if(s->pass1_rc && avctx->frame_number == 0)
1612  pic->quality = 2*FF_QP2LAMBDA;
1613  if (pic->quality) {
1614  s->qlog = qscale2qlog(pic->quality);
1615  s->lambda = pic->quality * 3/2;
1616  }
1617  if (s->qlog < 0 || (!pic->quality && (avctx->flags & AV_CODEC_FLAG_QSCALE))) {
1618  s->qlog= LOSSLESS_QLOG;
1619  s->lambda = 0;
1620  }//else keep previous frame's qlog until after motion estimation
1621 
1622  if (s->current_picture->data[0]) {
1623  int w = s->avctx->width;
1624  int h = s->avctx->height;
1625 
1626  s->mpvencdsp.draw_edges(s->current_picture->data[0],
1627  s->current_picture->linesize[0], w , h ,
1629  if (s->current_picture->data[2]) {
1630  s->mpvencdsp.draw_edges(s->current_picture->data[1],
1631  s->current_picture->linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
1632  EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
1633  s->mpvencdsp.draw_edges(s->current_picture->data[2],
1634  s->current_picture->linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
1635  EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
1636  }
1637  emms_c();
1638  }
1639 
1641 
1642  s->m.current_picture_ptr= &s->m.current_picture;
1643  s->m.current_picture.f = s->current_picture;
1644  s->m.current_picture.f->pts = pict->pts;
1645  if(pic->pict_type == AV_PICTURE_TYPE_P){
1646  int block_width = (width +15)>>4;
1647  int block_height= (height+15)>>4;
1648  int stride= s->current_picture->linesize[0];
1649 
1650  av_assert0(s->current_picture->data[0]);
1651  av_assert0(s->last_picture[0]->data[0]);
1652 
1653  s->m.avctx= s->avctx;
1654  s->m. last_picture.f = s->last_picture[0];
1655  s->m. new_picture.f = s->input_picture;
1656  s->m. last_picture_ptr= &s->m. last_picture;
1657  s->m.linesize = stride;
1658  s->m.uvlinesize= s->current_picture->linesize[1];
1659  s->m.width = width;
1660  s->m.height= height;
1661  s->m.mb_width = block_width;
1662  s->m.mb_height= block_height;
1663  s->m.mb_stride= s->m.mb_width+1;
1664  s->m.b8_stride= 2*s->m.mb_width+1;
1665  s->m.f_code=1;
1666  s->m.pict_type = pic->pict_type;
1667  s->m.motion_est= s->motion_est;
1668  s->m.me.scene_change_score=0;
1669  s->m.me.dia_size = avctx->dia_size;
1670  s->m.quarter_sample= (s->avctx->flags & AV_CODEC_FLAG_QPEL)!=0;
1671  s->m.out_format= FMT_H263;
1672  s->m.unrestricted_mv= 1;
1673 
1674  s->m.lambda = s->lambda;
1675  s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
1676  s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
1677 
1678  s->m.mecc= s->mecc; //move
1679  s->m.qdsp= s->qdsp; //move
1680  s->m.hdsp = s->hdsp;
1681  ff_init_me(&s->m);
1682  s->hdsp = s->m.hdsp;
1683  s->mecc= s->m.mecc;
1684  }
1685 
1686  if(s->pass1_rc){
1687  memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
1688  memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
1689  }
1690 
1691 redo_frame:
1692 
1693  s->spatial_decomposition_count= 5;
1694 
1695  while( !(width >>(s->chroma_h_shift + s->spatial_decomposition_count))
1696  || !(height>>(s->chroma_v_shift + s->spatial_decomposition_count)))
1697  s->spatial_decomposition_count--;
1698 
1699  if (s->spatial_decomposition_count <= 0) {
1700  av_log(avctx, AV_LOG_ERROR, "Resolution too low\n");
1701  return AVERROR(EINVAL);
1702  }
1703 
1704  s->m.pict_type = pic->pict_type;
1705  s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
1706 
1708 
1709  if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1710  for(plane_index=0; plane_index < s->nb_planes; plane_index++){
1711  calculate_visual_weight(s, &s->plane[plane_index]);
1712  }
1713  }
1714 
1715  encode_header(s);
1716  s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
1717  encode_blocks(s, 1);
1718  s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
1719 
1720  for(plane_index=0; plane_index < s->nb_planes; plane_index++){
1721  Plane *p= &s->plane[plane_index];
1722  int w= p->width;
1723  int h= p->height;
1724  int x, y;
1725 // int bits= put_bits_count(&s->c.pb);
1726 
1727  if (!s->memc_only) {
1728  //FIXME optimize
1729  if(pict->data[plane_index]) //FIXME gray hack
1730  for(y=0; y<h; y++){
1731  for(x=0; x<w; x++){
1732  s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
1733  }
1734  }
1735  predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
1736 
1737  if( plane_index==0
1738  && pic->pict_type == AV_PICTURE_TYPE_P
1739  && !(avctx->flags&AV_CODEC_FLAG_PASS2)
1740  && s->m.me.scene_change_score > s->scenechange_threshold){
1742  ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1744  s->keyframe=1;
1745  s->current_picture->key_frame=1;
1746  goto redo_frame;
1747  }
1748 
1749  if(s->qlog == LOSSLESS_QLOG){
1750  for(y=0; y<h; y++){
1751  for(x=0; x<w; x++){
1752  s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
1753  }
1754  }
1755  }else{
1756  for(y=0; y<h; y++){
1757  for(x=0; x<w; x++){
1758  s->spatial_dwt_buffer[y*w + x]= s->spatial_idwt_buffer[y*w + x] * (1 << ENCODER_EXTRA_BITS);
1759  }
1760  }
1761  }
1762 
1763  ff_spatial_dwt(s->spatial_dwt_buffer, s->temp_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1764 
1765  if(s->pass1_rc && plane_index==0){
1766  int delta_qlog = ratecontrol_1pass(s, pic);
1767  if (delta_qlog <= INT_MIN)
1768  return -1;
1769  if(delta_qlog){
1770  //reordering qlog in the bitstream would eliminate this reset
1772  memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
1773  memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
1774  encode_header(s);
1775  encode_blocks(s, 0);
1776  }
1777  }
1778 
1779  for(level=0; level<s->spatial_decomposition_count; level++){
1780  for(orientation=level ? 1 : 0; orientation<4; orientation++){
1781  SubBand *b= &p->band[level][orientation];
1782 
1783  quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
1784  if(orientation==0)
1785  decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
1786  if (!s->no_bitstream)
1787  encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
1788  av_assert0(b->parent==NULL || b->parent->stride == b->stride*2);
1789  if(orientation==0)
1790  correlate(s, b, b->ibuf, b->stride, 1, 0);
1791  }
1792  }
1793 
1794  for(level=0; level<s->spatial_decomposition_count; level++){
1795  for(orientation=level ? 1 : 0; orientation<4; orientation++){
1796  SubBand *b= &p->band[level][orientation];
1797 
1798  dequantize(s, b, b->ibuf, b->stride);
1799  }
1800  }
1801 
1802  ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1803  if(s->qlog == LOSSLESS_QLOG){
1804  for(y=0; y<h; y++){
1805  for(x=0; x<w; x++){
1806  s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
1807  }
1808  }
1809  }
1810  predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1811  }else{
1812  //ME/MC only
1813  if(pic->pict_type == AV_PICTURE_TYPE_I){
1814  for(y=0; y<h; y++){
1815  for(x=0; x<w; x++){
1816  s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x]=
1817  pict->data[plane_index][y*pict->linesize[plane_index] + x];
1818  }
1819  }
1820  }else{
1821  memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
1822  predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1823  }
1824  }
1825  if(s->avctx->flags&AV_CODEC_FLAG_PSNR){
1826  int64_t error= 0;
1827 
1828  if(pict->data[plane_index]) //FIXME gray hack
1829  for(y=0; y<h; y++){
1830  for(x=0; x<w; x++){
1831  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];
1832  error += d*d;
1833  }
1834  }
1835  s->avctx->error[plane_index] += error;
1836  s->encoding_error[plane_index] = error;
1837  }
1838 
1839  }
1840  emms_c();
1841 
1843 
1844  ff_snow_release_buffer(avctx);
1845 
1846  s->current_picture->coded_picture_number = avctx->frame_number;
1847  s->current_picture->pict_type = pic->pict_type;
1848  s->current_picture->quality = pic->quality;
1849  s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
1850  s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
1851  s->m.current_picture.f->display_picture_number =
1852  s->m.current_picture.f->coded_picture_number = avctx->frame_number;
1853  s->m.current_picture.f->quality = pic->quality;
1854  s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
1855  if(s->pass1_rc)
1856  if (ff_rate_estimate_qscale(&s->m, 0) < 0)
1857  return -1;
1858  if(avctx->flags&AV_CODEC_FLAG_PASS1)
1859  ff_write_pass1_stats(&s->m);
1860  s->m.last_pict_type = s->m.pict_type;
1861 
1862  emms_c();
1863 
1864  ff_side_data_set_encoder_stats(pkt, s->current_picture->quality,
1865  s->encoding_error,
1866  (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
1867  s->current_picture->pict_type);
1868 
1869  pkt->size = ff_rac_terminate(c, 0);
1870  if (s->current_picture->key_frame)
1872  *got_packet = 1;
1873 
1874  return 0;
1875 }
1876 
1878 {
1879  SnowContext *s = avctx->priv_data;
1880 
1883  av_frame_free(&s->input_picture);
1884  av_freep(&avctx->stats_out);
1885 
1886  return 0;
1887 }
1888 
1889 #define OFFSET(x) offsetof(SnowContext, x)
1890 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1891 static const AVOption options[] = {
1892  {"motion_est", "motion estimation algorithm", OFFSET(motion_est), AV_OPT_TYPE_INT, {.i64 = FF_ME_EPZS }, FF_ME_ZERO, FF_ME_ITER, VE, "motion_est" },
1893  { "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, VE, "motion_est" },
1894  { "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, VE, "motion_est" },
1895  { "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, VE, "motion_est" },
1896  { "iter", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ITER }, 0, 0, VE, "motion_est" },
1897  { "memc_only", "Only do ME/MC (I frames -> ref, P frame -> ME+MC).", OFFSET(memc_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1898  { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1899  { "intra_penalty", "Penalty for intra blocks in block decission", OFFSET(intra_penalty), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1900  { "iterative_dia_size", "Dia size for the iterative ME", OFFSET(iterative_dia_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1901  { "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, VE },
1902  { "pred", "Spatial decomposition type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, DWT_97, DWT_53, VE, "pred" },
1903  { "dwt97", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1904  { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
1905  { "rc_eq", "Set rate control equation. When computing the expression, besides the standard functions "
1906  "defined in the section 'Expression Evaluation', the following functions are available: "
1907  "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv "
1908  "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.",
1909  OFFSET(m.rc_eq), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1910  { NULL },
1911 };
1912 
1913 static const AVClass snowenc_class = {
1914  .class_name = "snow encoder",
1915  .item_name = av_default_item_name,
1916  .option = options,
1917  .version = LIBAVUTIL_VERSION_INT,
1918 };
1919 
1921  .name = "snow",
1922  .long_name = NULL_IF_CONFIG_SMALL("Snow"),
1923  .type = AVMEDIA_TYPE_VIDEO,
1924  .id = AV_CODEC_ID_SNOW,
1925  .priv_data_size = sizeof(SnowContext),
1926  .init = encode_init,
1927  .encode2 = encode_frame,
1928  .close = encode_end,
1929  .pix_fmts = (const enum AVPixelFormat[]){
1933  },
1934  .priv_class = &snowenc_class,
1935  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1937 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AVCodec
AVCodec.
Definition: codec.h:197
stride
int stride
Definition: mace.c:144
encode_subband
static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation)
Definition: snowenc.c:893
decorrelate
static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median)
Definition: snowenc.c:1324
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
set_blocks
static void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type)
Definition: snow.h:462
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
Plane::last_diag_mc
int last_diag_mc
Definition: snow.h:111
P_LEFT
#define P_LEFT
Definition: snowenc.c:216
level
uint8_t level
Definition: svq3.c:204
av_clip
#define av_clip
Definition: common.h:122
iterative_me
static void iterative_me(SnowContext *s)
Definition: snowenc.c:992
QEXPSHIFT
#define QEXPSHIFT
Definition: snow.h:507
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
FF_LAMBDA_SCALE
#define FF_LAMBDA_SCALE
Definition: avutil.h:226
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
libm.h
MID_STATE
#define MID_STATE
Definition: snow.h:40
color
Definition: vf_paletteuse.c:583
EDGE_BOTTOM
#define EDGE_BOTTOM
Definition: mpegvideoencdsp.h:30
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
FF_ME_EPZS
#define FF_ME_EPZS
Definition: motion_est.h:41
encode_end
static av_cold int encode_end(AVCodecContext *avctx)
Definition: snowenc.c:1877
ff_side_data_set_encoder_stats
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:587
LOG2_MB_SIZE
#define LOG2_MB_SIZE
Definition: snow.h:73
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:210
MotionEstContext
Motion estimation context.
Definition: motion_est.h:47
init_ref
static void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index)
Definition: motion_est.c:83
QBIAS_SHIFT
#define QBIAS_SHIFT
Definition: snow.h:164
ff_w97_32_c
int ff_w97_32_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
Definition: snow_dwt.c:837
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
DWT_97
#define DWT_97
Definition: snow_dwt.h:68
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
index
fg index
Definition: ffmpeg_filter.c:168
MAX_DMV
#define MAX_DMV
Definition: motion_est.h:37
update_last_header_values
static void update_last_header_values(SnowContext *s)
Definition: snowenc.c:1458
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
last_picture
enum AVPictureType last_picture
Definition: movenc.c:69
encode.h
b
#define b
Definition: input.c:40
DWT_53
#define DWT_53
Definition: snow_dwt.h:69
get_penalty_factor
static int get_penalty_factor(int lambda, int lambda2, int type)
Definition: snowenc.c:191
encode_subband_c0run
static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation)
Definition: snowenc.c:773
rangecoder.h
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:196
mpegvideo.h
ff_rate_control_init
av_cold int ff_rate_control_init(MpegEncContext *s)
Definition: ratecontrol.c:472
AV_CODEC_FLAG_PSNR
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:247
FF_LAMBDA_SHIFT
#define FF_LAMBDA_SHIFT
Definition: avutil.h:225
SnowContext
Definition: snow.h:114
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: snowenc.c:1561
QSHIFT
#define QSHIFT
Definition: snow.h:43
MAX_REF_FRAMES
#define MAX_REF_FRAMES
Definition: snow.h:47
AV_CODEC_FLAG_4MV
#define AV_CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
Definition: avcodec.h:214
ff_set_cmp
void ff_set_cmp(MECmpContext *c, me_cmp_func *cmp, int type)
Definition: me_cmp.c:475
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:404
put_symbol
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
Definition: ffv1enc.c:233
ff_snow_common_end
av_cold void ff_snow_common_end(SnowContext *s)
Definition: snow.c:689
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
ff_spatial_dwt
void ff_spatial_dwt(DWTELEM *buffer, DWTELEM *temp, int width, int height, int stride, int type, int decomposition_count)
Definition: snow_dwt.c:319
Plane::diag_mc
int diag_mc
Definition: snow.h:106
BlockNode::type
uint8_t type
Bitfield of BLOCK_*.
Definition: snow.h:56
ff_mpegvideoencdsp_init
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
Definition: mpegvideoencdsp.c:232
ff_spatial_idwt
void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height, int stride, int type, int decomposition_count)
Definition: snow_dwt.c:731
ff_init_range_encoder
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:42
LOG2_OBMC_MAX
#define LOG2_OBMC_MAX
Definition: snow.h:49
BlockNode
Definition: snow.h:51
AVCodecContext::refs
int refs
number of reference frames
Definition: avcodec.h:928
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:459
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2568
OFFSET
#define OFFSET(x)
Definition: snowenc.c:1889
ff_snow_pred_block
void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride, int sx, int sy, int b_w, int b_h, const BlockNode *block, int plane_index, int w, int h)
Definition: snow.c:319
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
FF_CMP_SSE
#define FF_CMP_SSE
Definition: avcodec.h:776
ff_sqrt
#define ff_sqrt
Definition: mathops.h:206
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
ff_rate_estimate_qscale
float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
Definition: ratecontrol.c:868
ff_snow_common_init_after_header
int ff_snow_common_init_after_header(AVCodecContext *avctx)
Definition: snow.c:511
lrint
#define lrint
Definition: tablegen.h:53
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
FF_CMP_BIT
#define FF_CMP_BIT
Definition: avcodec.h:780
ff_snow_get_buffer
int ff_snow_get_buffer(SnowContext *s, AVFrame *frame)
Definition: snow.c:70
ff_h263_encode_init
void ff_h263_encode_init(MpegEncContext *s)
Definition: ituh263enc.c:771
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
encode_blocks
static void encode_blocks(SnowContext *s, int search)
Definition: snowenc.c:1219
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:445
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
BLOCK_OPT
#define BLOCK_OPT
Block needs no checks in this round of iterative motion estiation.
Definition: snow.h:59
LOSSLESS_QLOG
#define LOSSLESS_QLOG
Definition: snow.h:45
calculate_visual_weight
static void calculate_visual_weight(SnowContext *s, Plane *p)
Definition: snowenc.c:1535
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:290
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
add_yblock
static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index)
Definition: snow.h:280
AV_INPUT_BUFFER_MIN_SIZE
#define AV_INPUT_BUFFER_MIN_SIZE
Definition: avcodec.h:185
ff_snow_common_init
av_cold int ff_snow_common_init(AVCodecContext *avctx)
Definition: snow.c:429
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
pass
#define pass
Definition: fft_template.c:603
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
ff_write_pass1_stats
void ff_write_pass1_stats(MpegEncContext *s)
Definition: ratecontrol.c:38
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
check_block_inter
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)
Definition: snowenc.c:941
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
run
uint8_t run
Definition: svq3.c:203
EDGE_WIDTH
#define EDGE_WIDTH
Definition: mpegpicture.h:33
snow.h
BlockNode::my
int16_t my
Motion vector component Y, see mv_scale.
Definition: snow.h:53
state
static struct @317 state
VE
#define VE
Definition: snowenc.c:1890
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:429
ff_rac_terminate
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition: rangecoder.c:109
ROUNDED_DIV
#define ROUNDED_DIV(a, b)
Definition: common.h:56
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
src
#define src
Definition: vp8dsp.c:255
ff_snow_release_buffer
void ff_snow_release_buffer(AVCodecContext *avctx)
Definition: snow.c:636
FF_ME_ITER
#define FF_ME_ITER
Definition: snow.h:38
mathops.h
get_dc
static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index)
Definition: snowenc.c:507
abs
#define abs(x)
Definition: cuda_runtime.h:35
correlate
static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median)
Definition: snowenc.c:1348
QROOT
#define QROOT
Definition: snow.h:44
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
ME_MAP_SIZE
#define ME_MAP_SIZE
Definition: motion_est.h:38
FF_ME_XONE
#define FF_ME_XONE
Definition: motion_est.h:42
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_epzs_motion_search
int ff_epzs_motion_search(struct MpegEncContext *s, int *mx_ptr, int *my_ptr, int P[10][2], int src_index, int ref_index, int16_t(*last_mv)[2], int ref_mv_scale, int size, int h)
Definition: motion_est_template.c:977
MB_SIZE
#define MB_SIZE
Definition: cinepakenc.c:55
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1232
AV_CODEC_FLAG_QPEL
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
Definition: avcodec.h:222
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:386
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:574
encode_header
static void encode_header(SnowContext *s)
Definition: snowenc.c:1385
FF_CMP_PSNR
#define FF_CMP_PSNR
Definition: avcodec.h:779
Plane::height
int height
Definition: cfhd.h:120
P
#define P
AVFrame::quality
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:417
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
FF_CMP_W53
#define FF_CMP_W53
Definition: avcodec.h:786
Plane::last_hcoeff
int8_t last_hcoeff[HTAPS_MAX/2]
Definition: snow.h:110
size
int size
Definition: twinvq_data.h:10344
ff_build_rac_states
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
encode_q_branch
static int encode_q_branch(SnowContext *s, int level, int x, int y)
Definition: snowenc.c:223
SubBand
Definition: cfhd.h:109
FF_CMP_SATD
#define FF_CMP_SATD
Definition: avcodec.h:777
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:235
height
#define height
Plane::htaps
int htaps
Definition: snow.h:104
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
Plane::last_htaps
int last_htaps
Definition: snow.h:109
Plane::width
int width
Definition: cfhd.h:119
snow_dwt.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
FF_CMP_SAD
#define FF_CMP_SAD
Definition: avcodec.h:775
encode_q_branch2
static void encode_q_branch2(SnowContext *s, int level, int x, int y)
Definition: snowenc.c:451
ff_snow_encoder
const AVCodec ff_snow_encoder
Definition: snowenc.c:1920
ff_get_mb_score
int ff_get_mb_score(struct MpegEncContext *s, int mx, int my, int src_index, int ref_index, int size, int h, int add_rate)
Definition: motion_est_template.c:192
ff_quant3bA
const int8_t ff_quant3bA[256]
Definition: snowdata.h:104
Plane::hcoeff
int8_t hcoeff[HTAPS_MAX/2]
Definition: snow.h:105
DWTELEM
int DWTELEM
Definition: dirac_dwt.h:26
ff_obmc_tab
const uint8_t *const ff_obmc_tab[4]
Definition: snowdata.h:123
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
FMT_H263
@ FMT_H263
Definition: mpegutils.h:125
ENCODER_EXTRA_BITS
#define ENCODER_EXTRA_BITS
Definition: snow.h:75
i
int i
Definition: input.c:406
log.h
pred_mv
static void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
Definition: diracdec.c:1390
FF_CMP_RD
#define FF_CMP_RD
Definition: avcodec.h:781
get_block_bits
static int get_block_bits(SnowContext *s, int x, int y, int w)
Definition: snowenc.c:565
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:50
ff_square_tab
const uint32_t ff_square_tab[512]
Definition: me_cmp.c:34
BLOCK_INTRA
#define BLOCK_INTRA
Intra block, inter otherwise.
Definition: snow.h:58
get_block_rd
static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, uint8_t(*obmc_edged)[MB_SIZE *2])
Definition: snowenc.c:603
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
qscale2qlog
static int qscale2qlog(int qscale)
Definition: snowenc.c:1478
av_always_inline
#define av_always_inline
Definition: attributes.h:49
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVCodecContext::dia_size
int dia_size
ME diamond size & shape.
Definition: avcodec.h:798
FF_CMP_NSSE
#define FF_CMP_NSSE
Definition: avcodec.h:785
AVCodecContext::mb_lmin
int mb_lmin
minimum MB Lagrange multiplier
Definition: avcodec.h:901
tb
#define tb
Definition: regdef.h:68
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:243
ff_qexp
const uint8_t ff_qexp[QROOT]
Definition: snowdata.h:128
predict_plane
static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add)
Definition: snow.h:455
pix_sum
static int pix_sum(uint8_t *pix, int line_size, int w, int h)
Definition: snowenc.c:159
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
ff_snow_frame_start
int ff_snow_frame_start(SnowContext *s)
Definition: snow.c:651
put_symbol2
static void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2)
Definition: snow.h:563
ff_init_me
int ff_init_me(MpegEncContext *s)
Definition: motion_est.c:306
AVCodecContext::height
int height
Definition: avcodec.h:552
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:589
ff_rate_control_uninit
av_cold void ff_rate_control_uninit(MpegEncContext *s)
Definition: ratecontrol.c:672
log2
#define log2(x)
Definition: libm.h:404
avcodec.h
mid_pred
#define mid_pred
Definition: mathops.h:97
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
search
static float search(FOCContext *foc, int pass, int maxpass, int xmin, int xmax, int ymin, int ymax, int *best_x, int *best_y, float best_score)
Definition: vf_find_rect.c:157
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: snowenc.c:39
options
static const AVOption options[]
Definition: snowenc.c:1891
square
static int square(int x)
Definition: roqvideoenc.c:194
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
put_rac
#define put_rac(C, S, B)
ff_snow_reset_contexts
void ff_snow_reset_contexts(SnowContext *s)
Definition: snow.c:97
encode_qlogs
static void encode_qlogs(SnowContext *s)
Definition: snowenc.c:1372
AVCodecContext
main external API structure.
Definition: avcodec.h:379
AV_CODEC_ID_SNOW
@ AV_CODEC_ID_SNOW
Definition: codec_id.h:262
EDGE_TOP
#define EDGE_TOP
Definition: mpegvideoencdsp.h:29
t2
#define t2
Definition: regdef.h:30
ME_CACHE_SIZE
#define ME_CACHE_SIZE
Definition: snow.h:172
FRAC_BITS
#define FRAC_BITS
Definition: g729postfilter.c:36
inverse
static int inverse(AudioFWTDNContext *s, double **in, int *in_length, double *out, int out_length, int ch, uint64_t sn)
Definition: af_afwtdn.c:791
check_4block_inter
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)
Definition: snowenc.c:946
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
get_4block_rd
static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index)
Definition: snowenc.c:704
FF_CMP_DCT
#define FF_CMP_DCT
Definition: avcodec.h:778
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
get_rac_count
static int get_rac_count(RangeCoder *c)
Definition: rangecoder.h:87
AVCodecContext::mb_lmax
int mb_lmax
maximum MB Lagrange multiplier
Definition: avcodec.h:908
Plane
Definition: cfhd.h:118
av_clip_uint8
#define av_clip_uint8
Definition: common.h:128
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
shift
static int shift(int a, int b)
Definition: sonic.c:83
BlockNode::level
uint8_t level
Definition: snow.h:61
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
same_block
static av_always_inline int same_block(BlockNode *a, BlockNode *b)
Definition: snow.h:270
packet_internal.h
Plane::band
SubBand band[DWT_LEVELS_3D][4]
Definition: cfhd.h:131
BlockNode::mx
int16_t mx
Motion vector component X, see mv_scale.
Definition: snow.h:52
AVCodecContext::frame_number
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1019
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:406
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
cr
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:216
FF_CMP_DCT264
#define FF_CMP_DCT264
Definition: avcodec.h:789
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
quantize
static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias)
Definition: snowenc.c:1241
dequantize
static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride)
Definition: snowenc.c:1302
ratecontrol_1pass
static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
Definition: snowenc.c:1483
d
d
Definition: ffmpeg_filter.c:156
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:552
null_block
static const BlockNode null_block
Definition: snow.h:64
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_w53_32_c
int ff_w53_32_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
Definition: snow_dwt.c:832
IDWTELEM
short IDWTELEM
Definition: dirac_dwt.h:27
pix_norm1
static int pix_norm1(uint8_t *pix, int line_size, int w)
Definition: snowenc.c:175
h
h
Definition: vp9dsp_template.c:2038
RangeCoder
Definition: mss3.c:61
snowenc_class
static const AVClass snowenc_class
Definition: snowenc.c:1913
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
check_block
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)
Definition: snowenc.c:900
int
int
Definition: ffmpeg_filter.c:156
P_TOP
#define P_TOP
Definition: snowenc.c:217
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
ff_snow_alloc_blocks
int ff_snow_alloc_blocks(SnowContext *s)
Definition: snow.c:111
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:34
BlockNode::ref
uint8_t ref
Reference frame index.
Definition: snow.h:54
P_TOPRIGHT
#define P_TOPRIGHT
Definition: snowenc.c:218
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:231
P_MEDIAN
#define P_MEDIAN
Definition: snowenc.c:219
FF_ME_ZERO
#define FF_ME_ZERO
Definition: motion_est.h:40
FF_CMP_W97
#define FF_CMP_W97
Definition: avcodec.h:787
h263.h
intmath.h