FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 112 125 89.6%
Functions: 12 12 100.0%
Branches: 48 66 72.7%

Line Branch Exec Source
1 /*
2 * FFV1 codec for libavcodec
3 *
4 * Copyright (c) 2003-2013 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 /**
24 * @file
25 * FF Video Codec 1 (a lossless codec)
26 */
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/mem.h"
31
32 #include "avcodec.h"
33 #include "ffv1.h"
34 #include "libavutil/refstruct.h"
35
36 205 av_cold int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s)
37 {
38
2/4
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 205 times.
205 if (!avctx->width || !avctx->height)
39 return AVERROR_INVALIDDATA;
40
41 205 s->avctx = avctx;
42 205 s->flags = avctx->flags;
43
44 205 s->width = avctx->width;
45 205 s->height = avctx->height;
46
47 // defaults
48 205 s->num_h_slices = 1;
49 205 s->num_v_slices = 1;
50
51 205 return 0;
52 }
53
54 1433 static void planes_free(AVRefStructOpaque opaque, void *obj)
55 {
56 1433 PlaneContext *planes = obj;
57
58
2/2
✓ Branch 0 taken 5732 times.
✓ Branch 1 taken 1433 times.
7165 for (int i = 0; i < MAX_PLANES; i++) {
59 5732 PlaneContext *p = &planes[i];
60
61 5732 av_freep(&p->state);
62 5732 av_freep(&p->vlc_state);
63 }
64 1433 }
65
66 1433 PlaneContext* ff_ffv1_planes_alloc(void)
67 {
68 1433 return av_refstruct_alloc_ext(sizeof(PlaneContext) * MAX_PLANES,
69 0, NULL, planes_free);
70 }
71
72 14548 av_cold int ff_ffv1_init_slice_state(const FFV1Context *f,
73 FFV1SliceContext *sc)
74 {
75 int j, i;
76
77
2/2
✓ Branch 0 taken 29096 times.
✓ Branch 1 taken 14548 times.
43644 for (j = 0; j < f->plane_count; j++) {
78 29096 PlaneContext *const p = &sc->plane[j];
79
80
2/2
✓ Branch 0 taken 16584 times.
✓ Branch 1 taken 12512 times.
29096 if (f->ac != AC_GOLOMB_RICE) {
81
2/2
✓ Branch 0 taken 2184 times.
✓ Branch 1 taken 14400 times.
16584 if (!p->state)
82 2184 p->state = av_malloc_array(p->context_count, CONTEXT_SIZE *
83 sizeof(uint8_t));
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16584 times.
16584 if (!p->state)
85 return AVERROR(ENOMEM);
86 } else {
87
2/2
✓ Branch 0 taken 1630 times.
✓ Branch 1 taken 10882 times.
12512 if (!p->vlc_state) {
88 1630 p->vlc_state = av_calloc(p->context_count, sizeof(*p->vlc_state));
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1630 times.
1630 if (!p->vlc_state)
90 return AVERROR(ENOMEM);
91
2/2
✓ Branch 0 taken 675324 times.
✓ Branch 1 taken 1630 times.
676954 for (i = 0; i < p->context_count; i++) {
92 675324 p->vlc_state[i].error_sum = 4;
93 675324 p->vlc_state[i].count = 1;
94 }
95 }
96 }
97 }
98
99
2/2
✓ Branch 0 taken 8292 times.
✓ Branch 1 taken 6256 times.
14548 if (f->ac == AC_RANGE_CUSTOM_TAB) {
100 //FIXME only redo if state_transition changed
101
2/2
✓ Branch 0 taken 2114460 times.
✓ Branch 1 taken 8292 times.
2122752 for (j = 1; j < 256; j++) {
102 2114460 sc->c. one_state[ j] = f->state_transition[j];
103 2114460 sc->c.zero_state[256 - j] = 256 - sc->c.one_state[j];
104 }
105 }
106
107 14548 return 0;
108 }
109
110 51 av_cold int ff_ffv1_init_slices_state(FFV1Context *f)
111 {
112 int i, ret;
113
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 51 times.
240 for (i = 0; i < f->max_slice_count; i++) {
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
189 if ((ret = ff_ffv1_init_slice_state(f, &f->slices[i])) < 0)
115 return AVERROR(ENOMEM);
116 }
117 51 return 0;
118 }
119
120 100 int ff_need_new_slices(int width, int num_h_slices, int chroma_shift) {
121 100 int mpw = 1<<chroma_shift;
122 100 int i = width * (int64_t)(num_h_slices - 1) / num_h_slices;
123
124
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
100 return width % mpw && (width - i) % mpw == 0;
125 }
126
127 28620 int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift) {
128 28620 int mpw = 1<<chroma_shift;
129 28620 int awidth = FFALIGN(width, mpw);
130
131
1/2
✓ Branch 0 taken 28620 times.
✗ Branch 1 not taken.
28620 if (f->combined_version <= 0x40002)
132 28620 return width * sx / num_h_slices;
133
134 sx = (2LL * awidth * sx + num_h_slices * mpw) / (2 * num_h_slices * mpw) * mpw;
135 if (sx == awidth)
136 sx = width;
137 return sx;
138 }
139
140 139 av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
141 {
142 139 int max_slice_count = f->num_h_slices * f->num_v_slices;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 av_assert0(max_slice_count > 0);
145
146 139 f->slices = av_calloc(max_slice_count, sizeof(*f->slices));
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (!f->slices)
148 return AVERROR(ENOMEM);
149
150 139 f->max_slice_count = max_slice_count;
151
152
2/2
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 139 times.
650 for (int i = 0; i < max_slice_count; i++) {
153 511 FFV1SliceContext *sc = &f->slices[i];
154 511 int sx = i % f->num_h_slices;
155 511 int sy = i / f->num_h_slices;
156 511 int sxs = ff_slice_coord(f, f->avctx->width , sx , f->num_h_slices, f->chroma_h_shift);
157 511 int sxe = ff_slice_coord(f, f->avctx->width , sx + 1, f->num_h_slices, f->chroma_h_shift);
158 511 int sys = ff_slice_coord(f, f->avctx->height, sy , f->num_v_slices, f->chroma_v_shift);
159 511 int sye = ff_slice_coord(f, f->avctx->height, sy + 1, f->num_v_slices, f->chroma_v_shift);
160
161 511 sc->slice_width = sxe - sxs;
162 511 sc->slice_height = sye - sys;
163 511 sc->slice_x = sxs;
164 511 sc->slice_y = sys;
165 511 sc->sx = sx;
166 511 sc->sy = sy;
167
168 511 sc->sample_buffer = av_malloc_array((f->width + 6), 3 * MAX_PLANES *
169 sizeof(*sc->sample_buffer));
170 511 sc->sample_buffer32 = av_malloc_array((f->width + 6), 3 * MAX_PLANES *
171 sizeof(*sc->sample_buffer32));
172
2/4
✓ Branch 0 taken 511 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 511 times.
511 if (!sc->sample_buffer || !sc->sample_buffer32)
173 return AVERROR(ENOMEM);
174
175 511 sc->plane = ff_ffv1_planes_alloc();
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 511 times.
511 if (!sc->plane)
177 return AVERROR(ENOMEM);
178 }
179
180 139 return 0;
181 }
182
183 191 int ff_ffv1_allocate_initial_states(FFV1Context *f)
184 {
185 int i;
186
187
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 191 times.
573 for (i = 0; i < f->quant_table_count; i++) {
188 382 f->initial_states[i] = av_malloc_array(f->context_count[i],
189 sizeof(*f->initial_states[i]));
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382 times.
382 if (!f->initial_states[i])
191 return AVERROR(ENOMEM);
192 382 memset(f->initial_states[i], 128,
193 382 f->context_count[i] * sizeof(*f->initial_states[i]));
194 }
195 191 return 0;
196 }
197
198 1843 void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
199 {
200 int i, j;
201
202
2/2
✓ Branch 0 taken 3686 times.
✓ Branch 1 taken 1843 times.
5529 for (i = 0; i < f->plane_count; i++) {
203 3686 PlaneContext *p = &sc->plane[i];
204
205
2/2
✓ Branch 0 taken 2104 times.
✓ Branch 1 taken 1582 times.
3686 if (f->ac != AC_GOLOMB_RICE) {
206
1/2
✓ Branch 0 taken 2104 times.
✗ Branch 1 not taken.
2104 if (f->initial_states[p->quant_table_index]) {
207 2104 memcpy(p->state, f->initial_states[p->quant_table_index],
208 2104 CONTEXT_SIZE * p->context_count);
209 } else
210 memset(p->state, 128, CONTEXT_SIZE * p->context_count);
211 } else {
212
2/2
✓ Branch 0 taken 1053612 times.
✓ Branch 1 taken 1582 times.
1055194 for (j = 0; j < p->context_count; j++) {
213 1053612 p->vlc_state[j].drift = 0;
214 1053612 p->vlc_state[j].error_sum = 4; //FFMAX((RANGE + 32)/64, 2);
215 1053612 p->vlc_state[j].bias = 0;
216 1053612 p->vlc_state[j].count = 1;
217 }
218 }
219 }
220 1843 }
221
222 4933324 int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
223 {
224 4933324 return get_symbol_inline(c, state, is_signed);
225 }
226
227 206 av_cold void ff_ffv1_close(FFV1Context *s)
228 {
229 int i, j;
230
231
2/2
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 206 times.
717 for (j = 0; j < s->max_slice_count; j++) {
232 511 FFV1SliceContext *sc = &s->slices[j];
233
234 511 av_freep(&sc->sample_buffer);
235 511 av_freep(&sc->sample_buffer32);
236
237 511 av_refstruct_unref(&sc->plane);
238 }
239
240 206 av_refstruct_unref(&s->slice_damaged);
241
242
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 206 times.
588 for (j = 0; j < s->quant_table_count; j++) {
243 382 av_freep(&s->initial_states[j]);
244
2/2
✓ Branch 0 taken 1002 times.
✓ Branch 1 taken 382 times.
1384 for (i = 0; i < s->max_slice_count; i++) {
245 1002 FFV1SliceContext *sc = &s->slices[i];
246 1002 av_freep(&sc->rc_stat2[j]);
247 }
248 382 av_freep(&s->rc_stat2[j]);
249 }
250
251 206 av_freep(&s->slices);
252 206 }
253