Mercurial > libavcodec.hg
annotate alacenc.c @ 10365:b44de7b79bee libavcodec
alacenc: add a fixed LPC coefficient mode as compression level 1. old
compression level 1 is now compression level 2 and is still the default.
author | jbr |
---|---|
date | Mon, 05 Oct 2009 21:27:36 +0000 |
parents | 2b8a327189cd |
children | 55f4c141dbb0 |
rev | line source |
---|---|
7595 | 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 "avcodec.h" | |
9428 | 23 #include "get_bits.h" |
9411
4cb7c65fc775
Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents:
9110
diff
changeset
|
24 #include "put_bits.h" |
7595 | 25 #include "dsputil.h" |
26 #include "lpc.h" | |
9110 | 27 #include "mathops.h" |
7595 | 28 |
29 #define DEFAULT_FRAME_SIZE 4096 | |
30 #define DEFAULT_SAMPLE_SIZE 16 | |
31 #define MAX_CHANNELS 8 | |
32 #define ALAC_EXTRADATA_SIZE 36 | |
33 #define ALAC_FRAME_HEADER_SIZE 55 | |
34 #define ALAC_FRAME_FOOTER_SIZE 3 | |
35 | |
36 #define ALAC_ESCAPE_CODE 0x1FF | |
37 #define ALAC_MAX_LPC_ORDER 30 | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
38 #define DEFAULT_MAX_PRED_ORDER 6 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
39 #define DEFAULT_MIN_PRED_ORDER 4 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
40 #define ALAC_MAX_LPC_PRECISION 9 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
41 #define ALAC_MAX_LPC_SHIFT 9 |
7595 | 42 |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
43 #define ALAC_CHMODE_LEFT_RIGHT 0 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
44 #define ALAC_CHMODE_LEFT_SIDE 1 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
45 #define ALAC_CHMODE_RIGHT_SIDE 2 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
46 #define ALAC_CHMODE_MID_SIDE 3 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
47 |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
48 typedef struct RiceContext { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
49 int history_mult; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
50 int initial_history; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
51 int k_modifier; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
52 int rice_modifier; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
53 } RiceContext; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
54 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
55 typedef struct LPCContext { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
56 int lpc_order; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
57 int lpc_coeff[ALAC_MAX_LPC_ORDER+1]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
58 int lpc_quant; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
59 } LPCContext; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
60 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
61 typedef struct AlacEncodeContext { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
62 int compression_level; |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
63 int min_prediction_order; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
64 int max_prediction_order; |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
65 int max_coded_frame_size; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
66 int write_sample_size; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
67 int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE]; |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
68 int32_t predictor_buf[DEFAULT_FRAME_SIZE]; |
7595 | 69 int interlacing_shift; |
70 int interlacing_leftweight; | |
71 PutBitContext pbctx; | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
72 RiceContext rc; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
73 LPCContext lpc[MAX_CHANNELS]; |
7595 | 74 DSPContext dspctx; |
75 AVCodecContext *avctx; | |
76 } AlacEncodeContext; | |
77 | |
78 | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
79 static void init_sample_buffers(AlacEncodeContext *s, int16_t *input_samples) |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
80 { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
81 int ch, i; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
82 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
83 for(ch=0;ch<s->avctx->channels;ch++) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
84 int16_t *sptr = input_samples + ch; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
85 for(i=0;i<s->avctx->frame_size;i++) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
86 s->sample_buf[ch][i] = *sptr; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
87 sptr += s->avctx->channels; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
88 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
89 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
90 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
91 |
7595 | 92 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size) |
93 { | |
94 int divisor, q, r; | |
95 | |
96 k = FFMIN(k, s->rc.k_modifier); | |
97 divisor = (1<<k) - 1; | |
98 q = x / divisor; | |
99 r = x % divisor; | |
100 | |
101 if(q > 8) { | |
102 // write escape code and sample value directly | |
103 put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE); | |
104 put_bits(&s->pbctx, write_sample_size, x); | |
105 } else { | |
106 if(q) | |
107 put_bits(&s->pbctx, q, (1<<q) - 1); | |
108 put_bits(&s->pbctx, 1, 0); | |
109 | |
110 if(k != 1) { | |
111 if(r > 0) | |
112 put_bits(&s->pbctx, k, r+1); | |
113 else | |
114 put_bits(&s->pbctx, k-1, 0); | |
115 } | |
116 } | |
117 } | |
118 | |
119 static void write_frame_header(AlacEncodeContext *s, int is_verbatim) | |
120 { | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
121 put_bits(&s->pbctx, 3, s->avctx->channels-1); // No. of channels -1 |
7595 | 122 put_bits(&s->pbctx, 16, 0); // Seems to be zero |
123 put_bits(&s->pbctx, 1, 1); // Sample count is in the header | |
124 put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field | |
125 put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim | |
10344
2b8a327189cd
put_bits can only reliably write up to 31 bit bits, above it relies on
reimar
parents:
10173
diff
changeset
|
126 put_bits32(&s->pbctx, s->avctx->frame_size); // No. of samples in the frame |
7595 | 127 } |
128 | |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
129 static void calc_predictor_params(AlacEncodeContext *s, int ch) |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
130 { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
131 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
132 int shift[MAX_LPC_ORDER]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
133 int opt_order; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
134 |
10365
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
135 if (s->avctx->compression_level > 1) { |
7618
2b023daf5329
alacenc: Use user-specified min and max prediction order.
ramiro
parents:
7617
diff
changeset
|
136 opt_order = ff_lpc_calc_coefs(&s->dspctx, s->sample_buf[ch], s->avctx->frame_size, s->min_prediction_order, s->max_prediction_order, |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
137 ALAC_MAX_LPC_PRECISION, coefs, shift, 1, ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
138 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
139 s->lpc[ch].lpc_order = opt_order; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
140 s->lpc[ch].lpc_quant = shift[opt_order-1]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
141 memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int)); |
10365
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
142 } else { |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
143 s->lpc[ch].lpc_order = 6; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
144 s->lpc[ch].lpc_quant = 6; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
145 s->lpc[ch].lpc_coeff[0] = 160; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
146 s->lpc[ch].lpc_coeff[1] = -190; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
147 s->lpc[ch].lpc_coeff[2] = 170; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
148 s->lpc[ch].lpc_coeff[3] = -130; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
149 s->lpc[ch].lpc_coeff[4] = 80; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
150 s->lpc[ch].lpc_coeff[5] = -25; |
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
151 } |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
152 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
153 |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
154 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n) |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
155 { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
156 int i, best; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
157 int32_t lt, rt; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
158 uint64_t sum[4]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
159 uint64_t score[4]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
160 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
161 /* calculate sum of 2nd order residual for each channel */ |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
162 sum[0] = sum[1] = sum[2] = sum[3] = 0; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
163 for(i=2; i<n; i++) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
164 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
165 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
166 sum[2] += FFABS((lt + rt) >> 1); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
167 sum[3] += FFABS(lt - rt); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
168 sum[0] += FFABS(lt); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
169 sum[1] += FFABS(rt); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
170 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
171 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
172 /* calculate score for each mode */ |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
173 score[0] = sum[0] + sum[1]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
174 score[1] = sum[0] + sum[3]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
175 score[2] = sum[1] + sum[3]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
176 score[3] = sum[2] + sum[3]; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
177 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
178 /* return mode with lowest score */ |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
179 best = 0; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
180 for(i=1; i<4; i++) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
181 if(score[i] < score[best]) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
182 best = i; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
183 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
184 } |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
185 return best; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
186 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
187 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
188 static void alac_stereo_decorrelation(AlacEncodeContext *s) |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
189 { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
190 int32_t *left = s->sample_buf[0], *right = s->sample_buf[1]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
191 int i, mode, n = s->avctx->frame_size; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
192 int32_t tmp; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
193 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
194 mode = estimate_stereo_mode(left, right, n); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
195 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
196 switch(mode) |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
197 { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
198 case ALAC_CHMODE_LEFT_RIGHT: |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
199 s->interlacing_leftweight = 0; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
200 s->interlacing_shift = 0; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
201 break; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
202 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
203 case ALAC_CHMODE_LEFT_SIDE: |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
204 for(i=0; i<n; i++) { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
205 right[i] = left[i] - right[i]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
206 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
207 s->interlacing_leftweight = 1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
208 s->interlacing_shift = 0; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
209 break; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
210 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
211 case ALAC_CHMODE_RIGHT_SIDE: |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
212 for(i=0; i<n; i++) { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
213 tmp = right[i]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
214 right[i] = left[i] - right[i]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
215 left[i] = tmp + (right[i] >> 31); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
216 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
217 s->interlacing_leftweight = 1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
218 s->interlacing_shift = 31; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
219 break; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
220 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
221 default: |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
222 for(i=0; i<n; i++) { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
223 tmp = left[i]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
224 left[i] = (tmp + right[i]) >> 1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
225 right[i] = tmp - right[i]; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
226 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
227 s->interlacing_leftweight = 1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
228 s->interlacing_shift = 1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
229 break; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
230 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
231 } |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
232 |
7619 | 233 static void alac_linear_predictor(AlacEncodeContext *s, int ch) |
234 { | |
235 int i; | |
236 LPCContext lpc = s->lpc[ch]; | |
237 | |
238 if(lpc.lpc_order == 31) { | |
239 s->predictor_buf[0] = s->sample_buf[ch][0]; | |
240 | |
241 for(i=1; i<s->avctx->frame_size; i++) | |
242 s->predictor_buf[i] = s->sample_buf[ch][i] - s->sample_buf[ch][i-1]; | |
243 | |
244 return; | |
245 } | |
246 | |
247 // generalised linear predictor | |
248 | |
249 if(lpc.lpc_order > 0) { | |
250 int32_t *samples = s->sample_buf[ch]; | |
251 int32_t *residual = s->predictor_buf; | |
252 | |
253 // generate warm-up samples | |
254 residual[0] = samples[0]; | |
255 for(i=1;i<=lpc.lpc_order;i++) | |
256 residual[i] = samples[i] - samples[i-1]; | |
257 | |
258 // perform lpc on remaining samples | |
259 for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) { | |
260 int sum = 1 << (lpc.lpc_quant - 1), res_val, j; | |
261 | |
262 for (j = 0; j < lpc.lpc_order; j++) { | |
263 sum += (samples[lpc.lpc_order-j] - samples[0]) * | |
264 lpc.lpc_coeff[j]; | |
265 } | |
266 | |
267 sum >>= lpc.lpc_quant; | |
268 sum += samples[0]; | |
9110 | 269 residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum, |
270 s->write_sample_size); | |
7619 | 271 res_val = residual[i]; |
272 | |
273 if(res_val) { | |
274 int index = lpc.lpc_order - 1; | |
275 int neg = (res_val < 0); | |
276 | |
277 while(index >= 0 && (neg ? (res_val < 0):(res_val > 0))) { | |
278 int val = samples[0] - samples[lpc.lpc_order - index]; | |
279 int sign = (val ? FFSIGN(val) : 0); | |
280 | |
281 if(neg) | |
282 sign*=-1; | |
283 | |
284 lpc.lpc_coeff[index] -= sign; | |
285 val *= sign; | |
286 res_val -= ((val >> lpc.lpc_quant) * | |
287 (lpc.lpc_order - index)); | |
288 index--; | |
289 } | |
290 } | |
291 samples++; | |
292 } | |
293 } | |
294 } | |
295 | |
296 static void alac_entropy_coder(AlacEncodeContext *s) | |
297 { | |
298 unsigned int history = s->rc.initial_history; | |
299 int sign_modifier = 0, i, k; | |
300 int32_t *samples = s->predictor_buf; | |
301 | |
302 for(i=0;i < s->avctx->frame_size;) { | |
303 int x; | |
304 | |
305 k = av_log2((history >> 9) + 3); | |
306 | |
307 x = -2*(*samples)-1; | |
308 x ^= (x>>31); | |
309 | |
310 samples++; | |
311 i++; | |
312 | |
313 encode_scalar(s, x - sign_modifier, k, s->write_sample_size); | |
314 | |
315 history += x * s->rc.history_mult | |
316 - ((history * s->rc.history_mult) >> 9); | |
317 | |
318 sign_modifier = 0; | |
319 if(x > 0xFFFF) | |
320 history = 0xFFFF; | |
321 | |
322 if((history < 128) && (i < s->avctx->frame_size)) { | |
323 unsigned int block_size = 0; | |
324 | |
325 k = 7 - av_log2(history) + ((history + 16) >> 6); | |
326 | |
327 while((*samples == 0) && (i < s->avctx->frame_size)) { | |
328 samples++; | |
329 i++; | |
330 block_size++; | |
331 } | |
332 encode_scalar(s, block_size, k, 16); | |
333 | |
334 sign_modifier = (block_size <= 0xFFFF); | |
335 | |
336 history = 0; | |
337 } | |
338 | |
339 } | |
340 } | |
341 | |
7595 | 342 static void write_compressed_frame(AlacEncodeContext *s) |
343 { | |
344 int i, j; | |
345 | |
7659
b87a9296e854
alacenc : perform decorrelation only for stereo samples
jai_menon
parents:
7620
diff
changeset
|
346 if(s->avctx->channels == 2) |
b87a9296e854
alacenc : perform decorrelation only for stereo samples
jai_menon
parents:
7620
diff
changeset
|
347 alac_stereo_decorrelation(s); |
7595 | 348 put_bits(&s->pbctx, 8, s->interlacing_shift); |
349 put_bits(&s->pbctx, 8, s->interlacing_leftweight); | |
350 | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
351 for(i=0;i<s->avctx->channels;i++) { |
7595 | 352 |
353 calc_predictor_params(s, i); | |
354 | |
355 put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd | |
356 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant); | |
357 | |
358 put_bits(&s->pbctx, 3, s->rc.rice_modifier); | |
359 put_bits(&s->pbctx, 5, s->lpc[i].lpc_order); | |
360 // predictor coeff. table | |
361 for(j=0;j<s->lpc[i].lpc_order;j++) { | |
362 put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]); | |
363 } | |
364 } | |
365 | |
366 // apply lpc and entropy coding to audio samples | |
367 | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
368 for(i=0;i<s->avctx->channels;i++) { |
7595 | 369 alac_linear_predictor(s, i); |
370 alac_entropy_coder(s); | |
371 } | |
372 } | |
373 | |
374 static av_cold int alac_encode_init(AVCodecContext *avctx) | |
375 { | |
376 AlacEncodeContext *s = avctx->priv_data; | |
377 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1); | |
378 | |
379 avctx->frame_size = DEFAULT_FRAME_SIZE; | |
7823
4525dcd81357
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
7659
diff
changeset
|
380 avctx->bits_per_coded_sample = DEFAULT_SAMPLE_SIZE; |
7595 | 381 |
382 if(avctx->sample_fmt != SAMPLE_FMT_S16) { | |
383 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n"); | |
384 return -1; | |
385 } | |
386 | |
387 // Set default compression level | |
388 if(avctx->compression_level == FF_COMPRESSION_DEFAULT) | |
10365
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
389 s->compression_level = 2; |
7595 | 390 else |
10365
b44de7b79bee
alacenc: add a fixed LPC coefficient mode as compression level 1. old
jbr
parents:
10344
diff
changeset
|
391 s->compression_level = av_clip(avctx->compression_level, 0, 2); |
7595 | 392 |
393 // Initialize default Rice parameters | |
394 s->rc.history_mult = 40; | |
395 s->rc.initial_history = 10; | |
396 s->rc.k_modifier = 14; | |
397 s->rc.rice_modifier = 4; | |
398 | |
10173
f55ca9a2b948
Fix max_coded_frame_size computation to account for byte alignment.
jai_menon
parents:
9428
diff
changeset
|
399 s->max_coded_frame_size = 8 + (avctx->frame_size*avctx->channels*avctx->bits_per_coded_sample>>3); |
7595 | 400 |
7823
4525dcd81357
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
7659
diff
changeset
|
401 s->write_sample_size = avctx->bits_per_coded_sample + avctx->channels - 1; // FIXME: consider wasted_bytes |
7595 | 402 |
403 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE); | |
404 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c')); | |
405 AV_WB32(alac_extradata+12, avctx->frame_size); | |
7823
4525dcd81357
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
7659
diff
changeset
|
406 AV_WB8 (alac_extradata+17, avctx->bits_per_coded_sample); |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
407 AV_WB8 (alac_extradata+21, avctx->channels); |
7595 | 408 AV_WB32(alac_extradata+24, s->max_coded_frame_size); |
7823
4525dcd81357
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
7659
diff
changeset
|
409 AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_coded_sample); // average bitrate |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
410 AV_WB32(alac_extradata+32, avctx->sample_rate); |
7595 | 411 |
412 // Set relevant extradata fields | |
413 if(s->compression_level > 0) { | |
414 AV_WB8(alac_extradata+18, s->rc.history_mult); | |
415 AV_WB8(alac_extradata+19, s->rc.initial_history); | |
416 AV_WB8(alac_extradata+20, s->rc.k_modifier); | |
417 } | |
418 | |
7619 | 419 s->min_prediction_order = DEFAULT_MIN_PRED_ORDER; |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
420 if(avctx->min_prediction_order >= 0) { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
421 if(avctx->min_prediction_order < MIN_LPC_ORDER || |
7620
5c2299115d1f
alacenc: compare against ALAC_MAX_LPC_ORDER instead of MAX_LPC_ORDER
jai_menon
parents:
7619
diff
changeset
|
422 avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) { |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
423 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
424 return -1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
425 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
426 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
427 s->min_prediction_order = avctx->min_prediction_order; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
428 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
429 |
7619 | 430 s->max_prediction_order = DEFAULT_MAX_PRED_ORDER; |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
431 if(avctx->max_prediction_order >= 0) { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
432 if(avctx->max_prediction_order < MIN_LPC_ORDER || |
7620
5c2299115d1f
alacenc: compare against ALAC_MAX_LPC_ORDER instead of MAX_LPC_ORDER
jai_menon
parents:
7619
diff
changeset
|
433 avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) { |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
434 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
435 return -1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
436 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
437 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
438 s->max_prediction_order = avctx->max_prediction_order; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
439 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
440 |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
441 if(s->max_prediction_order < s->min_prediction_order) { |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
442 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n", |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
443 s->min_prediction_order, s->max_prediction_order); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
444 return -1; |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
445 } |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
446 |
7595 | 447 avctx->extradata = alac_extradata; |
448 avctx->extradata_size = ALAC_EXTRADATA_SIZE; | |
449 | |
450 avctx->coded_frame = avcodec_alloc_frame(); | |
451 avctx->coded_frame->key_frame = 1; | |
452 | |
453 s->avctx = avctx; | |
454 dsputil_init(&s->dspctx, avctx); | |
455 | |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
456 return 0; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
457 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
458 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
459 static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame, |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
460 int buf_size, void *data) |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
461 { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
462 AlacEncodeContext *s = avctx->priv_data; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
463 PutBitContext *pb = &s->pbctx; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
464 int i, out_bytes, verbatim_flag = 0; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
465 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
466 if(avctx->frame_size > DEFAULT_FRAME_SIZE) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
467 av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n"); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
468 return -1; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
469 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
470 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
471 if(buf_size < 2*s->max_coded_frame_size) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
472 av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n"); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
473 return -1; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
474 } |
7595 | 475 |
7617
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
476 verbatim: |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
477 init_put_bits(pb, frame, buf_size); |
924dc060db81
Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents:
7604
diff
changeset
|
478 |
7604
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
479 if((s->compression_level == 0) || verbatim_flag) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
480 // Verbatim mode |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
481 int16_t *samples = data; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
482 write_frame_header(s, 1); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
483 for(i=0; i<avctx->frame_size*avctx->channels; i++) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
484 put_sbits(pb, 16, *samples++); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
485 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
486 } else { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
487 init_sample_buffers(s, data); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
488 write_frame_header(s, 0); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
489 write_compressed_frame(s); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
490 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
491 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
492 put_bits(pb, 3, 7); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
493 flush_put_bits(pb); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
494 out_bytes = put_bits_count(pb) >> 3; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
495 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
496 if(out_bytes > s->max_coded_frame_size) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
497 /* frame too large. use verbatim mode */ |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
498 if(verbatim_flag || (s->compression_level == 0)) { |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
499 /* still too large. must be an error. */ |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
500 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n"); |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
501 return -1; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
502 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
503 verbatim_flag = 1; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
504 goto verbatim; |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
505 } |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
506 |
6250ff63990b
Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
7598
diff
changeset
|
507 return out_bytes; |
7595 | 508 } |
509 | |
510 static av_cold int alac_encode_close(AVCodecContext *avctx) | |
511 { | |
512 av_freep(&avctx->extradata); | |
513 avctx->extradata_size = 0; | |
514 av_freep(&avctx->coded_frame); | |
515 return 0; | |
516 } | |
517 | |
518 AVCodec alac_encoder = { | |
519 "alac", | |
520 CODEC_TYPE_AUDIO, | |
521 CODEC_ID_ALAC, | |
522 sizeof(AlacEncodeContext), | |
523 alac_encode_init, | |
524 alac_encode_frame, | |
525 alac_encode_close, | |
526 .capabilities = CODEC_CAP_SMALL_LAST_FRAME, | |
7598 | 527 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"), |
7595 | 528 }; |