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