15 #include "libaf/af_format.h"
16 #ifdef USE_LIBAVCODEC_SO
17 #include <ffmpeg/avcodec.h>
19 #include "libavcodec/avcodec.h"
22 static AVCodec
*lavc_acodec
;
23 static AVCodecContext
*lavc_actx
;
24 extern char *lavc_param_acodec
;
25 extern int lavc_param_abitrate
;
26 extern int lavc_param_atag
;
27 extern int avcodec_inited
;
28 static int compressed_frame_size
= 0;
29 #ifdef USE_LIBAVFORMAT
30 extern unsigned int codec_get_wav_tag(int id
);
33 static int bind_lavc(audio_encoder_t
*encoder
, muxer_stream_t
*mux_a
)
35 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
)+lavc_actx
->extradata_size
+256);
36 mux_a
->wf
->wFormatTag
= lavc_param_atag
;
37 mux_a
->wf
->nChannels
= lavc_actx
->channels
;
38 mux_a
->wf
->nSamplesPerSec
= lavc_actx
->sample_rate
;
39 mux_a
->wf
->nAvgBytesPerSec
= (lavc_actx
->bit_rate
/ 8);
40 mux_a
->h
.dwRate
= mux_a
->wf
->nAvgBytesPerSec
;
41 if(lavc_actx
->block_align
)
42 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
= lavc_actx
->block_align
;
45 mux_a
->h
.dwScale
= (mux_a
->wf
->nAvgBytesPerSec
* lavc_actx
->frame_size
)/ mux_a
->wf
->nSamplesPerSec
; /* for cbr */
47 if ((mux_a
->wf
->nAvgBytesPerSec
*
48 lavc_actx
->frame_size
) % mux_a
->wf
->nSamplesPerSec
)
50 mux_a
->h
.dwScale
= lavc_actx
->frame_size
;
51 mux_a
->h
.dwRate
= lavc_actx
->sample_rate
;
52 mux_a
->h
.dwSampleSize
= 0; // Blocksize not constant
55 mux_a
->h
.dwSampleSize
= mux_a
->h
.dwScale
;
57 mux_a
->wf
->nBlockAlign
= mux_a
->h
.dwScale
;
58 mux_a
->h
.dwSuggestedBufferSize
= (encoder
->params
.audio_preload
*mux_a
->wf
->nAvgBytesPerSec
)/1000;
59 mux_a
->h
.dwSuggestedBufferSize
-= mux_a
->h
.dwSuggestedBufferSize
% mux_a
->wf
->nBlockAlign
;
61 switch(lavc_param_atag
)
63 case 0x11: /* imaadpcm */
64 mux_a
->wf
->wBitsPerSample
= 4;
65 mux_a
->wf
->cbSize
= 2;
66 ((uint16_t*)mux_a
->wf
)[sizeof(WAVEFORMATEX
)] =
67 ((lavc_actx
->block_align
- 4 * lavc_actx
->channels
) / (4 * lavc_actx
->channels
)) * 8 + 1;
70 mux_a
->wf
->cbSize
= 12;
71 mux_a
->wf
->wBitsPerSample
= 0; /* does not apply */
72 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->wID
= 1;
73 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->fdwFlags
= 2;
74 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nBlockSize
= mux_a
->wf
->nBlockAlign
;
75 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nFramesPerBlock
= 1;
76 ((MPEGLAYER3WAVEFORMAT
*) (mux_a
->wf
))->nCodecDelay
= 0;
79 mux_a
->wf
->wBitsPerSample
= 0; /* Unknown */
80 if (lavc_actx
->extradata
&& (lavc_actx
->extradata_size
> 0))
82 memcpy(mux_a
->wf
+1, lavc_actx
->extradata
, lavc_actx
->extradata_size
);
83 mux_a
->wf
->cbSize
= lavc_actx
->extradata_size
;
86 mux_a
->wf
->cbSize
= 0;
91 mux_a
->wf
= realloc(mux_a
->wf
, sizeof(WAVEFORMATEX
)+mux_a
->wf
->cbSize
);
93 encoder
->input_format
= AF_FORMAT_S16_NE
;
94 encoder
->min_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
;
95 encoder
->max_buffer_size
= mux_a
->h
.dwSuggestedBufferSize
*2;
100 static int encode_lavc(audio_encoder_t
*encoder
, uint8_t *dest
, void *src
, int size
, int max_size
)
103 n
= avcodec_encode_audio(lavc_actx
, dest
, size
, src
);
104 if(n
> compressed_frame_size
)
105 compressed_frame_size
= n
; //it's valid because lavc encodes in cbr mode
110 static int close_lavc(audio_encoder_t
*encoder
)
112 compressed_frame_size
= 0;
116 static int get_frame_size(audio_encoder_t
*encoder
)
118 return compressed_frame_size
;
121 static uint32_t lavc_find_atag(char *codec
)
126 if(! strcasecmp(codec
, "mp2"))
129 if(! strcasecmp(codec
, "mp3"))
132 if(! strcasecmp(codec
, "ac3"))
135 if(! strcasecmp(codec
, "adpcm_ima_wav"))
138 if(! strncasecmp(codec
, "bonk", 4))
145 int mpae_init_lavc(audio_encoder_t
*encoder
)
147 encoder
->params
.samples_per_frame
= encoder
->params
.sample_rate
;
148 encoder
->params
.bitrate
= encoder
->params
.sample_rate
* encoder
->params
.channels
* 2 * 8;
150 if(!lavc_param_acodec
)
152 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_NoLavcAudioCodecName
);
158 avcodec_register_all();
162 lavc_acodec
= avcodec_find_encoder_by_name(lavc_param_acodec
);
165 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_LavcAudioCodecNotFound
, lavc_param_acodec
);
168 if(lavc_param_atag
== 0)
170 #ifdef USE_LIBAVFORMAT
171 lavc_param_atag
= codec_get_wav_tag(lavc_acodec
->id
);
173 lavc_param_atag
= lavc_find_atag(lavc_param_acodec
);
177 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, "Couldn't find wav tag for specified codec, exit\n");
182 lavc_actx
= avcodec_alloc_context();
183 if(lavc_actx
== NULL
)
185 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntAllocateLavcContext
);
189 // put sample parameters
190 lavc_actx
->channels
= encoder
->params
.channels
;
191 lavc_actx
->sample_rate
= encoder
->params
.sample_rate
;
192 lavc_actx
->bit_rate
= encoder
->params
.bitrate
= lavc_param_abitrate
* 1000;
196 * Special case for adpcm_ima_wav.
197 * The bitrate is only dependant on samplerate.
198 * We have to known frame_size and block_align in advance,
199 * so I just copied the code from libavcodec/adpcm.c
201 * However, ms adpcm_ima_wav uses a block_align of 2048,
202 * lavc defaults to 1024
204 if(lavc_param_atag
== 0x11) {
206 int framesize
= (blkalign
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
207 lavc_actx
->bit_rate
= lavc_actx
->sample_rate
*8*blkalign
/framesize
;
210 if(avcodec_open(lavc_actx
, lavc_acodec
) < 0)
212 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CouldntOpenCodec
, lavc_param_acodec
, lavc_param_abitrate
);
216 if(lavc_param_atag
== 0x11) {
217 lavc_actx
->block_align
= 2048;
218 lavc_actx
->frame_size
= (lavc_actx
->block_align
- 4 * lavc_actx
->channels
) * 8 / (4 * lavc_actx
->channels
) + 1;
221 encoder
->decode_buffer_size
= lavc_actx
->frame_size
* 2 * encoder
->params
.channels
;
222 encoder
->bind
= bind_lavc
;
223 encoder
->get_frame_size
= get_frame_size
;
224 encoder
->encode
= encode_lavc
;
225 encoder
->close
= close_lavc
;