Lossless and colour preserving pix_fmt?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Lossless and colour preserving pix_fmt?

Jim DeLaHunt
Hello, ffmpeg'rs:

I'm trying to overlay animated GIFS onto one white background, with some
text at the top.  I'm looking for an incantation which a) does the job
and b) leaves all the colours of the animated gifs unchanged.

What options tell ffmpeg not to mess with the colour values, not to
change colour spaces, not to reduce precision of colour coordinate values?

When I try what seems obvious, I get an animated GIF result which has
the right layout, but has pixelation of some of the colours in the
animated gifs. For instance, solid 0000FF pixels in the input image
animated_blue_12.gif became speckled with black pixels in the output. I
suspect there is a colour space transform happening, and I don't want that.

% ffmpeg -i animated_blue_12.gif -filter_complex "
color=white@1.0:size=696x400,
drawtext=fontsize=20:font=Arial:x=10:y=10:text=Hamburgefons Hamburgefons Hamburgefons
       [background];
[0:v] setpts=PTS-STARTPTS [gs1];
[background][gs1] overlay=shortest=1:x=20:y=40
" -y blue+text_pixfmt.696x400.gif

I tried to insert `-pix_fmt +` to get all the parts of the overlay to
agree on the same pixel format, but that failed:

% ffmpeg -i animated_blue_12.gif -filter_complex "
color=white@1.0:size=696x400,
drawtext=fontsize=20:font=Arial:x=10:y=10:text=Hamburgefons Hamburgefons Hamburgefons
       [background];
[0:v] setpts=PTS-STARTPTS [gs1];
[background][gs1] overlay=shortest=1:x=20:y=40
" -pix_fmt + -y blue+text_pixfmt.696x400.gif
ffmpeg version 3.4.1 Copyright (c) 2000-2017 the FFmpeg developers
   built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
   configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample
--enable-libmp3lame --enable-libvorbis --enable-libopus --enable-librsvg --enable-libtheora
--enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libsoxr --enable-libspeex
--enable-libass --enable-libbluray --enable-lzma --enable-gnutls --enable-fontconfig --enable-libfreetype
--enable-libfribidi --disable-indev=jack --enable-opencl --disable-outdev=xv --enable-audiotoolbox
--enable-videotoolbox --enable-sdl2 --mandir=/opt/local/share/man --enable-shared --enable-pthreads
--cc=/usr/bin/clang --arch=x86_64 --enable-x86asm --enable-libx265 --enable-gpl --enable-postproc
--enable-libx264 --enable-libxvid
   libavutil      55. 78.100 / 55. 78.100
   libavcodec     57.107.100 / 57.107.100
   libavformat    57. 83.100 / 57. 83.100
   libavdevice    57. 10.100 / 57. 10.100
   libavfilter     6.107.100 /  6.107.100
   libavresample   3.  7.  0 /  3.  7.  0
   libswscale      4.  8.100 /  4.  8.100
   libswresample   2.  9.100 /  2.  9.100
   libpostproc    54.  7.100 / 54.  7.100
Input #0, gif, from 'animated_blue_12.gif':
   Duration: N/A, bitrate: N/A
     Stream #0:0: Video: gif, bgra, 212x318, 100 tbr, 100 tbn, 100 tbc
[Parsed_drawtext_1 @ 0x7f83c3e35620] Using "/Library/Fonts/Arial.ttf"
Stream mapping:
   Stream #0:0 (gif) -> setpts
   overlay -> Stream #0:0 (gif)
Press [q] to stop, [?] for help
[Parsed_drawtext_1 @ 0x7f83c3c0f560] Using "/Library/Fonts/Arial.ttf"
The filters 'Parsed_setpts_2' and 'Parsed_overlay_3' do not have a common format and automatic conversion is disabled.
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!

How do I tell ffmpeg not to mess with the colour values of this
video/animated GIF?

Thanks in advance,
       —Jim DeLaHunt, Vancouver, Canada

