FFmpeg coverage


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