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