commands: change property mechanism to use talloc strings
[mplayer.git] / libaf / af_lavcac3enc.c
blob1042f3691ea2095c9d8262f22185b775728328d9
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 "libmpcodecs/vd_ffmpeg.h"
30 #include "config.h"
31 #include "af.h"
32 #include "reorder_ch.h"
34 #include "libavcodec/avcodec.h"
35 #include "ffmpeg_files/intreadwrite.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 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->bps = 2;
122 af->data->nch = 2;
123 return test_output_res;
124 case AF_CONTROL_COMMAND_LINE:
125 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
126 s->bit_rate = 0;
127 s->min_channel_num = 0;
128 s->add_iec61937_header = 0;
129 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
130 &s->min_channel_num);
131 if (s->bit_rate < 1000)
132 s->bit_rate *= 1000;
133 if (s->bit_rate) {
134 for (i = 0; i < 19; ++i)
135 if (ac3_bitrate_tab[i] * 1000 == s->bit_rate)
136 break;
137 if (i >= 19) {
138 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
139 "bitrate %d, use default bitrate (check manpage to see "
140 "supported bitrates).\n", s->bit_rate);
141 s->bit_rate = 0;
144 if (s->min_channel_num == 0)
145 s->min_channel_num = 5;
146 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
147 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
148 s->min_channel_num);
149 return AF_OK;
151 return AF_UNKNOWN;
154 // Deallocate memory
155 static void uninit(struct af_instance_s* af)
157 if (af->data)
158 free(af->data->audio);
159 free(af->data);
160 if (af->setup) {
161 af_ac3enc_t *s = af->setup;
162 af->setup = NULL;
163 if(s->lavc_actx) {
164 if (s->lavc_actx->codec)
165 avcodec_close(s->lavc_actx);
166 free(s->lavc_actx);
168 free(s->pending_data);
169 free(s);
173 // Filter data through filter
174 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
176 af_ac3enc_t *s = af->setup;
177 af_data_t *c = data; // Current working data
178 af_data_t *l;
179 int len, left, outsize = 0, destsize;
180 char *buf, *src, *dest;
181 int max_output_len;
182 int frame_num = (data->len + s->pending_len) / s->expect_len;
183 int samplesize = af_fmt2bits(s->in_sampleformat) / 8;
185 if (s->add_iec61937_header)
186 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
187 else
188 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
190 if (af->data->len < max_output_len) {
191 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
192 "old len = %i, new len = %i\n", af->info->name, af->data->len,
193 max_output_len);
194 free(af->data->audio);
195 af->data->audio = malloc(max_output_len);
196 if (!af->data->audio) {
197 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
198 return NULL;
200 af->data->len = max_output_len;
203 l = af->data; // Local data
204 buf = (char *)l->audio;
205 src = (char *)c->audio;
206 left = c->len;
209 while (left > 0) {
210 if (left + s->pending_len < s->expect_len) {
211 memcpy(s->pending_data + s->pending_len, src, left);
212 src += left;
213 s->pending_len += left;
214 left = 0;
215 break;
218 dest = s->add_iec61937_header ? buf + 8 : buf;
219 destsize = (char *)l->audio + l->len - buf;
221 if (s->pending_len) {
222 int needs = s->expect_len - s->pending_len;
223 if (needs > 0) {
224 memcpy(s->pending_data + s->pending_len, src, needs);
225 src += needs;
226 left -= needs;
229 if (c->nch >= 5)
230 reorder_channel_nch(s->pending_data,
231 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
232 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
233 c->nch,
234 s->expect_len / samplesize, samplesize);
236 len = avcodec_encode_audio(s->lavc_actx, dest, destsize,
237 (void *)s->pending_data);
238 s->pending_len = 0;
240 else {
241 if (c->nch >= 5)
242 reorder_channel_nch(src,
243 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
244 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
245 c->nch,
246 s->expect_len / samplesize, samplesize);
247 len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src);
248 src += s->expect_len;
249 left -= s->expect_len;
251 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
252 len, s->pending_len);
254 if (s->add_iec61937_header) {
255 int bsmod = dest[5] & 0x7;
257 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
258 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
259 buf[4] = bsmod; // bsmod
260 buf[5] = 0x01; // data-type ac3
261 AV_WB16(buf + 6, len << 3); // number of bits in payload
263 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
264 len = AC3_FRAME_SIZE * 2 * 2;
267 outsize += len;
268 buf += len;
270 c->audio = l->audio;
271 c->nch = 2;
272 c->bps = 2;
273 c->len = outsize;
274 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
275 outsize, s->pending_len);
276 return c;
279 static int af_open(af_instance_t* af){
281 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
282 af->control=control;
283 af->uninit=uninit;
284 af->play=play;
285 af->mul=1;
286 af->data=calloc(1,sizeof(af_data_t));
287 af->setup=s;
289 init_avcodec();
291 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
292 if (!s->lavc_acodec) {
293 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
294 return AF_ERROR;
297 s->lavc_actx = avcodec_alloc_context();
298 if (!s->lavc_actx) {
299 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
300 return AF_ERROR;
302 // using deprecated SampleFormat/FMT, AV* versions only added in 2010-11
303 const enum SampleFormat *fmts = s->lavc_acodec->sample_fmts;
304 for (int i = 0; ; i++) {
305 if (fmts[i] == SAMPLE_FMT_NONE) {
306 mp_msg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, encoder doesn't "
307 "support expected sample formats!\n");
308 return AF_ERROR;
309 } else if (fmts[i] == SAMPLE_FMT_S16) {
310 s->in_sampleformat = AF_FORMAT_S16_NE;
311 s->lavc_actx->sample_fmt = fmts[i];
312 break;
313 } else if (fmts[i] == SAMPLE_FMT_FLT) {
314 s->in_sampleformat = AF_FORMAT_FLOAT_NE;
315 s->lavc_actx->sample_fmt = fmts[i];
316 break;
319 char buf[100];
320 mp_msg(MSGT_AFILTER, MSGL_V, "[af_lavcac3enc]: in sample format: %s\n",
321 af_fmt2str(s->in_sampleformat, buf, 100));
322 s->pending_data_size = AF_NCH * AC3_FRAME_SIZE *
323 af_fmt2bits(s->in_sampleformat) / 8;
324 s->pending_data = malloc(s->pending_data_size);
326 return AF_OK;
329 af_info_t af_info_lavcac3enc = {
330 "runtime encode to ac3 using libavcodec",
331 "lavcac3enc",
332 "Ulion",
334 AF_FLAGS_REENTRANT,
335 af_open