1 /*
2  * H.265 video codec.
3  * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4  *
5  * This file is part of libde265.
6  *
7  * libde265 is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * libde265 is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef DE265_NAL_H
22 #define DE265_NAL_H
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdint.h>
29 #ifdef HAVE_STDBOOL_H
30 #include <stdbool.h>
31 #endif
32 
33 #include "libde265/bitstream.h"
34 #include "libde265/cabac.h"
35 
36 struct nal_header {
nal_headernal_header37   nal_header() {
38     nal_unit_type = 0;
39     nuh_layer_id = 0;
40     nuh_temporal_id = 0;
41   }
42 
43   void read(bitreader* reader);
44   void write(CABAC_encoder& writer) const;
45 
46   void set(int unit_type, int layer_id=0, int temporal_id=0) {
47     nal_unit_type  =unit_type;
48     nuh_layer_id   =layer_id;
49     nuh_temporal_id=temporal_id;
50   }
51 
52   uint8_t nal_unit_type;
53   uint8_t nuh_layer_id;
54   uint8_t nuh_temporal_id;
55 };
56 
57 #define NAL_UNIT_TRAIL_N  0
58 #define NAL_UNIT_TRAIL_R  1
59 #define NAL_UNIT_TSA_N    2
60 #define NAL_UNIT_TSA_R    3
61 #define NAL_UNIT_STSA_N   4
62 #define NAL_UNIT_STSA_R   5
63 #define NAL_UNIT_RADL_N   6
64 #define NAL_UNIT_RADL_R   7
65 #define NAL_UNIT_RASL_N   8
66 #define NAL_UNIT_RASL_R   9
67 #define NAL_UNIT_RESERVED_VCL_N10  10
68 #define NAL_UNIT_RESERVED_VCL_N12  12
69 #define NAL_UNIT_RESERVED_VCL_N14  14
70 #define NAL_UNIT_RESERVED_VCL_R11  11
71 #define NAL_UNIT_RESERVED_VCL_R13  13
72 #define NAL_UNIT_RESERVED_VCL_R15  15
73 #define NAL_UNIT_BLA_W_LP   16     // BLA = broken link access
74 #define NAL_UNIT_BLA_W_RADL 17
75 #define NAL_UNIT_BLA_N_LP   18
76 #define NAL_UNIT_IDR_W_RADL 19
77 #define NAL_UNIT_IDR_N_LP   20
78 #define NAL_UNIT_CRA_NUT    21     // CRA = clean random access
79 #define NAL_UNIT_RESERVED_IRAP_VCL22 22
80 #define NAL_UNIT_RESERVED_IRAP_VCL23 23
81 #define NAL_UNIT_RESERVED_VCL24     24
82 #define NAL_UNIT_RESERVED_VCL25     25
83 #define NAL_UNIT_RESERVED_VCL26     26
84 #define NAL_UNIT_RESERVED_VCL27     27
85 #define NAL_UNIT_RESERVED_VCL28     28
86 #define NAL_UNIT_RESERVED_VCL29     29
87 #define NAL_UNIT_RESERVED_VCL30     30
88 #define NAL_UNIT_RESERVED_VCL31     31
89 #define NAL_UNIT_VPS_NUT       32
90 #define NAL_UNIT_SPS_NUT       33
91 #define NAL_UNIT_PPS_NUT       34
92 #define NAL_UNIT_AUD_NUT       35
93 #define NAL_UNIT_EOS_NUT       36
94 #define NAL_UNIT_EOB_NUT       37
95 #define NAL_UNIT_FD_NUT        38
96 #define NAL_UNIT_PREFIX_SEI_NUT 39
97 #define NAL_UNIT_SUFFIX_SEI_NUT 40
98 #define NAL_UNIT_RESERVED_NVCL41     41
99 #define NAL_UNIT_RESERVED_NVCL42     42
100 #define NAL_UNIT_RESERVED_NVCL43     43
101 #define NAL_UNIT_RESERVED_NVCL44     44
102 #define NAL_UNIT_RESERVED_NVCL45     45
103 #define NAL_UNIT_RESERVED_NVCL46     46
104 #define NAL_UNIT_RESERVED_NVCL47     47
105 
106 #define NAL_UNIT_UNDEFINED    255
107 
108 bool isIDR(uint8_t unit_type);
109 bool isBLA(uint8_t unit_type);
110 bool isCRA(uint8_t unit_type);
111 bool isRAP(uint8_t unit_type);
112 bool isRASL(uint8_t unit_type);
113 bool isIRAP(uint8_t unit_type);
114 bool isRADL(uint8_t unit_type);
115 bool isReferenceNALU(uint8_t unit_type);
116 bool isSublayerNonReference(uint8_t unit_type);
117 
118 const char* get_NAL_name(uint8_t unit_type);
119 
isIdrPic(uint8_t nal_unit_type)120 inline bool isIdrPic(uint8_t nal_unit_type) {
121   return (nal_unit_type == NAL_UNIT_IDR_W_RADL ||
122           nal_unit_type == NAL_UNIT_IDR_N_LP);
123 }
124 
isRapPic(uint8_t nal_unit_type)125 inline bool isRapPic(uint8_t nal_unit_type) {
126   return nal_unit_type >= 16 && nal_unit_type <= 23;
127 }
128 
129 #endif
130