FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/snowenc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 975 1211 80.5%
Branches: 593 800 74.1%

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/intmath.h"
22 #include "libavutil/libm.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avcodec.h"
27 #include "encode.h"
28 #include "internal.h"
29 #include "packet_internal.h"
30 #include "snow_dwt.h"
31 #include "snow.h"
32
33 #include "rangecoder.h"
34 #include "mathops.h"
35
36 #include "mpegvideo.h"
37 #include "h263.h"
38
39 9 static av_cold int encode_init(AVCodecContext *avctx)
40 {
41 9 SnowContext *s = avctx->priv_data;
42 int plane_index, ret;
43 int i;
44
45
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if(s->pred == DWT_97
46
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 && (avctx->flags & AV_CODEC_FLAG_QSCALE)
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 && avctx->global_quality == 0){
48 av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
49 return AVERROR(EINVAL);
50 }
51
52 9 s->spatial_decomposition_type= s->pred; //FIXME add decorrelator type r transform_type
53
54
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 s->mv_scale = (avctx->flags & AV_CODEC_FLAG_QPEL) ? 2 : 4;
55 9 s->block_max_depth= (avctx->flags & AV_CODEC_FLAG_4MV ) ? 1 : 0;
56
57
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 9 times.
36 for(plane_index=0; plane_index<3; plane_index++){
58 27 s->plane[plane_index].diag_mc= 1;
59 27 s->plane[plane_index].htaps= 6;
60 27 s->plane[plane_index].hcoeff[0]= 40;
61 27 s->plane[plane_index].hcoeff[1]= -10;
62 27 s->plane[plane_index].hcoeff[2]= 2;
63 27 s->plane[plane_index].fast_mc= 1;
64 }
65
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_snow_common_init(avctx)) < 0) {
67 return ret;
68 }
69 9 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
70
71 9 ff_snow_alloc_blocks(s);
72
73 9 s->version=0;
74
75 9 s->m.avctx = avctx;
76 9 s->m.bit_rate= avctx->bit_rate;
77 9 s->m.lmin = avctx->mb_lmin;
78 9 s->m.lmax = avctx->mb_lmax;
79 9 s->m.mb_num = (avctx->width * avctx->height + 255) / 256; // For ratecontrol
80
81 9 s->m.me.temp =
82 9 s->m.me.scratchpad = av_calloc(avctx->width + 64, 2*16*2*sizeof(uint8_t));
83 9 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
84 9 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
85 9 s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
86
4/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9 times.
9 if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
87 return AVERROR(ENOMEM);
88
89 9 ff_h263_encode_init(&s->m); //mv_penalty
90
91 9 s->max_ref_frames = av_clip(avctx->refs, 1, MAX_REF_FRAMES);
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if(avctx->flags&AV_CODEC_FLAG_PASS1){
94 if(!avctx->stats_out)
95 avctx->stats_out = av_mallocz(256);
96
97 if (!avctx->stats_out)
98 return AVERROR(ENOMEM);
99 }
100
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if((avctx->flags&AV_CODEC_FLAG_PASS2) || !(avctx->flags&AV_CODEC_FLAG_QSCALE)){
101 ret = ff_rate_control_init(&s->m);
102 if(ret < 0)
103 return ret;
104 }
105 9 s->pass1_rc= !(avctx->flags & (AV_CODEC_FLAG_QSCALE|AV_CODEC_FLAG_PASS2));
106
107
1/3
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
9 switch(avctx->pix_fmt){
108 9 case AV_PIX_FMT_YUV444P:
109 // case AV_PIX_FMT_YUV422P:
110 case AV_PIX_FMT_YUV420P:
111 // case AV_PIX_FMT_YUV411P:
112 case AV_PIX_FMT_YUV410P:
113 9 s->nb_planes = 3;
114 9 s->colorspace_type= 0;
115 9 break;
116 case AV_PIX_FMT_GRAY8:
117 s->nb_planes = 1;
118 s->colorspace_type = 1;
119 break;
120 /* case AV_PIX_FMT_RGB32:
121 s->colorspace= 1;
122 break;*/
123 default:
124 av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
125 return AVERROR_PATCHWELCOME;
126 }
127
128 9 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
129 &s->chroma_v_shift);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret) {
131 av_log(avctx, AV_LOG_ERROR, "pixel format invalid or unknown\n");
132 return ret;
133 }
134
135 9 ff_set_cmp(&s->mecc, s->mecc.me_cmp, s->avctx->me_cmp);
136 9 ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
137
138 9 s->input_picture = av_frame_alloc();
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!s->input_picture)
140 return AVERROR(ENOMEM);
141
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
143 return ret;
144
145
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if(s->motion_est == FF_ME_ITER){
146 6 int size= s->b_width * s->b_height << 2*s->block_max_depth;
147
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for(i=0; i<s->max_ref_frames; i++){
148 6 s->ref_mvs[i] = av_calloc(size, sizeof(*s->ref_mvs[i]));
149 6 s->ref_scores[i] = av_calloc(size, sizeof(*s->ref_scores[i]));
150
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])
151 return AVERROR(ENOMEM);
152 }
153 }
154
155 9 return 0;
156 }
157
158 //near copy & paste from dsputil, FIXME
159 827820 static int pix_sum(uint8_t * pix, int line_size, int w, int h)
160 {
161 int s, i, j;
162
163 827820 s = 0;
164
2/2
✓ Branch 0 taken 5408640 times.
✓ Branch 1 taken 827820 times.
6236460 for (i = 0; i < h; i++) {
165
2/2
✓ Branch 0 taken 44375040 times.
✓ Branch 1 taken 5408640 times.
49783680 for (j = 0; j < w; j++) {
166 44375040 s += pix[0];
167 44375040 pix ++;
168 }
169 5408640 pix += line_size - w;
170 }
171 827820 return s;
172 }
173
174 //near copy & paste from dsputil, FIXME
175 275940 static int pix_norm1(uint8_t * pix, int line_size, int w)
176 {
177 int s, i, j;
178 275940 const uint32_t *sq = ff_square_tab + 256;
179
180 275940 s = 0;
181
2/2
✓ Branch 0 taken 2704320 times.
✓ Branch 1 taken 275940 times.
2980260 for (i = 0; i < w; i++) {
182
2/2
✓ Branch 0 taken 29583360 times.
✓ Branch 1 taken 2704320 times.
32287680 for (j = 0; j < w; j ++) {
183 29583360 s += sq[pix[0]];
184 29583360 pix ++;
185 }
186 2704320 pix += line_size - w;
187 }
188 275940 return s;
189 }
190
191 1144148 static inline int get_penalty_factor(int lambda, int lambda2, int type){
192
2/7
✓ Branch 0 taken 810540 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 333608 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1144148 switch(type&0xFF){
193 810540 default:
194 case FF_CMP_SAD:
195 810540 return lambda>>FF_LAMBDA_SHIFT;
196 case FF_CMP_DCT:
197 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
198 case FF_CMP_W53:
199 return (4*lambda)>>(FF_LAMBDA_SHIFT);
200 333608 case FF_CMP_W97:
201 333608 return (2*lambda)>>(FF_LAMBDA_SHIFT);
202 case FF_CMP_SATD:
203 case FF_CMP_DCT264:
204 return (2*lambda)>>FF_LAMBDA_SHIFT;
205 case FF_CMP_RD:
206 case FF_CMP_PSNR:
207 case FF_CMP_SSE:
208 case FF_CMP_NSSE:
209 return lambda2>>FF_LAMBDA_SHIFT;
210 case FF_CMP_BIT:
211 return 1;
212 }
213 }
214
215 //FIXME copy&paste
216 #define P_LEFT P[1]
217 #define P_TOP P[2]
218 #define P_TOPRIGHT P[3]
219 #define P_MEDIAN P[4]
220 #define P_MV1 P[9]
221 #define FLAG_QPEL 1 //must be 1
222
223 281880 static int encode_q_branch(SnowContext *s, int level, int x, int y){
224 uint8_t p_buffer[1024];
225 uint8_t i_buffer[1024];
226 uint8_t p_state[sizeof(s->block_state)];
227 uint8_t i_state[sizeof(s->block_state)];
228 RangeCoder pc, ic;
229 281880 uint8_t *pbbak= s->c.bytestream;
230 281880 uint8_t *pbbak_start= s->c.bytestream_start;
231 int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
232 281880 const int w= s->b_width << s->block_max_depth;
233 281880 const int h= s->b_height << s->block_max_depth;
234 281880 const int rem_depth= s->block_max_depth - level;
235 281880 const int index= (x + y*w) << rem_depth;
236 281880 const int block_w= 1<<(LOG2_MB_SIZE - level);
237 281880 int trx= (x+1)<<rem_depth;
238 281880 int try= (y+1)<<rem_depth;
239
2/2
✓ Branch 0 taken 273240 times.
✓ Branch 1 taken 8640 times.
281880 const BlockNode *left = x ? &s->block[index-1] : &null_block;
240
2/2
✓ Branch 0 taken 270480 times.
✓ Branch 1 taken 11400 times.
281880 const BlockNode *top = y ? &s->block[index-w] : &null_block;
241
2/2
✓ Branch 0 taken 273240 times.
✓ Branch 1 taken 8640 times.
281880 const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
242
2/2
✓ Branch 0 taken 270480 times.
✓ Branch 1 taken 11400 times.
281880 const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
243
4/4
✓ Branch 0 taken 270480 times.
✓ Branch 1 taken 11400 times.
✓ Branch 2 taken 262395 times.
✓ Branch 3 taken 8085 times.
281880 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
244
8/8
✓ Branch 0 taken 270480 times.
✓ Branch 1 taken 11400 times.
✓ Branch 2 taken 262395 times.
✓ Branch 3 taken 8085 times.
✓ Branch 4 taken 127155 times.
✓ Branch 5 taken 135240 times.
✓ Branch 6 taken 27930 times.
✓ Branch 7 taken 99225 times.
281880 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
245 281880 int pl = left->color[0];
246 281880 int pcb= left->color[1];
247 281880 int pcr= left->color[2];
248 int pmx, pmy;
249 281880 int mx=0, my=0;
250 int l,cr,cb;
251 281880 const int stride= s->current_picture->linesize[0];
252 281880 const int uvstride= s->current_picture->linesize[1];
253 281880 uint8_t *current_data[3]= { s->input_picture->data[0] + (x + y* stride)*block_w,
254 281880 s->input_picture->data[1] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift),
255 281880 s->input_picture->data[2] + ((x*block_w)>>s->chroma_h_shift) + ((y*uvstride*block_w)>>s->chroma_v_shift)};
256 int P[10][2];
257 int16_t last_mv[3][2];
258 281880 int qpel= !!(s->avctx->flags & AV_CODEC_FLAG_QPEL); //unused
259 281880 const int shift= 1+qpel;
260 281880 MotionEstContext *c= &s->m.me;
261 281880 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
262 281880 int mx_context= av_log2(2*FFABS(left->mx - top->mx));
263 281880 int my_context= av_log2(2*FFABS(left->my - top->my));
264 281880 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
265 int ref, best_ref, ref_score, ref_mx, ref_my;
266
267 av_assert0(sizeof(s->block_state) >= 256);
268
2/2
✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 275940 times.
281880 if(s->keyframe){
269 5940 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
270 5940 return 0;
271 }
272
273 // clip predictors / edge ?
274
275 275940 P_LEFT[0]= left->mx;
276 275940 P_LEFT[1]= left->my;
277 275940 P_TOP [0]= top->mx;
278 275940 P_TOP [1]= top->my;
279 275940 P_TOPRIGHT[0]= tr->mx;
280 275940 P_TOPRIGHT[1]= tr->my;
281
282 275940 last_mv[0][0]= s->block[index].mx;
283 275940 last_mv[0][1]= s->block[index].my;
284 275940 last_mv[1][0]= right->mx;
285 275940 last_mv[1][1]= right->my;
286 275940 last_mv[2][0]= bottom->mx;
287 275940 last_mv[2][1]= bottom->my;
288
289 275940 s->m.mb_stride=2;
290 275940 s->m.mb_x=
291 275940 s->m.mb_y= 0;
292 275940 c->skip= 0;
293
294 av_assert1(c-> stride == stride);
295 av_assert1(c->uvstride == uvstride);
296
297 275940 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
298 275940 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
299 275940 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
300 275940 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_DMV;
301
302 275940 c->xmin = - x*block_w - 16+3;
303 275940 c->ymin = - y*block_w - 16+3;
304 275940 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
305 275940 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
306
307
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 275939 times.
275940 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
308
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 275939 times.
275940 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275940 times.
275940 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
310
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 275939 times.
275940 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
311
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 275939 times.
275940 if(P_TOPRIGHT[0] < (c->xmin * (1<<shift))) P_TOPRIGHT[0]= (c->xmin * (1<<shift));
312
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 275939 times.
275940 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
313
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 275939 times.
275940 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
314
315 275940 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
316 275940 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
317
318
2/2
✓ Branch 0 taken 11070 times.
✓ Branch 1 taken 264870 times.
275940 if (!y) {
319 11070 c->pred_x= P_LEFT[0];
320 11070 c->pred_y= P_LEFT[1];
321 } else {
322 264870 c->pred_x = P_MEDIAN[0];
323 264870 c->pred_y = P_MEDIAN[1];
324 }
325
326 275940 score= INT_MAX;
327 275940 best_ref= 0;
328
2/2
✓ Branch 0 taken 275940 times.
✓ Branch 1 taken 275940 times.
551880 for(ref=0; ref<s->ref_frames; ref++){
329 275940 init_ref(c, current_data, s->last_picture[ref]->data, NULL, block_w*x, block_w*y, 0);
330
331 275940 ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
332 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
333
334 av_assert2(ref_mx >= c->xmin);
335 av_assert2(ref_mx <= c->xmax);
336 av_assert2(ref_my >= c->ymin);
337 av_assert2(ref_my <= c->ymax);
338
339 275940 ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
340 275940 ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
341 275940 ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
342
2/2
✓ Branch 0 taken 8640 times.
✓ Branch 1 taken 267300 times.
275940 if(s->ref_mvs[ref]){
343 8640 s->ref_mvs[ref][index][0]= ref_mx;
344 8640 s->ref_mvs[ref][index][1]= ref_my;
345 8640 s->ref_scores[ref][index]= ref_score;
346 }
347
1/2
✓ Branch 0 taken 275940 times.
✗ Branch 1 not taken.
275940 if(score > ref_score){
348 275940 score= ref_score;
349 275940 best_ref= ref;
350 275940 mx= ref_mx;
351 275940 my= ref_my;
352 }
353 }
354 //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
355
356 // subpel search
357 275940 base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
358 275940 pc= s->c;
359 275940 pc.bytestream_start=
360 275940 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
361 275940 memcpy(p_state, s->block_state, sizeof(s->block_state));
362
363
2/2
✓ Branch 0 taken 53460 times.
✓ Branch 1 taken 222480 times.
275940 if(level!=s->block_max_depth)
364 53460 put_rac(&pc, &p_state[4 + s_context], 1);
365 275940 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275940 times.
275940 if(s->ref_frames > 1)
367 put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
368 275940 pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275940 times.
275940 put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275940 times.
275940 put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
371 275940 p_len= pc.bytestream - pc.bytestream_start;
372 275940 score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
373
374 275940 block_s= block_w*block_w;
375 275940 sum = pix_sum(current_data[0], stride, block_w, block_w);
376 275940 l= (sum + block_s/2)/block_s;
377 275940 iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
378
379
1/2
✓ Branch 0 taken 275940 times.
✗ Branch 1 not taken.
275940 if (s->nb_planes > 2) {
380 275940 block_s= block_w*block_w>>(s->chroma_h_shift + s->chroma_v_shift);
381 275940 sum = pix_sum(current_data[1], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
382 275940 cb= (sum + block_s/2)/block_s;
383 // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
384 275940 sum = pix_sum(current_data[2], uvstride, block_w>>s->chroma_h_shift, block_w>>s->chroma_v_shift);
385 275940 cr= (sum + block_s/2)/block_s;
386 // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
387 }else
388 cb = cr = 0;
389
390 275940 ic= s->c;
391 275940 ic.bytestream_start=
392 275940 ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
393 275940 memcpy(i_state, s->block_state, sizeof(s->block_state));
394
2/2
✓ Branch 0 taken 53460 times.
✓ Branch 1 taken 222480 times.
275940 if(level!=s->block_max_depth)
395 53460 put_rac(&ic, &i_state[4 + s_context], 1);
396 275940 put_rac(&ic, &i_state[1 + left->type + top->type], 1);
397 275940 put_symbol(&ic, &i_state[32], l-pl , 1);
398
1/2
✓ Branch 0 taken 275940 times.
✗ Branch 1 not taken.
275940 if (s->nb_planes > 2) {
399 275940 put_symbol(&ic, &i_state[64], cb-pcb, 1);
400 275940 put_symbol(&ic, &i_state[96], cr-pcr, 1);
401 }
402 275940 i_len= ic.bytestream - ic.bytestream_start;
403 275940 iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
404
405 av_assert1(iscore < 255*255*256 + s->lambda2*10);
406 av_assert1(iscore >= 0);
407 av_assert1(l>=0 && l<=255);
408 av_assert1(pl>=0 && pl<=255);
409
410
2/2
✓ Branch 0 taken 62100 times.
✓ Branch 1 taken 213840 times.
275940 if(level==0){
411 62100 int varc= iscore >> 8;
412 62100 int vard= score >> 8;
413
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 62098 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
62100 if (vard <= 64 || vard < varc)
414 62100 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
415 else
416 c->scene_change_score+= s->m.qscale;
417 }
418
419
2/2
✓ Branch 0 taken 53460 times.
✓ Branch 1 taken 222480 times.
275940 if(level!=s->block_max_depth){
420 53460 put_rac(&s->c, &s->block_state[4 + s_context], 0);
421 53460 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
422 53460 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
423 53460 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
424 53460 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
425 53460 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
426
427
3/4
✓ Branch 0 taken 45013 times.
✓ Branch 1 taken 8447 times.
✓ Branch 2 taken 45013 times.
✗ Branch 3 not taken.
53460 if(score2 < score && score2 < iscore)
428 45013 return score2;
429 }
430
431
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 230922 times.
230927 if(iscore < score){
432 5 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
433 5 memcpy(pbbak, i_buffer, i_len);
434 5 s->c= ic;
435 5 s->c.bytestream_start= pbbak_start;
436 5 s->c.bytestream= pbbak + i_len;
437 5 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
438 5 memcpy(s->block_state, i_state, sizeof(s->block_state));
439 5 return iscore;
440 }else{
441 230922 memcpy(pbbak, p_buffer, p_len);
442 230922 s->c= pc;
443 230922 s->c.bytestream_start= pbbak_start;
444 230922 s->c.bytestream= pbbak + p_len;
445 230922 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
446 230922 memcpy(s->block_state, p_state, sizeof(s->block_state));
447 230922 return score;
448 }
449 }
450
451 9600 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
452 9600 const int w= s->b_width << s->block_max_depth;
453 9600 const int rem_depth= s->block_max_depth - level;
454 9600 const int index= (x + y*w) << rem_depth;
455 9600 int trx= (x+1)<<rem_depth;
456 9600 BlockNode *b= &s->block[index];
457
2/2
✓ Branch 0 taken 8400 times.
✓ Branch 1 taken 1200 times.
9600 const BlockNode *left = x ? &s->block[index-1] : &null_block;
458
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 2400 times.
9600 const BlockNode *top = y ? &s->block[index-w] : &null_block;
459
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;
460
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
461 9600 int pl = left->color[0];
462 9600 int pcb= left->color[1];
463 9600 int pcr= left->color[2];
464 int pmx, pmy;
465 9600 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
466
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;
467
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;
468 9600 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
469
470
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 8640 times.
9600 if(s->keyframe){
471 960 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
472 960 return;
473 }
474
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8640 times.
8640 if(level!=s->block_max_depth){
476 if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
477 put_rac(&s->c, &s->block_state[4 + s_context], 1);
478 }else{
479 put_rac(&s->c, &s->block_state[4 + s_context], 0);
480 encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
481 encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
482 encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
483 encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
484 return;
485 }
486 }
487
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 8600 times.
8640 if(b->type & BLOCK_INTRA){
488 40 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
489 40 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
490 40 put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
491
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (s->nb_planes > 2) {
492 40 put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
493 40 put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
494 }
495 40 set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
496 }else{
497 8600 pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
498 8600 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8600 times.
8600 if(s->ref_frames > 1)
500 put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
501 8600 put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
502 8600 put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
503 8600 set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
504 }
505 }
506
507 56355 static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
508 int i, x2, y2;
509 56355 Plane *p= &s->plane[plane_index];
510 56355 const int block_size = MB_SIZE >> s->block_max_depth;
511
2/2
✓ Branch 0 taken 37570 times.
✓ Branch 1 taken 18785 times.
56355 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
512
2/2
✓ Branch 0 taken 37570 times.
✓ Branch 1 taken 18785 times.
56355 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
513
2/2
✓ Branch 0 taken 37570 times.
✓ Branch 1 taken 18785 times.
56355 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
514
2/2
✓ Branch 0 taken 37570 times.
✓ Branch 1 taken 18785 times.
56355 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
515 56355 const int ref_stride= s->current_picture->linesize[plane_index];
516 56355 uint8_t *src= s-> input_picture->data[plane_index];
517 56355 IDWTELEM *dst= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
518 56355 const int b_stride = s->b_width << s->block_max_depth;
519 56355 const int w= p->width;
520 56355 const int h= p->height;
521 56355 int index= mb_x + mb_y*b_stride;
522 56355 BlockNode *b= &s->block[index];
523 56355 BlockNode backup= *b;
524 56355 int ab=0;
525 56355 int aa=0;
526
527 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc stuff above
528
529 56355 b->type|= BLOCK_INTRA;
530 56355 b->color[plane_index]= 0;
531 56355 memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
532
533
2/2
✓ Branch 0 taken 225420 times.
✓ Branch 1 taken 56355 times.
281775 for(i=0; i<4; i++){
534 225420 int mb_x2= mb_x + (i &1) - 1;
535 225420 int mb_y2= mb_y + (i>>1) - 1;
536 225420 int x= block_w*mb_x2 + block_w/2;
537 225420 int y= block_h*mb_y2 + block_h/2;
538
539 225420 add_yblock(s, 0, NULL, dst + (i&1)*block_w + (i>>1)*obmc_stride*block_h, NULL, obmc,
540 x, y, block_w, block_h, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
541
542
2/2
✓ Branch 0 taken 2126720 times.
✓ Branch 1 taken 225420 times.
2352140 for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_h); y2++){
543
2/2
✓ Branch 0 taken 24128352 times.
✓ Branch 1 taken 2126720 times.
26255072 for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
544 24128352 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_h*mb_y - block_h/2))*obmc_stride;
545 24128352 int obmc_v= obmc[index];
546 int d;
547
2/2
✓ Branch 0 taken 1813056 times.
✓ Branch 1 taken 22315296 times.
24128352 if(y<0) obmc_v += obmc[index + block_h*obmc_stride];
548
2/2
✓ Branch 0 taken 762912 times.
✓ Branch 1 taken 23365440 times.
24128352 if(x<0) obmc_v += obmc[index + block_w];
549
2/2
✓ Branch 0 taken 1342560 times.
✓ Branch 1 taken 22785792 times.
24128352 if(y+block_h>h) obmc_v += obmc[index - block_h*obmc_stride];
550
2/2
✓ Branch 0 taken 629376 times.
✓ Branch 1 taken 23498976 times.
24128352 if(x+block_w>w) obmc_v += obmc[index - block_w];
551 //FIXME precalculate this or simplify it somehow else
552
553 24128352 d = -dst[index] + (1<<(FRAC_BITS-1));
554 24128352 dst[index] = d;
555 24128352 ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
556 24128352 aa += obmc_v * obmc_v; //FIXME precalculate this
557 }
558 }
559 }
560 56355 *b= backup;
561
562
2/2
✓ Branch 0 taken 56280 times.
✓ Branch 1 taken 75 times.
56355 return av_clip_uint8( ROUNDED_DIV(ab<<LOG2_OBMC_MAX, aa) ); //FIXME we should not need clipping
563 }
564
565 1304392 static inline int get_block_bits(SnowContext *s, int x, int y, int w){
566 1304392 const int b_stride = s->b_width << s->block_max_depth;
567 1304392 const int b_height = s->b_height<< s->block_max_depth;
568 1304392 int index= x + y*b_stride;
569 1304392 const BlockNode *b = &s->block[index];
570
2/2
✓ Branch 0 taken 1183544 times.
✓ Branch 1 taken 120848 times.
1304392 const BlockNode *left = x ? &s->block[index-1] : &null_block;
571
2/2
✓ Branch 0 taken 1138586 times.
✓ Branch 1 taken 165806 times.
1304392 const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
572
4/4
✓ Branch 0 taken 1138586 times.
✓ Branch 1 taken 165806 times.
✓ Branch 2 taken 1027679 times.
✓ Branch 3 taken 110907 times.
1304392 const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
573
4/4
✓ Branch 0 taken 1138586 times.
✓ Branch 1 taken 165806 times.
✓ Branch 2 taken 992035 times.
✓ Branch 3 taken 146551 times.
1304392 const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
574 int dmx, dmy;
575 // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
576 // int my_context= av_log2(2*FFABS(left->my - top->my));
577
578
6/6
✓ Branch 0 taken 1265838 times.
✓ Branch 1 taken 38554 times.
✓ Branch 2 taken 1234136 times.
✓ Branch 3 taken 31702 times.
✓ Branch 4 taken 127259 times.
✓ Branch 5 taken 1106877 times.
1304392 if(x<0 || x>=b_stride || y>=b_height)
579 197515 return 0;
580 /*
581 1 0 0
582 01X 1-2 1
583 001XX 3-6 2-3
584 0001XXX 7-14 4-7
585 00001XXXX 15-30 8-15
586 */
587 //FIXME try accurate rate
588 //FIXME intra and inter predictors if surrounding blocks are not the same type
589
2/2
✓ Branch 0 taken 23255 times.
✓ Branch 1 taken 1083622 times.
1106877 if(b->type & BLOCK_INTRA){
590 23255 return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
591 23255 + av_log2(2*FFABS(left->color[1] - b->color[1]))
592 23255 + av_log2(2*FFABS(left->color[2] - b->color[2])));
593 }else{
594 1083622 pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
595 1083622 dmx-= b->mx;
596 1083622 dmy-= b->my;
597 1083622 return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
598 1083622 + av_log2(2*FFABS(dmy))
599 1083622 + av_log2(2*b->ref));
600 }
601 }
602
603 316328 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, uint8_t (*obmc_edged)[MB_SIZE * 2]){
604 316328 Plane *p= &s->plane[plane_index];
605 316328 const int block_size = MB_SIZE >> s->block_max_depth;
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316328 times.
316328 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316328 times.
316328 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316328 times.
316328 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
609 316328 const int ref_stride= s->current_picture->linesize[plane_index];
610 316328 uint8_t *dst= s->current_picture->data[plane_index];
611 316328 uint8_t *src= s-> input_picture->data[plane_index];
612 316328 IDWTELEM *pred= (IDWTELEM*)s->m.sc.obmc_scratchpad + plane_index*block_size*block_size*4;
613 316328 uint8_t *cur = s->scratchbuf;
614 316328 uint8_t *tmp = s->emu_edge_buffer;
615 316328 const int b_stride = s->b_width << s->block_max_depth;
616 316328 const int b_height = s->b_height<< s->block_max_depth;
617 316328 const int w= p->width;
618 316328 const int h= p->height;
619 int distortion;
620 316328 int rate= 0;
621 316328 const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
622 316328 int sx= block_w*mb_x - block_w/2;
623 316328 int sy= block_h*mb_y - block_h/2;
624
2/2
✓ Branch 0 taken 38554 times.
✓ Branch 1 taken 277774 times.
316328 int x0= FFMAX(0,-sx);
625
2/2
✓ Branch 0 taken 82903 times.
✓ Branch 1 taken 233425 times.
316328 int y0= FFMAX(0,-sy);
626 316328 int x1= FFMIN(block_w*2, w-sx);
627 316328 int y1= FFMIN(block_h*2, h-sy);
628 int i,x,y;
629
630 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below chckinhg only block_w
631
632 316328 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);
633
634
2/2
✓ Branch 0 taken 8954624 times.
✓ Branch 1 taken 316328 times.
9270952 for(y=y0; y<y1; y++){
635 8954624 const uint8_t *obmc1= obmc_edged[y];
636 8954624 const IDWTELEM *pred1 = pred + y*obmc_stride;
637 8954624 uint8_t *cur1 = cur + y*ref_stride;
638 8954624 uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
639
2/2
✓ Branch 0 taken 270600576 times.
✓ Branch 1 taken 8954624 times.
279555200 for(x=x0; x<x1; x++){
640 #if FRAC_BITS >= LOG2_OBMC_MAX
641 int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
642 #else
643 270600576 int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
644 #endif
645 270600576 v = (v + pred1[x]) >> FRAC_BITS;
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270600576 times.
270600576 if(v&(~255)) v= ~(v>>31);
647 270600576 dst1[x] = v;
648 }
649 }
650
651 /* copy the regions where obmc[] = (uint8_t)256 */
652
2/2
✓ Branch 0 taken 277774 times.
✓ Branch 1 taken 38554 times.
316328 if(LOG2_OBMC_MAX == 8
653
2/2
✓ Branch 0 taken 31702 times.
✓ Branch 1 taken 246072 times.
277774 && (mb_x == 0 || mb_x == b_stride-1)
654
4/4
✓ Branch 0 taken 51972 times.
✓ Branch 1 taken 18284 times.
✓ Branch 2 taken 13562 times.
✓ Branch 3 taken 38410 times.
70256 && (mb_y == 0 || mb_y == b_height-1)){
655
2/2
✓ Branch 0 taken 17372 times.
✓ Branch 1 taken 14474 times.
31846 if(mb_x == 0)
656 17372 x1 = block_w;
657 else
658 14474 x0 = block_w;
659
2/2
✓ Branch 0 taken 18284 times.
✓ Branch 1 taken 13562 times.
31846 if(mb_y == 0)
660 18284 y1 = block_h;
661 else
662 13562 y0 = block_h;
663
2/2
✓ Branch 0 taken 254768 times.
✓ Branch 1 taken 31846 times.
286614 for(y=y0; y<y1; y++)
664 254768 memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
665 }
666
667
1/2
✓ Branch 0 taken 316328 times.
✗ Branch 1 not taken.
316328 if(block_w==16){
668 /* FIXME rearrange dsputil to fit 32x32 cmp functions */
669 /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
670 /* FIXME cmps overlap but do not cover the wavelet's whole support.
671 * So improving the score of one block is not strictly guaranteed
672 * to improve the score of the whole frame, thus iterative motion
673 * estimation does not always converge. */
674
1/2
✓ Branch 0 taken 316328 times.
✗ Branch 1 not taken.
316328 if(s->avctx->me_cmp == FF_CMP_W97)
675 316328 distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
676 else if(s->avctx->me_cmp == FF_CMP_W53)
677 distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
678 else{
679 distortion = 0;
680 for(i=0; i<4; i++){
681 int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
682 distortion += s->mecc.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
683 }
684 }
685 }else{
686 av_assert2(block_w==8);
687 distortion = s->mecc.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
688 }
689
690
1/2
✓ Branch 0 taken 316328 times.
✗ Branch 1 not taken.
316328 if(plane_index==0){
691
2/2
✓ Branch 0 taken 1265312 times.
✓ Branch 1 taken 316328 times.
1581640 for(i=0; i<4; i++){
692 /* ..RRr
693 * .RXx.
694 * rxx..
695 */
696 1265312 rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
697 }
698
2/2
✓ Branch 0 taken 39080 times.
✓ Branch 1 taken 277248 times.
316328 if(mb_x == b_stride-2)
699 39080 rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
700 }
701 316328 return distortion + rate*penalty_factor;
702 }
703
704 static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
705 int i, y2;
706 Plane *p= &s->plane[plane_index];
707 const int block_size = MB_SIZE >> s->block_max_depth;
708 const int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
709 const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
710 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
711 const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
712 const int ref_stride= s->current_picture->linesize[plane_index];
713 uint8_t *dst= s->current_picture->data[plane_index];
714 uint8_t *src= s-> input_picture->data[plane_index];
715 //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
716 // const has only been removed from zero_dst to suppress a warning
717 static IDWTELEM zero_dst[4096]; //FIXME
718 const int b_stride = s->b_width << s->block_max_depth;
719 const int w= p->width;
720 const int h= p->height;
721 int distortion= 0;
722 int rate= 0;
723 const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
724
725 av_assert2(s->chroma_h_shift == s->chroma_v_shift); //obmc and square assumtions below
726
727 for(i=0; i<9; i++){
728 int mb_x2= mb_x + (i%3) - 1;
729 int mb_y2= mb_y + (i/3) - 1;
730 int x= block_w*mb_x2 + block_w/2;
731 int y= block_h*mb_y2 + block_h/2;
732
733 add_yblock(s, 0, NULL, zero_dst, dst, obmc,
734 x, y, block_w, block_h, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
735
736 //FIXME find a cleaner/simpler way to skip the outside stuff
737 for(y2= y; y2<0; y2++)
738 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
739 for(y2= h; y2<y+block_h; y2++)
740 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
741 if(x<0){
742 for(y2= y; y2<y+block_h; y2++)
743 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
744 }
745 if(x+block_w > w){
746 for(y2= y; y2<y+block_h; y2++)
747 memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
748 }
749
750 av_assert1(block_w== 8 || block_w==16);
751 distortion += s->mecc.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
752 }
753
754 if(plane_index==0){
755 BlockNode *b= &s->block[mb_x+mb_y*b_stride];
756 int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
757
758 /* ..RRRr
759 * .RXXx.
760 * .RXXx.
761 * rxxx.
762 */
763 if(merged)
764 rate = get_block_bits(s, mb_x, mb_y, 2);
765 for(i=merged?4:0; i<9; i++){
766 static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
767 rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
768 }
769 }
770 return distortion + rate*penalty_factor;
771 }
772
773 21600 static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
774 21600 const int w= b->width;
775 21600 const int h= b->height;
776 int x, y;
777
778 if(1){
779 21600 int run=0;
780 21600 int *runs = s->run_buffer;
781 21600 int run_index=0;
782 int max_index;
783
784
2/2
✓ Branch 0 taken 366600 times.
✓ Branch 1 taken 21600 times.
388200 for(y=0; y<h; y++){
785
2/2
✓ Branch 0 taken 26496000 times.
✓ Branch 1 taken 366600 times.
26862600 for(x=0; x<w; x++){
786 26496000 int v, p=0;
787 26496000 int /*ll=0, */l=0, lt=0, t=0, rt=0;
788 26496000 v= src[x + y*stride];
789
790
2/2
✓ Branch 0 taken 25960200 times.
✓ Branch 1 taken 535800 times.
26496000 if(y){
791 25960200 t= src[x + (y-1)*stride];
792
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x){
793 25615200 lt= src[x - 1 + (y-1)*stride];
794 }
795
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x + 1 < w){
796 25615200 rt= src[x + 1 + (y-1)*stride];
797 }
798 }
799
2/2
✓ Branch 0 taken 26129400 times.
✓ Branch 1 taken 366600 times.
26496000 if(x){
800 26129400 l= src[x - 1 + y*stride];
801 /*if(x > 1){
802 if(orientation==1) ll= src[y + (x-2)*stride];
803 else ll= src[x - 2 + y*stride];
804 }*/
805 }
806
2/2
✓ Branch 0 taken 26392500 times.
✓ Branch 1 taken 103500 times.
26496000 if(parent){
807 26392500 int px= x>>1;
808 26392500 int py= y>>1;
809
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)
810 26380800 p= parent[px + py*2*stride];
811 }
812
2/2
✓ Branch 0 taken 2820927 times.
✓ Branch 1 taken 23675073 times.
26496000 if(!(/*ll|*/l|lt|t|rt|p)){
813
2/2
✓ Branch 0 taken 291329 times.
✓ Branch 1 taken 2529598 times.
2820927 if(v){
814 291329 runs[run_index++]= run;
815 291329 run=0;
816 }else{
817 2529598 run++;
818 }
819 }
820 }
821 }
822 21600 max_index= run_index;
823 21600 runs[run_index++]= run;
824 21600 run_index=0;
825 21600 run= runs[run_index++];
826
827 21600 put_symbol2(&s->c, b->state[30], max_index, 0);
828
2/2
✓ Branch 0 taken 16916 times.
✓ Branch 1 taken 4684 times.
21600 if(run_index <= max_index)
829 16916 put_symbol2(&s->c, b->state[1], run, 3);
830
831
2/2
✓ Branch 0 taken 366600 times.
✓ Branch 1 taken 21600 times.
388200 for(y=0; y<h; y++){
832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 366600 times.
366600 if(s->c.bytestream_end - s->c.bytestream < w*40){
833 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
834 return AVERROR(ENOMEM);
835 }
836
2/2
✓ Branch 0 taken 26496000 times.
✓ Branch 1 taken 366600 times.
26862600 for(x=0; x<w; x++){
837 26496000 int v, p=0;
838 26496000 int /*ll=0, */l=0, lt=0, t=0, rt=0;
839 26496000 v= src[x + y*stride];
840
841
2/2
✓ Branch 0 taken 25960200 times.
✓ Branch 1 taken 535800 times.
26496000 if(y){
842 25960200 t= src[x + (y-1)*stride];
843
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x){
844 25615200 lt= src[x - 1 + (y-1)*stride];
845 }
846
2/2
✓ Branch 0 taken 25615200 times.
✓ Branch 1 taken 345000 times.
25960200 if(x + 1 < w){
847 25615200 rt= src[x + 1 + (y-1)*stride];
848 }
849 }
850
2/2
✓ Branch 0 taken 26129400 times.
✓ Branch 1 taken 366600 times.
26496000 if(x){
851 26129400 l= src[x - 1 + y*stride];
852 /*if(x > 1){
853 if(orientation==1) ll= src[y + (x-2)*stride];
854 else ll= src[x - 2 + y*stride];
855 }*/
856 }
857
2/2
✓ Branch 0 taken 26392500 times.
✓ Branch 1 taken 103500 times.
26496000 if(parent){
858 26392500 int px= x>>1;
859 26392500 int py= y>>1;
860
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)
861 26380800 p= parent[px + py*2*stride];
862 }
863
2/2
✓ Branch 0 taken 23675073 times.
✓ Branch 1 taken 2820927 times.
26496000 if(/*ll|*/l|lt|t|rt|p){
864 23675073 int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
865
866 23675073 put_rac(&s->c, &b->state[0][context], !!v);
867 }else{
868
2/2
✓ Branch 0 taken 291329 times.
✓ Branch 1 taken 2529598 times.
2820927 if(!run){
869 291329 run= runs[run_index++];
870
871
2/2
✓ Branch 0 taken 274413 times.
✓ Branch 1 taken 16916 times.
291329 if(run_index <= max_index)
872 274413 put_symbol2(&s->c, b->state[1], run, 3);
873 av_assert2(v);
874 }else{
875 2529598 run--;
876 av_assert2(!v);
877 }
878 }
879
2/2
✓ Branch 0 taken 15993468 times.
✓ Branch 1 taken 10502532 times.
26496000 if(v){
880 15993468 int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
881 15993468 int l2= 2*FFABS(l) + (l<0);
882 15993468 int t2= 2*FFABS(t) + (t<0);
883
884 15993468 put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
885 15993468 put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
886 }
887 }
888 }
889 }
890 21600 return 0;
891 }
892
893 21600 static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
894 // encode_subband_qtree(s, b, src, parent, stride, orientation);
895 // encode_subband_z0run(s, b, src, parent, stride, orientation);
896 21600 return encode_subband_c0run(s, b, src, parent, stride, orientation);
897 // encode_subband_dzr(s, b, src, parent, stride, orientation);
898 }
899
900 435268 static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
901 435268 const int b_stride= s->b_width << s->block_max_depth;
902 435268 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
903 435268 BlockNode backup= *block;
904 unsigned value;
905 int rd, index;
906
907 av_assert2(mb_x>=0 && mb_y>=0);
908 av_assert2(mb_x<b_stride);
909
910
2/2
✓ Branch 0 taken 18870 times.
✓ Branch 1 taken 416398 times.
435268 if(intra){
911 18870 block->color[0] = p[0];
912 18870 block->color[1] = p[1];
913 18870 block->color[2] = p[2];
914 18870 block->type |= BLOCK_INTRA;
915 }else{
916 416398 index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
917 416398 value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
918
2/2
✓ Branch 0 taken 118940 times.
✓ Branch 1 taken 297458 times.
416398 if(s->me_cache[index] == value)
919 118940 return 0;
920 297458 s->me_cache[index]= value;
921
922 297458 block->mx= p[0];
923 297458 block->my= p[1];
924 297458 block->type &= ~BLOCK_INTRA;
925 }
926
927 316328 rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged) + s->intra_penalty * !!intra;
928
929 //FIXME chroma
930
2/2
✓ Branch 0 taken 65411 times.
✓ Branch 1 taken 250917 times.
316328 if(rd < *best_rd){
931 65411 *best_rd= rd;
932 65411 return 1;
933 }else{
934 250917 *block= backup;
935 250917 return 0;
936 }
937 }
938
939 /* special case for int[2] args we discard afterwards,
940 * fixes compilation problem with gcc 2.95 */
941 416398 static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, uint8_t (*obmc_edged)[MB_SIZE * 2], int *best_rd){
942 416398 int p[2] = {p0, p1};
943 416398 return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
944 }
945
946 static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
947 const int b_stride= s->b_width << s->block_max_depth;
948 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
949 BlockNode backup[4];
950 unsigned value;
951 int rd, index;
952
953 /* We don't initialize backup[] during variable declaration, because
954 * that fails to compile on MSVC: "cannot convert from 'BlockNode' to
955 * 'int16_t'". */
956 backup[0] = block[0];
957 backup[1] = block[1];
958 backup[2] = block[b_stride];
959 backup[3] = block[b_stride + 1];
960
961 av_assert2(mb_x>=0 && mb_y>=0);
962 av_assert2(mb_x<b_stride);
963 av_assert2(((mb_x|mb_y)&1) == 0);
964
965 index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
966 value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
967 if(s->me_cache[index] == value)
968 return 0;
969 s->me_cache[index]= value;
970
971 block->mx= p0;
972 block->my= p1;
973 block->ref= ref;
974 block->type &= ~BLOCK_INTRA;
975 block[1]= block[b_stride]= block[b_stride+1]= *block;
976
977 rd= get_4block_rd(s, mb_x, mb_y, 0);
978
979 //FIXME chroma
980 if(rd < *best_rd){
981 *best_rd= rd;
982 return 1;
983 }else{
984 block[0]= backup[0];
985 block[1]= backup[1];
986 block[b_stride]= backup[2];
987 block[b_stride+1]= backup[3];
988 return 0;
989 }
990 }
991
992 270 static void iterative_me(SnowContext *s){
993 int pass, mb_x, mb_y;
994 270 const int b_width = s->b_width << s->block_max_depth;
995 270 const int b_height= s->b_height << s->block_max_depth;
996 270 const int b_stride= b_width;
997 int color[3];
998
999 {
1000 270 RangeCoder r = s->c;
1001 uint8_t state[sizeof(s->block_state)];
1002 270 memcpy(state, s->block_state, sizeof(s->block_state));
1003
2/2
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 270 times.
1350 for(mb_y= 0; mb_y<s->b_height; mb_y++)
1004
2/2
✓ Branch 0 taken 8640 times.
✓ Branch 1 taken 1080 times.
9720 for(mb_x= 0; mb_x<s->b_width; mb_x++)
1005 8640 encode_q_branch(s, 0, mb_x, mb_y);
1006 270 s->c = r;
1007 270 memcpy(s->block_state, state, sizeof(s->block_state));
1008 }
1009
1010
1/2
✓ Branch 0 taken 964 times.
✗ Branch 1 not taken.
964 for(pass=0; pass<25; pass++){
1011 964 int change= 0;
1012
1013
2/2
✓ Branch 0 taken 3856 times.
✓ Branch 1 taken 964 times.
4820 for(mb_y= 0; mb_y<b_height; mb_y++){
1014
2/2
✓ Branch 0 taken 30848 times.
✓ Branch 1 taken 3856 times.
34704 for(mb_x= 0; mb_x<b_width; mb_x++){
1015 int dia_change, i, j, ref;
1016 30848 int best_rd= INT_MAX, ref_rd;
1017 BlockNode backup, ref_b;
1018 30848 const int index= mb_x + mb_y * b_stride;
1019 30848 BlockNode *block= &s->block[index];
1020
2/2
✓ Branch 0 taken 23136 times.
✓ Branch 1 taken 7712 times.
30848 BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
1021
2/2
✓ Branch 0 taken 26992 times.
✓ Branch 1 taken 3856 times.
30848 BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
1022
2/2
✓ Branch 0 taken 26992 times.
✓ Branch 1 taken 3856 times.
30848 BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
1023
2/2
✓ Branch 0 taken 23136 times.
✓ Branch 1 taken 7712 times.
30848 BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
1024
4/4
✓ Branch 0 taken 26992 times.
✓ Branch 1 taken 3856 times.
✓ Branch 2 taken 20244 times.
✓ Branch 3 taken 6748 times.
30848 BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
1025
4/4
✓ Branch 0 taken 26992 times.
✓ Branch 1 taken 3856 times.
✓ Branch 2 taken 20244 times.
✓ Branch 3 taken 6748 times.
30848 BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
1026
4/4
✓ Branch 0 taken 26992 times.
✓ Branch 1 taken 3856 times.
✓ Branch 2 taken 20244 times.
✓ Branch 3 taken 6748 times.
30848 BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
1027
4/4
✓ Branch 0 taken 26992 times.
✓ Branch 1 taken 3856 times.
✓ Branch 2 taken 20244 times.
✓ Branch 3 taken 6748 times.
30848 BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
1028 30848 const int b_w= (MB_SIZE >> s->block_max_depth);
1029 uint8_t obmc_edged[MB_SIZE * 2][MB_SIZE * 2];
1030
1031
4/4
✓ Branch 0 taken 22208 times.
✓ Branch 1 taken 8640 times.
✓ Branch 2 taken 12063 times.
✓ Branch 3 taken 10145 times.
30848 if(pass && (block->type & BLOCK_OPT))
1032 12063 continue;
1033 18785 block->type |= BLOCK_OPT;
1034
1035 18785 backup= *block;
1036
1037
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18764 times.
18785 if(!s->me_cache_generation)
1038 21 memset(s->me_cache, 0, sizeof(s->me_cache));
1039 18785 s->me_cache_generation += 1<<22;
1040
1041 //FIXME precalculate
1042 {
1043 int x, y;
1044
2/2
✓ Branch 0 taken 601120 times.
✓ Branch 1 taken 18785 times.
619905 for (y = 0; y < b_w * 2; y++)
1045 601120 memcpy(obmc_edged[y], ff_obmc_tab[s->block_max_depth] + y * b_w * 2, b_w * 2);
1046
2/2
✓ Branch 0 taken 2241 times.
✓ Branch 1 taken 16544 times.
18785 if(mb_x==0)
1047
2/2
✓ Branch 0 taken 71712 times.
✓ Branch 1 taken 2241 times.
73953 for(y=0; y<b_w*2; y++)
1048 71712 memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
1049
2/2
✓ Branch 0 taken 1847 times.
✓ Branch 1 taken 16938 times.
18785 if(mb_x==b_stride-1)
1050
2/2
✓ Branch 0 taken 59104 times.
✓ Branch 1 taken 1847 times.
60951 for(y=0; y<b_w*2; y++)
1051 59104 memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
1052
2/2
✓ Branch 0 taken 4990 times.
✓ Branch 1 taken 13795 times.
18785 if(mb_y==0){
1053
2/2
✓ Branch 0 taken 159680 times.
✓ Branch 1 taken 4990 times.
164670 for(x=0; x<b_w*2; x++)
1054 159680 obmc_edged[0][x] += obmc_edged[b_w-1][x];
1055
2/2
✓ Branch 0 taken 74850 times.
✓ Branch 1 taken 4990 times.
79840 for(y=1; y<b_w; y++)
1056 74850 memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
1057 }
1058
2/2
✓ Branch 0 taken 3690 times.
✓ Branch 1 taken 15095 times.
18785 if(mb_y==b_height-1){
1059
2/2
✓ Branch 0 taken 118080 times.
✓ Branch 1 taken 3690 times.
121770 for(x=0; x<b_w*2; x++)
1060 118080 obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
1061
2/2
✓ Branch 0 taken 55350 times.
✓ Branch 1 taken 3690 times.
59040 for(y=b_w; y<b_w*2-1; y++)
1062 55350 memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
1063 }
1064 }
1065
1066 //skip stuff outside the picture
1067
8/8
✓ Branch 0 taken 16544 times.
✓ Branch 1 taken 2241 times.
✓ Branch 2 taken 12138 times.
✓ Branch 3 taken 4406 times.
✓ Branch 4 taken 10781 times.
✓ Branch 5 taken 1357 times.
✓ Branch 6 taken 2915 times.
✓ Branch 7 taken 7866 times.
18785 if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
1068 10919 uint8_t *src= s-> input_picture->data[0];
1069 10919 uint8_t *dst= s->current_picture->data[0];
1070 10919 const int stride= s->current_picture->linesize[0];
1071 10919 const int block_w= MB_SIZE >> s->block_max_depth;
1072 10919 const int block_h= MB_SIZE >> s->block_max_depth;
1073 10919 const int sx= block_w*mb_x - block_w/2;
1074 10919 const int sy= block_h*mb_y - block_h/2;
1075 10919 const int w= s->plane[0].width;
1076 10919 const int h= s->plane[0].height;
1077 int y;
1078
1079
2/2
✓ Branch 0 taken 39920 times.
✓ Branch 1 taken 10919 times.
50839 for(y=sy; y<0; y++)
1080 39920 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1081
2/2
✓ Branch 0 taken 29520 times.
✓ Branch 1 taken 10919 times.
40439 for(y=h; y<sy+block_h*2; y++)
1082 29520 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
1083
2/2
✓ Branch 0 taken 2241 times.
✓ Branch 1 taken 8678 times.
10919 if(sx<0){
1084
2/2
✓ Branch 0 taken 71712 times.
✓ Branch 1 taken 2241 times.
73953 for(y=sy; y<sy+block_h*2; y++)
1085 71712 memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
1086 }
1087
2/2
✓ Branch 0 taken 1847 times.
✓ Branch 1 taken 9072 times.
10919 if(sx+block_w*2 > w){
1088
2/2
✓ Branch 0 taken 59104 times.
✓ Branch 1 taken 1847 times.
60951 for(y=sy; y<sy+block_h*2; y++)
1089 59104 memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
1090 }
1091 }
1092
1093 // intra(black) = neighbors' contribution to the current block
1094
2/2
✓ Branch 0 taken 56355 times.
✓ Branch 1 taken 18785 times.
75140 for(i=0; i < s->nb_planes; i++)
1095 56355 color[i]= get_dc(s, mb_x, mb_y, i);
1096
1097 // get previous score (cannot be cached due to OBMC)
1098
4/4
✓ Branch 0 taken 10145 times.
✓ Branch 1 taken 8640 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 10060 times.
18870 if(pass > 0 && (block->type&BLOCK_INTRA)){
1099 85 int color0[3]= {block->color[0], block->color[1], block->color[2]};
1100 85 check_block(s, mb_x, mb_y, color0, 1, obmc_edged, &best_rd);
1101 }else
1102 18700 check_block_inter(s, mb_x, mb_y, block->mx, block->my, obmc_edged, &best_rd);
1103
1104 18785 ref_b= *block;
1105 18785 ref_rd= best_rd;
1106
2/2
✓ Branch 0 taken 18785 times.
✓ Branch 1 taken 18785 times.
37570 for(ref=0; ref < s->ref_frames; ref++){
1107 18785 int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
1108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18785 times.
18785 if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
1109 continue;
1110 18785 block->ref= ref;
1111 18785 best_rd= INT_MAX;
1112
1113 18785 check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], obmc_edged, &best_rd);
1114 18785 check_block_inter(s, mb_x, mb_y, 0, 0, obmc_edged, &best_rd);
1115
2/2
✓ Branch 0 taken 13795 times.
✓ Branch 1 taken 4990 times.
18785 if(tb)
1116 13795 check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], obmc_edged, &best_rd);
1117
2/2
✓ Branch 0 taken 16544 times.
✓ Branch 1 taken 2241 times.
18785 if(lb)
1118 16544 check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], obmc_edged, &best_rd);
1119
2/2
✓ Branch 0 taken 16938 times.
✓ Branch 1 taken 1847 times.
18785 if(rb)
1120 16938 check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], obmc_edged, &best_rd);
1121
2/2
✓ Branch 0 taken 15095 times.
✓ Branch 1 taken 3690 times.
18785 if(bb)
1122 15095 check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], obmc_edged, &best_rd);
1123
1124 /* fullpel ME */
1125 //FIXME avoid subpel interpolation / round to nearest integer
1126 do{
1127 19209 int newx = block->mx;
1128 19209 int newy = block->my;
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19209 times.
19209 int dia_size = s->iterative_dia_size ? s->iterative_dia_size : FFMAX(s->avctx->dia_size, 1);
1130 19209 dia_change=0;
1131
2/2
✓ Branch 0 taken 38418 times.
✓ Branch 1 taken 19209 times.
57627 for(i=0; i < dia_size; i++){
1132
2/2
✓ Branch 0 taken 19209 times.
✓ Branch 1 taken 38418 times.
57627 for(j=0; j<i; j++){
1133 19209 dia_change |= check_block_inter(s, mb_x, mb_y, newx+4*(i-j), newy+(4*j), obmc_edged, &best_rd);
1134 19209 dia_change |= check_block_inter(s, mb_x, mb_y, newx-4*(i-j), newy-(4*j), obmc_edged, &best_rd);
1135 19209 dia_change |= check_block_inter(s, mb_x, mb_y, newx-(4*j), newy+4*(i-j), obmc_edged, &best_rd);
1136 19209 dia_change |= check_block_inter(s, mb_x, mb_y, newx+(4*j), newy-4*(i-j), obmc_edged, &best_rd);
1137 }
1138 }
1139
2/2
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 18785 times.
19209 }while(dia_change);
1140 /* subpel ME */
1141 do{
1142 static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
1143 27615 dia_change=0;
1144
2/2
✓ Branch 0 taken 220920 times.
✓ Branch 1 taken 27615 times.
248535 for(i=0; i<8; i++)
1145 220920 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], obmc_edged, &best_rd);
1146
2/2
✓ Branch 0 taken 8830 times.
✓ Branch 1 taken 18785 times.
27615 }while(dia_change);
1147 //FIXME or try the standard 2 pass qpel or similar
1148
1149 18785 mvr[0][0]= block->mx;
1150 18785 mvr[0][1]= block->my;
1151
2/2
✓ Branch 0 taken 4553 times.
✓ Branch 1 taken 14232 times.
18785 if(ref_rd > best_rd){
1152 4553 ref_rd= best_rd;
1153 4553 ref_b= *block;
1154 }
1155 }
1156 18785 best_rd= ref_rd;
1157 18785 *block= ref_b;
1158 18785 check_block(s, mb_x, mb_y, color, 1, obmc_edged, &best_rd);
1159 //FIXME RD style color selection
1160
2/2
✓ Branch 1 taken 4573 times.
✓ Branch 2 taken 14212 times.
18785 if(!same_block(block, &backup)){
1161
2/2
✓ Branch 0 taken 3343 times.
✓ Branch 1 taken 1230 times.
4573 if(tb ) tb ->type &= ~BLOCK_OPT;
1162
2/2
✓ Branch 0 taken 4048 times.
✓ Branch 1 taken 525 times.
4573 if(lb ) lb ->type &= ~BLOCK_OPT;
1163
2/2
✓ Branch 0 taken 4041 times.
✓ Branch 1 taken 532 times.
4573 if(rb ) rb ->type &= ~BLOCK_OPT;
1164
2/2
✓ Branch 0 taken 3568 times.
✓ Branch 1 taken 1005 times.
4573 if(bb ) bb ->type &= ~BLOCK_OPT;
1165
2/2
✓ Branch 0 taken 2936 times.
✓ Branch 1 taken 1637 times.
4573 if(tlb) tlb->type &= ~BLOCK_OPT;
1166
2/2
✓ Branch 0 taken 2964 times.
✓ Branch 1 taken 1609 times.
4573 if(trb) trb->type &= ~BLOCK_OPT;
1167
2/2
✓ Branch 0 taken 3164 times.
✓ Branch 1 taken 1409 times.
4573 if(blb) blb->type &= ~BLOCK_OPT;
1168
2/2
✓ Branch 0 taken 3156 times.
✓ Branch 1 taken 1417 times.
4573 if(brb) brb->type &= ~BLOCK_OPT;
1169 4573 change ++;
1170 }
1171 }
1172 }
1173 964 av_log(s->avctx, AV_LOG_DEBUG, "pass:%d changed:%d\n", pass, change);
1174
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 694 times.
964 if(!change)
1175 270 break;
1176 }
1177
1178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if(s->block_max_depth == 1){
1179 int change= 0;
1180 for(mb_y= 0; mb_y<b_height; mb_y+=2){
1181 for(mb_x= 0; mb_x<b_width; mb_x+=2){
1182 int i;
1183 int best_rd, init_rd;
1184 const int index= mb_x + mb_y * b_stride;
1185 BlockNode *b[4];
1186
1187 b[0]= &s->block[index];
1188 b[1]= b[0]+1;
1189 b[2]= b[0]+b_stride;
1190 b[3]= b[2]+1;
1191 if(same_block(b[0], b[1]) &&
1192 same_block(b[0], b[2]) &&
1193 same_block(b[0], b[3]))
1194 continue;
1195
1196 if(!s->me_cache_generation)
1197 memset(s->me_cache, 0, sizeof(s->me_cache));
1198 s->me_cache_generation += 1<<22;
1199
1200 init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
1201
1202 //FIXME more multiref search?
1203 check_4block_inter(s, mb_x, mb_y,
1204 (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
1205 (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
1206
1207 for(i=0; i<4; i++)
1208 if(!(b[i]->type&BLOCK_INTRA))
1209 check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
1210
1211 if(init_rd != best_rd)
1212 change++;
1213 }
1214 }
1215 av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
1216 }
1217 270 }
1218
1219 450 static void encode_blocks(SnowContext *s, int search){
1220 int x, y;
1221 450 int w= s->b_width;
1222 450 int h= s->b_height;
1223
1224
5/6
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 270 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 270 times.
✗ Branch 5 not taken.
450 if(s->motion_est == FF_ME_ITER && !s->keyframe && search)
1225 270 iterative_me(s);
1226
1227
2/2
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 450 times.
4350 for(y=0; y<h; y++){
1228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3900 times.
3900 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
1229 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1230 return;
1231 }
1232
2/2
✓ Branch 0 taken 69000 times.
✓ Branch 1 taken 3900 times.
72900 for(x=0; x<w; x++){
1233
3/4
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 9600 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 59400 times.
69000 if(s->motion_est == FF_ME_ITER || !search)
1234 9600 encode_q_branch2(s, 0, x, y);
1235 else
1236 59400 encode_q_branch (s, 0, x, y);
1237 }
1238 }
1239 }
1240
1241 21600 static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
1242 21600 const int w= b->width;
1243 21600 const int h= b->height;
1244 21600 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1245 21600 const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
1246 int x,y, thres1, thres2;
1247
1248
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 14400 times.
21600 if(s->qlog == LOSSLESS_QLOG){
1249
2/2
✓ Branch 0 taken 253800 times.
✓ Branch 1 taken 7200 times.
261000 for(y=0; y<h; y++)
1250
2/2
✓ Branch 0 taken 22809600 times.
✓ Branch 1 taken 253800 times.
23063400 for(x=0; x<w; x++)
1251 22809600 dst[x + y*stride]= src[x + y*stride];
1252 7200 return;
1253 }
1254
1255
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 12960 times.
14400 bias= bias ? 0 : (3*qmul)>>3;
1256 14400 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
1257 14400 thres2= 2*thres1;
1258
1259
2/2
✓ Branch 0 taken 12960 times.
✓ Branch 1 taken 1440 times.
14400 if(!bias){
1260
2/2
✓ Branch 0 taken 101520 times.
✓ Branch 1 taken 12960 times.
114480 for(y=0; y<h; y++){
1261
2/2
✓ Branch 0 taken 3317760 times.
✓ Branch 1 taken 101520 times.
3419280 for(x=0; x<w; x++){
1262 3317760 int i= src[x + y*stride];
1263
1264
2/2
✓ Branch 0 taken 550125 times.
✓ Branch 1 taken 2767635 times.
3317760 if((unsigned)(i+thres1) > thres2){
1265
2/2
✓ Branch 0 taken 275359 times.
✓ Branch 1 taken 274766 times.
550125 if(i>=0){
1266 275359 i<<= QEXPSHIFT;
1267 275359 i/= qmul; //FIXME optimize
1268 275359 dst[x + y*stride]= i;
1269 }else{
1270 274766 i= -i;
1271 274766 i<<= QEXPSHIFT;
1272 274766 i/= qmul; //FIXME optimize
1273 274766 dst[x + y*stride]= -i;
1274 }
1275 }else
1276 2767635 dst[x + y*stride]= 0;
1277 }
1278 }
1279 }else{
1280
2/2
✓ Branch 0 taken 11280 times.
✓ Branch 1 taken 1440 times.
12720 for(y=0; y<h; y++){
1281
2/2
✓ Branch 0 taken 368640 times.
✓ Branch 1 taken 11280 times.
379920 for(x=0; x<w; x++){
1282 368640 int i= src[x + y*stride];
1283
1284
2/2
✓ Branch 0 taken 202458 times.
✓ Branch 1 taken 166182 times.
368640 if((unsigned)(i+thres1) > thres2){
1285
2/2
✓ Branch 0 taken 101688 times.
✓ Branch 1 taken 100770 times.
202458 if(i>=0){
1286 101688 i<<= QEXPSHIFT;
1287 101688 i= (i + bias) / qmul; //FIXME optimize
1288 101688 dst[x + y*stride]= i;
1289 }else{
1290 100770 i= -i;
1291 100770 i<<= QEXPSHIFT;
1292 100770 i= (i + bias) / qmul; //FIXME optimize
1293 100770 dst[x + y*stride]= -i;
1294 }
1295 }else
1296 166182 dst[x + y*stride]= 0;
1297 }
1298 }
1299 }
1300 }
1301
1302 21600 static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
1303 21600 const int w= b->width;
1304 21600 const int h= b->height;
1305 21600 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1306 21600 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1307 21600 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1308 int x,y;
1309
1310
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 14400 times.
21600 if(s->qlog == LOSSLESS_QLOG) return;
1311
1312
2/2
✓ Branch 0 taken 112800 times.
✓ Branch 1 taken 14400 times.
127200 for(y=0; y<h; y++){
1313
2/2
✓ Branch 0 taken 3686400 times.
✓ Branch 1 taken 112800 times.
3799200 for(x=0; x<w; x++){
1314 3686400 int i= src[x + y*stride];
1315
2/2
✓ Branch 0 taken 375486 times.
✓ Branch 1 taken 3310914 times.
3686400 if(i<0){
1316 375486 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
1317
2/2
✓ Branch 0 taken 377019 times.
✓ Branch 1 taken 2933895 times.
3310914 }else if(i>0){
1318 377019 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
1319 }
1320 }
1321 }
1322 }
1323
1324 1350 static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1325 1350 const int w= b->width;
1326 1350 const int h= b->height;
1327 int x,y;
1328
1329
2/2
✓ Branch 0 taken 4050 times.
✓ Branch 1 taken 1350 times.
5400 for(y=h-1; y>=0; y--){
1330
2/2
✓ Branch 0 taken 27450 times.
✓ Branch 1 taken 4050 times.
31500 for(x=w-1; x>=0; x--){
1331 27450 int i= x + y*stride;
1332
1333
2/2
✓ Branch 0 taken 23400 times.
✓ Branch 1 taken 4050 times.
27450 if(x){
1334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23400 times.
23400 if(use_median){
1335 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1336 else src[i] -= src[i - 1];
1337 }else{
1338
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]);
1339 4500 else src[i] -= src[i - 1];
1340 }
1341 }else{
1342
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 1350 times.
4050 if(y) src[i] -= src[i - stride];
1343 }
1344 }
1345 }
1346 1350 }
1347
1348 1350 static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
1349 1350 const int w= b->width;
1350 1350 const int h= b->height;
1351 int x,y;
1352
1353
2/2
✓ Branch 0 taken 4050 times.
✓ Branch 1 taken 1350 times.
5400 for(y=0; y<h; y++){
1354
2/2
✓ Branch 0 taken 27450 times.
✓ Branch 1 taken 4050 times.
31500 for(x=0; x<w; x++){
1355 27450 int i= x + y*stride;
1356
1357
2/2
✓ Branch 0 taken 23400 times.
✓ Branch 1 taken 4050 times.
27450 if(x){
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23400 times.
23400 if(use_median){
1359 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
1360 else src[i] += src[i - 1];
1361 }else{
1362
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]);
1363 4500 else src[i] += src[i - 1];
1364 }
1365 }else{
1366
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 1350 times.
4050 if(y) src[i] += src[i - stride];
1367 }
1368 }
1369 }
1370 1350 }
1371
1372 45 static void encode_qlogs(SnowContext *s){
1373 int plane_index, level, orientation;
1374
1375
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 45 times.
135 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1376
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 90 times.
540 for(level=0; level<s->spatial_decomposition_count; level++){
1377
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 450 times.
1890 for(orientation=level ? 1:0; orientation<4; orientation++){
1378
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 990 times.
1440 if(orientation==2) continue;
1379 990 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
1380 }
1381 }
1382 }
1383 45 }
1384
1385 450 static void encode_header(SnowContext *s){
1386 int plane_index, i;
1387 uint8_t kstate[32];
1388
1389 450 memset(kstate, MID_STATE, sizeof(kstate));
1390
1391 450 put_rac(&s->c, kstate, s->keyframe);
1392
3/4
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 405 times.
450 if(s->keyframe || s->always_reset){
1393 45 ff_snow_reset_contexts(s);
1394 45 s->last_spatial_decomposition_type=
1395 45 s->last_qlog=
1396 45 s->last_qbias=
1397 45 s->last_mv_scale=
1398 45 s->last_block_max_depth= 0;
1399
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 45 times.
135 for(plane_index=0; plane_index<2; plane_index++){
1400 90 Plane *p= &s->plane[plane_index];
1401 90 p->last_htaps=0;
1402 90 p->last_diag_mc=0;
1403 90 memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
1404 }
1405 }
1406
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 405 times.
450 if(s->keyframe){
1407 45 put_symbol(&s->c, s->header_state, s->version, 0);
1408 45 put_rac(&s->c, s->header_state, s->always_reset);
1409 45 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
1410 45 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
1411 45 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1412 45 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
1413
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (s->nb_planes > 2) {
1414 45 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
1415 45 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
1416 }
1417 45 put_rac(&s->c, s->header_state, s->spatial_scalability);
1418 // put_rac(&s->c, s->header_state, s->rate_scalability);
1419 45 put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
1420
1421 45 encode_qlogs(s);
1422 }
1423
1424
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 45 times.
450 if(!s->keyframe){
1425 405 int update_mc=0;
1426
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 405 times.
1215 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1427 810 Plane *p= &s->plane[plane_index];
1428 810 update_mc |= p->last_htaps != p->htaps;
1429 810 update_mc |= p->last_diag_mc != p->diag_mc;
1430 810 update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1431 }
1432 405 put_rac(&s->c, s->header_state, update_mc);
1433
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 360 times.
405 if(update_mc){
1434
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 45 times.
135 for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
1435 90 Plane *p= &s->plane[plane_index];
1436 90 put_rac(&s->c, s->header_state, p->diag_mc);
1437 90 put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
1438
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 90 times.
360 for(i= p->htaps/2; i; i--)
1439 270 put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
1440 }
1441 }
1442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1443 put_rac(&s->c, s->header_state, 1);
1444 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
1445 encode_qlogs(s);
1446 }else
1447 405 put_rac(&s->c, s->header_state, 0);
1448 }
1449
1450 450 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
1451 450 put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
1452 450 put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
1453 450 put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
1454 450 put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
1455
1456 450 }
1457
1458 450 static void update_last_header_values(SnowContext *s){
1459 int plane_index;
1460
1461
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 45 times.
450 if(!s->keyframe){
1462
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 405 times.
1215 for(plane_index=0; plane_index<2; plane_index++){
1463 810 Plane *p= &s->plane[plane_index];
1464 810 p->last_diag_mc= p->diag_mc;
1465 810 p->last_htaps = p->htaps;
1466 810 memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
1467 }
1468 }
1469
1470 450 s->last_spatial_decomposition_type = s->spatial_decomposition_type;
1471 450 s->last_qlog = s->qlog;
1472 450 s->last_qbias = s->qbias;
1473 450 s->last_mv_scale = s->mv_scale;
1474 450 s->last_block_max_depth = s->block_max_depth;
1475 450 s->last_spatial_decomposition_count = s->spatial_decomposition_count;
1476 450 }
1477
1478 300 static int qscale2qlog(int qscale){
1479 300 return lrint(QROOT*log2(qscale / (float)FF_QP2LAMBDA))
1480 300 + 61*QROOT/8; ///< 64 > 60
1481 }
1482
1483 static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
1484 {
1485 /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
1486 * FIXME we know exact mv bits at this point,
1487 * but ratecontrol isn't set up to include them. */
1488 uint32_t coef_sum= 0;
1489 int level, orientation, delta_qlog;
1490
1491 for(level=0; level<s->spatial_decomposition_count; level++){
1492 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1493 SubBand *b= &s->plane[0].band[level][orientation];
1494 IDWTELEM *buf= b->ibuf;
1495 const int w= b->width;
1496 const int h= b->height;
1497 const int stride= b->stride;
1498 const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
1499 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1500 const int qdiv= (1<<16)/qmul;
1501 int x, y;
1502 //FIXME this is ugly
1503 for(y=0; y<h; y++)
1504 for(x=0; x<w; x++)
1505 buf[x+y*stride]= b->buf[x+y*stride];
1506 if(orientation==0)
1507 decorrelate(s, b, buf, stride, 1, 0);
1508 for(y=0; y<h; y++)
1509 for(x=0; x<w; x++)
1510 coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
1511 }
1512 }
1513
1514 /* ugly, ratecontrol just takes a sqrt again */
1515 av_assert0(coef_sum < INT_MAX);
1516 coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
1517
1518 if(pict->pict_type == AV_PICTURE_TYPE_I){
1519 s->m.current_picture.mb_var_sum= coef_sum;
1520 s->m.current_picture.mc_mb_var_sum= 0;
1521 }else{
1522 s->m.current_picture.mc_mb_var_sum= coef_sum;
1523 s->m.current_picture.mb_var_sum= 0;
1524 }
1525
1526 pict->quality= ff_rate_estimate_qscale(&s->m, 1);
1527 if (pict->quality < 0)
1528 return INT_MIN;
1529 s->lambda= pict->quality * 3/2;
1530 delta_qlog= qscale2qlog(pict->quality) - s->qlog;
1531 s->qlog+= delta_qlog;
1532 return delta_qlog;
1533 }
1534
1535 27 static void calculate_visual_weight(SnowContext *s, Plane *p){
1536 27 int width = p->width;
1537 27 int height= p->height;
1538 int level, orientation, x, y;
1539
1540
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 27 times.
162 for(level=0; level<s->spatial_decomposition_count; level++){
1541
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 135 times.
567 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1542 432 SubBand *b= &p->band[level][orientation];
1543 432 IDWTELEM *ibuf= b->ibuf;
1544 432 int64_t error=0;
1545
1546 432 memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
1547 432 ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
1548 432 ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
1549
2/2
✓ Branch 0 taken 39936 times.
✓ Branch 1 taken 432 times.
40368 for(y=0; y<height; y++){
1550
2/2
✓ Branch 0 taken 8478720 times.
✓ Branch 1 taken 39936 times.
8518656 for(x=0; x<width; x++){
1551 8478720 int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
1552 8478720 error += d*d;
1553 }
1554 }
1555
1556 432 b->qlog= (int)(QROOT * log2(352256.0/sqrt(error)) + 0.5);
1557 }
1558 }
1559 27 }
1560
1561 450 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1562 const AVFrame *pict, int *got_packet)
1563 {
1564 450 SnowContext *s = avctx->priv_data;
1565 450 RangeCoder * const c= &s->c;
1566 AVFrame *pic;
1567 450 const int width= s->avctx->width;
1568 450 const int height= s->avctx->height;
1569 int level, orientation, plane_index, i, y, ret;
1570 uint8_t rc_header_bak[sizeof(s->header_state)];
1571 uint8_t rc_block_bak[sizeof(s->block_state)];
1572
1573
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 450 times.
450 if ((ret = ff_alloc_packet(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
1574 return ret;
1575
1576 450 ff_init_range_encoder(c, pkt->data, pkt->size);
1577 450 ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1578
1579
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 450 times.
1800 for(i=0; i < s->nb_planes; i++){
1580
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 450 times.
1350 int hshift= i ? s->chroma_h_shift : 0;
1581
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 450 times.
1350 int vshift= i ? s->chroma_v_shift : 0;
1582
2/2
✓ Branch 0 taken 124800 times.
✓ Branch 1 taken 1350 times.
126150 for(y=0; y<AV_CEIL_RSHIFT(height, vshift); y++)
1583 124800 memcpy(&s->input_picture->data[i][y * s->input_picture->linesize[i]],
1584 124800 &pict->data[i][y * pict->linesize[i]],
1585 124800 AV_CEIL_RSHIFT(width, hshift));
1586 1350 s->mpvencdsp.draw_edges(s->input_picture->data[i], s->input_picture->linesize[i],
1587 1350 AV_CEIL_RSHIFT(width, hshift), AV_CEIL_RSHIFT(height, vshift),
1588 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1589 EDGE_TOP | EDGE_BOTTOM);
1590
1591 }
1592 450 emms_c();
1593 450 pic = s->input_picture;
1594 450 pic->pict_type = pict->pict_type;
1595 450 pic->quality = pict->quality;
1596
1597 450 s->m.picture_number= avctx->frame_number;
1598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if(avctx->flags&AV_CODEC_FLAG_PASS2){
1599 s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
1600 s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
1601 if(!(avctx->flags&AV_CODEC_FLAG_QSCALE)) {
1602 pic->quality = ff_rate_estimate_qscale(&s->m, 0);
1603 if (pic->quality < 0)
1604 return -1;
1605 }
1606 }else{
1607
3/4
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 405 times.
450 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
1608
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 405 times.
450 s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1609 }
1610
1611
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
450 if(s->pass1_rc && avctx->frame_number == 0)
1612 pic->quality = 2*FF_QP2LAMBDA;
1613
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
450 if (pic->quality) {
1614 300 s->qlog = qscale2qlog(pic->quality);
1615 300 s->lambda = pic->quality * 3/2;
1616 }
1617
5/6
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 300 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
450 if (s->qlog < 0 || (!pic->quality && (avctx->flags & AV_CODEC_FLAG_QSCALE))) {
1618 150 s->qlog= LOSSLESS_QLOG;
1619 150 s->lambda = 0;
1620 }//else keep previous frame's qlog until after motion estimation
1621
1622
2/2
✓ Branch 0 taken 441 times.
✓ Branch 1 taken 9 times.
450 if (s->current_picture->data[0]) {
1623 441 int w = s->avctx->width;
1624 441 int h = s->avctx->height;
1625
1626 441 s->mpvencdsp.draw_edges(s->current_picture->data[0],
1627 441 s->current_picture->linesize[0], w , h ,
1628 EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
1629
1/2
✓ Branch 0 taken 441 times.
✗ Branch 1 not taken.
441 if (s->current_picture->data[2]) {
1630 441 s->mpvencdsp.draw_edges(s->current_picture->data[1],
1631 441 s->current_picture->linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
1632 441 EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
1633 441 s->mpvencdsp.draw_edges(s->current_picture->data[2],
1634 441 s->current_picture->linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
1635 441 EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
1636 }
1637 441 emms_c();
1638 }
1639
1640 450 ff_snow_frame_start(s);
1641
1642 450 s->m.current_picture_ptr= &s->m.current_picture;
1643 450 s->m.current_picture.f = s->current_picture;
1644 450 s->m.current_picture.f->pts = pict->pts;
1645
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 45 times.
450 if(pic->pict_type == AV_PICTURE_TYPE_P){
1646 405 int block_width = (width +15)>>4;
1647 405 int block_height= (height+15)>>4;
1648 405 int stride= s->current_picture->linesize[0];
1649
1650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 av_assert0(s->current_picture->data[0]);
1651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 av_assert0(s->last_picture[0]->data[0]);
1652
1653 405 s->m.avctx= s->avctx;
1654 405 s->m. last_picture.f = s->last_picture[0];
1655 405 s->m. new_picture.f = s->input_picture;
1656 405 s->m. last_picture_ptr= &s->m. last_picture;
1657 405 s->m.linesize = stride;
1658 405 s->m.uvlinesize= s->current_picture->linesize[1];
1659 405 s->m.width = width;
1660 405 s->m.height= height;
1661 405 s->m.mb_width = block_width;
1662 405 s->m.mb_height= block_height;
1663 405 s->m.mb_stride= s->m.mb_width+1;
1664 405 s->m.b8_stride= 2*s->m.mb_width+1;
1665 405 s->m.f_code=1;
1666 405 s->m.pict_type = pic->pict_type;
1667 405 s->m.motion_est= s->motion_est;
1668 405 s->m.me.scene_change_score=0;
1669 405 s->m.me.dia_size = avctx->dia_size;
1670 405 s->m.quarter_sample= (s->avctx->flags & AV_CODEC_FLAG_QPEL)!=0;
1671 405 s->m.out_format= FMT_H263;
1672 405 s->m.unrestricted_mv= 1;
1673
1674 405 s->m.lambda = s->lambda;
1675 405 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
1676 405 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
1677
1678 405 s->m.mecc= s->mecc; //move
1679 405 s->m.qdsp= s->qdsp; //move
1680 405 s->m.hdsp = s->hdsp;
1681 405 ff_init_me(&s->m);
1682 405 s->hdsp = s->m.hdsp;
1683 405 s->mecc= s->m.mecc;
1684 }
1685
1686
1/2
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
450 if(s->pass1_rc){
1687 memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
1688 memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
1689 }
1690
1691 450 redo_frame:
1692
1693 450 s->spatial_decomposition_count= 5;
1694
1695 450 while( !(width >>(s->chroma_h_shift + s->spatial_decomposition_count))
1696
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 450 times.
450 || !(height>>(s->chroma_v_shift + s->spatial_decomposition_count)))
1697 s->spatial_decomposition_count--;
1698
1699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if (s->spatial_decomposition_count <= 0) {
1700 av_log(avctx, AV_LOG_ERROR, "Resolution too low\n");
1701 return AVERROR(EINVAL);
1702 }
1703
1704 450 s->m.pict_type = pic->pict_type;
1705
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 45 times.
450 s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
1706
1707 450 ff_snow_common_init_after_header(avctx);
1708
1709
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 441 times.
450 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
1710
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 9 times.
36 for(plane_index=0; plane_index < s->nb_planes; plane_index++){
1711 27 calculate_visual_weight(s, &s->plane[plane_index]);
1712 }
1713 }
1714
1715 450 encode_header(s);
1716 450 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
1717 450 encode_blocks(s, 1);
1718 450 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
1719
1720
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 450 times.
1800 for(plane_index=0; plane_index < s->nb_planes; plane_index++){
1721 1350 Plane *p= &s->plane[plane_index];
1722 1350 int w= p->width;
1723 1350 int h= p->height;
1724 int x, y;
1725 // int bits= put_bits_count(&s->c.pb);
1726
1727
1/2
✓ Branch 0 taken 1350 times.
✗ Branch 1 not taken.
1350 if (!s->memc_only) {
1728 //FIXME optimize
1729
1/2
✓ Branch 0 taken 1350 times.
✗ Branch 1 not taken.
1350 if(pict->data[plane_index]) //FIXME gray hack
1730
2/2
✓ Branch 0 taken 124800 times.
✓ Branch 1 taken 1350 times.
126150 for(y=0; y<h; y++){
1731
2/2
✓ Branch 0 taken 26496000 times.
✓ Branch 1 taken 124800 times.
26620800 for(x=0; x<w; x++){
1732 26496000 s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
1733 }
1734 }
1735 1350 predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
1736
1737
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 900 times.
1350 if( plane_index==0
1738
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 45 times.
450 && pic->pict_type == AV_PICTURE_TYPE_P
1739
1/2
✓ Branch 0 taken 405 times.
✗ Branch 1 not taken.
405 && !(avctx->flags&AV_CODEC_FLAG_PASS2)
1740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 && s->m.me.scene_change_score > s->scenechange_threshold){
1741 ff_init_range_encoder(c, pkt->data, pkt->size);
1742 ff_build_rac_states(c, (1LL<<32)/20, 256-8);
1743 pic->pict_type= AV_PICTURE_TYPE_I;
1744 s->keyframe=1;
1745 s->current_picture->key_frame=1;
1746 goto redo_frame;
1747 }
1748
1749
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 900 times.
1350 if(s->qlog == LOSSLESS_QLOG){
1750
2/2
✓ Branch 0 taken 86400 times.
✓ Branch 1 taken 450 times.
86850 for(y=0; y<h; y++){
1751
2/2
✓ Branch 0 taken 22809600 times.
✓ Branch 1 taken 86400 times.
22896000 for(x=0; x<w; x++){
1752 22809600 s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
1753 }
1754 }
1755 }else{
1756
2/2
✓ Branch 0 taken 38400 times.
✓ Branch 1 taken 900 times.
39300 for(y=0; y<h; y++){
1757
2/2
✓ Branch 0 taken 3686400 times.
✓ Branch 1 taken 38400 times.
3724800 for(x=0; x<w; x++){
1758 3686400 s->spatial_dwt_buffer[y*w + x]= s->spatial_idwt_buffer[y*w + x] * (1 << ENCODER_EXTRA_BITS);
1759 }
1760 }
1761 }
1762
1763 1350 ff_spatial_dwt(s->spatial_dwt_buffer, s->temp_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1764
1765
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1350 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1350 if(s->pass1_rc && plane_index==0){
1766 int delta_qlog = ratecontrol_1pass(s, pic);
1767 if (delta_qlog <= INT_MIN)
1768 return -1;
1769 if(delta_qlog){
1770 //reordering qlog in the bitstream would eliminate this reset
1771 ff_init_range_encoder(c, pkt->data, pkt->size);
1772 memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
1773 memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
1774 encode_header(s);
1775 encode_blocks(s, 0);
1776 }
1777 }
1778
1779
2/2
✓ Branch 0 taken 6750 times.
✓ Branch 1 taken 1350 times.
8100 for(level=0; level<s->spatial_decomposition_count; level++){
1780
2/2
✓ Branch 0 taken 21600 times.
✓ Branch 1 taken 6750 times.
28350 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1781 21600 SubBand *b= &p->band[level][orientation];
1782
1783 21600 quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
1784
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 20250 times.
21600 if(orientation==0)
1785 1350 decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
1786
1/2
✓ Branch 0 taken 21600 times.
✗ Branch 1 not taken.
21600 if (!s->no_bitstream)
1787
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);
1788
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);
1789
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 20250 times.
21600 if(orientation==0)
1790 1350 correlate(s, b, b->ibuf, b->stride, 1, 0);
1791 }
1792 }
1793
1794
2/2
✓ Branch 0 taken 6750 times.
✓ Branch 1 taken 1350 times.
8100 for(level=0; level<s->spatial_decomposition_count; level++){
1795
2/2
✓ Branch 0 taken 21600 times.
✓ Branch 1 taken 6750 times.
28350 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1796 21600 SubBand *b= &p->band[level][orientation];
1797
1798 21600 dequantize(s, b, b->ibuf, b->stride);
1799 }
1800 }
1801
1802 1350 ff_spatial_idwt(s->spatial_idwt_buffer, s->temp_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1803
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 900 times.
1350 if(s->qlog == LOSSLESS_QLOG){
1804
2/2
✓ Branch 0 taken 86400 times.
✓ Branch 1 taken 450 times.
86850 for(y=0; y<h; y++){
1805
2/2
✓ Branch 0 taken 22809600 times.
✓ Branch 1 taken 86400 times.
22896000 for(x=0; x<w; x++){
1806 22809600 s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
1807 }
1808 }
1809 }
1810 1350 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1811 }else{
1812 //ME/MC only
1813 if(pic->pict_type == AV_PICTURE_TYPE_I){
1814 for(y=0; y<h; y++){
1815 for(x=0; x<w; x++){
1816 s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x]=
1817 pict->data[plane_index][y*pict->linesize[plane_index] + x];
1818 }
1819 }
1820 }else{
1821 memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
1822 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1823 }
1824 }
1825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1350 times.
1350 if(s->avctx->flags&AV_CODEC_FLAG_PSNR){
1826 int64_t error= 0;
1827
1828 if(pict->data[plane_index]) //FIXME gray hack
1829 for(y=0; y<h; y++){
1830 for(x=0; x<w; x++){
1831 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];
1832 error += d*d;
1833 }
1834 }
1835 s->avctx->error[plane_index] += error;
1836 s->encoding_error[plane_index] = error;
1837 }
1838
1839 }
1840 450 emms_c();
1841
1842 450 update_last_header_values(s);
1843
1844 450 ff_snow_release_buffer(avctx);
1845
1846 450 s->current_picture->coded_picture_number = avctx->frame_number;
1847 450 s->current_picture->pict_type = pic->pict_type;
1848 450 s->current_picture->quality = pic->quality;
1849 450 s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
1850 450 s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
1851 450 s->m.current_picture.f->display_picture_number =
1852 450 s->m.current_picture.f->coded_picture_number = avctx->frame_number;
1853 450 s->m.current_picture.f->quality = pic->quality;
1854 450 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
1855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if(s->pass1_rc)
1856 if (ff_rate_estimate_qscale(&s->m, 0) < 0)
1857 return -1;
1858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if(avctx->flags&AV_CODEC_FLAG_PASS1)
1859 ff_write_pass1_stats(&s->m);
1860 450 s->m.last_pict_type = s->m.pict_type;
1861
1862 450 emms_c();
1863
1864 450 ff_side_data_set_encoder_stats(pkt, s->current_picture->quality,
1865 450 s->encoding_error,
1866 450 (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
1867 450 s->current_picture->pict_type);
1868
1869 450 pkt->size = ff_rac_terminate(c, 0);
1870
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 405 times.
450 if (s->current_picture->key_frame)
1871 45 pkt->flags |= AV_PKT_FLAG_KEY;
1872 450 *got_packet = 1;
1873
1874 450 return 0;
1875 }
1876
1877 9 static av_cold int encode_end(AVCodecContext *avctx)
1878 {
1879 9 SnowContext *s = avctx->priv_data;
1880
1881 9 ff_snow_common_end(s);
1882 9 ff_rate_control_uninit(&s->m);
1883 9 av_frame_free(&s->input_picture);
1884 9 av_freep(&avctx->stats_out);
1885
1886 9 return 0;
1887 }
1888
1889 #define OFFSET(x) offsetof(SnowContext, x)
1890 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1891 static const AVOption options[] = {
1892 {"motion_est", "motion estimation algorithm", OFFSET(motion_est), AV_OPT_TYPE_INT, {.i64 = FF_ME_EPZS }, FF_ME_ZERO, FF_ME_ITER, VE, "motion_est" },
1893 { "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, VE, "motion_est" },
1894 { "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, VE, "motion_est" },
1895 { "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, VE, "motion_est" },
1896 { "iter", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ITER }, 0, 0, VE, "motion_est" },
1897 { "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 },
1898 { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1899 { "intra_penalty", "Penalty for intra blocks in block decission", OFFSET(intra_penalty), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1900 { "iterative_dia_size", "Dia size for the iterative ME", OFFSET(iterative_dia_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1901 { "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, VE },
1902 { "pred", "Spatial decomposition type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, DWT_97, DWT_53, VE, "pred" },
1903 { "dwt97", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1904 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
1905 { "rc_eq", "Set rate control equation. When computing the expression, besides the standard functions "
1906 "defined in the section 'Expression Evaluation', the following functions are available: "
1907 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv "
1908 "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.",
1909 OFFSET(m.rc_eq), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1910 { NULL },
1911 };
1912
1913 static const AVClass snowenc_class = {
1914 .class_name = "snow encoder",
1915 .item_name = av_default_item_name,
1916 .option = options,
1917 .version = LIBAVUTIL_VERSION_INT,
1918 };
1919
1920 const AVCodec ff_snow_encoder = {
1921 .name = "snow",
1922 .long_name = NULL_IF_CONFIG_SMALL("Snow"),
1923 .type = AVMEDIA_TYPE_VIDEO,
1924 .id = AV_CODEC_ID_SNOW,
1925 .priv_data_size = sizeof(SnowContext),
1926 .init = encode_init,
1927 .encode2 = encode_frame,
1928 .close = encode_end,
1929 .pix_fmts = (const enum AVPixelFormat[]){
1930 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV444P,
1931 AV_PIX_FMT_GRAY8,
1932 AV_PIX_FMT_NONE
1933 },
1934 .priv_class = &snowenc_class,
1935 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1936 FF_CODEC_CAP_INIT_CLEANUP,
1937 };
1938