= camera on a raspberry *** DRAFT ***

== installation

NOTE: elvis left the building

 source /bin/cid-functions
 cid_add_line /boot/config.txt "start_x=1"
 cid_add_line /boot/config.txt "gpu_mem=128"

 vi /etc/modules-load.d/camera.conf 

----
bcm2835-v4l2
----

 modprobe -v bcm2835-v4l2

 yum install v4l-utils

 reboot

== xtra reading

The commands for the camera module are included as part of the raspberrypi-firmware package - which is installed by default.

 -------------------------------------------------------------------------------

The V4L2 driver by default only allows video recording up to 1280x720, else it glues together consecutive still screens resulting in videos of 4 fps or lower. 

Adding the following options removes this limitation:

/etc/modprobe.d/rpi-camera.conf

options bcm2835-v4l2 max_video_width=3240 max_video_height=2464

== testing

 rpm -q --quiet "xawtv" || yum -y install xawtv

 v4l-info

----
### v4l2 device info [/dev/video0] ###
general info
    VIDIOC_QUERYCAP
	driver                  : "bm2835 mmal"
	card                    : "mmal service 16.1"
	bus_info                : "platform:bcm2835-v4l2"
	version                 : 4.9.30
	capabilities            : 0x85200005 [VIDEO_CAPTURE,VIDEO_OVERLAY,?,READWRITE,STREAMING,(null)]

standards

inputs
    VIDIOC_ENUMINPUT(0)
	index                   : 0
	name                    : "Camera 0"
	type                    : CAMERA
	audioset                : 0
	tuner                   : 0
	std                     : 0x0 []
	status                  : 0x0 []

video capture
    VIDIOC_ENUM_FMT(0,VIDEO_CAPTURE)
	index                   : 0
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "Planar YUV 4:2:0"
	pixelformat             : 0x32315559 [YU12]
    VIDIOC_ENUM_FMT(1,VIDEO_CAPTURE)
	index                   : 1
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "YUYV 4:2:2"
	pixelformat             : 0x56595559 [YUYV]
    VIDIOC_ENUM_FMT(2,VIDEO_CAPTURE)
	index                   : 2
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "24-bit RGB 8-8-8"
	pixelformat             : 0x33424752 [RGB3]
    VIDIOC_ENUM_FMT(3,VIDEO_CAPTURE)
	index                   : 3
	type                    : VIDEO_CAPTURE
	flags                   : 1
	description             : "JFIF JPEG"
	pixelformat             : 0x4745504a [JPEG]
    VIDIOC_ENUM_FMT(4,VIDEO_CAPTURE)
	index                   : 4
	type                    : VIDEO_CAPTURE
	flags                   : 1
	description             : "H.264"
	pixelformat             : 0x34363248 [H264]
    VIDIOC_ENUM_FMT(5,VIDEO_CAPTURE)
	index                   : 5
	type                    : VIDEO_CAPTURE
	flags                   : 1
	description             : "Motion-JPEG"
	pixelformat             : 0x47504a4d [MJPG]
    VIDIOC_ENUM_FMT(6,VIDEO_CAPTURE)
	index                   : 6
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "YVYU 4:2:2"
	pixelformat             : 0x55595659 [YVYU]
    VIDIOC_ENUM_FMT(7,VIDEO_CAPTURE)
	index                   : 7
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "VYUY 4:2:2"
	pixelformat             : 0x59555956 [VYUY]
    VIDIOC_ENUM_FMT(8,VIDEO_CAPTURE)
	index                   : 8
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "UYVY 4:2:2"
	pixelformat             : 0x59565955 [UYVY]
    VIDIOC_ENUM_FMT(9,VIDEO_CAPTURE)
	index                   : 9
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "Y/CbCr 4:2:0"
	pixelformat             : 0x3231564e [NV12]
    VIDIOC_ENUM_FMT(10,VIDEO_CAPTURE)
	index                   : 10
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "24-bit BGR 8-8-8"
	pixelformat             : 0x33524742 [BGR3]
    VIDIOC_ENUM_FMT(11,VIDEO_CAPTURE)
	index                   : 11
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "Planar YVU 4:2:0"
	pixelformat             : 0x32315659 [YV12]
    VIDIOC_ENUM_FMT(12,VIDEO_CAPTURE)
	index                   : 12
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "Y/CrCb 4:2:0"
	pixelformat             : 0x3132564e [NV21]
    VIDIOC_ENUM_FMT(13,VIDEO_CAPTURE)
	index                   : 13
	type                    : VIDEO_CAPTURE
	flags                   : 0
	description             : "32-bit BGRA/X 8-8-8-8"
	pixelformat             : 0x34524742 [BGR4]
    VIDIOC_G_FMT(VIDEO_CAPTURE)
	type                    : VIDEO_CAPTURE
	fmt.pix.width           : 1024
	fmt.pix.height          : 768
	fmt.pix.pixelformat     : 0x4745504a [JPEG]
	fmt.pix.field           : NONE
	fmt.pix.bytesperline    : 0
	fmt.pix.sizeimage       : 786432
	fmt.pix.colorspace      : JPEG
	fmt.pix.priv            : 4276996862

