Opened 12 years ago

Last modified 3 years ago

#1766 new defect

Can't encode MPEG-2 at more than ~3Mb/s

Reported by: Agustín Dall'Alba Owned by:
Priority: normal Component: undetermined
Version: git-master Keywords: mpeg2video dvd
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug: I'm trying to convert a 720p25 video to DVD-spec 6500kbps MPEG2, but the output is <3Mb/s instead. The subjective quality is not that good, there are blocks in the background and the picture lacks sharpness. I'm using ffmpeg version git-2012-09-26-299c0b3.

The source video is a remuxed Sherlock.S01E01.720p.BluRay.x264-AVCHD, that can be easily found in P2P sites. With some guidance I can try encoding the original BluRay, but ripping it on linux has proven itself way too difficult for me and I figured it would be the same.

I originally used a much more complicated command line, but the issue is still there with the stripped-down ffmpeg -i "$1" -target pal-dvd -b:v 6450k -an -pass 1/2 "$1".mpg -report

I've attached ffmpeg's reports and the two-pass log, please tell me if you need anything else, I'll try to reply as soon as I can. Thanks!

Attachments (12)

report_firtspass.log.gz (203.0 KB ) - added by Agustín Dall'Alba 12 years ago.
report_secondpass.log.gz (226.6 KB ) - added by Agustín Dall'Alba 12 years ago.
Sherlock S01E01 sample.mkv (2.4 MB ) - added by Agustín Dall'Alba 12 years ago.
ffmpeg-20120927-122348.log (375.0 KB ) - added by Agustín Dall'Alba 12 years ago.
out.mpg (296.0 KB ) - added by Agustín Dall'Alba 12 years ago.
Output for Sherlock S01E01 sample.mkv
out.mpg.png (138.1 KB ) - added by Agustín Dall'Alba 12 years ago.
Enhanced frame from out.mpg
outframe26.jpg (18.4 KB ) - added by Agustín Dall'Alba 12 years ago.
Untouched still from out.mpg.
sourceframe26.png (639.4 KB ) - added by Agustín Dall'Alba 12 years ago.
Untouched frame from the sample, PNG format.
sourceframe26_scaled.jpg (19.6 KB ) - added by Agustín Dall'Alba 12 years ago.
Untouched frame from the sample, JPEG quality 0, quite blocky.
sourceframe26_scaled.2.jpg (19.6 KB ) - added by Agustín Dall'Alba 12 years ago.
Scaled frame from the sample, JPEG quality 0, very blocky.
sourceframe26.jpg (53.5 KB ) - added by Agustín Dall'Alba 12 years ago.
Untouched frame from the sample, JPEG quality 0, quite blocky.
sourceframe26_scaled.png (291.6 KB ) - added by Agustín Dall'Alba 12 years ago.
Scaled frame from the sample for comparison, PNG format.

Change History (23)

by Agustín Dall'Alba, 12 years ago

Attachment: report_firtspass.log.gz added

by Agustín Dall'Alba, 12 years ago

Attachment: report_secondpass.log.gz added

comment:1 by Carl Eugen Hoyos, 12 years ago

Keywords: mpeg2video, dvd → mpeg2video dvd
Version: unspecifiedgit-master

You could compare the output file size with the following:
$ ffmpeg -i Sherlock\ S01E01.mp4 -s 720x576 -qscale 2 -acodec ac3 -ab 448k out.mpg

If you still see the blocks and other quality issues, try to cut the relevant part from the original sample with:
$ ffmpeg -ss xxx -i Sherlock\ S01E01.mp4 -vcodec copy -t yyy out.h264
and provide the sample (if it does not show the same visual problems)

by Agustín Dall'Alba, 12 years ago

Attachment: Sherlock S01E01 sample.mkv added

by Agustín Dall'Alba, 12 years ago

Attachment: ffmpeg-20120927-122348.log added

comment:2 by Agustín Dall'Alba, 12 years ago

out.mpg is 2242MB long, averaging 3393 kb/s for video + audio. However ffprobe says

