FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_picture.c
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 101 129 78.3%
Functions: 6 6 100.0%
Branches: 40 66 60.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 "refstruct.h"
36 #include "thread.h"
37 #include "threadframe.h"
38
39 134419 void ff_h264_unref_picture(H264Picture *pic)
40 {
41 134419 int off = offsetof(H264Picture, f_grain) + sizeof(pic->f_grain);
42 int i;
43
44
3/4
✓ Branch 0 taken 134419 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81972 times.
✓ Branch 3 taken 52447 times.
134419 if (!pic->f || !pic->f->buf[0])
45 81972 return;
46
47 52447 ff_thread_release_ext_buffer(&pic->tf);
48 52447 av_frame_unref(pic->f_grain);
49 52447 ff_refstruct_unref(&pic->hwaccel_picture_private);
50
51 52447 ff_refstruct_unref(&pic->qscale_table_base);
52 52447 ff_refstruct_unref(&pic->mb_type_base);
53 52447 ff_refstruct_unref(&pic->pps);
54
2/2
✓ Branch 0 taken 104894 times.
✓ Branch 1 taken 52447 times.
157341 for (i = 0; i < 2; i++) {
55 104894 ff_refstruct_unref(&pic->motion_val_base[i]);
56 104894 ff_refstruct_unref(&pic->ref_index[i]);
57 }
58 52447 ff_refstruct_unref(&pic->decode_error_flags);
59
60 52447 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
61 }
62
63 27056 static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
64 {
65 27056 ff_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
66 27056 ff_refstruct_replace(&dst->mb_type_base, src->mb_type_base);
67 27056 ff_refstruct_replace(&dst->pps, src->pps);
68
69
2/2
✓ Branch 0 taken 54112 times.
✓ Branch 1 taken 27056 times.
81168 for (int i = 0; i < 2; i++) {
70 54112 ff_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
71 54112 ff_refstruct_replace(&dst->ref_index[i], src->ref_index[i]);
72 }
73
74 27056 ff_refstruct_replace(&dst->hwaccel_picture_private,
75 27056 src->hwaccel_picture_private);
76
77 27056 ff_refstruct_replace(&dst->decode_error_flags, src->decode_error_flags);
78
79 27056 dst->qscale_table = src->qscale_table;
80 27056 dst->mb_type = src->mb_type;
81
82
2/2
✓ Branch 0 taken 54112 times.
✓ Branch 1 taken 27056 times.
81168 for (int i = 0; i < 2; i++)
83 54112 dst->motion_val[i] = src->motion_val[i];
84
85
2/2
✓ Branch 0 taken 54112 times.
✓ Branch 1 taken 27056 times.
81168 for (int i = 0; i < 2; i++)
86 54112 dst->field_poc[i] = src->field_poc[i];
87
88 27056 memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
89 27056 memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
90
91 27056 dst->poc = src->poc;
92 27056 dst->frame_num = src->frame_num;
93 27056 dst->mmco_reset = src->mmco_reset;
94 27056 dst->long_ref = src->long_ref;
95 27056 dst->mbaff = src->mbaff;
96 27056 dst->field_picture = src->field_picture;
97 27056 dst->reference = src->reference;
98 27056 dst->recovered = src->recovered;
99 27056 dst->gray = src->gray;
100 27056 dst->invalid_gap = src->invalid_gap;
101 27056 dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
102 27056 dst->mb_width = src->mb_width;
103 27056 dst->mb_height = src->mb_height;
104 27056 dst->mb_stride = src->mb_stride;
105 27056 dst->needs_fg = src->needs_fg;
106 27056 }
107
108 26738 int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src)
109 {
110 int ret;
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26738 times.
26738 av_assert0(!dst->f->buf[0]);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26738 times.
26738 av_assert0(src->f->buf[0]);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26738 times.
26738 av_assert0(src->tf.f == src->f);
115
116 26738 dst->tf.f = dst->f;
117 26738 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26738 times.
26738 if (ret < 0)
119 goto fail;
120
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26738 times.
26738 if (src->needs_fg) {
122 ret = av_frame_ref(dst->f_grain, src->f_grain);
123 if (ret < 0)
124 goto fail;
125 }
126
127 26738 h264_copy_picture_params(dst, src);
128
129 26738 return 0;
130 fail:
131 ff_h264_unref_picture(dst);
132 return ret;
133 }
134
135 2294 int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src)
136 {
137 int ret;
138
139
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]) {
140 1976 ff_h264_unref_picture(dst);
141 1976 return 0;
142 }
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 av_assert0(src->tf.f == src->f);
145
146 318 dst->tf.f = dst->f;
147 318 ret = ff_thread_replace_frame(&dst->tf, &src->tf);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (ret < 0)
149 goto fail;
150
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
318 if (src->needs_fg) {
152 av_frame_unref(dst->f_grain);
153 ret = av_frame_ref(dst->f_grain, src->f_grain);
154 if (ret < 0)
155 goto fail;
156 }
157
158 318 h264_copy_picture_params(dst, src);
159
160 318 return 0;
161 fail:
162 ff_h264_unref_picture(dst);
163 return ret;
164 }
165
166 137540 void ff_h264_set_erpic(ERPicture *dst, const H264Picture *src)
167 {
168 #if CONFIG_ERROR_RESILIENCE
169 int i;
170
171 137540 memset(dst, 0, sizeof(*dst));
172
173
2/2
✓ Branch 0 taken 87517 times.
✓ Branch 1 taken 50023 times.
137540 if (!src)
174 87517 return;
175
176 50023 dst->f = src->f;
177 50023 dst->tf = &src->tf;
178
179
2/2
✓ Branch 0 taken 100046 times.
✓ Branch 1 taken 50023 times.
150069 for (i = 0; i < 2; i++) {
180 100046 dst->motion_val[i] = src->motion_val[i];
181 100046 dst->ref_index[i] = src->ref_index[i];
182 }
183
184 50023 dst->mb_type = src->mb_type;
185 50023 dst->field_picture = src->field_picture;
186 #endif /* CONFIG_ERROR_RESILIENCE */
187 }
188
189 28291 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
190 {
191 28291 AVCodecContext *const avctx = h->avctx;
192 28291 H264Picture *cur = h->cur_pic_ptr;
193 28291 int err = 0;
194 28291 h->mb_y = 0;
195
196
4/4
✓ Branch 0 taken 28097 times.
✓ Branch 1 taken 194 times.
✓ Branch 2 taken 28055 times.
✓ Branch 3 taken 42 times.
28291 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
197
2/2
✓ Branch 0 taken 18459 times.
✓ Branch 1 taken 9790 times.
28249 if (!h->droppable) {
198 18459 err = ff_h264_execute_ref_pic_marking(h);
199 18459 h->poc.prev_poc_msb = h->poc.poc_msb;
200 18459 h->poc.prev_poc_lsb = h->poc.poc_lsb;
201 }
202 28249 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
203 28249 h->poc.prev_frame_num = h->poc.frame_num;
204 }
205
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28291 times.
28291 if (avctx->hwaccel) {
207 err = FF_HW_SIMPLE_CALL(avctx, end_frame);
208 if (err < 0)
209 av_log(avctx, AV_LOG_ERROR,
210 "hardware accelerator failed to decode picture\n");
211
3/8
✓ Branch 0 taken 28097 times.
✓ Branch 1 taken 194 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28097 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
28291 } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) {
212 const AVFrameSideData *sd = av_frame_get_side_data(cur->f, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
213
214 err = AVERROR_INVALIDDATA;
215 if (sd) // a decoding error may have happened before the side data could be allocated
216 err = ff_h274_apply_film_grain(cur->f_grain, cur->f, &h->h274db,
217 (AVFilmGrainParams *) sd->data);
218 if (err < 0) {
219 av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film "
220 "grain, ignoring: %s\n", av_err2str(err));
221 cur->needs_fg = 0;
222 err = 0;
223 }
224 }
225
226
4/4
✓ Branch 0 taken 28097 times.
✓ Branch 1 taken 194 times.
✓ Branch 2 taken 18417 times.
✓ Branch 3 taken 9680 times.
28291 if (!in_setup && !h->droppable)
227 18417 ff_thread_report_progress(&cur->tf, INT_MAX,
228 18417 h->picture_structure == PICT_BOTTOM_FIELD);
229 28291 emms_c();
230
231 28291 h->current_slice = 0;
232
233 28291 return err;
234 }
235