video overlay
    VIDIOC_ENUM_FMT(0,VIDEO_OVERLAY)
	index                   : 0
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "Planar YUV 4:2:0"
	pixelformat             : 0x32315559 [YU12]
    VIDIOC_ENUM_FMT(1,VIDEO_OVERLAY)
	index                   : 1
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "YUYV 4:2:2"
	pixelformat             : 0x56595559 [YUYV]
    VIDIOC_ENUM_FMT(2,VIDEO_OVERLAY)
	index                   : 2
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "24-bit RGB 8-8-8"
	pixelformat             : 0x33424752 [RGB3]
    VIDIOC_ENUM_FMT(3,VIDEO_OVERLAY)
	index                   : 3
	type                    : VIDEO_OVERLAY
	flags                   : 1
	description             : "JFIF JPEG"
	pixelformat             : 0x4745504a [JPEG]
    VIDIOC_ENUM_FMT(4,VIDEO_OVERLAY)
	index                   : 4
	type                    : VIDEO_OVERLAY
	flags                   : 1
	description             : "H.264"
	pixelformat             : 0x34363248 [H264]
    VIDIOC_ENUM_FMT(5,VIDEO_OVERLAY)
	index                   : 5
	type                    : VIDEO_OVERLAY
	flags                   : 1
	description             : "Motion-JPEG"
	pixelformat             : 0x47504a4d [MJPG]
    VIDIOC_ENUM_FMT(6,VIDEO_OVERLAY)
	index                   : 6
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "YVYU 4:2:2"
	pixelformat             : 0x55595659 [YVYU]
    VIDIOC_ENUM_FMT(7,VIDEO_OVERLAY)
	index                   : 7
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "VYUY 4:2:2"
	pixelformat             : 0x59555956 [VYUY]
    VIDIOC_ENUM_FMT(8,VIDEO_OVERLAY)
	index                   : 8
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "UYVY 4:2:2"
	pixelformat             : 0x59565955 [UYVY]
    VIDIOC_ENUM_FMT(9,VIDEO_OVERLAY)
	index                   : 9
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "Y/CbCr 4:2:0"
	pixelformat             : 0x3231564e [NV12]
    VIDIOC_ENUM_FMT(10,VIDEO_OVERLAY)
	index                   : 10
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "24-bit BGR 8-8-8"
	pixelformat             : 0x33524742 [BGR3]
    VIDIOC_ENUM_FMT(11,VIDEO_OVERLAY)
	index                   : 11
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "Planar YVU 4:2:0"
	pixelformat             : 0x32315659 [YV12]
    VIDIOC_ENUM_FMT(12,VIDEO_OVERLAY)
	index                   : 12
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "Y/CrCb 4:2:0"
	pixelformat             : 0x3132564e [NV21]
    VIDIOC_ENUM_FMT(13,VIDEO_OVERLAY)
	index                   : 13
	type                    : VIDEO_OVERLAY
	flags                   : 0
	description             : "32-bit BGRA/X 8-8-8-8"
	pixelformat             : 0x34524742 [BGR4]
    VIDIOC_G_FMT(VIDEO_OVERLAY)
	type                    : VIDEO_OVERLAY
	fmt.win.w.left          : 150
	fmt.win.w.top           : 50
	fmt.win.w.width         : 1024
	fmt.win.w.height        : 768
	fmt.win.field           : NONE
	fmt.win.chromakey       : 0
	fmt.win.clips           : (nil)
	fmt.win.clipcount       : 0
	fmt.win.bitmap          : (nil)
    VIDIOC_G_FBUF
	capability              : 0x21 [EXTERNOVERLAY,(null)]
	flags                   : 0x2 [OVERLAY]
	base                    : (nil)
	fmt.width               : 1024
	fmt.height              : 768
	fmt.pixelformat         : 0x32315559 [YU12]
	fmt.field               : ANY
	fmt.bytesperline        : 1024
	fmt.sizeimage           : 1179648
	fmt.colorspace          : SMPTE170M
	fmt.priv                : 0

