]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_parser.c
lavu: Remove bit packing from AVComponentDescriptor
[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 "golomb.h"
26 #include "hevc.h"
27 #include "parser.h"
28
29 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
30
31 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
32
33 typedef struct HEVCParserContext {
34     ParseContext pc;
35
36     HEVCPacket pkt;
37     HEVCParamSets ps;
38
39     int parsed_extradata;
40 } HEVCParserContext;
41
42 static int hevc_parse_slice_header(AVCodecParserContext *s, HEVCNAL *nal,
43                                    AVCodecContext *avctx)
44 {
45     HEVCParserContext *ctx = s->priv_data;
46     GetBitContext *gb = &nal->gb;
47
48     HEVCPPS *pps;
49     HEVCSPS *sps;
50     unsigned int pps_id;
51
52     get_bits1(gb);          // first slice in pic
53     if (IS_IRAP_NAL(nal))
54         get_bits1(gb);      // no output of prior pics
55
56     pps_id = get_ue_golomb_long(gb);
57     if (pps_id >= MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
58         av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
59         return AVERROR_INVALIDDATA;
60     }
61     pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
62     sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
63
64     /* export the stream parameters */
65     s->coded_width  = sps->width;
66     s->coded_height = sps->height;
67     s->width        = sps->output_width;
68     s->height       = sps->output_height;
69     s->format       = sps->pix_fmt;
70     avctx->profile  = sps->ptl.general_ptl.profile_idc;
71     avctx->level    = sps->ptl.general_ptl.level_idc;
72
73     /* ignore the rest for now*/
74
75     return 0;
76 }
77
78 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
79                            int buf_size, AVCodecContext *avctx)
80 {
81     HEVCParserContext *ctx = s->priv_data;
82     int ret, i;
83
84     ret = ff_hevc_split_packet(&ctx->pkt, buf, buf_size, avctx, 0, 0);
85     if (ret < 0)
86         return ret;
87
88     for (i = 0; i < ctx->pkt.nb_nals; i++) {
89         HEVCNAL *nal = &ctx->pkt.nals[i];
90
91         /* ignore everything except parameter sets and VCL NALUs */
92         switch (nal->type) {
93         case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps);    break;
94         case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
95         case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps);    break;
96         case NAL_TRAIL_R:
97         case NAL_TRAIL_N:
98         case NAL_TSA_N:
99         case NAL_TSA_R:
100         case NAL_STSA_N:
101         case NAL_STSA_R:
102         case NAL_BLA_W_LP:
103         case NAL_BLA_W_RADL:
104         case NAL_BLA_N_LP:
105         case NAL_IDR_W_RADL:
106         case NAL_IDR_N_LP:
107         case NAL_CRA_NUT:
108         case NAL_RADL_N:
109         case NAL_RADL_R:
110         case NAL_RASL_N:
111         case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
112         }
113     }
114
115     return 0;
116 }
117
118 /**
119  * Find the end of the current frame in the bitstream.
120  * @return the position of the first byte of the next frame, or END_NOT_FOUND
121  */
122 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
123                                int buf_size)
124 {
125     HEVCParserContext *ctx = s->priv_data;
126     ParseContext       *pc = &ctx->pc;
127     int i;
128
129     for (i = 0; i < buf_size; i++) {
130         int nut;
131
132         pc->state64 = (pc->state64 << 8) | buf[i];
133
134         if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
135             continue;
136
137         nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
138         // Beginning of access unit
139         if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
140             (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
141             if (pc->frame_start_found) {
142                 pc->frame_start_found = 0;
143                 return i - 5;
144             }
145         } else if (nut <= NAL_RASL_R ||
146                    (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
147             int first_slice_segment_in_pic_flag = buf[i] >> 7;
148             if (first_slice_segment_in_pic_flag) {
149                 if (!pc->frame_start_found) {
150                     pc->frame_start_found = 1;
151                     s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
152                 } else { // First slice of next frame found
153                     pc->frame_start_found = 0;
154                     return i - 5;
155                 }
156             }
157         }
158     }
159
160     return END_NOT_FOUND;
161 }
162
163 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
164                       const uint8_t **poutbuf, int *poutbuf_size,
165                       const uint8_t *buf, int buf_size)
166 {
167     int next;
168
169     HEVCParserContext *ctx = s->priv_data;
170     ParseContext *pc = &ctx->pc;
171
172     if (avctx->extradata && !ctx->parsed_extradata) {
173         parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
174         ctx->parsed_extradata = 1;
175     }
176
177     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
178         next = buf_size;
179     } else {
180         next = hevc_find_frame_end(s, buf, buf_size);
181         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
182             *poutbuf      = NULL;
183             *poutbuf_size = 0;
184             return buf_size;
185         }
186     }
187
188     parse_nal_units(s, buf, buf_size, avctx);
189
190     *poutbuf      = buf;
191     *poutbuf_size = buf_size;
192     return next;
193 }
194
195 // Split after the parameter sets at the beginning of the stream if they exist.
196 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
197 {
198     int i;
199     uint32_t state = -1;
200     int has_ps = 0;
201
202     for (i = 0; i < buf_size; i++) {
203         state = (state << 8) | buf[i];
204         if (((state >> 8) & 0xFFFFFF) == START_CODE) {
205             int nut = (state >> 1) & 0x3F;
206             if (nut >= NAL_VPS && nut <= NAL_PPS)
207                 has_ps = 1;
208             else if (has_ps)
209                 return i - 3;
210             else // no parameter set at the beginning of the stream
211                 return 0;
212         }
213     }
214     return 0;
215 }
216
217 static void hevc_parser_close(AVCodecParserContext *s)
218 {
219     HEVCParserContext *ctx = s->priv_data;
220     int i;
221
222     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
223         av_buffer_unref(&ctx->ps.vps_list[i]);
224     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
225         av_buffer_unref(&ctx->ps.sps_list[i]);
226     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
227         av_buffer_unref(&ctx->ps.pps_list[i]);
228
229     for (i = 0; i < ctx->pkt.nals_allocated; i++)
230         av_freep(&ctx->pkt.nals[i].rbsp_buffer);
231     av_freep(&ctx->pkt.nals);
232     ctx->pkt.nals_allocated = 0;
233
234     av_freep(&ctx->pc.buffer);
235 }
236
237 AVCodecParser ff_hevc_parser = {
238     .codec_ids      = { AV_CODEC_ID_HEVC },
239     .priv_data_size = sizeof(HEVCParserContext),
240     .parser_parse   = hevc_parse,
241     .parser_close   = hevc_parser_close,
242     .split          = hevc_split,
243 };