2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/common.h"
22 #include "libavutil/mem.h"
30 typedef struct H264RedundantPPSContext {
31 CodedBitstreamContext *input;
32 CodedBitstreamContext *output;
34 CodedBitstreamFragment access_unit;
36 int global_pic_init_qp;
37 int current_pic_init_qp;
38 } H264RedundantPPSContext;
41 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
44 // Record the current value of pic_init_qp in order to fix up
45 // following slices, then overwrite with the global value.
46 ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
47 pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
49 // Some PPSs have this set, so it must be set in all of them.
50 // (Slices which do not use such a PPS on input will still have
51 // *_weight_l*flag as zero and therefore write equivalently.)
52 pps->weighted_pred_flag = 1;
57 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
58 H264RawSliceHeader *slice)
62 qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
63 slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
68 static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out)
70 H264RedundantPPSContext *ctx = bsf->priv_data;
72 CodedBitstreamFragment *au = &ctx->access_unit;
76 err = ff_bsf_get_packet(bsf, &in);
80 err = ff_cbs_read_packet(ctx->input, au, in);
85 for (i = 0; i < au->nb_units; i++) {
86 CodedBitstreamUnit *nal = &au->units[i];
88 if (nal->type == H264_NAL_SPS)
90 if (nal->type == H264_NAL_PPS) {
91 h264_redundant_pps_fixup_pps(ctx, nal->content);
93 av_log(ctx, AV_LOG_VERBOSE, "Deleting redundant PPS "
94 "at %"PRId64".\n", in->pts);
95 ff_cbs_delete_unit(ctx->input, au, i);
98 if (nal->type == H264_NAL_SLICE ||
99 nal->type == H264_NAL_IDR_SLICE) {
100 H264RawSlice *slice = nal->content;
101 h264_redundant_pps_fixup_slice(ctx, &slice->header);
105 err = ff_cbs_write_packet(ctx->output, out, au);
109 ff_cbs_fragment_uninit(ctx->output, au);
111 err = av_packet_copy_props(out, in);
120 static int h264_redundant_pps_init(AVBSFContext *bsf)
122 H264RedundantPPSContext *ctx = bsf->priv_data;
123 CodedBitstreamFragment *au = &ctx->access_unit;
126 err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
130 err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
134 ctx->global_pic_init_qp = 26;
136 if (bsf->par_in->extradata) {
137 err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
139 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
143 for (i = 0; i < au->nb_units; i++) {
144 if (au->units[i].type == H264_NAL_PPS)
145 h264_redundant_pps_fixup_pps(ctx, au->units[i].content);
148 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
150 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
154 ff_cbs_fragment_uninit(ctx->output, au);
160 static void h264_redundant_pps_close(AVBSFContext *bsf)
162 H264RedundantPPSContext *ctx = bsf->priv_data;
163 ff_cbs_close(&ctx->input);
164 ff_cbs_close(&ctx->output);
167 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
168 AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
171 const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
172 .name = "h264_redundant_pps",
173 .priv_data_size = sizeof(H264RedundantPPSContext),
174 .init = &h264_redundant_pps_init,
175 .close = &h264_redundant_pps_close,
176 .filter = &h264_redundant_pps_filter,
177 .codec_ids = h264_redundant_pps_codec_ids,