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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/*
* Copyright 2015 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
* by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: vorbis coded test suite using vorbisfile
last mod: $Id$
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisenc.h>
#define DATA_LEN 2048
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))
/* The following function is basically a hacked version of the code in
* examples/encoder_example.c */
void
write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch)
{
FILE * file ;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
vorbis_info vi;
vorbis_comment vc;
vorbis_dsp_state vd;
vorbis_block vb;
int eos = 0, ret;
if ((file = fopen (filename, "wb")) == NULL) {
printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
exit (1) ;
}
/********** Encode setup ************/
vorbis_info_init (&vi);
ret = vorbis_encode_init_vbr (&vi,ch,srate,q);
if (ret) {
printf ("vorbis_encode_init_vbr return %d\n", ret) ;
exit (1) ;
}
vorbis_comment_init (&vc);
vorbis_comment_add_tag (&vc,"ENCODER","test/util.c");
vorbis_analysis_init (&vd,&vi);
vorbis_block_init (&vd,&vb);
ogg_stream_init (&os,12345678);
{
ogg_packet header;
ogg_packet header_comm;
ogg_packet header_code;
vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code);
ogg_stream_packetin (&os,&header);
ogg_stream_packetin (&os,&header_comm);
ogg_stream_packetin (&os,&header_code);
/* Ensures the audio data will start on a new page. */
while (!eos){
int result = ogg_stream_flush (&os,&og);
if (result == 0)
break;
fwrite (og.header,1,og.header_len,file);
fwrite (og.body,1,og.body_len,file);
}
}
{
/* expose the buffer to submit data */
float **buffer = vorbis_analysis_buffer (&vd,count);
int i;
for(i=0;i<ch;i++)
memcpy (buffer [i], data, count * sizeof (float)) ;
/* tell the library how much we actually submitted */
vorbis_analysis_wrote (&vd,count);
vorbis_analysis_wrote (&vd,0);
}
while (vorbis_analysis_blockout (&vd,&vb) == 1) {
vorbis_analysis (&vb,NULL);
vorbis_bitrate_addblock (&vb);
while (vorbis_bitrate_flushpacket (&vd,&op)) {
ogg_stream_packetin (&os,&op);
while (!eos) {
int result = ogg_stream_pageout (&os,&og);
if (result == 0)
break;
fwrite (og.header,1,og.header_len,file);
fwrite (og.body,1,og.body_len,file);
if (ogg_page_eos (&og))
eos = 1;
}
}
}
ogg_stream_clear (&os);
vorbis_block_clear (&vb);
vorbis_dsp_clear (&vd);
vorbis_comment_clear (&vc);
vorbis_info_clear (&vi);
fclose (file) ;
}
/* The following function is basically a hacked version of the code in
* examples/decoder_example.c */
void
read_vorbis_data_or_die (const char *filename, int srate, float * data, int count)
{
ogg_sync_state oy;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
vorbis_info vi;
vorbis_comment vc;
vorbis_dsp_state vd;
vorbis_block vb;
FILE *file;
char *buffer;
int bytes;
int eos = 0;
int i;
int read_total = 0 ;
if ((file = fopen (filename, "rb")) == NULL) {
printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
exit (1) ;
}
ogg_sync_init (&oy);
{
/* fragile! Assumes all of our headers will fit in the first 8kB,
which currently they will */
buffer = ogg_sync_buffer (&oy,8192);
bytes = fread (buffer,1,8192,file);
ogg_sync_wrote (&oy,bytes);
if(ogg_sync_pageout (&oy,&og) != 1) {
if(bytes < 8192) {
printf ("Out of data.\n") ;
goto done_decode ;
}
fprintf (stderr,"Input does not appear to be an Ogg bitstream.\n");
exit (1);
}
ogg_stream_init (&os,ogg_page_serialno(&og));
vorbis_info_init (&vi);
vorbis_comment_init (&vc);
if (ogg_stream_pagein (&os,&og) < 0) {
fprintf (stderr,"Error reading first page of Ogg bitstream data.\n");
exit (1);
}
if (ogg_stream_packetout(&os,&op) != 1) {
fprintf (stderr,"Error reading initial header packet.\n");
exit (1);
}
if (vorbis_synthesis_headerin (&vi,&vc,&op) < 0) {
fprintf (stderr,"This Ogg bitstream does not contain Vorbis "
"audio data.\n");
exit (1);
}
i = 0;
while ( i < 2) {
while (i < 2) {
int result = ogg_sync_pageout (&oy,&og);
if(result == 0)
break;
if(result==1) {
ogg_stream_pagein(&os,&og);
while (i < 2) {
result = ogg_stream_packetout (&os,&op);
if (result == 0) break;
if (result < 0) {
fprintf (stderr,"Corrupt secondary header. Exiting.\n");
exit(1);
}
vorbis_synthesis_headerin (&vi,&vc,&op);
i++;
}
}
}
buffer = ogg_sync_buffer (&oy,4096);
bytes = fread (buffer,1,4096,file);
if (bytes == 0 && i < 2) {
fprintf (stderr,"End of file before finding all Vorbis headers!\n");
exit (1);
}
ogg_sync_wrote (&oy,bytes);
}
if (vi.rate != srate) {
printf ("\n\nError : File '%s' has sample rate of %ld when it should be %d.\n\n", filename, vi.rate, srate);
exit (1) ;
}
vorbis_synthesis_init (&vd,&vi);
vorbis_block_init (&vd,&vb);
while(!eos) {
while (!eos) {
int result = ogg_sync_pageout (&oy,&og);
if (result == 0)
break;
if (result < 0) {
fprintf (stderr,"Corrupt or missing data in bitstream; "
"continuing...\n");
} else {
ogg_stream_pagein (&os,&og);
while (1) {
result = ogg_stream_packetout (&os,&op);
if (result == 0)
break;
if (result < 0) {
/* no reason to complain; already complained above */
} else {
float **pcm;
int samples;
if (vorbis_synthesis (&vb,&op) == 0)
vorbis_synthesis_blockin(&vd,&vb);
while ((samples = vorbis_synthesis_pcmout (&vd,&pcm)) > 0 && read_total < count) {
int bout = samples < count ? samples : count;
bout = read_total + bout > count ? count - read_total : bout;
memcpy (data + read_total, pcm[0], bout * sizeof (float)) ;
vorbis_synthesis_read (&vd,bout);
read_total += bout ;
}
}
}
if (ogg_page_eos (&og)) eos = 1;
}
}
if (!eos) {
buffer = ogg_sync_buffer (&oy,4096);
bytes = fread (buffer,1,4096,file);
ogg_sync_wrote (&oy,bytes);
if (bytes == 0) eos = 1;
}
}
ogg_stream_clear (&os);
vorbis_block_clear (&vb);
vorbis_dsp_clear (&vd);
vorbis_comment_clear (&vc);
vorbis_info_clear (&vi);
}
done_decode:
/* OK, clean up the framer */
ogg_sync_clear (&oy);
fclose (file) ;
}
void
gen_windowed_sine (float *data, int len, float maximum)
{ int k ;
memset (data, 0, len * sizeof (float)) ;
len /= 2 ;
for (k = 0 ; k < len ; k++)
{ data [k] = sin (2.0 * k * M_PI * 1.0 / 32.0 + 0.4) ;
/* Apply Hanning Window. */
data [k] *= maximum * (0.5 - 0.5 * cos (2.0 * M_PI * k / ((len) - 1))) ;
}
return ;
}
void
set_data_in (float * data, unsigned len, float value)
{ unsigned k ;
for (k = 0 ; k < len ; k++)
data [k] = value ;
}
static int check_output (const float * data_in, unsigned len, float allowable);
int
main(void){
static float data_out [DATA_LEN] ;
static float data_in [DATA_LEN] ;
/* Do safest and most used sample rates first. */
int sample_rates [] = { 44100, 48000, 32000, 22050, 16000, 96000 } ;
unsigned k ;
int errors = 0 ;
int ch;
gen_windowed_sine (data_out, ARRAY_LEN (data_out), 0.95);
for(ch=1;ch<=8;ch++){
float q=-.05;
printf("\nTesting %d channel%s\n\n",ch,ch==1?"":"s");
while(q<1.){
for (k = 0 ; k < ARRAY_LEN (sample_rates); k ++) {
char filename [64] ;
snprintf (filename, sizeof (filename), "vorbis_%dch_q%.1f_%u.ogg", ch,q*10,sample_rates [k]);
printf (" %-20s : ", filename);
fflush (stdout);
/* Set to know value. */
set_data_in (data_in, ARRAY_LEN (data_in), 3.141);
write_vorbis_data_or_die (filename, sample_rates [k], q, data_out, ARRAY_LEN (data_out),ch);
read_vorbis_data_or_die (filename, sample_rates [k], data_in, ARRAY_LEN (data_in));
if (check_output (data_in, ARRAY_LEN (data_in), (.15f - .1f*q)) != 0)
errors ++ ;
else {
puts ("ok");
remove (filename);
}
}
q+=.1;
}
}
if (errors)
exit (1);
printf("ALL OK\n");
return 0;
}
static int
check_output (const float * data_in, unsigned len, float allowable)
{
float max_abs = 0.0 ;
unsigned k ;
for (k = 0 ; k < len ; k++) {
float temp = fabs (data_in [k]);
max_abs = MAX (max_abs, temp);
}
if (max_abs < 0.95-allowable) {
printf ("Error : max_abs (%f) too small.\n", max_abs);
return 1 ;
} else if (max_abs > .95+allowable) {
printf ("Error : max_abs (%f) too big.\n", max_abs);
return 1 ;
}
return 0 ;
}
|