summaryrefslogtreecommitdiff
path: root/libavcodec/hevc_parser.c (plain)
blob: 3e10ee401bbea2023b18ac14cc5854fe1ca546b8
1/*
2 * HEVC Annex B format parser
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
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#define ADVANCED_PARSER CONFIG_HEVC_DECODER
36
37typedef struct HEVCParserContext {
38 ParseContext pc;
39
40 H2645Packet pkt;
41 HEVCParamSets ps;
42
43 int parsed_extradata;
44
45#if ADVANCED_PARSER
46 HEVCContext h;
47#endif
48} HEVCParserContext;
49
50#if !ADVANCED_PARSER
51static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
52 AVCodecContext *avctx)
53{
54 HEVCParserContext *ctx = s->priv_data;
55 GetBitContext *gb = &nal->gb;
56
57 HEVCPPS *pps;
58 HEVCSPS *sps;
59 unsigned int pps_id;
60
61 get_bits1(gb); // first slice in pic
62 if (IS_IRAP_NAL(nal))
63 get_bits1(gb); // no output of prior pics
64
65 pps_id = get_ue_golomb_long(gb);
66 if (pps_id >= HEVC_MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
67 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
68 return AVERROR_INVALIDDATA;
69 }
70 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
71 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
72
73 /* export the stream parameters */
74 s->coded_width = sps->width;
75 s->coded_height = sps->height;
76 s->width = sps->output_width;
77 s->height = sps->output_height;
78 s->format = sps->pix_fmt;
79 avctx->profile = sps->ptl.general_ptl.profile_idc;
80 avctx->level = sps->ptl.general_ptl.level_idc;
81
82 /* ignore the rest for now*/
83
84 return 0;
85}
86
87static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
88 int buf_size, AVCodecContext *avctx)
89{
90 HEVCParserContext *ctx = s->priv_data;
91 int ret, i;
92
93 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
94 AV_CODEC_ID_HEVC, 1);
95 if (ret < 0)
96 return ret;
97
98 for (i = 0; i < ctx->pkt.nb_nals; i++) {
99 H2645NAL *nal = &ctx->pkt.nals[i];
100
101 /* ignore everything except parameter sets and VCL NALUs */
102 switch (nal->type) {
103 case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
104 case HEVC_NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
105 case HEVC_NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
106 case HEVC_NAL_TRAIL_R:
107 case HEVC_NAL_TRAIL_N:
108 case HEVC_NAL_TSA_N:
109 case HEVC_NAL_TSA_R:
110 case HEVC_NAL_STSA_N:
111 case HEVC_NAL_STSA_R:
112 case HEVC_NAL_BLA_W_LP:
113 case HEVC_NAL_BLA_W_RADL:
114 case HEVC_NAL_BLA_N_LP:
115 case HEVC_NAL_IDR_W_RADL:
116 case HEVC_NAL_IDR_N_LP:
117 case HEVC_NAL_CRA_NUT:
118 case HEVC_NAL_RADL_N:
119 case HEVC_NAL_RADL_R:
120 case HEVC_NAL_RASL_N:
121 case HEVC_NAL_RASL_R:
122 if (buf == avctx->extradata) {
123 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
124 return AVERROR_INVALIDDATA;
125 }
126 hevc_parse_slice_header(s, nal, avctx);
127 break;
128 }
129 }
130
131 return 0;
132}
133#endif
134
135/**
136 * Find the end of the current frame in the bitstream.
137 * @return the position of the first byte of the next frame, or END_NOT_FOUND
138 */
139static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
140 int buf_size)
141{
142 int i;
143 ParseContext *pc = s->priv_data;
144
145 for (i = 0; i < buf_size; i++) {
146 int nut;
147
148 pc->state64 = (pc->state64 << 8) | buf[i];
149
150 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
151 continue;
152
153 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
154 // Beginning of access unit
155 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_AUD) || nut == HEVC_NAL_SEI_PREFIX ||
156 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
157 if (pc->frame_start_found) {
158 pc->frame_start_found = 0;
159 return i - 5;
160 }
161 } else if (nut <= HEVC_NAL_RASL_R ||
162 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
163 int first_slice_segment_in_pic_flag = buf[i] >> 7;
164 if (first_slice_segment_in_pic_flag) {
165 if (!pc->frame_start_found) {
166 pc->frame_start_found = 1;
167 } else { // First slice of next frame found
168 pc->frame_start_found = 0;
169 return i - 5;
170 }
171 }
172 }
173 }
174
175 return END_NOT_FOUND;
176}
177
178#if ADVANCED_PARSER
179/**
180 * Parse NAL units of found picture and decode some basic information.
181 *
182 * @param s parser context.
183 * @param avctx codec context.
184 * @param buf buffer with field/frame data.
185 * @param buf_size size of the buffer.
186 */
187static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
188 int buf_size, AVCodecContext *avctx)
189{
190 HEVCParserContext *ctx = s->priv_data;
191 HEVCContext *h = &ctx->h;
192 GetBitContext *gb;
193 SliceHeader *sh = &h->sh;
194 HEVCParamSets *ps = &h->ps;
195 H2645Packet *pkt = &ctx->pkt;
196 const uint8_t *buf_end = buf + buf_size;
197 int state = -1, i;
198 H2645NAL *nal;
199 int is_global = buf == avctx->extradata;
200
201 if (!h->HEVClc)
202 h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
203 if (!h->HEVClc)
204 return AVERROR(ENOMEM);
205
206 gb = &h->HEVClc->gb;
207
208 /* set some sane default values */
209 s->pict_type = AV_PICTURE_TYPE_I;
210 s->key_frame = 0;
211 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
212
213 h->avctx = avctx;
214
215 ff_hevc_reset_sei(h);
216
217 if (!buf_size)
218 return 0;
219
220 if (pkt->nals_allocated < 1) {
221 H2645NAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
222 if (!tmp)
223 return AVERROR(ENOMEM);
224 pkt->nals = tmp;
225 memset(pkt->nals, 0, sizeof(*tmp));
226 pkt->nals_allocated = 1;
227 }
228
229 nal = &pkt->nals[0];
230
231 for (;;) {
232 int src_length, consumed;
233 int ret;
234 int num = 0, den = 0;
235 buf = avpriv_find_start_code(buf, buf_end, &state);
236 if (--buf + 2 >= buf_end)
237 break;
238 src_length = buf_end - buf;
239
240 h->nal_unit_type = (*buf >> 1) & 0x3f;
241 h->temporal_id = (*(buf + 1) & 0x07) - 1;
242 if (h->nal_unit_type <= HEVC_NAL_CRA_NUT) {
243 // Do not walk the whole buffer just to decode slice segment header
244 if (src_length > 20)
245 src_length = 20;
246 }
247
248 consumed = ff_h2645_extract_rbsp(buf, src_length, nal, 1);
249 if (consumed < 0)
250 return consumed;
251
252 ret = init_get_bits8(gb, nal->data + 2, nal->size);
253 if (ret < 0)
254 return ret;
255
256 switch (h->nal_unit_type) {
257 case HEVC_NAL_VPS:
258 ff_hevc_decode_nal_vps(gb, avctx, ps);
259 break;
260 case HEVC_NAL_SPS:
261 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
262 break;
263 case HEVC_NAL_PPS:
264 ff_hevc_decode_nal_pps(gb, avctx, ps);
265 break;
266 case HEVC_NAL_SEI_PREFIX:
267 case HEVC_NAL_SEI_SUFFIX:
268 ff_hevc_decode_nal_sei(h);
269 break;
270 case HEVC_NAL_SEI_DV_META:
271 /*
272 sample dolbyvision nal header:
273 00 00 01 8C 7C 01 19 08
274 nal_type =(0x7C >> 1) 0x3f;
275 */
276 avctx->has_dolby_vision_meta = 1;
277 break;
278 case HEVC_NAL_TRAIL_N:
279 case HEVC_NAL_TRAIL_R:
280 case HEVC_NAL_TSA_N:
281 case HEVC_NAL_TSA_R:
282 case HEVC_NAL_STSA_N:
283 case HEVC_NAL_STSA_R:
284 case HEVC_NAL_RADL_N:
285 case HEVC_NAL_RADL_R:
286 case HEVC_NAL_RASL_N:
287 case HEVC_NAL_RASL_R:
288 case HEVC_NAL_BLA_W_LP:
289 case HEVC_NAL_BLA_W_RADL:
290 case HEVC_NAL_BLA_N_LP:
291 case HEVC_NAL_IDR_W_RADL:
292 case HEVC_NAL_IDR_N_LP:
293 case HEVC_NAL_CRA_NUT:
294
295 if (is_global) {
296 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
297 return AVERROR_INVALIDDATA;
298 }
299
300 sh->first_slice_in_pic_flag = get_bits1(gb);
301 s->picture_structure = h->picture_struct;
302 s->field_order = h->picture_struct;
303
304 if (IS_IRAP(h)) {
305 s->key_frame = 1;
306 sh->no_output_of_prior_pics_flag = get_bits1(gb);
307 }
308
309 sh->pps_id = get_ue_golomb(gb);
310 if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
311 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
312 return AVERROR_INVALIDDATA;
313 }
314 ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
315
316 if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
317 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
318 return AVERROR_INVALIDDATA;
319 }
320 if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
321 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
322 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
323 }
324
325 s->coded_width = ps->sps->width;
326 s->coded_height = ps->sps->height;
327 s->width = ps->sps->output_width;
328 s->height = ps->sps->output_height;
329 s->format = ps->sps->pix_fmt;
330 avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
331 avctx->level = ps->sps->ptl.general_ptl.level_idc;
332
333 if (ps->vps->vps_timing_info_present_flag) {
334 num = ps->vps->vps_num_units_in_tick;
335 den = ps->vps->vps_time_scale;
336 } else if (ps->sps->vui.vui_timing_info_present_flag) {
337 num = ps->sps->vui.vui_num_units_in_tick;
338 den = ps->sps->vui.vui_time_scale;
339 }
340
341 if (num != 0 && den != 0)
342 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
343 num, den, 1 << 30);
344
345 if (!sh->first_slice_in_pic_flag) {
346 int slice_address_length;
347
348 if (ps->pps->dependent_slice_segments_enabled_flag)
349 sh->dependent_slice_segment_flag = get_bits1(gb);
350 else
351 sh->dependent_slice_segment_flag = 0;
352
353 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
354 ps->sps->ctb_height);
355 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
356 if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
357 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
358 sh->slice_segment_addr);
359 return AVERROR_INVALIDDATA;
360 }
361 } else
362 sh->dependent_slice_segment_flag = 0;
363
364 if (sh->dependent_slice_segment_flag)
365 break;
366
367 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
368 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
369
370 sh->slice_type = get_ue_golomb(gb);
371 if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
372 sh->slice_type == HEVC_SLICE_B)) {
373 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
374 sh->slice_type);
375 return AVERROR_INVALIDDATA;
376 }
377 s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
378 sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
379 AV_PICTURE_TYPE_I;
380
381 if (ps->pps->output_flag_present_flag)
382 sh->pic_output_flag = get_bits1(gb);
383
384 if (ps->sps->separate_colour_plane_flag)
385 sh->colour_plane_id = get_bits(gb, 2);
386
387 if (!IS_IDR(h)) {
388 sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
389 s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
390 } else
391 s->output_picture_number = h->poc = 0;
392
393 if (h->temporal_id == 0 &&
394 h->nal_unit_type != HEVC_NAL_TRAIL_N &&
395 h->nal_unit_type != HEVC_NAL_TSA_N &&
396 h->nal_unit_type != HEVC_NAL_STSA_N &&
397 h->nal_unit_type != HEVC_NAL_RADL_N &&
398 h->nal_unit_type != HEVC_NAL_RASL_N &&
399 h->nal_unit_type != HEVC_NAL_RADL_R &&
400 h->nal_unit_type != HEVC_NAL_RASL_R)
401 h->pocTid0 = h->poc;
402
403 return 0; /* no need to evaluate the rest */
404 }
405 buf += consumed;
406 }
407 /* didn't find a picture! */
408 if (!is_global)
409 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
410 return -1;
411}
412#endif
413
414static int hevc_parse(AVCodecParserContext *s,
415 AVCodecContext *avctx,
416 const uint8_t **poutbuf, int *poutbuf_size,
417 const uint8_t *buf, int buf_size)
418{
419 int next;
420 HEVCParserContext *ctx = s->priv_data;
421 ParseContext *pc = &ctx->pc;
422
423 if (avctx->extradata && !ctx->parsed_extradata) {
424 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
425 ctx->parsed_extradata = 1;
426 }
427
428 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
429 next = buf_size;
430 } else {
431 next = hevc_find_frame_end(s, buf, buf_size);
432 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
433 *poutbuf = NULL;
434 *poutbuf_size = 0;
435 return buf_size;
436 }
437 }
438
439 parse_nal_units(s, buf, buf_size, avctx);
440
441 *poutbuf = buf;
442 *poutbuf_size = buf_size;
443 return next;
444}
445
446// Split after the parameter sets at the beginning of the stream if they exist.
447static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
448{
449 const uint8_t *ptr = buf, *end = buf + buf_size;
450 uint32_t state = -1;
451 int has_vps = 0;
452 int has_sps = 0;
453 int has_pps = 0;
454 int nut;
455
456 while (ptr < end) {
457 ptr = avpriv_find_start_code(ptr, end, &state);
458 if ((state >> 8) != START_CODE)
459 break;
460 nut = (state >> 1) & 0x3F;
461 if (nut == HEVC_NAL_VPS)
462 has_vps = 1;
463 else if (nut == HEVC_NAL_SPS)
464 has_sps = 1;
465 else if (nut == HEVC_NAL_PPS)
466 has_pps = 1;
467 else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
468 nut != HEVC_NAL_AUD) {
469 if (has_vps && has_sps) {
470 while (ptr - 4 > buf && ptr[-5] == 0)
471 ptr--;
472 return ptr - 4 - buf;
473 }
474 }
475 }
476 return 0;
477}
478
479static void hevc_parser_close(AVCodecParserContext *s)
480{
481 HEVCParserContext *ctx = s->priv_data;
482 int i;
483
484#if ADVANCED_PARSER
485 HEVCContext *h = &ctx->h;
486
487 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
488 av_buffer_unref(&h->ps.vps_list[i]);
489 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
490 av_buffer_unref(&h->ps.sps_list[i]);
491 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
492 av_buffer_unref(&h->ps.pps_list[i]);
493
494 h->ps.sps = NULL;
495
496 av_freep(&h->HEVClc);
497#endif
498
499 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
500 av_buffer_unref(&ctx->ps.vps_list[i]);
501 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
502 av_buffer_unref(&ctx->ps.sps_list[i]);
503 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
504 av_buffer_unref(&ctx->ps.pps_list[i]);
505
506 ctx->ps.sps = NULL;
507
508 ff_h2645_packet_uninit(&ctx->pkt);
509
510 av_freep(&ctx->pc.buffer);
511}
512
513AVCodecParser ff_hevc_parser = {
514 .codec_ids = { AV_CODEC_ID_HEVC },
515 .priv_data_size = sizeof(HEVCParserContext),
516 .parser_parse = hevc_parse,
517 .parser_close = hevc_parser_close,
518 .split = hevc_split,
519};
520