]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_redundant_pps_bsf.c
Merge commit 'dad5fd59f3d6a8311365314cfcde0ebcd15c2b01'
[ffmpeg] / libavcodec / h264_redundant_pps_bsf.c
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 "cbs.h"
26 #include "cbs_h264.h"
27 #include "h264.h"
28
29
30 typedef struct H264RedundantPPSContext {
31     CodedBitstreamContext *input;
32     CodedBitstreamContext *output;
33
34     CodedBitstreamFragment access_unit;
35
36     int global_pic_init_qp;
37     int current_pic_init_qp;
38     int extradata_pic_init_qp;
39 } H264RedundantPPSContext;
40
41
42 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
43                                         H264RawPPS *pps)
44 {
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;
49
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;
54
55     return 0;
56 }
57
58 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
59                                           H264RawSliceHeader *slice)
60 {
61     int qp;
62
63     qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
64     slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
65
66     return 0;
67 }
68
69 static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out)
70 {
71     H264RedundantPPSContext *ctx = bsf->priv_data;
72     AVPacket *in;
73     CodedBitstreamFragment *au = &ctx->access_unit;
74     int au_has_sps;
75     int err, i;
76
77     err = ff_bsf_get_packet(bsf, &in);
78     if (err < 0)
79         return err;
80
81     err = ff_cbs_read_packet(ctx->input, au, in);
82     if (err < 0)
83         goto fail;
84
85     au_has_sps = 0;
86     for (i = 0; i < au->nb_units; i++) {
87         CodedBitstreamUnit *nal = &au->units[i];
88
89         if (nal->type == H264_NAL_SPS)
90             au_has_sps = 1;
91         if (nal->type == H264_NAL_PPS) {
92             err = h264_redundant_pps_fixup_pps(ctx, nal->content);
93             if (err < 0)
94                 goto fail;
95             if (!au_has_sps) {
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);
99                 if (err < 0)
100                     goto fail;
101             }
102         }
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);
107         }
108     }
109
110     err = ff_cbs_write_packet(ctx->output, out, au);
111     if (err < 0)
112         goto fail;
113
114
115     err = av_packet_copy_props(out, in);
116     if (err < 0)
117         goto fail;
118
119     err = 0;
120 fail:
121     ff_cbs_fragment_reset(ctx->output, au);
122     av_packet_free(&in);
123     if (err < 0)
124         av_packet_unref(out);
125
126     return err;
127 }
128
129 static int h264_redundant_pps_init(AVBSFContext *bsf)
130 {
131     H264RedundantPPSContext *ctx = bsf->priv_data;
132     CodedBitstreamFragment *au = &ctx->access_unit;
133     int err, i;
134
135     err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
136     if (err < 0)
137         return err;
138
139     err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
140     if (err < 0)
141         return err;
142
143     ctx->global_pic_init_qp = 26;
144
145     if (bsf->par_in->extradata) {
146         err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
147         if (err < 0) {
148             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
149             goto fail;
150         }
151
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);
155                 if (err < 0)
156                     goto fail;
157             }
158         }
159
160         ctx->extradata_pic_init_qp = ctx->current_pic_init_qp;
161         err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
162         if (err < 0) {
163             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
164             goto fail;
165         }
166     }
167
168     err = 0;
169 fail:
170     ff_cbs_fragment_reset(ctx->output, au);
171     return err;
172 }
173
174 static void h264_redundant_pps_flush(AVBSFContext *bsf)
175 {
176     H264RedundantPPSContext *ctx = bsf->priv_data;
177     ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
178 }
179
180 static void h264_redundant_pps_close(AVBSFContext *bsf)
181 {
182     H264RedundantPPSContext *ctx = bsf->priv_data;
183
184     ff_cbs_fragment_free(ctx->input, &ctx->access_unit);
185     ff_cbs_close(&ctx->input);
186     ff_cbs_close(&ctx->output);
187 }
188
189 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
190     AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
191 };
192
193 const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
194     .name           = "h264_redundant_pps",
195     .priv_data_size = sizeof(H264RedundantPPSContext),
196     .init           = &h264_redundant_pps_init,
197     .flush          = &h264_redundant_pps_flush,
198     .close          = &h264_redundant_pps_close,
199     .filter         = &h264_redundant_pps_filter,
200     .codec_ids      = h264_redundant_pps_codec_ids,
201 };