FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hevc_parse.c
Go to the documentation of this file.
1 /*
2  * HEVC common 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/intreadwrite.h"
26 #include "libavutil/mem.h"
27 
28 #include "hevc.h"
29 
30 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
31  * between these functions would be nice. */
33  HEVCNAL *nal)
34 {
35  int i, si, di;
36  uint8_t *dst;
37 
38  if (s)
39  nal->skipped_bytes = 0;
40 #define STARTCODE_TEST \
41  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
42  if (src[i + 2] != 3) { \
43  /* startcode, so we must be past the end */ \
44  length = i; \
45  } \
46  break; \
47  }
48 #if HAVE_FAST_UNALIGNED
49 #define FIND_FIRST_ZERO \
50  if (i > 0 && !src[i]) \
51  i--; \
52  while (src[i]) \
53  i++
54 #if HAVE_FAST_64BIT
55  for (i = 0; i + 1 < length; i += 9) {
56  if (!((~AV_RN64A(src + i) &
57  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
58  0x8000800080008080ULL))
59  continue;
60  FIND_FIRST_ZERO;
62  i -= 7;
63  }
64 #else
65  for (i = 0; i + 1 < length; i += 5) {
66  if (!((~AV_RN32A(src + i) &
67  (AV_RN32A(src + i) - 0x01000101U)) &
68  0x80008080U))
69  continue;
70  FIND_FIRST_ZERO;
72  i -= 3;
73  }
74 #endif /* HAVE_FAST_64BIT */
75 #else
76  for (i = 0; i + 1 < length; i += 2) {
77  if (src[i])
78  continue;
79  if (i > 0 && src[i - 1] == 0)
80  i--;
82  }
83 #endif /* HAVE_FAST_UNALIGNED */
84 
85  if (i >= length - 1) { // no escaped 0
86  nal->data =
87  nal->raw_data = src;
88  nal->size =
89  nal->raw_size = length;
90  return length;
91  }
92 
95  if (!nal->rbsp_buffer)
96  return AVERROR(ENOMEM);
97 
98  dst = nal->rbsp_buffer;
99 
100  memcpy(dst, src, i);
101  si = di = i;
102  while (si + 2 < length) {
103  // remove escapes (very rare 1:2^22)
104  if (src[si + 2] > 3) {
105  dst[di++] = src[si++];
106  dst[di++] = src[si++];
107  } else if (src[si] == 0 && src[si + 1] == 0) {
108  if (src[si + 2] == 3) { // escape
109  dst[di++] = 0;
110  dst[di++] = 0;
111  si += 3;
112 
113  if (s && nal->skipped_bytes_pos) {
114  nal->skipped_bytes++;
115  if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
116  nal->skipped_bytes_pos_size *= 2;
120  sizeof(*nal->skipped_bytes_pos));
121  if (!nal->skipped_bytes_pos) {
122  nal->skipped_bytes_pos_size = 0;
123  return AVERROR(ENOMEM);
124  }
125  }
126  if (nal->skipped_bytes_pos)
127  nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
128  }
129  continue;
130  } else // next start code
131  goto nsc;
132  }
133 
134  dst[di++] = src[si++];
135  }
136  while (si < length)
137  dst[di++] = src[si++];
138 
139 nsc:
140  memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
141 
142  nal->data = dst;
143  nal->size = di;
144  nal->raw_data = src;
145  nal->raw_size = si;
146  return si;
147 }
148 
149 static const char *nal_unit_name(int nal_type)
150 {
151  switch(nal_type) {
152  case NAL_TRAIL_N : return "TRAIL_N";
153  case NAL_TRAIL_R : return "TRAIL_R";
154  case NAL_TSA_N : return "TSA_N";
155  case NAL_TSA_R : return "TSA_R";
156  case NAL_STSA_N : return "STSA_N";
157  case NAL_STSA_R : return "STSA_R";
158  case NAL_RADL_N : return "RADL_N";
159  case NAL_RADL_R : return "RADL_R";
160  case NAL_RASL_N : return "RASL_N";
161  case NAL_RASL_R : return "RASL_R";
162  case NAL_BLA_W_LP : return "BLA_W_LP";
163  case NAL_BLA_W_RADL : return "BLA_W_RADL";
164  case NAL_BLA_N_LP : return "BLA_N_LP";
165  case NAL_IDR_W_RADL : return "IDR_W_RADL";
166  case NAL_IDR_N_LP : return "IDR_N_LP";
167  case NAL_CRA_NUT : return "CRA_NUT";
168  case NAL_VPS : return "VPS";
169  case NAL_SPS : return "SPS";
170  case NAL_PPS : return "PPS";
171  case NAL_AUD : return "AUD";
172  case NAL_EOS_NUT : return "EOS_NUT";
173  case NAL_EOB_NUT : return "EOB_NUT";
174  case NAL_FD_NUT : return "FD_NUT";
175  case NAL_SEI_PREFIX : return "SEI_PREFIX";
176  case NAL_SEI_SUFFIX : return "SEI_SUFFIX";
177  default : return "?";
178  }
179 }
180 
181 /**
182  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
183  * 0 if the unit should be skipped, 1 otherwise
184  */
185 static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
186 {
187  GetBitContext *gb = &nal->gb;
188  int nuh_layer_id;
189 
190  if (get_bits1(gb) != 0)
191  return AVERROR_INVALIDDATA;
192 
193  nal->type = get_bits(gb, 6);
194 
195  nuh_layer_id = get_bits(gb, 6);
196  nal->temporal_id = get_bits(gb, 3) - 1;
197  if (nal->temporal_id < 0)
198  return AVERROR_INVALIDDATA;
199 
200  av_log(avctx, AV_LOG_DEBUG,
201  "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
202  nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
203 
204  return nuh_layer_id == 0;
205 }
206 
207 
209  AVCodecContext *avctx, int is_nalff, int nal_length_size)
210 {
211  int consumed, ret = 0;
212 
213  pkt->nb_nals = 0;
214  while (length >= 4) {
215  HEVCNAL *nal;
216  int extract_length = 0;
217 
218  if (is_nalff) {
219  int i;
220  for (i = 0; i < nal_length_size; i++)
221  extract_length = (extract_length << 8) | buf[i];
222  buf += nal_length_size;
223  length -= nal_length_size;
224 
225  if (extract_length > length) {
226  av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
227  return AVERROR_INVALIDDATA;
228  }
229  } else {
230  /* search start code */
231  while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
232  ++buf;
233  --length;
234  if (length < 4) {
235  av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");
236  return AVERROR_INVALIDDATA;
237  }
238  }
239 
240  buf += 3;
241  length -= 3;
242  extract_length = length;
243  }
244 
245  if (pkt->nals_allocated < pkt->nb_nals + 1) {
246  int new_size = pkt->nals_allocated + 1;
247  void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
248 
249  if (!tmp)
250  return AVERROR(ENOMEM);
251 
252  pkt->nals = tmp;
253  memset(pkt->nals + pkt->nals_allocated, 0,
254  (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
255 
256  nal = &pkt->nals[pkt->nb_nals];
257  nal->skipped_bytes_pos_size = 1024; // initial buffer size
259  if (!nal->skipped_bytes_pos)
260  return AVERROR(ENOMEM);
261 
262  pkt->nals_allocated = new_size;
263  }
264  nal = &pkt->nals[pkt->nb_nals];
265 
266  consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
267  if (consumed < 0)
268  return consumed;
269 
270  pkt->nb_nals++;
271 
272  ret = init_get_bits8(&nal->gb, nal->data, nal->size);
273  if (ret < 0)
274  return ret;
275 
276  ret = hls_nal_unit(nal, avctx);
277  if (ret <= 0) {
278  if (ret < 0) {
279  av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
280  nal->type);
281  }
282  pkt->nb_nals--;
283  }
284 
285  buf += consumed;
286  length -= consumed;
287  }
288 
289  return 0;
290 }
291 
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
GetBitContext gb
Definition: hevc.h:759
Definition: hevc.h:96
int temporal_id
Definition: hevc.h:762
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
memory handling functions
const uint8_t * raw_data
Definition: hevc.h:757
static AVPacket pkt
Definition: h264.h:118
#define AV_RN32A(p)
Definition: intreadwrite.h:526
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Definition: h264.h:119
uint8_t
const uint8_t * data
Definition: hevc.h:754
int nals_allocated
Definition: hevc.h:773
uint8_t * rbsp_buffer
Definition: hevc.h:750
#define av_log(a,...)
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int nb_nals
Definition: hevc.h:772
#define AVERROR(e)
Definition: error.h:43
int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length, AVCodecContext *avctx, int is_nalff, int nal_length_size)
Split an input packet into NAL units.
Definition: hevc_parse.c:208
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
GLsizei GLsizei * length
Definition: opengl_enc.c:115
HEVCNAL * nals
Definition: hevc.h:771
int rbsp_buffer_size
Definition: hevc.h:751
int size
Definition: hevc.h:753
int raw_size
Definition: hevc.h:756
AVS_Value src
Definition: avisynth_c.h:482
Definition: hevc.h:749
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:441
main external API structure.
Definition: avcodec.h:1502
Definition: hevc.h:110
void * buf
Definition: avisynth_c.h:553
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
Definition: h264.h:120
int skipped_bytes
Definition: hevc.h:764
#define STARTCODE_TEST
int * skipped_bytes_pos
Definition: hevc.h:766
int skipped_bytes_pos_size
Definition: hevc.h:765
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
Definition: hevc.h:97
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:208
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
Definition: hevc_parse.c:185
enum NALUnitType type
Definition: hevc.h:761
int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length, HEVCNAL *nal)
Extract the raw (unescaped) HEVC bitstream.
Definition: hevc_parse.c:32
static const char * nal_unit_name(int nal_type)
Definition: hevc_parse.c:149
#define av_malloc_array(a, b)
#define AV_RN64A(p)
Definition: intreadwrite.h:530