--
     --Jim DeLaHunt, [hidden email]     http://blog.jdlh.com/ (http://jdlh.com/)
       multilingual websites consultant

       355-1027 Davie St, Vancouver BC V6E 4L2, Canada
          Canada mobile +1-604-376-8953

_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Lossless and colour preserving pix_fmt?

Carl Eugen Hoyos-2
2018-01-08 6:29 GMT+01:00 Jim DeLaHunt <[hidden email]>:

> I'm trying to overlay animated GIFS onto one white background, with some
> text at the top.  I'm looking for an incantation which a) does the job and

> b) leaves all the colours of the animated gifs unchanged.

Depending on the input file, this may not be possible.

To create a (nice looking) gif - with most likely different colours - you
need the palettegen and paletteuse filters.

Carl Eugen
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Lossless and colour preserving pix_fmt?

Jim DeLaHunt
In reply to this post by Jim DeLaHunt
On 2018-01-07 21:29, Jim DeLaHunt wrote:
> Hello, ffmpeg'rs:
>
> I'm trying to overlay animated GIFS onto one white background, with
> some text at the top.  I'm looking for an incantation which a) does
> the job and b) leaves all the colours of the animated gifs unchanged.
>
> What options tell ffmpeg not to mess with the colour values, not to
> change colour spaces, not to reduce precision of colour coordinate
> values?

The best answer to this appeared in an answer to SuperUser question,
/Lossless universal video format /[1]
<https://superuser.com/questions/486325/lossless-universal-video-format>.
[1] https://superuser.com/questions/486325/lossless-universal-video-format

Preserve colour space? Sorry, none. The Animated GIF colour values are
defined as 24-bit RGB values in sRGB space.  As far as I can tell it's
hard to get FFmpeg to use RGB for its operations, it really pushes you
to YUV (luminance and two chroma values). However, the YUV values are
24-bits. I speculate that perceptually, a conversion from sRGB to YUV
back to sRGB will be lossless.

You need to prevent lossy compression by the pixel format (encoder). The
*libx264* encoder with the option *-crf 0* provides lossless compression.

You also need to prevent downsampling of the U,V components of the YUV
colour value. This is explained well at /Recommended 8-Bit YUV Formats
for Video Rendering/
<https://msdn.microsoft.com/en-us/library/windows/desktop/dd206750%28v=vs.85%29.aspx>,
by Gary Sullivan and Stephen Estrop, Microsoft Corporation, 2002 and 2008.
[2]
https://msdn.microsoft.com/en-us/library/windows/desktop/dd206750(v=vs.85).aspx 
<https://msdn.microsoft.com/en-us/library/windows/desktop/dd206750%28v=vs.85%29.aspx>

The term *YUV444* refers to Y,U,V colour space, and 4:4:4 downsampling,
i.e. U,V at same resolution as Y.
*YUV420* refers to U,V at 1/2 horizontal x 1/2 vertical = 1/4 overall
resolution as Y.  That works against lossless encoding. Be sure your
encoding is using a pix_fmt like YUV444. As far as I can tell, *libx264
-crf 0* defaults to YUV444 pixel format.

Then Carl Eugen points out something else important: how to generate a
good palette for the output GIF.

On 2018-01-08 04:25, Carl Eugen Hoyos wrote:
> Depending on the input file, this may not be possible.
>
> To create a (nice looking) gif - with most likely different colours - you
> need the palettegen and paletteuse filters.
>
I was making an assumption that when ffmpeg saved to GIF format, it by
default created a palette which would render the animation well. It
looks like that's incorrect, and the *palettegen* filter
<http://ffmpeg.org/ffmpeg-all.html#palettegen-1>[3] can generate a
better palette. Maybe there's some strong technical reason for this,
such as the GIF output having to created a palette based on information
only from the first frame, while palettegen can gather information from
all frames.
[3] http://ffmpeg.org/ffmpeg-all.html#palettegen-1

In any case, here's how I addressed the problem.  I broke the task into
four steps.

1. Read the animated GIF in, store as libx264 -crf 0 format in a
Matroshka container.

2. Run the x264 video through palettegen, generating a palette as a PNG
image.

