1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*C
(c) 2005 bl0rg.net
**/
#include "conf.h"
#include <assert.h>
#include <stdio.h>
#include "ogg.h"
#include "file.h"
#include "vorbis.h"
#include "buf.h"
#include "pack.h"
#include "bv.h"
#ifdef DEBUG
void vorbis_stream_print(vorbis_stream_t *vorbis) {
fprintf(stderr, "audio channels: %d, sample_rate: %lu\n",
vorbis->audio_channels, vorbis->audio_sample_rate);
fprintf(stderr, "bitrate max: %lu, nominal: %lu, min: %lu\n",
vorbis->bitrate_maximum, vorbis->bitrate_nominal,
vorbis->bitrate_minimum);
fprintf(stderr, "blocksize 0: %d, blocksize 1: %d\n",
vorbis->blocksize_0, vorbis->blocksize_1);
}
#endif /* DEBUG */
/*M
\emph{Unpack the Vorbis identification header and check it is
correct.}
**/
int vorbis_unpack_id_hdr(vorbis_stream_t *vorbis) {
assert(vorbis != NULL);
assert(vorbis->id_hdr.data != NULL);
assert(vorbis->id_hdr.len == VORBIS_ID_HDR_SIZE);
unsigned char *ptr = vorbis->id_hdr.data + VORBIS_HDR_SIZE;
vorbis->vorbis_version = LE_UINT32_UNPACK(ptr);
vorbis->audio_channels = UINT8_UNPACK(ptr);
vorbis->audio_sample_rate = LE_UINT32_UNPACK(ptr);
vorbis->bitrate_maximum = LE_UINT32_UNPACK(ptr);
vorbis->bitrate_nominal = LE_UINT32_UNPACK(ptr);
vorbis->bitrate_minimum = LE_UINT32_UNPACK(ptr);
bv_t bv;
bv_init(&bv, ptr, 8);
vorbis->blocksize_1 = bv_get_bits(&bv, 4);
vorbis->blocksize_0 = bv_get_bits(&bv, 4);
#ifdef DEBUG
vorbis_stream_print(vorbis);
#endif /* DEBUG */
if ((vorbis->vorbis_version != 0) ||
(vorbis->audio_channels == 0) ||
(vorbis->audio_sample_rate == 0) ||
(vorbis->blocksize_0 > vorbis->blocksize_1))
return 0;
return 1;
}
/*M
\emph{Read the packet data in the OGG page beginning at segment
\verb|vorbis->segment|, and append it to the buffer \verb|packet|.
**/
int vorbis_packet_in_page(vorbis_stream_t *vorbis, ogg_page_t *page,
buf_t *packet) {
for (; vorbis->segment < page->page_segments; vorbis->segment++) {
if (page->lacing_values[vorbis->segment] > 0) {
buf_append(packet, ogg_segment(page, vorbis->segment),
page->lacing_values[vorbis->segment]);
}
if (page->lacing_values[vorbis->segment] < 255) {
vorbis->segment++;
return 1;
}
}
/*M
Not enough segments for complete packet.
**/
return 0;
}
/*M
\emph{Read the next Vorbis packet from an OGG stream.}
**/
int vorbis_next_packet(vorbis_stream_t *vorbis, buf_t *packet) {
assert(vorbis != NULL);
assert(packet != NULL);
assert(packet->data != NULL);
packet->len = 0;
/*M
Check if we have to read in a new page.
**/
if (vorbis->segment >= vorbis->page.page_segments) {
if (!ogg_next_page(&vorbis->file, &vorbis->page))
return 0;
vorbis->segment = 0;
}
again:
if (vorbis_packet_in_page(vorbis, &vorbis->page, packet))
return 1;
/*M
Not enough segments for complete packet in page, read a new page
and hope it is a continuation page.
**/
if (!ogg_next_page(&vorbis->file, &vorbis->page))
return 0;
vorbis->segment = 0;
/*M
Check if the next page is a continuation page.
**/
if (vorbis->page.b.continuation == 0) {
fprintf(stderr, "Subsequent page was not continuation page.\n");
return 0;
}
goto again;
}
/*M
\emph{Read the Vorbis headers from an OGG stream.}
**/
int vorbis_stream_read_hdrs(vorbis_stream_t *vorbis) {
assert(vorbis != NULL);
/*M
Read in the first OGG page which should contain only one segment
containing the Vorbis identification header.
**/
if (!ogg_next_page(&vorbis->file, &vorbis->hdr_pages[0]))
return 0;
if (vorbis->hdr_pages[0].page_segments > 1)
return 0;
if (!vorbis_packet_in_page(vorbis, vorbis->hdr_pages, &vorbis->id_hdr) ||
!vorbis_check_packet(&vorbis->id_hdr, 1) ||
!vorbis_unpack_id_hdr(vorbis))
return 0;
int i = 1;
/* read next page containing start of comment header */
if (!ogg_next_page(&vorbis->file, vorbis->hdr_pages + i))
return 0;
vorbis->segment = 0;
while (i < VORBIS_MAX_HDR_PAGES) {
if (!vorbis_packet_in_page(vorbis, vorbis->hdr_pages + i,
&vorbis->comment_hdr)) {
i++;
if (!ogg_next_page(&vorbis->file, vorbis->hdr_pages + i))
return 0;
vorbis->segment = 0;
} else {
if (!vorbis_check_packet(&vorbis->comment_hdr, 3))
return 0;
break;
}
}
if (i == VORBIS_MAX_HDR_PAGES)
return 0;
while (i < VORBIS_MAX_HDR_PAGES) {
if (!vorbis_packet_in_page(vorbis, vorbis->hdr_pages + i,
&vorbis->setup_hdr)) {
i++;
if (!ogg_next_page(&vorbis->file, vorbis->hdr_pages + i))
return 0;
vorbis->segment = 0;
} else {
if (!vorbis_check_packet(&vorbis->setup_hdr, 5))
return 0;
/* must be the last segment in packet */
if (vorbis->segment < (vorbis->hdr_pages[i].page_segments))
return 0;
break;
}
}
vorbis->hdr_pages_cnt = i + 1;
return 1;
}
/*C
**/
#ifdef VORBIS_TEST
#include <stdlib.h>
int main(int argc, char *argv[]) {
int retval = EXIT_SUCCESS;
char *f;
if (!(f = *++argv)) {
fprintf(stderr, "Usage: vorbistest oggfile\n");
return 1;
}
ogg_init();
vorbis_stream_t vorbis;
vorbis_stream_init(&vorbis);
buf_t packet;
packet.len = 0;
packet.size = 0;
packet.data = NULL;
buf_alloc(&packet, 512);
if (!file_open_read(&vorbis.file, f)) {
fprintf(stderr, "Could not open ogg file: %s\n", f);
retval = EXIT_FAILURE;
goto exit;
}
if (!vorbis_stream_read_hdrs(&vorbis)) {
fprintf(stderr, "Stream is not a OGG encapsulated Vorbis stream\n");
retval = EXIT_FAILURE;
goto exit;
}
while (vorbis_next_packet(&vorbis, &packet) > 0) {
fprintf(stderr, "Packet length: %lu, size %lu\n", packet.len, packet.size);
fgetc(stdin);
}
file_close(&vorbis.file);
exit:
vorbis_stream_destroy(&vorbis);
buf_free(&packet);
return retval;
}
#endif /* VORBIS_TEST */
|