| 1 | /** |
| 2 | * \file src/ogg.c |
| 3 | * \ingroup ogg |
| 4 | * |
| 5 | * High-level interface for libogg. |
| 6 | * |
| 7 | * This module is not meant to be a complete libogg wrapper, but rather a convenient and highly |
| 8 | * specialized layer above libogg and stdio. |
| 9 | */ |
| 10 | |
| 11 | #include <opustags.h>cxx . . $ `<file>/src/opustags.h`/ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | bool otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(const ogg_page& identification_header) |
| 17 | { |
| 18 | if (ogg_page_bos(&identification_header) == 0) |
| 19 | return false; |
| 20 | if (identification_header.body_len < 8) |
| 21 | return false; |
| 22 | return (memcmp(identification_header.body, "OpusHead", 8) == 0); |
| 23 | } |
| 24 | |
| 25 | bool otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader#::next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() |
| 26 | { |
| 27 | int rc; |
| 28 | while ((rc = ogg_sync_pageout(&synccxx . . $ ot/ogg_reader#sync., &pagecxx . . $ ot/ogg_reader#page.)) != 1) { |
| 29 | if (rc == -1) { |
| 30 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_streamcxx . . $ ot/st#bad_stream., |
| 31 | absolute_page_nocxx . . $ ot/ogg_reader#absolute_page_no. == (size_t) -1 ? "Input is not a valid Ogg file." |
| 32 | : "Unsynced data in stream."}; |
| 33 | } |
| 34 | if (ogg_sync_check(&synccxx . . $ ot/ogg_reader#sync.) != 0) |
| 35 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_sync_check signalled an error."}; |
| 36 | if (feof(filecxx . . $ ot/ogg_reader#file.)) { |
| 37 | if (synccxx . . $ ot/ogg_reader#sync..fill != synccxx . . $ ot/ogg_reader#sync..returned) |
| 38 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_streamcxx . . $ ot/st#bad_stream., "Unsynced data at end of stream."}; |
| 39 | return false; // end of sream |
| 40 | } |
| 41 | char* buf = ogg_sync_buffer(&synccxx . . $ ot/ogg_reader#sync., 65536); |
| 42 | if (buf == nullptr) |
| 43 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_sync_buffer failed."}; |
| 44 | size_t len = fread(buf, 1, 65536, filecxx . . $ ot/ogg_reader#file.); |
| 45 | if (ferror(filecxx . . $ ot/ogg_reader#file.)) |
| 46 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "fread error: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 47 | if (ogg_sync_wrote(&synccxx . . $ ot/ogg_reader#sync., len) != 0) |
| 48 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_sync_wrote failed."}; |
| 49 | } |
| 50 | ++absolute_page_nocxx . . $ ot/ogg_reader#absolute_page_no.; |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader#::process_header_packetcxx . . $ ot/ogg_reader#process_header_packet(4de049347c6f80eb).(const stdcxx . . $ std/::functioncxx . . $ std/function#<void(ogg_packet&)>& f) |
| 55 | { |
| 56 | if (ogg_page_continued(&pagecxx . . $ ot/ogg_reader#page.)) |
| 57 | throw statuscxx . . $ ot/status#status(15c445588664906). {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Unexpected continued header page."}; |
| 58 | |
| 59 | ogg_packet packet; |
| 60 | ogg_logical_streamcxx . . $ ot/ogg_logical_stream# streamcxx . . $ ot/ogg_logical_stream#ogg_logical_stream(d4f767463ce0a6b3).(ogg_page_serialno(&pagecxx . . $ ot/ogg_reader#page.)); |
| 61 | stream.pageno = ogg_page_pageno(&pagecxx . . $ ot/ogg_reader#page.); |
| 62 | |
| 63 | for (;;) { |
| 64 | if (ogg_stream_pagein(&stream, &pagecxx . . $ ot/ogg_reader#page.) != 0) |
| 65 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_stream_pagein failed."}; |
| 66 | |
| 67 | int rc = ogg_stream_packetout(&stream, &packet); |
| 68 | if (ogg_stream_check(&stream) != 0 || rc == -1) { |
| 69 | throw statuscxx . . $ ot/status#status(15c445588664906). {otcxx . . $ ot/::stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_stream_packetout failed."}; |
| 70 | } else if (rc == 0) { |
| 71 | // Not enough data: read the next page. |
| 72 | if (!next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).()) |
| 73 | throw statuscxx . . $ ot/status#status(15c445588664906). {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Unterminated header packet."}; |
| 74 | continue; |
| 75 | } else { |
| 76 | // The packet was successfully read. |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | f(cxx . . $ std/function#`operator()`(39e0826ffb540481).packet); |
| 82 | |
| 83 | /* Ensure that there are no other segments left in the packet using the lacing state of the |
| 84 | * stream. These are the relevant variables, as far as I understood them: |
| 85 | * - lacing_vals: extensible array containing the lacing values of the segments, |
| 86 | * - lacing_fill: number of elements in lacing_vals (not the capacity), |
| 87 | * - lacing_returned: index of the next segment to be processed. */ |
| 88 | if (stream.lacing_returned != stream.lacing_fill) |
| 89 | throw statuscxx . . $ ot/status#status(15c445588664906). {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Header page contains more than a single packet."}; |
| 90 | } |
| 91 | |
| 92 | void otcxx . . $ ot/::ogg_writercxx . . $ ot/ogg_writer#::write_pagecxx . . $ ot/ogg_writer#write_page(40553eaf79898ae3).(const ogg_page& page) |
| 93 | { |
| 94 | if (page.header_len < 0 || page.body_len < 0) |
| 95 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::int_overflowcxx . . $ ot/st#int_overflow., "Overflowing page length"}; |
| 96 | |
| 97 | long pageno = ogg_page_pageno(&page); |
| 98 | if (pageno != next_page_nocxx . . $ ot/ogg_writer#next_page_no.) |
| 99 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Output page number mismatch: expected %ld, got %ld.\n", next_page_nocxx . . $ ot/ogg_writer#next_page_no., pageno); |
| 100 | next_page_nocxx . . $ ot/ogg_writer#next_page_no. = pageno + 1; |
| 101 | |
| 102 | auto header_len = static_cast<size_t>(page.header_len); |
| 103 | auto body_len = static_cast<size_t>(page.body_len); |
| 104 | if (fwrite(page.header, 1, header_len, filecxx . . $ ot/ogg_writer#file.) < header_len) |
| 105 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "fwrite error: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 106 | if (fwrite(page.body, 1, body_len, filecxx . . $ ot/ogg_writer#file.) < body_len) |
| 107 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "fwrite error: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 108 | } |
| 109 | |
| 110 | void otcxx . . $ ot/::ogg_writercxx . . $ ot/ogg_writer#::write_header_packetcxx . . $ ot/ogg_writer#write_header_packet(39685ba16f0fc2d4).(int serialno, int pageno, ogg_packet& packet) |
| 111 | { |
| 112 | ogg_logical_streamcxx . . $ ot/ogg_logical_stream# streamcxx . . $ ot/ogg_logical_stream#ogg_logical_stream(d4f767463ce0a6b3).(serialno); |
| 113 | stream.b_o_s = (pageno != 0); |
| 114 | stream.pageno = pageno; |
| 115 | if (ogg_stream_packetin(&stream, &packet) != 0) |
| 116 | throw statuscxx . . $ ot/status#status(15c445588664906). {otcxx . . $ ot/::stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_stream_packetin failed"}; |
| 117 | |
| 118 | ogg_page page; |
| 119 | while (ogg_stream_flush(&stream, &page) != 0) |
| 120 | write_pagecxx . . $ ot/ogg_writer#write_page(40553eaf79898ae3).(page); |
| 121 | |
| 122 | if (ogg_stream_check(&stream) != 0) |
| 123 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::libogg_errorcxx . . $ ot/st#libogg_error., "ogg_stream_check failed"}; |
| 124 | } |
| 125 | |
| 126 | void otcxx . . $ ot/::renumber_pagecxx . . $ ot/renumber_page(99e4e6107b845fea).(ogg_page& page, long new_pageno) |
| 127 | { |
| 128 | // Quick optimization: don’t bother recomputing the CRC if the pageno did not change. |
| 129 | long old_pageno = ogg_page_pageno(&page); |
| 130 | if (old_pageno == new_pageno) |
| 131 | return; |
| 132 | |
| 133 | /** The pageno field is located at bytes 18 to 21 (0-indexed, little-endian). */ |
| 134 | uint32_t le_pageno = htole32cxx . . $ `<external>/15855202785537844739/endian.h:45:11`!(new_pageno); |
| 135 | memcpy(&page.header[18], &le_pageno, 4); |
| 136 | ogg_page_checksum_set(&page); |
| 137 | } |