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.
31 #include "reorder_ch.h"
33 #include "libavcodec/avcodec.h"
34 #include "libavcodec/ac3.h"
35 #include "libavutil/intreadwrite.h"
37 // Data for specific instances of this filter
38 typedef struct af_ac3enc_s
{
39 struct AVCodec
*lavc_acodec
;
40 struct AVCodecContext
*lavc_actx
;
41 int add_iec61937_header
;
49 extern int avcodec_initialized
;
51 // Initialization and runtime control
52 static int control(struct af_instance_s
*af
, int cmd
, void *arg
)
54 af_ac3enc_t
*s
= (af_ac3enc_t
*)af
->setup
;
55 af_data_t
*data
= (af_data_t
*)arg
;
56 int i
, bit_rate
, test_output_res
;
57 static const int default_bit_rate
[AC3_MAX_CHANNELS
+1] = \
58 {0, 96000, 192000, 256000, 384000, 448000, 448000};
61 case AF_CONTROL_REINIT
:
62 if (AF_FORMAT_IS_AC3(data
->format
) || data
->nch
< s
->min_channel_num
)
66 s
->expect_len
= AC3_FRAME_SIZE
* data
->nch
* data
->bps
;
67 if (s
->add_iec61937_header
)
68 af
->mul
= (double)AC3_FRAME_SIZE
* 2 * 2 / s
->expect_len
;
70 af
->mul
= (double)AC3_MAX_CODED_FRAME_SIZE
/ s
->expect_len
;
72 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
73 data
->nch
, data
->rate
, af
->mul
, s
->expect_len
);
75 af
->data
->format
= AF_FORMAT_S16_NE
;
76 if (data
->rate
== 48000 || data
->rate
== 44100 || data
->rate
== 32000)
77 af
->data
->rate
= data
->rate
;
79 af
->data
->rate
= 48000;
80 if (data
->nch
> AC3_MAX_CHANNELS
)
81 af
->data
->nch
= AC3_MAX_CHANNELS
;
83 af
->data
->nch
= data
->nch
;
85 test_output_res
= af_test_output(af
, data
);
87 bit_rate
= s
->bit_rate
? s
->bit_rate
: default_bit_rate
[af
->data
->nch
];
89 if (s
->lavc_actx
->channels
!= af
->data
->nch
||
90 s
->lavc_actx
->sample_rate
!= af
->data
->rate
||
91 s
->lavc_actx
->bit_rate
!= bit_rate
) {
93 if (s
->lavc_actx
->codec
)
94 avcodec_close(s
->lavc_actx
);
96 // Put sample parameters
97 s
->lavc_actx
->channels
= af
->data
->nch
;
98 s
->lavc_actx
->sample_rate
= af
->data
->rate
;
99 s
->lavc_actx
->bit_rate
= bit_rate
;
101 if(avcodec_open(s
->lavc_actx
, s
->lavc_acodec
) < 0) {
102 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate
);
106 af
->data
->format
= AF_FORMAT_AC3_BE
;
108 return test_output_res
;
109 case AF_CONTROL_COMMAND_LINE
:
110 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc cmdline: %s.\n", (char*)arg
);
112 s
->min_channel_num
= 0;
113 s
->add_iec61937_header
= 0;
114 sscanf((char*)arg
,"%d:%d:%d", &s
->add_iec61937_header
, &s
->bit_rate
,
115 &s
->min_channel_num
);
116 if (s
->bit_rate
< 1000)
119 for (i
= 0; i
< 19; ++i
)
120 if (ff_ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
123 mp_msg(MSGT_AFILTER
, MSGL_WARN
, "af_lavcac3enc unable set unsupported "
124 "bitrate %d, use default bitrate (check manpage to see "
125 "supported bitrates).\n", s
->bit_rate
);
129 if (s
->min_channel_num
== 0)
130 s
->min_channel_num
= 5;
131 mp_msg(MSGT_AFILTER
, MSGL_V
, "af_lavcac3enc config spdif:%d, bitrate:%d, "
132 "minchnum:%d.\n", s
->add_iec61937_header
, s
->bit_rate
,
140 static void uninit(struct af_instance_s
* af
)
143 free(af
->data
->audio
);
146 af_ac3enc_t
*s
= af
->setup
;
149 if (s
->lavc_actx
->codec
)
150 avcodec_close(s
->lavc_actx
);
153 free(s
->pending_data
);
158 // Filter data through filter
159 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
161 af_ac3enc_t
*s
= af
->setup
;
162 af_data_t
*c
= data
; // Current working data
164 int len
, left
, outsize
= 0, destsize
;
165 char *buf
, *src
, *dest
;
167 int frame_num
= (data
->len
+ s
->pending_len
) / s
->expect_len
;
169 if (s
->add_iec61937_header
)
170 max_output_len
= AC3_FRAME_SIZE
* 2 * 2 * frame_num
;
172 max_output_len
= AC3_MAX_CODED_FRAME_SIZE
* frame_num
;
174 if (af
->data
->len
< max_output_len
) {
175 mp_msg(MSGT_AFILTER
, MSGL_V
, "[libaf] Reallocating memory in module %s, "
176 "old len = %i, new len = %i\n", af
->info
->name
, af
->data
->len
,
178 free(af
->data
->audio
);
179 af
->data
->audio
= malloc(max_output_len
);
180 if (!af
->data
->audio
) {
181 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[libaf] Could not allocate memory \n");
184 af
->data
->len
= max_output_len
;
187 l
= af
->data
; // Local data
188 buf
= (char *)l
->audio
;
189 src
= (char *)c
->audio
;
194 if (left
+ s
->pending_len
< s
->expect_len
) {
195 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
197 s
->pending_len
+= left
;
202 dest
= s
->add_iec61937_header
? buf
+ 8 : buf
;
203 destsize
= (char *)l
->audio
+ l
->len
- buf
;
205 if (s
->pending_len
) {
206 int needs
= s
->expect_len
- s
->pending_len
;
208 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
214 reorder_channel_nch(s
->pending_data
,
215 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
216 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
218 s
->expect_len
/ 2, 2);
220 len
= avcodec_encode_audio(s
->lavc_actx
, dest
, destsize
,
221 (void *)s
->pending_data
);
226 reorder_channel_nch(src
,
227 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
228 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
230 s
->expect_len
/ 2, 2);
231 len
= avcodec_encode_audio(s
->lavc_actx
,dest
,destsize
,(void *)src
);
232 src
+= s
->expect_len
;
233 left
-= s
->expect_len
;
235 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "avcodec_encode_audio got %d, pending %d.\n",
236 len
, s
->pending_len
);
238 if (s
->add_iec61937_header
) {
239 int bsmod
= dest
[5] & 0x7;
241 AV_WB16(buf
, 0xF872); // iec 61937 syncword 1
242 AV_WB16(buf
+ 2, 0x4E1F); // iec 61937 syncword 2
243 buf
[4] = bsmod
; // bsmod
244 buf
[5] = 0x01; // data-type ac3
245 AV_WB16(buf
+ 6, len
<< 3); // number of bits in payload
247 memset(buf
+ 8 + len
, 0, AC3_FRAME_SIZE
* 2 * 2 - 8 - len
);
248 len
= AC3_FRAME_SIZE
* 2 * 2;
258 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "play return size %d, pending %d\n",
259 outsize
, s
->pending_len
);
263 static int af_open(af_instance_t
* af
){
265 af_ac3enc_t
*s
= calloc(1,sizeof(af_ac3enc_t
));
266 int pending_space
= 2 * AC3_MAX_CHANNELS
* AC3_FRAME_SIZE
;
267 s
->pending_data
= calloc(pending_space
, sizeof(char));
273 af
->data
=calloc(1,sizeof(af_data_t
));
276 if (!avcodec_initialized
){
278 avcodec_register_all();
279 avcodec_initialized
=1;
282 s
->lavc_acodec
= avcodec_find_encoder_by_name("ac3");
283 if (!s
->lavc_acodec
) {
284 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
288 s
->lavc_actx
= avcodec_alloc_context();
290 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't allocate context!\n");
297 af_info_t af_info_lavcac3enc
= {
298 "runtime encode to ac3 using libavcodec",