static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)

{

    char tmp_key[5];

    char key2[32], language[4] = {0};

    char *str = NULL;

    const char *key = NULL;

    uint16_t langcode = 0;

    uint32_t data_type = 0, str_size, str_size_alloc;

    int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;

    int raw = 0;

    int num = 0;



    switch (atom.type) {

    case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;

    case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;

    case MKTAG( 'X','M','P','_'):

        if (c->export_xmp) { key = "xmp"; raw = 1; } break;

    case MKTAG( 'a','A','R','T'): key = "album_artist";    break;

    case MKTAG( 'a','k','I','D'): key = "account_type";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 'a','p','I','D'): key = "account_id"; break;

    case MKTAG( 'c','a','t','g'): key = "category"; break;

    case MKTAG( 'c','p','i','l'): key = "compilation";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 'c','p','r','t'): key = "copyright"; break;

    case MKTAG( 'd','e','s','c'): key = "description"; break;

    case MKTAG( 'd','i','s','k'): key = "disc";

        parse = mov_metadata_track_or_disc_number; break;

    case MKTAG( 'e','g','i','d'): key = "episode_uid";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 'g','n','r','e'): key = "genre";

        parse = mov_metadata_gnre; break;

    case MKTAG( 'h','d','v','d'): key = "hd_video";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 'k','e','y','w'): key = "keywords";  break;

    case MKTAG( 'l','d','e','s'): key = "synopsis";  break;

    case MKTAG( 'l','o','c','i'):

        return mov_metadata_loci(c, pb, atom.size);

    case MKTAG( 'p','c','s','t'): key = "podcast";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 'p','g','a','p'): key = "gapless_playback";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;

    case MKTAG( 'r','t','n','g'): key = "rating";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;

    case MKTAG( 's','o','a','l'): key = "sort_album";   break;

    case MKTAG( 's','o','a','r'): key = "sort_artist";  break;

    case MKTAG( 's','o','c','o'): key = "sort_composer"; break;

    case MKTAG( 's','o','n','m'): key = "sort_name";    break;

    case MKTAG( 's','o','s','n'): key = "sort_show";    break;

    case MKTAG( 's','t','i','k'): key = "media_type";

        parse = mov_metadata_int8_no_padding; break;

    case MKTAG( 't','r','k','n'): key = "track";

        parse = mov_metadata_track_or_disc_number; break;

    case MKTAG( 't','v','e','n'): key = "episode_id"; break;

    case MKTAG( 't','v','e','s'): key = "episode_sort";

        parse = mov_metadata_int8_bypass_padding; break;

    case MKTAG( 't','v','n','n'): key = "network";   break;

    case MKTAG( 't','v','s','h'): key = "show";      break;

    case MKTAG( 't','v','s','n'): key = "season_number";

        parse = mov_metadata_int8_bypass_padding; break;

    case MKTAG(0xa9,'A','R','T'): key = "artist";    break;

    case MKTAG(0xa9,'P','R','D'): key = "producer";  break;

    case MKTAG(0xa9,'a','l','b'): key = "album";     break;

    case MKTAG(0xa9,'a','u','t'): key = "artist";    break;

    case MKTAG(0xa9,'c','h','p'): key = "chapter";   break;

    case MKTAG(0xa9,'c','m','t'): key = "comment";   break;

    case MKTAG(0xa9,'c','o','m'): key = "composer";  break;

    case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;

    case MKTAG(0xa9,'d','a','y'): key = "date";      break;

    case MKTAG(0xa9,'d','i','r'): key = "director";  break;

    case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;

    case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;

    case MKTAG(0xa9,'e','n','c'): key = "encoder";   break;

    case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;

    case MKTAG(0xa9,'g','e','n'): key = "genre";     break;

    case MKTAG(0xa9,'g','r','p'): key = "grouping";  break;

    case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;

    case MKTAG(0xa9,'i','n','f'): key = "comment";   break;

    case MKTAG(0xa9,'l','y','r'): key = "lyrics";    break;

    case MKTAG(0xa9,'m','a','k'): key = "make";      break;

    case MKTAG(0xa9,'m','o','d'): key = "model";     break;

    case MKTAG(0xa9,'n','a','m'): key = "title";     break;

    case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;

    case MKTAG(0xa9,'p','r','d'): key = "producer";  break;

    case MKTAG(0xa9,'p','r','f'): key = "performers"; break;

    case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;

    case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;

    case MKTAG(0xa9,'s','t','3'): key = "subtitle";  break;

    case MKTAG(0xa9,'s','w','r'): key = "encoder";   break;

    case MKTAG(0xa9,'t','o','o'): key = "encoder";   break;

    case MKTAG(0xa9,'t','r','k'): key = "track";     break;

    case MKTAG(0xa9,'u','r','l'): key = "URL";       break;

    case MKTAG(0xa9,'w','r','n'): key = "warning";   break;

    case MKTAG(0xa9,'w','r','t'): key = "composer";  break;

    case MKTAG(0xa9,'x','y','z'): key = "location";  break;

    }

