2 #include "serialization.hpp"
9 const uint32_t crc_lookup
[256]= {
10 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
11 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
12 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
13 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
14 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
15 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
16 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
17 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
18 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
19 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
20 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
21 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
22 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
23 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
24 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
25 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
26 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
27 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
28 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
29 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
30 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
31 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
32 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
33 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
34 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
35 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
36 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
37 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
38 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
39 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
40 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
41 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
42 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
43 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
44 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
45 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
46 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
47 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
48 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
49 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
50 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
51 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
52 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
53 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
54 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
55 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
56 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
57 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
58 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
59 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
60 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
61 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
62 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
63 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
64 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
65 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
66 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
67 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
68 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
69 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
70 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
71 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
72 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
73 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4
76 uint32_t ogg_crc32(uint32_t chain
, const uint8_t* data
, size_t size
)
80 for(size_t i
= 0; i
< size
; i
++)
81 chain
= (chain
<< 8) ^ crc_lookup
[(chain
>> 24) ^ data
[i
]];
87 ogg_page::ogg_page() throw()
90 flag_continue
= false;
93 last_incomplete
= false;
94 granulepos
= granulepos_none
;
100 memset(data
, 0, sizeof(data
));
101 memset(segments
, 0, sizeof(segments
));
102 memset(packets
, 0, sizeof(packets
));
105 ogg_page::ogg_page(const char* buffer
, size_t& advance
) throw(std::runtime_error
)
107 //Check validity of page header.
108 if(buffer
[0] != 'O' || buffer
[1] != 'g' || buffer
[2] != 'g' || buffer
[3] != 'S')
109 throw std::runtime_error("Bad Ogg page header");
111 throw std::runtime_error("Bad Ogg page version");
113 throw std::runtime_error("Bad Ogg page flags");
115 size_t b
= 27 + (unsigned char)buffer
[26];
116 for(unsigned i
= 0; i
< (unsigned char)buffer
[26]; i
++)
117 b
+= (unsigned char)buffer
[27 + i
];
119 uint32_t claimed
= read32ule(buffer
+ 22);
121 uint32_t actual
= ogg_crc32(0, NULL
, 0);
122 actual
= ogg_crc32(actual
, reinterpret_cast<const uint8_t*>(buffer
), 22);
123 actual
= ogg_crc32(actual
, reinterpret_cast<const uint8_t*>(&x
), 4);
124 actual
= ogg_crc32(actual
, reinterpret_cast<const uint8_t*>(buffer
+ 26), b
- 26);
125 if(claimed
!= actual
)
126 throw std::runtime_error("Bad Ogg page checksum");
127 //This packet is valid.
129 uint8_t flags
= buffer
[5];
130 flag_continue
= (flags
& 1);
131 flag_bos
= (flags
& 2);
132 flag_eos
= (flags
& 4);
133 granulepos
= read64ule(buffer
+ 6);
134 stream
= read32ule(buffer
+ 14);
135 sequence
= read32ule(buffer
+ 18);
136 segment_count
= buffer
[26];
137 memset(segments
, 0, sizeof(segments
));
139 memcpy(segments
, buffer
+ 27, segment_count
);
140 memset(data
, 0, sizeof(data
));
141 if(b
> 27 + segment_count
)
142 memcpy(data
, buffer
+ 27 + segment_count
, b
- 27 - segment_count
);
144 memset(packets
, 0, sizeof(packets
));
145 if(segment_count
> 0)
146 packets
[packet_count
++] = 0;
148 for(unsigned i
= 0; i
< segment_count
; i
++) {
150 if(segment_count
> i
+ 1 && segments
[i
] < 255)
151 packets
[packet_count
++] = dptr
;
153 packets
[packet_count
] = dptr
;
154 last_incomplete
= (!flag_eos
&& segment_count
> 0 && segments
[segment_count
- 1] == 255);
158 bool ogg_page::scan(const char* buffer
, size_t bufferlen
, bool eof
, size_t& advance
) throw()
160 const char* _buffer
= buffer
;
161 size_t buffer_left
= bufferlen
;
162 int capture_state
= 0;
164 while(buffer_left
>= 27) {
165 //Check capture pattern.
166 if(_buffer
[0] != 'O' || _buffer
[1] != 'g' || _buffer
[2] != 'g' || _buffer
[3] != 'S') {
172 //Check that version is valid.
173 if(_buffer
[4] != 0) {
179 //Check that flags are valid.
180 if(_buffer
[5] & 0xF8) {
186 //Check that segment table is present. If not, more data can uncover a page here.
187 if(27 + (unsigned char)_buffer
[26] > buffer_left
) {
197 //Check that all data is there. If not, more data can uncover a page here.
198 size_t b
= 27 + (unsigned char)_buffer
[26];
199 for(unsigned i
= 0; i
< (unsigned char)_buffer
[26]; i
++)
200 b
+= (unsigned char)_buffer
[27 + i
];
201 if(b
> buffer_left
) {
212 uint32_t claimed
= read32ule(_buffer
+ 22);
214 uint32_t actual
= ogg_crc32(0, NULL
, 0);
215 actual
= ogg_crc32(actual
, reinterpret_cast<const uint8_t*>(_buffer
), 22);
216 actual
= ogg_crc32(actual
, reinterpret_cast<const uint8_t*>(&x
), 4);
217 actual
= ogg_crc32(actual
, reinterpret_cast<const uint8_t*>(_buffer
+ 26), b
- 26);
218 if(claimed
!= actual
) {
219 //CRC check fails. Advance.
225 return true; //Here is a packet.
227 if(eof
&& buffer_left
< 27) {
228 //Advance to the end.
229 advance
+= buffer_left
;
233 bool ogg_page::append_packet(const uint8_t* _data
, size_t datalen
) throw()
235 //Compute the smallest amount of data we can't write.
236 size_t imin
= (255 - segment_count
) * 255;
238 return false; //Can't write.
239 //Okay, it fits. Write.
240 packets
[packet_count
++] = data_count
;
241 bool terminate
= false;
244 segments
[segment_count
++] = 255;
245 memcpy(data
+ data_count
, _data
, 255);
250 segments
[segment_count
++] = datalen
;
251 memcpy(data
+ data_count
, _data
, datalen
);
252 data_count
+= datalen
;
259 segments
[segment_count
++] = 0;
260 packets
[packet_count
] = data_count
;
261 last_incomplete
= false;
265 bool ogg_page::append_packet_incomplete(const uint8_t*& _data
, size_t& datalen
) throw()
267 //If we have absolutely no space, don't flag a packet.
268 if(segment_count
== 255)
270 packets
[packet_count
++] = data_count
;
271 //Append segments, one by one.
272 while(segment_count
< 255) {
274 segments
[segment_count
++] = 255;
275 memcpy(data
+ data_count
, _data
, 255);
280 //Final segment of packet.
281 segments
[segment_count
++] = datalen
;
282 memcpy(data
+ data_count
, _data
, datalen
);
283 data_count
+= datalen
;
286 packets
[packet_count
] = data_count
;
287 last_incomplete
= false;
291 packets
[packet_count
] = data_count
;
292 last_incomplete
= true;
296 void ogg_page::serialize(char* buffer
) const throw()
298 memcpy(buffer
, "OggS", 4);
300 buffer
[5] = (flag_continue
? 1 : 0) | (flag_bos
? 2 : 0) | (flag_eos
? 4 : 0);
301 write64ule(buffer
+ 6, granulepos
);
302 write32ule(buffer
+ 14, stream
);
303 write32ule(buffer
+ 18, sequence
);
304 write32ule(buffer
+ 22, 0); //CRC will be fixed later.
305 buffer
[26] = segment_count
;
306 memcpy(buffer
+ 27, segments
, segment_count
);
307 memcpy(buffer
+ 27 + segment_count
, data
, data_count
);
308 size_t plen
= 27 + segment_count
+ data_count
;
310 write32ule(buffer
+ 22, ogg_crc32(ogg_crc32(0, NULL
, 0), reinterpret_cast<uint8_t*>(buffer
), plen
));
313 const uint64_t ogg_page::granulepos_none
= 0xFFFFFFFFFFFFFFFFULL
;
316 struct oggopus_header
parse_oggopus_header(struct ogg_page
& page
) throw(std::runtime_error
)
318 struct oggopus_header h
;
319 if(page
.get_packet_count() != 1 || page
.get_last_packet_incomplete() || page
.get_continue())
320 throw std::runtime_error("OggOpus header page must have one complete packet");
321 if(!page
.get_bos() || page
.get_eos())
322 throw std::runtime_error("OggOpus header page must be first but not last page");
323 auto p
= page
.get_packet(0);
324 if(p
.second
< 8 || memcmp(p
.first
, "OpusHead", 8))
325 throw std::runtime_error("Bad OggOpus header magic");
326 if(p
.second
< 19 || (p
.first
[18] && p
.second
< 21 + p
.first
[5]))
327 throw std::runtime_error("OggOpus header packet truncated");
329 throw std::runtime_error("Zero channels not allowed");
330 if(p
.first
[8] & 0xF0)
331 throw std::runtime_error("Unsupported OggOpus version");
332 h
.version
= p
.first
[8];
333 h
.channels
= p
.first
[9];
334 h
.preskip
= read16ule(p
.first
+ 10);
335 h
.rate
= read32ule(p
.first
+ 12);
336 h
.gain
= read16sle(p
.first
+ 16);
337 h
.map_family
= p
.first
[18];
338 memset(h
.chanmap
, 255, sizeof(h
.chanmap
));
340 h
.streams
= p
.first
[19];
341 h
.coupled
= p
.first
[20];
342 if(h
.coupled
> h
.streams
)
343 throw std::runtime_error("More coupled streams than total streams.");
344 if(static_cast<int>(h
.streams
) > 255 - h
.coupled
)
345 throw std::runtime_error("Maximum of 255 physical channels exceeded");
346 memcpy(h
.chanmap
, p
.first
+ 21, h
.channels
);
347 for(unsigned i
= 0; i
< h
.channels
; i
++)
348 if(h
.chanmap
[i
] != 255 && h
.chanmap
[i
] > h
.streams
+ h
.coupled
)
349 throw std::runtime_error("Logical channel mapped to invalid physical channel");
353 throw std::runtime_error("Only 1 or 2 channels allowed with mapping family 0");
354 h
.coupled
= (h
.channels
== 2) ? 1 : 0;
356 if(h
.channels
== 2) h
.chanmap
[1] = 1;
361 struct oggopus_tags
parse_oggopus_tags(struct ogg_page
& page
) throw(std::bad_alloc
, std::runtime_error
)
363 struct oggopus_tags h
;
364 if(page
.get_packet_count() != 1 || page
.get_last_packet_incomplete() || page
.get_continue())
365 throw std::runtime_error("OggOpus tags page must have one complete packet");
367 throw std::runtime_error("OggOpus tags page must not be first page");
368 auto p
= page
.get_packet(0);
369 if(p
.second
< 8 || memcmp(p
.first
, "OpusTags", 8))
370 throw std::runtime_error("Bad OggOpus tags magic");
372 throw std::runtime_error("OggOpus header packet truncated");
376 itr
= itr
+ 4 + read32ule(p
.first
+ itr
);
377 if(itr
+ 4 > p
.second
)
378 throw std::runtime_error("OggOpus header packet truncated");
379 h
.vendor
= std::string(p
.first
+ oitr
+ 4, p
.first
+ itr
);
381 uint32_t headers
= read32ule(p
.first
+ itr
);
383 for(uint32_t i
= 0; i
< headers
; i
++) {
384 if(itr
+ 4 > p
.second
)
385 throw std::runtime_error("OggOpus header packet truncated");
386 itr
= itr
+ 4 + read32ule(p
.first
+ itr
);
387 h
.comments
.push_back(std::string(p
.first
+ oitr
+ 4, p
.first
+ itr
));
391 throw std::runtime_error("OggOpus header packet truncated");
395 struct ogg_page
serialize_oggopus_header(struct oggopus_header
& header
) throw(std::runtime_error
)
397 struct ogg_page page
;
398 unsigned char buffer
[276];
400 if(header
.version
!= 1)
401 throw std::runtime_error("Don't how to serialize this oggopus version");
402 if(!header
.channels
|| (header
.channels
> 2 && !header
.map_family
))
403 throw std::runtime_error("Illegal channel count");
404 if(header
.map_family
&& static_cast<int>(header
.streams
) > 255 - header
.coupled
)
405 throw std::runtime_error("Maximum of 255 physical channels exceeded");
406 if(header
.map_family
)
407 for(unsigned i
= 0; i
< header
.channels
; i
++)
408 if(header
.chanmap
[i
] != 255 && header
.chanmap
[i
] > header
.streams
+ header
.coupled
)
409 throw std::runtime_error("Logical channel mapped to invalid physical channel");
410 write64ube(buffer
, 0x4F70757348656164ULL
);
411 buffer
[8] = header
.version
;
412 buffer
[9] = header
.channels
;
413 write16ule(buffer
+ 10, header
.preskip
);
414 write32ule(buffer
+ 12, header
.rate
);
415 write16sle(buffer
+ 16, header
.gain
);
416 buffer
[18] = header
.map_family
;
417 if(header
.map_family
) {
418 buffer
[19] = header
.streams
;
419 buffer
[20] = header
.coupled
;
420 memcpy(buffer
+ 21, header
.chanmap
, header
.channels
);
421 bsize
= 21 + header
.channels
;
424 if(!page
.append_packet(buffer
, bsize
))
425 throw std::runtime_error("Header packet too large");
426 page
.set_granulepos(0);
427 page
.set_sequence(0);
432 struct ogg_page
serialize_oggopus_tags(struct oggopus_tags
& tags
) throw(std::runtime_error
)
434 struct ogg_page page
;
436 bool toolarge
= false;
437 toolarge
|= (tags
.vendor
.length() > 65016);
438 needed
+= tags
.vendor
.length();
439 toolarge
|= (tags
.comments
.size() > 16254);
440 for(auto i
: tags
.comments
) {
441 toolarge
|= (tags
.comments
.size() > 65016);
442 needed
+= (i
.length() + 4);
444 if(needed
> 65016 || toolarge
)
445 throw std::runtime_error("Set of comments too large");
446 uint8_t buffer
[65024];
448 write64ube(buffer
, 0x4F70757354616773ULL
);
449 write32ule(buffer
+ 8, tags
.vendor
.length());
450 std::copy(tags
.vendor
.begin(), tags
.vendor
.end(), reinterpret_cast<char*>(buffer
+ 12));
451 itr
= 12 + tags
.vendor
.length();
452 write32ule(buffer
+ itr
, tags
.comments
.size());
454 for(auto i
: tags
.comments
) {
455 write32ule(buffer
+ itr
, i
.length());
456 std::copy(i
.begin(), i
.end(), reinterpret_cast<char*>(buffer
+ itr
+ 4));
457 itr
+= (i
.length() + 4);
459 if(!page
.append_packet(buffer
, itr
))
460 throw std::runtime_error("Comment packet too large");
461 page
.set_granulepos(0);
462 page
.set_sequence(1);
466 ogg_stream_reader::ogg_stream_reader() throw()
470 errors_to
= &std::cerr
;
473 ogg_stream_reader::~ogg_stream_reader() throw()
477 void ogg_stream_reader::set_errors_to(std::ostream
& os
)
482 bool ogg_stream_reader::get_page(ogg_page
& page
) throw(std::exception
)
490 f
= ogg_page::scan(buffer
, left
, eof
, advance
);
492 //The ogg stream resyncs.
493 (*errors_to
) << "Warning: Ogg stream: Recapture after " << advance
<< " bytes." << std::endl
;
494 discard_buffer(advance
);
499 page
= ogg_page(buffer
, advance
);
500 discard_buffer(advance
);
504 void ogg_stream_reader::fill_buffer()
507 if(!eof
&& left
< sizeof(buffer
)) {
508 left
+= (r
= read(buffer
+ left
, sizeof(buffer
) - left
));
514 void ogg_stream_reader::discard_buffer(size_t amount
)
517 memmove(buffer
, buffer
+ amount
, left
- amount
);
521 ogg_stream_writer::ogg_stream_writer() throw()
525 ogg_stream_writer::~ogg_stream_writer() throw()
529 void ogg_stream_writer::put_page(const ogg_page
& page
) throw(std::exception
)
532 size_t s
= page
.serialize_size();
533 page
.serialize(buffer
);
537 ogg_stream_reader_iostreams::ogg_stream_reader_iostreams(std::istream
& stream
)
542 ogg_stream_reader_iostreams::~ogg_stream_reader_iostreams() throw()
546 size_t ogg_stream_reader_iostreams::read(char* buffer
, size_t size
) throw(std::exception
)
550 is
.read(buffer
, size
);
554 ogg_stream_writer_iostreams::ogg_stream_writer_iostreams(std::ostream
& stream
)
559 ogg_stream_writer_iostreams::~ogg_stream_writer_iostreams() throw()
563 void ogg_stream_writer_iostreams::write(const char* buffer
, size_t size
) throw(std::exception
)
566 throw std::runtime_error("Error writing data");
567 os
.write(buffer
, size
);
569 throw std::runtime_error("Error writing data");