h264_parser.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... parser
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "parser.h"
29 #include "h264data.h"
30 #include "golomb.h"
31 
32 #include <assert.h>
33 
34 
35 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
36 {
37  int i;
38  uint32_t state;
39  ParseContext *pc = &(h->s.parse_context);
40 // mb_addr= pc->mb_addr - 1;
41  state= pc->state;
42  if(state>13)
43  state= 7;
44 
45  for(i=0; i<buf_size; i++){
46  if(state==7){
47 #if HAVE_FAST_UNALIGNED
48  /* we check i<buf_size instead of i+3/7 because its simpler
49  * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
50  */
51 # if HAVE_FAST_64BIT
52  while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
53  i+=8;
54 # else
55  while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
56  i+=4;
57 # endif
58 #endif
59  for(; i<buf_size; i++){
60  if(!buf[i]){
61  state=2;
62  break;
63  }
64  }
65  }else if(state<=2){
66  if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
67  else if(buf[i]) state = 7;
68  else state>>=1; //2->1, 1->0, 0->0
69  }else if(state<=5){
70  int v= buf[i] & 0x1F;
71  if(v==6 || v==7 || v==8 || v==9){
72  if(pc->frame_start_found){
73  i++;
74  goto found;
75  }
76  }else if(v==1 || v==2 || v==5){
77  if(pc->frame_start_found){
78  state+=8;
79  continue;
80  }else
81  pc->frame_start_found = 1;
82  }
83  state= 7;
84  }else{
85  if(buf[i] & 0x80)
86  goto found;
87  state= 7;
88  }
89  }
90  pc->state= state;
91  return END_NOT_FOUND;
92 
93 found:
94  pc->state=7;
95  pc->frame_start_found= 0;
96  return i-(state&5);
97 }
98 
108  AVCodecContext *avctx,
109  const uint8_t *buf, int buf_size)
110 {
111  H264Context *h = s->priv_data;
112  const uint8_t *buf_end = buf + buf_size;
113  unsigned int pps_id;
114  unsigned int slice_type;
115  int state = -1;
116  const uint8_t *ptr;
117 
118  /* set some sane default values */
120  s->key_frame = 0;
121 
122  h->s.avctx= avctx;
123  h->sei_recovery_frame_cnt = -1;
124  h->sei_dpb_output_delay = 0;
125  h->sei_cpb_removal_delay = -1;
127 
128  if (!buf_size)
129  return 0;
130 
131  for(;;) {
132  int src_length, dst_length, consumed;
133  buf = avpriv_mpv_find_start_code(buf, buf_end, &state);
134  if(buf >= buf_end)
135  break;
136  --buf;
137  src_length = buf_end - buf;
138  switch (state & 0x1f) {
139  case NAL_SLICE:
140  case NAL_IDR_SLICE:
141  // Do not walk the whole buffer just to decode slice header
142  if (src_length > 20)
143  src_length = 20;
144  break;
145  }
146  ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
147  if (ptr==NULL || dst_length < 0)
148  break;
149 
150  init_get_bits(&h->s.gb, ptr, 8*dst_length);
151  switch(h->nal_unit_type) {
152  case NAL_SPS:
154  break;
155  case NAL_PPS:
157  break;
158  case NAL_SEI:
160  break;
161  case NAL_IDR_SLICE:
162  s->key_frame = 1;
163  /* fall through */
164  case NAL_SLICE:
165  get_ue_golomb(&h->s.gb); // skip first_mb_in_slice
166  slice_type = get_ue_golomb_31(&h->s.gb);
167  s->pict_type = golomb_to_pict_type[slice_type % 5];
168  if (h->sei_recovery_frame_cnt >= 0) {
169  /* key frame, since recovery_frame_cnt is set */
170  s->key_frame = 1;
171  }
172  pps_id= get_ue_golomb(&h->s.gb);
173  if(pps_id>=MAX_PPS_COUNT) {
174  av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
175  return -1;
176  }
177  if(!h->pps_buffers[pps_id]) {
178  av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
179  return -1;
180  }
181  h->pps= *h->pps_buffers[pps_id];
182  if(!h->sps_buffers[h->pps.sps_id]) {
183  av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
184  return -1;
185  }
186  h->sps = *h->sps_buffers[h->pps.sps_id];
188 
189  avctx->profile = ff_h264_get_profile(&h->sps);
190  avctx->level = h->sps.level_idc;
191 
192  if(h->sps.frame_mbs_only_flag){
194  }else{
195  if(get_bits1(&h->s.gb)) { //field_pic_flag
196  h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
197  } else {
199  }
200  }
201 
203  switch (h->sei_pic_struct) {
206  s->repeat_pict = 0;
207  break;
211  s->repeat_pict = 1;
212  break;
215  s->repeat_pict = 2;
216  break;
218  s->repeat_pict = 3;
219  break;
221  s->repeat_pict = 5;
222  break;
223  default:
224  s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
225  break;
226  }
227  } else {
228  s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
229  }
230 
231  return 0; /* no need to evaluate the rest */
232  }
233  buf += consumed;
234  }
235  /* didn't find a picture! */
236  av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n");
237  return -1;
238 }
239 
241  AVCodecContext *avctx,
242  const uint8_t **poutbuf, int *poutbuf_size,
243  const uint8_t *buf, int buf_size)
244 {
245  H264Context *h = s->priv_data;
246  ParseContext *pc = &h->s.parse_context;
247  int next;
248 
249  if (!h->got_first) {
250  h->got_first = 1;
251  if (avctx->extradata_size) {
252  h->s.avctx = avctx;
253  // must be done like in the decoder.
254  // otherwise opening the parser, creating extradata,
255  // and then closing and opening again
256  // will cause has_b_frames to be always set.
257  // NB: estimate_timings_from_pts behaves exactly like this.
258  if (!avctx->has_b_frames)
259  h->s.low_delay = 1;
261  }
262  }
263 
265  next= buf_size;
266  }else{
267  next= ff_h264_find_frame_end(h, buf, buf_size);
268 
269  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
270  *poutbuf = NULL;
271  *poutbuf_size = 0;
272  return buf_size;
273  }
274 
275  if(next<0 && next != END_NOT_FOUND){
276  assert(pc->last_index + next >= 0 );
277  ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
278  }
279  }
280 
281  parse_nal_units(s, avctx, buf, buf_size);
282 
283  if (h->sei_cpb_removal_delay >= 0) {
287  } else {
288  s->dts_sync_point = INT_MIN;
289  s->dts_ref_dts_delta = INT_MIN;
290  s->pts_dts_delta = INT_MIN;
291  }
292 
293  if (s->flags & PARSER_FLAG_ONCE) {
295  }
296 
297  *poutbuf = buf;
298  *poutbuf_size = buf_size;
299  return next;
300 }
301 
302 static int h264_split(AVCodecContext *avctx,
303  const uint8_t *buf, int buf_size)
304 {
305  int i;
306  uint32_t state = -1;
307  int has_sps= 0;
308 
309  for(i=0; i<=buf_size; i++){
310  if((state&0xFFFFFF1F) == 0x107)
311  has_sps=1;
312 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
313  }*/
314  if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
315  if(has_sps){
316  while(i>4 && buf[i-5]==0) i--;
317  return i-4;
318  }
319  }
320  if (i<buf_size)
321  state= (state<<8) | buf[i];
322  }
323  return 0;
324 }
325 
327 {
328  H264Context *h = s->priv_data;
329  ParseContext *pc = &h->s.parse_context;
330 
331  av_free(pc->buffer);
333 }
334 
336 {
337  H264Context *h = s->priv_data;
338  h->thread_context[0] = h;
339  h->s.slice_context_count = 1;
340  return 0;
341 }
342 
345  .priv_data_size = sizeof(H264Context),
346  .parser_init = init,
347  .parser_parse = h264_parse,
348  .parser_close = close,
349  .split = h264_split,
350 };
int ff_h264_decode_seq_parameter_set(H264Context *h)
Decode SPS.
Definition: h264_ps.c:305
#define PICT_TOP_FIELD
Definition: mpegvideo.h:639
5: top field, bottom field, top field repeated, in that order
Definition: h264.h:138
int sei_cpb_removal_delay
cpb_removal_delay in picture timing SEI message, see H.264 C.1.2
Definition: h264.h:561
3: top field, bottom field, in that order
Definition: h264.h:136
const uint8_t * ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length)
Decode a network abstraction layer unit.
Definition: h264.c:170
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
7: frame doubling
Definition: h264.h:140
#define MAX_PPS_COUNT
Definition: h264.h:43
int codec_ids[5]
Definition: avcodec.h:3855
int frame_mbs_only_flag
Definition: h264.h:163
int ff_h264_get_profile(SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264.c:2393
H264Context.
Definition: h264.h:254
struct H264Context H264Context
H264Context.
int dts_ref_dts_delta
Offset of the current timestamp against last timestamp sync point in units of AVCodecContext.time_base.
Definition: avcodec.h:3813
4: bottom field, top field, in that order
Definition: h264.h:137
int profile
profile
Definition: avcodec.h:2815
int frame_start_found
Definition: parser.h:34
static const uint8_t golomb_to_pict_type[5]
Definition: h264data.h:38
uint8_t
#define PICT_FRAME
Definition: mpegvideo.h:641
Definition: h264.h:108
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int frame_num
Definition: h264.h:465
int got_first
this flag is != 0 if we've parsed a frame
Definition: h264.h:449
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:289
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1634
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
int nal_unit_type
Definition: h264.h:440
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:215
PPS pps
current pps
Definition: h264.h:334
0: frame
Definition: h264.h:133
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:570
GetBitContext gb
Definition: mpegvideo.h:626
static char * split(char *message, char delim)
Definition: af_channelmap.c:86
int last_index
Definition: parser.h:31
static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse NAL units of found picture and decode some basic information.
Definition: h264_parser.c:107
SPS sps
current sps
Definition: h264.h:329
int size_in_bits
Definition: get_bits.h:55
PPS * pps_buffers[MAX_PPS_COUNT]
Definition: h264.h:454
int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
Decode PPS.
Definition: h264_ps.c:490
int level
level
Definition: avcodec.h:2885
AVCodecParser ff_h264_parser
Definition: h264_parser.c:343
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:453
struct H264Context * thread_context[MAX_THREADS]
Definition: h264.h:509
int pts_dts_delta
Presentation delay of current frame in units of AVCodecContext.time_base.
Definition: avcodec.h:3827
NULL
Definition: eval.c:52
const uint8_t * avpriv_mpv_find_start_code(const uint8_t *restrict p, const uint8_t *end, uint32_t *restrict state)
Definition: mpegvideo.c:147
uint8_t * buffer
Definition: parser.h:29
H264 / AVC / MPEG4 part10 codec data table
MpegEncContext s
Definition: h264.h:255
Definition: h264.h:109
1: top field
Definition: h264.h:134
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:96
int ff_h264_decode_sei(H264Context *h)
Decode SEI.
Definition: h264_sei.c:164
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
2: bottom field
Definition: h264.h:135
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1455
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
SEI_PicStructType sei_pic_struct
pic_struct in picture timing SEI message
Definition: h264.h:536
static int h264_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: h264_parser.c:302
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
#define END_NOT_FOUND
Definition: parser.h:40
unsigned int sps_id
Definition: h264.h:208
6: bottom field, top field, bottom field repeated, in that order
Definition: h264.h:139
int sei_buffering_period_present
Buffering period SEI flag.
Definition: h264.h:576
static uint32_t state
Definition: trasher.c:27
int pic_struct_present_flag
Definition: h264.h:191
av_cold void ff_h264_free_context(H264Context *h)
Free any data that may have been allocated in the H264 context like SPS, PPS etc. ...
Definition: h264.c:4256
struct AVCodecContext * avctx
Definition: mpegvideo.h:213
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:152
int picture_structure
Definition: mpegvideo.h:637
Definition: h264.h:107
int ff_h264_decode_extradata(H264Context *h)
Definition: h264.c:989
ParseContext parse_context
Definition: mpegvideo.h:512
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:3737
static int h264_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: h264_parser.c:240
8: frame tripling
Definition: h264.h:141
exp golomb vlc stuff
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3767
int sei_recovery_frame_cnt
recovery_frame_cnt from SEI message
Definition: h264.h:570
int level_idc
Definition: h264.h:149
int dts_sync_point
Synchronization point for start of timestamp generation.
Definition: avcodec.h:3798
static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
Definition: h264_parser.c:35
int sei_dpb_output_delay
dpb_output_delay in picture timing SEI message, see H.264 C.2.2
Definition: h264.h:556