annotate libmpcodecs/ae_lavc.c @ 33672:e576232a39d5

Prevent balance from hopping. Only recalculate the balance if the balance has changed, not if just the volume has changed. Because (at least with my soundcard) not all volume values can be stored, but seem to be mapped onto a discrete value set, recalculation the balance from the volume isn't accurate enough.
author ib
date Tue, 28 Jun 2011 18:16:06 +0000
parents 5a9d68a07ed1
children 30f5e5cd3676
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30421
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
1 /*
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
2 * This file is part of MPlayer.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
3 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
5 * it under the terms of the GNU General Public License as published by
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
7 * (at your option) any later version.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
8 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
12 * GNU General Public License for more details.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
13 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
14 * You should have received a copy of the GNU General Public License along
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
17 */
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29673
diff changeset
18
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
19 #include <stdio.h>
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
20 #include <stdlib.h>
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
21 #include <inttypes.h>
15238
2cc48c37a7eb FreeBSD fix
nexus
parents: 15234
diff changeset
22 #include <unistd.h>
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
23 #include <string.h>
15240
6d6792c02b6f macosx compilation fix
nicodvb
parents: 15238
diff changeset
24 #include <sys/types.h>
26203
0d255d03016f #include config.h before all other headers.
diego
parents: 26069
diff changeset
25 #include "config.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
26 #include "m_option.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
27 #include "mp_msg.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
28 #include "libmpdemux/aviheader.h"
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
29 #include "libmpdemux/ms_hdr.h"
22600
3c2b4a866c6a Add explicit location for headers from the stream/ directory.
diego
parents: 22316
diff changeset
30 #include "stream/stream.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22600
diff changeset
31 #include "libmpdemux/muxer.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
32 #include "ae_lavc.h"
31959
f957f330aa6d Introduce init_avcodec function to avoid duplicated FFmpeg initializations.
diego
parents: 31589
diff changeset
33 #include "vd_ffmpeg.h"
32017
922a375ec4af Move lavc_param_* extern declarations to ve.h.
diego
parents: 32016
diff changeset
34 #include "ve.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
35 #include "help_mp.h"
31589
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
36 #include "av_opts.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 15501
diff changeset
37 #include "libaf/af_format.h"
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
38 #include "libaf/reorder_ch.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
39 #include "libavcodec/avcodec.h"
28132
c28c36d595d5 Fix imaadpcm extradata with lavc encoder.
reimar
parents: 27341
diff changeset
40 #include "libavutil/intreadwrite.h"
32142
4614728cab25 build system: Merge all FFmpeg library checks into a single FFmpeg check.
diego
parents: 32017
diff changeset
41 #include "libavformat/avformat.h"
4614728cab25 build system: Merge all FFmpeg library checks into a single FFmpeg check.
diego
parents: 32017
diff changeset
42 #include "libmpdemux/mp_taglists.h"
32907
44f4ead8fdb0 Start adding support for lavc audio encoders that use other formats
reimar
parents: 32768
diff changeset
43 #include "fmt-conversion.h"
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
44
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
45 static AVCodec *lavc_acodec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
46 static AVCodecContext *lavc_actx;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
47 static int compressed_frame_size = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
48
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
49 static int bind_lavc(audio_encoder_t *encoder, muxer_stream_t *mux_a)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
50 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
51 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
52 mux_a->wf->wFormatTag = lavc_param_atag;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
53 mux_a->wf->nChannels = lavc_actx->channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
54 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
55 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
19360
d4d72e5eea07 pass average bitrate from encoder to (lavf) muxer
michael
parents: 17855
diff changeset
56 mux_a->avg_rate= lavc_actx->bit_rate;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
57 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
58 if(lavc_actx->block_align)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
59 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
60 else
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
61 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
62 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
63
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
64 if ((mux_a->wf->nAvgBytesPerSec *
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
65 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec)
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
66 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
67 mux_a->h.dwScale = lavc_actx->frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
68 mux_a->h.dwRate = lavc_actx->sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
69 mux_a->h.dwSampleSize = 0; // Blocksize not constant
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
70 }
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
71 else
21773
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
72 mux_a->h.dwSampleSize = 0;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
73 }
21773
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
74 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
75 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
76 else
03bc990498fc the avi spec does not allow random samplesize whoever wrote this should be shot
michael
parents: 21771
diff changeset
77 mux_a->wf->nBlockAlign = 1;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
78 mux_a->h.dwSuggestedBufferSize = (encoder->params.audio_preload*mux_a->wf->nAvgBytesPerSec)/1000;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
79 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
80
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
81 switch(lavc_param_atag)
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
82 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
83 case 0x11: /* imaadpcm */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
84 mux_a->wf->wBitsPerSample = 4;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
85 mux_a->wf->cbSize = 2;
28132
c28c36d595d5 Fix imaadpcm extradata with lavc encoder.
reimar
parents: 27341
diff changeset
86 AV_WL16(mux_a->wf+1, lavc_actx->frame_size);
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
87 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
88 case 0x55: /* mp3 */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
89 mux_a->wf->cbSize = 12;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
90 mux_a->wf->wBitsPerSample = 0; /* does not apply */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
91 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
92 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
93 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
94 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
95 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
96 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
97 default:
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
98 mux_a->wf->wBitsPerSample = 0; /* Unknown */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
99 if (lavc_actx->extradata && (lavc_actx->extradata_size > 0))
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
100 {
15501
7cdc07507650 wrong memcpy of extradata; 10l to whomever wrote that broken code
nicodvb
parents: 15244
diff changeset
101 memcpy(mux_a->wf+1, lavc_actx->extradata, lavc_actx->extradata_size);
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
102 mux_a->wf->cbSize = lavc_actx->extradata_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
103 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
104 else
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
105 mux_a->wf->cbSize = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
106 break;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
107 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
108
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
109 // Fix allocation
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
110 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
111
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
112 encoder->min_buffer_size = mux_a->h.dwSuggestedBufferSize;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
113 encoder->max_buffer_size = mux_a->h.dwSuggestedBufferSize*2;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
114
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
115 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
116 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
117
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
118 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
119 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
120 int n;
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
121 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
122 (!strcmp(lavc_acodec->name,"ac3") ||
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
123 !strcmp(lavc_acodec->name,"libfaac"))) {
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
124 int isac3 = !strcmp(lavc_acodec->name,"ac3");
33501
3209d6cbc393 Fix encoding to formats taking input other than 16 bits per sample.
reimar
parents: 33500
diff changeset
125 int bps = av_get_bytes_per_sample(lavc_actx->sample_fmt);
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
126 reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
29491
99eda963d27a Fix incorrect channel ordering for lavc audio codecs (specifically ffac3,
tack
parents: 29263
diff changeset
127 isac3 ? AF_CHANNEL_LAYOUT_LAVC_DEFAULT
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
128 : AF_CHANNEL_LAYOUT_AAC_DEFAULT,
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
129 encoder->params.channels,
33501
3209d6cbc393 Fix encoding to formats taking input other than 16 bits per sample.
reimar
parents: 33500
diff changeset
130 size / bps, bps);
25315
dfa8a510c81c Fix all current known multi-channel wrong order problems by adding
ulion
parents: 24359
diff changeset
131 }
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
132 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
133 compressed_frame_size = n;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
134 return n;
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
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
138 static int close_lavc(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
139 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
140 compressed_frame_size = 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
141 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
142 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
143
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
144 static int get_frame_size(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
145 {
17855
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
146 int sz = compressed_frame_size;
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
147 compressed_frame_size = 0;
4b1930c9345c do not randomly chop up packets, this isnt allowed in almost no container
michael
parents: 17842
diff changeset
148 return sz;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
149 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
150
15244
a8a8a4d69a1c restore old lavc_find_atag to be used when compiling mplayer without libavformat
nicodvb
parents: 15240
diff changeset
151
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
152 int mpae_init_lavc(audio_encoder_t *encoder)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
153 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
154 encoder->params.samples_per_frame = encoder->params.sample_rate;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
155 encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
156
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
157 if(!lavc_param_acodec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
158 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
159 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
160 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
161 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
162
31959
f957f330aa6d Introduce init_avcodec function to avoid duplicated FFmpeg initializations.
diego
parents: 31589
diff changeset
163 init_avcodec();
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
164
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
165 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
166 if (!lavc_acodec)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
167 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
168 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
169 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
170 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
171 if(lavc_param_atag == 0)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
172 {
21966
d9494ca70ca7 Simplify by using av_codec_get_id and include riff.h only in demux_lavf.c
reimar
parents: 21963
diff changeset
173 lavc_param_atag = av_codec_get_tag(mp_wav_taglists, lavc_acodec->id);
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
174 if(!lavc_param_atag)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
175 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
176 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n");
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
177 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
178 }
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 lavc_actx = avcodec_alloc_context();
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
182 if(lavc_actx == NULL)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
183 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
184 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
185 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
186 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
187
32768
3544ba7244bf Change deprecated PKT_FLAG_KEY, CODEC_TYPE_* and SAMPLE_FMT_* to their
reimar
parents: 32656
diff changeset
188 lavc_actx->codec_type = AVMEDIA_TYPE_AUDIO;
29673
a3cc38ad5878 Set codec_type and codec_id in codec context for lavc encoders.
reimar
parents: 29491
diff changeset
189 lavc_actx->codec_id = lavc_acodec->id;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
190 // put sample parameters
33500
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
191 lavc_actx->sample_fmt = AV_SAMPLE_FMT_S16;
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
192 if (lavc_acodec->sample_fmts) {
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
193 const enum AVSampleFormat *fmts;
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
194 lavc_actx->sample_fmt = lavc_acodec->sample_fmts[0]; // fallback to first format
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
195 for (fmts = lavc_acodec->sample_fmts; *fmts != AV_SAMPLE_FMT_NONE; fmts++) {
33504
5a9d68a07ed1 Pass "native" filter chain sample format to audio encoder.
reimar
parents: 33502
diff changeset
196 if (samplefmt2affmt(*fmts) == encoder->params.sample_format) { // preferred format found
33500
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
197 lavc_actx->sample_fmt = *fmts;
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
198 break;
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
199 }
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
200 }
c36fec5ba5b6 Always choose AV_SAMPLE_FMT_S16 then the encoder supports it.
reimar
parents: 33045
diff changeset
201 }
32907
44f4ead8fdb0 Start adding support for lavc audio encoders that use other formats
reimar
parents: 32768
diff changeset
202 encoder->input_format = samplefmt2affmt(lavc_actx->sample_fmt);
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
203 lavc_actx->channels = encoder->params.channels;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
204 lavc_actx->sample_rate = encoder->params.sample_rate;
28660
0812215421d7 Set time_base to 1/samplerate, like FFmpeg does, instead of leaving it at the
diego
parents: 28132
diff changeset
205 lavc_actx->time_base.num = 1;
0812215421d7 Set time_base to 1/samplerate, like FFmpeg does, instead of leaving it at the
diego
parents: 28132
diff changeset
206 lavc_actx->time_base.den = encoder->params.sample_rate;
23957
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
207 if(lavc_param_abitrate<1000)
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
208 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
209 else
74eb6825d332 allow to specify the audio bitrate in bits (some codecs need that ...)
michael
parents: 23121
diff changeset
210 lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate;
31589
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
211 if(lavc_param_audio_avopt){
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
212 if(parse_avopts(lavc_actx, lavc_param_audio_avopt) < 0){
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
213 mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_audio_avopt);
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
214 return 0;
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
215 }
ef92321146a1 Make it possible to set all options for lavc audio encoder.
reimar
parents: 30633
diff changeset
216 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28928
diff changeset
217
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
218
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
219 /*
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
220 * Special case for adpcm_ima_wav.
22316
f3d7a1b58a82 cosmetics: Fix some common typos, appropiate --> appropRiate,
diego
parents: 21966
diff changeset
221 * The bitrate is only dependent on samplerate.
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
222 * We have to known frame_size and block_align in advance,
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
223 * so I just copied the code from libavcodec/adpcm.c
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
224 *
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
225 * However, ms adpcm_ima_wav uses a block_align of 2048,
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
226 * lavc defaults to 1024
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
227 */
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
228 if(lavc_param_atag == 0x11) {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
229 int blkalign = 2048;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
230 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
231 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
232 }
17842
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
233 if((lavc_param_audio_global_header&1)
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
234 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
235 lavc_actx->flags |= CODEC_FLAG_GLOBAL_HEADER;
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
236 }
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
237 if(lavc_param_audio_global_header&2){
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
238 lavc_actx->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
7d5343254436 -lavcopts aglobal
michael
parents: 17354
diff changeset
239 }
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
240
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
241 if(avcodec_open(lavc_actx, lavc_acodec) < 0)
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
242 {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
243 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate);
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
244 return 0;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
245 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
246
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
247 if(lavc_param_atag == 0x11) {
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
248 lavc_actx->block_align = 2048;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
249 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
250 }
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
251
33501
3209d6cbc393 Fix encoding to formats taking input other than 16 bits per sample.
reimar
parents: 33500
diff changeset
252 encoder->decode_buffer_size = lavc_actx->frame_size *
3209d6cbc393 Fix encoding to formats taking input other than 16 bits per sample.
reimar
parents: 33500
diff changeset
253 av_get_bytes_per_sample(lavc_actx->sample_fmt) *
3209d6cbc393 Fix encoding to formats taking input other than 16 bits per sample.
reimar
parents: 33500
diff changeset
254 encoder->params.channels;
28928
f2b3f28982b8 Avoid ridiculously small decode_buffer_size (e.g. 4 with acodec=pcm_s16le)
reimar
parents: 28660
diff changeset
255 while (encoder->decode_buffer_size < 1024) encoder->decode_buffer_size *= 2;
15234
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
256 encoder->bind = bind_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
257 encoder->get_frame_size = get_frame_size;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
258 encoder->encode = encode_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
259 encoder->close = close_lavc;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
260
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
261 return 1;
e84a6ae9d51e audio encoding reworked
nicodvb
parents:
diff changeset
262 }