Mercurial > mplayer.hg
annotate libmpcodecs/ad_mpg123.c @ 35936:68a031dce5c1
Remove fsSelectedDirectoryUtf8.
It's obsolete after r36029.
author | ib |
---|---|
date | Thu, 21 Mar 2013 09:41:03 +0000 |
parents | 079b53acda6d |
children | de83009f96bd |
rev | line source |
---|---|
31524 | 1 /* |
2 * MPEG 1.0/2.0/2.5 audio layer I, II, III decoding with libmpg123 | |
3 * | |
34722 | 4 * Copyright (C) 2010-2012 Thomas Orgis <thomas@orgis.org> |
31524 | 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" | |
34174
a93891202051
Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents:
31981
diff
changeset
|
26 #include "mp_msg.h" |
31524 | 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 | |
34722 | 42 /* Reducing the ifdeffery to two main variants: |
43 * 1. most compatible to any libmpg123 version | |
44 * 2. fastest variant with recent libmpg123 (>=1.14) | |
45 * Running variant 2 on older libmpg123 versions may work in | |
46 * principle, but is not supported. | |
47 * So, please leave the check for MPG123_API_VERSION there, m-kay? | |
48 */ | |
31524 | 49 #include <mpg123.h> |
50 | |
34722 | 51 /* Enable faster mode of operation with newer libmpg123, avoiding |
52 * unnecessary memcpy() calls. */ | |
53 #if (defined MPG123_API_VERSION) && (MPG123_API_VERSION >= 33) | |
54 #define AD_MPG123_FRAMEWISE | |
55 #endif | |
31524 | 56 |
57 /* Switch for updating bitrate info of VBR files. Not essential. */ | |
58 #define AD_MPG123_MEAN_BITRATE | |
59 | |
60 struct ad_mpg123_context { | |
61 mpg123_handle *handle; | |
62 #ifdef AD_MPG123_MEAN_BITRATE | |
63 /* Running mean for bit rate, stream length estimation. */ | |
64 float mean_rate; | |
65 unsigned int mean_count; | |
66 /* Time delay for updates. */ | |
67 short delay; | |
68 #endif | |
69 /* If the stream is actually VBR. */ | |
70 char vbr; | |
71 }; | |
72 | |
73 /* This initializes libmpg123 and prepares the handle, including funky | |
74 * parameters. */ | |
75 static int preinit(sh_audio_t *sh) | |
76 { | |
77 int err, flag; | |
78 struct ad_mpg123_context *con; | |
79 /* Assumption: You always call preinit + init + uninit, on every file. | |
80 * But you stop at preinit in case it fails. | |
81 * If that is not true, one must ensure not to call mpg123_init / exit | |
82 * twice in a row. */ | |
83 if (mpg123_init() != MPG123_OK) | |
84 return 0; | |
85 | |
86 sh->context = malloc(sizeof(struct ad_mpg123_context)); | |
87 con = sh->context; | |
88 /* Auto-choice of optimized decoder (first argument NULL). */ | |
89 con->handle = mpg123_new(NULL, &err); | |
90 if (!con->handle) | |
91 goto bad_end; | |
92 | |
93 #ifdef CONFIG_FAKE_MONO | |
94 /* Guessing here: Default value triggers forced upmix of mono to stereo. */ | |
95 flag = fakemono == 0 ? MPG123_FORCE_STEREO : | |
96 fakemono == 1 ? MPG123_MONO_LEFT : | |
97 fakemono == 2 ? MPG123_MONO_RIGHT : 0; | |
98 if (mpg123_param(con->handle, MPG123_ADD_FLAGS, flag, 0.0) != MPG123_OK) | |
99 goto bad_end; | |
100 #endif | |
101 | |
102 /* Basic settings. | |
103 * Don't spill messages, enable better resync with non-seekable streams. | |
104 * Give both flags individually without error checking to keep going with | |
105 * old libmpg123. Generally, it is not fatal if the flags are not | |
106 * honored */ | |
107 mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0.0); | |
31666 | 108 /* Do not bail out on malformed streams at all. |
109 * MPlayer does not handle a decoder throwing the towel on crappy input. */ | |
110 mpg123_param(con->handle, MPG123_RESYNC_LIMIT, -1, 0.0); | |
31524 | 111 |
112 /* Open decisions: Configure libmpg123 to force encoding (or stay open about | |
113 * library builds that support only float or int32 output), (de)configure | |
114 * gapless decoding (won't work with seeking in MPlayer, though). | |
115 * Don't forget to eventually enable ReplayGain/RVA support, too. | |
116 * Let's try to run with the default for now. */ | |
117 | |
34722 | 118 /* That would produce floating point output. |
119 * You can get 32 and 24 bit ints, even 8 bit via format matrix. */ | |
120 /* mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); */ | |
121 | |
31524 | 122 /* Example for RVA choice (available since libmpg123 1.0.0): |
123 mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */ | |
124 | |
34722 | 125 #ifdef AD_MPG123_FRAMEWISE |
126 /* Prevent funky automatic resampling. | |
127 * This way, we can be sure that one frame will never produce | |
128 * more than 1152 stereo samples. */ | |
129 mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0.); | |
130 #else | |
131 /* Older mpg123 is vulnerable to concatenated streams when gapless cutting | |
132 * is enabled (will only play the jingle of a badly constructed radio | |
133 * stream). The versions using framewise decoding are fine with that. */ | |
134 mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0.); | |
135 #endif | |
136 | |
31524 | 137 return 1; |
138 | |
139 bad_end: | |
140 if (!con->handle) | |
141 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", | |
142 mpg123_plain_strerror(err)); | |
143 else | |
144 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", | |
145 mpg123_strerror(con->handle)); | |
146 | |
147 if (con->handle) | |
148 mpg123_delete(con->handle); | |
149 mpg123_exit(); | |
150 free(sh->context); | |
151 sh->context = NULL; | |
152 return 0; | |
153 } | |
154 | |
155 /* Compute bitrate from frame size. */ | |
156 static int compute_bitrate(struct mpg123_frameinfo *i) | |
157 { | |
158 static const int samples_per_frame[4][4] = { | |
159 {-1, 384, 1152, 1152}, /* MPEG 1 */ | |
160 {-1, 384, 1152, 576}, /* MPEG 2 */ | |
161 {-1, 384, 1152, 576}, /* MPEG 2.5 */ | |
162 {-1, -1, -1, -1}, /* Unknown */ | |
163 }; | |
164 return (int) ((i->framesize + 4) * 8 * i->rate * 0.001 / | |
165 samples_per_frame[i->version][i->layer] + 0.5); | |
166 } | |
167 | |
168 /* Opted against the header printout from old mp3lib, too much | |
169 * irrelevant info. This is modelled after the mpg123 app's | |
170 * standard output line. | |
171 * If more verbosity is demanded, one can add more detail and | |
172 * also throw in ID3v2 info which libmpg123 collects anyway. */ | |
173 static void print_header_compact(struct mpg123_frameinfo *i) | |
174 { | |
175 static const char *smodes[5] = { | |
176 "stereo", "joint-stereo", "dual-channel", "mono", "invalid" | |
177 }; | |
178 static const char *layers[4] = { | |
179 "Unknown", "I", "II", "III" | |
180 }; | |
181 static const char *versions[4] = { | |
182 "1.0", "2.0", "2.5", "x.x" | |
183 }; | |
184 | |
185 mp_msg(MSGT_DECAUDIO, MSGL_V, "MPEG %s layer %s, ", | |
186 versions[i->version], layers[i->layer]); | |
187 switch (i->vbr) { | |
188 case MPG123_CBR: | |
189 if (i->bitrate) | |
190 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s", i->bitrate); | |
191 else | |
192 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s (free format)", | |
193 compute_bitrate(i)); | |
194 break; | |
195 case MPG123_VBR: | |
196 mp_msg(MSGT_DECAUDIO, MSGL_V, "VBR"); | |
197 break; | |
198 case MPG123_ABR: | |
199 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s ABR", i->abr_rate); | |
200 break; | |
201 default: | |
202 mp_msg(MSGT_DECAUDIO, MSGL_V, "???"); | |
203 } | |
204 mp_msg(MSGT_DECAUDIO, MSGL_V, ", %ld Hz %s\n", i->rate, | |
205 smodes[i->mode]); | |
206 } | |
207 | |
208 /* This tries to extract a requested amount of decoded data. | |
209 * Even when you request 0 bytes, it will feed enough input so that | |
210 * the decoder _could_ have delivered something. | |
211 * Returns byte count >= 0, -1 on error. | |
212 * | |
213 * Thoughts on exact pts keeping: | |
214 * We have to assume that MPEG frames are cut in pieces by packet boundaries. | |
215 * Also, it might be possible that the first packet does not contain enough | |
216 * data to ensure initial stream sync... or re-sync on erroneous streams. | |
217 * So we need something robust to relate the decoded byte count to the correct | |
218 * time stamp. This is tricky, though. From the outside, you cannot tell if, | |
219 * after having fed two packets until the first output arrives, one should | |
220 * start counting from the first packet's pts or the second packet's. | |
221 * So, let's just count from the last fed package's pts. If the packets are | |
222 * exactly cut to MPEG frames, this will cause one frame mismatch in the | |
223 * beginning (when mpg123 peeks ahead for the following header), but will | |
224 * be corrected with the third frame already. One might add special code to | |
225 * not increment the base pts past the first packet's after a resync before | |
226 * the first decoded bytes arrived. */ | |
227 static int decode_a_bit(sh_audio_t *sh, unsigned char *buf, int count) | |
228 { | |
229 int ret = MPG123_OK; | |
230 int got = 0; | |
231 struct ad_mpg123_context *con = sh->context; | |
232 | |
233 /* There will be one MPG123_NEW_FORMAT message on first open. | |
34722 | 234 * This will be handled in init(). */ |
31524 | 235 do { |
236 size_t got_now = 0; | |
237 | |
34722 | 238 /* Feed the decoder. This will only fire from the second round on. */ |
31524 | 239 if (ret == MPG123_NEED_MORE) { |
240 int incount; | |
241 double pts; | |
242 unsigned char *inbuf; | |
243 /* Feed more input data. */ | |
244 incount = ds_get_packet_pts(sh->ds, &inbuf, &pts); | |
245 if (incount <= 0) | |
246 break; /* Apparently that's it. EOF. */ | |
247 | |
248 /* Next bytes from that presentation time. */ | |
249 if (pts != MP_NOPTS_VALUE) { | |
250 sh->pts = pts; | |
251 sh->pts_bytes = 0; | |
252 } | |
34722 | 253 |
254 #ifdef AD_MPG123_FRAMEWISE | |
255 /* Have to use mpg123_feed() to avoid decoding here. */ | |
256 ret = mpg123_feed(con->handle, inbuf, incount); | |
31524 | 257 #else |
258 /* Do not use mpg123_feed(), added in later libmpg123 versions. */ | |
259 ret = mpg123_decode(con->handle, inbuf, incount, NULL, 0, NULL); | |
34722 | 260 #endif |
261 if (ret == MPG123_ERR) | |
262 break; | |
31524 | 263 } |
34722 | 264 /* Theoretically, mpg123 could return MPG123_DONE, so be prepared. |
265 * Should not happen in our usage, but it is a valid return code. */ | |
31524 | 266 else if (ret == MPG123_ERR || ret == MPG123_DONE) |
267 break; | |
268 | |
34722 | 269 /* Try to decode a bit. This is the return value that counts |
270 * for the loop condition. */ | |
271 #ifdef AD_MPG123_FRAMEWISE | |
272 if (!buf) { /* fake call just for feeding to get format */ | |
34797
079b53acda6d
Workaround bug in mpg123. In rare cases, after seeking mplayer stops audio playback and would repeat "No stream opened. (code 24)" until next seek.
iive
parents:
34722
diff
changeset
|
273 ret = mpg123_getformat(con->handle, NULL, NULL, NULL); |
34722 | 274 } else { /* This is the decoding. One frame at a time. */ |
275 ret = mpg123_replace_buffer(con->handle, buf, count); | |
276 if (ret == MPG123_OK) | |
277 ret = mpg123_decode_frame(con->handle, NULL, NULL, &got_now); | |
278 } | |
279 #else | |
280 ret = mpg123_decode(con->handle, NULL, 0, buf + got, count - got, | |
281 &got_now); | |
282 #endif | |
283 | |
284 got += got_now; | |
285 sh->pts_bytes += got_now; | |
286 | |
287 #ifdef AD_MPG123_FRAMEWISE | |
288 } while (ret == MPG123_NEED_MORE || (got == 0 && count != 0)); | |
289 #else | |
31524 | 290 } while (ret == MPG123_NEED_MORE || got < count); |
34722 | 291 #endif |
31524 | 292 |
293 if (ret == MPG123_ERR) { | |
294 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 decoding failed: %s\n", | |
295 mpg123_strerror(con->handle)); | |
296 mpg123_close(con->handle); | |
297 return -1; | |
298 } | |
299 | |
300 return got; | |
301 } | |
302 | |
303 /* Close, reopen stream. Feed data until we know the format of the stream. | |
304 * 1 on success, 0 on error */ | |
305 static int reopen_stream(sh_audio_t *sh) | |
306 { | |
307 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context; | |
308 | |
309 mpg123_close(con->handle); | |
34722 | 310 /* No resetting of the context: |
311 * We do not want to loose the mean bitrate data. */ | |
31524 | 312 |
34722 | 313 /* Open and make sure we have fed enough data to get stream properties. */ |
314 if (MPG123_OK == mpg123_open_feed(con->handle) && | |
31524 | 315 /* Feed data until mpg123 is ready (has found stream beginning). */ |
34797
079b53acda6d
Workaround bug in mpg123. In rare cases, after seeking mplayer stops audio playback and would repeat "No stream opened. (code 24)" until next seek.
iive
parents:
34722
diff
changeset
|
316 !decode_a_bit(sh, NULL, 0)) { |
31524 | 317 return 1; |
318 } else { | |
319 mp_msg(MSGT_DECAUDIO, MSGL_ERR, | |
320 "mpg123 failed to reopen stream: %s\n", | |
321 mpg123_strerror(con->handle)); | |
322 mpg123_close(con->handle); | |
323 return 0; | |
324 } | |
325 } | |
326 | |
327 /* Now we really start accessing some data and determining file format. | |
328 * Paranoia note: The mpg123_close() on errors is not really necessary, | |
329 * But it ensures that we don't accidentally continue decoding with a | |
330 * bad state (possibly interpreting the format badly or whatnot). */ | |
331 static int init(sh_audio_t *sh) | |
332 { | |
333 long rate = 0; | |
334 int channels = 0; | |
335 int encoding = 0; | |
336 mpg123_id3v2 *v2; | |
337 struct mpg123_frameinfo finfo; | |
338 struct ad_mpg123_context *con = sh->context; | |
339 | |
340 /* We're open about any output format that libmpg123 will suggest. | |
341 * Note that a standard build will always default to 16 bit signed and | |
342 * the native sample rate of the file. */ | |
343 if (MPG123_OK == mpg123_format_all(con->handle) && | |
344 reopen_stream(sh) && | |
345 MPG123_OK == mpg123_getformat(con->handle, &rate, &channels, &encoding) && | |
346 /* Forbid the format to change later on. */ | |
347 MPG123_OK == mpg123_format_none(con->handle) && | |
348 MPG123_OK == mpg123_format(con->handle, rate, channels, encoding) && | |
349 /* Get MPEG header info. */ | |
350 MPG123_OK == mpg123_info(con->handle, &finfo) && | |
351 /* Since we queried format, mpg123 should have read past ID3v2 tags. | |
352 * We need to decide if printing of UTF-8 encoded text info is wanted. */ | |
353 MPG123_OK == mpg123_id3(con->handle, NULL, &v2)) { | |
354 /* If we are here, we passed all hurdles. Yay! Extract the info. */ | |
355 print_header_compact(&finfo); | |
356 /* Do we want to print out the UTF-8 Id3v2 info? | |
357 if (v2) | |
358 print_id3v2(v2); */ | |
359 | |
360 /* Have kb/s, want B/s | |
361 * For VBR, the first frame will be a bad estimate. */ | |
362 sh->i_bps = (finfo.bitrate ? finfo.bitrate : compute_bitrate(&finfo)) | |
363 * 1000 / 8; | |
34722 | 364 #ifdef AD_MPG123_MEAN_BITRATE |
365 con->delay = 1; | |
366 con->mean_rate = 0.; | |
367 con->mean_count = 0; | |
368 #endif | |
31524 | 369 con->vbr = (finfo.vbr != MPG123_CBR); |
370 sh->channels = channels; | |
371 sh->samplerate = rate; | |
372 /* Without external force, mpg123 will always choose signed encoding, | |
373 * and non-16-bit only on builds that don't support it. | |
374 * Be reminded that it doesn't matter to the MPEG file what encoding | |
375 * is produced from it. */ | |
376 switch (encoding) { | |
377 case MPG123_ENC_SIGNED_8: | |
378 sh->sample_format = AF_FORMAT_S8; | |
379 sh->samplesize = 1; | |
380 break; | |
381 case MPG123_ENC_SIGNED_16: | |
382 sh->sample_format = AF_FORMAT_S16_NE; | |
383 sh->samplesize = 2; | |
384 break; | |
385 /* To stay compatible with the oldest libmpg123 headers, do not rely | |
386 * on float and 32 bit encoding symbols being defined. | |
387 * Those formats came later */ | |
388 case 0x1180: /* MPG123_ENC_SIGNED_32 */ | |
389 sh->sample_format = AF_FORMAT_S32_NE; | |
390 sh->samplesize = 4; | |
391 break; | |
392 case 0x200: /* MPG123_ENC_FLOAT_32 */ | |
393 sh->sample_format = AF_FORMAT_FLOAT_NE; | |
394 sh->samplesize = 4; | |
395 break; | |
396 default: | |
397 mp_msg(MSGT_DECAUDIO, MSGL_ERR, | |
398 "Bad encoding from mpg123: %i.\n", encoding); | |
399 mpg123_close(con->handle); | |
400 return 0; | |
401 } | |
34722 | 402 #ifdef AD_MPG123_FRAMEWISE |
403 /* Going to decode directly to MPlayer's memory. It is important | |
404 * to have MPG123_AUTO_RESAMPLE disabled for the buffer size | |
405 * being an all-time limit. */ | |
406 sh->audio_out_minsize = 1152 * 2 * sh->samplesize; | |
407 #endif | |
31524 | 408 |
409 return 1; | |
410 } else { | |
411 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 init error: %s\n", | |
412 mpg123_strerror(con->handle)); | |
413 mpg123_close(con->handle); | |
414 return 0; | |
415 } | |
416 } | |
417 | |
418 static void uninit(sh_audio_t *sh) | |
419 { | |
420 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context; | |
421 | |
422 mpg123_close(con->handle); | |
423 mpg123_delete(con->handle); | |
424 free(sh->context); | |
425 sh->context = NULL; | |
426 mpg123_exit(); | |
427 } | |
428 | |
429 #ifdef AD_MPG123_MEAN_BITRATE | |
430 /* Update mean bitrate. This could be dropped if accurate time display | |
431 * on audio file playback is not desired. */ | |
432 static void update_info(sh_audio_t *sh) | |
433 { | |
434 struct ad_mpg123_context *con = sh->context; | |
435 if (con->vbr && --con->delay < 1) { | |
436 struct mpg123_frameinfo finfo; | |
437 if (MPG123_OK == mpg123_info(con->handle, &finfo)) { | |
438 if (++con->mean_count > ((unsigned int) -1) / 2) | |
439 con->mean_count = ((unsigned int) -1) / 4; | |
440 | |
441 /* Might not be numerically optimal, but works fine enough. */ | |
442 con->mean_rate = ((con->mean_count - 1) * con->mean_rate + | |
443 finfo.bitrate) / con->mean_count; | |
444 sh->i_bps = (int) (con->mean_rate * 1000 / 8); | |
445 | |
446 con->delay = 10; | |
447 } | |
448 } | |
449 } | |
450 #endif | |
451 | |
452 static int decode_audio(sh_audio_t *sh, unsigned char *buf, int minlen, | |
453 int maxlen) | |
454 { | |
455 int bytes; | |
456 | |
34722 | 457 bytes = decode_a_bit(sh, buf, maxlen); |
31524 | 458 if (bytes == 0) |
459 return -1; /* EOF */ | |
460 | |
461 #ifdef AD_MPG123_MEAN_BITRATE | |
462 update_info(sh); | |
463 #endif | |
464 return bytes; | |
465 } | |
466 | |
467 static int control(sh_audio_t *sh, int cmd, void *arg, ...) | |
468 { | |
469 switch (cmd) { | |
470 case ADCTRL_RESYNC_STREAM: | |
471 /* Close/reopen the stream for mpg123 to make sure it doesn't | |
472 * think that it still knows the exact stream position. | |
473 * Otherwise, we would have funny effects from the gapless code. | |
474 * Oh, and it helps to minimize artifacts from jumping in the stream. */ | |
475 if (reopen_stream(sh)) { | |
476 #ifdef AD_MPG123_MEAN_BITRATE | |
477 update_info(sh); | |
478 #endif | |
479 return CONTROL_TRUE; | |
480 } else { | |
34797
079b53acda6d
Workaround bug in mpg123. In rare cases, after seeking mplayer stops audio playback and would repeat "No stream opened. (code 24)" until next seek.
iive
parents:
34722
diff
changeset
|
481 /* MPlayer ignores this case! It just keeps on decoding. |
079b53acda6d
Workaround bug in mpg123. In rare cases, after seeking mplayer stops audio playback and would repeat "No stream opened. (code 24)" until next seek.
iive
parents:
34722
diff
changeset
|
482 * So we have to make sure resync never fails ... */ |
31524 | 483 mp_msg(MSGT_DECAUDIO, MSGL_ERR, |
484 "mpg123 cannot reopen stream for resync.\n"); | |
485 return CONTROL_FALSE; | |
486 } | |
487 break; | |
488 } | |
489 return CONTROL_UNKNOWN; | |
490 } |