• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2019 - 2021 Arthur Taylor <art@ified.ca>
3 ** Copyright (C) 2019 Erik de Castro Lopo <erikd@mega-nerd.com>
4 **
5 ** This program is free software ; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation ; either version 2.1 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU Lesser General Public License for more details.
14 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program ; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 
20 #include	"sfconfig.h"
21 
22 #include	<math.h>
23 
24 #include	"sndfile.h"
25 #include	"common.h"
26 #include	"mpeg.h"
27 
28 #if HAVE_MPEG
29 
30 #include	"sfendian.h"
31 #include	"id3.h"
32 
33 #include <mpg123.h>
34 
35 typedef struct
36 {	mpg123_handle *pmh ;
37 	size_t header_remaining ;
38 } MPEG_DEC_PRIVATE ;
39 
40 static int mpeg_dec_close (SF_PRIVATE *psf) ;
41 static sf_count_t mpeg_dec_seek (SF_PRIVATE *psf, int whence, sf_count_t count) ;
42 
43 static ssize_t mpeg_dec_io_read (void *priv, void *buffer, size_t nbytes) ;
44 static off_t mpeg_dec_io_lseek (void *priv, off_t offset, int whence) ;
45 
46 static ssize_t
mpeg_dec_io_read(void * priv,void * buffer,size_t nbytes)47 mpeg_dec_io_read (void *priv, void *buffer, size_t nbytes)
48 {	SF_PRIVATE *psf = (SF_PRIVATE *) priv ;
49 	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
50 
51 	if (pmp3d->header_remaining)
52 	{	if (pmp3d->header_remaining < nbytes)
53 			nbytes = pmp3d->header_remaining ;
54 		psf_binheader_readf (psf, "b", buffer, nbytes) ;
55 		pmp3d->header_remaining -= nbytes ;
56 		return nbytes ;
57 		} ;
58 
59 	return psf_fread (buffer, 1, nbytes, psf) ;
60 } /* mpeg_dec_io_read */
61 
62 static off_t
mpeg_dec_io_lseek(void * priv,off_t offset,int whence)63 mpeg_dec_io_lseek (void *priv, off_t offset, int whence)
64 {	SF_PRIVATE *psf = (SF_PRIVATE *) priv ;
65 
66 	return psf_fseek (psf, offset, whence) ;
67 } /* mpeg_dec_io_lseek */
68 
69 static int
mpeg_dec_close(SF_PRIVATE * psf)70 mpeg_dec_close (SF_PRIVATE *psf)
71 {	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
72 
73 	if (pmp3d)
74 	{	if (pmp3d->pmh)
75 		{	mpg123_close (pmp3d->pmh) ;
76 			mpg123_delete (pmp3d->pmh) ;
77 			pmp3d->pmh = NULL ;
78 			}
79 		free (psf->codec_data) ;
80 		psf->codec_data = NULL ;
81 		} ;
82 
83 	return 0 ;
84 } /* mpeg_dec_close */
85 
86 static sf_count_t
mpeg_dec_decode(SF_PRIVATE * psf,float * ptr,sf_count_t len)87 mpeg_dec_decode (SF_PRIVATE *psf, float *ptr, sf_count_t len)
88 {	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
89 	size_t done ;
90 	int error ;
91 
92 	error = mpg123_read (pmp3d->pmh, (unsigned char *) ptr, len * sizeof (float), &done) ;
93 
94 	if (error == MPG123_OK || error == MPG123_DONE)
95 		return done / sizeof (float) ;
96 
97 	if (error == MPG123_NEW_FORMAT)
98 	{	psf->error = SFE_MALFORMED_FILE ;
99 		return -1 ;
100 		} ;
101 
102 	psf->error = SFE_INTERNAL ;
103 	return -1 ;
104 } /* mpeg_dec_decode */
105 
106 static sf_count_t
mpeg_dec_read_s(SF_PRIVATE * psf,short * ptr,sf_count_t len)107 mpeg_dec_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
108 {	BUF_UNION ubuf ;
109 	sf_count_t total, readlen ;
110 	void (*convert) (const float *, short *, int, int) ;
111 	const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ;
112 
113 	convert = (psf->add_clipping) ? psf_f2s_clip_array : psf_f2s_array ;
114 	for (total = 0 ; total < len ; total += readlen)
115 	{	readlen = mpeg_dec_decode (psf, ubuf.fbuf, SF_MIN (buflen, len - total)) ;
116 		if (readlen <= 0)
117 			break ;
118 
119 		convert (ubuf.fbuf, ptr + total, readlen, SF_TRUE) ;
120 		} ;
121 
122 	return total ;
123 } /*mpeg_dec_read_s */
124 
125 static sf_count_t
mpeg_dec_read_i(SF_PRIVATE * psf,int * ptr,sf_count_t len)126 mpeg_dec_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
127 {	BUF_UNION ubuf ;
128 	sf_count_t total, readlen ;
129 	void (*convert) (const float *, int *, int, int) ;
130 	const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ;
131 
132 	convert = (psf->add_clipping) ? psf_f2i_clip_array : psf_f2i_array ;
133 	for (total = 0 ; total < len ; total += readlen)
134 	{	readlen = mpeg_dec_decode (psf, ubuf.fbuf, SF_MIN (buflen, len - total)) ;
135 		if (readlen <= 0)
136 			break ;
137 
138 		convert (ubuf.fbuf, ptr + total, readlen, SF_TRUE) ;
139 		} ;
140 
141 	return total ;
142 } /* mpeg_dec_read_i */
143 
144 static sf_count_t
mpeg_dec_read_f(SF_PRIVATE * psf,float * ptr,sf_count_t len)145 mpeg_dec_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
146 {	sf_count_t readlen ;
147 
148 	readlen = mpeg_dec_decode (psf, ptr, len) ;
149 	if (readlen <= 0)
150 		return 0 ;
151 
152 	if (psf->norm_float == SF_FALSE)
153 		for (int i = 0 ; i < readlen ; i++)
154 		{	ptr [i] *= (1.0f * 0x8000) ;
155 			} ;
156 
157 	return readlen ;
158 } /* mpeg_dec_read_f */
159 
160 static inline void
f2d_array(const float * src,int count,double * dest,double normfact)161 f2d_array (const float *src, int count, double *dest, double normfact)
162 {	for (int i = 0 ; i < count ; i++)
163 	{	dest [i] = src [i] * normfact ;
164 		}
165 } /* f2d_array */
166 
167 static sf_count_t
mpeg_dec_read_d(SF_PRIVATE * psf,double * ptr,sf_count_t len)168 mpeg_dec_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
169 {	BUF_UNION ubuf ;
170 	sf_count_t total, readlen ;
171 	double normfact ;
172 	const sf_count_t buflen = ARRAY_LEN (ubuf.fbuf) ;
173 
174 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 : (1.0 * 0x8000) ;
175 
176 	for (total = 0 ; total < len ; total += readlen)
177 	{	readlen = mpeg_dec_decode (psf, ubuf.fbuf, SF_MIN (buflen, len - total)) ;
178 		if (readlen <= 0)
179 			break ;
180 
181 		f2d_array (ubuf.fbuf, readlen, ptr + total, normfact) ;
182 		} ;
183 
184 	return total ;
185 } /* mpeg_dec_read_d */
186 
187 static sf_count_t
mpeg_dec_seek(SF_PRIVATE * psf,int mode,sf_count_t count)188 mpeg_dec_seek (SF_PRIVATE *psf, int mode, sf_count_t count)
189 {	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
190 	off_t ret ;
191 
192 	if (mode != SFM_READ || psf->file.mode != SFM_READ)
193 	{	psf->error = SFE_BAD_SEEK ;
194 		return PSF_SEEK_ERROR ;
195 		} ;
196 
197 	ret = mpg123_seek (pmp3d->pmh, count, SEEK_SET) ;
198 
199 	if (ret < 0)
200 		return PSF_SEEK_ERROR ;
201 
202 	return (sf_count_t) ret ;
203 } /* mpeg_dec_seek */
204 
205 static int
mpeg_dec_fill_sfinfo(mpg123_handle * mh,SF_INFO * info)206 mpeg_dec_fill_sfinfo (mpg123_handle *mh, SF_INFO *info)
207 {	int error ;
208 	int channels ;
209 	int encoding ;
210 	long rate ;
211 	off_t length ;
212 
213 	error = mpg123_getformat (mh, &rate, &channels, &encoding) ;
214 	if (error != MPG123_OK)
215 		return error ;
216 
217 	info->samplerate = rate ;
218 	info->channels = channels ;
219 
220 	length = mpg123_length (mh) ;
221 	if (length >= 0)
222 	{	info->frames = length ;
223 		info->seekable = SF_TRUE ;
224 		}
225 	else
226 	{	info->frames = SF_COUNT_MAX ;
227 		info->seekable = SF_FALSE ;
228 		}
229 
230 	/* Force 32-bit float samples. */
231 	if (encoding != MPG123_ENC_FLOAT_32)
232 	{	error = mpg123_format (mh, rate, channels, MPG123_ENC_FLOAT_32) ;
233 		} ;
234 
235 	return error ;
236 } /* mpeg_dec_fill_sfinfo */
237 
238 static void
mpeg_dec_print_frameinfo(SF_PRIVATE * psf,const struct mpg123_frameinfo * fi)239 mpeg_dec_print_frameinfo (SF_PRIVATE *psf, const struct mpg123_frameinfo *fi)
240 {	psf_log_printf (psf, "MPEG-1/2 Audio\n----------------------------------------\n") ;
241 	psf_log_printf (psf, "  MPEG version   : %s\n",
242 		fi->version == MPG123_1_0 ? "MPEG 1.0" :
243 		fi->version == MPG123_2_0 ? "MPEG 2.0" :
244 		fi->version == MPG123_2_5 ? "MPEG 2.5" : "???") ;
245 	psf_log_printf (psf, "  layer          : %d\n", fi->layer) ;
246 	psf_log_printf (psf, "  rate           : %d\n", fi->rate) ;
247 	psf_log_printf (psf, "  mode           : %s\n",
248 		fi->mode == MPG123_M_STEREO ? "stereo" :
249 		fi->mode == MPG123_M_JOINT ? "joint stereo" :
250 		fi->mode == MPG123_M_DUAL ? "dual channel" :
251 		fi->mode == MPG123_M_MONO ? "mono" : "???") ;
252 	psf_log_printf (psf, "  mode ext       : %d\n", fi->mode_ext) ;
253 	psf_log_printf (psf, "  framesize      : %d\n", fi->framesize) ;
254 	psf_log_printf (psf, "  crc            : %d\n", !! (fi->flags & MPG123_CRC)) ;
255 	psf_log_printf (psf, "  copyright flag : %d\n", !! (fi->flags & MPG123_COPYRIGHT)) ;
256 	psf_log_printf (psf, "  private flag   : %d\n", !! (fi->flags & MPG123_PRIVATE)) ;
257 	psf_log_printf (psf, "  original flag  : %d\n", !! (fi->flags & MPG123_ORIGINAL)) ;
258 	psf_log_printf (psf, "  emphasis       : %d\n", fi->emphasis) ;
259 	psf_log_printf (psf, "  bitrate mode   : ") ;
260 	switch (fi->vbr)
261 	{	case MPG123_CBR :
262 			psf_log_printf (psf, "constant\n") ;
263 			psf_log_printf (psf, "  bitrate        : %d kbps\n", fi->bitrate) ;
264 			break ;
265 		case MPG123_VBR :
266 			psf_log_printf (psf, "variable\n") ;
267 			break ;
268 
269 		case MPG123_ABR :
270 			psf_log_printf (psf, "average\n") ;
271 			psf_log_printf (psf, "  ABR target     : %d\n", fi->abr_rate) ;
272 			break ;
273 
274 		default :
275 			psf_log_printf (psf, "(%d) ???\n", fi->vbr) ;
276 			break ;
277 		} ;
278 } /* mpeg_dec_print_frameinfo */
279 
280 /*
281  * Like strlcpy, except the size argument is the maximum size of the input,
282  * always null terminates the output string. Thus, up to size + 1 bytes may be
283  * written.
284  *
285  * Returns the length of the copied string.
286  */
287 static int
strcpy_inbounded(char * dest,size_t size,const char * src)288 strcpy_inbounded (char *dest, size_t size, const char *src)
289 {	char *c = memccpy (dest, src, '\0', size) ;
290 	if (!c)
291 		c = dest + size ;
292 	*c = '\0' ;
293 	return c - dest ;
294 } /* strcpy_inbounded */
295 
296 static void
mpeg_decoder_read_strings_id3v1(SF_PRIVATE * psf,mpg123_id3v1 * tags)297 mpeg_decoder_read_strings_id3v1 (SF_PRIVATE *psf, mpg123_id3v1 *tags)
298 {	const char *genre ;
299 	char buf [31] ;
300 
301 	psf_log_printf (psf, "ID3v1 Tags\n") ;
302 
303 	if (strcpy_inbounded (buf, ARRAY_LEN (tags->title), tags->title))
304 	{	psf_log_printf (psf, "  Title       : %s\n", buf) ;
305 		psf_store_string (psf, SF_STR_TITLE, buf) ;
306 		} ;
307 
308 	if (strcpy_inbounded (buf, ARRAY_LEN (tags->artist), tags->artist))
309 	{	psf_log_printf (psf, "  Artist      : %s\n", buf) ;
310 		psf_store_string (psf, SF_STR_ARTIST, buf) ;
311 		} ;
312 
313 	if (strcpy_inbounded (buf, ARRAY_LEN (tags->album), tags->album))
314 	{	psf_log_printf (psf, "  Album       : %s\n", buf) ;
315 		psf_store_string (psf, SF_STR_ALBUM, buf) ;
316 		} ;
317 
318 	if (strcpy_inbounded (buf, ARRAY_LEN (tags->year), tags->year))
319 	{	psf_log_printf (psf, "  Year        : %s\n", buf) ;
320 		psf_store_string (psf, SF_STR_DATE, buf) ;
321 		} ;
322 
323 	if (strcpy_inbounded (buf, ARRAY_LEN (tags->comment), tags->comment))
324 	{	psf_log_printf (psf, "  Comment     : %s\n", buf) ;
325 		psf_store_string (psf, SF_STR_COMMENT, buf) ;
326 		} ;
327 
328 	/* ID3v1.1 Tracknumber */
329 	if (tags->comment [28] == '\0' && tags->comment [29] != '\0')
330 	{	snprintf (buf, ARRAY_LEN (buf), "%hhu", (unsigned char) tags->comment [29]) ;
331 		psf_log_printf (psf, "  Tracknumber : %s\n", buf) ;
332 		psf_store_string (psf, SF_STR_TRACKNUMBER, buf) ;
333 		} ;
334 
335 	if ((genre = id3_lookup_v1_genre (tags->genre)) != NULL)
336 	{	psf_log_printf (psf, "  Genre       : %s\n", genre) ;
337 		psf_store_string (psf, SF_STR_GENRE, genre) ;
338 		} ;
339 } /* mpeg_decoder_read_strings_id3v1 */
340 
341 static void
mpeg_decoder_read_strings_id3v2(SF_PRIVATE * psf,mpg123_id3v2 * tags)342 mpeg_decoder_read_strings_id3v2 (SF_PRIVATE *psf, mpg123_id3v2 *tags)
343 {	mpg123_text *text_frame ;
344 	size_t i ;
345 	uint32_t marker ;
346 	const char *title		= NULL ;
347 	const char *copyright	= NULL ;
348 	const char *software	= NULL ;
349 	const char *artist		= NULL ;
350 	const char *comment		= NULL ;
351 	const char *date		= NULL ;
352 	const char *album		= NULL ;
353 	const char *license		= NULL ;
354 	const char *tracknumber	= NULL ;
355 	const char *genre		= NULL ;
356 	const char *tlen		= NULL ;
357 
358 	psf_log_printf (psf, "ID3v2 Tags\n") ;
359 
360 	// Read the parsed text tags
361 	for (i = 0 ; i < tags->texts ; i++)
362 	{	text_frame = &tags->text [i] ;
363 		psf_log_printf (psf, "  %.4s        : %s\n", text_frame->id, text_frame->text.p) ;
364 
365 		// Thankfully mpg123 translates v2.2 3-byte frames to v2.3 4-byte for us.
366 		marker = MAKE_MARKER (text_frame->id [0], text_frame->id [1],
367 			text_frame->id [2], text_frame->id [3]) ;
368 
369 		/* Use our own map of frame types to metadata for text frames */
370 		switch (marker)
371 		{	case MAKE_MARKER ('T', 'I', 'T', '2') :
372 				title = text_frame->text.p ;
373 				break ;
374 
375 			case MAKE_MARKER ('T', 'C', 'O', 'P') :
376 				copyright = text_frame->text.p ;
377 				break ;
378 
379 			case MAKE_MARKER ('T', 'E', 'N', 'C') :
380 			case MAKE_MARKER ('T', 'S', 'S', 'E') :
381 				software = text_frame->text.p ;
382 				break ;
383 
384 			case MAKE_MARKER ('T', 'P', 'E', '1') :
385 				artist = text_frame->text.p ;
386 				break ;
387 
388 			case MAKE_MARKER ('T', 'A', 'L', 'B') :
389 				album = text_frame->text.p ;
390 				break ;
391 
392 			case MAKE_MARKER ('T', 'R', 'C', 'K') :
393 				tracknumber = text_frame->text.p ;
394 				break ;
395 
396 			case MAKE_MARKER ('T', 'Y', 'E', 'R') :
397 			case MAKE_MARKER ('T', 'D', 'R', 'C') :
398 			/* TODO (maybe)
399 			case MAKE_MARKER ('T', 'D', 'A', 'T') :
400 			case MAKE_MARKER ('T', 'I', 'M', 'E') :
401 			case MAKE_MARKER ('T', 'D', 'R', 'A') :
402 			*/
403 				date = text_frame->text.p ;
404 				break ;
405 
406 			case MAKE_MARKER ('T', 'O', 'W', 'N') :
407 				tracknumber = text_frame->text.p ;
408 				break ;
409 
410 			case MAKE_MARKER ('T', 'C', 'O', 'N') :
411 				genre = text_frame->text.p ;
412 				break ;
413 
414 			case MAKE_MARKER ('T', 'L', 'E', 'N') :
415 				tlen = text_frame->text.p ;
416 				break ;
417 			} ;
418 		} ;
419 
420 	/* Use mpg123's handling of comment headers, but print all the comment headers anyways. */
421 	if (tags->comment)
422 		comment = tags->comment->p ;
423 	for (i = 0 ; i < tags->comments ; i++)
424 	{	text_frame = &tags->comment_list [i] ;
425 		psf_log_printf (psf, "  %.4s        : (%s)[%s] %s\n", text_frame->id,
426 			text_frame->description. p, text_frame->lang, text_frame->text.p) ;
427 		} ;
428 
429 	/* Print extra headers */
430 	for (i = 0 ; i < tags->extras ; i++)
431 	{	text_frame = &tags->extra [i] ;
432 		psf_log_printf (psf, "  %.4s        : (%s) %s\n", text_frame->id,
433 			text_frame->description.p, text_frame->text.p) ;
434 		} ;
435 
436 	if (title != NULL)
437 		psf_store_string (psf, SF_STR_TITLE, title) ;
438 	if (copyright != NULL)
439 		psf_store_string (psf, SF_STR_COPYRIGHT, copyright) ;
440 	if (software != NULL)
441 		psf_store_string (psf, SF_STR_SOFTWARE, software) ;
442 	if (artist != NULL)
443 		psf_store_string (psf, SF_STR_ARTIST, artist) ;
444 	if (comment != NULL)
445 		psf_store_string (psf, SF_STR_COMMENT, comment) ;
446 	if (date != NULL)
447 		psf_store_string (psf, SF_STR_DATE, date) ;
448 	if (album != NULL)
449 		psf_store_string (psf, SF_STR_ALBUM, album) ;
450 	if (license != NULL)
451 		psf_store_string (psf, SF_STR_LICENSE, license) ;
452 	if (tracknumber != NULL)
453 		psf_store_string (psf, SF_STR_TRACKNUMBER, tracknumber) ;
454 	if (genre != NULL)
455 		psf_store_string (psf, SF_STR_GENRE, id3_process_v2_genre (genre)) ;
456 	if (tlen != NULL)
457 	{	/* If non-seekable, set framecount? Can we trust it? */
458 		} ;
459 } /* mpeg_decoder_read_strings_id3v2 */
460 
461 static void
mpeg_decoder_read_strings(SF_PRIVATE * psf)462 mpeg_decoder_read_strings (SF_PRIVATE *psf)
463 {	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
464 	mpg123_id3v1 *v1_tags ;
465 	mpg123_id3v2 *v2_tags ;
466 
467 	if (mpg123_id3 (pmp3d->pmh, &v1_tags, &v2_tags) != MPG123_OK)
468 		return ;
469 
470 	if (v1_tags != NULL)
471 		mpeg_decoder_read_strings_id3v1 (psf, v1_tags) ;
472 
473 	if (v2_tags != NULL)
474 		mpeg_decoder_read_strings_id3v2 (psf, v2_tags) ;
475 } /* mpeg_decoder_read_strings */
476 
477 static int
mpeg_dec_byterate(SF_PRIVATE * psf)478 mpeg_dec_byterate (SF_PRIVATE *psf)
479 {	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
480 	struct mpg123_frameinfo fi ;
481 
482 	if (mpg123_info (pmp3d->pmh, &fi) == MPG123_OK)
483 		return (fi.bitrate + 7) / 8 ;
484 
485 	return -1 ;
486 
487 } /* mpeg_dec_byterate */
488 
489 /*==============================================================================
490 ** exported functions
491 */
492 
493 int
mpeg_decoder_init(SF_PRIVATE * psf)494 mpeg_decoder_init (SF_PRIVATE *psf)
495 {	MPEG_DEC_PRIVATE *pmp3d ;
496 	struct mpg123_frameinfo fi ;
497 	int error ;
498 
499 	if (! (psf->file.mode & SFM_READ))
500 		return SFE_INTERNAL ;
501 
502 	/*
503 	** *** FIXME - Threading issues ***
504 	**
505 	** mpg123_init() is a global call that should only be called once, and
506 	** should be paried with mpg123_exit() when done. libsndfile does not
507 	** provide for these requirements.
508 	**
509 	** Currently this is a moot issue as mpg123_init() non-conditionally writes
510 	** static areas with calculated data, and mpg123_exit() is a NOP, but this
511 	** could change in a future version of it!
512 	**
513 	** From mpg123.h:
514 	** > This should be called once in a non-parallel context. It is not explicitly
515 	** > thread-safe, but repeated/concurrent calls still _should_ be safe as static
516 	** > tables are filled with the same values anyway.
517 	**
518 	** Note that calling mpg123_init() after it has already completed is a NOP.
519 	**
520 	** Update 2021-07-04
521 	** mpg123 upstream has confirmed that mpg132_init() will become a NOP in future,
522 	** so this is moot.
523 	*/
524 	if (mpg123_init () != MPG123_OK)
525 		return SFE_INTERNAL ;
526 
527 	psf->codec_data = pmp3d = calloc (1, sizeof (MPEG_DEC_PRIVATE)) ;
528 	if (!psf->codec_data)
529 		return SFE_MALLOC_FAILED ;
530 
531 	pmp3d->pmh = mpg123_new (NULL, &error) ;
532 	if (!pmp3d->pmh)
533 	{ psf_log_printf (psf, "Could not obtain a mpg123 handle: %s\n", mpg123_plain_strerror (error)) ;
534 		return SFE_INTERNAL ;
535 		} ;
536 
537 	psf->codec_close = mpeg_dec_close ;
538 
539 	mpg123_replace_reader_handle (pmp3d->pmh,
540 		mpeg_dec_io_read, mpeg_dec_io_lseek, NULL) ;
541 
542 	mpg123_param (pmp3d->pmh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 1.0) ;
543 	mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT | MPG123_GAPLESS, 1.0) ;
544 #if MPG123_API_VERSION >= 45
545 	mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_FRANKENSTEIN, 1.0) ;
546 #endif
547 
548 	/*
549 	** Need to pass the first MPEG frame to libmpg123, but that frame was read
550 	** into psf->binheader in order that we could identify the stream.
551 	*/
552 	if (psf->is_pipe)
553 	{	/*
554 		** Can't seek, so setup our libmpg123 io callbacks to read the binheader
555 		** buffer first.
556 		*/
557 		psf_binheader_readf (psf, "p", psf->dataoffset) ;
558 		pmp3d->header_remaining = psf_binheader_readf (psf, NULL) - psf->dataoffset ;
559 
560 		/* Tell libmpg123 we can't seek the file. */
561 		mpg123_param (pmp3d->pmh, MPG123_ADD_FLAGS, MPG123_NO_PEEK_END, 1.0) ;
562 		}
563 	else
564 	{	/*
565 		** libmpg123 can parse the ID3v2 header. Undo the embedded file offset if the
566 		** enclosing file data is the ID3v2 header.
567 		*/
568 		if (psf->id3_header.len > 0 && psf->id3_header.len + psf->id3_header.offset == psf->fileoffset)
569 			psf->fileoffset = psf->id3_header.offset ;
570 
571 		psf_fseek (psf, 0, SEEK_SET) ;
572 		} ;
573 
574 	error = mpg123_open_handle (pmp3d->pmh, psf) ;
575 	if (error != MPG123_OK)
576 	{	psf_log_printf (psf, "mpg123 could not open the file: %s\n", mpg123_plain_strerror (error)) ;
577 		return SFE_BAD_FILE ;
578 		} ;
579 
580 	if (mpeg_dec_fill_sfinfo (pmp3d->pmh, &psf->sf) != MPG123_OK)
581 	{	psf_log_printf (psf, "Cannot get MPEG decoder configuration: %s\n", mpg123_plain_strerror (error)) ;
582 		return SFE_BAD_FILE ;
583 		} ;
584 
585 	error = mpg123_info (pmp3d->pmh, &fi) ;
586 	if (error != MPG123_OK)
587 	{	psf_log_printf (psf, "Cannot get MPEG frame info: %s\n", mpg123_plain_strerror (error)) ;
588 		return SFE_INTERNAL ;
589 		}
590 
591 	switch (fi.layer)
592 	{	case 1 : psf->sf.format |= SF_FORMAT_MPEG_LAYER_I ; break ;
593 		case 2 : psf->sf.format |= SF_FORMAT_MPEG_LAYER_II ; break ;
594 		case 3 : psf->sf.format |= SF_FORMAT_MPEG_LAYER_III ; break ;
595 		default :
596 			return SFE_BAD_FILE ;
597 		} ;
598 	mpeg_dec_print_frameinfo (psf, &fi) ;
599 
600 	psf->read_short = mpeg_dec_read_s ;
601 	psf->read_int	= mpeg_dec_read_i ;
602 	psf->read_float	= mpeg_dec_read_f ;
603 	psf->read_double = mpeg_dec_read_d ;
604 	psf->seek		= mpeg_dec_seek ;
605 	psf->byterate	= mpeg_dec_byterate ;
606 
607 	mpeg_decoder_read_strings (psf) ;
608 
609 	return 0 ;
610 } /* mpeg_decoder_init */
611 
612 int
mpeg_decoder_get_bitrate_mode(SF_PRIVATE * psf)613 mpeg_decoder_get_bitrate_mode (SF_PRIVATE *psf)
614 {	MPEG_DEC_PRIVATE *pmp3d = (MPEG_DEC_PRIVATE *) psf->codec_data ;
615 	struct mpg123_frameinfo fi ;
616 
617 	if (mpg123_info (pmp3d->pmh, &fi) == MPG123_OK)
618 	{
619 		switch (fi.vbr)
620 		{	case MPG123_CBR : return SF_BITRATE_MODE_CONSTANT ;
621 			case MPG123_ABR : return SF_BITRATE_MODE_AVERAGE ;
622 			case MPG123_VBR : return SF_BITRATE_MODE_VARIABLE ;
623 			default : break ;
624 			} ;
625 		} ;
626 
627 	psf_log_printf (psf, "Cannot determine MPEG bitrate mode.\n") ;
628 	return -1 ;
629 } /* mpeg_decoder_get_bitrate_mode */
630 
631 #else /* HAVE_MPEG */
632 
mpeg_decoder_init(SF_PRIVATE * psf)633 int mpeg_decoder_init (SF_PRIVATE *psf)
634 {	psf_log_printf (psf, "This version of libsndfile was compiled without MPEG decode support.\n") ;
635 	return SFE_UNIMPLEMENTED ;
636 } /* mpeg_decoder_init */
637 
638 #endif /* HAVE_MPEG */
639