FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 113 126 89.7%
Functions: 11 11 100.0%
Branches: 48 70 68.6%

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