retry:

    if (c->itunes_metadata && atom.size > 8) {

        int data_size = avio_rb32(pb);

        int tag = avio_rl32(pb);

        if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {

            data_type = avio_rb32(pb); // type

            avio_rb32(pb); // unknown

            str_size = data_size - 16;

            atom.size -= 16;



            if (atom.type == MKTAG('c', 'o', 'v', 'r')) {

                int ret = mov_read_covr(c, pb, data_type, str_size);

                if (ret < 0) {

                    av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");

                }

                return ret;

            } else if (!key && c->found_hdlr_mdta && c->meta_keys) {

                uint32_t index = AV_RB32(&atom.type);

                if (index < c->meta_keys_count) {

                    key = c->meta_keys[index];

                } else {

                    av_log(c->fc, AV_LOG_WARNING,

                           "The index of 'data' is out of range: %d >= %d.\n",

                           index, c->meta_keys_count);

                }

            }

        } else return 0;

    } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {

        str_size = avio_rb16(pb); // string length

        if (str_size > atom.size) {

            raw = 1;

            avio_seek(pb, -2, SEEK_CUR);

            av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");

            goto retry;

        }

        langcode = avio_rb16(pb);

        ff_mov_lang_to_iso639(langcode, language);

        atom.size -= 4;

    } else

        str_size = atom.size;



    if (c->export_all && !key) {

        snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);

        key = tmp_key;

    }



    if (!key)

        return 0;

    if (atom.size < 0 || str_size >= INT_MAX/2)

        return AVERROR_INVALIDDATA;



    // Allocates enough space if data_type is a float32 number, otherwise

    // worst-case requirement for output string in case of utf8 coded input

    num = (data_type == 23);

    str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;

    str = av_mallocz(str_size_alloc);

    if (!str)

        return AVERROR(ENOMEM);



    if (parse)

        parse(c, pb, str_size, key);

    else {

        if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded

            mov_read_mac_string(c, pb, str_size, str, str_size_alloc);

        } else if (data_type == 23 && str_size >= 4) {  // BE float32

            float val = av_int2float(avio_rb32(pb));

            if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {

                av_log(c->fc, AV_LOG_ERROR,

                       "Failed to store the float32 number (%f) in string.\n", val);


                return AVERROR_INVALIDDATA;

            }

        } else {

            int ret = ffio_read_size(pb, str, str_size);

            if (ret < 0) {


                return ret;

            }

            str[str_size] = 0;

        }

        c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;

        av_dict_set(&c->fc->metadata, key, str, 0);

        if (*language && strcmp(language, "und")) {

            snprintf(key2, sizeof(key2), "%s-%s", key, language);

            av_dict_set(&c->fc->metadata, key2, str, 0);

        }

    }

    av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);

    av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",

            key, str, (char*)&atom.type, str_size_alloc, atom.size);



    av_freep(&str);

    return 0;

}