Libav
hevc_parser.c
Go to the documentation of this file.
1 /*
2  * HEVC Annex B format parser
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/common.h"
24 
25 #include "parser.h"
26 #include "hevc.h"
27 
28 #define START_CODE 0x000001
29 
30 
35  int buf_size)
36 {
37  int i;
38  ParseContext *pc = s->priv_data;
39 
40  for (i = 0; i < buf_size; i++) {
41  int nut;
42 
43  pc->state64 = (pc->state64 << 8) | buf[i];
44 
45  if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
46  continue;
47 
48  nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
49  // Beginning of access unit
50  if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
51  (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
52  if (pc->frame_start_found) {
53  pc->frame_start_found = 0;
54  return i - 5;
55  }
56  } else if (nut <= NAL_RASL_R ||
57  (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
58  int first_slice_segment_in_pic_flag = buf[i] >> 7;
59  if (first_slice_segment_in_pic_flag) {
60  if (!pc->frame_start_found) {
61  pc->frame_start_found = 1;
62  s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
63  } else { // First slice of next frame found
64  pc->frame_start_found = 0;
65  return i - 5;
66  }
67  }
68  }
69  }
70 
71  return END_NOT_FOUND;
72 }
73 
75  const uint8_t **poutbuf, int *poutbuf_size,
76  const uint8_t *buf, int buf_size)
77 {
78  int next;
79  ParseContext *pc = s->priv_data;
80 
82  next = buf_size;
83  } else {
84  next = hevc_find_frame_end(s, buf, buf_size);
85  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
86  *poutbuf = NULL;
87  *poutbuf_size = 0;
88  return buf_size;
89  }
90  }
91 
92  *poutbuf = buf;
93  *poutbuf_size = buf_size;
94  return next;
95 }
96 
97 // Split after the parameter sets at the beginning of the stream if they exist.
98 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
99 {
100  int i;
101  uint32_t state = -1;
102  int has_ps = 0;
103 
104  for (i = 0; i < buf_size; i++) {
105  state = (state << 8) | buf[i];
106  if (((state >> 8) & 0xFFFFFF) == START_CODE) {
107  int nut = (state >> 1) & 0x3F;
108  if (nut >= NAL_VPS && nut <= NAL_PPS)
109  has_ps = 1;
110  else if (has_ps)
111  return i - 3;
112  else // no parameter set at the beginning of the stream
113  return 0;
114  }
115  }
116  return 0;
117 }
118 
121  .priv_data_size = sizeof(ParseContext),
122  .parser_parse = hevc_parse,
123  .parser_close = ff_parse_close,
124  .split = hevc_split,
125 };
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:98
int codec_ids[5]
Definition: avcodec.h:3822
int frame_start_found
Definition: parser.h:34
uint8_t
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:217
static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: hevc_parser.c:34
static char * split(char *message, char delim)
Definition: af_channelmap.c:86
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:287
Definition: h264.h:116
NULL
Definition: eval.c:55
#define START_CODE
start_code_prefix_one_3bytes
Definition: hevc_parser.c:28
main external API structure.
Definition: avcodec.h:1050
Definition: hevc.h:110
AVCodecParser ff_hevc_parser
Definition: hevc_parser.c:119
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
#define END_NOT_FOUND
Definition: parser.h:40
static uint32_t state
Definition: trasher.c:27
Definition: h264.h:117
common internal and external API header
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3700
static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:74
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3714