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