Skip to content

Commit

Permalink
liboggedit: update to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Dec 12, 2017
1 parent 9bc4561 commit bf6d447
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
8 changes: 4 additions & 4 deletions plugins/liboggedit/oggedit.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@
#define OGGEDIT_WRITE_ERROR -14
/* Failed to flush pages from a stream, fatal Ogg internal error */
#define OGGEDIT_FLUSH_FAILED -15
/* Renaming tewmporary file failed (usually sticky bit or directory permissions) */
/* Renaming temporary file failed (usually sticky bit or directory permissions) */
#define OGGEDIT_RENAME_FAILED -16
/* Image file length not acceptable */
#define OGGEDIT_BAD_FILE_LENGTH -100
/* Cannot read data from image file */
#define OGGEDIT_CANT_READ_IMAGE_FILE -101

/* oggedit_utils.c */
uint8_t *oggedit_vorbis_channel_map(const int channel_count);
uint8_t *oggedit_vorbis_channel_map(const unsigned channel_count);

// map deadbeef key to vorbiscomment key
// NOTE: this function may modify the key value, e.g. when upper-casing
const char *oggedit_map_tag(char *key, const char *in_or_out);

/* oggedit_art.c */
const char *oggedit_album_art_type(const int type);
const char *oggedit_album_art_type(const uint32_t type);
char *oggedit_album_art_tag(DB_FILE *fp, int *res);

/* oggedit_flac.c */
Expand All @@ -92,6 +92,6 @@ off_t oggedit_write_vorbis_metadata(DB_FILE *in, const char *fname, const off_t
/* oggedit_opus.c */
int oggedit_write_opus_file(DB_FILE *in, const char *outname, const off_t offset, const bool all_streams);
off_t oggedit_opus_stream_info(DB_FILE *in, const off_t start_offset, const off_t end_offset, char **codecs);
off_t oggedit_write_opus_metadata(DB_FILE *in, const char *fname, const off_t offset, const size_t stream_size, const int output_gain, const int num_tags, char **tags);
off_t oggedit_write_opus_metadata(DB_FILE *in, const char *fname, const off_t offset, const off_t stream_size, const int output_gain, const uint32_t num_tags, char **tags);

#endif /* __OGGEDIT_H */
2 changes: 1 addition & 1 deletion plugins/liboggedit/oggedit_art.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <string.h>
#include "oggedit_internal.h"

const char *oggedit_album_art_type(const int type)
const char *oggedit_album_art_type(const uint32_t type)
{
switch (type) {
case 1:
Expand Down
35 changes: 23 additions & 12 deletions plugins/liboggedit/oggedit_internal.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ static char *cat_string(char *dest, const char *src, const char *sep)
return strcat(strcat(more, sep), src);
}

static int64_t int32_to_unsigned(int32_t value)
{
/* Represent a 32-bit numeric field as a positive signed integer, maintaining order */
return value < 0 ? value + UINT32_MAX + 1 : value;
}

static const char *codec_name(ogg_page *og)
{
typedef struct {
const size_t length;
const unsigned length;
const char *codec;
const char *magic;
} codec_t;
Expand Down Expand Up @@ -80,7 +86,7 @@ static const char *codec_name(ogg_page *og)
};

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)))
if (og->body_len >= match->length && !memcmp(og->body, match->magic, strlen(match->codec)))
return match->codec;

return "unknown";
Expand Down Expand Up @@ -109,7 +115,7 @@ static bool ensure_directory(const char *path)
{
struct stat stat_struct;
if (!stat(path, &stat_struct))
return !S_ISDIR(stat_struct.st_mode);
return S_ISDIR(stat_struct.st_mode);

if (errno != ENOENT)
return false;
Expand All @@ -118,10 +124,10 @@ static bool ensure_directory(const char *path)
if (!dir)
return false;

const int bad_dir = ensure_directory(dirname(dir));
const int is_dir = ensure_directory(dirname(dir));
free(dir);

return !bad_dir && !mkdir(path, 0777);
return is_dir && !mkdir(path, 0755);
}

FILE *open_new_file(const char *outname)
Expand Down Expand Up @@ -173,6 +179,11 @@ void cleanup(DB_FILE *in, FILE *out, ogg_sync_state *oy, void *buffer)
free(buffer);
}

static bool is_data_page(ogg_page *og, int64_t codec_serial, int64_t serial)
{
return ogg_page_granulepos(og) != 0 && serial == codec_serial;
}

static int64_t get_page(DB_FILE *in, ogg_sync_state *oy, ogg_page *og)
{
uint16_t chunks_left = MAXPAGE / CHUNKSIZE;
Expand All @@ -188,7 +199,7 @@ static int64_t get_page(DB_FILE *in, ogg_sync_state *oy, ogg_page *og)
ogg_sync_wrote(oy, bytes);
}

return ogg_page_serialno(og) + UINT32_MAX + 1;
return int32_to_unsigned(ogg_page_serialno(og));
}

static int64_t skip_to_bos(DB_FILE *in, ogg_sync_state *oy, ogg_page *og, const off_t offset)
Expand Down Expand Up @@ -271,7 +282,7 @@ int64_t copy_up_to_header(DB_FILE *in, FILE *out, ogg_sync_state *oy, ogg_page *
return serial;
}

long flush_stream(FILE *out, ogg_stream_state *os)
int64_t flush_stream(FILE *out, ogg_stream_state *os)
{
ogg_page og;
#ifdef HAVE_OGG_STREAM_FLUSH_FILL
Expand All @@ -282,7 +293,7 @@ long flush_stream(FILE *out, ogg_stream_state *os)
if (!write_page(out, &og))
return OGGEDIT_WRITE_ERROR;

const long pageno = ogg_stream_check(os) ? OGGEDIT_FLUSH_FAILED : ogg_page_pageno(&og);
const int64_t pageno = ogg_stream_check(os) ? OGGEDIT_FLUSH_FAILED : int32_to_unsigned(ogg_page_pageno(&og));
ogg_stream_clear(os);
return pageno;
}
Expand Down Expand Up @@ -321,7 +332,7 @@ off_t codec_stream_size(DB_FILE *in, ogg_sync_state *oy, const off_t start_offse
}

/* Skip to the first codec data page */
while (serial > OGGEDIT_EOF && !(ogg_page_granulepos(&og) > 0 && serial == codec_serial))
while (serial > OGGEDIT_EOF && !is_data_page(&og, codec_serial, serial))
serial = get_page(in, oy, &og);
if (serial <= OGGEDIT_EOF)
return serial;
Expand Down Expand Up @@ -383,14 +394,14 @@ int64_t init_read_stream(DB_FILE *in, ogg_sync_state *oy, ogg_stream_state *os,
return OGGEDIT_OK;
}

