]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_redundant_pps_bsf.c
avformat/argo_brp: support MASK streams
[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 "bsf_internal.h"
26 #include "cbs.h"
27 #include "cbs_h264.h"
28 #include "h264.h"
29
30
31 typedef struct H264RedundantPPSContext {
32     CodedBitstreamContext *input;
33     CodedBitstreamContext *output;
34
35     CodedBitstreamFragment access_unit;
36
37     int global_pic_init_qp;
38     int current_pic_init_qp;
39     int extradata_pic_init_qp;
40 } H264RedundantPPSContext;
41
42
43 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
44                                         CodedBitstreamUnit *unit)
45 {
46     H264RawPPS *pps;
47     int err;
48
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);
53     if (err < 0)
54         return err;
55     pps = unit->content;
56
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;
61
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;
66
67     return 0;
68 }
69
70 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
71                                           H264RawSliceHeader *slice)
72 {
73     int qp;
74
75     qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
76     slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
77
78     return 0;
79 }
80
81 static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *pkt)
82 {
83     H264RedundantPPSContext *ctx = bsf->priv_data;
84     CodedBitstreamFragment *au = &ctx->access_unit;
85     int au_has_sps;
86     int err, i;
87
88     err = ff_bsf_get_packet_ref(bsf, pkt);
89     if (err < 0)
90         return err;
91
92     err = ff_cbs_read_packet(ctx->input, au, pkt);
93     if (err < 0)
94         goto fail;
95
96     au_has_sps = 0;
97     for (i = 0; i < au->nb_units; i++) {
98         CodedBitstreamUnit *nal = &au->units[i];
99
100         if (nal->type == H264_NAL_SPS)
101             au_has_sps = 1;
102         if (nal->type == H264_NAL_PPS) {
103             err = h264_redundant_pps_fixup_pps(ctx, nal);
104             if (err < 0)
105                 goto fail;
106             if (!au_has_sps) {
107                 av_log(bsf, AV_LOG_VERBOSE, "Deleting redundant PPS "
108                        "at %"PRId64".\n", pkt->pts);
109                 ff_cbs_delete_unit(au, i);
110                 i--;
111                 continue;
112             }
113         }
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);
118         }
119     }
120
121     err = ff_cbs_write_packet(ctx->output, pkt, au);
122     if (err < 0)
123         goto fail;
124
125     err = 0;
126 fail:
127     ff_cbs_fragment_reset(au);
128     if (err < 0)
129         av_packet_unref(pkt);
130
131     return err;
132 }
133
134 static int h264_redundant_pps_init(AVBSFContext *bsf)
135 {
136     H264RedundantPPSContext *ctx = bsf->priv_data;
137     CodedBitstreamFragment *au = &ctx->access_unit;
138     int err, i;
139
140     err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
141     if (err < 0)
142         return err;
143
144     err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
145     if (err < 0)
146         return err;
147
148     ctx->global_pic_init_qp = 26;
149
150     if (bsf->par_in->extradata) {
151         err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
152         if (err < 0) {
153             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
154             goto fail;
155         }
156
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]);
160                 if (err < 0)
161                     goto fail;
162             }
163         }
164
165         ctx->extradata_pic_init_qp = ctx->current_pic_init_qp;
166         err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
167         if (err < 0) {
168             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
169             goto fail;
170         }
171     }
172
173     err = 0;
174 fail:
175     ff_cbs_fragment_reset(au);
176     return err;
177 }
178
179 static void h264_redundant_pps_flush(AVBSFContext *bsf)
180 {
181     H264RedundantPPSContext *ctx = bsf->priv_data;
182     ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
183 }
184
185 static void h264_redundant_pps_close(AVBSFContext *bsf)
186 {
187     H264RedundantPPSContext *ctx = bsf->priv_data;
188
189     ff_cbs_fragment_free(&ctx->access_unit);
190     ff_cbs_close(&ctx->input);
191     ff_cbs_close(&ctx->output);
192 }
193
194 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
195     AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
196 };
197
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,
206 };