Constant casting

Constant casting is when you are for example calling a external library function that does not understand the const (constant) type and so you will need to take it off because the program will do something funny otherwise, crash. etc..

Here is a example code of a const_cast

#include <iostream>
 
using namespace std;
 
int exampleextenalfuction(int value)
{
	// you can alter the value if you want to, since it is not longer a constant value
	return value  + value;
}
 
int main()
{
	const int constvalue = 2;
 
	cout << constvalue << endl;
 
	cout << "Calling a external function that does not use constant types" << endl;
 
	cout << exampleextenalfuction(const_cast<int&>(constvalue)) << endl;
}

Static casting

Static casting is when you want to convert one type into another, static casting is used allot of the time without even knowing. For example

int value = 2;
float t = float(value);

the float(value) is a static casting of converting one value into another. Below is a full code example

#include <iostream>
 
using namespace std;
int main()
{
	int i = 3;
	// the float(<value)> is basically a static cast from a interger value into a float
	cout << float(i) << endl;
	cout << static_cast<float>(i) << endl;
}

It is very small since static_cast’ing is very basic in nature, but the only problem is that you have to check to make sure that there is a error e.g not NULL.

Execution order

Execution order of programs code is very much a vital thing to understand and where some fault finds will take a long while to figure out. Basically like in maths where there is a order of calculating, well in coding structures and also multitasking and multi-threading setups the execution order may be incorrect for lines of code.

Here is some examples, the first is when will a function be called and the later when post/pre incrementation will take place.

#include <iostream>
 
using namespace std;
 
int value =1;
 
int setvalue2()
{
	cout << "setting value"<<endl;
	value = 2;
	return value;
}
 
int returnvalue()
{
	cout << "renting value"<< endl;
	return value;	
}
 
int main()
{
	// depending on the order of execution the value may be
	/* setvalue2 called first
		(setvalue2 = 2 / returnvalue = 2) = 1
	   returnvalue called first
		(setvalue2 = 2 / returnvalue = 1) = 2
	*/
	cout << setvalue2() / returnvalue() << endl;
 
	int i;
	i = i++ - ++i;	// not sure what i will be because the pre/post increaments 
	i = 3, i++, i++; // i will equal 5 because in correct order.
}

Dynamic Casting

Dynamic casting, means that you can convert one object into another that is off the same type. For example, if you had a base class called Shape and a inherited class called Rectangle then you are able to convert a Shape object into a Rectangle.

Rectangle *rec = dynamic_cast<Shape *>(shapeobject);

sort of thing, there has to be a virtual function within the base class otherwise the compiler will complain, but apart from that that is about it.

Dynamic casting allows for NULL returns which is the best thing, because you can test to see if the casting actually worked and not to do anything silly on a NULL object which would crash the program.

Pointer casting uses the sytnax

<type> *p_subclass = dynamic_cast<<type> *>( p_obj );

Reference will not throw an error/expcetion so will need to check std::bad_cast< typeinfo header >, here is the syntax

<type> subclass = dynamic_cast<<type> &>( ref_obj );

Hopefully this will make more sense for how and why it works.

#include <iostream>
 
using namespace std;
 
class classA 
{
	public	:
		int x;		// should be private
		char y;		// should be private
 
		classA();
 
 		virtual ~classA() {}; 	// need to have a virtual function for dynamic casting
};
 
// basic classA constructor
classA::classA()
{ 
	cout << "classA" << endl; 
	x = 1; 
	y = 'a';
}
 
 
class classB : public classA
{	
	public :
		int b;		// should be private
 
		classB();
 
		~classB() {};	// complete the virtual
};
 
// basic classB constructor
classB::classB()
{
	cout << "classB" << endl;
	x = 2;
	y = 'b';
}
 
