Mercurial > mplayer.hg
annotate libmpcodecs/ae_lavc.c @ 15267:ed3a0e55240f
(hopefully) better description of slave mode
author | diego |
---|---|
date | Tue, 26 Apr 2005 09:59:16 +0000 |
parents | a8a8a4d69a1c |
children | 7cdc07507650 |
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" |
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 | |
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; | |
29 | |
30 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a) | |
31 { | |
32 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256); | |
33 mux_a->wf->wFormatTag = lavc_param_atag; | |
34 mux_a->wf->nChannels = lavc_actx->channels; | |
35 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate; | |
36 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8); | |
37 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec; | |
38 if(lavc_actx->block_align) | |
39 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align; | |
40 else | |
41 { | |
42 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */ | |
43 | |
44 if ((mux_a->wf->nAvgBytesPerSec * | |
45 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec) | |
46 { | |
47 mux_a->h.dwScale = lavc_actx->frame_size; | |
48 mux_a->h.dwRate = lavc_actx->sample_rate; | |
49 mux_a->h.dwSampleSize = 0; // Blocksize not constant | |
50 } | |
51 else | |
52 mux_a->h.dwSampleSize = mux_a->h.dwScale; | |
53 } | |
54 mux_a->wf->nBlockAlign = mux_a->h.dwScale; | |
55 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000; | |
56 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign; | |
57 | |
58 switch(lavc_param_atag) | |
59 { | |
60 case 0x11: /* imaadpcm */ | |
61 mux_a->wf->wBitsPerSample = 4; | |
62 mux_a->wf->cbSize = 2; | |
63 ((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] = | |
64 ((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1; | |
65 break; | |
66 case 0x55: /* mp3 */ | |
67 mux_a->wf->cbSize = 12; | |
68 mux_a->wf->wBitsPerSample = 0; /* does not apply */ | |
69 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1; | |
70 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2; | |
71 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign; | |
72 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1; | |
73 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0; | |
74 break; | |
75 default: | |
76 mux_a->wf->wBitsPerSample = 0; /* Unknown */ | |
77 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0)) | |
78 { | |
79 memcpy(mux_a->wf+sizeof(WAVEFORMATEX), lavc_actx->extradata, | |
80 lavc_actx->extradata_size); | |
81 mux_a->wf->cbSize = lavc_actx->extradata_size; | |
82 } | |
83 else | |
84 mux_a->wf->cbSize = 0; | |
85 break; | |
86 } | |
87 | |
88 // Fix allocation | |
89 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize); | |
90 | |
91 encoder->input_format = AF_FORMAT_S16_NE; | |
92 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize; | |
93 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2; | |
94 | |
95 return 1; | |
96 } | |
97 | |
98 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size) | |
99 { | |
100 int n; | |
101 n = avcodec_encode_audio(lavc_actx, dest, size, src); | |
102 if(n > compressed_frame_size) | |
103 compressed_frame_size = n; //it's valid because lavc encodes in cbr mode | |
104 return n; | |
105 } | |
106 | |
107 | |
108 static int close_lavc(audio_encoder_t *encoder) | |
109 { | |
110 compressed_frame_size = 0; | |
111 return 1; | |
112 } | |
113 | |
114 static int get_frame_size(audio_encoder_t *encoder) | |
115 { | |
116 return compressed_frame_size; | |
117 } | |
118 | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
119 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
|
120 { |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
121 if(codec == NULL) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
122 return 0; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
123 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
124 if(! strcasecmp(codec, "mp2")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
125 return 0x50; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
126 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
127 if(! strcasecmp(codec, "mp3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
128 return 0x55; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
129 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
130 if(! strcasecmp(codec, "ac3")) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
131 return 0x2000; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
132 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
133 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
|
134 return 0x11; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
135 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
136 if(! strncasecmp(codec, "bonk", 4)) |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
137 return 0x2048; |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
138 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
139 return 0; |
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 |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
142 |
15234 | 143 int mpae_init_lavc(audio_encoder_t *encoder) |
144 { | |
145 encoder->params.samples_per_frame = encoder->params.sample_rate; | |
146 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8; | |
147 | |
148 if(!lavc_param_acodec) | |
149 { | |
150 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName); | |
151 return 0; | |
152 } | |
153 | |
154 if(!avcodec_inited){ | |
155 avcodec_init(); | |
156 avcodec_register_all(); | |
157 avcodec_inited=1; | |
158 } | |
159 | |
160 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec); | |
161 if (!lavc_acodec) | |
162 { | |
163 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec); | |
164 return 0; | |
165 } | |
166 if(lavc_param_atag == 0) | |
167 { | |
15244
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
168 #ifdef USE_LIBAVFORMAT |
15234 | 169 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
|
170 #else |
a8a8a4d69a1c
restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents:
15240
diff
changeset
|
171 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
|
172 #endif |
15234 | 173 if(!lavc_param_atag) |
174 { | |
175 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n"); | |
176 return 0; | |
177 } | |
178 } | |
179 | |
180 lavc_actx = avcodec_alloc_context(); | |
181 if(lavc_actx == NULL) | |
182 { | |
183 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext); | |
184 return 0; | |
185 } | |
186 | |
187 // put sample parameters | |
188 lavc_actx->channels = encoder->params.channels; | |
189 lavc_actx->sample_rate = encoder->params.sample_rate; | |
190 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000; | |
191 | |
192 | |
193 /* | |
194 * Special case for adpcm_ima_wav. | |
195 * The bitrate is only dependant on samplerate. | |
196 * We have to known frame_size and block_align in advance, | |
197 * so I just copied the code from libavcodec/adpcm.c | |
198 * | |
199 * However, ms adpcm_ima_wav uses a block_align of 2048, | |
200 * lavc defaults to 1024 | |
201 */ | |
202 if(lavc_param_atag == 0x11) { | |
203 int blkalign = 2048; | |
204 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; | |
205 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize; | |
206 } | |
207 | |
208 if(avcodec_open(lavc_actx, lavc_acodec) < 0) | |
209 { | |
210 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate); | |
211 return 0; | |
212 } | |
213 | |
214 if(lavc_param_atag == 0x11) { | |
215 lavc_actx->block_align = 2048; | |
216 lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; | |
217 } | |
218 | |
219 encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels; | |
220 encoder->bind = bind_lavc; | |
221 encoder->get_frame_size = get_frame_size; | |
222 encoder->encode = encode_lavc; | |
223 encoder->close = close_lavc; | |
224 | |
225 return 1; | |
226 } | |
227 |