Code Coverage Report for src/decode/packet.h


Hit Total Coverage
Lines: 5 5 100.0%
Branches: 54 54 100.0%

1 /*
2 * libnogg: a decoder library for Ogg Vorbis streams
3 * Copyright (c) 2014-2023 Andrew Church <achurch@achurch.org>
4 *
5 * This software may be copied and redistributed under certain conditions;
6 * see the file "COPYING" in the source code distribution for details.
7 * NO WARRANTY is provided with this software.
8 */
9
10 #ifndef NOGG_SRC_DECODE_PACKET_H
11 #define NOGG_SRC_DECODE_PACKET_H
12
13 /*************************************************************************/
14 /*************************************************************************/
15
16 /* Constant returned from get8_packet_raw() when trying to read past the
17 * end of a packet. */
18 #define EOP (-1)
19
20 /* Flag bits set in the page_flag field of the decoder handle based on the
21 * status of the current page. */
22 #define PAGEFLAG_continued_packet 1
23 #define PAGEFLAG_first_page 2
24 #define PAGEFLAG_last_page 4
25
26 /*-----------------------------------------------------------------------*/
27
28 /**
29 * next_segment: Advance to the next segment in the current packet.
30 *
31 * [Parameters]
32 * handle: Stream handle.
33 * [Return value]
34 * True on success, false on error or end of packet.
35 */
36 #define next_segment INTERNAL(next_segment)
37 extern bool next_segment(stb_vorbis *handle);
38
39 /**
40 * reset_page: Reset the current stream state to start reading from a new
41 * page. A call to this function should be followed by a call to start_page()
42 * or start_packet().
43 *
44 * [Parameters]
45 * handle: Stream handle.
46 */
47 #define reset_page INTERNAL(reset_page)
48 extern void reset_page(stb_vorbis *handle);
49
50 /**
51 * start_page: Verify that the stream read position is at the beginning
52 * of a valid Ogg page, and start reading from the page.
53 *
54 * On error, some bytes may have been consumed from the stream.
55 *
56 * [Parameters]
57 * handle: Stream handle.
58 * check_page_number: True to check the page number for sequentiality,
59 * false to skip the check.
60 * [Return value]
61 * True on success, false on error.
62 */
63 #define start_page INTERNAL(start_page)
64 extern bool start_page(stb_vorbis *handle, bool check_page_number);
65
66 /**
67 * start_packet: Start reading a new packet at the current stream read
68 * position, advancing to a new page if necessary. Invalid for packet
69 * mode decoders.
70 *
71 * [Parameters]
72 * handle: Stream handle.
73 * [Return value]
74 * True on success, false on error.
75 */
76 #define start_packet INTERNAL(start_packet)
77 extern bool start_packet(stb_vorbis *handle);
78
79 /**
80 * start_packet_direct: Start reading a new packet from the given data
81 * buffer. Only valid for packet mode decoders.
82 *
83 * [Parameters]
84 * handle: Stream handle.
85 * [Return value]
86 * True on success, false on error.
87 */
88 #define start_packet_direct INTERNAL(start_packet_direct)
89 extern void start_packet_direct(stb_vorbis *handle, const void *packet,
90 int32_t packet_len);
91
92 /**
93 * get8_packet: Read one byte from the current packet. The bit accumulator
94 * for bitstream reads is cleared.
95 *
96 * [Parameters]
97 * handle: Stream handle.
98 * [Return value]
99 * Byte read, or EOP on end of packet or error.
100 */
101 #define get8_packet INTERNAL(get8_packet)
102 extern int get8_packet(stb_vorbis *handle);
103
104 /**
105 * get32_packet: Read a byte-aligned 32-bit signed integer from the
106 * current packet. The bit accumulator for bitstream reads is cleared.
107 *
108 * This function does not check for end-of-packet.
109 *
110 * [Parameters]
111 * handle: Stream handle.
112 * [Return value]
113 * Value read.
114 */
115 #define get32_packet INTERNAL(get32_packet)
116 extern int32_t get32_packet(stb_vorbis *handle);
117
118 /**
119 * getn_packet: Read a sequence of bytes from the current packet. The bit
120 * accumulator for bitstream reads is cleared.
121 *
122 * [Parameters]
123 * handle: Stream handle.
124 * [Return value]
125 * True on success, false on end of packet or error.
126 */
127 #define getn_packet INTERNAL(getn_packet)
128 extern bool getn_packet(stb_vorbis *handle, void *buf, int len);
129
130 /**
131 * get_bits: Read a value of arbitrary bit length from the stream.
132 *
133 * [Parameters]
134 * handle: Stream handle.
135 * [Return value]
136 * Value read, or 0 on end of packet or error.
137 */
138 #define get_bits INTERNAL(get_bits)
139 extern uint32_t get_bits(stb_vorbis *handle, int count);
140
141 /**
142 * flush_packet: Advance the stream read position to the end of the
143 * current packet.
144 *
145 * [Parameters]
146 * handle: Stream handle.
147 * [Return value]
148 * False on unexpected end-of-file, true otherwise.
149 */
150 #define flush_packet INTERNAL(flush_packet)
151 extern bool flush_packet(stb_vorbis *handle);
152
153 /*-----------------------------------------------------------------------*/
154
155 /**
156 * get8_packet_raw: Read one byte from the current packet.
157 *
158 * [Parameters]
159 * handle: Stream handle.
160 * [Return value]
161 * Byte read, or EOP on end of packet or error.
162 */
163 static inline UNUSED int get8_packet_raw(stb_vorbis *handle)
164 {
165 (18/18) if (handle->segment_pos >= handle->segment_size) {
166 (36/36) if (handle->last_seg || !next_segment(handle)) {
167 return EOP;
168 }
169 }
170 return handle->segment_data[handle->segment_pos++];
171 }
172
173 /**
174 * fill_bits: Fill the bit accumulator with as much data as possible.
175 *
176 * [Parameters]
177 * handle: Stream handle.
178 */
179 static inline UNUSED void fill_bits(stb_vorbis *handle)
180 {
181 if (handle->valid_bits == 0) {
182 handle->acc = 0;
183 }
184 while (handle->valid_bits <= ((int)sizeof(long) * 8 - 8)) {
185 const int32_t byte = get8_packet_raw(handle);
186 if (UNLIKELY(byte == EOP)) {
187 break;
188 }
189 handle->acc |= (unsigned long)byte << handle->valid_bits;
190 handle->valid_bits += 8;
191 }
192 }
193
194 /*************************************************************************/
195 /*************************************************************************/
196
197 #endif // NOGG_SRC_DECODE_PACKET_H