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