2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/intmath.h"
22 #include "libavutil/libm.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
30 #include "rangecoder.h"
33 #include "mpegvideo.h"
36 static av_cold int encode_init(AVCodecContext *avctx)
38 SnowContext *s = avctx->priv_data;
42 #if FF_API_PRIVATE_OPT
43 FF_DISABLE_DEPRECATION_WARNINGS
44 if (avctx->prediction_method)
45 s->pred = avctx->prediction_method;
46 FF_ENABLE_DEPRECATION_WARNINGS
50 && (avctx->flags & AV_CODEC_FLAG_QSCALE)
51 && avctx->global_quality == 0){
52 av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
53 return AVERROR(EINVAL);
56 s->spatial_decomposition_type= s->pred; //FIXME add decorrelator type r transform_type
58 s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
59 s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
61 for(plane_index=0; plane_index<3; plane_index++){
62 s->plane[plane_index].diag_mc= 1;
63 s->plane[plane_index].htaps= 6;
64 s->plane[plane_index].hcoeff[0]= 40;
65 s->plane[plane_index].hcoeff[1]= -10;
66 s->plane[plane_index].hcoeff[2]= 2;
67 s->plane[plane_index].fast_mc= 1;
70 if ((ret = ff_snow_common_init(avctx)) < 0) {
73 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
75 ff_snow_alloc_blocks(s);
80 s->m.bit_rate= avctx->bit_rate;
81 s->m.lmin = avctx->mb_lmin;
82 s->m.lmax = avctx->mb_lmax;
85 s->m.me.scratchpad= av_mallocz_array((avctx->width+64), 2*16*2*sizeof(uint8_t));
86 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
87 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
88 s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
89 if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
90 return AVERROR(ENOMEM);
92 ff_h263_encode_init(&s->m); //mv_penalty
94 s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
96 if(avctx->flags&AV_CODEC_FLAG_PASS1){
98 avctx->stats_out = av_mallocz(256);
100 if (!avctx->stats_out)
101 return AVERROR(ENOMEM);
103 if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&AV_CODEC_FLAG_QSCALE)){
104 ret = ff_rate_control_init(&s->m);
108 s->pass1_rc= !(avctx->flags & (AV_CODEC_FLAG_QSCALE|AV_CODEC_FLAG_PASS2));
110 switch(avctx->pix_fmt){
111 case AV_PIX_FMT_YUV444P:
112 // case AV_PIX_FMT_YUV422P:
113 case AV_PIX_FMT_YUV420P:
114 // case AV_PIX_FMT_YUV411P:
115 case AV_PIX_FMT_YUV410P:
117 s->colorspace_type= 0;
119 case AV_PIX_FMT_GRAY8:
121 s->colorspace_type = 1;
123 /* case AV_PIX_FMT_RGB32:
127 av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
128 return AVERROR_PATCHWELCOME;
130 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
132 ff_set_cmp(&s->mecc, s->mecc.me_cmp, s->avctx->me_cmp);
133 ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
135 s->input_picture = av_frame_alloc();
136 if (!s->input_picture)
137 return AVERROR(ENOMEM);
139 if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
142 if(s->motion_est == FF_ME_ITER){
143 int size= s->b_width * s->b_height << 2*s->block_max_depth;
144 for(i=0; i<s->max_ref_frames; i++){
145 s->ref_mvs[i]= av_mallocz_array(size, sizeof(int16_t[2]));
146 s->ref_scores[i]= av_mallocz_array(size, sizeof(uint32_t));
147 if (!s->ref_mvs[i] || !s->ref_scores[i])
148 return AVERROR(ENOMEM);
155 //near copy & paste from dsputil, FIXME
156 static int pix_sum(uint8_t * pix, int line_size, int w, int h)
161 for (i = 0; i < h; i++) {
162 for (j = 0; j < w; j++) {
166 pix += line_size - w;
171 //near copy & paste from dsputil, FIXME
172 static int pix_norm1(uint8_t * pix, int line_size, int w)
175 uint32_t *sq = ff_square_tab + 256;
178 for (i = 0; i < w; i++) {
179 for (j = 0; j < w; j ++) {
183 pix += line_size - w;
188 static inline int get_penalty_factor(int lambda, int lambda2, int type){
192 return lambda>>FF_LAMBDA_SHIFT;
194 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
196 return (4*lambda)>>(FF_LAMBDA_SHIFT);
198 return (2*lambda)>>(FF_LAMBDA_SHIFT);
201 return (2*lambda)>>FF_LAMBDA_SHIFT;
206 return lambda2>>FF_LAMBDA_SHIFT;
215 #define P_TOPRIGHT P[3]
216 #define P_MEDIAN P[4]
218 #define FLAG_QPEL 1 //must be 1
220 static int encode_q_branch(SnowContext *s, int level, int x, int y){
221 uint8_t p_buffer[1024];
222 uint8_t i_buffer[1024];
223 uint8_t p_state[sizeof(s->block_state)];
224 uint8_t i_state[sizeof(s->block_state)];
226 uint8_t *pbbak= s->c.bytestream;
227 uint8_t *pbbak_start= s->c.bytestream_start;
228 int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
229 const int w= s->b_width << s->block_max_depth;
230 const int h= s->b_height << s->block_max_depth;
231 const int rem_depth= s->block_max_depth - level;
232 const int index= (x + y*w) << rem_depth;
233 const int block_w= 1<<(LOG2_MB_SIZE - level);
234 int trx= (x+1)<<rem_depth;
235 int try= (y+1)<<rem_depth;
236 const BlockNode *left = x ? &s->block[index-1] : &null_block;
237 const BlockNode *top = y ? &s->block[index-w] : &null_block;
238 const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
239 const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
240 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
241 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
242 int pl = left->color[0];
243 int pcb= left->color[1];
244 int pcr= left->color[2];
248 const int stride= s->current_picture->linesize[0];
249 const int uvstride= s->current_picture->linesize[1];
250 uint8_t *current_data[3]= { s->input_picture->data[0] + (x + y* stride)*block_w,
251 s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
252 s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
254 int16_t last_mv[3][2];
255 int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
256 const int shift= 1+qpel;
257 MotionEstContext *c= &s->m.me;
258 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
259 int mx_context= av_log2(2*FFABS(left->mx - top->mx));
260 int my_context= av_log2(2*FFABS(left->my - top->my));
261 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
262 int ref, best_ref, ref_score, ref_mx, ref_my;
264 av_assert0(sizeof(s->block_state) >= 256);
266 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
270 // clip predictors / edge ?
276 P_TOPRIGHT[0]= tr->mx;
277 P_TOPRIGHT[1]= tr->my;
279 last_mv[0][0]= s->block[index].mx;
280 last_mv[0][1]= s->block[index].my;
281 last_mv[1][0]= right->mx;
282 last_mv[1][1]= right->my;
283 last_mv[2][0]= bottom->mx;
284 last_mv[2][1]= bottom->my;
291 av_assert1(c-> stride == stride);
292 av_assert1(c->uvstride == uvstride);
294 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
295 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
296 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
297 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_DMV;
299 c->xmin = - x*block_w - 16+3;
300 c->ymin = - y*block_w - 16+3;
301 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
302 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
304 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
305 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
306 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
307 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
308 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
309 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
310 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
312 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
313 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
316 c->pred_x= P_LEFT[0];
317 c->pred_y= P_LEFT[1];
319 c->pred_x = P_MEDIAN[0];
320 c->pred_y = P_MEDIAN[1];
325 for(ref=0; ref<s->ref_frames; ref++){
326 init_ref(c, current_data, s->last_picture[ref]->data, NULL, block_w*x, block_w*y, 0);
328 ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
329 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
331 av_assert2(ref_mx >= c->xmin);
332 av_assert2(ref_mx <= c->xmax);
333 av_assert2(ref_my >= c->ymin);
334 av_assert2(ref_my <= c->ymax);
336 ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
337 ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
338 ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
340 s->ref_mvs[ref][index][0]= ref_mx;
341 s->ref_mvs[ref][index][1]= ref_my;
342 s->ref_scores[ref][index]= ref_score;
344 if(score > ref_score){
351 //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
354 base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
357 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
358 memcpy(p_state, s->block_state, sizeof(s->block_state));
360 if(level!=s->block_max_depth)
361 put_rac(&pc, &p_state[4 + s_context], 1);
362 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
363 if(s->ref_frames > 1)
364 put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
365 pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
366 put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
367 put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
368 p_len= pc.bytestream - pc.bytestream_start;
369 score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
371 block_s= block_w*block_w;
372 sum = pix_sum(current_data[0], stride, block_w, block_w);
373 l= (sum + block_s/2)/block_s;
374 iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
376 if (s->nb_planes > 2) {
377 block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
378 sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
379 cb= (sum + block_s/2)/block_s;
380 // iscore += pix_norm1(¤t_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
381 sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
382 cr= (sum + block_s/2)/block_s;
383 // iscore += pix_norm1(¤t_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
389 ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
390 memcpy(i_state, s->block_state, sizeof(s->block_state));
391 if(level!=s->block_max_depth)
392 put_rac(&ic, &i_state[4 + s_context], 1);
393 put_rac(&ic, &i_state[1 + left->type + top->type], 1);
394 put_symbol(&ic, &i_state[32], l-pl , 1);
395 if (s->nb_planes > 2) {
396 put_symbol(&ic, &i_state[64], cb-pcb, 1);
397 put_symbol(&ic, &i_state[96], cr-pcr, 1);
399 i_len= ic.bytestream - ic.bytestream_start;
400 iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
402 av_assert1(iscore < 255*255*256 + s->lambda2*10);
403 av_assert1(iscore >= 0);
404 av_assert1(l>=0 && l<=255);
405 av_assert1(pl>=0 && pl<=255);
408 int varc= iscore >> 8;
409 int vard= score >> 8;
410 if (vard <= 64 || vard < varc)
411 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
413 c->scene_change_score+= s->m.qscale;
416 if(level!=s->block_max_depth){
417 put_rac(&s->c, &s->block_state[4 + s_context], 0);
418 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
419 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
420 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
421 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
422 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
424 if(score2 < score && score2 < iscore)
429 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
430 memcpy(pbbak, i_buffer, i_len);
432 s->c.bytestream_start= pbbak_start;
433 s->c.bytestream= pbbak + i_len;
434 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
435 memcpy(s->block_state, i_state, sizeof(s->block_state));
438 memcpy(pbbak, p_buffer, p_len);
440 s->c.bytestream_start= pbbak_start;
441 s->c.bytestream= pbbak + p_len;
442 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
443 memcpy(s->block_state, p_state, sizeof(s->block_state));
448 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
449 const int w= s->b_width << s->block_max_depth;
450 const int rem_depth= s->block_max_depth - level;
451 const int index= (x + y*w) << rem_depth;
452 int trx= (x+1)<<rem_depth;
453 BlockNode *b= &s->block[index];
454 const BlockNode *left = x ? &s->block[index-1] : &null_block;
455 const BlockNode *top = y ? &s->block[index-w] : &null_block;
456 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
457 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
458 int pl = left->color[0];
459 int pcb= left->color[1];
460 int pcr= left->color[2];
462 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
463 int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
464 int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
465 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
468 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
472 if(level!=s->block_max_depth){
473 if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
474 put_rac(&s->c, &s->block_state[4 + s_context], 1);
476 put_rac(&s->c, &s->block_state[4 + s_context], 0);
477 encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
478 encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
479 encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
480 encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
484 if(b->type & BLOCK_INTRA){
485 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
486 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
487 put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
488 if (s->nb_planes > 2) {
489 put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
490 put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
492 set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
494 pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
495 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
496 if(s->ref_frames > 1)
497 put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
498 put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
499 put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
500 set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
504 static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
506 Plane *p= &s->plane[plane_index];
507 const int block_size = MB_SIZE >> s->block_max_depth;
508 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
509 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
510 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
511 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
512 const int ref_stride= s->current_picture->linesize[plane_index];
513 uint8_t *src= s-> input_picture->data[plane_index];
514 IDWTELEM *dst= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
515 const int b_stride = s->b_width << s->block_max_depth;
516 const int w= p->width;
517 const int h= p->height;
518 int index= mb_x + mb_y*b_stride;
519 BlockNode *b= &s->block[index];
520 BlockNode backup= *b;
524 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
526 b->type|= BLOCK_INTRA;
527 b->color[plane_index]= 0;
528 memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
531 int mb_x2= mb_x + (i &1) - 1;
532 int mb_y2= mb_y + (i>>1) - 1;
533 int x= block_w*mb_x2 + block_w/2;
534 int y= block_h*mb_y2 + block_h/2;
536 add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
537 x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
539 for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
540 for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
541 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
542 int obmc_v= obmc[index];
544 if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
545 if(x<0) obmc_v += obmc[index + block_w];
546 if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
547 if(x+block_w>w) obmc_v += obmc[index - block_w];
548 //FIXME precalculate this or simplify it somehow else
550 d = -dst[index] + (1<<(FRAC_BITS-1));
552 ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
553 aa += obmc_v * obmc_v; //FIXME precalculate this
559 return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
562 static inline int get_block_bits(SnowContext *s, int x, int y, int w){
563 const int b_stride = s->b_width << s->block_max_depth;
564 const int b_height = s->b_height<< s->block_max_depth;
565 int index= x + y*b_stride;
566 const BlockNode *b = &s->block[index];
567 const BlockNode *left = x ? &s->block[index-1] : &null_block;
568 const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
569 const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
570 const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
572 // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
573 // int my_context= av_log2(2*FFABS(left->my - top->my));
575 if(x<0 || x>=b_stride || y>=b_height)
584 //FIXME try accurate rate
585 //FIXME intra and inter predictors if surrounding blocks are not the same type
586 if(b->type & BLOCK_INTRA){
587 return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
588 + av_log2(2*FFABS(left->color[1] - b->color[1]))
589 + av_log2(2*FFABS(left->color[2] - b->color[2])));
591 pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
594 return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
595 + av_log2(2*FFABS(dmy))
596 + av_log2(2*b->ref));
600 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2]){
601 Plane *p= &s->plane[plane_index];
602 const int block_size = MB_SIZE >> s->block_max_depth;
603 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
604 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
605 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
606 const int ref_stride= s->current_picture->linesize[plane_index];
607 uint8_t *dst= s->current_picture->data[plane_index];
608 uint8_t *src= s-> input_picture->data[plane_index];
609 IDWTELEM *pred= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
610 uint8_t *cur = s->scratchbuf;
611 uint8_t *tmp = s->emu_edge_buffer;
612 const int b_stride = s->b_width << s->block_max_depth;
613 const int b_height = s->b_height<< s->block_max_depth;
614 const int w= p->width;
615 const int h= p->height;
618 const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
619 int sx= block_w*mb_x - block_w/2;
620 int sy= block_h*mb_y - block_h/2;
621 int x0= FFMAX(0,-sx);
622 int y0= FFMAX(0,-sy);
623 int x1= FFMIN(block_w*2, w-sx);
624 int y1= FFMIN(block_h*2, h-sy);
627 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
629 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);
631 for(y=y0; y<y1; y++){
632 const uint8_t *obmc1= obmc_edged[y];
633 const IDWTELEM *pred1 = pred + y*obmc_stride;
634 uint8_t *cur1 = cur + y*ref_stride;
635 uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
636 for(x=x0; x<x1; x++){
637 #if FRAC_BITS >= LOG2_OBMC_MAX
638 int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
640 int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
642 v = (v + pred1[x]) >> FRAC_BITS;
643 if(v&(~255)) v= ~(v>>31);
648 /* copy the regions where obmc[] = (uint8_t)256 */
649 if(LOG2_OBMC_MAX == 8
650 && (mb_x == 0 || mb_x == b_stride-1)
651 && (mb_y == 0 || mb_y == b_height-1)){
661 memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
665 /* FIXME rearrange dsputil to fit 32x32 cmp functions */
666 /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
667 /* FIXME cmps overlap but do not cover the wavelet's whole support.
668 * So improving the score of one block is not strictly guaranteed
669 * to improve the score of the whole frame, thus iterative motion
670 * estimation does not always converge. */
671 if(s->avctx->me_cmp == FF_CMP_W97)
672 distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
673 else if(s->avctx->me_cmp == FF_CMP_W53)
674 distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
678 int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
679 distortion += s->mecc.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
683 av_assert2(block_w==8);
684 distortion = s->mecc.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
693 rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
695 if(mb_x == b_stride-2)
696 rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
698 return distortion + rate*penalty_factor;
701 static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
703 Plane *p= &s->plane[plane_index];
704 const int block_size = MB_SIZE >> s->block_max_depth;
705 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
706 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
707 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
708 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
709 const int ref_stride= s->current_picture->linesize[plane_index];
710 uint8_t *dst= s->current_picture->data[plane_index];
711 uint8_t *src= s-> input_picture->data[plane_index];
712 //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
713 // const has only been removed from zero_dst to suppress a warning
714 static IDWTELEM zero_dst[4096]; //FIXME
715 const int b_stride = s->b_width << s->block_max_depth;
716 const int w= p->width;
717 const int h= p->height;
720 const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
722 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
725 int mb_x2= mb_x + (i%3) - 1;
726 int mb_y2= mb_y + (i/3) - 1;
727 int x= block_w*mb_x2 + block_w/2;
728 int y= block_h*mb_y2 + block_h/2;
730 add_yblock(s, 0, NULL, zero_dst, dst, obmc,
731 x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
733 //FIXME find a cleaner/simpler way to skip the outside stuff
734 for(y2= y; y2<0; y2++)
735 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
736 for(y2= h; y2<y+block_h; y2++)
737 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
739 for(y2= y; y2<y+block_h; y2++)
740 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
743 for(y2= y; y2<y+block_h; y2++)
744 memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
747 av_assert1(block_w== 8 || block_w==16);
748 distortion += s->mecc.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
752 BlockNode *b= &s->block[mb_x+mb_y*b_stride];
753 int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
761 rate = get_block_bits(s, mb_x, mb_y, 2);
762 for(i=merged?4:0; i<9; i++){
763 static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
764 rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
767 return distortion + rate*penalty_factor;
770 static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
771 const int w= b->width;
772 const int h= b->height;
777 int *runs = s->run_buffer;
784 int /*ll=0, */l=0, lt=0, t=0, rt=0;
785 v= src[x + y*stride];
788 t= src[x + (y-1)*stride];
790 lt= src[x - 1 + (y-1)*stride];
793 rt= src[x + 1 + (y-1)*stride];
797 l= src[x - 1 + y*stride];
799 if(orientation==1) ll= src[y + (x-2)*stride];
800 else ll= src[x - 2 + y*stride];
806 if(px<b->parent->width && py<b->parent->height)
807 p= parent[px + py*2*stride];
809 if(!(/*ll|*/l|lt|t|rt|p)){
811 runs[run_index++]= run;
819 max_index= run_index;
820 runs[run_index++]= run;
822 run= runs[run_index++];
824 put_symbol2(&s->c, b->state[30], max_index, 0);
825 if(run_index <= max_index)
826 put_symbol2(&s->c, b->state[1], run, 3);
829 if(s->c.bytestream_end - s->c.bytestream < w*40){
830 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
831 return AVERROR(ENOMEM);
835 int /*ll=0, */l=0, lt=0, t=0, rt=0;
836 v= src[x + y*stride];
839 t= src[x + (y-1)*stride];
841 lt= src[x - 1 + (y-1)*stride];
844 rt= src[x + 1 + (y-1)*stride];
848 l= src[x - 1 + y*stride];
850 if(orientation==1) ll= src[y + (x-2)*stride];
851 else ll= src[x - 2 + y*stride];
857 if(px<b->parent->width && py<b->parent->height)
858 p= parent[px + py*2*stride];
860 if(/*ll|*/l|lt|t|rt|p){
861 int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
863 put_rac(&s->c, &b->state[0][context], !!v);
866 run= runs[run_index++];
868 if(run_index <= max_index)
869 put_symbol2(&s->c, b->state[1], run, 3);
877 int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
878 int l2= 2*FFABS(l) + (l<0);
879 int t2= 2*FFABS(t) + (t<0);
881 put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
882 put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
890 static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
891 // encode_subband_qtree(s, b, src, parent, stride, orientation);
892 // encode_subband_z0run(s, b, src, parent, stride, orientation);
893 return encode_subband_c0run(s, b, src, parent, stride, orientation);
894 // encode_subband_dzr(s, b, src, parent, stride, orientation);
897 static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
898 const int b_stride= s->b_width << s->block_max_depth;
899 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
900 BlockNode backup= *block;
904 av_assert2(mb_x>=0 && mb_y>=0);
905 av_assert2(mb_x<b_stride);
908 block->color[0] = p[0];
909 block->color[1] = p[1];
910 block->color[2] = p[2];
911 block->type |= BLOCK_INTRA;
913 index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
914 value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
915 if(s->me_cache[index] == value)
917 s->me_cache[index]= value;
921 block->type &= ~BLOCK_INTRA;
924 rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged) + s->intra_penalty * !!intra;
936 /* special case for int[2] args we discard afterwards,
937 * fixes compilation problem with gcc 2.95 */
938 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){
940 return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
943 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){
944 const int b_stride= s->b_width << s->block_max_depth;
945 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
950 /* We don't initialize backup[] during variable declaration, because
951 * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
953 backup[0] = block[0];
954 backup[1] = block[1];
955 backup[2] = block[b_stride];
956 backup[3] = block[b_stride + 1];
958 av_assert2(mb_x>=0 && mb_y>=0);
959 av_assert2(mb_x<b_stride);
960 av_assert2(((mb_x|mb_y)&1) == 0);
962 index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
963 value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
964 if(s->me_cache[index] == value)
966 s->me_cache[index]= value;
971 block->type &= ~BLOCK_INTRA;
972 block[1]= block[b_stride]= block[b_stride+1]= *block;
974 rd= get_4block_rd(s, mb_x, mb_y, 0);
983 block[b_stride]= backup[2];
984 block[b_stride+1]= backup[3];
989 static void iterative_me(SnowContext *s){
990 int pass, mb_x, mb_y;
991 const int b_width = s->b_width << s->block_max_depth;
992 const int b_height= s->b_height << s->block_max_depth;
993 const int b_stride= b_width;
998 uint8_t state[sizeof(s->block_state)];
999 memcpy(state, s->block_state, sizeof(s->block_state));
1000 for(mb_y= 0; mb_y<s->b_height; mb_y++)
1001 for(mb_x= 0; mb_x<s->b_width; mb_x++)
1002 encode_q_branch(s, 0, mb_x, mb_y);
1004 memcpy(s->block_state, state, sizeof(s->block_state));
1007 for(pass=0; pass<25; pass++){
1010 for(mb_y= 0; mb_y<b_height; mb_y++){
1011 for(mb_x= 0; mb_x<b_width; mb_x++){
1012 int dia_change, i, j, ref;
1013 int best_rd= INT_MAX, ref_rd;
1014 BlockNode backup, ref_b;
1015 const int index= mb_x + mb_y * b_stride;
1016 BlockNode *block= &s->block[index];
1017 BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
1018 BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
1019 BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
1020 BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
1021 BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
1022 BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
1023 BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
1024 BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
1025 const int b_w= (MB_SIZE >> s->block_max_depth);
1026 uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
1028 if(pass && (block->type & BLOCK_OPT))
1030 block->type |= BLOCK_OPT;
1034 if(!s->me_cache_generation)
1035 memset(s->me_cache, 0, sizeof(s->me_cache));
1036 s->me_cache_generation += 1<<22;
1038 //FIXME precalculate
1041 for (y = 0; y < b_w * 2; y++)
1042 memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
1044 for(y=0; y<b_w*2; y++)
1045 memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
1046 if(mb_x==b_stride-1)
1047 for(y=0; y<b_w*2; y++)
1048 memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
1050 for(x=0; x<b_w*2; x++)
1051 obmc_edged[0][x] += obmc_edged[b_w-1][x];
1052 for(y=1; y<b_w; y++)
1053 memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
1055 if(mb_y==b_height-1){
1056 for(x=0; x<b_w*2; x++)
1057 obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
1058 for(y=b_w; y<b_w*2-1; y++)
1059 memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
1063 //skip stuff outside the picture
1064 if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
1065 uint8_t *src= s-> input_picture->data[0];
1066 uint8_t *dst= s->current_picture->data[0];
1067 const int stride= s->current_picture->linesize[0];
1068 const int block_w= MB_SIZE >> s->block_max_depth;
1069 const int block_h= MB_SIZE >> s->block_max_depth;
1070 const int sx= block_w*mb_x - block_w/2;
1071 const int sy= block_h*mb_y - block_h/2;
1072 const int w= s->plane[0].width;
1073 const int h= s->plane[0].height;
1077 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1078 for(y=h; y<sy+block_h*2; y++)
1079 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1081 for(y=sy; y<sy+block_h*2; y++)
1082 memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
1084 if(sx+block_w*2 > w){
1085 for(y=sy; y<sy+block_h*2; y++)
1086 memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
1090 // intra(black) = neighbors' contribution to the current block
1091 for(i=0; i < s->nb_planes; i++)
1092 color[i]= get_dc(s, mb_x, mb_y, i);
1094 // get previous score (cannot be cached due to OBMC)
1095 if(pass > 0 && (block->type&BLOCK_INTRA)){
1096 int color0[3]= {block->color[0], block->color[1], block->color[2]};
1097 check_block(s, mb_x, mb_y, color0, 1, obmc_edged, &best_rd);
1099 check_block_inter(s, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
1103 for(ref=0; ref < s->ref_frames; ref++){
1104 int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
1105 if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
1110 check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
1111 check_block_inter(s, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
1113 check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
1115 check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
1117 check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
1119 check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
1122 //FIXME avoid subpel interpolation / round to nearest integer
1124 int newx = block->mx;
1125 int newy = block->my;
1126 int dia_size = s->iterative_dia_size ? s->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
1128 for(i=0; i < dia_size; i++){
1130 dia_change |= check_block_inter(s, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
1131 dia_change |= check_block_inter(s, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
1132 dia_change |= check_block_inter(s, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
1133 dia_change |= check_block_inter(s, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
1139 static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
1142 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
1144 //FIXME or try the standard 2 pass qpel or similar
1146 mvr[0][0]= block->mx;
1147 mvr[0][1]= block->my;
1148 if(ref_rd > best_rd){
1155 check_block(s, mb_x, mb_y, color, 1, obmc_edged, &best_rd);
1156 //FIXME RD style color selection
1157 if(!same_block(block, &backup)){
1158 if(tb ) tb ->type &= ~BLOCK_OPT;
1159 if(lb ) lb ->type &= ~BLOCK_OPT;
1160 if(rb ) rb ->type &= ~BLOCK_OPT;
1161 if(bb ) bb ->type &= ~BLOCK_OPT;
1162 if(tlb) tlb->type &= ~BLOCK_OPT;
1163 if(trb) trb->type &= ~BLOCK_OPT;
1164 if(blb) blb->type &= ~BLOCK_OPT;
1165 if(brb) brb->type &= ~BLOCK_OPT;
1170 av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
1175 if(s->block_max_depth == 1){
1177 for(mb_y= 0; mb_y<b_height; mb_y+=2){
1178 for(mb_x= 0; mb_x<b_width; mb_x+=2){
1180 int best_rd, init_rd;
1181 const int index= mb_x + mb_y * b_stride;
1184 b[0]= &s->block[index];
1186 b[2]= b[0]+b_stride;
1188 if(same_block(b[0], b[1]) &&
1189 same_block(b[0], b[2]) &&
1190 same_block(b[0], b[3]))
1193 if(!s->me_cache_generation)
1194 memset(s->me_cache, 0, sizeof(s->me_cache));
1195 s->me_cache_generation += 1<<22;
1197 init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
1199 //FIXME more multiref search?
1200 check_4block_inter(s, mb_x, mb_y,
1201 (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
1202 (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
1205 if(!(b[i]->type&BLOCK_INTRA))
1206 check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
1208 if(init_rd != best_rd)
1212 av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
1216 static void encode_blocks(SnowContext *s, int search){
1221 if(s->motion_est == FF_ME_ITER && !s->keyframe && search)
1225 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
1226 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1230 if(s->motion_est == FF_ME_ITER || !search)
1231 encode_q_branch2(s, 0, x, y);
1233 encode_q_branch (s, 0, x, y);
1238 static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
1239 const int w= b->width;
1240 const int h= b->height;
1241 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1242 const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
1243 int x,y, thres1, thres2;
1245 if(s->qlog == LOSSLESS_QLOG){
1248 dst[x + y*stride]= src[x + y*stride];
1252 bias= bias ? 0 : (3*qmul)>>3;
1253 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
1259 int i= src[x + y*stride];
1261 if((unsigned)(i+thres1) > thres2){
1264 i/= qmul; //FIXME optimize
1265 dst[x + y*stride]= i;
1269 i/= qmul; //FIXME optimize
1270 dst[x + y*stride]= -i;
1273 dst[x + y*stride]= 0;
1279 int i= src[x + y*stride];
1281 if((unsigned)(i+thres1) > thres2){
1284 i= (i + bias) / qmul; //FIXME optimize
1285 dst[x + y*stride]= i;
1289 i= (i + bias) / qmul; //FIXME optimize
1290 dst[x + y*stride]= -i;
1293 dst[x + y*stride]= 0;
1299 static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
1300 const int w= b->width;
1301 const int h= b->height;
1302 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1303 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1304 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1307 if(s->qlog == LOSSLESS_QLOG) return;
1311 int i= src[x + y*stride];
1313 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
1315 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
1321 static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1322 const int w= b->width;
1323 const int h= b->height;
1326 for(y=h-1; y>=0; y--){
1327 for(x=w-1; x>=0; x--){
1328 int i= x + y*stride;
1332 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1333 else src[i] -= src[i - 1];
1335 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
1336 else src[i] -= src[i - 1];
1339 if(y) src[i] -= src[i - stride];
1345 static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1346 const int w= b->width;
1347 const int h= b->height;
1352 int i= x + y*stride;
1356 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1357 else src[i] += src[i - 1];
1359 if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
1360 else src[i] += src[i - 1];
1363 if(y) src[i] += src[i - stride];
1369 static void encode_qlogs(SnowContext *s){
1370 int plane_index, level, orientation;
1372 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1373 for(level=0; level<s->spatial_decomposition_count; level++){
1374 for(orientation=level ? 1:0; orientation<4; orientation++){
1375 if(orientation==2) continue;
1376 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
1382 static void encode_header(SnowContext *s){
1386 memset(kstate, MID_STATE, sizeof(kstate));
1388 put_rac(&s->c, kstate, s->keyframe);
1389 if(s->keyframe || s->always_reset){
1390 ff_snow_reset_contexts(s);
1391 s->last_spatial_decomposition_type=
1395 s->last_block_max_depth= 0;
1396 for(plane_index=0; plane_index<2; plane_index++){
1397 Plane *p= &s->plane[plane_index];
1400 memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
1404 put_symbol(&s->c, s->header_state, s->version, 0);
1405 put_rac(&s->c, s->header_state, s->always_reset);
1406 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
1407 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
1408 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1409 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
1410 if (s->nb_planes > 2) {
1411 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
1412 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
1414 put_rac(&s->c, s->header_state, s->spatial_scalability);
1415 // put_rac(&s->c, s->header_state, s->rate_scalability);
1416 put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
1423 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1424 Plane *p= &s->plane[plane_index];
1425 update_mc |= p->last_htaps != p->htaps;
1426 update_mc |= p->last_diag_mc != p->diag_mc;
1427 update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1429 put_rac(&s->c, s->header_state, update_mc);
1431 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1432 Plane *p= &s->plane[plane_index];
1433 put_rac(&s->c, s->header_state, p->diag_mc);
1434 put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
1435 for(i= p->htaps/2; i; i--)
1436 put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
1439 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1440 put_rac(&s->c, s->header_state, 1);
1441 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1444 put_rac(&s->c, s->header_state, 0);
1447 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
1448 put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
1449 put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
1450 put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
1451 put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
1455 static void update_last_header_values(SnowContext *s){
1459 for(plane_index=0; plane_index<2; plane_index++){
1460 Plane *p= &s->plane[plane_index];
1461 p->last_diag_mc= p->diag_mc;
1462 p->last_htaps = p->htaps;
1463 memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1467 s->last_spatial_decomposition_type = s->spatial_decomposition_type;
1468 s->last_qlog = s->qlog;
1469 s->last_qbias = s->qbias;
1470 s->last_mv_scale = s->mv_scale;
1471 s->last_block_max_depth = s->block_max_depth;
1472 s->last_spatial_decomposition_count = s->spatial_decomposition_count;
1475 static int qscale2qlog(int qscale){
1476 return lrint(QROOT*log2(qscale / (float)FF_QP2LAMBDA))
1477 + 61*QROOT/8; ///< 64 > 60
1480 static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
1482 /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
1483 * FIXME we know exact mv bits at this point,
1484 * but ratecontrol isn't set up to include them. */
1485 uint32_t coef_sum= 0;
1486 int level, orientation, delta_qlog;
1488 for(level=0; level<s->spatial_decomposition_count; level++){
1489 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1490 SubBand *b= &s->plane[0].band[level][orientation];
1491 IDWTELEM *buf= b->ibuf;
1492 const int w= b->width;
1493 const int h= b->height;
1494 const int stride= b->stride;
1495 const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
1496 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1497 const int qdiv= (1<<16)/qmul;
1499 //FIXME this is ugly
1502 buf[x+y*stride]= b->buf[x+y*stride];
1504 decorrelate(s, b, buf, stride, 1, 0);
1507 coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
1511 /* ugly, ratecontrol just takes a sqrt again */
1512 av_assert0(coef_sum < INT_MAX);
1513 coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
1515 if(pict->pict_type == AV_PICTURE_TYPE_I){
1516 s->m.current_picture.mb_var_sum= coef_sum;
1517 s->m.current_picture.mc_mb_var_sum= 0;
1519 s->m.current_picture.mc_mb_var_sum= coef_sum;
1520 s->m.current_picture.mb_var_sum= 0;
1523 pict->quality= ff_rate_estimate_qscale(&s->m, 1);
1524 if (pict->quality < 0)
1526 s->lambda= pict->quality * 3/2;
1527 delta_qlog= qscale2qlog(pict->quality) - s->qlog;
1528 s->qlog+= delta_qlog;
1532 static void calculate_visual_weight(SnowContext *s, Plane *p){
1533 int width = p->width;
1534 int height= p->height;
1535 int level, orientation, x, y;
1537 for(level=0; level<s->spatial_decomposition_count; level++){
1538 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1539 SubBand *b= &p->band[level][orientation];
1540 IDWTELEM *ibuf= b->ibuf;
1543 memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
1544 ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
1545 ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
1546 for(y=0; y<height; y++){
1547 for(x=0; x<width; x++){
1548 int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
1553 b->qlog= (int)(QROOT * log2(352256.0/sqrt(error)) + 0.5);
1558 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1559 const AVFrame *pict, int *got_packet)
1561 SnowContext *s = avctx->priv_data;
1562 RangeCoder * const c= &s->c;
1564 const int width= s->avctx->width;
1565 const int height= s->avctx->height;
1566 int level, orientation, plane_index, i, y, ret;
1567 uint8_t rc_header_bak[sizeof(s->header_state)];
1568 uint8_t rc_block_bak[sizeof(s->block_state)];
1570 if ((ret = ff_alloc_packet2(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1573 ff_init_range_encoder(c, pkt->data, pkt->size);
1574 ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1576 for(i=0; i < s->nb_planes; i++){
1577 int hshift= i ? s->chroma_h_shift : 0;
1578 int vshift= i ? s->chroma_v_shift : 0;
1579 for(y=0; y<AV_CEIL_RSHIFT(height, vshift); y++)
1580 memcpy(&s->input_picture->data[i][y * s->input_picture->linesize[i]],
1581 &pict->data[i][y * pict->linesize[i]],
1582 AV_CEIL_RSHIFT(width, hshift));
1583 s->mpvencdsp.draw_edges(s->input_picture->data[i], s->input_picture->linesize[i],
1584 AV_CEIL_RSHIFT(width, hshift), AV_CEIL_RSHIFT(height, vshift),
1585 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1586 EDGE_TOP | EDGE_BOTTOM);
1590 pic = s->input_picture;
1591 pic->pict_type = pict->pict_type;
1592 pic->quality = pict->quality;
1594 s->m.picture_number= avctx->frame_number;
1595 if(avctx->flags&AV_CODEC_FLAG_PASS2){
1596 s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
1597 s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
1598 if(!(avctx->flags&AV_CODEC_FLAG_QSCALE)) {
1599 pic->quality = ff_rate_estimate_qscale(&s->m, 0);
1600 if (pic->quality < 0)
1604 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
1605 s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1608 if(s->pass1_rc && avctx->frame_number == 0)
1609 pic->quality = 2*FF_QP2LAMBDA;
1611 s->qlog = qscale2qlog(pic->quality);
1612 s->lambda = pic->quality * 3/2;
1614 if (s->qlog < 0 || (!pic->quality && (avctx->flags & AV_CODEC_FLAG_QSCALE))) {
1615 s->qlog= LOSSLESS_QLOG;
1617 }//else keep previous frame's qlog until after motion estimation
1619 if (s->current_picture->data[0]) {
1620 int w = s->avctx->width;
1621 int h = s->avctx->height;
1623 s->mpvencdsp.draw_edges(s->current_picture->data[0],
1624 s->current_picture->linesize[0], w , h ,
1625 EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
1626 if (s->current_picture->data[2]) {
1627 s->mpvencdsp.draw_edges(s->current_picture->data[1],
1628 s->current_picture->linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
1629 EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
1630 s->mpvencdsp.draw_edges(s->current_picture->data[2],
1631 s->current_picture->linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
1632 EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
1637 ff_snow_frame_start(s);
1638 #if FF_API_CODED_FRAME
1639 FF_DISABLE_DEPRECATION_WARNINGS
1640 av_frame_unref(avctx->coded_frame);
1641 ret = av_frame_ref(avctx->coded_frame, s->current_picture);
1642 FF_ENABLE_DEPRECATION_WARNINGS
1647 s->m.current_picture_ptr= &s->m.current_picture;
1648 s->m.current_picture.f = s->current_picture;
1649 s->m.current_picture.f->pts = pict->pts;
1650 if(pic->pict_type == AV_PICTURE_TYPE_P){
1651 int block_width = (width +15)>>4;
1652 int block_height= (height+15)>>4;
1653 int stride= s->current_picture->linesize[0];
1655 av_assert0(s->current_picture->data[0]);
1656 av_assert0(s->last_picture[0]->data[0]);
1658 s->m.avctx= s->avctx;
1659 s->m. last_picture.f = s->last_picture[0];
1660 s->m. new_picture.f = s->input_picture;
1661 s->m. last_picture_ptr= &s->m. last_picture;
1662 s->m.linesize = stride;
1663 s->m.uvlinesize= s->current_picture->linesize[1];
1665 s->m.height= height;
1666 s->m.mb_width = block_width;
1667 s->m.mb_height= block_height;
1668 s->m.mb_stride= s->m.mb_width+1;
1669 s->m.b8_stride= 2*s->m.mb_width+1;
1671 s->m.pict_type = pic->pict_type;
1672 s->m.motion_est= s->motion_est;
1673 s->m.me.scene_change_score=0;
1674 s->m.me.dia_size = avctx->dia_size;
1675 s->m.quarter_sample= (s->avctx->flags & AV_CODEC_FLAG_QPEL)!=0;
1676 s->m.out_format= FMT_H263;
1677 s->m.unrestricted_mv= 1;
1679 s->m.lambda = s->lambda;
1680 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
1681 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
1683 s->m.mecc= s->mecc; //move
1684 s->m.qdsp= s->qdsp; //move
1685 s->m.hdsp = s->hdsp;
1687 s->hdsp = s->m.hdsp;
1692 memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
1693 memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
1698 s->spatial_decomposition_count= 5;
1700 while( !(width >>(s->chroma_h_shift + s->spatial_decomposition_count))
1701 || !(height>>(s->chroma_v_shift + s->spatial_decomposition_count)))
1702 s->spatial_decomposition_count--;
1704 if (s->spatial_decomposition_count <= 0) {
1705 av_log(avctx, AV_LOG_ERROR, "Resolution too low\n");
1706 return AVERROR(EINVAL);
1709 s->m.pict_type = pic->pict_type;
1710 s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
1712 ff_snow_common_init_after_header(avctx);
1714 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1715 for(plane_index=0; plane_index < s->nb_planes; plane_index++){
1716 calculate_visual_weight(s, &s->plane[plane_index]);
1721 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
1722 encode_blocks(s, 1);
1723 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
1725 for(plane_index=0; plane_index < s->nb_planes; plane_index++){
1726 Plane *p= &s->plane[plane_index];
1730 // int bits= put_bits_count(&s->c.pb);
1732 if (!s->memc_only) {
1734 if(pict->data[plane_index]) //FIXME gray hack
1737 s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
1740 predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
1742 #if FF_API_PRIVATE_OPT
1743 FF_DISABLE_DEPRECATION_WARNINGS
1744 if(s->avctx->scenechange_threshold)
1745 s->scenechange_threshold = s->avctx->scenechange_threshold;
1746 FF_ENABLE_DEPRECATION_WARNINGS
1750 && pic->pict_type == AV_PICTURE_TYPE_P
1751 && !(avctx->flags&AV_CODEC_FLAG_PASS2)
1752 && s->m.me.scene_change_score > s->scenechange_threshold){
1753 ff_init_range_encoder(c, pkt->data, pkt->size);
1754 ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1755 pic->pict_type= AV_PICTURE_TYPE_I;
1757 s->current_picture->key_frame=1;
1761 if(s->qlog == LOSSLESS_QLOG){
1764 s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
1770 s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
1775 ff_spatial_dwt(s->spatial_dwt_buffer, s->temp_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1777 if(s->pass1_rc && plane_index==0){
1778 int delta_qlog = ratecontrol_1pass(s, pic);
1779 if (delta_qlog <= INT_MIN)
1782 //reordering qlog in the bitstream would eliminate this reset
1783 ff_init_range_encoder(c, pkt->data, pkt->size);
1784 memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
1785 memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
1787 encode_blocks(s, 0);
1791 for(level=0; level<s->spatial_decomposition_count; level++){
1792 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1793 SubBand *b= &p->band[level][orientation];
1795 quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
1797 decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
1798 if (!s->no_bitstream)
1799 encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
1800 av_assert0(b->parent==NULL || b->parent->stride == b->stride*2);
1802 correlate(s, b, b->ibuf, b->stride, 1, 0);
1806 for(level=0; level<s->spatial_decomposition_count; level++){
1807 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1808 SubBand *b= &p->band[level][orientation];
1810 dequantize(s, b, b->ibuf, b->stride);
1814 ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1815 if(s->qlog == LOSSLESS_QLOG){
1818 s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
1822 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1825 if(pic->pict_type == AV_PICTURE_TYPE_I){
1828 s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x]=
1829 pict->data[plane_index][y*pict->linesize[plane_index] + x];
1833 memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
1834 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1837 if(s->avctx->flags&AV_CODEC_FLAG_PSNR){
1840 if(pict->data[plane_index]) //FIXME gray hack
1843 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];
1847 s->avctx->error[plane_index] += error;
1848 s->encoding_error[plane_index] = error;
1854 update_last_header_values(s);
1856 ff_snow_release_buffer(avctx);
1858 s->current_picture->coded_picture_number = avctx->frame_number;
1859 s->current_picture->pict_type = pic->pict_type;
1860 s->current_picture->quality = pic->quality;
1861 s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
1862 s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
1863 s->m.current_picture.f->display_picture_number =
1864 s->m.current_picture.f->coded_picture_number = avctx->frame_number;
1865 s->m.current_picture.f->quality = pic->quality;
1866 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
1868 if (ff_rate_estimate_qscale(&s->m, 0) < 0)
1870 if(avctx->flags&AV_CODEC_FLAG_PASS1)
1871 ff_write_pass1_stats(&s->m);
1872 s->m.last_pict_type = s->m.pict_type;
1873 #if FF_API_STAT_BITS
1874 FF_DISABLE_DEPRECATION_WARNINGS
1875 avctx->frame_bits = s->m.frame_bits;
1876 avctx->mv_bits = s->m.mv_bits;
1877 avctx->misc_bits = s->m.misc_bits;
1878 avctx->p_tex_bits = s->m.p_tex_bits;
1879 FF_ENABLE_DEPRECATION_WARNINGS
1884 ff_side_data_set_encoder_stats(pkt, s->current_picture->quality,
1886 (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
1887 s->current_picture->pict_type);
1889 #if FF_API_ERROR_FRAME
1890 FF_DISABLE_DEPRECATION_WARNINGS
1891 memcpy(s->current_picture->error, s->encoding_error, sizeof(s->encoding_error));
1892 FF_ENABLE_DEPRECATION_WARNINGS
1895 pkt->size = ff_rac_terminate(c);
1896 if (s->current_picture->key_frame)
1897 pkt->flags |= AV_PKT_FLAG_KEY;
1903 static av_cold int encode_end(AVCodecContext *avctx)
1905 SnowContext *s = avctx->priv_data;
1907 ff_snow_common_end(s);
1908 ff_rate_control_uninit(&s->m);
1909 av_frame_free(&s->input_picture);
1910 av_freep(&avctx->stats_out);
1915 #define OFFSET(x) offsetof(SnowContext, x)
1916 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1917 static const AVOption options[] = {
1918 {"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" },
1919 { "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, VE, "motion_est" },
1920 { "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, VE, "motion_est" },
1921 { "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, VE, "motion_est" },
1922 { "iter", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ITER }, 0, 0, VE, "motion_est" },
1923 { "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 },
1924 { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1925 { "intra_penalty", "Penalty for intra blocks in block decission", OFFSET(intra_penalty), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1926 { "iterative_dia_size", "Dia size for the iterative ME", OFFSET(iterative_dia_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1927 { "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, VE },
1928 { "pred", "Spatial decomposition type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, DWT_97, DWT_53, VE, "pred" },
1929 { "dwt97", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1930 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
1934 static const AVClass snowenc_class = {
1935 .class_name = "snow encoder",
1936 .item_name = av_default_item_name,
1938 .version = LIBAVUTIL_VERSION_INT,
1941 AVCodec ff_snow_encoder = {
1943 .long_name = NULL_IF_CONFIG_SMALL("Snow"),
1944 .type = AVMEDIA_TYPE_VIDEO,
1945 .id = AV_CODEC_ID_SNOW,
1946 .priv_data_size = sizeof(SnowContext),
1947 .init = encode_init,
1948 .encode2 = encode_frame,
1949 .close = encode_end,
1950 .pix_fmts = (const enum AVPixelFormat[]){
1951 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV444P,
1955 .priv_class = &snowenc_class,
1956 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1957 FF_CODEC_CAP_INIT_CLEANUP,