FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/snowenc.c
Date: 2026-04-17 20:31:08
Exec Total Coverage
Lines: 1084 1318 82.2%
Functions: 30 33 90.9%
Branches: 637 842 75.7%

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