Merge svn changes up to r30484
[mplayer/kovensky.git] / libaf / af_lavcac3enc.c
bloba86b7fbdbdceda6fdcd2162ff5ec2de35f322b81
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"
35 #include "libavutil/intreadwrite.h"
37 // Data for specific instances of this filter
38 typedef struct af_ac3enc_s {
39 struct AVCodec *lavc_acodec;
40 struct AVCodecContext *lavc_actx;
41 int add_iec61937_header;
42 int bit_rate;
43 char *pending_data;
44 int pending_len;
45 int expect_len;
46 int min_channel_num;
47 } af_ac3enc_t;
49 extern int avcodec_initialized;
51 // Initialization and runtime control
52 static int control(struct af_instance_s *af, int cmd, void *arg)
54 af_ac3enc_t *s = (af_ac3enc_t *)af->setup;
55 af_data_t *data = (af_data_t *)arg;
56 int i, bit_rate, test_output_res;
57 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
58 {0, 96000, 192000, 256000, 384000, 448000, 448000};
60 switch (cmd){
61 case AF_CONTROL_REINIT:
62 if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
63 return AF_DETACH;
65 s->pending_len = 0;
66 s->expect_len = AC3_FRAME_SIZE * data->nch * data->bps;
67 if (s->add_iec61937_header)
68 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
69 else
70 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
72 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
73 data->nch, data->rate, af->mul, s->expect_len);
75 af->data->format = AF_FORMAT_S16_NE;
76 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
77 af->data->rate = data->rate;
78 else
79 af->data->rate = 48000;
80 if (data->nch > AC3_MAX_CHANNELS)
81 af->data->nch = AC3_MAX_CHANNELS;
82 else
83 af->data->nch = data->nch;
84 af->data->bps = 2;
85 test_output_res = af_test_output(af, data);
87 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
89 if (s->lavc_actx->channels != af->data->nch ||
90 s->lavc_actx->sample_rate != af->data->rate ||
91 s->lavc_actx->bit_rate != bit_rate) {
93 if (s->lavc_actx->codec)
94 avcodec_close(s->lavc_actx);
96 // Put sample parameters
97 s->lavc_actx->channels = af->data->nch;
98 s->lavc_actx->sample_rate = af->data->rate;
99 s->lavc_actx->bit_rate = bit_rate;
101 if(avcodec_open(s->lavc_actx, s->lavc_acodec) < 0) {
102 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
103 return AF_ERROR;
106 af->data->format = AF_FORMAT_AC3_BE;
107 af->data->nch = 2;
108 return test_output_res;
109 case AF_CONTROL_COMMAND_LINE:
110 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
111 s->bit_rate = 0;
112 s->min_channel_num = 0;
113 s->add_iec61937_header = 0;
114 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
115 &s->min_channel_num);
116 if (s->bit_rate < 1000)
117 s->bit_rate *= 1000;
118 if (s->bit_rate) {
119 for (i = 0; i < 19; ++i)
120 if (ff_ac3_bitrate_tab[i] * 1000 == s->bit_rate)
121 break;
122 if (i >= 19) {
123 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
124 "bitrate %d, use default bitrate (check manpage to see "
125 "supported bitrates).\n", s->bit_rate);
126 s->bit_rate = 0;
129 if (s->min_channel_num == 0)
130 s->min_channel_num = 5;
131 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
132 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
133 s->min_channel_num);
134 return AF_OK;
136 return AF_UNKNOWN;
139 // Deallocate memory
140 static void uninit(struct af_instance_s* af)
142 if (af->data)
143 free(af->data->audio);
144 free(af->data);
145 if (af->setup) {
146 af_ac3enc_t *s = af->setup;
147 af->setup = NULL;
148 if(s->lavc_actx) {
149 if (s->lavc_actx->codec)
150 avcodec_close(s->lavc_actx);
151 free(s->lavc_actx);
153 free(s->pending_data);
154 free(s);
158 // Filter data through filter
159 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
161 af_ac3enc_t *s = af->setup;
162 af_data_t *c = data; // Current working data
163 af_data_t *l;
164 int len, left, outsize = 0, destsize;
165 char *buf, *src, *dest;
166 int max_output_len;
167 int frame_num = (data->len + s->pending_len) / s->expect_len;
169 if (s->add_iec61937_header)
170 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
171 else
172 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
174 if (af->data->len < max_output_len) {
175 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
176 "old len = %i, new len = %i\n", af->info->name, af->data->len,
177 max_output_len);
178 free(af->data->audio);
179 af->data->audio = malloc(max_output_len);
180 if (!af->data->audio) {
181 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
182 return NULL;
184 af->data->len = max_output_len;
187 l = af->data; // Local data
188 buf = (char *)l->audio;
189 src = (char *)c->audio;
190 left = c->len;
193 while (left > 0) {
194 if (left + s->pending_len < s->expect_len) {
195 memcpy(s->pending_data + s->pending_len, src, left);
196 src += left;
197 s->pending_len += left;
198 left = 0;
199 break;
202 dest = s->add_iec61937_header ? buf + 8 : buf;
203 destsize = (char *)l->audio + l->len - buf;
205 if (s->pending_len) {
206 int needs = s->expect_len - s->pending_len;
207 if (needs > 0) {
208 memcpy(s->pending_data + s->pending_len, src, needs);
209 src += needs;
210 left -= needs;
213 if (c->nch >= 5)
214 reorder_channel_nch(s->pending_data,
215 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
216 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
217 c->nch,
218 s->expect_len / 2, 2);
220 len = avcodec_encode_audio(s->lavc_actx, dest, destsize,
221 (void *)s->pending_data);
222 s->pending_len = 0;
224 else {
225 if (c->nch >= 5)
226 reorder_channel_nch(src,
227 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
228 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
229 c->nch,
230 s->expect_len / 2, 2);
231 len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src);
232 src += s->expect_len;
233 left -= s->expect_len;
235 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
236 len, s->pending_len);
238 if (s->add_iec61937_header) {
239 int bsmod = dest[5] & 0x7;
241 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
242 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
243 buf[4] = bsmod; // bsmod
244 buf[5] = 0x01; // data-type ac3
245 AV_WB16(buf + 6, len << 3); // number of bits in payload
247 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
248 len = AC3_FRAME_SIZE * 2 * 2;
251 outsize += len;
252 buf += len;
254 c->audio = l->audio;
255 c->nch = 2;
256 c->bps = 2;
257 c->len = outsize;
258 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
259 outsize, s->pending_len);
260 return c;
263 static int af_open(af_instance_t* af){
265 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
266 int pending_space = 2 * AC3_MAX_CHANNELS * AC3_FRAME_SIZE;
267 s->pending_data = calloc(pending_space, sizeof(char));
269 af->control=control;
270 af->uninit=uninit;
271 af->play=play;
272 af->mul=1;
273 af->data=calloc(1,sizeof(af_data_t));
274 af->setup=s;
276 if (!avcodec_initialized){
277 avcodec_init();
278 avcodec_register_all();
279 avcodec_initialized=1;
282 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
283 if (!s->lavc_acodec) {
284 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
285 return AF_ERROR;
288 s->lavc_actx = avcodec_alloc_context();
289 if (!s->lavc_actx) {
290 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
291 return AF_ERROR;
294 return AF_OK;
297 af_info_t af_info_lavcac3enc = {
298 "runtime encode to ac3 using libavcodec",
299 "lavcac3enc",
300 "Ulion",
302 AF_FLAGS_REENTRANT,
303 af_open