3. Read the x264 video in again, passing it and the palette through the
paletteuse filter, saving as animated GIF.

4. Delete the x264 video and the palette.

This gave me acceptable quality. Here's roughly the commands I used:

% ffmpeg -i animated_blue_12.gif -filter_complex "
color=color=white@1.0:size=696x400,
drawtext=fontsize=20:font=Arial:x=10:y=10:text=Hamburgefons Hamburgefons Hamburgefons
       [background];
[0:v] setpts=PTS-STARTPTS [gs1];
[background][gs1] overlay=format=yuv444:shortest=1:x=20:y=40
" -c:v libx264 -crf 0 -preset ultrafast -y temp.mkv

% ffmpeg -i temp.mkv -vf palettegen palette.png

% ffmpeg -i temp.mkv -i palette.png -filter_complex paletteuse -y blue+text.gif

% rm temp.mkv palette.png

I hope that's helpful for someone!  Thanks,
        —Jim DeLaHunt, Vancouver, Canada

--
     --Jim DeLaHunt, [hidden email]     http://blog.jdlh.com/ (http://jdlh.com/)
       multilingual websites consultant

       355-1027 Davie St, Vancouver BC V6E 4L2, Canada
          Canada mobile +1-604-376-8953

_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Lossless and colour preserving pix_fmt?

Carl Eugen Hoyos-2
2018-01-11 4:52 GMT+01:00 Jim DeLaHunt <[hidden email]>:

> On 2018-01-07 21:29, Jim DeLaHunt wrote:
>>
>> Hello, ffmpeg'rs:
>>
>> I'm trying to overlay animated GIFS onto one white background, with some
>> text at the top.  I'm looking for an incantation which a) does the job and
>> b) leaves all the colours of the animated gifs unchanged.
>>
>> What options tell ffmpeg not to mess with the colour values, not to change
>> colour spaces, not to reduce precision of colour coordinate values?
>
>
> The best answer to this appeared in an answer to SuperUser question,
> /Lossless universal video format /[1]
> <https://superuser.com/questions/486325/lossless-universal-video-format>.

Because of its age, this "answer" is not only outdated but in your
context simply wrong.

> Preserve colour space? Sorry, none. The Animated GIF colour values are
> defined as 24-bit RGB values in sRGB space.  As far as I can tell it's hard
> to get FFmpeg to use RGB for its operations, it really pushes you to YUV
> (luminance and two chroma values).

This is - in the context you provide - not true, the overlay filter supports rgb
which should visually improve the quality of your output video.

The better choice for a lossless video is typically ffv1.

Carl Eugen
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Lossless and colour preserving pix_fmt?

Jim DeLaHunt
On 2018-01-10 20:15, Carl Eugen Hoyos wrote:

> 2018-01-11 4:52 GMT+01:00 Jim DeLaHunt <[hidden email]>:
>> On 2018-01-07 21:29, Jim DeLaHunt wrote:
>>> Hello, ffmpeg'rs:
>>>
>>> I'm trying to overlay animated GIFS onto one white background, with some
>>> text at the top.  I'm looking for an incantation which a) does the job and
>>> b) leaves all the colours of the animated gifs unchanged.
>>>
>>> What options tell ffmpeg not to mess with the colour values, not to change
>>> colour spaces, not to reduce precision of colour coordinate values?
>>
>> The best answer to this appeared in an answer to SuperUser question,
>> /Lossless universal video format /[1]
>> <https://superuser.com/questions/486325/lossless-universal-video-format>.
> Because of its age, this "answer" is not only outdated but in your
> context simply wrong.

Great!  Want some SuperUser points?  Write a better answer to that
question there, and I'd be happy to upvote it. Whatever it takes to get
reliable guidance on using FFmpeg into written form. :-)

Best regards,
      —Jim DeLaHunt, Vancouver, Canada

--
     --Jim DeLaHunt, [hidden email]     http://blog.jdlh.com/ (http://jdlh.com/)
       multilingual websites consultant

       355-1027 Davie St, Vancouver BC V6E 4L2, Canada
          Canada mobile +1-604-376-8953

_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".