summaryrefslogtreecommitdiff
path: root/plugins/vorbis/vcedit.c
blob: 2f30aeb6195e2a61fae08956c9be4f24df39dc0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
  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;
}