annotate libmpcodecs/ad_mpg123.c @ 36595:145d72a9f950

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