]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_redundant_pps_bsf.c
Merge commit 'c7ab6aff66cba2f265f656ce8d56aa428d4ada76'
[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 } H264RedundantPPSContext;
39
40
41 static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
42                                         H264RawPPS *pps)
43 {
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;
48
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;
53
54     return 0;
55 }
56
57 static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
58                                           H264RawSliceHeader *slice)
59 {
60     int qp;
61
62     qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
63     slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
64
65     return 0;
66 }
67
68 static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out)
69 {
70     H264RedundantPPSContext *ctx = bsf->priv_data;
71     AVPacket *in;
72     CodedBitstreamFragment *au = &ctx->access_unit;
73     int au_has_sps;
74     int err, i;
75
76     err = ff_bsf_get_packet(bsf, &in);
77     if (err < 0)
78         return err;
79
80     err = ff_cbs_read_packet(ctx->input, au, in);
81     if (err < 0)
82         return err;
83
84     au_has_sps = 0;
85     for (i = 0; i < au->nb_units; i++) {
86         CodedBitstreamUnit *nal = &au->units[i];
87
88         if (nal->type == H264_NAL_SPS)
89             au_has_sps = 1;
90         if (nal->type == H264_NAL_PPS) {
91             h264_redundant_pps_fixup_pps(ctx, nal->content);
92             if (!au_has_sps) {
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);
96             }
97         }
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);
102         }
103     }
104
105     err = ff_cbs_write_packet(ctx->output, out, au);
106     if (err < 0)
107         return err;
108
109     ff_cbs_fragment_uninit(ctx->output, au);
110
111     err = av_packet_copy_props(out, in);
112     if (err < 0)
113         return err;
114
115     av_packet_free(&in);
116
117     return 0;
118 }
119
120 static int h264_redundant_pps_init(AVBSFContext *bsf)
121 {
122     H264RedundantPPSContext *ctx = bsf->priv_data;
123     CodedBitstreamFragment *au = &ctx->access_unit;
124     int err, i;
125
126     err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
127     if (err < 0)
128         return err;
129
130     err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
131     if (err < 0)
132         return err;
133
134     ctx->global_pic_init_qp = 26;
135
136     if (bsf->par_in->extradata) {
137         err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
138         if (err < 0) {
139             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
140             return err;
141         }
142
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);
146         }
147
148         err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
149         if (err < 0) {
150             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
151             return err;
152         }
153
154         ff_cbs_fragment_uninit(ctx->output, au);
155     }
156
157     return 0;
158 }
159
160 static void h264_redundant_pps_close(AVBSFContext *bsf)
161 {
162     H264RedundantPPSContext *ctx = bsf->priv_data;
163     ff_cbs_close(&ctx->input);
164     ff_cbs_close(&ctx->output);
165 }
166
167 static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
168     AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
169 };
170
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,
178 };