Input #0, mpeg, from 'out.mpg':
  Duration: 01:28:05.76, start: 0.994667, bitrate: 3393 kb/s
    Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p, 720x576 [SAR 1:1 DAR 5:4], 104857 kb/s, 25 fps, 25 tbr, 90k tbn, 25 tbc
    Stream #0:1[0x80]: Audio: ac3, 48000 Hz, 5.1(side), s16, 448 kb/s

Note the 104857 kb/s in the first stream.

FFmpeg complained a lot during the transcode about with [mpeg1video @ 0x1d56f40] warning, clipping 1 dct coefficients to -255..255

I've attached the full report and a short enough sample that is considerably blocky after after the transcode on my setup, particularly in the background.

comment:3 by Carl Eugen Hoyos, 12 years ago

I tested the following:
$ ffmpeg -i Sherlock\ S01E01\ sample.mkv -target pal-dvd -b:v 6450k -pass 1 out.mpg
$ ffmpeg -i Sherlock\ S01E01\ sample.mkv -target pal-dvd -b:v 6450k -pass 2 out.mpg

I don't see any quality issues with out.mpg.
Please provide an input sample that allows to reproduce quality issues for the output file.

Concerning the bitrate: Please understand that you cannot reach 6k for every input sample, if you encode with -qscale 2 you should see the approximate maximum file size.

comment:4 by Agustín Dall'Alba, 12 years ago

Using your exact command line with ffmpeg version git-2012-10-06-293e542 outputs a blocky background with heavy banding in my setup. I'll attach my out.mpg and a frame from it enhanced to show the banding that, while is not very visible in a well lit room, pops out in a darkened living room.

by Agustín Dall'Alba, 12 years ago

Attachment: out.mpg added

Output for Sherlock S01E01 sample.mkv

by Agustín Dall'Alba, 12 years ago

Attachment: out.mpg.png added

Enhanced frame from out.mpg

comment:5 by Carl Eugen Hoyos, 12 years ago

I can reproduce the banding with out.mpg.png but not with out.mpg.
How did you produce the png? Could you compare with:
$ ffmpeg -i out.mpg out%2d.png
or, since there may be quality loss when encoding from yuv420p (mpg) to rgb (png):
$ ffmpeg -i out.mpg -qscale 2 out%2d.jpg

comment:6 by Agustín Dall'Alba, 12 years ago

To produce out.mpg.png I extracted a frame with mplayer (in PNG) and then did a levels adjustment in The GIMP to bring out the banding, although It's clearly visible without the enhancement anyway. It's an artifact I'd expect to have at such a low bitrate, but I was aiming for the best quality I could get in a DVD5.

To discard a decoding issue I tested the file in Windows Media Player, VLC, and a standalone DVD player, and the blockiness is there in the three of them.

by Agustín Dall'Alba, 12 years ago

Attachment: outframe26.jpg added

Untouched still from out.mpg.

comment:7 by Agustín Dall'Alba, 12 years ago

I just tried to extract a jpg still from the source, and it showed the same kind of blockiness in the jpg output. PNG, on the other side, was perfect.

ffmpeg -v 9 -loglevel 99 -i Sherlock\ S01E01\ sample.mkv -ss 1 -vframes 1 -qscale 0 sourceframe26.jpg
ffmpeg version 1.0 Copyright (c) 2000-2012 the FFmpeg developers
  built on Sep 29 2012 11:22:50 with gcc 4.7.1 (GCC) 20120721 (prerelease)
  configuration: --prefix=/usr --enable-libmp3lame --enable-libvorbis --enable-libxvid --enable-libx264 --enable-libvpx --enable-libtheora --enable-libgsm --enable-libspeex --enable-postproc --enable-shared --enable-x11grab --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libschroedinger --enable-libopenjpeg --enable-librtmp --enable-libpulse --enable-libv4l2 --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-debug --disable-static
  libavutil      51. 73.101 / 51. 73.101
  libavcodec     54. 59.100 / 54. 59.100
  libavformat    54. 29.104 / 54. 29.104
  libavdevice    54.  2.101 / 54.  2.101
  libavfilter     3. 17.100 /  3. 17.100
  libswscale      2.  1.101 /  2.  1.101
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
[matroska,webm @ 0x1e3e240] Format matroska,webm probed with size=2048 and score=100
st:0 removing common factor 1000000 from timebase
[matroska,webm @ 0x1e3e240] File position before avformat_find_stream_info() is 1586
[h264 @ 0x1e41120] Using externally provided dimensions
[h264 @ 0x1e41120] no picture 
[matroska,webm @ 0x1e3e240] All info found
[matroska,webm @ 0x1e3e240] File position after avformat_find_stream_info() is 1908009
Input #0, matroska,webm, from 'Sherlock S01E01 sample.mkv':
  Metadata:
    -- CUT --
    ENCODER         : Lavf54.28.101
  Duration: 00:00:00.76, start: 0.000000, bitrate: 26954 kb/s
    Stream #0:0(eng), 45, 1/1000: Video: h264 (High), yuv420p, 1280x720, 1/50, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      CREATION_TIME   : 2010-08-28 16:03:10
      LANGUAGE        : eng
      HANDLER_NAME    : VideoHandler
