FFmpeg  4.0
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  /* H264/HEVC specifc fields */
41 
42  /* AVOptions */
43  int remove;
45 
46 static int val_in_array(const int *arr, int len, int val)
47 {
48  int i;
49  for (i = 0; i < len; i++)
50  if (arr[i] == val)
51  return 1;
52  return 0;
53 }
54 
56  uint8_t **data, int *size)
57 {
58  static const int extradata_nal_types_hevc[] = {
60  };
61  static const int extradata_nal_types_h264[] = {
63  };
64 
66 
67  int extradata_size = 0, filtered_size = 0;
68  const int *extradata_nal_types;
69  int nb_extradata_nal_types;
70  int i, has_sps = 0, has_vps = 0, ret = 0;
71 
72  if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
73  extradata_nal_types = extradata_nal_types_hevc;
74  nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
75  } else {
76  extradata_nal_types = extradata_nal_types_h264;
77  nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
78  }
79 
80  ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
81  ctx, 0, 0, ctx->par_in->codec_id, 1);
82  if (ret < 0)
83  return ret;
84 
85  for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
86  H2645NAL *nal = &s->h2645_pkt.nals[i];
87  if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
88  extradata_size += nal->raw_size + 3;
89  if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
90  if (nal->type == HEVC_NAL_SPS) has_sps = 1;
91  if (nal->type == HEVC_NAL_VPS) has_vps = 1;
92  } else {
93  if (nal->type == H264_NAL_SPS) has_sps = 1;
94  }
95  } else if (s->remove) {
96  filtered_size += nal->raw_size + 3;
97  }
98  }
99 
100  if (extradata_size &&
101  ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
102  (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
103  AVBufferRef *filtered_buf;
104  uint8_t *extradata, *filtered_data;
105 
106  if (s->remove) {
107  filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
108  if (!filtered_buf) {
109  return AVERROR(ENOMEM);
110  }
111  memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
112 
113  filtered_data = filtered_buf->data;
114  }
115 
116  extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
117  if (!extradata) {
118  av_buffer_unref(&filtered_buf);
119  return AVERROR(ENOMEM);
120  }
121  memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
122 
123  *data = extradata;
124  *size = extradata_size;
125 
126  for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
127  H2645NAL *nal = &s->h2645_pkt.nals[i];
128  if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
129  nal->type)) {
130  AV_WB24(extradata, 1); // startcode
131  memcpy(extradata + 3, nal->raw_data, nal->raw_size);
132  extradata += 3 + nal->raw_size;
133  } else if (s->remove) {
134  AV_WB24(filtered_data, 1); // startcode
135  memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
136  filtered_data += 3 + nal->raw_size;
137  }
138  }
139 
140  if (s->remove) {
141  av_buffer_unref(&pkt->buf);
142  pkt->buf = filtered_buf;
143  pkt->data = filtered_buf->data;
144  pkt->size = filtered_size;
145  }
146  }
147 
148  return 0;
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  uint8_t *extradata = NULL;
284  int extradata_size;
285  int ret = 0;
286 
287  ret = ff_bsf_get_packet_ref(ctx, pkt);
288  if (ret < 0)
289  return ret;
290 
291  ret = s->extract(ctx, pkt, &extradata, &extradata_size);
292  if (ret < 0)
293  goto fail;
294 
295  if (extradata) {
297  extradata, extradata_size);
298  if (ret < 0) {
299  av_freep(&extradata);
300  goto fail;
301  }
302  }
303 
304  return 0;
305 
306 fail:
307  av_packet_unref(pkt);
308  return ret;
309 }
310 
312 {
315 }
316 
317 static const enum AVCodecID codec_ids[] = {
326 };
327 
328 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
329 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
330 static const AVOption options[] = {
331  { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
332  { .i64 = 0 }, 0, 1, FLAGS },
333  { NULL },
334 };
335 
337  .class_name = "extract_extradata",
338  .item_name = av_default_item_name,
339  .option = options,
340  .version = LIBAVUTIL_VERSION_INT,
341 };
342 
344  .name = "extract_extradata",
345  .codec_ids = codec_ids,
346  .priv_data_size = sizeof(ExtractExtradataContext),
347  .priv_class = &extract_extradata_class,
350  .close = extract_extradata_close,
351 };
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:263
#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
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
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
static const struct @59 extract_tab[]
int size
Definition: avcodec.h:1431
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static AVPacket pkt
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5708
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
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
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 void extract_extradata_close(AVBSFContext *ctx)
static const AVClass extract_extradata_class
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:386
const char data[16]
Definition: mxf.c:90
const char * name
Definition: avcodec.h:5737
uint8_t * data
Definition: avcodec.h:1430
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:215
#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:1413
#define fail()
Definition: checkasm.h:116
int raw_size
Definition: h2645_parse.h:43
AVFormatContext * ctx
Definition: movenc.c:48
const AVBitStreamFilter ff_extract_extradata_bsf
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
static struct @271 state
if(ret< 0)
Definition: vf_mcdeint.c:279
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
int type
NAL unit type.
Definition: h2645_parse.h:51
#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:1158
Libavcodec external API header.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
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
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
#define FLAGS
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
const uint8_t * raw_data
Definition: h2645_parse.h:44
int len
H2645NAL * nals
Definition: h2645_parse.h:75
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)
#define av_freep(p)
static enum AVCodecID codec_ids[]
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1407
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5715
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:228
static const AVOption options[]