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"
25 #include "bsf_internal.h"
31 typedef struct H264RedundantPPSContext {
32 CodedBitstreamContext *input;
33 CodedBitstreamContext *output;
35 CodedBitstreamFragment access_unit;
37 int global_pic_init_qp;
38 int current_pic_init_qp;
39 int extradata_pic_init_qp;
40 } H264RedundantPPSContext;
43 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
44 CodedBitstreamUnit *unit)
49 // The changes we are about to perform affect the parsing process,
50 // so we must make sure that the PPS is writable, otherwise the
51 // parsing of future slices will be incorrect and even raise errors.
52 err = ff_cbs_make_unit_writable(ctx->input, unit);
57 // Record the current value of pic_init_qp in order to fix up
58 // following slices, then overwrite with the global value.
59 ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
60 pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
62 // Some PPSs have this set, so it must be set in all of them.
63 // (Slices which do not use such a PPS on input will still have
64 // *_weight_l*flag as zero and therefore write equivalently.)
65 pps->weighted_pred_flag = 1;
70 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
71 H264RawSliceHeader *slice)
75 qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
76 slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
81 static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *pkt)
83 H264RedundantPPSContext *ctx = bsf->priv_data;
84 CodedBitstreamFragment *au = &ctx->access_unit;
88 err = ff_bsf_get_packet_ref(bsf, pkt);
92 err = ff_cbs_read_packet(ctx->input, au, pkt);
97 for (i = 0; i < au->nb_units; i++) {
98 CodedBitstreamUnit *nal = &au->units[i];
100 if (nal->type == H264_NAL_SPS)
102 if (nal->type == H264_NAL_PPS) {
103 err = h264_redundant_pps_fixup_pps(ctx, nal);
107 av_log(bsf, AV_LOG_VERBOSE, "Deleting redundant PPS "
108 "at %"PRId64".\n", pkt->pts);
109 ff_cbs_delete_unit(au, i);
114 if (nal->type == H264_NAL_SLICE ||
115 nal->type == H264_NAL_IDR_SLICE) {
116 H264RawSlice *slice = nal->content;
117 h264_redundant_pps_fixup_slice(ctx, &slice->header);
121 err = ff_cbs_write_packet(ctx->output, pkt, au);
127 ff_cbs_fragment_reset(au);
129 av_packet_unref(pkt);
134 static int h264_redundant_pps_init(AVBSFContext *bsf)
136 H264RedundantPPSContext *ctx = bsf->priv_data;
137 CodedBitstreamFragment *au = &ctx->access_unit;
140 err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
144 err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
148 ctx->global_pic_init_qp = 26;
150 if (bsf->par_in->extradata) {
151 err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
153 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
157 for (i = 0; i < au->nb_units; i++) {
158 if (au->units[i].type == H264_NAL_PPS) {
159 err = h264_redundant_pps_fixup_pps(ctx, &au->units[i]);
165 ctx->extradata_pic_init_qp = ctx->current_pic_init_qp;
166 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
168 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
175 ff_cbs_fragment_reset(au);
179 static void h264_redundant_pps_flush(AVBSFContext *bsf)
181 H264RedundantPPSContext *ctx = bsf->priv_data;
182 ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
185 static void h264_redundant_pps_close(AVBSFContext *bsf)
187 H264RedundantPPSContext *ctx = bsf->priv_data;
189 ff_cbs_fragment_free(&ctx->access_unit);
190 ff_cbs_close(&ctx->input);
191 ff_cbs_close(&ctx->output);
194 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
195 AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
198 const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
199 .name = "h264_redundant_pps",
200 .priv_data_size = sizeof(H264RedundantPPSContext),
201 .init = &h264_redundant_pps_init,
202 .flush = &h264_redundant_pps_flush,
203 .close = &h264_redundant_pps_close,
204 .filter = &h264_redundant_pps_filter,
205 .codec_ids = h264_redundant_pps_codec_ids,