Please use -q:a or -q:v, -qscale is ambiguous
[buffer @ 0x2262480] Setting entry with key 'video_size' to value '1280x720'
[buffer @ 0x2262480] Setting entry with key 'pix_fmt' to value '0'
[buffer @ 0x2262480] Setting entry with key 'time_base' to value '1/1000'
[buffer @ 0x2262480] Setting entry with key 'pixel_aspect' to value '1/1'
[buffer @ 0x2262480] Setting entry with key 'sws_param' to value 'flags=2'
[buffer @ 0x2262480] Setting entry with key 'frame_rate' to value '25/1'
[graph 0 input from stream 0:0 @ 0x1e43460] w:1280 h:720 pixfmt:yuv420p tb:1/1000 fr:25/1 sar:1/1 sws_param:flags=2
[format @ 0x2275e40] auto-inserting filter 'auto-inserted scaler 0' between the filter 'Parsed_null_0' and the filter 'format'
[auto-inserted scaler 0 @ 0x2310b00] picking yuvj420p out of 2 ref:yuv420p alpha:0
[auto-inserted scaler 0 @ 0x2310b00] w:1280 h:720 fmt:yuv420p sar:1/1 -> w:1280 h:720 fmt:yuvj420p sar:1/1 flags:0x4
[mjpeg @ 0x2212c20] detected 4 logical cores
[mjpeg @ 0x1e79640] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x1e92240] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x1fe2ea0] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x202c360] intra_quant_bias = 96 inter_quant_bias = 0
[mjpeg @ 0x2212c20] intra_quant_bias = 96 inter_quant_bias = 0
[h264 @ 0x1e41120] detected 4 logical cores
Output #0, image2, to 'sourceframe26.jpg':
  Metadata:
    -- CUT --
    encoder         : Lavf54.29.104
    Stream #0:0(eng), 0, 1/90000: Video: mjpeg, yuvj420p, 1280x720 [SAR 1:1 DAR 16:9], 1/25, q=2-31, 200 kb/s, 90k tbn, 25 tbc (default)
    Metadata:
      CREATION_TIME   : 2010-08-28 16:03:10
      LANGUAGE        : eng
      HANDLER_NAME    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> mjpeg)
