File: vorbis.c

package info (click to toggle)
sox 14.4.2+git20190427-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,720 kB
  • sloc: ansic: 43,223; sh: 11,692; makefile: 333
file content (430 lines) | stat: -rw-r--r-- 11,631 bytes parent folder | download
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* libSoX Ogg Vorbis sound format handler
 * Copyright 2001, Stan Seibert <indigo@aztec.asu.edu>
 *
 * Portions from oggenc, (c) Michael Smith <msmith@labyrinth.net.au>,
 * ogg123, (c) Kenneth Arnold <kcarnold@yahoo.com>, and
 * libvorbisfile (c) Xiphophorus Company
 *
 * May 9, 2001 - Stan Seibert (indigo@aztec.asu.edu)
 * Ogg Vorbis handler initially written.
 *
 * July 5, 1991 - Skeleton file
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained.
 * Lance Norskog And Sundry Contributors are not responsible for
 * the consequences of using this software.
 */

#include "sox_i.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
#include <vorbis/vorbisenc.h>

#define DEF_BUF_LEN 4096

#define BUF_ERROR -1
#define BUF_EOF  0
#define BUF_DATA 1

#define HEADER_ERROR 0
#define HEADER_OK   1

/* Private data for Ogg Vorbis file */
typedef struct {
  ogg_stream_state os;
  ogg_page og;
  ogg_packet op;

  vorbis_dsp_state vd;
  vorbis_block vb;
  vorbis_info vi;
} vorbis_enc_t;

typedef struct {
  /* Decoding data */
  OggVorbis_File *vf;
  char *buf;
  size_t buf_len;
  size_t start;
  size_t end;     /* Unsent data samples in buf[start] through buf[end-1] */
  int current_section;
  int eof;

  vorbis_enc_t *vorbis_enc_data;
} priv_t;

/******** Callback functions used in ov_open_callbacks ************/

static size_t callback_read(void* ptr, size_t size, size_t nmemb, void* ft_data)
{
  sox_format_t* ft = (sox_format_t*)ft_data;
  size_t ret = lsx_readbuf(ft, ptr, size * nmemb);
  return ret / size;
}

static int callback_seek(void* ft_data, ogg_int64_t off, int whence)
{
  sox_format_t* ft = (sox_format_t*)ft_data;
  int ret = ft->seekable ? lsx_seeki(ft, (off_t)off, whence) : -1;

  if (ret == EBADF)
    ret = -1;
  return ret;
}

static int callback_close(void* ft_data UNUSED)
{
  /* Do nothing so sox can close the file for us */
  return 0;
}

static long callback_tell(void* ft_data)
{
  sox_format_t* ft = (sox_format_t*)ft_data;
  return lsx_tell(ft);
}

/********************* End callbacks *****************************/


/*
 * Do anything required before you start reading samples.
 * Read file header.
 *      Find out sampling rate,
 *      size and encoding of samples,
 *      mono/stereo/quad.
 */
static int startread(sox_format_t * ft)
{
  priv_t * vb = (priv_t *) ft->priv;
  vorbis_info *vi;
  vorbis_comment *vc;
  int i;

  ov_callbacks callbacks = {
    callback_read,
    callback_seek,
    callback_close,
    callback_tell
  };

  /* Allocate space for decoding structure */
  vb->vf = lsx_malloc(sizeof(OggVorbis_File));

  /* Init the decoder */
  if (ov_open_callbacks(ft, vb->vf, NULL, (size_t) 0, callbacks) < 0) {
    lsx_fail_errno(ft, SOX_EHDR, "Input not an Ogg Vorbis audio stream");
    return (SOX_EOF);
  }

  /* Get info about the Ogg Vorbis stream */
  vi = ov_info(vb->vf, -1);
  vc = ov_comment(vb->vf, -1);

  /* Record audio info */
  ft->signal.rate = vi->rate;
  ft->encoding.encoding = SOX_ENCODING_VORBIS;
  ft->signal.channels = vi->channels;

  /* ov_pcm_total doesn't work on non-seekable files so
   * skip that step in that case.  Also, it reports
   * "frame"-ish results so we must * channels.
   */
  if (ft->seekable)
    ft->signal.length = ov_pcm_total(vb->vf, -1) * ft->signal.channels;

  /* Record comments */
  for (i = 0; i < vc->comments; i++)
    sox_append_comment(&ft->oob.comments, vc->user_comments[i]);

  /* Setup buffer */
  vb->buf_len = DEF_BUF_LEN;
  vb->buf_len -= vb->buf_len % (vi->channels*2); /* 2 bytes per sample */
  vb->buf = lsx_calloc(vb->buf_len, sizeof(char));
  vb->start = vb->end = 0;

  /* Fill in other info */
  vb->eof = 0;
  vb->current_section = -1;

  return (SOX_SUCCESS);
}