controls
    VIDIOC_QUERYCTRL(BASE+0)
	id                      : 9963776
	type                    : INTEGER
	name                    : "Brightness"
	minimum                 : 0
	maximum                 : 100
	step                    : 1
	default_value           : 50
	flags                   : 32
    VIDIOC_QUERYCTRL(BASE+1)
	id                      : 9963777
	type                    : INTEGER
	name                    : "Contrast"
	minimum                 : -100
	maximum                 : 100
	step                    : 1
	default_value           : 0
	flags                   : 32
    VIDIOC_QUERYCTRL(BASE+2)
	id                      : 9963778
	type                    : INTEGER
	name                    : "Saturation"
	minimum                 : -100
	maximum                 : 100
	step                    : 1
	default_value           : 0
	flags                   : 32
    VIDIOC_QUERYCTRL(BASE+14)
	id                      : 9963790
	type                    : INTEGER
	name                    : "Red Balance"
	minimum                 : 1
	maximum                 : 7999
	step                    : 1
	default_value           : 1000
	flags                   : 32
    VIDIOC_QUERYCTRL(BASE+15)
	id                      : 9963791
	type                    : INTEGER
	name                    : "Blue Balance"
	minimum                 : 1
	maximum                 : 7999
	step                    : 1
	default_value           : 1000
	flags                   : 32
    VIDIOC_QUERYCTRL(BASE+20)
	id                      : 9963796
	type                    : BOOLEAN
	name                    : "Horizontal Flip"
	minimum                 : 0
	maximum                 : 1
	step                    : 1
	default_value           : 0
	flags                   : 0
    VIDIOC_QUERYCTRL(BASE+21)
	id                      : 9963797
	type                    : BOOLEAN
	name                    : "Vertical Flip"
	minimum                 : 0
	maximum                 : 1
	step                    : 1
	default_value           : 0
	flags                   : 0
    VIDIOC_QUERYCTRL(BASE+24)
	id                      : 9963800
	type                    : MENU
	name                    : "Power Line Frequency"
	minimum                 : 0
	maximum                 : 3
	step                    : 1
	default_value           : 1
	flags                   : 0
    VIDIOC_QUERYCTRL(BASE+27)
	id                      : 9963803
	type                    : INTEGER
	name                    : "Sharpness"
	minimum                 : -100
	maximum                 : 100
	step                    : 1
	default_value           : 0
	flags                   : 32
    VIDIOC_QUERYCTRL(BASE+31)
	id                      : 9963807
	type                    : MENU
	name                    : "Color Effects"
	minimum                 : 0
	maximum                 : 15
	step                    : 1
	default_value           : 0
	flags                   : 0
    VIDIOC_QUERYCTRL(BASE+34)
	id                      : 9963810
	type                    : INTEGER
	name                    : "Rotate"
	minimum                 : 0
	maximum                 : 360
	step                    : 90
	default_value           : 0
	flags                   : 0
    VIDIOC_QUERYCTRL(BASE+42)
	id                      : 9963818
	type                    : INTEGER
	name                    : "Color Effects, CbCr"
	minimum                 : 0
	maximum                 : 65535
	step                    : 1
	default_value           : 32896
	flags                   : 0