Press [q] to stop, [?] for help
[h264 @ 0x25e45e0] Using externally provided dimensions
[h264 @ 0x25e45e0] no picture 
[h264 @ 0x2629420] no picture 
No more output streams to write to, finishing.
frame=    1 fps=0.0 q=0.0 Lsize=       0kB time=00:00:00.04 bitrate=   0.0kbits/s    
video:54kB audio:0kB subtitle:0 global headers:0kB muxing overhead -100.000000%
[AVIOContext @ 0x1e46900] Statistics: 1936179 bytes read, 0 seeks
ffmpeg -v 9 -loglevel 99 -i Sherlock\ S01E01\ sample.mkv -ss 1 -s 720x576 -vframes 1 -qscale 0 sourceframe26_scaled.png
ffmpeg version 1.0 Copyright (c) 2000-2012 the FFmpeg developers
  built on Sep 29 2012 11:22:50 with gcc 4.7.1 (GCC) 20120721 (prerelease)
  configuration: --prefix=/usr --enable-libmp3lame --enable-libvorbis --enable-libxvid --enable-libx264 --enable-libvpx --enable-libtheora --enable-libgsm --enable-libspeex --enable-postproc --enable-shared --enable-x11grab --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libschroedinger --enable-libopenjpeg --enable-librtmp --enable-libpulse --enable-libv4l2 --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-debug --disable-static
  libavutil      51. 73.101 / 51. 73.101
  libavcodec     54. 59.100 / 54. 59.100
  libavformat    54. 29.104 / 54. 29.104
  libavdevice    54.  2.101 / 54.  2.101
  libavfilter     3. 17.100 /  3. 17.100
  libswscale      2.  1.101 /  2.  1.101
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
[matroska,webm @ 0xd88240] Format matroska,webm probed with size=2048 and score=100
st:0 removing common factor 1000000 from timebase
[matroska,webm @ 0xd88240] File position before avformat_find_stream_info() is 1586
[h264 @ 0xd8b120] Using externally provided dimensions
[h264 @ 0xd8b120] no picture 
[matroska,webm @ 0xd88240] All info found
[matroska,webm @ 0xd88240] File position after avformat_find_stream_info() is 1908009
Input #0, matroska,webm, from 'Sherlock S01E01 sample.mkv':
  Metadata:
    -- CUT --
    ENCODER         : Lavf54.28.101
  Duration: 00:00:00.76, start: 0.000000, bitrate: 26954 kb/s
    Stream #0:0(eng), 45, 1/1000: Video: h264 (High), yuv420p, 1280x720, 1/50, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      CREATION_TIME   : 2010-08-28 16:03:10
      LANGUAGE        : eng
      HANDLER_NAME    : VideoHandler
Please use -q:a or -q:v, -qscale is ambiguous
[buffer @ 0x11ac480] Setting entry with key 'video_size' to value '1280x720'
[buffer @ 0x11ac480] Setting entry with key 'pix_fmt' to value '0'
[buffer @ 0x11ac480] Setting entry with key 'time_base' to value '1/1000'
[buffer @ 0x11ac480] Setting entry with key 'pixel_aspect' to value '1/1'
[buffer @ 0x11ac480] Setting entry with key 'sws_param' to value 'flags=2'
[buffer @ 0x11ac480] Setting entry with key 'frame_rate' to value '25/1'
[graph 0 input from stream 0:0 @ 0xd8d460] w:1280 h:720 pixfmt:yuv420p tb:1/1000 fr:25/1 sar:1/1 sws_param:flags=2
[scaler for output stream 0:0 @ 0x1182200] picking rgb24 out of 7 ref:yuv420p alpha:0
[scaler for output stream 0:0 @ 0x1182200] w:1280 h:720 fmt:yuv420p sar:1/1 -> w:720 h:576 fmt:rgb24 sar:64/45 flags:0x4
[png @ 0x115cc20] detected 4 logical cores
[h264 @ 0xd8b120] detected 4 logical cores
Output #0, image2, to 'sourceframe26_scaled.png':
  Metadata:
    -- CUT --
    encoder         : Lavf54.29.104
    Stream #0:0(eng), 0, 1/90000: Video: png, rgb24, 720x576 [SAR 64:45 DAR 16:9], 1/25, q=2-31, 200 kb/s, 90k tbn, 25 tbc (default)
    Metadata:
      CREATION_TIME   : 2010-08-28 16:03:10
      LANGUAGE        : eng
      HANDLER_NAME    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> png)
Press [q] to stop, [?] for help
[h264 @ 0xe746c0] Using externally provided dimensions
[h264 @ 0xe746c0] no picture 
[h264 @ 0xeb9020] no picture 
No more output streams to write to, finishing.
frame=    1 fps=0.0 q=0.0 Lsize=       0kB time=00:00:00.04 bitrate=   0.0kbits/s    
video:309kB audio:0kB subtitle:0 global headers:0kB muxing overhead -100.000000%
[AVIOContext @ 0xd90900] Statistics: 1936179 bytes read, 0 seeks

by Agustín Dall'Alba, 12 years ago

Attachment: sourceframe26.png added

Untouched frame from the sample, PNG format.

by Agustín Dall'Alba, 12 years ago

Attachment: sourceframe26_scaled.jpg added