int main()
{
	classA newa;	// classA obj
	cout << "class A constructed" << endl;
	classB newb;	// classB obj
	cout << "class B constructed" << endl;
 
	cout << "NewA X " << newa.x << endl;
	cout << "NewB X " << newb.x << endl;
 
	// point a classB to a already created classB object
	classB *normalB = &newb;
	// dynamic_cast a normalB object (newb) to another classA object
	classA *dynA = dynamic_cast<classB *>(normalB);	
	cout << "dynamic A X " << dynA->x << endl;
 
	// does not work, because you cannot convert classA into a classB, but if classA was pointing to a classB type.
	classA *normalA = &newa;
	classB *dynB = dynamic_cast<classB *>(normalA);
	if (dynB)	// above produces a 0 because invalid.
	{
		cout << "dynamic B X " << dynB->x << endl;
		cout << "dynamic B X " << dynB->b << endl;
	}
 
	// this does work because it is converting from a pointer of classB type, which was "sitting" in a classA container
	// and then is converted back to a classB
	classA *normalA2 = &newb;
	classB *dynB2 = dynamic_cast<classB *>(normalA2);
	if (dynB2)	// above produces a 0 because invalid.
	{
		cout << "dynamic A2X " << normalA2->x << endl;	// output is 2 because it was a classB constructed
		cout << "dynamic B X " << dynB2->x << endl;
		cout << "dynamic B X " << dynB2->b << endl;
	}
 
}

How to convert VOB to DVD and Burn

Convert VOB-DVD Image

To create the IFO/BUP files for the VBO files I use a program (the IFO/BUP files are the same file but with different names)

  dvdauthor -o <path-to-file> -T - To generate the Table Of Contents file (TOC)
  cp *.vob <path-to-file>/VIDEO_TS - Copy the files into the video directory
  dvdauthor -o <path-to-file> vobs 

To generate multiple BUP files from the IFO files you could use something like

for file_i in *.IFO; do cp $file_i `basename $file_i .IFO`.BUP; done 

Burn to disk the vcd Image
To burn the dvd files I use a program called K3B for ease of use (K3B is very self explanatory).

How to convert AVI-MPEG to VCD

This shall serve as a reminder for myself and may help others. There is different versions of audio conversion, CBR (Constant Bit Rate) and VBR (Variable Bit Rate), it seems that many DVD players perfer the CBR. Below is both versions with links to the relevant programs and example command lines.

CBR AVI-MPEG

To convert the avi file into a mpeg file for a PAL 352×288 screen, there is different settings either use the ‘man’ or goto the Transcode website for viewing the conversion in more detail. The command line that I use is

transcode -i<AVI FILM> -V -y mpeg -F v,1 -E 44100 -b 224 -fps 25 -o <OUTPUTFILENAME>

This generates two files, *.m1v (the video segment) and *.mpa (the audio segment).

The options in the above command line are

  • -b Audio bitrate Kbit’s
  • -E Audio sampling rate (Hz)
  • -F codec
  • -i input file
  • -V use internal video codec (faster but some import modules do not support this)
  • -y video, audio export modules.
  • -fps Frames per second, 25 is for PAL, 29.97 for NTSC

Joining the OUTPUTFILENAME’s together

To join these two segments together

tcmplex -o <OUTPUT.MPG> -i <OUTPUTFILENAME.M1V> -p <OUTPUTFILENAME.MPA> -m v

This will create a MPEG file ready to be either watched or converted again into the VCD format and then burned.

VBR AVI-MPEG

The programs to download/use are obtained from these links

If the AVI file is in MS MPeg4 v3(DIVX3)

If the INPUT file is in a MS MPeg4 v3 (DIVX3) then you have to convert the file into a DIVX format for FFMPeg to do its stuff. To convert to the required file you will need the mencoder program from MPlayer website. To convert to the required file structure use the commands below.

mencoder -ovc lavc -lavcopts vcodec=mpeg4:vhq -oac copy INPUTFILE.AVI -o OUTPUT.AVI

The options produce a mpeg4 version of the input file using the lavc video converter. Also with the option ‘vhq’, it produces a very high quality output file.

