2001-10-29 02:11:18 +01:00
# define VCODEC_DIVX4 1
2001-10-31 01:25:28 +01:00
# define ACODEC_PCM 1
# define ACODEC_VBRMP3 2
2001-10-29 02:11:18 +01:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <signal.h>
2001-10-31 17:08:01 +01:00
# include "config.h"
2001-10-29 02:11:18 +01:00
# include "mp_msg.h"
# include "help_mp.h"
# include "codec-cfg.h"
# include "stream.h"
# include "demuxer.h"
# include "stheader.h"
# include "aviwrite.h"
# include "libvo/video_out.h"
2001-10-30 22:55:28 +01:00
# include "dec_audio.h"
# include "dec_video.h"
2001-10-29 02:11:18 +01:00
# include <encore2.h>
2001-11-03 01:44:02 +01:00
# include "divx4_vbr.h"
2001-10-29 02:11:18 +01:00
2001-10-31 17:08:01 +01:00
# include <lame/lame.h>
2001-10-31 01:25:28 +01:00
2001-10-29 02:11:18 +01:00
//--------------------------
// cache2:
2001-11-02 02:25:13 +01:00
static int stream_cache_size = 0 ;
2001-10-29 02:11:18 +01:00
# ifdef USE_STREAM_CACHE
extern int cache_fill_status ;
# else
# define cache_fill_status 0
# endif
2001-11-02 02:25:13 +01:00
int vcd_track = 0 ;
int audio_id = - 1 ;
int video_id = - 1 ;
int dvdsub_id = - 1 ;
2001-10-29 02:11:18 +01:00
char * audio_codec = NULL ; // override audio codec
char * video_codec = NULL ; // override video codec
int audio_family = - 1 ; // override audio codec family
int video_family = - 1 ; // override video codec family
2001-11-04 00:01:17 +01:00
int out_audio_codec = ACODEC_VBRMP3 ;
int out_video_codec = VCODEC_DIVX4 ;
2001-10-29 02:11:18 +01:00
// audio stream skip/resync functions requires only for seeking.
// (they should be implemented in the audio codec layer)
//void skip_audio_frame(sh_audio_t *sh_audio){}
//void resync_audio_stream(sh_audio_t *sh_audio){}
2001-11-02 02:25:13 +01:00
int verbose = 0 ; // must be global!
2001-10-29 02:11:18 +01:00
double video_time_usage = 0 ;
double vout_time_usage = 0 ;
static double audio_time_usage = 0 ;
static int total_time_usage_start = 0 ;
static int benchmark = 0 ;
2001-11-01 03:31:23 +01:00
// A-V sync:
int delay_corrected = 1 ;
static float default_max_pts_correction = - 1 ; //0.01f;
static float max_pts_correction = 0 ; //default_max_pts_correction;
static float c_total = 0 ;
2001-11-01 22:47:28 +01:00
float force_fps = 0 ;
float force_ofps = 0 ; // set to 24 for inverse telecine
2001-10-29 02:11:18 +01:00
2001-11-02 02:25:13 +01:00
int force_srate = 0 ;
2001-11-02 04:22:33 +01:00
char * out_filename = " test.avi " ;
char * mp3_filename = NULL ;
char * ac3_filename = NULL ;
2001-11-03 01:44:02 +01:00
static int pass = 0 ;
static char * passtmpfile = " divx2pass.log " ;
static int play_n_frames = - 1 ;
2001-11-04 00:01:17 +01:00
//char *out_audio_codec=NULL; // override audio codec
//char *out_video_codec=NULL; // override video codec
2001-11-02 04:22:33 +01:00
2001-10-31 17:08:01 +01:00
//#include "libmpeg2/mpeg2.h"
//#include "libmpeg2/mpeg2_internal.h"
2001-11-02 04:22:33 +01:00
ENC_PARAM divx4_param ;
2001-11-03 01:44:02 +01:00
int divx4_crispness = 100 ;
2001-11-02 04:22:33 +01:00
int lame_param_quality = 0 ; // best
int lame_param_vbr = vbr_default ;
int lame_param_mode = - 1 ; // unset
int lame_param_padding = - 1 ; // unset
int lame_param_br = - 1 ; // unset
int lame_param_ratio = - 1 ; // unset
2001-11-02 02:25:13 +01:00
//-------------------------- config stuff:
# include "cfgparser.h"
static int cfg_inc_verbose ( struct config * conf ) { + + verbose ; return 0 ; }
static int cfg_include ( struct config * conf , char * filename ) {
return parse_config_file ( conf , filename ) ;
}
# include "get_path.c"
# include "cfg-mplayer-def.h"
# include "cfg-mencoder.h"
2001-10-31 17:08:01 +01:00
//---------------------------------------------------------------------------
2001-11-02 11:26:58 +01:00
// dummy datas for gui :(
# ifdef HAVE_NEW_GUI
float rel_seek_secs = 0 ;
int abs_seek_pos = 0 ;
int use_gui = 0 ;
void exit_player ( char * how ) {
}
void vo_x11_putkey ( int key ) {
}
void vo_setwindow ( int w , int g ) {
}
void vo_setwindowsize ( int w , int h ) {
}
int vo_resize = 0 ;
int vo_expose = 0 ;
# endif
// ---
2001-10-31 17:08:01 +01:00
// mini dummy libvo:
2001-10-29 02:11:18 +01:00
static unsigned char * vo_image = NULL ;
static unsigned char * vo_image_ptr = NULL ;
static int vo_w , vo_h ;
static uint32_t draw_slice ( uint8_t * src [ ] , int stride [ ] , int w , int h , int x0 , int y0 ) {
int y ;
// printf("draw_slice %dx%d %d;%d\n",w,h,x,y);
// copy Y:
for ( y = 0 ; y < h ; y + + ) {
2001-11-02 21:05:36 +01:00
unsigned char * s = src [ 0 ] + stride [ 0 ] * y ;
2001-10-29 02:11:18 +01:00
unsigned char * d = vo_image + vo_w * ( y0 + y ) + x0 ;
memcpy ( d , s , w ) ;
}
x0 > > = 1 ; y0 > > = 1 ;
w > > = 1 ; h > > = 1 ;
// copy U:
for ( y = 0 ; y < h ; y + + ) {
2001-11-02 21:05:36 +01:00
unsigned char * s = src [ 2 ] + stride [ 2 ] * y ;
2001-10-29 02:11:18 +01:00
unsigned char * d = vo_image + vo_w * vo_h + ( vo_w > > 1 ) * ( y0 + y ) + x0 ;
memcpy ( d , s , w ) ;
}
// copy V:
for ( y = 0 ; y < h ; y + + ) {
2001-11-02 21:05:36 +01:00
unsigned char * s = src [ 1 ] + stride [ 1 ] * y ;
2001-10-29 02:11:18 +01:00
unsigned char * d = vo_image + vo_w * vo_h + vo_w * vo_h / 4 + ( vo_w > > 1 ) * ( y0 + y ) + x0 ;
memcpy ( d , s , w ) ;
}
}
static uint32_t draw_frame ( uint8_t * src [ ] ) {
// printf("This function shouldn't be called - report bug!\n");
// later: add YUY2->YV12 conversion here!
vo_image_ptr = src [ 0 ] ;
}
vo_functions_t video_out ;
2001-10-31 17:08:01 +01:00
//---------------------------------------------------------------------------
int dec_audio ( sh_audio_t * sh_audio , unsigned char * buffer , int total ) {
int size = 0 ;
int eof = 0 ;
while ( size < total & & ! eof ) {
int len = total - size ;
if ( len > MAX_OUTBURST ) len = MAX_OUTBURST ;
if ( len > sh_audio - > a_buffer_size ) len = sh_audio - > a_buffer_size ;
if ( len > sh_audio - > a_buffer_len ) {
int ret = decode_audio ( sh_audio ,
& sh_audio - > a_buffer [ sh_audio - > a_buffer_len ] ,
len - sh_audio - > a_buffer_len ,
sh_audio - > a_buffer_size - sh_audio - > a_buffer_len ) ;
if ( ret > 0 ) sh_audio - > a_buffer_len + = ret ; else eof = 1 ;
}
if ( len > sh_audio - > a_buffer_len ) len = sh_audio - > a_buffer_len ;
memcpy ( buffer + size , sh_audio - > a_buffer , len ) ;
sh_audio - > a_buffer_len - = len ; size + = len ;
if ( sh_audio - > a_buffer_len > 0 )
memcpy ( sh_audio - > a_buffer , & sh_audio - > a_buffer [ len ] , sh_audio - > a_buffer_len ) ;
}
return size ;
}
//---------------------------------------------------------------------------
2001-10-29 02:11:18 +01:00
static int eof = 0 ;
static void exit_sighandler ( int x ) {
eof = 1 ;
}
2001-11-02 02:25:13 +01:00
int main ( int argc , char * argv [ ] , char * envp [ ] ) {
2001-10-29 02:11:18 +01:00
stream_t * stream = NULL ;
demuxer_t * demuxer = NULL ;
demux_stream_t * d_audio = NULL ;
demux_stream_t * d_video = NULL ;
demux_stream_t * d_dvdsub = NULL ;
sh_audio_t * sh_audio = NULL ;
sh_video_t * sh_video = NULL ;
int file_format = DEMUXER_TYPE_UNKNOWN ;
int i ;
unsigned int out_fmt ;
aviwrite_t * muxer = NULL ;
aviwrite_stream_t * mux_a = NULL ;
aviwrite_stream_t * mux_v = NULL ;
FILE * muxer_f = NULL ;
ENC_FRAME enc_frame ;
ENC_RESULT enc_result ;
void * enc_handle = NULL ;
2001-10-31 17:08:01 +01:00
lame_global_flags * lame ;
2001-10-31 01:25:28 +01:00
2001-11-03 22:00:04 +01:00
float audio_preload = 0.5 ;
2001-10-31 00:17:35 +01:00
2001-11-01 22:47:28 +01:00
double v_pts_corr = 0 ;
double v_timer_corr = 0 ;
2001-11-01 03:31:23 +01:00
2001-11-02 02:25:13 +01:00
char * * filenames = NULL ;
char * filename = NULL ;
int num_filenames ;
2001-10-29 02:11:18 +01:00
//int out_buffer_size=0x200000;
//unsigned char* out_buffer=malloc(out_buffer_size);
2001-11-02 03:08:00 +01:00
mp_msg_init ( MSGL_STATUS ) ;
// check codec.conf
if ( ! parse_codec_cfg ( get_path ( " codecs.conf " ) ) ) {
if ( ! parse_codec_cfg ( DATADIR " /codecs.conf " ) ) {
mp_msg ( MSGT_MENCODER , MSGL_HINT , MSGTR_CopyCodecsConf ) ;
exit ( 0 ) ;
}
}
2001-10-29 02:11:18 +01:00
2001-11-03 01:44:02 +01:00
// set some defaults, before parsing configfile/commandline:
divx4_param . min_quantizer = 2 ;
divx4_param . max_quantizer = 31 ;
divx4_param . rc_period = 2000 ;
divx4_param . rc_reaction_period = 10 ;
divx4_param . rc_reaction_ratio = 20 ;
2001-11-02 02:25:13 +01:00
num_filenames = parse_command_line ( conf , argc , argv , envp , & filenames ) ;
if ( num_filenames < 0 ) exit ( 1 ) ; // error parsing cmdline
if ( ! num_filenames & & ! vcd_track & & ! dvd_title ) {
printf ( " \n Missing filename! \n \n " ) ;
exit ( 1 ) ;
}
2001-11-02 03:08:00 +01:00
mp_msg_init ( verbose + MSGL_STATUS ) ;
2001-10-31 23:59:56 +01:00
2001-11-02 02:25:13 +01:00
filename = ( num_filenames > 0 ) ? filenames [ 0 ] : NULL ;
stream = open_stream ( filename , vcd_track , & file_format ) ;
2001-10-29 02:11:18 +01:00
if ( ! stream ) {
printf ( " Cannot open file/device \n " ) ;
exit ( 1 ) ;
}
printf ( " success: format: %d data: 0x%X - 0x%X \n " , file_format , ( int ) ( stream - > start_pos ) , ( int ) ( stream - > end_pos ) ) ;
2001-11-02 02:25:13 +01:00
if ( stream_cache_size ) stream_enable_cache ( stream , stream_cache_size * 1024 ) ;
2001-10-29 02:11:18 +01:00
2001-11-02 02:25:13 +01:00
demuxer = demux_open ( stream , file_format , video_id , audio_id , dvdsub_id ) ;
2001-10-29 02:11:18 +01:00
if ( ! demuxer ) {
printf ( " Cannot open demuxer \n " ) ;
exit ( 1 ) ;
}
d_audio = demuxer - > audio ;
d_video = demuxer - > video ;
d_dvdsub = demuxer - > sub ;
sh_audio = d_audio - > sh ;
sh_video = d_video - > sh ;
if ( ! video_read_properties ( sh_video ) ) {
printf ( " Couldn't read video properties \n " ) ;
exit ( 1 ) ;
}
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_INFO , " [V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f \n " ,
2001-10-29 02:11:18 +01:00
demuxer - > file_format , sh_video - > format , sh_video - > disp_w , sh_video - > disp_h ,
sh_video - > fps , sh_video - > frametime
) ;
sh_video - > codec = NULL ;
2001-11-02 03:08:00 +01:00
if ( video_family ! = - 1 ) mp_msg ( MSGT_MENCODER , MSGL_INFO , MSGTR_TryForceVideoFmt , video_family ) ;
2001-10-29 02:11:18 +01:00
while ( 1 ) {
sh_video - > codec = find_codec ( sh_video - > format ,
sh_video - > bih ? ( ( unsigned int * ) & sh_video - > bih - > biCompression ) : NULL , sh_video - > codec , 0 ) ;
if ( ! sh_video - > codec ) {
if ( video_family ! = - 1 ) {
sh_video - > codec = NULL ; /* re-search */
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_WARN , MSGTR_CantFindVfmtFallback ) ;
2001-10-29 02:11:18 +01:00
video_family = - 1 ;
continue ;
}
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_ERR , MSGTR_CantFindVideoCodec , sh_video - > format ) ;
mp_msg ( MSGT_MENCODER , MSGL_HINT , MSGTR_TryUpgradeCodecsConfOrRTFM , get_path ( " codecs.conf " ) ) ;
2001-10-29 02:11:18 +01:00
exit ( 1 ) ;
}
if ( video_codec & & strcmp ( sh_video - > codec - > name , video_codec ) ) continue ;
else if ( video_family ! = - 1 & & sh_video - > codec - > driver ! = video_family ) continue ;
break ;
}
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_INFO , " %s video codec: [%s] drv:%d (%s) \n " , video_codec ? " Forcing " : " Detected " , sh_video - > codec - > name , sh_video - > codec - > driver , sh_video - > codec - > info ) ;
2001-10-29 02:11:18 +01:00
for ( i = 0 ; i < CODECS_MAX_OUTFMT ; i + + ) {
out_fmt = sh_video - > codec - > outfmt [ i ] ;
if ( out_fmt = = 0xFFFFFFFF ) continue ;
if ( out_fmt = = IMGFMT_YV12 ) break ;
if ( out_fmt = = IMGFMT_I420 ) break ;
if ( out_fmt = = IMGFMT_IYUV ) break ;
if ( out_fmt = = IMGFMT_YUY2 ) break ;
}
if ( i > = CODECS_MAX_OUTFMT ) {
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_FATAL , MSGTR_VOincompCodec ) ;
2001-10-29 02:11:18 +01:00
exit ( 1 ) ; // exit_player(MSGTR_Exit_error);
}
sh_video - > outfmtidx = i ;
if ( out_fmt = = IMGFMT_YV12 | | out_fmt = = IMGFMT_I420 | | out_fmt = = IMGFMT_IYUV ) {
vo_w = sh_video - > disp_w ;
vo_h = sh_video - > disp_h ;
vo_image = malloc ( vo_w * vo_h * 3 / 2 ) ;
vo_image_ptr = vo_image ;
}
if ( ! init_video ( sh_video ) ) {
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_FATAL , MSGTR_CouldntInitVideoCodec ) ;
2001-10-29 02:11:18 +01:00
exit ( 1 ) ;
}
2001-10-31 00:17:35 +01:00
if ( sh_audio ) {
// Go through the codec.conf and find the best codec...
sh_audio - > codec = NULL ;
2001-11-02 03:08:00 +01:00
if ( audio_family ! = - 1 ) mp_msg ( MSGT_MENCODER , MSGL_INFO , MSGTR_TryForceAudioFmt , audio_family ) ;
2001-10-31 00:17:35 +01:00
while ( 1 ) {
sh_audio - > codec = find_codec ( sh_audio - > format , NULL , sh_audio - > codec , 1 ) ;
if ( ! sh_audio - > codec ) {
if ( audio_family ! = - 1 ) {
sh_audio - > codec = NULL ; /* re-search */
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_ERR , MSGTR_CantFindAfmtFallback ) ;
2001-10-31 00:17:35 +01:00
audio_family = - 1 ;
continue ;
}
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_ERR , MSGTR_CantFindAudioCodec , sh_audio - > format ) ;
mp_msg ( MSGT_MENCODER , MSGL_HINT , MSGTR_TryUpgradeCodecsConfOrRTFM , get_path ( " codecs.conf " ) ) ;
2001-10-31 00:17:35 +01:00
sh_audio = d_audio - > sh = NULL ;
break ;
}
if ( audio_codec & & strcmp ( sh_audio - > codec - > name , audio_codec ) ) continue ;
else if ( audio_family ! = - 1 & & sh_audio - > codec - > driver ! = audio_family ) continue ;
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_INFO , " %s audio codec: [%s] drv:%d (%s) \n " , audio_codec ? " Forcing " : " Detected " , sh_audio - > codec - > name , sh_audio - > codec - > driver , sh_audio - > codec - > info ) ;
2001-10-31 00:17:35 +01:00
break ;
}
}
if ( sh_audio ) {
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_V , " Initializing audio codec... \n " ) ;
2001-10-31 00:17:35 +01:00
if ( ! init_audio ( sh_audio ) ) {
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_ERR , MSGTR_CouldntInitAudioCodec ) ;
2001-10-31 00:17:35 +01:00
sh_audio = d_audio - > sh = NULL ;
} else {
2001-11-02 03:08:00 +01:00
mp_msg ( MSGT_MENCODER , MSGL_INFO , " AUDIO: srate=%d chans=%d bps=%d sfmt=0x%X ratio: %d->%d \n " , sh_audio - > samplerate , sh_audio - > channels , sh_audio - > samplesize ,
2001-10-31 00:17:35 +01:00
sh_audio - > sample_format , sh_audio - > i_bps , sh_audio - > o_bps ) ;
}
}
2001-10-29 02:11:18 +01:00
// set up video encoder:
video_out . draw_slice = draw_slice ;
video_out . draw_frame = draw_frame ;
// set up output file:
2001-11-02 04:22:33 +01:00
muxer_f = fopen ( out_filename , " wb " ) ;
2001-10-29 02:11:18 +01:00
muxer = aviwrite_new_muxer ( ) ;
2001-10-31 00:17:35 +01:00
// ============= VIDEO ===============
2001-10-29 02:11:18 +01:00
mux_v = aviwrite_new_stream ( muxer , AVIWRITE_TYPE_VIDEO ) ;
mux_v - > buffer_size = 0x200000 ;
mux_v - > buffer = malloc ( mux_v - > buffer_size ) ;
mux_v - > source = sh_video ;
mux_v - > h . dwSampleSize = 0 ; // VBR
mux_v - > h . dwScale = 10000 ;
2001-11-01 22:47:28 +01:00
mux_v - > h . dwRate = mux_v - > h . dwScale * ( force_ofps ? force_ofps : sh_video - > fps ) ;
2001-10-29 02:11:18 +01:00
2001-11-04 00:01:17 +01:00
mux_v - > codec = out_video_codec ;
2001-10-30 22:55:28 +01:00
2001-10-29 02:11:18 +01:00
switch ( mux_v - > codec ) {
case 0 :
mux_v - > bih = sh_video - > bih ;
break ;
case VCODEC_DIVX4 :
mux_v - > bih = malloc ( sizeof ( BITMAPINFOHEADER ) ) ;
mux_v - > bih - > biSize = sizeof ( BITMAPINFOHEADER ) ;
mux_v - > bih - > biWidth = sh_video - > disp_w ;
mux_v - > bih - > biHeight = sh_video - > disp_h ;
2001-11-02 18:43:05 +01:00
mux_v - > bih - > biPlanes = 1 ;
2001-10-29 02:11:18 +01:00
mux_v - > bih - > biBitCount = 24 ;
mux_v - > bih - > biCompression = mmioFOURCC ( ' d ' , ' i ' , ' v ' , ' x ' ) ;
mux_v - > bih - > biSizeImage = mux_v - > bih - > biWidth * mux_v - > bih - > biHeight * ( mux_v - > bih - > biBitCount / 8 ) ;
break ;
}
2001-10-31 00:17:35 +01:00
// ============= AUDIO ===============
if ( sh_audio ) {
mux_a = aviwrite_new_stream ( muxer , AVIWRITE_TYPE_AUDIO ) ;
mux_a - > buffer_size = 0x100000 ; //16384;
mux_a - > buffer = malloc ( mux_a - > buffer_size ) ;
mux_a - > source = sh_audio ;
2001-11-04 00:01:17 +01:00
mux_a - > codec = out_audio_codec ;
2001-10-31 00:17:35 +01:00
switch ( mux_a - > codec ) {
case 0 :
mux_a - > h . dwSampleSize = sh_audio - > audio . dwSampleSize ;
mux_a - > h . dwScale = sh_audio - > audio . dwScale ;
mux_a - > h . dwRate = sh_audio - > audio . dwRate ;
mux_a - > wf = sh_audio - > wf ;
break ;
2001-10-31 01:25:28 +01:00
case ACODEC_PCM :
printf ( " CBR PCM audio selected \n " ) ;
mux_a - > h . dwSampleSize = 2 * sh_audio - > channels ;
mux_a - > h . dwScale = 1 ;
mux_a - > h . dwRate = sh_audio - > samplerate ;
mux_a - > wf = malloc ( sizeof ( WAVEFORMATEX ) ) ;
mux_a - > wf - > nBlockAlign = mux_a - > h . dwSampleSize ;
mux_a - > wf - > wFormatTag = 0x1 ; // PCM
mux_a - > wf - > nChannels = sh_audio - > channels ;
mux_a - > wf - > nSamplesPerSec = sh_audio - > samplerate ;
mux_a - > wf - > nAvgBytesPerSec = mux_a - > h . dwSampleSize * mux_a - > wf - > nSamplesPerSec ;
mux_a - > wf - > wBitsPerSample = 16 ;
mux_a - > wf - > cbSize = 0 ; // FIXME for l3codeca.acm
break ;
2001-10-31 00:17:35 +01:00
case ACODEC_VBRMP3 :
mux_a - > h . dwSampleSize = 0 ; // VBR
2001-11-03 22:00:04 +01:00
mux_a - > h . dwScale = 1152 ; // samples/frame
2001-10-31 00:17:35 +01:00
mux_a - > h . dwRate = sh_audio - > samplerate ;
2001-11-02 18:43:05 +01:00
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
2001-10-31 00:17:35 +01:00
mux_a - > wf - > wFormatTag = 0x55 ; // MP3
mux_a - > wf - > nChannels = sh_audio - > channels ;
2001-11-02 21:05:36 +01:00
mux_a - > wf - > nSamplesPerSec = force_srate ? force_srate : sh_audio - > samplerate ;
2001-11-02 18:43:05 +01:00
mux_a - > wf - > nAvgBytesPerSec = 192000 / 8 ; // FIXME!
2001-11-03 22:00:04 +01:00
mux_a - > wf - > nBlockAlign = 1152 ; // requires for l3codeca.acm + WMP 6.4
2001-11-02 18:43:05 +01:00
mux_a - > wf - > wBitsPerSample = 0 ; //16;
// from NaNdub: (requires for l3codeca.acm)
mux_a - > wf - > cbSize = 12 ;
( ( MPEGLAYER3WAVEFORMAT * ) ( mux_a - > wf ) ) - > wID = 1 ;
( ( MPEGLAYER3WAVEFORMAT * ) ( mux_a - > wf ) ) - > fdwFlags = 2 ;
2001-11-03 22:00:04 +01:00
( ( MPEGLAYER3WAVEFORMAT * ) ( mux_a - > wf ) ) - > nBlockSize = 1152 ; // ???
2001-11-02 18:43:05 +01:00
( ( MPEGLAYER3WAVEFORMAT * ) ( mux_a - > wf ) ) - > nFramesPerBlock = 1 ;
( ( MPEGLAYER3WAVEFORMAT * ) ( mux_a - > wf ) ) - > nCodecDelay = 0 ;
2001-10-31 00:17:35 +01:00
break ;
}
}
2001-10-31 01:25:28 +01:00
printf ( " Writting AVI header... \n " ) ;
2001-10-29 02:11:18 +01:00
aviwrite_write_header ( muxer , muxer_f ) ;
switch ( mux_v - > codec ) {
case 0 :
break ;
case VCODEC_DIVX4 :
// init divx4linux:
2001-11-02 04:22:33 +01:00
divx4_param . x_dim = sh_video - > disp_w ;
divx4_param . y_dim = sh_video - > disp_h ;
divx4_param . framerate = ( float ) mux_v - > h . dwRate / mux_v - > h . dwScale ;
if ( ! divx4_param . bitrate ) divx4_param . bitrate = 800000 ;
else if ( divx4_param . bitrate < = 16000 ) divx4_param . bitrate * = 1000 ;
if ( ! divx4_param . quality ) divx4_param . quality = 5 ; // the quality of compression ( 1 - fastest, 5 - best )
divx4_param . handle = NULL ;
encore ( NULL , ENC_OPT_INIT , & divx4_param , NULL ) ;
enc_handle = divx4_param . handle ;
2001-11-03 01:44:02 +01:00
switch ( out_fmt ) {
case IMGFMT_YV12 : enc_frame . colorspace = ENC_CSP_YV12 ; break ;
case IMGFMT_IYUV :
case IMGFMT_I420 : enc_frame . colorspace = ENC_CSP_I420 ; break ;
case IMGFMT_YUY2 : enc_frame . colorspace = ENC_CSP_YUY2 ; break ;
case IMGFMT_UYVY : enc_frame . colorspace = ENC_CSP_UYVY ; break ;
case IMGFMT_RGB24 :
case IMGFMT_BGR24 :
enc_frame . colorspace = ENC_CSP_RGB24 ; break ;
default :
mp_msg ( MSGT_MENCODER , MSGL_ERR , " divx4: unsupported out_fmt! \n " ) ;
}
switch ( pass ) {
case 1 :
VbrControl_init_2pass_vbr_analysis ( passtmpfile , divx4_param . quality ) ;
break ;
case 2 :
VbrControl_init_2pass_vbr_encoding ( passtmpfile ,
divx4_param . bitrate ,
divx4_param . framerate ,
divx4_crispness ,
divx4_param . quality ) ;
break ;
}
2001-10-29 02:11:18 +01:00
break ;
}
2001-10-31 17:08:01 +01:00
# if 1
2001-10-31 23:59:56 +01:00
if ( sh_audio )
2001-10-31 01:25:28 +01:00
switch ( mux_a - > codec ) {
case ACODEC_VBRMP3 :
lame = lame_init ( ) ;
2001-10-31 17:08:01 +01:00
lame_set_bWriteVbrTag ( lame , 0 ) ;
2001-10-31 01:25:28 +01:00
lame_set_in_samplerate ( lame , sh_audio - > samplerate ) ;
lame_set_num_channels ( lame , mux_a - > wf - > nChannels ) ;
2001-11-02 21:05:36 +01:00
lame_set_out_samplerate ( lame , mux_a - > wf - > nSamplesPerSec ) ;
2001-11-02 04:22:33 +01:00
if ( lame_param_vbr ) { // VBR:
lame_set_VBR ( lame , lame_param_vbr ) ; // vbr mode
lame_set_VBR_q ( lame , lame_param_quality + 1 ) ; // 1 = best vbr q 6=~128k
if ( lame_param_br > 0 ) lame_set_VBR_mean_bitrate_kbps ( lame , lame_param_br ) ;
} else { // CBR:
lame_set_quality ( lame , lame_param_quality ) ; // 0 = best q
if ( lame_param_br > 0 ) lame_set_brate ( lame , lame_param_br ) ;
}
if ( lame_param_mode > = 0 ) lame_set_mode ( lame , lame_param_mode ) ; // j-st
if ( lame_param_ratio > 0 ) lame_set_compression_ratio ( lame , lame_param_ratio ) ;
2001-10-31 01:25:28 +01:00
lame_init_params ( lame ) ;
2001-11-02 03:08:00 +01:00
if ( verbose ) {
2001-11-02 04:22:33 +01:00
lame_print_config ( lame ) ;
lame_print_internals ( lame ) ;
2001-11-02 03:08:00 +01:00
}
2001-10-31 01:25:28 +01:00
}
# endif
2001-10-29 02:11:18 +01:00
signal ( SIGINT , exit_sighandler ) ; // Interrupt from keyboard
signal ( SIGQUIT , exit_sighandler ) ; // Quit from keyboard
signal ( SIGTERM , exit_sighandler ) ; // kill
while ( ! eof ) {
2001-10-30 22:22:28 +01:00
float frame_time = 0 ;
2001-10-29 02:11:18 +01:00
int blit_frame = 0 ;
float a_pts = 0 ;
float v_pts = 0 ;
2001-10-30 22:55:28 +01:00
unsigned char * start = NULL ;
int in_size ;
2001-11-01 22:47:28 +01:00
int skip_flag = 0 ; // 1=skip -1=duplicate
2001-10-29 02:11:18 +01:00
2001-11-03 01:44:02 +01:00
if ( play_n_frames > = 0 ) {
- - play_n_frames ;
if ( play_n_frames < 0 ) break ;
}
2001-10-31 00:17:35 +01:00
if ( sh_audio ) {
// get audio:
2001-10-31 01:25:28 +01:00
while ( mux_a - > timer - audio_preload < mux_v - > timer ) {
2001-11-03 22:00:04 +01:00
int len = 0 ;
2001-10-31 00:17:35 +01:00
if ( mux_a - > h . dwSampleSize ) {
2001-11-01 03:31:23 +01:00
// CBR - copy 0.5 sec of audio
2001-10-31 01:25:28 +01:00
switch ( mux_a - > codec ) {
case 0 : // copy
len = sh_audio - > i_bps / 2 ;
len / = mux_a - > h . dwSampleSize ; if ( len < 1 ) len = 1 ;
len * = mux_a - > h . dwSampleSize ;
len = demux_read_data ( sh_audio - > ds , mux_a - > buffer , len ) ;
break ;
case ACODEC_PCM :
len = mux_a - > h . dwSampleSize * ( mux_a - > h . dwRate / 2 ) ;
2001-10-31 17:08:01 +01:00
len = dec_audio ( sh_audio , mux_a - > buffer , len ) ;
2001-10-31 01:25:28 +01:00
break ;
}
2001-10-31 00:17:35 +01:00
} else {
2001-11-01 03:31:23 +01:00
// VBR - encode/copy an audio frame
switch ( mux_a - > codec ) {
case 0 : // copy
printf ( " not yet implemented! \n " ) ;
break ;
case ACODEC_VBRMP3 :
2001-10-31 17:08:01 +01:00
while ( mux_a - > buffer_len < 4 ) {
unsigned char tmp [ 2304 ] ;
int len = dec_audio ( sh_audio , tmp , 2304 ) ;
if ( len < = 0 ) break ; // eof
len = lame_encode_buffer_interleaved ( lame ,
tmp , len / 4 ,
mux_a - > buffer + mux_a - > buffer_len , mux_a - > buffer_size - mux_a - > buffer_len ) ;
if ( len < 0 ) break ; // error
mux_a - > buffer_len + = len ;
}
if ( mux_a - > buffer_len < 4 ) break ;
len = mp_decode_mp3_header ( mux_a - > buffer ) ;
2001-11-02 21:05:36 +01:00
//printf("%d\n",len);
2001-10-31 17:08:01 +01:00
if ( len < = 0 ) break ; // bad frame!
while ( mux_a - > buffer_len < len ) {
unsigned char tmp [ 2304 ] ;
int len = dec_audio ( sh_audio , tmp , 2304 ) ;
if ( len < = 0 ) break ; // eof
len = lame_encode_buffer_interleaved ( lame ,
tmp , len / 4 ,
mux_a - > buffer + mux_a - > buffer_len , mux_a - > buffer_size - mux_a - > buffer_len ) ;
if ( len < 0 ) break ; // error
mux_a - > buffer_len + = len ;
}
2001-11-01 03:31:23 +01:00
break ;
}
2001-10-31 00:17:35 +01:00
}
2001-10-31 01:25:28 +01:00
if ( len < = 0 ) break ; // EOF?
aviwrite_write_chunk ( muxer , mux_a , muxer_f , len , 0 ) ;
2001-11-03 22:09:21 +01:00
if ( ! mux_a - > h . dwSampleSize & & mux_a - > timer > 0 )
mux_a - > wf - > nAvgBytesPerSec = mux_a - > size / mux_a - > timer ; // avg bps (VBR)
2001-10-31 17:08:01 +01:00
if ( mux_a - > buffer_len > = len ) {
mux_a - > buffer_len - = len ;
memcpy ( mux_a - > buffer , mux_a - > buffer + len , mux_a - > buffer_len ) ;
}
2001-10-31 00:17:35 +01:00
}
}
// get video frame!
in_size = video_read_frame ( sh_video , & frame_time , & start , force_fps ) ;
if ( in_size < 0 ) { eof = 1 ; break ; }
2001-10-29 02:11:18 +01:00
sh_video - > timer + = frame_time ;
2001-11-01 22:47:28 +01:00
v_timer_corr - = frame_time - ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ;
// check frame duplicate/drop:
if ( v_timer_corr > = ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ) {
v_timer_corr - = ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ;
+ + skip_flag ; // skip
} else
while ( v_timer_corr < = - ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ) {
v_timer_corr + = ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ;
- - skip_flag ; // dup
}
while ( ( v_pts_corr < = - ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate & & skip_flag > 0 )
| | ( v_pts_corr < = - 2 * ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ) ) {
v_pts_corr + = ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ;
- - skip_flag ; // dup
}
if ( ( v_pts_corr > = ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate & & skip_flag < 0 )
| | ( v_pts_corr > = 2 * ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ) )
if ( skip_flag < = 0 ) { // we can't skip more than 1 frame now
v_pts_corr - = ( float ) mux_v - > h . dwScale / mux_v - > h . dwRate ;
+ + skip_flag ; // skip
}
2001-10-29 02:11:18 +01:00
switch ( mux_v - > codec ) {
2001-10-30 22:55:28 +01:00
case 0 :
mux_v - > buffer = start ;
2001-11-02 21:05:36 +01:00
if ( skip_flag < = 0 ) aviwrite_write_chunk ( muxer , mux_v , muxer_f , in_size , ( sh_video - > ds - > flags & 1 ) ? 0x10 : 0 ) ;
2001-10-30 22:55:28 +01:00
break ;
2001-10-29 02:11:18 +01:00
case VCODEC_DIVX4 :
2001-10-30 22:55:28 +01:00
blit_frame = decode_video ( & video_out , sh_video , start , in_size , 0 ) ;
2001-11-02 21:05:36 +01:00
if ( skip_flag > 0 ) break ;
2001-10-30 22:55:28 +01:00
if ( ! blit_frame ) {
// empty.
aviwrite_write_chunk ( muxer , mux_v , muxer_f , 0 , 0 ) ;
break ;
}
2001-10-29 02:11:18 +01:00
enc_frame . image = vo_image_ptr ;
enc_frame . bitstream = mux_v - > buffer ;
enc_frame . length = mux_v - > buffer_size ;
2001-11-03 01:44:02 +01:00
enc_frame . mvs = NULL ;
2001-10-29 02:11:18 +01:00
enc_frame . quant = 0 ;
enc_frame . intra = 0 ;
2001-11-03 01:44:02 +01:00
if ( pass = = 2 ) { // handle 2-pass:
enc_frame . quant = VbrControl_get_quant ( ) ;
enc_frame . intra = VbrControl_get_intra ( ) ;
encore ( enc_handle , ENC_OPT_ENCODE_VBR , & enc_frame , & enc_result ) ;
VbrControl_update_2pass_vbr_encoding ( enc_result . motion_bits ,
enc_result . texture_bits ,
enc_result . total_bits ) ;
} else {
encore ( enc_handle , ENC_OPT_ENCODE , & enc_frame , & enc_result ) ;
if ( pass = = 1 ) {
VbrControl_update_2pass_vbr_analysis ( enc_result . is_key_frame ,
enc_result . motion_bits ,
enc_result . texture_bits ,
enc_result . total_bits ,
enc_result . quantizer ) ;
}
}
2001-10-29 02:11:18 +01:00
// printf("encoding...\n");
// printf(" len=%d key:%d qualt:%d \n",enc_frame.length,enc_result.is_key_frame,enc_result.quantizer);
aviwrite_write_chunk ( muxer , mux_v , muxer_f , enc_frame . length , enc_result . is_key_frame ? 0x10 : 0 ) ;
break ;
}
2001-11-01 22:47:28 +01:00
if ( skip_flag < 0 ) {
2001-11-01 03:31:23 +01:00
// duplicate frame
2001-11-01 22:47:28 +01:00
printf ( " \n duplicate %d frame(s)!!! \n " , - skip_flag ) ;
while ( skip_flag < 0 ) {
aviwrite_write_chunk ( muxer , mux_v , muxer_f , 0 , 0 ) ;
+ + skip_flag ;
}
2001-11-02 21:05:36 +01:00
} else
if ( skip_flag > 0 ) {
2001-11-01 03:31:23 +01:00
// skip frame
printf ( " \n skip frame!!! \n " ) ;
2001-11-01 22:47:28 +01:00
- - skip_flag ;
2001-11-01 03:31:23 +01:00
}
if ( sh_audio ) {
float AV_delay , x ;
// A-V sync!
if ( pts_from_bps ) {
unsigned int samples = ( sh_audio - > audio . dwSampleSize ) ?
( ( ds_tell ( d_audio ) - sh_audio - > a_in_buffer_len ) / sh_audio - > audio . dwSampleSize ) :
( d_audio - > pack_no ) ; // <- used for VBR audio
a_pts = samples * ( float ) sh_audio - > audio . dwScale / ( float ) sh_audio - > audio . dwRate ;
delay_corrected = 1 ;
} else {
// PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
a_pts = d_audio - > pts ;
if ( ! delay_corrected ) if ( a_pts ) delay_corrected = 1 ;
//printf("*** %5.3f ***\n",a_pts);
a_pts + = ( ds_tell_pts ( d_audio ) - sh_audio - > a_in_buffer_len ) / ( float ) sh_audio - > i_bps ;
}
v_pts = d_video - > pts ;
// av = compensated (with out buffering delay) A-V diff
2001-11-01 22:47:28 +01:00
AV_delay = ( a_pts - v_pts ) ; AV_delay - = mux_a - > timer - ( mux_v - > timer - ( v_timer_corr + v_pts_corr ) ) ;
2001-11-01 03:31:23 +01:00
// compensate input video timer by av:
x = AV_delay * 0.1f ;
if ( x < - max_pts_correction ) x = - max_pts_correction ; else
if ( x > max_pts_correction ) x = max_pts_correction ;
if ( default_max_pts_correction > = 0 )
max_pts_correction = default_max_pts_correction ;
else
max_pts_correction = sh_video - > frametime * 0.10 ; // +-10% of time
// sh_video->timer-=x;
c_total + = x ;
2001-11-01 22:47:28 +01:00
v_pts_corr + = x ;
2001-11-01 03:31:23 +01:00
2001-11-01 22:47:28 +01:00
printf ( " A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r " ,
2001-11-01 03:31:23 +01:00
a_pts , v_pts , a_pts - v_pts ,
( float ) ( mux_a - > timer - mux_v - > timer ) ,
2001-11-01 22:47:28 +01:00
AV_delay , c_total , v_pts_corr ) ;
2001-11-01 03:31:23 +01:00
}
#if 0
mp_msg ( MSGT_AVSYNC , MSGL_STATUS , " A:%6.1f V:%6.1f A-V:%7.3f ct:%7.3f %3d/%3d %2d%% %2d%% %4.1f%% %d%% \r " ,
a_pts , v_pts , a_pts - v_pts , c_total ,
( int ) sh_video - > num_frames , ( int ) sh_video - > num_frames_decoded ,
( sh_video - > timer > 0.5 ) ? ( int ) ( 100.0 * video_time_usage / ( double ) sh_video - > timer ) : 0 ,
( sh_video - > timer > 0.5 ) ? ( int ) ( 100.0 * vout_time_usage / ( double ) sh_video - > timer ) : 0 ,
( sh_video - > timer > 0.5 ) ? ( 100.0 * audio_time_usage / ( double ) sh_video - > timer ) : 0
, cache_fill_status
) ;
# endif
fflush ( stdout ) ;
2001-10-29 02:11:18 +01:00
} // while(!eof)
2001-11-02 03:08:00 +01:00
printf ( " \n Writting AVI index... \n " ) ;
2001-10-29 02:11:18 +01:00
aviwrite_write_index ( muxer , muxer_f ) ;
2001-11-02 03:08:00 +01:00
printf ( " Fixup AVI header... \n " ) ;
2001-10-29 02:11:18 +01:00
fseek ( muxer_f , 0 , SEEK_SET ) ;
aviwrite_write_header ( muxer , muxer_f ) ; // update header
fclose ( muxer_f ) ;
2001-11-02 02:25:13 +01:00
if ( stream ) free_stream ( stream ) ; // kill cache thread
2001-10-29 02:11:18 +01:00
}