h264无损编码 [英] h264 lossless coding

查看:492
本文介绍了h264无损编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在h264中进行完全无损编码? 无损"是指,如果将其馈入一系列帧并对其进行编码,然后从编码的视频中提取所有帧,则将获得与输入中逐像素,逐帧完全相同的帧.真的有可能吗? 举个例子:

Is it possible to do completely lossless encoding in h264? By lossless, I mean that if I feed it a series of frames and encode them, and then if I extract all the frames from the encoded video, I will get the exact same frames as in the input, pixel by pixel, frame by frame. Is that actually possible? Take this example:

我生成一堆帧,然后将图像序列编码为未压缩的AVI(使用virtualdub之类的东西),然后应用无损h264(帮助文件声称设置--qp 0可以进行无损压缩,但是我不知道这是否意味着该过程的任何点都没有损失,或者只是量化是无损的).然后,我可以使用诸如mplayer之类的东西从生成的h264视频中提取帧.

I generate a bunch of frames, then I encode the image sequence to an uncompressed AVI (with something like virtualdub), I then apply lossless h264 (the help files claim that setting --qp 0 makes lossless compression, but I am not sure if that means that there is no loss at any point of the process or that just the quantization is lossless). I can then extract the frames from the resulting h264 video with something like mplayer.

我先尝试了Handbrake,但事实证明它不支持无损编码.我尝试了x264,但是它崩溃了.可能是因为我的源AVI文件位于RGB颜色空间中,而不是YV12.无论如何,我都不知道如何将一系列YV12位图以及哪种格式提供给x264,所以我什至无法尝试.

I tried with Handbrake first, but it turns out it doesn't support lossless encoding. I tried x264 but it crashes. It may be because my source AVI file is in RGB colorspace instead of YV12. I don't know how to feed a series of YV12 bitmaps and in what format to x264 anyway, so I cannot even try.

总之,我想知道那是否有路可走

In summary what I want to know if that is there a way to go from

无损位图系列(在任何色彩空间中)->一些转换-> h264编码-> h264解码->一些转换->原始的无损位图系列

Series of lossless bitmaps (in any colorspace) -> some transformation -> h264 encode -> h264 decode -> some transformation -> the original series of lossless bitmaps

是否有办法实现这一目标?

If there a way to achieve this?

关于无损H264不太有意义,有一个非常有效的观点.我很清楚,我无法(仅凭眼睛)分辨出未压缩的剪辑与在H264中以高速率压缩的另一个剪辑之间的区别,但是我认为这并非没有用处.例如,对于存储视频进行编辑而不用占用大量空间,不损失质量以及每次保存文件时都不会花费太多编码时间可能很有用.

There is a VERY valid point about lossless H264 not making too much sense. I am well aware that there is no way I could tell (with just my eyes) the difference between and uncompressed clip and another compressed at a high rate in H264, but I don't think it is not without uses. For example, it may be useful for storing video for editing without taking huge amounts of space and not losing quality and spending too much encoding time every time the file is saved.

更新2:现在x264不会崩溃.我可以将avisynth或无损yv12 lagarith用作源(以避免出现色彩空间压缩警告).但是,即使使用--qp 0和rgb或yv12源,我仍然会有一些差异,虽然很小,但仍然存在.这令人不安,因为我在无损预测编码(--qp 0)上发现的所有信息都声称整个编码应该是无损的,但是我无法对此进行验证.

UPDATE 2: Now x264 doesn't crash. I can use as sources either avisynth or lossless yv12 lagarith (to avoid the colorspace compression warning). Howerver, even with --qp 0 and a rgb or yv12 source I still get some differences, minimal but present. This is troubling, because all the information I have found on lossless predictive coding (--qp 0) claims that the whole encoding should be lossless, but I am unable to verifiy this.

推荐答案

我将花一整天的时间试图弄清楚如何将YUV 4:4:4像素转换为x264,为此添加一个较晚的答案.尽管x264确实接受文件中的原始4:2:0像素,但要传递4:4:4像素确实非常困难.对于最新版本的ffmpeg,以下代码可用于完全无损编码和提取以验证编码.

I am going to add a late answer to this one after spending all day trying to figure out how to get YUV 4:4:4 pixels into x264. While x264 does accept raw 4:2:0 pixels in a file, it is really quite difficult getting 4:4:4 pixels passed in. With recent versions of ffmpeg, the following works for completely lossless encoding and extraction to verify the encoding.

首先,将原始的yuv 4:4:4像素以平面格式写入文件.这些平面是一组Y字节,然后是U和V字节,其中U和V使用128作为零值.现在,调用ffmpeg并使用"yuv444p"像素格式两次传入原始YUV帧的大小,如下所示:

First, write your raw yuv 4:4:4 pixels to a file in a planar format. The planes are a set of Y bytes, then the U and V bytes where U and V use 128 as the zero value. Now, invoke ffmpeg and pass in the size of the raw YUV frames as use the "yuv444p" pixel format twice, like so:

ffmpeg -y -s 480x480 -pix_fmt yuv444p -i Tree480.yuv \
-c:v libx264 -pix_fmt yuv444p -profile:v high444 -crf 0 \
-preset:v slow \
Tree480_lossless.m4v

完成对h264的编码并作为Quicktime文件包装后,就可以提取出完全相同的字节,如下所示:

Once the encoding to h264 and wrapping as a Quicktime file is done, one can extract the exact same bytes like so:

ffmpeg -y -i Tree480_lossless.m4v -vcodec rawvideo -pix_fmt yuv444p \
Tree480_m4v_decoded.yuv

最后,用diff验证两个二进制文件:

Finally, verify the two binary files with diff:

$ diff -s Tree480.yuv Tree480_m4v_decoded.yuv
Files Tree480.yuv and Tree480_m4v_decoded.yuv are identical

请记住,您需要自己将YUV字节写入文件,请勿让ffmpeg对YUV值进行任何转换!

Just keep in mind that you need to write the YUV bytes to a file yourself, do not let ffmpeg do any conversion of the YUV values!

这篇关于h264无损编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