Translation system changes part 2: replace macros by strings
[mplayer/glamo.git] / libaf / af_lavcac3enc.c
blob761debec018f25db26b85b8a3a7e86701f03698c
1 /*
2 * audio filter for runtime AC-3 encoding with libavcodec.
4 * Copyright (C) 2007 Ulion <ulion A gmail P com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
28 #include "config.h"
29 #include "af.h"
30 #include "help_mp.h"
31 #include "reorder_ch.h"
33 #include "libavcodec/avcodec.h"
34 #include "libavcodec/ac3.h"
36 // Data for specific instances of this filter
37 typedef struct af_ac3enc_s {
38 struct AVCodec *lavc_acodec;
39 struct AVCodecContext *lavc_actx;
40 int add_iec61937_header;
41 int bit_rate;
42 char *pending_data;
43 int pending_len;
44 int expect_len;
45 int min_channel_num;
46 } af_ac3enc_t;
48 extern int avcodec_initialized;
50 // Initialization and runtime control
51 static int control(struct af_instance_s *af, int cmd, void *arg)
53 af_ac3enc_t *s = (af_ac3enc_t *)af->setup;
54 af_data_t *data = (af_data_t *)arg;
55 int i, bit_rate, test_output_res;
56 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
57 {0, 96000, 192000, 256000, 384000, 448000, 448000};
59 switch (cmd){
60 case AF_CONTROL_REINIT:
61 if (data->format == AF_FORMAT_AC3 || data->nch < s->min_channel_num)
62 return AF_DETACH;
64 s->pending_len = 0;
65 s->expect_len = AC3_FRAME_SIZE * data->nch * data->bps;
66 if (s->add_iec61937_header)
67 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
68 else
69 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
71 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
72 data->nch, data->rate, af->mul, s->expect_len);
74 af->data->format = AF_FORMAT_S16_NE;
75 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
76 af->data->rate = data->rate;
77 else
78 af->data->rate = 48000;
79 if (data->nch > AC3_MAX_CHANNELS)
80 af->data->nch = AC3_MAX_CHANNELS;
81 else
82 af->data->nch = data->nch;
83 af->data->bps = 2;
84 test_output_res = af_test_output(af, data);
86 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
88 if (s->lavc_actx->channels != af->data->nch ||
89 s->lavc_actx->sample_rate != af->data->rate ||
90 s->lavc_actx->bit_rate != bit_rate) {
92 if (s->lavc_actx->codec)
93 avcodec_close(s->lavc_actx);
95 // Put sample parameters
96 s->lavc_actx->channels = af->data->nch;
97 s->lavc_actx->sample_rate = af->data->rate;
98 s->lavc_actx->bit_rate = bit_rate;
100 if(avcodec_open(s->lavc_actx, s->lavc_acodec) < 0) {
101 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
102 return AF_ERROR;
105 af->data->format = AF_FORMAT_AC3;
106 af->data->nch = 2;
107 return test_output_res;
108 case AF_CONTROL_COMMAND_LINE:
109 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
110 s->bit_rate = 0;
111 s->min_channel_num = 0;
112 s->add_iec61937_header = 0;
113 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
114 &s->min_channel_num);
115 if (s->bit_rate < 1000)
116 s->bit_rate *= 1000;
117 if (s->bit_rate) {
118 for (i = 0; i < 19; ++i)
119 if (ff_ac3_bitrate_tab[i] * 1000 == s->bit_rate)
120 break;
121 if (i >= 19) {
122 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
123 "bitrate %d, use default bitrate (check manpage to see "
124 "supported bitrates).\n", s->bit_rate);
125 s->bit_rate = 0;
128 if (s->min_channel_num == 0)
129 s->min_channel_num = 5;
130 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
131 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
132 s->min_channel_num);
133 return AF_OK;
135 return AF_UNKNOWN;
138 // Deallocate memory
139 static void uninit(struct af_instance_s* af)
141 if (af->data)
142 free(af->data->audio);
143 free(af->data);
144 if (af->setup) {
145 af_ac3enc_t *s = af->setup;
146 af->setup = NULL;
147 if(s->lavc_actx) {
148 if (s->lavc_actx->codec)
149 avcodec_close(s->lavc_actx);
150 free(s->lavc_actx);
152 free(s->pending_data);
153 free(s);
157 // Filter data through filter
158 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
160 af_ac3enc_t *s = af->setup;
161 af_data_t *c = data; // Current working data
162 af_data_t *l;
163 int len, left, outsize = 0, destsize;
164 char *buf, *src, *dest;
165 int max_output_len;
166 int frame_num = (data->len + s->pending_len) / s->expect_len;
168 if (s->add_iec61937_header)
169 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
170 else
171 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
173 if (af->data->len < max_output_len) {
174 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
175 "old len = %i, new len = %i\n", af->info->name, af->data->len,
176 max_output_len);
177 free(af->data->audio);
178 af->data->audio = malloc(max_output_len);
179 if (!af->data->audio) {
180 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
181 return NULL;
183 af->data->len = max_output_len;
186 l = af->data; // Local data
187 buf = (char *)l->audio;
188 src = (char *)c->audio;
189 left = c->len;
192 while (left > 0) {
193 if (left + s->pending_len < s->expect_len) {
194 memcpy(s->pending_data + s->pending_len, src, left);
195 src += left;
196 s->pending_len += left;
197 left = 0;
198 break;
201 dest = s->add_iec61937_header ? buf + 8 : buf;
202 destsize = (char *)l->audio + l->len - buf;
204 if (s->pending_len) {
205 int needs = s->expect_len - s->pending_len;
206 if (needs > 0) {
207 memcpy(s->pending_data + s->pending_len, src, needs);
208 src += needs;
209 left -= needs;
212 if (c->nch >= 5)
213 reorder_channel_nch(s->pending_data,
214 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
215 AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT,
216 c->nch,
217 s->expect_len / 2, 2);
219 len = avcodec_encode_audio(s->lavc_actx, dest, destsize,
220 (void *)s->pending_data);
221 s->pending_len = 0;
223 else {
224 if (c->nch >= 5)
225 reorder_channel_nch(src,
226 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
227 AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT,
228 c->nch,
229 s->expect_len / 2, 2);
230 len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src);
231 src += s->expect_len;
232 left -= s->expect_len;
234 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
235 len, s->pending_len);
237 if (s->add_iec61937_header) {
238 int16_t *out = (int16_t *)buf;
239 int bsmod = dest[5] & 0x7;
241 #ifndef WORDS_BIGENDIAN
242 int i;
243 char tmp;
244 for (i = 0; i < len; i += 2) {
245 tmp = dest[i];
246 dest[i] = dest[i+1];
247 dest[i+1] = tmp;
249 if (len & 1) {
250 dest[len] = dest[len-1];
251 dest[len-1] = 0;
252 len++;
254 #endif
255 out[0] = 0xF872; // iec 61937 syncword 1
256 out[1] = 0x4E1F; // iec 61937 syncword 2
257 out[2] = 0x0001; // data-type ac3
258 out[2] |= bsmod << 8; // bsmod
259 out[3] = len << 3; // number of bits in payload
261 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
262 len = AC3_FRAME_SIZE * 2 * 2;
265 outsize += len;
266 buf += len;
268 c->audio = l->audio;
269 c->nch = 2;
270 c->bps = 2;
271 c->len = outsize;
272 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
273 outsize, s->pending_len);
274 return c;
277 static int af_open(af_instance_t* af){
279 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
280 int pending_space = 2 * AC3_MAX_CHANNELS * AC3_FRAME_SIZE;
281 s->pending_data = calloc(pending_space, sizeof(char));
283 af->control=control;
284 af->uninit=uninit;
285 af->play=play;
286 af->mul=1;
287 af->data=calloc(1,sizeof(af_data_t));
288 af->setup=s;
290 if (!avcodec_initialized){
291 avcodec_init();
292 avcodec_register_all();
293 avcodec_initialized=1;
296 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
297 if (!s->lavc_acodec) {
298 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
299 return AF_ERROR;
302 s->lavc_actx = avcodec_alloc_context();
303 if (!s->lavc_actx) {
304 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
305 return AF_ERROR;
308 return AF_OK;
311 af_info_t af_info_lavcac3enc = {
312 "runtime encode to ac3 using libavcodec",
313 "lavcac3enc",
314 "Ulion",
316 AF_FLAGS_REENTRANT,
317 af_open