Convert AVI-MPEG

To convert AVI to MPEG use the ffmpeg program, with the commands as below. The INPUT.FILE is in AVI format and the OUTPUT.FILE is an MPEG format

ffmpeg -vcd -i INPUT.FILE -b 1150 -s 352x288 -vcodec mpeg1video -f mpegvideo OUTPUT.FILE

With advice, the ffmpeg build version 4753 (form cvs) the parameters are:-

ffmpeg -target vcd -i INPUT.FILE -b 1150 -s 352x288 -vcodec mpeg1video -f mpegvideo OUTPUT.FIL

The options enable an VCD Mpeg type file output(-vcd) with a bitrate of 1150 (-b) and an output screen size of 352×288 (-s). The codecs to convert to are mpeg1video (-vcodec) so that it is able to be converted to an vcd image with the vcdimager program.
Note: This is still a MPEG file and not a vcd file, you have to use vcdimager below to create a vcd file to burn to disk.

PAL Divx 3 avi file to NTSC vcd

Thanks to Marc for this

ffmpeg -i FILE.AVI -b 1150 -r 29.97 -s 352x240 -aspect 16:9 -vcodec mpeg1video -f mpegvideo FILE-NTSC.mpeg

If MPEG file is bigger than 700MB

If the MPeg file is over the maximum size of the cd, then I use the program mencoder from MPlayer. With the options as below

mencoder -oac copy -ovc copy INPUT.FILE -o OUTPUT.FILE -endpos 670mb

At the end of the copy it should say the frame that the mencoder finished on for the first file. Place that value in the line below (in the VALUE) to create the second value.
I usally subtract about 10 frames just to make sure.

mencoder -oac copy -ovc copy INPUT.FILE -o OUTPUT2.FILE -ss VALUE

Both of the files, INPUT & OUTPUT are mpeg files

This will create a MPEG file ready to be either watched or converted again into the VCD format and then burned.

Hope that helps, and this also servers as a reminder to me 🙂

How to convert AVI-MPEG to DVD

This shall serve as a reminder for myself and may help others. The software that I have used is

To convert the avi file into a mpeg file for a PAL 352×288 screen, there is different settings either use the ‘man’ or goto the Transcode website for viewing the conversion in more detail. The command line that I use is

  transcode -i<AVI FILM> -V -y mpeg -F d -E 44100 -b 224 -fps 25 -o <OUTPUTFILENAME>

This generates two files, *.m2v (the dvd video segment) and *.mpa (the audio segment).

The options in the above command line are

  • -b Audio bitrate Kbit’s
  • -E Audio sampling rate (Hz)
  • -F codec (d = dvd)
  • -i input file
  • -V use internal video codec (faster but some import modules do not support this)
  • -y video, audio export modules.
  • -fps Frames per second, 25 is for PAL, 29.97 for NTSC

If the audio is in the AC3 Codec then you may need to reduce the bitrate for encoding. This is done with a profile file dvd.prof, which containts the following entry.

   max_bitrate=4000000.0

this will reduce the bitrate to 4000 kpbs. The profile is passed to the transcode program within the 3rd parameter to -F, also the parameter -A means to use the internal AC3 codec.

  transcode -i<AVI FILM> -V -y mpeg -F d,,dvd.prof -m <OUTPUTFILENAME.AC3> -A 0x2000 -fps 25 -o <OUTPUTFILENAME>

Joining the OUTPUTFILENAME’s together

To join these two segments together

  tcmplex -o <OUTPUT.VOB> -i <OUTPUTFILENAME.M2V> -p <OUTPUTFILENAME.MPA> -m d

If using the AC3 codec then

  tcmplex -o <OUTPUT.VOB> -i <OUTPUTFILENAME.M2V> -p <OUTPUTFILENAME.AC3> -m d

This will create a MPEG file ready to be either watched or converted again into the DVD format and then burned.

Hope that helps, and this also servers as a reminder to me 🙂