annotate libmpcodecs/ad_mpg123.c @ 31705:41638ecd6025

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