annotate libmpcodecs/ae_lavc.c @ 17509:2de922240f73

synced with 1.216
author gabrov
date Mon, 30 Jan 2006 00:08:04 +0000
parents 60189cd9bbc8
children 7d5343254436
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
1 #include <stdio.h>
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
2 #include <stdlib.h>
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
3 #include <inttypes.h>
15238
2cc48c37a7eb FreeBSD fix
nexus
parents: 15234
diff changeset
4 #include <unistd.h>
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
5 #include <string.h>
15240
6d6792c02b6f macosx compilation fix
nicodvb
parents: 15238
diff changeset
6 #include <sys/types.h>
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
7 #include "m_option.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
8 #include "mp_msg.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
9 #include "aviheader.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
10 #include "ms_hdr.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
11 #include "muxer.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
12 #include "ae_lavc.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
13 #include "help_mp.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
14 #include "config.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
15 #include "libaf/af_format.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
16 #ifdef USE_LIBAVCODEC_SO
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
17 #include <ffmpeg/avcodec.h>
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
18 #else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
19 #include "libavcodec/avcodec.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
20 #endif
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
21
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
22 static AVCodec *lavc_acodec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
23 static AVCodecContext *lavc_actx;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
24 extern char *lavc_param_acodec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
25 extern int lavc_param_abitrate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
26 extern int lavc_param_atag;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
27 extern int avcodec_inited;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
28 static int compressed_frame_size = 0;
17354
60189cd9bbc8 added support for external libavformat
nicodvb
parents: 17096
diff changeset
29 #if defined(USE_LIBAVFORMAT) || defined(USE_LIBAVFORMAT_SO)
17096
rathann
parents: 17012
diff changeset
30 extern unsigned int codec_get_wav_tag(int id);
rathann
parents: 17012
diff changeset
31 #endif
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
32
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
33 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
34 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
35 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
36 mux_a->wf->wFormatTag = lavc_param_atag;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
37 mux_a->wf->nChannels = lavc_actx->channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
38 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
39 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
40 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
41 if(lavc_actx->block_align)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
42 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
43 else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
44 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
45 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
46
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
47 if ((mux_a->wf->nAvgBytesPerSec *
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
48 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
49 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
50 mux_a->h.dwScale = lavc_actx->frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
51 mux_a->h.dwRate = lavc_actx->sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
52 mux_a->h.dwSampleSize = 0; // Blocksize not constant
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
53 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
54 else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
55 mux_a->h.dwSampleSize = mux_a->h.dwScale;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
56 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
57 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
58 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
59 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
60
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
61 switch(lavc_param_atag)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
62 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
63 case 0x11: /* imaadpcm */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
64 mux_a->wf->wBitsPerSample = 4;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
65 mux_a->wf->cbSize = 2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
66 ((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] =
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
67 ((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
68 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
69 case 0x55: /* mp3 */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
70 mux_a->wf->cbSize = 12;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
71 mux_a->wf->wBitsPerSample = 0; /* does not apply */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
72 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
73 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
74 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
75 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
76 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
77 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
78 default:
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
79 mux_a->wf->wBitsPerSample = 0; /* Unknown */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
80 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0))
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
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
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
83 mux_a->wf->cbSize = lavc_actx->extradata_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
84 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
85 else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
86 mux_a->wf->cbSize = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
87 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
88 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
89
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
90 // Fix allocation
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
91 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
92
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
93 encoder->input_format = AF_FORMAT_S16_NE;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
94 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
95 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
96
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
97 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
98 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
99
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
100 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
101 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
102 int n;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
103 n = avcodec_encode_audio(lavc_actx, dest, size, src);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
104 if(n > compressed_frame_size)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
105 compressed_frame_size = n; //it's valid because lavc encodes in cbr mode
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
106 return n;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
107 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
108
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
109
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
110 static int close_lavc(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
111 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
112 compressed_frame_size = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
113 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
114 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
115
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
116 static int get_frame_size(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
117 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
118 return compressed_frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
119 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
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
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
145 int mpae_init_lavc(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
146 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
147 encoder->params.samples_per_frame = encoder->params.sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
148 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
149
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
150 if(!lavc_param_acodec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
151 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
152 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
153 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
154 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
155
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
156 if(!avcodec_inited){
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
157 avcodec_init();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
158 avcodec_register_all();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
159 avcodec_inited=1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
160 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
161
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
162 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
163 if (!lavc_acodec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
164 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
165 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
166 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
167 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
168 if(lavc_param_atag == 0)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
169 {
17354
60189cd9bbc8 added support for external libavformat
nicodvb
parents: 17096
diff changeset
170 #if defined(USE_LIBAVFORMAT) || defined(USE_LIBAVFORMAT_SO)
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
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
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
175 if(!lavc_param_atag)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
176 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
177 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
178 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
179 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
180 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
181
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
182 lavc_actx = avcodec_alloc_context();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
183 if(lavc_actx == NULL)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
184 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
185 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
186 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
187 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
188
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
189 // put sample parameters
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
190 lavc_actx->channels = encoder->params.channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
191 lavc_actx->sample_rate = encoder->params.sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
192 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
193
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
194
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
195 /*
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
196 * Special case for adpcm_ima_wav.
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
197 * The bitrate is only dependant on samplerate.
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
198 * We have to known frame_size and block_align in advance,
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
199 * so I just copied the code from libavcodec/adpcm.c
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
200 *
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
201 * However, ms adpcm_ima_wav uses a block_align of 2048,
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
202 * lavc defaults to 1024
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
203 */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
204 if(lavc_param_atag == 0x11) {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
205 int blkalign = 2048;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
206 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
207 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
208 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
209
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
210 if(avcodec_open(lavc_actx, lavc_acodec) < 0)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
211 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
212 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
213 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
214 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
215
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
216 if(lavc_param_atag == 0x11) {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
217 lavc_actx->block_align = 2048;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
218 lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
219 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
220
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
221 encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
222 encoder->bind = bind_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
223 encoder->get_frame_size = get_frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
224 encoder->encode = encode_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
225 encoder->close = close_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
226
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
227 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
228 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
229