├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── bs.h ├── h264_avcc.cpp ├── h264_avcc.h ├── h264_sei.cpp ├── h264_sei.h ├── h264_stream.cpp ├── h264_stream.h ├── h265_avcc.cpp ├── h265_avcc.h ├── h265_sei.cpp ├── h265_sei.h ├── h265_stream.cpp ├── h265_stream.h ├── h26x_sps_parse.cpp ├── h26x_sps_parse.h └── test_h26x_analyzer.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### CMake template 3 | CMakeLists.txt.user 4 | CMakeCache.txt 5 | CMakeFiles 6 | CMakeScripts 7 | Testing 8 | Makefile 9 | cmake_install.cmake 10 | install_manifest.txt 11 | compile_commands.json 12 | CTestTestfile.cmake 13 | _deps 14 | 15 | ### SublimeText template 16 | # Cache files for Sublime Text 17 | *.tmlanguage.cache 18 | *.tmPreferences.cache 19 | *.stTheme.cache 20 | 21 | # Workspace files are user-specific 22 | *.sublime-workspace 23 | 24 | # Project files should be checked into the repository, unless a significant 25 | # proportion of contributors will probably not be using Sublime Text 26 | # *.sublime-project 27 | 28 | # SFTP configuration file 29 | sftp-config.json 30 | sftp-config-alt*.json 31 | 32 | # Package control specific files 33 | Package Control.last-run 34 | Package Control.ca-list 35 | Package Control.ca-bundle 36 | Package Control.system-ca-bundle 37 | Package Control.cache/ 38 | Package Control.ca-certs/ 39 | Package Control.merged-ca-bundle 40 | Package Control.user-ca-bundle 41 | oscrypto-ca-bundle.crt 42 | bh_unicode_properties.cache 43 | 44 | # Sublime-github package stores a github token in this file 45 | # https://packagecontrol.io/packages/sublime-github 46 | GitHub.sublime-settings 47 | 48 | ### Windows template 49 | # Windows thumbnail cache files 50 | Thumbs.db 51 | Thumbs.db:encryptable 52 | ehthumbs.db 53 | ehthumbs_vista.db 54 | 55 | # Dump file 56 | *.stackdump 57 | 58 | # Folder config file 59 | [Dd]esktop.ini 60 | 61 | # Recycle Bin used on file shares 62 | $RECYCLE.BIN/ 63 | 64 | # Windows Installer files 65 | *.cab 66 | *.msi 67 | *.msix 68 | *.msm 69 | *.msp 70 | 71 | # Windows shortcuts 72 | *.lnk 73 | 74 | ### JetBrains template 75 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 76 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 77 | 78 | # User-specific stuff 79 | .idea/**/workspace.xml 80 | .idea/**/tasks.xml 81 | .idea/**/usage.statistics.xml 82 | .idea/**/dictionaries 83 | .idea/**/shelf 84 | 85 | # Generated files 86 | .idea/**/contentModel.xml 87 | 88 | # Sensitive or high-churn files 89 | .idea/**/dataSources/ 90 | .idea/**/dataSources.ids 91 | .idea/**/dataSources.local.xml 92 | .idea/**/sqlDataSources.xml 93 | .idea/**/dynamic.xml 94 | .idea/**/uiDesigner.xml 95 | .idea/**/dbnavigator.xml 96 | 97 | # Gradle 98 | .idea/**/gradle.xml 99 | .idea/**/libraries 100 | .idea/ 101 | # Gradle and Maven with auto-import 102 | # When using Gradle or Maven with auto-import, you should exclude module files, 103 | # since they will be recreated, and may cause churn. Uncomment if using 104 | # auto-import. 105 | # .idea/artifacts 106 | # .idea/compiler.xml 107 | # .idea/jarRepositories.xml 108 | # .idea/modules.xml 109 | # .idea/*.iml 110 | # .idea/modules 111 | # *.iml 112 | # *.ipr 113 | 114 | # CMake 115 | cmake-build-*/ 116 | 117 | # Mongo Explorer plugin 118 | .idea/**/mongoSettings.xml 119 | 120 | # File-based project format 121 | *.iws 122 | 123 | # IntelliJ 124 | out/ 125 | 126 | # mpeltonen/sbt-idea plugin 127 | .idea_modules/ 128 | 129 | # JIRA plugin 130 | atlassian-ide-plugin.xml 131 | 132 | # Cursive Clojure plugin 133 | .idea/replstate.xml 134 | 135 | # Crashlytics plugin (for Android Studio and IntelliJ) 136 | com_crashlytics_export_strings.xml 137 | crashlytics.properties 138 | crashlytics-build.properties 139 | fabric.properties 140 | 141 | # Editor-based Rest Client 142 | .idea/httpRequests 143 | 144 | # Android studio 3.1+ serialized cache file 145 | .idea/caches/build_file_checksums.ser 146 | 147 | ### Linux template 148 | *~ 149 | 150 | # temporary files which can be created if a process still has a handle open of a deleted file 151 | .fuse_hidden* 152 | 153 | # KDE directory preferences 154 | .directory 155 | 156 | # Linux trash folder which might appear on any partition or disk 157 | .Trash-* 158 | 159 | # .nfs files are created when an open file is removed but is still being accessed 160 | .nfs* 161 | 162 | ### TortoiseGit template 163 | # Project-level settings 164 | /.tgitconfig 165 | 166 | ### VisualStudioCode template 167 | .vscode/* 168 | !.vscode/settings.json 169 | !.vscode/tasks.json 170 | !.vscode/launch.json 171 | !.vscode/extensions.json 172 | *.code-workspace 173 | 174 | # Local History for Visual Studio Code 175 | .history/ 176 | 177 | ### Xcode template 178 | # Xcode 179 | # 180 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 181 | 182 | ## User settings 183 | xcuserdata/ 184 | 185 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 186 | *.xcscmblueprint 187 | *.xccheckout 188 | 189 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 190 | build/ 191 | DerivedData/ 192 | *.moved-aside 193 | *.pbxuser 194 | !default.pbxuser 195 | *.mode1v3 196 | !default.mode1v3 197 | *.mode2v3 198 | !default.mode2v3 199 | *.perspectivev3 200 | !default.perspectivev3 201 | 202 | ## Gcc Patch 203 | /*.gcno 204 | 205 | ### C++ template 206 | # Prerequisites 207 | *.d 208 | 209 | # Compiled Object files 210 | *.slo 211 | *.lo 212 | *.o 213 | *.obj 214 | 215 | # Precompiled Headers 216 | *.gch 217 | *.pch 218 | 219 | # Compiled Dynamic libraries 220 | *.so 221 | *.dylib 222 | *.dll 223 | 224 | # Fortran module files 225 | *.mod 226 | *.smod 227 | 228 | # Compiled Static libraries 229 | *.lai 230 | *.la 231 | *.a 232 | *.lib 233 | 234 | # Executables 235 | *.exe 236 | *.out 237 | *.app 238 | 239 | ### C template 240 | # Prerequisites 241 | *.d 242 | 243 | # Object files 244 | *.o 245 | *.ko 246 | *.obj 247 | *.elf 248 | 249 | # Linker output 250 | *.ilk 251 | *.map 252 | *.exp 253 | 254 | # Precompiled Headers 255 | *.gch 256 | *.pch 257 | 258 | # Libraries 259 | *.lib 260 | *.a 261 | *.la 262 | *.lo 263 | 264 | # Shared objects (inc. Windows DLLs) 265 | *.dll 266 | *.so 267 | *.so.* 268 | *.dylib 269 | 270 | # Executables 271 | *.exe 272 | *.out 273 | *.app 274 | *.i*86 275 | *.x86_64 276 | *.hex 277 | 278 | # Debug files 279 | *.dSYM/ 280 | *.su 281 | *.idb 282 | *.pdb 283 | 284 | # Kernel Module Compile Results 285 | *.mod* 286 | *.cmd 287 | .tmp_versions/ 288 | modules.order 289 | Module.symvers 290 | Mkfile.old 291 | dkms.conf 292 | 293 | ### Vim template 294 | # Swap 295 | [._]*.s[a-v][a-z] 296 | !*.svg # comment out if you don't need vector files 297 | [._]*.sw[a-p] 298 | [._]s[a-rt-v][a-z] 299 | [._]ss[a-gi-z] 300 | [._]sw[a-p] 301 | 302 | # Session 303 | Session.vim 304 | Sessionx.vim 305 | 306 | # Temporary 307 | .netrwhist 308 | *~ 309 | # Auto-generated tag files 310 | tags 311 | # Persistent undo 312 | [._]*.un~ 313 | 314 | ### macOS template 315 | # General 316 | .DS_Store 317 | .AppleDouble 318 | .LSOverride 319 | 320 | # Icon must end with two \r 321 | Icon 322 | 323 | # Thumbnails 324 | ._* 325 | 326 | # Files that might appear in the root of a volume 327 | .DocumentRevisions-V100 328 | .fseventsd 329 | .Spotlight-V100 330 | .TemporaryItems 331 | .Trashes 332 | .VolumeIcon.icns 333 | .com.apple.timemachine.donotpresent 334 | 335 | # Directories potentially created on remote AFP share 336 | .AppleDB 337 | .AppleDesktop 338 | Network Trash Folder 339 | Temporary Items 340 | .apdisk 341 | 342 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(h26x_sps_parse) 2 | 3 | #使能c++11 4 | set(CMAKE_CXX_STANDARD 11) 5 | cmake_minimum_required(VERSION 3.1.3) 6 | aux_source_directory(. SRC_LIST) 7 | 8 | add_executable(test_h26x_analysis ${SRC_LIST}) 9 | target_link_libraries(test_h26x_analysis ${LINK_LIB_LIST} pthread) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 老衲不出家 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # h26x_sps_parse 2 | > h26x sps info parse 3 | > 解析h26x sps 信息 4 | 5 | 基于雷神h264分析程序修改 6 | 7 | -------------------------------------------------------------------------------- /bs.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef _H264_BS_H 6 | #define _H264_BS_H 1 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | typedef struct 17 | { 18 | uint8_t* start; 19 | uint8_t* p; 20 | uint8_t* end; 21 | int bits_left; 22 | } bs_t; 23 | 24 | #define _OPTIMIZE_BS_ 1 25 | 26 | #if ( _OPTIMIZE_BS_ > 0 ) 27 | #ifndef FAST_U8 28 | #define FAST_U8 29 | #endif 30 | #endif 31 | 32 | 33 | static bs_t* bs_new(uint8_t* buf, size_t size); 34 | static void bs_free(bs_t* b); 35 | static bs_t* bs_clone( bs_t* dest, const bs_t* src ); 36 | static bs_t* bs_init(bs_t* b, uint8_t* buf, size_t size); 37 | static uint32_t bs_byte_aligned(bs_t* b); 38 | static int bs_eof(bs_t* b); 39 | static int bs_overrun(bs_t* b); 40 | static int bs_pos(bs_t* b); 41 | 42 | static uint32_t bs_peek_u1(bs_t* b); 43 | static uint32_t bs_read_u1(bs_t* b); 44 | static uint32_t bs_read_u(bs_t* b, int n); 45 | static uint32_t bs_read_f(bs_t* b, int n); 46 | static uint32_t bs_read_u8(bs_t* b); 47 | static uint32_t bs_read_ue(bs_t* b); 48 | static int32_t bs_read_se(bs_t* b); 49 | 50 | static void bs_write_u1(bs_t* b, uint32_t v); 51 | static void bs_write_u(bs_t* b, int n, uint32_t v); 52 | static void bs_write_f(bs_t* b, int n, uint32_t v); 53 | static void bs_write_u8(bs_t* b, uint32_t v); 54 | static void bs_write_ue(bs_t* b, uint32_t v); 55 | static void bs_write_se(bs_t* b, int32_t v); 56 | 57 | static int bs_read_bytes(bs_t* b, uint8_t* buf, int len); 58 | static int bs_write_bytes(bs_t* b, uint8_t* buf, int len); 59 | static int bs_skip_bytes(bs_t* b, int len); 60 | static uint32_t bs_next_bits(bs_t* b, int nbits); 61 | // IMPLEMENTATION 62 | 63 | static inline bs_t* bs_init(bs_t* b, uint8_t* buf, size_t size) 64 | { 65 | b->start = buf; 66 | b->p = buf; 67 | b->end = buf + size; 68 | b->bits_left = 8; 69 | return b; 70 | } 71 | 72 | static inline bs_t* bs_new(uint8_t* buf, size_t size) 73 | { 74 | bs_t* b = (bs_t*)malloc(sizeof(bs_t)); 75 | bs_init(b, buf, size); 76 | return b; 77 | } 78 | 79 | static inline void bs_free(bs_t* b) 80 | { 81 | free(b); 82 | } 83 | 84 | static inline bs_t* bs_clone(bs_t* dest, const bs_t* src) 85 | { 86 | dest->start = src->p; 87 | dest->p = src->p; 88 | dest->end = src->end; 89 | dest->bits_left = src->bits_left; 90 | return dest; 91 | } 92 | 93 | static inline uint32_t bs_byte_aligned(bs_t* b) 94 | { 95 | return (b->bits_left == 8); 96 | } 97 | 98 | static inline int bs_eof(bs_t* b) { if (b->p >= b->end) { return 1; } else { return 0; } } 99 | 100 | static inline int bs_overrun(bs_t* b) { if (b->p > b->end) { return 1; } else { return 0; } } 101 | 102 | static inline int bs_pos(bs_t* b) { if (b->p > b->end) { return (b->end - b->start); } else { return (b->p - b->start); } } 103 | 104 | static inline int bs_bytes_left(bs_t* b) { return (b->end - b->p); } 105 | 106 | static inline uint32_t bs_read_u1(bs_t* b) 107 | { 108 | uint32_t r = 0; 109 | 110 | b->bits_left--; 111 | 112 | if (! bs_eof(b)) 113 | { 114 | r = ((*(b->p)) >> b->bits_left) & 0x01; 115 | } 116 | 117 | if (b->bits_left == 0) { b->p ++; b->bits_left = 8; } 118 | 119 | return r; 120 | } 121 | 122 | static inline void bs_skip_u1(bs_t* b) 123 | { 124 | b->bits_left--; 125 | if (b->bits_left == 0) { b->p ++; b->bits_left = 8; } 126 | } 127 | 128 | static inline uint32_t bs_peek_u1(bs_t* b) 129 | { 130 | uint32_t r = 0; 131 | 132 | if (! bs_eof(b)) 133 | { 134 | r = ((*(b->p)) >> ( b->bits_left - 1 )); 135 | r &= 0x01; 136 | } 137 | return r; 138 | } 139 | 140 | 141 | static inline uint32_t bs_read_u(bs_t* b, int n) 142 | { 143 | uint32_t r = 0; 144 | int i; 145 | for (i = 0; i < n; i++) 146 | { 147 | r |= ( bs_read_u1(b) << ( n - i - 1 ) ); 148 | } 149 | return r; 150 | } 151 | 152 | static inline void bs_skip_u(bs_t* b, int n) 153 | { 154 | int i; 155 | for ( i = 0; i < n; i++ ) 156 | { 157 | bs_skip_u1( b ); 158 | } 159 | } 160 | 161 | static inline uint32_t bs_read_f(bs_t* b, int n) { return bs_read_u(b, n); } 162 | 163 | static inline uint32_t bs_read_u8(bs_t* b) 164 | { 165 | #ifdef FAST_U8 166 | if (b->bits_left == 8 && ! bs_eof(b)) // can do fast read 167 | { 168 | uint32_t r = b->p[0]; 169 | b->p++; 170 | return r; 171 | } 172 | #endif 173 | return bs_read_u(b, 8); 174 | } 175 | 176 | static inline uint32_t bs_read_ue(bs_t* b) 177 | { 178 | int32_t r = 0; 179 | int i = 0; 180 | 181 | while( (bs_read_u1(b) == 0) && (i < 32) && (!bs_eof(b)) ) 182 | { 183 | i++; 184 | } 185 | r = bs_read_u(b, i); 186 | r += (1 << i) - 1; 187 | return r; 188 | } 189 | 190 | static inline int32_t bs_read_se(bs_t* b) 191 | { 192 | int32_t r = bs_read_ue(b); 193 | if (r & 0x01) 194 | { 195 | r = (r+1)/2; 196 | } 197 | else 198 | { 199 | r = -(r/2); 200 | } 201 | return r; 202 | } 203 | 204 | 205 | static inline void bs_write_u1(bs_t* b, uint32_t v) 206 | { 207 | b->bits_left--; 208 | 209 | if (! bs_eof(b)) 210 | { 211 | // FIXME this is slow, but we must clear bit first 212 | // is it better to memset(0) the whole buffer during bs_init() instead? 213 | // if we don't do either, we introduce pretty nasty bugs 214 | (*(b->p)) &= ~(0x01 << b->bits_left); 215 | (*(b->p)) |= ((v & 0x01) << b->bits_left); 216 | } 217 | 218 | if (b->bits_left == 0) { b->p ++; b->bits_left = 8; } 219 | } 220 | 221 | static inline void bs_write_u(bs_t* b, int n, uint32_t v) 222 | { 223 | int i; 224 | for (i = 0; i < n; i++) 225 | { 226 | bs_write_u1(b, (v >> ( n - i - 1 ))&0x01 ); 227 | } 228 | } 229 | 230 | static inline void bs_write_f(bs_t* b, int n, uint32_t v) { bs_write_u(b, n, v); } 231 | 232 | static inline void bs_write_u8(bs_t* b, uint32_t v) 233 | { 234 | #ifdef FAST_U8 235 | if (b->bits_left == 8 && ! bs_eof(b)) // can do fast write 236 | { 237 | b->p[0] = v; 238 | b->p++; 239 | return; 240 | } 241 | #endif 242 | bs_write_u(b, 8, v); 243 | } 244 | 245 | static inline void bs_write_ue(bs_t* b, uint32_t v) 246 | { 247 | static const int len_table[256] = 248 | { 249 | 1, 250 | 1, 251 | 2,2, 252 | 3,3,3,3, 253 | 4,4,4,4,4,4,4,4, 254 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 255 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 256 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 257 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 258 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 259 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 260 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 261 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 262 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 263 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 264 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 265 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 266 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 267 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 268 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 269 | }; 270 | 271 | int len; 272 | 273 | if (v == 0) 274 | { 275 | bs_write_u1(b, 1); 276 | } 277 | else 278 | { 279 | v++; 280 | 281 | if (v >= 0x01000000) 282 | { 283 | len = 24 + len_table[ v >> 24 ]; 284 | } 285 | else if(v >= 0x00010000) 286 | { 287 | len = 16 + len_table[ v >> 16 ]; 288 | } 289 | else if(v >= 0x00000100) 290 | { 291 | len = 8 + len_table[ v >> 8 ]; 292 | } 293 | else 294 | { 295 | len = len_table[ v ]; 296 | } 297 | 298 | bs_write_u(b, 2*len-1, v); 299 | } 300 | } 301 | 302 | static inline void bs_write_se(bs_t* b, int32_t v) 303 | { 304 | if (v <= 0) 305 | { 306 | bs_write_ue(b, -v*2); 307 | } 308 | else 309 | { 310 | bs_write_ue(b, v*2 - 1); 311 | } 312 | } 313 | 314 | static inline int bs_read_bytes(bs_t* b, uint8_t* buf, int len) 315 | { 316 | int actual_len = len; 317 | if (b->end - b->p < actual_len) { actual_len = b->end - b->p; } 318 | if (actual_len < 0) { actual_len = 0; } 319 | memcpy(buf, b->p, actual_len); 320 | if (len < 0) { len = 0; } 321 | b->p += len; 322 | return actual_len; 323 | } 324 | 325 | static inline int bs_write_bytes(bs_t* b, uint8_t* buf, int len) 326 | { 327 | int actual_len = len; 328 | if (b->end - b->p < actual_len) { actual_len = b->end - b->p; } 329 | if (actual_len < 0) { actual_len = 0; } 330 | memcpy(b->p, buf, actual_len); 331 | if (len < 0) { len = 0; } 332 | b->p += len; 333 | return actual_len; 334 | } 335 | 336 | static inline int bs_skip_bytes(bs_t* b, int len) 337 | { 338 | int actual_len = len; 339 | if (b->end - b->p < actual_len) { actual_len = b->end - b->p; } 340 | if (actual_len < 0) { actual_len = 0; } 341 | if (len < 0) { len = 0; } 342 | b->p += len; 343 | return actual_len; 344 | } 345 | 346 | static inline uint32_t bs_next_bits(bs_t* bs, int nbits) 347 | { 348 | bs_t b; 349 | bs_clone(&b,bs); 350 | return bs_read_u(&b, nbits); 351 | } 352 | 353 | static inline uint64_t bs_next_bytes(bs_t* bs, int nbytes) 354 | { 355 | int i = 0; 356 | uint64_t val = 0; 357 | 358 | if ( (nbytes > 8) || (nbytes < 1) ) { return 0; } 359 | if (bs->p + nbytes > bs->end) { return 0; } 360 | 361 | for ( i = 0; i < nbytes; i++ ) { val = ( val << 8 ) | bs->p[i]; } 362 | return val; 363 | } 364 | 365 | #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 ) 366 | 367 | #ifdef __cplusplus 368 | } 369 | #endif 370 | 371 | #endif 372 | -------------------------------------------------------------------------------- /h264_avcc.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "h264_avcc.h" 11 | #include "bs.h" 12 | #include "h264_stream.h" 13 | 14 | avcc_t* avcc_new() 15 | { 16 | avcc_t* avcc = (avcc_t*)calloc(1, sizeof(avcc_t)); 17 | avcc->sps_table = NULL; 18 | avcc->pps_table = NULL; 19 | return avcc; 20 | } 21 | 22 | void avcc_free(avcc_t* avcc) 23 | { 24 | if (avcc->sps_table != NULL) { free(avcc->sps_table); } 25 | if (avcc->pps_table != NULL) { free(avcc->pps_table); } 26 | free(avcc); 27 | } 28 | 29 | int read_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b) 30 | { 31 | avcc->configurationVersion = bs_read_u8(b); 32 | avcc->AVCProfileIndication = bs_read_u8(b); 33 | avcc->profile_compatibility = bs_read_u8(b); 34 | avcc->AVCLevelIndication = bs_read_u8(b); 35 | /* int reserved = */ bs_read_u(b, 6); // '111111'b; 36 | avcc->lengthSizeMinusOne = bs_read_u(b, 2); 37 | /* int reserved = */ bs_read_u(b, 3); // '111'b; 38 | 39 | avcc->numOfSequenceParameterSets = bs_read_u(b, 5); 40 | avcc->sps_table = (sps_t**)calloc(avcc->numOfSequenceParameterSets, sizeof(sps_t*)); 41 | for (int i = 0; i < avcc->numOfSequenceParameterSets; i++) 42 | { 43 | int sequenceParameterSetLength = bs_read_u(b, 16); 44 | int len = sequenceParameterSetLength; 45 | uint8_t* buf = (uint8_t*)malloc(len); 46 | len = bs_read_bytes(b, buf, len); 47 | int rc = read_nal_unit(h, buf, len); 48 | free(buf); 49 | if (h->nal->nal_unit_type != NAL_UNIT_TYPE_SPS) { continue; } // TODO report errors 50 | if (rc < 0) { continue; } 51 | avcc->sps_table[i] = h->sps; // TODO copy data? 52 | } 53 | 54 | avcc->numOfPictureParameterSets = bs_read_u(b, 8); 55 | avcc->pps_table = (pps_t**)calloc(avcc->numOfSequenceParameterSets, sizeof(pps_t*)); 56 | for (int i = 0; i < avcc->numOfPictureParameterSets; i++) 57 | { 58 | int pictureParameterSetLength = bs_read_u(b, 16); 59 | int len = pictureParameterSetLength; 60 | uint8_t* buf = (uint8_t*)malloc(len); 61 | len = bs_read_bytes(b, buf, len); 62 | int rc = read_nal_unit(h, buf, len); 63 | free(buf); 64 | if (h->nal->nal_unit_type != NAL_UNIT_TYPE_PPS) { continue; } // TODO report errors 65 | if (rc < 0) { continue; } 66 | avcc->pps_table[i] = h->pps; // TODO copy data? 67 | } 68 | 69 | if (bs_overrun(b)) { return -1; } 70 | return bs_pos(b); 71 | } 72 | 73 | 74 | int write_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b) 75 | { 76 | bs_write_u8(b, 1); // configurationVersion = 1; 77 | bs_write_u8(b, avcc->AVCProfileIndication); 78 | bs_write_u8(b, avcc->profile_compatibility); 79 | bs_write_u8(b, avcc->AVCLevelIndication); 80 | bs_write_u(b, 6, 0x3F); // reserved = '111111'b; 81 | bs_write_u(b, 2, avcc->lengthSizeMinusOne); 82 | bs_write_u(b, 3, 0x07); // reserved = '111'b; 83 | 84 | bs_write_u(b, 5, avcc->numOfSequenceParameterSets); 85 | for (int i = 0; i < avcc->numOfSequenceParameterSets; i++) 86 | { 87 | int max_len = 1024; // FIXME 88 | uint8_t* buf = (uint8_t*)malloc(max_len); 89 | h->nal->nal_ref_idc = 3; // NAL_REF_IDC_PRIORITY_HIGHEST; 90 | h->nal->nal_unit_type = NAL_UNIT_TYPE_SPS; 91 | h->sps = avcc->sps_table[i]; 92 | int len = write_nal_unit(h, buf, max_len); 93 | if (len < 0) { free(buf); continue; } // TODO report errors 94 | int sequenceParameterSetLength = len; 95 | bs_write_u(b, 16, sequenceParameterSetLength); 96 | bs_write_bytes(b, buf, len); 97 | free(buf); 98 | } 99 | 100 | bs_write_u(b, 8, avcc->numOfPictureParameterSets); 101 | for (int i = 0; i < avcc->numOfPictureParameterSets; i++) 102 | { 103 | int max_len = 1024; // FIXME 104 | uint8_t* buf = (uint8_t*)malloc(max_len); 105 | h->nal->nal_ref_idc = 3; // NAL_REF_IDC_PRIORITY_HIGHEST; 106 | h->nal->nal_unit_type = NAL_UNIT_TYPE_PPS; 107 | h->pps = avcc->pps_table[i]; 108 | int len = write_nal_unit(h, buf, max_len); 109 | if (len < 0) { free(buf); continue; } // TODO report errors 110 | int pictureParameterSetLength = len; 111 | bs_write_u(b, 16, pictureParameterSetLength); 112 | bs_write_bytes(b, buf, len); 113 | free(buf); 114 | } 115 | 116 | if (bs_overrun(b)) { return -1; } 117 | return bs_pos(b); 118 | } 119 | 120 | #if 0 121 | void debug_avcc(avcc_t* avcc) 122 | { 123 | printf("======= AVC Decoder Configuration Record =======\n"); 124 | printf(" configurationVersion: %d\n", avcc->configurationVersion ); 125 | printf(" AVCProfileIndication: %d\n", avcc->AVCProfileIndication ); 126 | printf(" profile_compatibility: %d\n", avcc->profile_compatibility ); 127 | printf(" AVCLevelIndication: %d\n", avcc->AVCLevelIndication ); 128 | printf(" lengthSizeMinusOne: %d\n", avcc->lengthSizeMinusOne ); 129 | 130 | printf("\n"); 131 | printf(" numOfSequenceParameterSets: %d\n", avcc->numOfSequenceParameterSets ); 132 | for (int i = 0; i < avcc->numOfSequenceParameterSets; i++) 133 | { 134 | //printf(" sequenceParameterSetLength\n", avcc->sequenceParameterSetLength ); 135 | if (avcc->sps_table[i] == NULL) { printf(" null sps\n"); continue; } 136 | debug_sps(avcc->sps_table[i]); 137 | } 138 | 139 | printf("\n"); 140 | printf(" numOfPictureParameterSets: %d\n", avcc->numOfPictureParameterSets ); 141 | for (int i = 0; i < avcc->numOfPictureParameterSets; i++) 142 | { 143 | //printf(" pictureParameterSetLength\n", avcc->pictureParameterSetLength ); 144 | if (avcc->pps_table[i] == NULL) { printf(" null pps\n"); continue; } 145 | debug_pps(avcc->pps_table[i]); 146 | } 147 | } 148 | #endif -------------------------------------------------------------------------------- /h264_avcc.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H264_AVCC_H 6 | #define ZLMEDIAKIT_H264_AVCC_H 7 | 8 | 9 | 10 | #include 11 | #include 12 | 13 | #include "bs.h" 14 | #include "h264_stream.h" 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | /** 21 | AVC decoder configuration record, ISO/IEC 14496-15:2004(E), Section 5.2.4.1 22 | Seen in seen in mp4 files as 'avcC' atom 23 | Seen in flv files as AVCVIDEOPACKET with AVCPacketType == 0 24 | */ 25 | typedef struct 26 | { 27 | int configurationVersion; // = 1 28 | int AVCProfileIndication; 29 | int profile_compatibility; 30 | int AVCLevelIndication; 31 | // bit(6) reserved = '111111'b; 32 | int lengthSizeMinusOne; 33 | // bit(3) reserved = '111'b; 34 | int numOfSequenceParameterSets; 35 | sps_t** sps_table; 36 | int numOfPictureParameterSets; 37 | pps_t** pps_table; 38 | } avcc_t; 39 | 40 | avcc_t* avcc_new(); 41 | void avcc_free(avcc_t* avcc); 42 | int read_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b); 43 | int write_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b); 44 | void debug_avcc(avcc_t* avcc); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | 51 | 52 | #endif //ZLMEDIAKIT_H264_AVCC_H 53 | -------------------------------------------------------------------------------- /h264_sei.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | 6 | #include "bs.h" 7 | #include "h264_stream.h" 8 | #include "h264_sei.h" 9 | 10 | #include 11 | #include // malloc 12 | #include // memset 13 | 14 | sei_t* sei_new() 15 | { 16 | sei_t* s = (sei_t*)malloc(sizeof(sei_t)); 17 | memset(s, 0, sizeof(sei_t)); 18 | s->payload = NULL; 19 | return s; 20 | } 21 | 22 | void sei_free(sei_t* s) 23 | { 24 | if ( s->payload != NULL ) free(s->payload); 25 | free(s); 26 | } 27 | 28 | void read_sei_end_bits(h264_stream_t* h, bs_t* b ) 29 | { 30 | // if the message doesn't end at a byte border 31 | if ( !bs_byte_aligned( b ) ) 32 | { 33 | if ( !bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_one is 0!!!!\n"); 34 | while ( ! bs_byte_aligned( b ) ) 35 | { 36 | if ( bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_zero is 1!!!!\n"); 37 | } 38 | } 39 | 40 | read_rbsp_trailing_bits(h, b); 41 | } 42 | 43 | static void read_user_data_unregistered(h264_stream_t* h, bs_t* b, int payloadSize) 44 | { 45 | sei_t* s = h->sei; 46 | 47 | s->payload = (uint8_t*)malloc(payloadSize); 48 | 49 | int i; 50 | 51 | // uuid_iso_iec_11578 todo... 52 | for (i = 0; i < 16; i++) 53 | s->payload[i] = bs_read_u(b, 8); 54 | for (i = 16; i < payloadSize; i++) 55 | s->payload[i] = bs_read_u(b, 8); 56 | } 57 | 58 | // D.1 SEI payload syntax 59 | // todo����ͬ��sei���ṹ�岻ͬ����ֱ𴴽� 60 | void read_sei_payload(h264_stream_t* h, bs_t* b, int payloadType, int payloadSize) 61 | { 62 | sei_t* s = h->sei; 63 | 64 | switch (payloadType) 65 | { 66 | case 0: 67 | break; 68 | case 1: 69 | break; 70 | case 2: 71 | break; 72 | case 3: 73 | break; 74 | case 4: 75 | break; 76 | case 5: 77 | read_user_data_unregistered(h, b, payloadSize); 78 | break; 79 | default: 80 | break; 81 | } 82 | 83 | read_sei_end_bits(h, b); 84 | } 85 | 86 | // D.1 SEI payload syntax 87 | void write_sei_payload(h264_stream_t* h, bs_t* b, int payloadType, int payloadSize) 88 | { 89 | sei_t* s = h->sei; 90 | 91 | int i; 92 | for ( i = 0; i < s->payloadSize; i++ ) 93 | bs_write_u(b, 8, s->payload[i]); 94 | } 95 | -------------------------------------------------------------------------------- /h264_sei.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H264_SEI_H 6 | #define ZLMEDIAKIT_H264_SEI_H 7 | 8 | 9 | 10 | #include 11 | 12 | #include "bs.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | typedef struct 19 | { 20 | int payloadType; 21 | int payloadSize; 22 | uint8_t* payload; 23 | } sei_t; 24 | 25 | sei_t* sei_new(); 26 | void sei_free(sei_t* s); 27 | 28 | //D.1 SEI payload syntax 29 | #define SEI_TYPE_BUFFERING_PERIOD 0 30 | #define SEI_TYPE_PIC_TIMING 1 31 | #define SEI_TYPE_PAN_SCAN_RECT 2 32 | #define SEI_TYPE_FILLER_PAYLOAD 3 33 | #define SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35 4 34 | #define SEI_TYPE_USER_DATA_UNREGISTERED 5 35 | #define SEI_TYPE_RECOVERY_POINT 6 36 | #define SEI_TYPE_DEC_REF_PIC_MARKING_REPETITION 7 37 | #define SEI_TYPE_SPARE_PIC 8 38 | #define SEI_TYPE_SCENE_INFO 9 39 | #define SEI_TYPE_SUB_SEQ_INFO 10 40 | #define SEI_TYPE_SUB_SEQ_LAYER_CHARACTERISTICS 11 41 | #define SEI_TYPE_SUB_SEQ_CHARACTERISTICS 12 42 | #define SEI_TYPE_FULL_FRAME_FREEZE 13 43 | #define SEI_TYPE_FULL_FRAME_FREEZE_RELEASE 14 44 | #define SEI_TYPE_FULL_FRAME_SNAPSHOT 15 45 | #define SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_START 16 46 | #define SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_END 17 47 | #define SEI_TYPE_MOTION_CONSTRAINED_SLICE_GROUP_SET 18 48 | #define SEI_TYPE_FILM_GRAIN_CHARACTERISTICS 19 49 | #define SEI_TYPE_DEBLOCKING_FILTER_DISPLAY_PREFERENCE 20 50 | #define SEI_TYPE_STEREO_VIDEO_INFO 21 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | 57 | #endif //ZLMEDIAKIT_H264_SEI_H 58 | -------------------------------------------------------------------------------- /h264_stream.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H264_STREAM_H 6 | #define ZLMEDIAKIT_H264_STREAM_H 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "bs.h" 14 | #include "h264_sei.h" 15 | 16 | #include 17 | using std::vector; 18 | 19 | /** 20 | Sequence Parameter Set 21 | @see 7.3.2.1 Sequence parameter set RBSP syntax 22 | @see read_seq_parameter_set_rbsp 23 | @see write_seq_parameter_set_rbsp 24 | @see debug_sps 25 | */ 26 | typedef struct 27 | { 28 | int profile_idc; 29 | int constraint_set0_flag; 30 | int constraint_set1_flag; 31 | int constraint_set2_flag; 32 | int constraint_set3_flag; 33 | int constraint_set4_flag; 34 | int constraint_set5_flag; 35 | int reserved_zero_2bits; 36 | int level_idc; 37 | int seq_parameter_set_id; 38 | int chroma_format_idc; 39 | int separate_colour_plane_flag; 40 | int ChromaArrayType; 41 | int bit_depth_luma_minus8; 42 | int bit_depth_chroma_minus8; 43 | int qpprime_y_zero_transform_bypass_flag; 44 | int seq_scaling_matrix_present_flag; 45 | int seq_scaling_list_present_flag[8]; 46 | int ScalingList4x4[6]; 47 | int UseDefaultScalingMatrix4x4Flag[6]; 48 | int ScalingList8x8[2]; 49 | int UseDefaultScalingMatrix8x8Flag[2]; 50 | int log2_max_frame_num_minus4; 51 | int pic_order_cnt_type; 52 | int log2_max_pic_order_cnt_lsb_minus4; 53 | int delta_pic_order_always_zero_flag; 54 | int offset_for_non_ref_pic; 55 | int offset_for_top_to_bottom_field; 56 | int num_ref_frames_in_pic_order_cnt_cycle; 57 | int offset_for_ref_frame[256]; 58 | int max_num_ref_frames; 59 | int gaps_in_frame_num_value_allowed_flag; 60 | int pic_width_in_mbs_minus1; 61 | int pic_height_in_map_units_minus1; 62 | int frame_mbs_only_flag; 63 | int mb_adaptive_frame_field_flag; 64 | int direct_8x8_inference_flag; 65 | int frame_cropping_flag; 66 | int frame_crop_left_offset; 67 | int frame_crop_right_offset; 68 | int frame_crop_top_offset; 69 | int frame_crop_bottom_offset; 70 | int vui_parameters_present_flag; 71 | 72 | struct 73 | { 74 | int aspect_ratio_info_present_flag; 75 | int aspect_ratio_idc; 76 | int sar_width; 77 | int sar_height; 78 | int overscan_info_present_flag; 79 | int overscan_appropriate_flag; 80 | int video_signal_type_present_flag; 81 | int video_format; 82 | int video_full_range_flag; 83 | int colour_description_present_flag; 84 | int colour_primaries; 85 | int transfer_characteristics; 86 | int matrix_coefficients; 87 | int chroma_loc_info_present_flag; 88 | int chroma_sample_loc_type_top_field; 89 | int chroma_sample_loc_type_bottom_field; 90 | int timing_info_present_flag; 91 | int num_units_in_tick; 92 | int time_scale; 93 | int fixed_frame_rate_flag; 94 | int nal_hrd_parameters_present_flag; 95 | int vcl_hrd_parameters_present_flag; 96 | int low_delay_hrd_flag; 97 | int pic_struct_present_flag; 98 | int bitstream_restriction_flag; 99 | int motion_vectors_over_pic_boundaries_flag; 100 | int max_bytes_per_pic_denom; 101 | int max_bits_per_mb_denom; 102 | int log2_max_mv_length_horizontal; 103 | int log2_max_mv_length_vertical; 104 | int num_reorder_frames; 105 | int max_dec_frame_buffering; 106 | } vui; 107 | 108 | struct 109 | { 110 | int cpb_cnt_minus1; 111 | int bit_rate_scale; 112 | int cpb_size_scale; 113 | int bit_rate_value_minus1[32]; // up to cpb_cnt_minus1, which is <= 31 114 | int cpb_size_value_minus1[32]; 115 | int cbr_flag[32]; 116 | int initial_cpb_removal_delay_length_minus1; 117 | int cpb_removal_delay_length_minus1; 118 | int dpb_output_delay_length_minus1; 119 | int time_offset_length; 120 | } hrd; 121 | 122 | } sps_t; 123 | 124 | 125 | /** 126 | Picture Parameter Set 127 | @see 7.3.2.2 Picture parameter set RBSP syntax 128 | @see read_pic_parameter_set_rbsp 129 | @see write_pic_parameter_set_rbsp 130 | @see debug_pps 131 | */ 132 | typedef struct 133 | { 134 | int pic_parameter_set_id; 135 | int seq_parameter_set_id; 136 | int entropy_coding_mode_flag; 137 | int pic_order_present_flag; // 2005版本为此字段名 保留,不影响库本身write的编译,但实际不使用 138 | int bottom_field_pic_order_in_frame_present_flag; // 2013版本为此字段名 139 | int num_slice_groups_minus1; 140 | int slice_group_map_type; 141 | int run_length_minus1[8]; // up to num_slice_groups_minus1, which is <= 7 in Baseline and Extended, 0 otheriwse 142 | int top_left[8]; 143 | int bottom_right[8]; 144 | int slice_group_change_direction_flag; 145 | int slice_group_change_rate_minus1; 146 | int pic_size_in_map_units_minus1; 147 | int slice_group_id_bytes; 148 | vector slice_group_id; // FIXME what size? 149 | int num_ref_idx_l0_active_minus1; 150 | int num_ref_idx_l1_active_minus1; 151 | int weighted_pred_flag; 152 | int weighted_bipred_idc; 153 | int pic_init_qp_minus26; 154 | int pic_init_qs_minus26; 155 | int chroma_qp_index_offset; 156 | int deblocking_filter_control_present_flag; 157 | int constrained_intra_pred_flag; 158 | int redundant_pic_cnt_present_flag; 159 | 160 | // see if we carry any of the optional headers 161 | int _more_rbsp_data_present; 162 | 163 | int transform_8x8_mode_flag; 164 | int pic_scaling_matrix_present_flag; 165 | int pic_scaling_list_present_flag[8]; 166 | int* ScalingList4x4[6]; 167 | int UseDefaultScalingMatrix4x4Flag[6]; 168 | int* ScalingList8x8[2]; 169 | int UseDefaultScalingMatrix8x8Flag[2]; 170 | int second_chroma_qp_index_offset; 171 | } pps_t; 172 | 173 | // predictive weight table 174 | typedef struct 175 | { 176 | int luma_log2_weight_denom; 177 | int chroma_log2_weight_denom; 178 | int luma_weight_l0_flag[64]; 179 | int luma_weight_l0[64]; 180 | int luma_offset_l0[64]; 181 | int chroma_weight_l0_flag[64]; 182 | int chroma_weight_l0[64][2]; 183 | int chroma_offset_l0[64][2]; 184 | int luma_weight_l1_flag[64]; 185 | int luma_weight_l1[64]; 186 | int luma_offset_l1[64]; 187 | int chroma_weight_l1_flag[64]; 188 | int chroma_weight_l1[64][2]; 189 | int chroma_offset_l1[64][2]; 190 | } pwt_t; 191 | 192 | // ref pic list modification 193 | typedef struct 194 | { 195 | int modification_of_pic_nums_idc; 196 | int abs_diff_pic_num_minus1; 197 | int long_term_pic_num; 198 | } rplm_tt; 199 | 200 | typedef struct 201 | { 202 | int ref_pic_list_modification_flag_l0; 203 | int ref_pic_list_modification_flag_l1; 204 | 205 | vector rplm; 206 | } rplm_t; 207 | 208 | // decoded ref pic marking 209 | typedef struct 210 | { 211 | int memory_management_control_operation; 212 | int difference_of_pic_nums_minus1; 213 | int long_term_pic_num; 214 | int long_term_frame_idx; 215 | int max_long_term_frame_idx_plus1; 216 | } drpm_tt; 217 | typedef struct 218 | { 219 | int no_output_of_prior_pics_flag; 220 | int long_term_reference_flag; 221 | int adaptive_ref_pic_marking_mode_flag; 222 | 223 | vector drpm; 224 | } drpm_t; 225 | 226 | /** 227 | Slice Header 228 | @see 7.3.3 Slice header syntax 229 | @see read_slice_header_rbsp 230 | @see write_slice_header_rbsp 231 | @see debug_slice_header_rbsp 232 | */ 233 | typedef struct 234 | { 235 | int read_slice_type; // see if we only read slice type and return 236 | 237 | int first_mb_in_slice; 238 | int slice_type; 239 | int pic_parameter_set_id; 240 | int colour_plane_id; 241 | int frame_num_bytes; 242 | int frame_num; 243 | int field_pic_flag; 244 | int bottom_field_flag; 245 | int idr_pic_id; 246 | int pic_order_cnt_lsb_bytes; 247 | int pic_order_cnt_lsb; 248 | int delta_pic_order_cnt_bottom; 249 | int delta_pic_order_cnt[ 2 ]; 250 | int redundant_pic_cnt; 251 | int direct_spatial_mv_pred_flag; 252 | int num_ref_idx_active_override_flag; 253 | int num_ref_idx_l0_active_minus1; 254 | int num_ref_idx_l1_active_minus1; 255 | int cabac_init_idc; 256 | int slice_qp_delta; 257 | int sp_for_switch_flag; 258 | int slice_qs_delta; 259 | int disable_deblocking_filter_idc; 260 | int slice_alpha_c0_offset_div2; 261 | int slice_beta_offset_div2; 262 | int slice_group_change_cycle_bytes; 263 | int slice_group_change_cycle; 264 | 265 | pwt_t pwt; 266 | rplm_t rplm; 267 | drpm_t drpm; 268 | } slice_header_t; 269 | 270 | 271 | /** 272 | Access unit delimiter 273 | @see 7.3.1 NAL unit syntax 274 | @see read_nal_unit 275 | @see write_nal_unit 276 | @see debug_nal 277 | */ 278 | typedef struct 279 | { 280 | int primary_pic_type; 281 | } aud_t; 282 | 283 | /** 284 | Network Abstraction Layer (NAL) unit 285 | @see 7.3.1 NAL unit syntax 286 | @see read_nal_unit 287 | @see write_nal_unit 288 | @see debug_nal 289 | */ 290 | typedef struct 291 | { 292 | int forbidden_zero_bit; 293 | int nal_ref_idc; 294 | int nal_unit_type; 295 | void* parsed; // FIXME 296 | int sizeof_parsed; 297 | 298 | //uint8_t* rbsp_buf; 299 | //int rbsp_size; 300 | } nal_t; 301 | 302 | typedef struct 303 | { 304 | int _is_initialized; 305 | int sps_id; 306 | int initial_cpb_removal_delay; 307 | int initial_cpb_delay_offset; 308 | } sei_buffering_t; 309 | 310 | typedef struct 311 | { 312 | int clock_timestamp_flag; 313 | int ct_type; 314 | int nuit_field_based_flag; 315 | int counting_type; 316 | int full_timestamp_flag; 317 | int discontinuity_flag; 318 | int cnt_dropped_flag; 319 | int n_frames; 320 | 321 | int seconds_value; 322 | int minutes_value; 323 | int hours_value; 324 | 325 | int seconds_flag; 326 | int minutes_flag; 327 | int hours_flag; 328 | 329 | int time_offset; 330 | } picture_timestamp_t; 331 | 332 | typedef struct 333 | { 334 | int _is_initialized; 335 | int cpb_removal_delay; 336 | int dpb_output_delay; 337 | int pic_struct; 338 | picture_timestamp_t clock_timestamps[3]; // 3 is the maximum possible value 339 | } sei_picture_timing_t; 340 | 341 | 342 | typedef struct 343 | { 344 | int rbsp_size; 345 | uint8_t* rbsp_buf; 346 | } slice_data_rbsp_t; 347 | 348 | typedef struct 349 | { 350 | int type; // 0:h264 1:h265 351 | int init; 352 | int profile_idc; 353 | int level_idc; 354 | int tier_idc; 355 | int width; 356 | int height; 357 | int crop_left; 358 | int crop_right; 359 | int crop_top; 360 | int crop_bottom; 361 | float max_framerate; // 由SPS计算得到的帧率,为0表示SPS中没有相应的字段计算 362 | int chroma_format_idc; // YUV颜色空间 0: monochrome 1:420 2:422 3:444 363 | int encoding_type; // 为1表示CABAC 0表示CAVLC 364 | int bit_depth_luma; 365 | int bit_depth_chroma; 366 | } videoinfo_t; 367 | 368 | 369 | /** 370 | H264 stream 371 | Contains data structures for all NAL types that can be handled by this library. 372 | When reading, data is read into those, and when writing it is written from those. 373 | The reason why they are all contained in one place is that some of them depend on others, we need to 374 | have all of them available to read or write correctly. 375 | */ 376 | typedef struct 377 | { 378 | nal_t* nal; 379 | sps_t* sps; 380 | pps_t* pps; 381 | aud_t* aud; 382 | sei_t* sei; //This is a TEMP pointer at whats in h->seis... 383 | int num_seis; 384 | slice_header_t* sh; 385 | slice_data_rbsp_t* slice_data; 386 | 387 | sps_t* sps_table[32]; 388 | pps_t* pps_table[256]; 389 | sei_t** seis; 390 | videoinfo_t* info; 391 | } h264_stream_t; 392 | 393 | h264_stream_t* h264_new(); 394 | void h264_free(h264_stream_t* h); 395 | 396 | int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end); 397 | 398 | int rbsp_to_nal(const uint8_t* rbsp_buf, const int* rbsp_size, uint8_t* nal_buf, int* nal_size); 399 | int nal_to_rbsp(const int nal_header_size, const uint8_t* nal_buf, int* nal_size, uint8_t* rbsp_buf, int* rbsp_size); 400 | 401 | int read_nal_unit(h264_stream_t* h, uint8_t* buf, int size); 402 | int peek_nal_unit(h264_stream_t* h, uint8_t* buf, int size); 403 | 404 | void read_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b); 405 | void read_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag ); 406 | void read_vui_parameters(h264_stream_t* h, bs_t* b); 407 | void read_hrd_parameters(h264_stream_t* h, bs_t* b); 408 | 409 | void read_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b); 410 | 411 | void read_sei_rbsp(h264_stream_t* h, bs_t* b); 412 | void read_sei_message(h264_stream_t* h, bs_t* b); 413 | void read_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b); 414 | void read_end_of_seq_rbsp(h264_stream_t* h, bs_t* b); 415 | void read_end_of_stream_rbsp(h264_stream_t* h, bs_t* b); 416 | void read_filler_data_rbsp(h264_stream_t* h, bs_t* b); 417 | 418 | void read_slice_layer_rbsp(h264_stream_t* h, bs_t* b); 419 | void read_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b); 420 | void read_rbsp_trailing_bits(h264_stream_t* h, bs_t* b); 421 | void read_slice_header(h264_stream_t* h, bs_t* b); 422 | void read_ref_pic_list_modification(h264_stream_t* h, bs_t* b); 423 | void read_pred_weight_table(h264_stream_t* h, bs_t* b); 424 | void read_dec_ref_pic_marking(h264_stream_t* h, bs_t* b); 425 | 426 | int more_rbsp_trailing_data(h264_stream_t* h, bs_t* b); 427 | 428 | int is_slice_type(int slice_type, int cmp_type); 429 | 430 | int write_nal_unit(h264_stream_t* h, uint8_t* buf, int size); 431 | 432 | void write_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b); 433 | void write_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag ); 434 | void write_vui_parameters(h264_stream_t* h, bs_t* b); 435 | void write_hrd_parameters(h264_stream_t* h, bs_t* b); 436 | 437 | void write_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b); 438 | 439 | void write_sei_rbsp(h264_stream_t* h, bs_t* b); 440 | void write_sei_message(h264_stream_t* h, bs_t* b); 441 | void write_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b); 442 | void write_end_of_seq_rbsp(h264_stream_t* h, bs_t* b); 443 | void write_end_of_stream_rbsp(h264_stream_t* h, bs_t* b); 444 | void write_filler_data_rbsp(h264_stream_t* h, bs_t* b); 445 | 446 | void write_slice_layer_rbsp(h264_stream_t* h, bs_t* b); 447 | void write_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b); 448 | void write_rbsp_trailing_bits(h264_stream_t* h, bs_t* b); 449 | void write_slice_header(h264_stream_t* h, bs_t* b); 450 | void write_ref_pic_list_modification(h264_stream_t* h, bs_t* b); 451 | void write_pred_weight_table(h264_stream_t* h, bs_t* b); 452 | void write_dec_ref_pic_marking(h264_stream_t* h, bs_t* b); 453 | 454 | void debug_sps(sps_t* sps); 455 | void debug_pps(pps_t* pps); 456 | void debug_slice_header(slice_header_t* sh); 457 | void debug_nal(h264_stream_t* h, nal_t* nal); 458 | 459 | void debug_bytes(uint8_t* buf, int len); 460 | 461 | void read_sei_payload( h264_stream_t* h, bs_t* b, int payloadType, int payloadSize); 462 | void write_sei_payload( h264_stream_t* h, bs_t* b, int payloadType, int payloadSize); 463 | 464 | //NAL ref idc codes 465 | #define NAL_REF_IDC_PRIORITY_HIGHEST 3 466 | #define NAL_REF_IDC_PRIORITY_HIGH 2 467 | #define NAL_REF_IDC_PRIORITY_LOW 1 468 | #define NAL_REF_IDC_PRIORITY_DISPOSABLE 0 469 | 470 | //Table 7-1 NAL unit type codes 471 | #define NAL_UNIT_TYPE_UNSPECIFIED 0 // Unspecified 472 | #define NAL_UNIT_TYPE_CODED_SLICE_NON_IDR 1 // Coded slice of a non-IDR picture 473 | #define NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A 2 // Coded slice data partition A 474 | #define NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_B 3 // Coded slice data partition B 475 | #define NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_C 4 // Coded slice data partition C 476 | #define NAL_UNIT_TYPE_CODED_SLICE_IDR 5 // Coded slice of an IDR picture 477 | #define NAL_UNIT_TYPE_SEI 6 // Supplemental enhancement information (SEI) 478 | #define NAL_UNIT_TYPE_SPS 7 // Sequence parameter set 479 | #define NAL_UNIT_TYPE_PPS 8 // Picture parameter set 480 | #define NAL_UNIT_TYPE_AUD 9 // Access unit delimiter 481 | #define NAL_UNIT_TYPE_END_OF_SEQUENCE 10 // End of sequence 482 | #define NAL_UNIT_TYPE_END_OF_STREAM 11 // End of stream 483 | #define NAL_UNIT_TYPE_FILLER 12 // Filler data 484 | #define NAL_UNIT_TYPE_SPS_EXT 13 // Sequence parameter set extension 485 | // 14..18 // Reserved 486 | #define NAL_UNIT_TYPE_CODED_SLICE_AUX 19 // Coded slice of an auxiliary coded picture without partitioning 487 | // 20..23 // Reserved 488 | // 24..31 // Unspecified 489 | 490 | 491 | 492 | //7.4.3 Table 7-6. Name association to slice_type 493 | #define SH_SLICE_TYPE_P 0 // P (P slice) 494 | #define SH_SLICE_TYPE_B 1 // B (B slice) 495 | #define SH_SLICE_TYPE_I 2 // I (I slice) 496 | #define SH_SLICE_TYPE_SP 3 // SP (SP slice) 497 | #define SH_SLICE_TYPE_SI 4 // SI (SI slice) 498 | //as per footnote to Table 7-6, the *_ONLY slice types indicate that all other slices in that picture are of the same type 499 | #define SH_SLICE_TYPE_P_ONLY 5 // P (P slice) 500 | #define SH_SLICE_TYPE_B_ONLY 6 // B (B slice) 501 | #define SH_SLICE_TYPE_I_ONLY 7 // I (I slice) 502 | #define SH_SLICE_TYPE_SP_ONLY 8 // SP (SP slice) 503 | #define SH_SLICE_TYPE_SI_ONLY 9 // SI (SI slice) 504 | 505 | //Appendix E. Table E-1 Meaning of sample aspect ratio indicator 506 | #define SAR_Unspecified 0 // Unspecified 507 | #define SAR_1_1 1 // 1:1 508 | #define SAR_12_11 2 // 12:11 509 | #define SAR_10_11 3 // 10:11 510 | #define SAR_16_11 4 // 16:11 511 | #define SAR_40_33 5 // 40:33 512 | #define SAR_24_11 6 // 24:11 513 | #define SAR_20_11 7 // 20:11 514 | #define SAR_32_11 8 // 32:11 515 | #define SAR_80_33 9 // 80:33 516 | #define SAR_18_11 10 // 18:11 517 | #define SAR_15_11 11 // 15:11 518 | #define SAR_64_33 12 // 64:33 519 | #define SAR_160_99 13 // 160:99 520 | // 14..254 Reserved 521 | #define SAR_Extended 255 // Extended_SAR 522 | 523 | //7.4.3.1 Table 7-7 modification_of_pic_nums_idc operations for modification of reference picture lists 524 | #define RPLR_IDC_ABS_DIFF_ADD 0 525 | #define RPLR_IDC_ABS_DIFF_SUBTRACT 1 526 | #define RPLR_IDC_LONG_TERM 2 527 | #define RPLR_IDC_END 3 528 | 529 | //7.4.3.3 Table 7-9 Memory management control operation (memory_management_control_operation) values 530 | #define MMCO_END 0 531 | #define MMCO_SHORT_TERM_UNUSED 1 532 | #define MMCO_LONG_TERM_UNUSED 2 533 | #define MMCO_SHORT_TERM_TO_LONG_TERM 3 534 | #define MMCO_LONG_TERM_MAX_INDEX 4 535 | #define MMCO_ALL_UNUSED 5 536 | #define MMCO_CURRENT_TO_LONG_TERM 6 537 | 538 | //7.4.2.4 Table 7-5 Meaning of primary_pic_type 539 | #define AUD_PRIMARY_PIC_TYPE_I 0 // I 540 | #define AUD_PRIMARY_PIC_TYPE_IP 1 // I, P 541 | #define AUD_PRIMARY_PIC_TYPE_IPB 2 // I, P, B 542 | #define AUD_PRIMARY_PIC_TYPE_SI 3 // SI 543 | #define AUD_PRIMARY_PIC_TYPE_SISP 4 // SI, SP 544 | #define AUD_PRIMARY_PIC_TYPE_ISI 5 // I, SI 545 | #define AUD_PRIMARY_PIC_TYPE_ISIPSP 6 // I, SI, P, SP 546 | #define AUD_PRIMARY_PIC_TYPE_ISIPSPB 7 // I, SI, P, SP, B 547 | 548 | #define H264_PROFILE_BASELINE 66 549 | #define H264_PROFILE_MAIN 77 550 | #define H264_PROFILE_EXTENDED 88 551 | #define H264_PROFILE_HIGH 100 552 | 553 | // file handle for debug output 554 | extern FILE* h264_dbgfile; 555 | 556 | 557 | #endif //ZLMEDIAKIT_H264_STREAM_H 558 | -------------------------------------------------------------------------------- /h265_avcc.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "h265_avcc.h" 11 | #include "bs.h" 12 | #include "h265_stream.h" 13 | 14 | #if 0 15 | avcc_t* avcc_new() 16 | { 17 | avcc_t* avcc = (avcc_t*)calloc(1, sizeof(avcc_t)); 18 | avcc->sps_table = NULL; 19 | avcc->pps_table = NULL; 20 | return avcc; 21 | } 22 | 23 | void avcc_free(avcc_t* avcc) 24 | { 25 | if (avcc->sps_table != NULL) { free(avcc->sps_table); } 26 | if (avcc->pps_table != NULL) { free(avcc->pps_table); } 27 | free(avcc); 28 | } 29 | 30 | int read_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b) 31 | { 32 | avcc->configurationVersion = bs_read_u8(b); 33 | avcc->AVCProfileIndication = bs_read_u8(b); 34 | avcc->profile_compatibility = bs_read_u8(b); 35 | avcc->AVCLevelIndication = bs_read_u8(b); 36 | /* int reserved = */ bs_read_u(b, 6); // '111111'b; 37 | avcc->lengthSizeMinusOne = bs_read_u(b, 2); 38 | /* int reserved = */ bs_read_u(b, 3); // '111'b; 39 | 40 | avcc->numOfSequenceParameterSets = bs_read_u(b, 5); 41 | avcc->sps_table = (sps_t**)calloc(avcc->numOfSequenceParameterSets, sizeof(sps_t*)); 42 | for (int i = 0; i < avcc->numOfSequenceParameterSets; i++) 43 | { 44 | int sequenceParameterSetLength = bs_read_u(b, 16); 45 | int len = sequenceParameterSetLength; 46 | uint8_t* buf = (uint8_t*)malloc(len); 47 | len = bs_read_bytes(b, buf, len); 48 | int rc = read_nal_unit(h, buf, len); 49 | free(buf); 50 | if (h->nal->nal_unit_type != NAL_UNIT_TYPE_SPS) { continue; } // TODO report errors 51 | if (rc < 0) { continue; } 52 | avcc->sps_table[i] = h->sps; // TODO copy data? 53 | } 54 | 55 | avcc->numOfPictureParameterSets = bs_read_u(b, 8); 56 | avcc->pps_table = (pps_t**)calloc(avcc->numOfSequenceParameterSets, sizeof(pps_t*)); 57 | for (int i = 0; i < avcc->numOfPictureParameterSets; i++) 58 | { 59 | int pictureParameterSetLength = bs_read_u(b, 16); 60 | int len = pictureParameterSetLength; 61 | uint8_t* buf = (uint8_t*)malloc(len); 62 | len = bs_read_bytes(b, buf, len); 63 | int rc = read_nal_unit(h, buf, len); 64 | free(buf); 65 | if (h->nal->nal_unit_type != NAL_UNIT_TYPE_PPS) { continue; } // TODO report errors 66 | if (rc < 0) { continue; } 67 | avcc->pps_table[i] = h->pps; // TODO copy data? 68 | } 69 | 70 | if (bs_overrun(b)) { return -1; } 71 | return bs_pos(b); 72 | } 73 | 74 | 75 | int write_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b) 76 | { 77 | bs_write_u8(b, 1); // configurationVersion = 1; 78 | bs_write_u8(b, avcc->AVCProfileIndication); 79 | bs_write_u8(b, avcc->profile_compatibility); 80 | bs_write_u8(b, avcc->AVCLevelIndication); 81 | bs_write_u(b, 6, 0x3F); // reserved = '111111'b; 82 | bs_write_u(b, 2, avcc->lengthSizeMinusOne); 83 | bs_write_u(b, 3, 0x07); // reserved = '111'b; 84 | 85 | bs_write_u(b, 5, avcc->numOfSequenceParameterSets); 86 | for (int i = 0; i < avcc->numOfSequenceParameterSets; i++) 87 | { 88 | int max_len = 1024; // FIXME 89 | uint8_t* buf = (uint8_t*)malloc(max_len); 90 | h->nal->nal_ref_idc = 3; // NAL_REF_IDC_PRIORITY_HIGHEST; 91 | h->nal->nal_unit_type = NAL_UNIT_TYPE_SPS; 92 | h->sps = avcc->sps_table[i]; 93 | int len = write_nal_unit(h, buf, max_len); 94 | if (len < 0) { free(buf); continue; } // TODO report errors 95 | int sequenceParameterSetLength = len; 96 | bs_write_u(b, 16, sequenceParameterSetLength); 97 | bs_write_bytes(b, buf, len); 98 | free(buf); 99 | } 100 | 101 | bs_write_u(b, 8, avcc->numOfPictureParameterSets); 102 | for (int i = 0; i < avcc->numOfPictureParameterSets; i++) 103 | { 104 | int max_len = 1024; // FIXME 105 | uint8_t* buf = (uint8_t*)malloc(max_len); 106 | h->nal->nal_ref_idc = 3; // NAL_REF_IDC_PRIORITY_HIGHEST; 107 | h->nal->nal_unit_type = NAL_UNIT_TYPE_PPS; 108 | h->pps = avcc->pps_table[i]; 109 | int len = write_nal_unit(h, buf, max_len); 110 | if (len < 0) { free(buf); continue; } // TODO report errors 111 | int pictureParameterSetLength = len; 112 | bs_write_u(b, 16, pictureParameterSetLength); 113 | bs_write_bytes(b, buf, len); 114 | free(buf); 115 | } 116 | 117 | if (bs_overrun(b)) { return -1; } 118 | return bs_pos(b); 119 | } 120 | 121 | void debug_avcc(avcc_t* avcc) 122 | { 123 | printf("======= AVC Decoder Configuration Record =======\n"); 124 | printf(" configurationVersion: %d\n", avcc->configurationVersion ); 125 | printf(" AVCProfileIndication: %d\n", avcc->AVCProfileIndication ); 126 | printf(" profile_compatibility: %d\n", avcc->profile_compatibility ); 127 | printf(" AVCLevelIndication: %d\n", avcc->AVCLevelIndication ); 128 | printf(" lengthSizeMinusOne: %d\n", avcc->lengthSizeMinusOne ); 129 | 130 | printf("\n"); 131 | printf(" numOfSequenceParameterSets: %d\n", avcc->numOfSequenceParameterSets ); 132 | for (int i = 0; i < avcc->numOfSequenceParameterSets; i++) 133 | { 134 | //printf(" sequenceParameterSetLength\n", avcc->sequenceParameterSetLength ); 135 | if (avcc->sps_table[i] == NULL) { printf(" null sps\n"); continue; } 136 | debug_sps(avcc->sps_table[i]); 137 | } 138 | 139 | printf("\n"); 140 | printf(" numOfPictureParameterSets: %d\n", avcc->numOfPictureParameterSets ); 141 | for (int i = 0; i < avcc->numOfPictureParameterSets; i++) 142 | { 143 | //printf(" pictureParameterSetLength\n", avcc->pictureParameterSetLength ); 144 | if (avcc->pps_table[i] == NULL) { printf(" null pps\n"); continue; } 145 | debug_pps(avcc->pps_table[i]); 146 | } 147 | } 148 | #endif -------------------------------------------------------------------------------- /h265_avcc.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H265_AVCC_H 6 | #define ZLMEDIAKIT_H265_AVCC_H 7 | 8 | #include 9 | #include 10 | 11 | #include "bs.h" 12 | #include "h264_stream.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | /** 19 | AVC decoder configuration record, ISO/IEC 14496-15:2004(E), Section 5.2.4.1 20 | Seen in seen in mp4 files as 'avcC' atom 21 | Seen in flv files as AVCVIDEOPACKET with AVCPacketType == 0 22 | */ 23 | typedef struct 24 | { 25 | int configurationVersion; // = 1 26 | int AVCProfileIndication; 27 | int profile_compatibility; 28 | int AVCLevelIndication; 29 | // bit(6) reserved = '111111'b; 30 | int lengthSizeMinusOne; 31 | // bit(3) reserved = '111'b; 32 | int numOfSequenceParameterSets; 33 | sps_t** sps_table; 34 | int numOfPictureParameterSets; 35 | pps_t** pps_table; 36 | } avcc_t; 37 | 38 | avcc_t* avcc_new(); 39 | void avcc_free(avcc_t* avcc); 40 | int read_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b); 41 | int write_avcc(avcc_t* avcc, h264_stream_t* h, bs_t* b); 42 | void debug_avcc(avcc_t* avcc); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | 49 | 50 | #endif //ZLMEDIAKIT_H265_AVCC_H 51 | -------------------------------------------------------------------------------- /h265_sei.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #include "h265_sei.h" 6 | 7 | #include 8 | #include // malloc 9 | #include // memset 10 | 11 | 12 | h265_sei_t* h265_sei_new() 13 | { 14 | h265_sei_t* s = (h265_sei_t*)malloc(sizeof(h265_sei_t)); 15 | memset(s, 0, sizeof(h265_sei_t)); 16 | s->payload = NULL; 17 | return s; 18 | } 19 | 20 | void h265_sei_free(h265_sei_t* s) 21 | { 22 | if (s == NULL) return; 23 | if ( s->payload != NULL ) free(s->payload); 24 | free(s); 25 | } 26 | 27 | 28 | void h265_read_sei_end_bits(bs_t* b ) 29 | { 30 | // if the message doesn't end at a byte border 31 | if ( !bs_byte_aligned( b ) ) 32 | { 33 | if ( !bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_one is 0!!!!\n"); 34 | while ( ! bs_byte_aligned( b ) ) 35 | { 36 | if ( bs_read_u1( b ) ) fprintf(stderr, "WARNING: bit_equal_to_zero is 1!!!!\n"); 37 | } 38 | } 39 | 40 | h265_read_rbsp_trailing_bits(b); 41 | } 42 | 43 | static void read_user_data_unregistered(h265_stream_t* h, bs_t* b, int payloadSize) 44 | { 45 | h265_sei_t* s = h->sei; 46 | 47 | s->payload = (uint8_t*)malloc(payloadSize); 48 | 49 | int i; 50 | 51 | for (i = 0; i < 16; i++) 52 | s->payload[i] = bs_read_u(b, 8); 53 | for (i = 16; i < payloadSize; i++) 54 | s->payload[i] = bs_read_u(b, 8); 55 | } 56 | 57 | // D.1 SEI payload syntax 58 | void h265_read_sei_payload(h265_stream_t* h, bs_t* b, int payloadType, int payloadSize) 59 | { 60 | int sei_type = h->nal->nal_unit_type; 61 | h265_sei_t* s = h->sei; 62 | 63 | if (sei_type == NAL_UNIT_PREFIX_SEI) 64 | { 65 | switch (payloadType) 66 | { 67 | case 0: 68 | break; 69 | case 1: 70 | break; 71 | case 2: 72 | break; 73 | case 3: 74 | break; 75 | case 4: 76 | break; 77 | case 5: 78 | read_user_data_unregistered(h, b, payloadSize); 79 | break; 80 | default: 81 | break; 82 | } 83 | } 84 | else if (sei_type == NAL_UNIT_SUFFIX_SEI) 85 | { 86 | switch (payloadType) 87 | { 88 | case 3: 89 | break; 90 | case 4: 91 | break; 92 | case 5: 93 | break; 94 | case 17: 95 | break; 96 | case 22: 97 | break; 98 | case 132: 99 | break; 100 | default: 101 | break; 102 | } 103 | } 104 | 105 | 106 | h265_read_sei_end_bits(b); 107 | } 108 | 109 | #if 0 110 | // D.1 SEI payload syntax 111 | void write_sei_payload(h265_stream_t* h, bs_t* b, int payloadType, int payloadSize) 112 | { 113 | h265_sei_t* s = h->sei; 114 | 115 | int i; 116 | for ( i = 0; i < s->payloadSize; i++ ) 117 | bs_write_u(b, 8, s->payload[i]); 118 | } 119 | #endif 120 | -------------------------------------------------------------------------------- /h265_sei.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H265_SEI_H 6 | #define ZLMEDIAKIT_H265_SEI_H 7 | 8 | 9 | #include 10 | 11 | #include "bs.h" 12 | #include "h265_stream.h" 13 | 14 | h265_sei_t* h265_sei_new(); 15 | void h265_sei_free(h265_sei_t* s); 16 | void h265_read_sei_payload(h265_stream_t* h, bs_t* b, int payloadType, int payloadSize); 17 | 18 | //D.1 SEI payload syntax 19 | #define H265_SEI_TYPE_BUFFERING_PERIOD 0 20 | #define H265_SEI_TYPE_PIC_TIMING 1 21 | #define H265_SEI_TYPE_PAN_SCAN_RECT 2 22 | #define H265_SEI_TYPE_FILLER_PAYLOAD 3 23 | #define H265_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35 4 24 | #define H265_SEI_TYPE_USER_DATA_UNREGISTERED 5 25 | #define H265_SEI_TYPE_RECOVERY_POINT 6 26 | #define H265_SEI_TYPE_DEC_REF_PIC_MARKING_REPETITION 7 27 | #define H265_SEI_TYPE_SPARE_PIC 8 28 | #define H265_SEI_TYPE_SCENE_INFO 9 29 | #define H265_SEI_TYPE_SUB_SEQ_INFO 10 30 | #define H265_SEI_TYPE_SUB_SEQ_LAYER_CHARACTERISTICS 11 31 | #define H265_SEI_TYPE_SUB_SEQ_CHARACTERISTICS 12 32 | #define H265_SEI_TYPE_FULL_FRAME_FREEZE 13 33 | #define H265_SEI_TYPE_FULL_FRAME_FREEZE_RELEASE 14 34 | #define H265_SEI_TYPE_FULL_FRAME_SNAPSHOT 15 35 | #define H265_SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_START 16 36 | #define H265_SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_END 17 37 | #define H265_SEI_TYPE_MOTION_CONSTRAINED_SLICE_GROUP_SET 18 38 | #define H265_SEI_TYPE_FILM_GRAIN_CHARACTERISTICS 19 39 | #define H265_SEI_TYPE_DEBLOCKING_FILTER_DISPLAY_PREFERENCE 20 40 | #define H265_SEI_TYPE_STEREO_VIDEO_INFO 21 41 | 42 | 43 | #endif //ZLMEDIAKIT_H265_SEI_H 44 | -------------------------------------------------------------------------------- /h265_stream.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include "bs.h" 11 | #include "h265_stream.h" 12 | #include "h265_sei.h" 13 | 14 | FILE* h265_dbgfile = NULL; 15 | 16 | #define printf(...) fprintf((h265_dbgfile == NULL ? stdout : h265_dbgfile), __VA_ARGS__) 17 | 18 | /** 19 | Create a new H265 stream object. Allocates all structures contained within it. 20 | @return the stream object 21 | */ 22 | h265_stream_t* h265_new() 23 | { 24 | h265_stream_t* h = (h265_stream_t*)calloc(1, sizeof(h265_stream_t)); 25 | 26 | h->nal = (h265_nal_t*)calloc(1, sizeof(h265_nal_t)); 27 | 28 | // initialize tables 29 | for ( int i = 0; i < 16; i++ ) { h->vps_table[i] = (h265_vps_t*)calloc(1, sizeof(h265_vps_t)); } 30 | for ( int i = 0; i < 32; i++ ) { h->sps_table[i] = (h265_sps_t*)calloc(1, sizeof(h265_sps_t)); } 31 | for ( int i = 0; i < 256; i++ ) { h->pps_table[i] = (h265_pps_t*)calloc(1, sizeof(h265_pps_t)); } 32 | 33 | h->vps = h->vps_table[0]; 34 | h->sps = h->sps_table[0]; 35 | h->pps = h->pps_table[0]; 36 | h->aud = (h265_aud_t*)calloc(1, sizeof(h265_aud_t)); 37 | h->num_seis = 0; 38 | h->seis = NULL; 39 | h->sei = NULL; //This is a TEMP pointer at whats in h->seis... 40 | h->sh = (h265_slice_header_t*)calloc(1, sizeof(h265_slice_header_t)); 41 | 42 | h->info = (videoinfo_t*)calloc(1, sizeof(videoinfo_t)); 43 | h->info->type = 1; 44 | return h; 45 | } 46 | 47 | /** 48 | Free an existing H265 stream object. Frees all contained structures. 49 | @param[in,out] h the stream object 50 | */ 51 | void h265_free(h265_stream_t* h) 52 | { 53 | free(h->nal); 54 | for ( int i = 0; i < 16; i++ ) { if (h->vps_table[i]!=NULL) free( h->vps_table[i] ); } 55 | for ( int i = 0; i < 32; i++ ) { if (h->sps_table[i]!=NULL) free( h->sps_table[i] ); } 56 | for ( int i = 0; i < 256; i++ ) { if (h->pps_table[i]!=NULL) free( h->pps_table[i] ); } 57 | 58 | if (h->aud != NULL) 59 | { 60 | free(h->aud); 61 | } 62 | 63 | if(h->seis != NULL) 64 | { 65 | for( int i = 0; i < h->num_seis; i++ ) 66 | { 67 | h265_sei_t* sei = h->seis[i]; 68 | h265_sei_free(sei); 69 | } 70 | free(h->seis); 71 | } 72 | free(h->sh); 73 | free(h); 74 | } 75 | 76 | /** 77 | Read a NAL unit from a byte buffer. 78 | The buffer must start exactly at the beginning of the nal (after the start prefix). 79 | The NAL is read into h->nal and into other fields within h depending on its type (check h->nal->nal_unit_type after reading). 80 | @param[in,out] h the stream object 81 | @param[in] buf the buffer 82 | @param[in] size the size of the buffer 83 | @return the length of data actually read, or -1 on error 84 | */ 85 | //7.3.1 NAL unit syntax 86 | int h265_read_nal_unit(h265_stream_t* h, uint8_t* buf, int size) 87 | { 88 | h265_nal_t* nal = h->nal; 89 | 90 | bs_t* b = bs_new(buf, size); 91 | // nal header 92 | nal->forbidden_zero_bit = bs_read_f(b,1); 93 | nal->nal_unit_type = bs_read_u(b,6); 94 | nal->nuh_layer_id = bs_read_u(b,6); 95 | nal->nuh_temporal_id_plus1 = bs_read_u(b,3); 96 | nal->parsed = NULL; 97 | nal->sizeof_parsed = 0; 98 | bs_free(b); 99 | 100 | int nal_size = size; 101 | int rbsp_size = size; 102 | uint8_t* rbsp_buf = (uint8_t*)malloc(rbsp_size); 103 | 104 | int rc = nal_to_rbsp(2, buf, &nal_size, rbsp_buf, &rbsp_size); 105 | 106 | if (rc < 0) { free(rbsp_buf); return -1; } // handle conversion error 107 | 108 | b = bs_new(rbsp_buf, rbsp_size); 109 | 110 | // nal data 111 | switch ( nal->nal_unit_type ) 112 | { 113 | case NAL_UNIT_VPS: 114 | h265_read_vps_rbsp(h,b); 115 | break; 116 | case NAL_UNIT_SPS: 117 | h265_read_sps_rbsp(h, b); 118 | nal->parsed = h->sps; 119 | nal->sizeof_parsed = sizeof(h265_sps_t); 120 | break; 121 | case NAL_UNIT_PPS: 122 | h265_read_pps_rbsp(h, b); 123 | nal->parsed = h->pps; 124 | nal->sizeof_parsed = sizeof(h265_pps_t); 125 | break; 126 | case NAL_UNIT_PREFIX_SEI: 127 | h265_read_sei_rbsp(h, b); 128 | nal->parsed = h->sei; 129 | nal->sizeof_parsed = sizeof(h265_sei_t); 130 | break; 131 | case NAL_UNIT_SUFFIX_SEI: 132 | h265_read_sei_rbsp(h, b); 133 | nal->parsed = h->sei; 134 | nal->sizeof_parsed = sizeof(h265_sei_t); 135 | break; 136 | case NAL_UNIT_AUD: 137 | h265_read_aud_rbsp(h, b); 138 | nal->parsed = h->aud; 139 | nal->sizeof_parsed = sizeof(h265_aud_t); 140 | break; 141 | case NAL_UNIT_EOS: 142 | h265_read_end_of_seq_rbsp(h, b); 143 | break; 144 | case NAL_UNIT_EOB: 145 | h265_read_end_of_stream_rbsp(h, b); 146 | break; 147 | case NAL_UNIT_CODED_SLICE_TRAIL_N: 148 | case NAL_UNIT_CODED_SLICE_TRAIL_R: 149 | case NAL_UNIT_CODED_SLICE_TSA_N: 150 | case NAL_UNIT_CODED_SLICE_TSA_R: 151 | case NAL_UNIT_CODED_SLICE_STSA_N: 152 | case NAL_UNIT_CODED_SLICE_STSA_R: 153 | case NAL_UNIT_CODED_SLICE_RADL_N: 154 | case NAL_UNIT_CODED_SLICE_RADL_R: 155 | case NAL_UNIT_CODED_SLICE_RASL_N: 156 | case NAL_UNIT_CODED_SLICE_RASL_R: 157 | case NAL_UNIT_CODED_SLICE_BLA_W_LP: 158 | case NAL_UNIT_CODED_SLICE_BLA_W_RADL: 159 | case NAL_UNIT_CODED_SLICE_BLA_N_LP: 160 | case NAL_UNIT_CODED_SLICE_IDR_W_RADL: 161 | case NAL_UNIT_CODED_SLICE_IDR_N_LP: 162 | case NAL_UNIT_CODED_SLICE_CRA: 163 | h265_read_slice_layer_rbsp(h, b); 164 | nal->parsed = h->sh; 165 | nal->sizeof_parsed = sizeof(h265_slice_header_t); 166 | break; 167 | case NAL_UNIT_RESERVED_VCL_N10: 168 | case NAL_UNIT_RESERVED_VCL_R11: 169 | case NAL_UNIT_RESERVED_VCL_N12: 170 | case NAL_UNIT_RESERVED_VCL_R13: 171 | case NAL_UNIT_RESERVED_VCL_N14: 172 | case NAL_UNIT_RESERVED_VCL_R15: 173 | 174 | case NAL_UNIT_RESERVED_IRAP_VCL22: 175 | case NAL_UNIT_RESERVED_IRAP_VCL23: 176 | 177 | case NAL_UNIT_RESERVED_VCL24: 178 | case NAL_UNIT_RESERVED_VCL25: 179 | case NAL_UNIT_RESERVED_VCL26: 180 | case NAL_UNIT_RESERVED_VCL27: 181 | case NAL_UNIT_RESERVED_VCL28: 182 | case NAL_UNIT_RESERVED_VCL29: 183 | case NAL_UNIT_RESERVED_VCL30: 184 | case NAL_UNIT_RESERVED_VCL31: 185 | printf ("Note: found reserved VCL NAL unit.\n"); 186 | nal->parsed = NULL; 187 | nal->sizeof_parsed = 0; 188 | break; 189 | case NAL_UNIT_RESERVED_NVCL41: 190 | case NAL_UNIT_RESERVED_NVCL42: 191 | case NAL_UNIT_RESERVED_NVCL43: 192 | case NAL_UNIT_RESERVED_NVCL44: 193 | case NAL_UNIT_RESERVED_NVCL45: 194 | case NAL_UNIT_RESERVED_NVCL46: 195 | case NAL_UNIT_RESERVED_NVCL47: 196 | printf ("Note: found reserved NAL unit.\n"); 197 | nal->parsed = NULL; 198 | nal->sizeof_parsed = 0; 199 | break; 200 | case NAL_UNIT_UNSPECIFIED_48: 201 | case NAL_UNIT_UNSPECIFIED_49: 202 | case NAL_UNIT_UNSPECIFIED_50: 203 | case NAL_UNIT_UNSPECIFIED_51: 204 | case NAL_UNIT_UNSPECIFIED_52: 205 | case NAL_UNIT_UNSPECIFIED_53: 206 | case NAL_UNIT_UNSPECIFIED_54: 207 | case NAL_UNIT_UNSPECIFIED_55: 208 | case NAL_UNIT_UNSPECIFIED_56: 209 | case NAL_UNIT_UNSPECIFIED_57: 210 | case NAL_UNIT_UNSPECIFIED_58: 211 | case NAL_UNIT_UNSPECIFIED_59: 212 | case NAL_UNIT_UNSPECIFIED_60: 213 | case NAL_UNIT_UNSPECIFIED_61: 214 | case NAL_UNIT_UNSPECIFIED_62: 215 | case NAL_UNIT_UNSPECIFIED_63: 216 | printf ("Note: found unspecified NAL unit.\n"); 217 | nal->parsed = NULL; 218 | nal->sizeof_parsed = 0; 219 | break; 220 | default: 221 | // here comes the reserved/unspecified/ignored stuff 222 | nal->parsed = NULL; 223 | nal->sizeof_parsed = 0; 224 | return 0; 225 | } 226 | 227 | if (bs_overrun(b)) { bs_free(b); free(rbsp_buf); return -1; } 228 | 229 | bs_free(b); 230 | free(rbsp_buf); 231 | 232 | return nal_size; 233 | } 234 | 235 | void h265_read_ptl(profile_tier_level_t* ptl, bs_t* b, int profilePresentFlag, int max_sub_layers_minus1) 236 | { 237 | int i = 0; 238 | if (profilePresentFlag) 239 | { 240 | ptl->general_profile_space = bs_read_u(b, 2); 241 | ptl->general_tier_flag = bs_read_u1(b); 242 | ptl->general_profile_idc = bs_read_u(b, 5); 243 | for (i = 0; i < 32; i++) 244 | { 245 | ptl->general_profile_compatibility_flag[i] = bs_read_u1(b); 246 | } 247 | ptl->general_progressive_source_flag = bs_read_u1(b); 248 | ptl->general_interlaced_source_flag = bs_read_u1(b); 249 | ptl->general_non_packed_constraint_flag = bs_read_u1(b); 250 | ptl->general_frame_only_constraint_flag = bs_read_u1(b); 251 | if (ptl->general_profile_idc==4 || ptl->general_profile_compatibility_flag[4] || 252 | ptl->general_profile_idc==5 || ptl->general_profile_compatibility_flag[5] || 253 | ptl->general_profile_idc==6 || ptl->general_profile_compatibility_flag[6] || 254 | ptl->general_profile_idc==7 || ptl->general_profile_compatibility_flag[7]) 255 | { 256 | ptl->general_max_12bit_constraint_flag = bs_read_u1(b); 257 | ptl->general_max_10bit_constraint_flag = bs_read_u1(b); 258 | ptl->general_max_8bit_constraint_flag = bs_read_u1(b); 259 | ptl->general_max_422chroma_constraint_flag = bs_read_u1(b); 260 | ptl->general_max_420chroma_constraint_flag = bs_read_u1(b); 261 | ptl->general_max_monochrome_constraint_flag = bs_read_u1(b); 262 | ptl->general_intra_constraint_flag = bs_read_u1(b); 263 | ptl->general_one_picture_only_constraint_flag = bs_read_u1(b); 264 | ptl->general_lower_bit_rate_constraint_flag = bs_read_u1(b); 265 | uint64_t tmp1 = bs_read_u(b, 32); 266 | uint64_t tmp2 = bs_read_u(b, 2); 267 | ptl->general_reserved_zero_34bits = tmp1+tmp2; 268 | } 269 | else 270 | { 271 | uint64_t tmp1 = bs_read_u(b, 32); 272 | uint64_t tmp2 = bs_read_u(b, 11); 273 | ptl->general_reserved_zero_43bits = tmp1+tmp2; 274 | } 275 | if ((ptl->general_profile_idc>=1 && ptl->general_profile_idc<=5) || 276 | ptl->general_profile_compatibility_flag[1] || ptl->general_profile_compatibility_flag[2] || 277 | ptl->general_profile_compatibility_flag[3] || ptl->general_profile_compatibility_flag[4] || 278 | ptl->general_profile_compatibility_flag[5]) 279 | { 280 | ptl->general_inbld_flag = bs_read_u1(b); 281 | } 282 | else 283 | { 284 | ptl->general_reserved_zero_bit = bs_read_u1(b); 285 | } 286 | } 287 | ptl->general_level_idc = bs_read_u8(b); 288 | ptl->sub_layer_profile_present_flag.resize(max_sub_layers_minus1); 289 | ptl->sub_layer_level_present_flag.resize(max_sub_layers_minus1); 290 | for (i = 0; i < max_sub_layers_minus1; i++) 291 | { 292 | ptl->sub_layer_profile_present_flag[i] = bs_read_u1(b); 293 | ptl->sub_layer_level_present_flag[i] = bs_read_u1(b); 294 | } 295 | if (max_sub_layers_minus1 > 0) 296 | { 297 | for (i = max_sub_layers_minus1; i < 8; i++) 298 | { 299 | ptl->reserved_zero_2bits[i] = bs_read_u(b, 2); 300 | } 301 | } 302 | ptl->sub_layer_profile_space.resize(max_sub_layers_minus1); 303 | ptl->sub_layer_tier_flag.resize(max_sub_layers_minus1); 304 | ptl->sub_layer_profile_idc.resize(max_sub_layers_minus1); 305 | ptl->sub_layer_profile_compatibility_flag.resize(max_sub_layers_minus1); 306 | for (int j = 0; j < max_sub_layers_minus1; j++) 307 | { 308 | ptl->sub_layer_profile_compatibility_flag[j].resize(32); 309 | } 310 | ptl->sub_layer_progressive_source_flag.resize(max_sub_layers_minus1); 311 | ptl->sub_layer_interlaced_source_flag.resize(max_sub_layers_minus1); 312 | ptl->sub_layer_non_packed_constraint_flag.resize(max_sub_layers_minus1); 313 | ptl->sub_layer_frame_only_constraint_flag.resize(max_sub_layers_minus1); 314 | ptl->sub_layer_max_12bit_constraint_flag.resize(max_sub_layers_minus1); 315 | ptl->sub_layer_max_10bit_constraint_flag.resize(max_sub_layers_minus1); 316 | ptl->sub_layer_max_8bit_constraint_flag.resize(max_sub_layers_minus1); 317 | ptl->sub_layer_max_422chroma_constraint_flag.resize(max_sub_layers_minus1); 318 | ptl->sub_layer_max_420chroma_constraint_flag.resize(max_sub_layers_minus1); 319 | ptl->sub_layer_max_monochrome_constraint_flag.resize(max_sub_layers_minus1); 320 | ptl->sub_layer_intra_constraint_flag.resize(max_sub_layers_minus1); 321 | ptl->sub_layer_one_picture_only_constraint_flag.resize(max_sub_layers_minus1); 322 | ptl->sub_layer_lower_bit_rate_constraint_flag.resize(max_sub_layers_minus1); 323 | ptl->sub_layer_reserved_zero_34bits.resize(max_sub_layers_minus1); 324 | ptl->sub_layer_reserved_zero_43bits.resize(max_sub_layers_minus1); 325 | ptl->sub_layer_inbld_flag.resize(max_sub_layers_minus1); 326 | ptl->sub_layer_reserved_zero_bit.resize(max_sub_layers_minus1); 327 | ptl->sub_layer_level_idc.resize(max_sub_layers_minus1); 328 | for (i = 0; i < max_sub_layers_minus1; i++) 329 | { 330 | if (ptl->sub_layer_profile_present_flag[i]) 331 | { 332 | ptl->sub_layer_profile_space[i] = bs_read_u(b, 2); 333 | ptl->sub_layer_tier_flag[i] = bs_read_u1(b); 334 | ptl->sub_layer_profile_idc[i] = bs_read_u(b, 5); 335 | for (int j = 0; j < 32; j++) 336 | { 337 | ptl->sub_layer_profile_compatibility_flag[i][j] = bs_read_u1(b); 338 | } 339 | ptl->sub_layer_progressive_source_flag[i] = bs_read_u1(b); 340 | ptl->sub_layer_interlaced_source_flag[i] = bs_read_u1(b); 341 | ptl->sub_layer_non_packed_constraint_flag[i] = bs_read_u1(b); 342 | ptl->sub_layer_frame_only_constraint_flag[i] = bs_read_u1(b); 343 | if (ptl->sub_layer_profile_idc[i]==4 || ptl->sub_layer_profile_compatibility_flag[i][4] || 344 | ptl->sub_layer_profile_idc[i]==5 || ptl->sub_layer_profile_compatibility_flag[i][5] || 345 | ptl->sub_layer_profile_idc[i]==6 || ptl->sub_layer_profile_compatibility_flag[i][6] || 346 | ptl->sub_layer_profile_idc[i]==7 || ptl->sub_layer_profile_compatibility_flag[i][7]) 347 | { 348 | ptl->sub_layer_max_12bit_constraint_flag[i] = bs_read_u1(b); 349 | ptl->sub_layer_max_10bit_constraint_flag[i] = bs_read_u1(b); 350 | ptl->sub_layer_max_8bit_constraint_flag[i] = bs_read_u1(b); 351 | ptl->sub_layer_max_422chroma_constraint_flag[i] = bs_read_u1(b); 352 | ptl->sub_layer_max_420chroma_constraint_flag[i] = bs_read_u1(b); 353 | ptl->sub_layer_max_monochrome_constraint_flag[i] = bs_read_u1(b); 354 | ptl->sub_layer_intra_constraint_flag[i] = bs_read_u1(b); 355 | ptl->sub_layer_one_picture_only_constraint_flag[i] = bs_read_u1(b); 356 | ptl->sub_layer_lower_bit_rate_constraint_flag[i] = bs_read_u1(b); 357 | uint64_t tmp1 = bs_read_u(b, 32); 358 | uint64_t tmp2 = bs_read_u(b, 2); 359 | ptl->sub_layer_reserved_zero_34bits[i] = tmp1+tmp2; 360 | } 361 | else 362 | { 363 | uint64_t tmp1 = bs_read_u(b, 32); 364 | uint64_t tmp2 = bs_read_u(b, 12); 365 | ptl->sub_layer_reserved_zero_43bits[i] = tmp1+tmp2; 366 | } 367 | // to check 368 | if ((ptl->sub_layer_profile_idc[i]>=1 && ptl->sub_layer_profile_idc[i]<=5) || 369 | ptl->sub_layer_profile_compatibility_flag[i][1] || 370 | ptl->sub_layer_profile_compatibility_flag[i][2] || 371 | ptl->sub_layer_profile_compatibility_flag[i][3] || 372 | ptl->sub_layer_profile_compatibility_flag[i][4] || 373 | ptl->sub_layer_profile_compatibility_flag[i][5]) 374 | { 375 | ptl->sub_layer_inbld_flag[i] = bs_read_u1(b); 376 | } 377 | else 378 | { 379 | ptl->sub_layer_reserved_zero_bit[i] = bs_read_u1(b); 380 | } 381 | } 382 | if (ptl->sub_layer_level_present_flag[i]) 383 | { 384 | ptl->sub_layer_level_idc[i] = bs_read_u8(b); 385 | } 386 | } 387 | } 388 | 389 | // E.2.3 Sub-layer HRD parameters syntax 390 | // E.3.3 The variable CpbCnt is set equal to cpb_cnt_minus1[ subLayerId ]. 391 | void h265_read_sub_layer_hrd_parameters(sub_layer_hrd_parameters_t* subhrd, bs_t* b, int sub_pic_hrd_params_present_flag, int CpbCnt) 392 | { 393 | subhrd->bit_rate_value_minus1.resize(CpbCnt+1); 394 | subhrd->cpb_size_value_minus1.resize(CpbCnt+1); 395 | subhrd->cpb_size_du_value_minus1.resize(CpbCnt+1); 396 | subhrd->bit_rate_du_value_minus1.resize(CpbCnt+1); 397 | subhrd->cbr_flag.resize(CpbCnt+1); 398 | for (int i = 0; i <= CpbCnt; i++) 399 | { 400 | subhrd->bit_rate_value_minus1[i] = bs_read_ue(b); 401 | subhrd->cpb_size_value_minus1[i] = bs_read_ue(b); 402 | if (sub_pic_hrd_params_present_flag) 403 | { 404 | subhrd->cpb_size_du_value_minus1[i] = bs_read_ue(b); 405 | subhrd->bit_rate_du_value_minus1[i] = bs_read_ue(b); 406 | } 407 | subhrd->cbr_flag[i] = bs_read_u1(b); 408 | } 409 | } 410 | 411 | // E.2.2 HRD parameters syntax 412 | void h265_read_hrd_parameters(hrd_parameters_t* hrd, bs_t* b, int commonInfPresentFlag, int maxNumSubLayersMinus1) 413 | { 414 | if(commonInfPresentFlag) 415 | { 416 | hrd->nal_hrd_parameters_present_flag = bs_read_u1(b); 417 | hrd->vcl_hrd_parameters_present_flag = bs_read_u1(b); 418 | if (hrd->nal_hrd_parameters_present_flag || 419 | hrd->vcl_hrd_parameters_present_flag) 420 | { 421 | hrd->sub_pic_hrd_params_present_flag = bs_read_u1(b); 422 | if (hrd->sub_pic_hrd_params_present_flag) 423 | { 424 | hrd->tick_divisor_minus2 = bs_read_u8(b); 425 | hrd->du_cpb_removal_delay_increment_length_minus1 = bs_read_u(b, 5); 426 | hrd->sub_pic_cpb_params_in_pic_timing_sei_flag = bs_read_u1(b); 427 | hrd->dpb_output_delay_du_length_minus1 = bs_read_u(b, 5); 428 | } 429 | hrd->bit_rate_scale = bs_read_u(b, 4); 430 | hrd->cpb_size_scale = bs_read_u(b, 4); 431 | if (hrd->sub_pic_hrd_params_present_flag) 432 | { 433 | hrd->cpb_size_du_scale = bs_read_u(b, 4); 434 | } 435 | hrd->initial_cpb_removal_delay_length_minus1 = bs_read_u(b, 5); 436 | hrd->au_cpb_removal_delay_length_minus1 = bs_read_u(b, 5); 437 | hrd->dpb_output_delay_length_minus1 = bs_read_u(b, 5); 438 | } 439 | } 440 | hrd->fixed_pic_rate_general_flag.resize(maxNumSubLayersMinus1+1); 441 | hrd->fixed_pic_rate_within_cvs_flag.resize(maxNumSubLayersMinus1+1); 442 | hrd->elemental_duration_in_tc_minus1.resize(maxNumSubLayersMinus1+1); 443 | hrd->low_delay_hrd_flag.resize(maxNumSubLayersMinus1+1); 444 | hrd->cpb_cnt_minus1.resize(maxNumSubLayersMinus1+1); 445 | for (int i = 0; i <= maxNumSubLayersMinus1; i++) 446 | { 447 | hrd->fixed_pic_rate_general_flag[i] = bs_read_u1(b); 448 | if (!hrd->fixed_pic_rate_general_flag[i]) 449 | { 450 | hrd->fixed_pic_rate_within_cvs_flag[i] = bs_read_u1(b); 451 | } 452 | if (hrd->fixed_pic_rate_within_cvs_flag[i]) 453 | { 454 | hrd->elemental_duration_in_tc_minus1[i] = bs_read_ue(b); 455 | } 456 | else 457 | { 458 | hrd->low_delay_hrd_flag[i] = bs_read_u1(b); 459 | } 460 | if (!hrd->low_delay_hrd_flag[i]) 461 | { 462 | hrd->cpb_cnt_minus1[i] = bs_read_u1(b); 463 | } 464 | if(hrd->nal_hrd_parameters_present_flag) 465 | { 466 | h265_read_sub_layer_hrd_parameters(&(hrd->sub_layer_hrd_parameters), b, hrd->sub_pic_hrd_params_present_flag, hrd->cpb_cnt_minus1[i]); 467 | } 468 | if(hrd->vcl_hrd_parameters_present_flag) 469 | { 470 | h265_read_sub_layer_hrd_parameters(&(hrd->sub_layer_hrd_parameters_v), b, hrd->sub_pic_hrd_params_present_flag, hrd->cpb_cnt_minus1[i]); 471 | } 472 | } 473 | } 474 | 475 | // E.2.1 VUI parameters syntax 476 | void h265_read_vui_parameters(vui_parameters_t* vui, bs_t* b, int maxNumSubLayersMinus1) 477 | { 478 | vui->aspect_ratio_info_present_flag = bs_read_u1(b); 479 | if (vui->aspect_ratio_info_present_flag) 480 | { 481 | vui->aspect_ratio_idc = bs_read_u8(b); 482 | if (vui->aspect_ratio_idc == H265_SAR_Extended) 483 | { 484 | vui->sar_width = bs_read_u(b, 16); 485 | vui->sar_height = bs_read_u(b, 16); 486 | } 487 | } 488 | vui->overscan_info_present_flag = bs_read_u1(b); 489 | if (vui->overscan_info_present_flag) 490 | { 491 | vui->overscan_appropriate_flag = bs_read_u1(b); 492 | } 493 | vui->video_signal_type_present_flag = bs_read_u1(b); 494 | if (vui->video_signal_type_present_flag) 495 | { 496 | vui->video_format = bs_read_u(b, 3); 497 | vui->video_full_range_flag = bs_read_u1(b); 498 | vui->colour_description_present_flag = bs_read_u1(b); 499 | if (vui->colour_description_present_flag) 500 | { 501 | vui->colour_primaries = bs_read_u8(b); 502 | vui->transfer_characteristics = bs_read_u8(b); 503 | vui->matrix_coeffs = bs_read_u8(b); 504 | } 505 | } 506 | vui->chroma_loc_info_present_flag = bs_read_u1(b); 507 | if (vui->chroma_loc_info_present_flag) 508 | { 509 | vui->chroma_sample_loc_type_top_field = bs_read_ue(b); 510 | vui->chroma_sample_loc_type_bottom_field = bs_read_ue(b); 511 | } 512 | vui->neutral_chroma_indication_flag = bs_read_u1(b); 513 | vui->field_seq_flag = bs_read_u1(b); 514 | vui->frame_field_info_present_flag = bs_read_u1(b); 515 | vui->default_display_window_flag = bs_read_u1(b); 516 | if (vui->default_display_window_flag) 517 | { 518 | vui->def_disp_win_left_offset = bs_read_ue(b); 519 | vui->def_disp_win_right_offset = bs_read_ue(b); 520 | vui->def_disp_win_top_offset = bs_read_ue(b); 521 | vui->def_disp_win_bottom_offset = bs_read_ue(b); 522 | } 523 | vui->vui_timing_info_present_flag = bs_read_u1(b); 524 | if (vui->vui_timing_info_present_flag) 525 | { 526 | vui->vui_num_units_in_tick = bs_read_u(b, 32); 527 | vui->vui_time_scale = bs_read_u(b, 32); 528 | vui->vui_poc_proportional_to_timing_flag = bs_read_u1(b); 529 | if (vui->vui_poc_proportional_to_timing_flag) 530 | { 531 | vui->vui_num_ticks_poc_diff_one_minus1 = bs_read_ue(b); 532 | } 533 | vui->vui_hrd_parameters_present_flag = bs_read_u1(b); 534 | if (vui->vui_hrd_parameters_present_flag) 535 | { 536 | h265_read_hrd_parameters(&vui->hrd_parameters, b, 1, maxNumSubLayersMinus1); 537 | } 538 | } 539 | vui->bitstream_restriction_flag = bs_read_u1(b); 540 | if (vui->bitstream_restriction_flag) 541 | { 542 | vui->tiles_fixed_structure_flag = bs_read_u1(b); 543 | vui->motion_vectors_over_pic_boundaries_flag = bs_read_u1(b); 544 | vui->restricted_ref_pic_lists_flag = bs_read_u1(b); 545 | vui->min_spatial_segmentation_idc = bs_read_ue(b); 546 | vui->max_bytes_per_pic_denom = bs_read_ue(b); 547 | vui->max_bits_per_min_cu_denom = bs_read_ue(b); 548 | vui->log2_max_mv_length_horizontal = bs_read_ue(b); 549 | vui->log2_max_mv_length_vertical = bs_read_ue(b); 550 | } 551 | } 552 | 553 | // 7.3.4 Scaling list data syntax 554 | void h265_read_scaling_list(scaling_list_data_t* sld, bs_t* b) 555 | { 556 | for(int sizeId = 0; sizeId < 4; sizeId++) 557 | { 558 | for(int matrixId = 0; matrixId < 6; matrixId += ( sizeId == 3 ) ? 3 : 1) 559 | { 560 | sld->scaling_list_pred_mode_flag[sizeId][matrixId] = bs_read_u1(b); 561 | if (!sld->scaling_list_pred_mode_flag[sizeId][matrixId]) 562 | { 563 | sld->scaling_list_pred_matrix_id_delta[sizeId][matrixId] = bs_read_ue(b); 564 | } 565 | else 566 | { 567 | int nextCoef = 8; 568 | int coefNum = min(64, (1 << (4 + (sizeId << 1)))); 569 | sld->coefNum = coefNum; // tmp store 570 | if (sizeId > 1) 571 | { 572 | sld->scaling_list_dc_coef_minus8[sizeId - 2][matrixId] = bs_read_se(b); 573 | nextCoef = sld->scaling_list_dc_coef_minus8[sizeId - 2][matrixId] + 8; 574 | } 575 | for (int i = 0; i < sld->coefNum; i++) 576 | { 577 | int scaling_list_delta_coef = bs_read_se(b); 578 | nextCoef = (nextCoef + scaling_list_delta_coef + 256) % 256; 579 | sld->ScalingList[sizeId][matrixId][i] = nextCoef; 580 | } 581 | } 582 | } 583 | } 584 | } 585 | 586 | // st_ref_pic_set 587 | // 7.3.7 Short-term reference picture set syntax 588 | void h265_read_short_term_ref_pic_set(bs_t* b, h265_sps_t* sps, st_ref_pic_set_t*st, referencePictureSets_t* rps, int stRpsIdx) 589 | { 590 | st->inter_ref_pic_set_prediction_flag = 0; 591 | if (stRpsIdx != 0) 592 | { 593 | st->inter_ref_pic_set_prediction_flag = bs_read_u1(b); 594 | } 595 | if (st->inter_ref_pic_set_prediction_flag) 596 | { 597 | st->delta_idx_minus1 = 0; 598 | if (stRpsIdx == sps->m_RPSList.size()) 599 | { 600 | st->delta_idx_minus1 = bs_read_ue(b); 601 | } 602 | int rIdx = stRpsIdx - 1 - st->delta_idx_minus1; 603 | referencePictureSets_t* rpsRef = &sps->m_RPSList[rIdx]; 604 | 605 | st->delta_rps_sign = bs_read_u1(b); 606 | st->abs_delta_rps_minus1 = bs_read_ue(b); 607 | int deltaRPS = (1 - 2 * st->delta_rps_sign) * (st->abs_delta_rps_minus1 + 1); // delta_RPS 608 | st->used_by_curr_pic_flag.resize(rpsRef->m_numberOfPictures+1); 609 | st->use_delta_flag.resize(rpsRef->m_numberOfPictures+1); 610 | for (int j = 0; j <= rpsRef->m_numberOfPictures; j++) 611 | { 612 | st->used_by_curr_pic_flag[j] = bs_read_u1(b); 613 | int refIdc = st->used_by_curr_pic_flag[j]; 614 | if (!st->used_by_curr_pic_flag[j]) 615 | { 616 | st->use_delta_flag[j] = bs_read_u1(b); 617 | refIdc = st->use_delta_flag[j] << 1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0. 618 | } 619 | // todo furture 620 | if (refIdc == 1 || refIdc == 2) 621 | { 622 | 623 | } 624 | } 625 | } 626 | else 627 | { 628 | st->num_negative_pics = bs_read_ue(b); 629 | st->num_positive_pics = bs_read_ue(b); 630 | 631 | rps->m_numberOfNegativePictures = st->num_negative_pics; 632 | rps->m_numberOfPositivePictures = st->num_positive_pics; 633 | 634 | // to check... 635 | st->delta_poc_s0_minus1.resize(st->num_negative_pics); 636 | st->used_by_curr_pic_s0_flag.resize(st->num_negative_pics); 637 | for (int i = 0; i < st->num_negative_pics; i++) 638 | { 639 | st->delta_poc_s0_minus1[i] = bs_read_ue(b); 640 | st->used_by_curr_pic_s0_flag[i] = bs_read_u1(b); 641 | rps->m_used[i] = st->used_by_curr_pic_s0_flag[i]; 642 | } 643 | st->delta_poc_s1_minus1.resize(st->num_positive_pics); 644 | st->used_by_curr_pic_s1_flag.resize(st->num_positive_pics); 645 | for (int i = 0; i < st->num_positive_pics; i++) 646 | { 647 | st->delta_poc_s1_minus1[i] = bs_read_ue(b); 648 | st->used_by_curr_pic_s1_flag[i] = bs_read_u1(b); 649 | rps->m_used[i + st->num_negative_pics] = st->used_by_curr_pic_s1_flag[i]; 650 | } 651 | rps->m_numberOfPictures = rps->m_numberOfNegativePictures + rps->m_numberOfPositivePictures; 652 | } 653 | } 654 | 655 | 656 | static int getNumRpsCurrTempList(h265_slice_header_t *hrd) 657 | { 658 | int numRpsCurrTempList = 0; 659 | 660 | if (hrd->slice_type == H265_SH_SLICE_TYPE_I) 661 | { 662 | return 0; 663 | } 664 | if (hrd->m_pRPS == NULL) return 0; // tmp... 665 | // todo error 666 | for (int i = 0; 667 | i < hrd->m_pRPS->m_numberOfNegativePictures + hrd->m_pRPS->m_numberOfPositivePictures + hrd->m_pRPS->m_numberOfLongtermPictures; 668 | i++) 669 | { 670 | if (hrd->m_pRPS->m_used[i]) 671 | { 672 | numRpsCurrTempList++; 673 | } 674 | } 675 | 676 | return numRpsCurrTempList; 677 | } 678 | 679 | // ref_pic_lists_modification 680 | // 7.3.6.2 Reference picture list modification syntax 681 | void h265_read_ref_pic_lists_modification(bs_t* b, h265_slice_header_t* hrd) 682 | { 683 | hrd->ref_pic_lists_modification.ref_pic_list_modification_flag_l0 = bs_read_u1(b); 684 | if (hrd->ref_pic_lists_modification.ref_pic_list_modification_flag_l0) 685 | { 686 | int numRpsCurrTempList0 = getNumRpsCurrTempList(hrd); 687 | if (numRpsCurrTempList0 > 1) 688 | { 689 | int length = 1; 690 | numRpsCurrTempList0--; 691 | while (numRpsCurrTempList0 >>= 1) 692 | { 693 | length++; 694 | } 695 | for (int i = 0; i <= hrd->num_ref_idx_l0_active_minus1; i++) // 注意有等号,要注意边界 696 | { 697 | hrd->ref_pic_lists_modification.list_entry_l0[i] = bs_read_u(b, length); 698 | } 699 | } 700 | else 701 | { 702 | for (int i = 0; i <= hrd->num_ref_idx_l0_active_minus1; i ++) 703 | { 704 | hrd->ref_pic_lists_modification.list_entry_l0[i] = 0; 705 | } 706 | } 707 | } 708 | if (hrd->slice_type == H265_SH_SLICE_TYPE_B) 709 | { 710 | hrd->ref_pic_lists_modification.ref_pic_list_modification_flag_l1 = bs_read_u1(b); 711 | if (hrd->ref_pic_lists_modification.ref_pic_list_modification_flag_l1) 712 | { 713 | int numRpsCurrTempList1 = getNumRpsCurrTempList(hrd); 714 | if (numRpsCurrTempList1 > 1) 715 | { 716 | int length = 1; 717 | numRpsCurrTempList1--; 718 | while (numRpsCurrTempList1 >>= 1) 719 | { 720 | length++; 721 | } 722 | for (int i = 0; i <= hrd->num_ref_idx_l1_active_minus1; i++) // 注意有等号,要注意边界 723 | { 724 | hrd->ref_pic_lists_modification.list_entry_l1[i] = bs_read_u(b, length); 725 | } 726 | } 727 | else 728 | { 729 | for (int i = 0; i <= hrd->num_ref_idx_l1_active_minus1; i ++) 730 | { 731 | hrd->ref_pic_lists_modification.list_entry_l1[i] = 0; 732 | } 733 | } 734 | } 735 | } 736 | } 737 | 738 | // 7.3.6.3 Weighted prediction parameters syntax 739 | void h265_read_pred_weight_table(h265_stream_t* h, bs_t* b) 740 | { 741 | pred_weight_table_t* pwt = &h->sh->pred_weight_table; 742 | 743 | int l0_size = h->sh->num_ref_idx_l0_active_minus1+1; 744 | int l1_size = h->sh->num_ref_idx_l1_active_minus1+1; 745 | 746 | pwt->luma_weight_l0_flag.resize(l0_size); 747 | pwt->chroma_weight_l0_flag.resize(l0_size); 748 | pwt->delta_luma_weight_l0.resize(l0_size); 749 | pwt->luma_offset_l0.resize(l0_size); 750 | pwt->delta_chroma_weight_l0.resize(l0_size); 751 | pwt->delta_chroma_offset_l0.resize(l0_size); 752 | for (int j = 0; j < l0_size; j++) 753 | { 754 | pwt->delta_chroma_weight_l0[j].resize(2); 755 | pwt->delta_chroma_offset_l0[j].resize(2); 756 | } 757 | pwt->luma_weight_l1_flag.resize(l1_size); 758 | pwt->chroma_weight_l1_flag.resize(l1_size); 759 | pwt->delta_luma_weight_l1.resize(l1_size); 760 | pwt->luma_offset_l1.resize(l1_size); 761 | pwt->delta_chroma_weight_l1.resize(l1_size); 762 | pwt->delta_chroma_offset_l1.resize(l1_size); 763 | for (int j = 0; j < l1_size; j++) 764 | { 765 | pwt->delta_chroma_weight_l1[j].resize(2); 766 | pwt->delta_chroma_offset_l1[j].resize(2); 767 | } 768 | 769 | pwt->luma_log2_weight_denom = bs_read_ue(b); 770 | if (h->sps->chroma_format_idc != 0) 771 | { 772 | pwt->delta_chroma_log2_weight_denom = bs_read_se(b); 773 | } 774 | 775 | for (int i = 0; i <= h->sh->num_ref_idx_l0_active_minus1; i++) 776 | { 777 | pwt->luma_weight_l0_flag[i] = bs_read_u1(b); 778 | } 779 | if (h->sps->chroma_format_idc != 0) 780 | { 781 | for (int i = 0; i <= h->sh->num_ref_idx_l0_active_minus1; i++) 782 | { 783 | pwt->chroma_weight_l0_flag[i] = bs_read_u1(b); 784 | } 785 | } 786 | 787 | for (int i = 0; i <= h->sh->num_ref_idx_l0_active_minus1; i++) 788 | { 789 | if (pwt->luma_weight_l0_flag[i]) 790 | { 791 | pwt->delta_luma_weight_l0[i] = bs_read_se(b); 792 | pwt->luma_offset_l0[i] = bs_read_se(b); 793 | } 794 | if (pwt->chroma_weight_l0_flag[i]) 795 | { 796 | for (int j = 0; j < 2; j++) 797 | { 798 | pwt->delta_chroma_weight_l0[i][j] = bs_read_se(b); 799 | pwt->delta_chroma_offset_l0[i][j] = bs_read_se(b); 800 | } 801 | } 802 | } 803 | 804 | if (h->sh->slice_type == H265_SH_SLICE_TYPE_B) 805 | { 806 | for (int i = 0; i <= h->sh->num_ref_idx_l1_active_minus1; i++) 807 | { 808 | pwt->luma_weight_l1_flag[i] = bs_read_u1(b); 809 | } 810 | if (h->sps->chroma_format_idc != 0) 811 | { 812 | for (int i = 0; i <= h->sh->num_ref_idx_l1_active_minus1; i++) 813 | { 814 | pwt->chroma_weight_l1_flag[i] = bs_read_u1(b); 815 | } 816 | } 817 | for (int i = 0; i <= h->sh->num_ref_idx_l1_active_minus1; i++) 818 | { 819 | if (pwt->luma_weight_l1_flag[i]) 820 | { 821 | pwt->delta_luma_weight_l1[i] = bs_read_se(b); 822 | pwt->luma_offset_l1[i] = bs_read_se(b); 823 | } 824 | if (pwt->chroma_weight_l1_flag[i]) 825 | { 826 | for (int j = 0; j < 2; j++) 827 | { 828 | pwt->delta_chroma_weight_l1[i][j] = bs_read_se(b); 829 | pwt->delta_chroma_offset_l1[i][j] = bs_read_se(b); 830 | } 831 | } 832 | } 833 | } 834 | } 835 | 836 | void h265_read_vps_rbsp(h265_stream_t* h, bs_t* b) 837 | { 838 | int i = 0; 839 | int j = 0; 840 | 841 | int vps_video_parameter_set_id = bs_read_u(b, 4); 842 | // 选择正确的sps表 843 | h->vps = h->vps_table[vps_video_parameter_set_id]; 844 | h265_vps_t* vps = h->vps; 845 | memset(vps, 0, sizeof(h265_vps_t)); 846 | 847 | vps->vps_video_parameter_set_id = vps_video_parameter_set_id; 848 | vps->vps_base_layer_internal_flag = bs_read_u1(b); 849 | vps->vps_base_layer_available_flag = bs_read_u1(b); 850 | vps->vps_max_layers_minus1 = bs_read_u(b, 6); 851 | vps->vps_max_sub_layers_minus1 = bs_read_u(b, 3); 852 | vps->vps_temporal_id_nesting_flag = bs_read_u1(b); 853 | vps->vps_reserved_0xffff_16bits = bs_read_u(b, 16); 854 | 855 | // profile tier level... 856 | h265_read_ptl(&vps->ptl, b, 1, vps->vps_max_sub_layers_minus1); 857 | h->info->profile_idc = vps->ptl.general_profile_idc; 858 | h->info->level_idc = vps->ptl.general_level_idc; 859 | h->info->tier_idc = vps->ptl.general_tier_flag; 860 | 861 | vps->vps_sub_layer_ordering_info_present_flag = bs_read_u1(b); 862 | for (i = (vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers_minus1); 863 | i <= vps->vps_max_sub_layers_minus1; i++ ) 864 | { 865 | vps->vps_max_dec_pic_buffering_minus1[i] = bs_read_ue(b); 866 | vps->vps_max_num_reorder_pics[i] = bs_read_ue(b); 867 | vps->vps_max_latency_increase_plus1[i] = bs_read_ue(b); 868 | } 869 | vps->vps_max_layer_id = bs_read_u(b, 6); 870 | vps->vps_num_layer_sets_minus1 = bs_read_ue(b); 871 | vps->layer_id_included_flag.resize(vps->vps_num_layer_sets_minus1+1); 872 | for (unsigned int k = 0; k < vps->layer_id_included_flag.size(); k++) 873 | { 874 | vps->layer_id_included_flag[k].resize(vps->vps_max_layer_id); 875 | } 876 | for (i = 1; i <= vps->vps_num_layer_sets_minus1; i++) 877 | { 878 | vps->layer_id_included_flag[i].resize(vps->vps_num_layer_sets_minus1+1); 879 | } 880 | for (i = 1; i <= vps->vps_num_layer_sets_minus1; i++) 881 | { 882 | for (j = 0; j <= vps->vps_max_layer_id; j++) 883 | { 884 | vps->layer_id_included_flag[i][j] = bs_read_u1(b); 885 | } 886 | } 887 | vps->vps_timing_info_present_flag = bs_read_u1(b); 888 | if (vps->vps_timing_info_present_flag) 889 | { 890 | vps->vps_num_units_in_tick = bs_read_u(b, 32); 891 | vps->vps_time_scale = bs_read_u(b, 32); 892 | vps->vps_poc_proportional_to_timing_flag = bs_read_u1(b); 893 | if (vps->vps_poc_proportional_to_timing_flag) 894 | { 895 | vps->vps_num_ticks_poc_diff_one_minus1 = bs_read_ue(b); 896 | } 897 | vps->vps_num_hrd_parameters = bs_read_ue(b); 898 | vps->hrd_layer_set_idx.resize(vps->vps_num_hrd_parameters); 899 | vps->cprms_present_flag.resize(vps->vps_num_hrd_parameters); 900 | vps->hrd_layer_set_idx.resize(vps->vps_num_hrd_parameters); 901 | vps->cprms_present_flag.resize(vps->vps_num_hrd_parameters); 902 | for (i = 0; i < vps->vps_num_hrd_parameters; i++) 903 | { 904 | vps->hrd_layer_set_idx[i] = bs_read_ue(b); 905 | if (i > 0) 906 | { 907 | vps->cprms_present_flag[i] = bs_read_u1(b); 908 | } 909 | // hrd_parameters() 910 | h265_read_hrd_parameters(&(vps->hrd_parameters), b, vps->cprms_present_flag[i], vps->vps_max_sub_layers_minus1); 911 | } 912 | } 913 | vps->vps_extension_flag = bs_read_u1(b); 914 | if (vps->vps_extension_flag) 915 | { 916 | while (h265_more_rbsp_trailing_data(b)) 917 | { 918 | int sps_extension_data_flag = bs_read_u1(b); 919 | } 920 | } 921 | h265_read_rbsp_trailing_bits(b); 922 | } 923 | 924 | //7.3.2.1 Sequence parameter set RBSP syntax 925 | void h265_read_sps_rbsp(h265_stream_t* h, bs_t* b) 926 | { 927 | // NOTE 不能直接赋值给sps,因为还未知是哪一个sps。 928 | 929 | int sps_video_parameter_set_id = 0; 930 | int sps_max_sub_layers_minus1 = 0; 931 | int sps_temporal_id_nesting_flag = 0; 932 | int sps_seq_parameter_set_id = 0; 933 | profile_tier_level_t profile_tier_level; 934 | 935 | sps_video_parameter_set_id = bs_read_u(b, 4); 936 | sps_max_sub_layers_minus1 = bs_read_u(b, 3); 937 | sps_temporal_id_nesting_flag = bs_read_u1(b); 938 | 939 | // profile tier level... 940 | memset(&profile_tier_level, '\0', sizeof(profile_tier_level_t)); 941 | 942 | h265_read_ptl(&profile_tier_level, b, 1, sps_max_sub_layers_minus1); 943 | 944 | sps_seq_parameter_set_id = bs_read_ue(b); 945 | // 选择正确的sps表 946 | h->sps = h->sps_table[sps_seq_parameter_set_id]; 947 | h265_sps_t* sps = h->sps; 948 | memset(sps, 0, sizeof(h265_sps_t)); 949 | 950 | sps->sps_video_parameter_set_id = sps_video_parameter_set_id; 951 | sps->sps_max_sub_layers_minus1 = sps_max_sub_layers_minus1; 952 | sps->sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag; 953 | 954 | memcpy(&(sps->ptl), &profile_tier_level, sizeof(profile_tier_level_t)); // ptl 955 | 956 | sps->sps_seq_parameter_set_id = sps_seq_parameter_set_id; 957 | sps->chroma_format_idc = bs_read_ue(b); 958 | h->info->chroma_format_idc = sps->chroma_format_idc; 959 | if (sps->chroma_format_idc == 3) 960 | { 961 | sps->separate_colour_plane_flag = bs_read_u1(b); 962 | } 963 | sps->pic_width_in_luma_samples = bs_read_ue(b); 964 | sps->pic_height_in_luma_samples = bs_read_ue(b); 965 | 966 | h->info->width = sps->pic_width_in_luma_samples; 967 | h->info->height = sps->pic_height_in_luma_samples; 968 | 969 | sps->conformance_window_flag = bs_read_u1(b); 970 | if (sps->conformance_window_flag) 971 | { 972 | sps->conf_win_left_offset = bs_read_ue(b); 973 | sps->conf_win_right_offset = bs_read_ue(b); 974 | sps->conf_win_top_offset = bs_read_ue(b); 975 | sps->conf_win_bottom_offset = bs_read_ue(b); 976 | 977 | // calc width & height again... 978 | h->info->crop_left = sps->conf_win_left_offset; 979 | h->info->crop_right = sps->conf_win_right_offset; 980 | h->info->crop_top = sps->conf_win_top_offset; 981 | h->info->crop_bottom = sps->conf_win_bottom_offset; 982 | 983 | // 根据Table6-1及7.4.3.2.1重新计算宽、高 984 | // 注意:手册里加1,实际上不用 985 | // 参考:https://github.com/mbunkus/mkvtoolnix/issues/1152 986 | int sub_width_c = ((1 == sps->chroma_format_idc) || (2 == sps->chroma_format_idc)) && (0 == sps->separate_colour_plane_flag) ? 2 : 1; 987 | int sub_height_c = (1 == sps->chroma_format_idc) && (0 == sps->separate_colour_plane_flag) ? 2 : 1; 988 | h->info->width -= (sub_width_c*sps->conf_win_right_offset + sub_width_c*sps->conf_win_left_offset); 989 | h->info->height -= (sub_height_c*sps->conf_win_bottom_offset + sub_height_c*sps->conf_win_top_offset); 990 | } 991 | 992 | sps->bit_depth_luma_minus8 = bs_read_ue(b); 993 | sps->bit_depth_chroma_minus8 = bs_read_ue(b); 994 | 995 | // bit depth 996 | h->info->bit_depth_luma = sps->bit_depth_luma_minus8 + 8; 997 | h->info->bit_depth_chroma = sps->bit_depth_chroma_minus8 + 8; 998 | 999 | sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue(b); 1000 | 1001 | sps->sps_sub_layer_ordering_info_present_flag = bs_read_u1(b); 1002 | for (int i = (sps->sps_sub_layer_ordering_info_present_flag ? 0 : sps->sps_max_sub_layers_minus1); 1003 | i <= sps->sps_max_sub_layers_minus1; i++ ) 1004 | { 1005 | sps->sps_max_dec_pic_buffering_minus1[i] = bs_read_ue(b); 1006 | sps->sps_max_num_reorder_pics[i] = bs_read_ue(b); 1007 | sps->sps_max_latency_increase_plus1[i] = bs_read_ue(b); 1008 | } 1009 | 1010 | sps->log2_min_luma_coding_block_size_minus3 = bs_read_ue(b); 1011 | sps->log2_diff_max_min_luma_coding_block_size = bs_read_ue(b); 1012 | sps->log2_min_luma_transform_block_size_minus2 = bs_read_ue(b); 1013 | sps->log2_diff_max_min_luma_transform_block_size = bs_read_ue(b); 1014 | sps->max_transform_hierarchy_depth_inter = bs_read_ue(b); 1015 | sps->max_transform_hierarchy_depth_intra = bs_read_ue(b); 1016 | 1017 | sps->scaling_list_enabled_flag = bs_read_u1(b); 1018 | if (sps->scaling_list_enabled_flag) 1019 | { 1020 | sps->sps_scaling_list_data_present_flag = bs_read_u1(b); 1021 | if (sps->sps_scaling_list_data_present_flag) 1022 | { 1023 | // scaling_list_data() 1024 | h265_read_scaling_list(&(sps->scaling_list_data), b); 1025 | } 1026 | } 1027 | 1028 | sps->amp_enabled_flag = bs_read_u1(b); 1029 | sps->sample_adaptive_offset_enabled_flag = bs_read_u1(b); 1030 | sps->pcm_enabled_flag = bs_read_u1(b); 1031 | if (sps->pcm_enabled_flag) 1032 | { 1033 | sps->pcm_sample_bit_depth_luma_minus1 = bs_read_u(b, 4); 1034 | sps->pcm_sample_bit_depth_chroma_minus1 = bs_read_u(b, 4); 1035 | sps->log2_min_pcm_luma_coding_block_size_minus3 = bs_read_ue(b); 1036 | sps->log2_diff_max_min_pcm_luma_coding_block_size = bs_read_ue(b); 1037 | sps->pcm_loop_filter_disabled_flag = bs_read_u1(b); 1038 | } 1039 | 1040 | sps->num_short_term_ref_pic_sets = bs_read_ue(b); 1041 | // 根据num_short_term_ref_pic_sets创建数组 1042 | sps->st_ref_pic_set.resize(sps->num_short_term_ref_pic_sets); 1043 | sps->m_RPSList.resize(sps->num_short_term_ref_pic_sets); // 确定一共有多少个RPS列表 1044 | referencePictureSets_t* rps = NULL; 1045 | st_ref_pic_set_t* st = NULL; 1046 | for (int i = 0; i < sps->num_short_term_ref_pic_sets; i++) 1047 | { 1048 | st = &sps->st_ref_pic_set[i]; 1049 | rps = &sps->m_RPSList[i]; 1050 | h265_read_short_term_ref_pic_set(b, sps, st, rps, i); 1051 | } 1052 | 1053 | sps->long_term_ref_pics_present_flag = bs_read_u1(b); 1054 | if (sps->long_term_ref_pics_present_flag) 1055 | { 1056 | sps->num_long_term_ref_pics_sps = bs_read_ue(b); 1057 | sps->lt_ref_pic_poc_lsb_sps.resize(sps->num_long_term_ref_pics_sps); 1058 | sps->used_by_curr_pic_lt_sps_flag.resize(sps->num_long_term_ref_pics_sps); 1059 | for (int i = 0; i < sps->num_long_term_ref_pics_sps; i++) 1060 | { 1061 | sps->lt_ref_pic_poc_lsb_sps_bytes = sps->log2_max_pic_order_cnt_lsb_minus4 + 4; 1062 | sps->lt_ref_pic_poc_lsb_sps[i] = bs_read_u(b, sps->log2_max_pic_order_cnt_lsb_minus4 + 4); // u(v) 1063 | sps->used_by_curr_pic_lt_sps_flag[i] = bs_read_u1(b); 1064 | } 1065 | } 1066 | 1067 | sps->sps_temporal_mvp_enabled_flag = bs_read_u1(b); 1068 | sps->strong_intra_smoothing_enabled_flag = bs_read_u1(b); 1069 | sps->vui_parameters_present_flag = bs_read_u1(b); 1070 | if (sps->vui_parameters_present_flag) 1071 | { 1072 | h265_read_vui_parameters(&(sps->vui), b, sps->sps_max_sub_layers_minus1); 1073 | // calc fps 1074 | if (sps->vui.vui_num_units_in_tick != 0) 1075 | h->info->max_framerate = (float)(sps->vui.vui_time_scale) / (float)(sps->vui.vui_num_units_in_tick); 1076 | } 1077 | 1078 | sps->sps_extension_present_flag = bs_read_u1(b); 1079 | if (sps->sps_extension_present_flag) 1080 | { 1081 | sps->sps_range_extension_flag = bs_read_u1(b); 1082 | sps->sps_multilayer_extension_flag = bs_read_u1(b); 1083 | sps->sps_3d_extension_flag = bs_read_u1(b); 1084 | sps->sps_extension_5bits = bs_read_u(b, 5); 1085 | } 1086 | 1087 | if (sps->sps_range_extension_flag) 1088 | { 1089 | sps->sps_range_extension.transform_skip_rotation_enabled_flag = bs_read_u1(b); 1090 | sps->sps_range_extension.transform_skip_context_enabled_flag = bs_read_u1(b); 1091 | sps->sps_range_extension.implicit_rdpcm_enabled_flag = bs_read_u1(b); 1092 | sps->sps_range_extension.explicit_rdpcm_enabled_flag = bs_read_u1(b); 1093 | sps->sps_range_extension.extended_precision_processing_flag = bs_read_u1(b); 1094 | sps->sps_range_extension.intra_smoothing_disabled_flag = bs_read_u1(b); 1095 | sps->sps_range_extension.high_precision_offsets_enabled_flag = bs_read_u1(b); 1096 | sps->sps_range_extension.persistent_rice_adaptation_enabled_flag = bs_read_u1(b); 1097 | sps->sps_range_extension.cabac_bypass_alignment_enabled_flag = bs_read_u1(b); 1098 | } 1099 | if (sps->sps_multilayer_extension_flag) 1100 | { 1101 | // sps_multilayer_extension() 1102 | sps->inter_view_mv_vert_constraint_flag = bs_read_u1(b); 1103 | } 1104 | if (sps->sps_3d_extension_flag) 1105 | { 1106 | // todo sps_3d_extension( ) 1107 | } 1108 | if (sps->sps_extension_5bits) 1109 | { 1110 | while (h265_more_rbsp_trailing_data(b)) 1111 | { 1112 | int sps_extension_data_flag = bs_read_u1(b); 1113 | } 1114 | } 1115 | h265_read_rbsp_trailing_bits(b); 1116 | } 1117 | 1118 | 1119 | //7.3.2.2 Picture parameter set RBSP syntax 1120 | void h265_read_pps_rbsp(h265_stream_t* h, bs_t* b) 1121 | { 1122 | int pps_pic_parameter_set_id = bs_read_ue(b); // get id 1123 | 1124 | h265_pps_t* pps = h->pps = h->pps_table[pps_pic_parameter_set_id] ; 1125 | 1126 | memset(pps, 0, sizeof(h265_pps_t)); 1127 | 1128 | pps->pps_pic_parameter_set_id = pps_pic_parameter_set_id; 1129 | pps->pps_seq_parameter_set_id = bs_read_ue(b); 1130 | pps->dependent_slice_segments_enabled_flag = bs_read_u1(b); 1131 | pps->output_flag_present_flag = bs_read_u1(b); 1132 | pps->num_extra_slice_header_bits = bs_read_u(b, 3); 1133 | pps->sign_data_hiding_enabled_flag = bs_read_u1(b); 1134 | pps->cabac_init_present_flag = bs_read_u1(b); 1135 | pps->num_ref_idx_l0_default_active_minus1 = bs_read_ue(b); 1136 | pps->num_ref_idx_l1_default_active_minus1 = bs_read_ue(b); 1137 | pps->init_qp_minus26 = bs_read_se(b); 1138 | pps->constrained_intra_pred_flag = bs_read_u1(b); 1139 | pps->transform_skip_enabled_flag = bs_read_u1(b); 1140 | pps->cu_qp_delta_enabled_flag = bs_read_u1(b); 1141 | if (pps->cu_qp_delta_enabled_flag) 1142 | { 1143 | pps->diff_cu_qp_delta_depth = bs_read_ue(b); 1144 | } 1145 | 1146 | pps->pps_cb_qp_offset = bs_read_se(b); 1147 | pps->pps_cr_qp_offset = bs_read_se(b); 1148 | pps->pps_slice_chroma_qp_offsets_present_flag = bs_read_u1(b); 1149 | pps->weighted_pred_flag = bs_read_u1(b); 1150 | pps->weighted_bipred_flag = bs_read_u1(b); 1151 | pps->transquant_bypass_enabled_flag = bs_read_u1(b); 1152 | pps->tiles_enabled_flag = bs_read_u1(b); 1153 | pps->entropy_coding_sync_enabled_flag = bs_read_u1(b); 1154 | h->info->encoding_type = pps->entropy_coding_sync_enabled_flag; 1155 | 1156 | if (pps->tiles_enabled_flag) 1157 | { 1158 | pps->num_tile_columns_minus1 = bs_read_ue(b); 1159 | pps->num_tile_rows_minus1 = bs_read_ue(b); 1160 | pps->uniform_spacing_flag = bs_read_u1(b); 1161 | if (!pps->uniform_spacing_flag) 1162 | { 1163 | pps->column_width_minus1.resize(pps->num_tile_columns_minus1); 1164 | pps->row_height_minus1.resize(pps->num_tile_rows_minus1); 1165 | for (int i = 0; i < pps->num_tile_columns_minus1; i++) 1166 | { 1167 | pps->column_width_minus1[i] = bs_read_ue(b); 1168 | } 1169 | for (int i = 0; i < pps->num_tile_rows_minus1; i++) 1170 | { 1171 | pps->row_height_minus1[i] = bs_read_ue(b); 1172 | } 1173 | } 1174 | pps->loop_filter_across_tiles_enabled_flag = bs_read_u1(b); 1175 | } 1176 | 1177 | pps->pps_loop_filter_across_slices_enabled_flag = bs_read_u1(b); 1178 | pps->deblocking_filter_control_present_flag = bs_read_u1(b); 1179 | if (pps->deblocking_filter_control_present_flag) 1180 | { 1181 | pps->deblocking_filter_override_enabled_flag = bs_read_u1(b); 1182 | pps->pps_deblocking_filter_disabled_flag = bs_read_u1(b); 1183 | if (pps->pps_deblocking_filter_disabled_flag) 1184 | { 1185 | pps->pps_beta_offset_div2 = bs_read_se(b); 1186 | pps->pps_tc_offset_div2 = bs_read_se(b); 1187 | } 1188 | } 1189 | 1190 | pps->pps_scaling_list_data_present_flag = bs_read_u1(b); 1191 | if (pps->pps_scaling_list_data_present_flag) 1192 | { 1193 | // scaling_list_data() 1194 | h265_read_scaling_list(&(pps->scaling_list_data), b); 1195 | } 1196 | 1197 | pps->lists_modification_present_flag = bs_read_u1(b); 1198 | pps->log2_parallel_merge_level_minus2 = bs_read_ue(b); 1199 | pps->slice_segment_header_extension_present_flag = bs_read_u1(b); 1200 | pps->pps_extension_present_flag = bs_read_u1(b); 1201 | if (pps->pps_extension_present_flag) 1202 | { 1203 | pps->pps_range_extension_flag = bs_read_u1(b); 1204 | pps->pps_multilayer_extension_flag = bs_read_u1(b); 1205 | pps->pps_3d_extension_flag = bs_read_u1(b); 1206 | pps->pps_extension_5bits = bs_read_u(b, 5); 1207 | } 1208 | if (pps->pps_range_extension_flag) 1209 | { 1210 | if (pps->transform_skip_enabled_flag) 1211 | { 1212 | pps->pps_range_extension.log2_max_transform_skip_block_size_minus2 = bs_read_ue(b); 1213 | } 1214 | pps->pps_range_extension.cross_component_prediction_enabled_flag = bs_read_u1(b); 1215 | pps->pps_range_extension.chroma_qp_offset_list_enabled_flag = bs_read_u1(b); 1216 | if (pps->pps_range_extension.chroma_qp_offset_list_enabled_flag) 1217 | { 1218 | pps->pps_range_extension.diff_cu_chroma_qp_offset_depth = bs_read_ue(b); 1219 | pps->pps_range_extension.chroma_qp_offset_list_len_minus1 = bs_read_ue(b); 1220 | pps->pps_range_extension.cb_qp_offset_list.resize(pps->pps_range_extension.chroma_qp_offset_list_len_minus1); 1221 | pps->pps_range_extension.cr_qp_offset_list.resize(pps->pps_range_extension.chroma_qp_offset_list_len_minus1); 1222 | for (int i = 0; i < pps->pps_range_extension.chroma_qp_offset_list_len_minus1; i++) 1223 | { 1224 | pps->pps_range_extension.cb_qp_offset_list[i] = bs_read_se(b); 1225 | pps->pps_range_extension.cr_qp_offset_list[i] = bs_read_se(b); 1226 | } 1227 | } 1228 | pps->pps_range_extension.log2_sao_offset_scale_luma = bs_read_ue(b); 1229 | pps->pps_range_extension.log2_sao_offset_scale_chroma = bs_read_ue(b); 1230 | } 1231 | if (pps->pps_multilayer_extension_flag) 1232 | { 1233 | // todo sps_multilayer_extension( ) 1234 | } 1235 | if (pps->pps_3d_extension_flag) 1236 | { 1237 | // todo sps_3d_extension( ) 1238 | } 1239 | if (pps->pps_extension_5bits) 1240 | { 1241 | while (h265_more_rbsp_trailing_data(b)) 1242 | { 1243 | int pps_extension_data_flag = bs_read_u1(b); 1244 | } 1245 | } 1246 | h265_read_rbsp_trailing_bits(b); 1247 | } 1248 | 1249 | //7.3.6.1 General slice segment header syntax 1250 | void h265_read_slice_header(h265_stream_t* h, bs_t* b) 1251 | { 1252 | h265_slice_header_t* hrd = h->sh; 1253 | h265_sps_t* sps = NULL; 1254 | h265_pps_t* pps = NULL; 1255 | int nal_unit_type = h->nal->nal_unit_type; 1256 | int read_slice_type = hrd->read_slice_type; 1257 | 1258 | memset(hrd, 0, sizeof(h265_slice_header_t)); 1259 | 1260 | hrd->first_slice_segment_in_pic_flag = bs_read_u1(b); 1261 | 1262 | if (nal_unit_type >= NAL_UNIT_CODED_SLICE_BLA_W_LP && nal_unit_type <= NAL_UNIT_RESERVED_IRAP_VCL23) 1263 | { 1264 | hrd->no_output_of_prior_pics_flag = bs_read_u1(b); 1265 | } 1266 | hrd->slice_pic_parameter_set_id = bs_read_ue(b); 1267 | 1268 | pps = h->pps = h->pps_table[hrd->slice_pic_parameter_set_id]; 1269 | sps = h->sps = h->sps_table[pps->pps_seq_parameter_set_id]; 1270 | 1271 | hrd->dependent_slice_segment_flag = 0; 1272 | if (!hrd->first_slice_segment_in_pic_flag) 1273 | { 1274 | if (pps->dependent_slice_segments_enabled_flag) 1275 | { 1276 | hrd->dependent_slice_segment_flag = bs_read_u1(b); 1277 | } 1278 | int maxCUWidth = 1<<(sps->log2_min_luma_coding_block_size_minus3+3 + sps->log2_diff_max_min_luma_coding_block_size); 1279 | int maxCUHeight = maxCUWidth;// to check 1280 | int numCTUs = ((sps->pic_width_in_luma_samples+maxCUWidth-1)/maxCUWidth)*((sps->pic_height_in_luma_samples+maxCUHeight-1)/maxCUHeight);; 1281 | int bitsSliceSegmentAddress = 0; 1282 | while(numCTUs>(1<slice_segment_address_bytes = bitsSliceSegmentAddress; 1287 | hrd->slice_segment_address = bs_read_u(b, bitsSliceSegmentAddress); // u(v) 1288 | } 1289 | if (!hrd->dependent_slice_segment_flag) 1290 | { 1291 | hrd->slice_reserved_flag.resize(pps->num_extra_slice_header_bits); 1292 | for (int i = 0; i < pps->num_extra_slice_header_bits; i++) 1293 | { 1294 | hrd->slice_reserved_flag[i] = bs_read_u1(b); 1295 | } 1296 | hrd->slice_type = bs_read_ue(b); 1297 | 1298 | // we need slice type only 1299 | if (read_slice_type) return; 1300 | 1301 | if (pps->output_flag_present_flag) 1302 | { 1303 | hrd->pic_output_flag = bs_read_u1(b); 1304 | } 1305 | if (sps->separate_colour_plane_flag == 1) 1306 | { 1307 | hrd->colour_plane_id = bs_read_u(b, 2); 1308 | } 1309 | // IDR 1310 | if (nal_unit_type == NAL_UNIT_CODED_SLICE_IDR_W_RADL || nal_unit_type == NAL_UNIT_CODED_SLICE_IDR_N_LP) 1311 | { 1312 | referencePictureSets_t* rps = &hrd->m_localRPS; 1313 | memset(rps, '\0', sizeof(referencePictureSets_t)); 1314 | rps->m_numberOfPictures = 0; 1315 | rps->m_numberOfNegativePictures = 0; 1316 | rps->m_numberOfPositivePictures = 0; 1317 | hrd->m_pRPS = rps; 1318 | } 1319 | //if (nal_unit_type != NAL_UNIT_CODED_SLICE_IDR_W_RADL && nal_unit_type != NAL_UNIT_CODED_SLICE_IDR_N_LP) 1320 | else 1321 | { 1322 | hrd->slice_pic_order_cnt_lsb_bytes = sps->log2_max_pic_order_cnt_lsb_minus4 + 4; 1323 | hrd->slice_pic_order_cnt_lsb = bs_read_u(b, hrd->slice_pic_order_cnt_lsb_bytes); // poc v(u) 1324 | hrd->short_term_ref_pic_set_sps_flag = bs_read_u1(b); 1325 | if (!hrd->short_term_ref_pic_set_sps_flag) 1326 | { 1327 | referencePictureSets_t* rps = &hrd->m_localRPS; 1328 | hrd->m_pRPS = &hrd->m_localRPS; 1329 | // error here 1330 | // st_ref_pic_set(num_short_term_ref_pic_sets) 1331 | h265_read_short_term_ref_pic_set(b, sps, &hrd->st_ref_pic_set, rps, sps->num_short_term_ref_pic_sets); 1332 | } 1333 | // sps->num_short_term_ref_pic_set实际等于ssps->m_RPSList.size() 下同 1334 | else if (sps->num_short_term_ref_pic_sets > 1) 1335 | { 1336 | uint32_t numBits = 0; 1337 | while ((1 << numBits) < sps->num_short_term_ref_pic_sets) 1338 | { 1339 | numBits++; 1340 | } 1341 | if (numBits) 1342 | { 1343 | hrd->short_term_ref_pic_set_idx_bytes = numBits; 1344 | hrd->short_term_ref_pic_set_idx = bs_read_u(b, numBits); 1345 | } 1346 | else 1347 | { 1348 | hrd->short_term_ref_pic_set_idx = 0; 1349 | } 1350 | } 1351 | if (sps->long_term_ref_pics_present_flag) 1352 | { 1353 | if (sps->num_long_term_ref_pics_sps > 0) 1354 | { 1355 | hrd->num_long_term_sps = bs_read_ue(b); 1356 | } 1357 | uint32_t numLtrpInSPS = 0; 1358 | while (sps->num_long_term_ref_pics_sps > (1 << numLtrpInSPS)) 1359 | { 1360 | numLtrpInSPS++; 1361 | } 1362 | hrd->num_long_term_pics = bs_read_ue(b); 1363 | 1364 | int cnt = hrd->num_long_term_sps + hrd->num_long_term_pics; 1365 | hrd->lt_idx_sps.resize(cnt); 1366 | hrd->poc_lsb_lt.resize(cnt); 1367 | hrd->used_by_curr_pic_lt_flag.resize(cnt); 1368 | hrd->delta_poc_msb_present_flag.resize(cnt); 1369 | hrd->delta_poc_msb_cycle_lt.resize(cnt); 1370 | for (int i = 0; i < cnt; i++) 1371 | { 1372 | if (i < hrd->num_long_term_sps) 1373 | { 1374 | //if (sps->num_long_term_ref_pics_sps > 1) 1375 | // to confirm... 1376 | if (numLtrpInSPS > 0) 1377 | { 1378 | hrd->lt_idx_sps[i] = bs_read_u(b, numLtrpInSPS); // u(v) 1379 | } 1380 | } 1381 | else 1382 | { 1383 | hrd->poc_lsb_lt[i] = bs_read_u(b, sps->log2_max_pic_order_cnt_lsb_minus4+4); 1384 | hrd->used_by_curr_pic_lt_flag[i] = bs_read_u1(b); 1385 | } 1386 | hrd->delta_poc_msb_present_flag[i] = bs_read_u1(b); 1387 | if(hrd->delta_poc_msb_present_flag[i]) 1388 | { 1389 | hrd->delta_poc_msb_cycle_lt[i] = bs_read_ue(b); 1390 | } 1391 | } 1392 | } 1393 | if(sps->sps_temporal_mvp_enabled_flag) 1394 | { 1395 | hrd->slice_temporal_mvp_enabled_flag = bs_read_u1(b); 1396 | } 1397 | } 1398 | if(sps->sample_adaptive_offset_enabled_flag) 1399 | { 1400 | hrd->slice_sao_luma_flag = bs_read_u1(b); 1401 | bool ChromaArrayType = (sps->chroma_format_idc != CHROMA_400); 1402 | if (ChromaArrayType != 0) 1403 | { 1404 | hrd->slice_sao_chroma_flag = bs_read_u1(b); 1405 | } 1406 | } 1407 | if (hrd->slice_type == H265_SH_SLICE_TYPE_P || hrd->slice_type == H265_SH_SLICE_TYPE_B) 1408 | { 1409 | hrd->num_ref_idx_active_override_flag = bs_read_u1(b); 1410 | if (hrd->num_ref_idx_active_override_flag) 1411 | { 1412 | hrd->num_ref_idx_l0_active_minus1 = bs_read_ue(b); 1413 | if (hrd->slice_type == H265_SH_SLICE_TYPE_B) 1414 | { 1415 | hrd->num_ref_idx_l1_active_minus1 = bs_read_ue(b); 1416 | } 1417 | } 1418 | // to confirm... 1419 | int tmp = 0; 1420 | int NumPicTotalCurr = getNumRpsCurrTempList(hrd); 1421 | if(pps->lists_modification_present_flag && NumPicTotalCurr > 1) 1422 | { 1423 | h265_read_ref_pic_lists_modification(b, hrd); 1424 | } 1425 | if (hrd->slice_type == H265_SH_SLICE_TYPE_B) 1426 | { 1427 | hrd->mvd_l1_zero_flag = bs_read_u1(b); 1428 | } 1429 | if (pps->cabac_init_present_flag) 1430 | { 1431 | hrd->cabac_init_flag = bs_read_u1(b); 1432 | } 1433 | if (hrd->slice_temporal_mvp_enabled_flag) 1434 | { 1435 | if (hrd->slice_type == H265_SH_SLICE_TYPE_B) 1436 | { 1437 | hrd->collocated_from_l0_flag = bs_read_u1(b); 1438 | } 1439 | if ((hrd->collocated_from_l0_flag && hrd->num_ref_idx_l0_active_minus1 > 0) || 1440 | (!hrd->collocated_from_l0_flag && hrd->num_ref_idx_l1_active_minus1 > 0)) 1441 | { 1442 | hrd->collocated_ref_idx = bs_read_ue(b); 1443 | } 1444 | } 1445 | if ((pps->weighted_pred_flag && hrd->slice_type == H265_SH_SLICE_TYPE_P) || 1446 | (pps->weighted_bipred_flag && hrd->slice_type == H265_SH_SLICE_TYPE_B)) 1447 | { 1448 | h265_read_pred_weight_table(h, b); 1449 | } 1450 | hrd->five_minus_max_num_merge_cand = bs_read_ue(b); 1451 | } // end of slice_type P || B 1452 | hrd->slice_qp_delta = bs_read_se(b); 1453 | if (pps->pps_slice_chroma_qp_offsets_present_flag) 1454 | { 1455 | hrd->slice_cb_qp_offset = bs_read_se(b); 1456 | hrd->slice_cr_qp_offset = bs_read_se(b); 1457 | } 1458 | if (pps->pps_range_extension.chroma_qp_offset_list_enabled_flag) 1459 | { 1460 | hrd->cu_chroma_qp_offset_enabled_flag = bs_read_u1(b); 1461 | } 1462 | if (pps->deblocking_filter_override_enabled_flag) 1463 | { 1464 | hrd->deblocking_filter_override_flag = bs_read_u1(b); 1465 | } 1466 | if (hrd->deblocking_filter_override_flag) 1467 | { 1468 | hrd->slice_deblocking_filter_disabled_flag = bs_read_u1(b); 1469 | if (!hrd->slice_deblocking_filter_disabled_flag) 1470 | { 1471 | hrd->slice_beta_offset_div2 = bs_read_se(b); 1472 | hrd->slice_tc_offset_div2 = bs_read_se(b); 1473 | } 1474 | } 1475 | if (pps-> pps_loop_filter_across_slices_enabled_flag && 1476 | (hrd->slice_sao_luma_flag || hrd->slice_sao_chroma_flag || 1477 | !hrd->slice_deblocking_filter_disabled_flag)) 1478 | { 1479 | hrd->slice_loop_filter_across_slices_enabled_flag = bs_read_u1(b); 1480 | } 1481 | } // end of dependent_slice_segment_flag 1482 | 1483 | if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) 1484 | { 1485 | hrd->num_entry_point_offsets = bs_read_ue(b); 1486 | if (hrd->num_entry_point_offsets > 0) 1487 | { 1488 | hrd->offset_len_minus1 = bs_read_ue(b); 1489 | hrd->entry_point_offset_minus1_bytes = hrd->offset_len_minus1+1; 1490 | hrd->entry_point_offset_minus1.resize(hrd->num_entry_point_offsets); 1491 | // error 1492 | for (int i = 0; i < hrd->num_entry_point_offsets; i++) 1493 | { 1494 | // to confirm 1495 | // entry_point_offset_minus1为u(u),长度在offset_len_minus1的值加上1,见上 1496 | hrd->entry_point_offset_minus1[i] = bs_read_u(b, hrd->entry_point_offset_minus1_bytes); // u(v) 1497 | } 1498 | } 1499 | } 1500 | if (pps->slice_segment_header_extension_present_flag) 1501 | { 1502 | hrd->slice_segment_header_extension_length = bs_read_ue(b); 1503 | hrd->slice_segment_header_extension_data_byte.resize(hrd->slice_segment_header_extension_length); 1504 | for (int i = 0; i < hrd->slice_segment_header_extension_length; i++) 1505 | { 1506 | hrd->slice_segment_header_extension_data_byte[i] = bs_read_u8(b); 1507 | } 1508 | } 1509 | // byte_alignment() 1510 | } 1511 | 1512 | void h265_read_slice_layer_rbsp(h265_stream_t* h, bs_t* b) 1513 | { 1514 | h265_read_slice_header(h, b); 1515 | #if 0 1516 | 1517 | slice_data_rbsp_t* slice_data = h->slice_data; 1518 | 1519 | if ( slice_data != NULL ) 1520 | { 1521 | if ( slice_data->rbsp_buf != NULL ) free( slice_data->rbsp_buf ); 1522 | uint8_t *sptr = b->p + (!!b->bits_left); // CABAC-specific: skip alignment bits, if there are any 1523 | slice_data->rbsp_size = b->end - sptr; 1524 | 1525 | slice_data->rbsp_buf = (uint8_t*)malloc(slice_data->rbsp_size); 1526 | memcpy( slice_data->rbsp_buf, sptr, slice_data->rbsp_size ); 1527 | // ugly hack: since next NALU starts at byte border, we are going to be padded by trailing_bits; 1528 | return; 1529 | } 1530 | 1531 | // FIXME should read or skip data 1532 | //slice_data( ); /* all categories of slice_data( ) syntax */ 1533 | read_rbsp_slice_trailing_bits(h, b); 1534 | #endif 1535 | } 1536 | 1537 | //7.3.2.5 Access unit delimiter RBSP syntax 1538 | void h265_read_aud_rbsp(h265_stream_t* h, bs_t* b) 1539 | { 1540 | h->aud->pic_type = bs_read_u(b,3); 1541 | h265_read_rbsp_trailing_bits(b); 1542 | } 1543 | 1544 | //7.3.2.6 End of sequence RBSP syntax 1545 | void h265_read_end_of_seq_rbsp(h265_stream_t* h, bs_t* b) 1546 | { 1547 | } 1548 | 1549 | //7.3.2.7 End of stream RBSP syntax 1550 | void h265_read_end_of_stream_rbsp(h265_stream_t* h, bs_t* b) 1551 | { 1552 | } 1553 | 1554 | int h265_more_rbsp_data(bs_t* b) 1555 | { 1556 | if ( bs_eof(b) ) { return 0; } 1557 | if ( bs_peek_u1(b) == 1 ) { return 0; } // if next bit is 1, we've reached the stop bit 1558 | return 1; 1559 | } 1560 | 1561 | int h265_more_rbsp_trailing_data(bs_t* b) { return !bs_eof(b); } 1562 | 1563 | int __read_ff_coded_number(bs_t* b) 1564 | { 1565 | int n1 = 0; 1566 | int n2; 1567 | do 1568 | { 1569 | n2 = bs_read_u8(b); 1570 | n1 += n2; 1571 | } while (n2 == 0xff); 1572 | return n1; 1573 | } 1574 | 1575 | void h265_read_sei(h265_stream_t* h, bs_t* b) 1576 | { 1577 | h->sei->payloadType = __read_ff_coded_number(b); 1578 | h->sei->payloadSize = __read_ff_coded_number(b); 1579 | h265_read_sei_payload(h, b, h->sei->payloadType, h->sei->payloadSize); 1580 | h265_read_rbsp_trailing_bits(b); 1581 | } 1582 | 1583 | //7.3.2.4 Supplemental enhancement information RBSP syntax 1584 | void h265_read_sei_rbsp(h265_stream_t* h, bs_t* b) 1585 | { 1586 | //return; 1587 | for (int i = 0; i < h->num_seis; i++) 1588 | { 1589 | h265_sei_free(h->seis[i]); 1590 | } 1591 | 1592 | h->num_seis = 0; 1593 | do { 1594 | h->num_seis++; 1595 | h->seis = (h265_sei_t**)realloc(h->seis, h->num_seis * sizeof(sei_t*)); 1596 | h->seis[h->num_seis - 1] = h265_sei_new(); 1597 | h->sei = h->seis[h->num_seis - 1]; 1598 | h265_read_sei(h, b); 1599 | } while(h265_more_rbsp_data(b)); 1600 | 1601 | h265_more_rbsp_trailing_data(b); 1602 | } 1603 | 1604 | //7.3.2.10 RBSP slice trailing bits syntax 1605 | // 与h.264略有不同 1606 | void h265_read_rbsp_slice_trailing_bits(bs_t* b) 1607 | { 1608 | h265_read_rbsp_trailing_bits(b); 1609 | int cabac_zero_word; 1610 | while( h265_more_rbsp_trailing_data(b) ) 1611 | { 1612 | cabac_zero_word = bs_read_f(b,16); // equal to 0x0000 1613 | } 1614 | } 1615 | 1616 | //7.3.2.11 RBSP trailing bits syntax 1617 | void h265_read_rbsp_trailing_bits(bs_t* b) 1618 | { 1619 | int rbsp_stop_one_bit = bs_read_u1( b ); // equal to 1 1620 | 1621 | while( !bs_byte_aligned(b) ) 1622 | { 1623 | int rbsp_alignment_zero_bit = bs_read_u1( b ); // equal to 0 7 bits 1624 | } 1625 | } 1626 | -------------------------------------------------------------------------------- /h265_stream.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H265_STREAM_H 6 | #define ZLMEDIAKIT_H265_STREAM_H 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "bs.h" 14 | #include "h264_stream.h" // for nal_to_rbsp, etc... 15 | 16 | #ifndef min 17 | #define min(a, b) (((a) < (b)) ? (a) : (b)) 18 | #endif 19 | 20 | #include 21 | using std::vector; 22 | /** 23 | Network Abstraction Layer (NAL) unit 24 | @see 7.3.1 NAL unit syntax 25 | */ 26 | typedef struct 27 | { 28 | int forbidden_zero_bit; 29 | int nal_unit_type; 30 | int nuh_layer_id; 31 | int nuh_temporal_id_plus1; 32 | 33 | void* parsed; // FIXME 34 | int sizeof_parsed; 35 | 36 | //uint8_t* rbsp_buf; 37 | //int rbsp_size; 38 | } h265_nal_t; 39 | 40 | typedef struct 41 | { 42 | int payloadType; 43 | int payloadSize; 44 | uint8_t* payload; 45 | } h265_sei_t; 46 | 47 | /** 48 | Profile, tier and level 49 | @see 7.3.3 Profile, tier and level syntax 50 | */ 51 | typedef struct 52 | { 53 | uint8_t general_profile_space; 54 | uint8_t general_tier_flag; 55 | uint8_t general_profile_idc; 56 | uint8_t general_profile_compatibility_flag[32]; 57 | uint8_t general_progressive_source_flag; 58 | uint8_t general_interlaced_source_flag; 59 | uint8_t general_non_packed_constraint_flag; 60 | uint8_t general_frame_only_constraint_flag; 61 | uint8_t general_max_12bit_constraint_flag; 62 | uint8_t general_max_10bit_constraint_flag; 63 | uint8_t general_max_8bit_constraint_flag; 64 | uint8_t general_max_422chroma_constraint_flag; 65 | uint8_t general_max_420chroma_constraint_flag; 66 | uint8_t general_max_monochrome_constraint_flag; 67 | uint8_t general_intra_constraint_flag; 68 | uint8_t general_one_picture_only_constraint_flag; 69 | uint8_t general_lower_bit_rate_constraint_flag; 70 | uint64_t general_reserved_zero_34bits; // todo 71 | uint64_t general_reserved_zero_43bits; // todo 72 | uint8_t general_inbld_flag; 73 | uint8_t general_reserved_zero_bit; 74 | uint8_t general_level_idc; 75 | vector sub_layer_profile_present_flag; 76 | vector sub_layer_level_present_flag; 77 | uint8_t reserved_zero_2bits[8]; 78 | vector sub_layer_profile_space; 79 | vector sub_layer_tier_flag; 80 | vector sub_layer_profile_idc; 81 | vector > sub_layer_profile_compatibility_flag; 82 | vector sub_layer_progressive_source_flag; 83 | vector sub_layer_interlaced_source_flag; 84 | vector sub_layer_non_packed_constraint_flag; 85 | vector sub_layer_frame_only_constraint_flag; 86 | vector sub_layer_max_12bit_constraint_flag; 87 | vector sub_layer_max_10bit_constraint_flag; 88 | vector sub_layer_max_8bit_constraint_flag; 89 | vector sub_layer_max_422chroma_constraint_flag; 90 | vector sub_layer_max_420chroma_constraint_flag; 91 | vector sub_layer_max_monochrome_constraint_flag; 92 | vector sub_layer_intra_constraint_flag; 93 | vector sub_layer_one_picture_only_constraint_flag; 94 | vector sub_layer_lower_bit_rate_constraint_flag; 95 | vector sub_layer_reserved_zero_34bits; 96 | vector sub_layer_reserved_zero_43bits; 97 | vector sub_layer_inbld_flag; 98 | vector sub_layer_reserved_zero_bit; 99 | vector sub_layer_level_idc; 100 | 101 | } profile_tier_level_t; 102 | 103 | typedef struct 104 | { 105 | vector bit_rate_value_minus1; 106 | vector cpb_size_value_minus1; 107 | vector cpb_size_du_value_minus1; 108 | vector bit_rate_du_value_minus1; 109 | vector cbr_flag; 110 | } sub_layer_hrd_parameters_t; 111 | 112 | /** 113 | E.2.2 HRD parameters syntax 114 | */ 115 | typedef struct 116 | { 117 | uint8_t nal_hrd_parameters_present_flag; 118 | uint8_t vcl_hrd_parameters_present_flag; 119 | uint8_t sub_pic_hrd_params_present_flag; 120 | uint8_t tick_divisor_minus2; 121 | uint8_t du_cpb_removal_delay_increment_length_minus1; 122 | uint8_t sub_pic_cpb_params_in_pic_timing_sei_flag; 123 | uint8_t dpb_output_delay_du_length_minus1; 124 | uint8_t bit_rate_scale; 125 | uint8_t cpb_size_scale; 126 | uint8_t cpb_size_du_scale; 127 | uint8_t initial_cpb_removal_delay_length_minus1; 128 | uint8_t au_cpb_removal_delay_length_minus1; 129 | uint8_t dpb_output_delay_length_minus1; 130 | vector fixed_pic_rate_general_flag; 131 | vector fixed_pic_rate_within_cvs_flag; 132 | vector elemental_duration_in_tc_minus1; 133 | vector low_delay_hrd_flag; 134 | vector cpb_cnt_minus1; 135 | sub_layer_hrd_parameters_t sub_layer_hrd_parameters; // nal 136 | sub_layer_hrd_parameters_t sub_layer_hrd_parameters_v; // vlc 137 | } hrd_parameters_t; 138 | 139 | /** 140 | sps_range_extension 141 | @see 7.3.2.3.1 General picture parameter set RBSP syntax 142 | */ 143 | typedef struct 144 | { 145 | uint8_t transform_skip_rotation_enabled_flag; 146 | uint8_t transform_skip_context_enabled_flag; 147 | uint8_t implicit_rdpcm_enabled_flag; 148 | uint8_t explicit_rdpcm_enabled_flag; 149 | uint8_t extended_precision_processing_flag; 150 | uint8_t intra_smoothing_disabled_flag; 151 | uint8_t high_precision_offsets_enabled_flag; 152 | uint8_t persistent_rice_adaptation_enabled_flag; 153 | uint8_t cabac_bypass_alignment_enabled_flag; 154 | } sps_range_extension_t; 155 | 156 | /** 157 | Access unit delimiter 158 | @see 7.3.2.5 Access unit delimiter RBSP syntax 159 | */ 160 | typedef struct 161 | { 162 | uint8_t pic_type; 163 | } h265_aud_t; 164 | 165 | /** 166 | @see 7.3.2.3.2 Picture parameter set range extension syntax 167 | */ 168 | typedef struct 169 | { 170 | int log2_max_transform_skip_block_size_minus2; 171 | uint8_t cross_component_prediction_enabled_flag; 172 | uint8_t chroma_qp_offset_list_enabled_flag; 173 | int diff_cu_chroma_qp_offset_depth; 174 | int chroma_qp_offset_list_len_minus1; 175 | vector cb_qp_offset_list; 176 | vector cr_qp_offset_list; 177 | int log2_sao_offset_scale_luma; 178 | int log2_sao_offset_scale_chroma; 179 | } pps_range_extension_t; 180 | 181 | /** 182 | 7.3.4 Scaling list data syntax 183 | */ 184 | typedef struct 185 | { 186 | int scaling_list_pred_mode_flag[4][6]; 187 | int scaling_list_pred_matrix_id_delta[4][6]; 188 | int scaling_list_dc_coef_minus8[4][6]; 189 | int ScalingList[4][6][64]; 190 | int coefNum; 191 | } scaling_list_data_t; 192 | 193 | /** 194 | E.2.1 VUI parameters syntax 195 | */ 196 | typedef struct 197 | { 198 | uint8_t aspect_ratio_info_present_flag; 199 | uint8_t aspect_ratio_idc; 200 | int sar_width; 201 | int sar_height; 202 | uint8_t overscan_info_present_flag; 203 | uint8_t overscan_appropriate_flag; 204 | uint8_t video_signal_type_present_flag; 205 | uint8_t video_format; 206 | uint8_t video_full_range_flag; 207 | uint8_t colour_description_present_flag; 208 | uint8_t colour_primaries; 209 | uint8_t transfer_characteristics; 210 | uint8_t matrix_coeffs; 211 | uint8_t chroma_loc_info_present_flag; 212 | int chroma_sample_loc_type_top_field; 213 | int chroma_sample_loc_type_bottom_field; 214 | uint8_t neutral_chroma_indication_flag; 215 | uint8_t field_seq_flag; 216 | uint8_t frame_field_info_present_flag; 217 | uint8_t default_display_window_flag; 218 | int def_disp_win_left_offset; 219 | int def_disp_win_right_offset; 220 | int def_disp_win_top_offset; 221 | int def_disp_win_bottom_offset; 222 | uint8_t vui_timing_info_present_flag; 223 | uint32_t vui_num_units_in_tick; 224 | uint32_t vui_time_scale; 225 | uint8_t vui_poc_proportional_to_timing_flag; 226 | int vui_num_ticks_poc_diff_one_minus1; 227 | uint8_t vui_hrd_parameters_present_flag; 228 | hrd_parameters_t hrd_parameters; 229 | uint8_t bitstream_restriction_flag; 230 | uint8_t tiles_fixed_structure_flag; 231 | uint8_t motion_vectors_over_pic_boundaries_flag; 232 | uint8_t restricted_ref_pic_lists_flag; 233 | int min_spatial_segmentation_idc; 234 | int max_bytes_per_pic_denom; 235 | int max_bits_per_min_cu_denom; 236 | int log2_max_mv_length_horizontal; 237 | int log2_max_mv_length_vertical; 238 | } vui_parameters_t; 239 | 240 | /** 241 | Weighted prediction table 242 | @see 7.3.6.3 Weighted prediction parameters syntax 243 | */ 244 | typedef struct 245 | { 246 | int luma_log2_weight_denom; 247 | int delta_chroma_log2_weight_denom; 248 | vector luma_weight_l0_flag; 249 | vector chroma_weight_l0_flag; 250 | vector delta_luma_weight_l0; 251 | vector luma_offset_l0; 252 | vector > delta_chroma_weight_l0; 253 | vector > delta_chroma_offset_l0; 254 | vector luma_weight_l1_flag; 255 | vector chroma_weight_l1_flag; 256 | vector delta_luma_weight_l1; 257 | vector luma_offset_l1; 258 | vector > delta_chroma_weight_l1; 259 | vector > delta_chroma_offset_l1; 260 | } pred_weight_table_t; 261 | 262 | typedef struct 263 | { 264 | uint8_t inter_ref_pic_set_prediction_flag; 265 | int delta_idx_minus1; 266 | uint8_t delta_rps_sign; 267 | int abs_delta_rps_minus1; 268 | vector used_by_curr_pic_flag; 269 | vector use_delta_flag; 270 | int num_negative_pics; 271 | int num_positive_pics; 272 | 273 | #if 1 274 | vector delta_poc_s0_minus1; 275 | vector used_by_curr_pic_s0_flag; 276 | vector delta_poc_s1_minus1; 277 | vector used_by_curr_pic_s1_flag; 278 | #else 279 | int delta_poc_s0_minus1[16]; 280 | uint8_t used_by_curr_pic_s0_flag[16]; 281 | int delta_poc_s1_minus1[16]; 282 | uint8_t used_by_curr_pic_s1_flag[16]; 283 | #endif 284 | } st_ref_pic_set_t; 285 | 286 | #define MAX_NUM_REF_PICS 16 ///< max. number of pictures used for reference 287 | 288 | typedef struct 289 | { 290 | int m_numberOfPictures; 291 | int m_numberOfNegativePictures; 292 | int m_numberOfPositivePictures; 293 | int m_numberOfLongtermPictures; 294 | int m_deltaPOC[MAX_NUM_REF_PICS]; 295 | int m_POC[MAX_NUM_REF_PICS]; 296 | int m_used[MAX_NUM_REF_PICS]; 297 | int m_interRPSPrediction; 298 | int m_deltaRIdxMinus1; 299 | int m_deltaRPS; 300 | int m_numRefIdc; 301 | int m_refIdc[MAX_NUM_REF_PICS+1]; 302 | int m_bCheckLTMSB[MAX_NUM_REF_PICS]; 303 | int m_pocLSBLT[MAX_NUM_REF_PICS]; 304 | int m_deltaPOCMSBCycleLT[MAX_NUM_REF_PICS]; 305 | int m_deltaPocMSBPresentFlag[MAX_NUM_REF_PICS]; 306 | } referencePictureSets_t; 307 | 308 | typedef struct 309 | { 310 | uint8_t ref_pic_list_modification_flag_l0; 311 | uint32_t list_entry_l0[32]; // according to HM16.6 source code 312 | uint8_t ref_pic_list_modification_flag_l1; 313 | uint32_t list_entry_l1[32]; 314 | } ref_pic_lists_modification_t; 315 | 316 | /** 317 | Video Parameter Set 318 | @see 7.3.2.1 Video parameter set RBSP syntax 319 | */ 320 | typedef struct 321 | { 322 | uint8_t vps_video_parameter_set_id; // u(4) 323 | uint8_t vps_base_layer_internal_flag; // u(1) 324 | uint8_t vps_base_layer_available_flag; // u(1) 325 | uint8_t vps_max_layers_minus1; // u(6) 326 | uint8_t vps_max_sub_layers_minus1; // u(3) 327 | uint8_t vps_temporal_id_nesting_flag; // u(1) 328 | int vps_reserved_0xffff_16bits; // u(16) 329 | profile_tier_level_t ptl; 330 | uint8_t vps_sub_layer_ordering_info_present_flag; 331 | // Sublayers 332 | int vps_max_dec_pic_buffering_minus1[8]; // max u(3) 333 | int vps_max_num_reorder_pics[8]; 334 | int vps_max_latency_increase_plus1[8]; 335 | uint8_t vps_max_layer_id; 336 | int vps_num_layer_sets_minus1; 337 | vector > layer_id_included_flag; 338 | uint8_t vps_timing_info_present_flag; 339 | int vps_num_units_in_tick; 340 | int vps_time_scale; 341 | uint8_t vps_poc_proportional_to_timing_flag; 342 | int vps_num_ticks_poc_diff_one_minus1; 343 | int vps_num_hrd_parameters; 344 | vector hrd_layer_set_idx; 345 | vector cprms_present_flag; 346 | hrd_parameters_t hrd_parameters; 347 | uint8_t vps_extension_flag; 348 | uint8_t vps_extension_data_flag; 349 | } h265_vps_t; 350 | 351 | /** 352 | Sequence Parameter Set 353 | @see 7.3.2.2 Sequence parameter set RBSP syntax 354 | */ 355 | typedef struct 356 | { 357 | uint8_t sps_video_parameter_set_id; 358 | uint8_t sps_max_sub_layers_minus1; 359 | uint8_t sps_temporal_id_nesting_flag; 360 | profile_tier_level_t ptl; 361 | int sps_seq_parameter_set_id; 362 | int chroma_format_idc; 363 | uint8_t separate_colour_plane_flag; 364 | int pic_width_in_luma_samples; 365 | int pic_height_in_luma_samples; 366 | int conformance_window_flag; 367 | int conf_win_left_offset; 368 | int conf_win_right_offset; 369 | int conf_win_top_offset; 370 | int conf_win_bottom_offset; 371 | int bit_depth_luma_minus8; 372 | int bit_depth_chroma_minus8; 373 | int log2_max_pic_order_cnt_lsb_minus4; 374 | uint8_t sps_sub_layer_ordering_info_present_flag; 375 | int sps_max_dec_pic_buffering_minus1[8]; // max u(3) 376 | int sps_max_num_reorder_pics[8]; 377 | int sps_max_latency_increase_plus1[8]; 378 | int log2_min_luma_coding_block_size_minus3; 379 | int log2_diff_max_min_luma_coding_block_size; 380 | int log2_min_luma_transform_block_size_minus2; 381 | int log2_diff_max_min_luma_transform_block_size; 382 | int max_transform_hierarchy_depth_inter; 383 | int max_transform_hierarchy_depth_intra; 384 | uint8_t scaling_list_enabled_flag; 385 | uint8_t sps_infer_scaling_list_flag; 386 | int sps_scaling_list_ref_layer_id; 387 | int sps_scaling_list_data_present_flag; 388 | scaling_list_data_t scaling_list_data; 389 | uint8_t amp_enabled_flag; 390 | uint8_t sample_adaptive_offset_enabled_flag; 391 | uint8_t pcm_enabled_flag; 392 | uint8_t pcm_sample_bit_depth_luma_minus1; 393 | uint8_t pcm_sample_bit_depth_chroma_minus1; 394 | int log2_min_pcm_luma_coding_block_size_minus3; 395 | int log2_diff_max_min_pcm_luma_coding_block_size; 396 | uint8_t pcm_loop_filter_disabled_flag; 397 | int num_short_term_ref_pic_sets; 398 | vector st_ref_pic_set; 399 | vector m_RPSList; // store 400 | uint8_t long_term_ref_pics_present_flag; 401 | int num_long_term_ref_pics_sps; 402 | int lt_ref_pic_poc_lsb_sps_bytes; 403 | vector lt_ref_pic_poc_lsb_sps; 404 | vector used_by_curr_pic_lt_sps_flag; 405 | uint8_t sps_temporal_mvp_enabled_flag; 406 | uint8_t strong_intra_smoothing_enabled_flag; 407 | uint8_t vui_parameters_present_flag; 408 | vui_parameters_t vui; 409 | uint8_t sps_extension_present_flag; 410 | uint8_t sps_range_extension_flag; 411 | uint8_t sps_multilayer_extension_flag; 412 | uint8_t sps_3d_extension_flag; 413 | uint8_t sps_extension_5bits; 414 | sps_range_extension_t sps_range_extension; 415 | uint8_t inter_view_mv_vert_constraint_flag ; //sps_multilayer_extension_t sps_multilayer_extension; 416 | //sps_3d_extension_t sps_3d_extension; 417 | //int sps_extension_data_flag; // no need 418 | // rbsp_trailing_bits()... 419 | } h265_sps_t; 420 | 421 | /** 422 | Picture Parameter Set 423 | @see 7.3.2.3.1 General picture parameter set RBSP syntax 424 | */ 425 | typedef struct 426 | { 427 | uint8_t pps_pic_parameter_set_id; 428 | uint8_t pps_seq_parameter_set_id; 429 | uint8_t dependent_slice_segments_enabled_flag; 430 | uint8_t output_flag_present_flag; 431 | uint8_t num_extra_slice_header_bits; 432 | uint8_t sign_data_hiding_enabled_flag; 433 | uint8_t cabac_init_present_flag; 434 | int num_ref_idx_l0_default_active_minus1; 435 | int num_ref_idx_l1_default_active_minus1; 436 | int init_qp_minus26; 437 | uint8_t constrained_intra_pred_flag; 438 | uint8_t transform_skip_enabled_flag; 439 | uint8_t cu_qp_delta_enabled_flag; 440 | int diff_cu_qp_delta_depth; 441 | int pps_cb_qp_offset; 442 | int pps_cr_qp_offset; 443 | uint8_t pps_slice_chroma_qp_offsets_present_flag; 444 | uint8_t weighted_pred_flag; 445 | int weighted_bipred_flag; 446 | uint8_t transquant_bypass_enabled_flag; 447 | uint8_t tiles_enabled_flag; 448 | uint8_t entropy_coding_sync_enabled_flag; 449 | int num_tile_columns_minus1; 450 | int num_tile_rows_minus1; 451 | int uniform_spacing_flag; 452 | vector column_width_minus1; 453 | vector row_height_minus1; 454 | uint8_t loop_filter_across_tiles_enabled_flag; 455 | uint8_t pps_loop_filter_across_slices_enabled_flag; 456 | uint8_t deblocking_filter_control_present_flag; 457 | uint8_t deblocking_filter_override_enabled_flag; 458 | uint8_t pps_deblocking_filter_disabled_flag; 459 | int pps_beta_offset_div2; 460 | int pps_tc_offset_div2; 461 | uint8_t pps_scaling_list_data_present_flag; 462 | scaling_list_data_t scaling_list_data; 463 | uint8_t lists_modification_present_flag; 464 | int log2_parallel_merge_level_minus2; 465 | uint8_t slice_segment_header_extension_present_flag; 466 | uint8_t pps_extension_present_flag; 467 | uint8_t pps_range_extension_flag; 468 | uint8_t pps_multilayer_extension_flag; 469 | uint8_t pps_3d_extension_flag; 470 | uint8_t pps_extension_5bits; 471 | pps_range_extension_t pps_range_extension; 472 | //pps_multilayer_extension_t pps_multilayer_extension; 473 | //pps_3d_extension_t pps_3d_extension; 474 | uint8_t pps_extension_data_flag; 475 | // rbsp_trailing_bits( ) ... 476 | } h265_pps_t; 477 | 478 | /** 479 | Slice Header 480 | @see 7.3.6.1 General slice segment header syntax 481 | */ 482 | typedef struct 483 | { 484 | int read_slice_type; // see if we only read slice type and return 485 | 486 | int first_slice_segment_in_pic_flag; 487 | uint8_t no_output_of_prior_pics_flag; 488 | int slice_pic_parameter_set_id; 489 | uint8_t dependent_slice_segment_flag; 490 | int slice_segment_address; 491 | int slice_segment_address_bytes; 492 | vector slice_reserved_flag; 493 | int slice_type; 494 | uint8_t pic_output_flag; 495 | int colour_plane_id; 496 | int slice_pic_order_cnt_lsb_bytes; 497 | int slice_pic_order_cnt_lsb; 498 | uint8_t short_term_ref_pic_set_sps_flag; 499 | st_ref_pic_set_t st_ref_pic_set; 500 | referencePictureSets_t* m_pRPS; 501 | referencePictureSets_t m_localRPS; 502 | int short_term_ref_pic_set_idx; 503 | int short_term_ref_pic_set_idx_bytes; 504 | int num_long_term_sps; 505 | int num_long_term_pics; 506 | vector lt_idx_sps; 507 | vector poc_lsb_lt; 508 | vector used_by_curr_pic_lt_flag; 509 | vector delta_poc_msb_present_flag; 510 | vector delta_poc_msb_cycle_lt; 511 | uint8_t slice_temporal_mvp_enabled_flag; 512 | uint8_t slice_sao_luma_flag; 513 | uint8_t slice_sao_chroma_flag; 514 | uint8_t num_ref_idx_active_override_flag; 515 | int num_ref_idx_l0_active_minus1; 516 | int num_ref_idx_l1_active_minus1; 517 | ref_pic_lists_modification_t ref_pic_lists_modification; 518 | uint8_t mvd_l1_zero_flag; 519 | uint8_t cabac_init_flag; 520 | uint8_t collocated_from_l0_flag; 521 | int collocated_ref_idx; 522 | pred_weight_table_t pred_weight_table; 523 | int five_minus_max_num_merge_cand; 524 | int slice_qp_delta; 525 | int slice_cb_qp_offset; 526 | int slice_cr_qp_offset; 527 | uint8_t cu_chroma_qp_offset_enabled_flag; 528 | uint8_t deblocking_filter_override_flag; 529 | uint8_t slice_deblocking_filter_disabled_flag; 530 | int slice_beta_offset_div2; 531 | int slice_tc_offset_div2; 532 | uint8_t slice_loop_filter_across_slices_enabled_flag; 533 | int num_entry_point_offsets; 534 | int offset_len_minus1; 535 | int entry_point_offset_minus1_bytes; 536 | vector entry_point_offset_minus1; 537 | int slice_segment_header_extension_length; 538 | vector slice_segment_header_extension_data_byte; 539 | // byte_alignment( )... 540 | } h265_slice_header_t; 541 | 542 | typedef struct 543 | { 544 | int rbsp_size; 545 | uint8_t* rbsp_buf; 546 | } h265_slice_data_rbsp_t; 547 | 548 | /** 549 | H265 stream 550 | Contains data structures for all NAL types that can be handled by this library. 551 | When reading, data is read into those, and when writing it is written from those. 552 | The reason why they are all contained in one place is that some of them depend on others, we need to 553 | have all of them available to read or write correctly. 554 | */ 555 | typedef struct 556 | { 557 | h265_nal_t* nal; 558 | h265_vps_t* vps; 559 | h265_sps_t* sps; 560 | h265_pps_t* pps; 561 | h265_aud_t* aud; 562 | h265_sei_t* sei; //This is a TEMP pointer at whats in h->seis... 563 | int num_seis; 564 | h265_slice_header_t* sh; 565 | h265_slice_data_rbsp_t* slice_data; 566 | 567 | h265_vps_t* vps_table[16]; // --checked 568 | h265_sps_t* sps_table[32]; 569 | h265_pps_t* pps_table[256]; 570 | h265_sei_t** seis; 571 | videoinfo_t* info; 572 | } h265_stream_t; 573 | 574 | h265_stream_t* h265_new(); 575 | void h265_free(h265_stream_t* h); 576 | 577 | 578 | int h265_read_nal_unit(h265_stream_t* h, uint8_t* buf, int size); 579 | 580 | void h265_read_vps_rbsp(h265_stream_t* h, bs_t* b); 581 | void h265_read_sps_rbsp(h265_stream_t* h, bs_t* b); 582 | void h265_read_pps_rbsp(h265_stream_t* h, bs_t* b); 583 | void h265_read_sei_rbsp(h265_stream_t* h, bs_t* b); 584 | void h265_read_aud_rbsp(h265_stream_t* h, bs_t* b); 585 | void h265_read_slice_layer_rbsp(h265_stream_t* h, bs_t* b); 586 | void h265_read_end_of_seq_rbsp(h265_stream_t* h, bs_t* b); 587 | void h265_read_end_of_stream_rbsp(h265_stream_t* h, bs_t* b); 588 | void h265_read_rbsp_trailing_bits(bs_t* b); 589 | int h265_more_rbsp_trailing_data(bs_t* b); 590 | 591 | //Table 7-1 NAL unit type codes and NAL unit type classes 592 | enum NalUnitType 593 | { 594 | NAL_UNIT_CODED_SLICE_TRAIL_N = 0, // 0 595 | NAL_UNIT_CODED_SLICE_TRAIL_R, // 1 596 | 597 | NAL_UNIT_CODED_SLICE_TSA_N, // 2 598 | NAL_UNIT_CODED_SLICE_TSA_R, // 3 599 | 600 | NAL_UNIT_CODED_SLICE_STSA_N, // 4 601 | NAL_UNIT_CODED_SLICE_STSA_R, // 5 602 | 603 | NAL_UNIT_CODED_SLICE_RADL_N, // 6 604 | NAL_UNIT_CODED_SLICE_RADL_R, // 7 605 | 606 | NAL_UNIT_CODED_SLICE_RASL_N, // 8 607 | NAL_UNIT_CODED_SLICE_RASL_R, // 9 608 | 609 | NAL_UNIT_RESERVED_VCL_N10, 610 | NAL_UNIT_RESERVED_VCL_R11, 611 | NAL_UNIT_RESERVED_VCL_N12, 612 | NAL_UNIT_RESERVED_VCL_R13, 613 | NAL_UNIT_RESERVED_VCL_N14, 614 | NAL_UNIT_RESERVED_VCL_R15, 615 | 616 | NAL_UNIT_CODED_SLICE_BLA_W_LP, // 16 617 | NAL_UNIT_CODED_SLICE_BLA_W_RADL, // 17 618 | NAL_UNIT_CODED_SLICE_BLA_N_LP, // 18 619 | NAL_UNIT_CODED_SLICE_IDR_W_RADL, // 19 620 | NAL_UNIT_CODED_SLICE_IDR_N_LP, // 20 621 | NAL_UNIT_CODED_SLICE_CRA, // 21 622 | NAL_UNIT_RESERVED_IRAP_VCL22, 623 | NAL_UNIT_RESERVED_IRAP_VCL23, 624 | 625 | NAL_UNIT_RESERVED_VCL24, 626 | NAL_UNIT_RESERVED_VCL25, 627 | NAL_UNIT_RESERVED_VCL26, 628 | NAL_UNIT_RESERVED_VCL27, 629 | NAL_UNIT_RESERVED_VCL28, 630 | NAL_UNIT_RESERVED_VCL29, 631 | NAL_UNIT_RESERVED_VCL30, 632 | NAL_UNIT_RESERVED_VCL31, 633 | 634 | // non-VCL 635 | NAL_UNIT_VPS, // 32 636 | NAL_UNIT_SPS, // 33 637 | NAL_UNIT_PPS, // 34 638 | NAL_UNIT_AUD, // 35 639 | NAL_UNIT_EOS, // 36 640 | NAL_UNIT_EOB, // 37 641 | NAL_UNIT_FILLER_DATA, // 38 642 | NAL_UNIT_PREFIX_SEI, // 39 643 | NAL_UNIT_SUFFIX_SEI, // 40 644 | 645 | NAL_UNIT_RESERVED_NVCL41, 646 | NAL_UNIT_RESERVED_NVCL42, 647 | NAL_UNIT_RESERVED_NVCL43, 648 | NAL_UNIT_RESERVED_NVCL44, 649 | NAL_UNIT_RESERVED_NVCL45, 650 | NAL_UNIT_RESERVED_NVCL46, 651 | NAL_UNIT_RESERVED_NVCL47, 652 | NAL_UNIT_UNSPECIFIED_48, 653 | NAL_UNIT_UNSPECIFIED_49, 654 | NAL_UNIT_UNSPECIFIED_50, 655 | NAL_UNIT_UNSPECIFIED_51, 656 | NAL_UNIT_UNSPECIFIED_52, 657 | NAL_UNIT_UNSPECIFIED_53, 658 | NAL_UNIT_UNSPECIFIED_54, 659 | NAL_UNIT_UNSPECIFIED_55, 660 | NAL_UNIT_UNSPECIFIED_56, 661 | NAL_UNIT_UNSPECIFIED_57, 662 | NAL_UNIT_UNSPECIFIED_58, 663 | NAL_UNIT_UNSPECIFIED_59, 664 | NAL_UNIT_UNSPECIFIED_60, 665 | NAL_UNIT_UNSPECIFIED_61, 666 | NAL_UNIT_UNSPECIFIED_62, 667 | NAL_UNIT_UNSPECIFIED_63, 668 | NAL_UNIT_INVALID, 669 | }; 670 | 671 | // Table 7-7 �C Name association to slice_type --checked 672 | enum SliceType 673 | { 674 | H265_SH_SLICE_TYPE_B = 0, // P (P slice) 675 | H265_SH_SLICE_TYPE_P = 1, // B (B slice) 676 | H265_SH_SLICE_TYPE_I = 2, // I (I slice) 677 | }; 678 | 679 | //7.4.3.5 Table 7-2 �C Interpretation of pic_type --checked 680 | #define H265_AUD_PRIMARY_PIC_TYPE_I 0 // I 681 | #define H265_AUD_PRIMARY_PIC_TYPE_IP 1 // P, I 682 | #define H265_AUD_PRIMARY_PIC_TYPE_IPB 2 // B, P, I 683 | 684 | //Appendix E. Table E-1 Meaning of sample aspect ratio indicator --check 685 | #define H265_SAR_Unspecified 0 // Unspecified 686 | #define H265_SAR_1_1 1 // 1:1 687 | #define H265_SAR_12_11 2 // 12:11 688 | #define H265_SAR_10_11 3 // 10:11 689 | #define H265_SAR_16_11 4 // 16:11 690 | #define H265_SAR_40_33 5 // 40:33 691 | #define H265_SAR_24_11 6 // 24:11 692 | #define H265_SAR_20_11 7 // 20:11 693 | #define H265_SAR_32_11 8 // 32:11 694 | #define H265_SAR_80_33 9 // 80:33 695 | #define H265_SAR_18_11 10 // 18:11 696 | #define H265_SAR_15_11 11 // 15:11 697 | #define H265_SAR_64_33 12 // 64:33 698 | #define H265_SAR_160_99 13 // 160:99 699 | #define H265_SAR_4_3 14 // 160:99 700 | #define H265_SAR_3_2 15 // 160:99 701 | #define H265_SAR_2_1 16 // 160:99 702 | // 17..254 Reserved 703 | #define H265_SAR_Extended 255 // Extended_SAR 704 | 705 | /// chroma formats (according to semantics of chroma_format_idc) 706 | enum ChromaFormat 707 | { 708 | CHROMA_400 = 0, 709 | CHROMA_420 = 1, 710 | CHROMA_422 = 2, 711 | CHROMA_444 = 3, 712 | NUM_CHROMA_FORMAT = 4 713 | }; 714 | 715 | enum ProfileName 716 | { 717 | PROFILE_NONE = 0, 718 | PROFILE_MAIN = 1, 719 | PROFILE_MAIN10 = 2, 720 | PROFILE_MAINSTILLPICTURE = 3, 721 | PROFILE_MAINREXT = 4, 722 | PROFILE_HIGHTHROUGHPUTREXT = 5 723 | }; 724 | 725 | enum Tier 726 | { 727 | TIER_MAIN = 0, 728 | TIER_HIGH = 1, 729 | }; 730 | 731 | enum Level 732 | { 733 | // code = (level * 30) 734 | LEVEL_NONE = 0, 735 | LEVEL1 = 30, 736 | LEVEL2 = 60, 737 | LEVEL2_1 = 63, 738 | LEVEL3 = 90, 739 | LEVEL3_1 = 93, 740 | LEVEL4 = 120, 741 | LEVEL4_1 = 123, 742 | LEVEL5 = 150, 743 | LEVEL5_1 = 153, 744 | LEVEL5_2 = 156, 745 | LEVEL6 = 180, 746 | LEVEL6_1 = 183, 747 | LEVEL6_2 = 186, 748 | LEVEL8_5 = 255, 749 | }; 750 | 751 | // file handle for debug output 752 | extern FILE* h265_dbgfile; 753 | 754 | #endif //ZLMEDIAKIT_H265_STREAM_H 755 | -------------------------------------------------------------------------------- /h26x_sps_parse.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #include 6 | 7 | #include "h26x_sps_parse.h" 8 | #include "h265_stream.h" 9 | 10 | namespace monk { 11 | 12 | int32_t H26xParseSPS::h264Parse(const uint8_t *data, uint32_t dataSize, H26xSPSInfo& info) 13 | { 14 | if(data == nullptr || dataSize == 0) return -1; 15 | 16 | int32_t result = -1; 17 | h264_stream_t *h = h264_new(); 18 | result = read_nal_unit(h, (uint8_t *)data, dataSize); 19 | if(result == -1){ 20 | std::cout << "h265_read_nal_unit error." << std::endl; 21 | h264_free(h); 22 | return -1; 23 | } 24 | 25 | if(h->nal->nal_unit_type != NAL_UNIT_TYPE_SPS){ 26 | std::cout << "input not sps data." << std::endl; 27 | h264_free(h); 28 | return -1; 29 | } 30 | 31 | info.width = h->info->width; 32 | info.height = h->info->height; 33 | info.profile_idc = h->info->profile_idc; 34 | info.level_idc = h->info->level_idc; 35 | info.fps = h->info->max_framerate; 36 | 37 | h264_free(h); 38 | 39 | return 0; 40 | } 41 | 42 | int32_t H26xParseSPS::h265Parse(const uint8_t *data, uint32_t dataSize, H26xSPSInfo& info) 43 | { 44 | if(data == nullptr || dataSize == 0) return -1; 45 | 46 | int32_t result = -1; 47 | h265_stream_t *h = h265_new(); 48 | result = h265_read_nal_unit(h, (uint8_t *)data, dataSize); 49 | if(result == -1){ 50 | std::cout << "h265_read_nal_unit error." << std::endl; 51 | h265_free(h); 52 | return -1; 53 | } 54 | 55 | if(h->nal->nal_unit_type != NAL_UNIT_SPS){ 56 | std::cout << "input not sps data." << std::endl; 57 | h265_free(h); 58 | return -1; 59 | } 60 | 61 | info.width = h->info->width; 62 | info.height = h->info->height; 63 | info.profile_idc = h->info->profile_idc; 64 | info.level_idc = h->info->level_idc; 65 | info.fps = h->info->max_framerate; 66 | 67 | h265_free(h); 68 | 69 | return 0; 70 | } 71 | 72 | }// end namespace monk 73 | -------------------------------------------------------------------------------- /h26x_sps_parse.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #ifndef ZLMEDIAKIT_H26X_SPS_PARSE_H 6 | #define ZLMEDIAKIT_H26X_SPS_PARSE_H 7 | 8 | #include 9 | 10 | namespace monk { 11 | 12 | struct H26xSPSInfo 13 | { 14 | uint32_t profile_idc; 15 | uint32_t level_idc; 16 | 17 | uint32_t width; 18 | uint32_t height; 19 | uint32_t fps; //SPS中可能不包含FPS信息 20 | 21 | H26xSPSInfo() { 22 | profile_idc = 0; 23 | level_idc = 0; 24 | 25 | width = 0; 26 | height = 0; 27 | fps = 0; 28 | } 29 | }; 30 | 31 | class H26xParseSPS { 32 | public: 33 | /* 34 | 解析H264 SPS数据信息 35 | @param data SPS数据内容,需要Nal类型为0x7数据的开始(比如:67 42 00 28 ab 40 22 01 e3 cb cd c0 80 80 a9 02) 36 | @param dataSize SPS数据的长度 37 | @param info SPS解析之后的信息数据结构体 38 | @return success:0,fail:-1 39 | */ 40 | static int32_t h264Parse(const uint8_t *data, uint32_t dataSize, H26xSPSInfo& info); 41 | 42 | /* 43 | 解析H265 SPS数据信息 44 | @param data SPS数据内容 45 | @param dataSize SPS数据的长度 46 | @param info SPS解析之后的信息数据结构体 47 | @return success:0,fail:-1 48 | */ 49 | static int32_t h265Parse(const uint8_t *data, uint32_t dataSize, H26xSPSInfo& info); 50 | }; 51 | 52 | } // end namespace monk 53 | 54 | 55 | 56 | #endif //ZLMEDIAKIT_H26X_SPS_PARSE_H 57 | -------------------------------------------------------------------------------- /test_h26x_analyzer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by monktan on 2020/10/21. 3 | // 4 | 5 | #include 6 | 7 | #include "h26x_sps_parse.h" 8 | 9 | 10 | int main(int argc, char **argv) 11 | { 12 | unsigned char data_264[] = { 13 | 0x67,0x64,0x00,0x16,0xac,0xb2,0x81,0x40, 14 | 0x7b,0x42,0x00,0x00,0x03,0x00,0x02,0x00, 15 | 0x00,0x03,0x00,0x3d,0x1e,0x2c,0x5c,0xb0}; 16 | 17 | unsigned char data_265[] { 18 | 0X42,0X01,0X01,0X01,0X60,0X00,0X00,0X03,0X00,0X80,0X00,0X00,0X03,0X00,0X00, 19 | 0X03,0X00,0X5D,0XA0,0X02,0X80, 0X80,0X2D,0X16,0X59,0X5E,0X49,0X32,0XB8,0X04,0X00,0X00,0X03, 20 | 0X00,0X04,0X00,0X00,0X03,0X00,0X64,0X20 21 | }; 22 | 23 | monk::H26xSPSInfo h264_info; 24 | monk::H26xSPSInfo h265_info; 25 | 26 | int32_t result = -1; 27 | result = monk::H26xParseSPS::h264Parse(data_264, sizeof(data_264), h264_info); 28 | if(result != 0){ 29 | std::cout << "H26xParseSPS::h264Parse failed." << std::endl; 30 | return -1; 31 | } 32 | 33 | std::cout << "h264_info: width: " << h264_info.width 34 | << " , height: " << h264_info.height 35 | << " , fps: " << h264_info.fps 36 | << std::endl; 37 | 38 | result = monk::H26xParseSPS::h265Parse(data_265, sizeof(data_265), h265_info); 39 | if(result != 0){ 40 | std::cout << "H26xParseSPS::h264Parse failed." << std::endl; 41 | return -1; 42 | } 43 | 44 | std::cout << "h265_info: width: " << h265_info.width 45 | << " , height: " << h265_info.height 46 | << " , fps: " << h265_info.fps 47 | << std::endl; 48 | 49 | return 0; 50 | } --------------------------------------------------------------------------------