Pages

Showing posts with label OpenSource. Show all posts
Showing posts with label OpenSource. Show all posts

Monday, October 25, 2010

FPDF

This article explains how you can create pdf documents in PHP. While I searched google for 'php + pdf', I found so many libraries that one can use. The one that looked simple to use was FPDF.

All you need to do is copy the php files that come along with the FPDF download and paste them in your php 'include' folder and refer to the package in your code.

This is a simple example.

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

As shown in the example above, it is relatively simple to use the library. You can find the tutorials here.

You can create some complex pdfs such as the following,

A calender : Soruce Code Demo
A tree : Source Code Demo
Javascript support : Source Code Demo

You can find more such samples here. I hope you guys find it useful.

Sunday, October 10, 2010

Installing Ubuntu using Wubi

I have published this article today (10.10.10) to coincide with the release of 'Maverick Meerkat'

What do we normally do with PCs or laptops at home?

  • View Photos
  • Watch Movies
  • Listen to Music
  • Browse the Internet
  • Play Games
And in very rare cases, work with MS Office or do some random programming

The fact is, we dont need Windows Vista or Windows 7 to do the things mentioned above. Windows Vista/7 takes up a lot of RAM and is damn slow in starting up. And we invariably end of using a pirated version of windows which is always prone to virus attacks. For so many years I wanted to work with an linux operating system, but i was always scared of the installation process. I was scared that I might accidentally format one of my hard disks.

About an year ago, I got to know about Wubi from a colleague of mine. Wubi is nothing but an easy way to install Ubuntu on your personal computer. If you want to try out Ubuntu on your PC for a short period of time, Wubi is your best choice. Wubi installs Ubuntu on your PC as just another application on windows, but actually it installs an OS itself in your PC. That is how easy the whole installation is. Shown below is the snapshot of the installation screen.



It does look that simple, it really does. Choose any drive in your PC where you have sufficient amount of free space. I would suggest to choose 10GB as the installation size. Once you click on Install, it takes about 3-4 mts for Ubuntu to get installed. You need to reboot once and you will be shown with an option to load Ubuntu during the boot process. And thats it !! There you are, a new, stable and fast OS installed in your PC in absolutely no time.

It might take couple of days to get started with Ubuntu.You are not presented with the old problem of mounting the hard disks and USB drives of previous years. Everything is already done for you. The OS comes with pre-installed software that cover up all your basic needs.

Now, let us look at the pros and cons

Pros
  • Stable, Secure, Fast Operating system - you dont need to worry about virus attacks
  • No need to burn CDs. The iso is sufficient for the installation.
  • 100% Free
  • All basic software already installed
  • An alternate way to recover files if your Windows OS is infected by virus
  • Free upgrade of the OS available every 6 months.
  • Online repository of free software available.
  • Good forums available to answer all your queries and a very activity community.

Cons
  • Ubuntu installed using Wubi is not as fast as it can be. To get the full speed of an OS, you need to install it using the traditional procedure.
  • By default, Ubuntu does not support mp3 and other audio/video formats. This is done to avoid legal issues. To overcome this, you need to install VLC or install unrestricted-audio-packages from the online repository.
  • You might face some problems if you have graphic cards installed. This problem however has been fixed from Ubuntu 9.10 (Karmic Koala)
  • Few of your special keys in your keyboard might not work. For instance, your wifi-enable or bleutooth-enable key might not work on the keyboard. Workaround is to boot up the OS with those features already enabled.
If you guys face any problem in the installation or in using the OS, you can contact me at rmnforever@gmail.com or post your queries at Ubuntu forums

Saturday, September 25, 2010

FFMpeg - Using Libavcodec in your program


This will be my second post on ffmpeg. The first post was on building ffmpeg on windows using mingw.
In this post, we will see how you can use those libraries in your application and decode movie files. I am taking dranger's first tutorial as the source and I will build it using Visual C++ 6.0. You can find the source code here.

