Skip to content

Commit

Permalink
oof
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Oct 9, 2023
1 parent ad790cc commit 847c9e2
Show file tree
Hide file tree
Showing 26 changed files with 631 additions and 464 deletions.
6 changes: 0 additions & 6 deletions pkg/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,6 @@ encoder:
# one additional FFMPEG concat demux file
recording:
enabled: false
# image compression level:
# 0 - default compression
# -1 - no compression
# -2 - best speed
# -3 - best compression
compressLevel: 0
# name contains the name of the recording dir (or zip)
# format:
# %date:go_time_format% -- refer: https://go.dev/src/time/format.go
Expand Down
9 changes: 4 additions & 5 deletions pkg/config/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ type Server struct {
}

type Recording struct {
Enabled bool
CompressLevel int
Name string
Folder string
Zip bool
Enabled bool
Name string
Folder string
Zip bool
}

func (s *Server) WithFlags() {
Expand Down
56 changes: 56 additions & 0 deletions pkg/encoder/color/bgra/bgra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package bgra

import (
"image"
"image/color"
)

type BGRA struct {
image.RGBA
}

var BGRAModel = color.ModelFunc(func(c color.Color) color.Color {
if _, ok := c.(BGRAColor); ok {
return c
}
r, g, b, a := c.RGBA()
return BGRAColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
})

// BGRAColor represents a BGRA color.
type BGRAColor struct {
R, G, B, A uint8
}

func (c BGRAColor) RGBA() (r, g, b, a uint32) {
r = uint32(c.B)
r |= r << 8
g = uint32(c.G)
g |= g << 8
b = uint32(c.R)
b |= b << 8
a = uint32(255) //uint32(c.A)
a |= a << 8
return
}

func NewBGRA(r image.Rectangle) *BGRA {
return &BGRA{*image.NewRGBA(r)}
}

func (p *BGRA) ColorModel() color.Model { return BGRAModel }
func (p *BGRA) At(x, y int) color.Color {
i := p.PixOffset(x, y)
s := p.Pix[i : i+4 : i+4]
return BGRAColor{s[0], s[1], s[2], s[3]}
}

func (p *BGRA) Set(x, y int, c color.Color) {
i := p.PixOffset(x, y)
c1 := BGRAModel.Convert(c).(BGRAColor)
s := p.Pix[i : i+4 : i+4]
s[0] = c1.R
s[1] = c1.G
s[2] = c1.B
s[3] = 255
}
62 changes: 62 additions & 0 deletions pkg/encoder/color/rgb565/rgb565.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package rgb565

import (
"encoding/binary"
"image"
"image/color"
"math"
)

// RGB565 is an in-memory image whose At method returns RGB565 values.
type RGB565 struct {
// Pix holds the image's pixels, as RGB565 values in big-endian format. The pixel at
// (x, y) starts at Pix[(y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*2].
Pix []uint8
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}

// Model is the model for RGB565 colors.
var Model = color.ModelFunc(func(c color.Color) color.Color {
//if _, ok := c.(Color); ok {
// return c
//}
r, g, b, _ := c.RGBA()
return Color(uint16((r<<8)&rMask | (g<<3)&gMask | (b>>3)&bMask))
})

const (
rMask = 0b1111100000000000
gMask = 0b0000011111100000
bMask = 0b0000000000011111
)

// Color represents an RGB565 color.
type Color uint16

func (c Color) RGBA() (r, g, b, a uint32) {
return uint32(math.Round(float64(c&rMask>>11)*255.0/31.0)) << 8,
uint32(math.Round(float64(c&gMask>>5)*255.0/63.0)) << 8,
uint32(math.Round(float64(c&bMask)*255.0/31.0)) << 8,
0xffff
}

func NewRGB565(r image.Rectangle) *RGB565 {
return &RGB565{Pix: make([]uint8, r.Dx()*r.Dy()<<1), Stride: r.Dx() << 1, Rect: r}
}

func (p *RGB565) Bounds() image.Rectangle { return p.Rect }
func (p *RGB565) ColorModel() color.Model { return Model }
func (p *RGB565) PixOffset(x, y int) int { return (x-p.Rect.Min.X)<<1 + (y-p.Rect.Min.Y)*p.Stride }

func (p *RGB565) At(x, y int) color.Color {
i := p.PixOffset(x, y)
return Color(binary.LittleEndian.Uint16(p.Pix[i : i+2]))
}

func (p *RGB565) Set(x, y int, c color.Color) {
i := p.PixOffset(x, y)
binary.LittleEndian.PutUint16(p.Pix[i:i+2], uint16(Model.Convert(c).(Color)))
}
11 changes: 8 additions & 3 deletions pkg/encoder/encoder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package encoder

import (
"image"
"sync"
"sync/atomic"

Expand All @@ -10,7 +9,7 @@ import (
)

type (
InFrame *image.RGBA
InFrame Frame
OutFrame []byte
Encoder interface {
LoadBuf(input []byte)
Expand All @@ -21,6 +20,12 @@ type (
}
)

type Frame struct {
Data []byte
Stride int
W, H int
}

type VideoEncoder struct {
encoder Encoder
log *logger.Logger
Expand Down Expand Up @@ -54,7 +59,7 @@ func (vp *VideoEncoder) Encode(img InFrame) OutFrame {
return nil
}

yCbCr := vp.y.Process(img, vp.rot, vp.pf)
yCbCr := vp.y.Process(yuv.SrcFrame(img), vp.rot, vp.pf)
vp.encoder.LoadBuf(yCbCr)
vp.y.Put(&yCbCr)

Expand Down
6 changes: 3 additions & 3 deletions pkg/encoder/yuv/libyuv.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ import "github.com/giongto35/cloud-game/v3/pkg/encoder/yuv/libyuv"

//func Version() int { return int(C.LIBYUV_VERSION) }

const FourccRgbp uint32 = libyuv.FourccRgbp
const FourccArgb uint32 = libyuv.FourccArgb
const FourccAbgr uint32 = libyuv.FourccAbgr
const FourccRgbp = libyuv.FourccRgbp
const FourccArgb = libyuv.FourccArgb
const FourccAbgr = libyuv.FourccAbgr

func Y420(src []byte, dst []byte, w, h, stride int, dw, dh int, rot uint, pix uint32, cx, cy int) {
libyuv.Y420_(src, dst, w, h, stride, dw, dh, rot, pix, cx, cy)
Expand Down
25 changes: 13 additions & 12 deletions pkg/encoder/yuv/yuv.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package yuv

import (
"image"
"sync"
)
import "sync"

type SrcFrame struct {
Data []byte
Stride int
W, H int
}

type ImgProcessor interface {
Process(rgba *image.RGBA, rot uint, pf uint32) []byte
Process(img SrcFrame, rot uint, pf uint32) []byte
Put(*[]byte)
}

Expand All @@ -31,24 +34,22 @@ func NewYuvImgProcessor(w, h int) ImgProcessor {
}
}

// Process converts RGBA colorspace into YUV I420 format inside the internal buffer.
// Non-threaded version.
func (yuv *processor) Process(rgba *image.RGBA, rot uint, pf uint32) []byte {
x, y := rgba.Bounds().Dx(), rgba.Bounds().Dy()
// Process converts an image to YUV I420 format inside the internal buffer.
func (yuv *processor) Process(img SrcFrame, rot uint, pf uint32) []byte {
dx, dy := yuv.w, yuv.h
cx, cy := yuv.w, yuv.h
if rot == 90 || rot == 270 {
cx, cy = cy, cx
}

stride := rgba.Stride / 4
stride := img.Stride >> 2
if pf == FourccRgbp {
stride = rgba.Stride / 2
stride = img.Stride >> 1
}

buf := *yuv.pool.Get().(*[]byte)

Y420(rgba.Pix, buf, x, y, stride, dx, dy, rot, pf, cx, cy)
Y420(img.Data, buf, img.W, img.H, stride, dx, dy, rot, pf, cx, cy)
return buf
}

Expand Down
204 changes: 106 additions & 98 deletions pkg/encoder/yuv/yuv_test.go

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions pkg/worker/caged/app/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package app

import "image"

type App interface {
AudioSampleRate() int
Init() error
Expand All @@ -20,6 +18,12 @@ type Audio struct {
}

type Video struct {
Frame image.RGBA
Frame RawFrame
Duration int32
}

type RawFrame struct {
Data []byte
Stride int
W, H int
}
42 changes: 42 additions & 0 deletions pkg/worker/caged/libretro/canvas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package libretro

import "sync"

// Canvas is a stateful drawing surface
type Canvas struct {
w, h int
pool sync.Pool

Disabled bool
}

type Frame struct {
Data []byte
Stride int
W, H int
}

func NewCanvas(w, h, size int) *Canvas {
return &Canvas{w: w, h: h,
pool: sync.Pool{New: func() any { return &Frame{Data: make([]byte, size<<2)} }}}
}

func (c *Canvas) Get(w, h, stride int, data []byte) Frame {
i := c.pool.Get().(*Frame)
i.Stride = stride
i.W = w
i.H = h
l := stride * h
if data != nil {
i.Data = data
} else if len(i.Data) != l {
i.Data = i.Data[:l]
}
return *i
}

func (c *Canvas) Put(i *Frame) {
if !c.Disabled {
c.pool.Put(i)
}
}
Loading

0 comments on commit 847c9e2

Please sign in to comment.