2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
28 #include "libmpdemux/aviheader.h"
29 #include "libmpdemux/ms_hdr.h"
30 #include "stream/stream.h"
31 #include "libmpdemux/muxer.h"
34 #include "libaf/af_format.h"
35 #include "libaf/reorder_ch.h"
36 #include "libavcodec/avcodec.h"
37 #include "libavutil/intreadwrite.h"
39 static AVCodec
*lavc_acodec
;
40 static AVCodecContext
*lavc_actx
;
41 extern char *lavc_param_acodec
;
42 extern int lavc_param_abitrate
;
43 extern int lavc_param_atag
;
44 extern int lavc_param_audio_global_header
;
45 extern int avcodec_initialized
;
46 static int compressed_frame_size
= 0;
47 #ifdef CONFIG_LIBAVFORMAT
48 #include "libavformat/avformat.h"
49 extern const struct AVCodecTag
*mp_wav_taglists
[];
52 static int bind_lavc(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
54 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
)+lavc_actx
->extradata_size
+256);
55 mux_a
->wf
->wFormatTag
= lavc_param_atag
;
56 mux_a
->wf
->nChannels
= lavc_actx
->channels
;
57 mux_a
->wf
->nSamplesPerSec
= lavc_actx
->sample_rate
;
58 mux_a
->wf
->nAvgBytesPerSec
= (lavc_actx
->bit_rate
/ 8);
59 mux_a
->avg_rate
= lavc_actx
->bit_rate
;
60 mux_a
->h
.dwRate
= mux_a
->wf
->nAvgBytesPerSec
;
61 if(lavc_actx
->block_align
)
62 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
= lavc_actx
->block_align
;
65 mux_a
->h
.dwScale
= (mux_a
->wf
->nAvgBytesPerSec
* lavc_actx
->frame_size
)/ mux_a
->wf
->nSamplesPerSec
; /* for cbr */
67 if ((mux_a
->wf
->nAvgBytesPerSec
*
68 lavc_actx
->frame_size
) % mux_a
->wf
->nSamplesPerSec
)
70 mux_a
->h
.dwScale
= lavc_actx
->frame_size
;
71 mux_a
->h
.dwRate
= lavc_actx
->sample_rate
;
72 mux_a
->h
.dwSampleSize
= 0; // Blocksize not constant
75 mux_a
->h
.dwSampleSize
= 0;
77 if(mux_a
->h
.dwSampleSize
)
78 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwSampleSize
;
80 mux_a
->wf
->nBlockAlign
= 1;
81 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
82 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
84 switch(lavc_param_atag
)
86 case 0x11: /* imaadpcm */
87 mux_a
->wf
->wBitsPerSample
= 4;
88 mux_a
->wf
->cbSize
= 2;
89 AV_WL16(mux_a
->wf
+1, lavc_actx
->frame_size
);
92 mux_a
->wf
->cbSize
= 12;
93 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
94 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
95 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
96 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
97 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
98 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
101 mux_a
->wf
->wBitsPerSample
= 0; /* Unknown */
102 if (lavc_actx
->extradata
&& (lavc_actx
->extradata_size
> 0))
104 memcpy(mux_a
->wf
+1, lavc_actx
->extradata
, lavc_actx
->extradata_size
);
105 mux_a
->wf
->cbSize
= lavc_actx
->extradata_size
;
108 mux_a
->wf
->cbSize
= 0;
113 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
115 encoder
->input_format
= AF_FORMAT_S16_NE
;
116 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
117 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
122 static int encode_lavc(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int size
, int max_size
)
125 if ((encoder
->params
.channels
== 6 || encoder
->params
.channels
== 5) &&
126 (!strcmp(lavc_acodec
->name
,"ac3") ||
127 !strcmp(lavc_acodec
->name
,"libfaac"))) {
128 int isac3
= !strcmp(lavc_acodec
->name
,"ac3");
129 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
130 isac3
? AF_CHANNEL_LAYOUT_LAVC_DEFAULT
131 : AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
132 encoder
->params
.channels
,
135 n
= avcodec_encode_audio(lavc_actx
, dest
, size
, src
);
136 compressed_frame_size
= n
;
141 static int close_lavc(audio_encoder_t
*encoder
)
143 compressed_frame_size
= 0;
147 static int get_frame_size(audio_encoder_t
*encoder
)
149 int sz
= compressed_frame_size
;
150 compressed_frame_size
= 0;
154 #ifndef CONFIG_LIBAVFORMAT
155 static uint32_t lavc_find_atag(char *codec
)
160 if(! strcasecmp(codec
, "mp2"))
163 if(! strcasecmp(codec
, "mp3"))
166 if(! strcasecmp(codec
, "ac3"))
169 if(! strcasecmp(codec
, "adpcm_ima_wav"))
172 if(! strncasecmp(codec
, "bonk", 4))
180 int mpae_init_lavc(audio_encoder_t
*encoder
)
182 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
;
183 encoder
->params
.bitrate
= encoder
->params
.sample_rate
* encoder
->params
.channels
* 2 * 8;
185 if(!lavc_param_acodec
)
187 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_NoLavcAudioCodecName
);
191 if(!avcodec_initialized
){
193 avcodec_register_all();
194 avcodec_initialized
=1;
197 lavc_acodec
= avcodec_find_encoder_by_name(lavc_param_acodec
);
200 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LavcAudioCodecNotFound
, lavc_param_acodec
);
203 if(lavc_param_atag
== 0)
205 #ifdef CONFIG_LIBAVFORMAT
206 lavc_param_atag
= av_codec_get_tag(mp_wav_taglists
, lavc_acodec
->id
);
208 lavc_param_atag
= lavc_find_atag(lavc_param_acodec
);
212 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "Couldn't find wav tag for specified codec, exit\n");
217 lavc_actx
= avcodec_alloc_context();
218 if(lavc_actx
== NULL
)
220 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntAllocateLavcContext
);
224 lavc_actx
->codec_type
= CODEC_TYPE_AUDIO
;
225 lavc_actx
->codec_id
= lavc_acodec
->id
;
226 // put sample parameters
227 lavc_actx
->channels
= encoder
->params
.channels
;
228 lavc_actx
->sample_rate
= encoder
->params
.sample_rate
;
229 lavc_actx
->time_base
.num
= 1;
230 lavc_actx
->time_base
.den
= encoder
->params
.sample_rate
;
231 if(lavc_param_abitrate
<1000)
232 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
* 1000;
234 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
;
238 * Special case for adpcm_ima_wav.
239 * The bitrate is only dependent on samplerate.
240 * We have to known frame_size and block_align in advance,
241 * so I just copied the code from libavcodec/adpcm.c
243 * However, ms adpcm_ima_wav uses a block_align of 2048,
244 * lavc defaults to 1024
246 if(lavc_param_atag
== 0x11) {
248 int framesize
= (blkalign
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
249 lavc_actx
->bit_rate
= lavc_actx
->sample_rate
*8*blkalign
/framesize
;
251 if((lavc_param_audio_global_header
&1)
252 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
253 lavc_actx
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
255 if(lavc_param_audio_global_header
&2){
256 lavc_actx
->flags2
|= CODEC_FLAG2_LOCAL_HEADER
;
259 if(avcodec_open(lavc_actx
, lavc_acodec
) < 0)
261 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntOpenCodec
, lavc_param_acodec
, lavc_param_abitrate
);
265 if(lavc_param_atag
== 0x11) {
266 lavc_actx
->block_align
= 2048;
267 lavc_actx
->frame_size
= (lavc_actx
->block_align
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
270 encoder
->decode_buffer_size
= lavc_actx
->frame_size
* 2 * encoder
->params
.channels
;
271 while (encoder
->decode_buffer_size
< 1024) encoder
->decode_buffer_size
*= 2;
272 encoder
->bind
= bind_lavc
;
273 encoder
->get_frame_size
= get_frame_size
;
274 encoder
->encode
= encode_lavc
;
275 encoder
->close
= close_lavc
;