Sunday, October 5, 2008

Creating VCD from MOD files using GNU/Linux

I have the JVC Everio Hybrid camcorder (GZ-MG730). When a video is recorded using this camcorder, the video files are stored in the MOD file format (files with .mod extension). Here is some information to help you create a VCD using a bunch of MOD files. This worked for me on the Ubuntu 8.04 Hardy Heron (a GNU/Linux distribution) release.

Step 1: At many places, it is mentioned that we can simply rename the MOD files to MPG files. The MOD files are nothing by MPEG2 files. For example, VideoLAN doesn't work with MOD files, but when the extension of the file is changed from .mod to .mpg then it works. But when I did this renaming, the vcdxbuild software core dumped (at a later step). So this is what I did.

$ ffmpeg -i sample.mod -target svcd -sameq sample.mpg

Do this for all the MOD files that you have collected. At the end of this step you would have a bunch of MPG files.

Step 2: Generate an XML file that will describe the contents of the VCD. This can be done with the help of vcdxgen tool (which is part of VCDImager).

$ vcdxgen *.mpg

This creates a videocd.xml file. You can edit this file using your favourite editor to set the title and other meta information on the VCD. The order of the MPG files in the VCD can also be changed by modifying this XML file.

Step 3: Create a VCD image using the vcdxbuild tool (part of VCDImager). The input to this tool is the XML descriptor file generated in the previous step.

$ vcdxbuild videocd.xml

This is where it fails if the MOD extension is simply replaced into MPG extension. I don't know why it fails, but this is the reason I am suggesting to make use of ffmpeg and do the conversion. This command will generate two files videocd.cue and videocd.bin. The file videocd.bin is the binary file that contains the VCD image.

Step 4: Write the binary image from the previous step to the an empy CD. The tool cdrdao can be used for this purpose.

$ cdrdao scanbus # get device number of CD writer
$ cdrdao write --device 1,0,0 videocd.cue

Here is the full summary of the commands that has been used.
  1. ffmpeg -i sample.mod -target svcd -sameq sample.mpg
  2. vcdxgen *.mpg
  3. vcdxbuild videocd.xml
  4. cdrdao scanbus
  5. cdrdao write --device 1,0,0 videocd.cue
Enjoy.

Friday, October 3, 2008

Thread Local Storage

In multi-threaded programs, there is a need for global variables that are local to threads (called the thread local storage or thread specific storage). When using POSIX threads, a set of APIs are provided to access thread local (or thread specific) storage. The relevant APIs are pthread_key_create(), pthread_getspecific(), pthread_setspecific() and pthread_key_delete().

In my opinion this is cumbersome to use. There is a simpler method provided by the Solaris compiler to achieve the same result, the __thread keyword (Section 12.7.2 "Thread Local Storage" of the book "Solaris Application Programming" by Darryl Gove). Just by adding the keyword __thread to the declaration of a global variable, we get a thread local storage. This approach seems to be so much simpler and very easy to use. Here is an online reference.

Here is a C++ standard proposal for introducing a new keyword (thread_local) for this purpose.

Sunday, August 17, 2008

Programming Paradigms in C++

Here is a short article titled, "C++ Programming Styles and Libraries", by Bjarne Stroustrup, the creator of C++ programming language. In this short article, the author discusses (very briefly) about the various programming paradigms supported by C++. Like in many previous occasions, Bjarne Stroustrup stresses that C++ is a multiparadigm programming language and that there is no "one true method" of programming.

