Encoding x264编码严重质量损失

Encoding x264编码严重质量损失,encoding,ffmpeg,libx264,Encoding,Ffmpeg,Libx264,我使用repo将mjpeg流编码为h264流,但输出不太好。该流是一系列iPhone屏幕截图。在输出流中,甚至设置应用程序中两个项目之间的行也消失了。如何提高输出流的质量? 下面是x264用来初始化编码器的代码片段 func NewEncoder(w io.Writer, opts *Options) (e *Encoder, err error) { e = &Encoder{} e.w = w e.pts = 0 e.opts = opts

我使用repo将mjpeg流编码为h264流,但输出不太好。该流是一系列iPhone屏幕截图。在输出流中,甚至设置应用程序中两个项目之间的行也消失了。如何提高输出流的质量? 下面是x264用来初始化编码器的代码片段

func NewEncoder(w io.Writer, opts *Options) (e *Encoder, err error) {
    e = &Encoder{}

    e.w = w
    e.pts = 0
    e.opts = opts

    e.csp = x264c.CspI420

    e.nals = make([]*x264c.Nal, 3)
    e.img = NewYCbCr(image.Rect(0, 0, e.opts.Width, e.opts.Height))

    param := x264c.Param{}

    if e.opts.Preset != "" && e.opts.Profile != "" {
        ret := x264c.ParamDefaultPreset(&param, e.opts.Preset, e.opts.Tune)
        if ret < 0 {
            err = fmt.Errorf("x264: invalid preset/tune name")
            return
        }
    } else {
        x264c.ParamDefault(&param)
    }

    param.IWidth = int32(e.opts.Width)
    param.IHeight = int32(e.opts.Height)

    param.ICsp = e.csp
    param.BVfrInput = 0
    param.BRepeatHeaders = 1
    param.BAnnexb = 1

    param.ILogLevel = e.opts.LogLevel

    if e.opts.FrameRate > 0 {
        param.IFpsNum = uint32(e.opts.FrameRate)
        param.IFpsDen = 1

        param.IKeyintMax = int32(e.opts.FrameRate)
        param.BIntraRefresh = 1
    }

    if e.opts.Profile != "" {
        ret := x264c.ParamApplyProfile(&param, e.opts.Profile)
        if ret < 0 {
            err = fmt.Errorf("x264: invalid profile name")
            return
        }
    }

    // Allocate on create instead while encoding
    var picIn x264c.Picture
    ret := x264c.PictureAlloc(&picIn, e.csp, int32(e.opts.Width), int32(e.opts.Height))
    if ret < 0 {
        err = fmt.Errorf("x264: cannot allocate picture")
        return
    }
    e.picIn = picIn
    defer func() {
        // Cleanup if intialization fail
        if err != nil {
            x264c.PictureClean(&picIn)
        }
    }()

    e.e = x264c.EncoderOpen(&param)
    if e.e == nil {
        err = fmt.Errorf("x264: cannot open the encoder")
        return
    }

    ret = x264c.EncoderHeaders(e.e, e.nals, &e.nnals)
    if ret < 0 {
        err = fmt.Errorf("x264: cannot encode headers")
        return
    }

    if ret > 0 {
        b := C.GoBytes(e.nals[0].PPayload, C.int(ret))
        n, er := e.w.Write(b)
        if er != nil {
            err = er
            return
        }

        if int(ret) != n {
            err = fmt.Errorf("x264: error writing headers, size=%d, n=%d", ret, n)
        }
    }

    return
}
我还使用了ffmpeg(尽管api很旧)

package屏幕广播
进口(
/*
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类型定义结构{
int w,h;
int-pixfmt;
字符*预置[2];
字符*profile;
整数比特率;
int-get;
AVCodec*c;
AVCodecContext*ctx;
AVFrame*f;
AVPacket-pkt;
}H264附录t;
静态整数h264enc_新(h264enc_t*m){
m->c=avcodec\U find\U编码器(AV\U CODEC\U ID\U H264);
m->ctx=avcodec\u alloc\u context3(m->c);
m->ctx->宽度=m->w;
m->ctx->高度=m->h;
m->ctx->pix_fmt=m->pixfmt;
m->ctx->time_base=(AVRational){1,10};
av_opt_设置(m->ctx->PRV_数据,“预设”,“慢速”,0);
av_opt_设置(m->ctx->PRV_数据,“调谐”,“零延迟”,0);
av_opt_集合(m->ctx->PRV_数据,“配置文件”,“基线”,0);
av_opt_集(m->ctx->PRV_数据,“crf”,“18.0.”,0);
m->f=av_frame_alloc();
m->f->format=m->ctx->pix\U fmt;
m->f->宽度=m->ctx->宽度;
m->f->高度=m->ctx->高度;
avcodec_open2(m->ctx,m->c,空);
返回av_image_alloc(m->f->data,m->f->linesize,m->ctx->width,m->ctx->height,m->ctx->pix_fmt,32);
}
*/
“C”
“错误”
“图像”
“不安全”
//“日志”
)
H264型编码器结构{
m C.H264附录
头[]字节
Pixfmt image.YCbCrSubsampleRatio
W、 H int
pts int
}
func新H264编码器(w,h int)(m*H264编码器,错误){
m=&H264编码器{}
m、 m.w=(C.int)(w)
m、 m.h=(C.int)(h)
m、 W=W
m、 H=H
m、 Pixfmt=image.YCbCrSubsampleRatio420
m、 m.pixfmt=C.AV\u PIX\u FMT\u YUV420P
r:=C.h264enc_新(&m.m)
如果int(r)<0{
err=错误。新建(“打开编码器失败”)
返回
}
返回
}
func(m*H264编码器)编码(img*image.YCbCr)(数据[]字节,错误){
变量f*C.AVFrame
如果img==nil{
f=零
}否则{
如果img.SubsampleRatio!=m.Pixfmt{
err=错误。新建(“图像pixfmt不匹配”)
返回
}
如果img.Rect.Dx()!=m.W | | img.Rect.Dy()!=m.H{
err=错误。新建(“图像大小不匹配”)
返回
}
f=m.m.f
f、 数据[0]=(*C.uint8_t)(不安全的指针(&img.Y[0]))
f、 数据[1]=(*C.uint8_t)(不安全的指针(&img.Cb[0]))
f、 数据[2]=(*C.uint8_t)(不安全的指针(&img.Cr[0]))
f、 线宽[0]=(C.int)(img.YStride)
f、 线宽[1]=(C.int)(img.CStride)
f、 线宽[2]=(C.int)(img.CStride)
}
C.av_init_包(&m.m.pkt)
m、 m.pkt.data=nil
m、 m.pkt.size=0
f、 分数=(C.longlong)(m.pts)
m、 临时秘书处++
r:=C.avcodec\u encode\u video2(m.m.ctx和m.m.pkt、f和m.m.got)
延迟C.av\U数据包\U unref(&m.m.pkt)
如果int(r)<0{
err=错误。新建(“编码失败”)
返回
}
如果m.m.got==0{
err=错误。新建(“无图片”)
返回
}
如果m.m.pkt.size==0{
err=errors.New(“数据包大小==0”)
返回
}
数据=生成([]字节,m.m.pkt.size)
C.memcpy(
不安全。指针(&数据[0]),
不安全的指针(m.m.pkt.data),
(C.尺寸)(m.m.包装尺寸),
)
返回数据,无
}
但是得到了同样的结果。然而,当我使用ffmpeg二进制文件时,结果非常好,所以我想我设置了错误的参数,但我不知道是哪个参数。 如有任何建议,将不胜感激。如果你有更好的方法来实现这一点,我将不胜感激

供您参考:我必须使用golang来完成此操作


事实证明这不是细节损失,真正的问题是颜色损失。输出流应该是yuvj420p[0-255]格式,而不是yuv420p[16-239]。

您设置了比特率还是CRF?@szatmary是的,我设置了CRF并尝试了几个不同的值,但差别不大。我还需要将tune设置为zerolatency,将profile设置为baseline,以使转码是实时的。您共享的链接与您描述的内容不同。我们无法帮助您调试无法看到的代码。
opts := &x264.Options{
        Width:     int(width)/2*2,
        Height:    int(height)/2*2,
        FrameRate: 15,
        Tune:      "zerolatency",
        Preset:    "medium",
        Profile:   "baseline",
        LogLevel:  x264.LogNone,
    }
package screencast

import (

    /*
        #include <stdio.h>
        #include <stdlib.h>
        #include <stdint.h>
        #include <string.h>
        #include <libavcodec/avcodec.h>
        #include <libavformat/avformat.h>
        #include <libavutil/avutil.h>
        #include <libavutil/opt.h>
        #include <libavutil/channel_layout.h>
        #include <libavutil/common.h>
        #include <libavutil/imgutils.h>
        #include <libavutil/mathematics.h>
        #include <libavutil/samplefmt.h>

        typedef struct {
            int w, h;
            int pixfmt;
            char *preset[2];
            char *profile;
            int bitrate;
            int got;
            AVCodec *c;
            AVCodecContext *ctx;
            AVFrame *f;
            AVPacket pkt;
        } h264enc_t;

        static int h264enc_new(h264enc_t *m) {
            m->c = avcodec_find_encoder(AV_CODEC_ID_H264);
            m->ctx = avcodec_alloc_context3(m->c);
            m->ctx->width = m->w;
            m->ctx->height = m->h;
            m->ctx->pix_fmt = m->pixfmt;
            m->ctx->time_base = (AVRational){1,10};
            av_opt_set(m->ctx->priv_data, "preset", "slow", 0);
            av_opt_set(m->ctx->priv_data, "tune", "zerolatency", 0);
            av_opt_set(m->ctx->priv_data, "profile", "baseline", 0);
            av_opt_set(m->ctx->priv_data, "crf", "18.0.", 0);
            m->f = av_frame_alloc();
            m->f->format = m->ctx->pix_fmt;
            m->f->width = m->ctx->width;
            m->f->height = m->ctx->height;
            avcodec_open2(m->ctx, m->c, NULL);
            return av_image_alloc(m->f->data, m->f->linesize, m->ctx->width, m->ctx->height, m->ctx->pix_fmt, 32);
        }

    */
    "C"
    "errors"
    "image"
    "unsafe"
    //"log"
)

type H264Encoder struct {
    m      C.h264enc_t
    Header []byte
    Pixfmt image.YCbCrSubsampleRatio
    W, H   int
    pts    int
}

func NewH264Encoder(w, h int) (m *H264Encoder, err error) {
    m = &H264Encoder{}
    m.m.w = (C.int)(w)
    m.m.h = (C.int)(h)
    m.W = w
    m.H = h
    m.Pixfmt = image.YCbCrSubsampleRatio420
    m.m.pixfmt = C.AV_PIX_FMT_YUV420P
    r := C.h264enc_new(&m.m)
    if int(r) < 0 {
        err = errors.New("open encoder failed")
        return
    }
    return
}

func (m *H264Encoder) Encode(img *image.YCbCr) (data []byte, err error) {
    var f *C.AVFrame
    if img == nil {
        f = nil
    } else {
        if img.SubsampleRatio != m.Pixfmt {
            err = errors.New("image pixfmt not match")
            return
        }
        if img.Rect.Dx() != m.W || img.Rect.Dy() != m.H {
            err = errors.New("image size not match")
            return
        }
        f = m.m.f
        f.data[0] = (*C.uint8_t)(unsafe.Pointer(&img.Y[0]))
        f.data[1] = (*C.uint8_t)(unsafe.Pointer(&img.Cb[0]))
        f.data[2] = (*C.uint8_t)(unsafe.Pointer(&img.Cr[0]))
        f.linesize[0] = (C.int)(img.YStride)
        f.linesize[1] = (C.int)(img.CStride)
        f.linesize[2] = (C.int)(img.CStride)
    }

    C.av_init_packet(&m.m.pkt)
    m.m.pkt.data = nil
    m.m.pkt.size = 0
    f.pts = (C.longlong)(m.pts)
    m.pts++
    r := C.avcodec_encode_video2(m.m.ctx, &m.m.pkt, f, &m.m.got)
    defer C.av_packet_unref(&m.m.pkt)
    if int(r) < 0 {
        err = errors.New("encode failed")
        return
    }
    if m.m.got == 0 {
        err = errors.New("no picture")
        return
    }
    if m.m.pkt.size == 0 {
        err = errors.New("packet size == 0")
        return
    }

    data = make([]byte, m.m.pkt.size)
    C.memcpy(
        unsafe.Pointer(&data[0]),
        unsafe.Pointer(m.m.pkt.data),
        (C.size_t)(m.m.pkt.size),
    )
    return data, nil
}