Skip to content
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

The libavif encoder adds the MA1B or MA1A brand incorrectly #2514

Open
wantehchang opened this issue Dec 2, 2024 · 1 comment
Open

The libavif encoder adds the MA1B or MA1A brand incorrectly #2514

wantehchang opened this issue Dec 2, 2024 · 1 comment

Comments

@wantehchang
Copy link
Collaborator

In libavif/src/write.c, the code that adds the MA1B or MA1A brand is as follows:

    if ((imageMetadata->depth == 8) || (imageMetadata->depth == 10)) {     //
        if (imageMetadata->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) {        //
            AVIF_CHECKRES(avifRWStreamWriteChars(&s, "MA1B", 4));          // ... compatible_brands[]
        } else if (imageMetadata->yuvFormat == AVIF_PIXEL_FORMAT_YUV444) { //
            AVIF_CHECKRES(avifRWStreamWriteChars(&s, "MA1A", 4));          // ... compatible_brands[]
        }
    }

This code is incorrect because it does not check the AV1 profile or the AV1 level of the AV1 bitstreams.

@wantehchang
Copy link
Collaborator Author

The attached patch should fix the code that adds the MA1B brand:

diff --git a/src/write.c b/src/write.c
index cf4e1c79..52829208 100644
--- a/src/write.c
+++ b/src/write.c
@@ -3134,6 +3134,7 @@ avifResult avifEncoderFinish(avifEncoder * encoder, avifRWData * output)
     // -----------------------------------------------------------------------
     // Harvest configuration properties from sequence headers
 
+    avifBool isMA1B = AVIF_TRUE;
     for (uint32_t itemIndex = 0; itemIndex < encoder->data->items.count; ++itemIndex) {
         avifEncoderItem * item = &encoder->data->items.item[itemIndex];
         if (item->encodeOutput->samples.count > 0) {
@@ -3142,6 +3143,11 @@ avifResult avifEncoderFinish(avifEncoder * encoder, avifRWData * output)
             AVIF_CHECKERR(avifSequenceHeaderParse(&sequenceHeader, (const avifROData *)&firstSample->data, codecType),
                           avifGetErrorForItemCategory(item->itemCategory));
             item->av1C = sequenceHeader.av1C;
+            //  The MA1B brand: The AV1 profile shall be the Main Profile and
+            //  the level shall be 5.1 or lower.
+            if (item->av1C.seqProfile != 0 || item->av1C.seqLevelIdx0 > 13) {
+                isMA1B = AVIF_FALSE;
+            }
         }
     }
 
@@ -3239,10 +3245,11 @@ avifResult avifEncoderFinish(avifEncoder * encoder, avifRWData * output)
     }                                                                      //
     AVIF_CHECKRES(avifRWStreamWriteChars(&s, "mif1", 4));                  // ... compatible_brands[]
     AVIF_CHECKRES(avifRWStreamWriteChars(&s, "miaf", 4));                  // ... compatible_brands[]
+    if (isMA1B) {                                                          //
+        AVIF_CHECKRES(avifRWStreamWriteChars(&s, "MA1B", 4));              // ... compatible_brands[]
+    }                                                                      //
     if ((imageMetadata->depth == 8) || (imageMetadata->depth == 10)) {     //
-        if (imageMetadata->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) {        //
-            AVIF_CHECKRES(avifRWStreamWriteChars(&s, "MA1B", 4));          // ... compatible_brands[]
-        } else if (imageMetadata->yuvFormat == AVIF_PIXEL_FORMAT_YUV444) { //
+        if (imageMetadata->yuvFormat == AVIF_PIXEL_FORMAT_YUV444) {        //
             AVIF_CHECKRES(avifRWStreamWriteChars(&s, "MA1A", 4));          // ... compatible_brands[]
         }
     }

wantehchang added a commit to wantehchang/libavif that referenced this issue Dec 2, 2024
wantehchang added a commit to wantehchang/libavif that referenced this issue Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant