/*****************************************************************************
 *
 * confvidmpeg1mencoderdialog.cpp -- Input the lavc_mpeg1video codec parameters.
 *
 * Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *
 *****************************************************************************/

#include "../video/video_functions.h"

#include "confvidmpeg1mencoderdialog.h"
#include "userdefinedrezdialog.h"
#include "userdefinedfpsdialog.h"
#include "../encoder/encoder.h"
#include "macros.h"
#include <QtGui>

ConfVidMpeg1MEncoderDialog::ConfVidMpeg1MEncoderDialog(encoder_video_settings_t *settings, 
	video_t *track, QWidget *parent)
	: QDialog(parent),
	suspendMuxerSettingsUpdate(false),
	bitrateMin( 384 ),
	bitrateMax( 1856 )
{
	callerSettings = settings;
	videoSource = track;

	// Local copy allows to cancel changes
	encSettings = new encoder_video_settings_t( *settings );

	// Ensure consistent MPEG-1 settings
	encSettings->mpeg1.resX = qMax( encSettings->mpeg1.resX, (int) widthMin );
	encSettings->mpeg1.resX = qMin( encSettings->mpeg1.resX, (int) widthMax );
	encSettings->mpeg1.resY = qMax( encSettings->mpeg1.resY, (int) heightMin );
	encSettings->mpeg1.resY = qMin( encSettings->mpeg1.resY, (int) heightMax );
	encSettings->mpeg1.bitrate = qMax( encSettings->mpeg1.bitrate, bitrateMin );
	encSettings->mpeg1.bitrate = qMin( encSettings->mpeg1.bitrate, bitrateMax );
	encSettings->mpeg1.minvbr = qMin( encSettings->mpeg1.minvbr, encSettings->mpeg1.bitrate );
	encSettings->mpeg1.maxvbr = qMax( encSettings->mpeg1.maxvbr, encSettings->mpeg1.bitrate );
	if (encSettings->mpeg1.stream != "mpeg1") encSettings->mpeg1.stream = "xvcd"; 
	if (encSettings->mpeg1.mpeg_fps < 1 || encSettings->mpeg1.mpeg_fps > 8) encSettings->mpeg1.mpeg_fps = 4;

	setMinimumSize( 280, 360 );
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	verticalLayout = new QVBoxLayout(this);

	// Global buttons
	buttonBox = new QDialogButtonBox(this);

	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

	// Build all tabs
	tabWidget = new QTabWidget(this);
	tabWidget->setMovable(false);

	mainTab = new QWidget();
	advancedTab = new QWidget();
	muxerTab = new QWidget();

	tabWidget->addTab(mainTab, QString());
	tabWidget->addTab(advancedTab, QString());
	tabWidget->addTab(muxerTab, QString());

	verticalLayout->addWidget(tabWidget);
	verticalLayout->addWidget(buttonBox);

	tabWidget->setCurrentIndex(0);

	// Main tab 
	mainTabVLayout = new QVBoxLayout(mainTab);
	mainTabVLayout->setSpacing(12);

	// - TV standard
	tvGroupBox = new QGroupBox(mainTab);
	tvVLayout = new QVBoxLayout(tvGroupBox);
	tvNtscRadioButton = new QRadioButton(tvGroupBox);
	tvPalRadioButton = new QRadioButton(tvGroupBox);
	tvFreeRadioButton = new QRadioButton(tvGroupBox);
	
	tvVLayout->addWidget(tvNtscRadioButton);
	tvVLayout->addWidget(tvPalRadioButton);
	tvVLayout->addWidget(tvFreeRadioButton);

	mainTabVLayout->addWidget(tvGroupBox);

	// - Rez and framerate
	frameGroupBox = new QGroupBox(mainTab);
	horizontalLayout = new QHBoxLayout(frameGroupBox);
	rezComboBox = new QComboBox(frameGroupBox);
	fpsComboBox = new QComboBox(frameGroupBox);

	horizontalLayout->addWidget(rezComboBox);
	horizontalLayout->addWidget(fpsComboBox);

	mainTabVLayout->addWidget(frameGroupBox);

	// - Bitrate
	bitrateGroupBox = new QGroupBox(mainTab);
	bitrateVLayout = new QVBoxLayout(bitrateGroupBox);
	bitrateHLayout = new QHBoxLayout();
	bitrateSlider = new QSlider(bitrateGroupBox);
	bitrateLineEdit = new QLineEdit(bitrateGroupBox);

	bitrateSlider->setOrientation(Qt::Horizontal);
	bitrateSlider->setInvertedAppearance(false);
	bitrateSlider->setTickPosition(QSlider::NoTicks);

	bitrateSlider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	bitrateLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	bitrateLineEdit->setMaxLength(6);
	bitrateLineEdit->setMaximumWidth(50);

	bitrateHLayout->addWidget(bitrateSlider);
	bitrateHLayout->addWidget(bitrateLineEdit);

	bitrateVLayout->addLayout(bitrateHLayout);

	mainTabVLayout->addWidget(bitrateGroupBox);

	// - Format
	formatGroupBox = new QGroupBox(mainTab);
	formatVLayout = new QVBoxLayout(formatGroupBox);
	formatMpeg1RadioButton = new QRadioButton(formatGroupBox);
	formatVcdRadioButton = new QRadioButton(formatGroupBox);
	
	formatVLayout->addWidget(formatMpeg1RadioButton);
	formatVLayout->addWidget(formatVcdRadioButton);

	mainTabVLayout->addWidget(formatGroupBox);

	// - Force standard VCD
	standardVcdHLayout = new QHBoxLayout();
	standardVcdHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
	standardVcdCheckBox = new QCheckBox(mainTab);

	standardVcdHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	standardVcdHLayout->addWidget(standardVcdCheckBox);
	standardVcdHLayout->addItem(standardVcdHSpacer);

	mainTabVLayout->addLayout(standardVcdHLayout);

	// - Spacer
	verticalSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);

	mainTabVLayout->addItem(verticalSpacer);

	// Advanced tab
	advancedTabVLayout = new QVBoxLayout(advancedTab);
	advancedTabVLayout->setSpacing(12);

	// - Bitrate allocation
	br_allocGroupBox = new QGroupBox(advancedTab);
	br_allocVLayout = new QVBoxLayout(br_allocGroupBox);
	br_allocComboBox = new QComboBox(br_allocGroupBox);
	vbrGLayout = new QGridLayout();
	
	br_allocVLayout->addWidget(br_allocComboBox);
	br_allocVLayout->addLayout(vbrGLayout);

	advancedTabVLayout->addWidget(br_allocGroupBox);

	// -- VBR nominal
	vbr_nomLabel = new QLabel(br_allocGroupBox);
	vbr_nomSlider = new QSlider(br_allocGroupBox);
	vbr_nomLineEdit = new QLineEdit(br_allocGroupBox);

	vbr_nomSlider->setOrientation(Qt::Horizontal);
	vbr_nomSlider->setInvertedAppearance(false);
	vbr_nomSlider->setTickPosition(QSlider::NoTicks);

	vbr_nomLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	vbr_nomSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	vbr_nomLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	vbr_nomLineEdit->setMaxLength(4);
	vbr_nomLineEdit->setMaximumWidth(40);

	vbrGLayout->addWidget(vbr_nomLabel,		0, 0);
	vbrGLayout->addWidget(vbr_nomSlider,	0, 1);
	vbrGLayout->addWidget(vbr_nomLineEdit,	0, 2);

	// -- VBR min
	vbr_minLabel = new QLabel(br_allocGroupBox);
	vbr_minSlider = new QSlider(br_allocGroupBox);
	vbr_minLineEdit = new QLineEdit(br_allocGroupBox);

	vbr_minSlider->setOrientation(Qt::Horizontal);
	vbr_minSlider->setInvertedAppearance(false);
	vbr_minSlider->setTickPosition(QSlider::NoTicks);

	vbr_minLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	vbr_minSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	vbr_minLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	vbr_minLineEdit->setMaxLength(4);
	vbr_minLineEdit->setMaximumWidth(40);

	vbrGLayout->addWidget(vbr_minLabel,		1, 0);
	vbrGLayout->addWidget(vbr_minSlider,	1, 1);
	vbrGLayout->addWidget(vbr_minLineEdit,	1, 2);

	// -- VBR max
	vbr_maxLabel = new QLabel(br_allocGroupBox);
	vbr_maxSlider = new QSlider(br_allocGroupBox);
	vbr_maxLineEdit = new QLineEdit(br_allocGroupBox);

	vbr_maxSlider->setOrientation(Qt::Horizontal);
	vbr_maxSlider->setInvertedAppearance(false);
	vbr_maxSlider->setTickPosition(QSlider::NoTicks);

	vbr_minLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	vbr_maxSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	vbr_maxLineEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
	vbr_maxLineEdit->setMaxLength(4);
	vbr_maxLineEdit->setMaximumWidth(40);

	vbrGLayout->addWidget(vbr_maxLabel,		2, 0);
	vbrGLayout->addWidget(vbr_maxSlider,	2, 1);
	vbrGLayout->addWidget(vbr_maxLineEdit,	2, 2);

	// - GOP
	gopGroupBox = new QGroupBox(advancedTab);
	gopVLayout = new QVBoxLayout(gopGroupBox);

	gop_sizeHLayout = new QHBoxLayout();
	gop_sizeLineEdit = new QLineEdit(gopGroupBox);
	gop_sizeLineEdit->setMaxLength(3);
	gop_sizeLineEdit->setMaximumSize(QSize(50, 40));
	gop_sizeLabel = new QLabel(gopGroupBox);
	gop_sizeHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	gop_sizeHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	gop_sizeHLayout->addWidget(gop_sizeLineEdit);
	gop_sizeHLayout->addWidget(gop_sizeLabel);
	gop_sizeHLayout->addItem(gop_sizeHSpacer);

	closed_gopCheckBox = new QCheckBox(gopGroupBox);

	gopVLayout->addLayout(gop_sizeHLayout);
	gopVLayout->addWidget(closed_gopCheckBox);

	advancedTabVLayout->addWidget(gopGroupBox);

	// - Force letter-box
	letterBoxHLayout = new QHBoxLayout();
	letterBoxHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
	letterBoxCheckBox = new QCheckBox(advancedTab);

	letterBoxHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	letterBoxHLayout->addWidget(letterBoxCheckBox);
	letterBoxHLayout->addItem(letterBoxHSpacer);

	advancedTabVLayout->addLayout(letterBoxHLayout);

	// - Spacer
	advancedTabSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);

	advancedTabVLayout->addItem(advancedTabSpacer);

	// Muxer tab
	muxerTabVLayout = new QVBoxLayout(muxerTab);
	muxerTabVLayout->setSpacing(12);

	// - MPEG A/V Muxer settings
	muxerGroupBox = new QGroupBox(muxerTab);
	muxerVLayout = new QVBoxLayout(muxerGroupBox);

	muxerTabVLayout->addWidget(muxerGroupBox);

	// -- Pack size
	pack_sizeHLayout = new QHBoxLayout();
	pack_sizeLineEdit = new QLineEdit(muxerGroupBox);
	pack_sizeLineEdit->setMaximumSize(QSize(50, 40));
	pack_sizeLabel = new QLabel(muxerGroupBox);
	pack_sizeSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	pack_sizeHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	pack_sizeHLayout->addWidget(pack_sizeLineEdit);
	pack_sizeHLayout->addWidget(pack_sizeLabel);
	pack_sizeHLayout->addItem(pack_sizeSpacer);

	muxerVLayout->addLayout(pack_sizeHLayout);

	// -- Muxer rate
	muxrateHLayout = new QHBoxLayout();
	muxrateLineEdit = new QLineEdit(muxerGroupBox);
	muxrateLineEdit->setMaximumSize(QSize(50, 40));
	muxrateLabel = new QLabel(muxerGroupBox);
	muxrateSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	muxrateHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	muxrateHLayout->addWidget(muxrateLineEdit);
	muxrateHLayout->addWidget(muxrateLabel);
	muxrateHLayout->addItem(muxrateSpacer);

	muxerVLayout->addLayout(muxrateHLayout);

	// -- Better interleaving
	interleaving2HLayout = new QHBoxLayout();
	interleaving2CheckBox = new QCheckBox(muxerGroupBox);
	interleaving2Spacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	interleaving2HLayout->setSizeConstraint(QLayout::SetMaximumSize);
	interleaving2HLayout->addWidget(interleaving2CheckBox);
	interleaving2HLayout->addItem(interleaving2Spacer);

	muxerVLayout->addLayout(interleaving2HLayout);

	// -- Video delay
	vdelayHLayout = new QHBoxLayout();
	vdelayLineEdit = new QLineEdit(muxerGroupBox);
	vdelayLineEdit->setMaximumSize(QSize(50, 40));
	vdelayLabel = new QLabel(muxerGroupBox);
	vdelaySpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	vdelayHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	vdelayHLayout->addWidget(vdelayLineEdit);
	vdelayHLayout->addWidget(vdelayLabel);
	vdelayHLayout->addItem(vdelaySpacer);

	muxerVLayout->addLayout(vdelayHLayout);

	// -- Audio delay
	adelayHLayout = new QHBoxLayout();
	adelayLineEdit = new QLineEdit(muxerGroupBox);
	adelayLineEdit->setMaximumSize(QSize(50, 40));
	adelayLabel = new QLabel(muxerGroupBox);
	adelaySpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	adelayHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	adelayHLayout->addWidget(adelayLineEdit);
	adelayHLayout->addWidget(adelayLabel);
	adelayHLayout->addItem(adelaySpacer);

	muxerVLayout->addLayout(adelayHLayout);

	// -- Drop anticipated audio
	dropHLayout = new QHBoxLayout();
	dropCheckBox = new QCheckBox(muxerGroupBox);
	dropSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	dropHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	dropHLayout->addWidget(dropCheckBox);
	dropHLayout->addItem(dropSpacer);

	muxerVLayout->addLayout(dropHLayout);

	// -- Video buffer size
	vbuf_sizeHLayout = new QHBoxLayout();
	vbuf_sizeLineEdit = new QLineEdit(muxerGroupBox);
	vbuf_sizeLineEdit->setMaximumSize(QSize(50, 40));
	vbuf_sizeLabel = new QLabel(muxerGroupBox);
	vbuf_sizeSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	vbuf_sizeHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	vbuf_sizeHLayout->addWidget(vbuf_sizeLineEdit);
	vbuf_sizeHLayout->addWidget(vbuf_sizeLabel);
	vbuf_sizeHLayout->addItem(vbuf_sizeSpacer);

	muxerVLayout->addLayout(vbuf_sizeHLayout);

	// -- Audio buffer size
	abuf_sizeHLayout = new QHBoxLayout();
	abuf_sizeLineEdit = new QLineEdit(muxerGroupBox);
	abuf_sizeLineEdit->setMaximumSize(QSize(50, 40));
	abuf_sizeLabel = new QLabel(muxerGroupBox);
	abuf_sizeSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	abuf_sizeHLayout->setSizeConstraint(QLayout::SetMaximumSize);
	abuf_sizeHLayout->addWidget(abuf_sizeLineEdit);
	abuf_sizeHLayout->addWidget(abuf_sizeLabel);
	abuf_sizeHLayout->addItem(abuf_sizeSpacer);

	muxerVLayout->addLayout(abuf_sizeHLayout);

	// - Spacer
	muxerVSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);

	muxerTabVLayout->addItem(muxerVSpacer);

	// - Default muxer settings
	muxerDefaultHLayout = new QHBoxLayout();
	muxerDefaultPushButton = new QPushButton(muxerTab);
	muxerDefaultHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	muxerDefaultHLayout->addWidget(muxerDefaultPushButton);
	muxerDefaultHLayout->addItem(muxerDefaultHSpacer);

	muxerTabVLayout->addLayout(muxerDefaultHLayout);

	// Captions
	setWindowTitle( tr("MPEG-1 encoder settings") );

	tabWidget->setTabText(tabWidget->indexOf(mainTab), tr("General") );
	tabWidget->setTabText(tabWidget->indexOf(advancedTab), tr("Advanced") );
	tabWidget->setTabText(tabWidget->indexOf(muxerTab), tr("Muxer") );

	frameGroupBox->setTitle( tr(" Picture size - Frame rate ") );
	bitrateGroupBox->setTitle( tr(" Bitrate ( kbps ) ") );
	tvGroupBox->setTitle( tr(" TV standard ") );
	tvNtscRadioButton->setText( tr("NTSC") );
	tvPalRadioButton->setText( tr("PAL") );
	tvFreeRadioButton->setText( tr("Not standard") );
	formatGroupBox->setTitle( tr(" Stream format ") );
	formatMpeg1RadioButton->setText( tr("MPEG-1 packet stream") );
	formatVcdRadioButton->setText( tr("VideoCD packet stream") );
	standardVcdCheckBox->setText( tr("Force standard VideoCD") );

	br_allocGroupBox->setTitle( tr(" Bitrate allocation ") );
	vbr_nomLabel->setText( tr("- Nominal VBR") );
	vbr_minLabel->setText( tr("- Minimum VBR") );
	vbr_maxLabel->setText(   tr("- Maximum VBR") );
	gopGroupBox->setTitle( tr("  Group Of Pictures ") );
	gop_sizeLabel->setText( tr("GOP size") );
	closed_gopCheckBox->setText( tr("Closed GOP") );
	letterBoxCheckBox->setText( tr("Letter-box 4:3") );

	muxerGroupBox->setTitle( tr(" MPEG audio-video muxer ") );
	pack_sizeLabel->setText( tr("MPEG muxer packet size") );
	muxrateLabel->setText( tr("MPEG muxer rate") );
	vdelayLabel->setText( tr("Video delay") );
	adelayLabel->setText( tr("Audio delay") );
	vbuf_sizeLabel->setText( tr("Video buffer size") );
	abuf_sizeLabel->setText( tr("Audio buffer size") );
	interleaving2CheckBox->setText( tr("Better algorithm to interleave audio and video packets") );
	dropCheckBox->setText( tr("Drop the part of audio anticipated with video delay") );

	muxerDefaultPushButton->setText( tr("Default muxer settings") );

	// Tool tips and What's this
	setToolTip();
	setWhatsThis();

	setAttribute(Qt::WA_AlwaysShowToolTips);

	bitrateSlider->setRange( bitrateMin, bitrateMax );
	vbr_nomSlider->setRange( bitrateMin, bitrateMax );
	vbr_minSlider->setRange( bitrateMin, bitrateMax );
	vbr_maxSlider->setRange( bitrateMin, bitrateMax );

	bitrateValidator = new QIntValidator( bitrateMin, bitrateMax, this );
	vbr_nomValidator = new QIntValidator( bitrateMin, encSettings->mpeg1.bitrate, this );
	vbr_minValidator = new QIntValidator( bitrateMin, encSettings->mpeg1.bitrate, this );
	vbr_maxValidator = new QIntValidator( encSettings->mpeg1.bitrate, bitrateMax, this );

	bitrateLineEdit->setValidator( bitrateValidator );
	vbr_nomLineEdit->setValidator( vbr_nomValidator );
	vbr_minLineEdit->setValidator( vbr_minValidator );
	vbr_maxLineEdit->setValidator( vbr_maxValidator );

	populateFps(); // may alter tvButtons
	populateRez();
	populateBrAllocation();

	// Signals which don't change settings
	connect(bitrateLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(bitrateLineEditChanged(QString)));
	connect(bitrateSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(bitrateSliderChanged(int)));

	connect(vbr_nomLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(vbr_nomLineEditChanged(QString)));
	connect(vbr_nomSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(vbr_nomSliderChanged(int)));

	connect(vbr_minLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(vbr_minLineEditChanged(QString)));
	connect(vbr_minSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(vbr_minSliderChanged(int)));
	connect(vbr_minSlider, SIGNAL(sliderReleased()), 
		this, SLOT(vbr_minSliderReleased()));

	connect(vbr_maxLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(vbr_maxLineEditChanged(QString)));
	connect(vbr_maxSlider, SIGNAL(valueChanged(int)), 
		this, SLOT(vbr_maxSliderChanged(int)));
	connect(vbr_maxSlider, SIGNAL(sliderReleased()), 
		this, SLOT(vbr_maxSliderReleased()));

	// Update controls from current settings before connecting signals
	standardVcdCheckBox->setChecked( encSettings->mpeg1.force_vcd == true );
	standardVcdCheckBoxClicked(); // disable appropriate controls and call updateControls();

	// Now connect other signals (which controls are up-to-date)
	connect(rezComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectRez(int)));

	connect(fpsComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectFps(int)));

	connect(br_allocComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectBrAllocation(int)));

	connect(standardVcdCheckBox, SIGNAL(clicked()), 
		this, SLOT(standardVcdCheckBoxClicked()));
	connect(closed_gopCheckBox, SIGNAL(clicked()), 
		this, SLOT(closed_gopCheckBoxClicked()));
	connect(letterBoxCheckBox, SIGNAL(clicked()), 
		this, SLOT(letterBoxCheckBoxClicked()));

	connect(tvNtscRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectTv()));
	connect(tvPalRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectTv()));
	connect(tvFreeRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectTv()));

	connect(formatMpeg1RadioButton, SIGNAL(clicked()), 
		this, SLOT(selectFormat()));
	connect(formatVcdRadioButton, SIGNAL(clicked()), 
		this, SLOT(selectFormat()));

	connect(gop_sizeLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(gop_sizeLineEditChanged(QString)));

	connect(pack_sizeLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateMpegMuxerSettings()));
	connect(muxrateLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateMpegMuxerSettings()));
	connect(vdelayLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateMpegMuxerSettings()));
	connect(adelayLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateMpegMuxerSettings()));
	connect(vbuf_sizeLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateMpegMuxerSettings()));
	connect(abuf_sizeLineEdit, SIGNAL(textChanged(QString)), 
		this, SLOT(updateMpegMuxerSettings()));

	connect(interleaving2CheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateMpegMuxerSettings()));
	connect(dropCheckBox, SIGNAL(toggled(bool)), 
		this, SLOT(updateMpegMuxerSettings()));

	connect(muxerDefaultPushButton, SIGNAL(clicked()), 
		this, SLOT(muxerDefaultPushButtonClicked()));

	// Global buttons
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
}

