├── macroize.pl ├── README.md ├── MANIFEST ├── Makefile.am ├── configure.ac ├── m4 ├── ax_check_debug.m4 ├── ax_func_getopt_long.m4 └── ax_create_pkgconfig_info.m4 ├── Makefile.unix ├── h264_sei.h ├── h264_sei.in.c ├── h264_stream.h ├── hevc_nal.c ├── h264_stream.c ├── h264_sei.c ├── process.pl ├── h264_nal.c ├── hevc_analyze.c ├── bs.h ├── Doxyfile ├── hevc_stream.h ├── LICENSE └── hevc_stream.in.c /macroize.pl: -------------------------------------------------------------------------------- 1 | undef $/; 2 | $text = <>; 3 | $/ = "\n"; 4 | 5 | $text =~ s{(\w.*) = bs_read_(\w+)\(b\);}{value( $1, $2 );}g; 6 | $text =~ s{(\w.*) = bs_read_(\w+)\(b,\s*(\w.*)\);}{value( $1, $2($3) );}g; 7 | $text =~ s{read_(\w+)}{structure($1)}g; 8 | 9 | print $text; 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A library to read and write HEVC video bitstreams, in particular to examine or modify headers. 2 | 3 | Inspired by [h264bistream project](https://github.com/aizvorski/h264bitstream). Need merge in the near future 4 | 5 | ## Compile and Install 6 | 7 | refer [h264bistream project](https://github.com/aizvorski/h264bitstream) 8 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | CHANGES 2 | COPYING 3 | Doxyfile 4 | MANIFEST 5 | Makefile.am 6 | Makefile.unix 7 | README 8 | TODO 9 | bs.h 10 | configure.ac 11 | h264_sei.c 12 | h264_sei.h 13 | h264_stream.c 14 | h264_stream.h 15 | hevc_stream.c 16 | hevc_stream.h 17 | hevc_nal.c 18 | hevc_analyze.c 19 | m4/ax_check_debug.m4 20 | m4/ax_create_pkgconfig_info.m4 21 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | AUTOMAKE_OPTIONS = subdir-objects 2 | ACLOCAL_AMFLAGS = -I m4 3 | 4 | AM_CFLAGS = -I. -Wall -std=c99 $(EXTRA_CFLAGS) 5 | AM_LDFLAGS = -lm 6 | 7 | bin_PROGRAMS = hevc_analyze 8 | 9 | lib_LTLIBRARIES = libhevcbitstream.la 10 | 11 | libhevcbitstream_la_LDFLAGS = -no-undefined 12 | libhevcbitstream_la_SOURCES = hevc_stream.c h264_stream.c h264_sei.c hevc_nal.c h264_nal.c 13 | 14 | hevc_analyze_SOURCES = hevc_analyze.c 15 | hevc_analyze_LDADD = libhevcbitstream.la 16 | 17 | include_HEADERS = hevc_stream.h 18 | pkginclude_HEADERS = hevc_stream.h h264_stream.h h264_sei.h bs.h 19 | 20 | clean-local: 21 | rm -rf *.pc 22 | 23 | pkgconfigdir = $(libdir)/pkgconfig 24 | pkgconfig_DATA = libhevcbitstream.pc 25 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT(hevcbitstream, 0.2.0, aizvorski@gmail.com) 2 | 3 | AC_CONFIG_SRCDIR(./hevc_stream.c) 4 | 5 | AC_CONFIG_AUX_DIR(build-aux) 6 | 7 | # Make sure to compute the target so we know if we are on OS X 8 | AC_CANONICAL_TARGET 9 | 10 | # Build fat binaries on OS X 11 | EXTRA_CFLAGS='-std=c99 -Wno-error' 12 | 13 | if test ${target_os%%.*} = darwin9; then 14 | EXTRA_CFLAGS="-force_cpusubtype_ALL -mmacosx-version-min=10.4 -arch i386 -arch ppc -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.4u.sdk" 15 | CFLAGS+=${EXTRA_CFLAGS} 16 | am_cv_CC_dependencies_compiler_type=gcc 17 | fi 18 | 19 | 20 | AC_SUBST(EXTRA_CFLAGS) 21 | 22 | AM_INIT_AUTOMAKE([foreign]) 23 | 24 | AC_PROG_CC 25 | AM_PROG_CC_C_O 26 | AC_PROG_LD 27 | AC_PROG_INSTALL 28 | AC_PROG_LIBTOOL 29 | 30 | AC_CHECK_FUNCS(getopt_long, , AC_MSG_WARN(getopt_long not found. Long options will not work.) ) 31 | 32 | AC_CONFIG_FILES([Makefile]) 33 | AC_CONFIG_MACRO_DIR([m4]) 34 | 35 | AX_CHECK_DEBUG 36 | 37 | ax_create_pkgconfig_src_libdir=`pwd` 38 | AX_CREATE_PKGCONFIG_INFO(libhevcbitstream.pc, , '\\\${libdir}/libhevcbitstream.la') 39 | 40 | AC_OUTPUT 41 | -------------------------------------------------------------------------------- /m4/ax_check_debug.m4: -------------------------------------------------------------------------------- 1 | ############################################################# 2 | # AX_CHECK_DEBUG 3 | # 4 | # Debug and default optimization compile flags (DEBUG / NDEBUG, optimization level, -g) 5 | # 6 | # LICENSE 7 | # Copyright (C) 2012 BitGravity LLC, a Tata company 8 | # All Rights Reserved 9 | # 10 | ############################################################# 11 | 12 | 13 | AC_DEFUN([AX_CHECK_DEBUG], 14 | [ 15 | AC_MSG_CHECKING([Checking for --enable-debug flag]) 16 | AC_ARG_ENABLE([debug], 17 | [AS_HELP_STRING([--enable-debug], 18 | [Set DEBUG mode, no compiler optimization, default is no. Symbols (-g) is included in both debug and release mode])], 19 | [usedebug="$enableval"], 20 | [usedebug="no"]) 21 | 22 | AC_MSG_RESULT([$usedebug]) 23 | if test "x$usedebug" = "xyes" ; then 24 | AC_DEFINE([DEBUG],[],[Debug Mode]) 25 | AM_CFLAGS="$AM_CFLAGS -O0 -g -Wall -Werror" 26 | else 27 | AC_DEFINE([NDEBUG],[],[Release Mode]) 28 | #-O3 anyone? Kills using -g in release mode so we'll stick with O2 for now. 29 | AM_CFLAGS="$AM_CFLAGS -O2 -g -Wall" 30 | fi 31 | AC_SUBST([AM_CFLAGS]) 32 | ]) 33 | 34 | -------------------------------------------------------------------------------- /Makefile.unix: -------------------------------------------------------------------------------- 1 | VERSION = 0.2.0 2 | 3 | INCLUDES = 4 | LIBS = 5 | 6 | CC = gcc 7 | CFLAGS += -std=c99 -pedantic -Wall -W -Wshadow -Wwrite-strings -Wno-unused -g $(INCLUDES) 8 | 9 | LD = gcc 10 | LDFLAGS += $(LIBS) 11 | 12 | AR = ar 13 | ARFLAGS = rsc 14 | 15 | BINARIES = hevc_analyze 16 | 17 | all: libhevcbitstream.a $(BINARIES) 18 | 19 | hevc_stream.c: hevc_stream.in.c process.pl 20 | perl process.pl > hevc_stream.c < hevc_stream.in.c 21 | 22 | hevc_analyze: hevc_analyze.o libhevcbitstream.a 23 | $(LD) $(LDFLAGS) -o hevc_analyze hevc_analyze.o -L. -lhevcbitstream -lm 24 | 25 | libhevcbitstream.a: hevc_stream.c h264_stream.c hevc_nal.c h264_nal.c hevc_stream.h h264_sei.c h264_sei.h 26 | $(CC) $(CFLAGS) -c -o h264_nal.o h264_nal.c 27 | $(CC) $(CFLAGS) -c -o hevc_nal.o hevc_nal.c 28 | $(CC) $(CFLAGS) -c -o h264_stream.o h264_stream.c 29 | $(CC) $(CFLAGS) -c -o hevc_stream.o hevc_stream.c 30 | $(CC) $(CFLAGS) -c -o h264_sei.o h264_sei.c 31 | $(AR) $(ARFLAGS) libhevcbitstream.a hevc_stream.o hevc_nal.o h264_sei.o 32 | 33 | 34 | clean: 35 | rm -f *.o libhevcbitstream.a $(BINARIES) 36 | 37 | dox: hevc_stream.c hevc_stream.h bs.h Doxyfile 38 | doxygen Doxyfile 39 | 40 | dist: clean 41 | mkdir hevcbitstream-$(VERSION) 42 | tar c --files-from=MANIFEST -f tmp.tar ; cd hevcbitstream-$(VERSION) ; tar xf ../tmp.tar ; rm -f ../tmp.tar 43 | tar czf ../hevcbitstream-$(VERSION).tar.gz hevcbitstream-$(VERSION) 44 | rm -rf hevcbitstream-$(VERSION) 45 | 46 | -------------------------------------------------------------------------------- /h264_sei.h: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * 6 | * Written by Alex Izvorski and Alex Giladi 7 | * 8 | * This library 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 | * This library 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 this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #include 24 | 25 | #ifndef _H264_SEI_H 26 | #define _H264_SEI_H 1 27 | 28 | #include 29 | #include 30 | 31 | #include "bs.h" 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | typedef struct 38 | { 39 | int payloadType; 40 | int payloadSize; 41 | 42 | union 43 | { 44 | uint8_t* data; 45 | }; 46 | } sei_t; 47 | 48 | sei_t* sei_new(); 49 | void sei_free(sei_t* s); 50 | 51 | //D.1 SEI payload syntax 52 | #define SEI_TYPE_BUFFERING_PERIOD 0 53 | #define SEI_TYPE_PIC_TIMING 1 54 | #define SEI_TYPE_PAN_SCAN_RECT 2 55 | #define SEI_TYPE_FILLER_PAYLOAD 3 56 | #define SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35 4 57 | #define SEI_TYPE_USER_DATA_UNREGISTERED 5 58 | #define SEI_TYPE_RECOVERY_POINT 6 59 | #define SEI_TYPE_DEC_REF_PIC_MARKING_REPETITION 7 60 | #define SEI_TYPE_SPARE_PIC 8 61 | #define SEI_TYPE_SCENE_INFO 9 62 | #define SEI_TYPE_SUB_SEQ_INFO 10 63 | #define SEI_TYPE_SUB_SEQ_LAYER_CHARACTERISTICS 11 64 | #define SEI_TYPE_SUB_SEQ_CHARACTERISTICS 12 65 | #define SEI_TYPE_FULL_FRAME_FREEZE 13 66 | #define SEI_TYPE_FULL_FRAME_FREEZE_RELEASE 14 67 | #define SEI_TYPE_FULL_FRAME_SNAPSHOT 15 68 | #define SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_START 16 69 | #define SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_END 17 70 | #define SEI_TYPE_MOTION_CONSTRAINED_SLICE_GROUP_SET 18 71 | #define SEI_TYPE_FILM_GRAIN_CHARACTERISTICS 19 72 | #define SEI_TYPE_DEBLOCKING_FILTER_DISPLAY_PREFERENCE 20 73 | #define SEI_TYPE_STEREO_VIDEO_INFO 21 74 | #define SEI_TYPE_SCALABILITY_INFO 24 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /h264_sei.in.c: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * Copyright (C) 2012 Alex Izvorski 6 | * 7 | * This file is written by Leslie Wang 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "bs.h" 30 | #include "h264_stream.h" 31 | #include "h264_sei.h" 32 | 33 | #include 34 | #include // malloc 35 | #include // memset 36 | 37 | sei_t* sei_new() 38 | { 39 | sei_t* s = (sei_t*)calloc(1, sizeof(sei_t)); 40 | memset(s, 0, sizeof(sei_t)); 41 | s->data = NULL; 42 | return s; 43 | } 44 | 45 | void sei_free(sei_t* s) 46 | { 47 | switch( s->payloadType ) { 48 | default: 49 | if ( s->data != NULL ) free(s->data); 50 | } 51 | free(s); 52 | } 53 | 54 | void read_sei_end_bits(bs_t* b ) 55 | { 56 | // if the message doesn't end at a byte border 57 | if ( !bs_byte_aligned( b ) ) 58 | { 59 | if ( !bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_one is 0!!!!\n"); 60 | while ( ! bs_byte_aligned( b ) ) 61 | { 62 | if ( bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_zero is 1!!!!\n"); 63 | } 64 | } 65 | 66 | read_rbsp_trailing_bits(b); 67 | } 68 | 69 | #end_preamble 70 | 71 | #function_declarations 72 | 73 | // D.1 SEI payload syntax 74 | void structure(sei_payload)( sei_t* s, bs_t* b ) 75 | { 76 | int i; 77 | switch( s->payloadType ) 78 | { 79 | default: 80 | if( is_reading ) 81 | { 82 | s->data = (uint8_t*)calloc(1, s->payloadSize); 83 | } 84 | 85 | for ( i = 0; i < s->payloadSize; i++ ) 86 | value( s->data[i], u8 ); 87 | } 88 | 89 | //if( is_reading ) 90 | // read_sei_end_bits(h, b); 91 | } 92 | -------------------------------------------------------------------------------- /h264_stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * 6 | * Written by Alex Izvorski and Alex Giladi 7 | * 8 | * This library 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 | * This library 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 this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #ifndef _H264_STREAM_H 24 | #define _H264_STREAM_H 1 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "bs.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | //Appendix E. Table E-1 Meaning of sample aspect ratio indicator 37 | #define SAR_Unspecified 0 // Unspecified 38 | #define SAR_1_1 1 // 1:1 39 | #define SAR_12_11 2 // 12:11 40 | #define SAR_10_11 3 // 10:11 41 | #define SAR_16_11 4 // 16:11 42 | #define SAR_40_33 5 // 40:33 43 | #define SAR_24_11 6 // 24:11 44 | #define SAR_20_11 7 // 20:11 45 | #define SAR_32_11 8 // 32:11 46 | #define SAR_80_33 9 // 80:33 47 | #define SAR_18_11 10 // 18:11 48 | #define SAR_15_11 11 // 15:11 49 | #define SAR_64_33 12 // 64:33 50 | #define SAR_160_99 13 // 160:99 51 | // 14..254 Reserved 52 | #define SAR_Extended 255 // Extended_SAR 53 | 54 | extern int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end); 55 | 56 | extern int rbsp_to_nal(const uint8_t* rbsp_buf, const int* rbsp_size, uint8_t* nal_buf, int* nal_size); 57 | extern int nal_to_rbsp(const uint8_t* nal_buf, int* nal_size, uint8_t* rbsp_buf, int* rbsp_size); 58 | 59 | extern int more_rbsp_data(bs_t* bs); 60 | extern int more_rbsp_trailing_data(bs_t* b); 61 | 62 | extern int _read_ff_coded_number(bs_t* b); 63 | extern void _write_ff_coded_number(bs_t* b, int n); 64 | 65 | extern void debug_bytes(uint8_t* buf, int len); 66 | 67 | void read_rbsp_trailing_bits(bs_t* b); 68 | 69 | 70 | // file handle for debug output 71 | extern FILE* h264_dbgfile; 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /m4/ax_func_getopt_long.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_func_getopt_long.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_FUNC_GETOPT_LONG 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check for getopt_long support. 12 | # 13 | # This assume that the standard getopt.h file (from GNU libc) is available 14 | # as lib/gnugetopt.h. If needed, this file will be linked as getopt.h, but 15 | # we want to default to the system's getopt.h file. (See 16 | # http://sources.redhat.com/ml/automake/2000-09/msg00041.html for an 17 | # explanation about why using the system's getopt.h file is important.) 18 | # 19 | # LICENSE 20 | # 21 | # Copyright (c) 2008 Alexandre Duret-Lutz 22 | # 23 | # This program is free software; you can redistribute it and/or modify it 24 | # under the terms of the GNU General Public License as published by the 25 | # Free Software Foundation; either version 2 of the License, or (at your 26 | # option) any later version. 27 | # 28 | # This program is distributed in the hope that it will be useful, but 29 | # WITHOUT ANY WARRANTY; without even the implied warranty of 30 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 31 | # Public License for more details. 32 | # 33 | # You should have received a copy of the GNU General Public License along 34 | # with this program. If not, see . 35 | # 36 | # As a special exception, the respective Autoconf Macro's copyright owner 37 | # gives unlimited permission to copy, distribute and modify the configure 38 | # scripts that are the output of Autoconf when processing the Macro. You 39 | # need not follow the terms of the GNU General Public License when using 40 | # or distributing such scripts, even though portions of the text of the 41 | # Macro appear in them. The GNU General Public License (GPL) does govern 42 | # all other use of the material that constitutes the Autoconf Macro. 43 | # 44 | # This special exception to the GPL applies to versions of the Autoconf 45 | # Macro released by the Autoconf Archive. When you make and distribute a 46 | # modified version of the Autoconf Macro, you may extend this special 47 | # exception to the GPL to apply to your modified version as well. 48 | 49 | #serial 5 50 | 51 | AU_ALIAS([ADL_FUNC_GETOPT_LONG], [AX_FUNC_GETOPT_LONG]) 52 | AC_DEFUN([AX_FUNC_GETOPT_LONG], 53 | [AC_PREREQ(2.49)dnl 54 | # clean out junk possibly left behind by a previous configuration 55 | rm -f lib/getopt.h 56 | # Check for getopt_long support 57 | AC_CHECK_HEADERS([getopt.h]) 58 | AC_CHECK_FUNCS([getopt_long],, 59 | [# FreeBSD has a gnugetopt library for this 60 | AC_CHECK_LIB([gnugetopt],[getopt_long],[AC_DEFINE([HAVE_GETOPT_LONG])], 61 | [# use the GNU replacement 62 | AC_LIBOBJ(getopt) 63 | AC_LIBOBJ(getopt1) 64 | AC_CONFIG_LINKS([lib/getopt.h:lib/gnugetopt.h])])])]) 65 | -------------------------------------------------------------------------------- /hevc_nal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * hevcbitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * 6 | * Written by Alex Izvorski and Alex Giladi 7 | * 8 | * This library 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 | * This library 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 this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include "bs.h" 28 | #include "hevc_stream.h" 29 | 30 | /** 31 | Create a new HEVC stream object. Allocates all structures contained within it. 32 | @return the stream object 33 | */ 34 | hevc_stream_t* hevc_new() 35 | { 36 | hevc_stream_t* h = (hevc_stream_t*)calloc(1, sizeof(hevc_stream_t)); 37 | 38 | h->nal = (hevc_nal_t*)calloc(1, sizeof(hevc_nal_t)); 39 | 40 | // initialize tables 41 | for ( int i = 0; i < 32; i++ ) { h->sps_table[i] = (hevc_sps_t*)calloc(1, sizeof(hevc_sps_t)); } 42 | for ( int i = 0; i < 256; i++ ) { h->pps_table[i] = (hevc_pps_t*)calloc(1, sizeof(hevc_pps_t)); } 43 | 44 | h->vps = (hevc_vps_t*)calloc(1, sizeof(hevc_vps_t)); 45 | h->sps = (hevc_sps_t*)calloc(1, sizeof(hevc_sps_t)); 46 | h->pps = (hevc_pps_t*)calloc(1, sizeof(hevc_pps_t)); 47 | h->aud = (hevc_aud_t*)calloc(1, sizeof(hevc_aud_t)); 48 | /* TODO: support SEI 49 | h->num_seis = 0; 50 | h->seis = NULL; 51 | h->sei = NULL; //This is a TEMP pointer at whats in h->seis... 52 | */ 53 | h->sh = (hevc_slice_header_t*)calloc(1, sizeof(hevc_slice_header_t)); 54 | h->slice_data = (hevc_slice_data_rbsp_t*)calloc(1, sizeof(hevc_slice_data_rbsp_t)); 55 | 56 | return h; 57 | } 58 | 59 | 60 | /** 61 | Free an existing HEVC stream object. Frees all contained structures. 62 | @param[in,out] h the stream object 63 | */ 64 | void hevc_free(hevc_stream_t* h) 65 | { 66 | free(h->nal); 67 | 68 | for ( int i = 0; i < 32; i++ ) { free( h->sps_table[i] ); } 69 | for ( int i = 0; i < 256; i++ ) { free( h->pps_table[i] ); } 70 | 71 | /* TODO: support SEI 72 | if(h->seis != NULL) 73 | { 74 | for( int i = 0; i < h->num_seis; i++ ) 75 | { 76 | sei_t* sei = h->seis[i]; 77 | sei_free(sei); 78 | } 79 | free(h->seis); 80 | } 81 | */ 82 | free(h->slice_data); 83 | free(h->sh); 84 | 85 | free(h->aud); 86 | free(h->pps); 87 | free(h->sps); 88 | free(h->vps); 89 | 90 | free(h); 91 | } 92 | 93 | /** 94 | Read only the NAL headers (enough to determine unit type) from a byte buffer. 95 | @return unit type if read successfully, or -1 if this doesn't look like a nal 96 | */ 97 | int peek_hevc_nal_unit(hevc_stream_t* h, uint8_t* buf, int size) 98 | { 99 | hevc_nal_t* nal = h->nal; 100 | 101 | bs_t* b = bs_new(buf, size); 102 | 103 | /* forbidden_zero_bit */ bs_skip_u(b, 1); 104 | nal->nal_unit_type = bs_read_u(b, 6); 105 | nal->nal_layer_id = bs_read_u(b, 6); 106 | nal->nal_temporal_id_plus1 = bs_read_u(b, 3); 107 | 108 | bs_free(b); 109 | 110 | // basic verification, per 7.4.1 111 | if ( nal->nal_unit_type <= 0 || nal->nal_unit_type > MAX_HEVC_VAL_UNIT_TYPE ) { return -1; } 112 | 113 | return nal->nal_unit_type; 114 | } 115 | -------------------------------------------------------------------------------- /h264_stream.c: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * Copyright (C) 2012 Alex Izvorski 6 | * 7 | * Written by Alex Izvorski and Alex Giladi 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "bs.h" 30 | #include "h264_stream.h" 31 | #include "h264_sei.h" 32 | 33 | FILE* h264_dbgfile = NULL; 34 | 35 | #define printf(...) fprintf((h264_dbgfile == NULL ? stdout : h264_dbgfile), __VA_ARGS__) 36 | 37 | /** 38 | Calculate the log base 2 of the argument, rounded up. 39 | Zero or negative arguments return zero 40 | Idea from http://www.southwindsgames.com/blog/2009/01/19/fast-integer-log2-function-in-cc/ 41 | */ 42 | int intlog2(int x) 43 | { 44 | int log = 0; 45 | if (x < 0) { x = 0; } 46 | while ((x >> log) > 0) 47 | { 48 | log++; 49 | } 50 | if (log > 0 && x == 1<<(log-1)) { log--; } 51 | return log; 52 | } 53 | 54 | int is_slice_type(int slice_type, int cmp_type) 55 | { 56 | if (slice_type >= 5) { slice_type -= 5; } 57 | if (cmp_type >= 5) { cmp_type -= 5; } 58 | if (slice_type == cmp_type) { return 1; } 59 | else { return 0; } 60 | } 61 | 62 | int more_rbsp_data(bs_t* bs) 63 | { 64 | // TODO this version handles reading only. writing version? 65 | 66 | // no more data 67 | if (bs_eof(bs)) { return 0; } 68 | 69 | // no rbsp_stop_bit yet 70 | if (bs_peek_u1(bs) == 0) { return 1; } 71 | 72 | // next bit is 1, is it the rsbp_stop_bit? only if the rest of bits are 0 73 | bs_t bs_tmp; 74 | bs_clone(&bs_tmp, bs); 75 | bs_skip_u1(&bs_tmp); 76 | while(!bs_eof(&bs_tmp)) 77 | { 78 | // A later bit was 1, it wasn't the rsbp_stop_bit 79 | if (bs_read_u1(&bs_tmp) == 1) { return 1; } 80 | } 81 | 82 | // All following bits were 0, it was the rsbp_stop_bit 83 | return 0; 84 | } 85 | 86 | int more_rbsp_trailing_data(bs_t* b) { return !bs_eof(b); } 87 | 88 | int _read_ff_coded_number(bs_t* b) 89 | { 90 | int n1 = 0; 91 | int n2; 92 | do 93 | { 94 | n2 = bs_read_u8(b); 95 | n1 += n2; 96 | } while (n2 == 0xff); 97 | return n1; 98 | } 99 | 100 | void _write_ff_coded_number(bs_t* b, int n) 101 | { 102 | while (1) 103 | { 104 | if (n > 0xff) 105 | { 106 | bs_write_u8(b, 0xff); 107 | n -= 0xff; 108 | } 109 | else 110 | { 111 | bs_write_u8(b, n); 112 | break; 113 | } 114 | } 115 | } 116 | 117 | void debug_bytes(uint8_t* buf, int len) 118 | { 119 | int i; 120 | for (i = 0; i < len; i++) 121 | { 122 | printf("%02X ", buf[i]); 123 | if ((i+1) % 16 == 0) { printf ("\n"); } 124 | } 125 | printf("\n"); 126 | } 127 | 128 | //7.3.2.11 RBSP trailing bits syntax 129 | void read_rbsp_trailing_bits(bs_t* b) 130 | { 131 | /* rbsp_stop_one_bit */ bs_skip_u(b, 1); 132 | 133 | while( !bs_byte_aligned(b) ) 134 | { 135 | /* rbsp_alignment_zero_bit */ bs_skip_u(b, 1); 136 | } 137 | } 138 | 139 | 140 | -------------------------------------------------------------------------------- /h264_sei.c: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * Copyright (C) 2012 Alex Izvorski 6 | * 7 | * This file is written by Leslie Wang 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "bs.h" 30 | #include "h264_stream.h" 31 | #include "h264_sei.h" 32 | 33 | #include 34 | #include // malloc 35 | #include // memset 36 | 37 | sei_t* sei_new() 38 | { 39 | sei_t* s = (sei_t*)calloc(1, sizeof(sei_t)); 40 | memset(s, 0, sizeof(sei_t)); 41 | s->data = NULL; 42 | return s; 43 | } 44 | 45 | void sei_free(sei_t* s) 46 | { 47 | switch( s->payloadType ) { 48 | default: 49 | if ( s->data != NULL ) free(s->data); 50 | } 51 | free(s); 52 | } 53 | 54 | void read_sei_end_bits(bs_t* b ) 55 | { 56 | // if the message doesn't end at a byte border 57 | if ( !bs_byte_aligned( b ) ) 58 | { 59 | if ( !bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_one is 0!!!!\n"); 60 | while ( ! bs_byte_aligned( b ) ) 61 | { 62 | if ( bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_zero is 1!!!!\n"); 63 | } 64 | } 65 | 66 | read_rbsp_trailing_bits(b); 67 | } 68 | 69 | 70 | 71 | void read_sei_payload( sei_t* s, bs_t* b ); 72 | 73 | 74 | // D.1 SEI payload syntax 75 | void read_sei_payload( sei_t* s, bs_t* b ) 76 | { 77 | int i; 78 | switch( s->payloadType ) 79 | { 80 | default: 81 | if( 1 ) 82 | { 83 | s->data = (uint8_t*)calloc(1, s->payloadSize); 84 | } 85 | 86 | for ( i = 0; i < s->payloadSize; i++ ) 87 | s->data[i] = bs_read_u8(b); 88 | } 89 | 90 | //if( 1 ) 91 | // read_sei_end_bits(h, b); 92 | } 93 | 94 | 95 | void write_sei_payload( sei_t* s, bs_t* b ); 96 | 97 | 98 | // D.1 SEI payload syntax 99 | void write_sei_payload( sei_t* s, bs_t* b ) 100 | { 101 | int i; 102 | switch( s->payloadType ) 103 | { 104 | default: 105 | if( 0 ) 106 | { 107 | s->data = (uint8_t*)calloc(1, s->payloadSize); 108 | } 109 | 110 | for ( i = 0; i < s->payloadSize; i++ ) 111 | bs_write_u8(b, s->data[i]); 112 | } 113 | 114 | //if( 0 ) 115 | // read_sei_end_bits(h, b); 116 | } 117 | 118 | 119 | void read_debug_sei_payload( sei_t* s, bs_t* b ); 120 | 121 | 122 | // D.1 SEI payload syntax 123 | void read_debug_sei_payload( sei_t* s, bs_t* b ) 124 | { 125 | int i; 126 | switch( s->payloadType ) 127 | { 128 | default: 129 | if( 1 ) 130 | { 131 | s->data = (uint8_t*)calloc(1, s->payloadSize); 132 | } 133 | 134 | for ( i = 0; i < s->payloadSize; i++ ) 135 | printf("%ld.%d: ", b->p - b->start, b->bits_left); s->data[i] = bs_read_u8(b); printf("s->data[i]: %d \n", s->data[i]); 136 | } 137 | 138 | //if( 1 ) 139 | // read_sei_end_bits(h, b); 140 | } 141 | -------------------------------------------------------------------------------- /process.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | #/* 4 | # * h264bitstream - a library for reading and writing H.264 video 5 | # * Copyright (C) 2012 Alex Izvorski 6 | # * 7 | # * Written by Alex Izvorski 8 | # * 9 | # * This library is free software; you can redistribute it and/or 10 | # * modify it under the terms of the GNU Lesser General Public 11 | # * License as published by the Free Software Foundation; either 12 | # * version 2.1 of the License, or (at your option) any later version. 13 | # * 14 | # * This library is distributed in the hope that it will be useful, 15 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | # * Lesser General Public License for more details. 18 | # * 19 | # * You should have received a copy of the GNU Lesser General Public 20 | # * License along with this library; if not, write to the Free Software 21 | # * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | # */ 23 | 24 | undef $/; 25 | $code = <>; 26 | $/ = "\n"; 27 | 28 | $decl = ''; 29 | while ($code =~ m{\n(void structure.*)}mg) 30 | { 31 | $decl .= $1 . ";\n"; 32 | } 33 | 34 | $code =~ s{(.*)#end_preamble}{}s; 35 | $preamble = $1; 36 | 37 | print $preamble; 38 | 39 | $code =~ s{#function_declarations}{$decl}; 40 | 41 | $code_read = $code; 42 | $code_read =~ s{^(\s*) value \s* \( \s* ([^,]*) , (.*) \);}{ &proc_value_read($2, $3, $1) }exmg; 43 | $code_read =~ s{structure\( (\w+) \)}{read_$1}xg; 44 | $code_read =~ s{is_reading}{1}g; 45 | $code_read =~ s{is_writing}{0}g; 46 | print $code_read; 47 | 48 | $code_write = $code; 49 | $code_write =~ s{^(\s*) value \s* \( \s* ([^,]*) , (.*) \);}{ &proc_value_write($2, $3, $1) }exmg; 50 | $code_write =~ s{structure\( (\w+) \)}{write_$1}xg; 51 | $code_write =~ s{is_reading}{0}g; 52 | $code_write =~ s{is_writing}{1}g; 53 | print $code_write; 54 | 55 | $code_read_debug = $code; 56 | $code_read_debug =~ s{^(\s*) value \s* \( \s* ([^,]*) , (.*) \);}{ &proc_value_read_debug($2, $3, $1) }exmg; 57 | $code_read_debug =~ s{structure\( (\w+) \)}{read_debug_$1}xg; 58 | $code_read_debug =~ s{is_reading}{1}g; 59 | $code_read_debug =~ s{is_writing}{0}g; 60 | print $code_read_debug; 61 | 62 | $code_write_debug = $code; 63 | $code_write_debug =~ s{^(\s*) value \s* \( \s* ([^,]*) , (.*) \);}{ &proc_value_write_debug($2, $3, $1) }exmg; 64 | $code_write_debug =~ s{structure\( (\w+) \)}{write_debug_$1}xg; 65 | $code_write_debug =~ s{is_reading}{0}g; 66 | $code_write_debug =~ s{is_writing}{1}g; 67 | print $code_write_debug; 68 | 69 | sub proc_value_read 70 | { 71 | my ($s, $values, $indent) = @_; 72 | $values =~ s{^\s*}{}; 73 | $values =~ s{\s*$}{}; 74 | 75 | my $code; 76 | if ($values =~ m{u\((.*)\)}) { $code = "$s = bs_read_u(b, $1);"; } 77 | elsif ($values =~ m{f\((\d+),\s*(.*)\)}) { $code = "/* $s */ bs_skip_u(b, $1);"; } 78 | elsif ($values =~ m{(ue|se|ce|te|me|u8|u1)}) { $code = "$s = bs_read_$1(b);"; } 79 | elsif ($values eq 'ae') { $code = "$s = bs_read_ae(b);"; } 80 | else { $code = "// ERROR: value( $s, $values );"; } 81 | 82 | if ($values =~ m{ae} && $values ne 'ae') 83 | { 84 | $code = "if (cabac) { $s = bs_read_ae(b); }" . "\n${indent}" . "else { $code }"; 85 | } 86 | 87 | return $indent . $code; 88 | } 89 | 90 | sub proc_value_read_debug 91 | { 92 | my ($s, $values, $indent) = @_; 93 | $values =~ s{^\s*}{}; 94 | $values =~ s{\s*$}{}; 95 | 96 | my $code; 97 | if ($values =~ m{u\((.*)\)}) { $code = "$s = bs_read_u(b, $1);"; } 98 | elsif ($values =~ m{f\((\d+),\s*(.*)\)}) { $code = "int $s = bs_read_u(b, $1);"; } 99 | elsif ($values =~ m{(ue|se|ce|te|me|u8|u1)}) { $code = "$s = bs_read_$1(b);"; } 100 | elsif ($values eq 'ae') { $code = "$s = bs_read_ae(b);"; } 101 | else { $code = "// ERROR: value( $s, $values );"; } 102 | 103 | if ($values =~ m{ae} && $values ne 'ae') 104 | { 105 | $code = "if (cabac) { $s = bs_read_ae(b); }" . "\n${indent}" . "else { $code }"; 106 | } 107 | 108 | $code = "printf(\"\%ld.\%d: \", b->p - b->start, b->bits_left); ". 109 | $code . 110 | " printf(\"$s: \%d \\n\", $s); "; 111 | 112 | return $indent . $code; 113 | } 114 | 115 | sub proc_value_write 116 | { 117 | my ($s, $values, $indent) = @_; 118 | $values =~ s{^\s*}{}; 119 | $values =~ s{\s*$}{}; 120 | 121 | my $code; 122 | if ($values =~ m{u\((.*)\)}) { $code = "bs_write_u(b, $1, $s);"; } 123 | elsif ($values =~ m{f\((\d+),\s*(.*)\)}) { $code = "/* $s */ bs_write_u(b, $1, $2);"; } 124 | elsif ($values =~ m{(ue|se|ce|te|me|u8|u1)}) { $code = "bs_write_$1(b, $s);"; } 125 | elsif ($values eq 'ae') { $code = "bs_write_ae(b, $s);"; } 126 | else { $code = "// ERROR: value( $s, $values );"; } 127 | 128 | if ($values =~ m{ae} && $values ne 'ae') 129 | { 130 | $code = "if (cabac) { bs_write_ae(b, $s); }" . "\n${indent}" . "else { $code }"; 131 | } 132 | 133 | return $indent . $code; 134 | } 135 | 136 | 137 | sub proc_value_write_debug 138 | { 139 | my ($s, $values, $indent) = @_; 140 | $values =~ s{^\s*}{}; 141 | $values =~ s{\s*$}{}; 142 | 143 | my $code; 144 | if ($values =~ m{u\((.*)\)}) { $code = "bs_write_u(b, $1, $s);"; } 145 | elsif ($values =~ m{f\((\d+),\s*(.*)\)}) { $code = "int $s = $1; bs_write_u(b, $s, $2);"; } 146 | elsif ($values =~ m{(ue|se|ce|te|me|u8|u1)}) { $code = "bs_write_$1(b, $s);"; } 147 | elsif ($values eq 'ae') { 148 | my $s_name = &extract_value_name($s); 149 | $code = "bs_write_ae(h, b, $s, $s_name);"; 150 | } 151 | else { $code = "// ERROR: value( $s, $values );"; } 152 | 153 | if ($values =~ m{ae} && $values ne 'ae') 154 | { 155 | my $s_name = &extract_value_name($s); 156 | $code = "\n${indent}" . "if (h->pps->entropy_coding_mode_flag) {" . "\n${indent}" . " bs_write_ae(h, b, $s, $s_name);" . "\n${indent}" . "} else {" . "\n${indent}" . "$code" . "\n${indent}" . "}" . "\n${indent}"; 157 | } 158 | 159 | $code = "printf(\"\%d.\%d: \", b->p - b->start, b->bits_left); " . "\n${indent}" . 160 | $code . "\n${indent}" . 161 | "printf(\"$s: \%d ( %ld )\\n\", $s, decimal_to_binary( $s )); "; 162 | 163 | 164 | return $indent . $code; 165 | } 166 | 167 | -------------------------------------------------------------------------------- /h264_nal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * 6 | * Written by Alex Izvorski and Alex Giladi 7 | * 8 | * This library 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 | * This library 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 this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include "bs.h" 28 | 29 | /** 30 | Find the beginning and end of a NAL (Network Abstraction Layer) unit in a byte buffer containing H264 bitstream data. 31 | @param[in] buf the buffer 32 | @param[in] size the size of the buffer 33 | @param[out] nal_start the beginning offset of the nal 34 | @param[out] nal_end the end offset of the nal 35 | @return the length of the nal, or 0 if did not find start of nal, or -1 if did not find end of nal 36 | */ 37 | // DEPRECATED - this will be replaced by a similar function with a slightly different API 38 | int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end) 39 | { 40 | int i; 41 | // find start 42 | *nal_start = 0; 43 | *nal_end = 0; 44 | 45 | i = 0; 46 | while ( //( next_bits( 24 ) != 0x000001 && next_bits( 32 ) != 0x00000001 ) 47 | (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) && 48 | (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0x01) 49 | ) 50 | { 51 | i++; // skip leading zero 52 | if (i+4 >= size) { return 0; } // did not find nal start 53 | } 54 | 55 | if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) // ( next_bits( 24 ) != 0x000001 ) 56 | { 57 | i++; 58 | } 59 | 60 | if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) { /* error, should never happen */ return 0; } 61 | i+= 3; 62 | *nal_start = i; 63 | 64 | while ( //( next_bits( 24 ) != 0x000000 && next_bits( 24 ) != 0x000001 ) 65 | (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0) && 66 | (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) 67 | ) 68 | { 69 | i++; 70 | // FIXME the next line fails when reading a nal that ends exactly at the end of the data 71 | if (i+3 >= size) { *nal_end = size; return -1; } // did not find nal end, stream ended first 72 | } 73 | 74 | *nal_end = i; 75 | return (*nal_end - *nal_start); 76 | } 77 | 78 | 79 | /** 80 | Convert RBSP data to NAL data (Annex B format). 81 | The size of nal_buf must be 3/2 * the size of the rbsp_buf (rounded up) to guarantee the output will fit. 82 | If that is not true, output may be truncated and an error will be returned. 83 | If that is true, there is no possible error during this conversion. 84 | @param[in] rbsp_buf the rbsp data 85 | @param[in] rbsp_size pointer to the size of the rbsp data 86 | @param[in,out] nal_buf allocated memory in which to put the nal data 87 | @param[in,out] nal_size as input, pointer to the maximum size of the nal data; as output, filled in with the actual size of the nal data 88 | @return actual size of nal data, or -1 on error 89 | */ 90 | // 7.3.1 NAL unit syntax 91 | // 7.4.1.1 Encapsulation of an SODB within an RBSP 92 | int rbsp_to_nal(const uint8_t* rbsp_buf, const int* rbsp_size, uint8_t* nal_buf, int* nal_size) 93 | { 94 | int i; 95 | int j = 0; 96 | int count = 0; 97 | 98 | //if (*nal_size > 0) { nal_buf[0] = 0x00; } // zero out first byte since we start writing from second byte 99 | 100 | for ( i = 0; i < *rbsp_size ; ) 101 | { 102 | /* 103 | if ( j >= *nal_size ) 104 | { 105 | // error, not enough space 106 | return -1; 107 | } 108 | */ 109 | 110 | if ( ( count == 2 ) && !(rbsp_buf[i] & 0xFC) ) // HACK 0xFC 111 | { 112 | nal_buf[j] = 0x03; 113 | j++; 114 | count = 0; 115 | continue; 116 | } 117 | nal_buf[j] = rbsp_buf[i]; 118 | if ( rbsp_buf[i] == 0x00 ) 119 | { 120 | count++; 121 | } 122 | else 123 | { 124 | count = 0; 125 | } 126 | i++; 127 | j++; 128 | } 129 | 130 | *nal_size = j; 131 | return j; 132 | } 133 | 134 | /** 135 | Convert NAL data (Annex B format) to RBSP data. 136 | The size of rbsp_buf must be the same as size of the nal_buf to guarantee the output will fit. 137 | If that is not true, output may be truncated and an error will be returned. 138 | Additionally, certain byte sequences in the input nal_buf are not allowed in the spec and also cause the conversion to fail and an error to be returned. 139 | @param[in] nal_buf the nal data 140 | @param[in,out] nal_size as input, pointer to the size of the nal data; as output, filled in with the actual size of the nal data 141 | @param[in,out] rbsp_buf allocated memory in which to put the rbsp data 142 | @param[in,out] rbsp_size as input, pointer to the maximum size of the rbsp data; as output, filled in with the actual size of rbsp data 143 | @return actual size of rbsp data, or -1 on error 144 | */ 145 | // 7.3.1 NAL unit syntax 146 | // 7.4.1.1 Encapsulation of an SODB within an RBSP 147 | int nal_to_rbsp(const uint8_t* nal_buf, int* nal_size, uint8_t* rbsp_buf, int* rbsp_size) 148 | { 149 | int i; 150 | int j = 0; 151 | int count = 0; 152 | 153 | for( i = 0; i < *nal_size; i++ ) 154 | { 155 | // in NAL unit, 0x000000, 0x000001 or 0x000002 shall not occur at any byte-aligned position 156 | if( ( count == 2 ) && ( nal_buf[i] < 0x03) ) 157 | { 158 | return -1; 159 | } 160 | 161 | if( ( count == 2 ) && ( nal_buf[i] == 0x03) ) 162 | { 163 | // check the 4th byte after 0x000003, except when cabac_zero_word is used, in which case the last three bytes of this NAL unit must be 0x000003 164 | if((i < *nal_size - 1) && (nal_buf[i+1] > 0x03)) 165 | { 166 | return -1; 167 | } 168 | 169 | // if cabac_zero_word is used, the final byte of this NAL unit(0x03) is discarded, and the last two bytes of RBSP must be 0x0000 170 | if(i == *nal_size - 1) 171 | { 172 | break; 173 | } 174 | 175 | i++; 176 | count = 0; 177 | } 178 | 179 | if ( j >= *rbsp_size ) 180 | { 181 | // error, not enough space 182 | return -1; 183 | } 184 | 185 | rbsp_buf[j] = nal_buf[i]; 186 | if(nal_buf[i] == 0x00) 187 | { 188 | count++; 189 | } 190 | else 191 | { 192 | count = 0; 193 | } 194 | j++; 195 | } 196 | 197 | *nal_size = i; 198 | *rbsp_size = j; 199 | return j; 200 | } 201 | -------------------------------------------------------------------------------- /hevc_analyze.c: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * 5 | * Written by Alex Izvorski 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public 18 | * License along with this library; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | */ 21 | 22 | #include "h264_stream.h" 23 | #include "hevc_stream.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define BUFSIZE 32*1024*1024 32 | 33 | #if (defined(__GNUC__)) 34 | #define HAVE_GETOPT_LONG 35 | 36 | #include 37 | 38 | 39 | static struct option long_options[] = 40 | { 41 | { "probe", no_argument, NULL, 'p'}, 42 | { "output", required_argument, NULL, 'o'}, 43 | { "help", no_argument, NULL, 'h'}, 44 | { "verbose", required_argument, NULL, 'v'}, 45 | }; 46 | #endif 47 | 48 | static char options[] = 49 | "\t-o output_file, defaults to test.265\n" 50 | "\t-v verbose_level, print more info\n" 51 | "\t-p print codec for HTML5 video tag's codecs parameter, per RFC6381\n" 52 | "\t-h print this message and exit\n"; 53 | 54 | void usage( ) 55 | { 56 | 57 | fprintf( stderr, "hevc_analyze, version 0.2.0\n"); 58 | fprintf( stderr, "Analyze H.265 bitstreams in Annex B format\n"); 59 | fprintf( stderr, "Usage: \n"); 60 | 61 | fprintf( stderr, "hevc_analyze [options] \noptions:\n%s\n", options); 62 | } 63 | 64 | int main(int argc, char *argv[]) 65 | { 66 | FILE* infile; 67 | 68 | uint8_t* buf = (uint8_t*)malloc( BUFSIZE ); 69 | 70 | hevc_stream_t* h = hevc_new(); 71 | 72 | if (argc < 2) { usage(); return EXIT_FAILURE; } 73 | 74 | int opt_verbose = 1; 75 | int opt_probe = 0; 76 | 77 | #ifdef HAVE_GETOPT_LONG 78 | int c; 79 | int long_options_index; 80 | extern char* optarg; 81 | extern int optind; 82 | 83 | while ( ( c = getopt_long( argc, argv, "o:phv:", long_options, &long_options_index) ) != -1 ) 84 | { 85 | switch ( c ) 86 | { 87 | case 'o': 88 | if (h264_dbgfile == NULL) { h264_dbgfile = fopen( optarg, "wt"); } 89 | break; 90 | case 'p': 91 | opt_probe = 1; 92 | opt_verbose = 0; 93 | break; 94 | case 'v': 95 | opt_verbose = atoi( optarg ); 96 | break; 97 | case 'h': 98 | default: 99 | usage( ); 100 | return 1; 101 | } 102 | } 103 | 104 | infile = fopen(argv[optind], "rb"); 105 | 106 | #else 107 | 108 | infile = fopen(argv[1], "rb"); 109 | 110 | #endif 111 | 112 | if (infile == NULL) { fprintf( stderr, "!! Error: could not open file: %s \n", strerror(errno)); exit(EXIT_FAILURE); } 113 | 114 | if (h264_dbgfile == NULL) { h264_dbgfile = stdout; } 115 | 116 | 117 | size_t rsz = 0; 118 | size_t sz = 0; 119 | int64_t off = 0; 120 | uint8_t* p = buf; 121 | 122 | int nal_start, nal_end; 123 | 124 | while (1) 125 | { 126 | rsz = fread(buf + sz, 1, BUFSIZE - sz, infile); 127 | if (rsz == 0) 128 | { 129 | if (ferror(infile)) { fprintf( stderr, "!! Error: read failed: %s \n", strerror(errno)); break; } 130 | break; // if (feof(infile)) 131 | } 132 | 133 | sz += rsz; 134 | 135 | while (find_nal_unit(p, sz, &nal_start, &nal_end) > 0) 136 | { 137 | if ( opt_verbose > 0 ) 138 | { 139 | fprintf( h264_dbgfile, "!! Found NAL at offset %lld (0x%04llX), size %lld (0x%04llX) \n", 140 | (long long int)(off + (p - buf) + nal_start), 141 | (long long int)(off + (p - buf) + nal_start), 142 | (long long int)(nal_end - nal_start), 143 | (long long int)(nal_end - nal_start) ); 144 | 145 | debug_bytes(p-4, nal_end - nal_start + 4 >= 16 ? 16: nal_end - nal_start + 4); 146 | } 147 | 148 | p += nal_start; 149 | read_debug_hevc_nal_unit(h, p, nal_end - nal_start); 150 | /* 151 | if ( opt_probe && h->nal->nal_unit_type == NAL_UNIT_TYPE_SPS ) 152 | { 153 | // print codec parameter, per RFC 6381. 154 | int constraint_byte = h->sps->constraint_set0_flag << 7; 155 | constraint_byte = h->sps->constraint_set1_flag << 6; 156 | constraint_byte = h->sps->constraint_set2_flag << 5; 157 | constraint_byte = h->sps->constraint_set3_flag << 4; 158 | constraint_byte = h->sps->constraint_set4_flag << 3; 159 | constraint_byte = h->sps->constraint_set4_flag << 3; 160 | 161 | fprintf( h264_dbgfile, "codec: avc1.%02X%02X%02X\n",h->sps->profile_idc, constraint_byte, h->sps->level_idc ); 162 | 163 | // TODO: add more, move to h264_stream (?) 164 | break; // we've seen enough, bailing out. 165 | } 166 | */ 167 | if ( opt_verbose > 0 ) 168 | { 169 | // fprintf( h264_dbgfile, "XX "); 170 | // debug_bytes(p-4, nal_end - nal_start + 4 >= 16 ? 16: nal_end - nal_start + 4); 171 | 172 | // debug_nal(h, h->nal); 173 | } 174 | 175 | p += (nal_end - nal_start); 176 | sz -= nal_end; 177 | } 178 | 179 | // if no NALs found in buffer, discard it 180 | if (p == buf) 181 | { 182 | fprintf( stderr, "!! Did not find any NALs between offset %lld (0x%04llX), size %lld (0x%04llX), discarding \n", 183 | (long long int)off, 184 | (long long int)off, 185 | (long long int)off + sz, 186 | (long long int)off + sz); 187 | 188 | p = buf + sz; 189 | sz = 0; 190 | } else { 191 | //print last NAL 192 | if ( opt_verbose > 0 ) 193 | { 194 | fprintf( h264_dbgfile, "!! Found NAL at offset %lld (0x%04llX), size %lld (0x%04llX) \n", 195 | (long long int)(off + (p - buf) + nal_start), 196 | (long long int)(off + (p - buf) + nal_start), 197 | (long long int)(nal_end - nal_start), 198 | (long long int)(nal_end - nal_start) ); 199 | 200 | debug_bytes(p-4, nal_end - nal_start + 4 >= 16 ? 16: nal_end - nal_start + 4); 201 | } 202 | 203 | p += nal_start; 204 | read_debug_hevc_nal_unit(h, p, nal_end - nal_start); 205 | } 206 | 207 | memmove(buf, p, sz); 208 | off += p - buf; 209 | p = buf; 210 | } 211 | 212 | hevc_free(h); 213 | free(buf); 214 | 215 | fclose(h264_dbgfile); 216 | fclose(infile); 217 | 218 | return 0; 219 | } 220 | -------------------------------------------------------------------------------- /bs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * h264bitstream - a library for reading and writing H.264 video 3 | * Copyright (C) 2005-2007 Auroras Entertainment, LLC 4 | * Copyright (C) 2008-2011 Avail-TVN 5 | * 6 | * Written by Alex Izvorski and Alex Giladi 7 | * 8 | * This library 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 | * This library 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 this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | 23 | #ifndef _H264_BS_H 24 | #define _H264_BS_H 1 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | typedef struct 35 | { 36 | uint8_t* start; 37 | uint8_t* p; 38 | uint8_t* end; 39 | int bits_left; 40 | } bs_t; 41 | 42 | #define _OPTIMIZE_BS_ 1 43 | 44 | #if ( _OPTIMIZE_BS_ > 0 ) 45 | #ifndef FAST_U8 46 | #define FAST_U8 47 | #endif 48 | #endif 49 | 50 | 51 | static bs_t* bs_new(uint8_t* buf, size_t size); 52 | static void bs_free(bs_t* b); 53 | static bs_t* bs_clone( bs_t* dest, const bs_t* src ); 54 | static bs_t* bs_init(bs_t* b, uint8_t* buf, size_t size); 55 | static uint32_t bs_byte_aligned(bs_t* b); 56 | static int bs_eof(bs_t* b); 57 | static int bs_overrun(bs_t* b); 58 | static int bs_pos(bs_t* b); 59 | static int bs_pos_out(bs_t* b); //this is to get actual length even if p is larger than end 60 | 61 | static uint32_t bs_peek_u1(bs_t* b); 62 | static uint32_t bs_read_u1(bs_t* b); 63 | static uint32_t bs_read_u(bs_t* b, int n); 64 | static uint32_t bs_read_f(bs_t* b, int n); 65 | static uint32_t bs_read_u8(bs_t* b); 66 | static uint32_t bs_read_ue(bs_t* b); 67 | static int32_t bs_read_se(bs_t* b); 68 | 69 | static void bs_write_u1(bs_t* b, uint32_t v); 70 | static void bs_write_u(bs_t* b, int n, uint32_t v); 71 | static void bs_write_f(bs_t* b, int n, uint32_t v); 72 | static void bs_write_u8(bs_t* b, uint32_t v); 73 | static void bs_write_ue(bs_t* b, uint32_t v); 74 | static void bs_write_se(bs_t* b, int32_t v); 75 | 76 | static int bs_read_bytes(bs_t* b, uint8_t* buf, int len); 77 | static int bs_write_bytes(bs_t* b, uint8_t* buf, int len); 78 | static int bs_skip_bytes(bs_t* b, int len); 79 | static uint32_t bs_next_bits(bs_t* b, int nbits); 80 | // IMPLEMENTATION 81 | 82 | static inline bs_t* bs_init(bs_t* b, uint8_t* buf, size_t size) 83 | { 84 | b->start = buf; 85 | b->p = buf; 86 | b->end = buf + size; 87 | b->bits_left = 8; 88 | return b; 89 | } 90 | 91 | static inline bs_t* bs_new(uint8_t* buf, size_t size) 92 | { 93 | bs_t* b = (bs_t*)malloc(sizeof(bs_t)); 94 | bs_init(b, buf, size); 95 | return b; 96 | } 97 | 98 | static inline void bs_free(bs_t* b) 99 | { 100 | free(b); 101 | } 102 | 103 | static inline bs_t* bs_clone(bs_t* dest, const bs_t* src) 104 | { 105 | dest->start = src->p; 106 | dest->p = src->p; 107 | dest->end = src->end; 108 | dest->bits_left = src->bits_left; 109 | return dest; 110 | } 111 | 112 | static inline uint32_t bs_byte_aligned(bs_t* b) 113 | { 114 | return (b->bits_left == 8); 115 | } 116 | 117 | static inline int bs_eof(bs_t* b) { if (b->p >= b->end) { return 1; } else { return 0; } } 118 | 119 | static inline int bs_overrun(bs_t* b) { if (b->p > b->end) { return 1; } else { return 0; } } 120 | 121 | static inline int bs_pos(bs_t* b) { if (b->p > b->end) { return (b->end - b->start); } else { return (b->p - b->start); } } 122 | static inline int bs_pos_out(bs_t* b) { return (b->p - b->start); } 123 | 124 | static inline int bs_bytes_left(bs_t* b) { return (b->end - b->p); } 125 | 126 | static inline uint32_t bs_read_u1(bs_t* b) 127 | { 128 | uint32_t r = 0; 129 | 130 | b->bits_left--; 131 | 132 | if (! bs_eof(b)) 133 | { 134 | r = ((*(b->p)) >> b->bits_left) & 0x01; 135 | } 136 | 137 | if (b->bits_left == 0) { b->p ++; b->bits_left = 8; } 138 | 139 | return r; 140 | } 141 | 142 | static inline void bs_skip_u1(bs_t* b) 143 | { 144 | b->bits_left--; 145 | if (b->bits_left == 0) { b->p ++; b->bits_left = 8; } 146 | } 147 | 148 | static inline uint32_t bs_peek_u1(bs_t* b) 149 | { 150 | uint32_t r = 0; 151 | 152 | if (! bs_eof(b)) 153 | { 154 | r = ((*(b->p)) >> ( b->bits_left - 1 )) & 0x01; 155 | } 156 | return r; 157 | } 158 | 159 | 160 | static inline uint32_t bs_read_u(bs_t* b, int n) 161 | { 162 | uint32_t r = 0; 163 | int i; 164 | for (i = 0; i < n; i++) 165 | { 166 | r |= ( bs_read_u1(b) << ( n - i - 1 ) ); 167 | } 168 | return r; 169 | } 170 | 171 | static inline void bs_skip_u(bs_t* b, int n) 172 | { 173 | int i; 174 | for ( i = 0; i < n; i++ ) 175 | { 176 | bs_skip_u1( b ); 177 | } 178 | } 179 | 180 | static inline uint32_t bs_read_f(bs_t* b, int n) { return bs_read_u(b, n); } 181 | 182 | static inline uint32_t bs_read_u8(bs_t* b) 183 | { 184 | #ifdef FAST_U8 185 | if (b->bits_left == 8 && ! bs_eof(b)) // can do fast read 186 | { 187 | uint32_t r = b->p[0]; 188 | b->p++; 189 | return r; 190 | } 191 | #endif 192 | return bs_read_u(b, 8); 193 | } 194 | 195 | static inline uint32_t bs_read_ue(bs_t* b) 196 | { 197 | int32_t r = 0; 198 | int i = 0; 199 | 200 | while( (bs_read_u1(b) == 0) && (i < 32) && (!bs_eof(b)) ) 201 | { 202 | i++; 203 | } 204 | r = bs_read_u(b, i); 205 | r += (1 << i) - 1; 206 | return r; 207 | } 208 | 209 | static inline int32_t bs_read_se(bs_t* b) 210 | { 211 | int32_t r = bs_read_ue(b); 212 | if (r & 0x01) 213 | { 214 | r = (r+1)/2; 215 | } 216 | else 217 | { 218 | r = -(r/2); 219 | } 220 | return r; 221 | } 222 | 223 | 224 | static inline void bs_write_u1(bs_t* b, uint32_t v) 225 | { 226 | b->bits_left--; 227 | 228 | if (! bs_eof(b)) 229 | { 230 | // FIXME this is slow, but we must clear bit first 231 | // is it better to memset(0) the whole buffer during bs_init() instead? 232 | // if we don't do either, we introduce pretty nasty bugs 233 | (*(b->p)) &= ~(0x01 << b->bits_left); 234 | (*(b->p)) |= ((v & 0x01) << b->bits_left); 235 | } 236 | 237 | if (b->bits_left == 0) { b->p ++; b->bits_left = 8; } 238 | } 239 | 240 | static inline void bs_write_u(bs_t* b, int n, uint32_t v) 241 | { 242 | int i; 243 | for (i = 0; i < n; i++) 244 | { 245 | bs_write_u1(b, (v >> ( n - i - 1 ))&0x01 ); 246 | } 247 | } 248 | 249 | static inline void bs_write_f(bs_t* b, int n, uint32_t v) { bs_write_u(b, n, v); } 250 | 251 | static inline void bs_write_u8(bs_t* b, uint32_t v) 252 | { 253 | #ifdef FAST_U8 254 | if (b->bits_left == 8 && ! bs_eof(b)) // can do fast write 255 | { 256 | b->p[0] = v; 257 | b->p++; 258 | return; 259 | } 260 | #endif 261 | bs_write_u(b, 8, v); 262 | } 263 | 264 | static inline void bs_write_ue(bs_t* b, uint32_t v) 265 | { 266 | static const int len_table[256] = 267 | { 268 | 1, 269 | 1, 270 | 2,2, 271 | 3,3,3,3, 272 | 4,4,4,4,4,4,4,4, 273 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 274 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 275 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 276 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 277 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 278 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 279 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 280 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 281 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 282 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 283 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 284 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 285 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 286 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 287 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 288 | }; 289 | 290 | int len; 291 | 292 | if (v == 0) 293 | { 294 | bs_write_u1(b, 1); 295 | } 296 | else 297 | { 298 | v++; 299 | 300 | if (v >= 0x01000000) 301 | { 302 | len = 24 + len_table[ v >> 24 ]; 303 | } 304 | else if(v >= 0x00010000) 305 | { 306 | len = 16 + len_table[ v >> 16 ]; 307 | } 308 | else if(v >= 0x00000100) 309 | { 310 | len = 8 + len_table[ v >> 8 ]; 311 | } 312 | else 313 | { 314 | len = len_table[ v ]; 315 | } 316 | 317 | bs_write_u(b, 2*len-1, v); 318 | } 319 | } 320 | 321 | static inline void bs_write_se(bs_t* b, int32_t v) 322 | { 323 | if (v <= 0) 324 | { 325 | bs_write_ue(b, -v*2); 326 | } 327 | else 328 | { 329 | bs_write_ue(b, v*2 - 1); 330 | } 331 | } 332 | 333 | static inline int bs_read_bytes(bs_t* b, uint8_t* buf, int len) 334 | { 335 | int actual_len = len; 336 | if (b->end - b->p < actual_len) { actual_len = b->end - b->p; } 337 | if (actual_len < 0) { actual_len = 0; } 338 | memcpy(buf, b->p, actual_len); 339 | if (len < 0) { len = 0; } 340 | b->p += len; 341 | return actual_len; 342 | } 343 | 344 | static inline int bs_write_bytes(bs_t* b, uint8_t* buf, int len) 345 | { 346 | int actual_len = len; 347 | if (b->end - b->p < actual_len) { actual_len = b->end - b->p; } 348 | if (actual_len < 0) { actual_len = 0; } 349 | memcpy(b->p, buf, actual_len); 350 | if (len < 0) { len = 0; } 351 | b->p += len; 352 | return actual_len; 353 | } 354 | 355 | static inline int bs_skip_bytes(bs_t* b, int len) 356 | { 357 | int actual_len = len; 358 | if (b->end - b->p < actual_len) { actual_len = b->end - b->p; } 359 | if (actual_len < 0) { actual_len = 0; } 360 | if (len < 0) { len = 0; } 361 | b->p += len; 362 | return actual_len; 363 | } 364 | 365 | static inline uint32_t bs_next_bits(bs_t* bs, int nbits) 366 | { 367 | bs_t b; 368 | bs_clone(&b,bs); 369 | return bs_read_u(&b, nbits); 370 | } 371 | 372 | static inline uint64_t bs_next_bytes(bs_t* bs, int nbytes) 373 | { 374 | int i = 0; 375 | uint64_t val = 0; 376 | 377 | if ( (nbytes > 8) || (nbytes < 1) ) { return 0; } 378 | if (bs->p + nbytes > bs->end) { return 0; } 379 | 380 | for ( i = 0; i < nbytes; i++ ) { val = ( val << 8 ) | bs->p[i]; } 381 | return val; 382 | } 383 | 384 | #define bs_print_state(b) fprintf( stderr, "%s:%d@%s: b->p=0x%02hhX, b->left = %d\n", __FILE__, __LINE__, __FUNCTION__, *b->p, b->bits_left ) 385 | 386 | #ifdef __cplusplus 387 | } 388 | #endif 389 | 390 | #endif 391 | -------------------------------------------------------------------------------- /Doxyfile: -------------------------------------------------------------------------------- 1 | # Doxyfile 1.4.2 2 | 3 | #--------------------------------------------------------------------------- 4 | # Project related configuration options 5 | #--------------------------------------------------------------------------- 6 | PROJECT_NAME = 7 | PROJECT_NUMBER = 8 | OUTPUT_DIRECTORY = dox/ 9 | CREATE_SUBDIRS = NO 10 | OUTPUT_LANGUAGE = English 11 | USE_WINDOWS_ENCODING = NO 12 | BRIEF_MEMBER_DESC = YES 13 | REPEAT_BRIEF = YES 14 | ABBREVIATE_BRIEF = "The $name class" \ 15 | "The $name widget" \ 16 | "The $name file" \ 17 | is \ 18 | provides \ 19 | specifies \ 20 | contains \ 21 | represents \ 22 | a \ 23 | an \ 24 | the 25 | ALWAYS_DETAILED_SEC = NO 26 | INLINE_INHERITED_MEMB = NO 27 | FULL_PATH_NAMES = YES 28 | STRIP_FROM_PATH = ./ 29 | STRIP_FROM_INC_PATH = 30 | SHORT_NAMES = NO 31 | JAVADOC_AUTOBRIEF = NO 32 | MULTILINE_CPP_IS_BRIEF = NO 33 | DETAILS_AT_TOP = NO 34 | INHERIT_DOCS = YES 35 | DISTRIBUTE_GROUP_DOC = NO 36 | SEPARATE_MEMBER_PAGES = NO 37 | TAB_SIZE = 8 38 | ALIASES = 39 | OPTIMIZE_OUTPUT_FOR_C = YES 40 | OPTIMIZE_OUTPUT_JAVA = NO 41 | SUBGROUPING = YES 42 | #--------------------------------------------------------------------------- 43 | # Build related configuration options 44 | #--------------------------------------------------------------------------- 45 | EXTRACT_ALL = YES 46 | EXTRACT_PRIVATE = YES 47 | EXTRACT_STATIC = YES 48 | EXTRACT_LOCAL_CLASSES = YES 49 | EXTRACT_LOCAL_METHODS = NO 50 | HIDE_UNDOC_MEMBERS = NO 51 | HIDE_UNDOC_CLASSES = NO 52 | HIDE_FRIEND_COMPOUNDS = NO 53 | HIDE_IN_BODY_DOCS = NO 54 | INTERNAL_DOCS = NO 55 | CASE_SENSE_NAMES = YES 56 | HIDE_SCOPE_NAMES = NO 57 | SHOW_INCLUDE_FILES = YES 58 | INLINE_INFO = YES 59 | SORT_MEMBER_DOCS = YES 60 | SORT_BRIEF_DOCS = NO 61 | SORT_BY_SCOPE_NAME = NO 62 | GENERATE_TODOLIST = YES 63 | GENERATE_TESTLIST = YES 64 | GENERATE_BUGLIST = YES 65 | GENERATE_DEPRECATEDLIST= YES 66 | ENABLED_SECTIONS = 67 | MAX_INITIALIZER_LINES = 30 68 | SHOW_USED_FILES = YES 69 | SHOW_DIRECTORIES = YES 70 | FILE_VERSION_FILTER = 71 | #--------------------------------------------------------------------------- 72 | # configuration options related to warning and progress messages 73 | #--------------------------------------------------------------------------- 74 | QUIET = NO 75 | WARNINGS = YES 76 | WARN_IF_UNDOCUMENTED = YES 77 | WARN_IF_DOC_ERROR = YES 78 | WARN_NO_PARAMDOC = NO 79 | WARN_FORMAT = "$file:$line: $text" 80 | WARN_LOGFILE = 81 | #--------------------------------------------------------------------------- 82 | # configuration options related to the input files 83 | #--------------------------------------------------------------------------- 84 | INPUT = ./ 85 | FILE_PATTERNS = *.c \ 86 | *.cc \ 87 | *.cxx \ 88 | *.cpp \ 89 | *.c++ \ 90 | *.d \ 91 | *.java \ 92 | *.ii \ 93 | *.ixx \ 94 | *.ipp \ 95 | *.i++ \ 96 | *.inl \ 97 | *.h \ 98 | *.hh \ 99 | *.hxx \ 100 | *.hpp \ 101 | *.h++ \ 102 | *.idl \ 103 | *.odl \ 104 | *.cs \ 105 | *.php \ 106 | *.php3 \ 107 | *.inc \ 108 | *.m \ 109 | *.mm \ 110 | *.dox \ 111 | *.C \ 112 | *.CC \ 113 | *.C++ \ 114 | *.II \ 115 | *.I++ \ 116 | *.H \ 117 | *.HH \ 118 | *.H++ \ 119 | *.CS \ 120 | *.PHP \ 121 | *.PHP3 \ 122 | *.M \ 123 | *.MM 124 | RECURSIVE = NO 125 | EXCLUDE = 126 | EXCLUDE_SYMLINKS = NO 127 | EXCLUDE_PATTERNS = 128 | EXAMPLE_PATH = 129 | EXAMPLE_PATTERNS = * 130 | EXAMPLE_RECURSIVE = NO 131 | IMAGE_PATH = 132 | INPUT_FILTER = 133 | FILTER_PATTERNS = 134 | FILTER_SOURCE_FILES = NO 135 | #--------------------------------------------------------------------------- 136 | # configuration options related to source browsing 137 | #--------------------------------------------------------------------------- 138 | SOURCE_BROWSER = YES 139 | INLINE_SOURCES = NO 140 | STRIP_CODE_COMMENTS = YES 141 | REFERENCED_BY_RELATION = YES 142 | REFERENCES_RELATION = YES 143 | VERBATIM_HEADERS = YES 144 | #--------------------------------------------------------------------------- 145 | # configuration options related to the alphabetical class index 146 | #--------------------------------------------------------------------------- 147 | ALPHABETICAL_INDEX = NO 148 | COLS_IN_ALPHA_INDEX = 5 149 | IGNORE_PREFIX = 150 | #--------------------------------------------------------------------------- 151 | # configuration options related to the HTML output 152 | #--------------------------------------------------------------------------- 153 | GENERATE_HTML = YES 154 | HTML_OUTPUT = html 155 | HTML_FILE_EXTENSION = .html 156 | HTML_HEADER = 157 | HTML_FOOTER = 158 | HTML_STYLESHEET = 159 | HTML_ALIGN_MEMBERS = YES 160 | GENERATE_HTMLHELP = NO 161 | CHM_FILE = 162 | HHC_LOCATION = 163 | GENERATE_CHI = NO 164 | BINARY_TOC = NO 165 | TOC_EXPAND = NO 166 | DISABLE_INDEX = NO 167 | ENUM_VALUES_PER_LINE = 4 168 | GENERATE_TREEVIEW = YES 169 | TREEVIEW_WIDTH = 250 170 | #--------------------------------------------------------------------------- 171 | # configuration options related to the LaTeX output 172 | #--------------------------------------------------------------------------- 173 | GENERATE_LATEX = NO 174 | LATEX_OUTPUT = latex 175 | LATEX_CMD_NAME = latex 176 | MAKEINDEX_CMD_NAME = makeindex 177 | COMPACT_LATEX = NO 178 | PAPER_TYPE = a4wide 179 | EXTRA_PACKAGES = 180 | LATEX_HEADER = 181 | PDF_HYPERLINKS = NO 182 | USE_PDFLATEX = NO 183 | LATEX_BATCHMODE = NO 184 | LATEX_HIDE_INDICES = NO 185 | #--------------------------------------------------------------------------- 186 | # configuration options related to the RTF output 187 | #--------------------------------------------------------------------------- 188 | GENERATE_RTF = NO 189 | RTF_OUTPUT = rtf 190 | COMPACT_RTF = NO 191 | RTF_HYPERLINKS = NO 192 | RTF_STYLESHEET_FILE = 193 | RTF_EXTENSIONS_FILE = 194 | #--------------------------------------------------------------------------- 195 | # configuration options related to the man page output 196 | #--------------------------------------------------------------------------- 197 | GENERATE_MAN = NO 198 | MAN_OUTPUT = man 199 | MAN_EXTENSION = .3 200 | MAN_LINKS = NO 201 | #--------------------------------------------------------------------------- 202 | # configuration options related to the XML output 203 | #--------------------------------------------------------------------------- 204 | GENERATE_XML = NO 205 | XML_OUTPUT = xml 206 | XML_SCHEMA = 207 | XML_DTD = 208 | XML_PROGRAMLISTING = YES 209 | #--------------------------------------------------------------------------- 210 | # configuration options for the AutoGen Definitions output 211 | #--------------------------------------------------------------------------- 212 | GENERATE_AUTOGEN_DEF = NO 213 | #--------------------------------------------------------------------------- 214 | # configuration options related to the Perl module output 215 | #--------------------------------------------------------------------------- 216 | GENERATE_PERLMOD = NO 217 | PERLMOD_LATEX = NO 218 | PERLMOD_PRETTY = YES 219 | PERLMOD_MAKEVAR_PREFIX = 220 | #--------------------------------------------------------------------------- 221 | # Configuration options related to the preprocessor 222 | #--------------------------------------------------------------------------- 223 | ENABLE_PREPROCESSING = YES 224 | MACRO_EXPANSION = NO 225 | EXPAND_ONLY_PREDEF = NO 226 | SEARCH_INCLUDES = YES 227 | INCLUDE_PATH = 228 | INCLUDE_FILE_PATTERNS = 229 | PREDEFINED = 230 | EXPAND_AS_DEFINED = 231 | SKIP_FUNCTION_MACROS = YES 232 | #--------------------------------------------------------------------------- 233 | # Configuration::additions related to external references 234 | #--------------------------------------------------------------------------- 235 | TAGFILES = 236 | GENERATE_TAGFILE = 237 | ALLEXTERNALS = NO 238 | EXTERNAL_GROUPS = YES 239 | PERL_PATH = /usr/bin/perl 240 | #--------------------------------------------------------------------------- 241 | # Configuration options related to the dot tool 242 | #--------------------------------------------------------------------------- 243 | CLASS_DIAGRAMS = NO 244 | HIDE_UNDOC_RELATIONS = YES 245 | HAVE_DOT = YES 246 | CLASS_GRAPH = YES 247 | COLLABORATION_GRAPH = YES 248 | GROUP_GRAPHS = YES 249 | UML_LOOK = NO 250 | TEMPLATE_RELATIONS = NO 251 | INCLUDE_GRAPH = YES 252 | INCLUDED_BY_GRAPH = YES 253 | CALL_GRAPH = YES 254 | GRAPHICAL_HIERARCHY = YES 255 | DIRECTORY_GRAPH = YES 256 | DOT_IMAGE_FORMAT = png 257 | DOT_PATH = 258 | DOTFILE_DIRS = 259 | MAX_DOT_GRAPH_WIDTH = 1024 260 | MAX_DOT_GRAPH_HEIGHT = 1024 261 | MAX_DOT_GRAPH_DEPTH = 1000 262 | DOT_TRANSPARENT = NO 263 | DOT_MULTI_TARGETS = NO 264 | GENERATE_LEGEND = YES 265 | DOT_CLEANUP = YES 266 | #--------------------------------------------------------------------------- 267 | # Configuration::additions related to the search engine 268 | #--------------------------------------------------------------------------- 269 | SEARCHENGINE = NO 270 | -------------------------------------------------------------------------------- /m4/ax_create_pkgconfig_info.m4: -------------------------------------------------------------------------------- 1 | # ============================================================================ 2 | # http://www.gnu.org/software/autoconf-archive/ax_create_pkgconfig_info.html 3 | # ============================================================================ 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CREATE_PKGCONFIG_INFO [(outputfile, [requires [,libs [,summary [,cflags [, ldflags]]]]])] 8 | # 9 | # DESCRIPTION 10 | # 11 | # Defaults: 12 | # 13 | # $1 = $PACKAGE_NAME.pc 14 | # $2 = (empty) 15 | # $3 = $PACKAGE_LIBS $LIBS (as set at that point in configure.ac) 16 | # $4 = $PACKAGE_SUMMARY (or $1 Library) 17 | # $5 = $CPPFLAGS $PACKAGE_CFLAGS (as set at the point in configure.ac) 18 | # $6 = $LDFLAGS $PACKAGE_LDFLAGS (as set at the point in configure.ac) 19 | # 20 | # PACKAGE_NAME defaults to $PACKAGE if not set. 21 | # PACKAGE_LIBS defaults to -l$PACKAGE_NAME if not set. 22 | # 23 | # The resulting file is called $PACKAGE.pc.in / $PACKAGE.pc 24 | # 25 | # You will find this macro most useful in conjunction with 26 | # ax_spec_defaults that can read good initializers from the .spec file. In 27 | # consequencd, most of the generatable installable stuff can be made from 28 | # information being updated in a single place for the whole project. 29 | # 30 | # LICENSE 31 | # 32 | # Copyright (c) 2008 Guido U. Draheim 33 | # Copyright (c) 2008 Sven Verdoolaege 34 | # 35 | # This program is free software; you can redistribute it and/or modify it 36 | # under the terms of the GNU General Public License as published by the 37 | # Free Software Foundation; either version 2 of the License, or (at your 38 | # option) any later version. 39 | # 40 | # This program is distributed in the hope that it will be useful, but 41 | # WITHOUT ANY WARRANTY; without even the implied warranty of 42 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 43 | # Public License for more details. 44 | # 45 | # You should have received a copy of the GNU General Public License along 46 | # with this program. If not, see . 47 | # 48 | # As a special exception, the respective Autoconf Macro's copyright owner 49 | # gives unlimited permission to copy, distribute and modify the configure 50 | # scripts that are the output of Autoconf when processing the Macro. You 51 | # need not follow the terms of the GNU General Public License when using 52 | # or distributing such scripts, even though portions of the text of the 53 | # Macro appear in them. The GNU General Public License (GPL) does govern 54 | # all other use of the material that constitutes the Autoconf Macro. 55 | # 56 | # This special exception to the GPL applies to versions of the Autoconf 57 | # Macro released by the Autoconf Archive. When you make and distribute a 58 | # modified version of the Autoconf Macro, you may extend this special 59 | # exception to the GPL to apply to your modified version as well. 60 | 61 | #serial 9 62 | 63 | AC_DEFUN([AX_CREATE_PKGCONFIG_INFO],[dnl 64 | AS_VAR_PUSHDEF([PKGCONFIG_suffix],[ax_create_pkgconfig_suffix])dnl 65 | AS_VAR_PUSHDEF([PKGCONFIG_libdir],[ax_create_pkgconfig_libdir])dnl 66 | AS_VAR_PUSHDEF([PKGCONFIG_libfile],[ax_create_pkgconfig_libfile])dnl 67 | AS_VAR_PUSHDEF([PKGCONFIG_libname],[ax_create_pkgconfig_libname])dnl 68 | AS_VAR_PUSHDEF([PKGCONFIG_version],[ax_create_pkgconfig_version])dnl 69 | AS_VAR_PUSHDEF([PKGCONFIG_description],[ax_create_pkgconfig_description])dnl 70 | AS_VAR_PUSHDEF([PKGCONFIG_requires],[ax_create_pkgconfig_requires])dnl 71 | AS_VAR_PUSHDEF([PKGCONFIG_pkglibs],[ax_create_pkgconfig_pkglibs])dnl 72 | AS_VAR_PUSHDEF([PKGCONFIG_libs],[ax_create_pkgconfig_libs])dnl 73 | AS_VAR_PUSHDEF([PKGCONFIG_ldflags],[ax_create_pkgconfig_ldflags])dnl 74 | AS_VAR_PUSHDEF([PKGCONFIG_cppflags],[ax_create_pkgconfig_cppflags])dnl 75 | AS_VAR_PUSHDEF([PKGCONFIG_generate],[ax_create_pkgconfig_generate])dnl 76 | AS_VAR_PUSHDEF([PKGCONFIG_src_libdir],[ax_create_pkgconfig_src_libdir])dnl 77 | AS_VAR_PUSHDEF([PKGCONFIG_src_headers],[ax_create_pkgconfig_src_headers])dnl 78 | 79 | # we need the expanded forms... 80 | test "x$prefix" = xNONE && prefix=$ac_default_prefix 81 | test "x$exec_prefix" = xNONE && exec_prefix='${prefix}' 82 | 83 | AC_MSG_CHECKING(our pkgconfig libname) 84 | test ".$PKGCONFIG_libname" != "." || \ 85 | PKGCONFIG_libname="ifelse($1,,${PACKAGE_NAME},`basename $1 .pc`)" 86 | test ".$PKGCONFIG_libname" != "." || \ 87 | PKGCONFIG_libname="$PACKAGE" 88 | PKGCONFIG_libname=`eval echo "$PKGCONFIG_libname"` 89 | PKGCONFIG_libname=`eval echo "$PKGCONFIG_libname"` 90 | AC_MSG_RESULT($PKGCONFIG_libname) 91 | 92 | AC_MSG_CHECKING(our pkgconfig version) 93 | test ".$PKGCONFIG_version" != "." || \ 94 | PKGCONFIG_version="${PACKAGE_VERSION}" 95 | test ".$PKGCONFIG_version" != "." || \ 96 | PKGCONFIG_version="$VERSION" 97 | PKGCONFIG_version=`eval echo "$PKGCONFIG_version"` 98 | PKGCONFIG_version=`eval echo "$PKGCONFIG_version"` 99 | AC_MSG_RESULT($PKGCONFIG_version) 100 | 101 | AC_MSG_CHECKING(our pkgconfig_libdir) 102 | test ".$pkgconfig_libdir" = "." && \ 103 | pkgconfig_libdir='${libdir}/pkgconfig' 104 | PKGCONFIG_libdir=`eval echo "$pkgconfig_libdir"` 105 | PKGCONFIG_libdir=`eval echo "$PKGCONFIG_libdir"` 106 | PKGCONFIG_libdir=`eval echo "$PKGCONFIG_libdir"` 107 | AC_MSG_RESULT($pkgconfig_libdir) 108 | test "$pkgconfig_libdir" != "$PKGCONFIG_libdir" && ( 109 | AC_MSG_RESULT(expanded our pkgconfig_libdir... $PKGCONFIG_libdir)) 110 | AC_SUBST([pkgconfig_libdir]) 111 | 112 | AC_MSG_CHECKING(our pkgconfig_libfile) 113 | test ".$pkgconfig_libfile" != "." || \ 114 | pkgconfig_libfile="ifelse($1,,$PKGCONFIG_libname.pc,`basename $1`)" 115 | PKGCONFIG_libfile=`eval echo "$pkgconfig_libfile"` 116 | PKGCONFIG_libfile=`eval echo "$PKGCONFIG_libfile"` 117 | AC_MSG_RESULT($pkgconfig_libfile) 118 | test "$pkgconfig_libfile" != "$PKGCONFIG_libfile" && ( 119 | AC_MSG_RESULT(expanded our pkgconfig_libfile... $PKGCONFIG_libfile)) 120 | AC_SUBST([pkgconfig_libfile]) 121 | 122 | AC_MSG_CHECKING(our package / suffix) 123 | PKGCONFIG_suffix="$program_suffix" 124 | test ".$PKGCONFIG_suffix" != .NONE || PKGCONFIG_suffix="" 125 | AC_MSG_RESULT(${PACKAGE_NAME} / ${PKGCONFIG_suffix}) 126 | 127 | AC_MSG_CHECKING(our pkgconfig description) 128 | PKGCONFIG_description="ifelse($4,,$PACKAGE_SUMMARY,$4)" 129 | test ".$PKGCONFIG_description" != "." || \ 130 | PKGCONFIG_description="$PKGCONFIG_libname Library" 131 | PKGCONFIG_description=`eval echo "$PKGCONFIG_description"` 132 | PKGCONFIG_description=`eval echo "$PKGCONFIG_description"` 133 | AC_MSG_RESULT($PKGCONFIG_description) 134 | 135 | AC_MSG_CHECKING(our pkgconfig requires) 136 | PKGCONFIG_requires="ifelse($2,,$PACKAGE_REQUIRES,$2)" 137 | PKGCONFIG_requires=`eval echo "$PKGCONFIG_requires"` 138 | PKGCONFIG_requires=`eval echo "$PKGCONFIG_requires"` 139 | AC_MSG_RESULT($PKGCONFIG_requires) 140 | 141 | AC_MSG_CHECKING(our pkgconfig ext libs) 142 | PKGCONFIG_pkglibs="$PACKAGE_LIBS" 143 | test ".$PKGCONFIG_pkglibs" != "." || PKGCONFIG_pkglibs="-l$PKGCONFIG_libname" 144 | PKGCONFIG_libs="ifelse($3,,$PKGCONFIG_pkglibs $LIBS,$3)" 145 | PKGCONFIG_libs=`eval echo "$PKGCONFIG_libs"` 146 | PKGCONFIG_libs=`eval echo "$PKGCONFIG_libs"` 147 | AC_MSG_RESULT($PKGCONFIG_libs) 148 | 149 | AC_MSG_CHECKING(our pkgconfig cppflags) 150 | PKGCONFIG_cppflags="ifelse($5,,$CPPFLAGS $PACKAGE_CFLAGS,$5)" 151 | PKGCONFIG_cppflags=`eval echo "$PKGCONFIG_cppflags"` 152 | PKGCONFIG_cppflags=`eval echo "$PKGCONFIG_cppflags"` 153 | AC_MSG_RESULT($PKGCONFIG_cppflags) 154 | 155 | AC_MSG_CHECKING(our pkgconfig ldflags) 156 | PKGCONFIG_ldflags="ifelse($6,,$LDFLAGS $PACKAGE_LDFLAGS,$5)" 157 | PKGCONFIG_ldflags=`eval echo "$PKGCONFIG_ldflags"` 158 | PKGCONFIG_ldflags=`eval echo "$PKGCONFIG_ldflags"` 159 | AC_MSG_RESULT($PKGCONFIG_ldflags) 160 | 161 | test ".$PKGCONFIG_generate" != "." || \ 162 | PKGCONFIG_generate="ifelse($1,,$PKGCONFIG_libname.pc,$1)" 163 | PKGCONFIG_generate=`eval echo "$PKGCONFIG_generate"` 164 | PKGCONFIG_generate=`eval echo "$PKGCONFIG_generate"` 165 | test "$pkgconfig_libfile" != "$PKGCONFIG_generate" && ( 166 | AC_MSG_RESULT(generate the pkgconfig later... $PKGCONFIG_generate)) 167 | 168 | if test ".$PKGCONFIG_src_libdir" = "." ; then 169 | PKGCONFIG_src_libdir=`pwd` 170 | PKGCONFIG_src_libdir=`AS_DIRNAME("$PKGCONFIG_src_libdir/$PKGCONFIG_generate")` 171 | test ! -d $PKGCONFIG_src_libdir/src || \ 172 | PKGCONFIG_src_libdir="$PKGCONFIG_src_libdir/src" 173 | case ".$objdir" in 174 | *libs) PKGCONFIG_src_libdir="$PKGCONFIG_src_libdir/$objdir" ;; esac 175 | AC_MSG_RESULT(noninstalled pkgconfig -L $PKGCONFIG_src_libdir) 176 | fi 177 | 178 | if test ".$PKGCONFIG_src_headers" = "." ; then 179 | PKGCONFIG_src_headers=`pwd` 180 | v="$ac_top_srcdir" ; 181 | test ".$v" != "." || v="$ax_spec_dir" 182 | test ".$v" != "." || v="$srcdir" 183 | case "$v" in /*) PKGCONFIG_src_headers="" ;; esac 184 | PKGCONFIG_src_headers=`AS_DIRNAME("$PKGCONFIG_src_headers/$v/x")` 185 | test ! -d $PKGCONFIG_src_headers/incl[]ude || \ 186 | PKGCONFIG_src_headers="$PKGCONFIG_src_headers/incl[]ude" 187 | AC_MSG_RESULT(noninstalled pkgconfig -I $PKGCONFIG_src_headers) 188 | fi 189 | 190 | 191 | dnl AC_CONFIG_COMMANDS crap disallows to use $PKGCONFIG_libfile here... 192 | AC_CONFIG_COMMANDS([$ax_create_pkgconfig_generate],[ 193 | pkgconfig_generate="$ax_create_pkgconfig_generate" 194 | if test ! -f "$pkgconfig_generate.in" 195 | then generate="true" 196 | elif grep ' generated by configure ' $pkgconfig_generate.in >/dev/null 197 | then generate="true" 198 | else generate="false"; 199 | fi 200 | if $generate ; then 201 | AC_MSG_NOTICE(creating $pkgconfig_generate.in) 202 | cat > $pkgconfig_generate.in <conftest.sed < $pkgconfig_generate 244 | if test ! -s $pkgconfig_generate ; then 245 | AC_MSG_ERROR([$pkgconfig_generate is empty]) 246 | fi ; rm conftest.sed # DONE generate $pkgconfig_generate 247 | pkgconfig_uninstalled=`echo $pkgconfig_generate |sed 's/.pc$/-uninstalled.pc/'` 248 | AC_MSG_NOTICE(creating $pkgconfig_uninstalled) 249 | cat >conftest.sed < $pkgconfig_uninstalled 269 | if test ! -s $pkgconfig_uninstalled ; then 270 | AC_MSG_ERROR([$pkgconfig_uninstalled is empty]) 271 | fi ; rm conftest.sed # DONE generate $pkgconfig_uninstalled 272 | pkgconfig_requires_add=`echo ${pkgconfig_requires}` 273 | if test ".$pkgconfig_requires_add" != "." ; then 274 | pkgconfig_requires_add="pkg-config $pkgconfig_requires_add" 275 | else pkgconfig_requires_add=":" ; fi 276 | pkgconfig_uninstalled=`echo $pkgconfig_generate |sed 's/.pc$/-uninstalled.sh/'` 277 | AC_MSG_NOTICE(creating $pkgconfig_uninstalled) 278 | cat >conftest.sed <Name:>for option\\; do case \"\$option\" in --list-all|--name) echo > 297 | s>Description: *>\\;\\; --help) pkg-config --help \\; echo Buildscript Of > 298 | s>Version: *>\\;\\; --modversion|--version) echo > 299 | s>Requires:>\\;\\; --requires) echo $pkgconfig_requires_add> 300 | s>Libs: *>\\;\\; --libs) echo > 301 | s>Cflags: *>\\;\\; --cflags) echo > 302 | /--libs)/a\\ 303 | $pkgconfig_requires_add 304 | /--cflags)/a\\ 305 | $pkgconfig_requires_add\\ 306 | ;; --variable=*) eval echo '\$'\`echo \$option | sed -e 's/.*=//'\`\\ 307 | ;; --uninstalled) exit 0 \\ 308 | ;; *) ;; esac done 309 | AXEOF 310 | sed -f conftest.sed $pkgconfig_generate.in > $pkgconfig_uninstalled 311 | if test ! -s $pkgconfig_uninstalled ; then 312 | AC_MSG_ERROR([$pkgconfig_uninstalled is empty]) 313 | fi ; rm conftest.sed # DONE generate $pkgconfig_uninstalled 314 | ],[ 315 | dnl AC_CONFIG_COMMANDS crap, the AS_PUSHVAR defines are invalid here... 316 | ax_create_pkgconfig_generate="$ax_create_pkgconfig_generate" 317 | pkgconfig_prefix='$prefix' 318 | pkgconfig_execprefix='$exec_prefix' 319 | pkgconfig_bindir='$bindir' 320 | pkgconfig_libdir='$libdir' 321 | pkgconfig_includedir='$includedir' 322 | pkgconfig_datarootdir='$datarootdir' 323 | pkgconfig_datadir='$datadir' 324 | pkgconfig_sysconfdir='$sysconfdir' 325 | pkgconfig_suffix='$ax_create_pkgconfig_suffix' 326 | pkgconfig_package='$PACKAGE_NAME' 327 | pkgconfig_libname='$ax_create_pkgconfig_libname' 328 | pkgconfig_description='$ax_create_pkgconfig_description' 329 | pkgconfig_version='$ax_create_pkgconfig_version' 330 | pkgconfig_requires='$ax_create_pkgconfig_requires' 331 | pkgconfig_libs='$ax_create_pkgconfig_libs' 332 | pkgconfig_ldflags='$ax_create_pkgconfig_ldflags' 333 | pkgconfig_cppflags='$ax_create_pkgconfig_cppflags' 334 | pkgconfig_src_libdir='$ax_create_pkgconfig_src_libdir' 335 | pkgconfig_src_headers='$ax_create_pkgconfig_src_headers' 336 | ])dnl 337 | AS_VAR_POPDEF([PKGCONFIG_suffix])dnl 338 | AS_VAR_POPDEF([PKGCONFIG_libdir])dnl 339 | AS_VAR_POPDEF([PKGCONFIG_libfile])dnl 340 | AS_VAR_POPDEF([PKGCONFIG_libname])dnl 341 | AS_VAR_POPDEF([PKGCONFIG_version])dnl 342 | AS_VAR_POPDEF([PKGCONFIG_description])dnl 343 | AS_VAR_POPDEF([PKGCONFIG_requires])dnl 344 | AS_VAR_POPDEF([PKGCONFIG_pkglibs])dnl 345 | AS_VAR_POPDEF([PKGCONFIG_libs])dnl 346 | AS_VAR_POPDEF([PKGCONFIG_ldflags])dnl 347 | AS_VAR_POPDEF([PKGCONFIG_cppflags])dnl 348 | AS_VAR_POPDEF([PKGCONFIG_generate])dnl 349 | AS_VAR_POPDEF([PKGCONFIG_src_libdir])dnl 350 | AS_VAR_POPDEF([PKGCONFIG_src_headers])dnl 351 | ]) 352 | -------------------------------------------------------------------------------- /hevc_stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | hevc_stream.h 3 | Created by leslie_qiwa@gmail.com on 6/8/17 4 | */ 5 | 6 | 7 | #ifndef _HEVC_STREAM_H 8 | #define _HEVC_STREAM_H 1 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "bs.h" 15 | #include "h264_sei.h" 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #define MAX_NUM_SUBLAYERS 32 22 | #define MAX_NUM_HRD_PARAM 10 23 | #define MAX_CPB_CNT 32 24 | #define MAX_NUM_NEGATIVE_PICS 32 25 | #define MAX_NUM_POSITIVE_PICS 32 26 | #define MAX_NUM_REF_PICS_L0 32 27 | #define MAX_NUM_REF_PICS_L1 32 28 | #define MAX_NUM_SHORT_TERM_REF_PICS 32 29 | #define MAX_NUM_LONG_TERM_REF_PICS 32 30 | #define MAX_NUM_LONG_TERM_REF_PICS 32 31 | #define MAX_NUM_PALLETTE_PREDICTOR 32 32 | #define MAX_NUM_CHROMA_QP_OFFSET_LST 32 33 | #define MAX_NUM_ENTRY_POINT_OFFSET 32 34 | #define MAX_NUM_TILE_COLUMN 32 35 | #define MAX_NUM_TILE_ROW 32 36 | 37 | /** 38 | hrd_parameters 39 | @see E.2.2 HRD parameters syntax 40 | */ 41 | typedef struct 42 | { 43 | int bit_rate_value_minus1[MAX_CPB_CNT]; 44 | int cpb_size_value_minus1[MAX_CPB_CNT]; 45 | int cpb_size_du_value_minus1[MAX_CPB_CNT]; 46 | int bit_rate_du_value_minus1[MAX_CPB_CNT]; 47 | int cbr_flag[MAX_CPB_CNT]; 48 | } hevc_sub_layer_hrd_t; 49 | /** 50 | hrd_parameters 51 | @see E.2.2 HRD parameters syntax 52 | */ 53 | typedef struct 54 | { 55 | int nal_hrd_parameters_present_flag; 56 | int vcl_hrd_parameters_present_flag; 57 | int sub_pic_hrd_params_present_flag; 58 | int tick_divisor_minus2; 59 | int du_cpb_removal_delay_increment_length_minus1; 60 | int sub_pic_cpb_params_in_pic_timing_sei_flag; 61 | int dpb_output_delay_du_length_minus1; 62 | int bit_rate_scale; 63 | int cpb_size_scale; 64 | int cpb_size_du_scale; 65 | int initial_cpb_removal_delay_length_minus1; 66 | int au_cpb_removal_delay_length_minus1; 67 | int dpb_output_delay_length_minus1; 68 | int fixed_pic_rate_general_flag[MAX_NUM_SUBLAYERS]; 69 | int fixed_pic_rate_within_cvs_flag[MAX_NUM_SUBLAYERS]; 70 | int elemental_duration_in_tc_minus1[MAX_NUM_SUBLAYERS]; 71 | int low_delay_hrd_flag[MAX_NUM_SUBLAYERS]; 72 | int cpb_cnt_minus1[MAX_NUM_SUBLAYERS]; 73 | hevc_sub_layer_hrd_t sub_layer_hrd_nal[MAX_NUM_SUBLAYERS]; 74 | hevc_sub_layer_hrd_t sub_layer_hrd_vcl[MAX_NUM_SUBLAYERS]; 75 | } hevc_hrd_t; 76 | 77 | /** 78 | Profile, tier and level 79 | @see 7.3 Profile, tier and level syntax 80 | */ 81 | typedef struct 82 | { 83 | //profile parameters 84 | int general_profile_space; 85 | int general_tier_flag; 86 | int general_profile_idc; 87 | int general_profile_compatibility_flag[32]; 88 | int general_progressive_source_flag; 89 | int general_interlaced_source_flag; 90 | int general_non_packed_constraint_flag; 91 | int general_frame_only_constraint_flag; 92 | int general_max_12bit_constraint_flag; 93 | int general_max_10bit_constraint_flag; 94 | int general_max_8bit_constraint_flag; 95 | int general_max_422chroma_constraint_flag; 96 | int general_max_420chroma_constraint_flag; 97 | int general_max_monochrome_constraint_flag; 98 | int general_intra_constraint_flag; 99 | int general_one_picture_only_constraint_flag; 100 | int general_lower_bit_rate_constraint_flag; 101 | int general_max_14bit_constraint_flag; 102 | int general_inbld_flag; 103 | // level parameters 104 | int general_level_idc; 105 | int sub_layer_profile_present_flag[MAX_NUM_SUBLAYERS]; 106 | int sub_layer_level_present_flag[MAX_NUM_SUBLAYERS]; 107 | int sub_layer_profile_space[MAX_NUM_SUBLAYERS]; 108 | int sub_layer_tier_flag[MAX_NUM_SUBLAYERS]; 109 | int sub_layer_profile_idc[MAX_NUM_SUBLAYERS]; 110 | int sub_layer_profile_compatibility_flag[MAX_NUM_SUBLAYERS][32]; 111 | int sub_layer_progressive_source_flag[MAX_NUM_SUBLAYERS]; 112 | int sub_layer_interlaced_source_flag[MAX_NUM_SUBLAYERS]; 113 | int sub_layer_non_packed_constraint_flag[MAX_NUM_SUBLAYERS]; 114 | int sub_layer_frame_only_constraint_flag[MAX_NUM_SUBLAYERS]; 115 | int sub_layer_max_12bit_constraint_flag[MAX_NUM_SUBLAYERS]; 116 | int sub_layer_max_10bit_constraint_flag[MAX_NUM_SUBLAYERS]; 117 | int sub_layer_max_8bit_constraint_flag[MAX_NUM_SUBLAYERS]; 118 | int sub_layer_max_422chroma_constraint_flag[MAX_NUM_SUBLAYERS]; 119 | int sub_layer_max_420chroma_constraint_flag[MAX_NUM_SUBLAYERS]; 120 | int sub_layer_max_monochrome_constraint_flag[MAX_NUM_SUBLAYERS]; 121 | int sub_layer_intra_constraint_flag[MAX_NUM_SUBLAYERS]; 122 | int sub_layer_one_picture_only_constraint_flag[MAX_NUM_SUBLAYERS]; 123 | int sub_layer_lower_bit_rate_constraint_flag[MAX_NUM_SUBLAYERS]; 124 | int sub_layer_max_14bit_constraint_flag[MAX_NUM_SUBLAYERS]; 125 | int sub_layer_inbld_flag[MAX_NUM_SUBLAYERS]; 126 | int sub_layer_level_idc[MAX_NUM_SUBLAYERS]; 127 | } hevc_profile_tier_level_t; 128 | 129 | /** 130 | Scaling List Data 131 | @see 7.3.4 scaling list data syntax 132 | */ 133 | typedef struct 134 | { 135 | int scaling_list_pred_mode_flag[4][6]; 136 | int scaling_list_pred_matrix_id_delta[4][6]; 137 | int scaling_list_dc_coef_minus8[2][6]; 138 | int scaling_list_delta_coef[4][64]; 139 | } hevc_scaling_list_data_t; 140 | 141 | /** 142 | Video Parameter Set 143 | @see 7.3.2.1 Video parameter set RBSP syntax 144 | @see read_hevc_video_parameter_set_rbsp 145 | @see write_hevc_video_parameter_set_rbsp 146 | @see debug_vps 147 | */ 148 | typedef struct 149 | { 150 | int vps_video_parameter_set_id; 151 | int vps_base_layer_internal_flag; 152 | int vps_base_layer_available_flag; 153 | int vps_max_layers_minus1; 154 | int vps_max_sub_layers_minus1; 155 | int vps_temporal_id_nesting_flag; 156 | 157 | hevc_profile_tier_level_t ptl; 158 | 159 | int vps_sub_layer_ordering_info_present_flag; 160 | int vps_max_dec_pic_buffering_minus1[MAX_NUM_SUBLAYERS]; 161 | int vps_max_num_reorder_pics[MAX_NUM_SUBLAYERS]; 162 | int vps_max_latency_increase_plus1[MAX_NUM_SUBLAYERS]; 163 | int vps_max_layer_id; 164 | int vps_num_layer_sets_minus1; 165 | int layer_id_included_flag[MAX_NUM_SUBLAYERS][MAX_NUM_SUBLAYERS]; /*use same fixed value*/ 166 | int vps_timing_info_present_flag; 167 | int vps_num_units_in_tick; 168 | int vps_time_scale; 169 | int vps_poc_proportional_to_timing_flag; 170 | int vps_num_ticks_poc_diff_one_minus1; 171 | int vps_num_hrd_parameters; 172 | int hrd_layer_set_idx[MAX_NUM_HRD_PARAM]; 173 | int cprms_present_flag[MAX_NUM_HRD_PARAM]; 174 | hevc_hrd_t hrd[MAX_NUM_HRD_PARAM]; 175 | int vps_extension_flag; 176 | int vps_extension_data_flag; 177 | } hevc_vps_t; 178 | 179 | /** 180 | Sequence Parameter Set 181 | @see 7.3.2.2 Sequence parameter set RBSP syntax 182 | */ 183 | typedef struct 184 | { 185 | int inter_ref_pic_set_prediction_flag; 186 | int delta_idx_minus1; 187 | int delta_rps_sign; 188 | int abs_delta_rps_minus1; 189 | int used_by_curr_pic_flag[MAX_NUM_SHORT_TERM_REF_PICS]; 190 | int use_delta_flag[MAX_NUM_SHORT_TERM_REF_PICS]; 191 | int num_negative_pics; 192 | int num_positive_pics; 193 | int delta_poc_s0_minus1[MAX_NUM_NEGATIVE_PICS]; 194 | int used_by_curr_pic_s0_flag[MAX_NUM_NEGATIVE_PICS]; 195 | int delta_poc_s1_minus1[MAX_NUM_POSITIVE_PICS]; 196 | int used_by_curr_pic_s1_flag[MAX_NUM_NEGATIVE_PICS]; 197 | } hevc_st_ref_pic_set_t; 198 | 199 | /** 200 | Sequence Parameter Set 201 | @see 7.3.2.2 Sequence parameter set RBSP syntax 202 | */ 203 | typedef struct 204 | { 205 | int aspect_ratio_info_present_flag; 206 | int aspect_ratio_idc; 207 | int sar_width; 208 | int sar_height; 209 | int overscan_info_present_flag; 210 | int overscan_appropriate_flag; 211 | int video_signal_type_present_flag; 212 | int video_format; 213 | int video_full_range_flag; 214 | int colour_description_present_flag; 215 | int colour_primaries; 216 | int transfer_characteristics; 217 | int matrix_coefficients; 218 | int chroma_loc_info_present_flag; 219 | int chroma_sample_loc_type_top_field; 220 | int chroma_sample_loc_type_bottom_field; 221 | int neutral_chroma_indication_flag; 222 | int field_seq_flag; 223 | int frame_field_info_present_flag; 224 | int default_display_window_flag; 225 | int def_disp_win_left_offset; 226 | int def_disp_win_right_offset; 227 | int def_disp_win_top_offset; 228 | int def_disp_win_bottom_offset; 229 | int vui_timing_info_present_flag; 230 | int vui_num_units_in_tick; 231 | int vui_time_scale; 232 | int vui_poc_proportional_to_timing_flag; 233 | int vui_num_ticks_poc_diff_one_minus1; 234 | int vui_hrd_parameters_present_flag; 235 | hevc_hrd_t hrd; 236 | int bitstream_restriction_flag; 237 | int tiles_fixed_structure_flag; 238 | int motion_vectors_over_pic_boundaries_flag; 239 | int restricted_ref_pic_lists_flag; 240 | int min_spatial_segmentation_idc; 241 | int max_bytes_per_pic_denom; 242 | int max_bits_per_min_cu_denom; 243 | int log2_max_mv_length_horizontal; 244 | int log2_max_mv_length_vertical; 245 | } hevc_vui_t; 246 | 247 | /** 248 | Sequence Parameter Set range extension syntax 249 | @see 7.3.2.2.2 Sequence parameter set range extension syntax 250 | */ 251 | typedef struct 252 | { 253 | int transform_skip_rotation_enabled_flag; 254 | int transform_skip_context_enabled_flag; 255 | int implicit_rdpcm_enabled_flag; 256 | int explicit_rdpcm_enabled_flag; 257 | int extended_precision_processing_flag; 258 | int intra_smoothing_disabled_flag; 259 | int high_precision_offsets_enabled_flag; 260 | int persistent_rice_adaptation_enabled_flag; 261 | int cabac_bypass_alignment_enabled_flag; 262 | } hevc_sps_range_ext_t; 263 | 264 | /** 265 | Sequence Parameter Set screen content coding extension syntax 266 | @see 7.3.2.2.3 Sequence parameter set screen content coding extension syntax 267 | */ 268 | typedef struct 269 | { 270 | int sps_curr_pic_ref_enabled_flag; 271 | int palette_mode_enabled_flag; 272 | int palette_max_size; 273 | int delta_palette_max_predictor_size; 274 | int sps_palette_predictor_initializer_present_flag; 275 | int sps_num_palette_predictor_initializer_minus1; 276 | int sps_palette_predictor_initializers[3][MAX_NUM_PALLETTE_PREDICTOR]; 277 | int motion_vector_resolution_control_idc; 278 | int intra_boundary_filtering_disabled_flag; 279 | } hevc_sps_scc_ext_t; 280 | 281 | /** 282 | Sequence Parameter Set 283 | @see 7.3.2.2 Sequence parameter set RBSP syntax 284 | @see read_hevc_seq_parameter_set_rbsp 285 | @see write_hevc_seq_parameter_set_rbsp 286 | @see debug_sps 287 | */ 288 | typedef struct 289 | { 290 | int sps_video_parameter_set_id; 291 | int sps_max_sub_layers_minus1; 292 | int sps_temporal_id_nesting_flag; 293 | hevc_profile_tier_level_t ptl; 294 | int sps_seq_parameter_set_id; 295 | int chroma_format_idc; 296 | int separate_colour_plane_flag; 297 | int pic_width_in_luma_samples; 298 | int pic_height_in_luma_samples; 299 | int conformance_window_flag; 300 | int conf_win_left_offset; 301 | int conf_win_right_offset; 302 | int conf_win_top_offset; 303 | int conf_win_bottom_offset; 304 | int bit_depth_luma_minus8; 305 | int bit_depth_chroma_minus8; 306 | int log2_max_pic_order_cnt_lsb_minus4; 307 | int sps_sub_layer_ordering_info_present_flag; 308 | int sps_max_dec_pic_buffering_minus1[MAX_NUM_SUBLAYERS]; 309 | int sps_max_num_reorder_pics[MAX_NUM_SUBLAYERS]; 310 | int sps_max_latency_increase_plus1[MAX_NUM_SUBLAYERS]; 311 | int log2_min_luma_coding_block_size_minus3; 312 | int log2_diff_max_min_luma_coding_block_size; 313 | int log2_min_luma_transform_block_size_minus2; 314 | int log2_diff_max_min_luma_transform_block_size; 315 | int max_transform_hierarchy_depth_inter; 316 | int max_transform_hierarchy_depth_intra; 317 | int scaling_list_enabled_flag; 318 | int sps_scaling_list_data_present_flag; 319 | hevc_scaling_list_data_t scaling_list_data; 320 | int amp_enabled_flag; 321 | int sample_adaptive_offset_enabled_flag; 322 | int pcm_enabled_flag; 323 | int pcm_sample_bit_depth_luma_minus1; 324 | int pcm_sample_bit_depth_chroma_minus1; 325 | int log2_min_pcm_luma_coding_block_size_minus3; 326 | int log2_diff_max_min_pcm_luma_coding_block_size; 327 | int pcm_loop_filter_disabled_flag; 328 | int num_short_term_ref_pic_sets; 329 | hevc_st_ref_pic_set_t st_ref_pic_set[MAX_NUM_SHORT_TERM_REF_PICS]; 330 | int long_term_ref_pics_present_flag; 331 | int num_long_term_ref_pics_sps; 332 | int lt_ref_pic_poc_lsb_sps[MAX_NUM_LONG_TERM_REF_PICS]; 333 | int used_by_curr_pic_lt_sps_flag[MAX_NUM_LONG_TERM_REF_PICS]; 334 | int sps_temporal_mvp_enabled_flag; 335 | int strong_intra_smoothing_enabled_flag; 336 | int vui_parameters_present_flag; 337 | hevc_vui_t vui; 338 | int sps_extension_present_flag; 339 | int sps_range_extension_flag; 340 | int sps_multilayer_extension_flag; 341 | int sps_3d_extension_flag; 342 | int sps_extension_5bits; 343 | hevc_sps_range_ext_t sps_range_ext; 344 | //TODO: support sps_extension_data_flag; 345 | //TODO: support SVC/MVC extensions 346 | } hevc_sps_t; 347 | 348 | /** 349 | Picture Parameter Set range extension syntax 350 | @see 7.3.2.3.2 Picture parameter set range extension syntax 351 | */ 352 | typedef struct 353 | { 354 | int log2_max_transform_skip_block_size_minus2; 355 | int cross_component_prediction_enabled_flag; 356 | int chroma_qp_offset_list_enabled_flag; 357 | int diff_cu_chroma_qp_offset_depth; 358 | int chroma_qp_offset_list_len_minus1; 359 | int cb_qp_offset_list[MAX_NUM_CHROMA_QP_OFFSET_LST]; 360 | int cr_qp_offset_list[MAX_NUM_CHROMA_QP_OFFSET_LST]; 361 | int log2_sao_offset_scale_luma; 362 | int log2_sao_offset_scale_chroma; 363 | } hevc_pps_range_ext_t; 364 | 365 | /** 366 | Picture Parameter Set 367 | @see 7.3.2.3 Picture parameter set RBSP syntax 368 | @see read_hevc_pic_parameter_set_rbsp 369 | @see write_hevc_pic_parameter_set_rbsp 370 | @see debug_pps 371 | */ 372 | typedef struct 373 | { 374 | int pic_parameter_set_id; 375 | int seq_parameter_set_id; 376 | int dependent_slice_segments_enabled_flag; 377 | int output_flag_present_flag; 378 | int num_extra_slice_header_bits; 379 | int sign_data_hiding_enabled_flag; 380 | int cabac_init_present_flag; 381 | int num_ref_idx_l0_default_active_minus1; 382 | int num_ref_idx_l1_default_active_minus1; 383 | int init_qp_minus26; 384 | int constrained_intra_pred_flag; 385 | int transform_skip_enabled_flag; 386 | int cu_qp_delta_enabled_flag; 387 | int diff_cu_qp_delta_depth; 388 | int pps_cb_qp_offset; 389 | int pps_cr_qp_offset; 390 | int pps_slice_chroma_qp_offsets_present_flag; 391 | int weighted_pred_flag; 392 | int weighted_bipred_flag; 393 | int transquant_bypass_enabled_flag; 394 | int tiles_enabled_flag; 395 | int entropy_coding_sync_enabled_flag; 396 | int num_tile_columns_minus1; 397 | int num_tile_rows_minus1; 398 | int uniform_spacing_flag; 399 | int column_width_minus1[MAX_NUM_TILE_COLUMN]; 400 | int row_height_minus1[MAX_NUM_TILE_ROW]; 401 | int loop_filter_across_tiles_enabled_flag; 402 | int pps_loop_filter_across_slices_enabled_flag; 403 | int deblocking_filter_control_present_flag; 404 | int deblocking_filter_override_enabled_flag; 405 | int pps_deblocking_filter_disabled_flag; 406 | int pps_beta_offset_div2; 407 | int pps_tc_offset_div2; 408 | int pps_scaling_list_data_present_flag; 409 | hevc_scaling_list_data_t scaling_list_data; 410 | int lists_modification_present_flag; 411 | int log2_parallel_merge_level_minus2; 412 | int slice_segment_header_extension_present_flag; 413 | int pps_extension_present_flag; 414 | int pps_range_extension_flag; 415 | int pps_multilayer_extension_flag; 416 | int pps_3d_extension_flag; 417 | int pps_extension_5bits; 418 | hevc_pps_range_ext_t pps_range_ext; 419 | //TODO: support pps_extension_data_flag; 420 | //TODO: support SVC/MVC extensions 421 | } hevc_pps_t; 422 | 423 | /** 424 | Reference Picture List Modification 425 | @see 7.3.6.2 Reference picture list modification syntax 426 | */ 427 | typedef struct 428 | { 429 | int ref_pic_list_modification_flag_l0; 430 | int list_entry_l0[MAX_NUM_REF_PICS_L0]; 431 | int ref_pic_list_modification_flag_l1; 432 | int list_entry_l1[MAX_NUM_REF_PICS_L1]; 433 | } hevc_ref_pics_lists_mod_t; 434 | 435 | /** 436 | Weidht Prediction Table 437 | @see 7.3.6.3 Weighted prediction parameters syntax 438 | */ 439 | typedef struct 440 | { 441 | int luma_log2_weight_denom; 442 | int delta_chroma_log2_weight_denom; 443 | int luma_weight_l0_flag[MAX_NUM_REF_PICS_L0]; 444 | int chroma_weight_l0_flag[MAX_NUM_REF_PICS_L0]; 445 | int delta_luma_weight_l0[MAX_NUM_REF_PICS_L0]; 446 | int luma_offset_l0[MAX_NUM_REF_PICS_L0]; 447 | int delta_chroma_weight_l0[MAX_NUM_REF_PICS_L0][2]; 448 | int delta_chroma_offset_l0[MAX_NUM_REF_PICS_L0][2]; 449 | 450 | int luma_weight_l1_flag[MAX_NUM_REF_PICS_L1]; 451 | int chroma_weight_l1_flag[MAX_NUM_REF_PICS_L1]; 452 | int delta_luma_weight_l1[MAX_NUM_REF_PICS_L1]; 453 | int luma_offset_l1[MAX_NUM_REF_PICS_L1]; 454 | int delta_chroma_weight_l1[MAX_NUM_REF_PICS_L1][2]; 455 | int delta_chroma_offset_l1[MAX_NUM_REF_PICS_L1][2]; 456 | } hevc_pred_weight_table_t; 457 | 458 | /** 459 | Slice Segment Header 460 | @see 7.3.6 Slice segment header syntax 461 | @see read_hevc_slice_header_rbsp 462 | @see write_hevc_slice_header_rbsp 463 | @see debug_hevc_slice_header_rbsp 464 | */ 465 | typedef struct 466 | { 467 | int first_slice_segment_in_pic_flag; 468 | int no_output_of_prior_pics_flag; 469 | int pic_parameter_set_id; 470 | int dependent_slice_segment_flag; 471 | int slice_segment_address; 472 | int slice_type; 473 | int pic_output_flag; 474 | int colour_plane_id; 475 | int slice_pic_order_cnt_lsb; 476 | int short_term_ref_pic_set_sps_flag; 477 | hevc_st_ref_pic_set_t st_ref_pic_set; 478 | int short_term_ref_pic_set_idx; 479 | int num_long_term_sps; 480 | int num_long_term_pics; 481 | //TODO: is MAX_NUM_LONG_TERM_REF_PICS long enough? 482 | int lt_idx_sps[MAX_NUM_LONG_TERM_REF_PICS]; 483 | int poc_lsb_lt[MAX_NUM_LONG_TERM_REF_PICS]; 484 | int used_by_curr_pic_lt_flag[MAX_NUM_LONG_TERM_REF_PICS]; 485 | int delta_poc_msb_present_flag[MAX_NUM_LONG_TERM_REF_PICS]; 486 | int delta_poc_msb_cycle_lt[MAX_NUM_LONG_TERM_REF_PICS]; 487 | int slice_temporal_mvp_enabled_flag; 488 | int slice_sao_luma_flag; 489 | int slice_sao_chroma_flag; 490 | int num_ref_idx_active_override_flag; 491 | int num_ref_idx_l0_active_minus1; 492 | int num_ref_idx_l1_active_minus1; 493 | hevc_ref_pics_lists_mod_t rpld; 494 | int mvd_l1_zero_flag; 495 | int cabac_init_flag; 496 | int collocated_from_l0_flag; 497 | int collocated_ref_idx; 498 | hevc_pred_weight_table_t pwt; 499 | int five_minus_max_num_merge_cand; 500 | int slice_qp_delta; 501 | int slice_cb_qp_offset; 502 | int slice_cr_qp_offset; 503 | int cu_chroma_qp_offset_enabled_flag; 504 | int deblocking_filter_override_flag; 505 | int slice_deblocking_filter_disabled_flag; 506 | int slice_beta_offset_div2; 507 | int slice_tc_offset_div2; 508 | int slice_loop_filter_across_slices_enabled_flag; 509 | int num_entry_point_offsets; 510 | int offset_len_minus1; 511 | int entry_point_offset_minus1[MAX_NUM_ENTRY_POINT_OFFSET]; 512 | int slice_segment_header_extension_length; 513 | //TODO: slice_segment_header_extension_data_byte 514 | 515 | } hevc_slice_header_t; 516 | 517 | /** 518 | Network Abstraction Layer (NAL) unit 519 | @see 7.3.1 NAL unit syntax 520 | @see read_nal_unit 521 | @see write_nal_unit 522 | @see debug_nal 523 | */ 524 | typedef struct 525 | { 526 | int forbidden_zero_bit; 527 | int nal_unit_type; 528 | int nal_layer_id; 529 | int nal_temporal_id_plus1; 530 | } hevc_nal_t; 531 | 532 | typedef struct 533 | { 534 | int rbsp_size; 535 | uint8_t* rbsp_buf; 536 | } hevc_slice_data_rbsp_t; 537 | 538 | /** 539 | Access unit delimiter 540 | @see 7.3.5 Access unit delimiter syntax 541 | @see read_hevc_access_unit_delimiter 542 | @see write_hevc_access_unit_delimiter 543 | */ 544 | typedef struct 545 | { 546 | int primary_pic_type; 547 | } hevc_aud_t; 548 | 549 | /** 550 | HEVC stream 551 | Contains data structures for all NAL types that can be handled by this library. 552 | When reading, data is read into those, and when writing it is written from those. 553 | The reason why they are all contained in one place is that some of them depend on others, we need to 554 | have all of them available to read or write correctly. 555 | */ 556 | typedef struct 557 | { 558 | hevc_nal_t* nal; 559 | hevc_vps_t* vps; 560 | hevc_sps_t* sps; 561 | hevc_pps_t* pps; 562 | hevc_aud_t* aud; 563 | hevc_slice_header_t* sh; 564 | 565 | hevc_slice_data_rbsp_t* slice_data; 566 | 567 | hevc_sps_t* sps_table[32]; 568 | hevc_pps_t* pps_table[256]; 569 | } hevc_stream_t; 570 | 571 | hevc_stream_t* hevc_new(); 572 | void hevc_free(hevc_stream_t* h); 573 | 574 | int read_debug_hevc_nal_unit(hevc_stream_t* h, uint8_t* buf, int size); 575 | 576 | //Table 7-1 NAL unit type codes 577 | #define HEVC_NAL_UNIT_TYPE_TRAIL_N 0 // Coded slice segment of a non-TSA, non-STSA trailing picture 578 | #define HEVC_NAL_UNIT_TYPE_TRAIL_R 1 // Coded slice segment of a non-TSA, non-STSA trailing picture 579 | #define HEVC_NAL_UNIT_TYPE_TSA_N 2 // Coded slice segment of a TSA picture 580 | #define HEVC_NAL_UNIT_TYPE_TSA_R 3 // Coded slice segment of a TSA picture 581 | #define HEVC_NAL_UNIT_TYPE_STSA_N 4 // Coded slice segment of an STSA picture 582 | #define HEVC_NAL_UNIT_TYPE_STSA_R 5 // Coded slice segment of an STSA picture 583 | #define HEVC_NAL_UNIT_TYPE_RADL_N 6 // Coded slice segment of a RADL picture 584 | #define HEVC_NAL_UNIT_TYPE_RADL_R 7 // Coded slice segment of a RADL picture 585 | #define HEVC_NAL_UNIT_TYPE_RASL_N 8 // Coded slice segment of a RASL picture 586 | #define HEVC_NAL_UNIT_TYPE_RASL_R 9 // Coded slice segment of a RASL picture 587 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL_N10 10 // Reserved non-IRAP SLNR VCL NAL unit types 588 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL_R11 11 // Reserved non-IRAP sub-layer reference VCL NAL unit 589 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL_N12 12 // Reserved non-IRAP SLNR VCL NAL unit types 590 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL_R13 13 // Reserved non-IRAP sub-layer reference VCL NAL unit 591 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL_N14 14 // Reserved non-IRAP SLNR VCL NAL unit types 592 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL_R15 15 // Reserved non-IRAP sub-layer reference VCL NAL unit 593 | #define HEVC_NAL_UNIT_TYPE_BLA_W_LP 16 // Coded slice segment of a BLA picture 594 | #define HEVC_NAL_UNIT_TYPE_BLA_W_RADL 17 // Coded slice segment of a BLA picture 595 | #define HEVC_NAL_UNIT_TYPE_BLA_N_LP 18 // Coded slice segment of a BLA picture 596 | #define HEVC_NAL_UNIT_TYPE_IDR_W_RADL 19 // Coded slice segment of an IDR picture 597 | #define HEVC_NAL_UNIT_TYPE_IDR_N_LP 20 // Coded slice segment of an IDR picture 598 | #define HEVC_NAL_UNIT_TYPE_CRA_NUT 21 // Coded slice segment of a CRA picture 599 | #define HEVC_NAL_UNIT_TYPE_RSV_IRAP_VCL22 22 // Reserved IRAP VCL NAL unit types 600 | #define HEVC_NAL_UNIT_TYPE_RSV_IRAP_VCL23 23 // Reserved IRAP VCL NAL unit types 601 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL24 24 // Reserved non-IRAP VCL NAL unit types 602 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL25 25 // Reserved non-IRAP VCL NAL unit types 603 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL26 26 // Reserved non-IRAP VCL NAL unit types 604 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL27 27 // Reserved non-IRAP VCL NAL unit types 605 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL28 28 // Reserved non-IRAP VCL NAL unit types 606 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL29 29 // Reserved non-IRAP VCL NAL unit types 607 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL30 30 // Reserved non-IRAP VCL NAL unit types 608 | #define HEVC_NAL_UNIT_TYPE_RSV_VCL31 31 // Reserved non-IRAP VCL NAL unit types 609 | #define HEVC_NAL_UNIT_TYPE_VPS_NUT 32 // Video parameter set 610 | #define HEVC_NAL_UNIT_TYPE_SPS_NUT 33 // Sequence parameter set 611 | #define HEVC_NAL_UNIT_TYPE_PPS_NUT 34 // Picture parameter set 612 | #define HEVC_NAL_UNIT_TYPE_AUD_NUT 35 // Access unit delimiter 613 | #define HEVC_NAL_UNIT_TYPE_EOS_NUT 36 // End of sequence 614 | #define HEVC_NAL_UNIT_TYPE_EOB_NUT 37 // End of bitstream 615 | #define HEVC_NAL_UNIT_TYPE_FD_NUT 38 // Filler data 616 | #define HEVC_NAL_UNIT_TYPE_PREFIX_SEI_NUT 39 // Supplemental enhancement information 617 | #define HEVC_NAL_UNIT_TYPE_SUFFIX_SEI_NUT 40 // Supplemental enhancement information 618 | 619 | #define MAX_HEVC_VAL_UNIT_TYPE 40 620 | 621 | 622 | 623 | 624 | //7.4.3 Table 7-6. Name association to slice_type 625 | #define HEVC_SLICE_TYPE_B 0 // P (P slice) 626 | #define HEVC_SLICE_TYPE_P 1 // B (B slice) 627 | #define HEVC_SLICE_TYPE_I 2 // I (I slice) 628 | 629 | 630 | #define HEVC_PROFILE_BASELINE 66 631 | #define HEVC_PROFILE_MAIN 77 632 | #define HEVC_PROFILE_EXTENDED 88 633 | #define HEVC_PROFILE_HIGH 100 634 | 635 | // file handle for debug output 636 | extern FILE* h264_dbgfile; 637 | 638 | static inline long decimal_to_binary(int n) { 639 | int remainder; 640 | long binary = 0, i = 1; 641 | 642 | while(n != 0) { 643 | remainder = n%2; 644 | n = n/2; 645 | binary= binary + (remainder*i); 646 | i = i*10; 647 | } 648 | return binary; 649 | } 650 | #ifdef __cplusplus 651 | } 652 | #endif 653 | 654 | #endif 655 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /hevc_stream.in.c: -------------------------------------------------------------------------------- 1 | /* 2 | hevc_stream.c 3 | Created by leslie_qiwa@gmail.com on 6/8/17 4 | */ 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "bs.h" 13 | #include "h264_stream.h" 14 | #include "hevc_stream.h" 15 | #include "h264_sei.h" 16 | 17 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 18 | 19 | static void init_slice_hevc(hevc_stream_t* h) 20 | { 21 | memset(h->sh, 0, sizeof(hevc_slice_header_t)); 22 | 23 | h->sh->collocated_from_l0_flag = 1; 24 | } 25 | 26 | static int NumDeltaPocs[MAX_NUM_SHORT_TERM_REF_PICS]; 27 | static int NumNegativePics[MAX_NUM_NEGATIVE_PICS]; 28 | static int NumPositivePics[MAX_NUM_POSITIVE_PICS]; 29 | static int DeltaPocS0[MAX_NUM_REF_PICS_L0][MAX_NUM_NEGATIVE_PICS]; 30 | static int UsedByCurrPicS0[MAX_NUM_REF_PICS_L0][MAX_NUM_NEGATIVE_PICS]; 31 | static int DeltaPocS1[MAX_NUM_REF_PICS_L1][MAX_NUM_POSITIVE_PICS]; 32 | static int UsedByCurrPicS1[MAX_NUM_REF_PICS_L1][MAX_NUM_POSITIVE_PICS]; 33 | 34 | //TODO: return right one 35 | static int getNumPicTotalCurr(hevc_sps_t* sps, hevc_slice_header_t* sh) 36 | { 37 | int i; 38 | int NumPicTotalCurr = 0; 39 | int CurrRpsIdx = sps->num_short_term_ref_pic_sets; 40 | 41 | if( sh->short_term_ref_pic_set_sps_flag ) 42 | CurrRpsIdx = sh->short_term_ref_pic_set_idx; 43 | for( i = 0; i < NumNegativePics[ CurrRpsIdx ]; i++ ) 44 | if( UsedByCurrPicS0[ CurrRpsIdx ][ i ] ) 45 | NumPicTotalCurr++; 46 | for( i = 0; i < NumPositivePics[ CurrRpsIdx ]; i++) 47 | if( UsedByCurrPicS1[ CurrRpsIdx ][ i ] ) 48 | NumPicTotalCurr++; 49 | for( i = 0; i < sh->num_long_term_sps + sh->num_long_term_pics; i++ ) { 50 | int UsedByCurrPicLt = 0; 51 | if( i < sh->num_long_term_sps ) 52 | UsedByCurrPicLt = sps->used_by_curr_pic_lt_sps_flag[ sh->lt_idx_sps[ i ] ]; 53 | else 54 | UsedByCurrPicLt = sh->used_by_curr_pic_lt_flag[ i ]; 55 | if( UsedByCurrPicLt ) 56 | NumPicTotalCurr++; 57 | } 58 | return NumPicTotalCurr; 59 | } 60 | 61 | static void updateNumDeltaPocs(hevc_st_ref_pic_set_t *st_ref_pic_set, int stRpsIdx) { 62 | int RefRpsIdx = stRpsIdx - ( st_ref_pic_set->delta_idx_minus1 + 1 ); 63 | 64 | if( st_ref_pic_set->inter_ref_pic_set_prediction_flag ) { 65 | int i, j, dPoc; 66 | int deltaRps = ( 1 - 2 * st_ref_pic_set->delta_rps_sign ) * ( st_ref_pic_set->abs_delta_rps_minus1 + 1 ); 67 | i = 0; 68 | for( j = NumPositivePics[ RefRpsIdx ] - 1; j >= 0; j-- ) { 69 | dPoc = DeltaPocS1[ RefRpsIdx ][ j ] + deltaRps; 70 | if( dPoc < 0 && st_ref_pic_set->use_delta_flag[ NumNegativePics[ RefRpsIdx ] + j ] ) { 71 | DeltaPocS0[ stRpsIdx ][ i ] = dPoc; 72 | UsedByCurrPicS0[ stRpsIdx ][ i++ ] = st_ref_pic_set->used_by_curr_pic_flag[ NumNegativePics[ RefRpsIdx ] + j ]; 73 | } 74 | } 75 | if( deltaRps < 0 && st_ref_pic_set->use_delta_flag[ NumDeltaPocs[ RefRpsIdx ] ] ) { 76 | DeltaPocS0[ stRpsIdx ][ i ] = deltaRps; 77 | UsedByCurrPicS0[ stRpsIdx ][ i++ ] = st_ref_pic_set->used_by_curr_pic_flag[ NumDeltaPocs[ RefRpsIdx ] ]; 78 | } 79 | for( j = 0; j < NumNegativePics[ RefRpsIdx ]; j++ ) { 80 | dPoc = DeltaPocS0[ RefRpsIdx ][ j ] + deltaRps; 81 | if( dPoc < 0 && st_ref_pic_set->use_delta_flag[ j ] ) { 82 | DeltaPocS0[ stRpsIdx ][ i ] = dPoc; 83 | UsedByCurrPicS0[ stRpsIdx ][ i++ ] = st_ref_pic_set->used_by_curr_pic_flag[ j ]; 84 | } 85 | } 86 | NumNegativePics[ stRpsIdx ] = i; 87 | i = 0; 88 | for( j = NumNegativePics[ RefRpsIdx ] - 1; j >= 0; j-- ) { 89 | dPoc = DeltaPocS0[ RefRpsIdx ][ j ] + deltaRps; 90 | if( dPoc > 0 && st_ref_pic_set->use_delta_flag[ j ] ) { 91 | DeltaPocS1[ stRpsIdx ][ i ] = dPoc; 92 | UsedByCurrPicS1[ stRpsIdx ][ i++ ] = st_ref_pic_set->used_by_curr_pic_flag[ j ]; 93 | } 94 | } 95 | if( deltaRps > 0 && st_ref_pic_set->use_delta_flag[ NumDeltaPocs[ RefRpsIdx ] ] ) { 96 | DeltaPocS1[ stRpsIdx ][ i ] = deltaRps; 97 | UsedByCurrPicS1[ stRpsIdx ][ i++ ] = st_ref_pic_set->used_by_curr_pic_flag[ NumDeltaPocs[ RefRpsIdx ] ]; 98 | } 99 | for( j = 0; j < NumPositivePics[ RefRpsIdx ]; j++) { 100 | dPoc = DeltaPocS1[ RefRpsIdx ][ j ] + deltaRps; 101 | if( dPoc > 0 && st_ref_pic_set->use_delta_flag[ NumNegativePics[ RefRpsIdx ] + j ] ) { 102 | DeltaPocS1[ stRpsIdx ][ i ] = dPoc; 103 | UsedByCurrPicS1[ stRpsIdx ][ i++ ] = st_ref_pic_set->used_by_curr_pic_flag[ NumNegativePics[ RefRpsIdx ] + j ]; 104 | } 105 | } 106 | NumPositivePics[ stRpsIdx ] = i; 107 | } else { 108 | NumNegativePics[ stRpsIdx ] = st_ref_pic_set->num_negative_pics; 109 | NumPositivePics[ stRpsIdx ] = st_ref_pic_set->num_positive_pics; 110 | } 111 | 112 | NumDeltaPocs[ stRpsIdx ] = NumNegativePics[ stRpsIdx ] + NumPositivePics[ stRpsIdx ]; 113 | } 114 | 115 | static int getSliceSegmentAddressBitLength(hevc_sps_t *sps) { 116 | int MinCbLog2SizeY = sps->log2_min_luma_coding_block_size_minus3 + 3; 117 | int CtbLog2SizeY = MinCbLog2SizeY + sps->log2_diff_max_min_luma_coding_block_size; 118 | int CtbSizeY = 1 << CtbLog2SizeY; 119 | int PicWidthInCtbsY = ceil( sps->pic_width_in_luma_samples * 1.0f / CtbSizeY ); 120 | int PicHeightInCtbsY = ceil( sps->pic_height_in_luma_samples * 1.0f / CtbSizeY ); 121 | int PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY; 122 | return ceil( log2( PicSizeInCtbsY ) ) ; 123 | } 124 | 125 | #end_preamble 126 | 127 | #function_declarations 128 | 129 | 130 | //7.3.1 NAL unit syntax 131 | int structure(hevc_nal_unit)(hevc_stream_t* h, uint8_t* buf, int size) 132 | { 133 | hevc_nal_t* nal = h->nal; 134 | 135 | int nal_size = size; 136 | int rbsp_size = size; 137 | uint8_t* rbsp_buf = (uint8_t*)calloc(1, rbsp_size); 138 | 139 | if( is_reading ) 140 | { 141 | int rc = nal_to_rbsp(buf, &nal_size, rbsp_buf, &rbsp_size); 142 | 143 | if (rc < 0) { free(rbsp_buf); return -1; } // handle conversion error 144 | } 145 | 146 | if( is_writing ) 147 | { 148 | rbsp_size = size*3/4; // NOTE this may have to be slightly smaller (3/4 smaller, worst case) in order to be guaranteed to fit 149 | } 150 | 151 | bs_t* b = bs_new(rbsp_buf, rbsp_size); 152 | value( forbidden_zero_bit, f(1, 0) ); 153 | value( nal->nal_unit_type, u(6) ); 154 | value( nal->nal_layer_id, u(6) ); 155 | value( nal->nal_temporal_id_plus1, u(3) ); 156 | 157 | switch ( nal->nal_unit_type ) 158 | { 159 | case HEVC_NAL_UNIT_TYPE_TRAIL_N: 160 | case HEVC_NAL_UNIT_TYPE_TRAIL_R: 161 | case HEVC_NAL_UNIT_TYPE_TSA_N: 162 | case HEVC_NAL_UNIT_TYPE_TSA_R: 163 | case HEVC_NAL_UNIT_TYPE_STSA_N: 164 | case HEVC_NAL_UNIT_TYPE_STSA_R: 165 | case HEVC_NAL_UNIT_TYPE_RADL_N: 166 | case HEVC_NAL_UNIT_TYPE_RADL_R: 167 | case HEVC_NAL_UNIT_TYPE_RASL_N: 168 | case HEVC_NAL_UNIT_TYPE_RASL_R: 169 | case HEVC_NAL_UNIT_TYPE_BLA_W_LP: 170 | case HEVC_NAL_UNIT_TYPE_BLA_W_RADL: 171 | case HEVC_NAL_UNIT_TYPE_BLA_N_LP: 172 | case HEVC_NAL_UNIT_TYPE_IDR_W_RADL: 173 | case HEVC_NAL_UNIT_TYPE_IDR_N_LP: 174 | case HEVC_NAL_UNIT_TYPE_CRA_NUT: 175 | 176 | structure(hevc_slice_layer_rbsp)(h, b); 177 | break; 178 | 179 | #ifdef HAVE_SEI 180 | case NAL_UNIT_TYPE_SEI: 181 | structure(hevc_sei_rbsp)(h, b); 182 | break; 183 | #endif 184 | 185 | case HEVC_NAL_UNIT_TYPE_VPS_NUT: 186 | structure(hevc_video_parameter_set_rbsp)(h, b); 187 | break; 188 | 189 | case HEVC_NAL_UNIT_TYPE_SPS_NUT: 190 | structure(hevc_seq_parameter_set_rbsp)(h, b); 191 | break; 192 | 193 | case HEVC_NAL_UNIT_TYPE_PPS_NUT: 194 | structure(hevc_pic_parameter_set_rbsp)(h, b); 195 | break; 196 | 197 | default: 198 | return -1; 199 | } 200 | 201 | if (bs_overrun(b)) { bs_free(b); free(rbsp_buf); return -1; } 202 | 203 | if( is_writing ) 204 | { 205 | // now get the actual size used 206 | rbsp_size = bs_pos(b); 207 | 208 | int rc = rbsp_to_nal(rbsp_buf, &rbsp_size, buf, &nal_size); 209 | if (rc < 0) { bs_free(b); free(rbsp_buf); return -1; } 210 | } 211 | 212 | bs_free(b); 213 | free(rbsp_buf); 214 | 215 | return nal_size; 216 | } 217 | 218 | //7.3.2.1 Sequence parameter set RBSP syntax 219 | void structure(hevc_video_parameter_set_rbsp)(hevc_stream_t* h, bs_t* b) 220 | { 221 | int i, j; 222 | 223 | hevc_vps_t* vps = h->vps; 224 | if( is_reading ) 225 | { 226 | memset(vps, 0, sizeof(hevc_vps_t)); 227 | } 228 | 229 | value( vps->vps_video_parameter_set_id, u(4) ); 230 | value( vps->vps_base_layer_internal_flag, u1 ); 231 | value( vps->vps_base_layer_available_flag, u1 ); 232 | value( vps->vps_max_layers_minus1, u(6) ); 233 | value( vps->vps_max_sub_layers_minus1, u(3) ); 234 | value( vps->vps_temporal_id_nesting_flag, u1 ); 235 | value( vps_reserved_0xffff_16bits, f(16, 0xffff) ); 236 | 237 | structure(hevc_profile_tier_level)(&vps->ptl, b, 1, vps->vps_max_sub_layers_minus1); 238 | 239 | value( vps->vps_sub_layer_ordering_info_present_flag, u1 ); 240 | for( i = ( vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers_minus1 ); 241 | i <= vps->vps_max_sub_layers_minus1; i++ ) { 242 | value( vps->vps_max_dec_pic_buffering_minus1[ i ],ue ); 243 | value( vps->vps_max_num_reorder_pics[ i ], ue ); 244 | value( vps->vps_max_latency_increase_plus1[ i ], ue ); 245 | } 246 | value( vps->vps_max_layer_id, u(6) ); 247 | value( vps->vps_num_layer_sets_minus1, ue ); 248 | for( i = 1; i <= vps->vps_num_layer_sets_minus1; i++ ) 249 | for( j = 0; j <= vps->vps_max_layer_id; j++ ) { 250 | value( vps->layer_id_included_flag[ i ][ j ], u1 ); 251 | } 252 | value( vps->vps_timing_info_present_flag, u1 ); 253 | if( vps->vps_timing_info_present_flag ) { 254 | value( vps->vps_num_units_in_tick, u(32) ); 255 | value( vps->vps_time_scale, u(32) ); 256 | value( vps->vps_poc_proportional_to_timing_flag, u1 ); 257 | if( vps->vps_poc_proportional_to_timing_flag ) { 258 | value( vps->vps_num_ticks_poc_diff_one_minus1,ue ); 259 | } 260 | value( vps->vps_num_hrd_parameters, ue ); 261 | for( i = 0; i < vps->vps_num_hrd_parameters; i++ ) { 262 | value( vps->hrd_layer_set_idx[ i ], ue ); 263 | if (i > 0) { 264 | value( vps->cprms_present_flag[ i ], u1 ); 265 | } 266 | structure(hevc_hrd_parameters)(&vps->hrd[i], b, 267 | vps->cprms_present_flag[ i ], 268 | vps->vps_max_sub_layers_minus1); 269 | } 270 | } 271 | value( vps->vps_extension_flag, u1 ); 272 | //TODO: support extension data 273 | //if (vps->vps_extension_flag) 274 | 275 | structure(hevc_rbsp_trailing_bits)(b); 276 | } 277 | 278 | //7.3.2.2 Sequence parameter set RBSP syntax 279 | void structure(hevc_seq_parameter_set_rbsp)(hevc_stream_t* h, bs_t* b) 280 | { 281 | int i; 282 | 283 | hevc_sps_t* sps = h->sps; 284 | if( is_reading ) 285 | { 286 | memset(sps, 0, sizeof(hevc_sps_t)); 287 | } 288 | 289 | value( sps->sps_video_parameter_set_id, u(4) ); 290 | value( sps->sps_max_sub_layers_minus1, u(3) ); 291 | value( sps->sps_temporal_id_nesting_flag, u1 ); 292 | structure(hevc_profile_tier_level)(&sps->ptl, b, 1, sps->sps_max_sub_layers_minus1); 293 | value( sps->sps_seq_parameter_set_id, ue ); 294 | value( sps->chroma_format_idc, ue ); 295 | if( sps->chroma_format_idc == 3 ) { 296 | value( sps->separate_colour_plane_flag, u1 ); 297 | } 298 | value( sps->pic_width_in_luma_samples, ue ); 299 | value( sps->pic_height_in_luma_samples, ue ); 300 | value( sps->conformance_window_flag, u1 ); 301 | if( sps->conformance_window_flag ) { 302 | value( sps->conf_win_left_offset, ue ); 303 | value( sps->conf_win_right_offset, ue ); 304 | value( sps->conf_win_top_offset, ue ); 305 | value( sps->conf_win_bottom_offset, ue ); 306 | } 307 | value( sps->bit_depth_luma_minus8, ue ); 308 | value( sps->bit_depth_chroma_minus8, ue ); 309 | value( sps->log2_max_pic_order_cnt_lsb_minus4, ue ); 310 | value( sps->sps_sub_layer_ordering_info_present_flag, u1 ); 311 | for( i = ( sps->sps_sub_layer_ordering_info_present_flag ? 0 : sps->sps_max_sub_layers_minus1 ); 312 | i <= sps->sps_max_sub_layers_minus1; i++ ) { 313 | value( sps->sps_max_dec_pic_buffering_minus1 [ i ], ue ); 314 | value( sps->sps_max_num_reorder_pics [ i ], ue ); 315 | value( sps->sps_max_latency_increase_plus1 [ i ], ue ); 316 | } 317 | value( sps->log2_min_luma_coding_block_size_minus3, ue ); 318 | value( sps->log2_diff_max_min_luma_coding_block_size, ue ); 319 | value( sps->log2_min_luma_transform_block_size_minus2, ue ); 320 | value( sps->log2_diff_max_min_luma_transform_block_size, ue ); 321 | value( sps->max_transform_hierarchy_depth_inter, ue ); 322 | value( sps->max_transform_hierarchy_depth_intra, ue ); 323 | value( sps->scaling_list_enabled_flag, u1 ); 324 | 325 | if( sps->scaling_list_enabled_flag ) { 326 | value( sps->sps_scaling_list_data_present_flag, u1 ); 327 | if( sps->sps_scaling_list_data_present_flag ) { 328 | structure(hevc_scaling_list_data)(&sps->scaling_list_data, b); 329 | } 330 | } 331 | 332 | value( sps->amp_enabled_flag, u1 ); 333 | value( sps->sample_adaptive_offset_enabled_flag, u1 ); 334 | value( sps->pcm_enabled_flag, u1 ); 335 | if( sps->pcm_enabled_flag ) { 336 | value( sps->pcm_sample_bit_depth_luma_minus1, u(4) ); 337 | value( sps->pcm_sample_bit_depth_chroma_minus1, u(4) ); 338 | value( sps->log2_min_pcm_luma_coding_block_size_minus3, ue ); 339 | value( sps->log2_diff_max_min_pcm_luma_coding_block_size, ue ); 340 | value( sps->pcm_loop_filter_disabled_flag, u1 ); 341 | } 342 | value( sps->num_short_term_ref_pic_sets, ue ); 343 | for( i = 0; i < sps->num_short_term_ref_pic_sets; i++) { 344 | structure(hevc_st_ref_pic_set)(&sps->st_ref_pic_set[i], b, i, sps->num_short_term_ref_pic_sets); 345 | } 346 | 347 | value( sps->long_term_ref_pics_present_flag, u1 ); 348 | if( sps->long_term_ref_pics_present_flag ) { 349 | value( sps->num_long_term_ref_pics_sps, ue ); 350 | for( i = 0; i < sps->num_long_term_ref_pics_sps; i++ ) { 351 | value( sps->lt_ref_pic_poc_lsb_sps[ i ], u( sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ) ); 352 | value( sps->used_by_curr_pic_lt_sps_flag[ i ], u1 ); 353 | } 354 | } 355 | value( sps->sps_temporal_mvp_enabled_flag, u1 ); 356 | value( sps->strong_intra_smoothing_enabled_flag, u1 ); 357 | value( sps->vui_parameters_present_flag, u1 ); 358 | if( sps->vui_parameters_present_flag ) { 359 | structure(hevc_vui_parameters)(sps, b); 360 | } 361 | value( sps->sps_extension_present_flag, u1 ); 362 | 363 | if( sps->sps_extension_present_flag ) { 364 | value( sps->sps_range_extension_flag, u1 ); 365 | value( sps->sps_multilayer_extension_flag, u1 ); 366 | value( sps->sps_3d_extension_flag, u1 ); 367 | value( sps->sps_extension_5bits, u(5) ); 368 | } 369 | if( sps->sps_range_extension_flag ) { 370 | structure(hevc_sps_range_extension)( &sps->sps_range_ext, b); 371 | } 372 | 373 | if( is_reading ) 374 | { 375 | memcpy(h->sps_table[sps->sps_seq_parameter_set_id], h->sps, sizeof(hevc_sps_t)); 376 | } 377 | } 378 | 379 | //7.3.2.2.2 Sequence parameter set range extension syntax 380 | void structure(hevc_sps_range_extension)(hevc_sps_range_ext_t* sps_range_ext, bs_t* b) 381 | { 382 | value( sps_range_ext->transform_skip_rotation_enabled_flag, u1); 383 | value( sps_range_ext->transform_skip_context_enabled_flag, u1); 384 | value( sps_range_ext->implicit_rdpcm_enabled_flag, u1); 385 | value( sps_range_ext->explicit_rdpcm_enabled_flag, u1); 386 | value( sps_range_ext->extended_precision_processing_flag, u1); 387 | value( sps_range_ext->intra_smoothing_disabled_flag, u1); 388 | value( sps_range_ext->high_precision_offsets_enabled_flag, u1); 389 | value( sps_range_ext->persistent_rice_adaptation_enabled_flag, u1); 390 | value( sps_range_ext->cabac_bypass_alignment_enabled_flag, u1); 391 | } 392 | 393 | 394 | //7.3.2.3 Picture parameter set RBSP syntax 395 | void structure(hevc_pic_parameter_set_rbsp)(hevc_stream_t* h, bs_t* b) 396 | { 397 | int i; 398 | hevc_pps_t* pps = h->pps; 399 | if( is_reading ) 400 | { 401 | memset(pps, 0, sizeof(hevc_pps_t)); 402 | } 403 | 404 | value( pps->pic_parameter_set_id, ue); 405 | value( pps->seq_parameter_set_id, ue ); 406 | value( pps->dependent_slice_segments_enabled_flag, u1 ); 407 | value( pps->output_flag_present_flag, u1 ); 408 | value( pps->num_extra_slice_header_bits, u( 3 ) ); 409 | value( pps->sign_data_hiding_enabled_flag, u1 ); 410 | value( pps->cabac_init_present_flag, u1 ); 411 | value( pps->num_ref_idx_l0_default_active_minus1, ue ); 412 | value( pps->num_ref_idx_l1_default_active_minus1, ue ); 413 | value( pps->init_qp_minus26, se ); 414 | value( pps->constrained_intra_pred_flag, u1 ); 415 | value( pps->transform_skip_enabled_flag, u1 ); 416 | value( pps->cu_qp_delta_enabled_flag, u1 ); 417 | if( pps->cu_qp_delta_enabled_flag ) { 418 | value( pps->diff_cu_qp_delta_depth, ue); 419 | } 420 | value( pps->pps_cb_qp_offset, se ); 421 | value( pps->pps_cr_qp_offset, se ); 422 | value( pps->pps_slice_chroma_qp_offsets_present_flag, u1 ); 423 | value( pps->weighted_pred_flag, u1 ); 424 | value( pps->weighted_bipred_flag, u1 ); 425 | value( pps->transquant_bypass_enabled_flag, u1 ); 426 | value( pps->tiles_enabled_flag, u1 ); 427 | value( pps->entropy_coding_sync_enabled_flag, u1 ); 428 | if( pps->tiles_enabled_flag ) { 429 | value( pps->num_tile_columns_minus1, ue ); 430 | value( pps->num_tile_rows_minus1, ue ); 431 | value( pps->uniform_spacing_flag, u1 ); 432 | if( !pps->uniform_spacing_flag ) { 433 | for( i = 0; i < pps->num_tile_columns_minus1; i++ ) { 434 | value( pps->column_width_minus1[ i ], ue ); 435 | } 436 | for( i = 0; i < pps->num_tile_rows_minus1; i++ ) { 437 | value( pps->row_height_minus1[ i ], ue ); 438 | } 439 | } 440 | value( pps->loop_filter_across_tiles_enabled_flag, u1 ); 441 | } 442 | value( pps->pps_loop_filter_across_slices_enabled_flag, u1 ); 443 | value( pps->deblocking_filter_control_present_flag, u1 ); 444 | if( pps->deblocking_filter_control_present_flag ) { 445 | value( pps->deblocking_filter_override_enabled_flag, u1 ); 446 | value( pps->pps_deblocking_filter_disabled_flag, u1 ); 447 | if( pps->pps_deblocking_filter_disabled_flag ) { 448 | value( pps->pps_beta_offset_div2, se ); 449 | value( pps->pps_tc_offset_div2, se ); 450 | } 451 | } 452 | value( pps->pps_scaling_list_data_present_flag, u1 ); 453 | if( pps->pps_scaling_list_data_present_flag ) { 454 | structure(hevc_scaling_list_data)(&pps->scaling_list_data, b); 455 | } 456 | value( pps->lists_modification_present_flag, u1 ); 457 | value( pps->log2_parallel_merge_level_minus2, ue ); 458 | value( pps->slice_segment_header_extension_present_flag, u1 ); 459 | value( pps->pps_extension_present_flag, u1 ); 460 | if( pps->pps_extension_present_flag ) { 461 | value( pps->pps_range_extension_flag, u1 ); 462 | value( pps->pps_multilayer_extension_flag, u1 ); 463 | value( pps->pps_3d_extension_flag, u1 ); 464 | value( pps->pps_extension_5bits, u1 ); 465 | } 466 | if( pps->pps_range_extension_flag ) { 467 | structure(hevc_pps_range_extension)( pps, b); 468 | } 469 | 470 | structure(hevc_rbsp_trailing_bits)(b); 471 | 472 | if( is_reading ) 473 | { 474 | memcpy(h->pps_table[pps->pic_parameter_set_id], h->pps, sizeof(hevc_pps_t)); 475 | } 476 | } 477 | 478 | //7.3.2.3.2 Picture parameter set range extension syntax 479 | void structure(hevc_pps_range_extension)(hevc_pps_t* pps, bs_t* b) 480 | { 481 | hevc_pps_range_ext_t *pps_range_ext = &pps->pps_range_ext;; 482 | if( pps->transform_skip_enabled_flag ) { 483 | value( pps_range_ext->log2_max_transform_skip_block_size_minus2, ue); 484 | } 485 | value( pps_range_ext->cross_component_prediction_enabled_flag, u1); 486 | value( pps_range_ext->chroma_qp_offset_list_enabled_flag, u1); 487 | if( pps_range_ext->chroma_qp_offset_list_enabled_flag ) { 488 | value( pps_range_ext->diff_cu_chroma_qp_offset_depth, ue); 489 | value( pps_range_ext->chroma_qp_offset_list_len_minus1, ue); 490 | for( int i = 0; i <= pps_range_ext->chroma_qp_offset_list_len_minus1; i++ ) { 491 | value( pps_range_ext->cb_qp_offset_list[ i ], se); 492 | value( pps_range_ext->cr_qp_offset_list[ i ], se); 493 | } 494 | } 495 | value( pps_range_ext->log2_sao_offset_scale_luma, ue); 496 | value( pps_range_ext->log2_sao_offset_scale_chroma, ue); 497 | } 498 | 499 | #ifdef HAVE_SEI 500 | //7.3.2.4 Supplemental enhancement information RBSP syntax 501 | void structure(sei_rbsp)(hevc_stream_t* h, bs_t* b) 502 | { 503 | if( is_reading ) 504 | { 505 | for( int i = 0; i < h->num_seis; i++ ) { 506 | sei_free(h->seis[i]); 507 | } 508 | 509 | h->num_seis = 0; 510 | do { 511 | h->num_seis++; 512 | h->seis = (sei_t**)realloc(h->seis, h->num_seis * sizeof(sei_t*)); 513 | h->seis[h->num_seis - 1] = sei_new(); 514 | h->sei = h->seis[h->num_seis - 1]; 515 | structure(sei_message)(h, b); 516 | } while( more_rbsp_data(h, b) ); 517 | } 518 | 519 | if( is_writing ) 520 | { 521 | for (int i = 0; i < h->num_seis; i++) { 522 | h->sei = h->seis[i]; 523 | structure(sei_message)(h, b); 524 | } 525 | h->sei = NULL; 526 | } 527 | 528 | structure(hevc_rbsp_trailing_bits)(b); 529 | } 530 | 531 | //7.3.5 Supplemental enhancement information message syntax 532 | void structure(sei_message)(hevc_stream_t* h, bs_t* b) 533 | { 534 | if( is_writing ) 535 | { 536 | _write_ff_coded_number(b, h->sei->payloadType); 537 | _write_ff_coded_number(b, h->sei->payloadSize); 538 | } 539 | if( is_reading ) 540 | { 541 | h->sei->payloadType = _read_ff_coded_number(b); 542 | h->sei->payloadSize = _read_ff_coded_number(b); 543 | } 544 | structure(sei_payload)( h, b, h->sei->payloadType, h->sei->payloadSize ); 545 | } 546 | #endif 547 | 548 | //7.3.2.5 Access unit delimiter RBSP syntax 549 | void structure(hevc_access_unit_delimiter_rbsp)(hevc_stream_t* h, bs_t* b) 550 | { 551 | value( h->aud->primary_pic_type, u(3) ); 552 | structure(hevc_rbsp_trailing_bits)(b); 553 | } 554 | 555 | //7.3.2.6 End of sequence RBSP syntax 556 | void structure(hevc_end_of_seq_rbsp)() 557 | { 558 | } 559 | 560 | //7.3.2.7 End of bitstream RBSP syntax 561 | void structure(end_of_bitstream_rbsp)() 562 | { 563 | } 564 | 565 | //7.3.2.8 Filler data RBSP syntax 566 | void structure(filler_data_rbsp)(bs_t* b) 567 | { 568 | while( bs_next_bits(b, 8) == 0xFF ) 569 | { 570 | value( ff_byte, f(8, 0xFF) ); 571 | } 572 | structure(hevc_rbsp_trailing_bits)(b); 573 | } 574 | 575 | //7.3.2.9 Slice segment layer RBSP syntax 576 | void structure(hevc_slice_layer_rbsp)(hevc_stream_t* h, bs_t* b) 577 | { 578 | structure(hevc_slice_header)(h, b); 579 | hevc_slice_data_rbsp_t* slice_data = h->slice_data; 580 | 581 | if ( slice_data != NULL ) 582 | { 583 | if ( slice_data->rbsp_buf != NULL ) free( slice_data->rbsp_buf ); 584 | uint8_t *sptr = b->p + (!!b->bits_left); // CABAC-specific: skip alignment bits, if there are any 585 | slice_data->rbsp_size = b->end - sptr; 586 | 587 | slice_data->rbsp_buf = (uint8_t*)malloc(slice_data->rbsp_size); 588 | memcpy( slice_data->rbsp_buf, sptr, slice_data->rbsp_size ); 589 | } 590 | 591 | //structure(hevc_slice_data)(h, b); /* all categories of slice_data( ) syntax */ 592 | structure(hevc_rbsp_slice_trailing_bits)( b ); 593 | } 594 | 595 | //7.3.2.10 RBSP slice trailing bits syntax 596 | void structure(hevc_rbsp_slice_trailing_bits)(bs_t* b) 597 | { 598 | structure(hevc_rbsp_trailing_bits)(b); 599 | //while( more_rbsp_trailing_data(b) ) 600 | //{ 601 | // value( cabac_zero_word, f(16, 0x0000) ); 602 | //} 603 | } 604 | 605 | //7.3.2.11 RBSP trailing bits syntax 606 | void structure(hevc_rbsp_trailing_bits)(bs_t* b) 607 | { 608 | value( rbsp_stop_one_bit, f(1, 1) ); 609 | 610 | while( !bs_byte_aligned(b) ) 611 | { 612 | value( rbsp_alignment_zero_bit, f(1, 0) ); 613 | } 614 | } 615 | 616 | //7.3.2.12 Byte alignment syntax 617 | void structure(hevc_byte_alignment)(bs_t* b) 618 | { 619 | value( alignment_bit_equal_to_one, f(1, 1) ); 620 | 621 | while( !bs_byte_aligned(b) ) 622 | { 623 | value( alignment_bit_equal_to_zero, f(1, 0) ); 624 | } 625 | } 626 | 627 | //7.3.3 Profile, tier and level syntax 628 | void structure(hevc_profile_tier_level)(hevc_profile_tier_level_t* ptl, bs_t* b, int profilePresentFlag, int maxNumSubLayersMinus1) 629 | { 630 | int i, j; 631 | if( profilePresentFlag ) { 632 | value( ptl->general_profile_space, u(2) ); 633 | value( ptl->general_tier_flag, u1 ); 634 | value( ptl->general_profile_idc, u(5) ); 635 | for( i = 0; i < 32; i++ ) { 636 | value( ptl->general_profile_compatibility_flag[ i ], u1 ); 637 | } 638 | value( ptl->general_progressive_source_flag, u1 ); 639 | value( ptl->general_interlaced_source_flag, u1 ); 640 | value( ptl->general_non_packed_constraint_flag, u1 ); 641 | value( ptl->general_frame_only_constraint_flag, u1 ); 642 | if( ptl->general_profile_idc == 4 || ptl->general_profile_compatibility_flag[ 4 ] || 643 | ptl->general_profile_idc == 5 || ptl->general_profile_compatibility_flag[ 5 ] || 644 | ptl->general_profile_idc == 6 || ptl->general_profile_compatibility_flag[ 6 ] || 645 | ptl->general_profile_idc == 7 || ptl->general_profile_compatibility_flag[ 7 ] ) { 646 | 647 | value( ptl->general_max_12bit_constraint_flag, u1 ); 648 | value( ptl->general_max_10bit_constraint_flag, u1 ); 649 | value( ptl->general_max_8bit_constraint_flag, u1 ); 650 | value( ptl->general_max_422chroma_constraint_flag, u1 ); 651 | value( ptl->general_max_420chroma_constraint_flag, u1 ); 652 | value( ptl->general_max_monochrome_constraint_flag, u1 ); 653 | value( ptl->general_intra_constraint_flag, u1 ); 654 | value( ptl->general_one_picture_only_constraint_flag, u1 ); 655 | value( ptl->general_lower_bit_rate_constraint_flag, u1 ); 656 | value( general_reserved_zero_34bits, f(34, 0) ); 657 | } else { 658 | value( general_reserved_zero_43bits, f(43, 0) ); 659 | } 660 | if( ( ptl->general_profile_idc >= 1 && ptl->general_profile_idc <= 5 ) || 661 | ptl->general_profile_compatibility_flag[ 1 ] || 662 | ptl->general_profile_compatibility_flag[ 2 ] || 663 | ptl->general_profile_compatibility_flag[ 3 ] || 664 | ptl->general_profile_compatibility_flag[ 4 ] || 665 | ptl->general_profile_compatibility_flag[ 5 ] ) { 666 | 667 | value( ptl->general_inbld_flag, u1 ); 668 | } else { 669 | value( general_reserved_zero_bit, f(1, 0) ); 670 | } 671 | value( ptl->general_level_idc, u8 ); 672 | for( i = 0; i < maxNumSubLayersMinus1; i++ ) { 673 | value( ptl->sub_layer_profile_present_flag[ i ], u1 ); 674 | value( ptl->sub_layer_level_present_flag[ i ], u1 ); 675 | } 676 | if( maxNumSubLayersMinus1 > 0 ) { 677 | for( i = maxNumSubLayersMinus1; i < 8; i++ ) { 678 | value( reserved_zero_xxbits, f(2, 0) ); 679 | } 680 | } 681 | for( i = 0; i < maxNumSubLayersMinus1; i++ ) { 682 | if( ptl->sub_layer_profile_present_flag[ i ] ) { 683 | value( ptl->sub_layer_profile_space[ i ], u(2) ); 684 | value( ptl->sub_layer_tier_flag[ i ], u1 ); 685 | value( ptl->sub_layer_profile_idc[ i ], u(5) ); 686 | for( j = 0; j < 32; j++ ) { 687 | value( ptl->sub_layer_profile_compatibility_flag[ i ][ j ], u(1)); 688 | } 689 | value( ptl->sub_layer_progressive_source_flag[ i ], u1 ); 690 | value( ptl->sub_layer_interlaced_source_flag[ i ], u1 ); 691 | value( ptl->sub_layer_non_packed_constraint_flag[ i ], u1 ); 692 | value( ptl->sub_layer_frame_only_constraint_flag[ i ], u1 ); 693 | if( ptl->sub_layer_profile_idc[ i ] == 4 || 694 | ptl->sub_layer_profile_compatibility_flag[ i ][ 4 ] || 695 | ptl->sub_layer_profile_idc[ i ] == 5 || 696 | ptl->sub_layer_profile_compatibility_flag[ i ][ 5 ] || 697 | ptl->sub_layer_profile_idc[ i ] == 6 || 698 | ptl->sub_layer_profile_compatibility_flag[ i ][ 6 ] || 699 | ptl->sub_layer_profile_idc[ i ] == 7 || 700 | ptl->sub_layer_profile_compatibility_flag[ i ][ 7 ] ) { 701 | value( ptl->sub_layer_max_12bit_constraint_flag[ i ], u1 ); 702 | value( ptl->sub_layer_max_10bit_constraint_flag[ i ], u1 ); 703 | value( ptl->sub_layer_max_8bit_constraint_flag[ i ], u1 ); 704 | value( ptl->sub_layer_max_422chroma_constraint_flag[ i ], u1 ); 705 | value( ptl->sub_layer_max_420chroma_constraint_flag[ i ], u1 ); 706 | value( ptl->sub_layer_max_monochrome_constraint_flag[ i ], u1 ); 707 | value( ptl->sub_layer_intra_constraint_flag[ i ], u1 ); 708 | value( ptl->sub_layer_one_picture_only_constraint_flag[ i ], u1 ); 709 | value( ptl->sub_layer_lower_bit_rate_constraint_flag[ i ], u1 ); 710 | value( sub_layer_reserved_zero_34bits, f(34, 0) ); 711 | } else { 712 | value( sub_layer_reserved_zero_43bits, f(43, 0) ); 713 | } 714 | 715 | if( ( ptl->sub_layer_profile_idc[ i ] >= 1 && ptl->sub_layer_profile_idc[ i ] <= 5 ) || 716 | ptl->sub_layer_profile_compatibility_flag[ 1 ] || 717 | ptl->sub_layer_profile_compatibility_flag[ 2 ] || 718 | ptl->sub_layer_profile_compatibility_flag[ 3 ] || 719 | ptl->sub_layer_profile_compatibility_flag[ 4 ] || 720 | ptl->sub_layer_profile_compatibility_flag[ 5 ] ) { 721 | value( ptl->sub_layer_inbld_flag[ i ], u1 ); 722 | } else { 723 | value( sub_layer_reserved_zero_bit, f(1, 0) ); 724 | } 725 | } 726 | if( ptl->sub_layer_level_present_flag[ i ] ) { 727 | value( ptl->sub_layer_level_idc[ i ], u8 ); 728 | } 729 | } 730 | } 731 | } 732 | 733 | //7.3.4 Scaling list data syntax 734 | void structure(hevc_scaling_list_data)( hevc_scaling_list_data_t *sld, bs_t* b ) 735 | { 736 | int nextCoef, coefNum; 737 | for( int sizeId = 0; sizeId < 4; sizeId++ ) 738 | for( int matrixId = 0; matrixId < 6; matrixId += ( sizeId == 3 ) ? 3 : 1 ) { 739 | value( sld->scaling_list_pred_mode_flag[ sizeId ][ matrixId ], u1 ); 740 | if( !sld->scaling_list_pred_mode_flag[ sizeId ][ matrixId ] ) { 741 | value( sld->scaling_list_pred_matrix_id_delta[ sizeId ][ matrixId ], ue ); 742 | } else { 743 | nextCoef = 8; 744 | coefNum=MIN(64, (1 << (4+(sizeId << 1)))); 745 | if( sizeId > 1 ) { 746 | value( sld->scaling_list_dc_coef_minus8[ sizeId - 2 ][ matrixId ], se ); 747 | } 748 | 749 | for( int i = 0; i < coefNum; i++) { 750 | value( sld->scaling_list_delta_coef[ sizeId ][ matrixId ], se ); 751 | } 752 | } 753 | 754 | } 755 | } 756 | 757 | //7.3.6 Slice header syntax 758 | void structure(hevc_slice_header)(hevc_stream_t* h, bs_t* b) 759 | { 760 | int i; 761 | hevc_slice_header_t* sh = h->sh; 762 | if( is_reading ) 763 | { 764 | init_slice_hevc(h); 765 | } 766 | 767 | hevc_nal_t* nal = h->nal; 768 | 769 | value( sh->first_slice_segment_in_pic_flag, u1 ); 770 | if( nal->nal_unit_type >= HEVC_NAL_UNIT_TYPE_BLA_W_LP && 771 | nal->nal_unit_type <= HEVC_NAL_UNIT_TYPE_RSV_IRAP_VCL23) { 772 | value( sh->no_output_of_prior_pics_flag, u1 ); 773 | } 774 | value( sh->pic_parameter_set_id, ue ); 775 | 776 | hevc_pps_t* pps = &h->pps[sh->pic_parameter_set_id]; 777 | hevc_sps_t* sps = &h->sps[pps->seq_parameter_set_id]; 778 | 779 | //set default value 780 | sh->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1; 781 | sh->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1; 782 | 783 | if( !sh->first_slice_segment_in_pic_flag ) { 784 | if( pps->dependent_slice_segments_enabled_flag ) { 785 | value( sh->dependent_slice_segment_flag, u1 ); 786 | } 787 | value( sh->slice_segment_address, u( getSliceSegmentAddressBitLength( sps ) ) ); 788 | } 789 | 790 | if( !sh->dependent_slice_segment_flag ) { 791 | for( i = 0; i < pps->num_extra_slice_header_bits; i++ ) { 792 | value( slice_reserved_flag, f(1, 1) ); 793 | } 794 | value( sh->slice_type, ue ); 795 | if( pps->output_flag_present_flag ) { 796 | value( sh->pic_output_flag, u1 ); 797 | } 798 | if( sps->separate_colour_plane_flag == 1 ) { 799 | value( sh->colour_plane_id, u(2) ); 800 | } 801 | if( nal->nal_unit_type != HEVC_NAL_UNIT_TYPE_IDR_W_RADL && 802 | nal->nal_unit_type != HEVC_NAL_UNIT_TYPE_IDR_N_LP) { 803 | value( sh->slice_pic_order_cnt_lsb, u( sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ) ); 804 | value( sh->short_term_ref_pic_set_sps_flag, u1 ); 805 | if( !sh->short_term_ref_pic_set_sps_flag ) { 806 | structure(hevc_st_ref_pic_set)( &sh->st_ref_pic_set, b, sps->num_short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets ); 807 | } else if( sps->num_short_term_ref_pic_sets > 1 ) { 808 | value( sh->short_term_ref_pic_set_idx, u( ceil( log2( sps->num_short_term_ref_pic_sets ) ) ) ); 809 | } 810 | if( sps->long_term_ref_pics_present_flag ) { 811 | if( sps->num_long_term_ref_pics_sps > 0 ) { 812 | value( sh->num_long_term_sps, ue ); 813 | } 814 | value( sh->num_long_term_pics, ue ); 815 | for( i = 0; i < sh->num_long_term_sps + sh->num_long_term_pics; i++ ) { 816 | if( i < sh->num_long_term_sps ) { 817 | if( sps->num_long_term_ref_pics_sps > 1 ) { 818 | value( sh->lt_idx_sps[ i ], u( ceil( log2( sps->num_long_term_ref_pics_sps ) ) ) ); 819 | } 820 | } else { 821 | value( sh->poc_lsb_lt[ i ], u( sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ) ); 822 | value( sh->used_by_curr_pic_lt_flag[ i ], u1 ); 823 | } 824 | value( sh->delta_poc_msb_present_flag[ i ], u1 ); 825 | if( sh->delta_poc_msb_present_flag[ i ]) { 826 | value( sh->delta_poc_msb_cycle_lt[ i ], ue ); 827 | } 828 | } 829 | } 830 | if( sps->sps_temporal_mvp_enabled_flag ) { 831 | value( sh->slice_temporal_mvp_enabled_flag, u1 ); 832 | } 833 | } 834 | if( sps->sample_adaptive_offset_enabled_flag ) { 835 | value( sh->slice_sao_luma_flag, u1 ); 836 | int ChromaArrayType = 0; 837 | if( sps->separate_colour_plane_flag == 0) { 838 | ChromaArrayType = sps->chroma_format_idc; 839 | } 840 | if( ChromaArrayType != 0 ) { 841 | value( sh->slice_sao_chroma_flag, u1 ); 842 | } 843 | } 844 | if( sh->slice_type == HEVC_SLICE_TYPE_P || sh->slice_type == HEVC_SLICE_TYPE_B ){ 845 | value( sh->num_ref_idx_active_override_flag, u1 ); 846 | if( sh->num_ref_idx_active_override_flag ) { 847 | value( sh->num_ref_idx_l0_active_minus1, ue ); 848 | if( sh->slice_type == HEVC_SLICE_TYPE_B ) { 849 | value( sh->num_ref_idx_l1_active_minus1, ue ); 850 | } 851 | } 852 | if( pps->lists_modification_present_flag && getNumPicTotalCurr( sps, sh ) > 1 ) { 853 | structure(hevc_ref_pic_lists_modification)( h, b ); 854 | } 855 | 856 | if( sh->slice_type == HEVC_SLICE_TYPE_B ) { 857 | value( sh->mvd_l1_zero_flag, u1 ); 858 | } 859 | if( pps->cabac_init_present_flag ) { 860 | value( sh->cabac_init_flag, u1 ); 861 | } 862 | if( sh->slice_temporal_mvp_enabled_flag ) { 863 | if( sh->slice_type == HEVC_SLICE_TYPE_B ) { 864 | value( sh->collocated_from_l0_flag, u1 ); 865 | } 866 | if( ( sh->collocated_from_l0_flag && sh->num_ref_idx_l0_active_minus1 > 0 ) || 867 | ( !sh->collocated_from_l0_flag && sh->num_ref_idx_l1_active_minus1 > 0 ) ) { 868 | value( sh->collocated_ref_idx, ue ); 869 | } 870 | } 871 | if( ( pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_TYPE_P ) || 872 | ( pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_TYPE_B ) ) { 873 | structure(hevc_pred_weight_table)( h, b ); 874 | } 875 | value( sh->five_minus_max_num_merge_cand, ue ); 876 | } 877 | value( sh->slice_qp_delta, se ); 878 | if( pps->pps_slice_chroma_qp_offsets_present_flag ) { 879 | value( sh->slice_cb_qp_offset, se ); 880 | value( sh->slice_cr_qp_offset, se ); 881 | } 882 | if( pps->pps_range_ext.chroma_qp_offset_list_enabled_flag ) { 883 | value( sh->cu_chroma_qp_offset_enabled_flag, u1 ); 884 | } 885 | if( pps->deblocking_filter_override_enabled_flag ) { 886 | value( sh->deblocking_filter_override_flag, u1 ); 887 | } 888 | if( sh->deblocking_filter_override_flag ) { 889 | value( sh->slice_deblocking_filter_disabled_flag, u1 ); 890 | if( !sh->slice_deblocking_filter_disabled_flag ) { 891 | value( sh->slice_beta_offset_div2, se ); 892 | value( sh->slice_tc_offset_div2, se ); 893 | } 894 | } 895 | if( pps->pps_loop_filter_across_slices_enabled_flag && 896 | ( sh->slice_sao_luma_flag || sh->slice_sao_chroma_flag || !sh->slice_deblocking_filter_disabled_flag ) ) { 897 | value( sh->slice_loop_filter_across_slices_enabled_flag, u1 ); 898 | } 899 | } 900 | if( pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag ) { 901 | value( sh->num_entry_point_offsets, ue ); 902 | if( sh->num_entry_point_offsets > 0 ) { 903 | value( sh->offset_len_minus1, ue ); 904 | for( i = 0; i < sh->num_entry_point_offsets; i++ ) { 905 | value( sh->entry_point_offset_minus1[ i ], u( sh->offset_len_minus1 + 1 ) ); 906 | } 907 | } 908 | } 909 | if( pps->slice_segment_header_extension_present_flag ) { 910 | value( sh->slice_segment_header_extension_length, ue ); 911 | //TODO: support header extension, 912 | for( i = 0; i < sh->slice_segment_header_extension_length; i++) { 913 | value( slice_segment_header_extension_data_byte, f(8, 0) ); 914 | } 915 | } 916 | structure(hevc_byte_alignment)( b ); 917 | } 918 | 919 | //7.3.6.2 Reference picture list reordering syntax 920 | void structure(hevc_ref_pic_lists_modification)(hevc_stream_t* h, bs_t* b) 921 | { 922 | int i; 923 | hevc_slice_header_t* sh = h->sh; 924 | hevc_pps_t* pps = &h->pps[sh->pic_parameter_set_id]; 925 | hevc_sps_t* sps = &h->sps[pps->seq_parameter_set_id]; 926 | 927 | value( sh->rpld.ref_pic_list_modification_flag_l0, u1 ); 928 | if( sh->rpld.ref_pic_list_modification_flag_l0 ) { 929 | for( i = 0; i <= sh->num_ref_idx_l0_active_minus1; i++ ) { 930 | value( sh->rpld.list_entry_l0[ i ], u( ceil( log2( getNumPicTotalCurr( sps, sh ) ) ) ) ); 931 | } 932 | } 933 | 934 | if( sh->slice_type == HEVC_SLICE_TYPE_B ) { 935 | value( sh->rpld.ref_pic_list_modification_flag_l1, 1 ); 936 | if( sh->rpld.ref_pic_list_modification_flag_l1 ) { 937 | for( i = 0; i <= sh->num_ref_idx_l1_active_minus1; i++ ) { 938 | value( sh->rpld.list_entry_l1[ i ], u( ceil( log2( getNumPicTotalCurr( sps, sh ) ) ) ) ); 939 | } 940 | } 941 | } 942 | } 943 | 944 | //7.3.6.3 Prediction weight table syntax 945 | void structure(hevc_pred_weight_table)(hevc_stream_t* h, bs_t* b) 946 | { 947 | hevc_slice_header_t* sh = h->sh; 948 | hevc_pps_t* pps = &h->pps[sh->pic_parameter_set_id]; 949 | hevc_sps_t* sps = &h->sps[pps->seq_parameter_set_id]; 950 | hevc_pred_weight_table_t *pwt = &sh->pwt; 951 | 952 | int i, j; 953 | 954 | value( pwt->luma_log2_weight_denom, ue ); 955 | 956 | int ChromaArrayType = 0; 957 | if( sps->separate_colour_plane_flag == 0) { 958 | ChromaArrayType = sps->chroma_format_idc; 959 | } 960 | 961 | if( ChromaArrayType != 0 ) { 962 | value( pwt->delta_chroma_log2_weight_denom, se ); 963 | } 964 | 965 | for( i = 0; i <= sh->num_ref_idx_l0_active_minus1; i++ ) { 966 | value( pwt->luma_weight_l0_flag[i], u1 ); 967 | } 968 | if( ChromaArrayType != 0 ) 969 | for( i = 0; i <= sh->num_ref_idx_l0_active_minus1; i++ ) { 970 | value( pwt->chroma_weight_l0_flag[i], u1 ); 971 | } 972 | for( i = 0; i <= sh->num_ref_idx_l0_active_minus1; i++ ) { 973 | if( pwt->luma_weight_l0_flag[i] ) { 974 | value( pwt->delta_luma_weight_l0[i], se ); 975 | value( pwt->luma_offset_l0[i], se ); 976 | } 977 | if( pwt->chroma_weight_l0_flag[i] ) { 978 | for( j =0; j < 2; j++ ) { 979 | value( pwt->delta_chroma_weight_l0[i][j], se ); 980 | value( pwt->delta_chroma_offset_l0[i][j], se ); 981 | } 982 | } 983 | } 984 | if( sh->slice_type == HEVC_SLICE_TYPE_B ) { 985 | for( i = 0; i <= sh->num_ref_idx_l1_active_minus1; i++ ) { 986 | value( pwt->luma_weight_l1_flag[i], u1 ); 987 | } 988 | if( ChromaArrayType != 0 ) 989 | for( i = 0; i <= sh->num_ref_idx_l1_active_minus1; i++ ) { 990 | value( pwt->chroma_weight_l1_flag[i], u1 ); 991 | } 992 | for( i = 0; i <= sh->num_ref_idx_l1_active_minus1; i++ ) { 993 | if( pwt->luma_weight_l1_flag[i] ) { 994 | value( pwt->delta_luma_weight_l1[i], se ); 995 | value( pwt->luma_offset_l1[i], se ); 996 | } 997 | if( pwt->chroma_weight_l1_flag[i] ) { 998 | for( j =0; j < 2; j++ ) { 999 | value( pwt->delta_chroma_weight_l1[i][j], se ); 1000 | value( pwt->delta_chroma_offset_l1[i][j], se ); 1001 | } 1002 | } 1003 | } 1004 | } 1005 | } 1006 | 1007 | //7.3.7 Short-term reference picture set syntax 1008 | void structure(hevc_st_ref_pic_set)( hevc_st_ref_pic_set_t *st_ref_pic_set, bs_t* b, int stRpsIdx, int num_short_term_ref_pic_sets ) 1009 | { 1010 | int i, j; 1011 | 1012 | if( stRpsIdx != 0 ) { 1013 | value( st_ref_pic_set->inter_ref_pic_set_prediction_flag, u1); 1014 | } 1015 | if( st_ref_pic_set->inter_ref_pic_set_prediction_flag ) { 1016 | if( stRpsIdx == num_short_term_ref_pic_sets ) { 1017 | value( st_ref_pic_set->delta_idx_minus1, ue ); 1018 | } 1019 | value( st_ref_pic_set->delta_rps_sign, u1 ); 1020 | value( st_ref_pic_set->abs_delta_rps_minus1, ue ); 1021 | 1022 | int RefRpsIdx = stRpsIdx - ( st_ref_pic_set->delta_idx_minus1 + 1 ); 1023 | 1024 | for( j = 0; j <= NumDeltaPocs[ RefRpsIdx ]; j++ ) { 1025 | value( st_ref_pic_set->used_by_curr_pic_flag[ j ], u1 ); 1026 | if( !st_ref_pic_set->used_by_curr_pic_flag[ j ] ) { 1027 | value( st_ref_pic_set->use_delta_flag[ j ], u1 ); 1028 | } 1029 | } 1030 | } else { 1031 | value( st_ref_pic_set->num_negative_pics, ue ); 1032 | value( st_ref_pic_set->num_positive_pics, ue ); 1033 | for( i = 0; i < st_ref_pic_set->num_negative_pics; i++ ) { 1034 | value( st_ref_pic_set->delta_poc_s0_minus1[ i ], ue ); 1035 | value( st_ref_pic_set->used_by_curr_pic_s0_flag[ i ], u1 ); 1036 | 1037 | //update derived field 1038 | UsedByCurrPicS0[ stRpsIdx ][ i ] = st_ref_pic_set->used_by_curr_pic_s0_flag[ i ]; 1039 | 1040 | if( i == 0 ) { 1041 | DeltaPocS0[ stRpsIdx ][ i ] = -1 * ( st_ref_pic_set->delta_poc_s0_minus1[ i ] + 1 ); 1042 | } else { 1043 | DeltaPocS0[ stRpsIdx ][ i ] = DeltaPocS0[ stRpsIdx ][ i - 1 ] - ( st_ref_pic_set->delta_poc_s0_minus1[ i ] + 1 ); 1044 | } 1045 | } 1046 | for( i = 0; i < st_ref_pic_set->num_positive_pics; i++ ) { 1047 | value( st_ref_pic_set->delta_poc_s1_minus1[ i ], ue ); 1048 | value( st_ref_pic_set->used_by_curr_pic_s1_flag[ i ], u1 ); 1049 | 1050 | //update derived field 1051 | UsedByCurrPicS1[ stRpsIdx ][ i ] = st_ref_pic_set->used_by_curr_pic_s1_flag[ i ]; 1052 | 1053 | if( i == 0 ) { 1054 | DeltaPocS1[ stRpsIdx ][ i ] = st_ref_pic_set->delta_poc_s1_minus1[ i ] + 1; 1055 | } else { 1056 | DeltaPocS1[ stRpsIdx ][ i ] = DeltaPocS1[ stRpsIdx ][ i - 1 ] + ( st_ref_pic_set->delta_poc_s1_minus1[ i ] + 1 ); 1057 | } 1058 | } 1059 | } 1060 | updateNumDeltaPocs( st_ref_pic_set, stRpsIdx); 1061 | } 1062 | 1063 | //Appendix E.2.1 VUI parameters syntax 1064 | void structure(hevc_vui_parameters)(hevc_sps_t* sps, bs_t* b) 1065 | { 1066 | hevc_vui_t* vui = &sps->vui; 1067 | value( vui->aspect_ratio_info_present_flag, u1 ); 1068 | if( vui->aspect_ratio_info_present_flag ) 1069 | { 1070 | value( vui->aspect_ratio_idc, u8 ); 1071 | if( vui->aspect_ratio_idc == SAR_Extended ) 1072 | { 1073 | value( vui->sar_width, u(16) ); 1074 | value( vui->sar_height, u(16) ); 1075 | } 1076 | } 1077 | value( vui->overscan_info_present_flag, u1 ); 1078 | if( vui->overscan_info_present_flag ) { 1079 | value( vui->overscan_appropriate_flag, u1 ); 1080 | } 1081 | value( vui->video_signal_type_present_flag, u1 ); 1082 | if( vui->video_signal_type_present_flag ) { 1083 | value( vui->video_format, u(3) ); 1084 | value( vui->video_full_range_flag, u1 ); 1085 | value( vui->colour_description_present_flag, u1 ); 1086 | if( vui->colour_description_present_flag ) { 1087 | value( vui->colour_primaries, u8 ); 1088 | value( vui->transfer_characteristics, u8 ); 1089 | value( vui->matrix_coefficients, u8 ); 1090 | } 1091 | } 1092 | value( vui->chroma_loc_info_present_flag, u1 ); 1093 | if( vui->chroma_loc_info_present_flag ) { 1094 | value( vui->chroma_sample_loc_type_top_field, ue ); 1095 | value( vui->chroma_sample_loc_type_bottom_field, ue ); 1096 | } 1097 | 1098 | value( vui->neutral_chroma_indication_flag, u1 ); 1099 | value( vui->field_seq_flag, u1 ); 1100 | value( vui->frame_field_info_present_flag, u1 ); 1101 | value( vui->default_display_window_flag, u1 ); 1102 | if( vui->default_display_window_flag ) { 1103 | value( vui->def_disp_win_left_offset, ue ); 1104 | value( vui->def_disp_win_right_offset, ue ); 1105 | value( vui->def_disp_win_top_offset, ue ); 1106 | value( vui->def_disp_win_bottom_offset, ue ); 1107 | } 1108 | value( vui->vui_timing_info_present_flag, u1 ); 1109 | if( vui->vui_timing_info_present_flag ) { 1110 | value( vui->vui_num_units_in_tick, u(32) ); 1111 | value( vui->vui_time_scale, u(32) ); 1112 | value( vui->vui_poc_proportional_to_timing_flag, u1 ); 1113 | if( vui->vui_poc_proportional_to_timing_flag ) { 1114 | value( vui->vui_num_ticks_poc_diff_one_minus1, ue ); 1115 | } 1116 | value( vui->vui_hrd_parameters_present_flag, u1 ); 1117 | if( vui->vui_hrd_parameters_present_flag ) { 1118 | structure(hevc_hrd_parameters)( &vui->hrd, b, 1, sps->sps_max_sub_layers_minus1 ); 1119 | } 1120 | } 1121 | value( vui->bitstream_restriction_flag, u1 ); 1122 | if( vui->bitstream_restriction_flag ) 1123 | { 1124 | value( vui->tiles_fixed_structure_flag, u1 ); 1125 | value( vui->motion_vectors_over_pic_boundaries_flag, u1 ); 1126 | value( vui->restricted_ref_pic_lists_flag, u1 ); 1127 | value( vui->min_spatial_segmentation_idc, ue ); 1128 | value( vui->max_bytes_per_pic_denom, ue ); 1129 | value( vui->max_bits_per_min_cu_denom, ue ); 1130 | value( vui->log2_max_mv_length_horizontal, ue ); 1131 | value( vui->log2_max_mv_length_vertical, ue ); 1132 | } 1133 | } 1134 | 1135 | //Appendix E.2.2 HRD parameters syntax 1136 | void structure(hevc_hrd_parameters)(hevc_hrd_t* hrd, bs_t* b, int commonInfPresentFlag, int maxNumSubLayersMinus1) 1137 | { 1138 | if( commonInfPresentFlag ) { 1139 | value( hrd->nal_hrd_parameters_present_flag, u1 ); 1140 | value( hrd->vcl_hrd_parameters_present_flag, u1 ); 1141 | if( hrd->nal_hrd_parameters_present_flag || hrd->vcl_hrd_parameters_present_flag ){ 1142 | value( hrd->sub_pic_hrd_params_present_flag, u1 ); 1143 | if( hrd->sub_pic_hrd_params_present_flag ) { 1144 | value( hrd->tick_divisor_minus2, u8 ); 1145 | value( hrd->du_cpb_removal_delay_increment_length_minus1, u(5) ); 1146 | value( hrd->sub_pic_cpb_params_in_pic_timing_sei_flag, u1 ); 1147 | value( hrd->dpb_output_delay_du_length_minus1, u(5) ); 1148 | } 1149 | value( hrd->bit_rate_scale, u(4) ); 1150 | value( hrd->cpb_size_scale, u(4) ); 1151 | if( hrd->sub_pic_hrd_params_present_flag ) { 1152 | value( hrd->cpb_size_du_scale, u(4) ); 1153 | } 1154 | value( hrd->initial_cpb_removal_delay_length_minus1, u(5) ); 1155 | value( hrd->au_cpb_removal_delay_length_minus1, u(5) ); 1156 | value( hrd->dpb_output_delay_length_minus1, u(5) ); 1157 | } 1158 | } 1159 | 1160 | for( int i = 0; i <= maxNumSubLayersMinus1; i++ ) { 1161 | value( hrd->fixed_pic_rate_general_flag[ i ], u1 ); 1162 | if( !hrd->fixed_pic_rate_general_flag[ i ] ) { 1163 | value( hrd->fixed_pic_rate_within_cvs_flag[ i ], u1 ); 1164 | } 1165 | if( hrd->fixed_pic_rate_within_cvs_flag[ i ] ) { 1166 | value( hrd->elemental_duration_in_tc_minus1[ i ], ue ); 1167 | } else { 1168 | value( hrd->low_delay_hrd_flag[ i ], u1 ); 1169 | } 1170 | if( hrd->low_delay_hrd_flag[ i ] ) { 1171 | value( hrd->cpb_cnt_minus1[ i ], ue ); 1172 | } 1173 | if( hrd->nal_hrd_parameters_present_flag ) { 1174 | structure(hevc_sub_layer_hrd_parameters)(&hrd->sub_layer_hrd_nal[i], b, hrd->cpb_cnt_minus1[ i ] + 1, hrd->sub_pic_hrd_params_present_flag); 1175 | } 1176 | if( hrd->vcl_hrd_parameters_present_flag ) { 1177 | structure(hevc_sub_layer_hrd_parameters)(&hrd->sub_layer_hrd_vcl[i], b, hrd->cpb_cnt_minus1[ i ] + 1, hrd->sub_pic_hrd_params_present_flag); 1178 | } 1179 | } 1180 | } 1181 | 1182 | //Appendix E.2.3 Sub-layer HRD parameters syntax 1183 | void structure(hevc_sub_layer_hrd_parameters)(hevc_sub_layer_hrd_t* sub_layer_hrd, bs_t* b, int CpbCnt, int sub_pic_hrd_params_present_flag) 1184 | { 1185 | for( int i = 0; i <= CpbCnt; i++ ) { 1186 | value( sub_layer_hrd->bit_rate_value_minus1[i], ue ); 1187 | value( sub_layer_hrd->cpb_size_value_minus1[i], ue ); 1188 | if( sub_pic_hrd_params_present_flag ) { 1189 | value( sub_layer_hrd->cpb_size_du_value_minus1[i], ue ); 1190 | value( sub_layer_hrd->bit_rate_du_value_minus1[i], ue ); 1191 | } 1192 | value( sub_layer_hrd->cbr_flag[i], u1 ); 1193 | } 1194 | } 1195 | --------------------------------------------------------------------------------