FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
filter_units_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 <stdlib.h>
20 
21 #include "libavutil/common.h"
22 #include "libavutil/opt.h"
23 
24 #include "bsf.h"
25 #include "cbs.h"
26 
27 
28 typedef struct FilterUnitsContext {
29  const AVClass *class;
30 
33 
34  const char *pass_types;
35  const char *remove_types;
36 
37  enum {
41  } mode;
43  int nb_types;
45 
46 
47 static int filter_units_make_type_list(const char *list_string,
48  CodedBitstreamUnitType **type_list,
49  int *nb_types)
50 {
52  int pass, count;
53 
54  for (pass = 1; pass <= 2; pass++) {
55  long value, range_start, range_end;
56  const char *str;
57  char *value_end;
58 
59  count = 0;
60  for (str = list_string; *str;) {
61  value = strtol(str, &value_end, 0);
62  if (str == value_end)
63  goto invalid;
64  str = (const char *)value_end;
65  if (*str == '-') {
66  ++str;
67  range_start = value;
68  range_end = strtol(str, &value_end, 0);
69  if (str == value_end)
70  goto invalid;
71 
72  for (value = range_start; value < range_end; value++) {
73  if (pass == 2)
74  list[count] = value;
75  ++count;
76  }
77  } else {
78  if (pass == 2)
79  list[count] = value;
80  ++count;
81  }
82  if (*str == '|')
83  ++str;
84  }
85  if (pass == 1) {
86  list = av_malloc_array(count, sizeof(*list));
87  if (!list)
88  return AVERROR(ENOMEM);
89  }
90  }
91 
92  *type_list = list;
93  *nb_types = count;
94  return 0;
95 
96 invalid:
97  av_freep(&list);
98  return AVERROR(EINVAL);
99 }
100 
102 {
104  CodedBitstreamFragment *frag = &ctx->fragment;
105  AVPacket *in = NULL;
106  int err, i, j;
107 
108  while (1) {
109  err = ff_bsf_get_packet(bsf, &in);
110  if (err < 0)
111  return err;
112 
113  if (ctx->mode == NOOP) {
114  av_packet_move_ref(out, in);
115  av_packet_free(&in);
116  return 0;
117  }
118 
119  err = ff_cbs_read_packet(ctx->cbc, frag, in);
120  if (err < 0) {
121  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
122  goto fail;
123  }
124 
125  for (i = 0; i < frag->nb_units; i++) {
126  for (j = 0; j < ctx->nb_types; j++) {
127  if (frag->units[i].type == ctx->type_list[j])
128  break;
129  }
130  if (ctx->mode == REMOVE ? j < ctx->nb_types
131  : j >= ctx->nb_types) {
132  ff_cbs_delete_unit(ctx->cbc, frag, i);
133  --i;
134  }
135  }
136 
137  if (frag->nb_units > 0)
138  break;
139 
140  // Don't return packets with nothing in them.
141  av_packet_free(&in);
142  ff_cbs_fragment_uninit(ctx->cbc, frag);
143  }
144 
145  err = ff_cbs_write_packet(ctx->cbc, out, frag);
146  if (err < 0) {
147  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
148  goto fail;
149  }
150 
151  err = av_packet_copy_props(out, in);
152  if (err < 0)
153  goto fail;
154 
155 fail:
156  ff_cbs_fragment_uninit(ctx->cbc, frag);
157  av_packet_free(&in);
158 
159  return err;
160 }
161 
163 {
165  int err;
166 
167  if (ctx->pass_types && ctx->remove_types) {
168  av_log(bsf, AV_LOG_ERROR, "Exactly one of pass_types or "
169  "remove_types is required.\n");
170  return AVERROR(EINVAL);
171  }
172 
173  if (ctx->pass_types) {
174  ctx->mode = PASS;
176  &ctx->type_list, &ctx->nb_types);
177  if (err < 0) {
178  av_log(bsf, AV_LOG_ERROR, "Failed to parse pass_types.\n");
179  return err;
180  }
181  } else if (ctx->remove_types) {
182  ctx->mode = REMOVE;
184  &ctx->type_list, &ctx->nb_types);
185  if (err < 0) {
186  av_log(bsf, AV_LOG_ERROR, "Failed to parse remove_types.\n");
187  return err;
188  }
189  } else {
190  return 0;
191  }
192 
193  err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
194  if (err < 0)
195  return err;
196 
197  // Don't actually decompose anything, we only want the unit data.
198  ctx->cbc->decompose_unit_types = ctx->type_list;
199  ctx->cbc->nb_decompose_unit_types = 0;
200 
201  if (bsf->par_in->extradata) {
203 
204  err = ff_cbs_read_extradata(ctx->cbc, &ps, bsf->par_in);
205  if (err < 0) {
206  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
207  } else {
208  err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, &ps);
209  if (err < 0)
210  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
211  }
212 
213  ff_cbs_fragment_uninit(ctx->cbc, &ps);
214  }
215 
216  return err;
217 }
218 
220 {
222 
223  av_freep(&ctx->type_list);
224 
225  ff_cbs_close(&ctx->cbc);
226 }
227 
228 #define OFFSET(x) offsetof(FilterUnitsContext, x)
229 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
230 static const AVOption filter_units_options[] = {
231  { "pass_types", "List of unit types to pass through the filter.",
232  OFFSET(pass_types), AV_OPT_TYPE_STRING,
233  { .str = NULL }, .flags = FLAGS },
234  { "remove_types", "List of unit types to remove in the filter.",
235  OFFSET(remove_types), AV_OPT_TYPE_STRING,
236  { .str = NULL }, .flags = FLAGS },
237 
238  { NULL }
239 };
240 
241 static const AVClass filter_units_class = {
242  .class_name = "filter_units",
243  .item_name = av_default_item_name,
244  .option = filter_units_options,
245  .version = LIBAVUTIL_VERSION_INT,
246 };
247 
249  .name = "filter_units",
250  .priv_data_size = sizeof(FilterUnitsContext),
251  .priv_class = &filter_units_class,
253  .close = &filter_units_close,
256 };
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:144
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5721
static const AVClass filter_units_class
AVOption.
Definition: opt.h:246
const char * pass_types
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:320
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:56
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:67
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
The bitstream filter state.
Definition: avcodec.h:5687
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
CodedBitstreamContext * cbc
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5708
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
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:114
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
AVOptions.
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
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:212
const char * name
Definition: avcodec.h:5737
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:647
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:150
#define av_log(a,...)
static int filter_units_init(AVBSFContext *bsf)
#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:121
#define AVERROR(e)
Definition: error.h:43
GLsizei count
Definition: opengl_enc.c:109
static void filter_units_close(AVBSFContext *bsf)
#define fail()
Definition: checkasm.h:116
#define pass
Definition: fft_template.c:593
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:558
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:295
#define PASS(name)
Definition: fft_template.c:504
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
const char * remove_types
AVFormatContext * ctx
Definition: movenc.c:48
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:592
enum FilterUnitsContext::@60 mode
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:189
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:114
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
CodedBitstreamUnitType * type_list
const AVBitStreamFilter ff_filter_units_bsf
Describe the class of an AVClass context structure.
Definition: log.h:67
Context structure for coded bitstream operations.
Definition: cbs.h:156
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:95
#define FLAGS
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:170
CodedBitstreamFragment fragment
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:206
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:185
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs.c:43
static const AVOption filter_units_options[]
#define OFFSET(x)
static int filter_units_make_type_list(const char *list_string, CodedBitstreamUnitType **type_list, int *nb_types)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3898
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define av_malloc_array(a, b)
static int filter_units_filter(AVBSFContext *bsf, AVPacket *out)
This structure stores compressed data.
Definition: avcodec.h:1407
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5715