# Building mpeg4ip with V4L2 support

January, 2004
Bill May
Original version by Waqar Mohsin

Building on Redhat 9
Adding 2.6.1 Kernel to SuSE 9.0
Adding the 2.6.3 Kernel to Redhat 9.0

## A. Redhat Linux 9 Installation

This section only applies if you setting up a machine from scratch.
If not, go here
Pick the custom install option.

### 1. Package Group Selection

Package Name | Selected
X Window System | 31/33
GNOME | 35/35
KDE | 14/16
Editors | 2/4
Graphical Internet | 8/14
Sound and Video | 14/19
Graphics | 10/13
Server Configuration Tools | 9/13
Web Server | 12/17
Development Tools | 35/46
Kernel Development | 4/4
X Software Development | 18/18
GNOME Software Development | 42/48
Administration Tools | 11/11

### 2. Build a Custom Kernel

The instructions are given in Redhat Linux 9 Customization Guide. You must have the kernel source installed before continuing

1. Open a terminal.
2. `su -`
3. `cd /usr/src/linux-2.4`
4. `make mrproper`
5. `cp config/kernel-2.4.20-i686.config .config`
6. `make xconfig`
7. This will bring up the Linux Kernel Configuration window. Just click Save and Exit.
8. `make clean`
9. `make bzImage`
10. `make modules`
11. `make modules_install`
12. `make install`
13. Verify that `/boot/initrd-2.4.20-8custom.img` exists
14. `reboot`

### 3. Install V4L2

V4L2 is a work in progress. So far, I've only gotten the 20030717 CVS snapshot to work.

You should read the directions from the V4L2 web site - they only apply for the versions that we've specified.

1. Download `v4l2-20030717.tar.gz` to the home directory
2. `tar xvfz v4l2-20030717.tar.gz`
3. su
4. `cd v4l2`
5. `mv /usr/include/linux/videodev.h /usr/include/linux/videodev.h.orig```
6. `cp videodev.h /usr/include/linux```
7. `cp videodev2.h /usr/include/linux`
8. Edit `Makefile` and change `depmod` to `/sbin/depmod`
9. `make`
10. `make install`

### 4. Install New bttv Driver

1. Download `bttv9-20030717.tar.gz` to the home directory
2. `tar xvfz bttv9-20030717.tar.gz```
3. su
4. `cd bttv-0.9.12
   `
5. Edit `Makefile` and change `depmod` to `/sbin/depmod`
6. `make`
7. `make install`

### 5. Setup modprobe to load bttv from new location

The V4L2 and bttv drivers are installed in the /lib/modules/2.4.20-8custom/v4l2 directory. You should see a listing similar to this:
`-rw-r--r-- 1 root root 4936 Aug 4 18:54 btcx-risc.o
-rw-r--r-- 1 root root 113484 Aug 4 18:54 bttv.o
-rw-r--r-- 1 root root 15616 Jul 18 10:48 v4l1-compat.o
-rw-r--r-- 1 root y root 8084 Jul 18 10:48 v4l2-common.o
-rw-r--r-- 1 root root 19544 Aug 4 18:54 video-buf.o`
The original V4L2 and bttv drivers are installed in /lib/modules/2.4.20-8custom/kernel/drivers/media/video.
modprobe is a program which loads/unloads loadable kernel modules (LKMs). By default, modprobe searches for an LKM in /lib/modules/boot before looking in /lib/modules/2.4.20-8custom. This can be verified by using the '`modprobe -c`' command. modprobe also uses /lib/modules/2.4.20-8custom/modules.dep for resolving module dependencies.
The bttv driver installation does not remove the original bttv.o entry from the modules.dep file (bug ???). Since we want to load the bttv driver (and its dependencies) from the new location (/lib/modules/2.4.20-8custom/v4l2 directory), an easy way is to rename the original bttv LKM and remove its entry from the modules.dep file. We recommend removing all bttv.o files. Do a `find . -name bttv.o -print` and make sure all copies of bttv.o are purged. The following steps do that:

1. `cd /lib/modules/2.4.20-8custom/kernel/drivers/media/video`
2. `mv bttv.o bttv.o.orig` (We recommend `rm bttv.o`)
3. `cd /lib/modules/2.4.20-8custom`
4. Edit `modules.dep` and delete `/lib/modules/2.4.20-8custom/kernel/drivers/media/video/bttv.o` and its dependencies.
5. Reboot and select the custom kernel when the kernel load screen appears. Alternatively, change /etc/grub.conf to indicate a default of 0 (that will be the custom kernel you created, and will automatically select it when you reboot).

