FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/snowenc.c
Date: 2024-11-21 09:21:34
Exec Total Coverage
Lines: 1091 1321 82.6%
Functions: 30 33 90.9%
Branches: 635 838 75.8%

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