2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2002-2005 Michael Smith <msmith@xiph.org>
4 ** Copyright (C) 2007 John ffitch
6 ** This program is free software ; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation ; either version 2.1 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU Lesser General Public License for more details.
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program ; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ** Much of this code is based on the examples in libvorbis from the
23 ** XIPHOPHORUS Company http://www.xiph.org/ which has a BSD-style Licence
24 ** Copyright (c) 2002, Xiph.org Foundation
26 ** Redistribution and use in source and binary forms, with or without
27 ** modification, are permitted provided that the following conditions
30 ** - Redistributions of source code must retain the above copyright
31 ** notice, this list of conditions and the following disclaimer.
33 ** - Redistributions in binary form must reproduce the above copyright
34 ** notice, this list of conditions and the following disclaimer in the
35 ** documentation and/or other materials provided with the distribution.
37 ** - Neither the name of the Xiph.org Foundation nor the names of its
38 ** contributors may be used to endorse or promote products derived from
39 ** this software without specific prior written permission.
41 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 ** ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
45 ** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE,
48 ** DATA, OR PROFITS ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 #if HAVE_EXTERNAL_LIBS
74 #include <vorbis/codec.h>
75 #include <vorbis/vorbisenc.h>
79 typedef int convert_func (int, void *, int, int, float **) ;
81 static int vorbis_read_header (SF_PRIVATE
*psf
, int log_data
) ;
82 static int vorbis_write_header (SF_PRIVATE
*psf
, int calc_length
) ;
83 static int vorbis_close (SF_PRIVATE
*psf
) ;
84 static int vorbis_command (SF_PRIVATE
*psf
, int command
, void *data
, int datasize
) ;
85 static sf_count_t
vorbis_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
86 static sf_count_t
vorbis_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
87 static sf_count_t
vorbis_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
88 static sf_count_t
vorbis_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
89 static sf_count_t
vorbis_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
90 static sf_count_t
vorbis_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
91 static sf_count_t
vorbis_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
92 static sf_count_t
vorbis_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
93 static sf_count_t
vorbis_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
94 static sf_count_t
vorbis_read_sample (SF_PRIVATE
*psf
, void *ptr
, sf_count_t lens
, convert_func
*transfn
) ;
95 static sf_count_t
vorbis_length (SF_PRIVATE
*psf
) ;
102 static STR_PAIRS vorbis_metatypes
[] =
103 { { SF_STR_TITLE
, "Title" },
104 { SF_STR_COPYRIGHT
, "Copyright" },
105 { SF_STR_SOFTWARE
, "Software" },
106 { SF_STR_ARTIST
, "Artist" },
107 { SF_STR_COMMENT
, "Comment" },
108 { SF_STR_DATE
, "Date" },
109 { SF_STR_ALBUM
, "Album" },
110 { SF_STR_LICENSE
, "License" },
114 { /* Count current location */
116 /* Struct that stores all the static vorbis bitstream settings */
118 /* Struct that stores all the bitstream user comments */
119 vorbis_comment vcomment
;
120 /* Ventral working state for the packet->PCM decoder */
121 vorbis_dsp_state vdsp
;
122 /* Local working space for packet->PCM decode */
123 vorbis_block vblock
;
125 /* Encoding quality in range [0.0, 1.0]. */
130 vorbis_read_header (SF_PRIVATE
*psf
, int log_data
)
132 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
133 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
140 /* Weird stuff happens if these aren't called. */
141 ogg_stream_reset (&odata
->ostream
) ;
142 ogg_sync_reset (&odata
->osync
) ;
145 ** Grab some data at the head of the stream. We want the first page
146 ** (which is guaranteed to be small and only contain the Vorbis
147 ** stream initial header) We need the first page to get the stream
151 /* Expose the buffer */
152 buffer
= ogg_sync_buffer (&odata
->osync
, 4096L) ;
154 /* Grab the part of the header that has already been read. */
155 memcpy (buffer
, psf
->header
, psf
->headindex
) ;
156 bytes
= psf
->headindex
;
158 /* Submit a 4k block to libvorbis' Ogg layer */
159 bytes
+= psf_fread (buffer
+ psf
->headindex
, 1, 4096 - psf
->headindex
, psf
) ;
160 ogg_sync_wrote (&odata
->osync
, bytes
) ;
162 /* Get the first page. */
163 if ((nn
= ogg_sync_pageout (&odata
->osync
, &odata
->opage
)) != 1)
165 /* Have we simply run out of data? If so, we're done. */
169 /* Error case. Must not be Vorbis data */
170 psf_log_printf (psf
, "Input does not appear to be an Ogg bitstream.\n") ;
171 return SFE_MALFORMED_FILE
;
175 ** Get the serial number and set up the rest of decode.
176 ** Serialno first ; use it to set up a logical stream.
178 ogg_stream_clear (&odata
->ostream
) ;
179 ogg_stream_init (&odata
->ostream
, ogg_page_serialno (&odata
->opage
)) ;
181 if (ogg_stream_pagein (&odata
->ostream
, &odata
->opage
) < 0)
182 { /* Error ; stream version mismatch perhaps. */
183 psf_log_printf (psf
, "Error reading first page of Ogg bitstream data\n") ;
184 return SFE_MALFORMED_FILE
;
187 if (ogg_stream_packetout (&odata
->ostream
, &odata
->opacket
) != 1)
188 { /* No page? must not be vorbis. */
189 psf_log_printf (psf
, "Error reading initial header packet.\n") ;
190 return SFE_MALFORMED_FILE
;
194 ** This function (vorbis_read_header) gets called multiple times, so the OGG
195 ** and vorbis structs have to be cleared every time we pass through to
196 ** prevent memory leaks.
198 vorbis_block_clear (&vdata
->vblock
) ;
199 vorbis_dsp_clear (&vdata
->vdsp
) ;
200 vorbis_comment_clear (&vdata
->vcomment
) ;
201 vorbis_info_clear (&vdata
->vinfo
) ;
204 ** Extract the initial header from the first page and verify that the
205 ** Ogg bitstream is in fact Vorbis data.
207 ** I handle the initial header first instead of just having the code
208 ** read all three Vorbis headers at once because reading the initial
209 ** header is an easy way to identify a Vorbis bitstream and it's
210 ** useful to see that functionality seperated out.
212 vorbis_info_init (&vdata
->vinfo
) ;
213 vorbis_comment_init (&vdata
->vcomment
) ;
215 if (vorbis_synthesis_headerin (&vdata
->vinfo
, &vdata
->vcomment
, &odata
->opacket
) < 0)
216 { /* Error case ; not a vorbis header. */
217 psf_log_printf (psf
, "Found Vorbis in stream header, but vorbis_synthesis_headerin failed.\n") ;
218 return SFE_MALFORMED_FILE
;
222 ** Common Ogg metadata fields?
223 ** TITLE, VERSION, ALBUM, TRACKNUMBER, ARTIST, PERFORMER, COPYRIGHT, LICENSE,
224 ** ORGANIZATION, DESCRIPTION, GENRE, DATE, LOCATION, CONTACT, ISRC,
230 for (k
= 0 ; k
< ARRAY_LEN (vorbis_metatypes
) ; k
++)
233 dd
= vorbis_comment_query (&vdata
->vcomment
, vorbis_metatypes
[k
].name
, 0) ;
236 psf_store_string (psf
, vorbis_metatypes
[k
].id
, dd
) ;
241 ** At this point, we're sure we're Vorbis. We've set up the logical (Ogg)
242 ** bitstream decoder. Get the comment and codebook headers and set up the
245 ** The next two packets in order are the comment and codebook headers.
246 ** They're likely large and may span multiple pages. Thus we reead
247 ** and submit data until we get our two pacakets, watching that no
248 ** pages are missing. If a page is missing, error out ; losing a
249 ** header page is the only place where missing data is fatal.
252 i
= 0 ; /* Count of number of packets read */
254 { int result
= ogg_sync_pageout (&odata
->osync
, &odata
->opage
) ;
256 { /* Need more data */
257 buffer
= ogg_sync_buffer (&odata
->osync
, 4096) ;
258 bytes
= psf_fread (buffer
, 1, 4096, psf
) ;
260 if (bytes
== 0 && i
< 2)
261 { psf_log_printf (psf
, "End of file before finding all Vorbis headers!\n") ;
262 return SFE_MALFORMED_FILE
;
264 nn
= ogg_sync_wrote (&odata
->osync
, bytes
) ;
266 else if (result
== 1)
268 ** Don't complain about missing or corrupt data yet. We'll
269 ** catch it at the packet output phase.
271 ** We can ignore any errors here as they'll also become apparent
274 nn
= ogg_stream_pagein (&odata
->ostream
, &odata
->opage
) ;
276 { result
= ogg_stream_packetout (&odata
->ostream
, &odata
->opacket
) ;
280 { /* Uh oh ; data at some point was corrupted or missing!
281 ** We can't tolerate that in a header. Die. */
282 psf_log_printf (psf
, "Corrupt secondary header. Exiting.\n") ;
283 return SFE_MALFORMED_FILE
;
286 vorbis_synthesis_headerin (&vdata
->vinfo
, &vdata
->vcomment
, &odata
->opacket
) ;
293 { int printed_metadata_msg
= 0 ;
296 psf_log_printf (psf
, "Bitstream is %d channel, %D Hz\n", vdata
->vinfo
.channels
, vdata
->vinfo
.rate
) ;
297 psf_log_printf (psf
, "Encoded by : %s\n", vdata
->vcomment
.vendor
) ;
299 /* Throw the comments plus a few lines about the bitstream we're decoding. */
300 for (k
= 0 ; k
< ARRAY_LEN (vorbis_metatypes
) ; k
++)
303 dd
= vorbis_comment_query (&vdata
->vcomment
, vorbis_metatypes
[k
].name
, 0) ;
307 if (printed_metadata_msg
== 0)
308 { psf_log_printf (psf
, "Metadata :\n") ;
309 printed_metadata_msg
= 1 ;
312 psf_store_string (psf
, vorbis_metatypes
[k
].id
, dd
) ;
313 psf_log_printf (psf
, " %-10s : %s\n", vorbis_metatypes
[k
].name
, dd
) ;
316 psf_log_printf (psf
, "End\n") ;
319 psf
->sf
.samplerate
= vdata
->vinfo
.rate
;
320 psf
->sf
.channels
= vdata
->vinfo
.channels
;
321 psf
->sf
.format
= SF_FORMAT_OGG
| SF_FORMAT_VORBIS
;
323 /* OK, got and parsed all three headers. Initialize the Vorbis
324 ** packet->PCM decoder.
325 ** Central decode state. */
326 vorbis_synthesis_init (&vdata
->vdsp
, &vdata
->vinfo
) ;
328 /* Local state for most of the decode so multiple block decodes can
329 ** proceed in parallel. We could init multiple vorbis_block structures
331 vorbis_block_init (&vdata
->vdsp
, &vdata
->vblock
) ;
336 } /* vorbis_read_header */
339 vorbis_write_header (SF_PRIVATE
*psf
, int UNUSED (calc_length
))
341 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
342 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
345 vorbis_info_init (&vdata
->vinfo
) ;
347 /* The style of encoding should be selectable here, VBR quality mode. */
348 ret
= vorbis_encode_init_vbr (&vdata
->vinfo
, psf
->sf
.channels
, psf
->sf
.samplerate
, vdata
->quality
) ;
351 ret
= vorbis_encode_init (&vdata
->vinfo
, psf
->sf
.channels
, psf
->sf
.samplerate
, -1, 128000, -1) ; /* average bitrate mode */
352 ret
= ( vorbis_encode_setup_managed (&vdata
->vinfo
, psf
->sf
.channels
,
353 psf
->sf
.samplerate
, -1, 128000, -1) ||
354 vorbis_encode_ctl (&vdata
->vinfo
, OV_ECTL_RATEMANAGE_AVG
, NULL
) ||
355 vorbis_encode_setup_init (&vdata
->vinfo
)) ;
358 return SFE_BAD_OPEN_FORMAT
;
363 vorbis_comment_init (&vdata
->vcomment
) ;
365 vorbis_comment_add_tag (&vdata
->vcomment
, "ENCODER", "libsndfile") ;
366 for (k
= 0 ; k
< SF_MAX_STRINGS
; k
++)
367 { const char * name
;
369 if (psf
->strings
[k
].type
== 0)
372 switch (psf
->strings
[k
].type
)
373 { case SF_STR_TITLE
: name
= "TITLE" ; break ;
374 case SF_STR_COPYRIGHT
: name
= "COPYRIGHT" ; break ;
375 case SF_STR_SOFTWARE
: name
= "SOFTWARE" ; break ;
376 case SF_STR_ARTIST
: name
= "ARTIST" ; break ;
377 case SF_STR_COMMENT
: name
= "COMMENT" ; break ;
378 case SF_STR_DATE
: name
= "DATE" ; break ;
379 case SF_STR_ALBUM
: name
= "ALBUM" ; break ;
380 case SF_STR_LICENSE
: name
= "LICENSE" ; break ;
384 vorbis_comment_add_tag (&vdata
->vcomment
, name
, psf
->strings
[k
].str
) ;
387 /* set up the analysis state and auxiliary encoding storage */
388 vorbis_analysis_init (&vdata
->vdsp
, &vdata
->vinfo
) ;
389 vorbis_block_init (&vdata
->vdsp
, &vdata
->vblock
) ;
392 ** Set up our packet->stream encoder.
393 ** Pick a random serial number ; that way we can more likely build
394 ** chained streams just by concatenation.
397 ogg_stream_init (&odata
->ostream
, psf_rand_int32 ()) ;
399 /* Vorbis streams begin with three headers ; the initial header (with
400 most of the codec setup parameters) which is mandated by the Ogg
401 bitstream spec. The second header holds any comment fields. The
402 third header holds the bitstream codebook. We merely need to
403 make the headers, then pass them to libvorbis one at a time ;
404 libvorbis handles the additional Ogg bitstream constraints */
406 { ogg_packet header
;
407 ogg_packet header_comm
;
408 ogg_packet header_code
;
411 vorbis_analysis_headerout (&vdata
->vdsp
, &vdata
->vcomment
, &header
, &header_comm
, &header_code
) ;
412 ogg_stream_packetin (&odata
->ostream
, &header
) ; /* automatically placed in its own page */
413 ogg_stream_packetin (&odata
->ostream
, &header_comm
) ;
414 ogg_stream_packetin (&odata
->ostream
, &header_code
) ;
416 /* This ensures the actual
417 * audio data will start on a new page, as per spec
419 while ((result
= ogg_stream_flush (&odata
->ostream
, &odata
->opage
)) != 0)
420 { psf_fwrite (odata
->opage
.header
, 1, odata
->opage
.header_len
, psf
) ;
421 psf_fwrite (odata
->opage
.body
, 1, odata
->opage
.body_len
, psf
) ;
426 } /* vorbis_write_header */
429 vorbis_close (SF_PRIVATE
*psf
)
430 { OGG_PRIVATE
* odata
= psf
->container_data
;
431 VORBIS_PRIVATE
*vdata
= psf
->codec_data
;
433 if (odata
== NULL
|| vdata
== NULL
)
436 /* Clean up this logical bitstream ; before exit we shuld see if we're
437 ** followed by another [chained]. */
439 if (psf
->file
.mode
== SFM_WRITE
)
441 if (psf
->write_current
<= 0)
442 vorbis_write_header (psf
, 0) ;
444 vorbis_analysis_wrote (&vdata
->vdsp
, 0) ;
445 while (vorbis_analysis_blockout (&vdata
->vdsp
, &vdata
->vblock
) == 1)
448 /* analysis, assume we want to use bitrate management */
449 vorbis_analysis (&vdata
->vblock
, NULL
) ;
450 vorbis_bitrate_addblock (&vdata
->vblock
) ;
452 while (vorbis_bitrate_flushpacket (&vdata
->vdsp
, &odata
->opacket
))
453 { /* weld the packet into the bitstream */
454 ogg_stream_packetin (&odata
->ostream
, &odata
->opacket
) ;
456 /* write out pages (if any) */
458 { int result
= ogg_stream_pageout (&odata
->ostream
, &odata
->opage
) ;
459 if (result
== 0) break ;
460 psf_fwrite (odata
->opage
.header
, 1, odata
->opage
.header_len
, psf
) ;
461 psf_fwrite (odata
->opage
.body
, 1, odata
->opage
.body_len
, psf
) ;
463 /* this could be set above, but for illustrative purposes, I do
464 it here (to show that vorbis does know where the stream ends) */
466 if (ogg_page_eos (&odata
->opage
)) odata
->eos
= 1 ;
472 /* ogg_page and ogg_packet structs always point to storage in
473 libvorbis. They are never freed or manipulated directly */
475 vorbis_block_clear (&vdata
->vblock
) ;
476 vorbis_dsp_clear (&vdata
->vdsp
) ;
477 vorbis_comment_clear (&vdata
->vcomment
) ;
478 vorbis_info_clear (&vdata
->vinfo
) ;
484 ogg_vorbis_open (SF_PRIVATE
*psf
)
485 { OGG_PRIVATE
* odata
= psf
->container_data
;
486 VORBIS_PRIVATE
* vdata
= calloc (1, sizeof (VORBIS_PRIVATE
)) ;
490 { psf_log_printf (psf
, "%s : odata is NULL???\n", __func__
) ;
491 return SFE_INTERNAL
;
494 psf
->codec_data
= vdata
;
496 if (psf
->file
.mode
== SFM_RDWR
)
497 return SFE_BAD_MODE_RW
;
499 psf_log_printf (psf
, "Vorbis library version : %s\n", vorbis_version_string ()) ;
501 if (psf
->file
.mode
== SFM_READ
)
502 { /* Call this here so it only gets called once, so no memory is leaked. */
503 ogg_sync_init (&odata
->osync
) ;
505 if ((error
= vorbis_read_header (psf
, 1)))
508 psf
->read_short
= vorbis_read_s
;
509 psf
->read_int
= vorbis_read_i
;
510 psf
->read_float
= vorbis_read_f
;
511 psf
->read_double
= vorbis_read_d
;
512 psf
->sf
.frames
= vorbis_length (psf
) ;
515 psf
->codec_close
= vorbis_close
;
516 if (psf
->file
.mode
== SFM_WRITE
)
518 /* Set the default vorbis quality here. */
519 vdata
->quality
= 0.4 ;
521 psf
->write_header
= vorbis_write_header
;
522 psf
->write_short
= vorbis_write_s
;
523 psf
->write_int
= vorbis_write_i
;
524 psf
->write_float
= vorbis_write_f
;
525 psf
->write_double
= vorbis_write_d
;
527 psf
->sf
.frames
= SF_COUNT_MAX
; /* Unknown really */
528 psf
->str_flags
= SF_STR_ALLOW_START
;
532 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
534 psf
->seek
= vorbis_seek
;
535 psf
->command
= vorbis_command
;
537 /* FIXME, FIXME, FIXME : Hack these here for now and correct later. */
538 psf
->sf
.format
= SF_FORMAT_OGG
| SF_FORMAT_VORBIS
;
539 psf
->sf
.sections
= 1 ;
541 psf
->datalength
= 1 ;
542 psf
->dataoffset
= 0 ;
546 } /* ogg_vorbis_open */
549 vorbis_command (SF_PRIVATE
*psf
, int command
, void * data
, int datasize
)
550 { VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
553 { case SFC_SET_VBR_ENCODING_QUALITY
:
554 if (data
== NULL
|| datasize
!= sizeof (double))
557 if (psf
->have_written
)
560 vdata
->quality
= *((double *) data
) ;
563 vdata
->quality
= SF_MAX (0.0, SF_MIN (1.0, vdata
->quality
)) ;
565 psf_log_printf (psf
, "%s : Setting SFC_SET_VBR_ENCODING_QUALITY to %f.\n", __func__
, vdata
->quality
) ;
573 } /* vorbis_command */
576 vorbis_rnull (int samples
, void *UNUSED (vptr
), int UNUSED (off
) , int channels
, float **UNUSED (pcm
))
578 return samples
* channels
;
582 vorbis_rshort (int samples
, void *vptr
, int off
, int channels
, float **pcm
)
584 short *ptr
= (short*) vptr
+ off
;
586 for (j
= 0 ; j
< samples
; j
++)
587 for (n
= 0 ; n
< channels
; n
++)
588 ptr
[i
++] = lrintf (pcm
[n
][j
] * 32767.0f
) ;
590 } /* vorbis_rshort */
593 vorbis_rint (int samples
, void *vptr
, int off
, int channels
, float **pcm
)
595 int *ptr
= (int*) vptr
+ off
;
598 for (j
= 0 ; j
< samples
; j
++)
599 for (n
= 0 ; n
< channels
; n
++)
600 ptr
[i
++] = lrintf (pcm
[n
][j
] * 2147483647.0f
) ;
605 vorbis_rfloat (int samples
, void *vptr
, int off
, int channels
, float **pcm
)
607 float *ptr
= (float*) vptr
+ off
;
609 for (j
= 0 ; j
< samples
; j
++)
610 for (n
= 0 ; n
< channels
; n
++)
611 ptr
[i
++] = pcm
[n
][j
] ;
613 } /* vorbis_rfloat */
616 vorbis_rdouble (int samples
, void *vptr
, int off
, int channels
, float **pcm
)
618 double *ptr
= (double*) vptr
+ off
;
620 for (j
= 0 ; j
< samples
; j
++)
621 for (n
= 0 ; n
< channels
; n
++)
622 ptr
[i
++] = pcm
[n
][j
] ;
624 } /* vorbis_rdouble */
628 vorbis_read_sample (SF_PRIVATE
*psf
, void *ptr
, sf_count_t lens
, convert_func
*transfn
)
630 VORBIS_PRIVATE
*vdata
= psf
->codec_data
;
631 OGG_PRIVATE
*odata
= psf
->container_data
;
632 int len
, samples
, i
= 0 ;
635 len
= lens
/ psf
->sf
.channels
;
637 while ((samples
= vorbis_synthesis_pcmout (&vdata
->vdsp
, &pcm
)) > 0)
638 { if (samples
> len
) samples
= len
;
639 i
+= transfn (samples
, ptr
, i
, psf
->sf
.channels
, pcm
) ;
641 /* tell libvorbis how many samples we actually consumed */
642 vorbis_synthesis_read (&vdata
->vdsp
, samples
) ;
643 vdata
->loc
+= samples
;
645 return i
; /* Is this necessary */
647 goto start0
; /* Jump into the nasty nest */
648 while (len
> 0 && !odata
->eos
)
650 while (len
> 0 && !odata
->eos
)
651 { int result
= ogg_sync_pageout (&odata
->osync
, &odata
->opage
) ;
652 if (result
== 0) break ; /* need more data */
654 { /* missing or corrupt data at this page position */
655 psf_log_printf (psf
, "Corrupt or missing data in bitstream ; continuing...\n") ;
658 { /* can safely ignore errors at this point */
659 ogg_stream_pagein (&odata
->ostream
, &odata
->opage
) ;
662 { result
= ogg_stream_packetout (&odata
->ostream
, &odata
->opacket
) ;
664 break ; /* need more data */
666 { /* missing or corrupt data at this page position */
667 /* no reason to complain ; already complained above */
670 { /* we have a packet. Decode it */
671 if (vorbis_synthesis (&vdata
->vblock
, &odata
->opacket
) == 0) /* test for success! */
672 vorbis_synthesis_blockin (&vdata
->vdsp
, &vdata
->vblock
) ;
674 **pcm is a multichannel float vector. In stereo, for
675 example, pcm [0] is left, and pcm [1] is right. samples is
676 the size of each channel. Convert the float values
677 (-1.<=range<=1.) to whatever PCM format and write it out */
679 while ((samples
= vorbis_synthesis_pcmout (&vdata
->vdsp
, &pcm
)) > 0)
680 { if (samples
>len
) samples
= len
;
681 i
+= transfn (samples
, ptr
, i
, psf
->sf
.channels
, pcm
) ;
683 /* tell libvorbis how many samples we actually consumed */
684 vorbis_synthesis_read (&vdata
->vdsp
, samples
) ;
685 vdata
->loc
+= samples
;
687 return i
; /* Is this necessary */
691 if (ogg_page_eos (&odata
->opage
)) odata
->eos
= 1 ;
697 buffer
= ogg_sync_buffer (&odata
->osync
, 4096) ;
698 bytes
= psf_fread (buffer
, 1, 4096, psf
) ;
699 ogg_sync_wrote (&odata
->osync
, bytes
) ;
700 if (bytes
== 0) odata
->eos
= 1 ;
704 } /* vorbis_read_sample */
707 vorbis_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t lens
)
708 { return vorbis_read_sample (psf
, (void*) ptr
, lens
, vorbis_rshort
) ;
709 } /* vorbis_read_s */
712 vorbis_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t lens
)
713 { return vorbis_read_sample (psf
, (void*) ptr
, lens
, vorbis_rint
) ;
714 } /* vorbis_read_i */
717 vorbis_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t lens
)
718 { return vorbis_read_sample (psf
, (void*) ptr
, lens
, vorbis_rfloat
) ;
719 } /* vorbis_read_f */
722 vorbis_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t lens
)
723 { return vorbis_read_sample (psf
, (void*) ptr
, lens
, vorbis_rdouble
) ;
724 } /* vorbis_read_d */
726 /*==============================================================================
730 vorbis_write_samples (SF_PRIVATE
*psf
, OGG_PRIVATE
*odata
, VORBIS_PRIVATE
*vdata
, int in_frames
)
732 vorbis_analysis_wrote (&vdata
->vdsp
, in_frames
) ;
735 ** Vorbis does some data preanalysis, then divvies up blocks for
736 ** more involved (potentially parallel) processing. Get a single
737 ** block for encoding now.
739 while (vorbis_analysis_blockout (&vdata
->vdsp
, &vdata
->vblock
) == 1)
741 /* analysis, assume we want to use bitrate management */
742 vorbis_analysis (&vdata
->vblock
, NULL
) ;
743 vorbis_bitrate_addblock (&vdata
->vblock
) ;
745 while (vorbis_bitrate_flushpacket (&vdata
->vdsp
, &odata
->opacket
))
747 /* weld the packet into the bitstream */
748 ogg_stream_packetin (&odata
->ostream
, &odata
->opacket
) ;
750 /* write out pages (if any) */
752 { int result
= ogg_stream_pageout (&odata
->ostream
, &odata
->opage
) ;
755 psf_fwrite (odata
->opage
.header
, 1, odata
->opage
.header_len
, psf
) ;
756 psf_fwrite (odata
->opage
.body
, 1, odata
->opage
.body_len
, psf
) ;
758 /* This could be set above, but for illustrative purposes, I do
759 ** it here (to show that vorbis does know where the stream ends) */
760 if (ogg_page_eos (&odata
->opage
))
766 vdata
->loc
+= in_frames
;
767 } /* vorbis_write_data */
771 vorbis_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t lens
)
774 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
775 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
776 int in_frames
= lens
/ psf
->sf
.channels
;
777 float **buffer
= vorbis_analysis_buffer (&vdata
->vdsp
, in_frames
) ;
778 for (i
= 0 ; i
< in_frames
; i
++)
779 for (m
= 0 ; m
< psf
->sf
.channels
; m
++)
780 buffer
[m
][i
] = (float) (ptr
[j
++]) / 32767.0f
;
782 vorbis_write_samples (psf
, odata
, vdata
, in_frames
) ;
785 } /* vorbis_write_s */
788 vorbis_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t lens
)
790 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
791 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
792 int in_frames
= lens
/ psf
->sf
.channels
;
793 float **buffer
= vorbis_analysis_buffer (&vdata
->vdsp
, in_frames
) ;
794 for (i
= 0 ; i
< in_frames
; i
++)
795 for (m
= 0 ; m
< psf
->sf
.channels
; m
++)
796 buffer
[m
][i
] = (float) (ptr
[j
++]) / 2147483647.0f
;
798 vorbis_write_samples (psf
, odata
, vdata
, in_frames
) ;
801 } /* vorbis_write_i */
804 vorbis_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t lens
)
806 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
807 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
808 int in_frames
= lens
/ psf
->sf
.channels
;
809 float **buffer
= vorbis_analysis_buffer (&vdata
->vdsp
, in_frames
) ;
810 for (i
= 0 ; i
< in_frames
; i
++)
811 for (m
= 0 ; m
< psf
->sf
.channels
; m
++)
812 buffer
[m
][i
] = ptr
[j
++] ;
814 vorbis_write_samples (psf
, odata
, vdata
, in_frames
) ;
817 } /* vorbis_write_f */
820 vorbis_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t lens
)
822 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
823 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
824 int in_frames
= lens
/ psf
->sf
.channels
;
825 float **buffer
= vorbis_analysis_buffer (&vdata
->vdsp
, in_frames
) ;
826 for (i
= 0 ; i
< in_frames
; i
++)
827 for (m
= 0 ; m
< psf
->sf
.channels
; m
++)
828 buffer
[m
][i
] = (float) ptr
[j
++] ;
830 vorbis_write_samples (psf
, odata
, vdata
, in_frames
) ;
833 } /* vorbis_write_d */
836 vorbis_seek (SF_PRIVATE
*psf
, int UNUSED (mode
), sf_count_t offset
)
838 OGG_PRIVATE
*odata
= (OGG_PRIVATE
*) psf
->container_data
;
839 VORBIS_PRIVATE
*vdata
= (VORBIS_PRIVATE
*) psf
->codec_data
;
841 if (odata
== NULL
|| vdata
== NULL
)
845 { psf
->error
= SFE_BAD_SEEK
;
846 return ((sf_count_t
) -1) ;
849 if (psf
->file
.mode
== SFM_READ
)
850 { sf_count_t target
= offset
- vdata
->loc
;
853 { /* 12 to allow for OggS bit */
854 psf_fseek (psf
, 12, SEEK_SET
) ;
855 vorbis_read_header (psf
, 0) ; /* Reset state */
860 { sf_count_t m
= target
> 4096 ? 4096 : target
;
863 ** Need to multiply by channels here because the seek is done in
864 ** terms of frames and the read function is done in terms of
867 vorbis_read_sample (psf
, (void *) NULL
, m
* psf
->sf
.channels
, vorbis_rnull
) ;
878 /*==============================================================================
879 ** Most of the following code was snipped from Mike Smith's ogginfo utility
880 ** which is part of vorbis-tools.
881 ** Vorbis tools is released under the GPL but Mike has kindly allowed the
882 ** following to be relicensed as LGPL for libsndfile.
892 uint32_t serial
; /* must be 32 bit unsigned */
893 ogg_stream_state ostream
;
896 vorbis_comment vcomment
;
897 sf_count_t lastgranulepos
;
903 stream_processor
*streams
;
910 create_stream_set (void)
911 { stream_set
*set
= calloc (1, sizeof (stream_set
)) ;
913 set
->streams
= calloc (5, sizeof (stream_processor
)) ;
918 } /* create_stream_set */
921 vorbis_end (stream_processor
*stream
, sf_count_t
* len
)
922 { *len
+= stream
->lastgranulepos
;
923 vorbis_comment_clear (&stream
->vcomment
) ;
924 vorbis_info_clear (&stream
->vinfo
) ;
928 free_stream_set (stream_set
*set
, sf_count_t
* len
)
931 for (i
= 0 ; i
< set
->used
; i
++)
932 { if (!set
->streams
[i
].end
)
933 vorbis_end (&set
->streams
[i
], len
) ;
934 ogg_stream_clear (&set
->streams
[i
].ostream
) ;
937 free (set
->streams
) ;
939 } /* free_stream_set */
942 streams_open (stream_set
*set
)
945 for (i
= 0 ; i
< set
->used
; i
++)
946 if (!set
->streams
[i
].end
)
951 static stream_processor
*
952 find_stream_processor (stream_set
*set
, ogg_page
*page
)
953 { uint32_t serial
= ogg_page_serialno (page
) ;
956 stream_processor
*stream
;
958 for (i
= 0 ; i
< set
->used
; i
++)
960 if (serial
== set
->streams
[i
].serial
)
961 { /* We have a match! */
962 stream
= & (set
->streams
[i
]) ;
964 set
->in_headers
= 0 ;
965 /* if we have detected EOS, then this can't occur here. */
967 { stream
->isillegal
= 1 ;
972 stream
->end
= ogg_page_eos (page
) ;
973 stream
->serial
= serial
;
978 /* If there are streams open, and we've reached the end of the
979 ** headers, then we can't be starting a new stream.
980 ** XXX: might this sometimes catch ok streams if EOS flag is missing,
981 ** but the stream is otherwise ok?
983 if (streams_open (set
) && !set
->in_headers
)
986 set
->in_headers
= 1 ;
988 if (set
->allocated
< set
->used
)
989 stream
= &set
->streams
[set
->used
] ;
991 { set
->allocated
+= 5 ;
992 set
->streams
= realloc (set
->streams
, sizeof (stream_processor
) * set
->allocated
) ;
993 stream
= &set
->streams
[set
->used
] ;
999 stream
->isillegal
= invalid
;
1005 /* We end up processing the header page twice, but that's ok. */
1006 ogg_stream_init (&stream
->ostream
, serial
) ;
1007 ogg_stream_pagein (&stream
->ostream
, page
) ;
1008 res
= ogg_stream_packetout (&stream
->ostream
, &packet
) ;
1011 else if (packet
.bytes
>= 7 && memcmp (packet
.packet
, "\x01vorbis", 7) == 0)
1013 stream
->lastgranulepos
= 0 ;
1014 vorbis_comment_init (&stream
->vcomment
) ;
1015 vorbis_info_init (&stream
->vinfo
) ;
1018 res
= ogg_stream_packetout (&stream
->ostream
, &packet
) ;
1020 /* re-init, ready for processing */
1021 ogg_stream_clear (&stream
->ostream
) ;
1022 ogg_stream_init (&stream
->ostream
, serial
) ;
1025 stream
->end
= ogg_page_eos (page
) ;
1026 stream
->serial
= serial
;
1029 } /* find_stream_processor */
1032 vorbis_length_get_next_page (SF_PRIVATE
*psf
, ogg_sync_state
* osync
, ogg_page
*page
)
1033 { static const int CHUNK_SIZE
= 4500 ;
1035 while (ogg_sync_pageout (osync
, page
) <= 0)
1036 { char * buffer
= ogg_sync_buffer (osync
, CHUNK_SIZE
) ;
1037 int bytes
= psf_fread (buffer
, 1, 4096, psf
) ;
1040 { ogg_sync_wrote (osync
, 0) ;
1044 ogg_sync_wrote (osync
, bytes
) ;
1048 } /* vorbis_length_get_next_page */
1051 vorbis_length_aux (SF_PRIVATE
* psf
)
1053 ogg_sync_state osync
;
1055 sf_count_t len
= 0 ;
1056 stream_set
*processors
;
1058 processors
= create_stream_set () ;
1059 if (processors
== NULL
)
1060 return 0 ; // out of memory?
1062 ogg_sync_init (&osync
) ;
1064 while (vorbis_length_get_next_page (psf
, &osync
, &page
))
1066 stream_processor
*p
= find_stream_processor (processors
, &page
) ;
1073 if (p
->isillegal
&& !p
->shownillegal
)
1075 p
->shownillegal
= 1 ;
1076 /* If it's a new stream, we want to continue processing this page
1077 ** anyway to suppress additional spurious errors
1079 if (!p
->isnew
) continue ;
1083 { ogg_packet packet
;
1086 ogg_stream_pagein (&p
->ostream
, &page
) ;
1087 if (p
->doneheaders
< 3)
1090 while (ogg_stream_packetout (&p
->ostream
, &packet
) > 0)
1092 if (p
->doneheaders
< 3)
1093 { if (vorbis_synthesis_headerin (&p
->vinfo
, &p
->vcomment
, &packet
) < 0)
1099 { sf_count_t gp
= ogg_page_granulepos (&page
) ;
1100 if (gp
> 0) p
->lastgranulepos
= gp
;
1103 { vorbis_end (p
, &len
) ;
1109 ogg_sync_clear (&osync
) ;
1110 free_stream_set (processors
, &len
) ;
1113 } /* vorbis_length_aux */
1116 vorbis_length (SF_PRIVATE
*psf
)
1117 { sf_count_t length
;
1120 if (psf
->sf
.seekable
== 0)
1121 return SF_COUNT_MAX
;
1123 psf_fseek (psf
, 0, SEEK_SET
) ;
1124 length
= vorbis_length_aux (psf
) ;
1126 psf_fseek (psf
, 12, SEEK_SET
) ;
1127 if ((error
= vorbis_read_header (psf
, 0)) != 0)
1128 psf
->error
= error
;
1131 } /* vorbis_length */
1133 #else /* HAVE_EXTERNAL_LIBS */
1136 ogg_vorbis_open (SF_PRIVATE
*psf
)
1138 psf_log_printf (psf
, "This version of libsndfile was compiled without Ogg/Vorbis support.\n") ;
1139 return SFE_UNIMPLEMENTED
;
1140 } /* ogg_vorbis_open */