All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jcraft.jorbis.Info Maven / Gradle / Ivy

The newest version!
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
 * Copyright (C) 2000 ymnk, JCraft,Inc.
 *  
 * Written by: 2000 ymnk
 *   
 * Many thanks to 
 *   Monty  and 
 *   The XIPHOPHORUS Company http://www.xiph.org/ .
 * JOrbis has been based on their awesome works, Vorbis codec.
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package com.jcraft.jorbis;

import com.jcraft.jogg.*;

public class Info{
  private static final int OV_EBADPACKET=-136;
  private static final int OV_ENOTAUDIO=-135;

  private static byte[] _vorbis="vorbis".getBytes();
  private static final int VI_TIMEB=1;
  //  private static final int VI_FLOORB=1;
  private static final int VI_FLOORB=2;
  //  private static final int VI_RESB=1;
  private static final int VI_RESB=3;
  private static final int VI_MAPB=1;
  private static final int VI_WINDOWB=1;

  public int version;
  public int channels;
  public int rate;

  // The below bitrate declarations are *hints*.
  // Combinations of the three values carry the following implications:
  //     
  // all three set to the same value: 
  // implies a fixed rate bitstream
  // only nominal set: 
  // implies a VBR stream that averages the nominal bitrate.  No hard 
  // upper/lower limit
  // upper and or lower set: 
  // implies a VBR bitstream that obeys the bitrate limits. nominal 
  // may also be set to give a nominal rate.
  // none set:
  //  the coder does not care to speculate.

  int bitrate_upper;
  int bitrate_nominal;
  int bitrate_lower;

  // Vorbis supports only short and long blocks, but allows the
  // encoder to choose the sizes

  int[] blocksizes=new int[2];

  // modes are the primary means of supporting on-the-fly different
  // blocksizes, different channel mappings (LR or mid-side),
  // different residue backends, etc.  Each mode consists of a
  // blocksize flag and a mapping (along with the mapping setup

  int modes;
  int maps;
  int times;
  int floors;
  int residues;
  int books;
  int psys; // encode only

  InfoMode[] mode_param=null;

  int[] map_type=null;
  Object[] map_param=null;

  int[] time_type=null;
  Object[] time_param=null;

  int[] floor_type=null;
  Object[] floor_param=null;

  int[] residue_type=null;
  Object[] residue_param=null;

  StaticCodeBook[] book_param=null;

  PsyInfo[] psy_param=new PsyInfo[64]; // encode only

  // for block long/sort tuning; encode only
  int envelopesa;
  float preecho_thresh;
  float preecho_clamp;

  // used by synthesis, which has a full, alloced vi
  public void init(){
    rate=0;
  }

  public void clear(){
    for(int i=0; ibook_param)free(vi->book_param);
    book_param=null;

    for(int i=0; i=VI_TIMEB){
        clear();
        return (-1);
      }
      time_param[i]=FuncTime.time_P[time_type[i]].unpack(this, opb);
      if(time_param[i]==null){
        clear();
        return (-1);
      }
    }

    // floor backend settings
    floors=opb.read(6)+1;
    if(floor_type==null||floor_type.length!=floors)
      floor_type=new int[floors];
    if(floor_param==null||floor_param.length!=floors)
      floor_param=new Object[floors];

    for(int i=0; i=VI_FLOORB){
        clear();
        return (-1);
      }