Untouched frame from the sample, JPEG quality 0, quite blocky.

by Agustín Dall'Alba, 12 years ago

Attachment: sourceframe26_scaled.2.jpg added

Scaled frame from the sample, JPEG quality 0, very blocky.

by Agustín Dall'Alba, 12 years ago

Attachment: sourceframe26.jpg added

Untouched frame from the sample, JPEG quality 0, quite blocky.

by Agustín Dall'Alba, 12 years ago

Attachment: sourceframe26_scaled.png added

Scaled frame from the sample for comparison, PNG format.

comment:8 by Balling, 3 years ago

I do not think this is a bug. Less bits are used in darker regions, this is normal.

comment:9 by Agustín Dall'Alba, 3 years ago

Thank you for reading my old bug report. Surprisingly I can still reproduce this on ffmpeg n4.4 with libavcodec 58.134.100!

I don't understand why mpeg2video does not allocate more bits to preserve the film grain even when set at the maximum quality:

ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v mpeg2video -s 720x576 -q:v 1 -an test2.mkv

Even with -q 1 it still has visible banding and blocking where there was none in the source. Thinking maybe the scaler was to blame, I tried with other codecs:

ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v mjpeg -s 720x576 -q:v 1 -an testj.mkv
ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v mpeg1video -s 720x576 -q:v 1 -an test1.mkv
ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v mpeg4 -s 720x576 -q:v 1 -an test4.mkv
ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v libxvid -s 720x576 -q:v 1 -an testx.mkv
ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v libx264 -s 720x576 -crf 1 testh.mkv

mjpeg, mpeg1, mpeg2,and mpeg4 had the banding and blocking, but surprisingly libxvid made a file 5× larger that looked great and preserved the noise, even though it's the same format that mpeg4. libx264 also had no trouble. So perhaps there's something wrong a quantizer that's shared between all the internal mpeg* codecs?

Anyway, I no longer use these codecs for anything, so if you're not interested in looking more into this you might as well close it wontfix.

Cheers

in reply to:  9 comment:10 by pdr0, 3 years ago

Replying to Agustín Dall'Alba:

I don't understand why mpeg2video does not allocate more bits to preserve the film grain even when set at the maximum quality:

ffmpeg -i Sherlock\ S01E01\ sample.mkv -c:v mpeg2video -s 720x576 -q:v 1 -an test2.mkv

Even with -q 1 it still has visible banding and blocking where there was none in the source. Thinking maybe the scaler was to blame, I tried with other codecs:

That's not "maximum quality" - you haven't set the max quantizer, and you could use intra if you wanted to . That's for unrestricted use, not for DVD-video using compliant settings (you'd need vbv restriction and a few other things)

-q:v 1
1539kb/s 0.32MB
ffmpeg -i "Sherlock S01E01 sample.mkv" -c:v mpeg2video -s 720x576 -q:v 1 -an test2.mkv

limit qmax
4784kb/s 0.97MB
ffmpeg -i "Sherlock S01E01 sample.mkv" -c:v mpeg2video -s 720x576 -q:v 1 -qmin 1 -qmax 1 -an test3.mkv

limit qmax + intra
6113kb/s 1.24MB
ffmpeg -i "Sherlock S01E01 sample.mkv" -c:v mpeg2video -s 720x576 -q:v 1 -qmin 1 -qmax 1 -g 1 -an test4.mkv

comment:11 by Agustín Dall'Alba, 3 years ago

Thanks. Adding -qmin 1 -qmax 1 does seem to improve things but you can still clearly see blockiness in the background with the naked eye. I can't tell the difference with or without -g 1. There has to be a way for mpeg2video to deliver better quality than this.

I don't have easy access to another MPEG-2 encoder, however ffmpeg's mpeg4 suffers the same with -q:v 1 -qmin 1 -qmax 1 while libxvid preserves the film grain very well with nothing more than -q:v 1 . This makes me think that something's not right with libavcodec's mpeg* encoders.

Is there no way to encode MPEG-2 with ffmpeg and keep the film grain intact in this scene?

Cheers.

Last edited 3 years ago by Agustín Dall'Alba (previous) (diff)
Note: See TracTickets for help on using tickets.