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.
29 #include <libavcodec/avcodec.h>
30 #include <libavutil/intreadwrite.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
;
51 int pending_data_size
;
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};
69 case AF_CONTROL_REINIT
:
70 if (AF_FORMAT_IS_AC3(data
->format
) || data
->nch
< s
->min_channel_num
)
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
;
78 af
->data
->rate
= 48000;
79 if (data
->nch
> AC3_MAX_CHANNELS
)
80 af
->data
->nch
= AC3_MAX_CHANNELS
;
82 af
->data
->nch
= data
->nch
;
83 test_output_res
= af_test_output(af
, data
);
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
;
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
);
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
);
119 af
->data
->format
= AF_FORMAT_AC3_BE
;
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
);
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)
133 for (i
= 0; i
< 19; ++i
)
134 if (ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
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
);
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
,
154 static void uninit(struct af_instance_s
* af
)
157 free(af
->data
->audio
);
160 af_ac3enc_t
*s
= af
->setup
;
163 avcodec_close(s
->lavc_actx
);
164 av_free(s
->lavc_actx
);
166 free(s
->pending_data
);
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
177 int len
, left
, outsize
= 0, destsize
;
178 char *buf
, *src
, *dest
;
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
;
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
,
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");
198 af
->data
->len
= max_output_len
;
201 l
= af
->data
; // Local data
202 buf
= (char *)l
->audio
;
203 src
= (char *)c
->audio
;
208 if (left
+ s
->pending_len
< s
->expect_len
) {
209 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
211 s
->pending_len
+= left
;
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
;
222 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
228 reorder_channel_nch(s
->pending_data
,
229 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
230 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
232 s
->expect_len
/ samplesize
, samplesize
);
234 len
= avcodec_encode_audio(s
->lavc_actx
, dest
, destsize
,
235 (void *)s
->pending_data
);
240 reorder_channel_nch(src
,
241 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
242 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
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;
272 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "play return size %d, pending %d\n",
273 outsize
, s
->pending_len
);
277 static int af_open(af_instance_t
* af
){
279 af_ac3enc_t
*s
= calloc(1,sizeof(af_ac3enc_t
));
284 af
->data
=calloc(1,sizeof(af_data_t
));
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");
293 s
->lavc_actx
= avcodec_alloc_context3(s
->lavc_acodec
);
295 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't allocate context!\n");
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");
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
];
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
];
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
);
324 af_info_t af_info_lavcac3enc
= {
325 "runtime encode to ac3 using libavcodec",