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 "libmpcodecs/vd_ffmpeg.h"
32 #include "reorder_ch.h"
34 #include "libavcodec/avcodec.h"
35 #include "ffmpeg_files/intreadwrite.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 if (s
->lavc_actx
->codec
)
103 avcodec_close(s
->lavc_actx
);
105 // Put sample parameters
106 s
->lavc_actx
->channels
= af
->data
->nch
;
107 s
->lavc_actx
->sample_rate
= af
->data
->rate
;
108 s
->lavc_actx
->bit_rate
= bit_rate
;
110 if(avcodec_open(s
->lavc_actx
, s
->lavc_acodec
) < 0) {
111 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate
);
115 if (s
->lavc_actx
->frame_size
!= AC3_FRAME_SIZE
) {
116 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "lavcac3enc: unexpected ac3 "
117 "encoder frame size %d\n", s
->lavc_actx
->frame_size
);
120 af
->data
->format
= AF_FORMAT_AC3_BE
;
123 return test_output_res
;
124 case AF_CONTROL_COMMAND_LINE
:
125 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc cmdline: %s.\n", (char*)arg
);
127 s
->min_channel_num
= 0;
128 s
->add_iec61937_header
= 0;
129 sscanf((char*)arg
,"%d:%d:%d", &s
->add_iec61937_header
, &s
->bit_rate
,
130 &s
->min_channel_num
);
131 if (s
->bit_rate
< 1000)
134 for (i
= 0; i
< 19; ++i
)
135 if (ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
138 mp_msg(MSGT_AFILTER
, MSGL_WARN
, "af_lavcac3enc unable set unsupported "
139 "bitrate %d, use default bitrate (check manpage to see "
140 "supported bitrates).\n", s
->bit_rate
);
144 if (s
->min_channel_num
== 0)
145 s
->min_channel_num
= 5;
146 mp_msg(MSGT_AFILTER
, MSGL_V
, "af_lavcac3enc config spdif:%d, bitrate:%d, "
147 "minchnum:%d.\n", s
->add_iec61937_header
, s
->bit_rate
,
155 static void uninit(struct af_instance_s
* af
)
158 free(af
->data
->audio
);
161 af_ac3enc_t
*s
= af
->setup
;
164 if (s
->lavc_actx
->codec
)
165 avcodec_close(s
->lavc_actx
);
168 free(s
->pending_data
);
173 // Filter data through filter
174 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
176 af_ac3enc_t
*s
= af
->setup
;
177 af_data_t
*c
= data
; // Current working data
179 int len
, left
, outsize
= 0, destsize
;
180 char *buf
, *src
, *dest
;
182 int frame_num
= (data
->len
+ s
->pending_len
) / s
->expect_len
;
183 int samplesize
= af_fmt2bits(s
->in_sampleformat
) / 8;
185 if (s
->add_iec61937_header
)
186 max_output_len
= AC3_FRAME_SIZE
* 2 * 2 * frame_num
;
188 max_output_len
= AC3_MAX_CODED_FRAME_SIZE
* frame_num
;
190 if (af
->data
->len
< max_output_len
) {
191 mp_msg(MSGT_AFILTER
, MSGL_V
, "[libaf] Reallocating memory in module %s, "
192 "old len = %i, new len = %i\n", af
->info
->name
, af
->data
->len
,
194 free(af
->data
->audio
);
195 af
->data
->audio
= malloc(max_output_len
);
196 if (!af
->data
->audio
) {
197 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[libaf] Could not allocate memory \n");
200 af
->data
->len
= max_output_len
;
203 l
= af
->data
; // Local data
204 buf
= (char *)l
->audio
;
205 src
= (char *)c
->audio
;
210 if (left
+ s
->pending_len
< s
->expect_len
) {
211 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
213 s
->pending_len
+= left
;
218 dest
= s
->add_iec61937_header
? buf
+ 8 : buf
;
219 destsize
= (char *)l
->audio
+ l
->len
- buf
;
221 if (s
->pending_len
) {
222 int needs
= s
->expect_len
- s
->pending_len
;
224 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
230 reorder_channel_nch(s
->pending_data
,
231 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
232 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
234 s
->expect_len
/ samplesize
, samplesize
);
236 len
= avcodec_encode_audio(s
->lavc_actx
, dest
, destsize
,
237 (void *)s
->pending_data
);
242 reorder_channel_nch(src
,
243 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
244 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
246 s
->expect_len
/ samplesize
, samplesize
);
247 len
= avcodec_encode_audio(s
->lavc_actx
,dest
,destsize
,(void *)src
);
248 src
+= s
->expect_len
;
249 left
-= s
->expect_len
;
251 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "avcodec_encode_audio got %d, pending %d.\n",
252 len
, s
->pending_len
);
254 if (s
->add_iec61937_header
) {
255 int bsmod
= dest
[5] & 0x7;
257 AV_WB16(buf
, 0xF872); // iec 61937 syncword 1
258 AV_WB16(buf
+ 2, 0x4E1F); // iec 61937 syncword 2
259 buf
[4] = bsmod
; // bsmod
260 buf
[5] = 0x01; // data-type ac3
261 AV_WB16(buf
+ 6, len
<< 3); // number of bits in payload
263 memset(buf
+ 8 + len
, 0, AC3_FRAME_SIZE
* 2 * 2 - 8 - len
);
264 len
= AC3_FRAME_SIZE
* 2 * 2;
274 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "play return size %d, pending %d\n",
275 outsize
, s
->pending_len
);
279 static int af_open(af_instance_t
* af
){
281 af_ac3enc_t
*s
= calloc(1,sizeof(af_ac3enc_t
));
286 af
->data
=calloc(1,sizeof(af_data_t
));
291 s
->lavc_acodec
= avcodec_find_encoder_by_name("ac3");
292 if (!s
->lavc_acodec
) {
293 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
297 s
->lavc_actx
= avcodec_alloc_context();
299 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't allocate context!\n");
302 // using deprecated SampleFormat/FMT, AV* versions only added in 2010-11
303 const enum SampleFormat
*fmts
= s
->lavc_acodec
->sample_fmts
;
304 for (int i
= 0; ; i
++) {
305 if (fmts
[i
] == SAMPLE_FMT_NONE
) {
306 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, encoder doesn't "
307 "support expected sample formats!\n");
309 } else if (fmts
[i
] == SAMPLE_FMT_S16
) {
310 s
->in_sampleformat
= AF_FORMAT_S16_NE
;
311 s
->lavc_actx
->sample_fmt
= fmts
[i
];
313 } else if (fmts
[i
] == SAMPLE_FMT_FLT
) {
314 s
->in_sampleformat
= AF_FORMAT_FLOAT_NE
;
315 s
->lavc_actx
->sample_fmt
= fmts
[i
];
320 mp_msg(MSGT_AFILTER
, MSGL_V
, "[af_lavcac3enc]: in sample format: %s\n",
321 af_fmt2str(s
->in_sampleformat
, buf
, 100));
322 s
->pending_data_size
= AF_NCH
* AC3_FRAME_SIZE
*
323 af_fmt2bits(s
->in_sampleformat
) / 8;
324 s
->pending_data
= malloc(s
->pending_data_size
);
329 af_info_t af_info_lavcac3enc
= {
330 "runtime encode to ac3 using libavcodec",