FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_redundant_pps_bsf.c
Date: 2022-01-27 16:04:32
Exec Total Coverage
Lines: 0 36 0.0%
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
23 #include "bsf.h"
24 #include "bsf_internal.h"
25 #include "cbs.h"
26 #include "cbs_bsf.h"
27 #include "cbs_h264.h"
28 #include "h264.h"
29
30 #define NEW_GLOBAL_PIC_INIT_QP 26
31
32 typedef struct H264RedundantPPSContext {
33 CBSBSFContext common;
34 } H264RedundantPPSContext;
35
36
37 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
38 CodedBitstreamUnit *unit)
39 {
40 H264RawPPS *pps;
41 int err;
42
43 // The changes we are about to perform affect the parsing process,
44 // so we must make sure that the PPS is writable, otherwise the
45 // parsing of future slices will be incorrect and even raise errors.
46 err = ff_cbs_make_unit_writable(ctx->common.input, unit);
47 if (err < 0)
48 return err;
49 pps = unit->content;
50
51 // Overwrite pic_init_qp with the global value.
52 pps->pic_init_qp_minus26 = NEW_GLOBAL_PIC_INIT_QP - 26;
53
54 // Some PPSs have this set, so it must be set in all of them.
55 // (Slices which do not use such a PPS on input will still have
56 // *_weight_l*flag as zero and therefore write equivalently.)
57 pps->weighted_pred_flag = 1;
58
59 return 0;
60 }
61
62 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
63 H264RawSliceHeader *slice)
64 {
65 const CodedBitstreamH264Context *const in = ctx->common.input->priv_data;
66 const H264RawPPS *const pps = in->pps[slice->pic_parameter_set_id];
67
68 // We modified the PPS's qp value, now offset this by applying
69 // the negative offset to the slices.
70 slice->slice_qp_delta += pps->pic_init_qp_minus26
71 - (NEW_GLOBAL_PIC_INIT_QP - 26);
72
73 return 0;
74 }
75
76 static int h264_redundant_pps_update_fragment(AVBSFContext *bsf,
77 AVPacket *pkt,
78 CodedBitstreamFragment *au)
79 {
80 H264RedundantPPSContext *ctx = bsf->priv_data;
81 int au_has_sps;
82 int err, i;
83
84 au_has_sps = 0;
85 for (i = 0; i < au->nb_units; i++) {
86 CodedBitstreamUnit *nal = &au->units[i];
87
88 if (nal->type == H264_NAL_SPS)
89 au_has_sps = 1;
90 if (nal->type == H264_NAL_PPS) {
91 err = h264_redundant_pps_fixup_pps(ctx, nal);
92 if (err < 0)
93 return err;
94 if (!au_has_sps) {
95 av_log(bsf, AV_LOG_VERBOSE, "Deleting redundant PPS "
96 "at %"PRId64".\n", pkt->pts);
97 ff_cbs_delete_unit(au, i);
98 i--;
99 continue;
100 }
101 }
102 if (nal->type == H264_NAL_SLICE ||
103 nal->type == H264_NAL_IDR_SLICE) {
104 H264RawSlice *slice = nal->content;
105 h264_redundant_pps_fixup_slice(ctx, &slice->header);
106 }
107 }
108
109 return 0;
110 }
111
112 static const CBSBSFType h264_redundant_pps_type = {
113 .codec_id = AV_CODEC_ID_H264,
114 .fragment_name = "access unit",
115 .unit_name = "NAL unit",
116 .update_fragment = &h264_redundant_pps_update_fragment,
117 };
118
119 static int h264_redundant_pps_init(AVBSFContext *bsf)
120 {
121 return ff_cbs_bsf_generic_init(bsf, &h264_redundant_pps_type);
122 }
123
124 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
125 AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
126 };
127
128 const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
129 .name = "h264_redundant_pps",
130 .priv_data_size = sizeof(H264RedundantPPSContext),
131 .init = &h264_redundant_pps_init,
132 .close = &ff_cbs_bsf_generic_close,
133 .filter = &ff_cbs_bsf_generic_filter,
134 .codec_ids = h264_redundant_pps_codec_ids,
135 };
136