Mercurial > mplayer.hg
annotate libmpcodecs/ad_mpg123.c @ 32968:1fa7a299b1b4
Cosmetic: Add missing break.
author | ib |
---|---|
date | Tue, 08 Mar 2011 13:15:34 +0000 |
parents | ae5a36acc995 |
children | a93891202051 |
rev | line source |
---|---|
31524 | 1 /* |
2 * MPEG 1.0/2.0/2.5 audio layer I, II, III decoding with libmpg123 | |
3 * | |
4 * Copyright (C) 2010 Thomas Orgis <thomas@orgis.org> | |
5 * | |
6 * MPlayer is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * MPlayer 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 General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License along | |
17 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
19 */ | |
20 | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <unistd.h> | |
24 | |
25 #include "config.h" | |
26 | |
27 #include "ad_internal.h" | |
31981
ae5a36acc995
Add the proper include instead of declaring the fakemono variable extern.
diego
parents:
31666
diff
changeset
|
28 #include "dec_audio.h" |
31524 | 29 |
30 static const ad_info_t info = { | |
31 "MPEG 1.0/2.0/2.5 layers I, II, III", | |
32 "mpg123", | |
33 "Thomas Orgis", | |
34 "mpg123.org", | |
35 "High-performance decoder using libmpg123." | |
36 }; | |
37 | |
38 LIBAD_EXTERN(mpg123) | |
39 | |
40 #include "libvo/fastmemcpy.h" | |
41 | |
42 /* We avoid any usage of mpg123 API that is sensitive to the large file | |
43 * support setting. This ensures compatibility with a wide range of libmpg123 | |
44 * installs. This code is intended to work with version 1.0.0 of libmpg123. | |
45 * | |
46 * Though the chosen API subset is not affected by the choice of large file | |
47 * support, the mpg123 header (old versions of which) might include a check | |
48 * for matching _FILE_OFFSET_BITS. Since MPlayer does always define this one | |
49 * for large file support, we are safe for any default mpg123 install that | |
50 * either doesn't have such checks or defaults to the large value of | |
51 * _FILE_OFFSET_BITS . | |
52 * So, in short: There's no worry unless you have a non-default libmpg123 | |
53 * with intentionally disabled large file support. */ | |
54 /* You might need to #undef _FILE_OFFSET_BITS here on a 64 bit system | |
55 with released mpg123 1.12 when using callback API. SVN snapshots | |
56 should work fine. */ | |
57 #include <mpg123.h> | |
58 | |
59 /* Selection of mpg123 usage patterns: | |
60 * AD_MPG123_CALLBACK: Use callback API instead of feeding of memory buffers. | |
61 * That needs mpg123>=1.12, on x86-64 SVN snapshot because of | |
62 * _FILE_OFFSET_BITS being defined (see above). | |
63 * AD_MPG123_PACKET: Use packet-based input (including pts handling). | |
64 * AD_MPG123_SEEKBUFFER: Use internal mpg123 buffer to enhance stream parsing. | |
65 * Makes sense with callback API only. | |
66 * Any of those might affect I/O performance, might be significant compared | |
67 * to the excessively optimized decoding. | |
68 */ | |
69 /* #define AD_MPG123_CALLBACK */ | |
70 #define AD_MPG123_PACKET | |
71 /* #define AD_MPG123_SEEKBUFFER */ | |
72 | |
73 /* Switch for updating bitrate info of VBR files. Not essential. */ | |
74 #define AD_MPG123_MEAN_BITRATE | |
75 | |
76 struct ad_mpg123_context { | |
77 mpg123_handle *handle; | |
78 #ifdef AD_MPG123_MEAN_BITRATE | |
79 /* Running mean for bit rate, stream length estimation. */ | |
80 float mean_rate; | |
81 unsigned int mean_count; | |
82 /* Time delay for updates. */ | |
83 short delay; | |
84 #endif | |
85 /* If the stream is actually VBR. */ | |
86 char vbr; | |
87 #if (defined AD_MPG123_CALLBACK) && (defined AD_MPG123_PACKET) | |
88 unsigned char *packet; | |
89 int packleft; | |
90 #endif | |
91 }; | |
92 | |
93 static void context_reset(struct ad_mpg123_context *con) | |
94 { | |
95 #ifdef AD_MPG123_MEAN_BITRATE | |
96 con->mean_rate = 0.; | |
97 con->mean_count = 0; | |
98 con->delay = 1; | |
99 #endif | |
100 #if (defined AD_MPG123_CALLBACK) && (defined AD_MPG123_PACKET) | |
101 con->packet = NULL; | |
102 con->packleft = 0; | |
103 #endif | |
104 } | |
105 | |
106 | |
107 #ifdef AD_MPG123_CALLBACK | |
108 /* Mpg123 calls that for retrieving data. | |
109 * This wrapper is at least needed for the call frame (ssize_t vs. int). */ | |
110 static ssize_t read_callback(void *ash, void *buf, size_t count) | |
111 { | |
112 sh_audio_t *sh = ash; | |
113 #ifdef AD_MPG123_PACKET | |
114 struct ad_mpg123_context *con = sh->context; | |
115 unsigned char *target = buf; | |
116 int need = count; | |
117 ssize_t got = 0; | |
118 while (need > 0) { | |
119 if (con->packleft > 0) { | |
120 int get = need > con->packleft ? con->packleft : need; | |
121 /* Any difference to normal memcpy? */ | |
122 fast_memcpy(target, con->packet, get); | |
123 /* OK, that does look redundant. */ | |
124 con->packet += get; | |
125 con->packleft -= get; | |
126 target += get; | |
127 need -= get; | |
128 got += get; | |
129 } else { | |
130 double pts; | |
131 /* Feed more input data. */ | |
132 con->packleft = ds_get_packet_pts(sh->ds, &con->packet, &pts); | |
133 if (con->packleft <= 0) | |
134 break; /* Apparently that's it. EOF. */ | |
135 | |
136 /* Next bytes from that presentation time. */ | |
137 if (pts != MP_NOPTS_VALUE) { | |
138 sh->pts = pts; | |
139 sh->pts_bytes = 0; | |
140 } | |
141 } | |
142 } | |
143 return got; | |
144 #else | |
145 /* It returns int... with the meaning of byte count. */ | |
146 return (ssize_t) demux_read_data(sh->ds, buf, count); | |
147 #endif | |
148 } | |
149 | |
150 /* Arbitrary input seeking is not supported with this MPlayer API(?). | |
151 That also means that we won't read any ID3v1 tags. */ | |
152 static off_t seek_callback(void *sh, off_t pos, int whence) | |
153 { | |
154 return -1; | |
155 } | |
156 #endif | |
157 | |
158 /* This initializes libmpg123 and prepares the handle, including funky | |
159 * parameters. */ | |
160 static int preinit(sh_audio_t *sh) | |
161 { | |
162 int err, flag; | |
163 struct ad_mpg123_context *con; | |
164 /* Assumption: You always call preinit + init + uninit, on every file. | |
165 * But you stop at preinit in case it fails. | |
166 * If that is not true, one must ensure not to call mpg123_init / exit | |
167 * twice in a row. */ | |
168 if (mpg123_init() != MPG123_OK) | |
169 return 0; | |
170 | |
171 sh->context = malloc(sizeof(struct ad_mpg123_context)); | |
172 con = sh->context; | |
173 context_reset(con); | |
174 | |
175 /* Auto-choice of optimized decoder (first argument NULL). */ | |
176 con->handle = mpg123_new(NULL, &err); | |
177 if (!con->handle) | |
178 goto bad_end; | |
179 | |
180 #ifdef CONFIG_FAKE_MONO | |
181 /* Guessing here: Default value triggers forced upmix of mono to stereo. */ | |
182 flag = fakemono == 0 ? MPG123_FORCE_STEREO : | |
183 fakemono == 1 ? MPG123_MONO_LEFT : | |
184 fakemono == 2 ? MPG123_MONO_RIGHT : 0; | |
185 if (mpg123_param(con->handle, MPG123_ADD_FLAGS, flag, 0.0) != MPG123_OK) | |
186 goto bad_end; | |
187 #endif | |
188 #ifdef AD_MPG123_CALLBACK | |
189 /* The I/O is handled via callbacks to MPlayer stream functions, | |
190 * actually only the reading, as general seeking does not seem to be available */ | |
191 if (mpg123_replace_reader_handle(con->handle, read_callback, | |
192 seek_callback, NULL) != MPG123_OK) { | |
193 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 error: %s\n", | |
194 mpg123_strerror(con->handle)); | |
195 mpg123_exit(); | |
196 return 0; | |
197 } | |
198 #endif | |
199 | |
200 /* Basic settings. | |
201 * Don't spill messages, enable better resync with non-seekable streams. | |
202 * Give both flags individually without error checking to keep going with | |
203 * old libmpg123. Generally, it is not fatal if the flags are not | |
204 * honored */ | |
205 mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0.0); | |
206 /* Old headers don't know MPG123_SEEKBUFFER yet, so use the plain 0x100. */ | |
207 #ifdef AD_MPG123_SEEKBUFFER | |
208 mpg123_param(con->handle, MPG123_ADD_FLAGS, 0x100, 0.0); | |
209 #endif | |
31666 | 210 /* Do not bail out on malformed streams at all. |
211 * MPlayer does not handle a decoder throwing the towel on crappy input. */ | |
212 mpg123_param(con->handle, MPG123_RESYNC_LIMIT, -1, 0.0); | |
31524 | 213 |
214 /* Open decisions: Configure libmpg123 to force encoding (or stay open about | |
215 * library builds that support only float or int32 output), (de)configure | |
216 * gapless decoding (won't work with seeking in MPlayer, though). | |
217 * Don't forget to eventually enable ReplayGain/RVA support, too. | |
218 * Let's try to run with the default for now. */ | |
219 | |
220 /* Example for RVA choice (available since libmpg123 1.0.0): | |
221 mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */ | |
222 | |
223 return 1; | |
224 | |
225 bad_end: | |
226 if (!con->handle) | |
227 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", | |
228 mpg123_plain_strerror(err)); | |
229 else | |
230 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", | |
231 mpg123_strerror(con->handle)); | |
232 | |
233 if (con->handle) | |
234 mpg123_delete(con->handle); | |
235 mpg123_exit(); | |
236 free(sh->context); | |
237 sh->context = NULL; | |
238 return 0; | |
239 } | |
240 | |
241 /* Compute bitrate from frame size. */ | |
242 static int compute_bitrate(struct mpg123_frameinfo *i) | |
243 { | |
244 static const int samples_per_frame[4][4] = { | |
245 {-1, 384, 1152, 1152}, /* MPEG 1 */ | |
246 {-1, 384, 1152, 576}, /* MPEG 2 */ | |
247 {-1, 384, 1152, 576}, /* MPEG 2.5 */ | |
248 {-1, -1, -1, -1}, /* Unknown */ | |
249 }; | |
250 return (int) ((i->framesize + 4) * 8 * i->rate * 0.001 / | |
251 samples_per_frame[i->version][i->layer] + 0.5); | |
252 } | |
253 | |
254 /* Opted against the header printout from old mp3lib, too much | |
255 * irrelevant info. This is modelled after the mpg123 app's | |
256 * standard output line. | |
257 * If more verbosity is demanded, one can add more detail and | |
258 * also throw in ID3v2 info which libmpg123 collects anyway. */ | |
259 static void print_header_compact(struct mpg123_frameinfo *i) | |
260 { | |
261 static const char *smodes[5] = { | |
262 "stereo", "joint-stereo", "dual-channel", "mono", "invalid" | |
263 }; | |
264 static const char *layers[4] = { | |
265 "Unknown", "I", "II", "III" | |
266 }; | |
267 static const char *versions[4] = { | |
268 "1.0", "2.0", "2.5", "x.x" | |
269 }; | |
270 | |
271 mp_msg(MSGT_DECAUDIO, MSGL_V, "MPEG %s layer %s, ", | |
272 versions[i->version], layers[i->layer]); | |
273 switch (i->vbr) { | |
274 case MPG123_CBR: | |
275 if (i->bitrate) | |
276 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s", i->bitrate); | |
277 else | |
278 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s (free format)", | |
279 compute_bitrate(i)); | |
280 break; | |
281 case MPG123_VBR: | |
282 mp_msg(MSGT_DECAUDIO, MSGL_V, "VBR"); | |
283 break; | |
284 case MPG123_ABR: | |
285 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s ABR", i->abr_rate); | |
286 break; | |
287 default: | |
288 mp_msg(MSGT_DECAUDIO, MSGL_V, "???"); | |
289 } | |
290 mp_msg(MSGT_DECAUDIO, MSGL_V, ", %ld Hz %s\n", i->rate, | |
291 smodes[i->mode]); | |
292 } | |
293 | |
294 #ifndef AD_MPG123_CALLBACK | |
295 /* This tries to extract a requested amount of decoded data. | |
296 * Even when you request 0 bytes, it will feed enough input so that | |
297 * the decoder _could_ have delivered something. | |
298 * Returns byte count >= 0, -1 on error. | |
299 * | |
300 * Thoughts on exact pts keeping: | |
301 * We have to assume that MPEG frames are cut in pieces by packet boundaries. | |
302 * Also, it might be possible that the first packet does not contain enough | |
303 * data to ensure initial stream sync... or re-sync on erroneous streams. | |
304 * So we need something robust to relate the decoded byte count to the correct | |
305 * time stamp. This is tricky, though. From the outside, you cannot tell if, | |
306 * after having fed two packets until the first output arrives, one should | |
307 * start counting from the first packet's pts or the second packet's. | |
308 * So, let's just count from the last fed package's pts. If the packets are | |
309 * exactly cut to MPEG frames, this will cause one frame mismatch in the | |
310 * beginning (when mpg123 peeks ahead for the following header), but will | |
311 * be corrected with the third frame already. One might add special code to | |
312 * not increment the base pts past the first packet's after a resync before | |
313 * the first decoded bytes arrived. */ | |
314 static int decode_a_bit(sh_audio_t *sh, unsigned char *buf, int count) | |
315 { | |
316 int ret = MPG123_OK; | |
317 int got = 0; | |
318 struct ad_mpg123_context *con = sh->context; | |
319 | |
320 /* There will be one MPG123_NEW_FORMAT message on first open. | |
321 * This will be implicitly handled in reopen_stream(). */ | |
322 do { | |
323 size_t got_now = 0; | |
324 ret = mpg123_decode(con->handle, NULL, 0, buf + got, count - got, | |
325 &got_now); | |
326 got += got_now; | |
327 #ifdef AD_MPG123_PACKET | |
328 sh->pts_bytes += got_now; | |
329 #endif | |
330 | |
331 if (ret == MPG123_NEED_MORE) { | |
332 int incount; | |
333 #ifdef AD_MPG123_PACKET | |
334 double pts; | |
335 unsigned char *inbuf; | |
336 /* Feed more input data. */ | |
337 incount = ds_get_packet_pts(sh->ds, &inbuf, &pts); | |
338 if (incount <= 0) | |
339 break; /* Apparently that's it. EOF. */ | |
340 | |
341 /* Next bytes from that presentation time. */ | |
342 if (pts != MP_NOPTS_VALUE) { | |
343 sh->pts = pts; | |
344 sh->pts_bytes = 0; | |
345 } | |
346 #else | |
347 const int inbufsize = 4096; | |
348 unsigned char inbuf[inbufsize]; | |
349 /* Feed more input data. */ | |
350 incount = demux_read_data(((sh_audio_t *) sh)->ds, | |
351 inbuf, inbufsize); | |
352 if (incount == 0) | |
353 break; /* Apparently that's it. EOF. */ | |
354 #endif | |
355 | |
356 /* Do not use mpg123_feed(), added in later libmpg123 versions. */ | |
357 ret = mpg123_decode(con->handle, inbuf, incount, NULL, 0, NULL); | |
358 /* Return value is checked in the loop condition. | |
359 * It could be MPG12_OK now, it could need more. */ | |
360 } | |
361 /* Older mpg123 versions might indicate MPG123_DONE, so be prepared. */ | |
362 else if (ret == MPG123_ERR || ret == MPG123_DONE) | |
363 break; | |
364 | |
365 } while (ret == MPG123_NEED_MORE || got < count); | |
366 | |
367 if (ret == MPG123_ERR) { | |
368 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 decoding failed: %s\n", | |
369 mpg123_strerror(con->handle)); | |
370 mpg123_close(con->handle); | |
371 return -1; | |
372 } | |
373 | |
374 return got; | |
375 } | |
376 #endif | |
377 | |
378 /* Close, reopen stream. Feed data until we know the format of the stream. | |
379 * 1 on success, 0 on error */ | |
380 static int reopen_stream(sh_audio_t *sh) | |
381 { | |
382 long rate; | |
383 int chan, enc; | |
384 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context; | |
385 | |
386 mpg123_close(con->handle); | |
387 context_reset(con); | |
388 | |
389 #ifdef AD_MPG123_CALLBACK | |
390 if (MPG123_OK == mpg123_open_handle(con->handle, sh) && | |
391 #else | |
392 if (/* Open and make sure we have fed enough data to get stream properties. */ | |
393 MPG123_OK == mpg123_open_feed(con->handle) && | |
394 /* Feed data until mpg123 is ready (has found stream beginning). */ | |
395 !decode_a_bit(sh, NULL, 0) && | |
396 #endif | |
397 /* Not handing NULL pointers for compatibility with old libmpg123. */ | |
398 MPG123_OK == mpg123_getformat(con->handle, &rate, &chan, &enc)) { | |
399 return 1; | |
400 } else { | |
401 mp_msg(MSGT_DECAUDIO, MSGL_ERR, | |
402 "mpg123 failed to reopen stream: %s\n", | |
403 mpg123_strerror(con->handle)); | |
404 mpg123_close(con->handle); | |
405 return 0; | |
406 } | |
407 } | |
408 | |
409 /* Now we really start accessing some data and determining file format. | |
410 * Paranoia note: The mpg123_close() on errors is not really necessary, | |
411 * But it ensures that we don't accidentally continue decoding with a | |
412 * bad state (possibly interpreting the format badly or whatnot). */ | |
413 static int init(sh_audio_t *sh) | |
414 { | |
415 long rate = 0; | |
416 int channels = 0; | |
417 int encoding = 0; | |
418 mpg123_id3v2 *v2; | |
419 struct mpg123_frameinfo finfo; | |
420 struct ad_mpg123_context *con = sh->context; | |
421 | |
422 /* We're open about any output format that libmpg123 will suggest. | |
423 * Note that a standard build will always default to 16 bit signed and | |
424 * the native sample rate of the file. */ | |
425 if (MPG123_OK == mpg123_format_all(con->handle) && | |
426 reopen_stream(sh) && | |
427 MPG123_OK == mpg123_getformat(con->handle, &rate, &channels, &encoding) && | |
428 /* Forbid the format to change later on. */ | |
429 MPG123_OK == mpg123_format_none(con->handle) && | |
430 MPG123_OK == mpg123_format(con->handle, rate, channels, encoding) && | |
431 /* Get MPEG header info. */ | |
432 MPG123_OK == mpg123_info(con->handle, &finfo) && | |
433 /* Since we queried format, mpg123 should have read past ID3v2 tags. | |
434 * We need to decide if printing of UTF-8 encoded text info is wanted. */ | |
435 MPG123_OK == mpg123_id3(con->handle, NULL, &v2)) { | |
436 /* If we are here, we passed all hurdles. Yay! Extract the info. */ | |
437 print_header_compact(&finfo); | |
438 /* Do we want to print out the UTF-8 Id3v2 info? | |
439 if (v2) | |
440 print_id3v2(v2); */ | |
441 | |
442 /* Have kb/s, want B/s | |
443 * For VBR, the first frame will be a bad estimate. */ | |
444 sh->i_bps = (finfo.bitrate ? finfo.bitrate : compute_bitrate(&finfo)) | |
445 * 1000 / 8; | |
446 context_reset(con); | |
447 con->vbr = (finfo.vbr != MPG123_CBR); | |
448 sh->channels = channels; | |
449 sh->samplerate = rate; | |
450 /* Without external force, mpg123 will always choose signed encoding, | |
451 * and non-16-bit only on builds that don't support it. | |
452 * Be reminded that it doesn't matter to the MPEG file what encoding | |
453 * is produced from it. */ | |
454 switch (encoding) { | |
455 case MPG123_ENC_SIGNED_8: | |
456 sh->sample_format = AF_FORMAT_S8; | |
457 sh->samplesize = 1; | |
458 break; | |
459 case MPG123_ENC_SIGNED_16: | |
460 sh->sample_format = AF_FORMAT_S16_NE; | |
461 sh->samplesize = 2; | |
462 break; | |
463 /* To stay compatible with the oldest libmpg123 headers, do not rely | |
464 * on float and 32 bit encoding symbols being defined. | |
465 * Those formats came later */ | |
466 case 0x1180: /* MPG123_ENC_SIGNED_32 */ | |
467 sh->sample_format = AF_FORMAT_S32_NE; | |
468 sh->samplesize = 4; | |
469 break; | |
470 case 0x200: /* MPG123_ENC_FLOAT_32 */ | |
471 sh->sample_format = AF_FORMAT_FLOAT_NE; | |
472 sh->samplesize = 4; | |
473 break; | |
474 default: | |
475 mp_msg(MSGT_DECAUDIO, MSGL_ERR, | |
476 "Bad encoding from mpg123: %i.\n", encoding); | |
477 mpg123_close(con->handle); | |
478 return 0; | |
479 } | |
480 | |
481 return 1; | |
482 } else { | |
483 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 init error: %s\n", | |
484 mpg123_strerror(con->handle)); | |
485 mpg123_close(con->handle); | |
486 return 0; | |
487 } | |
488 } | |
489 | |
490 static void uninit(sh_audio_t *sh) | |
491 { | |
492 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context; | |
493 | |
494 mpg123_close(con->handle); | |
495 mpg123_delete(con->handle); | |
496 free(sh->context); | |
497 sh->context = NULL; | |
498 mpg123_exit(); | |
499 } | |
500 | |
501 #ifdef AD_MPG123_MEAN_BITRATE | |
502 /* Update mean bitrate. This could be dropped if accurate time display | |
503 * on audio file playback is not desired. */ | |
504 static void update_info(sh_audio_t *sh) | |
505 { | |
506 struct ad_mpg123_context *con = sh->context; | |
507 if (con->vbr && --con->delay < 1) { | |
508 struct mpg123_frameinfo finfo; | |
509 if (MPG123_OK == mpg123_info(con->handle, &finfo)) { | |
510 if (++con->mean_count > ((unsigned int) -1) / 2) | |
511 con->mean_count = ((unsigned int) -1) / 4; | |
512 | |
513 /* Might not be numerically optimal, but works fine enough. */ | |
514 con->mean_rate = ((con->mean_count - 1) * con->mean_rate + | |
515 finfo.bitrate) / con->mean_count; | |
516 sh->i_bps = (int) (con->mean_rate * 1000 / 8); | |
517 | |
518 con->delay = 10; | |
519 } | |
520 } | |
521 } | |
522 #endif | |
523 | |
524 static int decode_audio(sh_audio_t *sh, unsigned char *buf, int minlen, | |
525 int maxlen) | |
526 { | |
527 int bytes; | |
528 | |
529 #ifdef AD_MPG123_CALLBACK | |
530 struct ad_mpg123_context *con = sh->context; | |
531 size_t got_bytes = 0; | |
532 if (MPG123_ERR == mpg123_read(con->handle, buf, minlen, &got_bytes)) { | |
533 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Decoding error in mpg123: %s\n", | |
534 mpg123_strerror(con->handle)); | |
535 return -1; | |
536 } | |
537 #ifdef AD_MPG123_PACKET | |
538 sh->pts_bytes += got_bytes; | |
539 #endif | |
540 bytes = got_bytes; | |
541 #else | |
542 bytes = decode_a_bit(sh, buf, minlen); | |
543 #endif | |
544 | |
545 if (bytes == 0) | |
546 return -1; /* EOF */ | |
547 | |
548 #ifdef AD_MPG123_MEAN_BITRATE | |
549 update_info(sh); | |
550 #endif | |
551 | |
552 return bytes; | |
553 } | |
554 | |
555 static int control(sh_audio_t *sh, int cmd, void *arg, ...) | |
556 { | |
557 switch (cmd) { | |
558 case ADCTRL_RESYNC_STREAM: | |
559 /* Close/reopen the stream for mpg123 to make sure it doesn't | |
560 * think that it still knows the exact stream position. | |
561 * Otherwise, we would have funny effects from the gapless code. | |
562 * Oh, and it helps to minimize artifacts from jumping in the stream. */ | |
563 if (reopen_stream(sh)) { | |
564 #ifdef AD_MPG123_MEAN_BITRATE | |
565 update_info(sh); | |
566 #endif | |
567 return CONTROL_TRUE; | |
568 } else { | |
569 mp_msg(MSGT_DECAUDIO, MSGL_ERR, | |
570 "mpg123 cannot reopen stream for resync.\n"); | |
571 return CONTROL_FALSE; | |
572 } | |
573 break; | |
574 } | |
575 return CONTROL_UNKNOWN; | |
576 } |