comparison mencoder.c @ 13426:0f92ad66bbb5

encoding to mp2 with libtoolame
author nicodvb
date Tue, 21 Sep 2004 19:50:54 +0000
parents d198f255bee9
children 606d90e03a2f
comparison
equal deleted inserted replaced
13425:a87f1a948a24 13426:0f92ad66bbb5
14 #define ACODEC_COPY 0 14 #define ACODEC_COPY 0
15 #define ACODEC_PCM 1 15 #define ACODEC_PCM 1
16 #define ACODEC_VBRMP3 2 16 #define ACODEC_VBRMP3 2
17 #define ACODEC_NULL 3 17 #define ACODEC_NULL 3
18 #define ACODEC_LAVC 4 18 #define ACODEC_LAVC 4
19 #define ACODEC_TOOLAME 5
19 20
20 #include <stdio.h> 21 #include <stdio.h>
21 #include <stdlib.h> 22 #include <stdlib.h>
22 #include <string.h> 23 #include <string.h>
23 #include <signal.h> 24 #include <signal.h>
91 // tmp buffer for lavc audio encoding (to free!!!!!) 92 // tmp buffer for lavc audio encoding (to free!!!!!)
92 static void *lavc_abuf = NULL; 93 static void *lavc_abuf = NULL;
93 extern int avcodec_inited; 94 extern int avcodec_inited;
94 95
95 static uint32_t lavc_find_atag(char *codec); 96 static uint32_t lavc_find_atag(char *codec);
97 #endif
98
99 #ifdef HAVE_TOOLAME
100 #include "libmpcodecs/ae_toolame.h"
101 static mpae_toolame_ctx *mpae_toolame;
96 #endif 102 #endif
97 103
98 int vo_doublebuffering=0; 104 int vo_doublebuffering=0;
99 int vo_directrendering=0; 105 int vo_directrendering=0;
100 int vo_config_count=0; 106 int vo_config_count=0;
985 991
986 mp_msg(MSGT_MENCODER, MSGL_V, MSGTR_FramesizeBufsizeTag, lavc_actx->frame_size, lavc_actx->frame_size * 2 * lavc_actx->channels, mux_a->wf->wFormatTag); 992 mp_msg(MSGT_MENCODER, MSGL_V, MSGTR_FramesizeBufsizeTag, lavc_actx->frame_size, lavc_actx->frame_size * 2 * lavc_actx->channels, mux_a->wf->wFormatTag);
987 993
988 break; 994 break;
989 #endif 995 #endif
996
997 #ifdef HAVE_TOOLAME
998 case ACODEC_TOOLAME:
999 {
1000 int cn = audio_output_channels ? audio_output_channels : sh_audio->channels;
1001 int sr = force_srate ? force_srate : sh_audio->samplerate;
1002 int br;
1003
1004 mpae_toolame = mpae_init_toolame(cn, sr);
1005 if(mpae_toolame == NULL)
1006 {
1007 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't open toolame codec, exiting\n");
1008 exit(1);
1009 }
1010
1011 br = mpae_toolame->bitrate;
1012
1013 mux_a->wf = malloc(sizeof(WAVEFORMATEX)+256);
1014 mux_a->wf->wFormatTag = 0x50;
1015 mux_a->wf->nChannels = cn;
1016 mux_a->wf->nSamplesPerSec = sr;
1017 mux_a->wf->nAvgBytesPerSec = 1000 * (br / 8);
1018 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
1019 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * 1152)/ mux_a->wf->nSamplesPerSec; /* for cbr */
1020
1021 if ((mux_a->wf->nAvgBytesPerSec *
1022 1152) % mux_a->wf->nSamplesPerSec) {
1023 mux_a->h.dwScale = 1152;
1024 mux_a->h.dwRate = sr;
1025 mux_a->h.dwSampleSize = 0; // Blocksize not constant
1026 } else {
1027 mux_a->h.dwSampleSize = mux_a->h.dwScale;
1028 }
1029 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
1030 mux_a->h.dwSuggestedBufferSize = audio_preload*mux_a->wf->nAvgBytesPerSec;
1031 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
1032
1033 mux_a->wf->cbSize = 12;
1034 mux_a->wf->wBitsPerSample = 0; /* does not apply */
1035 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
1036 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
1037 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
1038 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
1039 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
1040
1041 // Fix allocation
1042 mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
1043
1044 // setup filter:
1045 if (!init_audio_filters(
1046 sh_audio,
1047 sh_audio->samplerate, sh_audio->channels,
1048 sh_audio->sample_format, sh_audio->samplesize,
1049 mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
1050 AFMT_S16_NE, 2,
1051 mux_a->h.dwSuggestedBufferSize,
1052 mux_a->h.dwSuggestedBufferSize*2)) {
1053 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't find matching filter / ao format!\n");
1054 exit(1);
1055 }
1056
1057 break;
1058 }
1059 #endif
990 } 1060 }
991 1061
992 if (verbose>1) print_wave_header(mux_a->wf); 1062 if (verbose>1) print_wave_header(mux_a->wf);
993 1063
994 if(audio_delay!=0.0){ 1064 if(audio_delay!=0.0){
1133 mux_a->buffer_len += avcodec_encode_audio(lavc_actx, mux_a->buffer + mux_a->buffer_len, size, lavc_abuf); 1203 mux_a->buffer_len += avcodec_encode_audio(lavc_actx, mux_a->buffer + mux_a->buffer_len, size, lavc_abuf);
1134 if (mux_a->h.dwSampleSize) { /* CBR */ 1204 if (mux_a->h.dwSampleSize) { /* CBR */
1135 /* 1205 /*
1136 * work around peculiar lame behaviour 1206 * work around peculiar lame behaviour
1137 */ 1207 */
1208 if (mux_a->buffer_len < mux_a->wf->nBlockAlign) {
1209 len = 0;
1210 } else {
1211 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1212 }
1213 } else { /* VBR */
1214 len = mux_a->buffer_len;
1215 }
1216 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1217 }
1218 #endif
1219 #ifdef HAVE_TOOLAME
1220 if((mux_a->codec == ACODEC_TOOLAME) && (mpae_toolame != NULL)){
1221 int size, rd_len;
1222 uint8_t buf[1152*2*2];
1223 size = 1152 * 2 * mux_a->wf->nChannels;
1224
1225 rd_len = dec_audio(sh_audio, buf, size);
1226 if(rd_len != size)
1227 break;
1228
1229 // Encode one frame
1230 mux_a->buffer_len += mpae_encode_toolame(mpae_toolame, mux_a->buffer + mux_a->buffer_len, 1152, (void*)buf, mux_a->buffer_size-mux_a->buffer_len);
1231 if (mux_a->h.dwSampleSize) { /* CBR */
1138 if (mux_a->buffer_len < mux_a->wf->nBlockAlign) { 1232 if (mux_a->buffer_len < mux_a->wf->nBlockAlign) {
1139 len = 0; 1233 len = 0;
1140 } else { 1234 } else {
1141 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign); 1235 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1142 } 1236 }