Post subject: FFmpeg writer: wrong colors in MP4
Sonia
She/Her
Joined: 12/6/2013
Posts: 435
Location: Brazil
I was messing around with Bizhawk's FFmpeg writer and noticed that MP4 dumps the colors very inaccurately. Here's a side by side comparison between it and Matroska Lossless: https://i.imgur.com/Gdw9Scy.png Left = Matroska Lossless / Right = MP4 As it can be seen, MP4 looks much brighter for some reason. Matroska matches what you see in the emulator, but MP4 doesn't. Is it normal for MP4 to look that way or is it something that needs to be fixed? Edit by feos: Unembedded the huge images.
Publisher
Joined: 4/23/2009
Posts: 1283
Seems more a levels issue to me. Edit: What FFMPEG settings did you do?
Sonia
She/Her
Joined: 12/6/2013
Posts: 435
Location: Brazil
Aktan wrote:
Seems more a levels issue to me. Edit: What FFMPEG settings did you do?
I didn't change any settings. Just selected MP4 and ran it as it is. Used Bizhawk 2.3 btw.
Publisher
Joined: 4/23/2009
Posts: 1283
Hmm, just cross my mind, how are you decoding it?
Sonia
She/Her
Joined: 12/6/2013
Posts: 435
Location: Brazil
I'm not doing anything extraordinary, really. Here's the step by step: 1. File > AVI/WAV > Config and Record AVI/WAV 2. FFmpeg Writer > MP4 > OK > Save
Publisher
Joined: 4/23/2009
Posts: 1283
No, you're misunderstanding. How are you getting that screenshot above? I think it be easier if you just post the info both those clips produce in MediaInfo, if you can.
Sonia
She/Her
Joined: 12/6/2013
Posts: 435
Location: Brazil
Aktan wrote:
No, you're misunderstanding. How are you getting that screenshot above?
Ah, for that I used Media Player Classic and took a print screen of the video. Might also note that I used the "Resize Video" feature of the emulator and set the size to 512x448, just for that pic.
Aktan wrote:
I think it be easier if you just post the info both those clips produce in MediaInfo, if you can.
Ok, here you go:
MKV
General
Unique ID                                : 301371894229990205192800449782004226462 (0xE2BA1D94A5A5908B21DBF5D06C06999E)
Complete name                            : C:\Users\siste\Desktop\X.mkv
Format                                   : Matroska
Format version                           : Version 2
File size                                : 64.3 KiB
Duration                                 : 205 ms
Overall bit rate                         : 2 569 kb/s
Writing application                      : Lavf54.6.101
Writing library                          : Lavf54.6.101

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High 4:4:4 Predictive@L2.1
Format settings                          : CABAC / 3 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 3 frames
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 200 ms
Width                                    : 256 pixels
Height                                   : 224 pixels
Display aspect ratio                     : 1.143
Frame rate mode                          : Constant
Frame rate                               : 60.098 FPS
Color space                              : RGB
Bit depth                                : 8 bits
Scan type                                : Progressive
Writing library                          : x264 core 125 r2200 999b753
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=0 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=0 / chroma_qp_offset=0 / threads=1 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc=cqp / mbtree=0 / qp=0
Default                                  : Yes
Forced                                   : No
Color range                              : Limited
Matrix coefficients                      : Identity

