misc/libtremor/tremor/info.c
changeset 7697 767d3c4153a1
parent 6045 9a7cc0f29430
child 7849 a12155461b34
equal deleted inserted replaced
7696:78a00bc68913 7697:767d3c4153a1
    23 #include <ctype.h>
    23 #include <ctype.h>
    24 #include "ogg.h"
    24 #include "ogg.h"
    25 #include "ivorbiscodec.h"
    25 #include "ivorbiscodec.h"
    26 #include "codec_internal.h"
    26 #include "codec_internal.h"
    27 #include "codebook.h"
    27 #include "codebook.h"
    28 #include "registry.h"
       
    29 #include "window.h"
       
    30 #include "misc.h"
    28 #include "misc.h"
       
    29 #include "os.h"
    31 
    30 
    32 /* helpers */
    31 /* helpers */
    33 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
    32 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
    34   while(bytes--){
    33   while(bytes--){
    35     *buf++=oggpack_read(o,8);
    34     *buf++=oggpack_read(o,8);
    94     for(i=0;i<vc->comments;i++)
    93     for(i=0;i<vc->comments;i++)
    95       if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
    94       if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
    96     if(vc->user_comments)_ogg_free(vc->user_comments);
    95     if(vc->user_comments)_ogg_free(vc->user_comments);
    97 	if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
    96 	if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
    98     if(vc->vendor)_ogg_free(vc->vendor);
    97     if(vc->vendor)_ogg_free(vc->vendor);
    99     memset(vc,0,sizeof(*vc));
    98   }
   100   }
    99   memset(vc,0,sizeof(*vc));
   101 }
   100 }
   102 
   101 
   103 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
   102 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
   104    They may be equal, but short will never ge greater than long */
   103    They may be equal, but short will never ge greater than long */
   105 int vorbis_info_blocksize(vorbis_info *vi,int zo){
   104 int vorbis_info_blocksize(vorbis_info *vi,int zo){
   117   codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
   116   codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
   118   int i;
   117   int i;
   119 
   118 
   120   if(ci){
   119   if(ci){
   121 
   120 
   122     for(i=0;i<ci->modes;i++)
   121     if(ci->mode_param)_ogg_free(ci->mode_param);
   123       if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
   122 
   124 
   123     if(ci->map_param){
   125     for(i=0;i<ci->maps;i++) /* unpack does the range checking */
   124       for(i=0;i<ci->maps;i++) /* unpack does the range checking */
   126       if(ci->map_param[i])
   125 	mapping_clear_info(ci->map_param+i);
   127 	_mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
   126       _ogg_free(ci->map_param);
   128 
   127     }
   129     for(i=0;i<ci->floors;i++) /* unpack does the range checking */
   128 
   130       if(ci->floor_param[i])
   129     if(ci->floor_param){
   131 	_floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
   130       for(i=0;i<ci->floors;i++) /* unpack does the range checking */
   132     
   131 	if(ci->floor_type[i])
   133     for(i=0;i<ci->residues;i++) /* unpack does the range checking */
   132 	  floor1_free_info(ci->floor_param[i]);
   134       if(ci->residue_param[i])
   133 	else
   135 	_residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
   134 	  floor0_free_info(ci->floor_param[i]);
   136 
   135       _ogg_free(ci->floor_param);
   137     for(i=0;i<ci->books;i++){
   136       _ogg_free(ci->floor_type);
   138       if(ci->book_param[i]){
   137     }
   139 	/* knows if the book was not alloced */
   138 
   140 	vorbis_staticbook_destroy(ci->book_param[i]);
   139     if(ci->residue_param){
   141       }
   140       for(i=0;i<ci->residues;i++) /* unpack does the range checking */
   142       if(ci->fullbooks)
   141 	res_clear_info(ci->residue_param+i);
   143 	vorbis_book_clear(ci->fullbooks+i);
   142       _ogg_free(ci->residue_param);
   144     }
   143     }
   145     if(ci->fullbooks)
   144 
   146 	_ogg_free(ci->fullbooks);
   145     if(ci->book_param){
       
   146       for(i=0;i<ci->books;i++)
       
   147 	vorbis_book_clear(ci->book_param+i);
       
   148       _ogg_free(ci->book_param);
       
   149     }
   147     
   150     
   148     _ogg_free(ci);
   151     _ogg_free(ci);
   149   }
   152   }
   150 
   153 
   151   memset(vi,0,sizeof(*vi));
   154   memset(vi,0,sizeof(*vi));
   217   int i;
   220   int i;
   218   if(!ci)return(OV_EFAULT);
   221   if(!ci)return(OV_EFAULT);
   219 
   222 
   220   /* codebooks */
   223   /* codebooks */
   221   ci->books=oggpack_read(opb,8)+1;
   224   ci->books=oggpack_read(opb,8)+1;
   222   /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
   225   ci->book_param=(codebook *)_ogg_calloc(ci->books,sizeof(*ci->book_param));
   223   for(i=0;i<ci->books;i++){
   226   for(i=0;i<ci->books;i++)
   224     ci->book_param[i]=(static_codebook *)_ogg_calloc(1,sizeof(*ci->book_param[i]));
   227     if(vorbis_book_unpack(opb,ci->book_param+i))goto err_out;
   225     if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
   228 
   226   }
   229   /* time backend settings, not actually used */
   227 
   230   i=oggpack_read(opb,6);
   228   /* time backend settings */
   231   for(;i>=0;i--)
   229   ci->times=oggpack_read(opb,6)+1;
   232     if(oggpack_read(opb,16)!=0)goto err_out;
   230   /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
       
   231   /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
       
   232   for(i=0;i<ci->times;i++){
       
   233     ci->time_type[i]=oggpack_read(opb,16);
       
   234     if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
       
   235     /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
       
   236        Vorbis I has no time backend */
       
   237     /*if(!ci->time_param[i])goto err_out;*/
       
   238   }
       
   239 
   233 
   240   /* floor backend settings */
   234   /* floor backend settings */
   241   ci->floors=oggpack_read(opb,6)+1;
   235   ci->floors=oggpack_read(opb,6)+1;
   242   /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
   236   ci->floor_param=_ogg_malloc(sizeof(*ci->floor_param)*ci->floors);
   243   /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
   237   ci->floor_type=_ogg_malloc(sizeof(*ci->floor_type)*ci->floors);
   244   for(i=0;i<ci->floors;i++){
   238   for(i=0;i<ci->floors;i++){
   245     ci->floor_type[i]=oggpack_read(opb,16);
   239     ci->floor_type[i]=oggpack_read(opb,16);
   246     if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
   240     if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
   247     ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
   241     if(ci->floor_type[i])
       
   242       ci->floor_param[i]=floor1_info_unpack(vi,opb);
       
   243     else
       
   244       ci->floor_param[i]=floor0_info_unpack(vi,opb);
   248     if(!ci->floor_param[i])goto err_out;
   245     if(!ci->floor_param[i])goto err_out;
   249   }
   246   }
   250 
   247 
   251   /* residue backend settings */
   248   /* residue backend settings */
   252   ci->residues=oggpack_read(opb,6)+1;
   249   ci->residues=oggpack_read(opb,6)+1;
   253   /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
   250   ci->residue_param=_ogg_malloc(sizeof(*ci->residue_param)*ci->residues);
   254   /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
   251   for(i=0;i<ci->residues;i++)
   255   for(i=0;i<ci->residues;i++){
   252     if(res_unpack(ci->residue_param+i,vi,opb))goto err_out;
   256     ci->residue_type[i]=oggpack_read(opb,16);
       
   257     if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
       
   258     ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
       
   259     if(!ci->residue_param[i])goto err_out;
       
   260   }
       
   261 
   253 
   262   /* map backend settings */
   254   /* map backend settings */
   263   ci->maps=oggpack_read(opb,6)+1;
   255   ci->maps=oggpack_read(opb,6)+1;
   264   /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
   256   ci->map_param=_ogg_malloc(sizeof(*ci->map_param)*ci->maps);
   265   /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
       
   266   for(i=0;i<ci->maps;i++){
   257   for(i=0;i<ci->maps;i++){
   267     ci->map_type[i]=oggpack_read(opb,16);
   258     if(oggpack_read(opb,16)!=0)goto err_out;
   268     if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
   259     if(mapping_info_unpack(ci->map_param+i,vi,opb))goto err_out;
   269     ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
       
   270     if(!ci->map_param[i])goto err_out;
       
   271   }
   260   }
   272   
   261   
   273   /* mode settings */
   262   /* mode settings */
   274   ci->modes=oggpack_read(opb,6)+1;
   263   ci->modes=oggpack_read(opb,6)+1;
   275   /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
   264   ci->mode_param=
       
   265     (vorbis_info_mode *)_ogg_malloc(ci->modes*sizeof(*ci->mode_param));
   276   for(i=0;i<ci->modes;i++){
   266   for(i=0;i<ci->modes;i++){
   277     ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
   267     ci->mode_param[i].blockflag=oggpack_read(opb,1);
   278     ci->mode_param[i]->blockflag=oggpack_read(opb,1);
   268     if(oggpack_read(opb,16))goto err_out;
   279     ci->mode_param[i]->windowtype=oggpack_read(opb,16);
   269     if(oggpack_read(opb,16))goto err_out;
   280     ci->mode_param[i]->transformtype=oggpack_read(opb,16);
   270     ci->mode_param[i].mapping=oggpack_read(opb,8);
   281     ci->mode_param[i]->mapping=oggpack_read(opb,8);
   271     if(ci->mode_param[i].mapping>=ci->maps)goto err_out;
   282 
       
   283     if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
       
   284     if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
       
   285     if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
       
   286   }
   272   }
   287   
   273   
   288   if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
   274   if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
   289 
   275 
   290   return(0);
   276   return(0);
   296 /* The Vorbis header is in three packets; the initial small packet in
   282 /* The Vorbis header is in three packets; the initial small packet in
   297    the first page that identifies basic parameters, a second packet
   283    the first page that identifies basic parameters, a second packet
   298    with bitstream comments and a third packet that holds the
   284    with bitstream comments and a third packet that holds the
   299    codebook. */
   285    codebook. */
   300 
   286 
   301 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
   287 int vorbis_dsp_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
   302   oggpack_buffer opb;
   288   oggpack_buffer opb;
   303   
   289   
   304   if(op){
   290   if(op){
   305     oggpack_readinit(&opb,op->packet);
   291     oggpack_readinit(&opb,op->packet);
   306 
   292