Code Coverage Report for src/decode/packet.h


Hit Total Coverage
Lines: 15 15 100.0%
Branches: 24 24 100.0%

1 /*
2 * libnogg: a decoder library for Ogg Vorbis streams
3 * Copyright (c) 2014-2019 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.
69 *
70 * [Parameters]
71 * handle: Stream handle.
72 * [Return value]
73 * True on success, false on error.
74 */
75 #define start_packet INTERNAL(start_packet)
76 extern bool start_packet(stb_vorbis *handle);
77
78 /**
79 * get8_packet: Read one byte from the current packet. The bit accumulator
80 * for bitstream reads is cleared.
81 *
82 * [Parameters]
83 * handle: Stream handle.
84 * [Return value]
85 * Byte read, or EOP on end of packet or error.
86 */
87 #define get8_packet INTERNAL(get8_packet)
88 extern int get8_packet(stb_vorbis *handle);
89
90 /**
91 * get32_packet: Read a byte-aligned 32-bit signed integer from the
92 * current packet. The bit accumulator for bitstream reads is cleared.
93 *
94 * This function does not check for end-of-packet.
95 *
96 * [Parameters]
97 * handle: Stream handle.
98 * [Return value]
99 * Value read.
100 */
101 #define get32_packet INTERNAL(get32_packet)
102 extern int32_t get32_packet(stb_vorbis *handle);
103
104 /**
105 * getn_packet: Read a sequence of bytes from the current packet. The bit
106 * accumulator for bitstream reads is cleared.
107 *
108 * [Parameters]
109 * handle: Stream handle.
110 * [Return value]
111 * True on success, false on end of packet or error.
112 */
113 #define getn_packet INTERNAL(getn_packet)
114 extern bool getn_packet(stb_vorbis *handle, void *buf, int len);
115
116 /**
117 * get_bits: Read a value of arbitrary bit length from the stream.
118 *
119 * [Parameters]
120 * handle: Stream handle.
121 * [Return value]
122 * Value read, or 0 on end of packet or error.
123 */
124 #define get_bits INTERNAL(get_bits)
125 extern uint32_t get_bits(stb_vorbis *handle, int count);
126
127 /**
128 * flush_packet: Advance the stream read position to the end of the
129 * current packet.
130 *
131 * [Parameters]
132 * handle: Stream handle.
133 * [Return value]
134 * False on unexpected end-of-file, true otherwise.
135 */
136 #define flush_packet INTERNAL(flush_packet)
137 extern bool flush_packet(stb_vorbis *handle);
138
139 /*-----------------------------------------------------------------------*/
140
141 /**
142 * get8_packet_raw: Read one byte from the current packet.
143 *
144 * [Parameters]
145 * handle: Stream handle.
146 * [Return value]
147 * Byte read, or EOP on end of packet or error.
148 */
149 static inline UNUSED int get8_packet_raw(stb_vorbis *handle)
150 {
151 (4/4) if (handle->segment_pos >= handle->segment_size) {
152 (8/8) if (handle->last_seg || !next_segment(handle)) {
153 return EOP;
154 }
155 }
156 return handle->segment_data[handle->segment_pos++];
157 }
158
159 /**
160 * fill_bits: Fill the bit accumulator with as much data as possible.
161 *
162 * [Parameters]
163 * handle: Stream handle.
164 */
165 static inline UNUSED void fill_bits(stb_vorbis *handle)
166 {
167 (4/4) if (handle->valid_bits == 0) {
168 handle->acc = 0;
169 }
170 (4/4) while (handle->valid_bits <= ((int)sizeof(long) * 8 - 8)) {
171 const int32_t byte = get8_packet_raw(handle);
172 (4/4) if (UNLIKELY(byte == EOP)) {
173 break;
174 }
175 handle->acc |= (unsigned long)byte << handle->valid_bits;
176 handle->valid_bits += 8;
177 }
178 }
179
180 /*************************************************************************/
181 /*************************************************************************/
182
183 #endif // NOGG_SRC_DECODE_PACKET_H