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 int extradata_pic_init_qp;
39 } H264RedundantPPSContext;
42 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
45 // Record the current value of pic_init_qp in order to fix up
46 // following slices, then overwrite with the global value.
47 ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
48 pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
50 // Some PPSs have this set, so it must be set in all of them.
51 // (Slices which do not use such a PPS on input will still have
52 // *_weight_l*flag as zero and therefore write equivalently.)
53 pps->weighted_pred_flag = 1;
58 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
59 H264RawSliceHeader *slice)
63 qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
64 slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
69 static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out)
71 H264RedundantPPSContext *ctx = bsf->priv_data;
73 CodedBitstreamFragment *au = &ctx->access_unit;
77 err = ff_bsf_get_packet(bsf, &in);
81 err = ff_cbs_read_packet(ctx->input, au, in);
86 for (i = 0; i < au->nb_units; i++) {
87 CodedBitstreamUnit *nal = &au->units[i];
89 if (nal->type == H264_NAL_SPS)
91 if (nal->type == H264_NAL_PPS) {
92 err = h264_redundant_pps_fixup_pps(ctx, nal->content);
96 av_log(bsf, AV_LOG_VERBOSE, "Deleting redundant PPS "
97 "at %"PRId64".\n", in->pts);
98 err = ff_cbs_delete_unit(ctx->input, au, i);
103 if (nal->type == H264_NAL_SLICE ||
104 nal->type == H264_NAL_IDR_SLICE) {
105 H264RawSlice *slice = nal->content;
106 h264_redundant_pps_fixup_slice(ctx, &slice->header);
110 err = ff_cbs_write_packet(ctx->output, out, au);
115 err = av_packet_copy_props(out, in);
121 ff_cbs_fragment_uninit(ctx->output, au);
124 av_packet_unref(out);
129 static int h264_redundant_pps_init(AVBSFContext *bsf)
131 H264RedundantPPSContext *ctx = bsf->priv_data;
132 CodedBitstreamFragment *au = &ctx->access_unit;
135 err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
139 err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
143 ctx->global_pic_init_qp = 26;
145 if (bsf->par_in->extradata) {
146 err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
148 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
152 for (i = 0; i < au->nb_units; i++) {
153 if (au->units[i].type == H264_NAL_PPS) {
154 err = h264_redundant_pps_fixup_pps(ctx, au->units[i].content);
160 ctx->extradata_pic_init_qp = ctx->current_pic_init_qp;
161 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
163 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
170 ff_cbs_fragment_uninit(ctx->output, au);
174 static void h264_redundant_pps_flush(AVBSFContext *bsf)
176 H264RedundantPPSContext *ctx = bsf->priv_data;
177 ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
180 static void h264_redundant_pps_close(AVBSFContext *bsf)
182 H264RedundantPPSContext *ctx = bsf->priv_data;
183 ff_cbs_close(&ctx->input);
184 ff_cbs_close(&ctx->output);
187 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
188 AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
191 const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
192 .name = "h264_redundant_pps",
193 .priv_data_size = sizeof(H264RedundantPPSContext),
194 .init = &h264_redundant_pps_init,
195 .flush = &h264_redundant_pps_flush,
196 .close = &h264_redundant_pps_close,
197 .filter = &h264_redundant_pps_filter,
198 .codec_ids = h264_redundant_pps_codec_ids,