      floor_param[i]=FuncFloor.floor_P[floor_type[i]].unpack(this, opb);
      if(floor_param[i]==null){
        clear();
        return (-1);
      }
    }

    // residue backend settings
    residues=opb.read(6)+1;

    if(residue_type==null||residue_type.length!=residues)
      residue_type=new int[residues];

    if(residue_param==null||residue_param.length!=residues)
      residue_param=new Object[residues];

    for(int i=0; i=VI_RESB){
        clear();
        return (-1);
      }
      residue_param[i]=FuncResidue.residue_P[residue_type[i]].unpack(this, opb);
      if(residue_param[i]==null){
        clear();
        return (-1);
      }
    }

    // map backend settings
    maps=opb.read(6)+1;
    if(map_type==null||map_type.length!=maps)
      map_type=new int[maps];
    if(map_param==null||map_param.length!=maps)
      map_param=new Object[maps];
    for(int i=0; i=VI_MAPB){
        clear();
        return (-1);
      }
      map_param[i]=FuncMapping.mapping_P[map_type[i]].unpack(this, opb);
      if(map_param[i]==null){
        clear();
        return (-1);
      }
    }

    // mode settings
    modes=opb.read(6)+1;
    if(mode_param==null||mode_param.length!=modes)
      mode_param=new InfoMode[modes];
    for(int i=0; i=VI_WINDOWB)
          ||(mode_param[i].transformtype>=VI_WINDOWB)
          ||(mode_param[i].mapping>=maps)){
        clear();
        return (-1);
      }
    }

    if(opb.read(1)!=1){
      clear();
      return (-1);
    }

    return (0);
  }

  // The Vorbis header is in three packets; the initial small packet in
  // the first page that identifies basic parameters, a second packet
  // with bitstream comments and a third packet that holds the
  // codebook.

  public int synthesis_headerin(Comment vc, Packet op){
    Buffer opb=new Buffer();

    if(op!=null){
      opb.readinit(op.packet_base, op.packet, op.bytes);

      // Which of the three types of header is this?
      // Also verify header-ness, vorbis
      {
        byte[] buffer=new byte[6];
        int packtype=opb.read(8);
        opb.read(buffer, 6);
        if(buffer[0]!='v'||buffer[1]!='o'||buffer[2]!='r'||buffer[3]!='b'
            ||buffer[4]!='i'||buffer[5]!='s'){
          // not a vorbis header
          return (-1);
        }
        switch(packtype){
          case 0x01: // least significant *bit* is read first
            if(op.b_o_s==0){
              // Not the initial packet
              return (-1);
            }
            if(rate!=0){
              // previously initialized info header
              return (-1);
            }
            return (unpack_info(opb));
          case 0x03: // least significant *bit* is read first
            if(rate==0){
              // um... we didn't get the initial header
              return (-1);
            }
            return (vc.unpack(opb));
          case 0x05: // least significant *bit* is read first
            if(rate==0||vc.vendor==null){
              // um... we didn;t get the initial header or comments yet
              return (-1);
            }
            return (unpack_books(opb));
          default:
            // Not a valid vorbis header type
            //return(-1);
            break;
        }
      }
    }
    return (-1);
  }

  // pack side
  int pack_info(Buffer opb){
    // preamble
    opb.write(0x01, 8);
    opb.write(_vorbis);

    // basic information about the stream
    opb.write(0x00, 32);
    opb.write(channels, 8);
    opb.write(rate, 32);

    opb.write(bitrate_upper, 32);
    opb.write(bitrate_nominal, 32);
    opb.write(bitrate_lower, 32);

    opb.write(Util.ilog2(blocksizes[0]), 4);
    opb.write(Util.ilog2(blocksizes[1]), 4);
    opb.write(1, 1);
    return (0);
  }

  int pack_books(Buffer opb){
    opb.write(0x05, 8);
    opb.write(_vorbis);

    // books
    opb.write(books-1, 8);
    for(int i=0; i1){
        modebits++;
        v>>>=1;
      }

      /* read our mode and pre/post windowsize */
      mode=opb.read(modebits);
    }
    if(mode==-1)
      return (OV_EBADPACKET);
    return (blocksizes[mode_param[mode].blockflag]);
  }

  public String toString(){
    return "version:"+new Integer(version)+", channels:"+new Integer(channels)
        +", rate:"+new Integer(rate)+", bitrate:"+new Integer(bitrate_upper)
        +","+new Integer(bitrate_nominal)+","+new Integer(bitrate_lower);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy