Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/h264_picture.c |
Date: | 2022-07-06 18:02:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 113 | 153 | 73.9% |
Branches: | 51 | 88 | 58.0% |
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 "error_resilience.h" | ||
30 | #include "avcodec.h" | ||
31 | #include "h264dec.h" | ||
32 | #include "mpegutils.h" | ||
33 | #include "thread.h" | ||
34 | #include "threadframe.h" | ||
35 | |||
36 | 123884 | void ff_h264_unref_picture(H264Context *h, H264Picture *pic) | |
37 | { | ||
38 | 123884 | int off = offsetof(H264Picture, f_grain) + sizeof(pic->f_grain); | |
39 | int i; | ||
40 | |||
41 |
3/4✓ Branch 0 taken 123884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75466 times.
✓ Branch 3 taken 48418 times.
|
123884 | if (!pic->f || !pic->f->buf[0]) |
42 | 75466 | return; | |
43 | |||
44 | 48418 | ff_thread_release_ext_buffer(h->avctx, &pic->tf); | |
45 | 48418 | ff_thread_release_buffer(h->avctx, pic->f_grain); | |
46 | 48418 | av_buffer_unref(&pic->hwaccel_priv_buf); | |
47 | |||
48 | 48418 | av_buffer_unref(&pic->qscale_table_buf); | |
49 | 48418 | av_buffer_unref(&pic->mb_type_buf); | |
50 | 48418 | av_buffer_unref(&pic->pps_buf); | |
51 |
2/2✓ Branch 0 taken 96836 times.
✓ Branch 1 taken 48418 times.
|
145254 | for (i = 0; i < 2; i++) { |
52 | 96836 | av_buffer_unref(&pic->motion_val_buf[i]); | |
53 | 96836 | av_buffer_unref(&pic->ref_index_buf[i]); | |
54 | } | ||
55 | |||
56 | 48418 | memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); | |
57 | } | ||
58 | |||
59 | 24930 | static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src) | |
60 | { | ||
61 | 24930 | dst->qscale_table = src->qscale_table; | |
62 | 24930 | dst->mb_type = src->mb_type; | |
63 | 24930 | dst->pps = src->pps; | |
64 | |||
65 |
2/2✓ Branch 0 taken 49860 times.
✓ Branch 1 taken 24930 times.
|
74790 | for (int i = 0; i < 2; i++) { |
66 | 49860 | dst->motion_val[i] = src->motion_val[i]; | |
67 | 49860 | dst->ref_index[i] = src->ref_index[i]; | |
68 | } | ||
69 | |||
70 |
2/2✓ Branch 0 taken 49860 times.
✓ Branch 1 taken 24930 times.
|
74790 | for (int i = 0; i < 2; i++) |
71 | 49860 | dst->field_poc[i] = src->field_poc[i]; | |
72 | |||
73 | 24930 | memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc)); | |
74 | 24930 | memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count)); | |
75 | |||
76 | 24930 | dst->poc = src->poc; | |
77 | 24930 | dst->frame_num = src->frame_num; | |
78 | 24930 | dst->mmco_reset = src->mmco_reset; | |
79 | 24930 | dst->long_ref = src->long_ref; | |
80 | 24930 | dst->mbaff = src->mbaff; | |
81 | 24930 | dst->field_picture = src->field_picture; | |
82 | 24930 | dst->reference = src->reference; | |
83 | 24930 | dst->recovered = src->recovered; | |
84 | 24930 | dst->invalid_gap = src->invalid_gap; | |
85 | 24930 | dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt; | |
86 | 24930 | dst->mb_width = src->mb_width; | |
87 | 24930 | dst->mb_height = src->mb_height; | |
88 | 24930 | dst->mb_stride = src->mb_stride; | |
89 | 24930 | dst->needs_fg = src->needs_fg; | |
90 | 24930 | } | |
91 | |||
92 | 24710 | int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src) | |
93 | { | ||
94 | int ret, i; | ||
95 | |||
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24710 times.
|
24710 | av_assert0(!dst->f->buf[0]); |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24710 times.
|
24710 | av_assert0(src->f->buf[0]); |
98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24710 times.
|
24710 | av_assert0(src->tf.f == src->f); |
99 | |||
100 | 24710 | dst->tf.f = dst->f; | |
101 | 24710 | ret = ff_thread_ref_frame(&dst->tf, &src->tf); | |
102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24710 times.
|
24710 | if (ret < 0) |
103 | ✗ | goto fail; | |
104 | |||
105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24710 times.
|
24710 | if (src->needs_fg) { |
106 | ✗ | ret = av_frame_ref(dst->f_grain, src->f_grain); | |
107 | ✗ | if (ret < 0) | |
108 | ✗ | goto fail; | |
109 | } | ||
110 | |||
111 | 24710 | dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf); | |
112 | 24710 | dst->mb_type_buf = av_buffer_ref(src->mb_type_buf); | |
113 | 24710 | dst->pps_buf = av_buffer_ref(src->pps_buf); | |
114 |
3/6✓ Branch 0 taken 24710 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24710 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24710 times.
|
24710 | if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf) { |
115 | ✗ | ret = AVERROR(ENOMEM); | |
116 | ✗ | goto fail; | |
117 | } | ||
118 | |||
119 |
2/2✓ Branch 0 taken 49420 times.
✓ Branch 1 taken 24710 times.
|
74130 | for (i = 0; i < 2; i++) { |
120 | 49420 | dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]); | |
121 | 49420 | dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]); | |
122 |
2/4✓ Branch 0 taken 49420 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49420 times.
|
49420 | if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) { |
123 | ✗ | ret = AVERROR(ENOMEM); | |
124 | ✗ | goto fail; | |
125 | } | ||
126 | } | ||
127 | |||
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24710 times.
|
24710 | if (src->hwaccel_picture_private) { |
129 | ✗ | dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf); | |
130 | ✗ | if (!dst->hwaccel_priv_buf) { | |
131 | ✗ | ret = AVERROR(ENOMEM); | |
132 | ✗ | goto fail; | |
133 | } | ||
134 | ✗ | dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data; | |
135 | } | ||
136 | |||
137 | 24710 | h264_copy_picture_params(dst, src); | |
138 | |||
139 | 24710 | return 0; | |
140 | ✗ | fail: | |
141 | ✗ | ff_h264_unref_picture(h, dst); | |
142 | ✗ | return ret; | |
143 | } | ||
144 | |||
145 | 1554 | int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture *src) | |
146 | { | ||
147 | int ret, i; | ||
148 | |||
149 |
3/4✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1334 times.
✓ Branch 3 taken 220 times.
|
1554 | if (!src->f || !src->f->buf[0]) { |
150 | 1334 | ff_h264_unref_picture(h, dst); | |
151 | 1334 | return 0; | |
152 | } | ||
153 | |||
154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
|
220 | av_assert0(src->tf.f == src->f); |
155 | |||
156 | 220 | dst->tf.f = dst->f; | |
157 | 220 | ff_thread_release_ext_buffer(h->avctx, &dst->tf); | |
158 | 220 | ret = ff_thread_ref_frame(&dst->tf, &src->tf); | |
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
|
220 | if (ret < 0) |
160 | ✗ | goto fail; | |
161 | |||
162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
|
220 | if (src->needs_fg) { |
163 | ✗ | ff_thread_release_buffer(h->avctx, dst->f_grain); | |
164 | ✗ | ret = av_frame_ref(dst->f_grain, src->f_grain); | |
165 | ✗ | if (ret < 0) | |
166 | ✗ | goto fail; | |
167 | } | ||
168 | |||
169 | 220 | ret = av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf); | |
170 | 220 | ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf); | |
171 | 220 | ret |= av_buffer_replace(&dst->pps_buf, src->pps_buf); | |
172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
|
220 | if (ret < 0) |
173 | ✗ | goto fail; | |
174 | |||
175 |
2/2✓ Branch 0 taken 440 times.
✓ Branch 1 taken 220 times.
|
660 | for (i = 0; i < 2; i++) { |
176 | 440 | ret = av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]); | |
177 | 440 | ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]); | |
178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440 times.
|
440 | if (ret < 0) |
179 | ✗ | goto fail; | |
180 | } | ||
181 | |||
182 | 220 | ret = av_buffer_replace(&dst->hwaccel_priv_buf, src->hwaccel_priv_buf); | |
183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
|
220 | if (ret < 0) |
184 | ✗ | goto fail; | |
185 | |||
186 | 220 | dst->hwaccel_picture_private = src->hwaccel_picture_private; | |
187 | |||
188 | 220 | h264_copy_picture_params(dst, src); | |
189 | |||
190 | 220 | return 0; | |
191 | ✗ | fail: | |
192 | ✗ | ff_h264_unref_picture(h, dst); | |
193 | ✗ | return ret; | |
194 | } | ||
195 | |||
196 | 125937 | void ff_h264_set_erpic(ERPicture *dst, H264Picture *src) | |
197 | { | ||
198 | #if CONFIG_ERROR_RESILIENCE | ||
199 | int i; | ||
200 | |||
201 | 125937 | memset(dst, 0, sizeof(*dst)); | |
202 | |||
203 |
2/2✓ Branch 0 taken 81163 times.
✓ Branch 1 taken 44774 times.
|
125937 | if (!src) |
204 | 81163 | return; | |
205 | |||
206 | 44774 | dst->f = src->f; | |
207 | 44774 | dst->tf = &src->tf; | |
208 | |||
209 |
2/2✓ Branch 0 taken 89548 times.
✓ Branch 1 taken 44774 times.
|
134322 | for (i = 0; i < 2; i++) { |
210 | 89548 | dst->motion_val[i] = src->motion_val[i]; | |
211 | 89548 | dst->ref_index[i] = src->ref_index[i]; | |
212 | } | ||
213 | |||
214 | 44774 | dst->mb_type = src->mb_type; | |
215 | 44774 | dst->field_picture = src->field_picture; | |
216 | #endif /* CONFIG_ERROR_RESILIENCE */ | ||
217 | } | ||
218 | |||
219 | 26296 | int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup) | |
220 | { | ||
221 | 26296 | AVCodecContext *const avctx = h->avctx; | |
222 | 26296 | H264Picture *cur = h->cur_pic_ptr; | |
223 | 26296 | int err = 0; | |
224 | 26296 | h->mb_y = 0; | |
225 | |||
226 |
4/4✓ Branch 0 taken 26103 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 26071 times.
✓ Branch 3 taken 32 times.
|
26296 | if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) { |
227 |
2/2✓ Branch 0 taken 17091 times.
✓ Branch 1 taken 9173 times.
|
26264 | if (!h->droppable) { |
228 | 17091 | err = ff_h264_execute_ref_pic_marking(h); | |
229 | 17091 | h->poc.prev_poc_msb = h->poc.poc_msb; | |
230 | 17091 | h->poc.prev_poc_lsb = h->poc.poc_lsb; | |
231 | } | ||
232 | 26264 | h->poc.prev_frame_num_offset = h->poc.frame_num_offset; | |
233 | 26264 | h->poc.prev_frame_num = h->poc.frame_num; | |
234 | } | ||
235 | |||
236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26296 times.
|
26296 | if (avctx->hwaccel) { |
237 | ✗ | err = avctx->hwaccel->end_frame(avctx); | |
238 | ✗ | if (err < 0) | |
239 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
240 | "hardware accelerator failed to decode picture\n"); | ||
241 |
3/8✓ Branch 0 taken 26103 times.
✓ Branch 1 taken 193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26103 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
26296 | } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) { |
242 | ✗ | AVFrameSideData *sd = av_frame_get_side_data(cur->f, AV_FRAME_DATA_FILM_GRAIN_PARAMS); | |
243 | |||
244 | ✗ | err = AVERROR_INVALIDDATA; | |
245 | ✗ | if (sd) // a decoding error may have happened before the side data could be allocated | |
246 | ✗ | err = ff_h274_apply_film_grain(cur->f_grain, cur->f, &h->h274db, | |
247 | ✗ | (AVFilmGrainParams *) sd->data); | |
248 | ✗ | if (err < 0) { | |
249 | ✗ | av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film " | |
250 | ✗ | "grain, ignoring: %s\n", av_err2str(err)); | |
251 | ✗ | cur->needs_fg = 0; | |
252 | ✗ | err = 0; | |
253 | } | ||
254 | } | ||
255 | |||
256 |
4/4✓ Branch 0 taken 26103 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 17041 times.
✓ Branch 3 taken 9062 times.
|
26296 | if (!in_setup && !h->droppable) |
257 | 17041 | ff_thread_report_progress(&cur->tf, INT_MAX, | |
258 | 17041 | h->picture_structure == PICT_BOTTOM_FIELD); | |
259 | 26296 | emms_c(); | |
260 | |||
261 | 26296 | h->current_slice = 0; | |
262 | |||
263 | 26296 | return err; | |
264 | } | ||
265 |