FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_picture.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 117 159 73.6%
Functions: 6 6 100.0%
Branches: 53 92 57.6%

Line Branch Exec Source
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG-4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/emms.h"
30 #include "error_resilience.h"
31 #include "avcodec.h"
32 #include "h264dec.h"
33 #include "hwaccel_internal.h"
34 #include "mpegutils.h"
35 #include "thread.h"
36 #include "threadframe.h"
37
38 128297 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
39 {
40 128297 int off = offsetof(H264Picture, f_grain) + sizeof(pic->f_grain);
41 int i;
42
43
3/4
✓ Branch 0 taken 128297 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78638 times.
✓ Branch 3 taken 49659 times.
128297 if (!pic->f || !pic->f->buf[0])
44 78638 return;
45
46 49659 ff_thread_release_ext_buffer(h->avctx, &pic->tf);
47 49659 ff_thread_release_buffer(h->avctx, pic->f_grain);
48 49659 av_buffer_unref(&pic->hwaccel_priv_buf);
49
50 49659 av_buffer_unref(&pic->qscale_table_buf);
51 49659 av_buffer_unref(&pic->mb_type_buf);
52 49659 av_buffer_unref(&pic->pps_buf);
53
2/2
✓ Branch 0 taken 99318 times.
✓ Branch 1 taken 49659 times.
148977 for (i = 0; i < 2; i++) {
54 99318 av_buffer_unref(&pic->motion_val_buf[i]);
55 99318 av_buffer_unref(&pic->ref_index_buf[i]);
56 }
57 49659 av_buffer_unref(&pic->decode_error_flags);
58
59 49659 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
60 }
61
62 25650 static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
63 {
64 25650 dst->qscale_table = src->qscale_table;
65 25650 dst->mb_type = src->mb_type;
66 25650 dst->pps = src->pps;
67
68
2/2
✓ Branch 0 taken 51300 times.
✓ Branch 1 taken 25650 times.
76950 for (int i = 0; i < 2; i++) {
69 51300 dst->motion_val[i] = src->motion_val[i];
70 51300 dst->ref_index[i] = src->ref_index[i];
71 }
72
73
2/2
✓ Branch 0 taken 51300 times.
✓ Branch 1 taken 25650 times.
76950 for (int i = 0; i < 2; i++)
74 51300 dst->field_poc[i] = src->field_poc[i];
75
76 25650 memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
77 25650 memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
78
79 25650 dst->poc = src->poc;
80 25650 dst->frame_num = src->frame_num;
81 25650 dst->mmco_reset = src->mmco_reset;
82 25650 dst->long_ref = src->long_ref;
83 25650 dst->mbaff = src->mbaff;
84 25650 dst->field_picture = src->field_picture;
85 25650 dst->reference = src->reference;
86 25650 dst->recovered = src->recovered;
87 25650 dst->invalid_gap = src->invalid_gap;
88 25650 dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
89 25650 dst->mb_width = src->mb_width;
90 25650 dst->mb_height = src->mb_height;
91 25650 dst->mb_stride = src->mb_stride;
92 25650 dst->needs_fg = src->needs_fg;
93 25650 }
94
95 25332 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
96 {
97 int ret, i;
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 av_assert0(!dst->f->buf[0]);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 av_assert0(src->f->buf[0]);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 av_assert0(src->tf.f == src->f);
102
103 25332 dst->tf.f = dst->f;
104 25332 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 if (ret < 0)
106 goto fail;
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 if (src->needs_fg) {
109 ret = av_frame_ref(dst->f_grain, src->f_grain);
110 if (ret < 0)
111 goto fail;
112 }
113
114 25332 dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
115 25332 dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
116 25332 dst->pps_buf = av_buffer_ref(src->pps_buf);
117
3/6
✓ Branch 0 taken 25332 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25332 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 25332 times.
25332 if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf) {
118 ret = AVERROR(ENOMEM);
119 goto fail;
120 }
121
122
2/2
✓ Branch 0 taken 50664 times.
✓ Branch 1 taken 25332 times.
75996 for (i = 0; i < 2; i++) {
123 50664 dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
124 50664 dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
125
2/4
✓ Branch 0 taken 50664 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50664 times.
50664 if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) {
126 ret = AVERROR(ENOMEM);
127 goto fail;
128 }
129 }
130
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 if (src->hwaccel_picture_private) {
132 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
133 if (!dst->hwaccel_priv_buf) {
134 ret = AVERROR(ENOMEM);
135 goto fail;
136 }
137 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
138 }
139
140 25332 ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25332 times.
25332 if (ret < 0)
142 goto fail;
143
144 25332 h264_copy_picture_params(dst, src);
145
146 25332 return 0;
147 fail:
148 ff_h264_unref_picture(h, dst);
149 return ret;
150 }
151
152 2294 int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture *src)
153 {
154 int ret, i;
155
156
3/4
✓ Branch 0 taken 2294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1976 times.
✓ Branch 3 taken 318 times.
2294 if (!src->f || !src->f->buf[0]) {
157 1976 ff_h264_unref_picture(h, dst);
158 1976 return 0;
159 }
160
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 av_assert0(src->tf.f == src->f);
162
163 318 dst->tf.f = dst->f;
164 318 ret = ff_thread_replace_frame(h->avctx, &dst->tf, &src->tf);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (ret < 0)
166 goto fail;
167
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (src->needs_fg) {
169 ff_thread_release_buffer(h->avctx, dst->f_grain);
170 ret = av_frame_ref(dst->f_grain, src->f_grain);
171 if (ret < 0)
172 goto fail;
173 }
174
175 318 ret = av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
176 318 ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
177 318 ret |= av_buffer_replace(&dst->pps_buf, src->pps_buf);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (ret < 0)
179 goto fail;
180
181
2/2
✓ Branch 0 taken 636 times.
✓ Branch 1 taken 318 times.
954 for (i = 0; i < 2; i++) {
182 636 ret = av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
183 636 ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
636 if (ret < 0)
185 goto fail;
186 }
187
188 318 ret = av_buffer_replace(&dst->hwaccel_priv_buf, src->hwaccel_priv_buf);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (ret < 0)
190 goto fail;
191
192 318 dst->hwaccel_picture_private = src->hwaccel_picture_private;
193
194 318 ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (ret < 0)
196 goto fail;
197
198 318 h264_copy_picture_params(dst, src);
199
200 318 return 0;
201 fail:
202 ff_h264_unref_picture(h, dst);
203 return ret;
204 }
205
206 129305 void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
207 {
208 #if CONFIG_ERROR_RESILIENCE
209 int i;
210
211 129305 memset(dst, 0, sizeof(*dst));
212
213
2/2
✓ Branch 0 taken 83359 times.
✓ Branch 1 taken 45946 times.
129305 if (!src)
214 83359 return;
215
216 45946 dst->f = src->f;
217 45946 dst->tf = &src->tf;
218
219
2/2
✓ Branch 0 taken 91892 times.
✓ Branch 1 taken 45946 times.
137838 for (i = 0; i < 2; i++) {
220 91892 dst->motion_val[i] = src->motion_val[i];
221 91892 dst->ref_index[i] = src->ref_index[i];
222 }
223
224 45946 dst->mb_type = src->mb_type;
225 45946 dst->field_picture = src->field_picture;
226 #endif /* CONFIG_ERROR_RESILIENCE */
227 }
228
229 26920 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
230 {
231 26920 AVCodecContext *const avctx = h->avctx;
232 26920 H264Picture *cur = h->cur_pic_ptr;
233 26920 int err = 0;
234 26920 h->mb_y = 0;
235
236
4/4
✓ Branch 0 taken 26727 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 26685 times.
✓ Branch 3 taken 42 times.
26920 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
237
2/2
✓ Branch 0 taken 17628 times.
✓ Branch 1 taken 9250 times.
26878 if (!h->droppable) {
238 17628 err = ff_h264_execute_ref_pic_marking(h);
239 17628 h->poc.prev_poc_msb = h->poc.poc_msb;
240 17628 h->poc.prev_poc_lsb = h->poc.poc_lsb;
241 }
242 26878 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
243 26878 h->poc.prev_frame_num = h->poc.frame_num;
244 }
245
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26920 times.
26920 if (avctx->hwaccel) {
247 err = FF_HW_SIMPLE_CALL(avctx, end_frame);
248 if (err < 0)
249 av_log(avctx, AV_LOG_ERROR,
250 "hardware accelerator failed to decode picture\n");
251
3/8
✓ Branch 0 taken 26727 times.
✓ Branch 1 taken 193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26727 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
26920 } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) {
252 AVFrameSideData *sd = av_frame_get_side_data(cur->f, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
253
254 err = AVERROR_INVALIDDATA;
255 if (sd) // a decoding error may have happened before the side data could be allocated
256 err = ff_h274_apply_film_grain(cur->f_grain, cur->f, &h->h274db,
257 (AVFilmGrainParams *) sd->data);
258 if (err < 0) {
259 av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film "
260 "grain, ignoring: %s\n", av_err2str(err));
261 cur->needs_fg = 0;
262 err = 0;
263 }
264 }
265
266
4/4
✓ Branch 0 taken 26727 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 17586 times.
✓ Branch 3 taken 9141 times.
26920 if (!in_setup && !h->droppable)
267 17586 ff_thread_report_progress(&cur->tf, INT_MAX,
268 17586 h->picture_structure == PICT_BOTTOM_FIELD);
269 26920 emms_c();
270
271 26920 h->current_slice = 0;
272
273 26920 return err;
274 }
275