1 /*
2 * ALAC audio encoder
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
28
29 #define DEFAULT_FRAME_SIZE 4096
30 #define ALAC_EXTRADATA_SIZE 36
31 #define ALAC_FRAME_HEADER_SIZE 55
32 #define ALAC_FRAME_FOOTER_SIZE 3
33
34 #define ALAC_ESCAPE_CODE 0x1FF
35 #define ALAC_MAX_LPC_ORDER 30
36 #define DEFAULT_MAX_PRED_ORDER 6
37 #define DEFAULT_MIN_PRED_ORDER 4
38 #define ALAC_MAX_LPC_PRECISION 9
39 #define ALAC_MAX_LPC_SHIFT 9
40
41 #define ALAC_CHMODE_LEFT_RIGHT 0
42 #define ALAC_CHMODE_LEFT_SIDE 1
43 #define ALAC_CHMODE_RIGHT_SIDE 2
44 #define ALAC_CHMODE_MID_SIDE 3
45
52
58
61 int verbatim;
/**< current frame verbatim mode flag */
78
79
82 {
83 int ch, i;
86
87 #define COPY_SAMPLES(type) do { \
88 for (ch = 0; ch < channels; ch++) { \
89 int32_t *bptr = s->sample_buf[ch]; \
90 const type *sptr = (const type *)samples[ch]; \
91 for (i = 0; i < s->frame_size; i++) \
92 bptr[i] = sptr[i] >> shift; \
93 } \
94 } while (0)
95
98 else
100 }
101
103 int k, int write_sample_size)
104 {
106
108 divisor = (1<<k) - 1;
109 q = x / divisor;
110 r = x % divisor;
111
112 if (q > 8) {
113 // write escape code and sample value directly
116 } else {
117 if (q)
120
121 if (k != 1) {
122 if (r > 0)
124 else
126 }
127 }
128 }
129
132 int instance)
133 {
134 int encode_fs = 0;
135
137 encode_fs = 1;
138
142 put_bits(&s->
pbctx, 1, encode_fs);
// Sample count is in the header
145 if (encode_fs)
147 }
148
150 {
153 int opt_order;
154
164 } else {
172
175 memcpy(s->
lpc[ch].
lpc_coeff, coefs[opt_order-1], opt_order*
sizeof(
int));
176 }
177 }
178
180 {
181 int i, best;
183 uint64_t sum[4];
184 uint64_t score[4];
185
186 /* calculate sum of 2nd order residual for each channel */
187 sum[0] = sum[1] = sum[2] = sum[3] = 0;
188 for (i = 2; i <
n; i++) {
189 lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
190 rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
191 sum[2] +=
FFABS((lt + rt) >> 1);
192 sum[3] +=
FFABS(lt - rt);
195 }
196
197 /* calculate score for each mode */
198 score[0] = sum[0] + sum[1];
199 score[1] = sum[0] + sum[3];
200 score[2] = sum[1] + sum[3];
201 score[3] = sum[2] + sum[3];
202
203 /* return mode with lowest score */
204 best = 0;
205 for (i = 1; i < 4; i++) {
206 if (score[i] < score[best])
207 best = i;
208 }
209 return best;
210 }
211
213 {
217
219
220 switch (mode) {
224 break;
226 for (i = 0; i <
n; i++)
227 right[i] = left[i] - right[i];
230 break;
232 for (i = 0; i <
n; i++) {
233 tmp = right[i];
234 right[i] = left[i] - right[i];
235 left[i] = tmp + (right[i] >> 31);
236 }
239 break;
240 default:
241 for (i = 0; i <
n; i++) {
242 tmp = left[i];
243 left[i] = (tmp + right[i]) >> 1;
244 right[i] = tmp - right[i];
245 }
248 break;
249 }
250 }
251
253 {
254 int i;
256
259
263 }
264
265 return;
266 }
267
268 // generalised linear predictor
269
273
274 // generate warm-up samples
275 residual[0] = samples[0];
278
279 // perform lpc on remaining samples
281 int sum = 1 << (lpc.
lpc_quant - 1), res_val, j;
282
284 sum += (samples[lpc.
lpc_order-j] - samples[0]) *
286 }
287
289 sum += samples[0];
292 res_val = residual[i];
293
294 if (res_val) {
296 int neg = (res_val < 0);
297
298 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
300 int sign = (val ?
FFSIGN(val) : 0);
301
302 if (neg)
303 sign *= -1;
304
306 val *= sign;
308 index--;
309 }
310 }
311 samples++;
312 }
313 }
314 }
315
317 {
319 int sign_modifier = 0, i, k;
321
323 int x;
324
325 k =
av_log2((history >> 9) + 3);
326
327 x = -2 * (*samples) -1;
328 x ^= x >> 31;
329
330 samples++;
331 i++;
332
334
337
338 sign_modifier = 0;
339 if (x > 0xFFFF)
340 history = 0xFFFF;
341
343 unsigned int block_size = 0;
344
345 k = 7 -
av_log2(history) + ((history + 16) >> 6);
346
347 while (*samples == 0 && i < s->frame_size) {
348 samples++;
349 i++;
350 block_size++;
351 }
353 sign_modifier = (block_size <= 0xFFFF);
354 history = 0;
355 }
356
357 }
358 }
359
363 {
364 uint8_t const *samples[2] = { samples0, samples1 };
365 int i, j, channels;
366 int prediction_type = 0;
368
369 channels = element ==
TYPE_CPE ? 2 : 1;
370
373 /* samples are channel-interleaved in verbatim mode */
379 for (j = 0; j < channels; j++)
381 samples_s32[j][i] >> shift);
382 } else {
383 int16_t const *samples_s16[2] = { (const int16_t *)samples0,
384 (const int16_t *)samples1 };
386 for (j = 0; j < channels; j++)
388 samples_s16[j][i]);
389 }
390 } else {
392 channels - 1;
393
396
397 if (channels == 2)
399 else
403
404 for (i = 0; i < channels; i++) {
406
409
412 // predictor coeff. table
415 }
416
417 // write extra bits if needed
421 for (j = 0; j < channels; j++) {
424 }
425 }
426 }
427
428 // apply lpc and entropy coding to audio samples
429 for (i = 0; i < channels; i++) {
431
432 // TODO: determine when this will actually help. for now it's not used.
433 if (prediction_type == 15) {
434 // 2nd pass 1st order filter
437 }
439 }
440 }
441 }
442
445 {
449 int ch, element, sce, cpe;
450
452
453 ch = element = sce = cpe = 0;
454 while (ch < s->avctx->channels) {
455 if (ch_elements[element] ==
TYPE_CPE) {
457 samples[ch_map[ch + 1]]);
458 cpe++;
459 ch += 2;
460 } else {
462 sce++;
463 ch++;
464 }
465 element++;
466 }
467
470
472 }
473
475 {
477 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
478 }
479
481 {
486 return 0;
487 }
488
490 {
494
496
501 } else {
504 }
505
506 // Set default compression level
509 else
511
512 // Initialize default Rice parameters
517
521
525 goto error;
526 }
528
539
540 // Set relevant extradata fields
545 }
546
554 goto error;
555 }
556
558 }
559
567 goto error;
568 }
569
571 }
572
575 "invalid prediction orders: min=%d max=%d\n",
578 goto error;
579 }
580
582
586 goto error;
587 }
588
589 return 0;
590 error:
593 }
594
597 {
599 int out_bytes, max_frame_size,
ret;
600
602
606 else
608
611
612 /* use verbatim mode for compression_level 0 */
616 } else {
619 }
620
622
623 if (out_bytes > max_frame_size) {
624 /* frame too large. use verbatim mode */
628 }
629
630 avpkt->
size = out_bytes;
631 *got_packet_ptr = 1;
632 return 0;
633 }
634
649 };