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  H264RawPPS *pps)
45 {
46  // Record the current value of pic_init_qp in order to fix up
47  // following slices, then overwrite with the global value.
48  ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
49  pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
50 
51  // Some PPSs have this set, so it must be set in all of them.
52  // (Slices which do not use such a PPS on input will still have
53  // *_weight_l*flag as zero and therefore write equivalently.)
54  pps->weighted_pred_flag = 1;
55 
56  return 0;
57 }
58 
60  H264RawSliceHeader *slice)
61 {
62  int qp;
63 
64  qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
65  slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
66 
67  return 0;
68 }
69 
71 {
73  CodedBitstreamFragment *au = &ctx->access_unit;
74  int au_has_sps;
75  int err, i;
76 
77  err = ff_bsf_get_packet_ref(bsf, pkt);
78  if (err < 0)
79  return err;
80 
81  err = ff_cbs_read_packet(ctx->input, au, pkt);
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) {
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", pkt->pts);
98  ff_cbs_delete_unit(ctx->input, au, i);
99  i--;
100  continue;
101  }
102  }
103  if (nal->type == H264_NAL_SLICE ||
104  nal->type == H264_NAL_IDR_SLICE) {
105  H264RawSlice *slice = nal->content;
107  }
108  }
109 
110  err = ff_cbs_write_packet(ctx->output, pkt, au);
111  if (err < 0)
112  goto fail;
113 
114  err = 0;
115 fail:
116  ff_cbs_fragment_reset(ctx->output, au);
117  if (err < 0)
119 
120  return err;
121 }
122 
124 {
126  CodedBitstreamFragment *au = &ctx->access_unit;
127  int err, i;
128 
129  err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
130  if (err < 0)
131  return err;
132 
133  err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
134  if (err < 0)
135  return err;
136 
137  ctx->global_pic_init_qp = 26;
138 
139  if (bsf->par_in->extradata) {
140  err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
141  if (err < 0) {
142  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
143  goto fail;
144  }
145 
146  for (i = 0; i < au->nb_units; i++) {
147  if (au->units[i].type == H264_NAL_PPS) {
149  if (err < 0)
150  goto fail;
151  }
152  }
153 
154  ctx->extradata_pic_init_qp = ctx->current_pic_init_qp;
155  err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
156  if (err < 0) {
157  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
158  goto fail;
159  }
160  }
161 
162  err = 0;
163 fail:
164  ff_cbs_fragment_reset(ctx->output, au);
165  return err;
166 }
167 
169 {
171  ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
172 }
173 
175 {
177 
178  ff_cbs_fragment_free(ctx->input, &ctx->access_unit);
179  ff_cbs_close(&ctx->input);
180  ff_cbs_close(&ctx->output);
181 }
182 
185 };
186 
188  .name = "h264_redundant_pps",
189  .priv_data_size = sizeof(H264RedundantPPSContext),
192  .close = &h264_redundant_pps_close,
195 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
ff_h264_redundant_pps_bsf
const AVBitStreamFilter ff_h264_redundant_pps_bsf
Definition: h264_redundant_pps_bsf.c:187
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
H264RedundantPPSContext::output
CodedBitstreamContext * output
Definition: h264_redundant_pps_bsf.c:33
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
H264RedundantPPSContext
Definition: h264_redundant_pps_bsf.c:31
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
bsf_internal.h
H264RedundantPPSContext::current_pic_init_qp
int current_pic_init_qp
Definition: h264_redundant_pps_bsf.c:38
h264_redundant_pps_fixup_slice
static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx, H264RawSliceHeader *slice)
Definition: h264_redundant_pps_bsf.c:59
ff_cbs_read_extradata
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:224
cbs_h264.h
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
AVBitStreamFilter::name
const char * name
Definition: bsf.h:99
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:157
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
cbs.h
filter
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
Definition: filter_design.txt:228
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:64
bsf.h
fail
#define fail()
Definition: checkasm.h:123
h264_redundant_pps_fixup_pps
static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx, H264RawPPS *pps)
Definition: h264_redundant_pps_bsf.c:43
ff_cbs_write_extradata
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:376
AVBSFContext::par_out
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:83
H264RedundantPPSContext::input
CodedBitstreamContext * input
Definition: h264_redundant_pps_bsf.c:32
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
ff_cbs_write_packet
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:401
H264RawSliceHeader::slice_qp_delta
int8_t slice_qp_delta
Definition: cbs_h264.h:417
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_cbs_delete_unit
void ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:799
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:500
h264_redundant_pps_flush
static void h264_redundant_pps_flush(AVBSFContext *bsf)
Definition: h264_redundant_pps_bsf.c:168
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
pps
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Definition: cbs_h264_syntax_template.c:404
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
H264RawSliceHeader
Definition: cbs_h264.h:351
H264RawSlice::header
H264RawSliceHeader header
Definition: cbs_h264.h:430
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:348
common.h
H264RedundantPPSContext::access_unit
CodedBitstreamFragment access_unit
Definition: h264_redundant_pps_bsf.c:35
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
ff_cbs_init
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
h264_redundant_pps_close
static void h264_redundant_pps_close(AVBSFContext *bsf)
Definition: h264_redundant_pps_bsf.c:174
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVBitStreamFilter
Definition: bsf.h:98
H264RedundantPPSContext::global_pic_init_qp
int global_pic_init_qp
Definition: h264_redundant_pps_bsf.c:37
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:142
ff_cbs_read_packet
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:242
h264_redundant_pps_init
static int h264_redundant_pps_init(AVBSFContext *bsf)
Definition: h264_redundant_pps_bsf.c:123
mem.h
H264RedundantPPSContext::extradata_pic_init_qp
int extradata_pic_init_qp
Definition: h264_redundant_pps_bsf.c:39
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
AVPacket
This structure stores compressed data.
Definition: packet.h:332
h264.h
h264_redundant_pps_codec_ids
static enum AVCodecID h264_redundant_pps_codec_ids[]
Definition: h264_redundant_pps_bsf.c:183
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:249
h264_redundant_pps_filter
static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *pkt)
Definition: h264_redundant_pps_bsf.c:70
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
H264RawSlice
Definition: cbs_h264.h:429
H264RawPPS
Definition: cbs_h264.h:180