LCOV - code coverage report
Current view: top level - libavcodec - hevc_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 150 195 76.9 %
Date: 2017-12-11 20:48:03 Functions: 5 6 83.3 %

          Line data    Source code
       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 "hevc_ps.h"
      28             : #include "hevc_sei.h"
      29             : #include "h2645_parse.h"
      30             : #include "internal.h"
      31             : #include "parser.h"
      32             : 
      33             : #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
      34             : 
      35             : #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
      36             : #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
      37             : 
      38             : typedef struct HEVCParserContext {
      39             :     ParseContext pc;
      40             : 
      41             :     H2645Packet pkt;
      42             :     HEVCParamSets ps;
      43             :     HEVCSEI sei;
      44             :     SliceHeader sh;
      45             : 
      46             :     int parsed_extradata;
      47             : 
      48             :     int poc;
      49             :     int pocTid0;
      50             : } HEVCParserContext;
      51             : 
      52       11397 : static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
      53             :                                    AVCodecContext *avctx)
      54             : {
      55       11397 :     HEVCParserContext *ctx = s->priv_data;
      56       11397 :     HEVCParamSets *ps = &ctx->ps;
      57       11397 :     HEVCSEI *sei = &ctx->sei;
      58       11397 :     SliceHeader *sh = &ctx->sh;
      59       11397 :     GetBitContext *gb = &nal->gb;
      60             :     const HEVCWindow *ow;
      61       11397 :     int i, num = 0, den = 0;
      62             : 
      63       11397 :     sh->first_slice_in_pic_flag = get_bits1(gb);
      64       11397 :     s->picture_structure = sei->picture_timing.picture_struct;
      65       11397 :     s->field_order = sei->picture_timing.picture_struct;
      66             : 
      67       11397 :     if (IS_IRAP_NAL(nal)) {
      68         373 :         s->key_frame = 1;
      69         373 :         sh->no_output_of_prior_pics_flag = get_bits1(gb);
      70             :     }
      71             : 
      72       11397 :     sh->pps_id = get_ue_golomb(gb);
      73       11397 :     if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
      74          11 :         av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
      75          11 :         return AVERROR_INVALIDDATA;
      76             :     }
      77       11386 :     ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
      78             : 
      79       11386 :     if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
      80           0 :         av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
      81           0 :         return AVERROR_INVALIDDATA;
      82             :     }
      83       11386 :     if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
      84         191 :         ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
      85         191 :         ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
      86             :     }
      87       11386 :     ow  = &ps->sps->output_window;
      88             : 
      89       11386 :     s->coded_width  = ps->sps->width;
      90       11386 :     s->coded_height = ps->sps->height;
      91       11386 :     s->width        = ps->sps->width  - ow->left_offset - ow->right_offset;
      92       11386 :     s->height       = ps->sps->height - ow->top_offset  - ow->bottom_offset;
      93       11386 :     s->format       = ps->sps->pix_fmt;
      94       11386 :     avctx->profile  = ps->sps->ptl.general_ptl.profile_idc;
      95       11386 :     avctx->level    = ps->sps->ptl.general_ptl.level_idc;
      96             : 
      97       11386 :     if (ps->vps->vps_timing_info_present_flag) {
      98         340 :         num = ps->vps->vps_num_units_in_tick;
      99         340 :         den = ps->vps->vps_time_scale;
     100       11046 :     } else if (ps->sps->vui.vui_timing_info_present_flag) {
     101         297 :         num = ps->sps->vui.vui_num_units_in_tick;
     102         297 :         den = ps->sps->vui.vui_time_scale;
     103             :     }
     104             : 
     105       11386 :     if (num != 0 && den != 0)
     106         637 :         av_reduce(&avctx->framerate.den, &avctx->framerate.num,
     107             :                   num, den, 1 << 30);
     108             : 
     109       11386 :     if (!sh->first_slice_in_pic_flag) {
     110             :         int slice_address_length;
     111             : 
     112           0 :         if (ps->pps->dependent_slice_segments_enabled_flag)
     113           0 :             sh->dependent_slice_segment_flag = get_bits1(gb);
     114             :         else
     115           0 :             sh->dependent_slice_segment_flag = 0;
     116             : 
     117           0 :         slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
     118           0 :                                               ps->sps->ctb_height);
     119           0 :         sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
     120           0 :         if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
     121           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
     122             :                    sh->slice_segment_addr);
     123           0 :             return AVERROR_INVALIDDATA;
     124             :         }
     125             :     } else
     126       11386 :         sh->dependent_slice_segment_flag = 0;
     127             : 
     128       11386 :     if (sh->dependent_slice_segment_flag)
     129           0 :         return 0; /* break; */
     130             : 
     131       11389 :     for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
     132           3 :         skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
     133             : 
     134       11386 :     sh->slice_type = get_ue_golomb(gb);
     135       19697 :     if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
     136        8311 :           sh->slice_type == HEVC_SLICE_B)) {
     137           0 :         av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
     138           0 :                sh->slice_type);
     139           0 :         return AVERROR_INVALIDDATA;
     140             :     }
     141       14461 :     s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
     142        3075 :                    sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
     143             :                                                AV_PICTURE_TYPE_I;
     144             : 
     145       11386 :     if (ps->pps->output_flag_present_flag)
     146         700 :         sh->pic_output_flag = get_bits1(gb);
     147             : 
     148       11386 :     if (ps->sps->separate_colour_plane_flag)
     149           0 :         sh->colour_plane_id = get_bits(gb, 2);
     150             : 
     151       11386 :     if (!IS_IDR_NAL(nal)) {
     152       11190 :         sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
     153       11190 :         s->output_picture_number = ctx->poc = ff_hevc_compute_poc(ps->sps, ctx->pocTid0, sh->pic_order_cnt_lsb, nal->type);
     154             :     } else
     155         196 :         s->output_picture_number = ctx->poc = 0;
     156             : 
     157       22433 :     if (nal->temporal_id == 0 &&
     158       19152 :         nal->type != HEVC_NAL_TRAIL_N &&
     159       16210 :         nal->type != HEVC_NAL_TSA_N &&
     160       16210 :         nal->type != HEVC_NAL_STSA_N &&
     161       16178 :         nal->type != HEVC_NAL_RADL_N &&
     162       15746 :         nal->type != HEVC_NAL_RASL_N &&
     163       15318 :         nal->type != HEVC_NAL_RADL_R &&
     164        7645 :         nal->type != HEVC_NAL_RASL_R)
     165        7041 :         ctx->pocTid0 = ctx->poc;
     166             : 
     167       11386 :     return 1; /* no need to evaluate the rest */
     168             : }
     169             : 
     170             : /**
     171             :  * Parse NAL units of found picture and decode some basic information.
     172             :  *
     173             :  * @param s parser context.
     174             :  * @param avctx codec context.
     175             :  * @param buf buffer with field/frame data.
     176             :  * @param buf_size size of the buffer.
     177             :  */
     178       11757 : static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
     179             :                            int buf_size, AVCodecContext *avctx)
     180             : {
     181       11757 :     HEVCParserContext *ctx = s->priv_data;
     182       11757 :     HEVCParamSets *ps = &ctx->ps;
     183       11757 :     HEVCSEI *sei = &ctx->sei;
     184       11757 :     int is_global = buf == avctx->extradata;
     185             :     int ret, i;
     186             : 
     187             :     /* set some sane default values */
     188       11757 :     s->pict_type         = AV_PICTURE_TYPE_I;
     189       11757 :     s->key_frame         = 0;
     190       11757 :     s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
     191             : 
     192       11757 :     ff_hevc_reset_sei(sei);
     193             : 
     194       11757 :     ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
     195             :                                 AV_CODEC_ID_HEVC, 1);
     196       11757 :     if (ret < 0)
     197           0 :         return ret;
     198             : 
     199       15490 :     for (i = 0; i < ctx->pkt.nb_nals; i++) {
     200       15130 :         H2645NAL *nal = &ctx->pkt.nals[i];
     201       15130 :         GetBitContext *gb = &nal->gb;
     202             : 
     203       15130 :         switch (nal->type) {
     204         403 :         case HEVC_NAL_VPS:
     205         403 :             ff_hevc_decode_nal_vps(gb, avctx, ps);
     206         403 :             break;
     207         401 :         case HEVC_NAL_SPS:
     208         401 :             ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
     209         401 :             break;
     210        1585 :         case HEVC_NAL_PPS:
     211        1585 :             ff_hevc_decode_nal_pps(gb, avctx, ps);
     212        1585 :             break;
     213         734 :         case HEVC_NAL_SEI_PREFIX:
     214             :         case HEVC_NAL_SEI_SUFFIX:
     215         734 :             ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
     216         734 :             break;
     217       11397 :         case HEVC_NAL_TRAIL_N:
     218             :         case HEVC_NAL_TRAIL_R:
     219             :         case HEVC_NAL_TSA_N:
     220             :         case HEVC_NAL_TSA_R:
     221             :         case HEVC_NAL_STSA_N:
     222             :         case HEVC_NAL_STSA_R:
     223             :         case HEVC_NAL_BLA_W_LP:
     224             :         case HEVC_NAL_BLA_W_RADL:
     225             :         case HEVC_NAL_BLA_N_LP:
     226             :         case HEVC_NAL_IDR_W_RADL:
     227             :         case HEVC_NAL_IDR_N_LP:
     228             :         case HEVC_NAL_CRA_NUT:
     229             :         case HEVC_NAL_RADL_N:
     230             :         case HEVC_NAL_RADL_R:
     231             :         case HEVC_NAL_RASL_N:
     232             :         case HEVC_NAL_RASL_R:
     233             : 
     234       11397 :             if (is_global) {
     235           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
     236           0 :                 return AVERROR_INVALIDDATA;
     237             :             }
     238             : 
     239       11397 :             ret = hevc_parse_slice_header(s, nal, avctx);
     240       11397 :             if (ret)
     241       11397 :                 return ret;
     242           0 :             break;
     243             :         }
     244             :     }
     245             :     /* didn't find a picture! */
     246         360 :     if (!is_global)
     247         185 :         av_log(avctx, AV_LOG_ERROR, "missing picture in access unit\n");
     248         360 :     return -1;
     249             : }
     250             : 
     251             : /**
     252             :  * Find the end of the current frame in the bitstream.
     253             :  * @return the position of the first byte of the next frame, or END_NOT_FOUND
     254             :  */
     255       86081 : static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
     256             :                                int buf_size)
     257             : {
     258       86081 :     HEVCParserContext *ctx = s->priv_data;
     259       86081 :     ParseContext       *pc = &ctx->pc;
     260             :     int i;
     261             : 
     262    76390240 :     for (i = 0; i < buf_size; i++) {
     263             :         int nut;
     264             : 
     265    76315373 :         pc->state64 = (pc->state64 << 8) | buf[i];
     266             : 
     267    76315373 :         if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
     268    76259787 :             continue;
     269             : 
     270       55586 :         nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
     271             :         // Beginning of access unit
     272       55586 :         if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
     273           0 :             (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
     274        7846 :             if (pc->frame_start_found) {
     275        1606 :                 pc->frame_start_found = 0;
     276        1606 :                 return i - 5;
     277             :             }
     278       50860 :         } else if (nut <= HEVC_NAL_RASL_R ||
     279        9169 :                    (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
     280       42704 :             int first_slice_segment_in_pic_flag = buf[i] >> 7;
     281       42704 :             if (first_slice_segment_in_pic_flag) {
     282       21005 :                 if (!pc->frame_start_found) {
     283       11397 :                     pc->frame_start_found = 1;
     284             :                 } else { // First slice of next frame found
     285        9608 :                     pc->frame_start_found = 0;
     286        9608 :                     return i - 5;
     287             :                 }
     288             :             }
     289             :         }
     290             :     }
     291             : 
     292       74867 :     return END_NOT_FOUND;
     293             : }
     294             : 
     295       86081 : static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
     296             :                       const uint8_t **poutbuf, int *poutbuf_size,
     297             :                       const uint8_t *buf, int buf_size)
     298             : {
     299             :     int next;
     300       86081 :     HEVCParserContext *ctx = s->priv_data;
     301       86081 :     ParseContext *pc = &ctx->pc;
     302             : 
     303       86081 :     if (avctx->extradata && !ctx->parsed_extradata) {
     304         175 :         parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
     305         175 :         ctx->parsed_extradata = 1;
     306             :     }
     307             : 
     308       86081 :     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
     309           0 :         next = buf_size;
     310             :     } else {
     311       86081 :         next = hevc_find_frame_end(s, buf, buf_size);
     312       86081 :         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
     313       74499 :             *poutbuf      = NULL;
     314       74499 :             *poutbuf_size = 0;
     315       74499 :             return buf_size;
     316             :         }
     317             :     }
     318             : 
     319       11582 :     parse_nal_units(s, buf, buf_size, avctx);
     320             : 
     321       11582 :     *poutbuf      = buf;
     322       11582 :     *poutbuf_size = buf_size;
     323       11582 :     return next;
     324             : }
     325             : 
     326             : // Split after the parameter sets at the beginning of the stream if they exist.
     327           0 : static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
     328             : {
     329           0 :     const uint8_t *ptr = buf, *end = buf + buf_size;
     330           0 :     uint32_t state = -1;
     331           0 :     int has_vps = 0;
     332           0 :     int has_sps = 0;
     333           0 :     int has_pps = 0;
     334             :     int nut;
     335             : 
     336           0 :     while (ptr < end) {
     337           0 :         ptr = avpriv_find_start_code(ptr, end, &state);
     338           0 :         if ((state >> 8) != START_CODE)
     339           0 :             break;
     340           0 :         nut = (state >> 1) & 0x3F;
     341           0 :         if (nut == HEVC_NAL_VPS)
     342           0 :             has_vps = 1;
     343           0 :         else if (nut == HEVC_NAL_SPS)
     344           0 :             has_sps = 1;
     345           0 :         else if (nut == HEVC_NAL_PPS)
     346           0 :             has_pps = 1;
     347           0 :         else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
     348             :                   nut != HEVC_NAL_AUD) {
     349           0 :             if (has_vps && has_sps) {
     350           0 :                 while (ptr - 4 > buf && ptr[-5] == 0)
     351           0 :                     ptr--;
     352           0 :                 return ptr - 4 - buf;
     353             :             }
     354             :         }
     355             :     }
     356           0 :     return 0;
     357             : }
     358             : 
     359         209 : static void hevc_parser_close(AVCodecParserContext *s)
     360             : {
     361         209 :     HEVCParserContext *ctx = s->priv_data;
     362             :     int i;
     363             : 
     364        3553 :     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
     365        3344 :         av_buffer_unref(&ctx->ps.vps_list[i]);
     366        3553 :     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
     367        3344 :         av_buffer_unref(&ctx->ps.sps_list[i]);
     368       13585 :     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
     369       13376 :         av_buffer_unref(&ctx->ps.pps_list[i]);
     370             : 
     371         209 :     ctx->ps.sps = NULL;
     372             : 
     373         209 :     ff_h2645_packet_uninit(&ctx->pkt);
     374         209 :     ff_hevc_reset_sei(&ctx->sei);
     375             : 
     376         209 :     av_freep(&ctx->pc.buffer);
     377         209 : }
     378             : 
     379             : AVCodecParser ff_hevc_parser = {
     380             :     .codec_ids      = { AV_CODEC_ID_HEVC },
     381             :     .priv_data_size = sizeof(HEVCParserContext),
     382             :     .parser_parse   = hevc_parse,
     383             :     .parser_close   = hevc_parser_close,
     384             :     .split          = hevc_split,
     385             : };

Generated by: LCOV version 1.13