FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_picture.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 113 156 72.4%
Branches: 51 92 55.4%

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/imgutils.h"
30 #include "internal.h"
31 #include "cabac.h"
32 #include "cabac_functions.h"
33 #include "error_resilience.h"
34 #include "avcodec.h"
35 #include "h264dec.h"
36 #include "h264data.h"
37 #include "h264chroma.h"
38 #include "h264_mvpred.h"
39 #include "mathops.h"
40 #include "mpegutils.h"
41 #include "rectangle.h"
42 #include "thread.h"
43
44 122945 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
45 {
46 122945 int off = offsetof(H264Picture, tf_grain) + sizeof(pic->tf_grain);
47 int i;
48
49
3/4
✓ Branch 0 taken 122945 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74905 times.
✓ Branch 3 taken 48040 times.
122945 if (!pic->f || !pic->f->buf[0])
50 74905 return;
51
52 48040 ff_thread_release_buffer(h->avctx, &pic->tf);
53 48040 ff_thread_release_buffer(h->avctx, &pic->tf_grain);
54 48040 av_buffer_unref(&pic->hwaccel_priv_buf);
55
56 48040 av_buffer_unref(&pic->qscale_table_buf);
57 48040 av_buffer_unref(&pic->mb_type_buf);
58 48040 av_buffer_unref(&pic->pps_buf);
59
2/2
✓ Branch 0 taken 96080 times.
✓ Branch 1 taken 48040 times.
144120 for (i = 0; i < 2; i++) {
60 96080 av_buffer_unref(&pic->motion_val_buf[i]);
61 96080 av_buffer_unref(&pic->ref_index_buf[i]);
62 }
63
64 48040 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
65 }
66
67 24742 static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
68 {
69 24742 dst->qscale_table = src->qscale_table;
70 24742 dst->mb_type = src->mb_type;
71 24742 dst->pps = src->pps;
72
73
2/2
✓ Branch 0 taken 49484 times.
✓ Branch 1 taken 24742 times.
74226 for (int i = 0; i < 2; i++) {
74 49484 dst->motion_val[i] = src->motion_val[i];
75 49484 dst->ref_index[i] = src->ref_index[i];
76 }
77
78
2/2
✓ Branch 0 taken 49484 times.
✓ Branch 1 taken 24742 times.
74226 for (int i = 0; i < 2; i++)
79 49484 dst->field_poc[i] = src->field_poc[i];
80
81 24742 memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
82 24742 memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
83
84 24742 dst->poc = src->poc;
85 24742 dst->frame_num = src->frame_num;
86 24742 dst->mmco_reset = src->mmco_reset;
87 24742 dst->long_ref = src->long_ref;
88 24742 dst->mbaff = src->mbaff;
89 24742 dst->field_picture = src->field_picture;
90 24742 dst->reference = src->reference;
91 24742 dst->recovered = src->recovered;
92 24742 dst->invalid_gap = src->invalid_gap;
93 24742 dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
94 24742 dst->mb_width = src->mb_width;
95 24742 dst->mb_height = src->mb_height;
96 24742 dst->mb_stride = src->mb_stride;
97 24742 dst->needs_fg = src->needs_fg;
98 24742 }
99
100 24517 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
101 {
102 int ret, i;
103
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24517 times.
24517 av_assert0(!dst->f->buf[0]);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24517 times.
24517 av_assert0(src->f->buf[0]);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24517 times.
24517 av_assert0(src->tf.f == src->f);
107
108 24517 dst->tf.f = dst->f;
109 24517 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24517 times.
24517 if (ret < 0)
111 goto fail;
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24517 times.
24517 if (src->needs_fg) {
114 av_assert0(src->tf_grain.f == src->f_grain);
115 dst->tf_grain.f = dst->f_grain;
116 ret = ff_thread_ref_frame(&dst->tf_grain, &src->tf_grain);
117 if (ret < 0)
118 goto fail;
119 }
120
121 24517 dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
122 24517 dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
123 24517 dst->pps_buf = av_buffer_ref(src->pps_buf);
124
3/6
✓ Branch 0 taken 24517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24517 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24517 times.
24517 if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf) {
125 ret = AVERROR(ENOMEM);
126 goto fail;
127 }
128
129
2/2
✓ Branch 0 taken 49034 times.
✓ Branch 1 taken 24517 times.
73551 for (i = 0; i < 2; i++) {
130 49034 dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
131 49034 dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
132
2/4
✓ Branch 0 taken 49034 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49034 times.
49034 if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) {
133 ret = AVERROR(ENOMEM);
134 goto fail;
135 }
136 }
137
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24517 times.
24517 if (src->hwaccel_picture_private) {
139 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
140 if (!dst->hwaccel_priv_buf) {
141 ret = AVERROR(ENOMEM);
142 goto fail;
143 }
144 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
145 }
146
147 24517 h264_copy_picture_params(dst, src);
148
149 24517 return 0;
150 fail:
151 ff_h264_unref_picture(h, dst);
152 return ret;
153 }
154
155 1591 int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture *src)
156 {
157 int ret, i;
158
159
3/4
✓ Branch 0 taken 1591 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1366 times.
✓ Branch 3 taken 225 times.
1591 if (!src->f || !src->f->buf[0]) {
160 1366 ff_h264_unref_picture(h, dst);
161 1366 return 0;
162 }
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 av_assert0(src->tf.f == src->f);
165
166 225 dst->tf.f = dst->f;
167 225 ff_thread_release_buffer(h->avctx, &dst->tf);
168 225 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 if (ret < 0)
170 goto fail;
171
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 if (src->needs_fg) {
173 av_assert0(src->tf_grain.f == src->f_grain);
174 dst->tf_grain.f = dst->f_grain;
175 ff_thread_release_buffer(h->avctx, &dst->tf_grain);
176 ret = ff_thread_ref_frame(&dst->tf_grain, &src->tf_grain);
177 if (ret < 0)
178 goto fail;
179 }
180
181 225 ret = av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
182 225 ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
183 225 ret |= av_buffer_replace(&dst->pps_buf, src->pps_buf);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 if (ret < 0)
185 goto fail;
186
187
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 225 times.
675 for (i = 0; i < 2; i++) {
188 450 ret = av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
189 450 ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if (ret < 0)
191 goto fail;
192 }
193
194 225 ret = av_buffer_replace(&dst->hwaccel_priv_buf, src->hwaccel_priv_buf);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 if (ret < 0)
196 goto fail;
197
198 225 dst->hwaccel_picture_private = src->hwaccel_picture_private;
199
200 225 h264_copy_picture_params(dst, src);
201
202 225 return 0;
203 fail:
204 ff_h264_unref_picture(h, dst);
205 return ret;
206 }
207
208 124835 void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
209 {
210 #if CONFIG_ERROR_RESILIENCE
211 int i;
212
213 124835 memset(dst, 0, sizeof(*dst));
214
215
2/2
✓ Branch 0 taken 80427 times.
✓ Branch 1 taken 44408 times.
124835 if (!src)
216 80427 return;
217
218 44408 dst->f = src->f;
219 44408 dst->tf = &src->tf;
220
221
2/2
✓ Branch 0 taken 88816 times.
✓ Branch 1 taken 44408 times.
133224 for (i = 0; i < 2; i++) {
222 88816 dst->motion_val[i] = src->motion_val[i];
223 88816 dst->ref_index[i] = src->ref_index[i];
224 }
225
226 44408 dst->mb_type = src->mb_type;
227 44408 dst->field_picture = src->field_picture;
228 #endif /* CONFIG_ERROR_RESILIENCE */
229 }
230
231 26111 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
232 {
233 26111 AVCodecContext *const avctx = h->avctx;
234 26111 H264Picture *cur = h->cur_pic_ptr;
235 26111 int err = 0;
236 26111 h->mb_y = 0;
237
238
4/4
✓ Branch 0 taken 25918 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 25886 times.
✓ Branch 3 taken 32 times.
26111 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
239
2/2
✓ Branch 0 taken 16906 times.
✓ Branch 1 taken 9173 times.
26079 if (!h->droppable) {
240 16906 err = ff_h264_execute_ref_pic_marking(h);
241 16906 h->poc.prev_poc_msb = h->poc.poc_msb;
242 16906 h->poc.prev_poc_lsb = h->poc.poc_lsb;
243 }
244 26079 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
245 26079 h->poc.prev_frame_num = h->poc.frame_num;
246 }
247
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26111 times.
26111 if (avctx->hwaccel) {
249 err = avctx->hwaccel->end_frame(avctx);
250 if (err < 0)
251 av_log(avctx, AV_LOG_ERROR,
252 "hardware accelerator failed to decode picture\n");
253
3/8
✓ Branch 0 taken 25918 times.
✓ Branch 1 taken 193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25918 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
26111 } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) {
254 AVFrameSideData *sd = av_frame_get_side_data(cur->f, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
255 av_assert0(sd); // always present if `cur->needs_fg`
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 25918 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 16856 times.
✓ Branch 3 taken 9062 times.
26111 if (!in_setup && !h->droppable)
267 16856 ff_thread_report_progress(&cur->tf, INT_MAX,
268 16856 h->picture_structure == PICT_BOTTOM_FIELD);
269 26111 emms_c();
270
271 26111 h->current_slice = 0;
272
273 26111 return err;
274 }
275