FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
extract_extradata_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 <stdint.h>
20 
21 #include "libavutil/common.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25 
26 #include "avcodec.h"
27 #include "bsf.h"
28 #include "h2645_parse.h"
29 #include "h264.h"
30 #include "hevc.h"
31 #include "vc1_common.h"
32 
33 typedef struct ExtractExtradataContext {
34  const AVClass *class;
35 
37  uint8_t **data, int *size);
38 
39  /* AVOptions */
40  int remove;
42 
43 static int val_in_array(const int *arr, int len, int val)
44 {
45  int i;
46  for (i = 0; i < len; i++)
47  if (arr[i] == val)
48  return 1;
49  return 0;
50 }
51 
53  uint8_t **data, int *size)
54 {
55  static const int extradata_nal_types_hevc[] = {
57  };
58  static const int extradata_nal_types_h264[] = {
60  };
61 
63 
64  H2645Packet h2645_pkt = { 0 };
65  int extradata_size = 0;
66  const int *extradata_nal_types;
67  int nb_extradata_nal_types;
68  int i, has_sps = 0, has_vps = 0, ret = 0;
69 
70  if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
71  extradata_nal_types = extradata_nal_types_hevc;
72  nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
73  } else {
74  extradata_nal_types = extradata_nal_types_h264;
75  nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
76  }
77 
78  ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
79  ctx, 0, 0, ctx->par_in->codec_id, 1);
80  if (ret < 0)
81  goto fail;
82 
83  for (i = 0; i < h2645_pkt.nb_nals; i++) {
84  H2645NAL *nal = &h2645_pkt.nals[i];
85  if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
86  extradata_size += nal->raw_size + 3;
87  if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
88  if (nal->type == HEVC_NAL_SPS) has_sps = 1;
89  if (nal->type == HEVC_NAL_VPS) has_vps = 1;
90  } else {
91  if (nal->type == H264_NAL_SPS) has_sps = 1;
92  }
93  }
94  }
95 
96  if (extradata_size &&
97  ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
98  (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
99  AVBufferRef *filtered_buf;
100  uint8_t *extradata, *filtered_data;
101 
102  if (s->remove) {
103  filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
104  if (!filtered_buf) {
105  ret = AVERROR(ENOMEM);
106  goto fail;
107  }
108  filtered_data = filtered_buf->data;
109  }
110 
111  extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
112  if (!extradata) {
113  av_buffer_unref(&filtered_buf);
114  ret = AVERROR(ENOMEM);
115  goto fail;
116  }
117 
118  *data = extradata;
119  *size = extradata_size;
120 
121  for (i = 0; i < h2645_pkt.nb_nals; i++) {
122  H2645NAL *nal = &h2645_pkt.nals[i];
123  if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
124  nal->type)) {
125  AV_WB24(extradata, 1); // startcode
126  memcpy(extradata + 3, nal->raw_data, nal->raw_size);
127  extradata += 3 + nal->raw_size;
128  } else if (s->remove) {
129  AV_WB24(filtered_data, 1); // startcode
130  memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
131  filtered_data += 3 + nal->raw_size;
132  }
133  }
134 
135  if (s->remove) {
136  av_buffer_unref(&pkt->buf);
137  pkt->buf = filtered_buf;
138  pkt->data = filtered_buf->data;
139  pkt->size = filtered_data - filtered_buf->data;
140  }
141  }
142 
143 fail:
144  ff_h2645_packet_uninit(&h2645_pkt);
145  return ret;
146 }
147 
149  uint8_t **data, int *size)
150 {
152  const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
153  uint32_t state = UINT32_MAX;
154  int has_extradata = 0, extradata_size = 0;
155 
156  while (ptr < end) {
157  ptr = avpriv_find_start_code(ptr, end, &state);
158  if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
159  has_extradata = 1;
160  } else if (has_extradata && IS_MARKER(state)) {
161  extradata_size = ptr - 4 - pkt->data;
162  break;
163  }
164  }
165 
166  if (extradata_size) {
167  *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
168  if (!*data)
169  return AVERROR(ENOMEM);
170 
171  memcpy(*data, pkt->data, extradata_size);
172  *size = extradata_size;
173 
174  if (s->remove) {
175  pkt->data += extradata_size;
176  pkt->size -= extradata_size;
177  }
178  }
179 
180  return 0;
181 }
182 
184  uint8_t **data, int *size)
185 {
187  uint32_t state = UINT32_MAX;
188  int i, found = 0;
189 
190  for (i = 0; i < pkt->size; i++) {
191  state = (state << 8) | pkt->data[i];
192  if (state == 0x1B3)
193  found = 1;
194  else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
195  if (i > 3) {
196  *size = i - 3;
197  *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
198  if (!*data)
199  return AVERROR(ENOMEM);
200 
201  memcpy(*data, pkt->data, *size);
202 
203  if (s->remove) {
204  pkt->data += *size;
205  pkt->size -= *size;
206  }
207  }
208  break;
209  }
210  }
211  return 0;
212 }
213 
215  uint8_t **data, int *size)
216 {
218  const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
219  uint32_t state = UINT32_MAX;
220 
221  while (ptr < end) {
222  ptr = avpriv_find_start_code(ptr, end, &state);
223  if (state == 0x1B3 || state == 0x1B6) {
224  if (ptr - pkt->data > 4) {
225  *size = ptr - 4 - pkt->data;
226  *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
227  if (!*data)
228  return AVERROR(ENOMEM);
229 
230  memcpy(*data, pkt->data, *size);
231 
232  if (s->remove) {
233  pkt->data += *size;
234  pkt->size -= *size;
235  }
236  }
237  break;
238  }
239  }
240  return 0;
241 }
242 
243 static const struct {
244  enum AVCodecID id;
246  uint8_t **data, int *size);
247 } extract_tab[] = {
255 };
256 
258 {
260  int i;
261 
262  for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
263  if (extract_tab[i].id == ctx->par_in->codec_id) {
264  s->extract = extract_tab[i].extract;
265  break;
266  }
267  }
268  if (!s->extract)
269  return AVERROR_BUG;
270 
271  return 0;
272 }
273 
275 {
277  AVPacket *in;
278  uint8_t *extradata = NULL;
279  int extradata_size;
280  int ret = 0;
281 
282  ret = ff_bsf_get_packet(ctx, &in);
283  if (ret < 0)
284  return ret;
285 
286  ret = s->extract(ctx, in, &extradata, &extradata_size);
287  if (ret < 0)
288  goto fail;
289 
290  if (extradata) {
292  extradata, extradata_size);
293  if (ret < 0) {
294  av_freep(&extradata);
295  goto fail;
296  }
297  }
298 
299  av_packet_move_ref(out, in);
300 
301 fail:
302  av_packet_free(&in);
303  return ret;
304 }
305 
306 static const enum AVCodecID codec_ids[] = {
315 };
316 
317 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
318 static const AVOption options[] = {
319  { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
320  { .i64 = 0 }, 0, 1 },
321  { NULL },
322 };
323 
325  .class_name = "extract_extradata",
326  .item_name = av_default_item_name,
327  .option = options,
328  .version = LIBAVUTIL_VERSION_INT,
329 };
330 
332  .name = "extract_extradata",
333  .codec_ids = codec_ids,
334  .priv_data_size = sizeof(ExtractExtradataContext),
335  .priv_class = &extract_extradata_class,
338 };
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id, int small_padding)
Split an input packet into NAL units.
Definition: h2645_parse.c:250
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
The bitstream filter state.
Definition: avcodec.h:5830
int size
Definition: avcodec.h:1658
static const struct @61 extract_tab[]
static AVPacket pkt
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5851
static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
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
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
static int extract_extradata_init(AVBSFContext *ctx)
uint8_t
#define av_malloc(s)
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int val_in_array(const int *arr, int len, int val)
static const AVClass extract_extradata_class
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, uint8_t clip)
Definition: cfhd.c:80
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:371
const char * name
Definition: avcodec.h:5880
uint8_t * data
Definition: avcodec.h:1657
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:644
ptrdiff_t size
Definition: opengl_enc.c:101
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
H.264 common definitions.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:214
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
static struct @253 state
#define IS_MARKER(state)
Definition: dca_parser.c:49
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1640
#define fail()
Definition: checkasm.h:89
int raw_size
Definition: h2645_parse.h:44
AVFormatContext * ctx
Definition: movenc.c:48
const AVBitStreamFilter ff_extract_extradata_bsf
#define AV_WB24(p, d)
Definition: intreadwrite.h:455
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:219
int type
NAL unit type.
Definition: h2645_parse.h:52
#define FF_ARRAY_ELEMS(a)
#define OFFSET(x)
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1412
Libavcodec external API header.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
uint8_t * data
The data buffer.
Definition: buffer.h:89
static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
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
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:295
A reference to a data buffer.
Definition: buffer.h:81
int
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:282
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:204
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
const uint8_t * raw_data
Definition: h2645_parse.h:45
int len
int(* extract)(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
H2645NAL * nals
Definition: h2645_parse.h:70
static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
int(* extract)(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
static enum AVCodecID codec_ids[]
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1634
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5858
static const AVOption options[]