1 | /**
|
---|
2 | * FLAC audio encoder
|
---|
3 | * Copyright (c) 2006 Justin Ruggles <jruggle@earthlink.net>
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "avcodec.h"
|
---|
21 | #include "bitstream.h"
|
---|
22 | #include "crc.h"
|
---|
23 | #include "golomb.h"
|
---|
24 |
|
---|
25 | #define FLAC_MAX_CH 8
|
---|
26 | #define FLAC_MIN_BLOCKSIZE 16
|
---|
27 | #define FLAC_MAX_BLOCKSIZE 65535
|
---|
28 |
|
---|
29 | #define FLAC_SUBFRAME_CONSTANT 0
|
---|
30 | #define FLAC_SUBFRAME_VERBATIM 1
|
---|
31 | #define FLAC_SUBFRAME_FIXED 8
|
---|
32 | #define FLAC_SUBFRAME_LPC 32
|
---|
33 |
|
---|
34 | #define FLAC_CHMODE_NOT_STEREO 0
|
---|
35 | #define FLAC_CHMODE_LEFT_RIGHT 1
|
---|
36 | #define FLAC_CHMODE_LEFT_SIDE 8
|
---|
37 | #define FLAC_CHMODE_RIGHT_SIDE 9
|
---|
38 | #define FLAC_CHMODE_MID_SIDE 10
|
---|
39 |
|
---|
40 | #define ORDER_METHOD_EST 0
|
---|
41 | #define ORDER_METHOD_2LEVEL 1
|
---|
42 | #define ORDER_METHOD_4LEVEL 2
|
---|
43 | #define ORDER_METHOD_8LEVEL 3
|
---|
44 | #define ORDER_METHOD_SEARCH 4
|
---|
45 |
|
---|
46 | #define FLAC_STREAMINFO_SIZE 34
|
---|
47 |
|
---|
48 | #define MIN_LPC_ORDER 1
|
---|
49 | #define MAX_LPC_ORDER 32
|
---|
50 | #define MAX_FIXED_ORDER 4
|
---|
51 | #define MAX_PARTITION_ORDER 8
|
---|
52 | #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
|
---|
53 | #define MAX_LPC_PRECISION 15
|
---|
54 | #define MAX_LPC_SHIFT 15
|
---|
55 | #define MAX_RICE_PARAM 14
|
---|
56 |
|
---|
57 | typedef struct CompressionOptions {
|
---|
58 | int compression_level;
|
---|
59 | int block_time_ms;
|
---|
60 | int use_lpc;
|
---|
61 | int lpc_coeff_precision;
|
---|
62 | int min_prediction_order;
|
---|
63 | int max_prediction_order;
|
---|
64 | int prediction_order_method;
|
---|
65 | int min_partition_order;
|
---|
66 | int max_partition_order;
|
---|
67 | } CompressionOptions;
|
---|
68 |
|
---|
69 | typedef struct RiceContext {
|
---|
70 | int porder;
|
---|
71 | int params[MAX_PARTITIONS];
|
---|
72 | } RiceContext;
|
---|
73 |
|
---|
74 | typedef struct FlacSubframe {
|
---|
75 | int type;
|
---|
76 | int type_code;
|
---|
77 | int obits;
|
---|
78 | int order;
|
---|
79 | int32_t coefs[MAX_LPC_ORDER];
|
---|
80 | int shift;
|
---|
81 | RiceContext rc;
|
---|
82 | int32_t samples[FLAC_MAX_BLOCKSIZE];
|
---|
83 | int32_t residual[FLAC_MAX_BLOCKSIZE];
|
---|
84 | } FlacSubframe;
|
---|
85 |
|
---|
86 | typedef struct FlacFrame {
|
---|
87 | FlacSubframe subframes[FLAC_MAX_CH];
|
---|
88 | int blocksize;
|
---|
89 | int bs_code[2];
|
---|
90 | uint8_t crc8;
|
---|
91 | int ch_mode;
|
---|
92 | } FlacFrame;
|
---|
93 |
|
---|
94 | typedef struct FlacEncodeContext {
|
---|
95 | PutBitContext pb;
|
---|
96 | int channels;
|
---|
97 | int ch_code;
|
---|
98 | int samplerate;
|
---|
99 | int sr_code[2];
|
---|
100 | int blocksize;
|
---|
101 | int max_framesize;
|
---|
102 | uint32_t frame_count;
|
---|
103 | FlacFrame frame;
|
---|
104 | CompressionOptions options;
|
---|
105 | AVCodecContext *avctx;
|
---|
106 | } FlacEncodeContext;
|
---|
107 |
|
---|
108 | static const int flac_samplerates[16] = {
|
---|
109 | 0, 0, 0, 0,
|
---|
110 | 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
|
---|
111 | 0, 0, 0, 0
|
---|
112 | };
|
---|
113 |
|
---|
114 | static const int flac_blocksizes[16] = {
|
---|
115 | 0,
|
---|
116 | 192,
|
---|
117 | 576, 1152, 2304, 4608,
|
---|
118 | 0, 0,
|
---|
119 | 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
|
---|
120 | };
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Writes streaminfo metadata block to byte array
|
---|
124 | */
|
---|
125 | static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
|
---|
126 | {
|
---|
127 | PutBitContext pb;
|
---|
128 |
|
---|
129 | memset(header, 0, FLAC_STREAMINFO_SIZE);
|
---|
130 | init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
|
---|
131 |
|
---|
132 | /* streaminfo metadata block */
|
---|
133 | put_bits(&pb, 16, s->blocksize);
|
---|
134 | put_bits(&pb, 16, s->blocksize);
|
---|
135 | put_bits(&pb, 24, 0);
|
---|
136 | put_bits(&pb, 24, s->max_framesize);
|
---|
137 | put_bits(&pb, 20, s->samplerate);
|
---|
138 | put_bits(&pb, 3, s->channels-1);
|
---|
139 | put_bits(&pb, 5, 15); /* bits per sample - 1 */
|
---|
140 | flush_put_bits(&pb);
|
---|
141 | /* total samples = 0 */
|
---|
142 | /* MD5 signature = 0 */
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Sets blocksize based on samplerate
|
---|
147 | * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
|
---|
148 | */
|
---|
149 | static int select_blocksize(int samplerate, int block_time_ms)
|
---|
150 | {
|
---|
151 | int i;
|
---|
152 | int target;
|
---|
153 | int blocksize;
|
---|
154 |
|
---|
155 | assert(samplerate > 0);
|
---|
156 | blocksize = flac_blocksizes[1];
|
---|
157 | target = (samplerate * block_time_ms) / 1000;
|
---|
158 | for(i=0; i<16; i++) {
|
---|
159 | if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
|
---|
160 | blocksize = flac_blocksizes[i];
|
---|
161 | }
|
---|
162 | }
|
---|
163 | return blocksize;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int flac_encode_init(AVCodecContext *avctx)
|
---|
167 | {
|
---|
168 | int freq = avctx->sample_rate;
|
---|
169 | int channels = avctx->channels;
|
---|
170 | FlacEncodeContext *s = avctx->priv_data;
|
---|
171 | int i, level;
|
---|
172 | uint8_t *streaminfo;
|
---|
173 |
|
---|
174 | s->avctx = avctx;
|
---|
175 |
|
---|
176 | if(avctx->sample_fmt != SAMPLE_FMT_S16) {
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if(channels < 1 || channels > FLAC_MAX_CH) {
|
---|
181 | return -1;
|
---|
182 | }
|
---|
183 | s->channels = channels;
|
---|
184 | s->ch_code = s->channels-1;
|
---|
185 |
|
---|
186 | /* find samplerate in table */
|
---|
187 | if(freq < 1)
|
---|
188 | return -1;
|
---|
189 | for(i=4; i<12; i++) {
|
---|
190 | if(freq == flac_samplerates[i]) {
|
---|
191 | s->samplerate = flac_samplerates[i];
|
---|
192 | s->sr_code[0] = i;
|
---|
193 | s->sr_code[1] = 0;
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | /* if not in table, samplerate is non-standard */
|
---|
198 | if(i == 12) {
|
---|
199 | if(freq % 1000 == 0 && freq < 255000) {
|
---|
200 | s->sr_code[0] = 12;
|
---|
201 | s->sr_code[1] = freq / 1000;
|
---|
202 | } else if(freq % 10 == 0 && freq < 655350) {
|
---|
203 | s->sr_code[0] = 14;
|
---|
204 | s->sr_code[1] = freq / 10;
|
---|
205 | } else if(freq < 65535) {
|
---|
206 | s->sr_code[0] = 13;
|
---|
207 | s->sr_code[1] = freq;
|
---|
208 | } else {
|
---|
209 | return -1;
|
---|
210 | }
|
---|
211 | s->samplerate = freq;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* set compression option defaults based on avctx->compression_level */
|
---|
215 | if(avctx->compression_level < 0) {
|
---|
216 | s->options.compression_level = 5;
|
---|
217 | } else {
|
---|
218 | s->options.compression_level = avctx->compression_level;
|
---|
219 | }
|
---|
220 | av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level);
|
---|
221 |
|
---|
222 | level= s->options.compression_level;
|
---|
223 | if(level > 5) {
|
---|
224 | av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
|
---|
225 | s->options.compression_level);
|
---|
226 | return -1;
|
---|
227 | }
|
---|
228 |
|
---|
229 | s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105})[level];
|
---|
230 | s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1})[level];
|
---|
231 | s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1})[level];
|
---|
232 | s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8})[level];
|
---|
233 | s->options.prediction_order_method = ORDER_METHOD_EST;
|
---|
234 | s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0})[level];
|
---|
235 | s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8})[level];
|
---|
236 |
|
---|
237 | /* set compression option overrides from AVCodecContext */
|
---|
238 | if(avctx->use_lpc >= 0) {
|
---|
239 | s->options.use_lpc = !!avctx->use_lpc;
|
---|
240 | }
|
---|
241 | av_log(avctx, AV_LOG_DEBUG, " use lpc: %s\n",
|
---|
242 | s->options.use_lpc? "yes" : "no");
|
---|
243 |
|
---|
244 | if(avctx->min_prediction_order >= 0) {
|
---|
245 | if(s->options.use_lpc) {
|
---|
246 | if(avctx->min_prediction_order < MIN_LPC_ORDER ||
|
---|
247 | avctx->min_prediction_order > MAX_LPC_ORDER) {
|
---|
248 | av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
|
---|
249 | avctx->min_prediction_order);
|
---|
250 | return -1;
|
---|
251 | }
|
---|
252 | } else {
|
---|
253 | if(avctx->min_prediction_order > MAX_FIXED_ORDER) {
|
---|
254 | av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
|
---|
255 | avctx->min_prediction_order);
|
---|
256 | return -1;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | s->options.min_prediction_order = avctx->min_prediction_order;
|
---|
260 | }
|
---|
261 | if(avctx->max_prediction_order >= 0) {
|
---|
262 | if(s->options.use_lpc) {
|
---|
263 | if(avctx->max_prediction_order < MIN_LPC_ORDER ||
|
---|
264 | avctx->max_prediction_order > MAX_LPC_ORDER) {
|
---|
265 | av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
|
---|
266 | avctx->max_prediction_order);
|
---|
267 | return -1;
|
---|
268 | }
|
---|
269 | } else {
|
---|
270 | if(avctx->max_prediction_order > MAX_FIXED_ORDER) {
|
---|
271 | av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
|
---|
272 | avctx->max_prediction_order);
|
---|
273 | return -1;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | s->options.max_prediction_order = avctx->max_prediction_order;
|
---|
277 | }
|
---|
278 | if(s->options.max_prediction_order < s->options.min_prediction_order) {
|
---|
279 | av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
|
---|
280 | s->options.min_prediction_order, s->options.max_prediction_order);
|
---|
281 | return -1;
|
---|
282 | }
|
---|
283 | av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
|
---|
284 | s->options.min_prediction_order, s->options.max_prediction_order);
|
---|
285 |
|
---|
286 | if(avctx->prediction_order_method >= 0) {
|
---|
287 | if(avctx->prediction_order_method > ORDER_METHOD_SEARCH) {
|
---|
288 | av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
|
---|
289 | avctx->prediction_order_method);
|
---|
290 | return -1;
|
---|
291 | }
|
---|
292 | s->options.prediction_order_method = avctx->prediction_order_method;
|
---|
293 | }
|
---|
294 | switch(avctx->prediction_order_method) {
|
---|
295 | case ORDER_METHOD_EST: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
|
---|
296 | "estimate"); break;
|
---|
297 | case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
|
---|
298 | "2-level"); break;
|
---|
299 | case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
|
---|
300 | "4-level"); break;
|
---|
301 | case ORDER_METHOD_8LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
|
---|
302 | "8-level"); break;
|
---|
303 | case ORDER_METHOD_SEARCH: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
|
---|
304 | "full search"); break;
|
---|
305 | }
|
---|
306 |
|
---|
307 | if(avctx->min_partition_order >= 0) {
|
---|
308 | if(avctx->min_partition_order > MAX_PARTITION_ORDER) {
|
---|
309 | av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
|
---|
310 | avctx->min_partition_order);
|
---|
311 | return -1;
|
---|
312 | }
|
---|
313 | s->options.min_partition_order = avctx->min_partition_order;
|
---|
314 | }
|
---|
315 | if(avctx->max_partition_order >= 0) {
|
---|
316 | if(avctx->max_partition_order > MAX_PARTITION_ORDER) {
|
---|
317 | av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
|
---|
318 | avctx->max_partition_order);
|
---|
319 | return -1;
|
---|
320 | }
|
---|
321 | s->options.max_partition_order = avctx->max_partition_order;
|
---|
322 | }
|
---|
323 | if(s->options.max_partition_order < s->options.min_partition_order) {
|
---|
324 | av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
|
---|
325 | s->options.min_partition_order, s->options.max_partition_order);
|
---|
326 | return -1;
|
---|
327 | }
|
---|
328 | av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
|
---|
329 | s->options.min_partition_order, s->options.max_partition_order);
|
---|
330 |
|
---|
331 | if(avctx->frame_size > 0) {
|
---|
332 | if(avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
|
---|
333 | avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
|
---|
334 | av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
|
---|
335 | avctx->frame_size);
|
---|
336 | return -1;
|
---|
337 | }
|
---|
338 | s->blocksize = avctx->frame_size;
|
---|
339 | } else {
|
---|
340 | s->blocksize = select_blocksize(s->samplerate, s->options.block_time_ms);
|
---|
341 | avctx->frame_size = s->blocksize;
|
---|
342 | }
|
---|
343 | av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", s->blocksize);
|
---|
344 |
|
---|
345 | /* set LPC precision */
|
---|
346 | if(avctx->lpc_coeff_precision > 0) {
|
---|
347 | if(avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
|
---|
348 | av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
|
---|
349 | avctx->lpc_coeff_precision);
|
---|
350 | return -1;
|
---|
351 | }
|
---|
352 | s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
|
---|
353 | } else {
|
---|
354 | /* select LPC precision based on block size */
|
---|
355 | if( s->blocksize <= 192) s->options.lpc_coeff_precision = 7;
|
---|
356 | else if(s->blocksize <= 384) s->options.lpc_coeff_precision = 8;
|
---|
357 | else if(s->blocksize <= 576) s->options.lpc_coeff_precision = 9;
|
---|
358 | else if(s->blocksize <= 1152) s->options.lpc_coeff_precision = 10;
|
---|
359 | else if(s->blocksize <= 2304) s->options.lpc_coeff_precision = 11;
|
---|
360 | else if(s->blocksize <= 4608) s->options.lpc_coeff_precision = 12;
|
---|
361 | else if(s->blocksize <= 8192) s->options.lpc_coeff_precision = 13;
|
---|
362 | else if(s->blocksize <= 16384) s->options.lpc_coeff_precision = 14;
|
---|
363 | else s->options.lpc_coeff_precision = 15;
|
---|
364 | }
|
---|
365 | av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
|
---|
366 | s->options.lpc_coeff_precision);
|
---|
367 |
|
---|
368 | /* set maximum encoded frame size in verbatim mode */
|
---|
369 | if(s->channels == 2) {
|
---|
370 | s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3);
|
---|
371 | } else {
|
---|
372 | s->max_framesize = 14 + (s->blocksize * s->channels * 2);
|
---|
373 | }
|
---|
374 |
|
---|
375 | streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
|
---|
376 | write_streaminfo(s, streaminfo);
|
---|
377 | avctx->extradata = streaminfo;
|
---|
378 | avctx->extradata_size = FLAC_STREAMINFO_SIZE;
|
---|
379 |
|
---|
380 | s->frame_count = 0;
|
---|
381 |
|
---|
382 | avctx->coded_frame = avcodec_alloc_frame();
|
---|
383 | avctx->coded_frame->key_frame = 1;
|
---|
384 |
|
---|
385 | return 0;
|
---|
386 | }
|
---|
387 |
|
---|
388 | static void init_frame(FlacEncodeContext *s)
|
---|
389 | {
|
---|
390 | int i, ch;
|
---|
391 | FlacFrame *frame;
|
---|
392 |
|
---|
393 | frame = &s->frame;
|
---|
394 |
|
---|
395 | for(i=0; i<16; i++) {
|
---|
396 | if(s->blocksize == flac_blocksizes[i]) {
|
---|
397 | frame->blocksize = flac_blocksizes[i];
|
---|
398 | frame->bs_code[0] = i;
|
---|
399 | frame->bs_code[1] = 0;
|
---|
400 | break;
|
---|
401 | }
|
---|
402 | }
|
---|
403 | if(i == 16) {
|
---|
404 | frame->blocksize = s->blocksize;
|
---|
405 | if(frame->blocksize <= 256) {
|
---|
406 | frame->bs_code[0] = 6;
|
---|
407 | frame->bs_code[1] = frame->blocksize-1;
|
---|
408 | } else {
|
---|
409 | frame->bs_code[0] = 7;
|
---|
410 | frame->bs_code[1] = frame->blocksize-1;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | for(ch=0; ch<s->channels; ch++) {
|
---|
415 | frame->subframes[ch].obits = 16;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Copy channel-interleaved input samples into separate subframes
|
---|
421 | */
|
---|
422 | static void copy_samples(FlacEncodeContext *s, int16_t *samples)
|
---|
423 | {
|
---|
424 | int i, j, ch;
|
---|
425 | FlacFrame *frame;
|
---|
426 |
|
---|
427 | frame = &s->frame;
|
---|
428 | for(i=0,j=0; i<frame->blocksize; i++) {
|
---|
429 | for(ch=0; ch<s->channels; ch++,j++) {
|
---|
430 | frame->subframes[ch].samples[i] = samples[j];
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
|
---|
437 |
|
---|
438 | static int find_optimal_param(uint32_t sum, int n)
|
---|
439 | {
|
---|
440 | int k, k_opt;
|
---|
441 | uint32_t nbits[MAX_RICE_PARAM+1];
|
---|
442 |
|
---|
443 | k_opt = 0;
|
---|
444 | nbits[0] = UINT32_MAX;
|
---|
445 | for(k=0; k<=MAX_RICE_PARAM; k++) {
|
---|
446 | nbits[k] = rice_encode_count(sum, n, k);
|
---|
447 | if(nbits[k] < nbits[k_opt]) {
|
---|
448 | k_opt = k;
|
---|
449 | }
|
---|
450 | }
|
---|
451 | return k_opt;
|
---|
452 | }
|
---|
453 |
|
---|
454 | static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
|
---|
455 | uint32_t *sums, int n, int pred_order)
|
---|
456 | {
|
---|
457 | int i;
|
---|
458 | int k, cnt, part;
|
---|
459 | uint32_t all_bits;
|
---|
460 |
|
---|
461 | part = (1 << porder);
|
---|
462 | all_bits = 0;
|
---|
463 |
|
---|
464 | cnt = (n >> porder) - pred_order;
|
---|
465 | for(i=0; i<part; i++) {
|
---|
466 | if(i == 1) cnt = (n >> porder);
|
---|
467 | k = find_optimal_param(sums[i], cnt);
|
---|
468 | rc->params[i] = k;
|
---|
469 | all_bits += rice_encode_count(sums[i], cnt, k);
|
---|
470 | }
|
---|
471 | all_bits += (4 * part);
|
---|
472 |
|
---|
473 | rc->porder = porder;
|
---|
474 |
|
---|
475 | return all_bits;
|
---|
476 | }
|
---|
477 |
|
---|
478 | static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
|
---|
479 | uint32_t sums[][MAX_PARTITIONS])
|
---|
480 | {
|
---|
481 | int i, j;
|
---|
482 | int parts;
|
---|
483 | uint32_t *res, *res_end;
|
---|
484 |
|
---|
485 | /* sums for highest level */
|
---|
486 | parts = (1 << pmax);
|
---|
487 | res = &data[pred_order];
|
---|
488 | res_end = &data[n >> pmax];
|
---|
489 | for(i=0; i<parts; i++) {
|
---|
490 | sums[pmax][i] = 0;
|
---|
491 | while(res < res_end){
|
---|
492 | sums[pmax][i] += *(res++);
|
---|
493 | }
|
---|
494 | res_end+= n >> pmax;
|
---|
495 | }
|
---|
496 | /* sums for lower levels */
|
---|
497 | for(i=pmax-1; i>=pmin; i--) {
|
---|
498 | parts = (1 << i);
|
---|
499 | for(j=0; j<parts; j++) {
|
---|
500 | sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
|
---|
501 | }
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
|
---|
506 | int32_t *data, int n, int pred_order)
|
---|
507 | {
|
---|
508 | int i;
|
---|
509 | uint32_t bits[MAX_PARTITION_ORDER+1];
|
---|
510 | int opt_porder;
|
---|
511 | RiceContext tmp_rc;
|
---|
512 | uint32_t *udata;
|
---|
513 | uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
|
---|
514 |
|
---|
515 | assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
|
---|
516 | assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
|
---|
517 | assert(pmin <= pmax);
|
---|
518 |
|
---|
519 | udata = av_malloc(n * sizeof(uint32_t));
|
---|
520 | for(i=0; i<n; i++) {
|
---|
521 | udata[i] = (2*data[i]) ^ (data[i]>>31);
|
---|
522 | }
|
---|
523 |
|
---|
524 | calc_sums(pmin, pmax, udata, n, pred_order, sums);
|
---|
525 |
|
---|
526 | opt_porder = pmin;
|
---|
527 | bits[pmin] = UINT32_MAX;
|
---|
528 | for(i=pmin; i<=pmax; i++) {
|
---|
529 | bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
|
---|
530 | if(bits[i] <= bits[opt_porder]) {
|
---|
531 | opt_porder = i;
|
---|
532 | *rc= tmp_rc;
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | av_freep(&udata);
|
---|
537 | return bits[opt_porder];
|
---|
538 | }
|
---|
539 |
|
---|
540 | static int get_max_p_order(int max_porder, int n, int order)
|
---|
541 | {
|
---|
542 | int porder = FFMIN(max_porder, av_log2(n^(n-1)));
|
---|
543 | if(order > 0)
|
---|
544 | porder = FFMIN(porder, av_log2(n/order));
|
---|
545 | return porder;
|
---|
546 | }
|
---|
547 |
|
---|
548 | static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
|
---|
549 | int32_t *data, int n, int pred_order,
|
---|
550 | int bps)
|
---|
551 | {
|
---|
552 | uint32_t bits;
|
---|
553 | pmin = get_max_p_order(pmin, n, pred_order);
|
---|
554 | pmax = get_max_p_order(pmax, n, pred_order);
|
---|
555 | bits = pred_order*bps + 6;
|
---|
556 | bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
|
---|
557 | return bits;
|
---|
558 | }
|
---|
559 |
|
---|
560 | static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
|
---|
561 | int32_t *data, int n, int pred_order,
|
---|
562 | int bps, int precision)
|
---|
563 | {
|
---|
564 | uint32_t bits;
|
---|
565 | pmin = get_max_p_order(pmin, n, pred_order);
|
---|
566 | pmax = get_max_p_order(pmax, n, pred_order);
|
---|
567 | bits = pred_order*bps + 4 + 5 + pred_order*precision + 6;
|
---|
568 | bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
|
---|
569 | return bits;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Apply Welch window function to audio block
|
---|
574 | */
|
---|
575 | static void apply_welch_window(const int32_t *data, int len, double *w_data)
|
---|
576 | {
|
---|
577 | int i, n2;
|
---|
578 | double w;
|
---|
579 | double c;
|
---|
580 |
|
---|
581 | n2 = (len >> 1);
|
---|
582 | c = 2.0 / (len - 1.0);
|
---|
583 | for(i=0; i<n2; i++) {
|
---|
584 | w = c - i - 1.0;
|
---|
585 | w = 1.0 - (w * w);
|
---|
586 | w_data[i] = data[i] * w;
|
---|
587 | w_data[len-1-i] = data[len-1-i] * w;
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Calculates autocorrelation data from audio samples
|
---|
593 | * A Welch window function is applied before calculation.
|
---|
594 | */
|
---|
595 | static void compute_autocorr(const int32_t *data, int len, int lag,
|
---|
596 | double *autoc)
|
---|
597 | {
|
---|
598 | int i, lag_ptr;
|
---|
599 | double tmp[len + lag];
|
---|
600 | double *data1= tmp + lag;
|
---|
601 |
|
---|
602 | apply_welch_window(data, len, data1);
|
---|
603 |
|
---|
604 | for(i=0; i<lag; i++){
|
---|
605 | autoc[i] = 1.0;
|
---|
606 | data1[i-lag]= 0.0;
|
---|
607 | }
|
---|
608 |
|
---|
609 | for(i=0; i<len; i++){
|
---|
610 | for(lag_ptr= i-lag; lag_ptr<=i; lag_ptr++){
|
---|
611 | autoc[i-lag_ptr] += data1[i] * data1[lag_ptr];
|
---|
612 | }
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Levinson-Durbin recursion.
|
---|
618 | * Produces LPC coefficients from autocorrelation data.
|
---|
619 | */
|
---|
620 | static void compute_lpc_coefs(const double *autoc, int max_order,
|
---|
621 | double lpc[][MAX_LPC_ORDER], double *ref)
|
---|
622 | {
|
---|
623 | int i, j, i2;
|
---|
624 | double r, err, tmp;
|
---|
625 | double lpc_tmp[MAX_LPC_ORDER];
|
---|
626 |
|
---|
627 | for(i=0; i<max_order; i++) lpc_tmp[i] = 0;
|
---|
628 | err = autoc[0];
|
---|
629 |
|
---|
630 | for(i=0; i<max_order; i++) {
|
---|
631 | r = -autoc[i+1];
|
---|
632 | for(j=0; j<i; j++) {
|
---|
633 | r -= lpc_tmp[j] * autoc[i-j];
|
---|
634 | }
|
---|
635 | r /= err;
|
---|
636 | ref[i] = fabs(r);
|
---|
637 |
|
---|
638 | err *= 1.0 - (r * r);
|
---|
639 |
|
---|
640 | i2 = (i >> 1);
|
---|
641 | lpc_tmp[i] = r;
|
---|
642 | for(j=0; j<i2; j++) {
|
---|
643 | tmp = lpc_tmp[j];
|
---|
644 | lpc_tmp[j] += r * lpc_tmp[i-1-j];
|
---|
645 | lpc_tmp[i-1-j] += r * tmp;
|
---|
646 | }
|
---|
647 | if(i & 1) {
|
---|
648 | lpc_tmp[j] += lpc_tmp[j] * r;
|
---|
649 | }
|
---|
650 |
|
---|
651 | for(j=0; j<=i; j++) {
|
---|
652 | lpc[i][j] = -lpc_tmp[j];
|
---|
653 | }
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Quantize LPC coefficients
|
---|
659 | */
|
---|
660 | static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
|
---|
661 | int32_t *lpc_out, int *shift)
|
---|
662 | {
|
---|
663 | int i;
|
---|
664 | double cmax;
|
---|
665 | int32_t qmax;
|
---|
666 | int sh;
|
---|
667 |
|
---|
668 | /* define maximum levels */
|
---|
669 | qmax = (1 << (precision - 1)) - 1;
|
---|
670 |
|
---|
671 | /* find maximum coefficient value */
|
---|
672 | cmax = 0.0;
|
---|
673 | for(i=0; i<order; i++) {
|
---|
674 | cmax= FFMAX(cmax, fabs(lpc_in[i]));
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* if maximum value quantizes to zero, return all zeros */
|
---|
678 | if(cmax * (1 << MAX_LPC_SHIFT) < 1.0) {
|
---|
679 | *shift = 0;
|
---|
680 | memset(lpc_out, 0, sizeof(int32_t) * order);
|
---|
681 | return;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* calculate level shift which scales max coeff to available bits */
|
---|
685 | sh = MAX_LPC_SHIFT;
|
---|
686 | while((cmax * (1 << sh) > qmax) && (sh > 0)) {
|
---|
687 | sh--;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /* since negative shift values are unsupported in decoder, scale down
|
---|
691 | coefficients instead */
|
---|
692 | if(sh == 0 && cmax > qmax) {
|
---|
693 | double scale = ((double)qmax) / cmax;
|
---|
694 | for(i=0; i<order; i++) {
|
---|
695 | lpc_in[i] *= scale;
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | /* output quantized coefficients and level shift */
|
---|
700 | for(i=0; i<order; i++) {
|
---|
701 | lpc_out[i] = (int32_t)(lpc_in[i] * (1 << sh));
|
---|
702 | }
|
---|
703 | *shift = sh;
|
---|
704 | }
|
---|
705 |
|
---|
706 | static int estimate_best_order(double *ref, int max_order)
|
---|
707 | {
|
---|
708 | int i, est;
|
---|
709 |
|
---|
710 | est = 1;
|
---|
711 | for(i=max_order-1; i>=0; i--) {
|
---|
712 | if(ref[i] > 0.10) {
|
---|
713 | est = i+1;
|
---|
714 | break;
|
---|
715 | }
|
---|
716 | }
|
---|
717 | return est;
|
---|
718 | }
|
---|
719 |
|
---|
720 | /**
|
---|
721 | * Calculate LPC coefficients for multiple orders
|
---|
722 | */
|
---|
723 | static int lpc_calc_coefs(const int32_t *samples, int blocksize, int max_order,
|
---|
724 | int precision, int32_t coefs[][MAX_LPC_ORDER],
|
---|
725 | int *shift)
|
---|
726 | {
|
---|
727 | double autoc[MAX_LPC_ORDER+1];
|
---|
728 | double ref[MAX_LPC_ORDER];
|
---|
729 | double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
|
---|
730 | int i;
|
---|
731 | int opt_order;
|
---|
732 |
|
---|
733 | assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
|
---|
734 |
|
---|
735 | compute_autocorr(samples, blocksize, max_order+1, autoc);
|
---|
736 |
|
---|
737 | compute_lpc_coefs(autoc, max_order, lpc, ref);
|
---|
738 |
|
---|
739 | opt_order = estimate_best_order(ref, max_order);
|
---|
740 |
|
---|
741 | i = opt_order-1;
|
---|
742 | quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
|
---|
743 |
|
---|
744 | return opt_order;
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
|
---|
749 | {
|
---|
750 | assert(n > 0);
|
---|
751 | memcpy(res, smp, n * sizeof(int32_t));
|
---|
752 | }
|
---|
753 |
|
---|
754 | static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
|
---|
755 | int order)
|
---|
756 | {
|
---|
757 | int i;
|
---|
758 |
|
---|
759 | for(i=0; i<order; i++) {
|
---|
760 | res[i] = smp[i];
|
---|
761 | }
|
---|
762 |
|
---|
763 | if(order==0){
|
---|
764 | for(i=order; i<n; i++)
|
---|
765 | res[i]= smp[i];
|
---|
766 | }else if(order==1){
|
---|
767 | for(i=order; i<n; i++)
|
---|
768 | res[i]= smp[i] - smp[i-1];
|
---|
769 | }else if(order==2){
|
---|
770 | for(i=order; i<n; i++)
|
---|
771 | res[i]= smp[i] - 2*smp[i-1] + smp[i-2];
|
---|
772 | }else if(order==3){
|
---|
773 | for(i=order; i<n; i++)
|
---|
774 | res[i]= smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3];
|
---|
775 | }else{
|
---|
776 | for(i=order; i<n; i++)
|
---|
777 | res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4];
|
---|
778 | }
|
---|
779 | }
|
---|
780 |
|
---|
781 | static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
|
---|
782 | int order, const int32_t *coefs, int shift)
|
---|
783 | {
|
---|
784 | int i, j;
|
---|
785 | int32_t pred;
|
---|
786 |
|
---|
787 | for(i=0; i<order; i++) {
|
---|
788 | res[i] = smp[i];
|
---|
789 | }
|
---|
790 | for(i=order; i<n; i++) {
|
---|
791 | pred = 0;
|
---|
792 | for(j=0; j<order; j++) {
|
---|
793 | pred += coefs[j] * smp[i-j-1];
|
---|
794 | }
|
---|
795 | res[i] = smp[i] - (pred >> shift);
|
---|
796 | }
|
---|
797 | }
|
---|
798 |
|
---|
799 | static int encode_residual(FlacEncodeContext *ctx, int ch)
|
---|
800 | {
|
---|
801 | int i, n;
|
---|
802 | int min_order, max_order, opt_order, precision;
|
---|
803 | int min_porder, max_porder;
|
---|
804 | FlacFrame *frame;
|
---|
805 | FlacSubframe *sub;
|
---|
806 | int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
|
---|
807 | int shift[MAX_LPC_ORDER];
|
---|
808 | int32_t *res, *smp;
|
---|
809 |
|
---|
810 | frame = &ctx->frame;
|
---|
811 | sub = &frame->subframes[ch];
|
---|
812 | res = sub->residual;
|
---|
813 | smp = sub->samples;
|
---|
814 | n = frame->blocksize;
|
---|
815 |
|
---|
816 | /* CONSTANT */
|
---|
817 | for(i=1; i<n; i++) {
|
---|
818 | if(smp[i] != smp[0]) break;
|
---|
819 | }
|
---|
820 | if(i == n) {
|
---|
821 | sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
|
---|
822 | res[0] = smp[0];
|
---|
823 | return sub->obits;
|
---|
824 | }
|
---|
825 |
|
---|
826 | /* VERBATIM */
|
---|
827 | if(n < 5) {
|
---|
828 | sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
|
---|
829 | encode_residual_verbatim(res, smp, n);
|
---|
830 | return sub->obits * n;
|
---|
831 | }
|
---|
832 |
|
---|
833 | min_order = ctx->options.min_prediction_order;
|
---|
834 | max_order = ctx->options.max_prediction_order;
|
---|
835 | min_porder = ctx->options.min_partition_order;
|
---|
836 | max_porder = ctx->options.max_partition_order;
|
---|
837 | precision = ctx->options.lpc_coeff_precision;
|
---|
838 |
|
---|
839 | /* FIXED */
|
---|
840 | if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {
|
---|
841 | uint32_t bits[MAX_FIXED_ORDER+1];
|
---|
842 | if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;
|
---|
843 | opt_order = 0;
|
---|
844 | bits[0] = UINT32_MAX;
|
---|
845 | for(i=min_order; i<=max_order; i++) {
|
---|
846 | encode_residual_fixed(res, smp, n, i);
|
---|
847 | bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
|
---|
848 | n, i, sub->obits);
|
---|
849 | if(bits[i] < bits[opt_order]) {
|
---|
850 | opt_order = i;
|
---|
851 | }
|
---|
852 | }
|
---|
853 | sub->order = opt_order;
|
---|
854 | sub->type = FLAC_SUBFRAME_FIXED;
|
---|
855 | sub->type_code = sub->type | sub->order;
|
---|
856 | if(sub->order != max_order) {
|
---|
857 | encode_residual_fixed(res, smp, n, sub->order);
|
---|
858 | return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
|
---|
859 | sub->order, sub->obits);
|
---|
860 | }
|
---|
861 | return bits[sub->order];
|
---|
862 | }
|
---|
863 |
|
---|
864 | /* LPC */
|
---|
865 | sub->order = lpc_calc_coefs(smp, n, max_order, precision, coefs, shift);
|
---|
866 | sub->type = FLAC_SUBFRAME_LPC;
|
---|
867 | sub->type_code = sub->type | (sub->order-1);
|
---|
868 | sub->shift = shift[sub->order-1];
|
---|
869 | for(i=0; i<sub->order; i++) {
|
---|
870 | sub->coefs[i] = coefs[sub->order-1][i];
|
---|
871 | }
|
---|
872 | encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
|
---|
873 | return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n, sub->order,
|
---|
874 | sub->obits, precision);
|
---|
875 | }
|
---|
876 |
|
---|
877 | static int encode_residual_v(FlacEncodeContext *ctx, int ch)
|
---|
878 | {
|
---|
879 | int i, n;
|
---|
880 | FlacFrame *frame;
|
---|
881 | FlacSubframe *sub;
|
---|
882 | int32_t *res, *smp;
|
---|
883 |
|
---|
884 | frame = &ctx->frame;
|
---|
885 | sub = &frame->subframes[ch];
|
---|
886 | res = sub->residual;
|
---|
887 | smp = sub->samples;
|
---|
888 | n = frame->blocksize;
|
---|
889 |
|
---|
890 | /* CONSTANT */
|
---|
891 | for(i=1; i<n; i++) {
|
---|
892 | if(smp[i] != smp[0]) break;
|
---|
893 | }
|
---|
894 | if(i == n) {
|
---|
895 | sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
|
---|
896 | res[0] = smp[0];
|
---|
897 | return sub->obits;
|
---|
898 | }
|
---|
899 |
|
---|
900 | /* VERBATIM */
|
---|
901 | sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
|
---|
902 | encode_residual_verbatim(res, smp, n);
|
---|
903 | return sub->obits * n;
|
---|
904 | }
|
---|
905 |
|
---|
906 | static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
|
---|
907 | {
|
---|
908 | int i, best;
|
---|
909 | int32_t lt, rt;
|
---|
910 | uint64_t sum[4];
|
---|
911 | uint64_t score[4];
|
---|
912 | int k;
|
---|
913 |
|
---|
914 | /* calculate sum of 2nd order residual for each channel */
|
---|
915 | sum[0] = sum[1] = sum[2] = sum[3] = 0;
|
---|
916 | for(i=2; i<n; i++) {
|
---|
917 | lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
|
---|
918 | rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
|
---|
919 | sum[2] += ABS((lt + rt) >> 1);
|
---|
920 | sum[3] += ABS(lt - rt);
|
---|
921 | sum[0] += ABS(lt);
|
---|
922 | sum[1] += ABS(rt);
|
---|
923 | }
|
---|
924 | /* estimate bit counts */
|
---|
925 | for(i=0; i<4; i++) {
|
---|
926 | k = find_optimal_param(2*sum[i], n);
|
---|
927 | sum[i] = rice_encode_count(2*sum[i], n, k);
|
---|
928 | }
|
---|
929 |
|
---|
930 | /* calculate score for each mode */
|
---|
931 | score[0] = sum[0] + sum[1];
|
---|
932 | score[1] = sum[0] + sum[3];
|
---|
933 | score[2] = sum[1] + sum[3];
|
---|
934 | score[3] = sum[2] + sum[3];
|
---|
935 |
|
---|
936 | /* return mode with lowest score */
|
---|
937 | best = 0;
|
---|
938 | for(i=1; i<4; i++) {
|
---|
939 | if(score[i] < score[best]) {
|
---|
940 | best = i;
|
---|
941 | }
|
---|
942 | }
|
---|
943 | if(best == 0) {
|
---|
944 | return FLAC_CHMODE_LEFT_RIGHT;
|
---|
945 | } else if(best == 1) {
|
---|
946 | return FLAC_CHMODE_LEFT_SIDE;
|
---|
947 | } else if(best == 2) {
|
---|
948 | return FLAC_CHMODE_RIGHT_SIDE;
|
---|
949 | } else {
|
---|
950 | return FLAC_CHMODE_MID_SIDE;
|
---|
951 | }
|
---|
952 | }
|
---|
953 |
|
---|
954 | /**
|
---|
955 | * Perform stereo channel decorrelation
|
---|
956 | */
|
---|
957 | static void channel_decorrelation(FlacEncodeContext *ctx)
|
---|
958 | {
|
---|
959 | FlacFrame *frame;
|
---|
960 | int32_t *left, *right;
|
---|
961 | int i, n;
|
---|
962 |
|
---|
963 | frame = &ctx->frame;
|
---|
964 | n = frame->blocksize;
|
---|
965 | left = frame->subframes[0].samples;
|
---|
966 | right = frame->subframes[1].samples;
|
---|
967 |
|
---|
968 | if(ctx->channels != 2) {
|
---|
969 | frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
|
---|
970 | return;
|
---|
971 | }
|
---|
972 |
|
---|
973 | frame->ch_mode = estimate_stereo_mode(left, right, n);
|
---|
974 |
|
---|
975 | /* perform decorrelation and adjust bits-per-sample */
|
---|
976 | if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
|
---|
977 | return;
|
---|
978 | }
|
---|
979 | if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
|
---|
980 | int32_t tmp;
|
---|
981 | for(i=0; i<n; i++) {
|
---|
982 | tmp = left[i];
|
---|
983 | left[i] = (tmp + right[i]) >> 1;
|
---|
984 | right[i] = tmp - right[i];
|
---|
985 | }
|
---|
986 | frame->subframes[1].obits++;
|
---|
987 | } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
|
---|
988 | for(i=0; i<n; i++) {
|
---|
989 | right[i] = left[i] - right[i];
|
---|
990 | }
|
---|
991 | frame->subframes[1].obits++;
|
---|
992 | } else {
|
---|
993 | for(i=0; i<n; i++) {
|
---|
994 | left[i] -= right[i];
|
---|
995 | }
|
---|
996 | frame->subframes[0].obits++;
|
---|
997 | }
|
---|
998 | }
|
---|
999 |
|
---|
1000 | static void put_sbits(PutBitContext *pb, int bits, int32_t val)
|
---|
1001 | {
|
---|
1002 | assert(bits >= 0 && bits <= 31);
|
---|
1003 |
|
---|
1004 | put_bits(pb, bits, val & ((1<<bits)-1));
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | static void write_utf8(PutBitContext *pb, uint32_t val)
|
---|
1008 | {
|
---|
1009 | int bytes, shift;
|
---|
1010 |
|
---|
1011 | if(val < 0x80){
|
---|
1012 | put_bits(pb, 8, val);
|
---|
1013 | return;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | bytes= (av_log2(val)+4) / 5;
|
---|
1017 | shift = (bytes - 1) * 6;
|
---|
1018 | put_bits(pb, 8, (256 - (256>>bytes)) | (val >> shift));
|
---|
1019 | while(shift >= 6){
|
---|
1020 | shift -= 6;
|
---|
1021 | put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | static void output_frame_header(FlacEncodeContext *s)
|
---|
1026 | {
|
---|
1027 | FlacFrame *frame;
|
---|
1028 | int crc;
|
---|
1029 |
|
---|
1030 | frame = &s->frame;
|
---|
1031 |
|
---|
1032 | put_bits(&s->pb, 16, 0xFFF8);
|
---|
1033 | put_bits(&s->pb, 4, frame->bs_code[0]);
|
---|
1034 | put_bits(&s->pb, 4, s->sr_code[0]);
|
---|
1035 | if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
|
---|
1036 | put_bits(&s->pb, 4, s->ch_code);
|
---|
1037 | } else {
|
---|
1038 | put_bits(&s->pb, 4, frame->ch_mode);
|
---|
1039 | }
|
---|
1040 | put_bits(&s->pb, 3, 4); /* bits-per-sample code */
|
---|
1041 | put_bits(&s->pb, 1, 0);
|
---|
1042 | write_utf8(&s->pb, s->frame_count);
|
---|
1043 | if(frame->bs_code[0] == 6) {
|
---|
1044 | put_bits(&s->pb, 8, frame->bs_code[1]);
|
---|
1045 | } else if(frame->bs_code[0] == 7) {
|
---|
1046 | put_bits(&s->pb, 16, frame->bs_code[1]);
|
---|
1047 | }
|
---|
1048 | if(s->sr_code[0] == 12) {
|
---|
1049 | put_bits(&s->pb, 8, s->sr_code[1]);
|
---|
1050 | } else if(s->sr_code[0] > 12) {
|
---|
1051 | put_bits(&s->pb, 16, s->sr_code[1]);
|
---|
1052 | }
|
---|
1053 | flush_put_bits(&s->pb);
|
---|
1054 | crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
|
---|
1055 | put_bits(&s->pb, 8, crc);
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | static void output_subframe_constant(FlacEncodeContext *s, int ch)
|
---|
1059 | {
|
---|
1060 | FlacSubframe *sub;
|
---|
1061 | int32_t res;
|
---|
1062 |
|
---|
1063 | sub = &s->frame.subframes[ch];
|
---|
1064 | res = sub->residual[0];
|
---|
1065 | put_sbits(&s->pb, sub->obits, res);
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
|
---|
1069 | {
|
---|
1070 | int i;
|
---|
1071 | FlacFrame *frame;
|
---|
1072 | FlacSubframe *sub;
|
---|
1073 | int32_t res;
|
---|
1074 |
|
---|
1075 | frame = &s->frame;
|
---|
1076 | sub = &frame->subframes[ch];
|
---|
1077 |
|
---|
1078 | for(i=0; i<frame->blocksize; i++) {
|
---|
1079 | res = sub->residual[i];
|
---|
1080 | put_sbits(&s->pb, sub->obits, res);
|
---|
1081 | }
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | static void output_residual(FlacEncodeContext *ctx, int ch)
|
---|
1085 | {
|
---|
1086 | int i, j, p, n, parts;
|
---|
1087 | int k, porder, psize, res_cnt;
|
---|
1088 | FlacFrame *frame;
|
---|
1089 | FlacSubframe *sub;
|
---|
1090 | int32_t *res;
|
---|
1091 |
|
---|
1092 | frame = &ctx->frame;
|
---|
1093 | sub = &frame->subframes[ch];
|
---|
1094 | res = sub->residual;
|
---|
1095 | n = frame->blocksize;
|
---|
1096 |
|
---|
1097 | /* rice-encoded block */
|
---|
1098 | put_bits(&ctx->pb, 2, 0);
|
---|
1099 |
|
---|
1100 | /* partition order */
|
---|
1101 | porder = sub->rc.porder;
|
---|
1102 | psize = n >> porder;
|
---|
1103 | parts = (1 << porder);
|
---|
1104 | put_bits(&ctx->pb, 4, porder);
|
---|
1105 | res_cnt = psize - sub->order;
|
---|
1106 |
|
---|
1107 | /* residual */
|
---|
1108 | j = sub->order;
|
---|
1109 | for(p=0; p<parts; p++) {
|
---|
1110 | k = sub->rc.params[p];
|
---|
1111 | put_bits(&ctx->pb, 4, k);
|
---|
1112 | if(p == 1) res_cnt = psize;
|
---|
1113 | for(i=0; i<res_cnt && j<n; i++, j++) {
|
---|
1114 | set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
|
---|
1120 | {
|
---|
1121 | int i;
|
---|
1122 | FlacFrame *frame;
|
---|
1123 | FlacSubframe *sub;
|
---|
1124 |
|
---|
1125 | frame = &ctx->frame;
|
---|
1126 | sub = &frame->subframes[ch];
|
---|
1127 |
|
---|
1128 | /* warm-up samples */
|
---|
1129 | for(i=0; i<sub->order; i++) {
|
---|
1130 | put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | /* residual */
|
---|
1134 | output_residual(ctx, ch);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
|
---|
1138 | {
|
---|
1139 | int i, cbits;
|
---|
1140 | FlacFrame *frame;
|
---|
1141 | FlacSubframe *sub;
|
---|
1142 |
|
---|
1143 | frame = &ctx->frame;
|
---|
1144 | sub = &frame->subframes[ch];
|
---|
1145 |
|
---|
1146 | /* warm-up samples */
|
---|
1147 | for(i=0; i<sub->order; i++) {
|
---|
1148 | put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | /* LPC coefficients */
|
---|
1152 | cbits = ctx->options.lpc_coeff_precision;
|
---|
1153 | put_bits(&ctx->pb, 4, cbits-1);
|
---|
1154 | put_sbits(&ctx->pb, 5, sub->shift);
|
---|
1155 | for(i=0; i<sub->order; i++) {
|
---|
1156 | put_sbits(&ctx->pb, cbits, sub->coefs[i]);
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | /* residual */
|
---|
1160 | output_residual(ctx, ch);
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | static void output_subframes(FlacEncodeContext *s)
|
---|
1164 | {
|
---|
1165 | FlacFrame *frame;
|
---|
1166 | FlacSubframe *sub;
|
---|
1167 | int ch;
|
---|
1168 |
|
---|
1169 | frame = &s->frame;
|
---|
1170 |
|
---|
1171 | for(ch=0; ch<s->channels; ch++) {
|
---|
1172 | sub = &frame->subframes[ch];
|
---|
1173 |
|
---|
1174 | /* subframe header */
|
---|
1175 | put_bits(&s->pb, 1, 0);
|
---|
1176 | put_bits(&s->pb, 6, sub->type_code);
|
---|
1177 | put_bits(&s->pb, 1, 0); /* no wasted bits */
|
---|
1178 |
|
---|
1179 | /* subframe */
|
---|
1180 | if(sub->type == FLAC_SUBFRAME_CONSTANT) {
|
---|
1181 | output_subframe_constant(s, ch);
|
---|
1182 | } else if(sub->type == FLAC_SUBFRAME_VERBATIM) {
|
---|
1183 | output_subframe_verbatim(s, ch);
|
---|
1184 | } else if(sub->type == FLAC_SUBFRAME_FIXED) {
|
---|
1185 | output_subframe_fixed(s, ch);
|
---|
1186 | } else if(sub->type == FLAC_SUBFRAME_LPC) {
|
---|
1187 | output_subframe_lpc(s, ch);
|
---|
1188 | }
|
---|
1189 | }
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | static void output_frame_footer(FlacEncodeContext *s)
|
---|
1193 | {
|
---|
1194 | int crc;
|
---|
1195 | flush_put_bits(&s->pb);
|
---|
1196 | crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
|
---|
1197 | put_bits(&s->pb, 16, crc);
|
---|
1198 | flush_put_bits(&s->pb);
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
|
---|
1202 | int buf_size, void *data)
|
---|
1203 | {
|
---|
1204 | int ch;
|
---|
1205 | FlacEncodeContext *s;
|
---|
1206 | int16_t *samples = data;
|
---|
1207 | int out_bytes;
|
---|
1208 |
|
---|
1209 | s = avctx->priv_data;
|
---|
1210 |
|
---|
1211 | s->blocksize = avctx->frame_size;
|
---|
1212 | init_frame(s);
|
---|
1213 |
|
---|
1214 | copy_samples(s, samples);
|
---|
1215 |
|
---|
1216 | channel_decorrelation(s);
|
---|
1217 |
|
---|
1218 | for(ch=0; ch<s->channels; ch++) {
|
---|
1219 | encode_residual(s, ch);
|
---|
1220 | }
|
---|
1221 | init_put_bits(&s->pb, frame, buf_size);
|
---|
1222 | output_frame_header(s);
|
---|
1223 | output_subframes(s);
|
---|
1224 | output_frame_footer(s);
|
---|
1225 | out_bytes = put_bits_count(&s->pb) >> 3;
|
---|
1226 |
|
---|
1227 | if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
|
---|
1228 | /* frame too large. use verbatim mode */
|
---|
1229 | for(ch=0; ch<s->channels; ch++) {
|
---|
1230 | encode_residual_v(s, ch);
|
---|
1231 | }
|
---|
1232 | init_put_bits(&s->pb, frame, buf_size);
|
---|
1233 | output_frame_header(s);
|
---|
1234 | output_subframes(s);
|
---|
1235 | output_frame_footer(s);
|
---|
1236 | out_bytes = put_bits_count(&s->pb) >> 3;
|
---|
1237 |
|
---|
1238 | if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
|
---|
1239 | /* still too large. must be an error. */
|
---|
1240 | av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
|
---|
1241 | return -1;
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | s->frame_count++;
|
---|
1246 | return out_bytes;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | static int flac_encode_close(AVCodecContext *avctx)
|
---|
1250 | {
|
---|
1251 | av_freep(&avctx->extradata);
|
---|
1252 | avctx->extradata_size = 0;
|
---|
1253 | av_freep(&avctx->coded_frame);
|
---|
1254 | return 0;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | AVCodec flac_encoder = {
|
---|
1258 | "flac",
|
---|
1259 | CODEC_TYPE_AUDIO,
|
---|
1260 | CODEC_ID_FLAC,
|
---|
1261 | sizeof(FlacEncodeContext),
|
---|
1262 | flac_encode_init,
|
---|
1263 | flac_encode_frame,
|
---|
1264 | flac_encode_close,
|
---|
1265 | NULL,
|
---|
1266 | .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
|
---|
1267 | };
|
---|