Do a find to make sure that you've overwritten all bttv.o files.

### 6. Testing the Installation

- `su -`
- `modprobe bttv`
- `modinfo bttv` should display something like

`filename: /lib/modules/2.4.20-8custom/v4l2/bttv.o
description: "bttv - v4l/v4l2 driver module for bt848/878 based cards"
author: "Ralph Metzler & Marcus Metzler & Gerd Knorr"
license: "GPL"
...
`

- `modinfo v4l2-common` should display something like

`filename: /lib/modules/2.4.20-8custom/v4l2/v4l2-common.o
description: "misc helper functions for v4l2 device drivers"
author: "Bill Dirks, Justin Schoeman, Gerd Knorr"
license: "GPL"`

## B. SuSE 9.0 Installation of 2.6.1 kernel

This section describes what I did when updating SuSE to a 2.6.1 kernel with V4L2 support. I'm not 100% happy with the results (it seems to return the default settings for the audio, which on my machine is muted, and microphone off), but it appears to work.

Note: I did not use the 2.6 test kernel sources that come with SuSE 9.0.

First, see the instructions on what RPMs are needed for SuSE 9.0 PRO in the main readme.

- First, download the 2.6.1 kernel. Untar it.
- Next, download the v4l2 patches. I used the 2.6.1rc3 patch. Patch them into the kernel source
- You'll have to make a minor change to kernel/exit.c. You'll have to add `EXPORT_SYMBOL(__set_special_pids);` and `EXPORT_SYMBOL(set_special_pids);` before the function calls.
- Configure the kernel. You can use make oldconfig. Make sure that you enable OSS (it says deprecated, but I was not able to get the ALSA OSS compatibility layer to work, so I needed it).
- Build the kernel.`make;make modules`
- Download and untar the bttv driver. I used 0.9.12.
- Make the bttv driver `make KDIR=`kernel source directory
- Install the kernel. `make install; make modules_install`
- Install the bttv driver `make install`
- Add the kernel to lilo or grub

At this point, you may have a bootable kernel. I, on the other hand, did not. It appeared not to load any of the modules for the PCI slots.

This was the difficult part. I went to the /etc directory, and used the `generate-modprobe.conf` command to generate a new modprobe.conf. Then I had to add command to install eth0, eth1 and the bttv driver. I also had to remove the snd_ prefixes from the options for snd-emu10k1 and options snd:

Here's the diff that I had to make after the `generate-modprobe.conf`:
`< options snd-emu10k1 snd_enable=1 snd_index=0
< options snd snd_cards_limit=1 snd_major=116
---
> options snd-emu10k1 enable=1 index=0
> options snd cards_limit=1 major=116
217a218,221
> install eth0 /bin/true
> install eth1 /bin/true
> install bttv /sbin/modprobe --ignore-install bttv && { /sbin/modprobe -k tuner; /sbin/modprobe -k msp3400; }
`

Good luck.

## C. Redhat 9.0 Installation of 2.6.3 kernel

Written by TrFremont

This was written 25th February 2004. URLs and available files change over time so if you are attempting this far in the future do not be surprised if things are not the same.

I am no Kernel expert. I did this to get rid of a bug. Mooie helped me a lot with how to do this.

### Why do this?

If you use Red Hat 9.0, a Brooktree chip capture card and have installed MPEG4IP and kept it up to date, you probaby have MPEG4IP V1.0 or V1.05, the bttv driver from bttv9-20030717.tar.gz and the V4L2 package distributed with MPEG4IP. The recent revisions contained in those packages cured the a/v sync problems, but there is a another, and more rare bug buried deep in that old bttv driver which sometimes causes video capture to freeze. In order to remove the bug you must update to a recent bttv package, which also needs a more recent v4l2 package, and they both need a more recent linux kernel than 2.4.20, which is the one that was distributed with RH 9.0.

Here are instructions on how to obtain, configure, build and install linux 2.6.3 kernel which contains the latest bttv and v4l2 packages.

If you have other than Brooktree hardware, there are drivers for other video capture systems included. I have not tried them, but they may match your hardware.

### Advantages

You get rid of the bug that causes video capture to hang. No more messing about with extra packages or kernel modules - everything is now available as options right in the new kernel.

