]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_parser.c
hevc: store the escaped/raw bitstream in HEVCNAL
[ffmpeg] / libavcodec / hevc_parser.c
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 ///< start_code_prefix_one_3bytes
29
30 /**
31  * Find the end of the current frame in the bitstream.
32  * @return the position of the first byte of the next frame, or END_NOT_FOUND
33  */
34 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
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
74 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
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
81     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
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
119 AVCodecParser ff_hevc_parser = {
120     .codec_ids      = { AV_CODEC_ID_HEVC },
121     .priv_data_size = sizeof(ParseContext),
122     .parser_parse   = hevc_parse,
123     .parser_close   = ff_parse_close,
124     .split          = hevc_split,
125 };