FFmpeg
h264_redundant_pps_bsf.c
Go to the documentation of this file.
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 {
34 
36 
41 
42 
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.
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 
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 
82 {
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;
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:
128  if (err < 0)
129  av_packet_unref(pkt);
130 
131  return err;
132 }
133 
135 {
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 
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:
176  return err;
177 }
178 
180 {
183 }
184 
186 {
188 
190  ff_cbs_close(&ctx->input);
191  ff_cbs_close(&ctx->output);
192 }
193 
196 };
197 
199  .name = "h264_redundant_pps",
200  .priv_data_size = sizeof(H264RedundantPPSContext),
203  .close = &h264_redundant_pps_close,
206 };
static int h264_redundant_pps_init(AVBSFContext *bsf)
int nb_units
Number of units in this fragment.
Definition: cbs.h:149
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:83
static void flush(AVCodecContext *avctx)
Memory handling functions.
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:397
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:74
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:70
The bitstream filter state.
Definition: bsf.h:49
static AVPacket pkt
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
const AVBitStreamFilter ff_h264_redundant_pps_bsf
CodedBitstreamContext * input
uint8_t weighted_pred_flag
Definition: cbs_h264.h:204
static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx, H264RawSliceHeader *slice)
int8_t slice_qp_delta
Definition: cbs_h264.h:417
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:238
const char * name
Definition: bsf.h:99
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
Coded bitstream unit structure.
Definition: cbs.h:66
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:103
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:164
#define av_log(a,...)
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:141
H.264 common definitions.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx, CodedBitstreamUnit *unit)
#define fail()
Definition: checkasm.h:123
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:372
AVFormatContext * ctx
Definition: movenc.c:48
CodedBitstreamContext * output
static void h264_redundant_pps_flush(AVBSFContext *bsf)
static void h264_redundant_pps_close(AVBSFContext *bsf)
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:118
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Context structure for coded bitstream operations.
Definition: cbs.h:170
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:790
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:220
CodedBitstreamFragment access_unit
common internal and external API header
static enum AVCodecID codec_ids[]
static enum AVCodecID h264_redundant_pps_codec_ids[]
H264RawSliceHeader header
Definition: cbs_h264.h:430
static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *pkt)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:155
int8_t pic_init_qp_minus26
Definition: cbs_h264.h:207
This structure stores compressed data.
Definition: packet.h:340
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
Definition: cbs.c:989
int i
Definition: input.c:407