FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h2645_parse.c
Go to the documentation of this file.
1 /*
2  * H.264/HEVC common parsing code
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <string.h>
22 
23 #include "config.h"
24 
25 #include "libavutil/intmath.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 
29 #include "hevc.h"
30 #include "h2645_parse.h"
31 
33  H2645NAL *nal)
34 {
35  int i, si, di;
36  uint8_t *dst;
37 
38  nal->skipped_bytes = 0;
39 #define STARTCODE_TEST \
40  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
41  if (src[i + 2] != 3 && src[i + 2] != 0) { \
42  /* startcode, so we must be past the end */ \
43  length = i; \
44  } \
45  break; \
46  }
47 #if HAVE_FAST_UNALIGNED
48 #define FIND_FIRST_ZERO \
49  if (i > 0 && !src[i]) \
50  i--; \
51  while (src[i]) \
52  i++
53 #if HAVE_FAST_64BIT
54  for (i = 0; i + 1 < length; i += 9) {
55  if (!((~AV_RN64A(src + i) &
56  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
57  0x8000800080008080ULL))
58  continue;
59  FIND_FIRST_ZERO;
61  i -= 7;
62  }
63 #else
64  for (i = 0; i + 1 < length; i += 5) {
65  if (!((~AV_RN32A(src + i) &
66  (AV_RN32A(src + i) - 0x01000101U)) &
67  0x80008080U))
68  continue;
69  FIND_FIRST_ZERO;
71  i -= 3;
72  }
73 #endif /* HAVE_FAST_64BIT */
74 #else
75  for (i = 0; i + 1 < length; i += 2) {
76  if (src[i])
77  continue;
78  if (i > 0 && src[i - 1] == 0)
79  i--;
81  }
82 #endif /* HAVE_FAST_UNALIGNED */
83 
84  if (i >= length - 1) { // no escaped 0
85  nal->data =
86  nal->raw_data = src;
87  nal->size =
88  nal->raw_size = length;
89  return length;
90  }
91 
94  if (!nal->rbsp_buffer)
95  return AVERROR(ENOMEM);
96 
97  dst = nal->rbsp_buffer;
98 
99  memcpy(dst, src, i);
100  si = di = i;
101  while (si + 2 < length) {
102  // remove escapes (very rare 1:2^22)
103  if (src[si + 2] > 3) {
104  dst[di++] = src[si++];
105  dst[di++] = src[si++];
106  } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
107  if (src[si + 2] == 3) { // escape
108  dst[di++] = 0;
109  dst[di++] = 0;
110  si += 3;
111 
112  if (nal->skipped_bytes_pos) {
113  nal->skipped_bytes++;
114  if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
115  nal->skipped_bytes_pos_size *= 2;
119  sizeof(*nal->skipped_bytes_pos));
120  if (!nal->skipped_bytes_pos) {
121  nal->skipped_bytes_pos_size = 0;
122  return AVERROR(ENOMEM);
123  }
124  }
125  if (nal->skipped_bytes_pos)
126  nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
127  }
128  continue;
129  } else // next start code
130  goto nsc;
131  }
132 
133  dst[di++] = src[si++];
134  }
135  while (si < length)
136  dst[di++] = src[si++];
137 
138 nsc:
139  memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
140 
141  nal->data = dst;
142  nal->size = di;
143  nal->raw_data = src;
144  nal->raw_size = si;
145  return si;
146 }
147 
148 static const char *nal_unit_name(int nal_type)
149 {
150  switch(nal_type) {
151  case NAL_TRAIL_N : return "TRAIL_N";
152  case NAL_TRAIL_R : return "TRAIL_R";
153  case NAL_TSA_N : return "TSA_N";
154  case NAL_TSA_R : return "TSA_R";
155  case NAL_STSA_N : return "STSA_N";
156  case NAL_STSA_R : return "STSA_R";
157  case NAL_RADL_N : return "RADL_N";
158  case NAL_RADL_R : return "RADL_R";
159  case NAL_RASL_N : return "RASL_N";
160  case NAL_RASL_R : return "RASL_R";
161  case NAL_BLA_W_LP : return "BLA_W_LP";
162  case NAL_BLA_W_RADL : return "BLA_W_RADL";
163  case NAL_BLA_N_LP : return "BLA_N_LP";
164  case NAL_IDR_W_RADL : return "IDR_W_RADL";
165  case NAL_IDR_N_LP : return "IDR_N_LP";
166  case NAL_CRA_NUT : return "CRA_NUT";
167  case NAL_VPS : return "VPS";
168  case NAL_SPS : return "SPS";
169  case NAL_PPS : return "PPS";
170  case NAL_AUD : return "AUD";
171  case NAL_EOS_NUT : return "EOS_NUT";
172  case NAL_EOB_NUT : return "EOB_NUT";
173  case NAL_FD_NUT : return "FD_NUT";
174  case NAL_SEI_PREFIX : return "SEI_PREFIX";
175  case NAL_SEI_SUFFIX : return "SEI_SUFFIX";
176  default : return "?";
177  }
178 }
179 
180 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
181 {
182  int size = nal->size;
183  int v;
184 
185  while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
186  size--;
187 
188  if (!size)
189  return 0;
190 
191  v = nal->data[size - 1];
192 
193  if (size > INT_MAX / 8)
194  return AVERROR(ERANGE);
195  size *= 8;
196 
197  /* remove the stop bit and following trailing zeros,
198  * or nothing for damaged bitstreams */
199  if (v)
200  size -= ff_ctz(v) + 1;
201 
202  return size;
203 }
204 
205 /**
206  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
207  * 0 if the unit should be skipped, 1 otherwise
208  */
209 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
210 {
211  GetBitContext *gb = &nal->gb;
212  int nuh_layer_id;
213 
214  if (get_bits1(gb) != 0)
215  return AVERROR_INVALIDDATA;
216 
217  nal->type = get_bits(gb, 6);
218 
219  nuh_layer_id = get_bits(gb, 6);
220  nal->temporal_id = get_bits(gb, 3) - 1;
221  if (nal->temporal_id < 0)
222  return AVERROR_INVALIDDATA;
223 
224  av_log(logctx, AV_LOG_DEBUG,
225  "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
226  nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
227 
228  return nuh_layer_id == 0;
229 }
230 
231 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
232 {
233  GetBitContext *gb = &nal->gb;
234 
235  if (get_bits1(gb) != 0)
236  return AVERROR_INVALIDDATA;
237 
238  nal->ref_idc = get_bits(gb, 2);
239  nal->type = get_bits(gb, 5);
240 
241  av_log(logctx, AV_LOG_DEBUG,
242  "nal_unit_type: %d, nal_ref_idc: %d\n",
243  nal->type, nal->ref_idc);
244 
245  return 1;
246 }
247 
249  void *logctx, int is_nalff, int nal_length_size,
250  enum AVCodecID codec_id)
251 {
252  int consumed, ret = 0;
253  const uint8_t *next_avc = is_nalff ? buf : buf + length;
254 
255  pkt->nb_nals = 0;
256  while (length >= 4) {
257  H2645NAL *nal;
258  int extract_length = 0;
259  int skip_trailing_zeros = 1;
260 
261  if (buf >= next_avc) {
262  int i;
263  for (i = 0; i < nal_length_size; i++)
264  extract_length = (extract_length << 8) | buf[i];
265  buf += nal_length_size;
266  length -= nal_length_size;
267 
268  if (extract_length > length) {
269  av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
270  return AVERROR_INVALIDDATA;
271  }
272  next_avc = buf + extract_length;
273  } else {
274  /* search start code */
275  while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
276  ++buf;
277  --length;
278  if (length < 4) {
279  if (pkt->nb_nals > 0) {
280  // No more start codes: we discarded some irrelevant
281  // bytes at the end of the packet.
282  return 0;
283  } else {
284  av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
285  return AVERROR_INVALIDDATA;
286  }
287  } else if (buf >= (next_avc - 3))
288  break;
289  }
290 
291  buf += 3;
292  length -= 3;
293  extract_length = length;
294 
295  if (buf >= next_avc) {
296  /* skip to the start of the next NAL */
297  int offset = next_avc - buf;
298  buf += offset;
299  length -= offset;
300  continue;
301  }
302  }
303 
304  if (pkt->nals_allocated < pkt->nb_nals + 1) {
305  int new_size = pkt->nals_allocated + 1;
306  void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
307 
308  if (!tmp)
309  return AVERROR(ENOMEM);
310 
311  pkt->nals = tmp;
312  memset(pkt->nals + pkt->nals_allocated, 0,
313  (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
314 
315  nal = &pkt->nals[pkt->nb_nals];
316  nal->skipped_bytes_pos_size = 1024; // initial buffer size
318  if (!nal->skipped_bytes_pos)
319  return AVERROR(ENOMEM);
320 
321  pkt->nals_allocated = new_size;
322  }
323  nal = &pkt->nals[pkt->nb_nals];
324 
325  consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);
326  if (consumed < 0)
327  return consumed;
328 
329  if (is_nalff && (extract_length != consumed) && extract_length)
330  av_log(logctx, AV_LOG_DEBUG,
331  "NALFF: Consumed only %d bytes instead of %d\n",
332  consumed, extract_length);
333 
334  pkt->nb_nals++;
335 
336  /* see commit 3566042a0 */
337  if (consumed < length - 3 &&
338  buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&
339  buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)
340  skip_trailing_zeros = 0;
341 
342  nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
343 
344  ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
345  if (ret < 0)
346  return ret;
347 
348  if (codec_id == AV_CODEC_ID_HEVC)
349  ret = hevc_parse_nal_header(nal, logctx);
350  else
351  ret = h264_parse_nal_header(nal, logctx);
352  if (ret <= 0 || nal->size <= 0) {
353  if (ret < 0) {
354  av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
355  nal->type);
356  }
357  pkt->nb_nals--;
358  }
359 
360  buf += consumed;
361  length -= consumed;
362  }
363 
364  return 0;
365 }
366 
368 {
369  int i;
370  for (i = 0; i < pkt->nals_allocated; i++) {
371  av_freep(&pkt->nals[i].rbsp_buffer);
372  av_freep(&pkt->nals[i].skipped_bytes_pos);
373  }
374  av_freep(&pkt->nals);
375  pkt->nals_allocated = 0;
376 }
#define ff_ctz
Definition: intmath.h:106
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
enum AVCodecID codec_id
Definition: ffmpeg_vaapi.c:149
int size
Definition: h2645_parse.h:33
Definition: hevc.h:97
Definition: h264.h:123
static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
Definition: h2645_parse.c:180
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
memory handling functions
static const char * nal_unit_name(int nal_type)
Definition: h2645_parse.c:148
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)
Split an input packet into NAL units.
Definition: h2645_parse.c:248
static AVPacket pkt
int size_bits
Size, in bits, of just the data, excluding the stop bit and any trailing padding. ...
Definition: h2645_parse.h:40
int skipped_bytes_pos_size
Definition: h2645_parse.h:58
#define AV_RN32A(p)
Definition: intreadwrite.h:526
#define STARTCODE_TEST
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:367
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
Definition: h264.h:124
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:189
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int * skipped_bytes_pos
Definition: h2645_parse.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
GLsizei GLsizei * length
Definition: opengl_enc.c:115
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int raw_size
Definition: h2645_parse.h:42
int ref_idc
H.264 only, nal_ref_idc.
Definition: h2645_parse.h:63
#define src
Definition: vp9dsp.c:530
Definition: h264.h:122
int type
NAL unit type.
Definition: h2645_parse.h:50
int nals_allocated
Definition: h2645_parse.h:70
static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
Definition: h2645_parse.c:209
Definition: hevc.h:111
static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
Definition: h2645_parse.c:231
const uint8_t * data
Definition: h2645_parse.h:34
void * buf
Definition: avisynth_c.h:553
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:499
int skipped_bytes
Definition: h2645_parse.h:57
Definition: hevc.h:98
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:208
GetBitContext gb
Definition: h2645_parse.h:45
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:731
const uint8_t * raw_data
Definition: h2645_parse.h:43
static uint8_t tmp[8]
Definition: des.c:38
H2645NAL * nals
Definition: h2645_parse.h:68
int rbsp_buffer_size
Definition: h2645_parse.h:31
int temporal_id
HEVC only, nuh_temporal_id_plus_1 - 1.
Definition: h2645_parse.h:55
uint8_t * rbsp_buffer
Definition: h2645_parse.h:30
#define av_freep(p)
#define av_malloc_array(a, b)
int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645NAL *nal)
Extract the raw (unescaped) bitstream.
Definition: h2645_parse.c:32
#define AV_RN64A(p)
Definition: intreadwrite.h:530