1 /*****************************************************************************
2 * nalu.h
3 *****************************************************************************
4 * Copyright (C) 2013-2017 L-SMASH project
5 *
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
20
21 /* This file is available under an ISC license. */
22
23 #define NALU_DEFAULT_BUFFER_SIZE (1<<16)
24 #define NALU_DEFAULT_NALU_LENGTH_SIZE 4 /* We always use 4 bytes length. */
25 #define NALU_SHORT_START_CODE_LENGTH 3
26 #define NALU_LONG_START_CODE_LENGTH 4
27 #define NALU_IO_ERROR UINT64_MAX - 1
28 #define NALU_NO_START_CODE_FOUND UINT64_MAX
29
30 /* Parameter Set Entry within AVC/HEVC Decoder Configuration Record */
31 typedef struct
32 {
33 uint16_t nalUnitLength;
34 uint8_t *nalUnit;
35 /* */
36 int unused;
37 } isom_dcr_ps_entry_t;
38
39 isom_dcr_ps_entry_t *isom_create_ps_entry
40 (
41 uint8_t *ps,
42 uint32_t ps_size
43 );
44
45 void isom_remove_dcr_ps
46 (
47 isom_dcr_ps_entry_t *ps
48 );
49
50 /* Convert EBSP (Encapsulated Byte Sequence Packets) to RBSP (Raw Byte Sequence Packets). */
51 uint8_t *nalu_remove_emulation_prevention
52 (
53 uint8_t *src,
54 uint64_t src_length,
55 uint8_t *dst
56 );
57
58 int nalu_import_rbsp_from_ebsp
59 (
60 lsmash_bits_t *bits,
61 uint8_t *rbsp_buffer,
62 uint8_t *ebsp,
63 uint64_t ebsp_size
64 );
65
66 int nalu_check_more_rbsp_data
67 (
68 lsmash_bits_t *bits
69 );
70
71 int nalu_get_max_ps_length
72 (
73 lsmash_entry_list_t *ps_list,
74 uint32_t *max_ps_length
75 );
76
77 int nalu_get_ps_count
78 (
79 lsmash_entry_list_t *ps_list,
80 uint32_t *ps_count
81 );
82
83 int nalu_check_same_ps_existence
84 (
85 lsmash_entry_list_t *ps_list,
86 void *ps_data,
87 uint32_t ps_length
88 );
89
90 int nalu_get_dcr_ps
91 (
92 lsmash_bs_t *bs,
93 lsmash_entry_list_t *list,
94 uint8_t entry_count
95 );
96
97 /* Return the offset from the beginning of stream if a start code is found.
98 * Return NALU_NO_START_CODE_FOUND otherwise. */
99 uint64_t nalu_find_first_start_code
100 (
101 lsmash_bs_t *bs
102 );
103
104 uint64_t nalu_get_codeNum
105 (
106 lsmash_bits_t *bits
107 );
108
nalu_decode_exp_golomb_ue(uint64_t codeNum)109 static inline uint64_t nalu_decode_exp_golomb_ue
110 (
111 uint64_t codeNum
112 )
113 {
114 return codeNum;
115 }
116
nalu_decode_exp_golomb_se(uint64_t codeNum)117 static inline int64_t nalu_decode_exp_golomb_se
118 (
119 uint64_t codeNum
120 )
121 {
122 if( codeNum & 1 )
123 return (int64_t)((codeNum >> 1) + 1);
124 return -1 * (int64_t)(codeNum >> 1);
125 }
126
nalu_get_exp_golomb_ue(lsmash_bits_t * bits)127 static inline uint64_t nalu_get_exp_golomb_ue
128 (
129 lsmash_bits_t *bits
130 )
131 {
132 uint64_t codeNum = nalu_get_codeNum( bits );
133 return nalu_decode_exp_golomb_ue( codeNum );
134 }
135
nalu_get_exp_golomb_se(lsmash_bits_t * bits)136 static inline uint64_t nalu_get_exp_golomb_se
137 (
138 lsmash_bits_t *bits
139 )
140 {
141 uint64_t codeNum = nalu_get_codeNum( bits );
142 return nalu_decode_exp_golomb_se( codeNum );
143 }
144
nalu_check_next_short_start_code(uint8_t * buf_pos,uint8_t * buf_end)145 static inline int nalu_check_next_short_start_code
146 (
147 uint8_t *buf_pos,
148 uint8_t *buf_end
149 )
150 {
151 return ((buf_pos + 2) < buf_end) && !buf_pos[0] && !buf_pos[1] && (buf_pos[2] == 0x01);
152 }
153