----

 v4l2-ctl --list-devices

----
mmal service 16.1 (platform:bcm2835-v4l2):
	/dev/video0
-----

 v4lctl list

----
attribute  | type   | current | default | comment
 -----------+--------+---------+---------+-------------------------------------
norm       | choice | (null)  | (null)  |
input      | choice | Camera  | Camera  | Camera 0
bright     | int    |      50 |      50 | range is 0 => 100
contrast   | int    |       0 |       0 | range is -100 => 100
color      | int    |       0 |       0 | range is -100 => 100
Red Balanc | int    |    1000 |    1000 | range is 1 => 7999
Blue Balan | int    |    1000 |    1000 | range is 1 => 7999
Horizontal | bool   | off     | off     |
Vertical F | bool   | off     | off     |
Power Line | choice | 50 Hz   | 50 Hz   | Disabled 50 Hz 60 Hz Auto
Sharpness  | int    |       0 |       0 | range is -100 => 100
Color Effe | choice | None    | None    | None Black & White Sepia Negative Emboss Sketch Sky Blue Grass Green Skin Whiten Vivid Aqua Art Freeze Silhouette Solarization Antique Set Cb/Cr
----

 v4lctl -c /dev/video0 snap jpeg 1280x1024 snap.jpg

 streamer -c /dev/video0 -b 16 -o snap.jpeg

// rpm -q --quiet "fswebcam" || yum -y install fswebcam
// fswebcam --device "/dev/video0" --resolution "1024" --jpeg "100" --save snap.jpg

== deps

 ldd /opt/raspivid/raspivid

----
 linux-vdso.so.1 =>  (0x7ee06000)
 libmmal_core.so => /opt/raspivid/libmmal_core.so (0x76f58000)
 libmmal_util.so => /opt/raspivid/libmmal_util.so (0x76f38000)
 libmmal_vc_client.so => /opt/raspivid/libmmal_vc_client.so (0x76f1d000)
 libbcm_host.so => /opt/raspivid/libbcm_host.so (0x76ef5000)
 libvcsm.so => /opt/raspivid/libvcsm.so (0x76ee1000)
 libvchiq_arm.so => /opt/raspivid/libvchiq_arm.so (0x76ecb000)
 libvcos.so => /opt/raspivid/libvcos.so (0x76eb1000)
 libpthread.so.0 => /lib/libpthread.so.0 (0x76e8a000)
 libdl.so.2 => /lib/libdl.so.2 (0x76e74000)
 librt.so.1 => /lib/librt.so.1 (0x76e5d000)
 libc.so.6 => /lib/libc.so.6 (0x76d0c000)
 /lib/ld-linux-armhf.so.3 (0x54b24000)
----

== streaming

 yum install nc

=== on the pi

 raspivid -t 0 -br 60 -w 1024 -h 768 -ih -fps 40 -o - | nc -4 -v -k -l -p 2222

=== on the client

 mplayer -fps 40 -demuxer h264es ffmpeg://tcp://mmc056e.d01.net:2222

== references

https://www.element14.com/community/servlet/JiveServlet/previewBody/54413-102-1-273177/Unofficial%20guide%20to%20getting%20up%20and%20running%20with%20the%20Raspberry%20Pi%20Camera.pdf

https://www.codeproject.com/Articles/665518/Raspberry-Pi-as-low-cost-HD-surveillance-camera


