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

Conversation

zyxar
Copy link
Member

@zyxar zyxar commented Mar 7, 2021

Description

This PR adds decoder support for pixel format BGRA & ARGB, and enables them in pkg avfoundation.

@zyxar zyxar changed the title Support rgb decoders Add decoder support for pixel format BGRA & ARGB Mar 7, 2021
@codecov
Copy link

codecov bot commented Mar 7, 2021

Codecov Report

Merging #315 (1ff2ff2) into master (3d3830f) will decrease coverage by 0.53%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #315      +/-   ##
==========================================
- Coverage   55.90%   55.37%   -0.54%     
==========================================
  Files          54       55       +1     
  Lines        2277     2299      +22     
==========================================
  Hits         1273     1273              
- Misses        903      925      +22     
  Partials      101      101              
Impacted Files Coverage Δ
pkg/frame/decode.go 50.00% <ø> (ø)
pkg/frame/rgb.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3d3830f...1ff2ff2. Read the comment docs.

*(*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.)

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 ?

"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)
}
}

@edaniels
Copy link
Member

edaniels commented Mar 3, 2023

HI @zyxar, do you still want to get this in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants