LCOV - code coverage report
Current view: top level - libavcodec - h2645_parse.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 174 203 85.7 %
Date: 2017-12-11 04:34:20 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * H.264/HEVC common parsing code
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <string.h>
      22             : 
      23             : #include "config.h"
      24             : 
      25             : #include "libavutil/intmath.h"
      26             : #include "libavutil/intreadwrite.h"
      27             : #include "libavutil/mem.h"
      28             : 
      29             : #include "bytestream.h"
      30             : #include "hevc.h"
      31             : #include "h2645_parse.h"
      32             : 
      33      180703 : int ff_h2645_extract_rbsp(const uint8_t *src, int length,
      34             :                           H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
      35             : {
      36             :     int i, si, di;
      37             :     uint8_t *dst;
      38             : 
      39      180703 :     nal->skipped_bytes = 0;
      40             : #define STARTCODE_TEST                                                  \
      41             :         if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \
      42             :             if (src[i + 2] != 3 && src[i + 2] != 0) {                   \
      43             :                 /* startcode, so we must be past the end */             \
      44             :                 length = i;                                             \
      45             :             }                                                           \
      46             :             break;                                                      \
      47             :         }
      48             : #if HAVE_FAST_UNALIGNED
      49             : #define FIND_FIRST_ZERO                                                 \
      50             :         if (i > 0 && !src[i])                                           \
      51             :             i--;                                                        \
      52             :         while (src[i])                                                  \
      53             :             i++
      54             : #if HAVE_FAST_64BIT
      55    44743429 :     for (i = 0; i + 1 < length; i += 9) {
      56   133997496 :         if (!((~AV_RN64A(src + i) &
      57    89331664 :                (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
      58             :               0x8000800080008080ULL))
      59    43388836 :             continue;
      60     1276996 :         FIND_FIRST_ZERO;
      61     1276996 :         STARTCODE_TEST;
      62     1173890 :         i -= 7;
      63             :     }
      64             : #else
      65             :     for (i = 0; i + 1 < length; i += 5) {
      66             :         if (!((~AV_RN32A(src + i) &
      67             :                (AV_RN32A(src + i) - 0x01000101U)) &
      68             :               0x80008080U))
      69             :             continue;
      70             :         FIND_FIRST_ZERO;
      71             :         STARTCODE_TEST;
      72             :         i -= 3;
      73             :     }
      74             : #endif /* HAVE_FAST_64BIT */
      75             : #else
      76             :     for (i = 0; i + 1 < length; i += 2) {
      77             :         if (src[i])
      78             :             continue;
      79             :         if (i > 0 && src[i - 1] == 0)
      80             :             i--;
      81             :         STARTCODE_TEST;
      82             :     }
      83             : #endif /* HAVE_FAST_UNALIGNED */
      84             : 
      85      180703 :     if (i >= length - 1 && small_padding) { // no escaped 0
      86      109595 :         nal->data     =
      87      109595 :         nal->raw_data = src;
      88      109595 :         nal->size     =
      89      109595 :         nal->raw_size = length;
      90      109595 :         return length;
      91       71108 :     } else if (i > length)
      92       20314 :         i = length;
      93             : 
      94       71108 :     nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
      95       71108 :     dst = nal->rbsp_buffer;
      96             : 
      97       71108 :     memcpy(dst, src, i);
      98       71108 :     si = di = i;
      99     9042284 :     while (si + 2 < length) {
     100             :         // remove escapes (very rare 1:2^22)
     101     8942471 :         if (src[si + 2] > 3) {
     102     8616485 :             dst[di++] = src[si++];
     103     8616485 :             dst[di++] = src[si++];
     104      325986 :         } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
     105       57947 :             if (src[si + 2] == 3) { // escape
     106       15544 :                 dst[di++] = 0;
     107       15544 :                 dst[di++] = 0;
     108       15544 :                 si       += 3;
     109             : 
     110       15544 :                 if (nal->skipped_bytes_pos) {
     111       14624 :                     nal->skipped_bytes++;
     112       14624 :                     if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
     113           0 :                         nal->skipped_bytes_pos_size *= 2;
     114           0 :                         av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
     115           0 :                         av_reallocp_array(&nal->skipped_bytes_pos,
     116           0 :                                 nal->skipped_bytes_pos_size,
     117             :                                 sizeof(*nal->skipped_bytes_pos));
     118           0 :                         if (!nal->skipped_bytes_pos) {
     119           0 :                             nal->skipped_bytes_pos_size = 0;
     120           0 :                             return AVERROR(ENOMEM);
     121             :                         }
     122             :                     }
     123       14624 :                     if (nal->skipped_bytes_pos)
     124       14624 :                         nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
     125             :                 }
     126       15544 :                 continue;
     127             :             } else // next start code
     128       42403 :                 goto nsc;
     129             :         }
     130             : 
     131     8884524 :         dst[di++] = src[si++];
     132             :     }
     133       61536 :     while (si < length)
     134        4126 :         dst[di++] = src[si++];
     135             : 
     136       28705 : nsc:
     137       71108 :     memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     138             : 
     139       71108 :     nal->data = dst;
     140       71108 :     nal->size = di;
     141       71108 :     nal->raw_data = src;
     142       71108 :     nal->raw_size = si;
     143       71108 :     rbsp->rbsp_buffer_size += si;
     144             : 
     145       71108 :     return si;
     146             : }
     147             : 
     148       92301 : static const char *nal_unit_name(int nal_type)
     149             : {
     150       92301 :     switch(nal_type) {
     151       16957 :     case HEVC_NAL_TRAIL_N    : return "TRAIL_N";
     152       41855 :     case HEVC_NAL_TRAIL_R    : return "TRAIL_R";
     153         348 :     case HEVC_NAL_TSA_N      : return "TSA_N";
     154          86 :     case HEVC_NAL_TSA_R      : return "TSA_R";
     155           4 :     case HEVC_NAL_STSA_N     : return "STSA_N";
     156           4 :     case HEVC_NAL_STSA_R     : return "STSA_R";
     157         176 :     case HEVC_NAL_RADL_N     : return "RADL_N";
     158         132 :     case HEVC_NAL_RADL_R     : return "RADL_R";
     159        2134 :     case HEVC_NAL_RASL_N     : return "RASL_N";
     160        2469 :     case HEVC_NAL_RASL_R     : return "RASL_R";
     161          16 :     case HEVC_NAL_BLA_W_LP   : return "BLA_W_LP";
     162           8 :     case HEVC_NAL_BLA_W_RADL : return "BLA_W_RADL";
     163           4 :     case HEVC_NAL_BLA_N_LP   : return "BLA_N_LP";
     164        1945 :     case HEVC_NAL_IDR_W_RADL : return "IDR_W_RADL";
     165          24 :     case HEVC_NAL_IDR_N_LP   : return "IDR_N_LP";
     166         717 :     case HEVC_NAL_CRA_NUT    : return "CRA_NUT";
     167        1206 :     case HEVC_NAL_VPS        : return "VPS";
     168        1200 :     case HEVC_NAL_SPS        : return "SPS";
     169        3691 :     case HEVC_NAL_PPS        : return "PPS";
     170        1240 :     case HEVC_NAL_AUD        : return "AUD";
     171           6 :     case HEVC_NAL_EOS_NUT    : return "EOS_NUT";
     172           2 :     case HEVC_NAL_EOB_NUT    : return "EOB_NUT";
     173         122 :     case HEVC_NAL_FD_NUT     : return "FD_NUT";
     174        1553 :     case HEVC_NAL_SEI_PREFIX : return "SEI_PREFIX";
     175       16402 :     case HEVC_NAL_SEI_SUFFIX : return "SEI_SUFFIX";
     176           0 :     default : return "?";
     177             :     }
     178             : }
     179             : 
     180      143813 : static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
     181             : {
     182      143813 :     int size = nal->size;
     183             :     int v;
     184             : 
     185      348225 :     while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
     186       60599 :         size--;
     187             : 
     188      143813 :     if (!size)
     189           0 :         return 0;
     190             : 
     191      143813 :     v = nal->data[size - 1];
     192             : 
     193      143813 :     if (size > INT_MAX / 8)
     194           0 :         return AVERROR(ERANGE);
     195      143813 :     size *= 8;
     196             : 
     197             :     /* remove the stop bit and following trailing zeros,
     198             :      * or nothing for damaged bitstreams */
     199      143813 :     if (v)
     200      143813 :         size -= ff_ctz(v) + 1;
     201             : 
     202      143813 :     return size;
     203             : }
     204             : 
     205             : /**
     206             :  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
     207             :  * 0 if the unit should be skipped, 1 otherwise
     208             :  */
     209       92301 : static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
     210             : {
     211       92301 :     GetBitContext *gb = &nal->gb;
     212             :     int nuh_layer_id;
     213             : 
     214       92301 :     if (get_bits1(gb) != 0)
     215           0 :         return AVERROR_INVALIDDATA;
     216             : 
     217       92301 :     nal->type = get_bits(gb, 6);
     218             : 
     219       92301 :     nuh_layer_id   = get_bits(gb, 6);
     220       92301 :     nal->temporal_id = get_bits(gb, 3) - 1;
     221       92301 :     if (nal->temporal_id < 0)
     222           0 :         return AVERROR_INVALIDDATA;
     223             : 
     224       92301 :     av_log(logctx, AV_LOG_DEBUG,
     225             :            "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
     226             :            nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
     227             : 
     228       92301 :     return nuh_layer_id == 0;
     229             : }
     230             : 
     231       51512 : static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
     232             : {
     233       51512 :     GetBitContext *gb = &nal->gb;
     234             : 
     235       51512 :     if (get_bits1(gb) != 0)
     236           0 :         return AVERROR_INVALIDDATA;
     237             : 
     238       51512 :     nal->ref_idc = get_bits(gb, 2);
     239       51512 :     nal->type    = get_bits(gb, 5);
     240             : 
     241       51512 :     av_log(logctx, AV_LOG_DEBUG,
     242             :            "nal_unit_type: %d, nal_ref_idc: %d\n",
     243             :            nal->type, nal->ref_idc);
     244             : 
     245       51512 :     return 1;
     246             : }
     247             : 
     248      139340 : static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
     249             : {
     250      139340 :     int i = 0;
     251             : 
     252      139340 :     if (buf + 3 >= next_avc)
     253           0 :         return next_avc - buf;
     254             : 
     255      304215 :     while (buf + i + 3 < next_avc) {
     256      164875 :         if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
     257      139340 :             break;
     258       25535 :         i++;
     259             :     }
     260      139340 :     return i + 3;
     261             : }
     262             : 
     263       52177 : int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
     264             :                           void *logctx, int is_nalff, int nal_length_size,
     265             :                           enum AVCodecID codec_id, int small_padding)
     266             : {
     267             :     GetByteContext bc;
     268       52177 :     int consumed, ret = 0;
     269       52177 :     int next_avc = is_nalff ? 0 : length;
     270       52177 :     int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
     271             : 
     272       52177 :     bytestream2_init(&bc, buf, length);
     273       52177 :     av_fast_padded_malloc(&pkt->rbsp.rbsp_buffer, &pkt->rbsp.rbsp_buffer_alloc_size, length + padding);
     274       52177 :     if (!pkt->rbsp.rbsp_buffer)
     275           0 :         return AVERROR(ENOMEM);
     276             : 
     277       52177 :     pkt->rbsp.rbsp_buffer_size = 0;
     278       52177 :     pkt->nb_nals = 0;
     279      248167 :     while (bytestream2_get_bytes_left(&bc) >= 4) {
     280             :         H2645NAL *nal;
     281      143814 :         int extract_length = 0;
     282      143814 :         int skip_trailing_zeros = 1;
     283             : 
     284      143814 :         if (bytestream2_tell(&bc) == next_avc) {
     285        4474 :             int i = 0;
     286        4474 :             extract_length = get_nalsize(nal_length_size,
     287        4474 :                                          bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
     288        4474 :             if (extract_length < 0)
     289           1 :                 return extract_length;
     290             : 
     291        4473 :             bytestream2_skip(&bc, nal_length_size);
     292             : 
     293        4473 :             next_avc = bytestream2_tell(&bc) + extract_length;
     294             :         } else {
     295             :             int buf_index;
     296             : 
     297      139340 :             if (bytestream2_tell(&bc) > next_avc)
     298           0 :                 av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
     299             : 
     300             :             /* search start code */
     301      139340 :             buf_index = find_next_start_code(bc.buffer, buf + next_avc);
     302             : 
     303      139340 :             bytestream2_skip(&bc, buf_index);
     304             : 
     305      139340 :             if (!bytestream2_get_bytes_left(&bc)) {
     306           0 :                 if (pkt->nb_nals > 0) {
     307             :                     // No more start codes: we discarded some irrelevant
     308             :                     // bytes at the end of the packet.
     309           0 :                     return 0;
     310             :                 } else {
     311           0 :                     av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
     312           0 :                     return AVERROR_INVALIDDATA;
     313             :                 }
     314             :             }
     315             : 
     316      139340 :             extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
     317             : 
     318      139340 :             if (bytestream2_tell(&bc) >= next_avc) {
     319             :                 /* skip to the start of the next NAL */
     320           0 :                 bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
     321           0 :                 continue;
     322             :             }
     323             :         }
     324             : 
     325      143813 :         if (pkt->nals_allocated < pkt->nb_nals + 1) {
     326        9752 :             int new_size = pkt->nals_allocated + 1;
     327        9752 :             void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
     328             : 
     329        9752 :             if (!tmp)
     330           0 :                 return AVERROR(ENOMEM);
     331             : 
     332        9752 :             pkt->nals = tmp;
     333        9752 :             memset(pkt->nals + pkt->nals_allocated, 0,
     334        9752 :                    (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
     335             : 
     336        9752 :             nal = &pkt->nals[pkt->nb_nals];
     337        9752 :             nal->skipped_bytes_pos_size = 1024; // initial buffer size
     338        9752 :             nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
     339        9752 :             if (!nal->skipped_bytes_pos)
     340           0 :                 return AVERROR(ENOMEM);
     341             : 
     342        9752 :             pkt->nals_allocated = new_size;
     343             :         }
     344      143813 :         nal = &pkt->nals[pkt->nb_nals];
     345             : 
     346      143813 :         consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
     347      143813 :         if (consumed < 0)
     348           0 :             return consumed;
     349             : 
     350      143813 :         if (is_nalff && (extract_length != consumed) && extract_length)
     351          12 :             av_log(logctx, AV_LOG_DEBUG,
     352             :                    "NALFF: Consumed only %d bytes instead of %d\n",
     353             :                    consumed, extract_length);
     354             : 
     355      143813 :         pkt->nb_nals++;
     356             : 
     357      143813 :         bytestream2_skip(&bc, consumed);
     358             : 
     359             :         /* see commit 3566042a0 */
     360      235634 :         if (bytestream2_get_bytes_left(&bc) >= 4 &&
     361       91821 :             bytestream2_peek_be32(&bc) == 0x000001E0)
     362           2 :             skip_trailing_zeros = 0;
     363             : 
     364      143813 :         nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
     365             : 
     366      143813 :         ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
     367      143813 :         if (ret < 0)
     368           0 :             return ret;
     369             : 
     370      143813 :         if (codec_id == AV_CODEC_ID_HEVC)
     371       92301 :             ret = hevc_parse_nal_header(nal, logctx);
     372             :         else
     373       51512 :             ret = h264_parse_nal_header(nal, logctx);
     374      143813 :         if (ret <= 0 || nal->size <= 0) {
     375           0 :             if (ret < 0) {
     376           0 :                 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
     377             :                        nal->type);
     378             :             }
     379           0 :             pkt->nb_nals--;
     380             :         }
     381             :     }
     382             : 
     383       52176 :     return 0;
     384             : }
     385             : 
     386        2135 : void ff_h2645_packet_uninit(H2645Packet *pkt)
     387             : {
     388             :     int i;
     389       11887 :     for (i = 0; i < pkt->nals_allocated; i++) {
     390        9752 :         av_freep(&pkt->nals[i].skipped_bytes_pos);
     391             :     }
     392        2135 :     av_freep(&pkt->nals);
     393        2135 :     pkt->nals_allocated = 0;
     394        2135 :     av_freep(&pkt->rbsp.rbsp_buffer);
     395        2135 :     pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
     396        2135 : }

Generated by: LCOV version 1.13