2 * Copyright (C) 2000 ymnk, JCraft,Inc.
4 * Written by: 2000 ymnk<ymnk@jcraft.com>
7 * Monty <monty@xiph.org> and
8 * The XIPHOPHORUS Company http://www.xiph.org/ .
9 * JOrbis has been based on their awesome works, Vorbis codec.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public License
13 * as published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 package com.jcraft.jorbis;
28 import com.jcraft.jogg.*;
31 private static final int OV_EBADPACKET=-136;
32 private static final int OV_ENOTAUDIO=-135;
34 private static byte[] _vorbis="vorbis".getBytes();
35 private static final int VI_TIMEB=1;
36 // private static final int VI_FLOORB=1;
37 private static final int VI_FLOORB=2;
38 // private static final int VI_RESB=1;
39 private static final int VI_RESB=3;
40 private static final int VI_MAPB=1;
41 private static final int VI_WINDOWB=1;
47 // The below bitrate declarations are *hints*.
48 // Combinations of the three values carry the following implications:
50 // all three set to the same value:
51 // implies a fixed rate bitstream
53 // implies a VBR stream that averages the nominal bitrate. No hard
55 // upper and or lower set:
56 // implies a VBR bitstream that obeys the bitrate limits. nominal
57 // may also be set to give a nominal rate.
59 // the coder does not care to speculate.
65 // Vorbis supports only short and long blocks, but allows the
66 // encoder to choose the sizes
68 int[] blocksizes=new int[2];
70 // modes are the primary means of supporting on-the-fly different
71 // blocksizes, different channel mappings (LR or mid-side),
72 // different residue backends, etc. Each mode consists of a
73 // blocksize flag and a mapping (along with the mapping setup
81 int psys; // encode only
83 InfoMode[] mode_param=null;
86 Object[] map_param=null;
89 Object[] time_param=null;
91 int[] floor_type=null;
92 Object[] floor_param=null;
94 int[] residue_type=null;
95 Object[] residue_param=null;
97 StaticCodeBook[] book_param=null;
99 PsyInfo[] psy_param=new PsyInfo[64]; // encode only
101 // for block long/sort tuning; encode only
103 float preecho_thresh;
106 // used by synthesis, which has a full, alloced vi
109 //memset(vi,0,sizeof(vorbis_info));
113 for(int i=0;i<modes;i++){ mode_param[i]=null; }
116 for(int i=0;i<maps;i++){ // unpack does the range checking
117 FuncMapping.mapping_P[map_type[i]].free_info(map_param[i]);
121 for(int i=0;i<times;i++){ // unpack does the range checking
122 FuncTime.time_P[time_type[i]].free_info(time_param[i]);
126 for(int i=0;i<floors;i++){ // unpack does the range checking
127 FuncFloor.floor_P[floor_type[i]].free_info(floor_param[i]);
131 for(int i=0;i<residues;i++){ // unpack does the range checking
132 FuncResidue.residue_P[residue_type[i]].free_info(residue_param[i]);
136 // the static codebooks *are* freed if you call info_clear, because
137 // decode side does alloc a 'static' codebook. Calling clear on the
138 // full codebook does not clear the static codebook (that's our
140 for(int i=0;i<books;i++){
141 // just in case the decoder pre-cleared to save space
142 if(book_param[i]!=null){
143 book_param[i].clear();
147 //if(vi->book_param)free(vi->book_param);
150 for(int i=0;i<psys;i++){
153 //if(vi->psy_param)free(vi->psy_param);
154 //memset(vi,0,sizeof(vorbis_info));
157 // Header packing/unpacking
158 int unpack_info(Buffer opb){
159 version=opb.read(32);
160 if(version!=0)return(-1);
162 channels=opb.read(8);
165 bitrate_upper=opb.read(32);
166 bitrate_nominal=opb.read(32);
167 bitrate_lower=opb.read(32);
169 blocksizes[0]=1<<opb.read(4);
170 blocksizes[1]=1<<opb.read(4);
175 (blocksizes[1]<blocksizes[0]) ||
177 //goto err_out; // EOP check
183 // vorbis_info_clear(vi);
187 // all of the real encoding details are here. The modes, books,
189 int unpack_books(Buffer opb){
194 if(book_param==null || book_param.length!=books)
195 book_param=new StaticCodeBook[books];
196 for(int i=0;i<books;i++){
197 book_param[i]=new StaticCodeBook();
198 if(book_param[i].unpack(opb)!=0){
205 // time backend settings
207 if(time_type==null || time_type.length!=times) time_type=new int[times];
208 if(time_param==null || time_param.length!=times)
209 time_param=new Object[times];
210 for(int i=0;i<times;i++){
211 time_type[i]=opb.read(16);
212 if(time_type[i]<0 || time_type[i]>=VI_TIMEB){
217 time_param[i]=FuncTime.time_P[time_type[i]].unpack(this, opb);
218 if(time_param[i]==null){
225 // floor backend settings
226 floors=opb.read(6)+1;
227 if(floor_type==null || floor_type.length!=floors)
228 floor_type=new int[floors];
229 if(floor_param==null || floor_param.length!=floors)
230 floor_param=new Object[floors];
232 for(int i=0;i<floors;i++){
233 floor_type[i]=opb.read(16);
234 if(floor_type[i]<0 || floor_type[i]>=VI_FLOORB){
240 floor_param[i]=FuncFloor.floor_P[floor_type[i]].unpack(this,opb);
241 if(floor_param[i]==null){
248 // residue backend settings
249 residues=opb.read(6)+1;
251 if(residue_type==null || residue_type.length!=residues)
252 residue_type=new int[residues];
254 if(residue_param==null || residue_param.length!=residues)
255 residue_param=new Object[residues];
257 for(int i=0;i<residues;i++){
258 residue_type[i]=opb.read(16);
259 if(residue_type[i]<0 || residue_type[i]>=VI_RESB){
264 residue_param[i]=FuncResidue.residue_P[residue_type[i]].unpack(this,opb);
265 if(residue_param[i]==null){
272 // map backend settings
274 if(map_type==null || map_type.length!=maps) map_type=new int[maps];
275 if(map_param==null || map_param.length!=maps) map_param=new Object[maps];
276 for(int i=0;i<maps;i++){
277 map_type[i]=opb.read(16);
278 if(map_type[i]<0 || map_type[i]>=VI_MAPB){
283 map_param[i]=FuncMapping.mapping_P[map_type[i]].unpack(this,opb);
284 if(map_param[i]==null){
293 if(mode_param==null || mode_param.length!=modes)
294 mode_param=new InfoMode[modes];
295 for(int i=0;i<modes;i++){
296 mode_param[i]=new InfoMode();
297 mode_param[i].blockflag=opb.read(1);
298 mode_param[i].windowtype=opb.read(16);
299 mode_param[i].transformtype=opb.read(16);
300 mode_param[i].mapping=opb.read(8);
302 if((mode_param[i].windowtype>=VI_WINDOWB)||
303 (mode_param[i].transformtype>=VI_WINDOWB)||
304 (mode_param[i].mapping>=maps)){
312 //goto err_out; // top level EOP check
319 // vorbis_info_clear(vi);
323 // The Vorbis header is in three packets; the initial small packet in
324 // the first page that identifies basic parameters, a second packet
325 // with bitstream comments and a third packet that holds the
328 public int synthesis_headerin(Comment vc, Packet op){
329 Buffer opb=new Buffer();
332 opb.readinit(op.packet_base, op.packet, op.bytes);
334 // Which of the three types of header is this?
335 // Also verify header-ness, vorbis
337 byte[] buffer=new byte[6];
338 int packtype=opb.read(8);
339 //memset(buffer,0,6);
341 if(buffer[0]!='v' || buffer[1]!='o' || buffer[2]!='r' ||
342 buffer[3]!='b' || buffer[4]!='i' || buffer[5]!='s'){
343 // not a vorbis header
347 case 0x01: // least significant *bit* is read first
349 // Not the initial packet
353 // previously initialized info header
356 return(unpack_info(opb));
357 case 0x03: // least significant *bit* is read first
359 // um... we didn't get the initial header
362 return(vc.unpack(opb));
363 case 0x05: // least significant *bit* is read first
364 if(rate==0 || vc.vendor==null){
365 // um... we didn;t get the initial header or comments yet
368 return(unpack_books(opb));
370 // Not a valid vorbis header type
380 int pack_info(Buffer opb){
385 // basic information about the stream
387 opb.write(channels,8);
390 opb.write(bitrate_upper,32);
391 opb.write(bitrate_nominal,32);
392 opb.write(bitrate_lower,32);
394 opb.write(ilog2(blocksizes[0]),4);
395 opb.write(ilog2(blocksizes[1]),4);
400 int pack_books(Buffer opb){
405 opb.write(books-1,8);
406 for(int i=0;i<books;i++){
407 if(book_param[i].pack(opb)!=0){
414 opb.write(times-1,6);
415 for(int i=0;i<times;i++){
416 opb.write(time_type[i],16);
417 FuncTime.time_P[time_type[i]].pack(this.time_param[i],opb);
421 opb.write(floors-1,6);
422 for(int i=0;i<floors;i++){
423 opb.write(floor_type[i],16);
424 FuncFloor.floor_P[floor_type[i]].pack(floor_param[i],opb);
428 opb.write(residues-1,6);
429 for(int i=0;i<residues;i++){
430 opb.write(residue_type[i],16);
431 FuncResidue.residue_P[residue_type[i]].pack(residue_param[i],opb);
436 for(int i=0;i<maps;i++){
437 opb.write(map_type[i],16);
438 FuncMapping.mapping_P[map_type[i]].pack(this,map_param[i],opb);
442 opb.write(modes-1,6);
443 for(int i=0;i<modes;i++){
444 opb.write(mode_param[i].blockflag,1);
445 opb.write(mode_param[i].windowtype,16);
446 opb.write(mode_param[i].transformtype,16);
447 opb.write(mode_param[i].mapping,8);
455 // static void v_writestring(Buffer o, byte[] s){
458 // o.write(s[i++],8);
462 // static void v_readstring(Buffer o, byte[] buf, int bytes){
464 // while(bytes--!=0){
465 // buf[i++]=o.read(8);
469 // private Buffer opb_blocksize=new Buffer();
470 public int blocksize(Packet op){
471 //codec_setup_info *ci=vi->codec_setup;
472 Buffer opb=new Buffer();
473 // synchronized(opb_blocksize){
476 opb.readinit(op.packet_base, op.packet, op.bytes);
478 /* Check the packet type */
480 /* Oops. This is not an audio data packet */
481 return(OV_ENOTAUDIO);
491 /* read our mode and pre/post windowsize */
492 mode=opb.read(modebits);
494 if(mode==-1)return(OV_EBADPACKET);
495 return(blocksizes[mode_param[mode].blockflag]);
499 private static int ilog2(int v){
508 public String toString(){
509 return "version:"+new Integer(version)+
510 ", channels:"+new Integer(channels)+
511 ", rate:"+new Integer(rate)+
512 ", bitrate:"+new Integer(bitrate_upper)+","+
513 new Integer(bitrate_nominal)+","+
514 new Integer(bitrate_lower);