fix
[mplayer/glamo.git] / libmpcodecs / ae_lavc.c
blobeab241ed9bd7a8f846202313f797c72ac2dc48ab
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <inttypes.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include "m_option.h"
8 #include "mp_msg.h"
9 #include "aviheader.h"
10 #include "ms_hdr.h"
11 #include "muxer.h"
12 #include "ae_lavc.h"
13 #include "help_mp.h"
14 #include "config.h"
15 #include "libaf/af_format.h"
16 #ifdef USE_LIBAVCODEC_SO
17 #include <ffmpeg/avcodec.h>
18 #else
19 #include "libavcodec/avcodec.h"
20 #endif
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);
31 #endif
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;
43 else
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
54 else
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;
68 break;
69 case 0x55: /* mp3 */
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;
77 break;
78 default:
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;
85 else
86 mux_a->wf->cbSize = 0;
87 break;
90 // Fix allocation
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;
97 return 1;
100 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
102 int n;
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
106 return n;
110 static int close_lavc(audio_encoder_t *encoder)
112 compressed_frame_size = 0;
113 return 1;
116 static int get_frame_size(audio_encoder_t *encoder)
118 return compressed_frame_size;
121 static uint32_t lavc_find_atag(char *codec)
123 if(codec == NULL)
124 return 0;
126 if(! strcasecmp(codec, "mp2"))
127 return 0x50;
129 if(! strcasecmp(codec, "mp3"))
130 return 0x55;
132 if(! strcasecmp(codec, "ac3"))
133 return 0x2000;
135 if(! strcasecmp(codec, "adpcm_ima_wav"))
136 return 0x11;
138 if(! strncasecmp(codec, "bonk", 4))
139 return 0x2048;
141 return 0;
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);
153 return 0;
156 if(!avcodec_inited){
157 avcodec_init();
158 avcodec_register_all();
159 avcodec_inited=1;
162 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
163 if (!lavc_acodec)
165 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
166 return 0;
168 if(lavc_param_atag == 0)
170 #ifdef USE_LIBAVFORMAT
171 lavc_param_atag = codec_get_wav_tag(lavc_acodec->id);
172 #else
173 lavc_param_atag = lavc_find_atag(lavc_param_acodec);
174 #endif
175 if(!lavc_param_atag)
177 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
178 return 0;
182 lavc_actx = avcodec_alloc_context();
183 if(lavc_actx == NULL)
185 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
186 return 0;
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) {
205 int blkalign = 2048;
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);
213 return 0;
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;
227 return 1;