# Different aspect ratios being displayed from different types of h264 encoders

**URL:** <https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042>\
**Category:** Application Development\
**Created:** [February 22, 2024, 8:56pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042 "2024-02-22T20:56:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 22, 2024, 8:56pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/1 "2024-02-22T20:56:58Z")

</div>

Hello,

When I run this pipeline using different h264 encoders I’m seeing different aspect ratios when viewing the output on VLC.

- gst-launch-1.0 filesrc location=video.ts ! tsdemux ! h264parse ! (h264 decoders) ! videoconvert ! videoscale ! video/x-raw, width=640, height=480 ! (h264 encoders) bitrate=3000 ! mpegtsmux ! queue ! filesink location=out.ts

I’m mainly more focused on utilizing the openh264enc, nvh264enc, and qsvh264enc. From my experience using these 3 encoders I notice the pixel resolution is the same, but the aspect ratio appears different when I view the output from each encoder.

I have already tested using the same matching decoders & encoders, and I have also tested using the same decoder for all 3 encoders which all results in different aspect ratios for each test scenario.

I was wondering if anyone has ideas why this is happening?

---

<div class="post-metadata">

**Author:** ![seungha](https://discourse.gstreamer.org/user_avatar/discourse.gstreamer.org/seungha/32/44_2.png) [@seungha](https://discourse.gstreamer.org/u/seungha)\
**Post date:** [February 23, 2024, 1:18pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/2 "2024-02-23T13:18:59Z")

</div>

How exactly are they different? (input resolution/aspect-ratio and encoded aspect ratio)

openh264enc does not write pixel-aspect-ratio into bitstream. So, unless aspect-ratio is written in container, we will lose aspect-ratio info, meaning that displayed aspect ratio will be wrong if it was not 1/1.

NVENC API requires display aspect ratio, not pixel-aspect-ratio. Then NVENC runtime will convert it to pixel-aspect-ratio and write it into bitstream. I’m not sure why the API was designed like that. Anyway, because of the par ↔ dar conversion, rounding error might happen.

In case of QSV, we pass pixel-aspect-ratio directly to QSV runtime. I think QSV will always preserve input pixel-aspect-ratio unless aspect ratio is so funny value (e.g., MAX\_UINT16 overflow)

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 26, 2024, 4:50pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/3 "2024-02-26T16:50:13Z")

</div>

File source resolution is 720x480 (3:2).  
Using videoscale my target resolution is 640x480 (4:3).

- openh264enc is able to encode to a 4:3 aspect ratio

- nvh264enc, and qsvh264enc come out wider than 4:3

This is all observed on VLC. Also I know opehn264enc comes out to 4:3 correctly, because when changing the aspect ratio option on VLC to 4:3. The video from the openh264enc stays the same, but nvh264enc and qsvh264enc video shrinks down to match the 4:3 option.

 ![openh264enc.PNG](https://discourse.gstreamer.org/uploads/default/original/1X/6e1f68f2fcdc8def4d29abac71a65a268400c107.jpeg)

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 26, 2024, 4:51pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/4 "2024-02-26T16:51:08Z")

</div>

![nvh264enc.PNG](https://discourse.gstreamer.org/uploads/default/original/1X/22a10e124c1977dd2368b914ed7839718f1c7112.jpeg)

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 26, 2024, 4:51pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/5 "2024-02-26T16:51:41Z")

</div>

![qsvh264enc.PNG](https://discourse.gstreamer.org/uploads/default/original/1X/c86153edd73ee079b67ce28128b17c754dcfcf17.jpeg)

---

<div class="post-metadata">

**Author:** ![seungha](https://discourse.gstreamer.org/user_avatar/discourse.gstreamer.org/seungha/32/44_2.png) [@seungha](https://discourse.gstreamer.org/u/seungha)\
**Post date:** [February 26, 2024, 5:39pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/6 "2024-02-26T17:39:06Z")

</div>

that explains aspect-ratio related bug/missing-feature in openh264enc. don’t forget that pixel aspect ratio is different from display aspect ratio.

see your pipeline, I assume original 720x480 (3:2 display aspect ratio) stream’s pixel aspect ratio is 1:1. Then output pixel aspect ratio of `videoconvert ! videoscale` will not be 1/1 because you didn’t specify it. Then the non-1/1 pixel aspect ratio should be written in bitstream (NVENC/QSV do it correctly, openh264enc does not)

Now you are applying non-1/1 aspect ratio option in vlc, and resulting final display aspect ratio is supposed to be different from the correct one (derived display aspect ratio by using resolution and pixel aspect ratio).

In short, openh264enc is wrong.

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 26, 2024, 6:35pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/7 "2024-02-26T18:35:52Z")

</div>

How would I be able to specify it? The add-borders property on videoconvert and videoscale?

---

<div class="post-metadata">

**Author:** ![seungha](https://discourse.gstreamer.org/user_avatar/discourse.gstreamer.org/seungha/32/44_2.png) [@seungha](https://discourse.gstreamer.org/u/seungha)\
**Post date:** [February 26, 2024, 7:06pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/8 "2024-02-26T19:06:04Z")

</div>

default `add-borders` is `true` already.

What you need is `pixel-aspect-ratio` filed after convert/scale, for example `... ! videoconvert ! videoscale ! video/x-raw,width=640,height=480,pixel-aspect-ratio=1/1 ! (your encoder) ! ..`

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 26, 2024, 8:09pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/9 "2024-02-26T20:09:45Z")

</div>

After adding the PAR to the caps filter now every output from each decoder does look the same. However when in full screen it stays in it’s PAR which is correct I believe. Is there a way to stretch the videos to fill up more of the screen?

Output with PAR specified:

 ![openPAR.PNG](https://discourse.gstreamer.org/uploads/default/original/1X/c046c59da2f873aec7fa6fdf233c8b2676ddfcef.jpeg)

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 29, 2024, 9:53pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/10 "2024-02-29T21:53:27Z")

</div>

Sorry let me clarify my last post. I noticed that after adding the PAR to the capsfilter on my pipeline, the video output maintains black bars on the top and bottom of my video. But when I don’t include the PAR my video output doesn’t include the black bars on the top and bottom.

My question is why is it when I don’t include the PAR on the capsfilter do I not get black bars on my output?

Output with PAR not specified:

 ![out.PNG](https://discourse.gstreamer.org/uploads/default/original/1X/0d7ec76b70bdb29014c41c34f99d84c33e25e518.jpeg)

---

<div class="post-metadata">

**Author:** ![Elo23](https://discourse.gstreamer.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@Elo23](https://discourse.gstreamer.org/u/Elo23)\
**Post date:** [February 29, 2024, 9:54pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/11 "2024-02-29T21:54:03Z")

</div>

Output with PAR specified:

 ![outPARFullscreen.PNG](https://discourse.gstreamer.org/uploads/default/original/1X/1e1f72f3de46a3aee79491e33fec17f4c8d2c27c.jpeg)

---

<div class="post-metadata">

**Author:** ![seungha](https://discourse.gstreamer.org/user_avatar/discourse.gstreamer.org/seungha/32/44_2.png) [@seungha](https://discourse.gstreamer.org/u/seungha)\
**Post date:** [March 1, 2024, 1:00pm UTC](https://discourse.gstreamer.org/t/different-aspect-ratios-being-displayed-from-different-types-of-h264-encoders/1042/12 "2024-03-01T13:00:34Z")

</div>

the black borders is unavoidable when you do resizing video to different display aspect ratio with identical pixel aspect ratio (1:1).

You can do `add-borders=false` if you don’t want the borders. Then `videoscale` will stretch image without borders (aspect ratio is not preserved)
