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>
36 #include <libavresample/avresample.h>
40 #include "reorder_ch.h"
43 #define AC3_MAX_CHANNELS 6
44 #define AC3_MAX_CODED_FRAME_SIZE 3840
45 #define AC3_FRAME_SIZE (6 * 256)
46 const uint16_t ac3_bitrate_tab
[19] = {
47 32, 40, 48, 56, 64, 80, 96, 112, 128,
48 160, 192, 224, 256, 320, 384, 448, 512, 576, 640
51 // Data for specific instances of this filter
52 typedef struct af_ac3enc_s
{
53 AVAudioResampleContext
*avr
;
54 uint8_t *resample_buf
[AC3_MAX_CHANNELS
];
57 struct AVCodec
*lavc_acodec
;
58 struct AVCodecContext
*lavc_actx
;
59 int add_iec61937_header
;
61 int pending_data_size
;
69 // Initialization and runtime control
70 static int control(struct af_instance_s
*af
, int cmd
, void *arg
)
72 af_ac3enc_t
*s
= (af_ac3enc_t
*)af
->setup
;
73 af_data_t
*data
= (af_data_t
*)arg
;
74 int i
, bit_rate
, test_output_res
;
75 static const int default_bit_rate
[AC3_MAX_CHANNELS
+1] = \
76 {0, 96000, 192000, 256000, 384000, 448000, 448000};
79 case AF_CONTROL_REINIT
:
80 if (AF_FORMAT_IS_AC3(data
->format
) || data
->nch
< s
->min_channel_num
)
83 af
->data
->format
= s
->in_sampleformat
;
84 af
->data
->bps
= af_fmt2bits(s
->in_sampleformat
) / 8;
85 if (data
->rate
== 48000 || data
->rate
== 44100 || data
->rate
== 32000)
86 af
->data
->rate
= data
->rate
;
88 af
->data
->rate
= 48000;
89 if (data
->nch
> AC3_MAX_CHANNELS
)
90 af
->data
->nch
= AC3_MAX_CHANNELS
;
92 af
->data
->nch
= data
->nch
;
93 test_output_res
= af_test_output(af
, data
);
96 s
->expect_len
= AC3_FRAME_SIZE
* data
->nch
* af
->data
->bps
;
97 assert(s
->expect_len
<= s
->pending_data_size
);
98 if (s
->add_iec61937_header
)
99 af
->mul
= (double)AC3_FRAME_SIZE
* 2 * 2 / s
->expect_len
;
101 af
->mul
= (double)AC3_MAX_CODED_FRAME_SIZE
/ s
->expect_len
;
103 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
104 data
->nch
, data
->rate
, af
->mul
, s
->expect_len
);
106 bit_rate
= s
->bit_rate
? s
->bit_rate
: default_bit_rate
[af
->data
->nch
];
108 if (s
->lavc_actx
->channels
!= af
->data
->nch
||
109 s
->lavc_actx
->sample_rate
!= af
->data
->rate
||
110 s
->lavc_actx
->bit_rate
!= bit_rate
) {
112 avcodec_close(s
->lavc_actx
);
116 av_get_default_channel_layout(af
->data
->nch
);
117 enum AVSampleFormat in_sample_fmt
=
118 av_get_packed_sample_fmt(s
->lavc_actx
->sample_fmt
);
121 avresample_close(s
->avr
);
123 if (af
->data
->nch
!= s
->lavc_actx
->channels
) {
124 av_freep(&s
->resample_buf
[0]);
125 ret
= av_samples_alloc(s
->resample_buf
, &s
->linesize
,
126 af
->data
->nch
, AC3_FRAME_SIZE
,
127 s
->lavc_actx
->sample_fmt
, 0);
130 av_strerror(ret
, error
, sizeof(error
));
131 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error allocating "
132 "resample buffer: %s\n", error
);
137 av_opt_set_int(s
->avr
, "in_channel_layout", ch_layout
, 0);
138 av_opt_set_int(s
->avr
, "out_channel_layout", ch_layout
, 0);
139 av_opt_set_int(s
->avr
, "in_sample_rate", af
->data
->rate
, 0);
140 av_opt_set_int(s
->avr
, "out_sample_rate", af
->data
->rate
, 0);
141 av_opt_set_int(s
->avr
, "in_sample_fmt", in_sample_fmt
, 0);
142 av_opt_set_int(s
->avr
, "out_sample_fmt", s
->lavc_actx
->sample_fmt
, 0);
144 if ((ret
= avresample_open(s
->avr
)) < 0) {
146 av_strerror(ret
, error
, sizeof(error
));
147 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error configuring "
148 "libavresample: %s\n", error
);
153 // Put sample parameters
154 s
->lavc_actx
->channels
= af
->data
->nch
;
155 s
->lavc_actx
->sample_rate
= af
->data
->rate
;
156 s
->lavc_actx
->bit_rate
= bit_rate
;
158 if (avcodec_open2(s
->lavc_actx
, s
->lavc_acodec
, NULL
) < 0) {
159 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate
);
163 if (s
->lavc_actx
->frame_size
!= AC3_FRAME_SIZE
) {
164 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "lavcac3enc: unexpected ac3 "
165 "encoder frame size %d\n", s
->lavc_actx
->frame_size
);
168 af
->data
->format
= AF_FORMAT_AC3_BE
;
171 return test_output_res
;
172 case AF_CONTROL_COMMAND_LINE
:
173 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc cmdline: %s.\n", (char*)arg
);
175 s
->min_channel_num
= 0;
176 s
->add_iec61937_header
= 0;
177 sscanf((char*)arg
,"%d:%d:%d", &s
->add_iec61937_header
, &s
->bit_rate
,
178 &s
->min_channel_num
);
179 if (s
->bit_rate
< 1000)
182 for (i
= 0; i
< 19; ++i
)
183 if (ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
186 mp_msg(MSGT_AFILTER
, MSGL_WARN
, "af_lavcac3enc unable set unsupported "
187 "bitrate %d, use default bitrate (check manpage to see "
188 "supported bitrates).\n", s
->bit_rate
);
192 if (s
->min_channel_num
== 0)
193 s
->min_channel_num
= 5;
194 mp_msg(MSGT_AFILTER
, MSGL_V
, "af_lavcac3enc config spdif:%d, bitrate:%d, "
195 "minchnum:%d.\n", s
->add_iec61937_header
, s
->bit_rate
,
203 static void uninit(struct af_instance_s
* af
)
206 free(af
->data
->audio
);
209 af_ac3enc_t
*s
= af
->setup
;
212 avcodec_close(s
->lavc_actx
);
213 av_free(s
->lavc_actx
);
215 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
216 avcodec_free_frame(&s
->frame
);
220 avresample_free(&s
->avr
);
221 av_freep(&s
->resample_buf
[0]);
222 free(s
->pending_data
);
227 static int encode_data(af_ac3enc_t
*s
, uint8_t *src
, uint8_t *dst
, int dst_len
)
231 int total_samples
= AC3_FRAME_SIZE
* s
->lavc_actx
->channels
;
232 int bps
= av_get_bytes_per_sample(s
->lavc_actx
->sample_fmt
);
235 if (s
->lavc_actx
->channels
>= 5)
236 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
237 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
238 s
->lavc_actx
->channels
,
241 s
->frame
->nb_samples
= AC3_FRAME_SIZE
;
242 s
->frame
->data
[0] = src
;
243 s
->frame
->linesize
[0] = total_samples
* bps
;
246 ret
= avresample_convert(s
->avr
, s
->resample_buf
, s
->linesize
,
247 AC3_FRAME_SIZE
, &src
, total_samples
* bps
,
250 av_strerror(ret
, error
, sizeof(error
));
251 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error converting audio sample "
252 "format: %s\n", error
);
254 } else if (ret
!= AC3_FRAME_SIZE
) {
255 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Not enough converted data.\n");
259 memcpy(s
->frame
->data
, s
->resample_buf
, sizeof(s
->resample_buf
));
260 s
->frame
->linesize
[0] = s
->linesize
;
263 av_init_packet(&pkt
);
267 ret
= avcodec_encode_audio2(s
->lavc_actx
, &pkt
, s
->frame
, &got_frame
);
269 av_strerror(ret
, error
, sizeof(error
));
270 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Error encoding audio: %s\n", error
);
273 return got_frame
? pkt
.size
: 0;
276 // Filter data through filter
277 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
279 af_ac3enc_t
*s
= af
->setup
;
280 af_data_t
*c
= data
; // Current working data
282 int len
, left
, outsize
= 0, destsize
;
283 char *buf
, *src
, *dest
;
285 int frame_num
= (data
->len
+ s
->pending_len
) / s
->expect_len
;
287 if (s
->add_iec61937_header
)
288 max_output_len
= AC3_FRAME_SIZE
* 2 * 2 * frame_num
;
290 max_output_len
= AC3_MAX_CODED_FRAME_SIZE
* frame_num
;
292 if (af
->data
->len
< max_output_len
) {
293 mp_msg(MSGT_AFILTER
, MSGL_V
, "[libaf] Reallocating memory in module %s, "
294 "old len = %i, new len = %i\n", af
->info
->name
, af
->data
->len
,
296 free(af
->data
->audio
);
297 af
->data
->audio
= malloc(max_output_len
);
298 if (!af
->data
->audio
) {
299 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[libaf] Could not allocate memory \n");
302 af
->data
->len
= max_output_len
;
305 l
= af
->data
; // Local data
306 buf
= (char *)l
->audio
;
307 src
= (char *)c
->audio
;
312 if (left
+ s
->pending_len
< s
->expect_len
) {
313 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
315 s
->pending_len
+= left
;
320 dest
= s
->add_iec61937_header
? buf
+ 8 : buf
;
321 destsize
= (char *)l
->audio
+ l
->len
- buf
;
323 if (s
->pending_len
) {
324 int needs
= s
->expect_len
- s
->pending_len
;
326 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
331 len
= encode_data(s
, s
->pending_data
, dest
, destsize
);
335 len
= encode_data(s
, src
, dest
, destsize
);
336 src
+= s
->expect_len
;
337 left
-= s
->expect_len
;
342 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "avcodec_encode_audio got %d, pending %d.\n",
343 len
, s
->pending_len
);
345 if (s
->add_iec61937_header
) {
346 int bsmod
= dest
[5] & 0x7;
348 AV_WB16(buf
, 0xF872); // iec 61937 syncword 1
349 AV_WB16(buf
+ 2, 0x4E1F); // iec 61937 syncword 2
350 buf
[4] = bsmod
; // bsmod
351 buf
[5] = 0x01; // data-type ac3
352 AV_WB16(buf
+ 6, len
<< 3); // number of bits in payload
354 memset(buf
+ 8 + len
, 0, AC3_FRAME_SIZE
* 2 * 2 - 8 - len
);
355 len
= AC3_FRAME_SIZE
* 2 * 2;
365 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "play return size %d, pending %d\n",
366 outsize
, s
->pending_len
);
370 static int af_open(af_instance_t
* af
){
372 af_ac3enc_t
*s
= calloc(1,sizeof(af_ac3enc_t
));
377 af
->data
=calloc(1,sizeof(af_data_t
));
380 s
->lavc_acodec
= avcodec_find_encoder_by_name("ac3");
381 if (!s
->lavc_acodec
) {
382 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
386 s
->lavc_actx
= avcodec_alloc_context3(s
->lavc_acodec
);
388 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't allocate context!\n");
391 s
->frame
= avcodec_alloc_frame();
394 const enum AVSampleFormat
*fmts
= s
->lavc_acodec
->sample_fmts
;
395 for (int i
= 0; ; i
++) {
396 if (fmts
[i
] == AV_SAMPLE_FMT_NONE
) {
397 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, encoder doesn't "
398 "support expected sample formats!\n");
401 enum AVSampleFormat fmt_packed
= av_get_packed_sample_fmt(fmts
[i
]);
402 if (fmt_packed
== AV_SAMPLE_FMT_S16
) {
403 s
->in_sampleformat
= AF_FORMAT_S16_NE
;
404 s
->lavc_actx
->sample_fmt
= fmts
[i
];
406 } else if (fmt_packed
== AV_SAMPLE_FMT_FLT
) {
407 s
->in_sampleformat
= AF_FORMAT_FLOAT_NE
;
408 s
->lavc_actx
->sample_fmt
= fmts
[i
];
412 if (av_sample_fmt_is_planar(s
->lavc_actx
->sample_fmt
)) {
413 s
->avr
= avresample_alloc_context();
418 mp_msg(MSGT_AFILTER
, MSGL_V
, "[af_lavcac3enc]: in sample format: %s\n",
419 af_fmt2str(s
->in_sampleformat
, buf
, 100));
420 s
->pending_data_size
= AF_NCH
* AC3_FRAME_SIZE
*
421 af_fmt2bits(s
->in_sampleformat
) / 8;
422 s
->pending_data
= malloc(s
->pending_data_size
);
427 af_info_t af_info_lavcac3enc
= {
428 "runtime encode to ac3 using libavcodec",