FFmpeg
h264_redundant_pps.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 <inttypes.h>
20 
21 #include "libavutil/log.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 "codec_id.h"
29 #include "h264.h"
30 #include "packet.h"
31 
32 #define NEW_GLOBAL_PIC_INIT_QP 26
33 
34 typedef struct H264RedundantPPSContext {
37 
38 
40  CodedBitstreamUnit *unit)
41 {
42  H264RawPPS *pps;
43  int err;
44 
45  // The changes we are about to perform affect the parsing process,
46  // so we must make sure that the PPS is writable, otherwise the
47  // parsing of future slices will be incorrect and even raise errors.
48  err = ff_cbs_make_unit_writable(ctx->common.input, unit);
49  if (err < 0)
50  return err;
51  pps = unit->content;
52 
53  // Overwrite pic_init_qp with the global value.
54  pps->pic_init_qp_minus26 = NEW_GLOBAL_PIC_INIT_QP - 26;
55 
56  // Some PPSs have this set, so it must be set in all of them.
57  // (Slices which do not use such a PPS on input will still have
58  // *_weight_l*flag as zero and therefore write equivalently.)
59  pps->weighted_pred_flag = 1;
60 
61  return 0;
62 }
63 
65  H264RawSliceHeader *slice)
66 {
67  const CodedBitstreamH264Context *const in = ctx->common.input->priv_data;
68  const H264RawPPS *const pps = in->pps[slice->pic_parameter_set_id];
69 
70  // We modified the PPS's qp value, now offset this by applying
71  // the negative offset to the slices.
72  slice->slice_qp_delta += pps->pic_init_qp_minus26
73  - (NEW_GLOBAL_PIC_INIT_QP - 26);
74 
75  return 0;
76 }
77 
79  AVPacket *pkt,
81 {
83  int err, i;
84 
85  for (i = 0; i < au->nb_units; i++) {
86  CodedBitstreamUnit *nal = &au->units[i];
87 
88  if (nal->type == H264_NAL_PPS) {
90  if (err < 0)
91  return err;
92  }
93  if (nal->type == H264_NAL_SLICE ||
94  nal->type == H264_NAL_IDR_SLICE) {
95  H264RawSlice *slice = nal->content;
97  }
98  }
99 
100  return 0;
101 }
102 
105  .fragment_name = "access unit",
106  .unit_name = "NAL unit",
107  .update_fragment = &h264_redundant_pps_update_fragment,
108 };
109 
111 {
113 }
114 
117 };
118 
120  .p.name = "h264_redundant_pps",
121  .p.codec_ids = h264_redundant_pps_codec_ids,
122  .priv_data_size = sizeof(H264RedundantPPSContext),
124  .close = &ff_cbs_bsf_generic_close,
126 };
H264RedundantPPSContext
Definition: h264_redundant_pps.c:34
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:110
bsf_internal.h
h264_redundant_pps_type
static const CBSBSFType h264_redundant_pps_type
Definition: h264_redundant_pps.c:103
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
CBSBSFType::codec_id
enum AVCodecID codec_id
Definition: cbs_bsf.h:32
cbs_h264.h
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:107
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
NEW_GLOBAL_PIC_INIT_QP
#define NEW_GLOBAL_PIC_INIT_QP
Definition: h264_redundant_pps.c:32
CodedBitstreamH264Context::pps
H264RawPPS * pps[H264_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h264.h:431
CBSBSFContext
Definition: cbs_bsf.h:53
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition: h263dsp.c:29
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:74
cbs.h
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
h264_redundant_pps_fixup_slice
static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx, H264RawSliceHeader *slice)
Definition: h264_redundant_pps.c:64
bsf.h
cbs_bsf.h
h264_redundant_pps_update_fragment
static int h264_redundant_pps_update_fragment(AVBSFContext *bsf, AVPacket *pkt, CodedBitstreamFragment *au)
Definition: h264_redundant_pps.c:78
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:1044
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:168
pkt
AVPacket * pkt
Definition: movenc.c:60
codec_id.h
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
H264RawSliceHeader::slice_qp_delta
int8_t slice_qp_delta
Definition: cbs_h264.h:396
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
FFBitStreamFilter
Definition: bsf_internal.h:27
h264_redundant_pps_codec_ids
static enum AVCodecID h264_redundant_pps_codec_ids[]
Definition: h264_redundant_pps.c:115
H264_NAL_PPS
@ H264_NAL_PPS
Definition: h264.h:42
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
CodedBitstreamH264Context
Definition: cbs_h264.h:424
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:155
ff_h264_redundant_pps_bsf
const FFBitStreamFilter ff_h264_redundant_pps_bsf
Definition: h264_redundant_pps.c:119
H264RawSliceHeader
Definition: cbs_h264.h:330
H264RawSlice::header
H264RawSliceHeader header
Definition: cbs_h264.h:409
log.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
packet.h
h264_redundant_pps_fixup_pps
static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx, CodedBitstreamUnit *unit)
Definition: h264_redundant_pps.c:39
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:83
ff_cbs_bsf_generic_filter
int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
Filter operation for CBS BSF.
Definition: cbs_bsf.c:61
H264RedundantPPSContext::common
CBSBSFContext common
Definition: h264_redundant_pps.c:35
h264_redundant_pps_init
static int h264_redundant_pps_init(AVBSFContext *bsf)
Definition: h264_redundant_pps.c:110
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
AVPacket
This structure stores compressed data.
Definition: packet.h:510
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
h264.h
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1288
H264RawSliceHeader::pic_parameter_set_id
uint8_t pic_parameter_set_id
Definition: cbs_h264.h:336
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:153
H264RawSlice
Definition: cbs_h264.h:408
H264RawPPS
Definition: cbs_h264.h:171