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.*;
30 // the comments are not part of vorbis_info so that vorbis_info can be
33 private static byte[] _vorbis="vorbis".getBytes();
35 private static final int OV_EFAULT=-129;
36 private static final int OV_EIMPL=-130;
38 // unlimited user comment fields. libvorbis writes 'libvorbis'
39 // whatever vendor is set to in encode
40 public byte[][] user_comments;
41 public int[] comment_lengths;
51 public void add(String comment){
52 add(comment.getBytes());
55 private void add(byte[] comment){
56 byte[][] foo=new byte[comments+2][];
57 if(user_comments!=null){
58 System.arraycopy(user_comments, 0, foo, 0, comments);
62 int[] goo=new int[comments+2];
63 if(comment_lengths!=null){
64 System.arraycopy(comment_lengths, 0, goo, 0, comments);
68 byte[] bar=new byte[comment.length+1];
69 System.arraycopy(comment, 0, bar, 0, comment.length);
70 user_comments[comments]=bar;
71 comment_lengths[comments]=comment.length;
73 user_comments[comments]=null;
76 public void add_tag(String tag, String contents){
77 if(contents==null) contents="";
78 add(tag+"="+contents);
82 private void add_tag(byte[] tag, byte[] contents){
83 byte[] foo=new byte[tag.length+contents.length+1];
85 for(int i=0; i<tag.length; i++){foo[j++]=tag[i];}
86 foo[j++]=(byte)'='; j++;
87 for(int i=0; i<contents.length; i++){foo[j++]=tag[i];}
92 // This is more or less the same as strncasecmp - but that doesn't exist
93 // * everywhere, and this is a fairly trivial function, so we include it
94 static boolean tagcompare(byte[] s1, byte[] s2, int n){
99 if('Z'>=u1 && u1>='A')u1=(byte)(u1-'A'+'a');
100 if('Z'>=u2 && u2>='A')u2=(byte)(u2-'A'+'a');
101 if(u1!=u2){ return false; }
107 public String query(String tag){
108 return query(tag, 0);
111 public String query(String tag, int count){
112 int foo=query(tag.getBytes(), count);
113 if(foo==-1)return null;
114 byte[] comment=user_comments[foo];
115 for(int i=0; i<comment_lengths[foo]; i++){
117 return new String(comment, i+1, comment_lengths[foo]-(i+1));
123 private int query(byte[] tag, int count){
126 int fulltaglen = tag.length + 1;
127 byte[] fulltag = new byte[fulltaglen];
128 System.arraycopy(tag, 0, fulltag, 0, tag.length);
129 fulltag[tag.length]=(byte)'=';
131 for(i=0;i<comments;i++){
132 if(tagcompare(user_comments[i], fulltag, fulltaglen)){
134 // We return a pointer to the data, not a copy
135 //return user_comments[i] + taglen + 1;
144 int unpack(Buffer opb){
145 int vendorlen=opb.read(32);
151 vendor=new byte[vendorlen+1];
152 opb.read(vendor,vendorlen);
153 comments=opb.read(32);
159 user_comments=new byte[comments+1][];
160 comment_lengths=new int[comments+1];
162 for(int i=0;i<comments;i++){
163 int len=opb.read(32);
169 comment_lengths[i]=len;
170 user_comments[i]=new byte[len+1];
171 opb.read(user_comments[i], len);
174 //goto err_out; // EOP check
181 // comment_clear(vc);
185 int pack(Buffer opb){
186 byte[] temp="Xiphophorus libVorbis I 20000508".getBytes();
193 opb.write(temp.length,32);
198 opb.write(comments,32);
200 for(int i=0;i<comments;i++){
201 if(user_comments[i]!=null){
202 opb.write(comment_lengths[i],32);
203 opb.write(user_comments[i]);
214 public int header_out(Packet op){
215 Buffer opb=new Buffer();
218 if(pack(opb)!=0) return OV_EIMPL;
220 op.packet_base = new byte[opb.bytes()];
222 op.bytes=opb.bytes();
223 System.arraycopy(opb.buffer(), 0, op.packet_base, 0, op.bytes);
231 for(int i=0;i<comments;i++)
232 user_comments[i]=null;
237 public String getVendor(){
238 return new String(vendor, 0, vendor.length-1);
240 public String getComment(int i){
241 if(comments<=i)return null;
242 return new String(user_comments[i], 0, user_comments[i].length-1);
244 public String toString(){
245 String foo="Vendor: "+new String(vendor, 0, vendor.length-1);
246 for(int i=0; i<comments; i++){
247 foo=foo+"\nComment: "+new String(user_comments[i], 0, user_comments[i].length-1);