FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "cbs.h"
26 #include "cbs_h264.h"
27 #include "h264.h"
28 
29 
30 typedef struct H264RedundantPPSContext {
33 
35 
40 
41 
43  H264RawPPS *pps)
44 {
45  // Record the current value of pic_init_qp in order to fix up
46  // following slices, then overwrite with the global value.
48  pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
49 
50  // Some PPSs have this set, so it must be set in all of them.
51  // (Slices which do not use such a PPS on input will still have
52  // *_weight_l*flag as zero and therefore write equivalently.)
53  pps->weighted_pred_flag = 1;
54 
55  return 0;
56 }
57 
59  H264RawSliceHeader *slice)
60 {
61  int qp;
62 
63  qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
64  slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
65 
66  return 0;
67 }
68 
70 {
72  AVPacket *in;
74  int au_has_sps;
75  int err, i;
76 
77  err = ff_bsf_get_packet(bsf, &in);
78  if (err < 0)
79  return err;
80 
81  err = ff_cbs_read_packet(ctx->input, au, in);
82  if (err < 0)
83  return err;
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 (!au_has_sps) {
94  av_log(ctx, AV_LOG_VERBOSE, "Deleting redundant PPS "
95  "at %"PRId64".\n", in->pts);
96  ff_cbs_delete_unit(ctx->input, au, i);
97  }
98  }
99  if (nal->type == H264_NAL_SLICE ||
100  nal->type == H264_NAL_IDR_SLICE) {
101  H264RawSlice *slice = nal->content;
103  }
104  }
105 
106  err = ff_cbs_write_packet(ctx->output, out, au);
107  if (err < 0)
108  return err;
109 
110  ff_cbs_fragment_uninit(ctx->output, au);
111 
112  err = av_packet_copy_props(out, in);
113  if (err < 0)
114  return err;
115 
116  av_packet_free(&in);
117 
118  return 0;
119 }
120 
122 {
125  int err, i;
126 
127  err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
128  if (err < 0)
129  return err;
130 
131  err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
132  if (err < 0)
133  return err;
134 
135  ctx->global_pic_init_qp = 26;
136 
137  if (bsf->par_in->extradata) {
138  err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
139  if (err < 0) {
140  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
141  return err;
142  }
143 
144  for (i = 0; i < au->nb_units; i++) {
145  if (au->units[i].type == H264_NAL_PPS)
147  }
148 
150  err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
151  if (err < 0) {
152  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
153  return err;
154  }
155 
156  ff_cbs_fragment_uninit(ctx->output, au);
157  }
158 
159  return 0;
160 }
161 
163 {
166 }
167 
169 {
171  ff_cbs_close(&ctx->input);
172  ff_cbs_close(&ctx->output);
173 }
174 
177 };
178 
180  .name = "h264_redundant_pps",
181  .priv_data_size = sizeof(H264RedundantPPSContext),
184  .close = &h264_redundant_pps_close,
187 };
static int h264_redundant_pps_init(AVBSFContext *bsf)
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5736
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:343
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:68
The bitstream filter state.
Definition: avcodec.h:5702
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5723
const AVBitStreamFilter ff_h264_redundant_pps_bsf
CodedBitstreamContext * input
uint8_t weighted_pred_flag
Definition: cbs_h264.h:205
static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx, H264RawSliceHeader *slice)
int8_t slice_qp_delta
Definition: cbs_h264.h:412
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:153
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
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:233
const char * name
Definition: avcodec.h:5752
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx, H264RawPPS *pps)
Coded bitstream unit structure.
Definition: cbs.h:64
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:153
#define av_log(a,...)
H.264 common definitions.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:564
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:318
AVFormatContext * ctx
Definition: movenc.c:48
CodedBitstreamContext * output
int 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:644
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:116
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out)
Context structure for coded bitstream operations.
Definition: cbs.h:159
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
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:213
CodedBitstreamFragment access_unit
common internal and external API header
static enum AVCodecID codec_ids[]
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:216
static enum AVCodecID h264_redundant_pps_codec_ids[]
H264RawSliceHeader header
Definition: cbs_h264.h:425
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3913
int8_t pic_init_qp_minus26
Definition: cbs_h264.h:208
FILE * out
Definition: movenc.c:54
This structure stores compressed data.
Definition: avcodec.h:1421
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5730
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1437