FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1enc_template.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 99 148 66.9%
Functions: 2 4 50.0%
Branches: 34 54 63.0%

Line Branch Exec Source
1 /*
2 * FFV1 encoder template
3 *
4 * Copyright (c) 2003-2016 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "ffv1_template.c"
24
25 static av_always_inline int
26 5505600 RENAME(encode_line)(FFV1Context *f, FFV1SliceContext *sc,
27 void *logctx,
28 int w, TYPE *sample[3], int plane_index, int bits,
29 int ac, int pass1)
30 {
31 5505600 PlaneContext *const p = &sc->plane[plane_index];
32 5505600 RangeCoder *const c = &sc->c;
33 int x;
34 5505600 int run_index = sc->run_index;
35 5505600 int run_count = 0;
36 5505600 int run_mode = 0;
37
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2752800 times.
5505600 if (bits == 0)
39 return 0;
40
41
2/2
✓ Branch 0 taken 1739000 times.
✓ Branch 1 taken 1013800 times.
5505600 if (ac != AC_GOLOMB_RICE) {
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1739000 times.
3478000 if (c->bytestream_end - c->bytestream < w * 35) {
43 av_log(logctx, AV_LOG_ERROR, "encoded Range Coder frame too large\n");
44 return AVERROR_INVALIDDATA;
45 }
46 } else {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1013800 times.
2027600 if (put_bytes_left(&sc->pb, 0) < w * 4) {
48 av_log(logctx, AV_LOG_ERROR, "encoded Golomb Rice frame too large\n");
49 return AVERROR_INVALIDDATA;
50 }
51 }
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2752800 times.
5505600 if (sc->slice_coding_mode == 1) {
54 for (x = 0; x < w; x++) {
55 int i;
56 int v = sample[0][x];
57 for (i = bits-1; i>=0; i--) {
58 uint8_t state = 128;
59 put_rac(c, &state, (v>>i) & 1);
60 }
61 }
62 return 0;
63 }
64
65
2/2
✓ Branch 0 taken 406350100 times.
✓ Branch 1 taken 2752800 times.
818205800 for (x = 0; x < w; x++) {
66 int diff, context;
67
68 812700200 context = RENAME(get_context)(f->quant_tables[p->quant_table_index],
69 812700200 sample[0] + x, sample[1] + x, sample[2] + x);
70 812700200 diff = sample[0][x] - RENAME(predict)(sample[0] + x, sample[1] + x);
71
72
2/2
✓ Branch 0 taken 184040154 times.
✓ Branch 1 taken 222309946 times.
812700200 if (context < 0) {
73 368080308 context = -context;
74 368080308 diff = -diff;
75 }
76
77 812700200 diff = fold(diff, bits);
78
79
2/2
✓ Branch 0 taken 238203400 times.
✓ Branch 1 taken 168146700 times.
812700200 if (ac != AC_GOLOMB_RICE) {
80
2/2
✓ Branch 0 taken 53431600 times.
✓ Branch 1 taken 184771800 times.
476406800 if (pass1) {
81 106863200 put_symbol_inline(c, p->state[context], diff, 1, sc->rc_stat,
82 106863200 sc->rc_stat2[p->quant_table_index][context]);
83 } else {
84 369543600 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
85 }
86 } else {
87
2/2
✓ Branch 0 taken 3890915 times.
✓ Branch 1 taken 164255785 times.
336293400 if (context == 0)
88 7781830 run_mode = 1;
89
90
2/2
✓ Branch 0 taken 6062596 times.
✓ Branch 1 taken 162084104 times.
336293400 if (run_mode) {
91
2/2
✓ Branch 0 taken 1575887 times.
✓ Branch 1 taken 4486709 times.
12125192 if (diff) {
92
2/2
✓ Branch 0 taken 1413677 times.
✓ Branch 1 taken 1575887 times.
5979128 while (run_count >= 1 << ff_log2_run[run_index]) {
93 2827354 run_count -= 1 << ff_log2_run[run_index];
94 2827354 run_index++;
95 2827354 put_bits(&sc->pb, 1, 1);
96 }
97
98 3151774 put_bits(&sc->pb, 1 + ff_log2_run[run_index], run_count);
99
2/2
✓ Branch 0 taken 1439479 times.
✓ Branch 1 taken 136408 times.
3151774 if (run_index)
100 2878958 run_index--;
101 3151774 run_count = 0;
102 3151774 run_mode = 0;
103
2/2
✓ Branch 0 taken 740677 times.
✓ Branch 1 taken 835210 times.
3151774 if (diff > 0)
104 1481354 diff--;
105 } else {
106 8973418 run_count++;
107 }
108 }
109
110 ff_dlog(logctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
111 run_count, run_index, run_mode, x,
112 (int)put_bits_count(&sc->pb));
113
114
2/2
✓ Branch 0 taken 163659991 times.
✓ Branch 1 taken 4486709 times.
336293400 if (run_mode == 0)
115 327319982 put_vlc_symbol(&sc->pb, &p->vlc_state[context], diff, bits);
116 }
117 }
118
2/2
✓ Branch 0 taken 57502 times.
✓ Branch 1 taken 2695298 times.
5505600 if (run_mode) {
119
2/2
✓ Branch 0 taken 43475 times.
✓ Branch 1 taken 57502 times.
201954 while (run_count >= 1 << ff_log2_run[run_index]) {
120 86950 run_count -= 1 << ff_log2_run[run_index];
121 86950 run_index++;
122 86950 put_bits(&sc->pb, 1, 1);
123 }
124
125
2/2
✓ Branch 0 taken 37708 times.
✓ Branch 1 taken 19794 times.
115004 if (run_count)
126 75416 put_bits(&sc->pb, 1, 1);
127 }
128 5505600 sc->run_index = run_index;
129
130 5505600 return 0;
131 }
132
133 static void RENAME(load_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
134 const uint8_t *src[4],
135 int w, int h, const int stride[4])
136 {
137 int x, y;
138 int transparency = f->transparency;
139
140 for (int p = 0; p<3 + transparency; p++)
141 memset(sc->fltmap[p], 0, 65536 * sizeof(**sc->fltmap));
142
143 for (y = 0; y < h; y++) {
144 for (x = 0; x < w; x++) {
145 int b, g, r, av_uninit(a);
146
147 if (sizeof(TYPE) == 4 || transparency) {
148 g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
149 b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
150 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
151 if (transparency)
152 a = *((const uint16_t *)(src[3] + x*2 + stride[3]*y));
153 } else {
154 b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
155 g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
156 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
157 }
158
159 sc->fltmap[0][g] = 1;
160 sc->fltmap[1][b] = 1;
161 sc->fltmap[2][r] = 1;
162 if (transparency)
163 sc->fltmap[3][a] = 1;
164 }
165 }
166 }
167
168 1600 static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
169 const uint8_t *src[4],
170 int w, int h, const int stride[4], int ac)
171 {
172 int x, y, p, i;
173 1600 const int ring_size = f->context_model ? 3 : 2;
174 TYPE *sample[4][3];
175 1600 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
176 1600 int lbd = f->bits_per_raw_sample <= 8;
177 1600 int packed = !src[1];
178 int bits[4], offset;
179 1600 int transparency = f->transparency;
180 1600 int packed_size = (3 + transparency)*2;
181
182 1600 ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
183
184 1600 sc->run_index = 0;
185
186 1600 memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
187 1600 (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
188
189 181200 for (y = 0; y < h; y++) {
190 538800 for (i = 0; i < ring_size; i++)
191 1796000 for (p = 0; p < MAX_PLANES; p++)
192 1436800 sample[p][i]= RENAME(sc->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
193
194 30708000 for (x = 0; x < w; x++) {
195 30528400 int b, g, r, av_uninit(a);
196 30528400 if (lbd) {
197 15264200 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
198 15264200 b = v & 0xFF;
199 15264200 g = (v >> 8) & 0xFF;
200 15264200 r = (v >> 16) & 0xFF;
201 15264200 a = v >> 24;
202 15264200 } else if (packed) {
203 15264200 const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
204 15264200 r = p[0];
205 15264200 g = p[1];
206 15264200 b = p[2];
207 15264200 if (transparency)
208 a = p[3];
209 } else if (sizeof(TYPE) == 4 || transparency) {
210 g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
211 b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
212 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
213 if (transparency)
214 a = *((const uint16_t *)(src[3] + x*2 + stride[3]*y));
215 } else {
216 b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
217 g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
218 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
219 }
220
221 30528400 if (sc->remap) {
222 g = sc->fltmap[0][g];
223 b = sc->fltmap[1][b];
224 r = sc->fltmap[2][r];
225 if (transparency)
226 a = sc->fltmap[3][a];
227 }
228
229 30528400 if (sc->slice_coding_mode != 1) {
230 30528400 b -= g;
231 30528400 r -= g;
232 30528400 g += (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
233 30528400 b += offset;
234 30528400 r += offset;
235 }
236
237 30528400 sample[0][0][x] = g;
238 30528400 sample[1][0][x] = b;
239 30528400 sample[2][0][x] = r;
240 30528400 sample[3][0][x] = a;
241 }
242 718400 for (p = 0; p < 3 + transparency; p++) {
243 int ret;
244 538800 sample[p][0][-1] = sample[p][1][0 ];
245 538800 sample[p][1][ w] = sample[p][1][w-1];
246 538800 if (bits[p] == 9)
247 269400 ret = RENAME(encode_line)(f, sc, f->avctx, w, sample[p], (p + 1) / 2, 9, ac, pass1);
248 else
249 269400 ret = RENAME(encode_line)(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
250 bits[p], ac, pass1);
251 538800 if (ret < 0)
252 return ret;
253 }
254 }
255 1600 return 0;
256 }
257
258