9 #include "libmpdemux/aviheader.h"
10 #include "libmpdemux/ms_hdr.h"
11 #include "stream/stream.h"
12 #include "libmpdemux/muxer.h"
16 #include "libaf/af_format.h"
17 #include "libaf/reorder_ch.h"
18 #ifdef USE_LIBAVCODEC_SO
19 #include <ffmpeg/avcodec.h>
21 #include "libavcodec/avcodec.h"
24 static AVCodec
*lavc_acodec
;
25 static AVCodecContext
*lavc_actx
;
26 extern char *lavc_param_acodec
;
27 extern int lavc_param_abitrate
;
28 extern int lavc_param_atag
;
29 extern int lavc_param_audio_global_header
;
30 extern int avcodec_inited
;
31 static int compressed_frame_size
= 0;
32 #ifdef USE_LIBAVFORMAT
33 #ifdef USE_LIBAVFORMAT_SO
34 #include <ffmpeg/avformat.h>
36 #include "libavformat/avformat.h"
38 extern const struct AVCodecTag
*mp_wav_taglists
[];
41 static int bind_lavc(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
43 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
)+lavc_actx
->extradata_size
+256);
44 mux_a
->wf
->wFormatTag
= lavc_param_atag
;
45 mux_a
->wf
->nChannels
= lavc_actx
->channels
;
46 mux_a
->wf
->nSamplesPerSec
= lavc_actx
->sample_rate
;
47 mux_a
->wf
->nAvgBytesPerSec
= (lavc_actx
->bit_rate
/ 8);
48 mux_a
->avg_rate
= lavc_actx
->bit_rate
;
49 mux_a
->h
.dwRate
= mux_a
->wf
->nAvgBytesPerSec
;
50 if(lavc_actx
->block_align
)
51 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
= lavc_actx
->block_align
;
54 mux_a
->h
.dwScale
= (mux_a
->wf
->nAvgBytesPerSec
* lavc_actx
->frame_size
)/ mux_a
->wf
->nSamplesPerSec
; /* for cbr */
56 if ((mux_a
->wf
->nAvgBytesPerSec
*
57 lavc_actx
->frame_size
) % mux_a
->wf
->nSamplesPerSec
)
59 mux_a
->h
.dwScale
= lavc_actx
->frame_size
;
60 mux_a
->h
.dwRate
= lavc_actx
->sample_rate
;
61 mux_a
->h
.dwSampleSize
= 0; // Blocksize not constant
64 mux_a
->h
.dwSampleSize
= 0;
66 if(mux_a
->h
.dwSampleSize
)
67 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwSampleSize
;
69 mux_a
->wf
->nBlockAlign
= 1;
70 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
71 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
73 switch(lavc_param_atag
)
75 case 0x11: /* imaadpcm */
76 mux_a
->wf
->wBitsPerSample
= 4;
77 mux_a
->wf
->cbSize
= 2;
78 ((uint16_t*)mux_a
->wf
)[sizeof(WAVEFORMATEX
)] =
79 ((lavc_actx
->block_align
- 4 * lavc_actx
->channels
) / (4 * lavc_actx
->channels
)) * 8 + 1;
82 mux_a
->wf
->cbSize
= 12;
83 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
84 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
85 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
86 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
87 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
88 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
91 mux_a
->wf
->wBitsPerSample
= 0; /* Unknown */
92 if (lavc_actx
->extradata
&& (lavc_actx
->extradata_size
> 0))
94 memcpy(mux_a
->wf
+1, lavc_actx
->extradata
, lavc_actx
->extradata_size
);
95 mux_a
->wf
->cbSize
= lavc_actx
->extradata_size
;
98 mux_a
->wf
->cbSize
= 0;
103 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
105 encoder
->input_format
= AF_FORMAT_S16_NE
;
106 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
107 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
112 static int encode_lavc(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int size
, int max_size
)
115 if ((encoder
->params
.channels
== 6 || encoder
->params
.channels
== 5) &&
116 (!strcmp(lavc_acodec
->name
,"ac3") ||
117 !strcmp(lavc_acodec
->name
,"libfaac"))) {
118 int isac3
= !strcmp(lavc_acodec
->name
,"ac3");
119 reorder_channel_nch(src
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
120 isac3
? AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
121 : AF_CHANNEL_LAYOUT_AAC_DEFAULT
,
122 encoder
->params
.channels
,
125 n
= avcodec_encode_audio(lavc_actx
, dest
, size
, src
);
126 compressed_frame_size
= n
;
131 static int close_lavc(audio_encoder_t
*encoder
)
133 compressed_frame_size
= 0;
137 static int get_frame_size(audio_encoder_t
*encoder
)
139 int sz
= compressed_frame_size
;
140 compressed_frame_size
= 0;
144 #ifndef USE_LIBAVFORMAT
145 static uint32_t lavc_find_atag(char *codec
)
150 if(! strcasecmp(codec
, "mp2"))
153 if(! strcasecmp(codec
, "mp3"))
156 if(! strcasecmp(codec
, "ac3"))
159 if(! strcasecmp(codec
, "adpcm_ima_wav"))
162 if(! strncasecmp(codec
, "bonk", 4))
170 int mpae_init_lavc(audio_encoder_t
*encoder
)
172 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
;
173 encoder
->params
.bitrate
= encoder
->params
.sample_rate
* encoder
->params
.channels
* 2 * 8;
175 if(!lavc_param_acodec
)
177 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_NoLavcAudioCodecName
);
183 avcodec_register_all();
187 lavc_acodec
= avcodec_find_encoder_by_name(lavc_param_acodec
);
190 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LavcAudioCodecNotFound
, lavc_param_acodec
);
193 if(lavc_param_atag
== 0)
195 #ifdef USE_LIBAVFORMAT
196 lavc_param_atag
= av_codec_get_tag(mp_wav_taglists
, lavc_acodec
->id
);
198 lavc_param_atag
= lavc_find_atag(lavc_param_acodec
);
202 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "Couldn't find wav tag for specified codec, exit\n");
207 lavc_actx
= avcodec_alloc_context();
208 if(lavc_actx
== NULL
)
210 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntAllocateLavcContext
);
214 // put sample parameters
215 lavc_actx
->channels
= encoder
->params
.channels
;
216 lavc_actx
->sample_rate
= encoder
->params
.sample_rate
;
217 if(lavc_param_abitrate
<1000)
218 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
* 1000;
220 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
;
224 * Special case for adpcm_ima_wav.
225 * The bitrate is only dependent on samplerate.
226 * We have to known frame_size and block_align in advance,
227 * so I just copied the code from libavcodec/adpcm.c
229 * However, ms adpcm_ima_wav uses a block_align of 2048,
230 * lavc defaults to 1024
232 if(lavc_param_atag
== 0x11) {
234 int framesize
= (blkalign
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
235 lavc_actx
->bit_rate
= lavc_actx
->sample_rate
*8*blkalign
/framesize
;
237 if((lavc_param_audio_global_header
&1)
238 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
239 lavc_actx
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
241 if(lavc_param_audio_global_header
&2){
242 lavc_actx
->flags2
|= CODEC_FLAG2_LOCAL_HEADER
;
245 if(avcodec_open(lavc_actx
, lavc_acodec
) < 0)
247 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntOpenCodec
, lavc_param_acodec
, lavc_param_abitrate
);
251 if(lavc_param_atag
== 0x11) {
252 lavc_actx
->block_align
= 2048;
253 lavc_actx
->frame_size
= (lavc_actx
->block_align
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
256 encoder
->decode_buffer_size
= lavc_actx
->frame_size
* 2 * encoder
->params
.channels
;
257 encoder
->bind
= bind_lavc
;
258 encoder
->get_frame_size
= get_frame_size
;
259 encoder
->encode
= encode_lavc
;
260 encoder
->close
= close_lavc
;