Example #1
0
static OpusTags *
tags_list(DB_playItem_t *it, OggOpusFile *opusfile, int link)
{
    const OpusTags *orig = op_tags (opusfile, link);

    OpusTags *tags = calloc (1, sizeof (OpusTags));
    if (!tags)
        return NULL;

    deadbeef->pl_lock ();
    for (DB_metaInfo_t *m = deadbeef->pl_get_metadata_head (it); m; m = m->next) {
        if (strchr (":!_", m->key[0])) {
            break;
        }
        char *key = strdupa (m->key);
        if (!strcasecmp(key, "R128_TRACK_GAIN")) {
            continue;
        }
        split_tag (tags, oggedit_map_tag (key, "meta2tag"), m->value, m->valuesize);
    }

    deadbeef->pl_unlock ();

    // preserve album art
    int i = 0;
    const char *tag;
    while ((tag = opus_tags_query(orig, ALBUM_ART_KEY, i++))) {
        split_tag (tags, ALBUM_ART_KEY, tag, (int)strlen (tag) + 1);
    }

    return tags;
}
Example #2
0
static int
update_vorbis_comments (DB_playItem_t *it, OggVorbis_File *vorbis_file, const int tracknum) {
    const vorbis_comment *vc = ov_comment(vorbis_file, tracknum);
    if (!vc) {
        trace("update_vorbis_comments: ov_comment failed\n");
        return -1;
    }

    deadbeef->pl_delete_all_meta (it);
    for (int i = 0; i < vc->comments; i++) {
        char *tag = strdup(vc->user_comments[i]);
        char *value;
        if (tag && (value = strchr(tag, '='))
#ifdef ANDROID
                && strlen (value) < 4000
#endif
           ) {
            *value++ = '\0';
            if (!replaygain_tag(it, DDB_REPLAYGAIN_ALBUMGAIN, tag, value) &&
                    !replaygain_tag(it, DDB_REPLAYGAIN_ALBUMPEAK, tag, value) &&
                    !replaygain_tag(it, DDB_REPLAYGAIN_TRACKGAIN, tag, value) &&
                    !replaygain_tag(it, DDB_REPLAYGAIN_TRACKPEAK, tag, value)
                    && strcasecmp (tag, "METADATA_BLOCK_PICTURE")) {
                add_meta(it, oggedit_map_tag(tag, "tag2meta"), value);
            }
        }
        if (tag) {
            free(tag);
        }
    }

    deadbeef->pl_add_meta (it, "title", NULL);
    uint32_t f = deadbeef->pl_get_item_flags (it);
    f &= ~DDB_TAG_MASK;
    f |= DDB_TAG_VORBISCOMMENTS;
    deadbeef->pl_set_item_flags (it, f);
    ddb_playlist_t *plt = deadbeef->plt_get_curr ();
    if (plt) {
        deadbeef->plt_modified (plt);
        deadbeef->plt_unref (plt);
    }
    deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);

    return 0;
}
Example #3
0
static int
opusdec_write_metadata (DB_playItem_t *it) {
    char fname[PATH_MAX];
    deadbeef->pl_get_meta (it, ":URI", fname, sizeof (fname));

    DB_FILE *fp = deadbeef->fopen (fname);
    if (!fp) {
        return -1;
    }

    int is_streaming = fp->vfs->is_streaming();
    const OpusFileCallbacks opcb = {
        .read = opus_file_read,
        .seek = is_streaming ? NULL : opus_file_seek,
        .tell = is_streaming ? NULL : opus_file_tell,
        .close = opus_file_close
    };

    int res = 0;

    OggOpusFile *opusfile = op_test_callbacks(fp, &opcb, NULL, 0, &res);
    if (!opusfile) {
        deadbeef->fclose (fp);
        return -1;
    }

    int link = 0;
    if (deadbeef->pl_get_item_flags (it) & DDB_IS_SUBTRACK) {
        link = deadbeef->pl_find_meta_int (it, ":TRACKNUM", 0);
    }

    OpusTags *tags = tags_list(it, opusfile, link);
    if (!tags) {
        op_free (opusfile);
        deadbeef->fclose (fp);
        return -1;
    }

    deadbeef->pl_lock();

    // RG info
    const char *track_gain_str = deadbeef->pl_find_meta (it, ddb_internal_rg_keys[DDB_REPLAYGAIN_TRACKGAIN]);
    float track_gain = 0;
    if (track_gain_str) {
        track_gain = atof (track_gain_str);
    }

    const char *album_gain_str = deadbeef->pl_find_meta (it, ddb_internal_rg_keys[DDB_REPLAYGAIN_ALBUMGAIN]);
    float album_gain = 0;
    if (album_gain_str) {
        album_gain = atof (album_gain_str);
    }

    if (track_gain_str) {
        char s[100];
        snprintf (s, sizeof (s), "%d", 0);
        split_tag (tags, oggedit_map_tag (strdupa ("R128_TRACK_GAIN"), "meta2tag"), s, (int)strlen (s) + 1);
    }

    float value = deadbeef->pl_get_item_replaygain (it, DDB_REPLAYGAIN_ALBUMGAIN);
    if (value != 0) {
        char s[100];
        snprintf (s, sizeof (s), "%d", (int)(album_gain - track_gain) * 256);
        split_tag (tags, oggedit_map_tag (strdupa ("R128_ALBUM_GAIN"), "meta2tag"), s, (int)strlen (s) + 1);
    }

    int header_gain = (track_gain - 5.f) * 256;

    const char *stream_size_string = deadbeef->pl_find_meta(it, ":STREAM SIZE");
    const size_t stream_size = stream_size_string ? (off_t)atoll(stream_size_string) : 0;
    deadbeef->pl_unlock();
    const off_t file_size = oggedit_write_opus_metadata (deadbeef->fopen(fname), fname, 0, stream_size, header_gain, tags->comments, tags->user_comments);
    opus_tags_clear(tags);

    res = 0;
    if (file_size <= 0) {
        res = -1;
    }

    op_free (opusfile);
    deadbeef->fclose (fp);

    if (!res) {
        set_meta_ll(it, ":FILE_SIZE", (int64_t)file_size);
        res = opusdec_read_metadata(it);
    }
    return res;
}


