FFmpeg  1.2.12
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #define UNCHECKED_BITSTREAM_READER 1
29 
30 #include "parser.h"
31 #include "h264data.h"
32 #include "golomb.h"
33 
34 
35 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
36 {
37  int i, j;
38  uint32_t state;
39  ParseContext *pc = &h->parse_context;
40  int next_avc= h->is_avc ? 0 : buf_size;
41 
42 // mb_addr= pc->mb_addr - 1;
43  state= pc->state;
44  if(state>13)
45  state= 7;
46 
47  if(h->is_avc && !h->nal_length_size)
48  av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
49 
50  for(i=0; i<buf_size; i++){
51  if(i >= next_avc) {
52  int nalsize = 0;
53  i = next_avc;
54  for(j = 0; j < h->nal_length_size; j++)
55  nalsize = (nalsize << 8) | buf[i++];
56  if(nalsize <= 0 || nalsize > buf_size - i){
57  av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
58  return buf_size;
59  }
60  next_avc= i + nalsize;
61  state= 5;
62  }
63 
64  if(state==7){
65 #if HAVE_FAST_UNALIGNED
66  /* we check i<buf_size instead of i+3/7 because its simpler
67  * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
68  */
69 # if HAVE_FAST_64BIT
70  while(i<next_avc && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
71  i+=8;
72 # else
73  while(i<next_avc && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
74  i+=4;
75 # endif
76 #endif
77  for(; i<next_avc; i++){
78  if(!buf[i]){
79  state=2;
80  break;
81  }
82  }
83  }else if(state<=2){
84  if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
85  else if(buf[i]) state = 7;
86  else state>>=1; //2->1, 1->0, 0->0
87  }else if(state<=5){
88  int v= buf[i] & 0x1F;
89  if(v==6 || v==7 || v==8 || v==9){
90  if(pc->frame_start_found){
91  i++;
92  goto found;
93  }
94  }else if(v==1 || v==2 || v==5){
95  state+=8;
96  continue;
97  }
98  state= 7;
99  }else{
100  h->parse_history[h->parse_history_count++]= buf[i];
101  if(h->parse_history_count>3){
102  unsigned int mb, last_mb= h->parse_last_mb;
103  GetBitContext gb;
104 
106  h->parse_history_count=0;
107  mb= get_ue_golomb_long(&gb);
108  last_mb= h->parse_last_mb;
109  h->parse_last_mb= mb;
110  if(pc->frame_start_found){
111  if(mb <= last_mb)
112  goto found;
113  }else
114  pc->frame_start_found = 1;
115  state= 7;
116  }
117  }
118  }
119  pc->state= state;
120  if(h->is_avc)
121  return next_avc;
122  return END_NOT_FOUND;
123 
124 found:
125  pc->state=7;
126  pc->frame_start_found= 0;
127  if(h->is_avc)
128  return next_avc;
129  return i-(state&5) - 3*(state>7);
130 }
131 
141  AVCodecContext *avctx,
142  const uint8_t * const buf, int buf_size)
143 {
144  H264Context *h = s->priv_data;
145  int buf_index, next_avc;
146  unsigned int pps_id;
147  unsigned int slice_type;
148  int state = -1;
149  const uint8_t *ptr;
150  int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
151 
152  /* set some sane default values */
154  s->key_frame = 0;
155 
156  h->avctx= avctx;
157  h->sei_recovery_frame_cnt = -1;
158  h->sei_dpb_output_delay = 0;
159  h->sei_cpb_removal_delay = -1;
161 
162  if (!buf_size)
163  return 0;
164 
165  buf_index = 0;
166  next_avc = h->is_avc ? 0 : buf_size;
167  for(;;) {
168  int src_length, dst_length, consumed, nalsize = 0;
169 
170  if (buf_index >= next_avc) {
171  nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
172  if (nalsize < 0)
173  break;
174  next_avc = buf_index + nalsize;
175  } else {
176  buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
177  if (buf_index >= buf_size)
178  break;
179  if (buf_index >= next_avc)
180  continue;
181  }
182  src_length = next_avc - buf_index;
183 
184  state = buf[buf_index];
185  switch (state & 0x1f) {
186  case NAL_SLICE:
187  case NAL_IDR_SLICE:
188  // Do not walk the whole buffer just to decode slice header
189  if (src_length > 20)
190  src_length = 20;
191  break;
192  }
193  ptr= ff_h264_decode_nal(h, buf + buf_index, &dst_length, &consumed, src_length);
194  if (ptr==NULL || dst_length < 0)
195  break;
196 
197  buf_index += consumed;
198 
199  init_get_bits(&h->gb, ptr, 8*dst_length);
200  switch(h->nal_unit_type) {
201  case NAL_SPS:
203  break;
204  case NAL_PPS:
206  break;
207  case NAL_SEI:
209  break;
210  case NAL_IDR_SLICE:
211  s->key_frame = 1;
212  /* fall through */
213  case NAL_SLICE:
214  get_ue_golomb_long(&h->gb); // skip first_mb_in_slice
215  slice_type = get_ue_golomb_31(&h->gb);
216  s->pict_type = golomb_to_pict_type[slice_type % 5];
217  if (h->sei_recovery_frame_cnt >= 0) {
218  /* key frame, since recovery_frame_cnt is set */
219  s->key_frame = 1;
220  }
221  pps_id= get_ue_golomb(&h->gb);
222  if(pps_id>=MAX_PPS_COUNT) {
223  av_log(h->avctx, AV_LOG_ERROR, "pps_id out of range\n");
224  return -1;
225  }
226  if(!h->pps_buffers[pps_id]) {
227  av_log(h->avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
228  return -1;
229  }
230  h->pps= *h->pps_buffers[pps_id];
231  if(!h->sps_buffers[h->pps.sps_id]) {
232  av_log(h->avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
233  return -1;
234  }
235  h->sps = *h->sps_buffers[h->pps.sps_id];
237 
238  avctx->profile = ff_h264_get_profile(&h->sps);
239  avctx->level = h->sps.level_idc;
240 
241  if(h->sps.frame_mbs_only_flag){
243  }else{
244  if(get_bits1(&h->gb)) { //field_pic_flag
245  h->picture_structure= PICT_TOP_FIELD + get_bits1(&h->gb); //bottom_field_flag
246  } else {
248  }
249  }
250 
252  switch (h->sei_pic_struct) {
255  s->repeat_pict = 0;
256  break;
260  s->repeat_pict = 1;
261  break;
264  s->repeat_pict = 2;
265  break;
267  s->repeat_pict = 3;
268  break;
270  s->repeat_pict = 5;
271  break;
272  default:
273  s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
274  break;
275  }
276  } else {
277  s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
278  }
279 
280  return 0; /* no need to evaluate the rest */
281  }
282  }
283  if (q264)
284  return 0;
285  /* didn't find a picture! */
286  av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
287  return -1;
288 }
289 
291  AVCodecContext *avctx,
292  const uint8_t **poutbuf, int *poutbuf_size,
293  const uint8_t *buf, int buf_size)
294 {
295  H264Context *h = s->priv_data;
296  ParseContext *pc = &h->parse_context;
297  int next;
298 
299  if (!h->got_first) {
300  h->got_first = 1;
301  if (avctx->extradata_size) {
302  h->avctx = avctx;
303  // must be done like in decoder, otherwise opening the parser,
304  // letting it create extradata and then closing and opening again
305  // will cause has_b_frames to be always set.
306  // Note that estimate_timings_from_pts does exactly this.
307  if (!avctx->has_b_frames)
308  h->low_delay = 1;
310  }
311  }
312 
314  next= buf_size;
315  }else{
316  next= ff_h264_find_frame_end(h, buf, buf_size);
317 
318  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
319  *poutbuf = NULL;
320  *poutbuf_size = 0;
321  return buf_size;
322  }
323 
324  if(next<0 && next != END_NOT_FOUND){
325  av_assert1(pc->last_index + next >= 0 );
326  ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
327  }
328  }
329 
330  parse_nal_units(s, avctx, buf, buf_size);
331 
332  if (h->sei_cpb_removal_delay >= 0) {
336  } else {
337  s->dts_sync_point = INT_MIN;
338  s->dts_ref_dts_delta = INT_MIN;
339  s->pts_dts_delta = INT_MIN;
340  }
341 
342  if (s->flags & PARSER_FLAG_ONCE) {
344  }
345 
346  *poutbuf = buf;
347  *poutbuf_size = buf_size;
348  return next;
349 }
350 
351 static int h264_split(AVCodecContext *avctx,
352  const uint8_t *buf, int buf_size)
353 {
354  int i;
355  uint32_t state = -1;
356  int has_sps= 0;
357 
358  for(i=0; i<=buf_size; i++){
359  if((state&0xFFFFFF1F) == 0x107)
360  has_sps=1;
361 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
362  }*/
363  if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
364  if(has_sps){
365  while(i>4 && buf[i-5]==0) i--;
366  return i-4;
367  }
368  }
369  if (i<buf_size)
370  state= (state<<8) | buf[i];
371  }
372  return 0;
373 }
374 
376 {
377  H264Context *h = s->priv_data;
378  ParseContext *pc = &h->parse_context;
379 
380  av_free(pc->buffer);
382 }
383 
385 {
386  H264Context *h = s->priv_data;
387  h->thread_context[0] = h;
388  h->slice_context_count = 1;
389  return 0;
390 }
391 
394  .priv_data_size = sizeof(H264Context),
395  .parser_init = init,
396  .parser_parse = h264_parse,
397  .parser_close = close,
398  .split = h264_split,
399 };