Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/h264_picture.c
Line
Count
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
void ff_h264_unref_picture(H264Picture *pic)
40
44.5M
{
41
44.5M
    int off = offsetof(H264Picture, f_grain) + sizeof(pic->f_grain);
42
44.5M
    int i;
43
44
44.5M
    if (!pic->f || !pic->f->buf[0])
45
35.8M
        return;
46
47
8.61M
    ff_thread_release_ext_buffer(&pic->tf);
48
8.61M
    av_frame_unref(pic->f_grain);
49
8.61M
    av_refstruct_unref(&pic->hwaccel_picture_private);
50
51
8.61M
    av_refstruct_unref(&pic->qscale_table_base);
52
8.61M
    av_refstruct_unref(&pic->mb_type_base);
53
8.61M
    av_refstruct_unref(&pic->pps);
54
25.8M
    for (i = 0; i < 2; i++) {
55
17.2M
        av_refstruct_unref(&pic->motion_val_base[i]);
56
17.2M
        av_refstruct_unref(&pic->ref_index[i]);
57
17.2M
    }
58
8.61M
    av_refstruct_unref(&pic->decode_error_flags);
59
60
8.61M
    memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
61
8.61M
}
62
63
static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
64
4.62M
{
65
4.62M
    av_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
66
4.62M
    av_refstruct_replace(&dst->mb_type_base,      src->mb_type_base);
67
4.62M
    av_refstruct_replace(&dst->pps, src->pps);
68
69
13.8M
    for (int i = 0; i < 2; i++) {
70
9.24M
        av_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
71
9.24M
        av_refstruct_replace(&dst->ref_index[i],       src->ref_index[i]);
72
9.24M
    }
73
74
4.62M
    av_refstruct_replace(&dst->hwaccel_picture_private,
75
4.62M
                          src->hwaccel_picture_private);
76
77
4.62M
    av_refstruct_replace(&dst->decode_error_flags, src->decode_error_flags);
78
79
4.62M
    dst->qscale_table = src->qscale_table;
80
4.62M
    dst->mb_type      = src->mb_type;
81
82
13.8M
    for (int i = 0; i < 2; i++)
83
9.24M
        dst->motion_val[i] = src->motion_val[i];
84
85
13.8M
    for (int i = 0; i < 2; i++)
86
9.24M
        dst->field_poc[i] = src->field_poc[i];
87
88
4.62M
    memcpy(dst->ref_poc,   src->ref_poc,   sizeof(src->ref_poc));
89
4.62M
    memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
90
91
4.62M
    dst->poc           = src->poc;
92
4.62M
    dst->frame_num     = src->frame_num;
93
4.62M
    dst->mmco_reset    = src->mmco_reset;
94
4.62M
    dst->long_ref      = src->long_ref;
95
4.62M
    dst->mbaff         = src->mbaff;
96
4.62M
    dst->field_picture = src->field_picture;
97
4.62M
    dst->reference     = src->reference;
98
4.62M
    dst->recovered     = src->recovered;
99
4.62M
    dst->gray          = src->gray;
100
4.62M
    dst->invalid_gap   = src->invalid_gap;
101
4.62M
    dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
102
4.62M
    dst->mb_width      = src->mb_width;
103
4.62M
    dst->mb_height     = src->mb_height;
104
4.62M
    dst->mb_stride     = src->mb_stride;
105
4.62M
    dst->needs_fg      = src->needs_fg;
106
4.62M
}
107
108
int ff_h264_ref_picture(H264Picture *dst, const H264Picture *src)
109
4.62M
{
110
4.62M
    int ret;
111
112
4.62M
    av_assert0(!dst->f->buf[0]);
113
4.62M
    av_assert0(src->f->buf[0]);
114
4.62M
    av_assert0(src->tf.f == src->f);
115
116
4.62M
    dst->tf.f = dst->f;
117
4.62M
    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
118
4.62M
    if (ret < 0)
119
0
        goto fail;
120
121
4.62M
    if (src->needs_fg) {
122
19.4k
        ret = av_frame_ref(dst->f_grain, src->f_grain);
123
19.4k
        if (ret < 0)
124
0
            goto fail;
125
19.4k
    }
126
127
4.62M
    h264_copy_picture_params(dst, src);
128
129
4.62M
    return 0;
130
0
fail:
131
0
    ff_h264_unref_picture(dst);
132
0
    return ret;
133
4.62M
}
134
135
int ff_h264_replace_picture(H264Picture *dst, const H264Picture *src)
136
0
{
137
0
    int ret;
138
139
0
    if (!src->f || !src->f->buf[0]) {
140
0
        ff_h264_unref_picture(dst);
141
0
        return 0;
142
0
    }
143
144
0
    av_assert0(src->tf.f == src->f);
145
146
0
    dst->tf.f = dst->f;
147
0
    ret = ff_thread_replace_frame(&dst->tf, &src->tf);
148
0
    if (ret < 0)
149
0
        goto fail;
150
151
0
    if (src->needs_fg) {
152
0
        av_frame_unref(dst->f_grain);
153
0
        ret = av_frame_ref(dst->f_grain, src->f_grain);
154
0
        if (ret < 0)
155
0
            goto fail;
156
0
    }
157
158
0
    h264_copy_picture_params(dst, src);
159
160
0
    return 0;
161
0
fail:
162
0
    ff_h264_unref_picture(dst);
163
0
    return ret;
164
0
}
165
166
void ff_h264_set_erpic(ERPicture *dst, const H264Picture *src)
167
15.3M
{
168
15.3M
#if CONFIG_ERROR_RESILIENCE
169
15.3M
    int i;
170
171
15.3M
    memset(dst, 0, sizeof(*dst));
172
173
15.3M
    if (!src)
174
12.3M
        return;
175
176
2.99M
    dst->f = src->f;
177
2.99M
    dst->tf = &src->tf;
178
179
8.98M
    for (i = 0; i < 2; i++) {
180
5.98M
        dst->motion_val[i] = src->motion_val[i];
181
5.98M
        dst->ref_index[i] = src->ref_index[i];
182
5.98M
    }
183
184
2.99M
    dst->mb_type = src->mb_type;
185
2.99M
    dst->field_picture = src->field_picture;
186
2.99M
#endif /* CONFIG_ERROR_RESILIENCE */
187
2.99M
}
188
189
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
190
2.43M
{
191
2.43M
    AVCodecContext *const avctx = h->avctx;
192
2.43M
    H264Picture *cur = h->cur_pic_ptr;
193
2.43M
    int err = 0;
194
2.43M
    h->mb_y = 0;
195
196
2.43M
    if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
197
2.43M
        if (!h->droppable) {
198
1.48M
            err = ff_h264_execute_ref_pic_marking(h);
199
1.48M
            h->poc.prev_poc_msb = h->poc.poc_msb;
200
1.48M
            h->poc.prev_poc_lsb = h->poc.poc_lsb;
201
1.48M
        }
202
2.43M
        h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
203
2.43M
        h->poc.prev_frame_num        = h->poc.frame_num;
204
2.43M
    }
