Page 1 of 4

[SOLVED] Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 06, 2011 7:13 pm
by Illico
_Updated 01/08/2012_

The following is just a discussion on what has to be implemented in future Serviio versions (version 1.0) for this to work. You won't be able to pass any custom parameters to FFmpeg as of now.

Since version 1.0 Serviio use the following solution (2) , it was implemented and fix the Sony Aspect Ratio issue.


I worked on ffmpeg option to use for Sony Bravia TV Aspect Ratio fixing.

1) This is the best solution
- no need to use the zoom option on Sony Bravia Menu
- All pixels are displayed
- Correct Display Aspect Ratio
- But there is a CPU overhead for the upscaling into hd resolution

ffmpeg option to use with AVI files <720 height resolution
With DAR > = 16:9
"-vf "scale=1280:1280/dar/sar","pad=1280:720:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3"
With DAR < 16:9
"-vf "scale=720*dar*sar:720","pad=1280:720:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3"



2) An alternative - same that PS3 Media Server/Mencoder option -, resulting less cpu overhead, but some pixels (16) are lost on left and right side !!
- add padding to have a 16/9 aspect ratio (top/bot or left/right depends of the source format ratio)
- Change DAR=4/3

-vf "pad=max(iw,ih*16/9):max(ih,iw/16*9):(ow-iw)/2:(oh-ih)/2:black,setdar=4:3"

Explain:
With DAR > = 16:9
-vf "pad=iw:iw/16*9:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3"
With DAR < 16:9
-vf "pad=ih*16/9:ih:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3"


3) Workaround for testing purpose
viewtopic.php?f=11&t=2956&start=30#p34000



_________________________________
Sony Bravia Display configuration
HOME -> Display -> Screen:
- Screen Format/Wide Mode (option name varies depending on your region/country) : Wide
-------- Enlarges the picture horizontally to fill the screen when the original source is 4:3. When the original source is 16:9 (High-definition source), select this option to display the 16:9 picture in its original size.
- Auto Format : Off
-------- Automatically changes “Screen Format/Wide Mode” to match the input signal
- 4:3 Default : Off (greyed)
-------- Sets the default screen format for a 4:3 broadcast.
- Auto Display Area : Off (greyed)
-------- Automatically adjusts the picture to the most appropriate display area.
- Display Area : +1
-------- Adjusts the picture display area. Select “Full Pixel” to display the original picture when parts of the picture are cut,“+1” to display the original picture,“Normal” to display the recommended size picture, or “-1” to enlarge the picture
.

EXAMPLE

PostPosted: Sat Aug 06, 2011 7:25 pm
by Illico

Stream #0.0: Video: mpeg4, yuv420p, 640x336 [PAR 1:1 DAR 40:21], 23.98 fps, 23.98 tbr, 23.98 tbn, 2997 tbc
DAR: Display Aspect Ratio is not conventional (4:3 or 16:9)
Original_640x336.png
Original_640x336.png (293 KiB) Viewed 73695 times

    This file is transcoded with ffmpeg (serviio 0.5.2)
ffmpeg.exe -i "Test1.avi" -y -threads 2 -vcodec mpeg2video -b 17000k -maxrate 17000k -bufsize 5097k -r 24000/1001 -g 15 -copyts -acodec ac3 -ab 192k -ac 2 -map 0:0 -map 0:1 -sn -f mpegts "TEST.stf"
Stream #0.0: Video: mpeg2video, yuv420p, 640x336 [PAR 1:1 DAR 40:21], q=2-31 , 17000 kb/s, 90k tbn, 23.98 tbc
Serviio_0.6.2_default.png
Serviio_0.6.2_default.png (332.01 KiB) Viewed 73695 times

The red rectangle represents the area visible on the Sony Bravia TV screen
- Some pixels were lost on left and right (not on screen displayed)
- The video is crushed (see fireplace globes, or Arrietty face)
- TOP /BOTTOM black bars were added

    Workarround: On Sony Bravia TV Display option : Select ZOOM

