Roly's Blog

Whatever will be, will be, the future's not ours to see.

0%

FFmpeg Glossary: DAR, PAR, and SAR

The SAR and DAR output in the FFmpeg terminal are as follows

1
2
3
4
5
6
7
8
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42mp41
creation_time : 2018-05-25T12:42:26.000000Z
Duration: 00:02:00.02, start: 0.000000, bitrate: 50449 kb/s
Stream #0:0[0x1](eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080, 50126 kb/s, 29.97 fps, 29.97 tbr, 30k tbn (default)

SAR (Sample Aspect Ratio) : 单个像素的宽度和像素的高度的比 (That is the width of a pixel divided by the height of the pixel.)

DAR (Display Aspect Ratio) : 预期显示的宽度与高度的比 (The intended display’s width-to-height aspect)

PAR (Picture Aspect Ratio) : 宽度与高度像素个数的比 (‘horizontal_size’:’vertical_size’)

What’s confusing:

Storage aspect ratio (SAR) is the ratio of numbers of pixels.

Pixel aspect ratio (PAR) is a mathematical ratio that describes how the width of a pixel in a digital image compared to the height of that pixel.

先查看FFmpeg中的avcodec_string函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
AVRational display_aspect_ratio;
...

if (enc->width) {
av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
enc->width, enc->height);
...

if (enc->sample_aspect_ratio.num) {
av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
enc->width * (int64_t)enc->sample_aspect_ratio.num,
enc->height * (int64_t)enc->sample_aspect_ratio.den,
1024 * 1024);
av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
display_aspect_ratio.num, display_aspect_ratio.den);
...

PAR 对应 enc->width, enc->height:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct AVCodecContext {
...

/* video only */
/**
* picture width / height.
*
* @note Those fields may not match the values of the last
* AVFrame output by avcodec_receive_frame() due frame
* reordering.
*
* - encoding: MUST be set by user.
* - decoding: May be set by the user before opening the decoder if known e.g.
* from the container. Some decoders will require the dimensions
* to be set by the caller. During decoding, the decoder may
* overwrite those values as required while parsing the data.
*/
int width, height;

[FFmpeg-user] Glossary: DAR, PAR, and SAR 中有详细介绍, 其中关于PAR:

1
2
3
4
5
6
7
8
9
PAR (picture aspect ratio [1]) [noun]: 1, The metadata ratio:
'horizontal_size':'vertical_size' [3], as a minimized, H:V, integer
ratio (e.g. 5:4, 3:2) [2][4] (also see "SAR", note [2]).
[1] It's mistakenly asserted by some that "PAR" is an acronym for "pixel aspect ratio".
[2] PAR can also be calculated from DAR & SAR thusly: PAR = DAR/SAR.
[3] Note that PAR is virtual (i.e. defined by dataset indexes, not
physical dimensions).
[4] H.262 doesn't define PAR, however, it does define a quotent that correlates with PAR, to wit: H.262 §6.3.3, aspect_ratio_information:
"SAR = DAR × horizontal_size/vertical_size".

SAR 对应 enc->sample_aspect_ratio:

1
2
3
4
5
6
7
8
9
10
11
typedef struct AVCodecContext {
...

/**
* sample aspect ratio (0 if unknown)
* That is the width of a pixel divided by the height of the pixel.
* Numerator and denominator must be relatively prime and smaller than 256 for some video standards.
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
AVRational sample_aspect_ratio;

DAR 对应 display_aspect_ratio:

av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,

enc->width * (int64_t)enc->sample_aspect_ratio.num,

enc->height * (int64_t)enc->sample_aspect_ratio.den,

1024 * 1024);

DAR = SAR × PAR

再来看 Wikipedia 中的词条 Pixel Aspect Ratio (PAR)和 Storage aspect ratio (SAR)

先说结论:

Pixel Aspect Ratio == Sample Aspect Ratio

Storage Aspect Ratio == Picture Aspect Ratio

Pixel aspect ratio (PAR)

是描述数位影像像素中,宽和高之间的比率。大多数的数位成像系统,会将影像显示为很多微小的正方型像素格。但部分成像系统,尤其是必须与标准画质电视相容者,会将影像显示为长方形的像素格。像素格的宽和高不一定等长,因此就用像素宽高比来描述之。

像素宽高比通常会用于描述标准画质电视相关的影像,除了少数特例以外。大多数的成像系统,包括遵守电影电视工程师协会标准和实务的系统,皆是使用正方像素。

PAR-1to1

PAR-2to1

Storage aspect ratio (SAR)

显示宽高比 Display Aspect Ratio(DAR),不同于存储宽高比 Storage Aspect Ratio(SAR),后者指的是像素总数的比值。当影像是用长方像素而非正方像素显示时,这两种宽高比就会不一样。像素本身的比例,称之为像素宽高比 Pixel Aspect Ratio (PAR),譬如正方像素就是1:1。三者之间的关系为:

DAR = SAR × PAR

一个 640 x 480 的 VGA 影像其 SAR 为 640/480 = 4:3,当显示在一个 4:3 的显示器上时(DAR = 4:3),其像素宽高比就为 1:1。相对而言,一个 720 x 576 的 D-1 PAL 影像其 SAR 为 5:4,若也显示在 4:3 的显示器上(DAR = 4:3),可知其像素宽高比就为 (4:3)/(5:4) = 16:15。

令人困惑的地方

origin formula
FFmpeg: DAR (Display Aspect Ratio) = SAR (Sample Aspect Ratio) × PAR (Picture Aspect Ratio)
Wikipedia: DAR (Display Aspect Ratio) = SAR (Storage Aspect Ratio) × PAR (Pixel Aspect Ratio)

Reference

https://en.wikipedia.org/wiki/Aspect_ratio_(image)#storage_aspect_ratio

https://en.wikipedia.org/wiki/Pixel_aspect_ratio

https://en.wikipedia.org/wiki/Display_aspect_ratio

https://lists.ffmpeg.org/pipermail/ffmpeg-user/2020-September/050282.html

https://www.animemusicvideos.org/guides/avtech3/theory-videoaspectratios.html

https://trac.ffmpeg.org/wiki/Scaling

https://stackoverflow.com/questions/18877243/why-ffmpeg-print-sar-instead-of-par