FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/snowenc.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 1089 1319 82.6%
Functions: 30 33 90.9%
Branches: 636 838 75.9%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/emms.h"
22 #include "libavutil/intmath.h"
23 #include "libavutil/libm.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avcodec.h"
29 #include "codec_internal.h"
30 #include "encode.h"
31 #include "internal.h" //For AVCodecInternal.recon_frame
32 #include "me_cmp.h"
33 #include "packet_internal.h"
34 #include "qpeldsp.h"
35 #include "snow_dwt.h"
36 #include "snow.h"
37
38 #include "rangecoder.h"
39 #include "mathops.h"
40
41 #include "mpegvideo.h"
42 #include "h263enc.h"
43
44 #define FF_ME_ITER 3
45
46 typedef struct SnowEncContext {
47 SnowContext com;
48 QpelDSPContext qdsp;
49 MpegvideoEncDSPContext mpvencdsp;
50
51 int lambda;
52 int lambda2;
53 int pass1_rc;
54
55 int pred;
56 int memc_only;
57 int no_bitstream;
58 int intra_penalty;
59 int motion_est;
60 int iterative_dia_size;
61 int scenechange_threshold;
62
63 MECmpContext mecc;
64 MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
65 #define ME_CACHE_SIZE 1024
66 unsigned me_cache[ME_CACHE_SIZE];
67 unsigned me_cache_generation;
68
69 uint64_t encoding_error[SNOW_MAX_PLANES];
70 } SnowEncContext;
71
72 557820 static void init_ref(MotionEstContext *c, const uint8_t *const src[3],
73 uint8_t *const ref[3], uint8_t *const ref2[3],
74 int x, int y, int ref_index)
75 {
76 557820 SnowContext *s = c->avctx->priv_data;
77 557820 const int offset[3] = {
78 557820 y*c-> stride + x,
79 557820 ((y*c->uvstride + x) >> s->chroma_h_shift),
80 557820 ((y*c->uvstride + x) >> s->chroma_h_shift),
81 };
82
2/2
✓ Branch 0 taken 1673460 times.
✓ Branch 1 taken 557820 times.
2231280 for (int i = 0; i < 3; i++) {
83 1673460 c->src[0][i] = src [i];
84 1673460 c->ref[0][i] = ref [i] + offset[i];
85 }
86 av_assert2(!ref_index);
87 557820 }
88
89 2810763 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
90 {
91
2/2
✓ Branch 0 taken 2212924 times.
✓ Branch 1 taken 597839 times.
2810763 if (v) {
92 2212924 const int a = FFABS(v);
93 2212924 const int e = av_log2(a);
94 2212924 const int el = FFMIN(e, 10);
95 int i;
96
97 2212924 put_rac(c, state + 0, 0);
98
99
2/2
✓ Branch 0 taken 5323053 times.
✓ Branch 1 taken 2212924 times.
7535977 for (i = 0; i < el; i++)
100 5323053 put_rac(c, state + 1 + i, 1); //1..10
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2212924 times.
2212924 for(; i < e; i++)
102 put_rac(c, state + 1 + 9, 1); //1..10
103
1/2
✓ Branch 0 taken 2212924 times.
✗ Branch 1 not taken.
2212924 put_rac(c, state + 1 + FFMIN(i, 9), 0);
104
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2212924 times.
2212924 for (i = e - 1; i >= el; i--)
106 put_rac(c, state + 22 + 9, (a >> i) & 1); //22..31
107
2/2
✓ Branch 0 taken 5323053 times.
✓ Branch 1 taken 2212924 times.
7535977 for(; i >= 0; i--)
108 5323053 put_rac(c, state + 22 + i, (a >> i) & 1); //22..31
109
110
2/2
✓ Branch 0 taken 2212501 times.
✓ Branch 1 taken 423 times.
2212924 if (is_signed)
111 2212501 put_rac(c, state + 11 + el, v < 0); //11..21
112 } else {
113 597839 put_rac(c, state + 0, 1);
114 }
115 2810763 }
116
117 16307034 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2)
118 {
119
2/2
✓ Branch 0 taken 6741439 times.
✓ Branch 1 taken 9565595 times.
16307034 int r = log2 >= 0 ? 1<<log2 : 1;
120
121 av_assert2(v >= 0);
122 av_assert2(log2 >= -4);
123
124
2/2
✓ Branch 0 taken 11893173 times.
✓ Branch 1 taken 16307034 times.
28200207 while (v >= r) {
125 11893173 put_rac(c, state + 4 + log2, 1);
126 11893173 v -= r;
127 11893173 log2++;
128
2/2
✓ Branch 0 taken 8610057 times.
✓ Branch 1 taken 3283116 times.
11893173 if (log2 > 0) r += r;
129 }
130 16307034 put_rac(c, state + 4 + log2, 0);
131
132
2/2
✓ Branch 0 taken 16640157 times.
✓ Branch 1 taken 16307034 times.
32947191 for (int i = log2 - 1; i >= 0; i--)
133 16640157 put_rac(c, state + 31 - i, (v >> i) & 1);
134 16307034 }
135
136 521 static int get_encode_buffer(SnowContext *s, AVFrame *frame)
137 {
138 int ret;
139
140 521 frame->width = s->avctx->width + 2 * EDGE_WIDTH;
141 521 frame->height = s->avctx->height + 2 * EDGE_WIDTH;
142
143 521 ret = ff_encode_alloc_frame(s->avctx, frame);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 521 times.
521 if (ret < 0)
145 return ret;
146
2/2
✓ Branch 0 taken 1563 times.
✓ Branch 1 taken 521 times.
2084 for (int i = 0; frame->data[i]; i++) {
147
2/2
✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 521 times.
1563 int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
148 1563 frame->linesize[i] +
149
2/2
✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 521 times.
1563 (EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
150 1563 frame->data[i] += offset;
151 }
152 521 frame->width = s->avctx->width;
153 521 frame->height = s->avctx->height;
154
155 521 return 0;
156 }
157
158 11 static av_cold int encode_init(AVCodecContext *avctx)
159 {
160 11 SnowEncContext *const enc = avctx->priv_data;
161 11 SnowContext *const s = &enc->com;
162 11 MpegEncContext *const mpv = &enc->m;
163 int plane_index, ret;
164 int i;
165
166
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 if (enc->pred == DWT_97
167
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 && (avctx->flags & AV_CODEC_FLAG_QSCALE)
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 && avctx->global_quality == 0){
169 av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
170 return AVERROR(EINVAL);
171 }
172
173 11 s->spatial_decomposition_type = enc->pred; //FIXME add decorrelator type r transform_type
174
175
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
176 11 s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
177
178
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 11 times.
44 for(plane_index=0; plane_index<3; plane_index++){
179 33 s->plane[plane_index].diag_mc= 1;
180 33 s->plane[plane_index].htaps= 6;
181 33 s->plane[plane_index].hcoeff[0]= 40;
182 33 s->plane[plane_index].hcoeff[1]= -10;
183 33 s->plane[plane_index].hcoeff[2]= 2;
184 33 s->plane[plane_index].fast_mc= 1;
185 }
186
187 // Must be before ff_snow_common_init()
188 11 ff_hpeldsp_init(&s->hdsp, avctx->flags);
189
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if ((ret = ff_snow_common_init(avctx)) < 0) {
190 return ret;
191 }
192
193 #define mcf(dx,dy)\
194 enc->qdsp.put_qpel_pixels_tab [0][dy+dx/4]=\
195 enc->qdsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
196 s->h264qpel.put_h264_qpel_pixels_tab[0][dy+dx/4];\
197 enc->qdsp.put_qpel_pixels_tab [1][dy+dx/4]=\
198 enc->qdsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
199 s->h264qpel.put_h264_qpel_pixels_tab[1][dy+dx/4];
200
201 11 mcf( 0, 0)
202 11 mcf( 4, 0)
203 11 mcf( 8, 0)
204 11 mcf(12, 0)
205 11 mcf( 0, 4)
206 11 mcf( 4, 4)
207 11 mcf( 8, 4)
208 11 mcf(12, 4)
209 11 mcf( 0, 8)
210 11 mcf( 4, 8)
211 11 mcf( 8, 8)
212 11 mcf(12, 8)
213 11 mcf( 0,12)
214 11 mcf( 4,12)
215 11 mcf( 8,12)
216 11 mcf(12,12)
217
218 11 ff_me_cmp_init(&enc->mecc, avctx);
219 11 ff_mpegvideoencdsp_init(&enc->mpvencdsp, avctx);
220
221 11 ff_snow_alloc_blocks(s);
222
223 11 s->version=0;
224
225 11 mpv->avctx = avctx;
226 11 mpv->bit_rate= avctx->bit_rate;
227 11 mpv->lmin = avctx->mb_lmin;
228 11 mpv->lmax = avctx->mb_lmax;
229 11 mpv->mb_num = (avctx->width * avctx->height + 255) / 256; // For ratecontrol
230
231 11 mpv->me.temp =
232 11 mpv->me.scratchpad = av_calloc(avctx->width + 64, 2*16*2*sizeof(uint8_t));
233 11 mpv->sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
234 11 mpv->me.map = av_mallocz(2 * ME_MAP_SIZE * sizeof(*mpv->me.map));
235
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
11 if (!mpv->me.scratchpad || !mpv->me.map || !mpv->sc.obmc_scratchpad)
236 return AVERROR(ENOMEM);
237 11 mpv->me.score_map = mpv->me.map + ME_MAP_SIZE;
238
239 11 ff_h263_encode_init(mpv); //mv_penalty
240
241 11 s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if(avctx->flags&AV_CODEC_FLAG_PASS1){
244 if(!avctx->stats_out)
245 avctx->stats_out = av_mallocz(256);
246
247 if (!avctx->stats_out)
248 return AVERROR(ENOMEM);
249 }
250
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&AV_CODEC_FLAG_QSCALE)){
251 ret = ff_rate_control_init(mpv);
252 if(ret < 0)
253 return ret;
254 }
255 11 enc->pass1_rc = !(avctx->flags & (AV_CODEC_FLAG_QSCALE|AV_CODEC_FLAG_PASS2));
256
257
1/3
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
11 switch(avctx->pix_fmt){
258 11 case AV_PIX_FMT_YUV444P:
259 // case AV_PIX_FMT_YUV422P:
260 case AV_PIX_FMT_YUV420P:
261 // case AV_PIX_FMT_YUV411P:
262 case AV_PIX_FMT_YUV410P:
263 11 s->nb_planes = 3;
264 11 s->colorspace_type= 0;
265 11 break;
266 case AV_PIX_FMT_GRAY8:
267 s->nb_planes = 1;
268 s->colorspace_type = 1;
269 break;
270 /* case AV_PIX_FMT_RGB32:
271 s->colorspace= 1;
272 break;*/
273 }
274
275 11 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
276 &s->chroma_v_shift);
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret)
278 return ret;
279
280 11 ret = ff_set_cmp(&enc->mecc, enc->mecc.me_cmp, s->avctx->me_cmp);
281 11 ret |= ff_set_cmp(&enc->mecc, enc->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
283 return AVERROR(EINVAL);
284
285 11 s->input_picture = av_frame_alloc();
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!s->input_picture)
287 return AVERROR(ENOMEM);
288
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if ((ret = get_encode_buffer(s, s->input_picture)) < 0)
290 return ret;
291
292
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 if (enc->motion_est == FF_ME_ITER) {
293 6 int size= s->b_width * s->b_height << 2*s->block_max_depth;
294
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for(i=0; i<s->max_ref_frames; i++){
295 6 s->ref_mvs[i] = av_calloc(size, sizeof(*s->ref_mvs[i]));
296 6 s->ref_scores[i] = av_calloc(size, sizeof(*s->ref_scores[i]));
297
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!s->ref_mvs[i] || !s->ref_scores[i])
298 return AVERROR(ENOMEM);
299 }
300 }
301
302 11 return 0;
303 }
304
305 //near copy & paste from dsputil, FIXME
306 1673460 static int pix_sum(const uint8_t * pix, int line_size, int w, int h)
307 {
308 int s, i, j;
309
310 1673460 s = 0;
311
2/2
✓ Branch 0 taken 11422080 times.
✓ Branch 1 taken 1673460 times.
13095540 for (i = 0; i < h; i++) {
312
2/2
✓ Branch 0 taken 98496000 times.
✓ Branch 1 taken 11422080 times.
109918080 for (j = 0; j < w; j++) {
313 98496000 s += pix[0];
314 98496000 pix ++;
315 }
316 11422080 pix += line_size - w;
317 }
318 1673460 return s;
319 }
320
321 //near copy & paste from dsputil, FIXME
322 557820 static int pix_norm1(const uint8_t * pix, int line_size, int w)
323 {
324 int s, i, j;
325 557820 const uint32_t *sq = ff_square_tab + 256;
326
327 557820 s = 0;
328
2/2
✓ Branch 0 taken 5711040 times.
✓ Branch 1 taken 557820 times.
6268860 for (i = 0; i < w; i++) {
329
2/2
✓ Branch 0 taken 65664000 times.
✓ Branch 1 taken 5711040 times.
71375040 for (j = 0; j < w; j ++) {
330 65664000 s += sq[pix[0]];
331 65664000 pix ++;
332 }
333 5711040 pix += line_size - w;
334 }
335 557820 return s;
336 }
337
338 1979900 static inline int get_penalty_factor(int lambda, int lambda2, int type){
339
3/7
✓ Branch 0 taken 1374300 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 323720 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 281880 times.
✗ Branch 6 not taken.
1979900 switch(type&0xFF){
340 1374300 default:
341 case FF_CMP_SAD:
342 1374300 return lambda>>FF_LAMBDA_SHIFT;
343 case FF_CMP_DCT:
344 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
345 case FF_CMP_W53:
346 return (4*lambda)>>(FF_LAMBDA_SHIFT);
347 323720 case FF_CMP_W97:
348 323720 return (2*lambda)>>(FF_LAMBDA_SHIFT);
349 case FF_CMP_SATD:
350 case FF_CMP_DCT264:
351 return (2*lambda)>>FF_LAMBDA_SHIFT;
352 281880 case FF_CMP_RD:
353 case FF_CMP_PSNR:
354 case FF_CMP_SSE:
355 case FF_CMP_NSSE:
356 281880 return lambda2>>FF_LAMBDA_SHIFT;
357 case FF_CMP_BIT:
358 return 1;
359 }
360 }
361
362 //FIXME copy&paste
363 #define P_LEFT P[1]
364 #define P_TOP P[2]
365 #define P_TOPRIGHT P[3]
366 #define P_MEDIAN P[4]
367 #define P_MV1 P[9]
368 #define FLAG_QPEL 1 //must be 1
369
370 567000 static int encode_q_branch(SnowEncContext *enc, int level, int x, int y)
371 {
372 567000 SnowContext *const s = &enc->com;
373 567000 MotionEstContext *const c = &enc->m.me;
374 uint8_t p_buffer[1024];
375 uint8_t i_buffer[1024];
376 uint8_t p_state[sizeof(s->block_state)];
377 uint8_t i_state[sizeof(s->block_state)];
378 RangeCoder pc, ic;
379 567000 uint8_t *pbbak= s->c.bytestream;
380 567000 uint8_t *pbbak_start= s->c.bytestream_start;
381 int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
382 567000 const int w= s->b_width << s->block_max_depth;
383 567000 const int h= s->b_height << s->block_max_depth;
384 567000 const int rem_depth= s->block_max_depth - level;
385 567000 const int index= (x + y*w) << rem_depth;
386 567000 const int block_w= 1<<(LOG2_MB_SIZE - level);
387 567000 int trx= (x+1)<<rem_depth;
388 567000 int try= (y+1)<<rem_depth;
389
2/2
✓ Branch 0 taken 554112 times.
✓ Branch 1 taken 12888 times.
567000 const BlockNode *left = x ? &s->block[index-1] : &null_block;
390
2/2
✓ Branch 0 taken 550290 times.
✓ Branch 1 taken 16710 times.
567000 const BlockNode *top = y ? &s->block[index-w] : &null_block;
391
2/2
✓ Branch 0 taken 554112 times.
✓ Branch 1 taken 12888 times.
567000 const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
392
2/2
✓ Branch 0 taken 550290 times.
✓ Branch 1 taken 16710 times.
567000 const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
393
4/4
✓ Branch 0 taken 550290 times.
✓ Branch 1 taken 16710 times.
✓ Branch 2 taken 538046 times.
✓ Branch 3 taken 12244 times.
567000 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
394
8/8
✓ Branch 0 taken 550290 times.
✓ Branch 1 taken 16710 times.
✓ Branch 2 taken 538046 times.
✓ Branch 3 taken 12244 times.
✓ Branch 4 taken 263951 times.
✓ Branch 5 taken 274095 times.
✓ Branch 6 taken 74130 times.
✓ Branch 7 taken 189821 times.
567000 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
395 567000 int pl = left->color[0];
396 567000 int pcb= left->color[1];
397 567000 int pcr= left->color[2];
398 int pmx, pmy;
399 567000 int mx=0, my=0;
400 int l,cr,cb;
401 567000 const int stride= s->current_picture->linesize[0];
402 567000 const int uvstride= s->current_picture->linesize[1];
403 567000 const uint8_t *const current_data[3] = { s->input_picture->data[0] + (x + y* stride)*block_w,
404 567000 s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
405 567000 s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
406 int P[10][2];
407 int16_t last_mv[3][2];
408 567000 int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
409 567000 const int shift= 1+qpel;
410 567000 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
411 567000 int mx_context= av_log2(2*FFABS(left->mx - top->mx));
412 567000 int my_context= av_log2(2*FFABS(left->my - top->my));
413 567000 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
414 int ref, best_ref, ref_score, ref_mx, ref_my;
415
416 av_assert0(sizeof(s->block_state) >= 256);
417
2/2
✓ Branch 0 taken 9180 times.
✓ Branch 1 taken 557820 times.
567000 if(s->keyframe){
418 9180 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
419 9180 return 0;
420 }
421
422 // clip predictors / edge ?
423
424 557820 P_LEFT[0]= left->mx;
425 557820 P_LEFT[1]= left->my;
426 557820 P_TOP [0]= top->mx;
427 557820 P_TOP [1]= top->my;
428 557820 P_TOPRIGHT[0]= tr->mx;
429 557820 P_TOPRIGHT[1]= tr->my;
430
431 557820 last_mv[0][0]= s->block[index].mx;
432 557820 last_mv[0][1]= s->block[index].my;
433 557820 last_mv[1][0]= right->mx;
434 557820 last_mv[1][1]= right->my;
435 557820 last_mv[2][0]= bottom->mx;
436 557820 last_mv[2][1]= bottom->my;
437
438 557820 enc->m.mb_stride = 2;
439 557820 enc->m.mb_x =
440 557820 enc->m.mb_y = 0;
441 557820 c->skip= 0;
442
443 av_assert1(c-> stride == stride);
444 av_assert1(c->uvstride == uvstride);
445
446 557820 c->penalty_factor = get_penalty_factor(enc->lambda, enc->lambda2, c->avctx->me_cmp);
447 557820 c->sub_penalty_factor= get_penalty_factor(enc->lambda, enc->lambda2, c->avctx->me_sub_cmp);
448 557820 c->mb_penalty_factor = get_penalty_factor(enc->lambda, enc->lambda2, c->avctx->mb_cmp);
449 557820 c->current_mv_penalty = c->mv_penalty[enc->m.f_code=1] + MAX_DMV;
450
451 557820 c->xmin = - x*block_w - 16+3;
452 557820 c->ymin = - y*block_w - 16+3;
453 557820 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
454 557820 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
455
456
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 557819 times.
557820 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
457
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 557643 times.
557820 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557820 times.
557820 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
459
2/2
✓ Branch 0 taken 1005 times.
✓ Branch 1 taken 556815 times.
557820 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
460
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 557819 times.
557820 if(P_TOPRIGHT[0] < (c->xmin * (1<<shift))) P_TOPRIGHT[0]= (c->xmin * (1<<shift));
461
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 557819 times.
557820 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
462
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 556813 times.
557820 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
463
464 557820 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
465 557820 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
466
467
2/2
✓ Branch 0 taken 16290 times.
✓ Branch 1 taken 541530 times.
557820 if (!y) {
468 16290 c->pred_x= P_LEFT[0];
469 16290 c->pred_y= P_LEFT[1];
470 } else {
471 541530 c->pred_x = P_MEDIAN[0];
472 541530 c->pred_y = P_MEDIAN[1];
473 }
474
475 557820 score= INT_MAX;
476 557820 best_ref= 0;
477
2/2
✓ Branch 0 taken 557820 times.
✓ Branch 1 taken 557820 times.
1115640 for(ref=0; ref<s->ref_frames; ref++){
478 557820 init_ref(c, current_data, s->last_picture[ref]->data, NULL, block_w*x, block_w*y, 0);
479
480 557820 ref_score= ff_epzs_motion_search(&enc->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
481 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
482
483 av_assert2(ref_mx >= c->xmin);
484 av_assert2(ref_mx <= c->xmax);
485 av_assert2(ref_my >= c->ymin);
486 av_assert2(ref_my <= c->ymax);
487
488 557820 ref_score= c->sub_motion_search(&enc->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
489 557820 ref_score= ff_get_mb_score(&enc->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
490 557820 ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
491
2/2
✓ Branch 0 taken 8640 times.
✓ Branch 1 taken 549180 times.
557820 if(s->ref_mvs[ref]){
492 8640 s->ref_mvs[ref][index][0]= ref_mx;
493 8640 s->ref_mvs[ref][index][1]= ref_my;
494 8640 s->ref_scores[ref][index]= ref_score;
495 }
496
1/2
✓ Branch 0 taken 557820 times.
✗ Branch 1 not taken.
557820 if(score > ref_score){
497 557820 score= ref_score;
498 557820 best_ref= ref;
499 557820 mx= ref_mx;
500 557820 my= ref_my;
501 }
502 }
503 //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
504
505 // subpel search
506 557820 base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
507 557820 pc= s->c;
508 557820 pc.bytestream_start=
509 557820 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
510 557820 memcpy(p_state, s->block_state, sizeof(s->block_state));
511
512
2/2
✓ Branch 0 taken 100440 times.
✓ Branch 1 taken 457380 times.
557820 if(level!=s->block_max_depth)
513 100440 put_rac(&pc, &p_state[4 + s_context], 1);
514 557820 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557820 times.
557820 if(s->ref_frames > 1)
516 put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
517 557820 pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557820 times.
557820 put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 557820 times.
557820 put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
520 557820 p_len= pc.bytestream - pc.bytestream_start;
521 557820 score += (enc->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
522
523 557820 block_s= block_w*block_w;
524 557820 sum = pix_sum(current_data[0], stride, block_w, block_w);
525 557820 l= (sum + block_s/2)/block_s;
526 557820 iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
527
528
1/2
✓ Branch 0 taken 557820 times.
✗ Branch 1 not taken.
557820 if (s->nb_planes > 2) {
529 557820 block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
530 557820 sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
531 557820 cb= (sum + block_s/2)/block_s;
532 // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
533 557820 sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
534 557820 cr= (sum + block_s/2)/block_s;
535 // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
536 }else
537 cb = cr = 0;
538
539 557820 ic= s->c;
540 557820 ic.bytestream_start=
541 557820 ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
542 557820 memcpy(i_state, s->block_state, sizeof(s->block_state));
543
2/2
✓ Branch 0 taken 100440 times.
✓ Branch 1 taken 457380 times.
557820 if(level!=s->block_max_depth)
544 100440 put_rac(&ic, &i_state[4 + s_context], 1);
545 557820 put_rac(&ic, &i_state[1 + left->type + top->type], 1);
546 557820 put_symbol(&ic, &i_state[32], l-pl , 1);
547
1/2
✓ Branch 0 taken 557820 times.
✗ Branch 1 not taken.
557820 if (s->nb_planes > 2) {
548 557820 put_symbol(&ic, &i_state[64], cb-pcb, 1);
549 557820 put_symbol(&ic, &i_state[96], cr-pcr, 1);
550 }
551 557820 i_len= ic.bytestream - ic.bytestream_start;
552 557820 iscore += (enc->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
553
554 av_assert1(iscore < 255*255*256 + enc->lambda2*10);
555 av_assert1(iscore >= 0);
556 av_assert1(l>=0 && l<=255);
557 av_assert1(pl>=0 && pl<=255);
558
559
2/2
✓ Branch 0 taken 156060 times.
✓ Branch 1 taken 401760 times.
557820 if(level==0){
560 156060 int varc= iscore >> 8;
561 156060 int vard= score >> 8;
562
4/4
✓ Branch 0 taken 34206 times.
✓ Branch 1 taken 121854 times.
✓ Branch 2 taken 12448 times.
✓ Branch 3 taken 21758 times.
156060 if (vard <= 64 || vard < varc)
563 134302 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
564 else
565 21758 c->scene_change_score += enc->m.qscale;
566 }
567
568
2/2
✓ Branch 0 taken 100440 times.
✓ Branch 1 taken 457380 times.
557820 if(level!=s->block_max_depth){
569 100440 put_rac(&s->c, &s->block_state[4 + s_context], 0);
570 100440 score2 = encode_q_branch(enc, level+1, 2*x+0, 2*y+0);
571 100440 score2+= encode_q_branch(enc, level+1, 2*x+1, 2*y+0);
572 100440 score2+= encode_q_branch(enc, level+1, 2*x+0, 2*y+1);
573 100440 score2+= encode_q_branch(enc, level+1, 2*x+1, 2*y+1);
574 100440 score2+= enc->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
575
576
4/4
✓ Branch 0 taken 88596 times.
✓ Branch 1 taken 11844 times.
✓ Branch 2 taken 88580 times.
✓ Branch 3 taken 16 times.
100440 if(score2 < score && score2 < iscore)
577 88580 return score2;
578 }
579
580
2/2
✓ Branch 0 taken 82082 times.
✓ Branch 1 taken 387158 times.
469240 if(iscore < score){
581 82082 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
582 82082 memcpy(pbbak, i_buffer, i_len);
583 82082 s->c= ic;
584 82082 s->c.bytestream_start= pbbak_start;
585 82082 s->c.bytestream= pbbak + i_len;
586 82082 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
587 82082 memcpy(s->block_state, i_state, sizeof(s->block_state));
588 82082 return iscore;
589 }else{
590 387158 memcpy(pbbak, p_buffer, p_len);
591 387158 s->c= pc;
592 387158 s->c.bytestream_start= pbbak_start;
593 387158 s->c.bytestream= pbbak + p_len;
594 387158 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
595 387158 memcpy(s->block_state, p_state, sizeof(s->block_state));
596 387158 return score;
597 }
598 }
599
600 9600 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
601 9600 const int w= s->b_width << s->block_max_depth;
602 9600 const int rem_depth= s->block_max_depth - level;
603 9600 const int index= (x + y*w) << rem_depth;
604 9600 int trx= (x+1)<<rem_depth;
605 9600 BlockNode *b= &s->block[index];
606
2/2
✓ Branch 0 taken 8400 times.
✓ Branch 1 taken 1200 times.
9600 const BlockNode *left = x ? &s->block[index-1] : &null_block;
607
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 2400 times.
9600 const BlockNode *top = y ? &s->block[index-w] : &null_block;
608
4/4
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 2400 times.
✓ Branch 2 taken 6300 times.
✓ Branch 3 taken 900 times.
9600 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
609
7/8
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 2400 times.
✓ Branch 2 taken 6300 times.
✓ Branch 3 taken 900 times.
✓ Branch 4 taken 2700 times.
✓ Branch 5 taken 3600 times.
✓ Branch 6 taken 2700 times.
✗ Branch 7 not taken.
9600 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
610 9600 int pl = left->color[0];
611 9600 int pcb= left->color[1];
612 9600 int pcr= left->color[2];
613 int pmx, pmy;
614 9600 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9600 times.
9600 int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9600 times.
9600 int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
617 9600 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
618
619
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 8640 times.
9600 if(s->keyframe){
620 960 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
621 960 return;
622 }
623
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8640 times.
8640 if(level!=s->block_max_depth){
625 if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
626 put_rac(&s->c, &s->block_state[4 + s_context], 1);
627 }else{
628 put_rac(&s->c, &s->block_state[4 + s_context], 0);
629 encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
630 encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
631 encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
632 encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
633 return;
634 }
635 }
636
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 8593 times.
8640 if(b->type & BLOCK_INTRA){
637 47 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
638 47 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
639 47 put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
640
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 if (s->nb_planes > 2) {
641 47 put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
642 47 put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
643 }
644 47 set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
645 }else{
646 8593 pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
647 8593 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8593 times.
8593 if(s->ref_frames > 1)
649 put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
650 8593 put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
651 8593 put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
652 8593 set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
653 }
654 }
655
656 54714 static int get_dc(SnowEncContext *enc, int mb_x, int mb_y, int plane_index)
657 {
658 54714 SnowContext *const s = &enc->com;
659 int i, x2, y2;
660 54714 Plane *p= &s->plane[plane_index];
661 54714 const int block_size = MB_SIZE >> s->block_max_depth;
662
2/2
✓ Branch 0 taken 36476 times.
✓ Branch 1 taken 18238 times.
54714 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
663
2/2
✓ Branch 0 taken 36476 times.
✓ Branch 1 taken 18238 times.
54714 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
664
2/2
✓ Branch 0 taken 36476 times.
✓ Branch 1 taken 18238 times.
54714 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
665
2/2
✓ Branch 0 taken 36476 times.
✓ Branch 1 taken 18238 times.
54714 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
666 54714 const int ref_stride= s->current_picture->linesize[plane_index];
667 54714 const uint8_t *src = s->input_picture->data[plane_index];
668 54714 IDWTELEM *dst= (IDWTELEM*)enc->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
669 54714 const int b_stride = s->b_width << s->block_max_depth;
670 54714 const int w= p->width;
671 54714 const int h= p->height;
672 54714 int index= mb_x + mb_y*b_stride;
673 54714 BlockNode *b= &s->block[index];
674 54714 BlockNode backup= *b;
675 54714 int ab=0;
676 54714 int aa=0;
677
678 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
679
680 54714 b->type|= BLOCK_INTRA;
681 54714 b->color[plane_index]= 0;
682 54714 memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
683
684
2/2
✓ Branch 0 taken 218856 times.
✓ Branch 1 taken 54714 times.
273570 for(i=0; i<4; i++){
685 218856 int mb_x2= mb_x + (i &1) - 1;
686 218856 int mb_y2= mb_y + (i>>1) - 1;
687 218856 int x= block_w*mb_x2 + block_w/2;
688 218856 int y= block_h*mb_y2 + block_h/2;
689
690 218856 add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
691 x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
692
693
2/2
✓ Branch 0 taken 2064576 times.
✓ Branch 1 taken 218856 times.
2283432 for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
694
2/2
✓ Branch 0 taken 23411232 times.
✓ Branch 1 taken 2064576 times.
25475808 for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
695 23411232 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
696 23411232 int obmc_v= obmc[index];
697 int d;
698
2/2
✓ Branch 0 taken 1795776 times.
✓ Branch 1 taken 21615456 times.
23411232 if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
699
2/2
✓ Branch 0 taken 762816 times.
✓ Branch 1 taken 22648416 times.
23411232 if(x<0) obmc_v += obmc[index + block_w];
700
2/2
✓ Branch 0 taken 1269024 times.
✓ Branch 1 taken 22142208 times.
23411232 if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
701
2/2
✓ Branch 0 taken 600864 times.
✓ Branch 1 taken 22810368 times.
23411232 if(x+block_w>w) obmc_v += obmc[index - block_w];
702 //FIXME precalculate this or simplify it somehow else
703
704 23411232 d = -dst[index] + (1<<(FRAC_BITS-1));
705 23411232 dst[index] = d;
706 23411232 ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
707 23411232 aa += obmc_v * obmc_v; //FIXME precalculate this
708 }
709 }
710 }
711 54714 *b= backup;
712
713
2/2
✓ Branch 0 taken 54643 times.
✓ Branch 1 taken 71 times.
54714 return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
714 }
715
716 1263062 static inline int get_block_bits(SnowContext *s, int x, int y, int w){
717 1263062 const int b_stride = s->b_width << s->block_max_depth;
718 1263062 const int b_height = s->b_height<< s->block_max_depth;
719 1263062 int index= x + y*b_stride;
720 1263062 const BlockNode *b = &s->block[index];
721
2/2
✓ Branch 0 taken 1142570 times.
✓ Branch 1 taken 120492 times.
1263062 const BlockNode *left = x ? &s->block[index-1] : &null_block;
722
2/2
✓ Branch 0 taken 1098940 times.
✓ Branch 1 taken 164122 times.
1263062 const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
723
4/4
✓ Branch 0 taken 1098940 times.
✓ Branch 1 taken 164122 times.
✓ Branch 2 taken 988583 times.
✓ Branch 3 taken 110357 times.
1263062 const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
724
4/4
✓ Branch 0 taken 1098940 times.
✓ Branch 1 taken 164122 times.
✓ Branch 2 taken 959537 times.
✓ Branch 3 taken 139403 times.
1263062 const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
725 int dmx, dmy;
726 // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
727 // int my_context= av_log2(2*FFABS(left->my - top->my));
728
729
6/6
✓ Branch 0 taken 1224517 times.
✓ Branch 1 taken 38545 times.
✓ Branch 2 taken 1194259 times.
✓ Branch 3 taken 30258 times.
✓ Branch 4 taken 119045 times.
✓ Branch 5 taken 1075214 times.
1263062 if(x<0 || x>=b_stride || y>=b_height)
730 187848 return 0;
731 /*
732 1 0 0
733 01X 1-2 1
734 001XX 3-6 2-3
735 0001XXX 7-14 4-7
736 00001XXXX 15-30 8-15
737 */
738 //FIXME try accurate rate
739 //FIXME intra and inter predictors if surrounding blocks are not the same type
740
2/2
✓ Branch 0 taken 22775 times.
✓ Branch 1 taken 1052439 times.
1075214 if(b->type & BLOCK_INTRA){
741 22775 return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
742 22775 + av_log2(2*FFABS(left->color[1] - b->color[1]))
743 22775 + av_log2(2*FFABS(left->color[2] - b->color[2])));
744 }else{
745 1052439 pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
746 1052439 dmx-= b->mx;
747 1052439 dmy-= b->my;
748 1052439 return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
749 1052439 + av_log2(2*FFABS(dmy))
750 1052439 + av_log2(2*b->ref));
751 }
752 }
753
754 306440 static int get_block_rd(SnowEncContext *enc, int mb_x, int mb_y,
755 int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2])
756 {
757 306440 SnowContext *const s = &enc->com;
758 306440 Plane *p= &s->plane[plane_index];
759 306440 const int block_size = MB_SIZE >> s->block_max_depth;
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306440 times.
306440 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306440 times.
306440 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306440 times.
306440 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
763 306440 const int ref_stride= s->current_picture->linesize[plane_index];
764 306440 uint8_t *dst= s->current_picture->data[plane_index];
765 306440 const uint8_t *src = s->input_picture->data[plane_index];
766 306440 IDWTELEM *pred= (IDWTELEM*)enc->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
767 306440 uint8_t *cur = s->scratchbuf;
768 306440 uint8_t *tmp = s->emu_edge_buffer;
769 306440 const int b_stride = s->b_width << s->block_max_depth;
770 306440 const int b_height = s->b_height<< s->block_max_depth;
771 306440 const int w= p->width;
772 306440 const int h= p->height;
773 int distortion;
774 306440 int rate= 0;
775 306440 const int penalty_factor = get_penalty_factor(enc->lambda, enc->lambda2, s->avctx->me_cmp);
776 306440 int sx= block_w*mb_x - block_w/2;
777 306440 int sy= block_h*mb_y - block_h/2;
778 306440 int x0= FFMAX(0,-sx);
779 306440 int y0= FFMAX(0,-sy);
780 306440 int x1= FFMIN(block_w*2, w-sx);
781 306440 int y1= FFMIN(block_h*2, h-sy);
782 int i,x,y;
783
784 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
785
786 306440 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);
787
788
2/2
✓ Branch 0 taken 8675728 times.
✓ Branch 1 taken 306440 times.
8982168 for(y=y0; y<y1; y++){
789 8675728 const uint8_t *obmc1= obmc_edged[y];
790 8675728 const IDWTELEM *pred1 = pred + y*obmc_stride;
791 8675728 uint8_t *cur1 = cur + y*ref_stride;
792 8675728 uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
793
2/2
✓ Branch 0 taken 262001664 times.
✓ Branch 1 taken 8675728 times.
270677392 for(x=x0; x<x1; x++){
794 #if FRAC_BITS >= LOG2_OBMC_MAX
795 int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
796 #else
797 262001664 int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
798 #endif
799 262001664 v = (v + pred1[x]) >> FRAC_BITS;
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262001664 times.
262001664 if(v&(~255)) v= ~(v>>31);
801 262001664 dst1[x] = v;
802 }
803 }
804
805 /* copy the regions where obmc[] = (uint8_t)256 */
806
2/2
✓ Branch 0 taken 267895 times.
✓ Branch 1 taken 38545 times.
306440 if(LOG2_OBMC_MAX == 8
807
2/2
✓ Branch 0 taken 30258 times.
✓ Branch 1 taken 237637 times.
267895 && (mb_x == 0 || mb_x == b_stride-1)
808
4/4
✓ Branch 0 taken 50641 times.
✓ Branch 1 taken 18162 times.
✓ Branch 2 taken 12962 times.
✓ Branch 3 taken 37679 times.
68803 && (mb_y == 0 || mb_y == b_height-1)){
809
2/2
✓ Branch 0 taken 17393 times.
✓ Branch 1 taken 13731 times.
31124 if(mb_x == 0)
810 17393 x1 = block_w;
811 else
812 13731 x0 = block_w;
813
2/2
✓ Branch 0 taken 18162 times.
✓ Branch 1 taken 12962 times.
31124 if(mb_y == 0)
814 18162 y1 = block_h;
815 else
816 12962 y0 = block_h;
817
2/2
✓ Branch 0 taken 248992 times.
✓ Branch 1 taken 31124 times.
280116 for(y=y0; y<y1; y++)
818 248992 memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
819 }
820
821
1/2
✓ Branch 0 taken 306440 times.
✗ Branch 1 not taken.
306440 if(block_w==16){
822 /* FIXME rearrange dsputil to fit 32x32 cmp functions */
823 /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
824 /* FIXME cmps overlap but do not cover the wavelet's whole support.
825 * So improving the score of one block is not strictly guaranteed
826 * to improve the score of the whole frame, thus iterative motion
827 * estimation does not always converge. */
828
1/2
✓ Branch 0 taken 306440 times.
✗ Branch 1 not taken.
306440 if(s->avctx->me_cmp == FF_CMP_W97)
829 306440 distortion = ff_w97_32_c(&enc->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
830 else if(s->avctx->me_cmp == FF_CMP_W53)
831 distortion = ff_w53_32_c(&enc->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
832 else{
833 distortion = 0;
834 for(i=0; i<4; i++){
835 int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
836 distortion += enc->mecc.me_cmp[0](&enc->m, src + off, dst + off, ref_stride, 16);
837 }
838 }
839 }else{
840 av_assert2(block_w==8);
841 distortion = enc->mecc.me_cmp[0](&enc->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
842 }
843
844
1/2
✓ Branch 0 taken 306440 times.
✗ Branch 1 not taken.
306440 if(plane_index==0){
845
2/2
✓ Branch 0 taken 1225760 times.
✓ Branch 1 taken 306440 times.
1532200 for(i=0; i<4; i++){
846 /* ..RRr
847 * .RXx.
848 * rxx..
849 */
850 1225760 rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
851 }
852
2/2
✓ Branch 0 taken 37302 times.
✓ Branch 1 taken 269138 times.
306440 if(mb_x == b_stride-2)
853 37302 rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
854 }
855 306440 return distortion + rate*penalty_factor;
856 }
857
858 static int get_4block_rd(SnowEncContext *enc, int mb_x, int mb_y, int plane_index)
859 {
860 SnowContext *const s = &enc->com;
861 int i, y2;
862 Plane *p= &s->plane[plane_index];
863 const int block_size = MB_SIZE >> s->block_max_depth;
864 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
865 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
866 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
867 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
868 const int ref_stride= s->current_picture->linesize[plane_index];
869 uint8_t *dst= s->current_picture->data[plane_index];
870 const uint8_t *src = s->input_picture->data[plane_index];
871 //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
872 // const has only been removed from zero_dst to suppress a warning
873 static IDWTELEM zero_dst[4096]; //FIXME
874 const int b_stride = s->b_width << s->block_max_depth;
875 const int w= p->width;
876 const int h= p->height;
877 int distortion= 0;
878 int rate= 0;
879 const int penalty_factor= get_penalty_factor(enc->lambda, enc->lambda2, s->avctx->me_cmp);
880
881 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
882
883 for(i=0; i<9; i++){
884 int mb_x2= mb_x + (i%3) - 1;
885 int mb_y2= mb_y + (i/3) - 1;
886 int x= block_w*mb_x2 + block_w/2;
887 int y= block_h*mb_y2 + block_h/2;
888
889 add_yblock(s, 0, NULL, zero_dst, dst, obmc,
890 x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
891
892 //FIXME find a cleaner/simpler way to skip the outside stuff
893 for(y2= y; y2<0; y2++)
894 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
895 for(y2= h; y2<y+block_h; y2++)
896 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
897 if(x<0){
898 for(y2= y; y2<y+block_h; y2++)
899 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
900 }
901 if(x+block_w > w){
902 for(y2= y; y2<y+block_h; y2++)
903 memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
904 }
905
906 av_assert1(block_w== 8 || block_w==16);
907 distortion += enc->mecc.me_cmp[block_w==8](&enc->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
908 }
909
910 if(plane_index==0){
911 BlockNode *b= &s->block[mb_x+mb_y*b_stride];
912 int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
913
914 /* ..RRRr
915 * .RXXx.
916 * .RXXx.
917 * rxxx.
918 */
919 if(merged)
920 rate = get_block_bits(s, mb_x, mb_y, 2);
921 for(i=merged?4:0; i<9; i++){
922 static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
923 rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
924 }
925 }
926 return distortion + rate*penalty_factor;
927 }
928
929 21600 static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
930 21600 const int w= b->width;
931 21600 const int h= b->height;
932 int x, y;
933
934 if(1){
935 21600 int run=0;
936 21600 int *runs = s->run_buffer;
937 21600 int run_index=0;
938 int max_index;
939
940
2/2
✓ Branch 0 taken 366600 times.
✓ Branch 1 taken 21600 times.
388200 for(y=0; y<h; y++){
941
2/2
✓ Branch 0 taken 26496000 times.
✓ Branch 1 taken 366600 times.
26862600 for(x=0; x<w; x++){
942 26496000 int v, p=0;
943 26496000 int /*ll=0, */l=0, lt=0, t=0, rt=0;
944 26496000 v= src[x + y*stride];
945
946
2/2
✓ Branch 0 taken 25960200 times.
✓ Branch 1 taken 535800 times.
26496000 if(y){
947 25960200 t= src[x + (y-1)*stride];
948
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x){
949 25615200 lt= src[x - 1 + (y-1)*stride];
950 }
951
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x + 1 < w){
952 25615200 rt= src[x + 1 + (y-1)*stride];
953 }
954 }
955
2/2
✓ Branch 0 taken 26129400 times.
✓ Branch 1 taken 366600 times.
26496000 if(x){
956 26129400 l= src[x - 1 + y*stride];
957 /*if(x > 1){
958 if(orientation==1) ll= src[y + (x-2)*stride];
959 else ll= src[x - 2 + y*stride];
960 }*/
961 }
962
2/2
✓ Branch 0 taken 26392500 times.
✓ Branch 1 taken 103500 times.
26496000 if(parent){
963 26392500 int px= x>>1;
964 26392500 int py= y>>1;
965
4/4
✓ Branch 0 taken 26387100 times.
✓ Branch 1 taken 5400 times.
✓ Branch 2 taken 26380800 times.
✓ Branch 3 taken 6300 times.
26392500 if(px<b->parent->width && py<b->parent->height)
966 26380800 p= parent[px + py*2*stride];
967 }
968
2/2
✓ Branch 0 taken 2820074 times.
✓ Branch 1 taken 23675926 times.
26496000 if(!(/*ll|*/l|lt|t|rt|p)){
969
2/2
✓ Branch 0 taken 291706 times.
✓ Branch 1 taken 2528368 times.
2820074 if(v){
970 291706 runs[run_index++]= run;
971 291706 run=0;
972 }else{
973 2528368 run++;
974 }
975 }
976 }
977 }
978 21600 max_index= run_index;
979 21600 runs[run_index++]= run;
980 21600 run_index=0;
981 21600 run= runs[run_index++];
982
983 21600 put_symbol2(&s->c, b->state[30], max_index, 0);
984
2/2
✓ Branch 0 taken 16975 times.
✓ Branch 1 taken 4625 times.
21600 if(run_index <= max_index)
985 16975 put_symbol2(&s->c, b->state[1], run, 3);
986
987
2/2
✓ Branch 0 taken 366600 times.
✓ Branch 1 taken 21600 times.
388200 for(y=0; y<h; y++){
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 366600 times.
366600 if(s->c.bytestream_end - s->c.bytestream < w*40){
989 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
990 return AVERROR(ENOMEM);
991 }
992
2/2
✓ Branch 0 taken 26496000 times.
✓ Branch 1 taken 366600 times.
26862600 for(x=0; x<w; x++){
993 26496000 int v, p=0;
994 26496000 int /*ll=0, */l=0, lt=0, t=0, rt=0;
995 26496000 v= src[x + y*stride];
996
997
2/2
✓ Branch 0 taken 25960200 times.
✓ Branch 1 taken 535800 times.
26496000 if(y){
998 25960200 t= src[x + (y-1)*stride];
999
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x){
1000 25615200 lt= src[x - 1 + (y-1)*stride];
1001 }
1002
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x + 1 < w){
1003 25615200 rt= src[x + 1 + (y-1)*stride];
1004 }
1005 }
1006
2/2
✓ Branch 0 taken 26129400 times.
✓ Branch 1 taken 366600 times.
26496000 if(x){
1007 26129400 l= src[x - 1 + y*stride];
1008 /*if(x > 1){
1009 if(orientation==1) ll= src[y + (x-2)*stride];
1010 else ll= src[x - 2 + y*stride];
1011 }*/
1012 }
1013
2/2
✓ Branch 0 taken 26392500 times.
✓ Branch 1 taken 103500 times.
26496000 if(parent){
1014 26392500 int px= x>>1;
1015 26392500 int py= y>>1;
1016
4/4
✓ Branch 0 taken 26387100 times.
✓ Branch 1 taken 5400 times.
✓ Branch 2 taken 26380800 times.
✓ Branch 3 taken 6300 times.
26392500 if(px<b->parent->width && py<b->parent->height)
1017 26380800 p= parent[px + py*2*stride];
1018 }
1019
2/2
✓ Branch 0 taken 23675926 times.
✓ Branch 1 taken 2820074 times.
26496000 if(/*ll|*/l|lt|t|rt|p){
1020 23675926 int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
1021
1022 23675926 put_rac(&s->c, &b->state[0][context], !!v);
1023 }else{
1024
2/2
✓ Branch 0 taken 291706 times.
✓ Branch 1 taken 2528368 times.
2820074 if(!run){
1025 291706 run= runs[run_index++];
1026
1027
2/2
✓ Branch 0 taken 274731 times.
✓ Branch 1 taken 16975 times.
291706 if(run_index <= max_index)
1028 274731 put_symbol2(&s->c, b->state[1], run, 3);
1029 av_assert2(v);
1030 }else{
1031 2528368 run--;
1032 av_assert2(!v);
1033 }
1034 }
1035
2/2
✓ Branch 0 taken 15993728 times.
✓ Branch 1 taken 10502272 times.
26496000 if(v){
1036 15993728 int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
1037 15993728 int l2= 2*FFABS(l) + (l<0);
1038 15993728 int t2= 2*FFABS(t) + (t<0);
1039
1040 15993728 put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
1041 15993728 put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
1042 }
1043 }
1044 }
1045 }
1046 21600 return 0;
1047 }
1048
1049 21600 static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
1050 // encode_subband_qtree(s, b, src, parent, stride, orientation);
1051 // encode_subband_z0run(s, b, src, parent, stride, orientation);
1052 21600 return encode_subband_c0run(s, b, src, parent, stride, orientation);
1053 // encode_subband_dzr(s, b, src, parent, stride, orientation);
1054 }
1055
1056 18314 static av_always_inline int check_block_intra(SnowEncContext *enc, int mb_x, int mb_y, int p[3],
1057 uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd)
1058 {
1059 18314 SnowContext *const s = &enc->com;
1060 18314 const int b_stride= s->b_width << s->block_max_depth;
1061 18314 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
1062 18314 BlockNode backup= *block;
1063 int rd;
1064
1065 av_assert2(mb_x>=0 && mb_y>=0);
1066 av_assert2(mb_x<b_stride);
1067
1068 18314 block->color[0] = p[0];
1069 18314 block->color[1] = p[1];
1070 18314 block->color[2] = p[2];
1071 18314 block->type |= BLOCK_INTRA;
1072
1073 18314 rd = get_block_rd(enc, mb_x, mb_y, 0, obmc_edged) + enc->intra_penalty;
1074
1075 //FIXME chroma
1076
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 18179 times.
18314 if(rd < *best_rd){
1077 135 *best_rd= rd;
1078 135 return 1;
1079 }else{
1080 18179 *block= backup;
1081 18179 return 0;
1082 }
1083 }
1084
1085 /* special case for int[2] args we discard afterwards,
1086 * fixes compilation problem with gcc 2.95 */
1087 404968 static av_always_inline int check_block_inter(SnowEncContext *enc,
1088 int mb_x, int mb_y, int p0, int p1,
1089 uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd)
1090 {
1091 404968 SnowContext *const s = &enc->com;
1092 404968 const int b_stride = s->b_width << s->block_max_depth;
1093 404968 BlockNode *block = &s->block[mb_x + mb_y * b_stride];
1094 404968 BlockNode backup = *block;
1095 unsigned value;
1096 int rd, index;
1097
1098 av_assert2(mb_x >= 0 && mb_y >= 0);
1099 av_assert2(mb_x < b_stride);
1100
1101 404968 index = (p0 + 31 * p1) & (ME_CACHE_SIZE-1);
1102 404968 value = enc->me_cache_generation + (p0 >> 10) + p1 * (1 << 6) + (block->ref << 12);
1103
2/2
✓ Branch 0 taken 116842 times.
✓ Branch 1 taken 288126 times.
404968 if (enc->me_cache[index] == value)
1104 116842 return 0;
1105 288126 enc->me_cache[index] = value;
1106
1107 288126 block->mx = p0;
1108 288126 block->my = p1;
1109 288126 block->type &= ~BLOCK_INTRA;
1110
1111 288126 rd = get_block_rd(enc, mb_x, mb_y, 0, obmc_edged);
1112
1113 //FIXME chroma
1114
2/2
✓ Branch 0 taken 63690 times.
✓ Branch 1 taken 224436 times.
288126 if (rd < *best_rd) {
1115 63690 *best_rd = rd;
1116 63690 return 1;
1117 } else {
1118 224436 *block = backup;
1119 224436 return 0;
1120 }
1121 }
1122
1123 static av_always_inline int check_4block_inter(SnowEncContext *enc, int mb_x, int mb_y,
1124 int p0, int p1, int ref, int *best_rd)
1125 {
1126 SnowContext *const s = &enc->com;
1127 const int b_stride= s->b_width << s->block_max_depth;
1128 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
1129 BlockNode backup[4];
1130 unsigned value;
1131 int rd, index;
1132
1133 /* We don't initialize backup[] during variable declaration, because
1134 * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
1135 * 'int16_t'". */
1136 backup[0] = block[0];
1137 backup[1] = block[1];
1138 backup[2] = block[b_stride];
1139 backup[3] = block[b_stride + 1];
1140
1141 av_assert2(mb_x>=0 && mb_y>=0);
1142 av_assert2(mb_x<b_stride);
1143 av_assert2(((mb_x|mb_y)&1) == 0);
1144
1145 index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
1146 value = enc->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
1147 if (enc->me_cache[index] == value)
1148 return 0;
1149 enc->me_cache[index] = value;
1150
1151 block->mx= p0;
1152 block->my= p1;
1153 block->ref= ref;
1154 block->type &= ~BLOCK_INTRA;
1155 block[1]= block[b_stride]= block[b_stride+1]= *block;
1156
1157 rd = get_4block_rd(enc, mb_x, mb_y, 0);
1158
1159 //FIXME chroma
1160 if(rd < *best_rd){
1161 *best_rd= rd;
1162 return 1;
1163 }else{
1164 block[0]= backup[0];
1165 block[1]= backup[1];
1166 block[b_stride]= backup[2];
1167 block[b_stride+1]= backup[3];
1168 return 0;
1169 }
1170 }
1171
1172 270 static void iterative_me(SnowEncContext *enc)
1173 {
1174 270 SnowContext *const s = &enc->com;
1175 int pass, mb_x, mb_y;
1176 270 const int b_width = s->b_width << s->block_max_depth;
1177 270 const int b_height= s->b_height << s->block_max_depth;
1178 270 const int b_stride= b_width;
1179 int color[3];
1180
1181 {
1182 270 RangeCoder r = s->c;
1183 uint8_t state[sizeof(s->block_state)];
1184 270 memcpy(state, s->block_state, sizeof(s->block_state));
1185
2/2
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 270 times.
1350 for(mb_y= 0; mb_y<s->b_height; mb_y++)
1186
2/2
✓ Branch 0 taken 8640 times.
✓ Branch 1 taken 1080 times.
9720 for(mb_x= 0; mb_x<s->b_width; mb_x++)
1187 8640 encode_q_branch(enc, 0, mb_x, mb_y);
1188 270 s->c = r;
1189 270 memcpy(s->block_state, state, sizeof(s->block_state));
1190 }
1191
1192
1/2
✓ Branch 0 taken 933 times.
✗ Branch 1 not taken.
933 for(pass=0; pass<25; pass++){
1193 933 int change= 0;
1194
1195
2/2
✓ Branch 0 taken 3732 times.
✓ Branch 1 taken 933 times.
4665 for(mb_y= 0; mb_y<b_height; mb_y++){
1196
2/2
✓ Branch 0 taken 29856 times.
✓ Branch 1 taken 3732 times.
33588 for(mb_x= 0; mb_x<b_width; mb_x++){
1197 int dia_change, i, j, ref;
1198 29856 int best_rd= INT_MAX, ref_rd;
1199 BlockNode backup, ref_b;
1200 29856 const int index= mb_x + mb_y * b_stride;
1201 29856 BlockNode *block= &s->block[index];
1202
2/2
✓ Branch 0 taken 22392 times.
✓ Branch 1 taken 7464 times.
29856 BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
1203
2/2
✓ Branch 0 taken 26124 times.
✓ Branch 1 taken 3732 times.
29856 BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
1204
2/2
✓ Branch 0 taken 26124 times.
✓ Branch 1 taken 3732 times.
29856 BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
1205
2/2
✓ Branch 0 taken 22392 times.
✓ Branch 1 taken 7464 times.
29856 BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
1206
4/4
✓ Branch 0 taken 26124 times.
✓ Branch 1 taken 3732 times.
✓ Branch 2 taken 19593 times.
✓ Branch 3 taken 6531 times.
29856 BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
1207
4/4
✓ Branch 0 taken 26124 times.
✓ Branch 1 taken 3732 times.
✓ Branch 2 taken 19593 times.
✓ Branch 3 taken 6531 times.
29856 BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
1208
4/4
✓ Branch 0 taken 26124 times.
✓ Branch 1 taken 3732 times.
✓ Branch 2 taken 19593 times.
✓ Branch 3 taken 6531 times.
29856 BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
1209
4/4
✓ Branch 0 taken 26124 times.
✓ Branch 1 taken 3732 times.
✓ Branch 2 taken 19593 times.
✓ Branch 3 taken 6531 times.
29856 BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
1210 29856 const int b_w= (MB_SIZE >> s->block_max_depth);
1211 uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
1212
1213
4/4
✓ Branch 0 taken 21216 times.
✓ Branch 1 taken 8640 times.
✓ Branch 2 taken 11618 times.
✓ Branch 3 taken 9598 times.
29856 if(pass && (block->type & BLOCK_OPT))
1214 11618 continue;
1215 18238 block->type |= BLOCK_OPT;
1216
1217 18238 backup= *block;
1218
1219
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18217 times.
18238 if (!enc->me_cache_generation)
1220 21 memset(enc->me_cache, 0, sizeof(enc->me_cache));
1221 18238 enc->me_cache_generation += 1<<22;
1222
1223 //FIXME precalculate
1224 {
1225 int x, y;
1226
2/2
✓ Branch 0 taken 583616 times.
✓ Branch 1 taken 18238 times.
601854 for (y = 0; y < b_w * 2; y++)
1227 583616 memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
1228
2/2
✓ Branch 0 taken 2241 times.
✓ Branch 1 taken 15997 times.
18238 if(mb_x==0)
1229
2/2
✓ Branch 0 taken 71712 times.
✓ Branch 1 taken 2241 times.
73953 for(y=0; y<b_w*2; y++)
1230 71712 memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
1231
2/2
✓ Branch 0 taken 1763 times.
✓ Branch 1 taken 16475 times.
18238 if(mb_x==b_stride-1)
1232
2/2
✓ Branch 0 taken 56416 times.
✓ Branch 1 taken 1763 times.
58179 for(y=0; y<b_w*2; y++)
1233 56416 memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
1234
2/2
✓ Branch 0 taken 4942 times.
✓ Branch 1 taken 13296 times.
18238 if(mb_y==0){
1235
2/2
✓ Branch 0 taken 158144 times.
✓ Branch 1 taken 4942 times.
163086 for(x=0; x<b_w*2; x++)
1236 158144 obmc_edged[0][x] += obmc_edged[b_w-1][x];
1237
2/2
✓ Branch 0 taken 74130 times.
✓ Branch 1 taken 4942 times.
79072 for(y=1; y<b_w; y++)
1238 74130 memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
1239 }
1240
2/2
✓ Branch 0 taken 3492 times.
✓ Branch 1 taken 14746 times.
18238 if(mb_y==b_height-1){
1241
2/2
✓ Branch 0 taken 111744 times.
✓ Branch 1 taken 3492 times.
115236 for(x=0; x<b_w*2; x++)
1242 111744 obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
1243
2/2
✓ Branch 0 taken 52380 times.
✓ Branch 1 taken 3492 times.
55872 for(y=b_w; y<b_w*2-1; y++)
1244 52380 memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
1245 }
1246 }
1247
1248 //skip stuff outside the picture
1249
8/8
✓ Branch 0 taken 15997 times.
✓ Branch 1 taken 2241 times.
✓ Branch 2 taken 11647 times.
✓ Branch 3 taken 4350 times.
✓ Branch 4 taken 10354 times.
✓ Branch 5 taken 1293 times.
✓ Branch 6 taken 2743 times.
✓ Branch 7 taken 7611 times.
18238 if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
1250 10627 const uint8_t *src = s->input_picture->data[0];
1251 10627 uint8_t *dst= s->current_picture->data[0];
1252 10627 const int stride= s->current_picture->linesize[0];
1253 10627 const int block_w= MB_SIZE >> s->block_max_depth;
1254 10627 const int block_h= MB_SIZE >> s->block_max_depth;
1255 10627 const int sx= block_w*mb_x - block_w/2;
1256 10627 const int sy= block_h*mb_y - block_h/2;
1257 10627 const int w= s->plane[0].width;
1258 10627 const int h= s->plane[0].height;
1259 int y;
1260
1261
2/2
✓ Branch 0 taken 39536 times.
✓ Branch 1 taken 10627 times.
50163 for(y=sy; y<0; y++)
1262 39536 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1263
2/2
✓ Branch 0 taken 27936 times.
✓ Branch 1 taken 10627 times.
38563 for(y=h; y<sy+block_h*2; y++)
1264 27936 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1265
2/2
✓ Branch 0 taken 2241 times.
✓ Branch 1 taken 8386 times.
10627 if(sx<0){
1266
2/2
✓ Branch 0 taken 71712 times.
✓ Branch 1 taken 2241 times.
73953 for(y=sy; y<sy+block_h*2; y++)
1267 71712 memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
1268 }
1269
2/2
✓ Branch 0 taken 1763 times.
✓ Branch 1 taken 8864 times.
10627 if(sx+block_w*2 > w){
1270
2/2
✓ Branch 0 taken 56416 times.
✓ Branch 1 taken 1763 times.
58179 for(y=sy; y<sy+block_h*2; y++)
1271 56416 memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
1272 }
1273 }
1274
1275 // intra(black) = neighbors' contribution to the current block
1276
2/2
✓ Branch 0 taken 54714 times.
✓ Branch 1 taken 18238 times.
72952 for(i=0; i < s->nb_planes; i++)
1277 54714 color[i]= get_dc(enc, mb_x, mb_y, i);
1278
1279 // get previous score (cannot be cached due to OBMC)
1280
4/4
✓ Branch 0 taken 9598 times.
✓ Branch 1 taken 8640 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 9522 times.
18314 if(pass > 0 && (block->type&BLOCK_INTRA)){
1281 76 int color0[3]= {block->color[0], block->color[1], block->color[2]};
1282 76 check_block_intra(enc, mb_x, mb_y, color0, obmc_edged, &best_rd);
1283 }else
1284 18162 check_block_inter(enc, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
1285
1286 18238 ref_b= *block;
1287 18238 ref_rd= best_rd;
1288
2/2
✓ Branch 0 taken 18238 times.
✓ Branch 1 taken 18238 times.
36476 for(ref=0; ref < s->ref_frames; ref++){
1289 18238 int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
1290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18238 times.
18238 if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
1291 continue;
1292 18238 block->ref= ref;
1293 18238 best_rd= INT_MAX;
1294
1295 18238 check_block_inter(enc, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
1296 18238 check_block_inter(enc, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
1297
2/2
✓ Branch 0 taken 13296 times.
✓ Branch 1 taken 4942 times.
18238 if(tb)
1298 13296 check_block_inter(enc, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
1299
2/2
✓ Branch 0 taken 15997 times.
✓ Branch 1 taken 2241 times.
18238 if(lb)
1300 15997 check_block_inter(enc, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
1301
2/2
✓ Branch 0 taken 16475 times.
✓ Branch 1 taken 1763 times.
18238 if(rb)
1302 16475 check_block_inter(enc, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
1303
2/2
✓ Branch 0 taken 14746 times.
✓ Branch 1 taken 3492 times.
18238 if(bb)
1304 14746 check_block_inter(enc, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
1305
1306 /* fullpel ME */
1307 //FIXME avoid subpel interpolation / round to nearest integer
1308 do{
1309 18658 int newx = block->mx;
1310 18658 int newy = block->my;
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18658 times.
18658 int dia_size = enc->iterative_dia_size ? enc->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
1312 18658 dia_change=0;
1313
2/2
✓ Branch 0 taken 37316 times.
✓ Branch 1 taken 18658 times.
55974 for(i=0; i < dia_size; i++){
1314
2/2
✓ Branch 0 taken 18658 times.
✓ Branch 1 taken 37316 times.
55974 for(j=0; j<i; j++){
1315 18658 dia_change |= check_block_inter(enc, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
1316 18658 dia_change |= check_block_inter(enc, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
1317 18658 dia_change |= check_block_inter(enc, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
1318 18658 dia_change |= check_block_inter(enc, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
1319 }
1320 }
1321
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 18238 times.
18658 }while(dia_change);
1322 /* subpel ME */
1323 do{
1324 static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
1325 26898 dia_change=0;
1326
2/2
✓ Branch 0 taken 215184 times.
✓ Branch 1 taken 26898 times.
242082 for(i=0; i<8; i++)
1327 215184 dia_change |= check_block_inter(enc, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
1328
2/2
✓ Branch 0 taken 8660 times.
✓ Branch 1 taken 18238 times.
26898 }while(dia_change);
1329 //FIXME or try the standard 2 pass qpel or similar
1330
1331 18238 mvr[0][0]= block->mx;
1332 18238 mvr[0][1]= block->my;
1333
2/2
✓ Branch 0 taken 4362 times.
✓ Branch 1 taken 13876 times.
18238 if(ref_rd > best_rd){
1334 4362 ref_rd= best_rd;
1335 4362 ref_b= *block;
1336 }
1337 }
1338 18238 best_rd= ref_rd;
1339 18238 *block= ref_b;
1340 18238 check_block_intra(enc, mb_x, mb_y, color, obmc_edged, &best_rd);
1341 //FIXME RD style color selection
1342
2/2
✓ Branch 1 taken 4386 times.
✓ Branch 2 taken 13852 times.
18238 if(!same_block(block, &backup)){
1343
2/2
✓ Branch 0 taken 3174 times.
✓ Branch 1 taken 1212 times.
4386 if(tb ) tb ->type &= ~BLOCK_OPT;
1344
2/2
✓ Branch 0 taken 3853 times.
✓ Branch 1 taken 533 times.
4386 if(lb ) lb ->type &= ~BLOCK_OPT;
1345
2/2
✓ Branch 0 taken 3892 times.
✓ Branch 1 taken 494 times.
4386 if(rb ) rb ->type &= ~BLOCK_OPT;
1346
2/2
✓ Branch 0 taken 3458 times.
✓ Branch 1 taken 928 times.
4386 if(bb ) bb ->type &= ~BLOCK_OPT;
1347
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 1614 times.
4386 if(tlb) tlb->type &= ~BLOCK_OPT;
1348
2/2
✓ Branch 0 taken 2822 times.
✓ Branch 1 taken 1564 times.
4386 if(trb) trb->type &= ~BLOCK_OPT;
1349
2/2
✓ Branch 0 taken 3031 times.
✓ Branch 1 taken 1355 times.
4386 if(blb) blb->type &= ~BLOCK_OPT;
1350
2/2
✓ Branch 0 taken 3076 times.
✓ Branch 1 taken 1310 times.
4386 if(brb) brb->type &= ~BLOCK_OPT;
1351 4386 change ++;
1352 }
1353 }
1354 }
1355 933 av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
1356
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 663 times.
933 if(!change)
1357 270 break;
1358 }
1359
1360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if(s->block_max_depth == 1){
1361 int change= 0;
1362 for(mb_y= 0; mb_y<b_height; mb_y+=2){
1363 for(mb_x= 0; mb_x<b_width; mb_x+=2){
1364 int i;
1365 int best_rd, init_rd;
1366 const int index= mb_x + mb_y * b_stride;
1367 BlockNode *b[4];
1368
1369 b[0]= &s->block[index];
1370 b[1]= b[0]+1;
1371 b[2]= b[0]+b_stride;
1372 b[3]= b[2]+1;
1373 if(same_block(b[0], b[1]) &&
1374 same_block(b[0], b[2]) &&
1375 same_block(b[0], b[3]))
1376 continue;
1377
1378 if (!enc->me_cache_generation)
1379 memset(enc->me_cache, 0, sizeof(enc->me_cache));
1380 enc->me_cache_generation += 1<<22;
1381
1382 init_rd = best_rd = get_4block_rd(enc, mb_x, mb_y, 0);
1383
1384 //FIXME more multiref search?
1385 check_4block_inter(enc, mb_x, mb_y,
1386 (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
1387 (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
1388
1389 for(i=0; i<4; i++)
1390 if(!(b[i]->type&BLOCK_INTRA))
1391 check_4block_inter(enc, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
1392
1393 if(init_rd != best_rd)
1394 change++;
1395 }
1396 }
1397 av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
1398 }
1399 270 }
1400
1401 510 static void encode_blocks(SnowEncContext *enc, int search)
1402 {
1403 510 SnowContext *const s = &enc->com;
1404 int x, y;
1405 510 int w= s->b_width;
1406 510 int h= s->b_height;
1407
1408
5/6
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 270 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 270 times.
✗ Branch 5 not taken.
510 if (enc->motion_est == FF_ME_ITER && !s->keyframe && search)
1409 270 iterative_me(enc);
1410
1411
2/2
✓ Branch 0 taken 6060 times.
✓ Branch 1 taken 510 times.
6570 for(y=0; y<h; y++){
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6060 times.
6060 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
1413 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1414 return;
1415 }
1416
2/2
✓ Branch 0 taken 166200 times.
✓ Branch 1 taken 6060 times.
172260 for(x=0; x<w; x++){
1417
3/4
✓ Branch 0 taken 156600 times.
✓ Branch 1 taken 9600 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 156600 times.
166200 if (enc->motion_est == FF_ME_ITER || !search)
1418 9600 encode_q_branch2(s, 0, x, y);
1419 else
1420 156600 encode_q_branch (enc, 0, x, y);
1421 }
1422 }
1423 }
1424
1425 21600 static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
1426 21600 const int w= b->width;
1427 21600 const int h= b->height;
1428 21600 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1429 21600 const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
1430 int x,y, thres1, thres2;
1431
1432
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 14400 times.
21600 if(s->qlog == LOSSLESS_QLOG){
1433
2/2
✓ Branch 0 taken 253800 times.
✓ Branch 1 taken 7200 times.
261000 for(y=0; y<h; y++)
1434
2/2
✓ Branch 0 taken 22809600 times.
✓ Branch 1 taken 253800 times.
23063400 for(x=0; x<w; x++)
1435 22809600 dst[x + y*stride]= src[x + y*stride];
1436 7200 return;
1437 }
1438
1439
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 12960 times.
14400 bias= bias ? 0 : (3*qmul)>>3;
1440 14400 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
1441 14400 thres2= 2*thres1;
1442
1443
2/2
✓ Branch 0 taken 12960 times.
✓ Branch 1 taken 1440 times.
14400 if(!bias){
1444
2/2
✓ Branch 0 taken 101520 times.
✓ Branch 1 taken 12960 times.
114480 for(y=0; y<h; y++){
1445
2/2
✓ Branch 0 taken 3317760 times.
✓ Branch 1 taken 101520 times.
3419280 for(x=0; x<w; x++){
1446 3317760 int i= src[x + y*stride];
1447
1448
2/2
✓ Branch 0 taken 550371 times.
✓ Branch 1 taken 2767389 times.
3317760 if((unsigned)(i+thres1) > thres2){
1449
2/2
✓ Branch 0 taken 275604 times.
✓ Branch 1 taken 274767 times.
550371 if(i>=0){
1450 275604 i<<= QEXPSHIFT;
1451 275604 i/= qmul; //FIXME optimize
1452 275604 dst[x + y*stride]= i;
1453 }else{
1454 274767 i= -i;
1455 274767 i<<= QEXPSHIFT;
1456 274767 i/= qmul; //FIXME optimize
1457 274767 dst[x + y*stride]= -i;
1458 }
1459 }else
1460 2767389 dst[x + y*stride]= 0;
1461 }
1462 }
1463 }else{
1464
2/2
✓ Branch 0 taken 11280 times.
✓ Branch 1 taken 1440 times.
12720 for(y=0; y<h; y++){
1465
2/2
✓ Branch 0 taken 368640 times.
✓ Branch 1 taken 11280 times.
379920 for(x=0; x<w; x++){
1466 368640 int i= src[x + y*stride];
1467
1468
2/2
✓ Branch 0 taken 202458 times.
✓ Branch 1 taken 166182 times.
368640 if((unsigned)(i+thres1) > thres2){
1469
2/2
✓ Branch 0 taken 101688 times.
✓ Branch 1 taken 100770 times.
202458 if(i>=0){
1470 101688 i<<= QEXPSHIFT;
1471 101688 i= (i + bias) / qmul; //FIXME optimize
1472 101688 dst[x + y*stride]= i;
1473 }else{
1474 100770 i= -i;
1475 100770 i<<= QEXPSHIFT;
1476 100770 i= (i + bias) / qmul; //FIXME optimize
1477 100770 dst[x + y*stride]= -i;
1478 }
1479 }else
1480 166182 dst[x + y*stride]= 0;
1481 }
1482 }
1483 }
1484 }
1485
1486 21600 static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
1487 21600 const int w= b->width;
1488 21600 const int h= b->height;
1489 21600 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1490 21600 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1491 21600 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1492 int x,y;
1493
1494
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 14400 times.
21600 if(s->qlog == LOSSLESS_QLOG) return;
1495
1496
2/2
✓ Branch 0 taken 112800 times.
✓ Branch 1 taken 14400 times.
127200 for(y=0; y<h; y++){
1497
2/2
✓ Branch 0 taken 3686400 times.
✓ Branch 1 taken 112800 times.
3799200 for(x=0; x<w; x++){
1498 3686400 int i= src[x + y*stride];
1499
2/2
✓ Branch 0 taken 375500 times.
✓ Branch 1 taken 3310900 times.
3686400 if(i<0){
1500 375500 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
1501
2/2
✓ Branch 0 taken 377275 times.
✓ Branch 1 taken 2933625 times.
3310900 }else if(i>0){
1502 377275 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
1503 }
1504 }
1505 }
1506 }
1507
1508 1350 static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1509 1350 const int w= b->width;
1510 1350 const int h= b->height;
1511 int x,y;
1512
1513
2/2
✓ Branch 0 taken 4050 times.
✓ Branch 1 taken 1350 times.
5400 for(y=h-1; y>=0; y--){
1514
2/2
✓ Branch 0 taken 27450 times.
✓ Branch 1 taken 4050 times.
31500 for(x=w-1; x>=0; x--){
1515 27450 int i= x + y*stride;
1516
1517
2/2
✓ Branch 0 taken 23400 times.
✓ Branch 1 taken 4050 times.
27450 if(x){
1518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23400 times.
23400 if(use_median){
1519 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1520 else src[i] -= src[i - 1];
1521 }else{
1522
2/2
✓ Branch 0 taken 18900 times.
✓ Branch 1 taken 4500 times.
23400 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
1523 4500 else src[i] -= src[i - 1];
1524 }
1525 }else{
1526
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 1350 times.
4050 if(y) src[i] -= src[i - stride];
1527 }
1528 }
1529 }
1530 1350 }
1531
1532 1350 static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1533 1350 const int w= b->width;
1534 1350 const int h= b->height;
1535 int x,y;
1536
1537
2/2
✓ Branch 0 taken 4050 times.
✓ Branch 1 taken 1350 times.
5400 for(y=0; y<h; y++){
1538
2/2
✓ Branch 0 taken 27450 times.
✓ Branch 1 taken 4050 times.
31500 for(x=0; x<w; x++){
1539 27450 int i= x + y*stride;
1540
1541
2/2
✓ Branch 0 taken 23400 times.
✓ Branch 1 taken 4050 times.
27450 if(x){
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23400 times.
23400 if(use_median){
1543 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1544 else src[i] += src[i - 1];
1545 }else{
1546
2/2
✓ Branch 0 taken 18900 times.
✓ Branch 1 taken 4500 times.
23400 if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
1547 4500 else src[i] += src[i - 1];
1548 }
1549 }else{
1550
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 1350 times.
4050 if(y) src[i] += src[i - stride];
1551 }
1552 }
1553 }
1554 1350 }
1555
1556 47 static void encode_qlogs(SnowContext *s){
1557 int plane_index, level, orientation;
1558
1559
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 47 times.
141 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1560
2/2
✓ Branch 0 taken 470 times.
✓ Branch 1 taken 94 times.
564 for(level=0; level<s->spatial_decomposition_count; level++){
1561
2/2
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 470 times.
1974 for(orientation=level ? 1:0; orientation<4; orientation++){
1562
2/2
✓ Branch 0 taken 470 times.
✓ Branch 1 taken 1034 times.
1504 if(orientation==2) continue;
1563 1034 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
1564 }
1565 }
1566 }
1567 47 }
1568
1569 510 static void encode_header(SnowContext *s){
1570 int plane_index, i;
1571 uint8_t kstate[32];
1572
1573 510 memset(kstate, MID_STATE, sizeof(kstate));
1574
1575 510 put_rac(&s->c, kstate, s->keyframe);
1576
3/4
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 463 times.
510 if(s->keyframe || s->always_reset){
1577 47 ff_snow_reset_contexts(s);
1578 47 s->last_spatial_decomposition_type=
1579 47 s->last_qlog=
1580 47 s->last_qbias=
1581 47 s->last_mv_scale=
1582 47 s->last_block_max_depth= 0;
1583
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 47 times.
141 for(plane_index=0; plane_index<2; plane_index++){
1584 94 Plane *p= &s->plane[plane_index];
1585 94 p->last_htaps=0;
1586 94 p->last_diag_mc=0;
1587 94 memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
1588 }
1589 }
1590
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 463 times.
510 if(s->keyframe){
1591 47 put_symbol(&s->c, s->header_state, s->version, 0);
1592 47 put_rac(&s->c, s->header_state, s->always_reset);
1593 47 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
1594 47 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
1595 47 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1596 47 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
1597
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 if (s->nb_planes > 2) {
1598 47 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
1599 47 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
1600 }
1601 47 put_rac(&s->c, s->header_state, s->spatial_scalability);
1602 // put_rac(&s->c, s->header_state, s->rate_scalability);
1603 47 put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
1604
1605 47 encode_qlogs(s);
1606 }
1607
1608
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 47 times.
510 if(!s->keyframe){
1609 463 int update_mc=0;
1610
2/2
✓ Branch 0 taken 926 times.
✓ Branch 1 taken 463 times.
1389 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1611 926 Plane *p= &s->plane[plane_index];
1612 926 update_mc |= p->last_htaps != p->htaps;
1613 926 update_mc |= p->last_diag_mc != p->diag_mc;
1614 926 update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1615 }
1616 463 put_rac(&s->c, s->header_state, update_mc);
1617
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 416 times.
463 if(update_mc){
1618
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 47 times.
141 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1619 94 Plane *p= &s->plane[plane_index];
1620 94 put_rac(&s->c, s->header_state, p->diag_mc);
1621 94 put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
1622
2/2
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 94 times.
376 for(i= p->htaps/2; i; i--)
1623 282 put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
1624 }
1625 }
1626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 463 times.
463 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1627 put_rac(&s->c, s->header_state, 1);
1628 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1629 encode_qlogs(s);
1630 }else
1631 463 put_rac(&s->c, s->header_state, 0);
1632 }
1633
1634 510 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
1635 510 put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
1636 510 put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
1637 510 put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
1638 510 put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
1639
1640 510 }
1641
1642 510 static void update_last_header_values(SnowContext *s){
1643 int plane_index;
1644
1645
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 47 times.
510 if(!s->keyframe){
1646
2/2
✓ Branch 0 taken 926 times.
✓ Branch 1 taken 463 times.
1389 for(plane_index=0; plane_index<2; plane_index++){
1647 926 Plane *p= &s->plane[plane_index];
1648 926 p->last_diag_mc= p->diag_mc;
1649 926 p->last_htaps = p->htaps;
1650 926 memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1651 }
1652 }
1653
1654 510 s->last_spatial_decomposition_type = s->spatial_decomposition_type;
1655 510 s->last_qlog = s->qlog;
1656 510 s->last_qbias = s->qbias;
1657 510 s->last_mv_scale = s->mv_scale;
1658 510 s->last_block_max_depth = s->block_max_depth;
1659 510 s->last_spatial_decomposition_count = s->spatial_decomposition_count;
1660 510 }
1661
1662 360 static int qscale2qlog(int qscale){
1663 360 return lrint(QROOT*log2(qscale / (float)FF_QP2LAMBDA))
1664 360 + 61*QROOT/8; ///< 64 > 60
1665 }
1666
1667 static int ratecontrol_1pass(SnowEncContext *enc, AVFrame *pict)
1668 {
1669 SnowContext *const s = &enc->com;
1670 /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
1671 * FIXME we know exact mv bits at this point,
1672 * but ratecontrol isn't set up to include them. */
1673 uint32_t coef_sum= 0;
1674 int level, orientation, delta_qlog;
1675
1676 for(level=0; level<s->spatial_decomposition_count; level++){
1677 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1678 SubBand *b= &s->plane[0].band[level][orientation];
1679 IDWTELEM *buf= b->ibuf;
1680 const int w= b->width;
1681 const int h= b->height;
1682 const int stride= b->stride;
1683 const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
1684 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1685 const int qdiv= (1<<16)/qmul;
1686 int x, y;
1687 //FIXME this is ugly
1688 for(y=0; y<h; y++)
1689 for(x=0; x<w; x++)
1690 buf[x+y*stride]= b->buf[x+y*stride];
1691 if(orientation==0)
1692 decorrelate(s, b, buf, stride, 1, 0);
1693 for(y=0; y<h; y++)
1694 for(x=0; x<w; x++)
1695 coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
1696 }
1697 }
1698
1699 /* ugly, ratecontrol just takes a sqrt again */
1700 av_assert0(coef_sum < INT_MAX);
1701 coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
1702
1703 if(pict->pict_type == AV_PICTURE_TYPE_I){
1704 enc->m.mb_var_sum = coef_sum;
1705 enc->m.mc_mb_var_sum = 0;
1706 }else{
1707 enc->m.mc_mb_var_sum = coef_sum;
1708 enc->m.mb_var_sum = 0;
1709 }
1710
1711 pict->quality= ff_rate_estimate_qscale(&enc->m, 1);
1712 if (pict->quality < 0)
1713 return INT_MIN;
1714 enc->lambda= pict->quality * 3/2;
1715 delta_qlog= qscale2qlog(pict->quality) - s->qlog;
1716 s->qlog+= delta_qlog;
1717 return delta_qlog;
1718 }
1719
1720 33 static void calculate_visual_weight(SnowContext *s, Plane *p){
1721 33 int width = p->width;
1722 33 int height= p->height;
1723 int level, orientation, x, y;
1724
1725
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 33 times.
198 for(level=0; level<s->spatial_decomposition_count; level++){
1726 165 int64_t error=0;
1727
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 165 times.
693 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1728 528 SubBand *b= &p->band[level][orientation];
1729 528 IDWTELEM *ibuf= b->ibuf;
1730
1731 528 memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
1732 528 ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
1733 528 ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
1734
2/2
✓ Branch 0 taken 76800 times.
✓ Branch 1 taken 528 times.
77328 for(y=0; y<height; y++){
1735
2/2
✓ Branch 0 taken 28385280 times.
✓ Branch 1 taken 76800 times.
28462080 for(x=0; x<width; x++){
1736 28385280 int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
1737 28385280 error += d*d;
1738 }
1739 }
1740
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 363 times.
528 if (orientation == 2)
1741 165 error /= 2;
1742 528 b->qlog= (int)(QROOT * log2(352256.0/sqrt(error)) + 0.5);
1743
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 165 times.
528 if (orientation != 1)
1744 363 error = 0;
1745 }
1746 165 p->band[level][1].qlog = p->band[level][2].qlog;
1747 }
1748 33 }
1749
1750 510 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1751 const AVFrame *pict, int *got_packet)
1752 {
1753 510 SnowEncContext *const enc = avctx->priv_data;
1754 510 SnowContext *const s = &enc->com;
1755 510 MpegEncContext *const mpv = &enc->m;
1756 510 RangeCoder * const c= &s->c;
1757 510 AVCodecInternal *avci = avctx->internal;
1758 AVFrame *pic;
1759 510 const int width= s->avctx->width;
1760 510 const int height= s->avctx->height;
1761 int level, orientation, plane_index, i, y, ret;
1762 uint8_t rc_header_bak[sizeof(s->header_state)];
1763 uint8_t rc_block_bak[sizeof(s->block_state)];
1764
1765
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 510 times.
510 if ((ret = ff_alloc_packet(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
1766 return ret;
1767
1768 510 ff_init_range_encoder(c, pkt->data, pkt->size);
1769 510 ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1770
1771
2/2
✓ Branch 0 taken 1530 times.
✓ Branch 1 taken 510 times.
2040 for(i=0; i < s->nb_planes; i++){
1772
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 510 times.
1530 int hshift= i ? s->chroma_h_shift : 0;
1773
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 510 times.
1530 int vshift= i ? s->chroma_v_shift : 0;
1774
2/2
✓ Branch 0 taken 193920 times.
✓ Branch 1 taken 1530 times.
195450 for(y=0; y<AV_CEIL_RSHIFT(height, vshift); y++)
1775 193920 memcpy(&s->input_picture->data[i][y * s->input_picture->linesize[i]],
1776 193920 &pict->data[i][y * pict->linesize[i]],
1777 193920 AV_CEIL_RSHIFT(width, hshift));
1778 1530 enc->mpvencdsp.draw_edges(s->input_picture->data[i], s->inpu