merge down from merged-API-layer branch: cvs -q up -dP -j API_LAYER_MERGING_BASELINE...
[flac.git] / src / plugin_winamp2 / playback.c
1 /* in_flac - Winamp2 FLAC input plugin\r
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson\r
3  *\r
4  * This program is free software; you can redistribute it and/or\r
5  * modify it under the terms of the GNU General Public License\r
6  * as published by the Free Software Foundation; either version 2\r
7  * of the License, or (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program; if not, write to the Free Software\r
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
17  */\r
18 \r
19 #if HAVE_CONFIG_H\r
20 #  include <config.h>\r
21 #endif\r
22 \r
23 #include <limits.h> /* for INT_MAX */\r
24 #include <stdlib.h>\r
25 #include <string.h> /* for memmove() */\r
26 #include "playback.h"\r
27 #include "share/grabbag.h"\r
28 \r
29 \r
30 static FLAC__int32 reservoir_[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS][FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/];\r
31 static FLAC__int32 *reservoir__[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] = { reservoir_[0], reservoir_[1] }; /*@@@ kind of a hard-coded hack */\r
32 static unsigned wide_samples_in_reservoir_;\r
33 static output_config_t cfg;     /* local copy */\r
34 \r
35 static unsigned bh_index_last_w, bh_index_last_o, written_time_last;\r
36 static FLAC__int64 decode_position, decode_position_last;\r
37 \r
38 /*\r
39  *  callbacks\r
40  */\r
41 \r
42 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)\r
43 {\r
44         stream_data_struct *stream_data = (stream_data_struct*)client_data;\r
45         const unsigned channels = stream_data->channels, wide_samples = frame->header.blocksize;\r
46         unsigned channel;\r
47 \r
48         (void)decoder;\r
49 \r
50         if (stream_data->abort_flag)\r
51                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;\r
52 \r
53         for (channel = 0; channel < channels; channel++)\r
54                 memcpy(&reservoir_[channel][wide_samples_in_reservoir_], buffer[channel], sizeof(buffer[0][0]) * wide_samples);\r
55 \r
56         wide_samples_in_reservoir_ += wide_samples;\r
57 \r
58         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;\r
59 }\r
60 \r
61 static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)\r
62 {\r
63         stream_data_struct *stream_data = (stream_data_struct*)client_data;\r
64         (void)decoder;\r
65 \r
66         if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)\r
67         {\r
68                 stream_data->total_samples = metadata->data.stream_info.total_samples;\r
69                 stream_data->bits_per_sample = metadata->data.stream_info.bits_per_sample;\r
70                 stream_data->channels = metadata->data.stream_info.channels;\r
71                 stream_data->sample_rate = metadata->data.stream_info.sample_rate;\r
72 \r
73                 if (stream_data->bits_per_sample!=8 && stream_data->bits_per_sample!=16 && stream_data->bits_per_sample!=24)\r
74                 {\r
75                         FLAC_plugin__show_error("This plugin can only handle 8/16/24-bit samples.");\r
76                         stream_data->abort_flag = true;\r
77                         return;\r
78                 }\r
79 \r
80                 {\r
81                         /* with VC++ you have to spoon feed it the casting from uint64->int64->double */\r
82                         FLAC__uint64 l = (FLAC__uint64)((double)(FLAC__int64)stream_data->total_samples / (double)stream_data->sample_rate * 1000.0 + 0.5);\r
83                         if (l > INT_MAX)\r
84                                 l = INT_MAX;\r
85                         stream_data->length_in_msec = (int)l;\r
86                 }\r
87         }\r
88         else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)\r
89         {\r
90                 double gain, peak;\r
91                 if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, /*strict=*/false, &gain, &peak))\r
92                 {\r
93                         stream_data->has_replaygain = true;\r
94                         stream_data->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);\r
95                 }\r
96         }\r
97 }\r
98 \r
99 static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)\r
100 {\r
101         stream_data_struct *stream_data = (stream_data_struct*)client_data;\r
102         (void)decoder;\r
103 \r
104         if (cfg.misc.stop_err || status!=FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)\r
105                 stream_data->abort_flag = true;\r
106 }\r
107 \r
108 /*\r
109  *  init/delete\r
110  */\r
111 \r
112 FLAC__bool FLAC_plugin__decoder_init(FLAC__StreamDecoder *decoder, const char *filename, FLAC__int64 filesize, stream_data_struct *stream_data, output_config_t *config)\r
113 {\r
114         FLAC__StreamDecoderInitStatus init_status;\r
115 \r
116         FLAC__ASSERT(decoder);\r
117         FLAC_plugin__decoder_finish(decoder);\r
118         /* init decoder */\r
119         FLAC__stream_decoder_set_md5_checking(decoder, false);\r
120         FLAC__stream_decoder_set_metadata_ignore_all(decoder);\r
121         FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);\r
122         FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);\r
123 \r
124         if ((init_status = FLAC__stream_decoder_init_file(decoder, filename, write_callback, metadata_callback, error_callback, /*client_data=*/stream_data)) != FLAC__STREAM_DECODER_INIT_STATUS_OK)\r
125         {\r
126                 FLAC_plugin__show_error("Error while initializing decoder (%s [%s]).", FLAC__StreamDecoderInitStatusString[init_status], FLAC__stream_decoder_get_resolved_state_string(decoder));\r
127                 return false;\r
128         }\r
129         /* process */\r
130         cfg = *config;\r
131         wide_samples_in_reservoir_ = 0;\r
132         stream_data->is_playing = false;\r
133         stream_data->abort_flag = false;\r
134         stream_data->has_replaygain = false;\r
135 \r
136         if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder))\r
137         {\r
138                 FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__stream_decoder_get_resolved_state_string(decoder));\r
139                 return false;\r
140         }\r
141         /* check results */\r
142         if (stream_data->abort_flag) return false;                /* metadata callback already popped up the error dialog */\r
143         /* init replaygain */\r
144         stream_data->output_bits_per_sample = stream_data->has_replaygain && cfg.replaygain.enable ?\r
145                 cfg.resolution.replaygain.bps_out :\r
146                 cfg.resolution.normal.dither_24_to_16 ? min(stream_data->bits_per_sample, 16) : stream_data->bits_per_sample;\r
147 \r
148         if (stream_data->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)\r
149                 FLAC__replaygain_synthesis__init_dither_context(&stream_data->dither_context, stream_data->bits_per_sample, cfg.resolution.replaygain.noise_shaping);\r
150         /* more inits */\r
151         stream_data->eof = false;\r
152         stream_data->seek_to = -1;\r
153         stream_data->is_playing = true;\r
154         stream_data->average_bps = (unsigned)(filesize / (125.*(double)(FLAC__int64)stream_data->total_samples/(double)stream_data->sample_rate));\r
155         \r
156         bh_index_last_w = 0;\r
157         bh_index_last_o = BITRATE_HIST_SIZE;\r
158         decode_position = 0;\r
159         decode_position_last = 0;\r
160         written_time_last = 0;\r
161 \r
162         return true;\r
163 }\r
164 \r
165 void FLAC_plugin__decoder_finish(FLAC__StreamDecoder *decoder)\r
166 {\r
167         if (decoder && FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED)\r
168                 FLAC__stream_decoder_finish(decoder);\r
169 }\r
170 \r
171 void FLAC_plugin__decoder_delete(FLAC__StreamDecoder *decoder)\r
172 {\r
173         if (decoder)\r
174         {\r
175                 FLAC_plugin__decoder_finish(decoder);\r
176                 FLAC__stream_decoder_delete(decoder);\r
177         }\r
178 }\r
179 \r
180 /*\r
181  *  decode\r
182  */\r
183 \r
184 int FLAC_plugin__seek(FLAC__StreamDecoder *decoder, stream_data_struct *stream_data)\r
185 {\r
186         int pos;\r
187         FLAC__uint64 target_sample = stream_data->total_samples * stream_data->seek_to / stream_data->length_in_msec;\r
188 \r
189         if (stream_data->total_samples > 0 && target_sample >= stream_data->total_samples && target_sample > 0)\r
190                 target_sample = stream_data->total_samples - 1;\r
191 \r
192         /* even if the seek fails we have to reset these so that we don't repeat the seek */\r
193         stream_data->seek_to = -1;\r
194         stream_data->eof = false;\r
195         wide_samples_in_reservoir_ = 0;\r
196         pos = (int)(target_sample*1000 / stream_data->sample_rate);\r
197 \r
198         if (!FLAC__stream_decoder_seek_absolute(decoder, target_sample)) {\r
199                 if(FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_SEEK_ERROR)\r
200                         FLAC__stream_decoder_flush(decoder);\r
201                 pos = -1;\r
202         }\r
203 \r
204         bh_index_last_o = bh_index_last_w = (pos/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;\r
205         if (!FLAC__stream_decoder_get_decode_position(decoder, &decode_position))\r
206                 decode_position = 0;\r
207 \r
208         return pos;\r
209 }\r
210 \r
211 unsigned FLAC_plugin__decode(FLAC__StreamDecoder *decoder, stream_data_struct *stream_data, char *sample_buffer)\r
212 {\r
213         /* fill reservoir */\r
214         while (wide_samples_in_reservoir_ < SAMPLES_PER_WRITE)\r
215         {\r
216                 if (FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)\r
217                 {\r
218                         stream_data->eof = true;\r
219                         break;\r
220                 }\r
221                 else if (!FLAC__stream_decoder_process_single(decoder))\r
222                 {\r
223                         FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__stream_decoder_get_resolved_state_string(decoder));\r
224                         stream_data->eof = true;\r
225                         break;\r
226                 }\r
227                 if (!FLAC__stream_decoder_get_decode_position(decoder, &decode_position))\r
228                         decode_position = 0;\r
229         }\r
230         /* output samples */\r
231         if (wide_samples_in_reservoir_ > 0)\r
232         {\r
233                 const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);\r
234                 const unsigned channels = stream_data->channels;\r
235                 unsigned i;\r
236                 int bytes;\r
237 \r
238                 if (cfg.replaygain.enable && stream_data->has_replaygain)\r
239                 {\r
240                         bytes = FLAC__replaygain_synthesis__apply_gain(\r
241                                 sample_buffer,\r
242                                 true, /* little_endian_data_out */\r
243                                 stream_data->output_bits_per_sample == 8, /* unsigned_data_out */\r
244                                 reservoir__,\r
245                                 n,\r
246                                 channels,\r
247                                 stream_data->bits_per_sample,\r
248                                 stream_data->output_bits_per_sample,\r
249                                 stream_data->replay_scale,\r
250                                 cfg.replaygain.hard_limit,\r
251                                 cfg.resolution.replaygain.dither,\r
252                                 &stream_data->dither_context\r
253                         );\r
254                 }\r
255                 else\r
256                 {\r
257                         bytes = FLAC__plugin_common__pack_pcm_signed_little_endian(\r
258                                 sample_buffer,\r
259                                 reservoir__,\r
260                                 n,\r
261                                 channels,\r
262                                 stream_data->bits_per_sample,\r
263                                 stream_data->output_bits_per_sample\r
264                         );\r
265                 }\r
266 \r
267                 wide_samples_in_reservoir_ -= n;\r
268                 for (i = 0; i < channels; i++)\r
269                         memmove(&reservoir_[i][0], &reservoir_[i][n], sizeof(reservoir_[0][0]) * wide_samples_in_reservoir_);\r
270 \r
271                 return bytes;\r
272         }\r
273         else\r
274         {\r
275                 stream_data->eof = true;\r
276                 return 0;\r
277         }\r
278 }\r
279 \r
280 int FLAC_plugin__get_rate(unsigned written_time, unsigned output_time, stream_data_struct *stream_data)\r
281 {\r
282         static int bitrate_history_[BITRATE_HIST_SIZE];\r
283         unsigned bh_index_w = (written_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;\r
284         unsigned bh_index_o = (output_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;\r
285 \r
286         /* written bitrate */\r
287         if (bh_index_w != bh_index_last_w)\r
288         {\r
289                 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE-1)%BITRATE_HIST_SIZE] =\r
290                         decode_position>decode_position_last && written_time > written_time_last ?\r
291                         (unsigned)(8000*(decode_position - decode_position_last)/(written_time - written_time_last)) :\r
292                         stream_data->average_bps;\r
293 \r
294                 bh_index_last_w = bh_index_w;\r
295                 written_time_last = written_time;\r
296                 decode_position_last = decode_position;\r
297         }\r
298 \r
299         /* output bitrate */\r
300         if (bh_index_o!=bh_index_last_o && bh_index_o!=bh_index_last_w)\r
301         {\r
302                 bh_index_last_o = bh_index_o;\r
303                 return bitrate_history_[bh_index_o];\r
304         }\r
305 \r
306         return 0;\r
307 }\r