Mercurial > mplayer.hg
annotate libmpcodecs/ae_lavc.c @ 17268:2ec81c50a763
sync to x264 r390 (bime)
author | lorenm |
---|---|
date | Fri, 30 Dec 2005 05:15:27 +0000 |
parents | c7f6ba8fb63c |
children | 60189cd9bbc8 |
rev | line source |
---|---|
15234 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <inttypes.h> | |
15238 | 4 #include <unistd.h> |
15234 | 5 #include <string.h> |
15240 | 6 #include <sys/types.h> |
15234 | 7 #include "m_option.h" |
17012 | 8 #include "mp_msg.h" |
15234 | 9 #include "aviheader.h" |
10 #include "ms_hdr.h" | |
11 #include "muxer.h" | |
12 #include "ae_lavc.h" | |
13 #include "help_mp.h" | |
17012 | 14 #include "config.h" |
15 #include "libaf/af_format.h" | |
15234 | 16 #ifdef USE_LIBAVCODEC_SO |
17 #include <ffmpeg/avcodec.h> | |
18 #else | |
19 #include "libavcodec/avcodec.h" | |
20 #endif | |
21 | |
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; | |
17096 | 29 #ifdef USE_LIBAVFORMAT |
30 extern unsigned int codec_get_wav_tag(int id); | |
31 #endif | |
15234 | 32 |
33 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a) | |
34 { | |
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 | |
44 { | |
45 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */ | |
46 | |
47 if ((mux_a->wf->nAvgBytesPerSec * | |
48 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec) | |
49 { | |
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 | |
53 } | |
54 else | |
55 mux_a->h.dwSampleSize = mux_a->h.dwScale; | |
56 } | |
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; | |
60 | |
61 switch(lavc_param_atag) | |
62 { | |
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)) | |
81 { | |
15501
7cdc07507650
wrong memcpy of extradata; 10l to whomever wrote that broken code
nicodvb
parents:
15244
diff
changeset
|
82 memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size); |
15234 | 83 mux_a->wf->cbSize = lavc_actx->extradata_size; |
84 } | |
85 else | |
86 mux_a->wf->cbSize = 0; | |
87 break; | |
88 } | |
89 | |
90 // Fix allocation | |
91 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize); | |
92 | |
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; | |
96 | |
97 return 1; | |
98 } | |
99 | |
100 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size) | |
101 { | |
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; | |
107 } | |
108 | |
109 | |
110 static int close_lavc(audio_encoder_t *encoder) | |
111 { | |
112 compressed_frame_size = 0; | |
113 return 1; | |
114 } | |
115 | |
116 static int get_frame_size(audio_encoder_t *encoder) | |
117 { | |
118 return compressed_frame_size; | |
119 } | |
120 | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
121 static uint32_t lavc_find_atag(char *codec) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
122 { |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
123 if(codec == NULL) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
124 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
125 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
126 if(! strcasecmp(codec, "mp2")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
127 return 0x50; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
128 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
129 if(! strcasecmp(codec, "mp3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
130 return 0x55; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
131 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
132 if(! strcasecmp(codec, "ac3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
133 return 0x2000; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
134 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
135 if(! strcasecmp(codec, "adpcm_ima_wav")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
136 return 0x11; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
137 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
138 if(! strncasecmp(codec, "bonk", 4)) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
139 return 0x2048; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
140 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
141 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
142 } |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
143 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
144 |
15234 | 145 int mpae_init_lavc(audio_encoder_t *encoder) |
146 { | |
147 encoder->params.samples_per_frame = encoder->params.sample_rate; | |
148 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8; | |
149 | |
150 if(!lavc_param_acodec) | |
151 { | |
152 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName); | |
153 return 0; | |
154 } | |
155 | |
156 if(!avcodec_inited){ | |
157 avcodec_init(); | |
158 avcodec_register_all(); | |
159 avcodec_inited=1; | |
160 } | |
161 | |
162 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec); | |
163 if (!lavc_acodec) | |
164 { | |
165 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec); | |
166 return 0; | |
167 } | |
168 if(lavc_param_atag == 0) | |
169 { | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
170 #ifdef USE_LIBAVFORMAT |
15234 | 171 lavc_param_atag = codec_get_wav_tag(lavc_acodec->id); |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
172 #else |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
173 lavc_param_atag = lavc_find_atag(lavc_param_acodec); |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
174 #endif |
15234 | 175 if(!lavc_param_atag) |
176 { | |
177 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n"); | |
178 return 0; | |
179 } | |
180 } | |
181 | |
182 lavc_actx = avcodec_alloc_context(); | |
183 if(lavc_actx == NULL) | |
184 { | |
185 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext); | |
186 return 0; | |
187 } | |
188 | |
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; | |
193 | |
194 | |
195 /* | |
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 | |
200 * | |
201 * However, ms adpcm_ima_wav uses a block_align of 2048, | |
202 * lavc defaults to 1024 | |
203 */ | |
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; | |
208 } | |
209 | |
210 if(avcodec_open(lavc_actx, lavc_acodec) < 0) | |
211 { | |
212 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate); | |
213 return 0; | |
214 } | |
215 | |
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; | |
219 } | |
220 | |
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; | |
226 | |
227 return 1; | |
228 } | |
229 |