Debian Codegraph @

src/ogg.cc

raw symbols SCIP
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>
12
13 #include <errno.h>
14 #include <string.h>
15
16 bool ot::is_opus_stream(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 ot::ogg_reader::next_page()
26 {
27 int rc;
28 while ((rc = ogg_sync_pageout(&sync, &page)) != 1) {
29 if (rc == -1) {
30 throw status {st::bad_stream,
31 absolute_page_no == (size_t) -1 ? "Input is not a valid Ogg file."
32 : "Unsynced data in stream."};
33 }
34 if (ogg_sync_check(&sync) != 0)
35 throw status {st::libogg_error, "ogg_sync_check signalled an error."};
36 if (feof(file)) {
37 if (sync.fill != sync.returned)
38 throw status {st::bad_stream, "Unsynced data at end of stream."};
39 return false; // end of sream
40 }
41 char* buf = ogg_sync_buffer(&sync, 65536);
42 if (buf == nullptr)
43 throw status {st::libogg_error, "ogg_sync_buffer failed."};
44 size_t len = fread(buf, 1, 65536, file);
45 if (ferror(file))
46 throw status {st::standard_error, "fread error: "s + strerror(errno)};
47 if (ogg_sync_wrote(&sync, len) != 0)
48 throw status {st::libogg_error, "ogg_sync_wrote failed."};
49 }
50 ++absolute_page_no;
51 return true;
52 }
53
54 void ot::ogg_reader::process_header_packet(const std::function<void(ogg_packet&)>& f)
55 {
56 if (ogg_page_continued(&page))
57 throw status {ot::st::error, "Unexpected continued header page."};
58
59 ogg_packet packet;
60 ogg_logical_stream stream(ogg_page_serialno(&page));
61 stream.pageno = ogg_page_pageno(&page);
62
63 for (;;) {
64 if (ogg_stream_pagein(&stream, &page) != 0)
65 throw status {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 status {ot::st::libogg_error, "ogg_stream_packetout failed."};
70 } else if (rc == 0) {
71 // Not enough data: read the next page.
72 if (!next_page())
73 throw status {ot::st::error, "Unterminated header packet."};
74 continue;
75 } else {
76 // The packet was successfully read.
77 break;
78 }
79 }
80
81 f(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 status {ot::st::error, "Header page contains more than a single packet."};
90 }
91
92 void ot::ogg_writer::write_page(const ogg_page& page)
93 {
94 if (page.header_len < 0 || page.body_len < 0)
95 throw status {st::int_overflow, "Overflowing page length"};
96
97 long pageno = ogg_page_pageno(&page);
98 if (pageno != next_page_no)
99 fprintf(stderr, "Output page number mismatch: expected %ld, got %ld.\n", next_page_no, pageno);
100 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, file) < header_len)
105 throw status {st::standard_error, "fwrite error: "s + strerror(errno)};
106 if (fwrite(page.body, 1, body_len, file) < body_len)
107 throw status {st::standard_error, "fwrite error: "s + strerror(errno)};
108 }
109
110 void ot::ogg_writer::write_header_packet(int serialno, int pageno, ogg_packet& packet)
111 {
112 ogg_logical_stream stream(serialno);
113 stream.b_o_s = (pageno != 0);
114 stream.pageno = pageno;
115 if (ogg_stream_packetin(&stream, &packet) != 0)
116 throw status {ot::st::libogg_error, "ogg_stream_packetin failed"};
117
118 ogg_page page;
119 while (ogg_stream_flush(&stream, &page) != 0)
120 write_page(page);
121
122 if (ogg_stream_check(&stream) != 0)
123 throw status {st::libogg_error, "ogg_stream_check failed"};
124 }
125
126 void ot::renumber_page(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 = htole32(new_pageno);
135 memcpy(&page.header[18], &le_pageno, 4);
136 ogg_page_checksum_set(&page);
137 }