[FFmpeg-user] clipping samples to broadcast range in an mpeg2video output

Dave Rice dave at dericed.com
Mon Feb 8 20:48:41 CET 2016

> On Feb 7, 2016, at 4:57 PM, Andy Furniss <adf.lists at gmail.com> wrote:
> Dave Rice wrote:
>>>> On Feb 7, 2016, at 1:42 PM, Paul B Mahol <onemda at gmail.com>
>>>> wrote:
>>>>> On 2/5/16, Paul B Mahol <onemda at gmail.com> wrote: On 2/5/16,
>>>>> Dave Rice <dave at dericed.com> wrote: Hi all, I am having trouble
>>>>> creating an mpeg2video output that conforms to 16-235 broadcast
>>>>> range. I'd like to find a way to have an input which is
>>>>> yuv420p but has luma values out of broadcast range but clip to
>>>>> broadcast range while encoding to mpeg2.
>>>>> To demonstrate the issue, I generate a yuv420p file with
>>>>> samples in the out of broadcast range via:
>>>>> ffmpeg -f lavfi -i
>>>>> "nullsrc=s=256x256,geq=random(1)/hypot(X-cos(N*0.07)*W/2-W/2\,Y-sin(N*0.09)*H/2-H/2)^2*1000000*sin(N*0.02):128:128"
> -c:v ffv1 -t 10 sample.mkv
>>>>> I can then clip the samples using clipval in the lut filter and
>>>>> see the clipped values in a waveform with:
>>>>> ffmpeg -i sample.mkv -vf lut=y=clipval:u=clipval:v=clipval -c:v
>>>>> ffv1 -f nut - | ffplay - -vf waveform The waveform shows the
>>>>> values clipped at 16 and 235.
>>>>> However I need the output to use mpeg2video. When I change
>>>>> lossless ffv1 to mpeg2video with:
>>>>> ffmpeg -i sample.mkv -vf lut=y=clipval:u=clipval:v=clipval
>>>>> -c:v mpeg2video -f nut - | ffplay - -vf waveform
>>>>> The output shows many sample values above 235. The lossy
>>>>> mpeg2video encoding seems to place values above 235 while the
>>>>> lossless ffv1 doesn't. Is there a way to encode in mpeg2video
>>>>> without having values over 235 in the output?
>>>> Appears to be mpeg2video bug, can others confirm?
>>> It appears that having out of range values is OK, its player
>>> responsibility to clip values that are out of range.
>> The players involved are broadcast television servers which do not
>> clip the values. The broadcast specification that I'm trying to
>> adhere to requires both mpeg2 encoding and clipping out of range
>> values. Dave Rice
> Disclaimer - I know nothing about broadcasting!
> Random observations.
> I have looked at the yuv values of decoded broadcast mpeg2/h264 and
> there are plenty of over/under samples produced.
> Your sample is quite extreme so has more than "real" stuff would.

OK, I made a new test with real world stuff. I uploaded the real-world sample along with a waveform gif and then waveform gifs for the sample encoded to ffv1 and mpeg2video. It is here: https://github.com/dericed/ffmpeg-mpeg2video-clipping/blob/master/README.md <https://github.com/dericed/ffmpeg-mpeg2video-clipping/blob/master/README.md>

The git of the ffv1 encoding shows that out of broadcast range values are clipped (by the lut filter), but with the mpeg2video encoding it introduces many out of broadcast range values.

> If your source is full range then maybe it should be scaled rather than
> clipped.

The source file is yuv422p10le, not yuvj422p10le. I believe the sample is intended to be interpreted at broadcast range and not full range, though it has values out of broadcast range.

> "The players involved are broadcast television servers"
> confuses me - are they mpeg encoders needing clipped raw input, in which
> case the lut does its job, or do they take some high bitrate mpeg2 you
> are trying to make? If so maybe there will be less over/under from that
> than this test.

The mpeg decoders (of the broadcast server) need high bitrate mpeg2 with clipped luma values. However when the mpeg2video encoder encodes the clipped output of lut, it adds out of range values back.
Dave Rice

More information about the ffmpeg-user mailing list