/* Refill the buffer with samples.  Returns BUF_EOF if the end of the
 * vorbis data was reached while the buffer was being filled,
 * BUF_ERROR is something bad happens, and BUF_DATA otherwise */
static int refill_buffer(priv_t * vb)
{
  int num_read;

  if (vb->start == vb->end)     /* Samples all played */
    vb->start = vb->end = 0;

  while (vb->end < vb->buf_len) {
    num_read = ov_read(vb->vf, vb->buf + vb->end,
        (int) (vb->buf_len - vb->end), 0, 2, 1, &vb->current_section);
    if (num_read == 0)
      return (BUF_EOF);
    else if (num_read == OV_HOLE)
      lsx_warn("Warning: hole in stream; probably harmless");
    else if (num_read < 0)
      return (BUF_ERROR);
    else
      vb->end += num_read;
  }
  return (BUF_DATA);
}


/*
 * Read up to len samples from file.
 * Convert to signed longs.
 * Place in buf[].
 * Return number of samples read.
 */

static size_t read_samples(sox_format_t * ft, sox_sample_t * buf, size_t len)
{
  priv_t * vb = (priv_t *) ft->priv;
  size_t i;
  int ret;
  sox_sample_t l;


  for (i = 0; i < len; i++) {
    if (vb->start == vb->end) {
      if (vb->eof)
        break;
      ret = refill_buffer(vb);
      if (ret == BUF_EOF || ret == BUF_ERROR) {
        vb->eof = 1;
        if (vb->end == 0)
          break;
      }
    }

    l = (vb->buf[vb->start + 1] << 24)
        | (0xffffff & (vb->buf[vb->start] << 16));
    *(buf + i) = l;
    vb->start += 2;
  }
  return i;
}

/*
 * Do anything required when you stop reading samples.
 * Don't close input file!
 */
static int stopread(sox_format_t * ft)
{
  priv_t * vb = (priv_t *) ft->priv;

  free(vb->buf);
  ov_clear(vb->vf);

  return (SOX_SUCCESS);
}

/* Write a page of ogg data to a file.  Taken directly from encode.c in
 * oggenc.   Returns the number of bytes written. */
static int oe_write_page(ogg_page * page, sox_format_t * ft)
{
  int written;

  written = lsx_writebuf(ft, page->header, (size_t) page->header_len);
  written += lsx_writebuf(ft, page->body, (size_t) page->body_len);

  return written;
}

/* Write out the header packets.  Derived mostly from encode.c in oggenc.
 * Returns HEADER_OK if the header can be written, HEADER_ERROR otherwise. */
static int write_vorbis_header(sox_format_t * ft, vorbis_enc_t * ve)
{
  ogg_packet header_main;
  ogg_packet header_comments;
  ogg_packet header_codebooks;
  vorbis_comment vc;
  int i, ret = HEADER_OK;

  memset(&vc, 0, sizeof(vc));
  vc.comments = sox_num_comments(ft->oob.comments);
  if (vc.comments) {     /* Make the comment structure */
    vc.comment_lengths = lsx_calloc((size_t)vc.comments, sizeof(*vc.comment_lengths));
    vc.user_comments = lsx_calloc((size_t)vc.comments, sizeof(*vc.user_comments));
    for (i = 0; i < vc.comments; ++i) {
      static const char prepend[] = "Comment=";
      char * text = lsx_calloc(strlen(prepend) + strlen(ft->oob.comments[i]) + 1, sizeof(*text));
      /* Prepend `Comment=' if no field-name already in the comment */
      if (!strchr(ft->oob.comments[i], '='))
        strcpy(text, prepend);
      vc.user_comments[i] = strcat(text, ft->oob.comments[i]);
      vc.comment_lengths[i] = strlen(text);
    }
  }
  if (vorbis_analysis_headerout(    /* Build the packets */
      &ve->vd, &vc, &header_main, &header_comments, &header_codebooks) < 0) {
      ret = HEADER_ERROR;
      goto cleanup;
  }

  ogg_stream_packetin(&ve->os, &header_main);   /* And stream them out */
  ogg_stream_packetin(&ve->os, &header_comments);
  ogg_stream_packetin(&ve->os, &header_codebooks);

  while (ogg_stream_flush(&ve->os, &ve->og) && ret == HEADER_OK)
    if (!oe_write_page(&ve->og, ft))
      ret = HEADER_ERROR;
cleanup:
  for (i = 0; i < vc.comments; ++i)
    free(vc.user_comments[i]);
  free(vc.user_comments);
  free(vc.comment_lengths);
  return ret;
}