This post has been published in CodeProject.com. You can view it here. The Codeproject image appearing in this post will indicate that the articles are also present in CodeProject.

Explanation of the code

Opening the video file

// Register all formats and codecs
av_register_all();

// Open video file
if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
return -1; // Couldn't open file

// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information

// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], 0);

First, we initialize ffmpeg by calling 'av_register_all()' at the starting of the program. This registers all supported formats and codecs.
Next we open the video file using av_open_input_file(). The first parameter is the pointer to the 'AVFormatContext' which we will use in our program to refer to the video file. The second parameter is the name of the file to be opened. The last three parameters are for file format, buffer size, and format options. By setting it to NULL and 0, libavformat detects and fills values on it own.
We dump information about the input file using 'dump_format()'

Opening the decoder

videoStream=-1;
for(i=0; inb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
  videoStream=i;
  break;
}
if(videoStream==-1)
return -1;
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

We loop through the list of streams present in the input file to locate the 'video' stream present in it. The file can typically contain one or more audio/video streams in it. Once a video stream is located, we extract the codec type of the video stream. This codec type will be used to initialize the decode as shown below. avcoded_find_decoder accepts the codec id (obtained from the video file) as input and returns a pointer of type 'AVCodec'. The codec is then open by making a call to 'avcodec_open()'

pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1;

Allocating buffers for decoding

numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,  pCodecCtx->width, pCodecCtx->height);

This part of the code is fairly elementary. We calculate the size of the buffer required, allocate a temporary buffer using malloc

Decoding frames

i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
if(packet.stream_index==videoStream) {
  avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size); 
  if(frameFinished) {
  img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
                (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width,
                pCodecCtx->height);

if(++i<=5)
  SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,
        i);
  }
}

Well, this is the part of the code that excites me the most, decoding the frames :) You read frames the file using av_read_frame. This function returns a non-zero positive value if the file has not reached its end. After receiving a packet, we first check if it is a video packet. We pass the CodecContext, Packet data and size as the input to the function. If decoding of the packet is successful, the 'pFrame' pointer points to decoded data. Success or failure of the decoding process is indicated by the 'frameFinished'. If it fails, the value will be '0'. We then convert the data from YUV to RBG format so that we can write to the file. I guess, by default, the decoded data is of type YUV.

Cleanup

av_free(buffer);
av_free(pFrameRGB);
av_free(pFrame);
avcodec_close(pCodecCtx);
av_close_input_file(pFormatCtx);

This is the part of the code i normally hate. But any mistake in this part will result in memory leaks or run time exceptions. So we will release all the allocated buffers and frames using av_free(). We will also close the codecontext and file using avcodec_close() and av_close_input_file() respectively.

Steps to build the application
  1. Create a win32 console application using Visual C++. Let us name it 'ffmpeg'.
  2. Create a folder named 'Libraries' and copy the following library files (avcodec.lib, avformat.lib, avutil.lib) to that folder. 
  3. Add 'tutorial1.c' to the project.
  4. Open the 'Project Settings' dialog box and in the C++ tab, choose the 'Preprocessor' option. In the 'Additional Include Directories', add the folder where the ffmpeg header files are present. (ex: C:\ffmpeg) 
  5. Navigate to the 'Link' tab in the project settings dialog and choose 'Input' as category. Add the following libraries to the 'Object/Library modules' text box : avformat.lib avcodec.lib avutil.lib. Add 'libraries' to the 'Additional Library path' text box.
  6. Build the application and you should be able to build it without any problem.
I hope to write on more post on encoding data using FFmpeg. That will complete the cycle.

Thursday, July 29, 2010

FFMpeg - Building on Windows

FFMpeg is probably the most widely used Encoder/Decoder library. It helps you when are working in a project that involves video processing. As with any open source project, it has very limited documentation. An article by by Martin Böhme (Using libavformat and libavcodec) and Stephen Dranger (FFmpeg and SDL Tutorial) were the only good articles available around. Apart from this, you might need to look into OutputExample.c that comes with the FFMpeg source code. I had a very hard time trying to learn from these tutorials and use 'libavcodec' library in my programs.This has been my prime motivating factor to start this blog.

