FFmpeg coverage


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