FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/snowenc.c
Date: 2026-08-19 01:31:13
Exec Total Coverage
Lines: 1190 1323 89.9%
Functions: 32 33 97.0%
Branches: 701 848 82.7%

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