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 
23 #include "bsf.h"
24 #include "bsf_internal.h"
25 #include "cbs.h"
26 #include "cbs_bsf.h"
27 #include "cbs_h264.h"
28 #include "h264.h"
29 
30 
31 typedef struct H264RedundantPPSContext {
33 
38 
39 
41  CodedBitstreamUnit *unit)
42 {
43  H264RawPPS *pps;
44  int err;
45 
46  // The changes we are about to perform affect the parsing process,
47  // so we must make sure that the PPS is writable, otherwise the
48  // parsing of future slices will be incorrect and even raise errors.
49  err = ff_cbs_make_unit_writable(ctx->common.input, unit);
50  if (err < 0)
51  return err;
52  pps = unit->content;
53 
54  // Record the current value of pic_init_qp in order to fix up
55  // following slices, then overwrite with the global value.
56  ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
57  pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
58 
59  // Some PPSs have this set, so it must be set in all of them.
60  // (Slices which do not use such a PPS on input will still have
61  // *_weight_l*flag as zero and therefore write equivalently.)
62  pps->weighted_pred_flag = 1;
63 
64  return 0;
65 }
66 
68  H264RawSliceHeader *slice)
69 {
70  int qp;
71 
72  qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
73  slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
74 
75  return 0;
76 }
77 
79  AVPacket *pkt,
81 {
83  int au_has_sps;
84  int err, i;
85 
86  au_has_sps = 0;
87  for (i = 0; i < au->nb_units; i++) {
88  CodedBitstreamUnit *nal = &au->units[i];
89 
90  if (nal->type == H264_NAL_SPS)
91  au_has_sps = 1;
92  if (nal->type == H264_NAL_PPS) {
94  if (err < 0)
95  return err;
96  if (!au_has_sps) {
97  av_log(bsf, AV_LOG_VERBOSE, "Deleting redundant PPS "
98  "at %"PRId64".\n", pkt->pts);
99  ff_cbs_delete_unit(au, i);
100  i--;
101  continue;
102  }
103  }
104  if (nal->type == H264_NAL_SLICE ||
105  nal->type == H264_NAL_IDR_SLICE) {
106  H264RawSlice *slice = nal->content;
108  }
109  }
110 
111  return 0;
112 }
113 
115 {
117  ctx->current_pic_init_qp = ctx->extradata_pic_init_qp;
118 }
119 
122  .fragment_name = "access unit",
123  .unit_name = "NAL unit",
124  .update_fragment = &h264_redundant_pps_update_fragment,
125 };
126 
128 {
130 
131  ctx->global_pic_init_qp = 26;
132 
134 }
135 
138 };
139 
141  .name = "h264_redundant_pps",
142  .priv_data_size = sizeof(H264RedundantPPSContext),
145  .close = &ff_cbs_bsf_generic_close,
148 };
ff_h264_redundant_pps_bsf
const AVBitStreamFilter ff_h264_redundant_pps_bsf
Definition: h264_redundant_pps_bsf.c:140
H264RedundantPPSContext
Definition: h264_redundant_pps_bsf.c:31
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
ff_cbs_bsf_generic_init
int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
Initialise generic CBS BSF setup.
Definition: cbs_bsf.c:113
bsf_internal.h
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
H264RedundantPPSContext::current_pic_init_qp
int current_pic_init_qp
Definition: h264_redundant_pps_bsf.c:35
h264_redundant_pps_update_fragment
static int h264_redundant_pps_update_fragment(AVBSFContext *bsf, AVPacket *pkt, CodedBitstreamFragment *au)
Definition: h264_redundant_pps_bsf.c:78
CBSBSFType::codec_id
enum AVCodecID codec_id
Definition: cbs_bsf.h:32
h264_redundant_pps_fixup_slice
static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx, H264RawSliceHeader *slice)
Definition: h264_redundant_pps_bsf.c:67
h264_redundant_pps_type
static const CBSBSFType h264_redundant_pps_type
Definition: h264_redundant_pps_bsf.c:120
cbs_h264.h
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:106
AVBitStreamFilter::name
const char * name
Definition: bsf.h:99
CBSBSFContext
Definition: cbs_bsf.h:53
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:73
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:69
bsf.h
cbs_bsf.h
ff_cbs_make_unit_writable
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:1012
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:167
pkt
AVPacket * pkt
Definition: movenc.c:59
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:121
H264RawSliceHeader::slice_qp_delta
int8_t slice_qp_delta
Definition: cbs_h264.h:376
h264_redundant_pps_fixup_pps
static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx, CodedBitstreamUnit *unit)
Definition: h264_redundant_pps_bsf.c:40
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
h264_redundant_pps_flush
static void h264_redundant_pps_flush(AVBSFContext *bsf)
Definition: h264_redundant_pps_bsf.c:114
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
pps
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Definition: cbs_h264_syntax_template.c:404
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
CBSBSFType
Definition: cbs_bsf.h:31
ff_cbs_bsf_generic_close
void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
Close a generic CBS BSF instance.
Definition: cbs_bsf.c:153
H264RawSliceHeader
Definition: cbs_h264.h:310
H264RawSlice::header
H264RawSliceHeader header
Definition: cbs_h264.h:389
i
int i
Definition: input.c:406
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
common.h
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
ff_cbs_bsf_generic_filter
int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
Filter operation for CBS BSF.
Definition: cbs_bsf.c:64
H264RedundantPPSContext::common
CBSBSFContext common
Definition: h264_redundant_pps_bsf.c:32
AVBitStreamFilter
Definition: bsf.h:98
H264RedundantPPSContext::global_pic_init_qp
int global_pic_init_qp
Definition: h264_redundant_pps_bsf.c:34
h264_redundant_pps_init
static int h264_redundant_pps_init(AVBSFContext *bsf)
Definition: h264_redundant_pps_bsf.c:127
H264RedundantPPSContext::extradata_pic_init_qp
int extradata_pic_init_qp
Definition: h264_redundant_pps_bsf.c:36
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
AVPacket
This structure stores compressed data.
Definition: packet.h:350
h264.h
h264_redundant_pps_codec_ids
static enum AVCodecID h264_redundant_pps_codec_ids[]
Definition: h264_redundant_pps_bsf.c:136
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:152
ff_cbs_delete_unit
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:813
H264RawSlice
Definition: cbs_h264.h:388
H264RawPPS
Definition: cbs_h264.h:171