From fa28197390eb314dbd1c83ccaca3da8957538b60 Mon Sep 17 00:00:00 2001 From: sergystepanov Date: Sat, 27 Feb 2021 18:09:55 +0300 Subject: [PATCH] Refactor and update VPX codec (#285) * Refactor and update VPX codec * Add VPX codec options support * Add generic video encoding pipe * Tweak in/out channels for the pipe * Update YUV encoder * Clean VP8 encoder --- configs/config.yaml | 8 +- pkg/config/encoder/config.go | 4 + pkg/encoder/h264/encoder.go | 95 ---- pkg/encoder/h264/x264.go | 18 +- pkg/encoder/pipe.go | 60 +++ pkg/encoder/type.go | 5 +- pkg/encoder/vpx/encoder.go | 182 ------- pkg/encoder/vpx/libvpx.go | 156 ++++++ pkg/encoder/vpx/msvc.h | 27 - pkg/encoder/vpx/options.go | 17 + pkg/encoder/vpx/tools_common.c | 692 -------------------------- pkg/encoder/vpx/tools_common.h | 150 ------ pkg/encoder/vpx/vpx_config.h | 80 --- pkg/{util/codec.go => encoder/yuv.go} | 37 +- pkg/worker/room/media.go | 17 +- pkg/worker/room/media_test.go | 23 +- pkg/worker/room/room.go | 2 +- pkg/worker/room/room_test.go | 10 +- 18 files changed, 298 insertions(+), 1285 deletions(-) delete mode 100644 pkg/encoder/h264/encoder.go create mode 100644 pkg/encoder/pipe.go delete mode 100644 pkg/encoder/vpx/encoder.go create mode 100644 pkg/encoder/vpx/libvpx.go delete mode 100644 pkg/encoder/vpx/msvc.h create mode 100644 pkg/encoder/vpx/options.go delete mode 100644 pkg/encoder/vpx/tools_common.c delete mode 100644 pkg/encoder/vpx/tools_common.h delete mode 100644 pkg/encoder/vpx/vpx_config.h rename pkg/{util/codec.go => encoder/yuv.go} (63%) diff --git a/configs/config.yaml b/configs/config.yaml index 660c3c5f..ef51aac5 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -151,7 +151,7 @@ encoder: frame: 20 frequency: 48000 video: - # h264, vpx + # h264, vpx (VP8) codec: vpx # see: https://trac.ffmpeg.org/wiki/Encode/H.264 h264: @@ -165,6 +165,12 @@ encoder: tune: zerolatency # 0-3 logLevel: 0 + # see: https://www.webmproject.org/docs/encoder-parameters + vpx: + # target bitrate (KBit/s) + bitrate: 1200 + # force keyframe interval + keyframeInterval: 5 # run without a game # (experimental) withoutGame: false diff --git a/pkg/config/encoder/config.go b/pkg/config/encoder/config.go index cc023022..3a782597 100644 --- a/pkg/config/encoder/config.go +++ b/pkg/config/encoder/config.go @@ -21,6 +21,10 @@ type Video struct { Tune string LogLevel int } + Vpx struct { + Bitrate uint + KeyframeInterval uint + } } func (a *Audio) GetFrameDuration() int { diff --git a/pkg/encoder/h264/encoder.go b/pkg/encoder/h264/encoder.go deleted file mode 100644 index 0b8896d1..00000000 --- a/pkg/encoder/h264/encoder.go +++ /dev/null @@ -1,95 +0,0 @@ -package h264 - -import ( - "bytes" - "log" - "runtime/debug" - - "github.com/giongto35/cloud-game/v2/pkg/encoder" - "github.com/giongto35/cloud-game/v2/pkg/util" -) - -// Encoder converts a yuvI420 image to h264 frame. -type Encoder struct { - Output chan encoder.OutFrame - Input chan encoder.InFrame - done chan struct{} - - buf *bytes.Buffer - enc *H264 - - // C - width int - height int - fps int -} - -// NewEncoder creates h264 encoder -func NewEncoder(width, height int, options ...Option) (encoder.Encoder, error) { - enc := &Encoder{ - Output: make(chan encoder.OutFrame, 10), - Input: make(chan encoder.InFrame, 2), - done: make(chan struct{}), - - buf: bytes.NewBuffer(make([]byte, 0)), - width: width, - height: height, - } - - if err := enc.init(options...); err != nil { - return nil, err - } - - return enc, nil -} - -func (e *Encoder) init(options ...Option) error { - enc, err := NewH264Encoder(e.buf, e.width, e.height, options...) - if err != nil { - panic(err) - } - e.enc = enc - - go e.startLooping() - return nil -} - -func (e *Encoder) startLooping() { - defer func() { - if r := recover(); r != nil { - log.Println("Warn: Recovered panic in encoding ", r) - log.Println(debug.Stack()) - } - }() - - size := int(float32(e.width*e.height) * 1.5) - yuv := make([]byte, size, size) - - for img := range e.Input { - util.RgbaToYuvInplace(img.Image, yuv, e.width, e.height) - err := e.enc.Encode(yuv) - if err != nil { - log.Println("err encoding ", img.Image, " using h264") - } - e.Output <- encoder.OutFrame{Data: e.buf.Bytes(), Timestamp: img.Timestamp} - e.buf.Reset() - } - close(e.Output) - close(e.done) -} - -// Release release memory and stop loop -func (e *Encoder) release() { - close(e.Input) - <-e.done - err := e.enc.Close() - if err != nil { - log.Println("Failed to close H264 encoder") - } -} - -func (e *Encoder) GetInputChan() chan encoder.InFrame { return e.Input } - -func (e *Encoder) GetOutputChan() chan encoder.OutFrame { return e.Output } - -func (e *Encoder) Stop() { e.release() } diff --git a/pkg/encoder/h264/x264.go b/pkg/encoder/h264/x264.go index 7efea860..7af504b5 100644 --- a/pkg/encoder/h264/x264.go +++ b/pkg/encoder/h264/x264.go @@ -4,7 +4,6 @@ package h264 import "C" import ( "fmt" - "io" "log" x264 "github.com/sergystepanov/x264-go/v2/x264c/external" @@ -12,7 +11,6 @@ import ( type H264 struct { ref *x264.T - w io.Writer width int32 lumaSize int32 @@ -25,7 +23,7 @@ type H264 struct { pts int64 } -func NewH264Encoder(w io.Writer, width, height int, options ...Option) (encoder *H264, err error) { +func NewEncoder(width, height int, options ...Option) (encoder *H264, err error) { libVersion := int(x264.Build) if libVersion < 150 { @@ -82,7 +80,6 @@ func NewH264Encoder(w io.Writer, width, height int, options ...Option) (encoder lumaSize: int32(width * height), chromaSize: int32(width*height) / 4, nals: make([]*x264.Nal, 1), - w: w, width: int32(width), } @@ -93,15 +90,10 @@ func NewH264Encoder(w io.Writer, width, height int, options ...Option) (encoder err = fmt.Errorf("x264: cannot open the encoder") return } - - if ret := x264.EncoderHeaders(encoder.ref, encoder.nals, &encoder.nnals); ret > 0 { - _, err = encoder.w.Write(C.GoBytes(encoder.nals[0].PPayload, C.int(ret))) - } - return } -func (e *H264) Encode(yuv []byte) (err error) { +func (e *H264) Encode(yuv []byte) []byte { var picIn, picOut x264.Picture picIn.Img.ICsp = e.csp @@ -124,13 +116,13 @@ func (e *H264) Encode(yuv []byte) (err error) { }() if ret := x264.EncoderEncode(e.ref, e.nals, &e.nnals, &picIn, &picOut); ret > 0 { - _, err = e.w.Write(C.GoBytes(e.nals[0].PPayload, C.int(ret))) + return C.GoBytes(e.nals[0].PPayload, C.int(ret)) // ret should be equal to writer writes } - return + return []byte{} } -func (e *H264) Close() error { +func (e *H264) Shutdown() error { x264.EncoderClose(e.ref) return nil } diff --git a/pkg/encoder/pipe.go b/pkg/encoder/pipe.go new file mode 100644 index 00000000..1e815e4f --- /dev/null +++ b/pkg/encoder/pipe.go @@ -0,0 +1,60 @@ +package encoder + +import "log" + +type VideoPipe struct { + Input chan InFrame + Output chan OutFrame + done chan struct{} + + encoder Encoder + + // frame size + w, h int +} + +// NewVideoPipe returns new video encoder pipe. +// By default it waits for RGBA images on the input channel, +// converts them into YUV I420 format, +// encodes with provided video encoder, and +// puts the result into the output channel. +func NewVideoPipe(enc Encoder, w, h int) *VideoPipe { + return &VideoPipe{ + Input: make(chan InFrame, 1), + Output: make(chan OutFrame, 2), + done: make(chan struct{}), + + encoder: enc, + + w: w, + h: h, + } +} + +// Start begins video encoding pipe. +// Should be wrapped into a goroutine. +func (vp *VideoPipe) Start() { + defer func() { + if r := recover(); r != nil { + log.Println("Warn: Recovered panic in encoding ", r) + } + }() + + yuv := NewYuvBuffer(vp.w, vp.h) + for img := range vp.Input { + frame := vp.encoder.Encode(yuv.FromRGBA(img.Image).data) + if len(frame) > 0 { + vp.Output <- OutFrame{Data: frame, Timestamp: img.Timestamp} + } + } + close(vp.Output) + close(vp.done) +} + +func (vp *VideoPipe) Stop() { + close(vp.Input) + <-vp.done + if err := vp.encoder.Shutdown(); err != nil { + log.Println("error: failed to close the encoder") + } +} diff --git a/pkg/encoder/type.go b/pkg/encoder/type.go index 1720a868..83696959 100644 --- a/pkg/encoder/type.go +++ b/pkg/encoder/type.go @@ -13,7 +13,6 @@ type OutFrame struct { } type Encoder interface { - GetInputChan() chan InFrame - GetOutputChan() chan OutFrame - Stop() + Encode(input []byte) []byte + Shutdown() error } diff --git a/pkg/encoder/vpx/encoder.go b/pkg/encoder/vpx/encoder.go deleted file mode 100644 index 50c70d67..00000000 --- a/pkg/encoder/vpx/encoder.go +++ /dev/null @@ -1,182 +0,0 @@ -package vpx - -import ( - "fmt" - "log" - "unsafe" - - "github.com/giongto35/cloud-game/v2/pkg/encoder" - "github.com/giongto35/cloud-game/v2/pkg/util" -) - -// https://chromium.googlesource.com/webm/libvpx/+/master/examples/simple_encoder.c - -/* -#cgo pkg-config: vpx -#include -#include "vpx/vpx_encoder.h" -#include "tools_common.h" - -typedef struct GoBytes { - void *bs; - int size; -} GoBytesType; - -vpx_codec_err_t call_vpx_codec_enc_config_default(const VpxInterface *encoder, vpx_codec_enc_cfg_t *cfg) { - return vpx_codec_enc_config_default(encoder->codec_interface(), cfg, 0); -} -vpx_codec_err_t call_vpx_codec_enc_init(vpx_codec_ctx_t *codec, const VpxInterface *encoder, vpx_codec_enc_cfg_t *cfg) { - return vpx_codec_enc_init(codec, encoder->codec_interface(), cfg, 0); -} -GoBytesType get_frame_buffer(vpx_codec_ctx_t *codec, vpx_codec_iter_t *iter) { - // iter has set to NULL when after add new image - GoBytesType bytes = {NULL, 0}; - const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(codec, iter); - if (pkt != NULL && pkt->kind == VPX_CODEC_CX_FRAME_PKT) { - bytes.bs = pkt->data.frame.buf; - bytes.size = pkt->data.frame.sz; - } - return bytes; -} -*/ -import "C" - -const chanSize = 2 - -// VpxEncoder yuvI420 image to vp8 video -type VpxEncoder struct { - Output chan encoder.OutFrame - Input chan encoder.InFrame - done chan struct{} - - width int - height int - // C - fps C.int - bitrate C.uint - keyFrameInterval C.int - frameCount C.int - vpxCodexCtx C.vpx_codec_ctx_t - vpxImage C.vpx_image_t - vpxCodexIter C.vpx_codec_iter_t -} - -// NewEncoder create vp8 encoder -func NewEncoder(w, h, fps, bitrate, keyframe int) (encoder.Encoder, error) { - v := &VpxEncoder{ - Output: make(chan encoder.OutFrame, 5*chanSize), - Input: make(chan encoder.InFrame, chanSize), - done: make(chan struct{}), - - // C - width: w, - height: h, - fps: C.int(fps), - bitrate: C.uint(bitrate), - keyFrameInterval: C.int(keyframe), - frameCount: C.int(0), - } - - if err := v.init(); err != nil { - return nil, err - } - - return v, nil -} - -func (v *VpxEncoder) init() error { - v.frameCount = 0 - - codecName := C.CString("vp8") - encoder := C.get_vpx_encoder_by_name(codecName) - C.free(unsafe.Pointer(codecName)) - - if encoder == nil { - return fmt.Errorf("get_vpx_encoder_by_name failed") - } - if C.vpx_img_alloc(&v.vpxImage, C.VPX_IMG_FMT_I420, C.uint(v.width), C.uint(v.height), 0) == nil { - return fmt.Errorf("vpx_img_alloc failed") - } - - var cfg C.vpx_codec_enc_cfg_t - if C.call_vpx_codec_enc_config_default(encoder, &cfg) != 0 { - return fmt.Errorf("Failed to get default codec config") - } - cfg.g_w = C.uint(v.width) - cfg.g_h = C.uint(v.height) - cfg.g_timebase.num = 1 - cfg.g_timebase.den = v.fps - cfg.rc_target_bitrate = v.bitrate - cfg.g_error_resilient = 1 - - if C.call_vpx_codec_enc_init(&v.vpxCodexCtx, encoder, &cfg) != 0 { - return fmt.Errorf("Failed to initialize encoder") - } - go v.startLooping() - return nil -} - -func (v *VpxEncoder) startLooping() { - defer func() { - if r := recover(); r != nil { - log.Println("Warn: Recovered panic in encoding ", r) - } - }() - - size := int(float32(v.width*v.height) * 1.5) - yuv := make([]byte, size, size) - - for img := range v.Input { - util.RgbaToYuvInplace(img.Image, yuv, v.width, v.height) - - // Add Image - v.vpxCodexIter = nil - C.vpx_img_read(&v.vpxImage, unsafe.Pointer(&yuv[0])) - - var flags C.int - if v.keyFrameInterval > 0 && v.frameCount%v.keyFrameInterval == 0 { - flags |= C.VPX_EFLAG_FORCE_KF - } - if C.vpx_codec_encode(&v.vpxCodexCtx, &v.vpxImage, C.vpx_codec_pts_t(v.frameCount), 1, C.vpx_enc_frame_flags_t(flags), C.VPX_DL_REALTIME) != 0 { - fmt.Println("Failed to encode frame") - } - v.frameCount++ - - // Get Frame - goBytes := C.get_frame_buffer(&v.vpxCodexCtx, &v.vpxCodexIter) - if goBytes.bs == nil { - continue - } - bs := C.GoBytes(goBytes.bs, goBytes.size) - // if buffer is full skip frame - if len(v.Output) >= cap(v.Output) { - continue - } - v.Output <- encoder.OutFrame{Data: bs, Timestamp: img.Timestamp} - } - close(v.Output) - close(v.done) -} - -// Release release memory and stop loop -func (v *VpxEncoder) release() { - close(v.Input) - <-v.done - C.vpx_img_free(&v.vpxImage) - C.vpx_codec_destroy(&v.vpxCodexCtx) -} - -// GetInputChan returns input channel -func (v *VpxEncoder) GetInputChan() chan encoder.InFrame { - return v.Input -} - -// GetInputChan returns output channel -func (v *VpxEncoder) GetOutputChan() chan encoder.OutFrame { - return v.Output -} - -// GetDoneChan returns done channel -func (v *VpxEncoder) Stop() { - v.release() -} diff --git a/pkg/encoder/vpx/libvpx.go b/pkg/encoder/vpx/libvpx.go new file mode 100644 index 00000000..683cad09 --- /dev/null +++ b/pkg/encoder/vpx/libvpx.go @@ -0,0 +1,156 @@ +package vpx + +/* +#cgo pkg-config: vpx + +#include "vpx/vpx_encoder.h" +#include "vpx/vpx_image.h" +#include "vpx/vp8cx.h" + +#include +#include + +#define VP8_FOURCC 0x30385056 + +typedef struct VpxInterface { + const char *const name; + const uint32_t fourcc; + vpx_codec_iface_t *(*const codec_interface)(); +} VpxInterface; + +typedef struct FrameBuffer { + void *ptr; + int size; +} FrameBuffer; + +vpx_codec_err_t call_vpx_codec_enc_config_default(const VpxInterface *encoder, vpx_codec_enc_cfg_t *cfg) { + return vpx_codec_enc_config_default(encoder->codec_interface(), cfg, 0); +} +vpx_codec_err_t call_vpx_codec_enc_init(vpx_codec_ctx_t *codec, const VpxInterface *encoder, vpx_codec_enc_cfg_t *cfg) { + return vpx_codec_enc_init(codec, encoder->codec_interface(), cfg, 0); +} + +FrameBuffer get_frame_buffer(vpx_codec_ctx_t *codec, vpx_codec_iter_t *iter) { + // iter has set to NULL when after add new image + FrameBuffer fb = {NULL, 0}; + const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(codec, iter); + if (pkt != NULL && pkt->kind == VPX_CODEC_CX_FRAME_PKT) { + fb.ptr = pkt->data.frame.buf; + fb.size = pkt->data.frame.sz; + } + return fb; +} + +const VpxInterface vpx_encoders[] = {{ "vp8", VP8_FOURCC, &vpx_codec_vp8_cx }}; + +int vpx_img_plane_width(const vpx_image_t *img, int plane) { + if (plane > 0 && img->x_chroma_shift > 0) + return (img->d_w + 1) >> img->x_chroma_shift; + else + return img->d_w; +} + +int vpx_img_plane_height(const vpx_image_t *img, int plane) { + if (plane > 0 && img->y_chroma_shift > 0) + return (img->d_h + 1) >> img->y_chroma_shift; + else + return img->d_h; +} + +void vpx_img_read(vpx_image_t *dst, void *src) { + for (int plane = 0; plane < 3; ++plane) { + unsigned char *buf = dst->planes[plane]; + const int stride = dst->stride[plane]; + const int w = vpx_img_plane_width(dst, plane); + const int h = vpx_img_plane_height(dst, plane); + + for (int y = 0; y < h; ++y) { + memcpy(buf, src, w); + buf += stride; + src += w; + } + } +} +*/ +import "C" +import ( + "fmt" + "unsafe" +) + +type Vpx struct { + frameCount C.int + image C.vpx_image_t + codecCtx C.vpx_codec_ctx_t + codecIter C.vpx_codec_iter_t + + kfi C.int +} + +func NewEncoder(width, height int, options ...Option) (*Vpx, error) { + encoder := &C.vpx_encoders[0] + if encoder == nil { + return nil, fmt.Errorf("couldn't get the encoder") + } + + opts := &Options{ + Bitrate: 1200, + KeyframeInt: 5, + } + + for _, opt := range options { + opt(opts) + } + + vpx := Vpx{ + frameCount: C.int(0), + kfi: C.int(opts.KeyframeInt), + } + + if C.vpx_img_alloc(&vpx.image, C.VPX_IMG_FMT_I420, C.uint(width), C.uint(height), 1) == nil { + return nil, fmt.Errorf("vpx_img_alloc failed") + } + + var cfg C.vpx_codec_enc_cfg_t + if C.call_vpx_codec_enc_config_default(encoder, &cfg) != 0 { + return nil, fmt.Errorf("failed to get default codec config") + } + + cfg.g_w = C.uint(width) + cfg.g_h = C.uint(height) + cfg.rc_target_bitrate = C.uint(opts.Bitrate) + cfg.g_error_resilient = 1 + + if C.call_vpx_codec_enc_init(&vpx.codecCtx, encoder, &cfg) != 0 { + return nil, fmt.Errorf("failed to initialize encoder") + } + + return &vpx, nil +} + +// see: https://chromium.googlesource.com/webm/libvpx/+/master/examples/simple_encoder.c +func (vpx *Vpx) Encode(yuv []byte) []byte { + vpx.codecIter = nil + C.vpx_img_read(&vpx.image, unsafe.Pointer(&yuv[0])) + + var flags C.int + if vpx.kfi > 0 && vpx.frameCount%vpx.kfi == 0 { + flags |= C.VPX_EFLAG_FORCE_KF + } + if C.vpx_codec_encode(&vpx.codecCtx, &vpx.image, C.vpx_codec_pts_t(vpx.frameCount), 1, C.vpx_enc_frame_flags_t(flags), C.VPX_DL_REALTIME) != 0 { + fmt.Println("Failed to encode frame") + } + vpx.frameCount++ + + fb := C.get_frame_buffer(&vpx.codecCtx, &vpx.codecIter) + if fb.ptr == nil { + return []byte{} + } + return C.GoBytes(fb.ptr, fb.size) +} + +func (vpx *Vpx) Shutdown() error { + C.vpx_img_free(&vpx.image) + C.vpx_codec_destroy(&vpx.codecCtx) + return nil +} diff --git a/pkg/encoder/vpx/msvc.h b/pkg/encoder/vpx/msvc.h deleted file mode 100644 index bc0057b4..00000000 --- a/pkg/encoder/vpx/msvc.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2015 The WebM project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ -#ifndef VPX_VPX_PORTS_MSVC_H_ -#define VPX_VPX_PORTS_MSVC_H_ -#ifdef _MSC_VER -#include "./vpx_config.h" -#if _MSC_VER < 1900 // VS2015 provides snprintf -#define snprintf _snprintf -#endif // _MSC_VER < 1900 -#if _MSC_VER < 1800 // VS2013 provides round -#include -static INLINE double round(double x) { - if (x < 0) - return ceil(x - 0.5); - else - return floor(x + 0.5); -} -#endif // _MSC_VER < 1800 -#endif // _MSC_VER -#endif // VPX_VPX_PORTS_MSVC_H_ \ No newline at end of file diff --git a/pkg/encoder/vpx/options.go b/pkg/encoder/vpx/options.go new file mode 100644 index 00000000..e582985b --- /dev/null +++ b/pkg/encoder/vpx/options.go @@ -0,0 +1,17 @@ +package vpx + +type Options struct { + // Target bandwidth to use for this stream, in kilobits per second. + Bitrate uint + // Force keyframe interval. + KeyframeInt uint +} + +type Option func(*Options) + +func WithOptions(arg Options) Option { + return func(args *Options) { + args.Bitrate = arg.Bitrate + args.KeyframeInt = arg.KeyframeInt + } +} diff --git a/pkg/encoder/vpx/tools_common.c b/pkg/encoder/vpx/tools_common.c deleted file mode 100644 index 36dfe480..00000000 --- a/pkg/encoder/vpx/tools_common.c +++ /dev/null @@ -1,692 +0,0 @@ -/* - * Copyright (c) 2010 The WebM project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ -#include -#include -#include -#include -#include -#include "./tools_common.h" -#if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER -#include "vpx/vp8cx.h" -#endif -#if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER -#include "vpx/vp8dx.h" -#endif -#if defined(_WIN32) || defined(__OS2__) -#include -#include -#ifdef __OS2__ -#define _setmode setmode -#define _fileno fileno -#define _O_BINARY O_BINARY -#endif -#endif -#define LOG_ERROR(label) \ - do { \ - const char *l = label; \ - va_list ap; \ - va_start(ap, fmt); \ - if (l) fprintf(stderr, "%s: ", l); \ - vfprintf(stderr, fmt, ap); \ - fprintf(stderr, "\n"); \ - va_end(ap); \ - } while (0) -#if CONFIG_ENCODERS -/* Swallow warnings about unused results of fread/fwrite */ -static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) { - return fread(ptr, size, nmemb, stream); -} -#define fread wrap_fread -#endif -FILE *set_binary_mode(FILE *stream) { - (void)stream; -#if defined(_WIN32) || defined(__OS2__) - _setmode(_fileno(stream), _O_BINARY); -#endif - return stream; -} -void die(const char *fmt, ...) { - LOG_ERROR(NULL); - // usage_exit(); - exit(EXIT_FAILURE); -} -void fatal(const char *fmt, ...) { - LOG_ERROR("Fatal"); - exit(EXIT_FAILURE); -} -void warn(const char *fmt, ...) { LOG_ERROR("Warning"); } -void die_codec(vpx_codec_ctx_t *ctx, const char *s) { - const char *detail = vpx_codec_error_detail(ctx); - printf("%s: %s\n", s, vpx_codec_error(ctx)); - if (detail) printf(" %s\n", detail); - exit(EXIT_FAILURE); -} -int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) { - FILE *f = input_ctx->file; - struct FileTypeDetectionBuffer *detect = &input_ctx->detect; - int plane = 0; - int shortread = 0; - const int bytespp = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1; - for (plane = 0; plane < 3; ++plane) { - uint8_t *ptr; - const int w = vpx_img_plane_width(yuv_frame, plane); - const int h = vpx_img_plane_height(yuv_frame, plane); - int r; - /* Determine the correct plane based on the image format. The for-loop - * always counts in Y,U,V order, but this may not match the order of - * the data on disk. - */ - switch (plane) { - case 1: - ptr = - yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V - : VPX_PLANE_U]; - break; - case 2: - ptr = - yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U - : VPX_PLANE_V]; - break; - default: ptr = yuv_frame->planes[plane]; - } - for (r = 0; r < h; ++r) { - size_t needed = w * bytespp; - size_t buf_position = 0; - const size_t left = detect->buf_read - detect->position; - if (left > 0) { - const size_t more = (left < needed) ? left : needed; - memcpy(ptr, detect->buf + detect->position, more); - buf_position = more; - needed -= more; - detect->position += more; - } - if (needed > 0) { - shortread |= (fread(ptr + buf_position, 1, needed, f) < needed); - } - ptr += yuv_frame->stride[plane]; - } - } - return shortread; -} -static const VpxInterface vpx_encoders[] = { -#if CONFIG_VP8_ENCODER - { "vp8", VP8_FOURCC, &vpx_codec_vp8_cx }, -#endif -#if CONFIG_VP9_ENCODER - { "vp9", VP9_FOURCC, &vpx_codec_vp9_cx }, -#endif -}; - -int get_vpx_encoder_count(void) { - return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]); -} -const VpxInterface *get_vpx_encoder_by_index(int i) { return &vpx_encoders[i]; } -const VpxInterface *get_vpx_encoder_by_name(const char *name) { - int i; - for (i = 0; i < get_vpx_encoder_count(); ++i) { - const VpxInterface *encoder = get_vpx_encoder_by_index(i); - if (strcmp(encoder->name, name) == 0) return encoder; - } - return NULL; -} -#if CONFIG_DECODERS -static const VpxInterface vpx_decoders[] = { -#if CONFIG_VP8_DECODER - { "vp8", VP8_FOURCC, &vpx_codec_vp8_dx }, -#endif -#if CONFIG_VP9_DECODER - { "vp9", VP9_FOURCC, &vpx_codec_vp9_dx }, -#endif -}; -int get_vpx_decoder_count(void) { - return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]); -} -const VpxInterface *get_vpx_decoder_by_index(int i) { return &vpx_decoders[i]; } -const VpxInterface *get_vpx_decoder_by_name(const char *name) { - int i; - for (i = 0; i < get_vpx_decoder_count(); ++i) { - const VpxInterface *const decoder = get_vpx_decoder_by_index(i); - if (strcmp(decoder->name, name) == 0) return decoder; - } - return NULL; -} -const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) { - int i; - for (i = 0; i < get_vpx_decoder_count(); ++i) { - const VpxInterface *const decoder = get_vpx_decoder_by_index(i); - if (decoder->fourcc == fourcc) return decoder; - } - return NULL; -} -#endif // CONFIG_DECODERS -int vpx_img_plane_width(const vpx_image_t *img, int plane) { - if (plane > 0 && img->x_chroma_shift > 0) - return (img->d_w + 1) >> img->x_chroma_shift; - else - return img->d_w; -} -int vpx_img_plane_height(const vpx_image_t *img, int plane) { - if (plane > 0 && img->y_chroma_shift > 0) - return (img->d_h + 1) >> img->y_chroma_shift; - else - return img->d_h; -} -void vpx_img_write(const vpx_image_t *img, FILE *file) { - int plane; - for (plane = 0; plane < 3; ++plane) { - const unsigned char *buf = img->planes[plane]; - const int stride = img->stride[plane]; - const int w = vpx_img_plane_width(img, plane) * - ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); - const int h = vpx_img_plane_height(img, plane); - int y; - for (y = 0; y < h; ++y) { - fwrite(buf, 1, w, file); - buf += stride; - } - } -} - -int vpx_img_read(vpx_image_t *img, void *bs) { - int plane; - for (plane = 0; plane < 3; ++plane) { - unsigned char *buf = img->planes[plane]; - const int stride = img->stride[plane]; - const int w = vpx_img_plane_width(img, plane) * - ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); - const int h = vpx_img_plane_height(img, plane); - int y; - for (y = 0; y < h; ++y) { - memcpy(buf, bs, w); - // if (fread(buf, 1, w, file) != (size_t)w) return 0; - buf += stride; - bs += w; - } - } - return 1; -} -// TODO(dkovalev) change sse_to_psnr signature: double -> int64_t -double sse_to_psnr(double samples, double peak, double sse) { - static const double kMaxPSNR = 100.0; - if (sse > 0.0) { - const double psnr = 10.0 * log10(samples * peak * peak / sse); - return psnr > kMaxPSNR ? kMaxPSNR : psnr; - } else { - return kMaxPSNR; - } -} -#if CONFIG_ENCODERS -int read_frame(struct VpxInputContext *input_ctx, vpx_image_t *img) { - FILE *f = input_ctx->file; - - y4m_input *y4m = &input_ctx->y4m; - int shortread = 0; - if (input_ctx->file_type == FILE_TYPE_Y4M) { - if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0; - } else { - shortread = read_yuv_frame(input_ctx, img); - } - return !shortread; -} -int file_is_y4m(const char detect[4]) { - if (memcmp(detect, "YUV4", 4) == 0) { - return 1; - } - return 0; -} -int fourcc_is_ivf(const char detect[4]) { - if (memcmp(detect, "DKIF", 4) == 0) { - return 1; - } - return 0; -} -void open_input_file(struct VpxInputContext *input) { - /* Parse certain options from the input file, if possible */ - input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb") - : set_binary_mode(stdin); - if (!input->file) fatal("Failed to open input file"); - if (!fseeko(input->file, 0, SEEK_END)) { - /* Input file is seekable. Figure out how long it is, so we can get - * progress info. - */ - input->length = ftello(input->file); - rewind(input->file); - } - /* Default to 1:1 pixel aspect ratio. */ - input->pixel_aspect_ratio.numerator = 1; - input->pixel_aspect_ratio.denominator = 1; - /* For RAW input sources, these bytes will applied on the first frame - * in read_frame(). - */ - input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file); - input->detect.position = 0; - if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) { - if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, - input->only_i420) >= 0) { - input->file_type = FILE_TYPE_Y4M; - input->width = input->y4m.pic_w; - input->height = input->y4m.pic_h; - input->pixel_aspect_ratio.numerator = input->y4m.par_n; - input->pixel_aspect_ratio.denominator = input->y4m.par_d; - input->framerate.numerator = input->y4m.fps_n; - input->framerate.denominator = input->y4m.fps_d; - input->fmt = input->y4m.vpx_fmt; - input->bit_depth = input->y4m.bit_depth; - } else { - fatal("Unsupported Y4M stream."); - } - } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) { - fatal("IVF is not supported as input."); - } else { - input->file_type = FILE_TYPE_RAW; - } -} -void close_input_file(struct VpxInputContext *input) { - fclose(input->file); - if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m); -} -#endif -// TODO(debargha): Consolidate the functions below into a separate file. -#if CONFIG_VP9_HIGHBITDEPTH -static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src, - int input_shift) { - // Note the offset is 1 less than half. - const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0; - int plane; - if (dst->d_w != src->d_w || dst->d_h != src->d_h || - dst->x_chroma_shift != src->x_chroma_shift || - dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt || - input_shift < 0) { - fatal("Unsupported image conversion"); - } - switch (src->fmt) { - case VPX_IMG_FMT_I42016: - case VPX_IMG_FMT_I42216: - case VPX_IMG_FMT_I44416: - case VPX_IMG_FMT_I44016: break; - default: fatal("Unsupported image conversion"); break; - } - for (plane = 0; plane < 3; plane++) { - int w = src->d_w; - int h = src->d_h; - int x, y; - if (plane) { - w = (w + src->x_chroma_shift) >> src->x_chroma_shift; - h = (h + src->y_chroma_shift) >> src->y_chroma_shift; - } - for (y = 0; y < h; y++) { - uint16_t *p_src = - (uint16_t *)(src->planes[plane] + y * src->stride[plane]); - uint16_t *p_dst = - (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]); - for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset; - } - } -} -static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src, - int input_shift) { - // Note the offset is 1 less than half. - const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0; - int plane; - if (dst->d_w != src->d_w || dst->d_h != src->d_h || - dst->x_chroma_shift != src->x_chroma_shift || - dst->y_chroma_shift != src->y_chroma_shift || - dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH || input_shift < 0) { - fatal("Unsupported image conversion"); - } - switch (src->fmt) { - case VPX_IMG_FMT_I420: - case VPX_IMG_FMT_I422: - case VPX_IMG_FMT_I444: - case VPX_IMG_FMT_I440: break; - default: fatal("Unsupported image conversion"); break; - } - for (plane = 0; plane < 3; plane++) { - int w = src->d_w; - int h = src->d_h; - int x, y; - if (plane) { - w = (w + src->x_chroma_shift) >> src->x_chroma_shift; - h = (h + src->y_chroma_shift) >> src->y_chroma_shift; - } - for (y = 0; y < h; y++) { - uint8_t *p_src = src->planes[plane] + y * src->stride[plane]; - uint16_t *p_dst = - (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]); - for (x = 0; x < w; x++) { - *p_dst++ = (*p_src++ << input_shift) + offset; - } - } - } -} -void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src, int input_shift) { - if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { - highbd_img_upshift(dst, src, input_shift); - } else { - lowbd_img_upshift(dst, src, input_shift); - } -} -void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) { - int plane; - if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w || - dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift || - dst->y_chroma_shift != src->y_chroma_shift) { - fatal("Unsupported image conversion"); - } - switch (dst->fmt) { - case VPX_IMG_FMT_I420: - case VPX_IMG_FMT_I422: - case VPX_IMG_FMT_I444: - case VPX_IMG_FMT_I440: break; - default: fatal("Unsupported image conversion"); break; - } - for (plane = 0; plane < 3; plane++) { - int w = src->d_w; - int h = src->d_h; - int x, y; - if (plane) { - w = (w + src->x_chroma_shift) >> src->x_chroma_shift; - h = (h + src->y_chroma_shift) >> src->y_chroma_shift; - } - for (y = 0; y < h; y++) { - uint16_t *p_src = - (uint16_t *)(src->planes[plane] + y * src->stride[plane]); - uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane]; - for (x = 0; x < w; x++) { - *p_dst++ = (uint8_t)(*p_src++); - } - } - } -} -static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src, - int down_shift) { - int plane; - if (dst->d_w != src->d_w || dst->d_h != src->d_h || - dst->x_chroma_shift != src->x_chroma_shift || - dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt || - down_shift < 0) { - fatal("Unsupported image conversion"); - } - switch (src->fmt) { - case VPX_IMG_FMT_I42016: - case VPX_IMG_FMT_I42216: - case VPX_IMG_FMT_I44416: - case VPX_IMG_FMT_I44016: break; - default: fatal("Unsupported image conversion"); break; - } - for (plane = 0; plane < 3; plane++) { - int w = src->d_w; - int h = src->d_h; - int x, y; - if (plane) { - w = (w + src->x_chroma_shift) >> src->x_chroma_shift; - h = (h + src->y_chroma_shift) >> src->y_chroma_shift; - } - for (y = 0; y < h; y++) { - uint16_t *p_src = - (uint16_t *)(src->planes[plane] + y * src->stride[plane]); - uint16_t *p_dst = - (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]); - for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift; - } - } -} -static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src, - int down_shift) { - int plane; - if (dst->d_w != src->d_w || dst->d_h != src->d_h || - dst->x_chroma_shift != src->x_chroma_shift || - dst->y_chroma_shift != src->y_chroma_shift || - src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH || down_shift < 0) { - fatal("Unsupported image conversion"); - } - switch (dst->fmt) { - case VPX_IMG_FMT_I420: - case VPX_IMG_FMT_I422: - case VPX_IMG_FMT_I444: - case VPX_IMG_FMT_I440: break; - default: fatal("Unsupported image conversion"); break; - } - for (plane = 0; plane < 3; plane++) { - int w = src->d_w; - int h = src->d_h; - int x, y; - if (plane) { - w = (w + src->x_chroma_shift) >> src->x_chroma_shift; - h = (h + src->y_chroma_shift) >> src->y_chroma_shift; - } - for (y = 0; y < h; y++) { - uint16_t *p_src = - (uint16_t *)(src->planes[plane] + y * src->stride[plane]); - uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane]; - for (x = 0; x < w; x++) { - *p_dst++ = *p_src++ >> down_shift; - } - } - } -} -void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src, int down_shift) { - if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { - highbd_img_downshift(dst, src, down_shift); - } else { - lowbd_img_downshift(dst, src, down_shift); - } -} -#endif // CONFIG_VP9_HIGHBITDEPTH -int compare_img(const vpx_image_t *const img1, const vpx_image_t *const img2) { - uint32_t l_w = img1->d_w; - uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; - const uint32_t c_h = - (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; - uint32_t i; - int match = 1; - match &= (img1->fmt == img2->fmt); - match &= (img1->d_w == img2->d_w); - match &= (img1->d_h == img2->d_h); -#if CONFIG_VP9_HIGHBITDEPTH - if (img1->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { - l_w *= 2; - c_w *= 2; - } -#endif - for (i = 0; i < img1->d_h; ++i) - match &= (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y], - img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y], - l_w) == 0); - for (i = 0; i < c_h; ++i) - match &= (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U], - img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U], - c_w) == 0); - for (i = 0; i < c_h; ++i) - match &= (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V], - img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V], - c_w) == 0); - return match; -} -#define mmin(a, b) ((a) < (b) ? (a) : (b)) -#if CONFIG_VP9_HIGHBITDEPTH -void find_mismatch_high(const vpx_image_t *const img1, - const vpx_image_t *const img2, int yloc[4], int uloc[4], - int vloc[4]) { - uint16_t *plane1, *plane2; - uint32_t stride1, stride2; - const uint32_t bsize = 64; - const uint32_t bsizey = bsize >> img1->y_chroma_shift; - const uint32_t bsizex = bsize >> img1->x_chroma_shift; - const uint32_t c_w = - (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; - const uint32_t c_h = - (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; - int match = 1; - uint32_t i, j; - yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1; - plane1 = (uint16_t *)img1->planes[VPX_PLANE_Y]; - plane2 = (uint16_t *)img2->planes[VPX_PLANE_Y]; - stride1 = img1->stride[VPX_PLANE_Y] / 2; - stride2 = img2->stride[VPX_PLANE_Y] / 2; - for (i = 0, match = 1; match && i < img1->d_h; i += bsize) { - for (j = 0; match && j < img1->d_w; j += bsize) { - int k, l; - const int si = mmin(i + bsize, img1->d_h) - i; - const int sj = mmin(j + bsize, img1->d_w) - j; - for (k = 0; match && k < si; ++k) { - for (l = 0; match && l < sj; ++l) { - if (*(plane1 + (i + k) * stride1 + j + l) != - *(plane2 + (i + k) * stride2 + j + l)) { - yloc[0] = i + k; - yloc[1] = j + l; - yloc[2] = *(plane1 + (i + k) * stride1 + j + l); - yloc[3] = *(plane2 + (i + k) * stride2 + j + l); - match = 0; - break; - } - } - } - } - } - uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1; - plane1 = (uint16_t *)img1->planes[VPX_PLANE_U]; - plane2 = (uint16_t *)img2->planes[VPX_PLANE_U]; - stride1 = img1->stride[VPX_PLANE_U] / 2; - stride2 = img2->stride[VPX_PLANE_U] / 2; - for (i = 0, match = 1; match && i < c_h; i += bsizey) { - for (j = 0; match && j < c_w; j += bsizex) { - int k, l; - const int si = mmin(i + bsizey, c_h - i); - const int sj = mmin(j + bsizex, c_w - j); - for (k = 0; match && k < si; ++k) { - for (l = 0; match && l < sj; ++l) { - if (*(plane1 + (i + k) * stride1 + j + l) != - *(plane2 + (i + k) * stride2 + j + l)) { - uloc[0] = i + k; - uloc[1] = j + l; - uloc[2] = *(plane1 + (i + k) * stride1 + j + l); - uloc[3] = *(plane2 + (i + k) * stride2 + j + l); - match = 0; - break; - } - } - } - } - } - vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1; - plane1 = (uint16_t *)img1->planes[VPX_PLANE_V]; - plane2 = (uint16_t *)img2->planes[VPX_PLANE_V]; - stride1 = img1->stride[VPX_PLANE_V] / 2; - stride2 = img2->stride[VPX_PLANE_V] / 2; - for (i = 0, match = 1; match && i < c_h; i += bsizey) { - for (j = 0; match && j < c_w; j += bsizex) { - int k, l; - const int si = mmin(i + bsizey, c_h - i); - const int sj = mmin(j + bsizex, c_w - j); - for (k = 0; match && k < si; ++k) { - for (l = 0; match && l < sj; ++l) { - if (*(plane1 + (i + k) * stride1 + j + l) != - *(plane2 + (i + k) * stride2 + j + l)) { - vloc[0] = i + k; - vloc[1] = j + l; - vloc[2] = *(plane1 + (i + k) * stride1 + j + l); - vloc[3] = *(plane2 + (i + k) * stride2 + j + l); - match = 0; - break; - } - } - } - } - } -} -#endif // CONFIG_VP9_HIGHBITDEPTH -void find_mismatch(const vpx_image_t *const img1, const vpx_image_t *const img2, - int yloc[4], int uloc[4], int vloc[4]) { - const uint32_t bsize = 64; - const uint32_t bsizey = bsize >> img1->y_chroma_shift; - const uint32_t bsizex = bsize >> img1->x_chroma_shift; - const uint32_t c_w = - (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; - const uint32_t c_h = - (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; - int match = 1; - uint32_t i, j; - yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1; - for (i = 0, match = 1; match && i < img1->d_h; i += bsize) { - for (j = 0; match && j < img1->d_w; j += bsize) { - int k, l; - const int si = mmin(i + bsize, img1->d_h) - i; - const int sj = mmin(j + bsize, img1->d_w) - j; - for (k = 0; match && k < si; ++k) { - for (l = 0; match && l < sj; ++l) { - if (*(img1->planes[VPX_PLANE_Y] + - (i + k) * img1->stride[VPX_PLANE_Y] + j + l) != - *(img2->planes[VPX_PLANE_Y] + - (i + k) * img2->stride[VPX_PLANE_Y] + j + l)) { - yloc[0] = i + k; - yloc[1] = j + l; - yloc[2] = *(img1->planes[VPX_PLANE_Y] + - (i + k) * img1->stride[VPX_PLANE_Y] + j + l); - yloc[3] = *(img2->planes[VPX_PLANE_Y] + - (i + k) * img2->stride[VPX_PLANE_Y] + j + l); - match = 0; - break; - } - } - } - } - } - uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1; - for (i = 0, match = 1; match && i < c_h; i += bsizey) { - for (j = 0; match && j < c_w; j += bsizex) { - int k, l; - const int si = mmin(i + bsizey, c_h - i); - const int sj = mmin(j + bsizex, c_w - j); - for (k = 0; match && k < si; ++k) { - for (l = 0; match && l < sj; ++l) { - if (*(img1->planes[VPX_PLANE_U] + - (i + k) * img1->stride[VPX_PLANE_U] + j + l) != - *(img2->planes[VPX_PLANE_U] + - (i + k) * img2->stride[VPX_PLANE_U] + j + l)) { - uloc[0] = i + k; - uloc[1] = j + l; - uloc[2] = *(img1->planes[VPX_PLANE_U] + - (i + k) * img1->stride[VPX_PLANE_U] + j + l); - uloc[3] = *(img2->planes[VPX_PLANE_U] + - (i + k) * img2->stride[VPX_PLANE_U] + j + l); - match = 0; - break; - } - } - } - } - } - vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1; - for (i = 0, match = 1; match && i < c_h; i += bsizey) { - for (j = 0; match && j < c_w; j += bsizex) { - int k, l; - const int si = mmin(i + bsizey, c_h - i); - const int sj = mmin(j + bsizex, c_w - j); - for (k = 0; match && k < si; ++k) { - for (l = 0; match && l < sj; ++l) { - if (*(img1->planes[VPX_PLANE_V] + - (i + k) * img1->stride[VPX_PLANE_V] + j + l) != - *(img2->planes[VPX_PLANE_V] + - (i + k) * img2->stride[VPX_PLANE_V] + j + l)) { - vloc[0] = i + k; - vloc[1] = j + l; - vloc[2] = *(img1->planes[VPX_PLANE_V] + - (i + k) * img1->stride[VPX_PLANE_V] + j + l); - vloc[3] = *(img2->planes[VPX_PLANE_V] + - (i + k) * img2->stride[VPX_PLANE_V] + j + l); - match = 0; - break; - } - } - } - } - } -} \ No newline at end of file diff --git a/pkg/encoder/vpx/tools_common.h b/pkg/encoder/vpx/tools_common.h deleted file mode 100644 index ee0946e2..00000000 --- a/pkg/encoder/vpx/tools_common.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2010 The WebM project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ -#ifndef VPX_TOOLS_COMMON_H_ -#define VPX_TOOLS_COMMON_H_ -#include -#include "./vpx_config.h" -#include "vpx/vpx_codec.h" -#include "vpx/vpx_image.h" -#include "vpx/vpx_integer.h" -#include "msvc.h" -#if CONFIG_ENCODERS -#include "./y4minput.h" -#endif -#if defined(_MSC_VER) -/* MSVS uses _f{seek,tell}i64. */ -#define fseeko _fseeki64 -#define ftello _ftelli64 -typedef int64_t FileOffset; -#elif defined(_WIN32) -/* MinGW uses f{seek,tell}o64 for large files. */ -#define fseeko fseeko64 -#define ftello ftello64 -typedef off64_t FileOffset; -#elif CONFIG_OS_SUPPORT -#include /* NOLINT */ -typedef off_t FileOffset; -/* Use 32-bit file operations in WebM file format when building ARM - * executables (.axf) with RVCT. */ -#else -#define fseeko fseek -#define ftello ftell -typedef long FileOffset; /* NOLINT */ -#endif /* CONFIG_OS_SUPPORT */ -#if CONFIG_OS_SUPPORT -#if defined(_MSC_VER) -#include /* NOLINT */ -#define isatty _isatty -#define fileno _fileno -#else -#include /* NOLINT */ -#endif /* _MSC_VER */ -#endif /* CONFIG_OS_SUPPORT */ -#define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo) -#ifndef PATH_MAX -#define PATH_MAX 512 -#endif -#define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */ -#define IVF_FILE_HDR_SZ 32 -#define RAW_FRAME_HDR_SZ sizeof(uint32_t) -#define VP8_FOURCC 0x30385056 -#define VP9_FOURCC 0x30395056 -enum VideoFileType { - FILE_TYPE_RAW, - FILE_TYPE_IVF, - FILE_TYPE_Y4M, - FILE_TYPE_WEBM -}; -struct FileTypeDetectionBuffer { - char buf[4]; - size_t buf_read; - size_t position; -}; -struct VpxRational { - int numerator; - int denominator; -}; -struct VpxInputContext { - const char *filename; - FILE *file; - int64_t length; - struct FileTypeDetectionBuffer detect; - enum VideoFileType file_type; - uint32_t width; - uint32_t height; - struct VpxRational pixel_aspect_ratio; - vpx_img_fmt_t fmt; - vpx_bit_depth_t bit_depth; - int only_i420; - uint32_t fourcc; - struct VpxRational framerate; -#if CONFIG_ENCODERS - y4m_input y4m; -#endif -}; -#ifdef __cplusplus -extern "C" { -#endif -#if defined(__GNUC__) -#define VPX_NO_RETURN __attribute__((noreturn)) -#else -#define VPX_NO_RETURN -#endif -/* Sets a stdio stream into binary mode */ -FILE *set_binary_mode(FILE *stream); -void die(const char *fmt, ...) VPX_NO_RETURN; -void fatal(const char *fmt, ...) VPX_NO_RETURN; -void warn(const char *fmt, ...); -void die_codec(vpx_codec_ctx_t *ctx, const char *s) VPX_NO_RETURN; -/* The tool including this file must define usage_exit() */ -// void usage_exit(void) VPX_NO_RETURN; -#undef VPX_NO_RETURN -int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame); -typedef struct VpxInterface { - const char *const name; - const uint32_t fourcc; - vpx_codec_iface_t *(*const codec_interface)(); -} VpxInterface; -int get_vpx_encoder_count(void); -const VpxInterface *get_vpx_encoder_by_index(int i); -const VpxInterface *get_vpx_encoder_by_name(const char *name); -int get_vpx_decoder_count(void); -const VpxInterface *get_vpx_decoder_by_index(int i); -const VpxInterface *get_vpx_decoder_by_name(const char *name); -const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc); -int vpx_img_plane_width(const vpx_image_t *img, int plane); -int vpx_img_plane_height(const vpx_image_t *img, int plane); -void vpx_img_write(const vpx_image_t *img, FILE *file); -int vpx_img_read(vpx_image_t *img, void *bs); -double sse_to_psnr(double samples, double peak, double mse); -#if CONFIG_ENCODERS -int read_frame(struct VpxInputContext *input_ctx, vpx_image_t *img); -int file_is_y4m(const char detect[4]); -int fourcc_is_ivf(const char detect[4]); -void open_input_file(struct VpxInputContext *input); -void close_input_file(struct VpxInputContext *input); -#endif -#if CONFIG_VP9_HIGHBITDEPTH -void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src, int input_shift); -void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src, int down_shift); -void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src); -#endif -int compare_img(const vpx_image_t *const img1, const vpx_image_t *const img2); -#if CONFIG_VP9_HIGHBITDEPTH -void find_mismatch_high(const vpx_image_t *const img1, - const vpx_image_t *const img2, int yloc[4], int uloc[4], - int vloc[4]); -#endif -void find_mismatch(const vpx_image_t *const img1, const vpx_image_t *const img2, - int yloc[4], int uloc[4], int vloc[4]); -#ifdef __cplusplus -} /* extern "C" */ -#endif -#endif // VPX_TOOLS_COMMON_H_ \ No newline at end of file diff --git a/pkg/encoder/vpx/vpx_config.h b/pkg/encoder/vpx/vpx_config.h deleted file mode 100644 index 99d3eb94..00000000 --- a/pkg/encoder/vpx/vpx_config.h +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (c) 2011 The WebM project authors. All Rights Reserved. */ -/* */ -/* Use of this source code is governed by a BSD-style license */ -/* that can be found in the LICENSE file in the root of the source */ -/* tree. An additional intellectual property rights grant can be found */ -/* in the file PATENTS. All contributing project authors may */ -/* be found in the AUTHORS file in the root of the source tree. */ -/* This file automatically generated by configure. Do not edit! */ -#ifndef VPX_CONFIG_H -#define VPX_CONFIG_H -#define RESTRICT -#define ARCH_ARM 0 -#define ARCH_MIPS 0 -#define ARCH_X86 0 -#define ARCH_X86_64 1 -#define ARCH_PPC32 0 -#define ARCH_PPC64 0 -#define HAVE_EDSP 0 -#define HAVE_MEDIA 0 -#define HAVE_NEON 0 -#define HAVE_MIPS32 0 -#define HAVE_DSPR2 0 -#define HAVE_MMX 1 -#define HAVE_SSE 1 -#define HAVE_SSE2 1 -#define HAVE_SSE3 1 -#define HAVE_SSSE3 1 -#define HAVE_SSE4_1 1 -#define HAVE_ALTIVEC 0 -#define HAVE_VPX_PORTS 1 -#define HAVE_STDINT_H 1 -#define HAVE_ALT_TREE_LAYOUT 0 -#define HAVE_PTHREAD_H 1 -#define HAVE_SYS_MMAN_H 1 -#define HAVE_UNISTD_H 1 -#define CONFIG_EXTERNAL_BUILD 0 -#define CONFIG_INSTALL_DOCS 0 -#define CONFIG_INSTALL_BINS 1 -#define CONFIG_INSTALL_LIBS 1 -#define CONFIG_INSTALL_SRCS 0 -#define CONFIG_DEBUG 0 -#define CONFIG_GPROF 0 -#define CONFIG_GCOV 0 -#define CONFIG_RVCT 0 -#define CONFIG_GCC 1 -#define CONFIG_MSVS 0 -#define CONFIG_PIC 0 -#define CONFIG_BIG_ENDIAN 0 -#define CONFIG_CODEC_SRCS 0 -#define CONFIG_DEBUG_LIBS 0 -#define CONFIG_FAST_UNALIGNED 1 -#define CONFIG_MEM_MANAGER 0 -#define CONFIG_MEM_TRACKER 1 -#define CONFIG_MEM_CHECKS 0 -#define CONFIG_MD5 1 -#define CONFIG_DEQUANT_TOKENS 0 -#define CONFIG_DC_RECON 0 -#define CONFIG_RUNTIME_CPU_DETECT 1 -#define CONFIG_POSTPROC 1 -#define CONFIG_MULTITHREAD 1 -#define CONFIG_INTERNAL_STATS 0 -#define CONFIG_VP8_ENCODER 1 -#define CONFIG_VP8_DECODER 1 -#define CONFIG_VP8 1 -#define CONFIG_ENCODERS 0 -#define CONFIG_DECODERS 0 -#define CONFIG_STATIC_MSVCRT 0 -#define CONFIG_SPATIAL_RESAMPLING 1 -#define CONFIG_REALTIME_ONLY 0 -#define CONFIG_ONTHEFLY_BITPACKING 1 -#define CONFIG_ERROR_CONCEALMENT 1 -#define CONFIG_SHARED 0 -#define CONFIG_STATIC 1 -#define CONFIG_SMALL 0 -#define CONFIG_POSTPROC_VISUALIZER 1 -#define CONFIG_OS_SUPPORT 1 -#define CONFIG_UNIT_TESTS 1 -#define CONFIG_MULTI_RES_ENCODING 1 -#define CONFIG_TEMPORAL_DENOISING 1 -#endif /* VPX_CONFIG_H */ \ No newline at end of file diff --git a/pkg/util/codec.go b/pkg/encoder/yuv.go similarity index 63% rename from pkg/util/codec.go rename to pkg/encoder/yuv.go index 9d542694..82e4c59d 100644 --- a/pkg/util/codec.go +++ b/pkg/encoder/yuv.go @@ -1,16 +1,14 @@ -// credit to https://github.com/poi5305/go-yuv2webRTC/blob/master/webrtc/webrtc.go -package util +package encoder import ( "image" "unsafe" ) -// https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency +// see: https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency +// credit to https://github.com/poi5305/go-yuv2webRTC/blob/master/webrtc/webrtc.go /* -#cgo CFLAGS: -O3 - void rgba2yuv(void * destination, void * source, int width, int height, int stride) { const int image_size = width * height; unsigned char * rgba = source; @@ -46,19 +44,22 @@ void rgba2yuv(void * destination, void * source, int width, int height, int stri */ import "C" -// RgbaToYuv convert to yuv from rgba -func RgbaToYuv(rgba *image.RGBA) []byte { - w := rgba.Rect.Max.X - h := rgba.Rect.Max.Y - size := int(float32(w*h) * 1.5) - stride := rgba.Stride - w*4 - yuv := make([]byte, size, size) - C.rgba2yuv(unsafe.Pointer(&yuv[0]), unsafe.Pointer(&rgba.Pix[0]), C.int(w), C.int(h), C.int(stride)) - return yuv +type Yuv struct { + data []byte + w, h int } -// RgbaToYuvInplace convert to yuv from rgba inplace to yuv. Avoid reallocation -func RgbaToYuvInplace(rgba *image.RGBA, yuv []byte, width, height int) { - stride := rgba.Stride - width*4 - C.rgba2yuv(unsafe.Pointer(&yuv[0]), unsafe.Pointer(&rgba.Pix[0]), C.int(width), C.int(height), C.int(stride)) +func NewYuvBuffer(w, h int) Yuv { + size := int(float32(w*h) * 1.5) + return Yuv{ + data: make([]byte, size, size), + w: w, + h: h, + } +} + +// FromRGBA converts RGBA colorspace into YUV I420 format inside the internal buffer. +func (yuv *Yuv) FromRGBA(rgba *image.RGBA) *Yuv { + C.rgba2yuv(unsafe.Pointer(&yuv.data[0]), unsafe.Pointer(&rgba.Pix[0]), C.int(yuv.w), C.int(yuv.h), C.int(0)) + return yuv } diff --git a/pkg/worker/room/media.go b/pkg/worker/room/media.go index 53b2c1e9..feac0514 100644 --- a/pkg/worker/room/media.go +++ b/pkg/worker/room/media.go @@ -82,24 +82,23 @@ func (r *Room) startVideo(width, height int, video encoderConfig.Video) { LogLevel: int32(video.H264.LogLevel), })) } else { - enc, err = vpx.NewEncoder(width, height, 20, 1200, 5) + enc, err = vpx.NewEncoder(width, height, vpx.WithOptions(vpx.Options{ + Bitrate: video.Vpx.Bitrate, + KeyframeInt: video.Vpx.KeyframeInterval, + })) } - defer func() { - enc.Stop() - }() - if err != nil { fmt.Println("error create new encoder", err) return } - r.encoder = enc + r.vPipe = encoder.NewVideoPipe(enc, width, height) + einput, eoutput := r.vPipe.Input, r.vPipe.Output - einput := enc.GetInputChan() - eoutput := enc.GetOutputChan() + go r.vPipe.Start() + defer r.vPipe.Stop() - // send screenshot go func() { defer func() { if r := recover(); r != nil { diff --git a/pkg/worker/room/media_test.go b/pkg/worker/room/media_test.go index a577efcd..92e2657d 100644 --- a/pkg/worker/room/media_test.go +++ b/pkg/worker/room/media_test.go @@ -2,9 +2,10 @@ package room import ( "image" - col "image/color" + "image/color" "math/rand" "testing" + "time" "github.com/giongto35/cloud-game/v2/pkg/encoder" "github.com/giongto35/cloud-game/v2/pkg/encoder/h264" @@ -25,11 +26,12 @@ func benchmarkEncoder(w, h int, codec encoder.VideoCodec, b *testing.B) { if codec == encoder.H264 { enc, _ = h264.NewEncoder(w, h) } else { - enc, _ = vpx.NewEncoder(w, h, 20, 1200, 5) + enc, _ = vpx.NewEncoder(w, h) } - defer enc.Stop() - in, out := enc.GetInputChan(), enc.GetOutputChan() + pipe := encoder.NewVideoPipe(enc, w, h) + go pipe.Start() + defer pipe.Stop() image1 := genTestImage(w, h, rand.New(rand.NewSource(int64(1))).Float32()) image2 := genTestImage(w, h, rand.New(rand.NewSource(int64(2))).Float32()) @@ -39,8 +41,12 @@ func benchmarkEncoder(w, h int, codec encoder.VideoCodec, b *testing.B) { if i%2 == 0 { im = image2 } - in <- encoder.InFrame{Image: im} - <-out + pipe.Input <- encoder.InFrame{Image: im} + select { + case <-pipe.Output: + case <-time.After(5 * time.Second): + b.Fatalf("encoder didn't produce an image") + } } } @@ -48,9 +54,8 @@ func genTestImage(w, h int, seed float32) *image.RGBA { img := image.NewRGBA(image.Rectangle{Max: image.Point{X: w, Y: h}}) for x := 0; x < w; x++ { for y := 0; y < h; y++ { - var color col.Color - color = col.RGBA{R: uint8(seed * 255), G: uint8(seed * 255), B: uint8(seed * 255), A: 0xff} - img.Set(x, y, color) + col := color.RGBA{R: uint8(seed * 255), G: uint8(seed * 255), B: uint8(seed * 255), A: 0xff} + img.Set(x, y, col) } } return img diff --git a/pkg/worker/room/room.go b/pkg/worker/room/room.go index 2c8850db..33946661 100644 --- a/pkg/worker/room/room.go +++ b/pkg/worker/room/room.go @@ -56,7 +56,7 @@ type Room struct { // GameName gameName string - encoder encoder.Encoder + vPipe *encoder.VideoPipe } const ( diff --git a/pkg/worker/room/room_test.go b/pkg/worker/room/room_test.go index d0e43082..a273edca 100644 --- a/pkg/worker/room/room_test.go +++ b/pkg/worker/room/room_test.go @@ -90,7 +90,7 @@ func TestRoom(t *testing.T) { codec: test.codec, }) t.Logf("The game [%v] has been loaded", test.game.Name) - waitNFrames(test.frames, room.encoder.GetOutputChan()) + waitNFrames(test.frames, room.vPipe.Output) room.Close() } // hack: wait room destruction @@ -122,7 +122,7 @@ func TestRoomWithGL(t *testing.T) { codec: test.codec, }) t.Logf("The game [%v] has been loaded", test.game.Name) - waitNFrames(test.frames, room.encoder.GetOutputChan()) + waitNFrames(test.frames, room.vPipe.Output) room.Close() } // hack: wait room destruction @@ -161,7 +161,7 @@ func TestAllEmulatorRooms(t *testing.T) { autoGlContext: autoGlContext, }) t.Logf("The game [%v] has been loaded", test.game.Name) - waitNFrames(test.frames, room.encoder.GetOutputChan()) + waitNFrames(test.frames, room.vPipe.Output) if renderFrames { img := room.director.GetViewport().(*image.RGBA) @@ -245,7 +245,7 @@ func getRoomMock(cfg roomMockConfig) roomMock { wasted := 0 go func() { sleepDeltaMs := 10 - for room.director == nil || room.encoder == nil { + for room.director == nil || room.vPipe == nil { time.Sleep(time.Duration(sleepDeltaMs) * time.Millisecond) wasted++ if wasted > 1000 { @@ -315,7 +315,7 @@ func benchmarkRoom(rom games.GameMetadata, codec encoder.VideoCodec, frames int, game: rom, codec: codec, }) - waitNFrames(frames, room.encoder.GetOutputChan()) + waitNFrames(frames, room.vPipe.Output) room.Close() } }