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