FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/snowenc.c
Date: 2026-10-03 16:45:23
Exec Total Coverage
Lines: 1240 1323 93.7%
Functions: 33 33 100.0%
Branches: 736 848 86.8%

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