Serviio_0.6.2_default_with_Zoom.png
Serviio_0.6.2_default_with_Zoom.png (281.32 KiB) Viewed 73695 times


- The video is not crushed or stretch : OK
- No black bars : OK
- But some pixels were lost on left and right : BAD


- Some links -
http://howto-pages.org/ffmpeg/#anamorphic

FFMPEG VIDEO FILTERING

PostPosted: Sun Aug 07, 2011 9:25 am
by Illico
    FFMPEG VIDEO FILTERING USED : SCALE, PAD and SETDAR option
ffmpeg.exe -i "Test1.avi" -y -threads 2 -vcodec mpeg2video -b 17000k -maxrate 17000k -bufsize 5097k -r 25 -g 15 -vf "scale=1280:1280/640*336","pad=1280:720:(ow-iw)/2:(oh-ih)/2:blue","setdar=4:3" -copyts -acodec ac3 -ab 192k -ac 2 -map 0:0 -map 0:1 -sn -f mpegts "Test1-Final.avi"

Input #0, avi, from 'Test1.avi':
Metadata:
encoder : Lavf53.6.0
Duration: 00:01:00.91, start: 0.000000, bitrate: 1395 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 640x336 [PAR 1:1 DAR 40:21], 23.98 fps,23.98 tbr, 23.98 tbn, 2997 tbc
Stream #0.1: Audio: mp2, 48000 Hz, stereo, s16, 64 kb/s
[buffer @ 018dbfa0] w:640 h:336 pixfmt:yuv420p tb:1/1000000 sar:1/1 sws_param:
[setdar @ 0194d640] a:4/3
[scale @ 018e4000] w:640 h:336 fmt:yuv420p -> w:1280 h:672 fmt:yuv420p flags:0x4
[pad @ 018c4040] w:1280 h:672 -> w:1280 h:720 x:0 y:24 color:0x29F06EFF[yuva]
[setdar @ 0194d640] w:1280 h:720 -> dar:4/3 sar:3/4
Output #0, mpegts, to 'Test1-Final.avi':
Metadata:
encoder : Lavf53.6.0
Stream #0.0: Video: mpeg2video, yuv420p, 1280x720 [PAR 3:4 DAR 4:3], q=2-31, 17000 kb/s, 90k tbn, 25 tbc
Stream #0.1: Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Stream #0.1 -> #0.1


Sony ZOOM display option not needed
The following image is the screen display result, the "blue" area is the padding adding by the ffmpeg option, must be set to black for the final solution.
Serviio_0.6.2_Aspect_Ratio_Solution.png
Serviio_0.6.2_Aspect_Ratio_Solution.png (941.84 KiB) Viewed 73932 times


I found out that the TV doesn't deal with non-HD stuff properly! so I need to expand/upscale to a HD : 1280x720 resolution.

_________________________________
Sony Bravia Display configuration
HOME -> Display -> Screen:
- Screen Format/Wide Mode (option name varies depending on your region/country) : Wide
-------- Enlarges the picture horizontally to fill the screen when the original source is 4:3. When the original source is 16:9 (High-definition source), select this option to display the 16:9 picture in its original size.
- Auto Format : Off
-------- Automatically changes “Screen Format/Wide Mode” to match the input signal
- 4:3 Default : Off (greyed)
-------- Sets the default screen format for a 4:3 broadcast.
- Auto Display Area : Off (greyed)
-------- Automatically adjusts the picture to the most appropriate display area.
- Display Area : +1
-------- Adjusts the picture display area. Select “Full Pixel” to display the original picture when parts of the picture are cut,“+1” to display the original picture,“Normal” to display the recommended size picture, or “-1” to enlarge the picture

Some others examples

PostPosted: Sun Aug 07, 2011 9:25 am
by Illico
FFMPEG option used for some avi files < 720 vertical resolution ( Standard definition)
Upscaling to 1280x720 (High Definition) with padding if needed and DAR change:

Test0.avi
Stream #0.0: Video: mpeg4, yuv420p, 608x320 [PAR 1:1 DAR 19:10], 25 tbr, 25 tbn, 25 tbc

