major plugin revamp based on x-fixer's code
[flac.git] / src / plugin_winamp2 / playback.c
1 /* in_flac - Winamp2 FLAC input plugin\r
2  * Copyright (C) 2000,2001,2002,2003,2004  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 #include <stdlib.h>\r
20 #include "playback.h"\r
21 #include "share/grabbag.h"\r
22 \r
23 \r
24 static FLAC__int32 reservoir_[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS][FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/];\r
25 static FLAC__int32 *reservoir__[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] = { reservoir_[0], reservoir_[1] }; /*@@@ kind of a hard-coded hack */\r
26 static unsigned wide_samples_in_reservoir_;\r
27 static output_config_t cfg;     /* local copy */\r
28 \r
29 static unsigned bh_index_last_w, bh_index_last_o, written_time_last;\r
30 static FLAC__int64 decode_position, decode_position_last;\r
31 \r
32 /*\r
33  *  callbacks\r
34  */\r
35 \r
36 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)\r
37 {\r
38         file_info_struct *file_info = (file_info_struct*)client_data;\r
39         const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;\r
40         unsigned channel;\r
41 \r
42         (void)decoder;\r
43 \r
44         if (file_info->abort_flag)\r
45                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;\r
46 \r
47         for (channel = 0; channel < channels; channel++)\r
48                 memcpy(&reservoir_[channel][wide_samples_in_reservoir_], buffer[channel], sizeof(buffer[0][0]) * wide_samples);\r
49 \r
50         wide_samples_in_reservoir_ += wide_samples;\r
51 \r
52         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;\r
53 }\r
54 \r
55 static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)\r
56 {\r
57         file_info_struct *file_info = (file_info_struct*)client_data;\r
58         (void)decoder;\r
59 \r
60         if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)\r
61         {\r
62                 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */\r
63                 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xfffffffful);\r
64                 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;\r
65                 file_info->channels = metadata->data.stream_info.channels;\r
66                 file_info->sample_rate = metadata->data.stream_info.sample_rate;\r
67 \r
68                 if (file_info->bits_per_sample!=8 && file_info->bits_per_sample!=16 && file_info->bits_per_sample!=24)\r
69                 {\r
70                         FLAC_plugin__show_error("This plugin can only handle 8/16/24-bit samples.");\r
71                         file_info->abort_flag = true;\r
72                         return;\r
73                 }\r
74                 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);\r
75         }\r
76         else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)\r
77         {\r
78                 double gain, peak;\r
79                 if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, &gain, &peak))\r
80                 {\r
81                         file_info->has_replaygain = true;\r
82                         file_info->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);\r
83                 }\r
84         }\r
85 }\r
86 \r
87 static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)\r
88 {\r
89         file_info_struct *file_info = (file_info_struct*)client_data;\r
90         (void)decoder;\r
91 \r
92         if (cfg.misk.stop_err || status!=FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)\r
93                 file_info->abort_flag = true;\r
94 }\r
95 \r
96 /*\r
97  *  init/delete\r
98  */\r
99 \r
100 FLAC__bool FLAC_plugin__decoder_init(FLAC__FileDecoder *decoder, const char *filename, FLAC__int64 filesize, file_info_struct *file_info, output_config_t *config)\r
101 {\r
102         FLAC__ASSERT(decoder);\r
103         FLAC_plugin__decoder_finish(decoder);\r
104         /* init decoder */\r
105         FLAC__file_decoder_set_md5_checking(decoder, false);\r
106         FLAC__file_decoder_set_filename(decoder, filename);\r
107         FLAC__file_decoder_set_metadata_ignore_all(decoder);\r
108         FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);\r
109         FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);\r
110         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback);\r
111         FLAC__file_decoder_set_write_callback(decoder, write_callback);\r
112         FLAC__file_decoder_set_error_callback(decoder, error_callback);\r
113         FLAC__file_decoder_set_client_data(decoder, file_info);\r
114 \r
115         if (FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)\r
116         {\r
117                 FLAC_plugin__show_error("Error while initializing decoder (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);\r
118                 return false;\r
119         }\r
120         /* process */\r
121         cfg = *config;\r
122         wide_samples_in_reservoir_ = 0;\r
123         file_info->is_playing = false;\r
124         file_info->abort_flag = false;\r
125         file_info->has_replaygain = false;\r
126 \r
127         if (!FLAC__file_decoder_process_until_end_of_metadata(decoder))\r
128         {\r
129                 FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);\r
130                 return false;\r
131         }\r
132         /* check results */\r
133         if (file_info->abort_flag) return false;                /* metadata callback already popped up the error dialog */\r
134         /* init replaygain */\r
135         file_info->output_bits_per_sample = file_info->has_replaygain && cfg.replaygain.enable ?\r
136                 cfg.resolution.replaygain.bps_out :\r
137                 cfg.resolution.normal.dither_24_to_16 ? min(file_info->bits_per_sample, 16) : file_info->bits_per_sample;\r
138 \r
139         if (file_info->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)\r
140                 FLAC__replaygain_synthesis__init_dither_context(&file_info->dither_context, file_info->bits_per_sample, cfg.resolution.replaygain.noise_shaping);\r
141         /* more inits */\r
142         file_info->eof = false;\r
143         file_info->seek_to = -1;\r
144         file_info->is_playing = true;\r
145         file_info->average_bps = (unsigned)(filesize / (125.*file_info->total_samples/file_info->sample_rate));\r
146         \r
147         bh_index_last_w = 0;\r
148         bh_index_last_o = BITRATE_HIST_SIZE;\r
149         decode_position = 0;\r
150         decode_position_last = 0;\r
151         written_time_last = 0;\r
152 \r
153         return true;\r
154 }\r
155 \r
156 void FLAC_plugin__decoder_finish(FLAC__FileDecoder *decoder)\r
157 {\r
158         if (decoder && FLAC__file_decoder_get_state(decoder)!=FLAC__FILE_DECODER_UNINITIALIZED)\r
159                 FLAC__file_decoder_finish(decoder);\r
160 }\r
161 \r
162 void FLAC_plugin__decoder_delete(FLAC__FileDecoder *decoder)\r
163 {\r
164         if (decoder)\r
165         {\r
166                 FLAC_plugin__decoder_finish(decoder);\r
167                 FLAC__file_decoder_delete(decoder);\r
168         }\r
169 }\r
170 \r
171 /*\r
172  *  decode\r
173  */\r
174 \r
175 int FLAC_plugin__seek(FLAC__FileDecoder *decoder, file_info_struct *file_info)\r
176 {\r
177         int pos;\r
178         const FLAC__uint64 target_sample =\r
179                 (FLAC__uint64)file_info->total_samples*file_info->seek_to / file_info->length_in_msec;\r
180 \r
181         if (!FLAC__file_decoder_seek_absolute(decoder, target_sample))\r
182                 return -1;\r
183 \r
184         file_info->seek_to = -1;\r
185         file_info->eof = false;\r
186         wide_samples_in_reservoir_ = 0;\r
187         pos = (int)(target_sample*1000 / file_info->sample_rate);\r
188 \r
189         bh_index_last_o = bh_index_last_w = (pos/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;\r
190         if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))\r
191                 decode_position = 0;\r
192 \r
193         return pos;\r
194 }\r
195 \r
196 unsigned FLAC_plugin__decode(FLAC__FileDecoder *decoder, file_info_struct *file_info, char *sample_buffer)\r
197 {\r
198         /* fill reservoir */\r
199         while (wide_samples_in_reservoir_ < SAMPLES_PER_WRITE)\r
200         {\r
201                 if (FLAC__file_decoder_get_state(decoder) == FLAC__FILE_DECODER_END_OF_FILE)\r
202                 {\r
203                         file_info->eof = true;\r
204                         break;\r
205                 }\r
206                 else if (!FLAC__file_decoder_process_single(decoder))\r
207                 {\r
208                         FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);\r
209                         file_info->eof = true;\r
210                         break;\r
211                 }\r
212                 if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))\r
213                         decode_position = 0;\r
214         }\r
215         /* output samples */\r
216         if (wide_samples_in_reservoir_ > 0)\r
217         {\r
218                 const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);\r
219                 const unsigned channels = file_info->channels;\r
220                 unsigned i;\r
221                 int bytes;\r
222 \r
223                 if (cfg.replaygain.enable && file_info->has_replaygain)\r
224                 {\r
225                         bytes = FLAC__replaygain_synthesis__apply_gain(\r
226                                 sample_buffer,\r
227                                 true, /* little_endian_data_out */\r
228                                 file_info->output_bits_per_sample == 8, /* unsigned_data_out */\r
229                                 reservoir__,\r
230                                 n,\r
231                                 channels,\r
232                                 file_info->bits_per_sample,\r
233                                 file_info->output_bits_per_sample,\r
234                                 file_info->replay_scale,\r
235                                 cfg.replaygain.hard_limit,\r
236                                 cfg.resolution.replaygain.dither,\r
237                                 &file_info->dither_context\r
238                         );\r
239                 }\r
240                 else\r
241                 {\r
242                         bytes = FLAC__plugin_common__pack_pcm_signed_little_endian(\r
243                                 sample_buffer,\r
244                                 reservoir__,\r
245                                 n,\r
246                                 channels,\r
247                                 file_info->bits_per_sample,\r
248                                 file_info->output_bits_per_sample\r
249                         );\r
250                 }\r
251 \r
252                 wide_samples_in_reservoir_ -= n;\r
253                 for (i = 0; i < channels; i++)\r
254                         memmove(&reservoir_[i][0], &reservoir_[i][n], sizeof(reservoir_[0][0]) * wide_samples_in_reservoir_);\r
255 \r
256                 return bytes;\r
257         }\r
258         else\r
259         {\r
260                 file_info->eof = true;\r
261                 return 0;\r
262         }\r
263 }\r
264 \r
265 int FLAC_plugin__get_rate(unsigned written_time, unsigned output_time, file_info_struct *file_info)\r
266 {\r
267         static int bitrate_history_[BITRATE_HIST_SIZE];\r
268         unsigned bh_index_w = (written_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;\r
269         unsigned bh_index_o = (output_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;\r
270 \r
271         /* written bitrate */\r
272         if (bh_index_w != bh_index_last_w)\r
273         {\r
274                 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE-1)%BITRATE_HIST_SIZE] =\r
275                         decode_position>decode_position_last && written_time > written_time_last ?\r
276                         (unsigned)(8000*(decode_position - decode_position_last)/(written_time - written_time_last)) :\r
277                         file_info->average_bps;\r
278 \r
279                 bh_index_last_w = bh_index_w;\r
280                 written_time_last = written_time;\r
281                 decode_position_last = decode_position;\r
282         }\r
283 \r
284         /* output bitrate */\r
285         if (bh_index_o!=bh_index_last_o && bh_index_o!=bh_index_last_w)\r
286         {\r
287                 bh_index_last_o = bh_index_o;\r
288                 return bitrate_history_[bh_index_o];\r
289         }\r
290 \r
291         return 0;\r
292 }\r