comparison alacenc.c @ 7604:6250ff63990b libavcodec

Import more ok'd parts of ALAC encoder from GSoC repo.
author ramiro
date Sun, 17 Aug 2008 22:47:40 +0000
parents 60dd0089cdba
children 924dc060db81
comparison
equal deleted inserted replaced
7603:20c91136fe71 7604:6250ff63990b
31 #define ALAC_FRAME_HEADER_SIZE 55 31 #define ALAC_FRAME_HEADER_SIZE 55
32 #define ALAC_FRAME_FOOTER_SIZE 3 32 #define ALAC_FRAME_FOOTER_SIZE 3
33 33
34 #define ALAC_ESCAPE_CODE 0x1FF 34 #define ALAC_ESCAPE_CODE 0x1FF
35 #define ALAC_MAX_LPC_ORDER 30 35 #define ALAC_MAX_LPC_ORDER 30
36 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 typedef struct RiceContext {
42 int history_mult;
43 int initial_history;
44 int k_modifier;
45 int rice_modifier;
46 } RiceContext;
47
48 typedef struct LPCContext {
49 int lpc_order;
50 int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
51 int lpc_quant;
52 } LPCContext;
53
54 typedef struct AlacEncodeContext {
55 int compression_level;
56 int max_coded_frame_size;
57 int write_sample_size;
58 int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE];
37 int interlacing_shift; 59 int interlacing_shift;
38 int interlacing_leftweight; 60 int interlacing_leftweight;
39 PutBitContext pbctx; 61 PutBitContext pbctx;
62 RiceContext rc;
63 LPCContext lpc[MAX_CHANNELS];
40 DSPContext dspctx; 64 DSPContext dspctx;
41 AVCodecContext *avctx; 65 AVCodecContext *avctx;
42 } AlacEncodeContext; 66 } AlacEncodeContext;
43 67
68
69 static void init_sample_buffers(AlacEncodeContext *s, int16_t *input_samples)
70 {
71 int ch, i;
72
73 for(ch=0;ch<s->avctx->channels;ch++) {
74 int16_t *sptr = input_samples + ch;
75 for(i=0;i<s->avctx->frame_size;i++) {
76 s->sample_buf[ch][i] = *sptr;
77 sptr += s->avctx->channels;
78 }
79 }
80 }
44 81
45 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size) 82 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
46 { 83 {
47 int divisor, q, r; 84 int divisor, q, r;
48 85
69 } 106 }
70 } 107 }
71 108
72 static void write_frame_header(AlacEncodeContext *s, int is_verbatim) 109 static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
73 { 110 {
74 put_bits(&s->pbctx, 3, s->channels-1); // No. of channels -1 111 put_bits(&s->pbctx, 3, s->avctx->channels-1); // No. of channels -1
75 put_bits(&s->pbctx, 16, 0); // Seems to be zero 112 put_bits(&s->pbctx, 16, 0); // Seems to be zero
76 put_bits(&s->pbctx, 1, 1); // Sample count is in the header 113 put_bits(&s->pbctx, 1, 1); // Sample count is in the header
77 put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field 114 put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field
78 put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim 115 put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim
79 put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of samples in the frame 116 put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of samples in the frame
80 } 117 }
81 118
119 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
120 {
121 int i, best;
122 int32_t lt, rt;
123 uint64_t sum[4];
124 uint64_t score[4];
125
126 /* calculate sum of 2nd order residual for each channel */
127 sum[0] = sum[1] = sum[2] = sum[3] = 0;
128 for(i=2; i<n; i++) {
129 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
130 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
131 sum[2] += FFABS((lt + rt) >> 1);
132 sum[3] += FFABS(lt - rt);
133 sum[0] += FFABS(lt);
134 sum[1] += FFABS(rt);
135 }
136
137 /* calculate score for each mode */
138 score[0] = sum[0] + sum[1];
139 score[1] = sum[0] + sum[3];
140 score[2] = sum[1] + sum[3];
141 score[3] = sum[2] + sum[3];
142
143 /* return mode with lowest score */
144 best = 0;
145 for(i=1; i<4; i++) {
146 if(score[i] < score[best]) {
147 best = i;
148 }
149 }
150
82 static void write_compressed_frame(AlacEncodeContext *s) 151 static void write_compressed_frame(AlacEncodeContext *s)
83 { 152 {
84 int i, j; 153 int i, j;
85 154
86 /* only simple mid/side decorrelation supported as of now */ 155 /* only simple mid/side decorrelation supported as of now */
87 alac_stereo_decorrelation(s); 156 alac_stereo_decorrelation(s);
88 put_bits(&s->pbctx, 8, s->interlacing_shift); 157 put_bits(&s->pbctx, 8, s->interlacing_shift);
89 put_bits(&s->pbctx, 8, s->interlacing_leftweight); 158 put_bits(&s->pbctx, 8, s->interlacing_leftweight);
90 159
91 for(i=0;i<s->channels;i++) { 160 for(i=0;i<s->avctx->channels;i++) {
92 161
93 calc_predictor_params(s, i); 162 calc_predictor_params(s, i);
94 163
95 put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd 164 put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd
96 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant); 165 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
103 } 172 }
104 } 173 }
105 174
106 // apply lpc and entropy coding to audio samples 175 // apply lpc and entropy coding to audio samples
107 176
108 for(i=0;i<s->channels;i++) { 177 for(i=0;i<s->avctx->channels;i++) {
109 alac_linear_predictor(s, i); 178 alac_linear_predictor(s, i);
110 alac_entropy_coder(s); 179 alac_entropy_coder(s);
111 } 180 }
112 } 181 }
113 182
116 AlacEncodeContext *s = avctx->priv_data; 185 AlacEncodeContext *s = avctx->priv_data;
117 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1); 186 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
118 187
119 avctx->frame_size = DEFAULT_FRAME_SIZE; 188 avctx->frame_size = DEFAULT_FRAME_SIZE;
120 avctx->bits_per_sample = DEFAULT_SAMPLE_SIZE; 189 avctx->bits_per_sample = DEFAULT_SAMPLE_SIZE;
121 s->channels = avctx->channels;
122 s->samplerate = avctx->sample_rate;
123 190
124 if(avctx->sample_fmt != SAMPLE_FMT_S16) { 191 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
125 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n"); 192 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
126 return -1; 193 return -1;
127 } 194 }
137 s->rc.initial_history = 10; 204 s->rc.initial_history = 10;
138 s->rc.k_modifier = 14; 205 s->rc.k_modifier = 14;
139 s->rc.rice_modifier = 4; 206 s->rc.rice_modifier = 4;
140 207
141 s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE + 208 s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE +
142 avctx->frame_size*s->channels*avctx->bits_per_sample)>>3; 209 avctx->frame_size*avctx->channels*avctx->bits_per_sample)>>3;
143 210
144 s->write_sample_size = avctx->bits_per_sample + s->channels - 1; // FIXME: consider wasted_bytes 211 s->write_sample_size = avctx->bits_per_sample + avctx->channels - 1; // FIXME: consider wasted_bytes
145 212
146 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE); 213 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
147 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c')); 214 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
148 AV_WB32(alac_extradata+12, avctx->frame_size); 215 AV_WB32(alac_extradata+12, avctx->frame_size);
149 AV_WB8 (alac_extradata+17, avctx->bits_per_sample); 216 AV_WB8 (alac_extradata+17, avctx->bits_per_sample);
150 AV_WB8 (alac_extradata+21, s->channels); 217 AV_WB8 (alac_extradata+21, avctx->channels);
151 AV_WB32(alac_extradata+24, s->max_coded_frame_size); 218 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
152 AV_WB32(alac_extradata+28, s->samplerate*s->channels*avctx->bits_per_sample); // average bitrate 219 AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_sample); // average bitrate
153 AV_WB32(alac_extradata+32, s->samplerate); 220 AV_WB32(alac_extradata+32, avctx->sample_rate);
154 221
155 // Set relevant extradata fields 222 // Set relevant extradata fields
156 if(s->compression_level > 0) { 223 if(s->compression_level > 0) {
157 AV_WB8(alac_extradata+18, s->rc.history_mult); 224 AV_WB8(alac_extradata+18, s->rc.history_mult);
158 AV_WB8(alac_extradata+19, s->rc.initial_history); 225 AV_WB8(alac_extradata+19, s->rc.initial_history);
166 avctx->coded_frame->key_frame = 1; 233 avctx->coded_frame->key_frame = 1;
167 234
168 s->avctx = avctx; 235 s->avctx = avctx;
169 dsputil_init(&s->dspctx, avctx); 236 dsputil_init(&s->dspctx, avctx);
170 237
171 allocate_sample_buffers(s);
172
173 return 0; 238 return 0;
174 } 239 }
175 240
241 static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
242 int buf_size, void *data)
243 {
244 AlacEncodeContext *s = avctx->priv_data;
245 PutBitContext *pb = &s->pbctx;
246 int i, out_bytes, verbatim_flag = 0;
247
248 if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
249 av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
250 return -1;
251 }
252
253 if(buf_size < 2*s->max_coded_frame_size) {
254 av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
255 return -1;
256 }
257
258 if((s->compression_level == 0) || verbatim_flag) {
259 // Verbatim mode
260 int16_t *samples = data;
261 write_frame_header(s, 1);
262 for(i=0; i<avctx->frame_size*avctx->channels; i++) {
263 put_sbits(pb, 16, *samples++);
264 }
265 } else {
266 init_sample_buffers(s, data);
267 write_frame_header(s, 0);
268 write_compressed_frame(s);
269 }
270
271 put_bits(pb, 3, 7);
272 flush_put_bits(pb);
273 out_bytes = put_bits_count(pb) >> 3;
274
275 if(out_bytes > s->max_coded_frame_size) {
276 /* frame too large. use verbatim mode */
277 if(verbatim_flag || (s->compression_level == 0)) {
278 /* still too large. must be an error. */
279 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
280 return -1;
281 }
282 verbatim_flag = 1;
283 goto verbatim;
284 }
285
286 return out_bytes;
287 }
288
176 static av_cold int alac_encode_close(AVCodecContext *avctx) 289 static av_cold int alac_encode_close(AVCodecContext *avctx)
177 { 290 {
178 AlacEncodeContext *s = avctx->priv_data;
179
180 av_freep(&avctx->extradata); 291 av_freep(&avctx->extradata);
181 avctx->extradata_size = 0; 292 avctx->extradata_size = 0;
182 av_freep(&avctx->coded_frame); 293 av_freep(&avctx->coded_frame);
183 free_sample_buffers(s);
184 return 0; 294 return 0;
185 } 295 }
186 296
187 AVCodec alac_encoder = { 297 AVCodec alac_encoder = {
188 "alac", 298 "alac",