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