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