annotate libmpcodecs/ad_mpg123.c @ 34173:a5d8b198c214

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