-
-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chroma subsampling does not lowpass filter (take average) before discarding samples #521
Comments
Taking the average is a form of low-pass filtering. It also puts the subsampled chroma at the center of the 2x2 chroma block. Most YCbCr-to-RGB conversion functions used in image decoding assume the center chroma sample position. A proof of concept for a fix for strukturag#521.
I understand your point and know that the color-conversion functions are ad-hoc solutions that still need to be improved. In the case above (RGB -> YCbCr) the conversion takes place when encoding the image. According to the AV1 spec What we should do: When encoding, the chroma sample position should be signaled to the AV1 encoder. When decoding, we should read the chroma sample position from the AV1 bitstream and select the appropriate filter. |
BTW: this is for 4:2:0 only. For 4:2:2 I did not find any information about chroma sample position in the AV1 spec. My guess is that it is colocated with the left sample as this is consistent with the two modes in 4:2:0. (Any references would be appreciated.) |
As HEVC uses vertically centered for 4:2:0 and colocated on left sample for 4:2:2, let's use the same when encoding AV1. |
Hi Dirk, The In practice, the center chroma sample position is the most important to AVIF still images. Please support the center chroma sample position and signal For 4:2:2, the AV1 bitstream does not signal the chroma sample position and the chroma sample position is not documented in the AV1 spec. I guess the implicitly assumed chroma sample position for 4:2:2 for videos is |
Note that the YCbCr-to-RGB conversion functions in libheif/heif_colorconversion.cc also assume the center chroma sample position. For example, in
This further supports my suggestion that the center chroma sample position is the most important to implement for AVIF because the assumption of the center position is widespread, even though it is not specified in the AV1 spec. |
Thank you, @wantehchang for the background information on AV1. Ok, this is fine with me. If AV1 supports (is going to support) CSP_CENTER, I can make this the default. I've looked up in the JPEG (JFIF) standard how chroma subsampling is carried out. How could we handle 4:2:2 in AVIF? Since chroma position is not signaled in AV1 for 4:2:2, could we simply |
Hi Dirk, Thank you for looking up the 4:2:2 chroma sample position in JPEG. Leo Barnes told me the same thing. How we should handle 4:2:2 in AVIF is a difficult question. I would suggest we take the first option: simply define this to be horizontally centered (a la CSP_CENTER). In my experience, many people will intuitively take an average if we ask them to subsample. I believe this is another reason why the center chroma sample position is commonly used, in addition to it being used in JPEG. Note: libavif's
|
AFAICT, this goes against most current implementations (focusing on video content though), where 4:2:2 is assumed to be co-located (and seems to be the only option and thus also not signalled/ignored in H.265). Otherwise (chroma_format_idc is not equal to 1), the values of the syntax elements chroma_sample_loc_type_top_field and chroma_sample_loc_type_bottom_field shall be ignored. When chroma_format_idc is equal to 2 (4:2:2 chroma format) or 3 (4:4:4 chroma format), the location of chroma samples is specified in clause 6.2. AV1 and AVIF are of course not bound by this (nor JFIF), but would just feel a bit "weird" in this case, that's all... Also from the JFIF spec: The 4:2:2 form of sub-sampling is used primarily for video-related applications (especially with the use of interlaced-scan systems). For JFIF usage, the use of sub-sampling formats other than 4:2:0 is therefore discouraged, as such other formats may not be supported in some applications. |
Although the H.265 spec says to ignore it because it INSISTS you use MPEG2 chroma for 4:2:2 and not JPEG, if the plan is to support JPEG anyway for ease of use then signaling it and treating the info as valid anyway is the lesser evil than just assuming. Not sure whether continuing to signal type 1 (centered) or using type 3 (top row centered) is better, since they're kind of equivalent at 4:2:2. For AV1, I don't know. Stick it in Exif? Use location 3 despite it being reserved? Very annoying they didn't even consider that use case. |
Thanks @kmilos for pointing out the note about 4:2:2 in the JFIF spec. As there is no consistent way to specify the sample location with the 4:2:2 video codecs, I decided to use colocated chroma samples for 4:2:2. At least for the moment. Since 4:2:2 is probably never used for images in practice, JPEG compatibility is not a big issue, and this choice is likely irrelevant anyways. |
The RGB-to-YCbCr conversion code in libheif/heif_colorconversion.cc performs chroma subsampling by discarding samples only, without first lowpass filtering (taking a weighted average of) the samples. A representative example is the second nested for loop in
Op_RGB24_32_to_YCbCr::convert_colorspace()
:Consider 4:2:0. This nested for loop subsamples each 2x2 block of chroma samples by taking the top-left sample and discarding the rest. This has two problems:
I will upload a pull request as a proof of concept for a fix.
The text was updated successfully, but these errors were encountered: