diff options
author | Ian Nartowicz <iann@crunchbang> | 2014-06-04 22:45:30 +0100 |
---|---|---|
committer | Ian Nartowicz <iann@crunchbang> | 2014-06-04 22:45:30 +0100 |
commit | 823a4064ebcd2662f8e480f99326ac75dd6404a7 (patch) | |
tree | 420f6df26d390479f5573acb4f922d62d458ae8c /plugins/vorbis | |
parent | 923bdcd04d441869dc0fc9350c5bfb856f6e5677 (diff) |
Full release of oggedit
Diffstat (limited to 'plugins/vorbis')
-rw-r--r-- | plugins/vorbis/Makefile.am | 4 | ||||
-rw-r--r-- | plugins/vorbis/vcedit.c | 289 | ||||
-rw-r--r-- | plugins/vorbis/vcedit.h | 35 | ||||
-rw-r--r-- | plugins/vorbis/vorbis.c | 857 |
4 files changed, 403 insertions, 782 deletions
diff --git a/plugins/vorbis/Makefile.am b/plugins/vorbis/Makefile.am index 859d1af0..8b09c687 100644 --- a/plugins/vorbis/Makefile.am +++ b/plugins/vorbis/Makefile.am @@ -1,8 +1,8 @@ if HAVE_VORBIS vorbisdir = $(libdir)/$(PACKAGE) pkglib_LTLIBRARIES = vorbis.la -vorbis_la_SOURCES = vorbis.c vcedit.c vcedit.h vceditaux.h i18n.h -vorbis_la_LDFLAGS = -module -avoid-version -lm +vorbis_la_SOURCES = vorbis.c +vorbis_la_LDFLAGS = -module -avoid-version -lm -export-symbols-regex vorbis_load vorbis_la_LIBADD = $(LDADD) $(VORBIS_LIBS) ../liboggedit/liboggedit.a AM_CFLAGS = $(CFLAGS) $(VORBIS_CFLAGS) -std=c99 diff --git a/plugins/vorbis/vcedit.c b/plugins/vorbis/vcedit.c deleted file mode 100644 index 2f30aeb6..00000000 --- a/plugins/vorbis/vcedit.c +++ /dev/null @@ -1,289 +0,0 @@ -/* - This file is part of Deadbeef Player source code - http://deadbeef.sourceforge.net - - Ogg Vorbis plugin Ogg edit functions - - Copyright (C) 2014 Ian Nartowicz <deadbeef@nartowicz.co.uk> - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - -*/ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <limits.h> -#include <unistd.h> -#include <stdbool.h> -#include <sys/stat.h> -#include <ogg/ogg.h> -#include <deadbeef.h> -#include "vcedit.h" - -#define VORBISNAME "Vorbis" -#define TAGMAGIC "\3vorbis" -#define CODEMAGIC "\5vorbis" - -#define CHUNKSIZE 4096 -#define MAXOGGPAGE 65536 - -static const char *ogg_codec(ogg_page *og) -{ - typedef struct { - const char *magic; - const size_t length; - const char *codec; - } codec_t; - const codec_t codecs[] = { - {.codec = "Opus", .length = 19, .magic = "OpusHead"}, - {.codec = "Vorbis", .length = 30, .magic = "\1vorbis"}, - {.codec = "Flac", .length = 47, .magic = "\177FLAC"}, - {.codec = "Speex", .length = 80, .magic = "Speex "}, - {.codec = "Celt", .length = 80, .magic = "CELT"}, // obsolete - {.codec = "MIDI", .length = 13, .magic = "OggMIDI\0"}, - {.codec = "PCM", .length = 28, .magic = "PCM "}, - {.codec = "Theora", .length = 42, .magic = "\200theora"}, - {.codec = "Daala", .length = 38, .magic = "\200daala"}, - {.codec = "Dirac", .length = 5, .magic = "BBCD\0"}, - {.codec = "Skeleton", .length = 80, .magic = "fishead\0"}, - {.codec = "Kate", .length = 64, .magic = "\200kate\0\0\0"}, - {.codec = "CMML", .length = 29, .magic = "CMML\0\0\0\0"}, - {.codec = "YUV4MPEG", .length = 8, .magic = "YUV4Mpeg"}, - {.codec = "UVS", .length = 48, .magic = "UVS "}, - {.codec = "YUV", .length = 32, .magic = "\0YUV"}, - {.codec = "RGB", .length = 24, .magic = "\0RGB"}, - {.codec = "JNG", .length = 48, .magic = "\213JNG\r\n\032\n"}, - {.codec = "MNG", .length = 48, .magic = "\212MNG\r\n\032\n"}, - {.codec = "PNG", .length = 48, .magic = "\211PNG\r\n\032\n"}, - {.codec = "Spots", .length = 52, .magic = "SPOTS\0\0\0"}, - {.codec = NULL} - }; - - for (const codec_t *match = codecs; match->codec; match++) - if ((size_t)og->body_len >= match->length && !memcmp(og->body, match->magic, strlen(match->codec))) - return match->codec; - - return "unknown"; -} - -static void _oggpack_chars(oggpack_buffer *opb, const char *s, size_t length) -{ - while (length--) - oggpack_write(opb, *s++, 8); -} - -static void _oggpack_string(oggpack_buffer *opb, const char *s) -{ - oggpack_write(opb, strlen(s), 32); - _oggpack_chars(opb, s, strlen(s)); -} - -static int get_page(DB_FILE *in, ogg_sync_state *oy, ogg_page *og, char **lasterror) -{ - uint16_t chunks_left = MAXOGGPAGE / CHUNKSIZE; - while (ogg_sync_pageout(oy, og) != 1) { - char *buffer = ogg_sync_buffer(oy, CHUNKSIZE); - if (!in || !buffer || !chunks_left--) { - *lasterror = "can't find Ogg bitstream."; - return -1; - } - - const size_t bytes = in->vfs->read(buffer, 1, CHUNKSIZE, in); - if (!bytes) { - *lasterror = "unexpected EOF."; - return 0; - } - - ogg_sync_wrote(oy, bytes); - } - - return ogg_page_serialno(og); -} - -static bool write_page(FILE *out, ogg_page *og) -{ - return fwrite(og->header, 1, og->header_len, out) != (size_t)og->header_len || - fwrite(og->body, 1, og->body_len, out) != (size_t)og->body_len; -} - -static int write_page_and_get_next(DB_FILE *in, FILE *out, ogg_sync_state *oy, ogg_page *og, char **lasterror) -{ - if (write_page(out, og)) - return -1; - - return get_page(in, oy, og, lasterror); -} - -static bool write_vorbis_tags(FILE *out, const int serial, const char *vendor, const size_t num_tags, char **tags, ogg_packet *codebooks) -{ - oggpack_buffer opb; - oggpack_writeinit(&opb); - _oggpack_chars(&opb, TAGMAGIC, strlen(TAGMAGIC)); - _oggpack_string(&opb, vendor); - oggpack_write(&opb, num_tags, 32); - for (size_t i = 0; i < num_tags; i++) - _oggpack_string(&opb, tags[i]); - oggpack_write(&opb, 1, 1); - oggpack_writealign(&opb); - - - ogg_stream_state os; - ogg_stream_init(&os, serial); - os.b_o_s = 1; - os.pageno = 1; - ogg_packet op; - memset(&op, '\0', sizeof(op)); - op.packet = oggpack_get_buffer(&opb); - op.bytes = oggpack_bytes(&opb); - ogg_stream_packetin(&os, &op); - oggpack_writeclear(&opb); - ogg_stream_packetin(&os, codebooks); - - ogg_page og; - while (ogg_stream_flush(&os, &og)) - if (write_page(out, &og)) - return true; - - return ogg_stream_check(&os) || ogg_stream_clear(&os); -} - -static int extract_codebooks(DB_FILE *in, ogg_sync_state *oy, ogg_page *og, int serial, ogg_packet *codebooks, char **lasterror) -{ - memset(codebooks, '\0', sizeof(codebooks)); - ogg_stream_state os; - if (ogg_stream_init(&os, serial)) { - *lasterror = "cannot init Ogg stream."; - return -1; - } - os.pageno = 1; - os.b_o_s = 1; - - ogg_packet op; - ogg_stream_pagein(&os, og); - do { - while (ogg_stream_packetpeek(&os, NULL) == 0) { - if ((serial = get_page(in, oy, og, lasterror)) <= 0) - goto cleanup; - fprintf(stderr, "Serial: %d, %ld bytes (%s)\n", serial, og->header_len + og->body_len, og->body); - if (ogg_stream_pagein(&os, og)) { - *lasterror = "failed to stream page for codebooks packet."; - goto cleanup; - } - } - } while (ogg_stream_packetout(&os, &op) != 1 || op.bytes < strlen(CODEMAGIC) || memcmp(op.packet, CODEMAGIC, strlen(CODEMAGIC))); - - if (!(codebooks->packet = malloc(op.bytes))) { - *lasterror = "cannot allocate codebooks packet."; - goto cleanup; - } - codebooks->bytes = op.bytes; - memcpy(codebooks->packet, op.packet, op.bytes); - -cleanup: - ogg_stream_clear(&os); - - return codebooks->packet ? serial : -1; -} - -off_t vcedit_write_metadata(DB_FILE *in, const char *fname, int link, const char *vendor, const int num_tags, char **tags, char **lasterror) -{ - off_t file_size = 0; - ogg_page og; - ogg_sync_state oy; - ogg_sync_init(&oy); - - char outname[PATH_MAX]; - snprintf(outname, PATH_MAX, "%s.temp", fname); - FILE *out = fopen(outname, "w+b"); - if (!out) { - *lasterror = "unable to open temporary file for writing."; - goto cleanup; - } - if (!in) { - *lasterror = "file not opened for reading."; - goto cleanup; - } - struct stat stat_struct; - if (!stat(fname, &stat_struct)) - chmod(outname, stat_struct.st_mode); - - /* Copy through pages until we reach the right info header */ - int codec_serial = 0; - int serial = get_page(in, &oy, &og, lasterror); - while (serial > 0 && !codec_serial) { - while (serial > 0 && !ogg_page_bos(&og)) - serial = write_page_and_get_next(in, out, &oy, &og, lasterror); - - while (serial > 0 && ogg_page_bos(&og)) { - if (link < 1 && !strcmp(ogg_codec(&og), VORBISNAME)) { - codec_serial = serial; - } - serial = write_page_and_get_next(in, out, &oy, &og, lasterror); - } - link--; - } - - /* Copy additional pages up to our comment header */ - while (serial > 0 && serial != codec_serial) - serial = write_page_and_get_next(in, out, &oy, &og, lasterror); - if (serial <= 0) - goto cleanup; - - /* Add the codebook packet to our comment header and save it */ - ogg_packet codebooks; - if ((serial = extract_codebooks(in, &oy, &og, codec_serial, &codebooks, lasterror)) <= 0) - goto cleanup; - const bool write_tags = write_vorbis_tags(out, codec_serial, vendor, num_tags, tags, &codebooks); - ogg_packet_clear(&codebooks); - if (write_tags) { - *lasterror = "internal error writing Vorbis comment header packet."; - goto cleanup; - } - - /* Blindly copy through the remaining pages */ - serial = get_page(in, &oy, &og, lasterror); - while (serial > 0) - serial = write_page_and_get_next(in, out, &oy, &og, lasterror); - if (serial < 0) - goto cleanup; - - fseeko(out, 0, SEEK_END); - file_size = ftello(out); - -cleanup: - if (in) - in->vfs->close(in); - - if (out) - fclose(out); - - ogg_sync_clear(&oy); - - if (file_size <= 0) { - unlink(outname); - if (!*lasterror) - *lasterror = "error writing new file, changes backed out."; - return -1; - } - - rename(outname, fname); - return file_size; -} diff --git a/plugins/vorbis/vcedit.h b/plugins/vorbis/vcedit.h deleted file mode 100644 index 7eabe877..00000000 --- a/plugins/vorbis/vcedit.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - This file is part of Deadbeef Player source code - http://deadbeef.sourceforge.net - - Ogg Vorbis plugin Ogg edit functions header - - Copyright (C) 2014 Ian Nartowicz <deadbeef@nartowicz.co.uk> - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - -*/ - -#ifndef __VCEDIT_H -#define __VCEDIT_H - -#define ALBUM_ART_KEY "METADATA_BLOCK_PICTURE" -#define ALBUM_ART_META "metadata_block_picture" - -off_t vcedit_write_metadata(DB_FILE *in, const char *fname, int link, const char *vendor, const int num_tags, char **tags, char **lasterror); - -#endif /* __VCEDIT_H */ diff --git a/plugins/vorbis/vorbis.c b/plugins/vorbis/vorbis.c index 1bd4a382..00577268 100644 --- a/plugins/vorbis/vorbis.c +++ b/plugins/vorbis/vorbis.c @@ -3,7 +3,7 @@ Copyright (C) 2009-2014 Alexey Yakovenko and other contributors This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages + warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, @@ -11,12 +11,12 @@ freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. + misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ @@ -24,24 +24,28 @@ #ifdef HAVE_CONFIG_H # include <config.h> #endif -#include <vorbis/codec.h> #include <vorbis/vorbisfile.h> #include <string.h> #include <stdlib.h> -#include <assert.h> #include <limits.h> #include <unistd.h> #include <math.h> #include <stdbool.h> #include "../../deadbeef.h" -#include "vcedit.h" +#include "../liboggedit/oggedit.h" #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) -//#define trace(...) { fprintf (stderr, __VA_ARGS__); } +// #define trace(...) { fprintf (stderr, __VA_ARGS__); } #define trace(fmt,...) +#if WORDS_BIGENDIAN +#define CVORBIS_ENDIANNESS 1 +#else +#define CVORBIS_ENDIANNESS 0 +#endif + #define RG_REFERENCE_LOUDNESS -1 #define DELIMITER "\n - \n" @@ -51,13 +55,11 @@ static DB_functions_t *deadbeef; typedef struct { DB_fileinfo_t info; OggVorbis_File vorbis_file; - vorbis_info *vi; int cur_bit_stream; - int startsample; - int endsample; - int currentsample; - int last_comment_update; - DB_playItem_t *ptrack; + float next_update; + DB_playItem_t *it; + const DB_playItem_t *new_track; + uint8_t *channel_map; } ogg_info_t; static size_t @@ -88,18 +90,18 @@ static const char *gain_tag_name(const int tag_enum) { switch(tag_enum) { - case DDB_REPLAYGAIN_ALBUMGAIN: - return "REPLAYGAIN_ALBUM_GAIN"; - case DDB_REPLAYGAIN_ALBUMPEAK: - return "REPLAYGAIN_ALBUM_PEAK"; - case DDB_REPLAYGAIN_TRACKGAIN: - return "REPLAYGAIN_TRACK_GAIN"; - case DDB_REPLAYGAIN_TRACKPEAK: - return "REPLAYGAIN_TRACK_PEAK"; - case RG_REFERENCE_LOUDNESS: - return "REPLAYGAIN_REFERENCE_LOUDNESS"; - default: - return NULL; + case DDB_REPLAYGAIN_ALBUMGAIN: + return "REPLAYGAIN_ALBUM_GAIN"; + case DDB_REPLAYGAIN_ALBUMPEAK: + return "REPLAYGAIN_ALBUM_PEAK"; + case DDB_REPLAYGAIN_TRACKGAIN: + return "REPLAYGAIN_TRACK_GAIN"; + case DDB_REPLAYGAIN_TRACKPEAK: + return "REPLAYGAIN_TRACK_PEAK"; + case RG_REFERENCE_LOUDNESS: + return "REPLAYGAIN_REFERENCE_LOUDNESS"; + default: + return NULL; } } @@ -107,18 +109,18 @@ static const char *gain_meta_key(const int tag_enum) { switch(tag_enum) { - case DDB_REPLAYGAIN_ALBUMGAIN: - return ":REPLAYGAIN_ALBUMGAIN"; - case DDB_REPLAYGAIN_ALBUMPEAK: - return ":REPLAYGAIN_ALBUMPEAK"; - case DDB_REPLAYGAIN_TRACKGAIN: - return ":REPLAYGAIN_TRACKGAIN"; - case DDB_REPLAYGAIN_TRACKPEAK: - return ":REPLAYGAIN_TRACKPEAK"; - case RG_REFERENCE_LOUDNESS: - return ":REPLAYGAIN_REFERENCE_LOUDNESS"; - default: - return NULL; + case DDB_REPLAYGAIN_ALBUMGAIN: + return ":REPLAYGAIN_ALBUMGAIN"; + case DDB_REPLAYGAIN_ALBUMPEAK: + return ":REPLAYGAIN_ALBUMPEAK"; + case DDB_REPLAYGAIN_TRACKGAIN: + return ":REPLAYGAIN_TRACKGAIN"; + case DDB_REPLAYGAIN_TRACKPEAK: + return ":REPLAYGAIN_TRACKPEAK"; + case RG_REFERENCE_LOUDNESS: + return ":REPLAYGAIN_REFERENCE_LOUDNESS"; + default: + return NULL; } } @@ -131,95 +133,62 @@ is_special_tag(const char *tag) { !strcasecmp(tag, gain_tag_name(RG_REFERENCE_LOUDNESS)); } -static const char *metainfo[] = { - "ARTIST", "artist", - "TITLE", "title", - "ALBUM", "album", - "TRACKNUMBER", "track", - "DATE", "year", - "GENRE", "genre", - "COMMENT", "comment", - "PERFORMER", "performer", - "COMPOSER", "composer", - "DISCNUMBER", "disc", - "COPYRIGHT", "copyright", - "TOTALTRACKS", "numtracks", - "TRACKTOTAL", "numtracks", - NULL -}; +static bool +add_meta(DB_playItem_t *it, const char *key, const char *value) +{ + const char *old_value = deadbeef->pl_find_meta(it, key); + if (old_value) { + char *new_value = malloc(strlen(old_value) + strlen(DELIMITER) + strlen(value) + 1); + if (new_value) { + sprintf(new_value, "%s"DELIMITER"%s", old_value, value); + deadbeef->pl_replace_meta(it, key, new_value); + free(new_value); + return true; + } + } + else { + deadbeef->pl_add_meta(it, key, value); + return true; + } +} + +static bool +replaygain_tag(DB_playItem_t *it, const int tag_enum, const char *tag, const char *value) +{ + if (strcasecmp(gain_tag_name(tag_enum), tag)) + return false; + + deadbeef->pl_set_item_replaygain (it, tag_enum, atof(value)); + return true; +} -// refresh_playlist == 1 means "send playlistchanged event if metadata had been changed" -// refresh_playlist == 2 means "don't change memory, just check for changes" static int -update_vorbis_comments (DB_playItem_t *it, vorbis_comment *vc, int refresh_playlist) { - if (refresh_playlist == 1) { - if (!update_vorbis_comments (it, vc, 2)) { - return 0; - } +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; } - if (vc) { - if (refresh_playlist != 2) { - deadbeef->pl_delete_all_meta (it); - } - for (int i = 0; i < vc->comments; i++) { - char *s = vc->user_comments[i]; - int m; - for (m = 0; metainfo[m]; m += 2) { - int l = strlen (metainfo[m]); - if (vc->comment_lengths[i] > l && !strncasecmp (metainfo[m], s, l) && s[l] == '=') { - if (refresh_playlist == 2) { - deadbeef->pl_lock (); - const char *val = deadbeef->pl_find_meta (it, metainfo[m+1]); - if (!val || strcmp (val, s+l+1)) { - deadbeef->pl_unlock (); - return 1; - } - deadbeef->pl_unlock (); - } - else { - deadbeef->pl_append_meta (it, metainfo[m+1], s + l + 1); - break; - } - } - } - if (!metainfo[m] && refresh_playlist != 2) { - if (!strncasecmp (s, "cuesheet=", 9)) { - deadbeef->pl_add_meta (it, "cuesheet", s + 9); - } - else if (!strncasecmp (s, "replaygain_album_gain=", 22)) { - deadbeef->pl_set_item_replaygain (it, DDB_REPLAYGAIN_ALBUMGAIN, atof (s+22)); - } - else if (!strncasecmp (s, "replaygain_album_peak=", 22)) { - deadbeef->pl_set_item_replaygain (it, DDB_REPLAYGAIN_ALBUMPEAK, atof (s+22)); - } - else if (!strncasecmp (s, "replaygain_track_gain=", 22)) { - deadbeef->pl_set_item_replaygain (it, DDB_REPLAYGAIN_TRACKGAIN, atof (s+22)); - } - else if (!strncasecmp (s, "replaygain_track_peak=", 22)) { - deadbeef->pl_set_item_replaygain (it, DDB_REPLAYGAIN_TRACKPEAK, atof (s+22)); + 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, '='))) { + *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)) { + if (!strcasecmp(tag, gain_tag_name(RG_REFERENCE_LOUDNESS))) + deadbeef->pl_replace_meta(it, gain_meta_key(RG_REFERENCE_LOUDNESS), value); + else + add_meta(it, oggedit_map_tag(tag, "tag2meta"), value); } - else if (!strncasecmp (s, "replaygain_reference_loudness=", 30)) { - deadbeef->pl_replace_meta(it, gain_meta_key(RG_REFERENCE_LOUDNESS), s+30); - } - else { - const char *p = s; - while (*p && *p != '=') { - p++; - } - if (*p == '=') { - char key[p-s+1]; - memcpy (key, s, p-s); - key[p-s] = 0; - deadbeef->pl_add_meta (it, key, p+1); - } - } - } + free(tag); } } - if (refresh_playlist == 2) { - return 0; - } + deadbeef->pl_add_meta (it, "title", NULL); uint32_t f = deadbeef->pl_get_item_flags (it); f &= ~DDB_TAG_MASK; @@ -230,35 +199,59 @@ update_vorbis_comments (DB_playItem_t *it, vorbis_comment *vc, int refresh_playl deadbeef->plt_modified (plt); deadbeef->plt_unref (plt); } + deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, 0, 0); + + return 0; +} - if (refresh_playlist) { - deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, 0, 0); +static void +set_meta_ll(DB_playItem_t *it, const char *key, const int64_t value) +{ + char string[11]; + sprintf(string, "%lld", value); + deadbeef->pl_replace_meta(it, key, string); +} + +static int +cvorbis_seek_sample (DB_fileinfo_t *_info, int sample) { + ogg_info_t *info = (ogg_info_t *)_info; + if (sample < 0) { + trace ("vorbis: negative seek sample - ignored, but it is a bug!\n"); + return -1; + } + if (!info->info.file) { + trace ("vorbis: file is NULL on seek\n"); + return -1; } + sample += info->it->startsample; + trace ("vorbis: seek to sample %d\n", sample); + int res = ov_pcm_seek (&info->vorbis_file, sample); + if (res != 0 && res != OV_ENOSEEK) { + trace ("vorbis: error %x seeking to sample %d\n", res, sample); + return -1; + } + int tell = ov_pcm_tell (&info->vorbis_file); + if (tell != sample) { + trace ("vorbis: failed to do sample-accurate seek (%d->%d)\n", sample, tell); + } + trace ("vorbis: seek successful\n") + _info->readpos = (float)(sample - info->it->startsample)/_info->fmt.samplerate; + info->next_update = -2; return 0; } static DB_fileinfo_t * cvorbis_open (uint32_t hints) { - DB_fileinfo_t *_info = malloc (sizeof (ogg_info_t)); - ogg_info_t *info = (ogg_info_t *)_info; - memset (info, 0, sizeof (ogg_info_t)); - return _info; + return calloc(1, sizeof (ogg_info_t)); } static int cvorbis_init (DB_fileinfo_t *_info, DB_playItem_t *it) { + trace("cvorbis_init\n"); ogg_info_t *info = (ogg_info_t *)_info; - info->info.file = NULL; - info->vi = NULL; - if (it->endsample > 0) { - info->cur_bit_stream = -1; - } - else { - int tracknum = deadbeef->pl_find_meta_int (it, ":TRACKNUM", -1); - info->cur_bit_stream = tracknum; - } - info->ptrack = it; + info->new_track = info->it = it; deadbeef->pl_item_ref (it); + deadbeef->pl_replace_meta (it, "!FILETYPE", "OggVorbis"); deadbeef->pl_lock (); info->info.file = deadbeef->fopen (deadbeef->pl_find_meta (it, ":URI")); @@ -267,8 +260,7 @@ cvorbis_init (DB_fileinfo_t *_info, DB_playItem_t *it) { trace ("ogg: failed to open file %s\n", deadbeef->pl_find_meta (it, ":URI")); return -1; } - int ln = deadbeef->fgetlength (info->info.file); - if (info->info.file->vfs->is_streaming () && ln == -1) { + if (info->info.file->vfs->is_streaming () && deadbeef->fgetlength (info->info.file) == -1) { ov_callbacks ovcb = { .read_func = cvorbis_fread, .seek_func = NULL, @@ -304,61 +296,55 @@ cvorbis_init (DB_fileinfo_t *_info, DB_playItem_t *it) { trace ("ov_open_callbacks returned %d\n", err); return -1; } -// deadbeef->pl_set_item_duration (it, ov_time_total (&vorbis_file, -1)); } - info->vi = ov_info (&info->vorbis_file, info->cur_bit_stream); - if (!info->vi) { // not a vorbis stream +// _info->readpos = 0; + if (info->info.file->vfs->is_streaming ()) { + info->it->startsample = 0; + if (deadbeef->pl_get_item_duration (it) < 0) { + it->endsample = -1; + } + else { + it->endsample = ov_pcm_total (&info->vorbis_file, -1)-1; + } + if (update_vorbis_comments (it, &info->vorbis_file, -1)) + return -1; + deadbeef->pl_set_meta_int(info->it, ":TRACKNUM", 0); + } + else { + cvorbis_seek_sample (_info, 1); // vorbisfile bug + } + + vorbis_info *vi = ov_info (&info->vorbis_file, -1); + if (!vi) { trace ("not a vorbis stream\n"); return -1; } - if (info->vi->rate <= 0) { + if (vi->rate <= 0) { trace ("vorbis: bad samplerate\n"); return -1; } _info->plugin = &plugin; _info->fmt.bps = 16; - //_info->dataSize = ov_pcm_total (&vorbis_file, -1) * vi->channels * 2; - _info->fmt.channels = info->vi->channels; - _info->fmt.samplerate = info->vi->rate; - + _info->fmt.samplerate = vi->rate; + _info->fmt.channels = vi->channels; + info->channel_map = oggedit_vorbis_channel_map(vi->channels); for (int i = 0; i < _info->fmt.channels; i++) { _info->fmt.channelmask |= 1 << i; } - _info->readpos = 0; - info->currentsample = 0; - if (!info->info.file->vfs->is_streaming ()) { - if (it->endsample > 0) { - info->startsample = it->startsample; - info->endsample = it->endsample; - plugin.seek_sample (_info, 0); - } - else { - info->startsample = 0; - info->endsample = ov_pcm_total (&info->vorbis_file, -1)-1; - } - } - else { - info->startsample = 0; - if (deadbeef->pl_get_item_duration (it) < 0) { - info->endsample = -1; - } - else { - info->endsample = ov_pcm_total (&info->vorbis_file, -1)-1; - } - vorbis_comment *vc = ov_comment (&info->vorbis_file, -1); - update_vorbis_comments (it, vc, 1); - } return 0; } static void cvorbis_free (DB_fileinfo_t *_info) { + trace("cvorbis_free\n"); ogg_info_t *info = (ogg_info_t *)_info; if (info) { - if (info->ptrack) { - deadbeef->pl_item_unref (info->ptrack); + if (info->it) { + deadbeef->pl_item_unref (info->it); } + free(info->channel_map); + info->channel_map = NULL; if (info->info.file) { if (info->vorbis_file.datasource) { ov_clear (&info->vorbis_file); @@ -371,165 +357,137 @@ cvorbis_free (DB_fileinfo_t *_info) { } } -static int -cvorbis_read (DB_fileinfo_t *_info, char *bytes, int size) { - ogg_info_t *info = (ogg_info_t *)_info; -// trace ("cvorbis_read %d bytes\n", size); +static void send_event(DB_playItem_t *it, const int event_enum) +{ + ddb_event_track_t *event = (ddb_event_track_t *)deadbeef->event_alloc(event_enum); + if (event->track = it) + deadbeef->pl_item_ref(event->track); + deadbeef->event_send((ddb_event_t *)event, 0, 0); +} - _info->fmt.channels = info->vi->channels; - _info->fmt.samplerate = info->vi->rate; +static bool +new_streaming_link(ogg_info_t *info, const int new_link) +{ + if (info->cur_bit_stream == 0 && new_link != 1) + return false; + + trace("Streaming link changed from %d to %d\n", info->cur_bit_stream, new_link); + deadbeef->pl_set_meta_int(info->it, ":TRACKNUM", new_link); + update_vorbis_comments(info->it, &info->vorbis_file, new_link); + send_event(info->it, DB_EV_SONGSTARTED); + send_event(info->it, DB_EV_TRACKINFOCHANGED); + deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, 0, 0); + info->cur_bit_stream = new_link; + + vorbis_info *vi = ov_info (&info->vorbis_file, new_link); + if (vi && info->info.fmt.channels != vi->channels || info->info.fmt.samplerate != vi->rate) { + // Streamer can't do this, so re-init the stream + trace("Stream channel count changed from %d to %d\n", info->info.fmt.channels, vi->channels); + trace("Stream channel count changed from %d to %d\n", info->info.fmt.samplerate, vi->rate); + deadbeef->sendmessage(DB_EV_PAUSE, 0, 0, 0); + deadbeef->sendmessage(DB_EV_TOGGLE_PAUSE, 0, 0, 0); + return true; + } + + return false; +} - int samplesize = _info->fmt.channels * _info->fmt.bps / 8; +static void +map_channels(int16_t *dest, const int16_t *src, const int16_t *src_end, const uint8_t *map, const int channel_count) +{ + while (src < src_end) { + for (uint8_t *map_ptr = (uint8_t *)map; map_ptr < map + channel_count; map_ptr++, src++) + dest[*map_ptr] = *src; + dest += channel_count; + } +} - if (!info->info.file->vfs->is_streaming ()) { - if (info->currentsample + size / samplesize > info->endsample) { - size = (info->endsample - info->currentsample + 1) * samplesize; - trace ("size truncated to %d bytes, cursample=%d, info->endsample=%d, totalsamples=%lld\n", size, info->currentsample, info->endsample, ov_pcm_total (&info->vorbis_file, -1)); - if (size <= 0) { - return 0; - } - } +static bool +is_playing_track(const DB_playItem_t *it) +{ + DB_playItem_t *track = deadbeef->streamer_get_playing_track(); + if (track) + deadbeef->pl_item_unref(track); + return track == it; +} + +static int +cvorbis_read (DB_fileinfo_t *_info, char *buffer, int bytes_to_read) { + ogg_info_t *info = (ogg_info_t *)_info; + + /* Work round some streamer limitations and infobar issue #22 */ + if (info->new_track && is_playing_track(info->new_track)) { + info->new_track = NULL; + send_event(info->it, DB_EV_TRACKINFOCHANGED); + info->next_update = -2; } - else { - if (info->ptrack && info->currentsample - info->last_comment_update > 5 * _info->fmt.samplerate) { - if (info->ptrack) { - info->last_comment_update = info->currentsample; - vorbis_comment *vc = ov_comment (&info->vorbis_file, -1); - update_vorbis_comments (info->ptrack, vc, 1); - ddb_event_track_t *ev = (ddb_event_track_t *)deadbeef->event_alloc (DB_EV_TRACKINFOCHANGED); - ev->track = info->ptrack; - if (ev->track) { - deadbeef->pl_item_ref (ev->track); - } - deadbeef->event_send ((ddb_event_t *)ev, 0, 0); - } - else { - info->ptrack = NULL; - } - } + + /* Don't read past the end of a sub-track */ + if (deadbeef->pl_get_item_flags(info->it) & DDB_IS_SUBTRACK) { + const ogg_int64_t bytes_left = (info->it->endsample - ov_pcm_tell(&info->vorbis_file)) * 2 * _info->fmt.channels; + if (bytes_left < bytes_to_read) + bytes_to_read = bytes_left; } -// trace ("cvorbis_read %d bytes[2]\n", size); - int initsize = size; - int64_t ret; - for (;;) - { - // read ogg - int endianess = 0; -#if WORDS_BIGENDIAN - endianess = 1; -#endif - if (_info->fmt.channels <= 2 || _info->fmt.channels == 4) { - ret = (int64_t)ov_read (&info->vorbis_file, bytes, size, endianess, 2, 1, &info->cur_bit_stream); - } - else { - int16_t temp[size/2]; - ret=ov_read (&info->vorbis_file, (char *)temp, size, endianess, 2, 1, &info->cur_bit_stream); - if (ret > 0) { - // remap channels to wav format - int idx = _info->fmt.channels - 3; - static int remap[6][8] = { - {0,2,1}, - {0,1,2,3}, // should not be used - {0,2,1,3,4}, - {0,2,1,4,5,3}, - {0,2,1,4,5,6,3}, - {0,2,1,6,7,4,5,3} - }; - - if (_info->fmt.channels > 8) { - fprintf (stderr, "vorbis plugin doesn't support more than 8 channels\n"); - return -1; - } + /* Read until we have enough bytes to satisfy streamer, or there are none left */ + char map_buffer[info->channel_map ? bytes_to_read : 0]; + char *ptr = info->channel_map ? map_buffer : buffer; + int ret = OV_HOLE; + int bytes_read = 0; + while ((ret > 0 || ret == OV_HOLE) && bytes_read < bytes_to_read) + { + int new_link = -1; + ret=ov_read (&info->vorbis_file, ptr+bytes_read, bytes_to_read-bytes_read, CVORBIS_ENDIANNESS, 2, 1, &new_link); - int i, j; - int16_t *src = temp; - int n = ret / samplesize; - for (i = 0; i < n; i++) { - for (j = 0; j < _info->fmt.channels; j++) { - ((int16_t *)(bytes + samplesize * i))[remap[idx][j]] = src[j]; - } - src += _info->fmt.channels; - } - } - } - if (ret <= 0) - { - if (ret < 0) { - trace ("ov_read returned %lld\n", ret); - switch (ret) { - case OV_HOLE: - trace ("OV_HOLE\n"); - break; - case OV_EBADLINK: - trace ("OV_EBADLINK\n"); - break; - case OV_EINVAL: - trace ("OV_EINVAL\n"); - break; - } - } - if (ret == OV_HOLE) { - continue; - } - // error or eof - break; + if (ret < 0) { + trace("cvorbis_read: ov_read returned %d\n", ret); } - else if (ret < size) - { - info->currentsample += ret / samplesize; - size -= ret; - bytes += ret; + else if (new_link != info->cur_bit_stream && !ov_seekable(&info->vorbis_file) && new_streaming_link(info, new_link)) { + bytes_read = bytes_to_read; } else { - info->currentsample += ret / samplesize; - size = 0; - break; + bytes_read += ret; } - } - _info->readpos = (float)(ov_pcm_tell(&info->vorbis_file)-info->startsample)/info->vi->rate; - //trace ("cvorbis_read got %d bytes, readpos %f, info->currentsample %d, ret %d\n", initsize-size, _info->readpos, info->currentsample, ret); - deadbeef->streamer_set_bitrate (ov_bitrate (&info->vorbis_file, info->cur_bit_stream)/1000); - return initsize - size; -} -static int -cvorbis_seek_sample (DB_fileinfo_t *_info, int sample) { - ogg_info_t *info = (ogg_info_t *)_info; - if (sample < 0) { - trace ("vorbis: negative seek sample - ignored, but it is a bug!\n"); - return -1; - } - if (!info->info.file) { - trace ("vorbis: file is NULL on seek\n"); - return -1; - } - trace ("vorbis: seek to sample %d\n", sample); - sample += info->startsample; - int res = ov_pcm_seek (&info->vorbis_file, sample); - if (res != 0 && res != OV_ENOSEEK) { - trace ("vorbis: error %x seeking to sample %d\n", res, sample); - return -1; +// trace("cvorbis_read got %d bytes towards %d bytes (%d bytes still required)\n", ret, bytes_to_read, bytes_to_read-bytes_read); } - int tell = ov_pcm_tell (&info->vorbis_file); - if (tell != sample) { - trace ("oggvorbis: failed to do sample-accurate seek (%d->%d)\n", sample, tell); + + if (info->channel_map) + map_channels((int16_t *)buffer, (int16_t *)map_buffer, (int16_t *)(ptr+bytes_read), info->channel_map, _info->fmt.channels); + + _info->readpos = (float)(ov_pcm_tell(&info->vorbis_file) - info->it->startsample) / _info->fmt.samplerate; + if (_info->readpos > info->next_update) { + const int rate = ov_bitrate_instant(&info->vorbis_file) / 1000; + if (rate > 0) { + deadbeef->streamer_set_bitrate(rate); + info->next_update = info->next_update <= 0 ? info->next_update + 1 : _info->readpos + 5; + } } - trace ("vorbis: seek successful\n") - info->currentsample = sample; - _info->readpos = (float)(ov_pcm_tell(&info->vorbis_file) - info->startsample)/info->vi->rate; - return 0; +// trace("cvorbis_read returning %d bytes out of %d\n", bytes_read, bytes_to_read); + return bytes_read; } static int cvorbis_seek (DB_fileinfo_t *_info, float time) { ogg_info_t *info = (ogg_info_t *)_info; - return cvorbis_seek_sample (_info, time * info->vi->rate); + return cvorbis_seek_sample (_info, time * info->info.fmt.samplerate); +} + +static off_t sample_offset(OggVorbis_File *vorbis_file, const ogg_int64_t sample) +{ + if (sample <= 0 || sample == ov_pcm_total(vorbis_file, -1)) + return 0; + + if (ov_pcm_seek(vorbis_file, sample)) { + trace("ov_pcm_seek failed to find sample %lld: %d\n", sample, ov_pcm_seek(vorbis_file, sample)); + return -1; + } + + return ov_raw_tell(vorbis_file); } static DB_playItem_t * cvorbis_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { - // check for validity DB_FILE *fp = deadbeef->fopen (fname); if (!fp) { trace ("vorbis: failed to fopen %s\n", fname); @@ -538,7 +496,6 @@ cvorbis_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { int64_t fsize = deadbeef->fgetlength (fp); if (fp->vfs->is_streaming ()) { DB_playItem_t *it = deadbeef->pl_item_alloc_init (fname, plugin.plugin.id); - deadbeef->pl_add_meta (it, "!FILETYPE", "OggVorbis"); deadbeef->plt_set_item_duration (plt, it, -1); deadbeef->pl_add_meta (it, "title", NULL); after = deadbeef->plt_insert_item (plt, after, it); @@ -553,7 +510,6 @@ cvorbis_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { .tell_func = cvorbis_ftell }; OggVorbis_File vorbis_file; - vorbis_info *vi; int err = ov_open_callbacks (fp, &vorbis_file, NULL, 0, ovcb); if (err != 0) { trace ("ov_open_callbacks returned %d\n", err); @@ -565,8 +521,8 @@ cvorbis_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { long nstreams = ov_streams (&vorbis_file); int currentsample = 0; for (int stream = 0; stream < nstreams; stream++) { - vi = ov_info (&vorbis_file, stream); - if (!vi) { // not a vorbis stream + const vorbis_info *vi = ov_info (&vorbis_file, stream); + if (!vi) { trace ("vorbis: ov_info failed for file %s stream %d\n", fname, stream); continue; } @@ -574,32 +530,35 @@ cvorbis_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { int totalsamples = ov_pcm_total (&vorbis_file, stream); DB_playItem_t *it = deadbeef->pl_item_alloc_init (fname, plugin.plugin.id); - deadbeef->pl_add_meta (it, ":FILETYPE", "OggVorbis"); deadbeef->pl_set_meta_int (it, ":TRACKNUM", stream); deadbeef->plt_set_item_duration (plt, it, duration); if (nstreams > 1) { it->startsample = currentsample; - it->endsample = currentsample + totalsamples; + it->endsample = currentsample + totalsamples - 1; deadbeef->pl_set_item_flags (it, DDB_IS_SUBTRACK); } - // metainfo - vorbis_comment *vc = ov_comment (&vorbis_file, stream); - update_vorbis_comments (it, vc, 0); + if (update_vorbis_comments (it, &vorbis_file, stream)) + continue; int samplerate = vi->rate; - char s[100]; - snprintf (s, sizeof (s), "%lld", fsize); - deadbeef->pl_add_meta (it, ":FILE_SIZE", s); + const off_t start_offset = sample_offset(&vorbis_file, it->startsample-1); + const off_t end_offset = sample_offset(&vorbis_file, it->endsample); + char *filetype = NULL; + const off_t stream_size = oggedit_vorbis_stream_info(deadbeef->fopen(fname), start_offset, end_offset, &filetype); + if (filetype) { + deadbeef->pl_replace_meta(it, ":FILETYPE", filetype); + free(filetype); + } + if (stream_size > 0) { + set_meta_ll(it, ":STREAM SIZE", stream_size); + deadbeef->pl_set_meta_int(it, ":BITRATE", 8.f * samplerate * stream_size / totalsamples / 1000); + } + set_meta_ll (it, ":FILE_SIZE", fsize); deadbeef->pl_add_meta (it, ":BPS", "16"); - snprintf (s, sizeof (s), "%d", vi->channels); - deadbeef->pl_add_meta (it, ":CHANNELS", s); - snprintf (s, sizeof (s), "%d", samplerate); - deadbeef->pl_add_meta (it, ":SAMPLERATE", s); - int br = ov_bitrate (&vorbis_file, stream)/1000; - snprintf (s, sizeof (s), "%d", br); - deadbeef->pl_add_meta (it, ":BITRATE", s); - + deadbeef->pl_set_meta_int (it, ":CHANNELS", vi->channels); + deadbeef->pl_set_meta_int (it, ":SAMPLERATE", samplerate); +// deadbeef->pl_set_meta_int (it, ":BITRATE", ov_bitrate (&vorbis_file, stream)/1000); if (nstreams == 1) { DB_playItem_t *cue = deadbeef->plt_insert_cue (plt, after, it, totalsamples, samplerate); @@ -610,10 +569,12 @@ cvorbis_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) { return cue; } - // embedded cue deadbeef->pl_lock (); - const char *cuesheet = deadbeef->pl_find_meta (it, "cuesheet"); - if (cuesheet) { + const char *cuesheet_meta = deadbeef->pl_find_meta (it, "cuesheet"); + if (cuesheet_meta) { + trace("Embedded cuesheet found for %s\n", fname); + const char *last_sheet = strstr(cuesheet_meta, DELIMITER); + const char *cuesheet = last_sheet ? last_sheet + strlen(DELIMITER) : cuesheet_meta; cue = deadbeef->plt_insert_cue_from_buffer (plt, after, it, cuesheet, strlen (cuesheet), totalsamples, samplerate); if (cue) { deadbeef->pl_unlock (); @@ -647,7 +608,6 @@ vorbis_stop (void) { } int cvorbis_read_metadata (DB_playItem_t *it) { - int err = -1; DB_FILE *fp = NULL; OggVorbis_File vorbis_file; vorbis_info *vi = NULL; @@ -661,7 +621,7 @@ cvorbis_read_metadata (DB_playItem_t *it) { } if (fp->vfs->is_streaming ()) { trace ("cvorbis_read_metadata: failed to fopen %s\n", deadbeef->pl_find_meta (it, ":URI")); - goto error; + return -1; } ov_callbacks ovcb = { .read_func = cvorbis_fread, @@ -672,28 +632,23 @@ cvorbis_read_metadata (DB_playItem_t *it) { int res = ov_open_callbacks (fp, &vorbis_file, NULL, 0, ovcb); if (res != 0) { trace ("cvorbis_read_metadata: ov_open_callbacks returned %d\n", res); - goto error; + return -1; } int tracknum = deadbeef->pl_find_meta_int (it, ":TRACKNUM", -1); vi = ov_info (&vorbis_file, tracknum); - if (!vi) { // not a vorbis stream + if (!vi) { trace ("cvorbis_read_metadata: failed to ov_open %s\n", deadbeef->pl_find_meta (it, ":URI")); - goto error; - } - - // metainfo - vorbis_comment *vc = ov_comment (&vorbis_file, tracknum); - if (vc) { - update_vorbis_comments (it, vc, 0); + ov_clear (&vorbis_file); + return -1; } - err = 0; -error: - if (fp) { + if (update_vorbis_comments (it, &vorbis_file, tracknum)) { ov_clear (&vorbis_file); + return -1; } - return err; + ov_clear (&vorbis_file); + return 0; } static void @@ -733,50 +688,16 @@ merge_gain_tag(DB_playItem_t *it, vorbis_comment *vc, vorbis_comment *tags, cons } } -static const char -*map_tag(const char *key) -{ - for (int i = 0; metainfo[i]; i += 2) - if (!strcasecmp (metainfo[i+1], key)) - return metainfo[i]; - return key; -} - static vorbis_comment -*create_tags_list(DB_playItem_t *it, const char *fname, vorbis_comment *tags) +*tags_list(DB_playItem_t *it, OggVorbis_File *vorbis_file) { - DB_FILE *fp = deadbeef->fopen (fname); - if (!fp) { - trace ("vorbis: failed to fopen %s\n", fname); - return NULL; - } - ov_callbacks ovcb = { - .read_func = cvorbis_fread, - .seek_func = cvorbis_fseek, - .close_func = cvorbis_fclose, - .tell_func = cvorbis_ftell - }; - OggVorbis_File vorbis_file; - int err = ov_test_callbacks (fp, &vorbis_file, NULL, 0, ovcb); - if (err != 0) { - trace ("ov_test_callbacks returned %d\n", err); - deadbeef->fclose (fp); + vorbis_comment *vc = ov_comment (vorbis_file, -1); + if (!vc) return NULL; - } - vorbis_comment *vc = ov_comment (&vorbis_file, -1); - if (!vc) { - trace ("ov_comment failed\n"); - ov_clear (&vorbis_file); + vorbis_comment *tags = calloc(1, sizeof(vorbis_comment)); + if (!tags) return NULL; - } - - vorbis_comment_init(tags); - if (!(tags->vendor = strdup(vc->vendor))) { - ov_clear (&vorbis_file); - trace("create_tags_list: cannot allocate tags list\n"); - return NULL; - } deadbeef->pl_lock (); merge_gain_tag(it, vc, tags, DDB_REPLAYGAIN_ALBUMGAIN, "%0.2f dB", -100, 100); @@ -784,15 +705,15 @@ static vorbis_comment merge_gain_tag(it, vc, tags, DDB_REPLAYGAIN_TRACKGAIN, "%0.2f dB", -100, 100); merge_gain_tag(it, vc, tags, DDB_REPLAYGAIN_TRACKPEAK, "%0.8f", 0, 2); merge_gain_tag(it, vc, tags, RG_REFERENCE_LOUDNESS, "%0.1f dB", 0, 128); - DB_metaInfo_t *m = deadbeef->pl_get_metadata_head(it); - while (m) { - if (m->key[0] != ':' && m->key[0] != '!' && !is_special_tag(m->key)) - split_tag(tags, map_tag(m->key), (char *)m->value); - m = m->next; + for (DB_metaInfo_t *m = deadbeef->pl_get_metadata_head(it); m; m = m->next) { + char *key = strdup(m->key); + if (key && key[0] != ':' && key[0] != '!' && !is_special_tag(key)) { + split_tag(tags, oggedit_map_tag(key, "meta2tag"), m->value); + free(key); + } } deadbeef->pl_unlock (); - ov_clear (&vorbis_file); return tags; } @@ -801,22 +722,46 @@ cvorbis_write_metadata (DB_playItem_t *it) { char fname[PATH_MAX]; deadbeef->pl_get_meta (it, ":URI", fname, sizeof (fname)); - vorbis_comment tags; - if (!create_tags_list(it, fname, &tags)) + DB_FILE *fp = deadbeef->fopen (fname); + if (!fp) { + trace ("cvorbis_write_metadata: failed to fopen %s\n", fname); return -1; + } + ov_callbacks ovcb = { + .read_func = cvorbis_fread, + .seek_func = cvorbis_fseek, + .close_func = cvorbis_fclose, + .tell_func = cvorbis_ftell + }; + OggVorbis_File vorbis_file; + int err = ov_test_callbacks (fp, &vorbis_file, NULL, 0, ovcb); + if (err != 0) { + trace ("ov_test_callbacks returned %d\n", err); + deadbeef->fclose (fp); + return -1; + } + + vorbis_comment *tags = tags_list(it, &vorbis_file); + ov_clear(&vorbis_file); + if (!tags) { + trace("cvorbis_write_metadata: tags list allocation failed\n"); + return -1; + } - char *vorbis_error = NULL; - const off_t file_size = vcedit_write_metadata (deadbeef->fopen(fname), fname, -1, tags.vendor, tags.comments, tags.user_comments, &vorbis_error); - vorbis_comment_clear(&tags); + deadbeef->pl_lock(); + 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_vorbis_metadata (deadbeef->fopen(fname), fname, 0, stream_size, tags->comments, tags->user_comments); + vorbis_comment_clear(tags); + free(tags); if (file_size <= 0) { - trace ("cvorbis_write_metadata: failed to write tags to %s, error: %s\n", fname, vorbis_error); + trace ("cvorbis_write_metadata: failed to write tags to %s, code %d\n", fname, file_size); return -1; } - deadbeef->pl_set_meta_int(it, ":FILE_SIZE", file_size); + set_meta_ll(it, ":FILE_SIZE", (int64_t)file_size); return cvorbis_read_metadata(it); - - return 0; } @@ -833,61 +778,61 @@ static DB_decoder_t plugin = { .plugin.name = "OggVorbis decoder", .plugin.descr = "OggVorbis decoder using standard xiph.org libraries", .plugin.copyright = - "OggVorbis plugin for DeaDBeeF\n" - "Copyright (C) 2009-2014 Alexey Yakovenko et al.\n" - "\n" - "vcedit.c\n" - "Ogg Vorbis plugin Ogg edit functions\n" - "\n" - "Copyright (C) 2014 Ian Nartowicz <deadbeef@nartowicz.co.uk>\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" - "Uses libogg,libvorbis Copyright (c) 2002, Xiph.org Foundation\n" - "\n" - "Redistribution and use in source and binary forms, with or without\n" - "modification, are permitted provided that the following conditions\n" - "are met:\n" - "\n" - "- Redistributions of source code must retain the above copyright\n" - "notice, this list of conditions and the following disclaimer.\n" - "\n" - "- Redistributions in binary form must reproduce the above copyright\n" - "notice, this list of conditions and the following disclaimer in the\n" - "documentation and/or other materials provided with the distribution.\n" - "\n" - "- Neither the name of the DeaDBeeF Player nor the names of its\n" - "contributors may be used to endorse or promote products derived from\n" - "this software without specific prior written permission.\n" - "\n" - "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n" - "``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n" - "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n" - "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR\n" - "CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n" - "EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n" - "PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n" - "PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n" - "LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n" - "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n" - "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" + "OggVorbis plugin for DeaDBeeF\n" + "Copyright (C) 2009-2014 Alexey Yakovenko et al.\n" + "\n" + "vcedit.c\n" + "Ogg Vorbis plugin Ogg edit functions\n" + "\n" + "Copyright (C) 2014 Ian Nartowicz <deadbeef@nartowicz.co.uk>\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" + "Uses libogg,libvorbis Copyright (c) 2002, Xiph.org Foundation\n" + "\n" + "Redistribution and use in source and binary forms, with or without\n" + "modification, are permitted provided that the following conditions\n" + "are met:\n" + "\n" + "- Redistributions of source code must retain the above copyright\n" + "notice, this list of conditions and the following disclaimer.\n" + "\n" + "- Redistributions in binary form must reproduce the above copyright\n" + "notice, this list of conditions and the following disclaimer in the\n" + "documentation and/or other materials provided with the distribution.\n" + "\n" + "- Neither the name of the DeaDBeeF Player nor the names of its\n" + "contributors may be used to endorse or promote products derived from\n" + "this software without specific prior written permission.\n" + "\n" + "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n" + "``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n" + "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n" + "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR\n" + "CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n" + "EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n" + "PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n" + "PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n" + "LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n" + "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n" + "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" , .plugin.website = "http://deadbeef.sf.net", .plugin.start = vorbis_start, |