10 #include "libmpdemux/aviheader.h"
11 #include "libmpdemux/ms_hdr.h"
12 #include "stream/stream.h"
13 #include "libmpdemux/muxer.h"
16 #include "libaf/af_format.h"
17 #include "libaf/reorder_ch.h"
18 #include "libavcodec/avcodec.h"
19 #include "libavutil/intreadwrite.h"
21 static AVCodec
*lavc_acodec
;
22 static AVCodecContext
*lavc_actx
;
23 extern char *lavc_param_acodec
;
24 extern int lavc_param_abitrate
;
25 extern int lavc_param_atag
;
26 extern int lavc_param_audio_global_header
;
27 extern int avcodec_initialized
;
28 static int compressed_frame_size
= 0;
29 #ifdef CONFIG_LIBAVFORMAT
30 #include "libavformat/avformat.h"
31 extern const struct AVCodecTag
*mp_wav_taglists
[];
34 static int bind_lavc(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
36 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
)+lavc_actx
->extradata_size
+256);
37 mux_a
->wf
->wFormatTag
= lavc_param_atag
;
38 mux_a
->wf
->nChannels
= lavc_actx
->channels
;
39 mux_a
->wf
->nSamplesPerSec
= lavc_actx
->sample_rate
;
40 mux_a
->wf
->nAvgBytesPerSec
= (lavc_actx
->bit_rate
/ 8);
41 mux_a
->avg_rate
= lavc_actx
->bit_rate
;
42 mux_a
->h
.dwRate
= mux_a
->wf
->nAvgBytesPerSec
;
43 if(lavc_actx
->block_align
)
44 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
= lavc_actx
->block_align
;
47 mux_a
->h
.dwScale
= (mux_a
->wf
->nAvgBytesPerSec
* lavc_actx
->frame_size
)/ mux_a
->wf
->nSamplesPerSec
; /* for cbr */
49 if ((mux_a
->wf
->nAvgBytesPerSec
*
50 lavc_actx
->frame_size
) % mux_a
->wf
->nSamplesPerSec
)
52 mux_a
->h
.dwScale
= lavc_actx
->frame_size
;
53 mux_a
->h
.dwRate
= lavc_actx
->sample_rate
;
54 mux_a
->h
.dwSampleSize
= 0; // Blocksize not constant
57 mux_a
->h
.dwSampleSize
= 0;
59 if(mux_a
->h
.dwSampleSize
)
60 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwSampleSize
;
62 mux_a
->wf
->nBlockAlign
= 1;
63 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
64 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
66 switch(lavc_param_atag
)
68 case 0x11: /* imaadpcm */
69 mux_a
->wf
->wBitsPerSample
= 4;
70 mux_a
->wf
->cbSize
= 2;
71 AV_WL16(mux_a
->wf
+1, lavc_actx
->frame_size
);
74 mux_a
->wf
->cbSize
= 12;
75 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
76 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
77 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
78 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
79 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
80 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
83 mux_a
->wf
->wBitsPerSample
= 0; /* Unknown */
84 if (lavc_actx
->extradata
&& (lavc_actx
->extradata_size
> 0))
86 memcpy(mux_a
->wf
+1, lavc_actx
->extradata
, lavc_actx
->extradata_size
);
87 mux_a
->wf
->cbSize
= lavc_actx
->extradata_size
;
90 mux_a
->wf
->cbSize
= 0;
95 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
97 encoder
->input_format
= AF_FORMAT_S16_NE
;
98 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
99 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
104 static int encode_lavc(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int size
, int max_size
)
107 if ((encoder
->params
.channels
== 6 || encoder
->params
.channels
== 5) &&
108 (!strcmp(lavc_acodec
->name
,"ac3") ||
109 !strcmp(lavc_acodec
->name
,"libfaac"))) {
110 int isac3
= !strcmp(lavc_acodec
->name
,"ac3");
111 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
112 isac3
? AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
113 : AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
114 encoder
->params
.channels
,
117 n
= avcodec_encode_audio(lavc_actx
, dest
, size
, src
);
118 compressed_frame_size
= n
;
123 static int close_lavc(audio_encoder_t
*encoder
)
125 compressed_frame_size
= 0;
129 static int get_frame_size(audio_encoder_t
*encoder
)
131 int sz
= compressed_frame_size
;
132 compressed_frame_size
= 0;
136 #ifndef CONFIG_LIBAVFORMAT
137 static uint32_t lavc_find_atag(char *codec
)
142 if(! strcasecmp(codec
, "mp2"))
145 if(! strcasecmp(codec
, "mp3"))
148 if(! strcasecmp(codec
, "ac3"))
151 if(! strcasecmp(codec
, "adpcm_ima_wav"))
154 if(! strncasecmp(codec
, "bonk", 4))
162 int mpae_init_lavc(audio_encoder_t
*encoder
)
164 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
;
165 encoder
->params
.bitrate
= encoder
->params
.sample_rate
* encoder
->params
.channels
* 2 * 8;
167 if(!lavc_param_acodec
)
169 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_NoLavcAudioCodecName
);
173 if(!avcodec_initialized
){
175 avcodec_register_all();
176 avcodec_initialized
=1;
179 lavc_acodec
= avcodec_find_encoder_by_name(lavc_param_acodec
);
182 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LavcAudioCodecNotFound
, lavc_param_acodec
);
185 if(lavc_param_atag
== 0)
187 #ifdef CONFIG_LIBAVFORMAT
188 lavc_param_atag
= av_codec_get_tag(mp_wav_taglists
, lavc_acodec
->id
);
190 lavc_param_atag
= lavc_find_atag(lavc_param_acodec
);
194 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "Couldn't find wav tag for specified codec, exit\n");
199 lavc_actx
= avcodec_alloc_context();
200 if(lavc_actx
== NULL
)
202 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntAllocateLavcContext
);
206 // put sample parameters
207 lavc_actx
->channels
= encoder
->params
.channels
;
208 lavc_actx
->sample_rate
= encoder
->params
.sample_rate
;
209 if(lavc_param_abitrate
<1000)
210 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
* 1000;
212 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
;
216 * Special case for adpcm_ima_wav.
217 * The bitrate is only dependent on samplerate.
218 * We have to known frame_size and block_align in advance,
219 * so I just copied the code from libavcodec/adpcm.c
221 * However, ms adpcm_ima_wav uses a block_align of 2048,
222 * lavc defaults to 1024
224 if(lavc_param_atag
== 0x11) {
226 int framesize
= (blkalign
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
227 lavc_actx
->bit_rate
= lavc_actx
->sample_rate
*8*blkalign
/framesize
;
229 if((lavc_param_audio_global_header
&1)
230 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
231 lavc_actx
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
233 if(lavc_param_audio_global_header
&2){
234 lavc_actx
->flags2
|= CODEC_FLAG2_LOCAL_HEADER
;
237 if(avcodec_open(lavc_actx
, lavc_acodec
) < 0)
239 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntOpenCodec
, lavc_param_acodec
, lavc_param_abitrate
);
243 if(lavc_param_atag
== 0x11) {
244 lavc_actx
->block_align
= 2048;
245 lavc_actx
->frame_size
= (lavc_actx
->block_align
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
248 encoder
->decode_buffer_size
= lavc_actx
->frame_size
* 2 * encoder
->params
.channels
;
249 encoder
->bind
= bind_lavc
;
250 encoder
->get_frame_size
= get_frame_size
;
251 encoder
->encode
= encode_lavc
;
252 encoder
->close
= close_lavc
;