MAX_PCI_DEVICES 64 is not enough on my system (even though lspci only shows 25 device...
[mplayer/greg.git] / libmpcodecs / ae_lavc.c
blobfe18ab786e6beb75d18aea8d290453933462261f
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 "config.h"
8 #include "m_option.h"
9 #include "mp_msg.h"
10 #include "libmpdemux/aviheader.h"
11 #include "libmpdemux/ms_hdr.h"
12 #include "stream/stream.h"
13 #include "libmpdemux/muxer.h"
14 #include "ae_lavc.h"
15 #include "help_mp.h"
16 #include "libaf/af_format.h"
17 #include "libaf/reorder_ch.h"
18 #include "libavcodec/avcodec.h"
20 static AVCodec *lavc_acodec;
21 static AVCodecContext *lavc_actx;
22 extern char *lavc_param_acodec;
23 extern int lavc_param_abitrate;
24 extern int lavc_param_atag;
25 extern int lavc_param_audio_global_header;
26 extern int avcodec_initialized;
27 static int compressed_frame_size = 0;
28 #ifdef CONFIG_LIBAVFORMAT
29 #include "libavformat/avformat.h"
30 extern const struct AVCodecTag *mp_wav_taglists[];
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->avg_rate= lavc_actx->bit_rate;
41 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
42 if(lavc_actx->block_align)
43 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
44 else
46 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
48 if ((mux_a->wf->nAvgBytesPerSec *
49 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec)
51 mux_a->h.dwScale = lavc_actx->frame_size;
52 mux_a->h.dwRate = lavc_actx->sample_rate;
53 mux_a->h.dwSampleSize = 0; // Blocksize not constant
55 else
56 mux_a->h.dwSampleSize = 0;
58 if(mux_a->h.dwSampleSize)
59 mux_a->wf->nBlockAlign = mux_a->h.dwSampleSize;
60 else
61 mux_a->wf->nBlockAlign = 1;
62 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
63 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
65 switch(lavc_param_atag)
67 case 0x11: /* imaadpcm */
68 mux_a->wf->wBitsPerSample = 4;
69 mux_a->wf->cbSize = 2;
70 ((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] =
71 ((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1;
72 break;
73 case 0x55: /* mp3 */
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;
81 break;
82 default:
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;
89 else
90 mux_a->wf->cbSize = 0;
91 break;
94 // Fix allocation
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;
101 return 1;
104 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
106 int n;
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,
115 size / 2, 2);
117 n = avcodec_encode_audio(lavc_actx, dest, size, src);
118 compressed_frame_size = n;
119 return n;
123 static int close_lavc(audio_encoder_t *encoder)
125 compressed_frame_size = 0;
126 return 1;
129 static int get_frame_size(audio_encoder_t *encoder)
131 int sz = compressed_frame_size;
132 compressed_frame_size = 0;
133 return sz;
136 #ifndef CONFIG_LIBAVFORMAT
137 static uint32_t lavc_find_atag(char *codec)
139 if(codec == NULL)
140 return 0;
142 if(! strcasecmp(codec, "mp2"))
143 return 0x50;
145 if(! strcasecmp(codec, "mp3"))
146 return 0x55;
148 if(! strcasecmp(codec, "ac3"))
149 return 0x2000;
151 if(! strcasecmp(codec, "adpcm_ima_wav"))
152 return 0x11;
154 if(! strncasecmp(codec, "bonk", 4))
155 return 0x2048;
157 return 0;
159 #endif
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);
170 return 0;
173 if(!avcodec_initialized){
174 avcodec_init();
175 avcodec_register_all();
176 avcodec_initialized=1;
179 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
180 if (!lavc_acodec)
182 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
183 return 0;
185 if(lavc_param_atag == 0)
187 #ifdef CONFIG_LIBAVFORMAT
188 lavc_param_atag = av_codec_get_tag(mp_wav_taglists, lavc_acodec->id);
189 #else
190 lavc_param_atag = lavc_find_atag(lavc_param_acodec);
191 #endif
192 if(!lavc_param_atag)
194 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
195 return 0;
199 lavc_actx = avcodec_alloc_context();
200 if(lavc_actx == NULL)
202 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
203 return 0;
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;
211 else
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) {
225 int blkalign = 2048;
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);
240 return 0;
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;
254 return 1;