ffmpeg.exe -i "Test0.avi" -y -threads 2 -vcodec mpeg2video -b 17000k -maxrate 17000k -bufsize 5097k -r 25 -g 15 -vf "scale=1280:1280/608*320","pad=1280:720:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3" -copyts -acodec ac3 -ab 192k -ac 2 -map 0:0 -map 0:1 -sn -f mpegts "Test0-Final.avi"

Stream #0.0: Video: mpeg2video, yuv420p, 1280x720 [PAR 3:4 DAR 4:3], q=2-31, 17000 kb/s, 90k tbn, 25 tbc


Test1.avi
Stream #0.0: Video: mpeg4, yuv420p, 640x336 [PAR 1:1 DAR 40:21], 23.98 fps, 23.98 tbr, 23.98 tbn, 2997 tbc

ffmpeg.exe -i "\\Buffalo\share\Video\Samples\Aspect_Ratio\Test1.avi" -y -threads 2 -vcodec mpeg2video -b 17000k -maxrate 17000k -bufsize 5097k -r 25 -g 15 -vf "scale=1280:1280/640*336","pad=1280:720:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3" -copyts -acodec ac3 -ab 192k -ac 2 -map 0:0 -map 0:1 -sn -f mpegts "Test1-Final.avi"

Stream #0.0: Video: mpeg2video, yuv420p, 1280x720 [PAR 3:4 DAR 4:3], q=2-31, 17000 kb/s, 90k tbn, 25 tbc


Test2.avi
Stream #0.0: Video: mpeg4, yuv420p, 640x352 [PAR 1:1 DAR 20:11], 23.98 fps, 23.98 tbr, 23.98 tbn, 2997 tbc

ffmpeg.exe -i "\\Buffalo\share\Video\Samples\Aspect_Ratio\Test2.avi" -y -threads 2 -vcodec mpeg2video -b 17000k -maxrate 17000k -bufsize 5097k -r 25 -g 15 -vf "scale=1280:1280/640*352","pad=1280:720:(ow-iw)/2:(oh-ih)/2:blue","setdar=4:3" -copyts -acodec ac3 -ab 192k -ac 2 -map 0:0 -map 0:1 -sn -f mpegts "Test2-Final.avi"

Stream #0.0: Video: mpeg2video, yuv420p, 1280x720 [PAR 3:4 DAR 4:3], q=2-31, 17000 kb/s, 90k tbn, 25 tbc

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sun Aug 07, 2011 9:26 am
by Illico
-

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sun Aug 07, 2011 9:28 am
by Illico
For the moment, this is the ffmpeg option I use (simplify: no need to know the input resolution):

-vf "scale=1280:-1","pad=1280:720:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3"

- The original file is scale into HDTV format with 1280 pixel width, and same input DAR(1280x1280/inputDAR)
- Then padding with black bar to a 16:9 display ratio (1280x720)
- And DAR is set to 4:3 ratio.

see FFMPEG documentation
for details.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Fri Aug 12, 2011 7:37 am
by Illico
Zip, do you think we could add an "FFMPEG Custom Options" to the ffmpeg command line when transcoding is used (like PS3 Media Server with mencoder custom option : Such as "-vf softskip,expand=::::1:16/9,dsize=4/3") ?
I already opened a ticket for that feature : https://bitbucket.org/xnejp03/serviio/i ... n-renderer

Example of ffmpeg custom option : -vf "scale=1280:-1","pad=1280:720:(ow-iw)/2:(oh-ih)/2:black","setdar=4:3"
In fact, that could be a first step for Sony Aspect ratio fixing.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Fri Aug 12, 2011 9:23 am
by zip
I'd rather Serviio generating these values based of the file information and profile automatically.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Fri Aug 12, 2011 9:55 am
by Illico
OK, so now I'll look for other input resolutions (1920x1080).

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 20, 2011 12:27 am
by Xmantium
Brilliant findings illico, one of the biggest issues with the Sony Bravia TVs is this, it affects when playing .avi and streaming video with latest beta.

