Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
22 | #include "libavutil/mem.h" | ||
23 | #include "libavutil/opt.h" | ||
24 | |||
25 | #include "avcodec.h" | ||
26 | #include "codec_internal.h" | ||
27 | #include "encode.h" | ||
28 | #include "put_bits.h" | ||
29 | #include "lpc.h" | ||
30 | #include "mathops.h" | ||
31 | #include "alac_data.h" | ||
32 | |||
33 | #define DEFAULT_FRAME_SIZE 4096 | ||
34 | #define ALAC_EXTRADATA_SIZE 36 | ||
35 | #define ALAC_FRAME_HEADER_SIZE 55 | ||
36 | #define ALAC_FRAME_FOOTER_SIZE 3 | ||
37 | |||
38 | #define ALAC_ESCAPE_CODE 0x1FF | ||
39 | #define ALAC_MAX_LPC_ORDER 30 | ||
40 | #define DEFAULT_MAX_PRED_ORDER 6 | ||
41 | #define DEFAULT_MIN_PRED_ORDER 4 | ||
42 | #define ALAC_MAX_LPC_PRECISION 9 | ||
43 | #define ALAC_MIN_LPC_SHIFT 0 | ||
44 | #define ALAC_MAX_LPC_SHIFT 9 | ||
45 | |||
46 | #define ALAC_CHMODE_LEFT_RIGHT 0 | ||
47 | #define ALAC_CHMODE_LEFT_SIDE 1 | ||
48 | #define ALAC_CHMODE_RIGHT_SIDE 2 | ||
49 | #define ALAC_CHMODE_MID_SIDE 3 | ||
50 | |||
51 | typedef struct RiceContext { | ||
52 | int history_mult; | ||
53 | int initial_history; | ||
54 | int k_modifier; | ||
55 | int rice_modifier; | ||
56 | } RiceContext; | ||
57 | |||
58 | typedef struct AlacLPCContext { | ||
59 | int lpc_order; | ||
60 | int lpc_coeff[ALAC_MAX_LPC_ORDER+1]; | ||
61 | int lpc_quant; | ||
62 | } AlacLPCContext; | ||
63 | |||
64 | typedef struct AlacEncodeContext { | ||
65 | const AVClass *class; | ||
66 | AVCodecContext *avctx; | ||
67 | int frame_size; /**< current frame size */ | ||
68 | int verbatim; /**< current frame verbatim mode flag */ | ||
69 | int compression_level; | ||
70 | int min_prediction_order; | ||
71 | int max_prediction_order; | ||
72 | int max_coded_frame_size; | ||
73 | int write_sample_size; | ||
74 | int extra_bits; | ||
75 | int32_t sample_buf[2][DEFAULT_FRAME_SIZE]; | ||
76 | int32_t predictor_buf[2][DEFAULT_FRAME_SIZE]; | ||
77 | int interlacing_shift; | ||
78 | int interlacing_leftweight; | ||
79 | PutBitContext pbctx; | ||
80 | RiceContext rc; | ||
81 | AlacLPCContext lpc[2]; | ||
82 | LPCContext lpc_ctx; | ||
83 | } AlacEncodeContext; | ||
84 | |||
85 | |||
86 | 1781 | static void init_sample_buffers(AlacEncodeContext *s, int channels, | |
87 | const uint8_t *samples[2]) | ||
88 | { | ||
89 | int ch, i; | ||
90 | 1781 | int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 - | |
91 | 1781 | s->avctx->bits_per_raw_sample; | |
92 | |||
93 | #define COPY_SAMPLES(type) do { \ | ||
94 | for (ch = 0; ch < channels; ch++) { \ | ||
95 | int32_t *bptr = s->sample_buf[ch]; \ | ||
96 | const type *sptr = (const type *)samples[ch]; \ | ||
97 | for (i = 0; i < s->frame_size; i++) \ | ||
98 | bptr[i] = sptr[i] >> shift; \ | ||
99 | } \ | ||
100 | } while (0) | ||
101 | |||
102 |
2/2✓ Branch 0 taken 1407 times.
✓ Branch 1 taken 374 times.
|
1781 | if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) |
103 |
4/4✓ Branch 0 taken 11520000 times.
✓ Branch 1 taken 2814 times.
✓ Branch 2 taken 2814 times.
✓ Branch 3 taken 1407 times.
|
11524221 | COPY_SAMPLES(int32_t); |
104 | else | ||
105 |
4/4✓ Branch 0 taken 3042900 times.
✓ Branch 1 taken 748 times.
✓ Branch 2 taken 748 times.
✓ Branch 3 taken 374 times.
|
3044022 | COPY_SAMPLES(int16_t); |
106 | 1781 | } | |
107 | |||
108 | 14430194 | static void encode_scalar(AlacEncodeContext *s, int x, | |
109 | int k, int write_sample_size) | ||
110 | { | ||
111 | int divisor, q, r; | ||
112 | |||
113 | 14430194 | k = FFMIN(k, s->rc.k_modifier); | |
114 | 14430194 | divisor = (1<<k) - 1; | |
115 | 14430194 | q = x / divisor; | |
116 | 14430194 | r = x % divisor; | |
117 | |||
118 |
2/2✓ Branch 0 taken 25847 times.
✓ Branch 1 taken 14404347 times.
|
14430194 | if (q > 8) { |
119 | // write escape code and sample value directly | ||
120 | 25847 | put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE); | |
121 | 25847 | put_bits(&s->pbctx, write_sample_size, x); | |
122 | } else { | ||
123 |
2/2✓ Branch 0 taken 7865003 times.
✓ Branch 1 taken 6539344 times.
|
14404347 | if (q) |
124 | 7865003 | put_bits(&s->pbctx, q, (1<<q) - 1); | |
125 | 14404347 | put_bits(&s->pbctx, 1, 0); | |
126 | |||
127 |
2/2✓ Branch 0 taken 14401767 times.
✓ Branch 1 taken 2580 times.
|
14404347 | if (k != 1) { |
128 |
2/2✓ Branch 0 taken 12874329 times.
✓ Branch 1 taken 1527438 times.
|
14401767 | if (r > 0) |
129 | 12874329 | put_bits(&s->pbctx, k, r+1); | |
130 | else | ||
131 | 1527438 | put_bits(&s->pbctx, k-1, 0); | |
132 | } | ||
133 | } | ||
134 | 14430194 | } | |
135 | |||
136 | 2353 | static void write_element_header(AlacEncodeContext *s, | |
137 | enum AlacRawDataBlockType element, | ||
138 | int instance) | ||
139 | { | ||
140 | 2353 | int encode_fs = 0; | |
141 | |||
142 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
|
2353 | if (s->frame_size < DEFAULT_FRAME_SIZE) |
143 | 9 | encode_fs = 1; | |
144 | |||
145 | 2353 | put_bits(&s->pbctx, 3, element); // element type | |
146 | 2353 | put_bits(&s->pbctx, 4, instance); // element instance | |
147 | 2353 | put_bits(&s->pbctx, 12, 0); // unused header bits | |
148 | 2353 | put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header | |
149 | 2353 | put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit) | |
150 | 2353 | put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim | |
151 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
|
2353 | if (encode_fs) |
152 | 9 | put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame | |
153 | 2353 | } | |
154 | |||
155 | 3562 | static void calc_predictor_params(AlacEncodeContext *s, int ch) | |
156 | { | ||
157 | int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; | ||
158 | int shift[MAX_LPC_ORDER]; | ||
159 | int opt_order; | ||
160 | |||
161 |
2/2✓ Branch 0 taken 1274 times.
✓ Branch 1 taken 2288 times.
|
3562 | if (s->compression_level == 1) { |
162 | 1274 | s->lpc[ch].lpc_order = 6; | |
163 | 1274 | s->lpc[ch].lpc_quant = 6; | |
164 | 1274 | s->lpc[ch].lpc_coeff[0] = 160; | |
165 | 1274 | s->lpc[ch].lpc_coeff[1] = -190; | |
166 | 1274 | s->lpc[ch].lpc_coeff[2] = 170; | |
167 | 1274 | s->lpc[ch].lpc_coeff[3] = -130; | |
168 | 1274 | s->lpc[ch].lpc_coeff[4] = 80; | |
169 | 1274 | s->lpc[ch].lpc_coeff[5] = -25; | |
170 | } else { | ||
171 | 2288 | opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch], | |
172 | s->frame_size, | ||
173 | s->min_prediction_order, | ||
174 | s->max_prediction_order, | ||
175 | ALAC_MAX_LPC_PRECISION, coefs, shift, | ||
176 | FF_LPC_TYPE_LEVINSON, 0, | ||
177 | ORDER_METHOD_EST, ALAC_MIN_LPC_SHIFT, | ||
178 | ALAC_MAX_LPC_SHIFT, 1); | ||
179 | |||
180 | 2288 | s->lpc[ch].lpc_order = opt_order; | |
181 | 2288 | s->lpc[ch].lpc_quant = shift[opt_order-1]; | |
182 | 2288 | memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int)); | |
183 | } | ||
184 | 3562 | } | |
185 | |||
186 | 1781 | static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n) | |
187 | { | ||
188 | int i, best; | ||
189 | int32_t lt, rt; | ||
190 | uint64_t sum[4]; | ||
191 | uint64_t score[4]; | ||
192 | |||
193 | /* calculate sum of 2nd order residual for each channel */ | ||
194 | 1781 | sum[0] = sum[1] = sum[2] = sum[3] = 0; | |
195 |
2/2✓ Branch 0 taken 7277888 times.
✓ Branch 1 taken 1781 times.
|
7279669 | for (i = 2; i < n; i++) { |
196 | 7277888 | lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2]; | |
197 | 7277888 | rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2]; | |
198 |
2/2✓ Branch 0 taken 3674830 times.
✓ Branch 1 taken 3603058 times.
|
7277888 | sum[2] += FFABS((lt + rt) >> 1); |
199 |
2/2✓ Branch 0 taken 3737436 times.
✓ Branch 1 taken 3540452 times.
|
7277888 | sum[3] += FFABS(lt - rt); |
200 |
2/2✓ Branch 0 taken 3698084 times.
✓ Branch 1 taken 3579804 times.
|
7277888 | sum[0] += FFABS(lt); |
201 |
2/2✓ Branch 0 taken 3681625 times.
✓ Branch 1 taken 3596263 times.
|
7277888 | sum[1] += FFABS(rt); |
202 | } | ||
203 | |||
204 | /* calculate score for each mode */ | ||
205 | 1781 | score[0] = sum[0] + sum[1]; | |
206 | 1781 | score[1] = sum[0] + sum[3]; | |
207 | 1781 | score[2] = sum[1] + sum[3]; | |
208 | 1781 | score[3] = sum[2] + sum[3]; | |
209 | |||
210 | /* return mode with lowest score */ | ||
211 | 1781 | best = 0; | |
212 |
2/2✓ Branch 0 taken 5343 times.
✓ Branch 1 taken 1781 times.
|
7124 | for (i = 1; i < 4; i++) { |
213 |
2/2✓ Branch 0 taken 597 times.
✓ Branch 1 taken 4746 times.
|
5343 | if (score[i] < score[best]) |
214 | 597 | best = i; | |
215 | } | ||
216 | 1781 | return best; | |
217 | } | ||
218 | |||
219 | 1781 | static void alac_stereo_decorrelation(AlacEncodeContext *s) | |
220 | { | ||
221 | 1781 | int32_t *left = s->sample_buf[0], *right = s->sample_buf[1]; | |
222 | 1781 | int i, mode, n = s->frame_size; | |
223 | int32_t tmp; | ||
224 | |||
225 | 1781 | mode = estimate_stereo_mode(left, right, n); | |
226 | |||
227 |
4/4✓ Branch 0 taken 1425 times.
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 121 times.
|
1781 | switch (mode) { |
228 | 1425 | case ALAC_CHMODE_LEFT_RIGHT: | |
229 | 1425 | s->interlacing_leftweight = 0; | |
230 | 1425 | s->interlacing_shift = 0; | |
231 | 1425 | break; | |
232 | 145 | case ALAC_CHMODE_LEFT_SIDE: | |
233 |
2/2✓ Branch 0 taken 592280 times.
✓ Branch 1 taken 145 times.
|
592425 | for (i = 0; i < n; i++) |
234 | 592280 | right[i] = left[i] - right[i]; | |
235 | 145 | s->interlacing_leftweight = 1; | |
236 | 145 | s->interlacing_shift = 0; | |
237 | 145 | break; | |
238 | 90 | case ALAC_CHMODE_RIGHT_SIDE: | |
239 |
2/2✓ Branch 0 taken 368640 times.
✓ Branch 1 taken 90 times.
|
368730 | for (i = 0; i < n; i++) { |
240 | 368640 | tmp = right[i]; | |
241 | 368640 | right[i] = left[i] - right[i]; | |
242 | 368640 | left[i] = tmp + (right[i] >> 31); | |
243 | } | ||
244 | 90 | s->interlacing_leftweight = 1; | |
245 | 90 | s->interlacing_shift = 31; | |
246 | 90 | break; | |
247 | 121 | default: | |
248 |
2/2✓ Branch 0 taken 486802 times.
✓ Branch 1 taken 121 times.
|
486923 | for (i = 0; i < n; i++) { |
249 | 486802 | tmp = left[i]; | |
250 | 486802 | left[i] = (tmp + right[i]) >> 1; | |
251 | 486802 | right[i] = tmp - right[i]; | |
252 | } | ||
253 | 121 | s->interlacing_leftweight = 1; | |
254 | 121 | s->interlacing_shift = 1; | |
255 | 121 | break; | |
256 | } | ||
257 | 1781 | } | |
258 | |||
259 | 3562 | static void alac_linear_predictor(AlacEncodeContext *s, int ch) | |
260 | { | ||
261 | int i; | ||
262 | 3562 | AlacLPCContext lpc = s->lpc[ch]; | |
263 | 3562 | int32_t *residual = s->predictor_buf[ch]; | |
264 | |||
265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3562 times.
|
3562 | if (lpc.lpc_order == 31) { |
266 | ✗ | residual[0] = s->sample_buf[ch][0]; | |
267 | |||
268 | ✗ | for (i = 1; i < s->frame_size; i++) { | |
269 | ✗ | residual[i] = s->sample_buf[ch][i ] - | |
270 | ✗ | s->sample_buf[ch][i - 1]; | |
271 | } | ||
272 | |||
273 | ✗ | return; | |
274 | } | ||
275 | |||
276 | // generalised linear predictor | ||
277 | |||
278 |
1/2✓ Branch 0 taken 3562 times.
✗ Branch 1 not taken.
|
3562 | if (lpc.lpc_order > 0) { |
279 | 3562 | int32_t *samples = s->sample_buf[ch]; | |
280 | |||
281 | // generate warm-up samples | ||
282 | 3562 | residual[0] = samples[0]; | |
283 |
2/2✓ Branch 0 taken 31197 times.
✓ Branch 1 taken 3562 times.
|
34759 | for (i = 1; i <= lpc.lpc_order; i++) |
284 | 31197 | residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size); | |
285 | |||
286 | // perform lpc on remaining samples | ||
287 |
2/2✓ Branch 0 taken 14528141 times.
✓ Branch 1 taken 3562 times.
|
14531703 | for (i = lpc.lpc_order + 1; i < s->frame_size; i++) { |
288 | 14528141 | int sum = 1 << (lpc.lpc_quant - 1), res_val, j; | |
289 | |||
290 |
2/2✓ Branch 0 taken 127115964 times.
✓ Branch 1 taken 14528141 times.
|
141644105 | for (j = 0; j < lpc.lpc_order; j++) { |
291 | 127115964 | sum += (samples[lpc.lpc_order-j] - samples[0]) * | |
292 | 127115964 | lpc.lpc_coeff[j]; | |
293 | } | ||
294 | |||
295 | 14528141 | sum >>= lpc.lpc_quant; | |
296 | 14528141 | sum += samples[0]; | |
297 | 14528141 | residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum, | |
298 | 14528141 | s->write_sample_size); | |
299 | 14528141 | res_val = residual[i]; | |
300 | |||
301 |
2/2✓ Branch 0 taken 13747876 times.
✓ Branch 1 taken 780265 times.
|
14528141 | if (res_val) { |
302 | 13747876 | int index = lpc.lpc_order - 1; | |
303 | 13747876 | int neg = (res_val < 0); | |
304 | |||
305 |
8/8✓ Branch 0 taken 69809376 times.
✓ Branch 1 taken 4402062 times.
✓ Branch 2 taken 34289798 times.
✓ Branch 3 taken 35519578 times.
✓ Branch 4 taken 28132311 times.
✓ Branch 5 taken 6157487 times.
✓ Branch 6 taken 32331251 times.
✓ Branch 7 taken 3188327 times.
|
74211438 | while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) { |
306 | 60463562 | int val = samples[0] - samples[lpc.lpc_order - index]; | |
307 |
4/4✓ Branch 0 taken 59881539 times.
✓ Branch 1 taken 582023 times.
✓ Branch 2 taken 29776850 times.
✓ Branch 3 taken 30104689 times.
|
60463562 | int sign = (val ? FFSIGN(val) : 0); |
308 | |||
309 |
2/2✓ Branch 0 taken 28132311 times.
✓ Branch 1 taken 32331251 times.
|
60463562 | if (neg) |
310 | 28132311 | sign *= -1; | |
311 | |||
312 | 60463562 | lpc.lpc_coeff[index] -= sign; | |
313 | 60463562 | val *= sign; | |
314 | 60463562 | res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index); | |
315 | 60463562 | index--; | |
316 | } | ||
317 | } | ||
318 | 14528141 | samples++; | |
319 | } | ||
320 | } | ||
321 | } | ||
322 | |||
323 | 3562 | static void alac_entropy_coder(AlacEncodeContext *s, int ch) | |
324 | { | ||
325 | 3562 | unsigned int history = s->rc.initial_history; | |
326 | 3562 | int sign_modifier = 0, i, k; | |
327 | 3562 | int32_t *samples = s->predictor_buf[ch]; | |
328 | |||
329 |
2/2✓ Branch 0 taken 14430063 times.
✓ Branch 1 taken 3562 times.
|
14433625 | for (i = 0; i < s->frame_size;) { |
330 | int x; | ||
331 | |||
332 | 14430063 | k = av_log2((history >> 9) + 3); | |
333 | |||
334 | 14430063 | x = -2 * (*samples) -1; | |
335 | 14430063 | x ^= x >> 31; | |
336 | |||
337 | 14430063 | samples++; | |
338 | 14430063 | i++; | |
339 | |||
340 | 14430063 | encode_scalar(s, x - sign_modifier, k, s->write_sample_size); | |
341 | |||
342 | 14430063 | history += x * s->rc.history_mult - | |
343 | 14430063 | ((history * s->rc.history_mult) >> 9); | |
344 | |||
345 | 14430063 | sign_modifier = 0; | |
346 |
2/2✓ Branch 0 taken 11363 times.
✓ Branch 1 taken 14418700 times.
|
14430063 | if (x > 0xFFFF) |
347 | 11363 | history = 0xFFFF; | |
348 | |||
349 |
3/4✓ Branch 0 taken 131 times.
✓ Branch 1 taken 14429932 times.
✓ Branch 2 taken 131 times.
✗ Branch 3 not taken.
|
14430063 | if (history < 128 && i < s->frame_size) { |
350 | 131 | unsigned int block_size = 0; | |
351 | |||
352 | 131 | k = 7 - av_log2(history) + ((history + 16) >> 6); | |
353 | |||
354 |
4/4✓ Branch 0 taken 132869 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 132837 times.
✓ Branch 3 taken 32 times.
|
132968 | while (*samples == 0 && i < s->frame_size) { |
355 | 132837 | samples++; | |
356 | 132837 | i++; | |
357 | 132837 | block_size++; | |
358 | } | ||
359 | 131 | encode_scalar(s, block_size, k, 16); | |
360 | 131 | sign_modifier = (block_size <= 0xFFFF); | |
361 | 131 | history = 0; | |
362 | } | ||
363 | |||
364 | } | ||
365 | 3562 | } | |
366 | |||
367 | 2353 | static void write_element(AlacEncodeContext *s, | |
368 | enum AlacRawDataBlockType element, int instance, | ||
369 | const uint8_t *samples0, const uint8_t *samples1) | ||
370 | { | ||
371 | 2353 | const uint8_t *samples[2] = { samples0, samples1 }; | |
372 | int i, j, channels; | ||
373 | 2353 | int prediction_type = 0; | |
374 | 2353 | PutBitContext *pb = &s->pbctx; | |
375 | |||
376 |
1/2✓ Branch 0 taken 2353 times.
✗ Branch 1 not taken.
|
2353 | channels = element == TYPE_CPE ? 2 : 1; |
377 | |||
378 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 1781 times.
|
2353 | if (s->verbatim) { |
379 | 572 | write_element_header(s, element, instance); | |
380 | /* samples are channel-interleaved in verbatim mode */ | ||
381 |
2/2✓ Branch 0 taken 469 times.
✓ Branch 1 taken 103 times.
|
572 | if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) { |
382 | 469 | int shift = 32 - s->avctx->bits_per_raw_sample; | |
383 | 469 | const int32_t *samples_s32[2] = { (const int32_t *)samples0, | |
384 | (const int32_t *)samples1 }; | ||
385 |
2/2✓ Branch 0 taken 1920000 times.
✓ Branch 1 taken 469 times.
|
1920469 | for (i = 0; i < s->frame_size; i++) |
386 |
2/2✓ Branch 0 taken 3840000 times.
✓ Branch 1 taken 1920000 times.
|
5760000 | for (j = 0; j < channels; j++) |
387 | 3840000 | put_sbits(pb, s->avctx->bits_per_raw_sample, | |
388 | 3840000 | samples_s32[j][i] >> shift); | |
389 | } else { | ||
390 | 103 | const int16_t *samples_s16[2] = { (const int16_t *)samples0, | |
391 | (const int16_t *)samples1 }; | ||
392 |
2/2✓ Branch 0 taken 418950 times.
✓ Branch 1 taken 103 times.
|
419053 | for (i = 0; i < s->frame_size; i++) |
393 |
2/2✓ Branch 0 taken 837900 times.
✓ Branch 1 taken 418950 times.
|
1256850 | for (j = 0; j < channels; j++) |
394 | 837900 | put_sbits(pb, s->avctx->bits_per_raw_sample, | |
395 | 837900 | samples_s16[j][i]); | |
396 | } | ||
397 | } else { | ||
398 | 1781 | s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits + | |
399 | 1781 | channels - 1; | |
400 | |||
401 | 1781 | init_sample_buffers(s, channels, samples); | |
402 | 1781 | write_element_header(s, element, instance); | |
403 | |||
404 | // extract extra bits if needed | ||
405 |
2/2✓ Branch 0 taken 1407 times.
✓ Branch 1 taken 374 times.
|
1781 | if (s->extra_bits) { |
406 | 1407 | uint32_t mask = (1 << s->extra_bits) - 1; | |
407 |
2/2✓ Branch 0 taken 2814 times.
✓ Branch 1 taken 1407 times.
|
4221 | for (j = 0; j < channels; j++) { |
408 | 2814 | int32_t *extra = s->predictor_buf[j]; | |
409 | 2814 | int32_t *smp = s->sample_buf[j]; | |
410 |
2/2✓ Branch 0 taken 11520000 times.
✓ Branch 1 taken 2814 times.
|
11522814 | for (i = 0; i < s->frame_size; i++) { |
411 | 11520000 | extra[i] = smp[i] & mask; | |
412 | 11520000 | smp[i] >>= s->extra_bits; | |
413 | } | ||
414 | } | ||
415 | } | ||
416 | |||
417 |
1/2✓ Branch 0 taken 1781 times.
✗ Branch 1 not taken.
|
1781 | if (channels == 2) |
418 | 1781 | alac_stereo_decorrelation(s); | |
419 | else | ||
420 | ✗ | s->interlacing_shift = s->interlacing_leftweight = 0; | |
421 | 1781 | put_bits(pb, 8, s->interlacing_shift); | |
422 | 1781 | put_bits(pb, 8, s->interlacing_leftweight); | |
423 | |||
424 |
2/2✓ Branch 0 taken 3562 times.
✓ Branch 1 taken 1781 times.
|
5343 | for (i = 0; i < channels; i++) { |
425 | 3562 | calc_predictor_params(s, i); | |
426 | |||
427 | 3562 | put_bits(pb, 4, prediction_type); | |
428 | 3562 | put_bits(pb, 4, s->lpc[i].lpc_quant); | |
429 | |||
430 | 3562 | put_bits(pb, 3, s->rc.rice_modifier); | |
431 | 3562 | put_bits(pb, 5, s->lpc[i].lpc_order); | |
432 | // predictor coeff. table | ||
433 |
2/2✓ Branch 0 taken 31197 times.
✓ Branch 1 taken 3562 times.
|
34759 | for (j = 0; j < s->lpc[i].lpc_order; j++) |
434 | 31197 | put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]); | |
435 | } | ||
436 | |||
437 | // write extra bits if needed | ||
438 |
2/2✓ Branch 0 taken 1407 times.
✓ Branch 1 taken 374 times.
|
1781 | if (s->extra_bits) { |
439 |
2/2✓ Branch 0 taken 5760000 times.
✓ Branch 1 taken 1407 times.
|
5761407 | for (i = 0; i < s->frame_size; i++) { |
440 |
2/2✓ Branch 0 taken 11520000 times.
✓ Branch 1 taken 5760000 times.
|
17280000 | for (j = 0; j < channels; j++) { |
441 | 11520000 | put_bits(pb, s->extra_bits, s->predictor_buf[j][i]); | |
442 | } | ||
443 | } | ||
444 | } | ||
445 | |||
446 | // apply lpc and entropy coding to audio samples | ||
447 |
2/2✓ Branch 0 taken 3562 times.
✓ Branch 1 taken 1781 times.
|
5343 | for (i = 0; i < channels; i++) { |
448 | 3562 | alac_linear_predictor(s, i); | |
449 | |||
450 | // TODO: determine when this will actually help. for now it's not used. | ||
451 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3562 times.
|
3562 | if (prediction_type == 15) { |
452 | // 2nd pass 1st order filter | ||
453 | ✗ | int32_t *residual = s->predictor_buf[i]; | |
454 | ✗ | for (j = s->frame_size - 1; j > 0; j--) | |
455 | ✗ | residual[j] -= residual[j - 1]; | |
456 | } | ||
457 | 3562 | alac_entropy_coder(s, i); | |
458 | } | ||
459 | } | ||
460 | 2353 | } | |
461 | |||
462 | 2353 | static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, | |
463 | uint8_t * const *samples) | ||
464 | { | ||
465 | 2353 | PutBitContext *pb = &s->pbctx; | |
466 | 2353 | int channels = s->avctx->ch_layout.nb_channels; | |
467 | 2353 | const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[channels - 1]; | |
468 | 2353 | const uint8_t *ch_map = ff_alac_channel_layout_offsets[channels - 1]; | |
469 | int ch, element, sce, cpe; | ||
470 | |||
471 | 2353 | init_put_bits(pb, avpkt->data, avpkt->size); | |
472 | |||
473 | 2353 | ch = element = sce = cpe = 0; | |
474 |
2/2✓ Branch 0 taken 2353 times.
✓ Branch 1 taken 2353 times.
|
4706 | while (ch < channels) { |
475 |
1/2✓ Branch 0 taken 2353 times.
✗ Branch 1 not taken.
|
2353 | if (ch_elements[element] == TYPE_CPE) { |
476 | 2353 | write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]], | |
477 | 2353 | samples[ch_map[ch + 1]]); | |
478 | 2353 | cpe++; | |
479 | 2353 | ch += 2; | |
480 | } else { | ||
481 | ✗ | write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL); | |
482 | ✗ | sce++; | |
483 | ✗ | ch++; | |
484 | } | ||
485 | 2353 | element++; | |
486 | } | ||
487 | |||
488 | 2353 | put_bits(pb, 3, TYPE_END); | |
489 | 2353 | flush_put_bits(pb); | |
490 | |||
491 | 2353 | return put_bytes_output(pb); | |
492 | } | ||
493 | |||
494 | 18 | static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps) | |
495 | { | ||
496 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE); |
497 | 18 | return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8; | |
498 | } | ||
499 | |||
500 | 9 | static av_cold int alac_encode_close(AVCodecContext *avctx) | |
501 | { | ||
502 | 9 | AlacEncodeContext *s = avctx->priv_data; | |
503 | 9 | ff_lpc_end(&s->lpc_ctx); | |
504 | 9 | return 0; | |
505 | } | ||
506 | |||
507 | 9 | static av_cold int alac_encode_init(AVCodecContext *avctx) | |
508 | { | ||
509 | 9 | AlacEncodeContext *s = avctx->priv_data; | |
510 | int ret; | ||
511 | uint8_t *alac_extradata; | ||
512 | |||
513 | 9 | avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE; | |
514 | |||
515 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) { |
516 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (avctx->bits_per_raw_sample != 24) |
517 | 4 | av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n"); | |
518 | 4 | avctx->bits_per_raw_sample = 24; | |
519 | } else { | ||
520 | 5 | avctx->bits_per_raw_sample = 16; | |
521 | 5 | s->extra_bits = 0; | |
522 | } | ||
523 | |||
524 | // Set default compression level | ||
525 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
|
9 | if (avctx->compression_level == FF_COMPRESSION_DEFAULT) |
526 | 2 | s->compression_level = 2; | |
527 | else | ||
528 | 7 | s->compression_level = av_clip(avctx->compression_level, 0, 2); | |
529 | |||
530 | // Initialize default Rice parameters | ||
531 | 9 | s->rc.history_mult = 40; | |
532 | 9 | s->rc.initial_history = 10; | |
533 | 9 | s->rc.k_modifier = 14; | |
534 | 9 | s->rc.rice_modifier = 4; | |
535 | |||
536 | 9 | s->max_coded_frame_size = get_max_frame_size(avctx->frame_size, | |
537 | avctx->ch_layout.nb_channels, | ||
538 | avctx->bits_per_raw_sample); | ||
539 | |||
540 | 9 | avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE); | |
541 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!avctx->extradata) |
542 | ✗ | return AVERROR(ENOMEM); | |
543 | 9 | avctx->extradata_size = ALAC_EXTRADATA_SIZE; | |
544 | |||
545 | 9 | alac_extradata = avctx->extradata; | |
546 | 9 | AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE); | |
547 | 9 | AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c')); | |
548 | 9 | AV_WB32(alac_extradata+12, avctx->frame_size); | |
549 | 9 | AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample); | |
550 | 9 | AV_WB8 (alac_extradata+21, avctx->ch_layout.nb_channels); | |
551 | 9 | AV_WB32(alac_extradata+24, s->max_coded_frame_size); | |
552 | 9 | AV_WB32(alac_extradata+28, | |
553 | avctx->sample_rate * avctx->ch_layout.nb_channels * avctx->bits_per_raw_sample); // average bitrate | ||
554 | 9 | AV_WB32(alac_extradata+32, avctx->sample_rate); | |
555 | |||
556 | // Set relevant extradata fields | ||
557 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | if (s->compression_level > 0) { |
558 | 7 | AV_WB8(alac_extradata+18, s->rc.history_mult); | |
559 | 7 | AV_WB8(alac_extradata+19, s->rc.initial_history); | |
560 | 7 | AV_WB8(alac_extradata+20, s->rc.k_modifier); | |
561 | } | ||
562 | |||
563 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (s->max_prediction_order < s->min_prediction_order) { |
564 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
565 | "invalid prediction orders: min=%d max=%d\n", | ||
566 | s->min_prediction_order, s->max_prediction_order); | ||
567 | ✗ | return AVERROR(EINVAL); | |
568 | } | ||
569 | |||
570 | 9 | s->avctx = avctx; | |
571 | |||
572 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size, |
573 | s->max_prediction_order, | ||
574 | FF_LPC_TYPE_LEVINSON)) < 0) { | ||
575 | ✗ | return ret; | |
576 | } | ||
577 | |||
578 | 9 | return 0; | |
579 | } | ||
580 | |||
581 | 2353 | static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
582 | const AVFrame *frame, int *got_packet_ptr) | ||
583 | { | ||
584 | 2353 | AlacEncodeContext *s = avctx->priv_data; | |
585 | int out_bytes, max_frame_size, ret; | ||
586 | |||
587 | 2353 | s->frame_size = frame->nb_samples; | |
588 | |||
589 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
|
2353 | if (frame->nb_samples < DEFAULT_FRAME_SIZE) |
590 | 9 | max_frame_size = get_max_frame_size(s->frame_size, avctx->ch_layout.nb_channels, | |
591 | avctx->bits_per_raw_sample); | ||
592 | else | ||
593 | 2344 | max_frame_size = s->max_coded_frame_size; | |
594 | |||
595 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2353 times.
|
2353 | if ((ret = ff_alloc_packet(avctx, avpkt, 4 * max_frame_size)) < 0) |
596 | ✗ | return ret; | |
597 | |||
598 | /* use verbatim mode for compression_level 0 */ | ||
599 |
2/2✓ Branch 0 taken 1781 times.
✓ Branch 1 taken 572 times.
|
2353 | if (s->compression_level) { |
600 | 1781 | s->verbatim = 0; | |
601 | 1781 | s->extra_bits = avctx->bits_per_raw_sample - 16; | |
602 | } else { | ||
603 | 572 | s->verbatim = 1; | |
604 | 572 | s->extra_bits = 0; | |
605 | } | ||
606 | |||
607 | 2353 | out_bytes = write_frame(s, avpkt, frame->extended_data); | |
608 | |||
609 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2353 times.
|
2353 | if (out_bytes > max_frame_size) { |
610 | /* frame too large. use verbatim mode */ | ||
611 | ✗ | s->verbatim = 1; | |
612 | ✗ | s->extra_bits = 0; | |
613 | ✗ | out_bytes = write_frame(s, avpkt, frame->extended_data); | |
614 | } | ||
615 | |||
616 | 2353 | avpkt->size = out_bytes; | |
617 | 2353 | *got_packet_ptr = 1; | |
618 | 2353 | return 0; | |
619 | } | ||
620 | |||
621 | #define OFFSET(x) offsetof(AlacEncodeContext, x) | ||
622 | #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
623 | static const AVOption options[] = { | ||
624 | { "min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MIN_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE }, | ||
625 | { "max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MAX_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE }, | ||
626 | |||
627 | { NULL }, | ||
628 | }; | ||
629 | |||
630 | static const AVClass alacenc_class = { | ||
631 | .class_name = "alacenc", | ||
632 | .item_name = av_default_item_name, | ||
633 | .option = options, | ||
634 | .version = LIBAVUTIL_VERSION_INT, | ||
635 | }; | ||
636 | |||
637 | const FFCodec ff_alac_encoder = { | ||
638 | .p.name = "alac", | ||
639 | CODEC_LONG_NAME("ALAC (Apple Lossless Audio Codec)"), | ||
640 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
641 | .p.id = AV_CODEC_ID_ALAC, | ||
642 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME | | ||
643 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
644 | .priv_data_size = sizeof(AlacEncodeContext), | ||
645 | .p.priv_class = &alacenc_class, | ||
646 | .init = alac_encode_init, | ||
647 | FF_CODEC_ENCODE_CB(alac_encode_frame), | ||
648 | .close = alac_encode_close, | ||
649 | .p.ch_layouts = ff_alac_ch_layouts, | ||
650 | .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P, | ||
651 | AV_SAMPLE_FMT_S16P, | ||
652 | AV_SAMPLE_FMT_NONE }, | ||
653 | }; | ||
654 |