void ConfVidMpeg1MEncoderDialog::accept()
{
	*callerSettings = *encSettings;

	QDialog::accept();
}

void ConfVidMpeg1MEncoderDialog::updateControls()
{
	int i;

	// State flags
	bool ntsc, pal, vcd;

	suspendMuxerSettingsUpdate = true;

	ntsc = encSettings->mpeg1.resX == 352 && encSettings->mpeg1.resY == 240
		&& ( encSettings->mpeg1.mpeg_fps == 1 || encSettings->mpeg1.mpeg_fps == 4 || encSettings->mpeg1.mpeg_fps == 7 );

	pal = encSettings->mpeg1.resX == 352 && encSettings->mpeg1.resY == 288
			&& ( encSettings->mpeg1.mpeg_fps == 3 || encSettings->mpeg1.mpeg_fps == 6 );

	vcd = encSettings->mpeg1.stream == "xvcd"
			&& encSettings->mpeg1.letter_box
			&& ( (ntsc && encSettings->mpeg1.bitrate == 1150)
					|| (pal && encSettings->mpeg1.bitrate == 1150) );

	// Update GUI
	bitrateSlider->setValue( encSettings->mpeg1.bitrate );
	bitrateLineEdit->setText( QString::number(encSettings->mpeg1.bitrate) );

	vbr_nomSlider->setValue( encSettings->mpeg1.bitrate );
	vbr_nomLineEdit->setText( QString::number(encSettings->mpeg1.bitrate) );

	vbr_minSlider->setValue( encSettings->mpeg1.minvbr );
	vbr_minLineEdit->setText( QString::number(encSettings->mpeg1.minvbr) );

	vbr_maxSlider->setValue( encSettings->mpeg1.maxvbr );
	vbr_maxLineEdit->setText( QString::number(encSettings->mpeg1.maxvbr) );

	vbr_minValidator->setRange( bitrateMin, encSettings->mpeg1.bitrate );
	vbr_maxValidator->setRange( encSettings->mpeg1.bitrate, bitrateMax );

	if ( encSettings->mpeg1.cbr )
	{
		br_allocComboBox->setCurrentIndex( 0 ); // CBR

		vbr_minLabel->setEnabled(false);
		vbr_minSlider->setEnabled(false);
		vbr_minLineEdit->setEnabled(false);
		vbr_maxLabel->setEnabled(false);
		vbr_maxSlider->setEnabled(false);
		vbr_maxLineEdit->setEnabled(false);
	}
	else
	{
		br_allocComboBox->setCurrentIndex( 1 ); // VBR
		
		vbr_minLabel->setEnabled(true);
		vbr_minSlider->setEnabled(true);
		vbr_minLineEdit->setEnabled(true);
		vbr_maxLabel->setEnabled(true);
		vbr_maxSlider->setEnabled(true);
		vbr_maxLineEdit->setEnabled(true);		
	}

	formatMpeg1RadioButton->setChecked( encSettings->mpeg1.stream == "mpeg1" );
	formatVcdRadioButton->setChecked( encSettings->mpeg1.stream == "xvcd" );

	tvNtscRadioButton->setChecked( ntsc );
	tvPalRadioButton->setChecked( pal );
	tvFreeRadioButton->setChecked( !ntsc && !pal );

	letterBoxCheckBox->setChecked( encSettings->mpeg1.letter_box );

	i = rezComboBox->findData( QSize(encSettings->mpeg1.resX, encSettings->mpeg1.resY) );

	if ( i == -1 )
	{
		rezComboBox->setItemData( 1, QSize(encSettings->mpeg1.resX, encSettings->mpeg1.resY) );
		rezComboBox->setItemText( 1, tr("%1x%2 (user defined)")
										.arg(encSettings->mpeg1.resX)
										.arg(encSettings->mpeg1.resY) );
		rezComboBox->setCurrentIndex( 1 );
	}
	else
	{
		rezComboBox->setCurrentIndex( i );
	}
	
	i = fpsComboBox->findData( encSettings->mpeg1.mpeg_fps );

	if ( i == -1 ) // should not happened
	{
		fpsComboBox->setCurrentIndex( 0 );
		encSettings->mpeg1.mpeg_fps = fpsComboBox->itemData( 0 ).toInt();
	}
	else
	{
		fpsComboBox->setCurrentIndex( i );
	}

	gop_sizeLineEdit->setText( QString::number(encSettings->mpeg1.gop_size) );
	closed_gopCheckBox->setChecked( encSettings->mpeg1.closed_gop );

	pack_sizeLineEdit->setText( QString::number(encSettings->mpeg1.pack_size) );
	muxrateLineEdit->setText( QString::number(encSettings->mpeg1.muxrate) );
	vdelayLineEdit->setText( QString::number(encSettings->mpeg1.vdelay) );
	adelayLineEdit->setText( QString::number(encSettings->mpeg1.adelay) );
	vbuf_sizeLineEdit->setText( QString::number(encSettings->mpeg1.vbuf_size) );
	abuf_sizeLineEdit->setText( QString::number(encSettings->mpeg1.abuf_size) );

	interleaving2CheckBox->setChecked( encSettings->mpeg1.interleaving2 );
	dropCheckBox->setChecked( encSettings->mpeg1.drop );

	suspendMuxerSettingsUpdate = false;
}

