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/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>
39 #include "reorder_ch.h"
41 #ifdef CONFIG_LIBAVRESAMPLE
42 #include <libavresample/avresample.h>
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
];
61 struct AVCodec
*lavc_acodec
;
62 struct AVCodecContext
*lavc_actx
;
63 int add_iec61937_header
;
65 int pending_data_size
;
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};
83 case AF_CONTROL_REINIT
:
84 if (AF_FORMAT_IS_AC3(data
->format
) || data
->nch
< s
->min_channel_num
)
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
;
92 af
->data
->rate
= 48000;
93 if (data
->nch
> AC3_MAX_CHANNELS
)
94 af
->data
->nch
= AC3_MAX_CHANNELS
;
96 af
->data
->nch
= data
->nch
;
97 test_output_res
= af_test_output(af
, data
);
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
;
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
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
);
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);
135 av_strerror(ret
, error
, sizeof(error
));
136 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error allocating "
137 "resample buffer: %s\n", 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) {
151 av_strerror(ret
, error
, sizeof(error
));
152 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error configuring "
153 "libavresample: %s\n", error
);
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
);
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
);
174 af
->data
->format
= AF_FORMAT_AC3_BE
;
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
);
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)
188 for (i
= 0; i
< 19; ++i
)
189 if (ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
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
);
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
,
209 static void uninit(struct af_instance_s
* af
)
212 free(af
->data
->audio
);
215 af_ac3enc_t
*s
= af
->setup
;
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
);
226 #ifdef CONFIG_LIBAVRESAMPLE
227 avresample_free(&s
->avr
);
228 av_freep(&s
->resample_buf
[0]);
230 free(s
->pending_data
);
235 static int encode_data(af_ac3enc_t
*s
, uint8_t *src
, uint8_t *dst
, int dst_len
)
239 int total_samples
= AC3_FRAME_SIZE
* s
->lavc_actx
->channels
;
240 int bps
= av_get_bytes_per_sample(s
->lavc_actx
->sample_fmt
);
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
,
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
255 ret
= avresample_convert(s
->avr
, s
->resample_buf
, s
->linesize
,
256 AC3_FRAME_SIZE
, &src
, total_samples
* bps
,
259 av_strerror(ret
, error
, sizeof(error
));
260 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error converting audio sample "
261 "format: %s\n", error
);
263 } else if (ret
!= AC3_FRAME_SIZE
) {
264 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Not enough converted data.\n");
268 memcpy(s
->frame
->data
, s
->resample_buf
, sizeof(s
->resample_buf
));
269 s
->frame
->linesize
[0] = s
->linesize
;
273 av_init_packet(&pkt
);
277 ret
= avcodec_encode_audio2(s
->lavc_actx
, &pkt
, s
->frame
, &got_frame
);
279 av_strerror(ret
, error
, sizeof(error
));
280 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error encoding audio: %s\n", error
);
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
292 int len
, left
, outsize
= 0, destsize
;
293 char *buf
, *src
, *dest
;
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
;
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
,
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");
312 af
->data
->len
= max_output_len
;
315 l
= af
->data
; // Local data
316 buf
= (char *)l
->audio
;
317 src
= (char *)c
->audio
;
322 if (left
+ s
->pending_len
< s
->expect_len
) {
323 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
325 s
->pending_len
+= left
;
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
;
336 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
341 len
= encode_data(s
, s
->pending_data
, dest
, destsize
);
345 len
= encode_data(s
, src
, dest
, destsize
);
346 src
+= s
->expect_len
;
347 left
-= s
->expect_len
;
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;
375 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "play return size %d, pending %d\n",
376 outsize
, s
->pending_len
);
380 static int af_open(af_instance_t
* af
){
382 af_ac3enc_t
*s
= calloc(1,sizeof(af_ac3enc_t
));
387 af
->data
=calloc(1,sizeof(af_data_t
));
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");
396 s
->lavc_actx
= avcodec_alloc_context3(s
->lavc_acodec
);
398 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't allocate context!\n");
401 s
->frame
= avcodec_alloc_frame();
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");
411 enum AVSampleFormat fmt_packed
= fmts
[i
];
412 #ifdef CONFIG_LIBAVRESAMPLE
413 fmt_packed
= av_get_packed_sample_fmt(fmt_packed
);
415 if (fmt_packed
== AV_SAMPLE_FMT_S16
) {
416 s
->in_sampleformat
= AF_FORMAT_S16_NE
;
417 s
->lavc_actx
->sample_fmt
= fmts
[i
];
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
];
425 if (av_sample_fmt_is_planar(s
->lavc_actx
->sample_fmt
)) {
426 #ifdef CONFIG_LIBAVRESAMPLE
427 s
->avr
= avresample_alloc_context();
431 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Libavresample is required for this "
432 "filter to work with this libavcodec version.\n");
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
);
446 af_info_t af_info_lavcac3enc
= {
447 "runtime encode to ac3 using libavcodec",