annotate libmpcodecs/ae_lavc.c @ 25376:382aeacc771f

The buffer used for pread need be aligned, but currently it got an offset 23 to the structure head. This will cause the pread always got random data on some machines (such as my iMac G5 PPC with 10.5 os) so can not play vcd. I also tried use DKIOCCDREAD ioctl call, but the result is same -- buffer need be aligned. It could be a bug of os x or its dev lib. Now fix this problem by move the buffer to a good aligned position in structure.
author ulion
date Sat, 15 Dec 2007 12:17:51 +0000
parents dfa8a510c81c
children afa125da85cf
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"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
9 #include "libmpdemux/aviheader.h"
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
10 #include "libmpdemux/ms_hdr.h"
22600
3c2b4a866c6a Add explicit location for headers from the stream/ directory.
diego
parents: 22316
diff changeset
11 #include "stream/stream.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
12 #include "libmpdemux/muxer.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
13 #include "ae_lavc.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
14 #include "help_mp.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
15 #include "config.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
16 #include "libaf/af_format.h"
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
17 #include "libaf/reorder_ch.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
18 #ifdef USE_LIBAVCODEC_SO
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
19 #include <ffmpeg/avcodec.h>
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
20 #else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
21 #include "libavcodec/avcodec.h"
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
22 #endif
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
23
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
24 static AVCodec *lavc_acodec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
25 static AVCodecContext *lavc_actx;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
26 extern char *lavc_param_acodec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
27 extern int lavc_param_abitrate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
28 extern int lavc_param_atag;
17842
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
29 extern int lavc_param_audio_global_header;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
30 extern int avcodec_inited;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
31 static int compressed_frame_size = 0;
23121
a257dd426da5 Simplify preprocessor directives: There is a general variable for
diego
parents: 22601
diff changeset
32 #ifdef USE_LIBAVFORMAT
21820
cd705f30bb31 Include libavformat/riff.h when building with static libavformat and
reimar
parents: 21773
diff changeset
33 #ifdef USE_LIBAVFORMAT_SO
cd705f30bb31 Include libavformat/riff.h when building with static libavformat and
reimar
parents: 21773
diff changeset
34 #include <ffmpeg/avformat.h>
cd705f30bb31 Include libavformat/riff.h when building with static libavformat and
reimar
parents: 21773
diff changeset
35 #else
cd705f30bb31 Include libavformat/riff.h when building with static libavformat and
reimar
parents: 21773
diff changeset
36 #include "libavformat/avformat.h"
21822
fc4ce8a91a2e Include "internal" libavformat/riff.h also when dynamic libavformat is used
reimar
parents: 21820
diff changeset
37 #endif
21966
d9494ca70ca7 Simplify by using av_codec_get_id and include riff.h only in demux_lavf.c
reimar
parents: 21963
diff changeset
38 extern const struct AVCodecTag *mp_wav_taglists[];
17096
rathann
parents: 17012
diff changeset
39 #endif
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
40
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
41 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
42 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
43 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
44 mux_a->wf->wFormatTag = lavc_param_atag;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
45 mux_a->wf->nChannels = lavc_actx->channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
46 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
47 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
19360
d4d72e5eea07 pass average bitrate from encoder to (lavf) muxer
michael
parents: 17855
diff changeset
48 mux_a->avg_rate= lavc_actx->bit_rate;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
49 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
50 if(lavc_actx->block_align)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
51 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
52 else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
53 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
54 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
55
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
56 if ((mux_a->wf->nAvgBytesPerSec *
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
57 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
58 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
59 mux_a->h.dwScale = lavc_actx->frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
60 mux_a->h.dwRate = lavc_actx->sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
61 mux_a->h.dwSampleSize = 0; // Blocksize not constant
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
62 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
63 else
21773
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
64 mux_a->h.dwSampleSize = 0;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
65 }
21773
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
66 if(mux_a->h.dwSampleSize)
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
67 mux_a->wf->nBlockAlign = mux_a->h.dwSampleSize;
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
68 else
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
69 mux_a->wf->nBlockAlign = 1;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
70 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
71 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
72
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
73 switch(lavc_param_atag)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
74 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
75 case 0x11: /* imaadpcm */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
76 mux_a->wf->wBitsPerSample = 4;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
77 mux_a->wf->cbSize = 2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
78 ((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] =
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
79 ((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
80 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
81 case 0x55: /* mp3 */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
82 mux_a->wf->cbSize = 12;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
83 mux_a->wf->wBitsPerSample = 0; /* does not apply */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
84 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
85 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
86 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
87 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
88 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
89 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
90 default:
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
91 mux_a->wf->wBitsPerSample = 0; /* Unknown */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
92 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0))
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
93 {
15501
7cdc07507650 wrong memcpy of extradata; 10l to whomever wrote that broken code
nicodvb
parents: 15244
diff changeset
94 memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size);
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
95 mux_a->wf->cbSize = lavc_actx->extradata_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
96 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
97 else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
98 mux_a->wf->cbSize = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
99 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
100 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
101
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
102 // Fix allocation
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
103 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
104
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
105 encoder->input_format = AF_FORMAT_S16_NE;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
106 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
107 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
108
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
109 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
110 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
111
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
112 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
113 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
114 int n;
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
115 if ((encoder->params.channels == 6 || encoder->params.channels == 5) &&
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
116 (!strcmp(lavc_acodec->name,"ac3") ||
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
117 !strcmp(lavc_acodec->name,"libfaac"))) {
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
118 int isac3 = !strcmp(lavc_acodec->name,"ac3");
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
119 reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
120 isac3 ? AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
121 : AF_CHANNEL_LAYOUT_AAC_DEFAULT,
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
122 encoder->params.channels,
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
123 size / 2, 2);
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
124 }
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
125 n = avcodec_encode_audio(lavc_actx, dest, size, src);
17855
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
126 compressed_frame_size = n;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
127 return n;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
128 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
129
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
130
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
131 static int close_lavc(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
132 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
133 compressed_frame_size = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
134 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
135 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
136
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
137 static int get_frame_size(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
138 {
17855
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
139 int sz = compressed_frame_size;
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
140 compressed_frame_size = 0;
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
141 return sz;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
142 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
143
24359
30b770392f2c warning fix:
diego
parents: 23957
diff changeset
144 #ifndef USE_LIBAVFORMAT
15244
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
145 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
146 {
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
147 if(codec == NULL)
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
148 return 0;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
149
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
150 if(! strcasecmp(codec, "mp2"))
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
151 return 0x50;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
152
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
153 if(! strcasecmp(codec, "mp3"))
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
154 return 0x55;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
155
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
156 if(! strcasecmp(codec, "ac3"))
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
157 return 0x2000;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
158
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
159 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
160 return 0x11;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
161
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
162 if(! strncasecmp(codec, "bonk", 4))
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
163 return 0x2048;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
164
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
165 return 0;
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
166 }
24359
30b770392f2c warning fix:
diego
parents: 23957
diff changeset
167 #endif
15244
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
168
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
169
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
170 int mpae_init_lavc(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
171 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
172 encoder->params.samples_per_frame = encoder->params.sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
173 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
174
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
175 if(!lavc_param_acodec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
176 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
177 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName);
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 if(!avcodec_inited){
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
182 avcodec_init();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
183 avcodec_register_all();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
184 avcodec_inited=1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
185 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
186
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
187 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
188 if (!lavc_acodec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
189 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
190 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
191 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
192 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
193 if(lavc_param_atag == 0)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
194 {
23121
a257dd426da5 Simplify preprocessor directives: There is a general variable for
diego
parents: 22601
diff changeset
195 #ifdef USE_LIBAVFORMAT
21966
d9494ca70ca7 Simplify by using av_codec_get_id and include riff.h only in demux_lavf.c
reimar
parents: 21963
diff changeset
196 lavc_param_atag = av_codec_get_tag(mp_wav_taglists, lavc_acodec->id);
15244
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
197 #else
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
198 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
199 #endif
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
200 if(!lavc_param_atag)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
201 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
202 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
203 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
204 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
205 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
206
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
207 lavc_actx = avcodec_alloc_context();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
208 if(lavc_actx == NULL)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
209 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
210 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
211 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
212 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
213
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
214 // put sample parameters
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
215 lavc_actx->channels = encoder->params.channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
216 lavc_actx->sample_rate = encoder->params.sample_rate;
23957
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
217 if(lavc_param_abitrate<1000)
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
218 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000;
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
219 else
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
220 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
221
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
222
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
223 /*
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
224 * Special case for adpcm_ima_wav.
22316
f3d7a1b58a82 cosmetics: Fix some common typos, appropiate --> appropRiate,
diego
parents: 21966
diff changeset
225 * The bitrate is only dependent on samplerate.
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
226 * We have to known frame_size and block_align in advance,
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
227 * so I just copied the code from libavcodec/adpcm.c
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
228 *
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
229 * However, ms adpcm_ima_wav uses a block_align of 2048,
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
230 * lavc defaults to 1024
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
231 */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
232 if(lavc_param_atag == 0x11) {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
233 int blkalign = 2048;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
234 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
235 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
236 }
17842
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
237 if((lavc_param_audio_global_header&1)
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
238 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
239 lavc_actx->flags |= CODEC_FLAG_GLOBAL_HEADER;
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
240 }
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
241 if(lavc_param_audio_global_header&2){
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
242 lavc_actx->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
243 }
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
244
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
245 if(avcodec_open(lavc_actx, lavc_acodec) < 0)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
246 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
247 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
248 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
249 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
250
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
251 if(lavc_param_atag == 0x11) {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
252 lavc_actx->block_align = 2048;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
253 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
254 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
255
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
256 encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
257 encoder->bind = bind_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
258 encoder->get_frame_size = get_frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
259 encoder->encode = encode_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
260 encoder->close = close_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
261
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
262 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
263 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
264