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"
33 #include "libaf/af_format.h"
34 #include "libaf/reorder_ch.h"
35 #include "libavcodec/avcodec.h"
36 #include "ffmpeg_files/intreadwrite.h"
38 static AVCodec
*lavc_acodec
;
39 static AVCodecContext
*lavc_actx
;
40 extern char *lavc_param_acodec
;
41 extern int lavc_param_abitrate
;
42 extern int lavc_param_atag
;
43 extern int lavc_param_audio_global_header
;
44 extern int avcodec_initialized
;
45 static int compressed_frame_size
= 0;
46 #include "libmpdemux/mp_taglists.h"
48 static int bind_lavc(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
50 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
)+lavc_actx
->extradata_size
+256);
51 mux_a
->wf
->wFormatTag
= lavc_param_atag
;
52 mux_a
->wf
->nChannels
= lavc_actx
->channels
;
53 mux_a
->wf
->nSamplesPerSec
= lavc_actx
->sample_rate
;
54 mux_a
->wf
->nAvgBytesPerSec
= (lavc_actx
->bit_rate
/ 8);
55 mux_a
->avg_rate
= lavc_actx
->bit_rate
;
56 mux_a
->h
.dwRate
= mux_a
->wf
->nAvgBytesPerSec
;
57 if(lavc_actx
->block_align
)
58 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
= lavc_actx
->block_align
;
61 mux_a
->h
.dwScale
= (mux_a
->wf
->nAvgBytesPerSec
* lavc_actx
->frame_size
)/ mux_a
->wf
->nSamplesPerSec
; /* for cbr */
63 if ((mux_a
->wf
->nAvgBytesPerSec
*
64 lavc_actx
->frame_size
) % mux_a
->wf
->nSamplesPerSec
)
66 mux_a
->h
.dwScale
= lavc_actx
->frame_size
;
67 mux_a
->h
.dwRate
= lavc_actx
->sample_rate
;
68 mux_a
->h
.dwSampleSize
= 0; // Blocksize not constant
71 mux_a
->h
.dwSampleSize
= 0;
73 if(mux_a
->h
.dwSampleSize
)
74 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwSampleSize
;
76 mux_a
->wf
->nBlockAlign
= 1;
77 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
78 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
80 switch(lavc_param_atag
)
82 case 0x11: /* imaadpcm */
83 mux_a
->wf
->wBitsPerSample
= 4;
84 mux_a
->wf
->cbSize
= 2;
85 AV_WL16(mux_a
->wf
+1, lavc_actx
->frame_size
);
88 mux_a
->wf
->cbSize
= 12;
89 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
90 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
91 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
92 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
93 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
94 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
97 mux_a
->wf
->wBitsPerSample
= 0; /* Unknown */
98 if (lavc_actx
->extradata
&& (lavc_actx
->extradata_size
> 0))
100 memcpy(mux_a
->wf
+1, lavc_actx
->extradata
, lavc_actx
->extradata_size
);
101 mux_a
->wf
->cbSize
= lavc_actx
->extradata_size
;
104 mux_a
->wf
->cbSize
= 0;
109 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
111 encoder
->input_format
= AF_FORMAT_S16_NE
;
112 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
113 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
118 static int encode_lavc(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int size
, int max_size
)
121 if ((encoder
->params
.channels
== 6 || encoder
->params
.channels
== 5) &&
122 (!strcmp(lavc_acodec
->name
,"ac3") ||
123 !strcmp(lavc_acodec
->name
,"libfaac"))) {
124 int isac3
= !strcmp(lavc_acodec
->name
,"ac3");
125 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
126 isac3
? AF_CHANNEL_LAYOUT_LAVC_DEFAULT
127 : AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
128 encoder
->params
.channels
,
131 n
= avcodec_encode_audio(lavc_actx
, dest
, size
, src
);
132 compressed_frame_size
= n
;
137 static int close_lavc(audio_encoder_t
*encoder
)
139 compressed_frame_size
= 0;
143 static int get_frame_size(audio_encoder_t
*encoder
)
145 int sz
= compressed_frame_size
;
146 compressed_frame_size
= 0;
150 int mpae_init_lavc(audio_encoder_t
*encoder
)
152 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
;
153 encoder
->params
.bitrate
= encoder
->params
.sample_rate
* encoder
->params
.channels
* 2 * 8;
155 if(!lavc_param_acodec
)
157 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "Audio LAVC, Missing codec name!\n");
161 if(!avcodec_initialized
){
163 avcodec_register_all();
164 avcodec_initialized
=1;
167 lavc_acodec
= avcodec_find_encoder_by_name(lavc_param_acodec
);
170 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "Audio LAVC, couldn't find encoder for codec %s.\n", lavc_param_acodec
);
173 if(lavc_param_atag
== 0)
175 lavc_param_atag
= mp_av_codec_get_tag(mp_wav_taglists
, lavc_acodec
->id
);
178 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "Couldn't find wav tag for specified codec, exit\n");
183 lavc_actx
= avcodec_alloc_context();
184 if(lavc_actx
== NULL
)
186 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "Audio LAVC, couldn't allocate context!\n");
190 lavc_actx
->codec_type
= CODEC_TYPE_AUDIO
;
191 lavc_actx
->codec_id
= lavc_acodec
->id
;
192 // put sample parameters
193 lavc_actx
->channels
= encoder
->params
.channels
;
194 lavc_actx
->sample_rate
= encoder
->params
.sample_rate
;
195 lavc_actx
->time_base
.num
= 1;
196 lavc_actx
->time_base
.den
= encoder
->params
.sample_rate
;
197 if(lavc_param_abitrate
<1000)
198 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
* 1000;
200 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
;
204 * Special case for adpcm_ima_wav.
205 * The bitrate is only dependent on samplerate.
206 * We have to known frame_size and block_align in advance,
207 * so I just copied the code from libavcodec/adpcm.c
209 * However, ms adpcm_ima_wav uses a block_align of 2048,
210 * lavc defaults to 1024
212 if(lavc_param_atag
== 0x11) {
214 int framesize
= (blkalign
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
215 lavc_actx
->bit_rate
= lavc_actx
->sample_rate
*8*blkalign
/framesize
;
217 if((lavc_param_audio_global_header
&1)
218 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
219 lavc_actx
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
221 if(lavc_param_audio_global_header
&2){
222 lavc_actx
->flags2
|= CODEC_FLAG2_LOCAL_HEADER
;
225 if(avcodec_open(lavc_actx
, lavc_acodec
) < 0)
227 mp_tmsg(MSGT_MENCODER
, MSGL_FATAL
, "Couldn't open codec %s, br=%d.\n", lavc_param_acodec
, lavc_param_abitrate
);
231 if(lavc_param_atag
== 0x11) {
232 lavc_actx
->block_align
= 2048;
233 lavc_actx
->frame_size
= (lavc_actx
->block_align
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
236 encoder
->decode_buffer_size
= lavc_actx
->frame_size
* 2 * encoder
->params
.channels
;
237 while (encoder
->decode_buffer_size
< 1024) encoder
->decode_buffer_size
*= 2;
238 encoder
->bind
= bind_lavc
;
239 encoder
->get_frame_size
= get_frame_size
;
240 encoder
->encode
= encode_lavc
;
241 encoder
->close
= close_lavc
;