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