mixer: fix lowering hw volume while muted
[mplayer.git] / libaf / af_lavcac3enc.c
blob2aa69e6b3658bf7e4ca6ff886166c61c363514ed
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/audioconvert.h>
31 #include <libavutil/error.h>
32 #include <libavutil/intreadwrite.h>
33 #include <libavutil/mem.h>
34 #include <libavutil/opt.h>
35 #include <libavutil/samplefmt.h>
37 #include "config.h"
38 #include "af.h"
39 #include "reorder_ch.h"
41 #ifdef CONFIG_LIBAVRESAMPLE
42 #include <libavresample/avresample.h>
43 #endif
45 #define AC3_MAX_CHANNELS 6
46 #define AC3_MAX_CODED_FRAME_SIZE 3840
47 #define AC3_FRAME_SIZE (6 * 256)
48 const uint16_t ac3_bitrate_tab[19] = {
49 32, 40, 48, 56, 64, 80, 96, 112, 128,
50 160, 192, 224, 256, 320, 384, 448, 512, 576, 640
53 // Data for specific instances of this filter
54 typedef struct af_ac3enc_s {
55 #ifdef CONFIG_LIBAVRESAMPLE
56 AVAudioResampleContext *avr;
57 uint8_t *resample_buf[AC3_MAX_CHANNELS];
58 int linesize;
59 #endif
60 AVFrame *frame;
61 struct AVCodec *lavc_acodec;
62 struct AVCodecContext *lavc_actx;
63 int add_iec61937_header;
64 int bit_rate;
65 int pending_data_size;
66 char *pending_data;
67 int pending_len;
68 int expect_len;
69 int min_channel_num;
70 int in_sampleformat;
71 } af_ac3enc_t;
73 // Initialization and runtime control
74 static int control(struct af_instance_s *af, int cmd, void *arg)
76 af_ac3enc_t *s = (af_ac3enc_t *)af->setup;
77 af_data_t *data = (af_data_t *)arg;
78 int i, bit_rate, test_output_res;
79 static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
80 {0, 96000, 192000, 256000, 384000, 448000, 448000};
82 switch (cmd){
83 case AF_CONTROL_REINIT:
84 if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
85 return AF_DETACH;
87 af->data->format = s->in_sampleformat;
88 af->data->bps = af_fmt2bits(s->in_sampleformat) / 8;
89 if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
90 af->data->rate = data->rate;
91 else
92 af->data->rate = 48000;
93 if (data->nch > AC3_MAX_CHANNELS)
94 af->data->nch = AC3_MAX_CHANNELS;
95 else
96 af->data->nch = data->nch;
97 test_output_res = af_test_output(af, data);
99 s->pending_len = 0;
100 s->expect_len = AC3_FRAME_SIZE * data->nch * af->data->bps;
101 assert(s->expect_len <= s->pending_data_size);
102 if (s->add_iec61937_header)
103 af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
104 else
105 af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
107 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
108 data->nch, data->rate, af->mul, s->expect_len);
110 bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
112 if (s->lavc_actx->channels != af->data->nch ||
113 s->lavc_actx->sample_rate != af->data->rate ||
114 s->lavc_actx->bit_rate != bit_rate) {
116 avcodec_close(s->lavc_actx);
118 #ifdef CONFIG_LIBAVRESAMPLE
119 if (s->avr) {
120 uint64_t ch_layout =
121 av_get_default_channel_layout(af->data->nch);
122 enum AVSampleFormat in_sample_fmt =
123 av_get_packed_sample_fmt(s->lavc_actx->sample_fmt);
124 int ret;
126 avresample_close(s->avr);
128 if (af->data->nch != s->lavc_actx->channels) {
129 av_freep(&s->resample_buf[0]);
130 ret = av_samples_alloc(s->resample_buf, &s->linesize,
131 af->data->nch, AC3_FRAME_SIZE,
132 s->lavc_actx->sample_fmt, 0);
133 if (ret < 0) {
134 uint8_t error[128];
135 av_strerror(ret, error, sizeof(error));
136 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error allocating "
137 "resample buffer: %s\n", error);
138 return AF_ERROR;
142 av_opt_set_int(s->avr, "in_channel_layout", ch_layout, 0);
143 av_opt_set_int(s->avr, "out_channel_layout", ch_layout, 0);
144 av_opt_set_int(s->avr, "in_sample_rate", af->data->rate, 0);
145 av_opt_set_int(s->avr, "out_sample_rate", af->data->rate, 0);
146 av_opt_set_int(s->avr, "in_sample_fmt", in_sample_fmt, 0);
147 av_opt_set_int(s->avr, "out_sample_fmt", s->lavc_actx->sample_fmt, 0);
149 if ((ret = avresample_open(s->avr)) < 0) {
150 uint8_t error[128];
151 av_strerror(ret, error, sizeof(error));
152 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error configuring "
153 "libavresample: %s\n", error);
154 return AF_ERROR;
157 #endif
159 // Put sample parameters
160 s->lavc_actx->channels = af->data->nch;
161 s->lavc_actx->sample_rate = af->data->rate;
162 s->lavc_actx->bit_rate = bit_rate;
164 if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) {
165 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
166 return AF_ERROR;
169 if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) {
170 mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 "
171 "encoder frame size %d\n", s->lavc_actx->frame_size);
172 return AF_ERROR;
174 af->data->format = AF_FORMAT_AC3_BE;
175 af->data->bps = 2;
176 af->data->nch = 2;
177 return test_output_res;
178 case AF_CONTROL_COMMAND_LINE:
179 mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
180 s->bit_rate = 0;
181 s->min_channel_num = 0;
182 s->add_iec61937_header = 0;
183 sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
184 &s->min_channel_num);
185 if (s->bit_rate < 1000)
186 s->bit_rate *= 1000;
187 if (s->bit_rate) {
188 for (i = 0; i < 19; ++i)
189 if (ac3_bitrate_tab[i] * 1000 == s->bit_rate)
190 break;
191 if (i >= 19) {
192 mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
193 "bitrate %d, use default bitrate (check manpage to see "
194 "supported bitrates).\n", s->bit_rate);
195 s->bit_rate = 0;
198 if (s->min_channel_num == 0)
199 s->min_channel_num = 5;
200 mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
201 "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
202 s->min_channel_num);
203 return AF_OK;
205 return AF_UNKNOWN;
208 // Deallocate memory
209 static void uninit(struct af_instance_s* af)
211 if (af->data)
212 free(af->data->audio);
213 free(af->data);
214 if (af->setup) {
215 af_ac3enc_t *s = af->setup;
216 af->setup = NULL;
217 if(s->lavc_actx) {
218 avcodec_close(s->lavc_actx);
219 av_free(s->lavc_actx);
221 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
222 avcodec_free_frame(&s->frame);
223 #else
224 av_freep(&s->frame);
225 #endif
226 #ifdef CONFIG_LIBAVRESAMPLE
227 avresample_free(&s->avr);
228 av_freep(&s->resample_buf[0]);
229 #endif
230 free(s->pending_data);
231 free(s);
235 static int encode_data(af_ac3enc_t *s, uint8_t *src, uint8_t *dst, int dst_len)
237 AVPacket pkt;
238 uint8_t error[128];
239 int total_samples = AC3_FRAME_SIZE * s->lavc_actx->channels;
240 int bps = av_get_bytes_per_sample(s->lavc_actx->sample_fmt);
241 int ret, got_frame;
243 if (s->lavc_actx->channels >= 5)
244 reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
245 AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
246 s->lavc_actx->channels,
247 total_samples, bps);
249 s->frame->nb_samples = AC3_FRAME_SIZE;
250 s->frame->data[0] = src;
251 s->frame->linesize[0] = total_samples * bps;
253 #ifdef CONFIG_LIBAVRESAMPLE
254 if (s->avr) {
255 ret = avresample_convert(s->avr, s->resample_buf, s->linesize,
256 AC3_FRAME_SIZE, &src, total_samples * bps,
257 AC3_FRAME_SIZE);
258 if (ret < 0) {
259 av_strerror(ret, error, sizeof(error));
260 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error converting audio sample "
261 "format: %s\n", error);
262 return AF_ERROR;
263 } else if (ret != AC3_FRAME_SIZE) {
264 mp_msg(MSGT_AFILTER, MSGL_ERR, "Not enough converted data.\n");
265 return -1;
268 memcpy(s->frame->data, s->resample_buf, sizeof(s->resample_buf));
269 s->frame->linesize[0] = s->linesize;
271 #endif
273 av_init_packet(&pkt);
274 pkt.data = dst;
275 pkt.size = dst_len;
277 ret = avcodec_encode_audio2(s->lavc_actx, &pkt, s->frame, &got_frame);
278 if (ret < 0) {
279 av_strerror(ret, error, sizeof(error));
280 mp_msg(MSGT_AFILTER, MSGL_ERR, "Error encoding audio: %s\n", error);
281 return ret;
283 return got_frame ? pkt.size : 0;
286 // Filter data through filter
287 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
289 af_ac3enc_t *s = af->setup;
290 af_data_t *c = data; // Current working data
291 af_data_t *l;
292 int len, left, outsize = 0, destsize;
293 char *buf, *src, *dest;
294 int max_output_len;
295 int frame_num = (data->len + s->pending_len) / s->expect_len;
297 if (s->add_iec61937_header)
298 max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
299 else
300 max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
302 if (af->data->len < max_output_len) {
303 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
304 "old len = %i, new len = %i\n", af->info->name, af->data->len,
305 max_output_len);
306 free(af->data->audio);
307 af->data->audio = malloc(max_output_len);
308 if (!af->data->audio) {
309 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
310 return NULL;
312 af->data->len = max_output_len;
315 l = af->data; // Local data
316 buf = (char *)l->audio;
317 src = (char *)c->audio;
318 left = c->len;
321 while (left > 0) {
322 if (left + s->pending_len < s->expect_len) {
323 memcpy(s->pending_data + s->pending_len, src, left);
324 src += left;
325 s->pending_len += left;
326 left = 0;
327 break;
330 dest = s->add_iec61937_header ? buf + 8 : buf;
331 destsize = (char *)l->audio + l->len - buf;
333 if (s->pending_len) {
334 int needs = s->expect_len - s->pending_len;
335 if (needs > 0) {
336 memcpy(s->pending_data + s->pending_len, src, needs);
337 src += needs;
338 left -= needs;
341 len = encode_data(s, s->pending_data, dest, destsize);
342 s->pending_len = 0;
344 else {
345 len = encode_data(s, src, dest, destsize);
346 src += s->expect_len;
347 left -= s->expect_len;
349 if (len <= 0)
350 return NULL;
352 mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
353 len, s->pending_len);
355 if (s->add_iec61937_header) {
356 int bsmod = dest[5] & 0x7;
358 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
359 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
360 buf[4] = bsmod; // bsmod
361 buf[5] = 0x01; // data-type ac3
362 AV_WB16(buf + 6, len << 3); // number of bits in payload
364 memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
365 len = AC3_FRAME_SIZE * 2 * 2;
368 outsize += len;
369 buf += len;
371 c->audio = l->audio;
372 c->nch = 2;
373 c->bps = 2;
374 c->len = outsize;
375 mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
376 outsize, s->pending_len);
377 return c;
380 static int af_open(af_instance_t* af){
382 af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
383 af->control=control;
384 af->uninit=uninit;
385 af->play=play;
386 af->mul=1;
387 af->data=calloc(1,sizeof(af_data_t));
388 af->setup=s;
390 s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
391 if (!s->lavc_acodec) {
392 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
393 return AF_ERROR;
396 s->lavc_actx = avcodec_alloc_context3(s->lavc_acodec);
397 if (!s->lavc_actx) {
398 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
399 return AF_ERROR;
401 s->frame = avcodec_alloc_frame();
402 if (!s->frame)
403 return AF_ERROR;
404 const enum AVSampleFormat *fmts = s->lavc_acodec->sample_fmts;
405 for (int i = 0; ; i++) {
406 if (fmts[i] == AV_SAMPLE_FMT_NONE) {
407 mp_msg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, encoder doesn't "
408 "support expected sample formats!\n");
409 return AF_ERROR;
411 enum AVSampleFormat fmt_packed = fmts[i];
412 #ifdef CONFIG_LIBAVRESAMPLE
413 fmt_packed = av_get_packed_sample_fmt(fmt_packed);
414 #endif
415 if (fmt_packed == AV_SAMPLE_FMT_S16) {
416 s->in_sampleformat = AF_FORMAT_S16_NE;
417 s->lavc_actx->sample_fmt = fmts[i];
418 break;
419 } else if (fmt_packed == AV_SAMPLE_FMT_FLT) {
420 s->in_sampleformat = AF_FORMAT_FLOAT_NE;
421 s->lavc_actx->sample_fmt = fmts[i];
422 break;
425 if (av_sample_fmt_is_planar(s->lavc_actx->sample_fmt)) {
426 #ifdef CONFIG_LIBAVRESAMPLE
427 s->avr = avresample_alloc_context();
428 if (!s->avr)
429 abort();
430 #else
431 mp_msg(MSGT_AFILTER, MSGL_ERR, "Libavresample is required for this "
432 "filter to work with this libavcodec version.\n");
433 return AF_ERROR;
434 #endif
436 char buf[100];
437 mp_msg(MSGT_AFILTER, MSGL_V, "[af_lavcac3enc]: in sample format: %s\n",
438 af_fmt2str(s->in_sampleformat, buf, 100));
439 s->pending_data_size = AF_NCH * AC3_FRAME_SIZE *
440 af_fmt2bits(s->in_sampleformat) / 8;
441 s->pending_data = malloc(s->pending_data_size);
443 return AF_OK;
446 af_info_t af_info_lavcac3enc = {
447 "runtime encode to ac3 using libavcodec",
448 "lavcac3enc",
449 "Ulion",
451 AF_FLAGS_REENTRANT,
452 af_open