### Disadvantages

The Red Hat organization does not support the use of later kernels, so if you have a Red Hat bug show up, they might not be able to help you solve it. However, the only bug I have found so far is that the memory useage is no longer show correctly in the System Monitor tool. I have not yet investigated how to fix this. Compiling, shells, Mozilla, all seem to work ok with 2.6.3.

### Upgrade Relaiblility

This upgrade has been completed on two machines so far. The second machine upgrade went almost like clockwork. One machine had RH9.0 plus all the latest upgrades from Red Hat. The other had no upgrades at all, was stock RH9.0 installed from ISO CDs.

### Utilities Needed

If you have compiled and installed MPEG4IP you should have enough to deal with the kernel.

### Method In Brief

Download and unpackage the kernel source. Run configure, select options. Make. Make Install. Fix bootloader. Reboot.

### Method in Detail

Log in as root, go to http://www.kernel.org
Click on the 2.6.3 link (latest stable at time of writing) and save the .bz2 file to your /root/ directory.
unzip and untar the files. You will then have a /root/linux-2.6.3/ directory.
(You can put the files any place, though there is a warning about NOT using /usr/src/linux area as other files around there may mess things up.)

Make sure you are in root directory -
`#cd /root`
Reset the ownership of all the package files -
`#chown -R root.root linux-2.6.3`
Move to the package directory -
`#cd linux-2.6.3`
(You can read the README file there, it has alternate build commands and note about versions of compilers needed etc.)

Run the configuration program -
`#make menuconfig`
(It will check some things, then bring up a menu system for selecting options.)

You must now select the bttv and v4l2 items, they are _not_ on by default. Navigate through the items, turn on the ones shown [Y] below.

    I2C Support
      [Y] I2C Support
          I2C Algorithms
          [Y] bit banging
          [Y] Video For Linux
             Video For Linux
    	 Video Adapters
    	 [Y] BT848 Video For Linux

(If you have a capture card or system other than BT848, select what is appropriate. I do not know how well the other options work)

(Also, you _may_ have to turn on support for the audio card you have. One machine with on-board audio worked with nothing extra. The other machine with a PCI sound blaster card needed its support option enabled before sound worked.)

Possibly -

    [Y] for audio device that you have.

Mpeg4ip note: see above as how to configure with OSS enabled.

Exit and save the configuration.
Run make
`#make`
(It takes less than an hour on 1.7G P4.)

Install
`#make install`
(Note that if you have to do this a second time, and you are running the on the new kernel from the first attempt, make install may not work. You have to reboot to the old kernel to install the new one.)

I use the grub boot loader. Make install puts an entry in /etc/grub.conf however it does not set the boot information correctly, you have to fix that by hand to make it boot properly.

Look for the section that starts 'title Red hat Linux (2.6.3)
Look about two lines below that for the line that starts 'kernel....
If that line ends with 'root=LABEL=/' you need to change that to something like 'root=/dev/hda3'
If you look at around the 7th line in the file, there may be a comment that shows the correct root= setting for your system. Use that.

You can change the default boot option by changing the number at the end of the 'default=x' line, make x 0 to select the first of\ption, 1 for the second...

Save the grub.conf file.
Shutdown / restart the machine.
Watch the boot messages, make sure the new 2.6.3 kernel gets selected.
You may see error/warning messages early in the boot process, something like
`QM_MODULES: Function not implemented`
`Likely about initializing USB keyboard, usb mouse, ohci1394.`

These _appear_ to be the system still trying to load modules which are not used in this kernel, as they are already in the kernel now. If the rest of the boot proceeds I think you can ignore those warnings.

You may be able to get rid of them by fixing something in /lib/modules. But that may also prevent booting to the old kernel in the future.

mp4live and gmp4player will work without re-compiling them, however on one machine I did re-compile mpeg4ip without incident.

MPEG4IP NOTE: if you went from an non-V4L2 kernel, you will need to re-bootstrap mpeg4ip and recompile

If you have to change something in the kernel config, and recompile the kernel, you might want to

`#make mrproper`
first, to clean everything up.

If you get lost in changing options and things end up working worse rather than better, you can start again by
`#rm .config`
then
`#make menuconfig`
it will start afresh.

MPEG4IP NOTE - this is a good primer - read the above SuSE install in addition to this if some things are not clear.

Remember, mpeg4ip is not the kernel project - we cannot support your kernel install - we can barely do our own.

Good luck!