int read_packet(DB_FILE *in, ogg_sync_state *oy, ogg_stream_state *os, ogg_page *og, ogg_packet *header, int pages)
int64_t read_packet(DB_FILE *in, ogg_sync_state *oy, ogg_stream_state *os, ogg_page *og, ogg_packet *header, int64_t pages)
{
ogg_packet op;
do {
while (ogg_stream_packetpeek(os, NULL) == 0) {
const int64_t serial = get_page(in, oy, og);
if (serial <= OGGEDIT_EOF) {
return (int)serial;
return serial;
}
if ((uint32_t)os->serialno == (uint32_t)serial) {
pages++;
Expand Down Expand Up @@ -463,7 +474,7 @@ int64_t copy_remaining_pages(DB_FILE *in, FILE *out, ogg_sync_state *oy, const i
int64_t serial;
do
serial = get_page(in, oy, &og);
while (serial > OGGEDIT_EOF && serial == codec_serial && ogg_page_granulepos(&og) <= 0);
while (serial > OGGEDIT_EOF && is_data_page(&og, codec_serial, serial));
if (serial <= OGGEDIT_EOF)
return serial;

Expand Down
4 changes: 2 additions & 2 deletions plugins/liboggedit/oggedit_internal.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ off_t file_size(const char *fname);
void cleanup(DB_FILE *in, FILE *out, ogg_sync_state *oy, void *buffer);
int64_t copy_up_to_codec(DB_FILE *in, FILE *out, ogg_sync_state *oy, ogg_page *og, const off_t start_offset, const off_t link_offset, const char *codec);
int64_t copy_up_to_header(DB_FILE *in, FILE *out, ogg_sync_state *oy, ogg_page *og, const int64_t codec_serial);
long flush_stream(FILE *out, ogg_stream_state *os);
int64_t flush_stream(FILE *out, ogg_stream_state *os);
char *codec_names(DB_FILE *in, ogg_sync_state *oy, const off_t link_offset);
off_t codec_stream_size(DB_FILE *in, ogg_sync_state *oy, const off_t start_offset, const off_t end_offset, const char *codec);
char *parse_vendor(const ogg_packet *op, const size_t magic_length);
int64_t init_read_stream(DB_FILE *in, ogg_sync_state *oy, ogg_stream_state *os, ogg_page *og, const off_t offset, const char *codec);
int read_packet(DB_FILE *in, ogg_sync_state *oy, ogg_stream_state *os, ogg_page *og, ogg_packet *header, int pages);
int64_t read_packet(DB_FILE *in, ogg_sync_state *oy, ogg_stream_state *os, ogg_page *og, ogg_packet *header, int64_t pages);
ogg_packet *fill_vc_packet(const char *magic, const size_t magic_length, const char *vendor, const size_t num_tags, char **tags, const bool framing, const size_t padding, ogg_packet *op);
size_t vc_size(const char *vendor, size_t num_tags, char **tags);
int64_t copy_remaining_pages(DB_FILE *in, FILE *out, ogg_sync_state *oy, const int64_t codec_serial, uint32_t pageno);
Expand Down
18 changes: 9 additions & 9 deletions plugins/liboggedit/oggedit_opus.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ off_t oggedit_opus_stream_info(DB_FILE *in, const off_t start_offset, const off_
ogg_sync_state oy;
ogg_sync_init(&oy);
*codecs = codec_names(in, &oy, start_offset);
const int64_t stream_size = codec_stream_size(in, &oy, start_offset, end_offset, OPUSNAME);
const off_t stream_size = codec_stream_size(in, &oy, start_offset, end_offset, OPUSNAME);
cleanup(in, NULL, &oy, NULL);
return stream_size;
}

static ptrdiff_t check_opus_header(DB_FILE *in, ogg_sync_state *oy, const off_t offset, char **vendor)
static long check_opus_header(DB_FILE *in, ogg_sync_state *oy, const off_t offset, char **vendor)
{
ogg_stream_state os;
ogg_page og;
Expand All @@ -77,7 +77,7 @@ static ptrdiff_t check_opus_header(DB_FILE *in, ogg_sync_state *oy, const off_t
return serial;

ogg_packet op;
const int pages = read_packet(in, oy, &os, &og, &op, 1);
const long pages = read_packet(in, oy, &os, &og, &op, 1);
ogg_stream_clear(&os);
if (pages <= OGGEDIT_EOF)
return pages;
Expand All @@ -98,7 +98,7 @@ static ptrdiff_t check_opus_header(DB_FILE *in, ogg_sync_state *oy, const off_t
return op.bytes;
}

static long write_opus_tags(FILE *out, const int64_t serial, const char *vendor, const size_t num_tags, char **tags, const size_t padding)
static int64_t write_opus_tags(FILE *out, const int64_t serial, const char *vendor, const uint32_t num_tags, char **tags, const size_t padding)
{
ogg_packet op;
if (!fill_vc_packet(TAGMAGIC, strlen(TAGMAGIC), vendor, num_tags, tags, false, padding, &op))
Expand All @@ -115,7 +115,7 @@ static long write_opus_tags(FILE *out, const int64_t serial, const char *vendor,
return flush_stream(out, &os);
}

off_t oggedit_write_opus_metadata(DB_FILE *in, const char *fname, const off_t offset, const size_t stream_size, const int output_gain, const int num_tags, char **tags)
off_t oggedit_write_opus_metadata(DB_FILE *in, const char *fname, const off_t offset, const off_t stream_size, const int output_gain, const uint32_t num_tags, char **tags)
{
off_t res;
char tempname[PATH_MAX] = "";
Expand All @@ -131,15 +131,15 @@ off_t oggedit_write_opus_metadata(DB_FILE *in, const char *fname, const off_t of
}

/* Should we write the tags packet directly into the existing file ... */
const ptrdiff_t tags_packet_size = check_opus_header(in, &oy, offset, &vendor);
const long tags_packet_size = check_opus_header(in, &oy, offset, &vendor);
if (tags_packet_size <= OGGEDIT_EOF) {
res = tags_packet_size;
goto cleanup;
}
const size_t metadata_size = strlen(TAGMAGIC) + vc_size(vendor, num_tags, tags);
ptrdiff_t padding = tags_packet_size - metadata_size;
size_t padding = tags_packet_size - metadata_size;
const off_t file_size_k = in->vfs->getlength(in) / 1000;
const size_t stream_size_k = stream_size ? stream_size / 1000 : file_size_k;
const off_t stream_size_k = stream_size ? stream_size / 1000 : file_size_k;
if (file_size_k < 100 || padding < 0 || padding > file_size_k/10+stream_size_k+metadata_size) {
res = open_temp_file(fname, tempname, &out);
if (res) {
Expand Down Expand Up @@ -168,7 +168,7 @@ off_t oggedit_write_opus_metadata(DB_FILE *in, const char *fname, const off_t of
res = opus_serial;
goto cleanup;
}
const long pageno = write_opus_tags(out, opus_serial, vendor, num_tags, tags, (size_t)padding);
const int64_t pageno = write_opus_tags(out, opus_serial, vendor, num_tags, tags, padding);
if (pageno < OGGEDIT_EOF) {
res = pageno;
goto cleanup;
Expand Down
6 changes: 3 additions & 3 deletions plugins/liboggedit/oggedit_utils.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#include <ctype.h>
#include "oggedit_internal.h"

uint8_t *oggedit_vorbis_channel_map(const int channel_count)
uint8_t *oggedit_vorbis_channel_map(const unsigned channel_count)
{
size_t map_size = channel_count * sizeof(uint8_t);
unsigned map_size = channel_count * sizeof(uint8_t);
uint8_t *map = malloc(map_size);
if (!map)
return NULL;
Expand Down Expand Up @@ -121,7 +121,7 @@ const char *oggedit_map_tag(char *key, const char *in_or_out)

/* Upper-case all Vorbis Comment tag names */
if (*in_or_out == 'm')
for (size_t i = 0; key[i]; i++)
for (unsigned i = 0; key[i]; i++)
key[i] = toupper(key[i]);

return key;
Expand Down

0 comments on commit bf6d447

Please sign in to comment.