// define plugin interface
static DB_decoder_t plugin = {
    DB_PLUGIN_SET_API_VERSION
    .plugin.version_major = 1,
    .plugin.version_minor = 0,
    .plugin.type = DB_PLUGIN_DECODER,
    .plugin.flags = DDB_PLUGIN_FLAG_LOGGING,
    .plugin.name = "Opus player",
    .plugin.id = "opus",
    .plugin.descr = "Opus player based on libogg, libopus and libopusfile.",
    .plugin.copyright = 
        "deadbeef-opus\n"
        "Copyright (C) 2009-2017 Alexey Yakovenko and other contributors\n"
        "\n"
        "This software is provided 'as-is', without any express or implied\n"
        "warranty.  In no event will the authors be held liable for any damages\n"
        "arising from the use of this software.\n"
        "\n"
        "Permission is granted to anyone to use this software for any purpose,\n"
        "including commercial applications, and to alter it and redistribute it\n"
        "freely, subject to the following restrictions:\n"
        "\n"
        "1. The origin of this software must not be misrepresented; you must not\n"
        " claim that you wrote the original software. If you use this software\n"
        " in a product, an acknowledgment in the product documentation would be\n"
        " appreciated but is not required.\n"
        "\n"
        "2. Altered source versions must be plainly marked as such, and must not be\n"
        " misrepresented as being the original software.\n"
        "\n"
        "3. This notice may not be removed or altered from any source distribution.\n"
        "\n\n\n"
        "libogg, opus, opusfile\n"
        "Copyright (c) 1994-2013 Xiph.Org Foundation and contributors\n"
        "\n\n\n"
        "liboggedit\n"
        "Copyright (C) 2014 Ian Nartowicz <*****@*****.**>\n",
    .open = opusdec_open,
    .open2 = opusdec_open2,
    .init = opusdec_init,
    .free = opusdec_free,
    .read = opusdec_read,
    .seek = opusdec_seek,
    .seek_sample = opusdec_seek_sample,
    .insert = opusdec_insert,
    .read_metadata = opusdec_read_metadata,
    .write_metadata = opusdec_write_metadata,
    .exts = exts,
};

DB_plugin_t *
opus_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}
Example #4
0
static int
update_vorbis_comments (DB_playItem_t *it, OggOpusFile *opusfile, const int tracknum) {
    const OpusTags *vc = op_tags (opusfile, tracknum);
    if (!vc) {
        return -1;
    }

    deadbeef->pl_delete_all_meta (it);

    for (int i = 0; i < vc->comments; i++) {
        char *tag = strdup(vc->user_comments[i]);
        char *value;
        if (tag && (value = strchr(tag, '='))
#ifdef ANDROID
            && strlen (value) < 4000
#endif
            ) {
            // skip the ignored RG fields, and the picture
            if (_is_replaygain_tag (it, tag) || !strcasecmp (tag, "METADATA_BLOCK_PICTURE")) {
                free (tag);
                continue;
            }
            *value++ = '\0';
            deadbeef->pl_append_meta(it, oggedit_map_tag(tag, "tag2meta"), value);
        }
        if (tag) {
            free(tag);
        }
    }

    const char *r128_trackgain = deadbeef->pl_find_meta (it, "R128_TRACK_GAIN");
    if (r128_trackgain) {
        int trackgain = atoi (r128_trackgain) + op_head (opusfile, tracknum)->output_gain;
        if (trackgain != 0) {
            deadbeef->pl_set_item_replaygain (it, DDB_REPLAYGAIN_TRACKGAIN, trackgain / 256.0f + 5.0f);
            deadbeef->pl_delete_meta (it, "R128_TRACK_GAIN");
        }
    }

    int albumgain = op_head (opusfile, tracknum)->output_gain;
    const char *r128_albumgain = deadbeef->pl_find_meta (it, "R128_ALBUM_GAIN");
    if (r128_albumgain) {
        albumgain += atoi (r128_albumgain);
        deadbeef->pl_delete_meta (it, "R128_ALBUM_GAIN");
    }
    if (albumgain != 0) {
        deadbeef->pl_set_item_replaygain (it, DDB_REPLAYGAIN_ALBUMGAIN, albumgain / 256.0f + 5.0f);
    }

    char s[100];
    int output_gain = op_head (opusfile, tracknum)->output_gain;
    snprintf (s, sizeof (s), "%0.2f dB", output_gain / 256.0f + 5.0f);
    deadbeef->pl_replace_meta (it, ":OPUS_HEADER_GAIN", s);

    deadbeef->pl_set_meta_int (it, ":SAMPLERATE_ORIGINAL", op_head (opusfile, tracknum)->input_sample_rate);

    deadbeef->pl_add_meta (it, "title", NULL);
    uint32_t f = deadbeef->pl_get_item_flags (it);
    f &= ~DDB_TAG_MASK;
    f |= DDB_TAG_VORBISCOMMENTS;
    deadbeef->pl_set_item_flags (it, f);
    ddb_playlist_t *plt = deadbeef->plt_get_curr ();
    if (plt) {
        deadbeef->plt_modified (plt);
        deadbeef->plt_unref (plt);
    }

    return 0;
}