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

Generated by: LCOV version 1.13