Skip to content

Commit

Permalink
Update about packed and planar YUV formats
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 10, 2025
1 parent 7dc9beb commit 8390713
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/v4l2/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (d *Device) StreamOff() (err error) {
return ioctl(d.fd, VIDIOC_REQBUFS, unsafe.Pointer(&rb))
}

func (d *Device) Capture(cositedYUV bool) ([]byte, error) {
func (d *Device) Capture(planarYUV bool) ([]byte, error) {
dec := v4l2_buffer{
typ: V4L2_BUF_TYPE_VIDEO_CAPTURE,
memory: V4L2_MEMORY_MMAP,
Expand All @@ -206,7 +206,7 @@ func (d *Device) Capture(cositedYUV bool) ([]byte, error) {
}

buf := make([]byte, dec.bytesused)
if cositedYUV {
if planarYUV {
YUYV2YUV(buf, d.bufs[dec.index][:dec.bytesused])
} else {
copy(buf, d.bufs[dec.index][:dec.bytesused])
Expand Down
2 changes: 1 addition & 1 deletion pkg/v4l2/device/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Formats = []Format{
{V4L2_PIX_FMT_MJPEG, "Motion-JPEG", "mjpeg"},
}

// YUYV2YUV convert [Y0 Cb Y1 Cr] to cosited [Y0Y1... Cb... Cr...]
// YUYV2YUV convert packed YUV to planar YUV
func YUYV2YUV(dst, src []byte) {
n := len(src)
i0 := 0
Expand Down
4 changes: 2 additions & 2 deletions pkg/v4l2/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func (c *Producer) Start() error {
return err
}

cositedYUV := c.Medias[0].Codecs[0].Name == core.CodecRAW
planarYUV := c.Medias[0].Codecs[0].Name == core.CodecRAW

for {
buf, err := c.dev.Capture(cositedYUV)
buf, err := c.dev.Capture(planarYUV)
if err != nil {
return err
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/y4m/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
## Planar YUV formats

Packed YUV - yuyv422 - YUYV 4:2:2
Semi-Planar - nv12 - Y/CbCr 4:2:0
Planar YUV - yuv420p - Planar YUV 4:2:0 - aka. [cosited](https://manned.org/yuv4mpeg.5)

```
[video4linux2,v4l2 @ 0x55fddc42a940] Raw : yuyv422 : YUYV 4:2:2 : 1920x1080
[video4linux2,v4l2 @ 0x55fddc42a940] Raw : nv12 : Y/CbCr 4:2:0 : 1920x1080
[video4linux2,v4l2 @ 0x55fddc42a940] Raw : yuv420p : Planar YUV 4:2:0 : 1920x1080
```

## Useful links

- https://learn.microsoft.com/en-us/windows/win32/medfound/recommended-8-bit-yuv-formats-for-video-rendering
- https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Video_concepts
- https://fourcc.org/yuv.php#YV12
- https://docs.kernel.org/userspace-api/media/v4l/pixfmt-yuv-planar.html
- https://gist.github.com/Jim-Bar/3cbba684a71d1a9d468a6711a6eddbeb

0 comments on commit 8390713

Please sign in to comment.