Libav
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 "libavutil/attributes.h"
29 #include "parser.h"
30 #include "h264data.h"
31 #include "golomb.h"
32 #include "internal.h"
33 
34 #include <assert.h>
35 
36 
37 static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
38  int buf_size)
39 {
40  int i;
41  uint32_t state;
42  ParseContext *pc = &h->parse_context;
43 // mb_addr= pc->mb_addr - 1;
44  state = pc->state;
45  if (state > 13)
46  state = 7;
47 
48  for (i = 0; i < buf_size; i++) {
49  if (state == 7) {
50  i += h->h264dsp.h264_find_start_code_candidate(buf + i, buf_size - i);
51  if (i < buf_size)
52  state = 2;
53  } else if (state <= 2) {
54  if (buf[i] == 1)
55  state ^= 5; // 2->7, 1->4, 0->5
56  else if (buf[i])
57  state = 7;
58  else
59  state >>= 1; // 2->1, 1->0, 0->0
60  } else if (state <= 5) {
61  int nalu_type = buf[i] & 0x1F;
62  if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
63  nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
64  if (pc->frame_start_found) {
65  i++;
66  goto found;
67  }
68  } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
69  nalu_type == NAL_IDR_SLICE) {
70  if (pc->frame_start_found) {
71  state += 8;
72  continue;
73  } else
74  pc->frame_start_found = 1;
75  }
76  state = 7;
77  } else {
78  if (buf[i] & 0x80)
79  goto found;
80  state = 7;
81  }
82  }
83  pc->state = state;
84  return END_NOT_FOUND;
85 
86 found:
87  pc->state = 7;
88  pc->frame_start_found = 0;
89  return i - (state & 5);
90 }
91 
93 {
94  H264Context *h = s->priv_data;
95 
96  h->slice_type_nos = s->pict_type & 3;
97 
99  get_ue_golomb(&h->gb); // redundant_pic_count
100 
101  if (ff_set_ref_count(h) < 0)
102  return AVERROR_INVALIDDATA;
103 
104  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
105  int list;
106  for (list = 0; list < h->list_count; list++) {
107  if (get_bits1(&h->gb)) {
108  int index;
109  for (index = 0; ; index++) {
110  unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
111 
112  if (reordering_of_pic_nums_idc < 3)
113  get_ue_golomb(&h->gb);
114  else if (reordering_of_pic_nums_idc > 3) {
116  "illegal reordering_of_pic_nums_idc %d\n",
117  reordering_of_pic_nums_idc);
118  return AVERROR_INVALIDDATA;
119  } else
120  break;
121 
122  if (index >= h->ref_count[list]) {
124  "reference count %d overflow\n", index);
125  return AVERROR_INVALIDDATA;
126  }
127  }
128  }
129  }
130  }
131 
135 
136  if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
137  int i;
138  for (i = 0; i < MAX_MMCO_COUNT; i++) {
139  MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
140  if (opcode > (unsigned) MMCO_LONG) {
142  "illegal memory management control operation %d\n",
143  opcode);
144  return AVERROR_INVALIDDATA;
145  }
146  if (opcode == MMCO_END)
147  return 0;
148  else if (opcode == MMCO_RESET)
149  return 1;
150 
151  if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
152  get_ue_golomb(&h->gb);
153  if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
154  opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
155  get_ue_golomb_31(&h->gb);
156  }
157  }
158 
159  return 0;
160 }
161 
171  AVCodecContext *avctx,
172  const uint8_t *buf, int buf_size)
173 {
174  H264Context *h = s->priv_data;
175  const uint8_t *buf_end = buf + buf_size;
176  unsigned int pps_id;
177  unsigned int slice_type;
178  int state = -1, got_reset = 0;
179  const uint8_t *ptr;
180  int field_poc[2];
181 
182  /* set some sane default values */
184  s->key_frame = 0;
186 
187  h->avctx = avctx;
189 
190  if (!buf_size)
191  return 0;
192 
193  for (;;) {
194  int src_length, dst_length, consumed;
195  buf = avpriv_find_start_code(buf, buf_end, &state);
196  if (buf >= buf_end)
197  break;
198  --buf;
199  src_length = buf_end - buf;
200  switch (state & 0x1f) {
201  case NAL_SLICE:
202  case NAL_IDR_SLICE:
203  // Do not walk the whole buffer just to decode slice header
204  if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
205  /* IDR or disposable slice
206  * No need to decode many bytes because MMCOs shall not be present. */
207  if (src_length > 60)
208  src_length = 60;
209  } else {
210  /* To decode up to MMCOs */
211  if (src_length > 1000)
212  src_length = 1000;
213  }
214  break;
215  }
216  ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
217  if (ptr == NULL || dst_length < 0)
218  break;
219 
220  init_get_bits(&h->gb, ptr, 8 * dst_length);
221  switch (h->nal_unit_type) {
222  case NAL_SPS:
224  break;
225  case NAL_PPS:
227  break;
228  case NAL_SEI:
230  break;
231  case NAL_IDR_SLICE:
232  s->key_frame = 1;
233 
234  h->prev_frame_num = 0;
235  h->prev_frame_num_offset = 0;
236  h->prev_poc_msb =
237  h->prev_poc_lsb = 0;
238  /* fall through */
239  case NAL_SLICE:
240  get_ue_golomb(&h->gb); // skip first_mb_in_slice
241  slice_type = get_ue_golomb_31(&h->gb);
242  s->pict_type = golomb_to_pict_type[slice_type % 5];
243  if (h->sei_recovery_frame_cnt >= 0) {
244  /* key frame, since recovery_frame_cnt is set */
245  s->key_frame = 1;
246  }
247  pps_id = get_ue_golomb(&h->gb);
248  if (pps_id >= MAX_PPS_COUNT) {
250  "pps_id %u out of range\n", pps_id);
251  return -1;
252  }
253  if (!h->pps_buffers[pps_id]) {
255  "non-existing PPS %u referenced\n", pps_id);
256  return -1;
257  }
258  h->pps = *h->pps_buffers[pps_id];
259  if (!h->sps_buffers[h->pps.sps_id]) {
261  "non-existing SPS %u referenced\n", h->pps.sps_id);
262  return -1;
263  }
264  h->sps = *h->sps_buffers[h->pps.sps_id];
266 
267  avctx->profile = ff_h264_get_profile(&h->sps);
268  avctx->level = h->sps.level_idc;
269 
270  if (h->sps.frame_mbs_only_flag) {
272  } else {
273  if (get_bits1(&h->gb)) { // field_pic_flag
274  h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
275  } else {
277  }
278  }
279 
280  if (h->nal_unit_type == NAL_IDR_SLICE)
281  get_ue_golomb(&h->gb); /* idr_pic_id */
282  if (h->sps.poc_type == 0) {
283  h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
284 
285  if (h->pps.pic_order_present == 1 &&
288  }
289 
290  if (h->sps.poc_type == 1 &&
292  h->delta_poc[0] = get_se_golomb(&h->gb);
293 
294  if (h->pps.pic_order_present == 1 &&
296  h->delta_poc[1] = get_se_golomb(&h->gb);
297  }
298 
299  /* Decode POC of this picture.
300  * The prev_ values needed for decoding POC of the next picture are not set here. */
301  field_poc[0] = field_poc[1] = INT_MAX;
302  ff_init_poc(h, field_poc, &s->output_picture_number);
303 
304  /* Continue parsing to check if MMCO_RESET is present.
305  * FIXME: MMCO_RESET could appear in non-first slice.
306  * Maybe, we should parse all undisposable non-IDR slice of this
307  * picture until encountering MMCO_RESET in a slice of it. */
308  if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
309  got_reset = scan_mmco_reset(s);
310  if (got_reset < 0)
311  return got_reset;
312  }
313 
314  /* Set up the prev_ values for decoding POC of the next picture. */
315  h->prev_frame_num = got_reset ? 0 : h->frame_num;
316  h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
317  if (h->nal_ref_idc != 0) {
318  if (!got_reset) {
319  h->prev_poc_msb = h->poc_msb;
320  h->prev_poc_lsb = h->poc_lsb;
321  } else {
322  h->prev_poc_msb = 0;
323  h->prev_poc_lsb =
324  h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
325  }
326  }
327 
328  if (h->sps.pic_struct_present_flag) {
329  switch (h->sei_pic_struct) {
332  s->repeat_pict = 0;
333  break;
337  s->repeat_pict = 1;
338  break;
341  s->repeat_pict = 2;
342  break;
344  s->repeat_pict = 3;
345  break;
347  s->repeat_pict = 5;
348  break;
349  default:
350  s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
351  break;
352  }
353  } else {
354  s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
355  }
356 
357  if (h->picture_structure == PICT_FRAME) {
359  if (h->sps.pic_struct_present_flag) {
360  switch (h->sei_pic_struct) {
364  break;
368  break;
369  default:
371  break;
372  }
373  } else {
374  if (field_poc[0] < field_poc[1])
376  else if (field_poc[0] > field_poc[1])
378  else
380  }
381  } else {
384  else
387  }
388 
389  return 0; /* no need to evaluate the rest */
390  }
391  buf += consumed;
392  }
393  /* didn't find a picture! */
394  av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
395  return -1;
396 }
397 
399  AVCodecContext *avctx,
400  const uint8_t **poutbuf, int *poutbuf_size,
401  const uint8_t *buf, int buf_size)
402 {
403  H264Context *h = s->priv_data;
404  ParseContext *pc = &h->parse_context;
405  int next;
406 
407  if (!h->got_first) {
408  h->got_first = 1;
409  if (avctx->extradata_size) {
410  h->avctx = avctx;
411  // must be done like in the decoder.
412  // otherwise opening the parser, creating extradata,
413  // and then closing and opening again
414  // will cause has_b_frames to be always set.
415  // NB: estimate_timings_from_pts behaves exactly like this.
416  if (!avctx->has_b_frames)
417  h->low_delay = 1;
419  }
420  }
421 
423  next = buf_size;
424  } else {
425  next = h264_find_frame_end(h, buf, buf_size);
426 
427  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
428  *poutbuf = NULL;
429  *poutbuf_size = 0;
430  return buf_size;
431  }
432 
433  if (next < 0 && next != END_NOT_FOUND) {
434  assert(pc->last_index + next >= 0);
435  h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
436  }
437  }
438 
439  parse_nal_units(s, avctx, buf, buf_size);
440 
441  if (h->sei_cpb_removal_delay >= 0) {
445  } else {
446  s->dts_sync_point = INT_MIN;
447  s->dts_ref_dts_delta = INT_MIN;
448  s->pts_dts_delta = INT_MIN;
449  }
450 
451  if (s->flags & PARSER_FLAG_ONCE) {
453  }
454 
455  *poutbuf = buf;
456  *poutbuf_size = buf_size;
457  return next;
458 }
459 
460 static int h264_split(AVCodecContext *avctx,
461  const uint8_t *buf, int buf_size)
462 {
463  int i;
464  uint32_t state = -1;
465  int has_sps = 0;
466 
467  for (i = 0; i <= buf_size; i++) {
468  if ((state & 0xFFFFFF1F) == 0x107)
469  has_sps = 1;
470  /* if((state&0xFFFFFF1F) == 0x101 ||
471  * (state&0xFFFFFF1F) == 0x102 ||
472  * (state&0xFFFFFF1F) == 0x105) {
473  * }
474  */
475  if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
476  (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
477  if (has_sps) {
478  while (i > 4 && buf[i - 5] == 0)
479  i--;
480  return i - 4;
481  }
482  }
483  if (i < buf_size)
484  state = (state << 8) | buf[i];
485  }
486  return 0;
487 }
488 
490 {
491  H264Context *h = s->priv_data;
492  ParseContext *pc = &h->parse_context;
493 
494  av_free(pc->buffer);
496 }
497 
499 {
500  H264Context *h = s->priv_data;
501  h->thread_context[0] = h;
502  h->slice_context_count = 1;
503  ff_h264dsp_init(&h->h264dsp, 8, 1);
504  return 0;
505 }
506 
509  .priv_data_size = sizeof(H264Context),
510  .parser_init = init,
511  .parser_parse = h264_parse,
512  .parser_close = close,
513  .split = h264_split,
514 };