GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/h264_redundant_pps_bsf.c Lines: 0 41 0.0 %
Date: 2021-04-20 15:25:36 Branches: 0 16 0.0 %

Line Branch Exec Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include <string.h>
20
21
#include "libavutil/common.h"
22
#include "libavutil/mem.h"
23
24
#include "bsf.h"
25
#include "bsf_internal.h"
26
#include "cbs.h"
27
#include "cbs_bsf.h"
28
#include "cbs_h264.h"
29
#include "h264.h"
30
31
32
typedef struct H264RedundantPPSContext {
33
    CBSBSFContext common;
34
35
    int global_pic_init_qp;
36
    int current_pic_init_qp;
37
    int extradata_pic_init_qp;
38
} H264RedundantPPSContext;
39
40
41
static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
42
                                        CodedBitstreamUnit *unit)
43
{
44
    H264RawPPS *pps;
45
    int err;
46
47
    // The changes we are about to perform affect the parsing process,
48
    // so we must make sure that the PPS is writable, otherwise the
49
    // parsing of future slices will be incorrect and even raise errors.
50
    err = ff_cbs_make_unit_writable(ctx->common.input, unit);
51
    if (err < 0)
52
        return err;
53
    pps = unit->content;
54
55
    // Record the current value of pic_init_qp in order to fix up
56
    // following slices, then overwrite with the global value.
57
    ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
58
    pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
59
60
    // Some PPSs have this set, so it must be set in all of them.
61
    // (Slices which do not use such a PPS on input will still have
62
    // *_weight_l*flag as zero and therefore write equivalently.)
63
    pps->weighted_pred_flag = 1;
64
65
    return 0;
66
}
67
68
static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
69
                                          H264RawSliceHeader *slice)
70
{
71
    int qp;
72
73
    qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
74
    slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
75
76
    return 0;
77
}
78
79
static int h264_redundant_pps_update_fragment(AVBSFContext *bsf,
80
                                              AVPacket *pkt,
81
                                              CodedBitstreamFragment *au)
82
{
83
    H264RedundantPPSContext *ctx = bsf->priv_data;
84
    int au_has_sps;
85
    int err, i;
86
87
    au_has_sps = 0;
88
    for (i = 0; i < au->nb_units; i++) {
89
        CodedBitstreamUnit *nal = &au->units[i];
90
91
        if (nal->type == H264_NAL_SPS)
92
            au_has_sps = 1;
93
        if (nal->type == H264_NAL_PPS) {
94
            err = h264_redundant_pps_fixup_pps(ctx, nal);
95
            if (err < 0)
96
                return err;
97
            if (!au_has_sps) {
98
                av_log(bsf, AV_LOG_VERBOSE, "Deleting redundant PPS "
99
                       "at %"PRId64".\n", pkt->pts);
100
                ff_cbs_delete_unit(au, i);
101
                i--;
102
                continue;
103
            }
104
        }
105
        if (nal->type == H264_NAL_SLICE ||
106
            nal->type == H264_NAL_IDR_SLICE) {
107
            H264RawSlice *slice = nal->content;
108
            h264_redundant_pps_fixup_slice(ctx, &slice->header);
109
        }
110
    }
111
112
    return 0;
113
}
114
115
static void h264_redundant_pps_flush(AVBSFContext *bsf)
116
{
117
    H264RedundantPPSContext *ctx = bsf->priv_data;
118
    ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
119
}
120
121
static const CBSBSFType h264_redundant_pps_type = {
122
    .codec_id        = AV_CODEC_ID_H264,
123
    .fragment_name   = "access unit",
124
    .unit_name       = "NAL unit",
125
    .update_fragment = &h264_redundant_pps_update_fragment,
126
};
127
128
static int h264_redundant_pps_init(AVBSFContext *bsf)
129
{
130
    H264RedundantPPSContext *ctx = bsf->priv_data;
131
132
    ctx->global_pic_init_qp = 26;
133
134
    return ff_cbs_bsf_generic_init(bsf, &h264_redundant_pps_type);
135
}
136
137
static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
138
    AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
139
};
140
141
const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
142
    .name           = "h264_redundant_pps",
143
    .priv_data_size = sizeof(H264RedundantPPSContext),
144
    .init           = &h264_redundant_pps_init,
145
    .flush          = &h264_redundant_pps_flush,
146
    .close          = &ff_cbs_bsf_generic_close,
147
    .filter         = &ff_cbs_bsf_generic_filter,
148
    .codec_ids      = h264_redundant_pps_codec_ids,
149
};