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"
36 // Data for specific instances of this filter
37 typedef struct af_ac3enc_s
{
38 struct AVCodec
*lavc_acodec
;
39 struct AVCodecContext
*lavc_actx
;
40 int add_iec61937_header
;
48 extern int avcodec_initialized
;
50 // Initialization and runtime control
51 static int control(struct af_instance_s
*af
, int cmd
, void *arg
)
53 af_ac3enc_t
*s
= (af_ac3enc_t
*)af
->setup
;
54 af_data_t
*data
= (af_data_t
*)arg
;
55 int i
, bit_rate
, test_output_res
;
56 static const int default_bit_rate
[AC3_MAX_CHANNELS
+1] = \
57 {0, 96000, 192000, 256000, 384000, 448000, 448000};
60 case AF_CONTROL_REINIT
:
61 if (data
->format
== AF_FORMAT_AC3
|| data
->nch
< s
->min_channel_num
)
65 s
->expect_len
= AC3_FRAME_SIZE
* data
->nch
* data
->bps
;
66 if (s
->add_iec61937_header
)
67 af
->mul
= (double)AC3_FRAME_SIZE
* 2 * 2 / s
->expect_len
;
69 af
->mul
= (double)AC3_MAX_CODED_FRAME_SIZE
/ s
->expect_len
;
71 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
72 data
->nch
, data
->rate
, af
->mul
, s
->expect_len
);
74 af
->data
->format
= AF_FORMAT_S16_NE
;
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
;
84 test_output_res
= af_test_output(af
, data
);
86 bit_rate
= s
->bit_rate
? s
->bit_rate
: default_bit_rate
[af
->data
->nch
];
88 if (s
->lavc_actx
->channels
!= af
->data
->nch
||
89 s
->lavc_actx
->sample_rate
!= af
->data
->rate
||
90 s
->lavc_actx
->bit_rate
!= bit_rate
) {
92 if (s
->lavc_actx
->codec
)
93 avcodec_close(s
->lavc_actx
);
95 // Put sample parameters
96 s
->lavc_actx
->channels
= af
->data
->nch
;
97 s
->lavc_actx
->sample_rate
= af
->data
->rate
;
98 s
->lavc_actx
->bit_rate
= bit_rate
;
100 if(avcodec_open(s
->lavc_actx
, s
->lavc_acodec
) < 0) {
101 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate
);
105 af
->data
->format
= AF_FORMAT_AC3
;
107 return test_output_res
;
108 case AF_CONTROL_COMMAND_LINE
:
109 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc cmdline: %s.\n", (char*)arg
);
111 s
->min_channel_num
= 0;
112 s
->add_iec61937_header
= 0;
113 sscanf((char*)arg
,"%d:%d:%d", &s
->add_iec61937_header
, &s
->bit_rate
,
114 &s
->min_channel_num
);
115 if (s
->bit_rate
< 1000)
118 for (i
= 0; i
< 19; ++i
)
119 if (ff_ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
122 mp_msg(MSGT_AFILTER
, MSGL_WARN
, "af_lavcac3enc unable set unsupported "
123 "bitrate %d, use default bitrate (check manpage to see "
124 "supported bitrates).\n", s
->bit_rate
);
128 if (s
->min_channel_num
== 0)
129 s
->min_channel_num
= 5;
130 mp_msg(MSGT_AFILTER
, MSGL_V
, "af_lavcac3enc config spdif:%d, bitrate:%d, "
131 "minchnum:%d.\n", s
->add_iec61937_header
, s
->bit_rate
,
139 static void uninit(struct af_instance_s
* af
)
142 free(af
->data
->audio
);
145 af_ac3enc_t
*s
= af
->setup
;
148 if (s
->lavc_actx
->codec
)
149 avcodec_close(s
->lavc_actx
);
152 free(s
->pending_data
);
157 // Filter data through filter
158 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
160 af_ac3enc_t
*s
= af
->setup
;
161 af_data_t
*c
= data
; // Current working data
163 int len
, left
, outsize
= 0, destsize
;
164 char *buf
, *src
, *dest
;
166 int frame_num
= (data
->len
+ s
->pending_len
) / s
->expect_len
;
168 if (s
->add_iec61937_header
)
169 max_output_len
= AC3_FRAME_SIZE
* 2 * 2 * frame_num
;
171 max_output_len
= AC3_MAX_CODED_FRAME_SIZE
* frame_num
;
173 if (af
->data
->len
< max_output_len
) {
174 mp_msg(MSGT_AFILTER
, MSGL_V
, "[libaf] Reallocating memory in module %s, "
175 "old len = %i, new len = %i\n", af
->info
->name
, af
->data
->len
,
177 free(af
->data
->audio
);
178 af
->data
->audio
= malloc(max_output_len
);
179 if (!af
->data
->audio
) {
180 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[libaf] Could not allocate memory \n");
183 af
->data
->len
= max_output_len
;
186 l
= af
->data
; // Local data
187 buf
= (char *)l
->audio
;
188 src
= (char *)c
->audio
;
193 if (left
+ s
->pending_len
< s
->expect_len
) {
194 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
196 s
->pending_len
+= left
;
201 dest
= s
->add_iec61937_header
? buf
+ 8 : buf
;
202 destsize
= (char *)l
->audio
+ l
->len
- buf
;
204 if (s
->pending_len
) {
205 int needs
= s
->expect_len
- s
->pending_len
;
207 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
213 reorder_channel_nch(s
->pending_data
,
214 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
215 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
217 s
->expect_len
/ 2, 2);
219 len
= avcodec_encode_audio(s
->lavc_actx
, dest
, destsize
,
220 (void *)s
->pending_data
);
225 reorder_channel_nch(src
,
226 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
227 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
229 s
->expect_len
/ 2, 2);
230 len
= avcodec_encode_audio(s
->lavc_actx
,dest
,destsize
,(void *)src
);
231 src
+= s
->expect_len
;
232 left
-= s
->expect_len
;
234 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "avcodec_encode_audio got %d, pending %d.\n",
235 len
, s
->pending_len
);
237 if (s
->add_iec61937_header
) {
238 int16_t *out
= (int16_t *)buf
;
239 int bsmod
= dest
[5] & 0x7;
244 for (i
= 0; i
< len
; i
+= 2) {
250 dest
[len
] = dest
[len
-1];
255 out
[0] = 0xF872; // iec 61937 syncword 1
256 out
[1] = 0x4E1F; // iec 61937 syncword 2
257 out
[2] = 0x0001; // data-type ac3
258 out
[2] |= bsmod
<< 8; // bsmod
259 out
[3] = 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
));
280 int pending_space
= 2 * AC3_MAX_CHANNELS
* AC3_FRAME_SIZE
;
281 s
->pending_data
= calloc(pending_space
, sizeof(char));
287 af
->data
=calloc(1,sizeof(af_data_t
));
290 if (!avcodec_initialized
){
292 avcodec_register_all();
293 avcodec_initialized
=1;
296 s
->lavc_acodec
= avcodec_find_encoder_by_name("ac3");
297 if (!s
->lavc_acodec
) {
298 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3");
302 s
->lavc_actx
= avcodec_alloc_context();
304 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Audio LAVC, couldn't allocate context!\n");
311 af_info_t af_info_lavcac3enc
= {
312 "runtime encode to ac3 using libavcodec",