--- orig/cfg-mencoder.h 2003-11-01 13:41:03.000000000 +0100 +++ main/cfg-mencoder.h 2003-11-01 13:39:14.000000000 +0100 @@ -46,6 +46,12 @@ #ifdef USE_LIBAVCODEC extern m_option_t lavcopts_conf[]; +m_option_t alavcopts_conf[]={ + {"acodec", &alavc_param_codec, CONF_TYPE_STRING, 0, 0, 0, NULL}, + {"abitrate", &alavc_param_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL}, + {"atag", &alavc_codec_tag, CONF_TYPE_INT, 0, 0, 0, NULL}, + {NULL, NULL, 0, 0, 0, 0, NULL} +}; #endif #ifdef USE_WIN32DLL @@ -108,12 +114,21 @@ #else {"mp3lame", "MPlayer was compiled without libmp3lame support!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, #endif +#ifdef USE_LIBAVCODEC + {"alavc", &out_audio_codec, CONF_TYPE_FLAG, 0, 0, ACODEC_LAVC, NULL}, +#else + {"alavc", "MPlayer was compiled without libavcodec! See README or DOCS!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, +#endif + {"help", "\nAvailable codecs:\n" " copy - frame copy, without re-encoding (useful for AC3)\n" " pcm - uncompressed PCM audio\n" #ifdef HAVE_MP3LAME " mp3lame - cbr/abr/vbr MP3 using libmp3lame\n" #endif +#ifdef USE_LIBAVCODEC + " alavc - ffmpeg audio encoder (mp2, ac3, ...)\n" +#endif "\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, {NULL, NULL, 0, 0, 0, 0, NULL} }; @@ -202,8 +217,10 @@ #endif #ifdef USE_LIBAVCODEC {"lavcopts", lavcopts_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL}, + {"alavcopts", alavcopts_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL}, #else {"lavcopts", "MPlayer was compiled without libavcodec! See README or DOCS!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, + {"alavcopts", "MPlayer was compiled without libavcodec! See README or DOCS!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, #endif #ifdef USE_WIN32DLL {"vfwopts", vfwopts_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL}, --- orig/mencoder.c 2003-11-01 13:41:24.000000000 +0100 +++ main/mencoder.c 2003-11-01 13:39:35.000000000 +0100 @@ -14,6 +14,7 @@ #define ACODEC_PCM 1 #define ACODEC_VBRMP3 2 #define ACODEC_NULL 3 +#define ACODEC_LAVC 4 #include #include @@ -71,6 +72,19 @@ #include "osdep/timer.h" +#ifdef USE_LIBAVCODEC +// for lavc audio encoding +#include "libavcodec/avcodec.h" +AVCodec *alavc_codec; +AVCodecContext *alavc_ctx = NULL; +static char *alavc_param_codec = "mp2"; +static int alavc_param_bitrate = 128; +static int alavc_ok = 0; +static uint16_t alavc_codec_tag = 0x0; +// tmp buffer for lavc audio encoding (to free!!!!!) +void *alavc_buf = NULL; +#endif + int vo_doublebuffering=0; int vo_directrendering=0; int vo_config_count=0; @@ -792,6 +806,54 @@ } break; #endif +#ifdef USE_LIBAVCODEC +case ACODEC_LAVC: + mux_a->h.dwRate = (1000 * alavc_param_bitrate / 8); + mux_a->h.dwScale = 1; + mux_a->h.dwSampleSize = 1; + + if (sizeof(MPEGLAYER3WAVEFORMAT) != 30) + mp_msg(MSGT_MENCODER, MSGL_WARN, + "sizeof(MPEGLAYER3WAVEFORMAT)==%d!=30, maybe broken C compiler?\n", + sizeof(MPEGLAYER3WAVEFORMAT)); + + mux_a->wf = malloc(sizeof(MPEGLAYER3WAVEFORMAT)); // should be 30 + + mux_a->wf->nChannels = audio_output_channels ? audio_output_channels : sh_audio->channels; + mux_a->wf->nSamplesPerSec = force_srate ? force_srate : sh_audio->samplerate; + mux_a->wf->nAvgBytesPerSec = (alavc_param_bitrate / 8) * 1000; + // mux_a->wf->nBlockAlign=(mux_a->h.dwRate<32000) ? 576 : 1152; + mux_a->wf->nBlockAlign = 1; + mux_a->wf->wBitsPerSample = 16; + mux_a->wf->cbSize = 0; + // mux_a->h.dwSampleSize = sh_audio->samplesize * mux_a->wf->nChannels; + + ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1; + ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2; + ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = (mux_a->h.dwRate < 32000) ? 576 : 1152; // ??? + ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1; + ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0; + + if(alavc_codec_tag != 0) + mux_a->wf->wFormatTag = alavc_codec_tag; + else + mux_a->wf->wFormatTag = alavc_find_tag(alavc_param_codec); + + // setup filter: + if (!init_audio_filters( + sh_audio, sh_audio->samplerate, sh_audio->channels, + sh_audio->sample_format, sh_audio->samplesize, + mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels, +#ifdef WORDS_BIGENDIAN + AFMT_S16_BE, 2, +#else + AFMT_S16_LE, 2, +#endif + 4608, mux_a->h.dwRate * mux_a->wf->nChannels * 2)) { + mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't find matching filter / ao format!\n"); + } + break; +#endif } if (verbose>1) print_wave_header(mux_a->wf); @@ -846,6 +908,52 @@ } break; #endif +#ifdef USE_LIBAVCODEC +case ACODEC_LAVC: + if(! alavc_param_codec) + { + mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio LAVC, Missing codec name!\n"); + exit(1); + } + avcodec_init(); + avcodec_register_all(); + + alavc_codec = avcodec_find_encoder_by_name(alavc_param_codec); + if (!alavc_codec) + { + mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio LAVC, couldn't find encoder for codec %s\n", alavc_param_codec); + break; + } + + alavc_ctx = avcodec_alloc_context(); + if(alavc_ctx == NULL) + { + mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio LAVC, couldn't allocate context!\n"); + break; + } + + // put sample parameters + alavc_ctx->bit_rate = alavc_param_bitrate * 1000; + alavc_ctx->channels = mux_a->wf->nChannels; + alavc_ctx->sample_rate = mux_a->wf->nSamplesPerSec; + + if(avcodec_open(alavc_ctx, alavc_codec) < 0) + { + mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't open codec %s, br=%d\n", alavc_param_codec, alavc_param_bitrate); + break; + } + + alavc_buf = malloc(alavc_ctx->frame_size * 2 * mux_a->wf->nChannels); + if(alavc_buf == NULL) + { + fprintf(stderr, "Couldn't allocate %d bytes\n", alavc_ctx->frame_size * 2 * mux_a->wf->nChannels); + break; + } + + alavc_ok = 1; + mp_msg(MSGT_MENCODER, MSGL_V, "FRAME_SIZE: %d, BUFFER_SIZE: %d, TAG: 0x%x\n", alavc_ctx->frame_size, alavc_ctx->frame_size * 2 * alavc_ctx->channels, mux_a->wf->wFormatTag); + break; +#endif } signal(SIGINT,exit_sighandler); // Interrupt from keyboard @@ -932,6 +1040,25 @@ len=mux_a->h.dwSampleSize*(mux_a->h.dwRate/audio_density); len=dec_audio(sh_audio,mux_a->buffer,len); break; +#ifdef USE_LIBAVCODEC + case ACODEC_LAVC: + { + if(alavc_ok) + { + int size, rd_len; + + size = alavc_ctx->frame_size * 2 * mux_a->wf->nChannels; + + rd_len = dec_audio(sh_audio, alavc_buf, size); + if(rd_len != size) + break; + + // Encode one frame + len = avcodec_encode_audio(alavc_ctx, &(mux_a->buffer[0]), size, alavc_buf); + } + } + break; +#endif } } else { // VBR - encode/copy an audio frame @@ -1294,6 +1421,11 @@ if(demuxer) free_demuxer(demuxer); if(stream) free_stream(stream); // kill cache thread +#ifdef USE_LIBAVCODEC +if(alavc_buf != NULL) + free(alavc_buf); +#endif + return interrupted; } @@ -1600,3 +1732,20 @@ mencoder_exit(0, NULL); } #endif + +#ifdef USE_LIBAVCODEC +uint32_t alavc_find_tag(char *codec) +{ + if(codec == NULL) + return 0; + + if(! strcasecmp(codec, "mp2")) + return 0x50; + + if(! strcasecmp(codec, "ac3")) + return 0x2000; + + return 0; +} +#endif +