FFmpeg  3.4.9
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  memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
118 
119  *data = extradata;
120  *size = extradata_size;
121 
122  for (i = 0; i < h2645_pkt.nb_nals; i++) {
123  H2645NAL *nal = &h2645_pkt.nals[i];
124  if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
125  nal->type)) {
126  AV_WB24(extradata, 1); // startcode
127  memcpy(extradata + 3, nal->raw_data, nal->raw_size);
128  extradata += 3 + nal->raw_size;
129  } else if (s->remove) {
130  AV_WB24(filtered_data, 1); // startcode
131  memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
132  filtered_data += 3 + nal->raw_size;
133  }
134  }
135 
136  if (s->remove) {
137  av_buffer_unref(&pkt->buf);
138  pkt->buf = filtered_buf;
139  pkt->data = filtered_buf->data;
140  pkt->size = filtered_data - filtered_buf->data;
141 
142  memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
143  }
144  }
145 
146 fail:
147  ff_h2645_packet_uninit(&h2645_pkt);
148  return ret;
149 }
150 
152  uint8_t **data, int *size)
153 {
155  const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
156  uint32_t state = UINT32_MAX;
157  int has_extradata = 0, extradata_size = 0;
158 
159  while (ptr < end) {
160  ptr = avpriv_find_start_code(ptr, end, &state);
161  if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
162  has_extradata = 1;
163  } else if (has_extradata && IS_MARKER(state)) {
164  extradata_size = ptr - 4 - pkt->data;
165  break;
166  }
167  }
168 
169  if (extradata_size) {
170  *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
171  if (!*data)
172  return AVERROR(ENOMEM);
173 
174  memcpy(*data, pkt->data, extradata_size);
175  memset(*data + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
176  *size = extradata_size;
177 
178  if (s->remove) {
179  pkt->data += extradata_size;
180  pkt->size -= extradata_size;
181  }
182  }
183 
184  return 0;
185 }
186 
188  uint8_t **data, int *size)
189 {
191  uint32_t state = UINT32_MAX;
192  int i, found = 0;
193 
194  for (i = 0; i < pkt->size; i++) {
195  state = (state << 8) | pkt->data[i];
196  if (state == 0x1B3)
197  found = 1;
198  else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
199  if (i > 3) {
200  *size = i - 3;
201  *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
202  if (!*data)
203  return AVERROR(ENOMEM);
204 
205  memcpy(*data, pkt->data, *size);
206  memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
207 
208  if (s->remove) {
209  pkt->data += *size;
210  pkt->size -= *size;
211  }
212  }
213  break;
214  }
215  }
216  return 0;
217 }
218 
220  uint8_t **data, int *size)
221 {
223  const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
224  uint32_t state = UINT32_MAX;
225 
226  while (ptr < end) {
227  ptr = avpriv_find_start_code(ptr, end, &state);
228  if (state == 0x1B3 || state == 0x1B6) {
229  if (ptr - pkt->data > 4) {
230  *size = ptr - 4 - pkt->data;
231  *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
232  if (!*data)
233  return AVERROR(ENOMEM);
234 
235  memcpy(*data, pkt->data, *size);
236  memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
237 
238  if (s->remove) {
239  pkt->data += *size;
240  pkt->size -= *size;
241  }
242  }
243  break;
244  }
245  }
246  return 0;
247 }
248 
249 static const struct {
250  enum AVCodecID id;
252  uint8_t **data, int *size);
253 } extract_tab[] = {
256  { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
258  { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12 },
259  { AV_CODEC_ID_MPEG4, extract_extradata_mpeg4 },
261 };
262 
264 {
266  int i;
267 
268  for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
269  if (extract_tab[i].id == ctx->par_in->codec_id) {
270  s->extract = extract_tab[i].extract;
271  break;
272  }
273  }
274  if (!s->extract)
275  return AVERROR_BUG;
276 
277  return 0;
278 }
279 
281 {
283  AVPacket *in;
284  uint8_t *extradata = NULL;
285  int extradata_size;
286  int ret = 0;
287 
288  ret = ff_bsf_get_packet(ctx, &in);
289  if (ret < 0)
290  return ret;
291 
292  ret = s->extract(ctx, in, &extradata, &extradata_size);
293  if (ret < 0)
294  goto fail;
295 
296  if (extradata) {
298  extradata, extradata_size);
299  if (ret < 0) {
300  av_freep(&extradata);
301  goto fail;
302  }
303  }
304 
305  av_packet_move_ref(out, in);
306 
307 fail:
308  av_packet_free(&in);
309  return ret;
310 }
311 
312 static const enum AVCodecID codec_ids[] = {
321 };
322 
323 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
324 static const AVOption options[] = {
325  { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
326  { .i64 = 0 }, 0, 1 },
327  { NULL },
328 };
329 
331  .class_name = "extract_extradata",
332  .item_name = av_default_item_name,
333  .option = options,
334  .version = LIBAVUTIL_VERSION_INT,
335 };
336 
338  .name = "extract_extradata",
339  .codec_ids = codec_ids,
340  .priv_data_size = sizeof(ExtractExtradataContext),
341  .priv_class = &extract_extradata_class,
344 };
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
static const struct @63 extract_tab[]
#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
int size
int(* extract)(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size)
AVOption.
Definition: opt.h:246
static struct @260 state
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
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:4152
The bitstream filter state.
Definition: avcodec.h:5914
int size
Definition: avcodec.h:1680
static AVPacket pkt
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5935
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 data[16]
Definition: mxf.c:90
const char * name
Definition: avcodec.h:5964
uint8_t * data
Definition: avcodec.h:1679
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:673
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
#define IS_MARKER(state)
Definition: dca_parser.c:51
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1662
#define fail()
Definition: checkasm.h:109
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
if(ret< 0)
Definition: vf_mcdeint.c:279
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:1420
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)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
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
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:202
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:777
const uint8_t * raw_data
Definition: h2645_parse.h:45
int len
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)
static enum AVCodecID codec_ids[]
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1656
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5942
static const AVOption options[]