Mercurial > libavcodec.hg
comparison vorbis_enc.c @ 3818:6a737f2d852e libavcodec
Original Commit: r12 | ods15 | 2006-09-22 09:28:30 +0300 (Fri, 22 Sep 2006) | 5 lines
entry_t -> cb_entry_t
add float * dimentions, it will be needed for deciding the correct codebook entry
document blocksize
first steps to creating the context to be used
author | ods15 |
---|---|
date | Mon, 02 Oct 2006 05:55:33 +0000 |
parents | 3ca15f0d4882 |
children | 92063e9b6728 |
comparison
equal
deleted
inserted
replaced
3817:3ca15f0d4882 | 3818:6a737f2d852e |
---|---|
35 #define BUFFER_SIZE (1024*64) | 35 #define BUFFER_SIZE (1024*64) |
36 | 36 |
37 typedef struct { | 37 typedef struct { |
38 int len; | 38 int len; |
39 uint32_t codeword; | 39 uint32_t codeword; |
40 } entry_t; | 40 } cb_entry_t; |
41 | 41 |
42 typedef struct { | 42 typedef struct { |
43 int nentries; | 43 int nentries; |
44 entry_t * entries; | 44 cb_entry_t * entries; |
45 int ndimentions; | 45 int ndimentions; |
46 float min; | 46 float min; |
47 float delta; | 47 float delta; |
48 int seq_p; | 48 int seq_p; |
49 int lookup; | 49 int lookup; |
50 //float * dimentions; | |
51 int * quantlist; | 50 int * quantlist; |
51 float * dimentions; | |
52 } codebook_t; | 52 } codebook_t; |
53 | 53 |
54 typedef struct { | 54 typedef struct { |
55 int dim; | 55 int dim; |
56 int subclass; | 56 int subclass; |
92 } vorbis_mode_t; | 92 } vorbis_mode_t; |
93 | 93 |
94 typedef struct { | 94 typedef struct { |
95 int channels; | 95 int channels; |
96 int sample_rate; | 96 int sample_rate; |
97 int blocksize[2]; | 97 int blocksize[2]; // in (1<<n) format |
98 | 98 |
99 int ncodebooks; | 99 int ncodebooks; |
100 codebook_t * codebooks; | 100 codebook_t * codebooks; |
101 | 101 |
102 int nfloors; | 102 int nfloors; |
109 mapping_t * mappings; | 109 mapping_t * mappings; |
110 | 110 |
111 int nmodes; | 111 int nmodes; |
112 vorbis_mode_t * modes; | 112 vorbis_mode_t * modes; |
113 } venc_context_t; | 113 } venc_context_t; |
114 | |
115 static int cb_lookup_vals(int lookup, int dimentions, int entries) { | |
116 if (lookup == 1) { | |
117 int tmp, i; | |
118 for (tmp = 0; ; tmp++) { | |
119 int n = 1; | |
120 for (i = 0; i < dimentions; i++) n *= tmp; | |
121 if (n > entries) break; | |
122 } | |
123 return tmp - 1; | |
124 } else if (lookup == 2) return dimentions * entries; | |
125 return 0; | |
126 } | |
127 | |
128 static void ready_codebook(codebook_t * cb) { | |
129 int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
130 int i; | |
131 | |
132 for (i = 0; i < cb->nentries; i++) { | |
133 cb_entry_t * e = &cb->entries[i]; | |
134 int j = 0; | |
135 if (h[0]) h[0] = 0; | |
136 else for (j = e->len; !h[j]; j--) assert(j); | |
137 e->codeword = h[j]; | |
138 h[j] = 0; | |
139 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1)); | |
140 } | |
141 for (i = 0; i < 33; i++) assert(!h[i]); | |
142 | |
143 if (!cb->lookup) cb->dimentions = NULL; | |
144 else { | |
145 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); | |
146 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions); | |
147 for (i = 0; i < cb->nentries; i++) { | |
148 float last = 0; | |
149 int j; | |
150 int div = 1; | |
151 for (j = 0; j < cb->ndimentions; j++) { | |
152 int off; | |
153 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1 | |
154 else off = i * cb->ndimentions + j; // lookup type 2 | |
155 | |
156 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta; | |
157 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j]; | |
158 div *= vals; | |
159 } | |
160 } | |
161 } | |
162 | |
163 } | |
164 | |
165 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) { | |
166 codebook_t * cb; | |
167 int i; | |
168 | |
169 venc->channels = avccontext->channels; | |
170 venc->sample_rate = avccontext->sample_rate; | |
171 venc->blocksize[0] = venc->blocksize[1] = 8; | |
172 | |
173 venc->ncodebooks = 3; | |
174 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks); | |
175 | |
176 // codebook 1 - floor1 book, values 0..255 | |
177 cb = &venc->codebooks[0]; | |
178 cb->nentries = 256; | |
179 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries); | |
180 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8; | |
181 cb->ndimentions = 0; | |
182 cb->min = 0.; | |
183 cb->delta = 0.; | |
184 cb->seq_p = 0; | |
185 cb->lookup = 0; | |
186 cb->quantlist = NULL; | |
187 ready_codebook(cb); | |
188 | |
189 // codebook 2 - classbook, values 0..0, dimentions 200 | |
190 cb = &venc->codebooks[1]; | |
191 cb->nentries = 1; | |
192 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries); | |
193 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1; | |
194 cb->ndimentions = 200; | |
195 cb->min = 0.; | |
196 cb->delta = 0.; | |
197 cb->seq_p = 0; | |
198 cb->lookup = 0; | |
199 cb->quantlist = NULL; | |
200 ready_codebook(cb); | |
201 | |
202 // codebook 3 - vector, for the residue, dimentions 1. values -32768..32767 | |
203 cb = &venc->codebooks[2]; | |
204 cb->nentries = 32767 - (-32768); | |
205 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries); | |
206 for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 16; | |
207 cb->ndimentions = 1; | |
208 cb->min = -32768.; | |
209 cb->delta = 1.; | |
210 cb->seq_p = 0; | |
211 cb->lookup = 2; | |
212 cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries)); | |
213 for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i; | |
214 ready_codebook(cb); | |
215 | |
216 } | |
114 | 217 |
115 static inline int ilog(unsigned int a) { | 218 static inline int ilog(unsigned int a) { |
116 int i; | 219 int i; |
117 for (i = 0; a >> i; i++); | 220 for (i = 0; a >> i; i++); |
118 return i; | 221 return i; |
163 } | 266 } |
164 } | 267 } |
165 | 268 |
166 put_bits(pb, 4, cb->lookup); | 269 put_bits(pb, 4, cb->lookup); |
167 if (cb->lookup) { | 270 if (cb->lookup) { |
168 int tmp, bits = ilog(cb->quantlist[0]); | 271 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); |
169 | 272 int bits = ilog(cb->quantlist[0]); |
170 if (cb->lookup == 1) { | |
171 for (tmp = 0; ; tmp++) { | |
172 int n = 1; | |
173 for (i = 0; i < cb->ndimentions; i++) n *= tmp; | |
174 if (n > cb->nentries) break; | |
175 } | |
176 tmp--; | |
177 } else tmp = cb->ndimentions * cb->nentries; | |
178 | 273 |
179 for (i = 1; i < tmp; i++) bits = FFMIN(bits, ilog(cb->quantlist[i])); | 274 for (i = 1; i < tmp; i++) bits = FFMIN(bits, ilog(cb->quantlist[i])); |
180 | 275 |
181 put_float(pb, cb->min); | 276 put_float(pb, cb->min); |
182 put_float(pb, cb->delta); | 277 put_float(pb, cb->delta); |
358 | 453 |
359 static int vorbis_encode_init(AVCodecContext * avccontext) | 454 static int vorbis_encode_init(AVCodecContext * avccontext) |
360 { | 455 { |
361 venc_context_t * venc = avccontext->priv_data; | 456 venc_context_t * venc = avccontext->priv_data; |
362 | 457 |
363 venc->channels = avccontext->channels; | 458 create_vorbis_context(venc, avccontext); |
364 venc->sample_rate = avccontext->sample_rate; | |
365 | 459 |
366 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate; | 460 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate; |
367 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0; | 461 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0; |
368 | 462 |
369 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata); | 463 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata); |