libavcodec/h2645_parse.h
fa936a30
 /*
  * H.264/HEVC common parsing code
  *
3c4ca4c5
  * This file is part of FFmpeg.
fa936a30
  *
3c4ca4c5
  * FFmpeg is free software; you can redistribute it and/or
fa936a30
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
3c4ca4c5
  * FFmpeg is distributed in the hope that it will be useful,
fa936a30
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
3c4ca4c5
  * License along with FFmpeg; if not, write to the Free Software
fa936a30
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #ifndef AVCODEC_H2645_PARSE_H
 #define AVCODEC_H2645_PARSE_H
 
 #include <stdint.h>
 
 #include "avcodec.h"
 #include "get_bits.h"
 
cc13bc8c
 #define MAX_MBPAIR_SIZE (256*1024) // a tighter bound could be calculated if someone cares about a few bytes
 
fa936a30
 typedef struct H2645NAL {
     uint8_t *rbsp_buffer;
     int rbsp_buffer_size;
 
     int size;
     const uint8_t *data;
 
90ed6c5c
     /**
      * Size, in bits, of just the data, excluding the stop bit and any trailing
      * padding. I.e. what HEVC calls SODB.
      */
     int size_bits;
 
fa936a30
     int raw_size;
     const uint8_t *raw_data;
 
     GetBitContext gb;
 
b667252a
     /**
      * NAL unit type
      */
fa936a30
     int type;
b667252a
 
     /**
      * HEVC only, nuh_temporal_id_plus_1 - 1
      */
fa936a30
     int temporal_id;
3c4ca4c5
 
     int skipped_bytes;
     int skipped_bytes_pos_size;
     int *skipped_bytes_pos;
b667252a
     /**
41ed7ab4
      * H.264 only, nal_ref_idc
b667252a
      */
     int ref_idc;
fa936a30
 } H2645NAL;
 
 /* an input packet split into unescaped NAL units */
 typedef struct H2645Packet {
     H2645NAL *nals;
     int nb_nals;
     int nals_allocated;
 } H2645Packet;
 
 /**
  * Extract the raw (unescaped) bitstream.
  */
 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
cc13bc8c
                           H2645NAL *nal, int small_padding);
fa936a30
 
 /**
  * Split an input packet into NAL units.
  */
 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
b667252a
                           void *logctx, int is_nalff, int nal_length_size,
cc13bc8c
                           enum AVCodecID codec_id, int small_padding);
fa936a30
 
8229eff4
 /**
  * Free all the allocated memory in the packet.
  */
 void ff_h2645_packet_uninit(H2645Packet *pkt);
 
f10ea03d
 static inline int get_nalsize(int nal_length_size, const uint8_t *buf,
                               int buf_size, int *buf_index, void *logctx)
 {
     int i, nalsize = 0;
 
     if (*buf_index >= buf_size - nal_length_size) {
         // the end of the buffer is reached, refill it
         return AVERROR(EAGAIN);
     }
 
     for (i = 0; i < nal_length_size; i++)
         nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++];
     if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
         av_log(logctx, AV_LOG_ERROR,
2f1a539d
                "Invalid NAL unit size (%d > %d).\n", nalsize, buf_size - *buf_index);
f10ea03d
         return AVERROR_INVALIDDATA;
     }
     return nalsize;
 }
 
fa936a30
 #endif /* AVCODEC_H2645_PARSE_H */