The first thing about Open Source projects is, they dont distribute binaries. They distribute only the source code. So it makes your life harder to understand to download the source code, setup the build environment and then take a build. This process, though, is inevitable. If you really wish to skip the building process and get only a build, you can get them here. (P.S. these builds are outdated. If you plan to use it in your commercial application, you might need to search for an LGPL build. Else you will end up in FFMpeg's Hall of Shame)

Let's start with the build process. :)

Stuff to Download
  1. Download MinGW from here.
  2. Download MSYS from here.
  3. Download updated bash for MSYS from here.
  4. Get the latest snapshot of ffmpeg from here

Installing MinGW
  1. Click 'Next'
  2. Choose 'Download and Install' and click 'Next'
  3. Click 'I Agree'
  4. Choose 'Current' and then 'Next'
  5. Choose 'MinGW base tools' and 'MinGW Make'
  6. Choose Destination folder as 'C:\MinGW'
  7. Click 'Install'. The download will take some time and then will complete the installation.
  8. Click 'Next' and then 'Finish' to complete the installation.
Installing MinSys
  1. Click 'Yes'
  2. Click 'Next'
  3. Agree to the license agreement by clicking 'Yes'
  4. Click 'Next'
  5. Choose 'C:\msys\1.0' as destination folder and click 'Next'. Click 'Yes' to create a directory if not already there
  6. 'Installation for i386 based CPUs' should be selected, Click 'Next'
  7. Click 'Next' and then 'Install'
  8. The command window that opens will ask if you wish to continue with post install, type 'y'and press enter.
  9. Type 'y' and press enter when it asks if you have MinGW installed.
  10. Type 'C:/mingw' when it asks for your MinGW installation. (without quotes)
  11. Type 'y' when if it asks for adding mount bindings.
  12. Press enter and then Click 'Finish' to complete the installation.
Building FFMpeg
  1. Extract CoreUtils to a folder. Copy the contents in the extracted 'bin' folder to "C:\msys\1.0\bin"
  2. Extract the ffmpeg sources. I’ll assume you’ve extracted them to c:\work\ffmpeg .
  3. Open MSYS and navigate to C:\work\ffmpeg
  4. Type "./configure --enable-memalign-hack --enable-shared" and press Enter. --enable-memalign-hack is to specify that you need windows binaries and --enable-shared is to specify to build 'libavcodec.dll' and 'libavformat.dll'
  5. By default, it is an LGPL build. You need specify -enable-gpl. For more info on command line arguments, type ./configure --help
  6. Type 'make' and press 'Enter'
And there you have it, your ffmpeg binaries are ready to be used.

I have personally verified all the above mentioned steps, but then you may face problems during the whole process. The following links might of use in that case.

Installing MSYS
http://www.transana.org/developers/setup/AudioExtract/MSYS-Win.htm

Building ffmpeg on windows
http://www.gooli.org/blog/building-ffmpeg-for-windows-with-msys-and-mingw/
http://www.ffmpeg.org/general.html#SEC20

Building FFMpeg on linux is fairly easy. I have not tried building it on other platforms.

Wednesday, July 28, 2010

WAMP : Windows - Apache - MySQL - PHP


I have been using phptriad 2.2.1 on Windows for a very long time. It just made my work so easy. The combination of PHP, MySQL and Apache is extremely powerful when you need to quickly develop web pages.

Unfortunately, there were no newer versions of the triad available on the internet, and therefore I was not able to work with PHP5. As I searched for alternative solutions, I came across WAMP Server.

The WAMP Server looks a compact solution. The features (upgrade, opening up localhost, PHPMyAdmin) available in the System Tray menu make your life lot easier. For people who used the 'htdocs' folder to copy your PHP scripts, you must create a folder in the 'www' directory and paste your code in there.