annotate alacenc.c @ 7618:2b023daf5329 libavcodec

alacenc: Use user-specified min and max prediction order.
author ramiro
date Tue, 19 Aug 2008 16:12:05 +0000
parents 924dc060db81
children 0753d03d232a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
1 /**
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
2 * ALAC audio encoder
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
4 *
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
5 * This file is part of FFmpeg.
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
6 *
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
11 *
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
15 * Lesser General Public License for more details.
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
16 *
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
20 */
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
21
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
22 #include "avcodec.h"
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
23 #include "bitstream.h"
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
24 #include "dsputil.h"
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
25 #include "lpc.h"
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
26
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
27 #define DEFAULT_FRAME_SIZE 4096
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
28 #define DEFAULT_SAMPLE_SIZE 16
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
29 #define MAX_CHANNELS 8
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
30 #define ALAC_EXTRADATA_SIZE 36
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
31 #define ALAC_FRAME_HEADER_SIZE 55
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
32 #define ALAC_FRAME_FOOTER_SIZE 3
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
33
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
34 #define ALAC_ESCAPE_CODE 0x1FF
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
35 #define ALAC_MAX_LPC_ORDER 30
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
36 #define DEFAULT_MAX_PRED_ORDER 6
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
37 #define DEFAULT_MIN_PRED_ORDER 4
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
38 #define ALAC_MAX_LPC_PRECISION 9
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
39 #define ALAC_MAX_LPC_SHIFT 9
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
40
7617
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
41 #define ALAC_CHMODE_LEFT_RIGHT 0
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
42 #define ALAC_CHMODE_LEFT_SIDE 1
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
43 #define ALAC_CHMODE_RIGHT_SIDE 2
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
44 #define ALAC_CHMODE_MID_SIDE 3
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
45
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
46 typedef struct RiceContext {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
47 int history_mult;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
48 int initial_history;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
49 int k_modifier;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
50 int rice_modifier;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
51 } RiceContext;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
52
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
53 typedef struct LPCContext {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
54 int lpc_order;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
55 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
56 int lpc_quant;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
57 } LPCContext;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
58
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
59 typedef struct AlacEncodeContext {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
60 int compression_level;
7617
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
61 int min_prediction_order;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
62 int max_prediction_order;
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
63 int max_coded_frame_size;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
64 int write_sample_size;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
65 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
66 int32_t predictor_buf[DEFAULT_FRAME_SIZE];
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
67 int interlacing_shift;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
68 int interlacing_leftweight;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
69 PutBitContext pbctx;
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
70 RiceContext rc;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
71 LPCContext lpc[MAX_CHANNELS];
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
72 DSPContext dspctx;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
73 AVCodecContext *avctx;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
74 } AlacEncodeContext;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
75
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
76
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
77 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
78 {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
79 int ch, i;
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 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
82 int16_t *sptr = input_samples + ch;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
83 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
84 s->sample_buf[ch][i] = *sptr;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
85 sptr += s->avctx->channels;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
86 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
87 }
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
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
90 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
91 {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
92 int divisor, q, r;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
93
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
94 k = FFMIN(k, s->rc.k_modifier);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
95 divisor = (1<<k) - 1;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
96 q = x / divisor;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
97 r = x % divisor;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
98
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
99 if(q > 8) {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
100 // write escape code and sample value directly
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
101 put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
102 put_bits(&s->pbctx, write_sample_size, x);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
103 } else {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
104 if(q)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
105 put_bits(&s->pbctx, q, (1<<q) - 1);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
106 put_bits(&s->pbctx, 1, 0);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
107
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
108 if(k != 1) {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
109 if(r > 0)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
110 put_bits(&s->pbctx, k, r+1);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
111 else
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
112 put_bits(&s->pbctx, k-1, 0);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
113 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
114 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
115 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
116
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
117 static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
118 {
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
119 put_bits(&s->pbctx, 3, s->avctx->channels-1); // No. of channels -1
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
120 put_bits(&s->pbctx, 16, 0); // Seems to be zero
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
121 put_bits(&s->pbctx, 1, 1); // Sample count is in the header
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
122 put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
123 put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
124 put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of samples in the frame
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
125 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
126
7617
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
127 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
128 {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
129 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
130 int shift[MAX_LPC_ORDER];
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
131 int opt_order;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
132
7618
2b023daf5329 alacenc: Use user-specified min and max prediction order.
ramiro
parents: 7617
diff changeset
133 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
134 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
135
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
136 s->lpc[ch].lpc_order = opt_order;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
137 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
138 memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
139 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
140
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
141 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
142 {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
143 int i, best;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
144 int32_t lt, rt;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
145 uint64_t sum[4];
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
146 uint64_t score[4];
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
147
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
148 /* 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
149 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
150 for(i=2; i<n; i++) {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
151 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
152 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
153 sum[2] += FFABS((lt + rt) >> 1);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
154 sum[3] += FFABS(lt - rt);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
155 sum[0] += FFABS(lt);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
156 sum[1] += FFABS(rt);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
157 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
158
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
159 /* calculate score for each mode */
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
160 score[0] = sum[0] + sum[1];
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
161 score[1] = sum[0] + sum[3];
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
162 score[2] = sum[1] + sum[3];
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
163 score[3] = sum[2] + sum[3];
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
164
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
165 /* return mode with lowest score */
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
166 best = 0;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
167 for(i=1; i<4; i++) {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
168 if(score[i] < score[best]) {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
169 best = i;
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 }
7617
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
172 return best;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
173 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
174
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
175 static void alac_stereo_decorrelation(AlacEncodeContext *s)
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
176 {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
177 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
178 int i, mode, n = s->avctx->frame_size;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
179 int32_t tmp;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
180
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
181 mode = estimate_stereo_mode(left, right, n);
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
182
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
183 switch(mode)
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
184 {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
185 case ALAC_CHMODE_LEFT_RIGHT:
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
186 s->interlacing_leftweight = 0;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
187 s->interlacing_shift = 0;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
188 break;
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 case ALAC_CHMODE_LEFT_SIDE:
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
191 for(i=0; i<n; i++) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
192 right[i] = left[i] - right[i];
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 s->interlacing_leftweight = 1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
195 s->interlacing_shift = 0;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
196 break;
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_RIGHT_SIDE:
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
199 for(i=0; i<n; i++) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
200 tmp = right[i];
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
201 right[i] = left[i] - right[i];
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
202 left[i] = tmp + (right[i] >> 31);
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
203 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
204 s->interlacing_leftweight = 1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
205 s->interlacing_shift = 31;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
206 break;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
207
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
208 default:
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
209 for(i=0; i<n; i++) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
210 tmp = left[i];
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
211 left[i] = (tmp + right[i]) >> 1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
212 right[i] = tmp - right[i];
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
213 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
214 s->interlacing_leftweight = 1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
215 s->interlacing_shift = 1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
216 break;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
217 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
218 }
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
219
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
220 static void write_compressed_frame(AlacEncodeContext *s)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
221 {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
222 int i, j;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
223
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
224 /* only simple mid/side decorrelation supported as of now */
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
225 alac_stereo_decorrelation(s);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
226 put_bits(&s->pbctx, 8, s->interlacing_shift);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
227 put_bits(&s->pbctx, 8, s->interlacing_leftweight);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
228
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
229 for(i=0;i<s->avctx->channels;i++) {
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
230
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
231 calc_predictor_params(s, i);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
232
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
233 put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
234 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
235
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
236 put_bits(&s->pbctx, 3, s->rc.rice_modifier);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
237 put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
238 // predictor coeff. table
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
239 for(j=0;j<s->lpc[i].lpc_order;j++) {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
240 put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
241 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
242 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
243
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
244 // apply lpc and entropy coding to audio samples
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
245
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
246 for(i=0;i<s->avctx->channels;i++) {
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
247 alac_linear_predictor(s, i);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
248 alac_entropy_coder(s);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
249 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
250 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
251
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
252 static av_cold int alac_encode_init(AVCodecContext *avctx)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
253 {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
254 AlacEncodeContext *s = avctx->priv_data;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
255 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
256
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
257 avctx->frame_size = DEFAULT_FRAME_SIZE;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
258 avctx->bits_per_sample = DEFAULT_SAMPLE_SIZE;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
259
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
260 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
261 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
262 return -1;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
263 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
264
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
265 // Set default compression level
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
266 if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
267 s->compression_level = 1;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
268 else
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
269 s->compression_level = av_clip(avctx->compression_level, 0, 1);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
270
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
271 // Initialize default Rice parameters
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
272 s->rc.history_mult = 40;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
273 s->rc.initial_history = 10;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
274 s->rc.k_modifier = 14;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
275 s->rc.rice_modifier = 4;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
276
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
277 s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE +
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
278 avctx->frame_size*avctx->channels*avctx->bits_per_sample)>>3;
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
279
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
280 s->write_sample_size = avctx->bits_per_sample + avctx->channels - 1; // FIXME: consider wasted_bytes
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
281
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
282 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
283 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
284 AV_WB32(alac_extradata+12, avctx->frame_size);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
285 AV_WB8 (alac_extradata+17, avctx->bits_per_sample);
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
286 AV_WB8 (alac_extradata+21, avctx->channels);
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
287 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
288 AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_sample); // average bitrate
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
289 AV_WB32(alac_extradata+32, avctx->sample_rate);
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
290
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
291 // Set relevant extradata fields
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
292 if(s->compression_level > 0) {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
293 AV_WB8(alac_extradata+18, s->rc.history_mult);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
294 AV_WB8(alac_extradata+19, s->rc.initial_history);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
295 AV_WB8(alac_extradata+20, s->rc.k_modifier);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
296 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
297
7617
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
298 if(avctx->min_prediction_order >= 0) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
299 if(avctx->min_prediction_order < MIN_LPC_ORDER ||
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
300 avctx->min_prediction_order > MAX_LPC_ORDER) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
301 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
302 return -1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
303 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
304
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
305 s->min_prediction_order = avctx->min_prediction_order;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
306 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
307
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
308 if(avctx->max_prediction_order >= 0) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
309 if(avctx->max_prediction_order < MIN_LPC_ORDER ||
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
310 avctx->max_prediction_order > MAX_LPC_ORDER) {
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
311 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
312 return -1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
313 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
314
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
315 s->max_prediction_order = avctx->max_prediction_order;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
316 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
317
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
318 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
319 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
320 s->min_prediction_order, s->max_prediction_order);
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
321 return -1;
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
322 }
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
323
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
324 avctx->extradata = alac_extradata;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
325 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
326
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
327 avctx->coded_frame = avcodec_alloc_frame();
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
328 avctx->coded_frame->key_frame = 1;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
329
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
330 s->avctx = avctx;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
331 dsputil_init(&s->dspctx, avctx);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
332
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
333 return 0;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
334 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
335
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
336 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
337 int buf_size, void *data)
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
338 {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
339 AlacEncodeContext *s = avctx->priv_data;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
340 PutBitContext *pb = &s->pbctx;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
341 int i, out_bytes, verbatim_flag = 0;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
342
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
343 if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
344 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
345 return -1;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
346 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
347
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
348 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
349 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
350 return -1;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
351 }
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
352
7617
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
353 verbatim:
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
354 init_put_bits(pb, frame, buf_size);
924dc060db81 Import more OKed parts of ALAC encoder from GSoC repo.
ramiro
parents: 7604
diff changeset
355
7604
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
356 if((s->compression_level == 0) || verbatim_flag) {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
357 // Verbatim mode
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
358 int16_t *samples = data;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
359 write_frame_header(s, 1);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
360 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
361 put_sbits(pb, 16, *samples++);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
362 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
363 } else {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
364 init_sample_buffers(s, data);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
365 write_frame_header(s, 0);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
366 write_compressed_frame(s);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
367 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
368
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
369 put_bits(pb, 3, 7);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
370 flush_put_bits(pb);
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
371 out_bytes = put_bits_count(pb) >> 3;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
372
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
373 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
374 /* frame too large. use verbatim mode */
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
375 if(verbatim_flag || (s->compression_level == 0)) {
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
376 /* still too large. must be an error. */
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
377 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
378 return -1;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
379 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
380 verbatim_flag = 1;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
381 goto verbatim;
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
382 }
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
383
6250ff63990b Import more ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents: 7598
diff changeset
384 return out_bytes;
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
385 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
386
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
387 static av_cold int alac_encode_close(AVCodecContext *avctx)
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
388 {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
389 av_freep(&avctx->extradata);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
390 avctx->extradata_size = 0;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
391 av_freep(&avctx->coded_frame);
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
392 return 0;
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
393 }
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
394
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
395 AVCodec alac_encoder = {
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
396 "alac",
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
397 CODEC_TYPE_AUDIO,
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
398 CODEC_ID_ALAC,
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
399 sizeof(AlacEncodeContext),
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
400 alac_encode_init,
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
401 alac_encode_frame,
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
402 alac_encode_close,
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
403 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
7598
60dd0089cdba alacenc: NULL_IF_CONFIG_SMALL long_name.
ramiro
parents: 7595
diff changeset
404 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
7595
30af3a9775de Import ok'd parts of ALAC encoder from GSoC repo.
ramiro
parents:
diff changeset
405 };