The main programming methods supported by C++ are the procedural (he calls it C++ as a better C), object-based programming (he doesn't use this exact wording but mentions about encapsulation and abstraction), object-oriented programming and generic programming (C++ templates).

Also, he stresses about the use (and development) of C++ libraries for various domains. He believes that availability of many such C++ libraries will improve the value of C++ programming language itself. As examples of good libraries he mentions, among others, the Adaptive Communication Environment (ACE) library and Boost library.

A small article to point us, C++ programmers, in the right direction.

Saturday, June 14, 2008

FSF New Online Store

Finally, the Free Software Foundation has updated their online store with a new look and definitely a new underlying software. This is a good thing. For a long time now I was worried that they weren't doing much about their online store. The next thing that they should be doing is to add more variety to their GNU Gear. They can take some hints from Canonical online store.

Thursday, October 11, 2007

BOSS GNU/Linux Distribution

My friend, Peri, pointed out the BOSS GNU/Linux distribution to me. When I visited the link I was glad to see that the distribution is developed by Center for Development of Advanced Computing (C-DAC), which comes under the Department of Information Technology. So this can be considered as an official distribution of the Government of India!

Two days back I downloaded the live CD of this distribution. It booted up without any problems. And the default desktop was GNOME. This is what they say in their first page,
Made specifically for the Indian environment , it consists of a pleasing Desktop environment coupled with Indian language support and other packages that are most relevant for use in the government domain.
After reading this, my expectation was that, it would be easy to change the language settings of the desktop. I wanted to see the desktop in Hindi and Tamil languages. But even after one hour of trying I couldn't figure it out. Obviously I am not looking at the right places. Anyway, I will be trying this distribution many more times in the future and provide feedback so that they can improve it further.

If you haven't tried it, do give it a try.

Monday, October 8, 2007

Programming Question 3: Perfect Numbers

An integer n is called perfect provided it equals the sum of all its divisors that are both positive and less than n. For example, 28 is perfect because the positive divisors of 28 are 1, 2, 4, 7, 14 and 28. Note that 1 + 2 + 4 + 7 + 14 = 28.
  1. There is a perfect number smaller than 28. Find it.
  2. Write a computer program to find the next perfect number after 28.
This question is from the book, "Mathematics: A Discrete Introduction" by Edward R Scheinerman.

Friday, October 5, 2007

Outline of the Flex Generated Scanner Routine yylex()

Recently I was implementing a parser for the Network Access Identifier (NAI) using flex and yacc (that comes default with an Red Hat Enterprise Linux installation). While working on these tools, I realized how important it was to understand the code that is generated, to appreciate the functioning of these tools. It might not be essential to know every detail of the generated code (but that is a good exercise) to make effective use of these tools, but knowing the big picture will help you to grasp the essence of the tools.

To understand how flex works, it will be enlightening to see the file lex.yy.c that it generates. We all know that the output of the lexical analyzer (flex) is the C function yylex(). While the actual generated yylex() routine is complex, its outline is provided below.
 1 int yylex(void)
2
{
3
YY_USER_INIT;
4 while
(1) {
5
read_input_and_match_pattern();
6 int
yy_act = find_correct_action();
7 switch
( yy_act ) {
8 case
1:
9
YY_RULE_SETUP;
10
/* user-defined action */
11
YY_BREAK;
12 break
;
13
/* more-user-defined-actions */
14 case
82:
15
YY_RULE_SETUP;
16
/* pre-defined action */
17
YY_BREAK;
18 break
;
19
/* more-pre-defined-actions */
20 default
:
21
/* error: no action */
22
}
23
}
24
}
Here are some useful information to help in understanding the big picture.
  • The macros YY_USER_INIT, YY_RULE_SETUP, YY_BREAK can be defined by the user. They are the hooks provided by the tool, so that you can have a little bit of customization for the generated yylex() function. Under normal circumstances, you will be pointing them to your own functions.
  • I hope you are aware of the flex rules that you specify. The rules have patterns in the left hand side and user-defined actions in the right hand side. These user defined actions would be part of the switch-case statement. This information would be very useful for you to understand where the C-code that you write in the flex input file goes.
  • Apart from the user-defined actions, there are also pre-defined actions. The predefined action for any character is to print it to the standard output. Similarly, there are predefined actions for the end-of-file.
  • If you look at the above yylex() function, you will notice that it will not return unless the user-defined action uses the return statement. When you have to make flex and yacc work together, then you have to return from the lex generated scanner after every token.
The functions read_input_and_match_pattern() and find_correct_action() used in the above code snippet are just placeholder functions (pseudo-code) to show the functionality provided by the relevant portion of the yylex() routine. Let that not confuse you. Remember that the point of this post is to know the place of the user-defined action in the yylex() routine.