void ConfVidMpeg1MEncoderDialog::updateMpegMuxerSettings()
{
	if (suspendMuxerSettingsUpdate) return;

	encSettings->mpeg1.pack_size = pack_sizeLineEdit->text().toInt();		
	encSettings->mpeg1.muxrate =  muxrateLineEdit->text().toInt();
	encSettings->mpeg1.interleaving2 = interleaving2CheckBox->isChecked();
	encSettings->mpeg1.vdelay =  vdelayLineEdit->text().toInt();
	encSettings->mpeg1.adelay =  adelayLineEdit->text().toInt();
	encSettings->mpeg1.drop = dropCheckBox->isChecked();
	encSettings->mpeg1.vbuf_size =  vbuf_sizeLineEdit->text().toInt();
	encSettings->mpeg1.abuf_size =  abuf_sizeLineEdit->text().toInt();
}

void ConfVidMpeg1MEncoderDialog::enterUserDefinedSize()
{
	QSize size(encSettings->user_defined.mpeg1.resX, encSettings->user_defined.mpeg1.resY);

	QSize min = QSize(widthMin, heightMin);
	QSize max = QSize(widthMax, heightMax);

	UserDefinedRezDialog *dlg = new UserDefinedRezDialog(&size, min, max, this);
	int res = dlg->exec();		
	Q_CHECK_PTR(dlg);
	delete(dlg);

	if ( res == QDialog::Rejected ) 
	{
		return;
	}

	encSettings->user_defined.mpeg1.resX = size.width();
	encSettings->user_defined.mpeg1.resY = size.height();

	// Update combo box
	rezComboBox->setItemText( 1, tr("%1x%2 (user defined)")
		.arg(size.width())
		.arg(size.height()));
	rezComboBox->setItemData( 1, size);

	// Select user defined rezolution
	rezComboBox->setCurrentIndex( 1 );

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::populateFps()
{
	int mpeg_fps(0);

	fpsComboBox->clear();

	// Item #0 (first must be the recommended framerate)
	if ( tvNtscRadioButton->isChecked() )
	{
			mpeg_fps = (videoSource->fps != 23.976)? 4 : 1;
	}
	else if ( tvPalRadioButton->isChecked() )
	{
		mpeg_fps = 3;
	}
	else
	{
		mpeg_fps = Video::intDvdFps( videoSource->fps );
	}

	if ( mpeg_fps == 0 ) // forbidden
	{
		mpeg_fps = 2;	// 24 FPS
	}

	encSettings->recommendation.mpeg1.mpeg_fps = mpeg_fps;

	fpsComboBox->addItem( tr("%1 FPS (recommended)")
		.arg( Video::strDvdFps(mpeg_fps) ), mpeg_fps);
	
	// Build a list of MPEG authorized fps 
	QList<int> commonFps;
	commonFps << 1	// 23.976
		<< 2		// 24.0
		<< 3		// 25.0
		<< 4		// 29.97
		<< 5		// 30.0
		<< 6		// 50.0
		<< 7		// 59.94
		<< 8;		// 60.0

	QList<int> ntscFps, PalFps;
	ntscFps << 1	// 23.976
		<< 4		// 29.97
		<< 7;		// 59.94
	PalFps << 3		// 25.0
		<< 6;		// 50.0

	// Item #2... (push every common fps) 
	while (!commonFps.isEmpty() )
	{
		mpeg_fps = commonFps.takeFirst();

		if ( ntscFps.indexOf( mpeg_fps ) != -1 )
		{
			fpsComboBox->addItem( tr("%1 FPS (NTSC)")
				.arg( Video::strDvdFps(mpeg_fps) ) , mpeg_fps);
		}
		else if ( PalFps.indexOf( mpeg_fps ) != -1 )
		{
			fpsComboBox->addItem( tr("%1 FPS (PAL)")
				.arg( Video::strDvdFps(mpeg_fps) ) , mpeg_fps);
		}
		else
		{
			fpsComboBox->addItem( tr("%1 FPS")
				.arg( Video::strDvdFps(mpeg_fps) ) , mpeg_fps);
		}
	}

	// Select current fps
	int index = fpsComboBox->findData( encSettings->mpeg1.mpeg_fps );

	if ( index == -1 ) // should not happened
	{
		fpsComboBox->setCurrentIndex( 0 );
		encSettings->mpeg1.mpeg_fps = fpsComboBox->itemData( 0 ).toInt();
	}
	else
	{
		fpsComboBox->setCurrentIndex( index );
	}

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::populateRez()
{
	QSize size;
	
	rezComboBox->clear();

	// First must be the recommended size
	if ( tvNtscRadioButton->isChecked() && standardVcdCheckBox->isChecked() )
	{
		size.setWidth(352);
		size.setHeight(240);
	}
	else if ( tvPalRadioButton->isChecked() && standardVcdCheckBox->isChecked() )
	{
		size.setWidth(352);
		size.setHeight(288);
	}
	else
	{
		size.setWidth(320);
		size.setHeight(240);
	}

	encSettings->recommendation.mpeg1.resX = size.width();
	encSettings->recommendation.mpeg1.resY = size.height();

	// Item #0
	rezComboBox->addItem( tr("%1x%2 (recommended)")
		.arg(size.width())
		.arg(size.height()) , size);
	
	// Second must be the user defined
	size = QSize(encSettings->user_defined.mpeg1.resX, encSettings->user_defined.mpeg1.resY);

	if (size.width() <= 0)
	{
		size.setWidth(352);
	}

	if (size.height() <= 0)
	{
		size.setHeight(240);
	}

	encSettings->user_defined.mpeg1.resX = size.width();
	encSettings->user_defined.mpeg1.resY = size.height();

	// Item #1
	rezComboBox->addItem( tr("%1x%2 (user defined)")
		.arg(size.width())
		.arg(size.height()) , size);

	// Build a list of most common rez 
	QList<QSize> commonSizes;
	commonSizes << QSize( 320, 240 )
		<< QSize( 352, 240 )
		<< QSize( 352, 288 )
		<< QSize( 384, 288 );

	QList<QSize> vcdSizes;
	vcdSizes << QSize( 352, 240 )
		<< QSize( 352, 288 );

	// Item #2... (push every common rez) 
	while (!commonSizes.isEmpty() )
	{
		size = commonSizes.takeFirst();

		if ( vcdSizes.indexOf( size ) == -1 )
		{
			rezComboBox->addItem( tr("%1x%2")
				.arg(size.width())
				.arg(size.height()) , size);
		}
		else
		{
			rezComboBox->addItem( tr("%1x%2 (VideoCD)")
				.arg(size.width())
				.arg(size.height()) , size);
		}
	}

	// Select current rez
	size = QSize(encSettings->mpeg1.resX, encSettings->mpeg1.resY);

	if (size.width() <= 0)
	{
		size.setWidth(352);
	}

	if (size.height() <= 0)
	{
		size.setHeight(240);
	}

	encSettings->mpeg1.resX = size.width();
	encSettings->mpeg1.resY = size.height();

	int index = rezComboBox->findData( size );

	if (index == -1)
	{
		// Missing current rez in list is forced to be the user defined (item #1)
		index = 1;
		rezComboBox->setItemText( index, tr("%1x%2 (user defined)")
			.arg(size.width())
			.arg(size.height()));
		rezComboBox->setItemData( index, size);
	}

	rezComboBox->setCurrentIndex( index );

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::populateBrAllocation()
{
	br_allocComboBox->clear();

	br_allocComboBox->addItem( tr("CBR - Constant Bit Rate"), CBR );
	br_allocComboBox->addItem( tr("VBR - Variable Bit Rate"), VBR );

	if ( encSettings->mpeg1.cbr )
	{
		br_allocComboBox->setCurrentIndex( 0 ); // CBR
	}
	else
	{
		br_allocComboBox->setCurrentIndex( 1 ); // VBR
	}
}

void ConfVidMpeg1MEncoderDialog::bitrateLineEditChanged(const QString &value)
{
	encSettings->mpeg1.bitrate = value.toInt();

	encSettings->mpeg1.minvbr = qMin( encSettings->mpeg1.minvbr, encSettings->mpeg1.bitrate);
	encSettings->mpeg1.maxvbr = qMax( encSettings->mpeg1.maxvbr, encSettings->mpeg1.bitrate);

	bitrateSlider->setValue( encSettings->mpeg1.bitrate );

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::bitrateSliderChanged(const int value)
{
	encSettings->mpeg1.bitrate = value;

	if ( encSettings->mpeg1.cbr )
	{
		encSettings->mpeg1.minvbr = encSettings->mpeg1.bitrate;
		encSettings->mpeg1.maxvbr = encSettings->mpeg1.bitrate;
	}
	else
	{
		encSettings->mpeg1.minvbr = qMin( encSettings->mpeg1.minvbr, encSettings->mpeg1.bitrate);
		encSettings->mpeg1.maxvbr = qMax( encSettings->mpeg1.maxvbr, encSettings->mpeg1.bitrate);
	}

	bitrateLineEdit->setText( QString::number(value) );

	vbr_nomSlider->setValue( value );

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::vbr_nomSliderChanged(const int value)
{
	vbr_nomLineEdit->setText( QString::number(value) );

	bitrateSlider->setValue( value );
}

void ConfVidMpeg1MEncoderDialog::vbr_nomLineEditChanged(const QString &value)
{
	bitrateSlider->setValue( value.toInt() );
}

void ConfVidMpeg1MEncoderDialog::standardVcdCheckBoxClicked()
{
	if ( standardVcdCheckBox->isChecked() ) 
	{
		encSettings->mpeg1.force_vcd = true;

		if ( tvPalRadioButton->isChecked() || (tvFreeRadioButton->isChecked() && videoSource->fps == 25.0) )
		{
			// TV standard: PAL
			encSettings->mpeg1.vcdPal();
		}
		else
		{
			// TV standard: NTSC
			encSettings->mpeg1.vcdNtsc( videoSource->fps );
		}

		frameGroupBox->setEnabled(false);
		bitrateGroupBox->setEnabled(false);
		br_allocGroupBox->setEnabled(false);
		formatGroupBox->setEnabled(false);
		letterBoxCheckBox->setEnabled(false);
		pack_sizeLineEdit->setEnabled(false);
		muxerDefaultPushButton->setEnabled(false);

#ifdef WIN32
#pragma message (ToBeDone "we should also set audio settings from here (a signal->slot) so is MPEG-1 layer II Audio - " __FUNCTION__)
#endif
	}
	else
	{
		encSettings->mpeg1.force_vcd = false;

		// For all but VCD
		frameGroupBox->setEnabled(true);
		bitrateGroupBox->setEnabled(true);
		br_allocGroupBox->setEnabled(true);
		formatGroupBox->setEnabled(true);
		letterBoxCheckBox->setEnabled(true);
		pack_sizeLineEdit->setEnabled(true);
		muxerDefaultPushButton->setEnabled(true);
	}

	populateFps();
	populateRez();
	updateControls();
}

void ConfVidMpeg1MEncoderDialog::closed_gopCheckBoxClicked()
{
	encSettings->mpeg1.closed_gop = ( closed_gopCheckBox->isChecked() );

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::letterBoxCheckBoxClicked()
{
	encSettings->mpeg1.letter_box = ( letterBoxCheckBox->isChecked() );

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::selectRez(const int index)
{
	// Do nothing if empty or reset
	if (index < 0)
	{
		return;
	}

	if (index == 1)
	{
		enterUserDefinedSize();
	}

	QSize size = rezComboBox->itemData(index).toSize();

	encSettings->mpeg1.resX = size.width();
	encSettings->mpeg1.resY = size.height();

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::selectFps(const int index)
{
	// Do nothing if empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mpeg1.mpeg_fps = fpsComboBox->itemData(index).toInt();

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::selectTv()
{
	// First do custom, the quick one
	if ( tvFreeRadioButton->isChecked() )
	{
		return;
	}

	// Then proceed for each preset
	if ( tvNtscRadioButton->isChecked() )
	{
		if ( standardVcdCheckBox->isChecked() )
		{
			encSettings->mpeg1.vcdNtsc( videoSource->fps );
		}
		else
		{
			encSettings->mpeg1.tvNtsc( videoSource->fps );
		}
	}
	else if ( tvPalRadioButton->isChecked() )
	{
		if ( standardVcdCheckBox->isChecked() )
		{
			encSettings->mpeg1.vcdPal();
		}
		else
		{
			encSettings->mpeg1.tvPal();
		}
	}
	else
	{
		// Should not happen, a bug to pop-up and fix
#ifdef WIN32
#pragma message (Reminder "a bug to report in log - " __FUNCTION__)
#endif
		encSettings->mpeg1.tvNtsc();
	}

	populateFps();
	populateRez();
	updateControls();
}

void ConfVidMpeg1MEncoderDialog::vbr_minLineEditChanged(const QString &str)
{
	int value = str.toInt();

	if ( value > encSettings->mpeg1.bitrate )
	{
		encSettings->mpeg1.minvbr = encSettings->mpeg1.bitrate;
	}
	else
	{
		encSettings->mpeg1.minvbr = value;
	}

	vbr_minSlider->setValue( encSettings->mpeg1.minvbr );
}

void ConfVidMpeg1MEncoderDialog::vbr_minSliderChanged(const int value)
{
	if ( value > encSettings->mpeg1.bitrate )
	{
		encSettings->mpeg1.minvbr = encSettings->mpeg1.bitrate;
	}
	else
	{
		encSettings->mpeg1.minvbr = value;
	}

	vbr_minLineEdit->setText( QString::number(encSettings->mpeg1.minvbr) );
}

void ConfVidMpeg1MEncoderDialog::vbr_minSliderReleased()
{
	if ( vbr_minSlider->value() > encSettings->mpeg1.bitrate )
	{
		encSettings->mpeg1.minvbr = encSettings->mpeg1.bitrate;
		vbr_minSlider->setValue( encSettings->mpeg1.minvbr );
	}
}

void ConfVidMpeg1MEncoderDialog::vbr_maxLineEditChanged(const QString &str)
{
	int value = str.toInt();

	if ( value < encSettings->mpeg1.bitrate )
	{
		encSettings->mpeg1.maxvbr = encSettings->mpeg1.bitrate;
	}
	else
	{
		encSettings->mpeg1.maxvbr = value;
	}

	vbr_maxSlider->setValue( encSettings->mpeg1.maxvbr );
}

void ConfVidMpeg1MEncoderDialog::vbr_maxSliderChanged(const int value)
{
	if ( vbr_maxSlider->value() < encSettings->mpeg1.bitrate )
	{
		encSettings->mpeg1.maxvbr = encSettings->mpeg1.bitrate;
	}
	else
	{
		encSettings->mpeg1.maxvbr = value;
	}

	vbr_maxLineEdit->setText( QString::number(encSettings->mpeg1.maxvbr) );
}

void ConfVidMpeg1MEncoderDialog::vbr_maxSliderReleased()
{
	if ( vbr_maxSlider->value() < encSettings->mpeg1.bitrate )
	{
		encSettings->mpeg1.maxvbr = encSettings->mpeg1.bitrate;
		vbr_maxSlider->setValue( encSettings->mpeg1.maxvbr );
	}
}

void ConfVidMpeg1MEncoderDialog::gop_sizeLineEditChanged(const QString &str)
{
	encSettings->mpeg1.gop_size = str.toInt();
}

void ConfVidMpeg1MEncoderDialog::selectBrAllocation(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	if ( br_allocComboBox->itemData( index ).toInt() ==  VBR )
	{
		encSettings->mpeg1.cbr = false;

		encSettings->mpeg1.minvbr = bitrateMin;
		vbr_minSlider->setValue( encSettings->mpeg1.minvbr );
		vbr_minLineEdit->setText( QString::number(encSettings->mpeg1.minvbr) );

		encSettings->mpeg1.maxvbr = bitrateMax;
		vbr_maxSlider->setValue( encSettings->mpeg1.maxvbr );
		vbr_maxLineEdit->setText( QString::number(encSettings->mpeg1.maxvbr) );

		vbr_minLabel->setEnabled(true);
		vbr_minSlider->setEnabled(true);
		vbr_minLineEdit->setEnabled(true);
		vbr_maxLabel->setEnabled(true);
		vbr_maxSlider->setEnabled(true);
		vbr_maxLineEdit->setEnabled(true);		
	}
	else
	{
		encSettings->mpeg1.cbr = true;
		encSettings->mpeg1.minvbr = encSettings->mpeg1.bitrate;
		encSettings->mpeg1.maxvbr = encSettings->mpeg1.bitrate;

		vbr_minSlider->setValue( encSettings->mpeg1.minvbr );
		vbr_minLineEdit->setText( QString::number(encSettings->mpeg1.minvbr) );
		vbr_maxSlider->setValue( encSettings->mpeg1.maxvbr );
		vbr_maxLineEdit->setText( QString::number(encSettings->mpeg1.maxvbr) );

		vbr_minLabel->setEnabled(false);
		vbr_minSlider->setEnabled(false);
		vbr_minLineEdit->setEnabled(false);
		vbr_maxLabel->setEnabled(false);
		vbr_maxSlider->setEnabled(false);
		vbr_maxLineEdit->setEnabled(false);
	}
}

void ConfVidMpeg1MEncoderDialog::muxerDefaultPushButtonClicked()
{
	encSettings->mpeg1.pack_size = 0;		
	encSettings->mpeg1.muxrate = 0;
	encSettings->mpeg1.interleaving2 = false;
	encSettings->mpeg1.vdelay = 0;
	encSettings->mpeg1.adelay = 0;
	encSettings->mpeg1.drop = false;
	encSettings->mpeg1.vbuf_size = 40;
	encSettings->mpeg1.abuf_size = 8;

	updateControls();
}

void ConfVidMpeg1MEncoderDialog::selectFormat()
{
	encSettings->mpeg1.stream = ( formatMpeg1RadioButton->isChecked() )? 
								"mpeg1" : "xvcd";
	updateControls();
}

void ConfVidMpeg1MEncoderDialog::setToolTip()
{
#ifdef WIN32
	tvGroupBox->setToolTip( tr("Preset a TV standard, see WhatsThis...") );
#else
	tvGroupBox->setToolTip( tr("Preset a TV standard.") );
#endif

	bitrateGroupBox->setToolTip( boxToolTip("vbitrate", tr("None"), QString("<%1-%2>").arg( bitrateMin ).arg( bitrateMax ),
						tr("Sets the video bitrate in kbit/s for MPEG-1/2 video.")) );
	
	vbr_minLabel->setToolTip( boxToolTip("minvbr", tr("None"), QString("<%1-br>").arg( bitrateMin ),
						tr("VBR minimum bitrate allowed in kbit/s.")) );
	vbr_minSlider->setToolTip( vbr_minLabel->toolTip() );
	vbr_minLineEdit->setToolTip( vbr_minLabel->toolTip() );

	vbr_maxLabel->setToolTip( boxToolTip("maxvbr", tr("None"), QString("<br-%1>").arg( bitrateMax ),
						tr("VBR maximum bitrate allowed in kbit/s.")) );
	vbr_maxSlider->setToolTip( vbr_maxLabel->toolTip() );
	vbr_maxLineEdit->setToolTip( vbr_maxLabel->toolTip() );

	rezComboBox->setToolTip( boxToolTip("vwidth x vheight", "Input Picture Size", "<1-4095> x <1-4095>",
						tr("Set the video width and height when video is MPEG-1/2.")) );
	
	fpsComboBox->setToolTip( boxToolTip("vframerate", tr("None"), "<24000/1001 | 24 | 25 | 30000/1001 | 30 | 50 | 60000/1001 | 60>",
						tr("Sets the framerate for MPEG-1/2 video. This option will be ignored if used with the telecine option.")) );

	formatGroupBox->setToolTip( boxToolTip("format", "xvcd", "<mpeg1 | xvcd>",
						tr("Stream format.")) );

	standardVcdCheckBox->setToolTip( tr("Force VideoCD standard settings") );
	letterBoxCheckBox->setToolTip( tr("Scale and crop picture to output compliant 4:3 video") );

#ifdef WIN32
	gopGroupBox->setToolTip( tr("GOP settings, see WhatsThis...") );
#else
	gopGroupBox->setToolTip( tr("GOP settings.") );
#endif

	gop_sizeLineEdit->setToolTip( editToolTip("keyint", "<up to 300>") );
	gop_sizeLabel->setToolTip( labelToolTip( tr("15 for NTSC, 12 for PAL"), 
						tr("Ususal values are 15 for NTSC and 12 for PAL. The maximum values are 18 for NTSC and 15 for PAL "
"but you can set up to 300 while 30-40 frames per GOP are already crazy sizes (used by KVCD).")) );

	closed_gopCheckBox->setToolTip( tr("Close all GOPs") );

	pack_sizeLineEdit->setToolTip( editToolTip("size", "<up to 65535>") );
	pack_sizeLabel->setToolTip( labelToolTip(QString::number(2048), 
						tr("Pack size in bytes, do not change unless you know exactly what you are doing. "
"Default is 2048, VCD standard require 2324.")) );

	muxrateLineEdit->setToolTip( editToolTip("muxrate") );
	muxrateLabel->setToolTip( labelToolTip( tr("0 for not set"),
						tr("Nominal muxrate in kbit/s used in the pack headers. Default \"not set\" let the encoder "
"choose the appropriate rate to avoid buffer underflows.")) );

	vdelayLineEdit->setToolTip( editToolTip("vdelay", "<0-32760>") );
	vdelayLabel->setToolTip( labelToolTip(QString::number(0),
						tr("Initial video delay time, in milliseconds, use it if you want to delay video with respect to audio. It doesn't work with :drop.")) );

	adelayLineEdit->setToolTip( editToolTip("adelay", "<0-32760>") );
	adelayLabel->setToolTip( labelToolTip(QString::number(0),
						tr("Initial audio delay time, in milliseconds, use it if you want to delay audio with respect to video.")) );

	vbuf_sizeLineEdit->setToolTip( editToolTip("vbuf_size", "<40-1194>") );
	vbuf_sizeLabel->setToolTip( labelToolTip(tr("Not Set"),
						tr("Sets the size of the video decoder's buffer, expressed in kilo-bytes. Specify it only if the bitrate of the video stream is "
"too high for the chosen format and if you know perfectly well what you are doing. A too high value may lead to an unplayable "
"movie, depending on the player's capabilities.")) );

	abuf_sizeLineEdit->setToolTip( editToolTip("abuf_size", "<4-64>") );
	abuf_sizeLabel->setToolTip( labelToolTip(tr("Not Set"),
						tr("Sets the size of the audio decoder's buffer, expressed in kilo-bytes. The same principle as for vbuf_size applies.")) );

	interleaving2CheckBox->setToolTip( boxToolTip("interleaving2", tr("Not Set"), "",
						tr("Uses a better algorithm to interleave audio and video packets, "
"based on the principle that the muxer will always try to fill the stream with the largest percentage of free space.")) );

	dropCheckBox->setToolTip( boxToolTip("drop", tr("Not Set"), "",
						tr("When used with vdelay the muxer drops the part of audio that was anticipated.")) );
}

void ConfVidMpeg1MEncoderDialog::setWhatsThis()
{
#ifdef WIN32
#pragma message (Reminder "why not using QTextDocument with insertHtml(), insertList() and insertTable() - " __FUNCTION__)
#endif

	gopGroupBox->setWhatsThis( tr("I-frames store complete frame info within the frame and is therefore suited for random access. "
"P-frames provide compression using motion vectors relative to the previous frame ( I or P ). B-frames provide maximum compression "
"but requires the previous as well as next frame for computation. Therefore, processing of B-frames require more buffer on the "
"decoded side. A configuration of the Group of Pictures (GOP) should be selected based on these factors. I-frame only sequences "
"gives least compression, but is useful for random access, FF/FR and editability. I and P frame sequences give moderate compression "
"but and a certain degree of random access, FF/FR functionality. I,P & B frame sequences give very high compression but also increases "
"the coding/decoding delay significantly.") );

	tvGroupBox->setWhatsThis( tr("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html>\n"
"<head>\n"
"<meta name=\"qrichtext\" content=\"1\" />\n"
"<style type=\"text/css\">\n"
"p, li {\n"
"	white-space: pre-wrap;\n"
"}\n"
".centercell {\n"
"	text-align: center;\n"
"}\n"
".mybold {\n"
"	font-weight: 600;\n"
"}\n"
"</style>\n"
"</head>\n"
"<body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p>Output movie<span class=\"mybold\"> TV standard</span>.</p>\n"
"<p>\n"
"<table border=\"0.25\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" cellspacing=\"0\" cellpadding=\"0\">\n"
" <tr>\n"
"  <td><span class=\"mybold\">NTSC</span> North America </td>\n"
"  <td class=\"centercell\">Canada, Mexico, United States </td>\n"
" </tr>\n"
" <tr>\n"
"  <td><span class=\"mybold\">NTSC</span> Central America & Caribbean</td>\n"
"  <td class=\"centercell\">Antigua and Barbuda, Aruba, Bahamas, Barbados, Belize, Bermuda, British Virgin Islands, Cayman Islands, Costa Rica, Cuba, "
" Dominica, Dominican Republic, El Salvador, Guatemala, Grenada, Haiti, Honduras, Jamaica, Leeward Islands, Montserrat, Netherlands Antilles, Nicaragua, "
" Panama, Puerto Rico (U.S.), Saint Kitts and Nevis, Saint Lucia, Saint Vincent and the Grenadines, Trinidad and Tobago, U.S. Virgin Islands </td>\n"
" </tr>\n"
" <tr>\n"
"  <td><span class=\"mybold\">NTSC</span> South America </td>\n"
"  <td class=\"centercell\">Bolivia, Colombia, Ecuador, Guyana, Chile, Peru, Suriname, Venezuela </td>\n"
" </tr>\n"
" <tr>\n"
"  <td><span class=\"mybold\">NTSC</span> Asia </td>\n"
"  <td class=\"centercell\">Japan, South Korea, Republic of China (Taiwan)T, Union of Myanmar (Burma), Philippines </td>\n"
" </tr>\n"
" <tr>\n"
"  <td><span class=\"mybold\">PAL</span> (rest of the world)</td>\n"
"  <td class=\"centercell\">Afghanistan, Albania, Angola, Ascension, Australia, Austria, Bahrain, Bangladesh, Bosnia and Herzegovina, Botswana, Brunei, Bulgaria,"
" Cameroon, Cape Verde, China, Chile, Christmas Island, Cook Islands, Croatia, Cyprus, Czech Republic, East Timor, Egypt, Eritrea, Ethiopia, Falkland Islands, Fiji, France,"
" Gambia, Georgia, Germany, Ghana, Gibraltar, Greece, Guinea-Bissau, Guinea, Hong Kong, Hungary, Iceland, India, Indonesia, Iran, Iraq, Ireland, Israel, Italy,"
" Jordan, Kenya, Kuwait, Laos, Lebanon, Lesotho, Liberia, Liechtenstein, Lithuania, Macau, Macedonia, Malawi, Malaysia, Maldives, Malta, Montenegro, Morocco,"
" Mozambique, Myanmar, Namibia, Nepal, Netherlands, New Zealand, Nigeria, Norfolk Island, North Korea, Oman, Pakistan, Palestine, Papua New Guinea, Poland, Portugal,"
" Qatar, Romania, Samoa, Saudi Arabia, Serbia, Seychelles, Sierra Leone, Singapore, Slovakia, Slovenia, Solomon Islands, Somalia, South Africa, Sri Lanka,"
" Sudan, Swaziland, Syria, Tanzania, Thailand, Tonga, Tristan da Cunha, Tunisia, Turkey, Uganda, Ukraine, United Arab Emirates, United Kingdom,"
" Vanuatu, Vatican City, Vietnam, Yemen, Zambia, Zanzibar, Zimbabwe </td>\n"
" </tr>\n"
"</table>\n"
"</p>\n"
"<p>NTSC and PAL will be replaced world-wide with DVB-T (Terrestrial Digital Video Broadcast) in the coming years. </p>\n"
"</body>\n"
"</html>") );

	rezComboBox->setWhatsThis( tr("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html>\n"
"<head>\n"
"<meta name=\"qrichtext\" content=\"1\" />\n"
"<style type=\"text/css\">\n"
"p, li {\n"
"	white-space: pre-wrap;\n"
"}\n"
".centercell {\n"
"	text-align: center;\n"
"}\n"
".mybold {\n"
"	font-weight: 600;\n"
"}\n"
"</style>\n"
"</head>\n"
"<body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p>Output movie<span class=\"mybold\"> picture size</span>. Recommended resolutions:</p>\n"
"<p>\n"
"<table border=\"0.25\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" cellspacing=\"0\" cellpadding=\"0\">\n"
" <tr class=\"mybold\">\n"
"  <td>Display </td>\n"
"  <td class=\"centercell\"> NTSC </td>\n"
"  <td class=\"centercell\"> PAL </td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Handheld </td>\n"
"  <td class=\"centercell\">192x144 </td>\n"
"  <td class=\"centercell\">192x144 </td>\n"
" </tr>\n"
" <tr>\n"
""
"  <td>Portable </td>\n"
"  <td class=\"centercell\">352x240 </td>\n"
"  <td class=\"centercell\">352x288 </td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Large Handheld </td>\n"
"  <td class=\"centercell\">384x288 </td>\n"
"  <td class=\"centercell\">384x288 </td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Notebook / Desktop </td>\n"
"  <td class=\"centercell\">640x360 </td>\n"
"  <td class=\"centercell\">640x360 </td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Home Theater </td>\n"
"  <td class=\"centercell\">720x480 </td>\n"
"  <td class=\"centercell\">720x576 </td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Maximum MPEG-1 Constrained </td>\n"
"  <td class=\"centercell\">768x480 </td>\n"
"  <td class=\"centercell\">768x576 </td>\n"
" </tr>\n"
"</table>\n"
"</p>\n"
"<p>Recommended picture height are 480 or 240 for NTSC movies (which are 480-line video sources), and 576 or 288 for PAL movies (which are 576-line video sources). </p>\n"
"</body>\n"
"</html>") );

	fpsComboBox->setWhatsThis( tr("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html>\n"
"<head>\n"
"<meta name=\"qrichtext\" content=\"1\" />\n"
"<style type=\"text/css\">\n"
"p, li {\n"
"	white-space: pre-wrap;\n"
"}\n"
".centercell {\n"
"	text-align: center;\n"
"}\n"
".mybold {\n"
"	font-weight: 600;\n"
"}\n"
"</style>\n"
"</head>\n"
"<body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p>Output movie<span class=\"mybold\"> frame rate</span>. Recommended frame rate (fps, images/second):</p>\n"
"<p>\n"
"<table border=\"0.25\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" cellspacing=\"0\" cellpadding=\"0\">\n"
" <tr class=\"mybold\">\n"
"  <td>Display </td>\n"
"  <td class=\"centercell\"> NTSC </td>\n"
"  <td class=\"centercell\"> PAL </td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Handheld </td>\n"
"  <td class=\"centercell\">15 FPS</td>\n"
"  <td class=\"centercell\">15 FPS</td>\n"
" </tr>\n"
""
" <tr>\n"
"  <td>Portable </td>\n"
"  <td class=\"centercell\">15 FPS</td>\n"
"  <td class=\"centercell\">15 FPS</td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Large Handheld </td>\n"
"  <td class=\"centercell\">23.976-29.97 FPS</td>\n"
"  <td class=\"centercell\">25 FPS</td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Notebook / Desktop </td>\n"
"  <td class=\"centercell\">23.976-29.97 FPS</td>\n"
"  <td class=\"centercell\">25 FPS</td>\n"
" </tr>\n"
" <tr>\n"
"  <td>Home Theater </td>\n"
"  <td class=\"centercell\">23.976-29.97 FPS</td>\n"
"  <td class=\"centercell\">25 FPS</td>\n"
" </tr>\n"
" <tr>\n"
"  <td>HDTV </td>\n"
"  <td class=\"centercell\">23.976-29.97 FPS</td>\n"
"  <td class=\"centercell\">25 FPS</td>\n"
" </tr>\n"
"</table>\n"
"</body>\n"
"</html>") );
}

QString ConfVidMpeg1MEncoderDialog::labelToolTip( const QString &defaults, const QString &details )
{
	QString strDefault( tr("Default: ") ); 

	return tr(	"<p><b>%1</b> %2</p>"
				"<p></p>"
				"<p>%3</p>" )
				.arg(Qt::escape(strDefault)).arg(Qt::escape(defaults))
				.arg(Qt::escape(details));
}

QString ConfVidMpeg1MEncoderDialog::editToolTip( const QString &name, const QString &range )
{
	return tr("<b>%1</b> %2").arg( Qt::escape(name) ).arg( Qt::escape(range) );
}

QString ConfVidMpeg1MEncoderDialog::boxToolTip( const QString &name, const QString &defaults, const QString &range, const QString &details )
{
	QString strDefault( tr("Default: ") ); 

	return tr(	"<p><b>%1</b> %2</p>"
				"<p></p>"
				"<p><b>%3</b> %4</p>"
				"<p></p>"
				"<p>%5</p>" )
				.arg(Qt::escape(name)).arg(Qt::escape(range))
				.arg(Qt::escape(strDefault)).arg(Qt::escape(defaults))
				.arg(Qt::escape(details));
}
