Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decoder support for pixel format BGRA & ARGB #315

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/avfoundation/AVFoundationBind/AVFoundationBind.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ typedef enum AVBindFrameFormat {
AVBindFrameFormatNV12,
AVBindFrameFormatYUY2,
AVBindFrameFormatUYVY,
AVBindFrameFormatBGRA,
AVBindFrameFormatARGB,
} AVBindFrameFormat;

typedef void (*AVBindDataCallback)(void *userData, void *buf, int len);
Expand Down
12 changes: 12 additions & 0 deletions pkg/avfoundation/AVFoundationBind/AVFoundationBind.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ STATUS frameFormatToFourCC(AVBindFrameFormat format, FourCharCode *pFourCC) {
case AVBindFrameFormatYUY2:
*pFourCC = kCVPixelFormatType_422YpCbCr8_yuvs;
break;
case AVBindFrameFormatBGRA:
*pFourCC = kCVPixelFormatType_32ARGB;
break;
case AVBindFrameFormatARGB:
*pFourCC = kCVPixelFormatType_32BGRA;
break;
// TODO: Add the rest of frame formats
default:
retStatus = STATUS_UNSUPPORTED_FRAME_FORMAT;
Expand All @@ -170,6 +176,12 @@ STATUS frameFormatFromFourCC(FourCharCode fourCC, AVBindFrameFormat *pFormat) {
case kCVPixelFormatType_422YpCbCr8_yuvs:
*pFormat = AVBindFrameFormatYUY2;
break;
case kCVPixelFormatType_32ARGB:
*pFormat = AVBindFrameFormatBGRA;
break;
case kCVPixelFormatType_32BGRA:
*pFormat = AVBindFrameFormatARGB;
break;
// TODO: Add the rest of frame formats
default:
retStatus = STATUS_UNSUPPORTED_FRAME_FORMAT;
Expand Down
8 changes: 8 additions & 0 deletions pkg/avfoundation/avfoundation_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func frameFormatToAVBind(f frame.Format) (C.AVBindFrameFormat, bool) {
return C.AVBindFrameFormatYUY2, true
case frame.FormatUYVY:
return C.AVBindFrameFormatUYVY, true
case frame.FormatBGRA:
return C.AVBindFrameFormatBGRA, true
case frame.FormatARGB:
return C.AVBindFrameFormatARGB, true
default:
return 0, false
}
Expand All @@ -63,6 +67,10 @@ func frameFormatFromAVBind(f C.AVBindFrameFormat) (frame.Format, bool) {
return frame.FormatYUY2, true
case C.AVBindFrameFormatUYVY:
return frame.FormatUYVY, true
case C.AVBindFrameFormatBGRA:
return frame.FormatBGRA, true
case C.AVBindFrameFormatARGB:
return frame.FormatARGB, true
default:
return "", false
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/frame/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (

// FormatRGBA https://www.fourcc.org/pixel-format/rgb-rgba/
FormatRGBA Format = "RGBA"
FormatARGB Format = "ARGB"
FormatBGRA Format = "BGRA"

// FormatMJPEG https://www.fourcc.org/mjpg/
FormatMJPEG = "MJPEG"
Expand All @@ -40,6 +42,8 @@ var decoderMap = map[Format]decoderFunc{
FormatUYVY: decodeUYVY,
FormatMJPEG: decodeMJPEG,
FormatZ16: decodeZ16,
FormatARGB: decodeARGB,
FormatBGRA: decodeBGRA,
}

func NewDecoder(f Format) (Decoder, error) {
Expand Down
44 changes: 44 additions & 0 deletions pkg/frame/rgb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package frame

import (
"fmt"
"image"
"math/bits"
"unsafe"
)

func decodeARGB(frame []byte, width, height int) (image.Image, func(), error) {
size := 4 * width * height
if size > len(frame) {
return nil, func() {}, fmt.Errorf("frame length (%d) less than expected (%d)", len(frame), size)
}
r := image.Rect(0, 0, width, height)
for i := 0; i < size; i += 4 {
*(*uint32)(unsafe.Pointer(&frame[i])) = func(v uint32) uint32 {
return (v & 0xFF00FF00) | (v&0xFF)<<16 | (v&0xFF0000)>>16
}(*(*uint32)(unsafe.Pointer(&frame[i])))
//frame[i], frame[i+2] = frame[i+2], frame[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a conversion from ARGB to GRAB. (frame[i] is A and frame[i+2] is G in the original ARGB data)
Maybe the implementations of decodeARGB and decodeBGRA are swapped?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it only matters to the name of the pixel format.

according to libyuv formats:

kCMPixelFormat_32ARGB          = 32,      FOURCC_BGRA
kCMPixelFormat_32BGRA          = 'BGRA',  FOURCC_ARGB

and I use the FOURCC_* naming to to name frame.FormatBGRA and frame.FormatARGB which results a similar mapping in avfoundation pkg.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misread endian. (It should be little endian on x86 and ARM)

This func seems converting 0xXXYYZZWW (input) to 0xXXWWZZYY (output).
As the output byte order for image.RGBA is RGBA order (XX=A, WW=B, ZZ=G, YY=R on little endian uint32), input byte order shoud be BGRA.

I guess
https://github.com/pion/mediadevices/pull/315/files#diff-99707109ce57457c8ab1161707d059373858d14ae66e06aed05c50501e29eaa6R148-R153
https://github.com/pion/mediadevices/pull/315/files#diff-99707109ce57457c8ab1161707d059373858d14ae66e06aed05c50501e29eaa6R179-R184
are reversed and name of decodeARGB and decodeBGRA should be swapped?

(I don't have any Macintosh computers, so I couldn't test it on the real environment.)

}
return &image.RGBA{
Pix: frame[:size:size],
Stride: 4 * r.Dx(),
Rect: r,
}, func() {}, nil
}

func decodeBGRA(frame []byte, width, height int) (image.Image, func(), error) {
size := 4 * width * height
if size > len(frame) {
return nil, func() {}, fmt.Errorf("frame length (%d) less than expected (%d)", len(frame), size)
}
r := image.Rect(0, 0, width, height)
for i := 0; i < size; i += 4 {
*(*uint32)(unsafe.Pointer(&frame[i])) = bits.RotateLeft32(*(*uint32)(unsafe.Pointer(&frame[i])), -8)
//frame[i], frame[i+1], frame[i+2], frame[i+3] = frame[i+1], frame[i+2], frame[i+3], frame[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BGRA to GRAB ?

}
return &image.RGBA{
Pix: frame[:size:size],
Stride: 4 * r.Dx(),
Rect: r,
}, func() {}, nil
}
48 changes: 48 additions & 0 deletions pkg/frame/rgb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package frame

import (
"fmt"
"testing"
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a simple test like:

func TestDecodeYUY2(t *testing.T) {
const (
width = 2
height = 2
)
input := []byte{
// Y Cb Y Cr
0x01, 0x82, 0x03, 0x84,
0x05, 0x86, 0x07, 0x88,
}
expected := &image.YCbCr{
Y: []byte{0x01, 0x03, 0x05, 0x07},
YStride: width,
Cb: []byte{0x82, 0x86},
Cr: []byte{0x84, 0x88},
CStride: width / 2,
SubsampleRatio: image.YCbCrSubsampleRatio422,
Rect: image.Rect(0, 0, width, height),
}
decoder, err := NewDecoder(FormatYUY2)
if err != nil {
t.Fatal(err)
}
img, _, err := decoder.Decode(input, width, height)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, img) {
t.Errorf("Wrong decode result,\nexpected:\n%+v\ngot:\n%+v", expected, img)
}
}

func BenchmarkDecodeBGRA(b *testing.B) {
sizes := []struct {
width, height int
}{
{640, 480},
{1920, 1080},
}
for _, sz := range sizes {
sz := sz
b.Run(fmt.Sprintf("%dx%d", sz.width, sz.height), func(b *testing.B) {
input := make([]byte, sz.width*sz.height*4)
for i := 0; i < b.N; i++ {
_, _, err := decodeBGRA(input, sz.width, sz.height)
if err != nil {
b.Fatal(err)
}
}
})
}
}

func BenchmarkDecodeARGB(b *testing.B) {
sizes := []struct {
width, height int
}{
{640, 480},
{1920, 1080},
}
for _, sz := range sizes {
sz := sz
b.Run(fmt.Sprintf("%dx%d", sz.width, sz.height), func(b *testing.B) {
input := make([]byte, sz.width*sz.height*4)
for i := 0; i < b.N; i++ {
_, _, err := decodeARGB(input, sz.width, sz.height)
if err != nil {
b.Fatal(err)
}
}
})
}
}