205
206
2.43M
    if (avctx->hwaccel) {
207
0
        err = FF_HW_SIMPLE_CALL(avctx, end_frame);
208
0
        if (err < 0)
209
0
            av_log(avctx, AV_LOG_ERROR,
210
0
                   "hardware accelerator failed to decode picture\n");
211
2.43M
    } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) {
212
2.60k
        const AVFrameSideData *sd = av_frame_get_side_data(cur->f, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
213
214
2.60k
        err = AVERROR_INVALIDDATA;
215
2.60k
        if (sd) // a decoding error may have happened before the side data could be allocated
216
2.51k
            err = ff_h274_apply_film_grain(cur->f_grain, cur->f,
217
2.51k
                                           (AVFilmGrainParams *) sd->data);
218
2.60k
        if (err < 0) {
219
1.50k
            av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film "
220
1.50k
                   "grain, ignoring: %s\n", av_err2str(err));
221
1.50k
            cur->needs_fg = 0;
222
1.50k
            err = 0;
223
1.50k
        }
224
2.60k
    }
225
226
2.43M
    if (!in_setup && !h->droppable)
227
882k
        ff_thread_report_progress(&cur->tf, INT_MAX,
228
882k
                                  h->picture_structure == PICT_BOTTOM_FIELD);
229
2.43M
    emms_c();
230
231
2.43M
    h->current_slice = 0;
232
233
2.43M
    return err;
234
2.43M
}