configure: Attempt to fix compilation with FreeBSD's dvdio.h
[mplayer/glamo.git] / libaf / af_lavcac3enc.c
blob10a9eb58c8528740420168b7bacb919741f9970c
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 "config.h"
30 #include "af.h"
31 #include "reorder_ch.h"
33 #include "libavcodec/avcodec.h"
34 #include "ffmpeg_files/intreadwrite.h"
36 #define AC3_MAX_CHANNELS 6
37 #define AC3_MAX_CODED_FRAME_SIZE 3840
38 #define AC3_FRAME_SIZE (6 * 256)
39 const uint16_t ac3_bitrate_tab[19] = {
40 32, 40, 48, 56, 64, 80, 96, 112, 128,
41 160, 192, 224, 256, 320, 384, 448, 512, 576, 640
44 // Data for specific instances of this filter
45 typedef struct af_ac3enc_s {
46 struct AVCodec *lavc_acodec;
47 struct AVCodecContext *lavc_actx;
48 int add_iec61937_header;
49 int bit_rate;
50 int pending_data_size;
51 char *pending_data;
52 int pending_len;
53 int expect_len;
54 int min_channel_num;
55 } af_ac3enc_t;
57 extern int avcodec_initialized;
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 = AF_FORMAT_S16_NE;
74 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
75 af->data->rate = data->rate;
76 else
77 af->data->rate = 48000;
78 if (data->nch > AC3_MAX_CHANNELS)
79 af->data->nch = AC3_MAX_CHANNELS;
80 else
81 af->data->nch = data->nch;
82 af->data->bps = 2;
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 if (s->lavc_actx->codec)
103 avcodec_close(s->lavc_actx);
105 // Put sample parameters
106 s->lavc_actx->channels = af->data->nch;
107 s->lavc_actx->sample_rate = af->data->rate;
108 s->lavc_actx->bit_rate = bit_rate;
110 if(avcodec_open(s->lavc_actx, s->lavc_acodec) < 0) {
111 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
112 return AF_ERROR;
115 if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) {
116 mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 "
117 "encoder frame size %d\n", s->lavc_actx->frame_size);
118 return AF_ERROR;
120 af->data->format = AF_FORMAT_AC3_BE;
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 if (s->lavc_actx->codec)
164 avcodec_close(s->lavc_actx);
165 free(s->lavc_actx);
167 free(s->pending_data);
168 free(s);
172 // Filter data through filter
173 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
175 af_ac3enc_t *s = af->setup;
176 af_data_t *c = data; // Current working data
177 af_data_t *l;
178 int len, left, outsize = 0, destsize;
179 char *buf, *src, *dest;
180 int max_output_len;
181 int frame_num = (data->len + s->pending_len) / s->expect_len;
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 / 2, 2);
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 / 2, 2);
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 s->pending_data_size = 2 * AF_NCH * AC3_FRAME_SIZE;
281 s->pending_data = malloc(s->pending_data_size);
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