]>
Commit | Line | Data |
---|---|---|
964dd0bc JW |
1 | /* JOrbis |
2 | * Copyright (C) 2000 ymnk, JCraft,Inc. | |
3 | * | |
4 | * Written by: 2000 ymnk<ymnk@jcraft.com> | |
5 | * | |
6 | * Many thanks to | |
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. | |
10 | * | |
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. | |
15 | ||
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. | |
20 | * | |
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. | |
24 | */ | |
25 | ||
26 | package com.jcraft.jorbis; | |
27 | ||
28 | import com.jcraft.jogg.*; | |
29 | ||
30 | // the comments are not part of vorbis_info so that vorbis_info can be | |
31 | // static storage | |
32 | public class Comment{ | |
33 | private static byte[] _vorbis="vorbis".getBytes(); | |
34 | ||
35 | private static final int OV_EFAULT=-129; | |
36 | private static final int OV_EIMPL=-130; | |
37 | ||
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; | |
42 | public int comments; | |
43 | public byte[] vendor; | |
44 | ||
45 | public void init(){ | |
46 | user_comments=null; | |
47 | comments=0; | |
48 | vendor=null; | |
49 | } | |
50 | ||
51 | public void add(String comment){ | |
52 | add(comment.getBytes()); | |
53 | } | |
54 | ||
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); | |
59 | } | |
60 | user_comments=foo; | |
61 | ||
62 | int[] goo=new int[comments+2]; | |
63 | if(comment_lengths!=null){ | |
64 | System.arraycopy(comment_lengths, 0, goo, 0, comments); | |
65 | } | |
66 | comment_lengths=goo; | |
67 | ||
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; | |
72 | comments++; | |
73 | user_comments[comments]=null; | |
74 | } | |
75 | ||
76 | public void add_tag(String tag, String contents){ | |
77 | if(contents==null) contents=""; | |
78 | add(tag+"="+contents); | |
79 | } | |
80 | ||
81 | /* | |
82 | private void add_tag(byte[] tag, byte[] contents){ | |
83 | byte[] foo=new byte[tag.length+contents.length+1]; | |
84 | int j=0; | |
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];} | |
88 | add(foo); | |
89 | } | |
90 | */ | |
91 | ||
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){ | |
95 | int c=0; | |
96 | byte u1, u2; | |
97 | while(c < n){ | |
98 | u1=s1[c]; u2=s2[c]; | |
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; } | |
102 | c++; | |
103 | } | |
104 | return true; | |
105 | } | |
106 | ||
107 | public String query(String tag){ | |
108 | return query(tag, 0); | |
109 | } | |
110 | ||
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++){ | |
116 | if(comment[i]=='='){ | |
117 | return new String(comment, i+1, comment_lengths[foo]-(i+1)); | |
118 | } | |
119 | } | |
120 | return null; | |
121 | } | |
122 | ||
123 | private int query(byte[] tag, int count){ | |
124 | int i=0; | |
125 | int found = 0; | |
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)'='; | |
130 | ||
131 | for(i=0;i<comments;i++){ | |
132 | if(tagcompare(user_comments[i], fulltag, fulltaglen)){ | |
133 | if(count==found){ | |
134 | // We return a pointer to the data, not a copy | |
135 | //return user_comments[i] + taglen + 1; | |
136 | return i; | |
137 | } | |
138 | else{ found++; } | |
139 | } | |
140 | } | |
141 | return -1; | |
142 | } | |
143 | ||
144 | int unpack(Buffer opb){ | |
145 | int vendorlen=opb.read(32); | |
146 | if(vendorlen<0){ | |
147 | //goto err_out; | |
148 | clear(); | |
149 | return(-1); | |
150 | } | |
151 | vendor=new byte[vendorlen+1]; | |
152 | opb.read(vendor,vendorlen); | |
153 | comments=opb.read(32); | |
154 | if(comments<0){ | |
155 | //goto err_out; | |
156 | clear(); | |
157 | return(-1); | |
158 | } | |
159 | user_comments=new byte[comments+1][]; | |
160 | comment_lengths=new int[comments+1]; | |
161 | ||
162 | for(int i=0;i<comments;i++){ | |
163 | int len=opb.read(32); | |
164 | if(len<0){ | |
165 | //goto err_out; | |
166 | clear(); | |
167 | return(-1); | |
168 | } | |
169 | comment_lengths[i]=len; | |
170 | user_comments[i]=new byte[len+1]; | |
171 | opb.read(user_comments[i], len); | |
172 | } | |
173 | if(opb.read(1)!=1){ | |
174 | //goto err_out; // EOP check | |
175 | clear(); | |
176 | return(-1); | |
177 | ||
178 | } | |
179 | return(0); | |
180 | // err_out: | |
181 | // comment_clear(vc); | |
182 | // return(-1); | |
183 | } | |
184 | ||
185 | int pack(Buffer opb){ | |
186 | byte[] temp="Xiphophorus libVorbis I 20000508".getBytes(); | |
187 | ||
188 | // preamble | |
189 | opb.write(0x03,8); | |
190 | opb.write(_vorbis); | |
191 | ||
192 | // vendor | |
193 | opb.write(temp.length,32); | |
194 | opb.write(temp); | |
195 | ||
196 | // comments | |
197 | ||
198 | opb.write(comments,32); | |
199 | if(comments!=0){ | |
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]); | |
204 | } | |
205 | else{ | |
206 | opb.write(0,32); | |
207 | } | |
208 | } | |
209 | } | |
210 | opb.write(1,1); | |
211 | return(0); | |
212 | } | |
213 | ||
214 | public int header_out(Packet op){ | |
215 | Buffer opb=new Buffer(); | |
216 | opb.writeinit(); | |
217 | ||
218 | if(pack(opb)!=0) return OV_EIMPL; | |
219 | ||
220 | op.packet_base = new byte[opb.bytes()]; | |
221 | op.packet=0; | |
222 | op.bytes=opb.bytes(); | |
223 | System.arraycopy(opb.buffer(), 0, op.packet_base, 0, op.bytes); | |
224 | op.b_o_s=0; | |
225 | op.e_o_s=0; | |
226 | op.granulepos=0; | |
227 | return 0; | |
228 | } | |
229 | ||
230 | void clear(){ | |
231 | for(int i=0;i<comments;i++) | |
232 | user_comments[i]=null; | |
233 | user_comments=null; | |
234 | vendor=null; | |
235 | } | |
236 | ||
237 | public String getVendor(){ | |
238 | return new String(vendor, 0, vendor.length-1); | |
239 | } | |
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); | |
243 | } | |
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); | |
248 | } | |
249 | foo=foo+"\n"; | |
250 | return foo; | |
251 | } | |
252 | } |