annotate src/libvorbis-1.3.3/lib/info.c @ 91:9fe94270bdf5

Further builds
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 20 Mar 2013 14:58:12 +0000
parents 98c1576536ae
children
rev   line source
cannam@86 1 /********************************************************************
cannam@86 2 * *
cannam@86 3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
cannam@86 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
cannam@86 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
cannam@86 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
cannam@86 7 * *
cannam@86 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
cannam@86 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
cannam@86 10 * *
cannam@86 11 ********************************************************************
cannam@86 12
cannam@86 13 function: maintain the info structure, info <-> header packets
cannam@86 14 last mod: $Id: info.c 18186 2012-02-03 22:08:44Z xiphmont $
cannam@86 15
cannam@86 16 ********************************************************************/
cannam@86 17
cannam@86 18 /* general handling of the header and the vorbis_info structure (and
cannam@86 19 substructures) */
cannam@86 20
cannam@86 21 #include <stdlib.h>
cannam@86 22 #include <string.h>
cannam@86 23 #include <ctype.h>
cannam@86 24 #include <ogg/ogg.h>
cannam@86 25 #include "vorbis/codec.h"
cannam@86 26 #include "codec_internal.h"
cannam@86 27 #include "codebook.h"
cannam@86 28 #include "registry.h"
cannam@86 29 #include "window.h"
cannam@86 30 #include "psy.h"
cannam@86 31 #include "misc.h"
cannam@86 32 #include "os.h"
cannam@86 33
cannam@86 34 #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.3"
cannam@86 35 #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20120203 (Omnipresent)"
cannam@86 36
cannam@86 37 /* helpers */
cannam@86 38 static int ilog2(unsigned int v){
cannam@86 39 int ret=0;
cannam@86 40 if(v)--v;
cannam@86 41 while(v){
cannam@86 42 ret++;
cannam@86 43 v>>=1;
cannam@86 44 }
cannam@86 45 return(ret);
cannam@86 46 }
cannam@86 47
cannam@86 48 static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
cannam@86 49
cannam@86 50 while(bytes--){
cannam@86 51 oggpack_write(o,*s++,8);
cannam@86 52 }
cannam@86 53 }
cannam@86 54
cannam@86 55 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
cannam@86 56 while(bytes--){
cannam@86 57 *buf++=oggpack_read(o,8);
cannam@86 58 }
cannam@86 59 }
cannam@86 60
cannam@86 61 void vorbis_comment_init(vorbis_comment *vc){
cannam@86 62 memset(vc,0,sizeof(*vc));
cannam@86 63 }
cannam@86 64
cannam@86 65 void vorbis_comment_add(vorbis_comment *vc,const char *comment){
cannam@86 66 vc->user_comments=_ogg_realloc(vc->user_comments,
cannam@86 67 (vc->comments+2)*sizeof(*vc->user_comments));
cannam@86 68 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
cannam@86 69 (vc->comments+2)*sizeof(*vc->comment_lengths));
cannam@86 70 vc->comment_lengths[vc->comments]=strlen(comment);
cannam@86 71 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
cannam@86 72 strcpy(vc->user_comments[vc->comments], comment);
cannam@86 73 vc->comments++;
cannam@86 74 vc->user_comments[vc->comments]=NULL;
cannam@86 75 }
cannam@86 76
cannam@86 77 void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
cannam@86 78 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
cannam@86 79 strcpy(comment, tag);
cannam@86 80 strcat(comment, "=");
cannam@86 81 strcat(comment, contents);
cannam@86 82 vorbis_comment_add(vc, comment);
cannam@86 83 }
cannam@86 84
cannam@86 85 /* This is more or less the same as strncasecmp - but that doesn't exist
cannam@86 86 * everywhere, and this is a fairly trivial function, so we include it */
cannam@86 87 static int tagcompare(const char *s1, const char *s2, int n){
cannam@86 88 int c=0;
cannam@86 89 while(c < n){
cannam@86 90 if(toupper(s1[c]) != toupper(s2[c]))
cannam@86 91 return !0;
cannam@86 92 c++;
cannam@86 93 }
cannam@86 94 return 0;
cannam@86 95 }
cannam@86 96
cannam@86 97 char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
cannam@86 98 long i;
cannam@86 99 int found = 0;
cannam@86 100 int taglen = strlen(tag)+1; /* +1 for the = we append */
cannam@86 101 char *fulltag = alloca(taglen+ 1);
cannam@86 102
cannam@86 103 strcpy(fulltag, tag);
cannam@86 104 strcat(fulltag, "=");
cannam@86 105
cannam@86 106 for(i=0;i<vc->comments;i++){
cannam@86 107 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
cannam@86 108 if(count == found)
cannam@86 109 /* We return a pointer to the data, not a copy */
cannam@86 110 return vc->user_comments[i] + taglen;
cannam@86 111 else
cannam@86 112 found++;
cannam@86 113 }
cannam@86 114 }
cannam@86 115 return NULL; /* didn't find anything */
cannam@86 116 }
cannam@86 117
cannam@86 118 int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
cannam@86 119 int i,count=0;
cannam@86 120 int taglen = strlen(tag)+1; /* +1 for the = we append */
cannam@86 121 char *fulltag = alloca(taglen+1);
cannam@86 122 strcpy(fulltag,tag);
cannam@86 123 strcat(fulltag, "=");
cannam@86 124
cannam@86 125 for(i=0;i<vc->comments;i++){
cannam@86 126 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
cannam@86 127 count++;
cannam@86 128 }
cannam@86 129
cannam@86 130 return count;
cannam@86 131 }
cannam@86 132
cannam@86 133 void vorbis_comment_clear(vorbis_comment *vc){
cannam@86 134 if(vc){
cannam@86 135 long i;
cannam@86 136 if(vc->user_comments){
cannam@86 137 for(i=0;i<vc->comments;i++)
cannam@86 138 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
cannam@86 139 _ogg_free(vc->user_comments);
cannam@86 140 }
cannam@86 141 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
cannam@86 142 if(vc->vendor)_ogg_free(vc->vendor);
cannam@86 143 memset(vc,0,sizeof(*vc));
cannam@86 144 }
cannam@86 145 }
cannam@86 146
cannam@86 147 /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
cannam@86 148 They may be equal, but short will never ge greater than long */
cannam@86 149 int vorbis_info_blocksize(vorbis_info *vi,int zo){
cannam@86 150 codec_setup_info *ci = vi->codec_setup;
cannam@86 151 return ci ? ci->blocksizes[zo] : -1;
cannam@86 152 }
cannam@86 153
cannam@86 154 /* used by synthesis, which has a full, alloced vi */
cannam@86 155 void vorbis_info_init(vorbis_info *vi){
cannam@86 156 memset(vi,0,sizeof(*vi));
cannam@86 157 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
cannam@86 158 }
cannam@86 159
cannam@86 160 void vorbis_info_clear(vorbis_info *vi){
cannam@86 161 codec_setup_info *ci=vi->codec_setup;
cannam@86 162 int i;
cannam@86 163
cannam@86 164 if(ci){
cannam@86 165
cannam@86 166 for(i=0;i<ci->modes;i++)
cannam@86 167 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
cannam@86 168
cannam@86 169 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
cannam@86 170 if(ci->map_param[i]) /* this may be cleaning up an aborted
cannam@86 171 unpack, in which case the below type
cannam@86 172 cannot be trusted */
cannam@86 173 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
cannam@86 174
cannam@86 175 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
cannam@86 176 if(ci->floor_param[i]) /* this may be cleaning up an aborted
cannam@86 177 unpack, in which case the below type
cannam@86 178 cannot be trusted */
cannam@86 179 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
cannam@86 180
cannam@86 181 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
cannam@86 182 if(ci->residue_param[i]) /* this may be cleaning up an aborted
cannam@86 183 unpack, in which case the below type
cannam@86 184 cannot be trusted */
cannam@86 185 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
cannam@86 186
cannam@86 187 for(i=0;i<ci->books;i++){
cannam@86 188 if(ci->book_param[i]){
cannam@86 189 /* knows if the book was not alloced */
cannam@86 190 vorbis_staticbook_destroy(ci->book_param[i]);
cannam@86 191 }
cannam@86 192 if(ci->fullbooks)
cannam@86 193 vorbis_book_clear(ci->fullbooks+i);
cannam@86 194 }
cannam@86 195 if(ci->fullbooks)
cannam@86 196 _ogg_free(ci->fullbooks);
cannam@86 197
cannam@86 198 for(i=0;i<ci->psys;i++)
cannam@86 199 _vi_psy_free(ci->psy_param[i]);
cannam@86 200
cannam@86 201 _ogg_free(ci);
cannam@86 202 }
cannam@86 203
cannam@86 204 memset(vi,0,sizeof(*vi));
cannam@86 205 }
cannam@86 206
cannam@86 207 /* Header packing/unpacking ********************************************/
cannam@86 208
cannam@86 209 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
cannam@86 210 codec_setup_info *ci=vi->codec_setup;
cannam@86 211 if(!ci)return(OV_EFAULT);
cannam@86 212
cannam@86 213 vi->version=oggpack_read(opb,32);
cannam@86 214 if(vi->version!=0)return(OV_EVERSION);
cannam@86 215
cannam@86 216 vi->channels=oggpack_read(opb,8);
cannam@86 217 vi->rate=oggpack_read(opb,32);
cannam@86 218
cannam@86 219 vi->bitrate_upper=oggpack_read(opb,32);
cannam@86 220 vi->bitrate_nominal=oggpack_read(opb,32);
cannam@86 221 vi->bitrate_lower=oggpack_read(opb,32);
cannam@86 222
cannam@86 223 ci->blocksizes[0]=1<<oggpack_read(opb,4);
cannam@86 224 ci->blocksizes[1]=1<<oggpack_read(opb,4);
cannam@86 225
cannam@86 226 if(vi->rate<1)goto err_out;
cannam@86 227 if(vi->channels<1)goto err_out;
cannam@86 228 if(ci->blocksizes[0]<64)goto err_out;
cannam@86 229 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
cannam@86 230 if(ci->blocksizes[1]>8192)goto err_out;
cannam@86 231
cannam@86 232 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
cannam@86 233
cannam@86 234 return(0);
cannam@86 235 err_out:
cannam@86 236 vorbis_info_clear(vi);
cannam@86 237 return(OV_EBADHEADER);
cannam@86 238 }
cannam@86 239
cannam@86 240 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
cannam@86 241 int i;
cannam@86 242 int vendorlen=oggpack_read(opb,32);
cannam@86 243 if(vendorlen<0)goto err_out;
cannam@86 244 if(vendorlen>opb->storage-8)goto err_out;
cannam@86 245 vc->vendor=_ogg_calloc(vendorlen+1,1);
cannam@86 246 _v_readstring(opb,vc->vendor,vendorlen);
cannam@86 247 i=oggpack_read(opb,32);
cannam@86 248 if(i<0)goto err_out;
cannam@86 249 if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out;
cannam@86 250 vc->comments=i;
cannam@86 251 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
cannam@86 252 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
cannam@86 253
cannam@86 254 for(i=0;i<vc->comments;i++){
cannam@86 255 int len=oggpack_read(opb,32);
cannam@86 256 if(len<0)goto err_out;
cannam@86 257 if(len>opb->storage-oggpack_bytes(opb))goto err_out;
cannam@86 258 vc->comment_lengths[i]=len;
cannam@86 259 vc->user_comments[i]=_ogg_calloc(len+1,1);
cannam@86 260 _v_readstring(opb,vc->user_comments[i],len);
cannam@86 261 }
cannam@86 262 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
cannam@86 263
cannam@86 264 return(0);
cannam@86 265 err_out:
cannam@86 266 vorbis_comment_clear(vc);
cannam@86 267 return(OV_EBADHEADER);
cannam@86 268 }
cannam@86 269
cannam@86 270 /* all of the real encoding details are here. The modes, books,
cannam@86 271 everything */
cannam@86 272 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
cannam@86 273 codec_setup_info *ci=vi->codec_setup;
cannam@86 274 int i;
cannam@86 275 if(!ci)return(OV_EFAULT);
cannam@86 276
cannam@86 277 /* codebooks */
cannam@86 278 ci->books=oggpack_read(opb,8)+1;
cannam@86 279 if(ci->books<=0)goto err_out;
cannam@86 280 for(i=0;i<ci->books;i++){
cannam@86 281 ci->book_param[i]=vorbis_staticbook_unpack(opb);
cannam@86 282 if(!ci->book_param[i])goto err_out;
cannam@86 283 }
cannam@86 284
cannam@86 285 /* time backend settings; hooks are unused */
cannam@86 286 {
cannam@86 287 int times=oggpack_read(opb,6)+1;
cannam@86 288 if(times<=0)goto err_out;
cannam@86 289 for(i=0;i<times;i++){
cannam@86 290 int test=oggpack_read(opb,16);
cannam@86 291 if(test<0 || test>=VI_TIMEB)goto err_out;
cannam@86 292 }
cannam@86 293 }
cannam@86 294
cannam@86 295 /* floor backend settings */
cannam@86 296 ci->floors=oggpack_read(opb,6)+1;
cannam@86 297 if(ci->floors<=0)goto err_out;
cannam@86 298 for(i=0;i<ci->floors;i++){
cannam@86 299 ci->floor_type[i]=oggpack_read(opb,16);
cannam@86 300 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
cannam@86 301 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
cannam@86 302 if(!ci->floor_param[i])goto err_out;
cannam@86 303 }
cannam@86 304
cannam@86 305 /* residue backend settings */
cannam@86 306 ci->residues=oggpack_read(opb,6)+1;
cannam@86 307 if(ci->residues<=0)goto err_out;
cannam@86 308 for(i=0;i<ci->residues;i++){
cannam@86 309 ci->residue_type[i]=oggpack_read(opb,16);
cannam@86 310 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
cannam@86 311 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
cannam@86 312 if(!ci->residue_param[i])goto err_out;
cannam@86 313 }
cannam@86 314
cannam@86 315 /* map backend settings */
cannam@86 316 ci->maps=oggpack_read(opb,6)+1;
cannam@86 317 if(ci->maps<=0)goto err_out;
cannam@86 318 for(i=0;i<ci->maps;i++){
cannam@86 319 ci->map_type[i]=oggpack_read(opb,16);
cannam@86 320 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
cannam@86 321 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
cannam@86 322 if(!ci->map_param[i])goto err_out;
cannam@86 323 }
cannam@86 324
cannam@86 325 /* mode settings */
cannam@86 326 ci->modes=oggpack_read(opb,6)+1;
cannam@86 327 if(ci->modes<=0)goto err_out;
cannam@86 328 for(i=0;i<ci->modes;i++){
cannam@86 329 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
cannam@86 330 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
cannam@86 331 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
cannam@86 332 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
cannam@86 333 ci->mode_param[i]->mapping=oggpack_read(opb,8);
cannam@86 334
cannam@86 335 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
cannam@86 336 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
cannam@86 337 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
cannam@86 338 if(ci->mode_param[i]->mapping<0)goto err_out;
cannam@86 339 }
cannam@86 340
cannam@86 341 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
cannam@86 342
cannam@86 343 return(0);
cannam@86 344 err_out:
cannam@86 345 vorbis_info_clear(vi);
cannam@86 346 return(OV_EBADHEADER);
cannam@86 347 }
cannam@86 348
cannam@86 349 /* Is this packet a vorbis ID header? */
cannam@86 350 int vorbis_synthesis_idheader(ogg_packet *op){
cannam@86 351 oggpack_buffer opb;
cannam@86 352 char buffer[6];
cannam@86 353
cannam@86 354 if(op){
cannam@86 355 oggpack_readinit(&opb,op->packet,op->bytes);
cannam@86 356
cannam@86 357 if(!op->b_o_s)
cannam@86 358 return(0); /* Not the initial packet */
cannam@86 359
cannam@86 360 if(oggpack_read(&opb,8) != 1)
cannam@86 361 return 0; /* not an ID header */
cannam@86 362
cannam@86 363 memset(buffer,0,6);
cannam@86 364 _v_readstring(&opb,buffer,6);
cannam@86 365 if(memcmp(buffer,"vorbis",6))
cannam@86 366 return 0; /* not vorbis */
cannam@86 367
cannam@86 368 return 1;
cannam@86 369 }
cannam@86 370
cannam@86 371 return 0;
cannam@86 372 }
cannam@86 373
cannam@86 374 /* The Vorbis header is in three packets; the initial small packet in
cannam@86 375 the first page that identifies basic parameters, a second packet
cannam@86 376 with bitstream comments and a third packet that holds the
cannam@86 377 codebook. */
cannam@86 378
cannam@86 379 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
cannam@86 380 oggpack_buffer opb;
cannam@86 381
cannam@86 382 if(op){
cannam@86 383 oggpack_readinit(&opb,op->packet,op->bytes);
cannam@86 384
cannam@86 385 /* Which of the three types of header is this? */
cannam@86 386 /* Also verify header-ness, vorbis */
cannam@86 387 {
cannam@86 388 char buffer[6];
cannam@86 389 int packtype=oggpack_read(&opb,8);
cannam@86 390 memset(buffer,0,6);
cannam@86 391 _v_readstring(&opb,buffer,6);
cannam@86 392 if(memcmp(buffer,"vorbis",6)){
cannam@86 393 /* not a vorbis header */
cannam@86 394 return(OV_ENOTVORBIS);
cannam@86 395 }
cannam@86 396 switch(packtype){
cannam@86 397 case 0x01: /* least significant *bit* is read first */
cannam@86 398 if(!op->b_o_s){
cannam@86 399 /* Not the initial packet */
cannam@86 400 return(OV_EBADHEADER);
cannam@86 401 }
cannam@86 402 if(vi->rate!=0){
cannam@86 403 /* previously initialized info header */
cannam@86 404 return(OV_EBADHEADER);
cannam@86 405 }
cannam@86 406
cannam@86 407 return(_vorbis_unpack_info(vi,&opb));
cannam@86 408
cannam@86 409 case 0x03: /* least significant *bit* is read first */
cannam@86 410 if(vi->rate==0){
cannam@86 411 /* um... we didn't get the initial header */
cannam@86 412 return(OV_EBADHEADER);
cannam@86 413 }
cannam@86 414
cannam@86 415 return(_vorbis_unpack_comment(vc,&opb));
cannam@86 416
cannam@86 417 case 0x05: /* least significant *bit* is read first */
cannam@86 418 if(vi->rate==0 || vc->vendor==NULL){
cannam@86 419 /* um... we didn;t get the initial header or comments yet */
cannam@86 420 return(OV_EBADHEADER);
cannam@86 421 }
cannam@86 422
cannam@86 423 return(_vorbis_unpack_books(vi,&opb));
cannam@86 424
cannam@86 425 default:
cannam@86 426 /* Not a valid vorbis header type */
cannam@86 427 return(OV_EBADHEADER);
cannam@86 428 break;
cannam@86 429 }
cannam@86 430 }
cannam@86 431 }
cannam@86 432 return(OV_EBADHEADER);
cannam@86 433 }
cannam@86 434
cannam@86 435 /* pack side **********************************************************/
cannam@86 436
cannam@86 437 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
cannam@86 438 codec_setup_info *ci=vi->codec_setup;
cannam@86 439 if(!ci)return(OV_EFAULT);
cannam@86 440
cannam@86 441 /* preamble */
cannam@86 442 oggpack_write(opb,0x01,8);
cannam@86 443 _v_writestring(opb,"vorbis", 6);
cannam@86 444
cannam@86 445 /* basic information about the stream */
cannam@86 446 oggpack_write(opb,0x00,32);
cannam@86 447 oggpack_write(opb,vi->channels,8);
cannam@86 448 oggpack_write(opb,vi->rate,32);
cannam@86 449
cannam@86 450 oggpack_write(opb,vi->bitrate_upper,32);
cannam@86 451 oggpack_write(opb,vi->bitrate_nominal,32);
cannam@86 452 oggpack_write(opb,vi->bitrate_lower,32);
cannam@86 453
cannam@86 454 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
cannam@86 455 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
cannam@86 456 oggpack_write(opb,1,1);
cannam@86 457
cannam@86 458 return(0);
cannam@86 459 }
cannam@86 460
cannam@86 461 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
cannam@86 462 int bytes = strlen(ENCODE_VENDOR_STRING);
cannam@86 463
cannam@86 464 /* preamble */
cannam@86 465 oggpack_write(opb,0x03,8);
cannam@86 466 _v_writestring(opb,"vorbis", 6);
cannam@86 467
cannam@86 468 /* vendor */
cannam@86 469 oggpack_write(opb,bytes,32);
cannam@86 470 _v_writestring(opb,ENCODE_VENDOR_STRING, bytes);
cannam@86 471
cannam@86 472 /* comments */
cannam@86 473
cannam@86 474 oggpack_write(opb,vc->comments,32);
cannam@86 475 if(vc->comments){
cannam@86 476 int i;
cannam@86 477 for(i=0;i<vc->comments;i++){
cannam@86 478 if(vc->user_comments[i]){
cannam@86 479 oggpack_write(opb,vc->comment_lengths[i],32);
cannam@86 480 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
cannam@86 481 }else{
cannam@86 482 oggpack_write(opb,0,32);
cannam@86 483 }
cannam@86 484 }
cannam@86 485 }
cannam@86 486 oggpack_write(opb,1,1);
cannam@86 487
cannam@86 488 return(0);
cannam@86 489 }
cannam@86 490
cannam@86 491 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
cannam@86 492 codec_setup_info *ci=vi->codec_setup;
cannam@86 493 int i;
cannam@86 494 if(!ci)return(OV_EFAULT);
cannam@86 495
cannam@86 496 oggpack_write(opb,0x05,8);
cannam@86 497 _v_writestring(opb,"vorbis", 6);
cannam@86 498
cannam@86 499 /* books */
cannam@86 500 oggpack_write(opb,ci->books-1,8);
cannam@86 501 for(i=0;i<ci->books;i++)
cannam@86 502 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
cannam@86 503
cannam@86 504 /* times; hook placeholders */
cannam@86 505 oggpack_write(opb,0,6);
cannam@86 506 oggpack_write(opb,0,16);
cannam@86 507
cannam@86 508 /* floors */
cannam@86 509 oggpack_write(opb,ci->floors-1,6);
cannam@86 510 for(i=0;i<ci->floors;i++){
cannam@86 511 oggpack_write(opb,ci->floor_type[i],16);
cannam@86 512 if(_floor_P[ci->floor_type[i]]->pack)
cannam@86 513 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
cannam@86 514 else
cannam@86 515 goto err_out;
cannam@86 516 }
cannam@86 517
cannam@86 518 /* residues */
cannam@86 519 oggpack_write(opb,ci->residues-1,6);
cannam@86 520 for(i=0;i<ci->residues;i++){
cannam@86 521 oggpack_write(opb,ci->residue_type[i],16);
cannam@86 522 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
cannam@86 523 }
cannam@86 524
cannam@86 525 /* maps */
cannam@86 526 oggpack_write(opb,ci->maps-1,6);
cannam@86 527 for(i=0;i<ci->maps;i++){
cannam@86 528 oggpack_write(opb,ci->map_type[i],16);
cannam@86 529 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
cannam@86 530 }
cannam@86 531
cannam@86 532 /* modes */
cannam@86 533 oggpack_write(opb,ci->modes-1,6);
cannam@86 534 for(i=0;i<ci->modes;i++){
cannam@86 535 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
cannam@86 536 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
cannam@86 537 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
cannam@86 538 oggpack_write(opb,ci->mode_param[i]->mapping,8);
cannam@86 539 }
cannam@86 540 oggpack_write(opb,1,1);
cannam@86 541
cannam@86 542 return(0);
cannam@86 543 err_out:
cannam@86 544 return(-1);
cannam@86 545 }
cannam@86 546
cannam@86 547 int vorbis_commentheader_out(vorbis_comment *vc,
cannam@86 548 ogg_packet *op){
cannam@86 549
cannam@86 550 oggpack_buffer opb;
cannam@86 551
cannam@86 552 oggpack_writeinit(&opb);
cannam@86 553 if(_vorbis_pack_comment(&opb,vc)){
cannam@86 554 oggpack_writeclear(&opb);
cannam@86 555 return OV_EIMPL;
cannam@86 556 }
cannam@86 557
cannam@86 558 op->packet = _ogg_malloc(oggpack_bytes(&opb));
cannam@86 559 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
cannam@86 560
cannam@86 561 op->bytes=oggpack_bytes(&opb);
cannam@86 562 op->b_o_s=0;
cannam@86 563 op->e_o_s=0;
cannam@86 564 op->granulepos=0;
cannam@86 565 op->packetno=1;
cannam@86 566
cannam@86 567 oggpack_writeclear(&opb);
cannam@86 568 return 0;
cannam@86 569 }
cannam@86 570
cannam@86 571 int vorbis_analysis_headerout(vorbis_dsp_state *v,
cannam@86 572 vorbis_comment *vc,
cannam@86 573 ogg_packet *op,
cannam@86 574 ogg_packet *op_comm,
cannam@86 575 ogg_packet *op_code){
cannam@86 576 int ret=OV_EIMPL;
cannam@86 577 vorbis_info *vi=v->vi;
cannam@86 578 oggpack_buffer opb;
cannam@86 579 private_state *b=v->backend_state;
cannam@86 580
cannam@86 581 if(!b){
cannam@86 582 ret=OV_EFAULT;
cannam@86 583 goto err_out;
cannam@86 584 }
cannam@86 585
cannam@86 586 /* first header packet **********************************************/
cannam@86 587
cannam@86 588 oggpack_writeinit(&opb);
cannam@86 589 if(_vorbis_pack_info(&opb,vi))goto err_out;
cannam@86 590
cannam@86 591 /* build the packet */
cannam@86 592 if(b->header)_ogg_free(b->header);
cannam@86 593 b->header=_ogg_malloc(oggpack_bytes(&opb));
cannam@86 594 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
cannam@86 595 op->packet=b->header;
cannam@86 596 op->bytes=oggpack_bytes(&opb);
cannam@86 597 op->b_o_s=1;
cannam@86 598 op->e_o_s=0;
cannam@86 599 op->granulepos=0;
cannam@86 600 op->packetno=0;
cannam@86 601
cannam@86 602 /* second header packet (comments) **********************************/
cannam@86 603
cannam@86 604 oggpack_reset(&opb);
cannam@86 605 if(_vorbis_pack_comment(&opb,vc))goto err_out;
cannam@86 606
cannam@86 607 if(b->header1)_ogg_free(b->header1);
cannam@86 608 b->header1=_ogg_malloc(oggpack_bytes(&opb));
cannam@86 609 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
cannam@86 610 op_comm->packet=b->header1;
cannam@86 611 op_comm->bytes=oggpack_bytes(&opb);
cannam@86 612 op_comm->b_o_s=0;
cannam@86 613 op_comm->e_o_s=0;
cannam@86 614 op_comm->granulepos=0;
cannam@86 615 op_comm->packetno=1;
cannam@86 616
cannam@86 617 /* third header packet (modes/codebooks) ****************************/
cannam@86 618
cannam@86 619 oggpack_reset(&opb);
cannam@86 620 if(_vorbis_pack_books(&opb,vi))goto err_out;
cannam@86 621
cannam@86 622 if(b->header2)_ogg_free(b->header2);
cannam@86 623 b->header2=_ogg_malloc(oggpack_bytes(&opb));
cannam@86 624 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
cannam@86 625 op_code->packet=b->header2;
cannam@86 626 op_code->bytes=oggpack_bytes(&opb);
cannam@86 627 op_code->b_o_s=0;
cannam@86 628 op_code->e_o_s=0;
cannam@86 629 op_code->granulepos=0;
cannam@86 630 op_code->packetno=2;
cannam@86 631
cannam@86 632 oggpack_writeclear(&opb);
cannam@86 633 return(0);
cannam@86 634 err_out:
cannam@86 635 memset(op,0,sizeof(*op));
cannam@86 636 memset(op_comm,0,sizeof(*op_comm));
cannam@86 637 memset(op_code,0,sizeof(*op_code));
cannam@86 638
cannam@86 639 if(b){
cannam@86 640 oggpack_writeclear(&opb);
cannam@86 641 if(b->header)_ogg_free(b->header);
cannam@86 642 if(b->header1)_ogg_free(b->header1);
cannam@86 643 if(b->header2)_ogg_free(b->header2);
cannam@86 644 b->header=NULL;
cannam@86 645 b->header1=NULL;
cannam@86 646 b->header2=NULL;
cannam@86 647 }
cannam@86 648 return(ret);
cannam@86 649 }
cannam@86 650
cannam@86 651 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
cannam@86 652 if(granulepos == -1) return -1;
cannam@86 653
cannam@86 654 /* We're not guaranteed a 64 bit unsigned type everywhere, so we
cannam@86 655 have to put the unsigned granpo in a signed type. */
cannam@86 656 if(granulepos>=0){
cannam@86 657 return((double)granulepos/v->vi->rate);
cannam@86 658 }else{
cannam@86 659 ogg_int64_t granuleoff=0xffffffff;
cannam@86 660 granuleoff<<=31;
cannam@86 661 granuleoff|=0x7ffffffff;
cannam@86 662 return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate);
cannam@86 663 }
cannam@86 664 }
cannam@86 665
cannam@86 666 const char *vorbis_version_string(void){
cannam@86 667 return GENERAL_VENDOR_STRING;
cannam@86 668 }