windows support: unicode filenames
[mplayer.git] / libaf / af_lavcac3enc.c
blob8cc9884f604ae55fb414eeb8936a98d8568ab867
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>
27 #include <assert.h>
29 #include <libavcodec/avcodec.h>
30 #include <libavutil/intreadwrite.h>
32 #include "config.h"
33 #include "af.h"
34 #include "reorder_ch.h"
37 #define AC3_MAX_CHANNELS 6
38 #define AC3_MAX_CODED_FRAME_SIZE 3840
39 #define AC3_FRAME_SIZE (6 * 256)
40 const uint16_t ac3_bitrate_tab[19] = {
41 32, 40, 48, 56, 64, 80, 96, 112, 128,
42 160, 192, 224, 256, 320, 384, 448, 512, 576, 640
45 // Data for specific instances of this filter
46 typedef struct af_ac3enc_s {
47 struct AVCodec *lavc_acodec;
48 struct AVCodecContext *lavc_actx;
49 int add_iec61937_header;
50 int bit_rate;
51 int pending_data_size;
52 char *pending_data;
53 int pending_len;
54 int expect_len;
55 int min_channel_num;
56 int in_sampleformat;
57 } af_ac3enc_t;
59 // Initialization and runtime control
60 static int control(struct af_instance_s *af, int cmd, void *arg)
62 af_ac3enc_t *s = (af_ac3enc_t *)af->setup;
63 af_data_t *data = (af_data_t *)arg;
64 int i, bit_rate, test_output_res;
65 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
66 {0, 96000, 192000, 256000, 384000, 448000, 448000};
68 switch (cmd){
69 case AF_CONTROL_REINIT:
70 if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
71 return AF_DETACH;
73 af->data->format = s->in_sampleformat;
74 af->data->bps = af_fmt2bits(s->in_sampleformat) / 8;
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 test_output_res = af_test_output(af, data);
85 s->pending_len = 0;
86 s->expect_len = AC3_FRAME_SIZE * data->nch * af->data->bps;
87 assert(s->expect_len <= s->pending_data_size);
88 if (s->add_iec61937_header)
89 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
90 else
91 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
93 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
94 data->nch, data->rate, af->mul, s->expect_len);
96 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
98 if (s->lavc_actx->channels != af->data->nch ||
99 s->lavc_actx->sample_rate != af->data->rate ||
100 s->lavc_actx->bit_rate != bit_rate) {
102 avcodec_close(s->lavc_actx);
104 // Put sample parameters
105 s->lavc_actx->channels = af->data->nch;
106 s->lavc_actx->sample_rate = af->data->rate;
107 s->lavc_actx->bit_rate = bit_rate;
109 if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) {
110 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
111 return AF_ERROR;
114 if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) {
115 mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 "
116 "encoder frame size %d\n", s->lavc_actx->frame_size);
117 return AF_ERROR;
119 af->data->format = AF_FORMAT_AC3_BE;
120 af->data->bps = 2;
121 af->data->nch = 2;
122 return test_output_res;
123 case AF_CONTROL_COMMAND_LINE:
124 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
125 s->bit_rate = 0;
126 s->min_channel_num = 0;
127 s->add_iec61937_header = 0;
128 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
129 &s->min_channel_num);
130 if (s->bit_rate < 1000)
131 s->bit_rate *= 1000;
132 if (s->bit_rate) {
133 for (i = 0; i < 19; ++i)
134 if (ac3_bitrate_tab[i] * 1000 == s->bit_rate)
135 break;
136 if (i >= 19) {
137 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
138 "bitrate %d, use default bitrate (check manpage to see "
139 "supported bitrates).\n", s->bit_rate);
140 s->bit_rate = 0;
143 if (s->min_channel_num == 0)
144 s->min_channel_num = 5;
145 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
146 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
147 s->min_channel_num);
148 return AF_OK;
150 return AF_UNKNOWN;
153 // Deallocate memory
154 static void uninit(struct af_instance_s* af)
156 if (af->data)
157 free(af->data->audio);
158 free(af->data);
159 if (af->setup) {
160 af_ac3enc_t *s = af->setup;
161 af->setup = NULL;
162 if(s->lavc_actx) {
163 avcodec_close(s->lavc_actx);
164 av_free(s->lavc_actx);
166 free(s->pending_data);
167 free(s);
171 // Filter data through filter
172 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
174 af_ac3enc_t *s = af->setup;
175 af_data_t *c = data; // Current working data
176 af_data_t *l;
177 int len, left, outsize = 0, destsize;
178 char *buf, *src, *dest;
179 int max_output_len;
180 int frame_num = (data->len + s->pending_len) / s->expect_len;
181 int samplesize = af_fmt2bits(s->in_sampleformat) / 8;
183 if (s->add_iec61937_header)
184 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
185 else
186 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
188 if (af->data->len < max_output_len) {
189 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
190 "old len = %i, new len = %i\n", af->info->name, af->data->len,
191 max_output_len);
192 free(af->data->audio);
193 af->data->audio = malloc(max_output_len);
194 if (!af->data->audio) {
195 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
196 return NULL;
198 af->data->len = max_output_len;
201 l = af->data; // Local data
202 buf = (char *)l->audio;
203 src = (char *)c->audio;
204 left = c->len;
207 while (left > 0) {
208 if (left + s->pending_len < s->expect_len) {
209 memcpy(s->pending_data + s->pending_len, src, left);
210 src += left;
211 s->pending_len += left;
212 left = 0;
213 break;
216 dest = s->add_iec61937_header ? buf + 8 : buf;
217 destsize = (char *)l->audio + l->len - buf;
219 if (s->pending_len) {
220 int needs = s->expect_len - s->pending_len;
221 if (needs > 0) {
222 memcpy(s->pending_data + s->pending_len, src, needs);
223 src += needs;
224 left -= needs;
227 if (c->nch >= 5)
228 reorder_channel_nch(s->pending_data,
229 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
230 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
231 c->nch,
232 s->expect_len / samplesize, samplesize);
234 len = avcodec_encode_audio(s->lavc_actx, dest, destsize,
235 (void *)s->pending_data);
236 s->pending_len = 0;
238 else {
239 if (c->nch >= 5)
240 reorder_channel_nch(src,
241 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
242 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
243 c->nch,
244 s->expect_len / samplesize, samplesize);
245 len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src);
246 src += s->expect_len;
247 left -= s->expect_len;
249 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
250 len, s->pending_len);
252 if (s->add_iec61937_header) {
253 int bsmod = dest[5] & 0x7;
255 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
256 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
257 buf[4] = bsmod; // bsmod
258 buf[5] = 0x01; // data-type ac3
259 AV_WB16(buf + 6, 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 af->control=control;
281 af->uninit=uninit;
282 af->play=play;
283 af->mul=1;
284 af->data=calloc(1,sizeof(af_data_t));
285 af->setup=s;
287 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
288 if (!s->lavc_acodec) {
289 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
290 return AF_ERROR;
293 s->lavc_actx = avcodec_alloc_context3(s->lavc_acodec);
294 if (!s->lavc_actx) {
295 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
296 return AF_ERROR;
298 const enum AVSampleFormat *fmts = s->lavc_acodec->sample_fmts;
299 for (int i = 0; ; i++) {
300 if (fmts[i] == AV_SAMPLE_FMT_NONE) {
301 mp_msg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, encoder doesn't "
302 "support expected sample formats!\n");
303 return AF_ERROR;
304 } else if (fmts[i] == AV_SAMPLE_FMT_S16) {
305 s->in_sampleformat = AF_FORMAT_S16_NE;
306 s->lavc_actx->sample_fmt = fmts[i];
307 break;
308 } else if (fmts[i] == AV_SAMPLE_FMT_FLT) {
309 s->in_sampleformat = AF_FORMAT_FLOAT_NE;
310 s->lavc_actx->sample_fmt = fmts[i];
311 break;
314 char buf[100];
315 mp_msg(MSGT_AFILTER, MSGL_V, "[af_lavcac3enc]: in sample format: %s\n",
316 af_fmt2str(s->in_sampleformat, buf, 100));
317 s->pending_data_size = AF_NCH * AC3_FRAME_SIZE *
318 af_fmt2bits(s->in_sampleformat) / 8;
319 s->pending_data = malloc(s->pending_data_size);
321 return AF_OK;
324 af_info_t af_info_lavcac3enc = {
325 "runtime encode to ac3 using libavcodec",
326 "lavcac3enc",
327 "Ulion",
329 AF_FLAGS_REENTRANT,
330 af_open