Home > Back-end >  Why is the FLAC__metadata_get_tags function of libFLAC not deterministic?
Why is the FLAC__metadata_get_tags function of libFLAC not deterministic?

Time:03-14

While dabbling with libFLAC I tried out the FLAC__metadata_get_tags function with this C program:

#include <stdio.h>
#include <FLAC/metadata.h>

int main() {
    FLAC__StreamMetadata* metas;
    if (FLAC__metadata_get_tags("/tmp/test.flac", &metas) && metas != NULL) {
        for (int i = 0;; i  ) {
            printf("type: %d, length: %d\n", (metas i)->type, (metas i)->length);
            if ((metas i)->is_last) {
                break;
            }
        }
        FLAC__metadata_object_delete(metas);
    }
}

When running this program multiple times, I get different outputs. There seems to be a varying number of metadata entries with type 0 (FLAC__METADATA_TYPE_STREAMINFO). Sometimes I even get undefined types and negative lenghts.

Why is that? I would expect the program to always give the same output. I'm also not sure what a metadata entry with length 0 or even a negative length is supposed to mean.

Here is one example execution:

$ clang -I/usr/local/include -L/usr/local/lib -lFLAC -logg test.c && ./a.out
type: 4, length: 213
type: 0, length: 0
type: 0, length: 0
type: 11, length: 1083592000
type: -538976289, length: 0

CodePudding user response:

FLAC__StreamMetadata is not a list of metadata, it's one metadata, and only one TYPE_VORBIS_COMMENT is filled.

https://github.com/xiph/flac/blob/27c615706cedd252a206dd77e3910dfa395dcc49/src/libFLAC/metadata_iterators.c#L217

I found code how to iterate over it, from https://isrc.iscas.ac.cn/gitlab/mirrors/github.com/musicplayerdaemon_mpd/-/blob/6419cc54ac200b217d99ee35d1e2d57dbfda3f55/src/decoder/flac_plugin.c :

#include <stdio.h>
#include <FLAC/metadata.h>

int main() {
    FLAC__StreamMetadata* metas;
    if (FLAC__metadata_get_tags("./test.flac", &metas) && metas != NULL) {
        for (unsigned i = 0; i < metas->data.vorbis_comment.num_comments; i  ) {
            char *ptr;
            if ((ptr = (char*)metas->data.vorbis_comment.comments[i].entry) != NULL) {
                printf("%s\n", ptr);
            }
        }
        FLAC__metadata_object_delete(metas);
    }
}

On the .flac downloaded from https://helpguide.sony.net/high-res/sample1/v1/en/index.html outputs:

ALBUM=Bee Moved
TITLE=Bee Moved
ALBUMARTIST=Blue Monday FM
MRAT=0
ARTIST=Blue Monday FM

Overall, when in doubt - when the project is open-source, check sources.

  • Related