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
;
58 // Initialization and runtime control
59 static int control(struct af_instance_s
*af
, int cmd
, void *arg
)
61 af_ac3enc_t
*s
= (af_ac3enc_t
*)af
->setup
;
62 af_data_t
*data
= (af_data_t
*)arg
;
63 int i
, bit_rate
, test_output_res
;
64 static const int default_bit_rate
[AC3_MAX_CHANNELS
+1] = \
65 {0, 96000, 192000, 256000, 384000, 448000, 448000};
68 case AF_CONTROL_REINIT
:
69 if (AF_FORMAT_IS_AC3(data
->format
) || data
->nch
< s
->min_channel_num
)
72 af
->data
->format
= AF_FORMAT_S16_NE
;
73 if (data
->rate
== 48000 || data
->rate
== 44100 || data
->rate
== 32000)
74 af
->data
->rate
= data
->rate
;
76 af
->data
->rate
= 48000;
77 if (data
->nch
> AC3_MAX_CHANNELS
)
78 af
->data
->nch
= AC3_MAX_CHANNELS
;
80 af
->data
->nch
= data
->nch
;
82 test_output_res
= af_test_output(af
, data
);
85 s
->expect_len
= AC3_FRAME_SIZE
* data
->nch
* af
->data
->bps
;
86 assert(s
->expect_len
<= s
->pending_data_size
);
87 if (s
->add_iec61937_header
)
88 af
->mul
= (double)AC3_FRAME_SIZE
* 2 * 2 / s
->expect_len
;
90 af
->mul
= (double)AC3_MAX_CODED_FRAME_SIZE
/ s
->expect_len
;
92 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc reinit: %d, %d, %f, %d.\n",
93 data
->nch
, data
->rate
, af
->mul
, s
->expect_len
);
95 bit_rate
= s
->bit_rate
? s
->bit_rate
: default_bit_rate
[af
->data
->nch
];
97 if (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 (s
->lavc_actx
->codec
)
102 avcodec_close(s
->lavc_actx
);
104 // Put sample parameters
105 s
->lavc_actx
->channels
= af
->data
->nch
;
106 s
->lavc_actx
->sample_rate
= af
->data
->rate
;
107 s
->lavc_actx
->bit_rate
= bit_rate
;
109 if(avcodec_open(s
->lavc_actx
, s
->lavc_acodec
) < 0) {
110 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate
);
114 if (s
->lavc_actx
->frame_size
!= AC3_FRAME_SIZE
) {
115 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "lavcac3enc: unexpected ac3 "
116 "encoder frame size %d\n", s
->lavc_actx
->frame_size
);
119 af
->data
->format
= AF_FORMAT_AC3_BE
;
121 return test_output_res
;
122 case AF_CONTROL_COMMAND_LINE
:
123 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "af_lavcac3enc cmdline: %s.\n", (char*)arg
);
125 s
->min_channel_num
= 0;
126 s
->add_iec61937_header
= 0;
127 sscanf((char*)arg
,"%d:%d:%d", &s
->add_iec61937_header
, &s
->bit_rate
,
128 &s
->min_channel_num
);
129 if (s
->bit_rate
< 1000)
132 for (i
= 0; i
< 19; ++i
)
133 if (ac3_bitrate_tab
[i
] * 1000 == s
->bit_rate
)
136 mp_msg(MSGT_AFILTER
, MSGL_WARN
, "af_lavcac3enc unable set unsupported "
137 "bitrate %d, use default bitrate (check manpage to see "
138 "supported bitrates).\n", s
->bit_rate
);
142 if (s
->min_channel_num
== 0)
143 s
->min_channel_num
= 5;
144 mp_msg(MSGT_AFILTER
, MSGL_V
, "af_lavcac3enc config spdif:%d, bitrate:%d, "
145 "minchnum:%d.\n", s
->add_iec61937_header
, s
->bit_rate
,
153 static void uninit(struct af_instance_s
* af
)
156 free(af
->data
->audio
);
159 af_ac3enc_t
*s
= af
->setup
;
162 if (s
->lavc_actx
->codec
)
163 avcodec_close(s
->lavc_actx
);
166 free(s
->pending_data
);
171 // Filter data through filter
172 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
174 af_ac3enc_t
*s
= af
->setup
;
175 af_data_t
*c
= data
; // Current working data
177 int len
, left
, outsize
= 0, destsize
;
178 char *buf
, *src
, *dest
;
180 int frame_num
= (data
->len
+ s
->pending_len
) / s
->expect_len
;
182 if (s
->add_iec61937_header
)
183 max_output_len
= AC3_FRAME_SIZE
* 2 * 2 * frame_num
;
185 max_output_len
= AC3_MAX_CODED_FRAME_SIZE
* frame_num
;
187 if (af
->data
->len
< max_output_len
) {
188 mp_msg(MSGT_AFILTER
, MSGL_V
, "[libaf] Reallocating memory in module %s, "
189 "old len = %i, new len = %i\n", af
->info
->name
, af
->data
->len
,
191 free(af
->data
->audio
);
192 af
->data
->audio
= malloc(max_output_len
);
193 if (!af
->data
->audio
) {
194 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[libaf] Could not allocate memory \n");
197 af
->data
->len
= max_output_len
;
200 l
= af
->data
; // Local data
201 buf
= (char *)l
->audio
;
202 src
= (char *)c
->audio
;
207 if (left
+ s
->pending_len
< s
->expect_len
) {
208 memcpy(s
->pending_data
+ s
->pending_len
, src
, left
);
210 s
->pending_len
+= left
;
215 dest
= s
->add_iec61937_header
? buf
+ 8 : buf
;
216 destsize
= (char *)l
->audio
+ l
->len
- buf
;
218 if (s
->pending_len
) {
219 int needs
= s
->expect_len
- s
->pending_len
;
221 memcpy(s
->pending_data
+ s
->pending_len
, src
, needs
);
227 reorder_channel_nch(s
->pending_data
,
228 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
229 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
231 s
->expect_len
/ 2, 2);
233 len
= avcodec_encode_audio(s
->lavc_actx
, dest
, destsize
,
234 (void *)s
->pending_data
);
239 reorder_channel_nch(src
,
240 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
241 AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
243 s
->expect_len
/ 2, 2);
244 len
= avcodec_encode_audio(s
->lavc_actx
,dest
,destsize
,(void *)src
);
245 src
+= s
->expect_len
;
246 left
-= s
->expect_len
;
248 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "avcodec_encode_audio got %d, pending %d.\n",
249 len
, s
->pending_len
);
251 if (s
->add_iec61937_header
) {
252 int bsmod
= dest
[5] & 0x7;
254 AV_WB16(buf
, 0xF872); // iec 61937 syncword 1
255 AV_WB16(buf
+ 2, 0x4E1F); // iec 61937 syncword 2
256 buf
[4] = bsmod
; // bsmod
257 buf
[5] = 0x01; // data-type ac3
258 AV_WB16(buf
+ 6, len
<< 3); // number of bits in payload
260 memset(buf
+ 8 + len
, 0, AC3_FRAME_SIZE
* 2 * 2 - 8 - len
);
261 len
= AC3_FRAME_SIZE
* 2 * 2;
271 mp_msg(MSGT_AFILTER
, MSGL_DBG2
, "play return size %d, pending %d\n",
272 outsize
, s
->pending_len
);
276 static int af_open(af_instance_t
* af
){
278 af_ac3enc_t
*s
= calloc(1,sizeof(af_ac3enc_t
));
279 s
->pending_data_size
= 2 * AF_NCH
* AC3_FRAME_SIZE
;
280 s
->pending_data
= malloc(s
->pending_data_size
);
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");
306 af_info_t af_info_lavcac3enc
= {
307 "runtime encode to ac3 using libavcodec",