I dont mind a temporary fix, can you include this custom ffmpeg code on Sony tv profile to be injected when it's executed?

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 20, 2011 9:06 am
by james
I have this same problem on my Sony KDL 40EX403. The 1024x576 MKV files (which get transcoded as AVC) look perfect though - excellent picture quality and correct aspect ratio.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 27, 2011 2:35 pm
by Xmantium
I just installed Mezzmo Media Server to test and they use FFMPEG as their transcoding engine and they fixed the aspect ratio for Sony Bravia
This proves this can be done with ffmpeg

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 27, 2011 5:27 pm
by sloebric
Xmantium wrote:Brilliant findings illico, one of the biggest issues with the Sony Bravia TVs is this, it affects when playing .avi and streaming video with latest beta.

I dont mind a temporary fix, can you include this custom ffmpeg code on Sony tv profile to be injected when it's executed?


A temp option whilst you figure out how to do it dynamically would be great. I gotta say Serviio is by far the best DLNA server out there. I've looked at them all. The only thing missing for me is transcoded M4V/H265+AC3 into MPEG-PS+AC3 having the correct aspect ratio. I've got it working with MediaTomb with the "-aspect 16:9" option. So I switch back and forth as needed, but try explaining that to my wife.

Here's my script in case it helps...

#!/bin/bash
INPUT="$1"
OUTPUT="$2"
DAR=`ffprobe -show_streams "${INPUT}" | grep -m 1 'display_aspect_ratio' | awk -F= '{ print $2 }'`
FPS=`ffprobe -show_streams "${INPUT}" | grep -m 1 'avg_frame_rate' | awk -F= '{ print $2 }'`
AC3_TRACK=`ffprobe -show_streams "${INPUT}" | grep ac3 -B10 | grep 'index=' | awk -F= '{ print $2 }'`
/usr/bin/ffmpeg -y -i "${INPUT}" -threads 2 -g 15 -vcodec mpeg2video -sameq -acodec copy -map 0:0 -map 0:${AC3_TRACK} -copyts -aspect 16:9 -r ${FPS} -sn -f vob "${OUTPUT}"

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 27, 2011 10:02 pm
by Xmantium
Where do you pu this script?

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 27, 2011 10:29 pm
by sloebric
Xmantium wrote:Where do you pu this script?


I should've clarified...this is the script I call for my MediaTomb setup. MT allows you to call an arbitrary script and pass two parameters (input and output filenames). Thought it could help Serviio devs.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sat Aug 27, 2011 10:31 pm
by zip
does this work with files with any aspect ratio? some are different than 16:9.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sun Aug 28, 2011 2:05 am
by Xmantium
I don't know why Sony don't fix this aspect ratio issue.
Ps3ms have to manipulate MEncoder to play the correct ratio
I don't how Mezzmo uses to manipulate ffmpeg, but have noticed the encoding compared to Serviio is under par, so much jaggies on edges, it's like watching an old Playstation One game!
Never tried MediaTomb but I'd like stay with Serviio as my ONLY one stop shop media server.
Would be great if this can be fixed as I hate pressing zoom on remote every time a file is transcoded.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Sun Aug 28, 2011 8:02 pm
by sloebric
zip wrote:does this work with files with any aspect ratio? some are different than 16:9.


So far, so good. I did try to do it programmatically by querying the DAR and then using that. I found that some files didn't have this set and decided to just brute force it to 16:9. I'll freely admit I'm a total hack and sure there's a better way with some error checking. e.g. use DAR when possible and then use 16:9 when all else fails.

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Wed Aug 31, 2011 6:56 am
by Illico
On latest ffmpeg 0.8 changelog, I see :
- mpeg2 aspect ratio detection fixed
Don't know what this...any idea?

Re: Sony Bravia - Aspect Ratio fixing

PostPosted: Wed Aug 31, 2011 8:51 pm
by Xmantium
My bad, looking for any possibe auto fixes for the Sony Bravia aspect ratio without IR remote