static int startwrite(sox_format_t * ft)
{
  priv_t * vb = (priv_t *) ft->priv;
  vorbis_enc_t *ve;
  long rate;
  double quality = 3;           /* Default compression quality gives ~112kbps */

  ft->encoding.encoding = SOX_ENCODING_VORBIS;

  /* Allocate memory for all of the structures */
  ve = vb->vorbis_enc_data = lsx_malloc(sizeof(vorbis_enc_t));

  vorbis_info_init(&ve->vi);

  /* TODO */
  rate = ft->signal.rate;
  if (rate)
    lsx_fail_errno(ft, SOX_EHDR,
      "Error setting-up Ogg Vorbis encoder; check sample-rate & # of channels");

  /* Use encoding to average bit rate of VBR as specified by the -C option */
  if (ft->encoding.compression != HUGE_VAL) {
    if (ft->encoding.compression < -1 || ft->encoding.compression > 10) {
      lsx_fail_errno(ft, SOX_EINVAL,
                     "Vorbis compression quality nust be between -1 and 10");
      return SOX_EOF;
    }
    quality = ft->encoding.compression;
  }
#define IGNORE_WARNING \
  if (vorbis_encode_init_vbr(&ve->vi, ft->signal.channels, ft->signal.rate + .5, quality / 10))
#include "ignore-warning.h"
  {
    lsx_fail_errno(ft, SOX_EFMT, "libVorbis cannot encode this sample-rate or # of channels");
    return SOX_EOF;
  }

  vorbis_analysis_init(&ve->vd, &ve->vi);
  vorbis_block_init(&ve->vd, &ve->vb);

  ogg_stream_init(&ve->os, INT_MAX & (int)RANQD1);  /* Random serial number */

  if (write_vorbis_header(ft, ve) == HEADER_ERROR) {
    lsx_fail_errno(ft, SOX_EHDR,
                   "Error writing header for Ogg Vorbis audio stream");
    return (SOX_EOF);
  }

  return (SOX_SUCCESS);
}

static size_t write_samples(sox_format_t * ft, const sox_sample_t * buf,
                        size_t len)
{
  priv_t * vb = (priv_t *) ft->priv;
  vorbis_enc_t *ve = vb->vorbis_enc_data;
  size_t samples = len / ft->signal.channels;
  float **buffer = vorbis_analysis_buffer(&ve->vd, (int) samples);
  size_t i, j;
  int ret;
  int eos = 0;

  /* Copy samples into vorbis buffer */
  for (i = 0; i < samples; i++)
    for (j = 0; j < ft->signal.channels; j++)
      buffer[j][i] = buf[i * ft->signal.channels + j]
          / ((float) SOX_SAMPLE_MAX);

  vorbis_analysis_wrote(&ve->vd, (int) samples);

  while (vorbis_analysis_blockout(&ve->vd, &ve->vb) == 1) {
    /* Do the main analysis, creating a packet */
    vorbis_analysis(&ve->vb, &ve->op);
    vorbis_bitrate_addblock(&ve->vb);

    /* Add packet to bitstream */
    while (vorbis_bitrate_flushpacket(&ve->vd, &ve->op)) {
      ogg_stream_packetin(&ve->os, &ve->op);

      /* If we've gone over a page boundary, we can do actual
       * output, so do so (for however many pages are available)
       */

      while (!eos) {
        int result = ogg_stream_pageout(&ve->os, &ve->og);

        if (!result)
          break;

        ret = oe_write_page(&ve->og, ft);
        if (!ret)
          return 0;

        if (ogg_page_eos(&ve->og))
          eos = 1;
      }
    }
  }

  return (len);
}

static int stopwrite(sox_format_t * ft)
{
  priv_t * vb = (priv_t *) ft->priv;
  vorbis_enc_t *ve = vb->vorbis_enc_data;

  /* Close out the remaining data */
  write_samples(ft, NULL, (size_t) 0);

  ogg_stream_clear(&ve->os);
  vorbis_block_clear(&ve->vb);
  vorbis_dsp_clear(&ve->vd);
  vorbis_info_clear(&ve->vi);

  return (SOX_SUCCESS);
}

static int seek(sox_format_t * ft, uint64_t offset)
{
  priv_t * vb = (priv_t *) ft->priv;

  return ov_pcm_seek(vb->vf, (ogg_int64_t)(offset / ft->signal.channels))? SOX_EOF:SOX_SUCCESS;
}

LSX_FORMAT_HANDLER(vorbis)
{
  static const char *names[] = {"vorbis", "ogg", NULL};
  static const unsigned encodings[] = {SOX_ENCODING_VORBIS, 0, 0};
  static sox_format_handler_t handler = {SOX_LIB_VERSION_CODE,
    "Xiph.org's ogg-vorbis lossy compression", names, 0,
    startread, read_samples, stopread,
    startwrite, write_samples, stopwrite,
    seek, encodings, NULL, sizeof(priv_t)
  };
  return &handler;
}