2 * HEVC Annex B format parser
4 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * This file is part of FFmpeg.
8 * FFmpeg 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.
13 * FFmpeg 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/common.h"
27 #include "hevc_parse.h"
30 #include "h2645_parse.h"
34 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
36 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
37 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
39 typedef struct HEVCParserContext {
54 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55 AVCodecContext *avctx)
57 HEVCParserContext *ctx = s->priv_data;
58 HEVCParamSets *ps = &ctx->ps;
59 HEVCSEI *sei = &ctx->sei;
60 GetBitContext *gb = &nal->gb;
62 int i, num = 0, den = 0;
64 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
65 enum HEVCSliceType slice_type;
67 first_slice_in_pic_flag = get_bits1(gb);
68 s->picture_structure = sei->picture_timing.picture_struct;
69 s->field_order = sei->picture_timing.picture_struct;
71 if (IS_IRAP_NAL(nal)) {
73 skip_bits1(gb); // no_output_of_prior_pics_flag
76 pps_id = get_ue_golomb(gb);
77 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
78 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
79 return AVERROR_INVALIDDATA;
81 ps->pps = (HEVCPPS*)ps->pps_list[pps_id]->data;
83 if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
84 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
85 return AVERROR_INVALIDDATA;
87 if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
88 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
89 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
91 ow = &ps->sps->output_window;
93 s->coded_width = ps->sps->width;
94 s->coded_height = ps->sps->height;
95 s->width = ps->sps->width - ow->left_offset - ow->right_offset;
96 s->height = ps->sps->height - ow->top_offset - ow->bottom_offset;
97 s->format = ps->sps->pix_fmt;
98 avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
99 avctx->level = ps->sps->ptl.general_ptl.level_idc;
101 if (ps->vps->vps_timing_info_present_flag) {
102 num = ps->vps->vps_num_units_in_tick;
103 den = ps->vps->vps_time_scale;
104 } else if (ps->sps->vui.vui_timing_info_present_flag) {
105 num = ps->sps->vui.vui_num_units_in_tick;
106 den = ps->sps->vui.vui_time_scale;
109 if (num != 0 && den != 0)
110 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
113 if (!first_slice_in_pic_flag) {
114 unsigned int slice_segment_addr;
115 int slice_address_length;
117 if (ps->pps->dependent_slice_segments_enabled_flag)
118 dependent_slice_segment_flag = get_bits1(gb);
120 dependent_slice_segment_flag = 0;
122 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
123 ps->sps->ctb_height);
124 slice_segment_addr = get_bitsz(gb, slice_address_length);
125 if (slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
126 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
128 return AVERROR_INVALIDDATA;
131 dependent_slice_segment_flag = 0;
133 if (dependent_slice_segment_flag)
134 return 0; /* break; */
136 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
137 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
139 slice_type = get_ue_golomb_31(gb);
140 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
141 slice_type == HEVC_SLICE_B)) {
142 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
144 return AVERROR_INVALIDDATA;
146 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
147 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
150 if (ps->pps->output_flag_present_flag)
151 skip_bits1(gb); // pic_output_flag
153 if (ps->sps->separate_colour_plane_flag)
154 skip_bits(gb, 2); // colour_plane_id
156 if (!IS_IDR_NAL(nal)) {
157 int pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
158 s->output_picture_number = ctx->poc =
159 ff_hevc_compute_poc(ps->sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
161 s->output_picture_number = ctx->poc = 0;
163 if (nal->temporal_id == 0 &&
164 nal->type != HEVC_NAL_TRAIL_N &&
165 nal->type != HEVC_NAL_TSA_N &&
166 nal->type != HEVC_NAL_STSA_N &&
167 nal->type != HEVC_NAL_RADL_N &&
168 nal->type != HEVC_NAL_RASL_N &&
169 nal->type != HEVC_NAL_RADL_R &&
170 nal->type != HEVC_NAL_RASL_R)
171 ctx->pocTid0 = ctx->poc;
173 return 1; /* no need to evaluate the rest */
177 * Parse NAL units of found picture and decode some basic information.
179 * @param s parser context.
180 * @param avctx codec context.
181 * @param buf buffer with field/frame data.
182 * @param buf_size size of the buffer.
184 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
185 int buf_size, AVCodecContext *avctx)
187 HEVCParserContext *ctx = s->priv_data;
188 HEVCParamSets *ps = &ctx->ps;
189 HEVCSEI *sei = &ctx->sei;
192 /* set some sane default values */
193 s->pict_type = AV_PICTURE_TYPE_I;
195 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
197 ff_hevc_reset_sei(sei);
199 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
200 ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
204 for (i = 0; i < ctx->pkt.nb_nals; i++) {
205 H2645NAL *nal = &ctx->pkt.nals[i];
206 GetBitContext *gb = &nal->gb;
208 if (nal->nuh_layer_id > 0)
213 ff_hevc_decode_nal_vps(gb, avctx, ps);
216 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
219 ff_hevc_decode_nal_pps(gb, avctx, ps);
221 case HEVC_NAL_SEI_PREFIX:
222 case HEVC_NAL_SEI_SUFFIX:
223 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
225 case HEVC_NAL_TRAIL_N:
226 case HEVC_NAL_TRAIL_R:
229 case HEVC_NAL_STSA_N:
230 case HEVC_NAL_STSA_R:
231 case HEVC_NAL_BLA_W_LP:
232 case HEVC_NAL_BLA_W_RADL:
233 case HEVC_NAL_BLA_N_LP:
234 case HEVC_NAL_IDR_W_RADL:
235 case HEVC_NAL_IDR_N_LP:
236 case HEVC_NAL_CRA_NUT:
237 case HEVC_NAL_RADL_N:
238 case HEVC_NAL_RADL_R:
239 case HEVC_NAL_RASL_N:
240 case HEVC_NAL_RASL_R:
241 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
243 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
246 ret = hevc_parse_slice_header(s, nal, avctx);
252 /* didn't find a picture! */
253 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
258 * Find the end of the current frame in the bitstream.
259 * @return the position of the first byte of the next frame, or END_NOT_FOUND
261 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
264 HEVCParserContext *ctx = s->priv_data;
265 ParseContext *pc = &ctx->pc;
268 for (i = 0; i < buf_size; i++) {
271 pc->state64 = (pc->state64 << 8) | buf[i];
273 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
276 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
277 // Beginning of access unit
278 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
279 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
280 if (pc->frame_start_found) {
281 pc->frame_start_found = 0;
284 } else if (nut <= HEVC_NAL_RASL_R ||
285 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
286 int first_slice_segment_in_pic_flag = buf[i] >> 7;
287 if (first_slice_segment_in_pic_flag) {
288 if (!pc->frame_start_found) {
289 pc->frame_start_found = 1;
290 } else { // First slice of next frame found
291 pc->frame_start_found = 0;
298 return END_NOT_FOUND;
301 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
302 const uint8_t **poutbuf, int *poutbuf_size,
303 const uint8_t *buf, int buf_size)
306 HEVCParserContext *ctx = s->priv_data;
307 ParseContext *pc = &ctx->pc;
308 int is_dummy_buf = !buf_size;
309 const uint8_t *dummy_buf = buf;
311 if (avctx->extradata && !ctx->parsed_extradata) {
312 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
313 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
315 ctx->parsed_extradata = 1;
318 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
321 next = hevc_find_frame_end(s, buf, buf_size);
322 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
329 is_dummy_buf &= (dummy_buf == buf);
332 parse_nal_units(s, buf, buf_size, avctx);
335 *poutbuf_size = buf_size;
339 static void hevc_parser_close(AVCodecParserContext *s)
341 HEVCParserContext *ctx = s->priv_data;
343 ff_hevc_ps_uninit(&ctx->ps);
344 ff_h2645_packet_uninit(&ctx->pkt);
345 ff_hevc_reset_sei(&ctx->sei);
347 av_freep(&ctx->pc.buffer);
350 const AVCodecParser ff_hevc_parser = {
351 .codec_ids = { AV_CODEC_ID_HEVC },
352 .priv_data_size = sizeof(HEVCParserContext),
353 .parser_parse = hevc_parse,
354 .parser_close = hevc_parser_close,