Audio
ID                                       : 2
Format                                   : PCM
Codec ID                                 : A_PCM/INT/LIT
Duration                                 : 205 ms
Bit rate mode                            : Constant
Channel(s)                               : 2 channels
Sampling rate                            : 44.1 kHz
Bit depth                                : 16 bits
Default                                  : Yes
Forced                                   : No
MP4
General
Complete name                            : C:\Users\siste\Desktop\X.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 13.5 KiB
Duration                                 : 242 ms
Overall bit rate                         : 458 kb/s
Writing application                      : Lavf54.6.101

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L2.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 200 ms
Bit rate                                 : 325 kb/s
Width                                    : 256 pixels
Height                                   : 224 pixels
Display aspect ratio                     : 1.143
Frame rate mode                          : Constant
Frame rate                               : 60.098 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.094
Stream size                              : 7.92 KiB (59%)
Writing library                          : x264 core 125 r2200 999b753
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=6 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=23.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : mp4a-40-2
Duration                                 : 242 ms
Duration_LastFrame                       : -14 ms
Bit rate mode                            : Constant
Bit rate                                 : 136 kb/s
Channel(s)                               : 2 channels
Channel positions                        : Front: L R
Sampling rate                            : 44.1 kHz
Frame rate                               : 43.066 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 3.99 KiB (29%)
Default                                  : Yes
Alternate group                          : 1
Publisher
Joined: 4/23/2009
Posts: 1283
Okay thanks, your MediaInfo is weird with all the repeats. Anyway, the MKV is in RGB with color range info while the MP4 is in YV12 with no color range, so it was guessed as limited in MPC. I'm thinking if you set the flag to be full color range in the MP4, it will have the correct colors, assuming MPC is setup correctly. Edit: I guess you fixed the info, lol.
Sonia
She/Her
Joined: 12/6/2013
Posts: 435
Location: Brazil
Aktan wrote:
Edit: I guess you fixed the info, lol.
Yeah, that's because Advanced mode was checked. Seems like it only adds redundant info for the most part. :P Anyway, I spent the past couple hours messing around with MPC's video decoder settings and that didn't solve anything, so I gave up and switched to VLC. The colors show up correctly there (or at least closer to what you see in the emulator), so things are good now.
Joined: 10/14/2013
Posts: 335
Location: Australia
Aktan wrote:
I'm thinking if you set the flag to be full color range in the MP4, it will have the correct colors
It should probably be set to write the metadata in Bizhawk for cases like this. Do you know the ffmpeg command? (I think it's -color_range 2, but I'd like to be certain).
I'm not as active as I once was, but I can be reached here if I should be needed.
Publisher
Joined: 4/23/2009
Posts: 1283
Sonia wrote:
Anyway, I spent the past couple hours messing around with MPC's video decoder settings and that didn't solve anything, so I gave up and switched to VLC. The colors show up correctly there (or at least closer to what you see in the emulator), so things are good now.
Sorry, I should have clarified that it's still bad that the encoded file doesn't have the correct flag (aka Bizhawk should be telling FFMPEG to write the flag). All I was saying is that it should be an easy fix without the need of capturing again. This is assuming it is a flag issue, which could be, but I don't know the full FFMPEG line Bizhawk uses. The fact that it is corrected there in VLC is really weird. Unless the flag is there, VLC should assume limited color range and look incorrect still. You can try something like method #4 listed here: http://avisynth.nl/index.php/Luminance_levels. Try the shader "0-255 to 16-235" or the opposite. For good measure, try the shader "BT.601 to BT.709".
TheCoreyBurton wrote:
It should probably be set to write the metadata in Bizhawk for cases like this. Do you know the ffmpeg command? (I think it's -color_range 2, but I'd like to be certain).
I would make sure my assumption is correct first before finding the flag. While writing this post, I was second guessing myself if indeed this is a levels problem. I'll do a test locally to speed things up, then I can actually figure the problem instead of guessing, but I really think it's not that FFMPEG is doing something wrong, it's more like just a missing flag, either range flag or color matrix flag. Edit: Okay, I did the test locally. Basically Bizhawk sends it to FFMPEG and FFMPEG guesses on a lot of the settings (aka uses default). Most people won't know how to set it correctly hence a simple line is used, but to get it correct, the line is a lot longer. It also doesn't help that Bizhawk came with an older version of FFMPEG. What you can do is get a newer version and place it in the dll folder of Bizhawk. Then in FFmpeg writer settings, choose [Custom] and use this as the command:
-vf scale=out_color_matrix=bt601:in_range=pc:out_range=tv -c:a aac -c:v libx264 -f mp4 -color_primaries smpte170m -color_trc smpte170m -colorspace smpte170m -color_range tv
Notice this is a lot longer than the simple
-c:a libvo_aacenc -c:v libx264 -f mp4
I should note the line I used is meant for SD games. HD games should have a different line. Also note, I choose limited color range since it is more widely used. Some may want to change the range to be full. As you can see, there is a lot of "complications" when choosing the options.