2007-12-13 12:38:17 +00:00
|
|
|
/*
|
|
|
|
* audio filter for runtime AC-3 encoding with libavcodec.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Ulion <ulion A gmail P com>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2008-05-14 17:24:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-13 12:38:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
2010-05-14 00:54:31 +00:00
|
|
|
#include <assert.h>
|
2007-12-13 12:38:17 +00:00
|
|
|
|
2012-02-01 18:01:16 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavutil/intreadwrite.h>
|
2012-08-15 20:23:02 +00:00
|
|
|
#include <libavutil/mem.h>
|
2012-02-01 18:01:16 +00:00
|
|
|
|
2007-12-13 12:38:17 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "af.h"
|
|
|
|
#include "reorder_ch.h"
|
|
|
|
|
2010-05-14 00:54:31 +00:00
|
|
|
|
|
|
|
#define AC3_MAX_CHANNELS 6
|
|
|
|
#define AC3_MAX_CODED_FRAME_SIZE 3840
|
|
|
|
#define AC3_FRAME_SIZE (6 * 256)
|
|
|
|
const uint16_t ac3_bitrate_tab[19] = {
|
|
|
|
32, 40, 48, 56, 64, 80, 96, 112, 128,
|
|
|
|
160, 192, 224, 256, 320, 384, 448, 512, 576, 640
|
|
|
|
};
|
2007-12-13 12:38:17 +00:00
|
|
|
|
|
|
|
// Data for specific instances of this filter
|
|
|
|
typedef struct af_ac3enc_s {
|
|
|
|
struct AVCodec *lavc_acodec;
|
|
|
|
struct AVCodecContext *lavc_actx;
|
|
|
|
int add_iec61937_header;
|
|
|
|
int bit_rate;
|
2010-05-14 00:54:31 +00:00
|
|
|
int pending_data_size;
|
2007-12-13 12:38:17 +00:00
|
|
|
char *pending_data;
|
|
|
|
int pending_len;
|
|
|
|
int expect_len;
|
|
|
|
int min_channel_num;
|
2011-02-03 02:55:20 +00:00
|
|
|
int in_sampleformat;
|
2007-12-13 12:38:17 +00:00
|
|
|
} af_ac3enc_t;
|
|
|
|
|
|
|
|
// Initialization and runtime control
|
|
|
|
static int control(struct af_instance_s *af, int cmd, void *arg)
|
|
|
|
{
|
2011-11-14 19:05:49 +00:00
|
|
|
af_ac3enc_t *s = af->setup;
|
|
|
|
af_data_t *data = arg;
|
2007-12-13 12:38:17 +00:00
|
|
|
int i, bit_rate, test_output_res;
|
|
|
|
static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \
|
|
|
|
{0, 96000, 192000, 256000, 384000, 448000, 448000};
|
|
|
|
|
|
|
|
switch (cmd){
|
|
|
|
case AF_CONTROL_REINIT:
|
2010-01-11 20:27:52 +00:00
|
|
|
if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num)
|
2007-12-13 12:38:17 +00:00
|
|
|
return AF_DETACH;
|
|
|
|
|
2011-02-03 02:55:20 +00:00
|
|
|
af->data->format = s->in_sampleformat;
|
|
|
|
af->data->bps = af_fmt2bits(s->in_sampleformat) / 8;
|
2007-12-13 12:38:17 +00:00
|
|
|
if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000)
|
|
|
|
af->data->rate = data->rate;
|
|
|
|
else
|
|
|
|
af->data->rate = 48000;
|
|
|
|
if (data->nch > AC3_MAX_CHANNELS)
|
|
|
|
af->data->nch = AC3_MAX_CHANNELS;
|
|
|
|
else
|
|
|
|
af->data->nch = data->nch;
|
|
|
|
test_output_res = af_test_output(af, data);
|
|
|
|
|
2010-10-14 19:28:09 +00:00
|
|
|
s->pending_len = 0;
|
|
|
|
s->expect_len = AC3_FRAME_SIZE * data->nch * af->data->bps;
|
|
|
|
assert(s->expect_len <= s->pending_data_size);
|
|
|
|
if (s->add_iec61937_header)
|
|
|
|
af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len;
|
|
|
|
else
|
|
|
|
af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len;
|
|
|
|
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
|
|
|
|
data->nch, data->rate, af->mul, s->expect_len);
|
|
|
|
|
2007-12-13 12:38:17 +00:00
|
|
|
bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch];
|
|
|
|
|
|
|
|
if (s->lavc_actx->channels != af->data->nch ||
|
|
|
|
s->lavc_actx->sample_rate != af->data->rate ||
|
|
|
|
s->lavc_actx->bit_rate != bit_rate) {
|
|
|
|
|
2012-01-28 11:41:36 +00:00
|
|
|
avcodec_close(s->lavc_actx);
|
2007-12-13 12:38:17 +00:00
|
|
|
|
|
|
|
// Put sample parameters
|
|
|
|
s->lavc_actx->channels = af->data->nch;
|
|
|
|
s->lavc_actx->sample_rate = af->data->rate;
|
|
|
|
s->lavc_actx->bit_rate = bit_rate;
|
|
|
|
|
2012-01-28 11:41:36 +00:00
|
|
|
if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate);
|
2007-12-13 12:38:17 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2010-05-14 00:54:31 +00:00
|
|
|
if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) {
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 "
|
|
|
|
"encoder frame size %d\n", s->lavc_actx->frame_size);
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2010-01-11 20:40:51 +00:00
|
|
|
af->data->format = AF_FORMAT_AC3_BE;
|
2011-02-03 02:55:20 +00:00
|
|
|
af->data->bps = 2;
|
2007-12-13 12:38:17 +00:00
|
|
|
af->data->nch = 2;
|
|
|
|
return test_output_res;
|
|
|
|
case AF_CONTROL_COMMAND_LINE:
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg);
|
2007-12-13 12:38:17 +00:00
|
|
|
s->bit_rate = 0;
|
|
|
|
s->min_channel_num = 0;
|
|
|
|
s->add_iec61937_header = 0;
|
2011-11-14 19:05:49 +00:00
|
|
|
sscanf(arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate,
|
2007-12-13 12:38:17 +00:00
|
|
|
&s->min_channel_num);
|
|
|
|
if (s->bit_rate < 1000)
|
|
|
|
s->bit_rate *= 1000;
|
|
|
|
if (s->bit_rate) {
|
|
|
|
for (i = 0; i < 19; ++i)
|
2010-05-14 00:54:31 +00:00
|
|
|
if (ac3_bitrate_tab[i] * 1000 == s->bit_rate)
|
2007-12-13 12:38:17 +00:00
|
|
|
break;
|
|
|
|
if (i >= 19) {
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported "
|
2007-12-13 12:38:17 +00:00
|
|
|
"bitrate %d, use default bitrate (check manpage to see "
|
|
|
|
"supported bitrates).\n", s->bit_rate);
|
|
|
|
s->bit_rate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s->min_channel_num == 0)
|
|
|
|
s->min_channel_num = 5;
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, "
|
2007-12-13 12:38:17 +00:00
|
|
|
"minchnum:%d.\n", s->add_iec61937_header, s->bit_rate,
|
|
|
|
s->min_channel_num);
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deallocate memory
|
|
|
|
static void uninit(struct af_instance_s* af)
|
|
|
|
{
|
|
|
|
if (af->data)
|
|
|
|
free(af->data->audio);
|
|
|
|
free(af->data);
|
|
|
|
if (af->setup) {
|
|
|
|
af_ac3enc_t *s = af->setup;
|
|
|
|
af->setup = NULL;
|
|
|
|
if(s->lavc_actx) {
|
2012-01-28 11:41:36 +00:00
|
|
|
avcodec_close(s->lavc_actx);
|
|
|
|
av_free(s->lavc_actx);
|
2007-12-13 12:38:17 +00:00
|
|
|
}
|
|
|
|
free(s->pending_data);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data through filter
|
|
|
|
static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
|
|
{
|
|
|
|
af_ac3enc_t *s = af->setup;
|
|
|
|
af_data_t *c = data; // Current working data
|
|
|
|
af_data_t *l;
|
|
|
|
int len, left, outsize = 0, destsize;
|
|
|
|
char *buf, *src, *dest;
|
2008-01-11 10:15:46 +00:00
|
|
|
int max_output_len;
|
|
|
|
int frame_num = (data->len + s->pending_len) / s->expect_len;
|
2011-03-31 20:44:14 +00:00
|
|
|
int samplesize = af_fmt2bits(s->in_sampleformat) / 8;
|
2008-01-11 10:15:46 +00:00
|
|
|
|
|
|
|
if (s->add_iec61937_header)
|
|
|
|
max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num;
|
|
|
|
else
|
|
|
|
max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num;
|
|
|
|
|
|
|
|
if (af->data->len < max_output_len) {
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
|
2008-01-11 10:15:46 +00:00
|
|
|
"old len = %i, new len = %i\n", af->info->name, af->data->len,
|
|
|
|
max_output_len);
|
|
|
|
free(af->data->audio);
|
|
|
|
af->data->audio = malloc(max_output_len);
|
|
|
|
if (!af->data->audio) {
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n");
|
2008-01-11 10:15:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
af->data->len = max_output_len;
|
|
|
|
}
|
2007-12-13 12:38:17 +00:00
|
|
|
|
|
|
|
l = af->data; // Local data
|
2011-11-14 19:05:49 +00:00
|
|
|
buf = l->audio;
|
|
|
|
src = c->audio;
|
2007-12-13 12:38:17 +00:00
|
|
|
left = c->len;
|
|
|
|
|
|
|
|
|
|
|
|
while (left > 0) {
|
|
|
|
if (left + s->pending_len < s->expect_len) {
|
|
|
|
memcpy(s->pending_data + s->pending_len, src, left);
|
|
|
|
src += left;
|
|
|
|
s->pending_len += left;
|
|
|
|
left = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = s->add_iec61937_header ? buf + 8 : buf;
|
|
|
|
destsize = (char *)l->audio + l->len - buf;
|
|
|
|
|
|
|
|
if (s->pending_len) {
|
|
|
|
int needs = s->expect_len - s->pending_len;
|
|
|
|
if (needs > 0) {
|
|
|
|
memcpy(s->pending_data + s->pending_len, src, needs);
|
|
|
|
src += needs;
|
|
|
|
left -= needs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->nch >= 5)
|
|
|
|
reorder_channel_nch(s->pending_data,
|
|
|
|
AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
|
2009-08-18 22:24:36 +00:00
|
|
|
AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
|
2007-12-13 12:38:17 +00:00
|
|
|
c->nch,
|
2011-03-31 20:44:14 +00:00
|
|
|
s->expect_len / samplesize, samplesize);
|
2007-12-13 12:38:17 +00:00
|
|
|
|
|
|
|
len = avcodec_encode_audio(s->lavc_actx, dest, destsize,
|
|
|
|
(void *)s->pending_data);
|
|
|
|
s->pending_len = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (c->nch >= 5)
|
|
|
|
reorder_channel_nch(src,
|
|
|
|
AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
|
2009-08-18 22:24:36 +00:00
|
|
|
AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
|
2007-12-13 12:38:17 +00:00
|
|
|
c->nch,
|
2011-03-31 20:44:14 +00:00
|
|
|
s->expect_len / samplesize, samplesize);
|
2007-12-13 12:38:17 +00:00
|
|
|
len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src);
|
|
|
|
src += s->expect_len;
|
|
|
|
left -= s->expect_len;
|
|
|
|
}
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n",
|
2007-12-13 12:38:17 +00:00
|
|
|
len, s->pending_len);
|
|
|
|
|
|
|
|
if (s->add_iec61937_header) {
|
|
|
|
int bsmod = dest[5] & 0x7;
|
|
|
|
|
2010-01-11 20:40:51 +00:00
|
|
|
AV_WB16(buf, 0xF872); // iec 61937 syncword 1
|
|
|
|
AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
|
|
|
|
buf[4] = bsmod; // bsmod
|
|
|
|
buf[5] = 0x01; // data-type ac3
|
|
|
|
AV_WB16(buf + 6, len << 3); // number of bits in payload
|
2007-12-13 12:38:17 +00:00
|
|
|
|
|
|
|
memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len);
|
|
|
|
len = AC3_FRAME_SIZE * 2 * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
outsize += len;
|
|
|
|
buf += len;
|
|
|
|
}
|
|
|
|
c->audio = l->audio;
|
|
|
|
c->nch = 2;
|
|
|
|
c->bps = 2;
|
|
|
|
c->len = outsize;
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n",
|
2007-12-13 12:38:17 +00:00
|
|
|
outsize, s->pending_len);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int af_open(af_instance_t* af){
|
|
|
|
|
|
|
|
af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t));
|
|
|
|
af->control=control;
|
|
|
|
af->uninit=uninit;
|
|
|
|
af->play=play;
|
|
|
|
af->mul=1;
|
|
|
|
af->data=calloc(1,sizeof(af_data_t));
|
|
|
|
af->setup=s;
|
|
|
|
|
|
|
|
s->lavc_acodec = avcodec_find_encoder_by_name("ac3");
|
|
|
|
if (!s->lavc_acodec) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
|
2007-12-13 12:38:17 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-28 11:41:36 +00:00
|
|
|
s->lavc_actx = avcodec_alloc_context3(s->lavc_acodec);
|
2007-12-13 12:38:17 +00:00
|
|
|
if (!s->lavc_actx) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n");
|
2007-12-13 12:38:17 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2012-01-28 11:41:36 +00:00
|
|
|
const enum AVSampleFormat *fmts = s->lavc_acodec->sample_fmts;
|
2011-02-03 02:55:20 +00:00
|
|
|
for (int i = 0; ; i++) {
|
2012-01-28 11:41:36 +00:00
|
|
|
if (fmts[i] == AV_SAMPLE_FMT_NONE) {
|
2011-02-03 02:55:20 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, encoder doesn't "
|
|
|
|
"support expected sample formats!\n");
|
|
|
|
return AF_ERROR;
|
2012-01-28 11:41:36 +00:00
|
|
|
} else if (fmts[i] == AV_SAMPLE_FMT_S16) {
|
2011-02-03 02:55:20 +00:00
|
|
|
s->in_sampleformat = AF_FORMAT_S16_NE;
|
|
|
|
s->lavc_actx->sample_fmt = fmts[i];
|
|
|
|
break;
|
2012-01-28 11:41:36 +00:00
|
|
|
} else if (fmts[i] == AV_SAMPLE_FMT_FLT) {
|
2011-02-03 02:55:20 +00:00
|
|
|
s->in_sampleformat = AF_FORMAT_FLOAT_NE;
|
|
|
|
s->lavc_actx->sample_fmt = fmts[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char buf[100];
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[af_lavcac3enc]: in sample format: %s\n",
|
|
|
|
af_fmt2str(s->in_sampleformat, buf, 100));
|
|
|
|
s->pending_data_size = AF_NCH * AC3_FRAME_SIZE *
|
|
|
|
af_fmt2bits(s->in_sampleformat) / 8;
|
|
|
|
s->pending_data = malloc(s->pending_data_size);
|
2007-12-13 12:38:17 +00:00
|
|
|
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
af_info_t af_info_lavcac3enc = {
|
|
|
|
"runtime encode to ac3 using libavcodec",
|
|
|
|
"lavcac3enc",
|
|
|
|
"Ulion",
|
|
|
|
"",
|
|
|
|
AF_FLAGS_REENTRANT,
|
|
|
|
af_open
|
|
|
|
};
|