configure.ac: Move the encoders before the audio outputs.
[mpd-mk.git] / src / decoder_api.c
blob948ccb567655ca259279a2e44f594995a6de8a4d
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "decoder_api.h"
22 #include "decoder_internal.h"
23 #include "decoder_control.h"
24 #include "player_control.h"
25 #include "audio.h"
26 #include "song.h"
27 #include "buffer.h"
28 #include "pipe.h"
29 #include "chunk.h"
31 #include <glib.h>
33 #include <assert.h>
34 #include <stdlib.h>
36 #undef G_LOG_DOMAIN
37 #define G_LOG_DOMAIN "decoder"
39 void
40 decoder_initialized(struct decoder *decoder,
41 const struct audio_format *audio_format,
42 bool seekable, float total_time)
44 struct decoder_control *dc = decoder->dc;
45 struct audio_format_string af_string;
47 assert(dc->state == DECODE_STATE_START);
48 assert(dc->pipe != NULL);
49 assert(decoder != NULL);
50 assert(decoder->stream_tag == NULL);
51 assert(decoder->decoder_tag == NULL);
52 assert(!decoder->seeking);
53 assert(audio_format != NULL);
54 assert(audio_format_defined(audio_format));
55 assert(audio_format_valid(audio_format));
57 dc->in_audio_format = *audio_format;
58 getOutputAudioFormat(audio_format, &dc->out_audio_format);
60 dc->seekable = seekable;
61 dc->total_time = total_time;
63 decoder_lock(dc);
64 dc->state = DECODE_STATE_DECODE;
65 decoder_unlock(dc);
67 player_lock_signal();
69 g_debug("audio_format=%s, seekable=%s",
70 audio_format_to_string(&dc->in_audio_format, &af_string),
71 seekable ? "true" : "false");
73 if (!audio_format_equals(&dc->in_audio_format,
74 &dc->out_audio_format))
75 g_debug("converting to %s",
76 audio_format_to_string(&dc->out_audio_format,
77 &af_string));
80 enum decoder_command decoder_get_command(G_GNUC_UNUSED struct decoder * decoder)
82 const struct decoder_control *dc = decoder->dc;
84 assert(dc->pipe != NULL);
86 return dc->command;
89 void
90 decoder_command_finished(struct decoder *decoder)
92 struct decoder_control *dc = decoder->dc;
94 decoder_lock(dc);
96 assert(dc->command != DECODE_COMMAND_NONE);
97 assert(dc->command != DECODE_COMMAND_SEEK ||
98 dc->seek_error || decoder->seeking);
99 assert(dc->pipe != NULL);
101 if (decoder->seeking) {
102 decoder->seeking = false;
104 /* delete frames from the old song position */
106 if (decoder->chunk != NULL) {
107 music_buffer_return(dc->buffer, decoder->chunk);
108 decoder->chunk = NULL;
111 music_pipe_clear(dc->pipe, dc->buffer);
113 decoder->timestamp = dc->seek_where;
116 dc->command = DECODE_COMMAND_NONE;
117 decoder_unlock(dc);
119 player_lock_signal();
122 double decoder_seek_where(G_GNUC_UNUSED struct decoder * decoder)
124 const struct decoder_control *dc = decoder->dc;
126 assert(dc->command == DECODE_COMMAND_SEEK);
127 assert(dc->pipe != NULL);
129 decoder->seeking = true;
131 return dc->seek_where;
134 void decoder_seek_error(struct decoder * decoder)
136 struct decoder_control *dc = decoder->dc;
138 assert(dc->command == DECODE_COMMAND_SEEK);
139 assert(dc->pipe != NULL);
141 dc->seek_error = true;
142 decoder->seeking = false;
144 decoder_command_finished(decoder);
147 size_t decoder_read(struct decoder *decoder,
148 struct input_stream *is,
149 void *buffer, size_t length)
151 const struct decoder_control *dc =
152 decoder != NULL ? decoder->dc : NULL;
153 GError *error = NULL;
154 size_t nbytes;
156 assert(decoder == NULL ||
157 dc->state == DECODE_STATE_START ||
158 dc->state == DECODE_STATE_DECODE);
159 assert(is != NULL);
160 assert(buffer != NULL);
162 if (length == 0)
163 return 0;
165 while (true) {
166 /* XXX don't allow decoder==NULL */
167 if (decoder != NULL &&
168 /* ignore the SEEK command during initialization,
169 the plugin should handle that after it has
170 initialized successfully */
171 (dc->command != DECODE_COMMAND_SEEK ||
172 (dc->state != DECODE_STATE_START && !decoder->seeking)) &&
173 dc->command != DECODE_COMMAND_NONE)
174 return 0;
176 nbytes = input_stream_read(is, buffer, length, &error);
178 if (G_UNLIKELY(nbytes == 0 && error != NULL)) {
179 g_warning("%s", error->message);
180 g_error_free(error);
181 return 0;
184 if (nbytes > 0 || input_stream_eof(is))
185 return nbytes;
187 /* sleep for a fraction of a second! */
188 /* XXX don't sleep, wait for an event instead */
189 g_usleep(10000);
193 void
194 decoder_timestamp(struct decoder *decoder, double t)
196 assert(decoder != NULL);
197 assert(t >= 0);
199 decoder->timestamp = t;
203 * Sends a #tag as-is to the music pipe. Flushes the current chunk
204 * (decoder.chunk) if there is one.
206 static enum decoder_command
207 do_send_tag(struct decoder *decoder, struct input_stream *is,
208 const struct tag *tag)
210 struct music_chunk *chunk;
212 if (decoder->chunk != NULL) {
213 /* there is a partial chunk - flush it, we want the
214 tag in a new chunk */
215 decoder_flush_chunk(decoder);
216 player_lock_signal();
219 assert(decoder->chunk == NULL);
221 chunk = decoder_get_chunk(decoder, is);
222 if (chunk == NULL) {
223 assert(decoder->dc->command != DECODE_COMMAND_NONE);
224 return decoder->dc->command;
227 chunk->tag = tag_dup(tag);
228 return DECODE_COMMAND_NONE;
231 static bool
232 update_stream_tag(struct decoder *decoder, struct input_stream *is)
234 struct tag *tag;
236 tag = is != NULL
237 ? input_stream_tag(is)
238 : NULL;
239 if (tag == NULL) {
240 tag = decoder->song_tag;
241 if (tag == NULL)
242 return false;
244 /* no stream tag present - submit the song tag
245 instead */
246 decoder->song_tag = NULL;
249 if (decoder->stream_tag != NULL)
250 tag_free(decoder->stream_tag);
252 decoder->stream_tag = tag;
253 return true;
256 enum decoder_command
257 decoder_data(struct decoder *decoder,
258 struct input_stream *is,
259 const void *_data, size_t length,
260 uint16_t kbit_rate)
262 struct decoder_control *dc = decoder->dc;
263 const char *data = _data;
264 GError *error = NULL;
265 enum decoder_command cmd;
267 assert(dc->state == DECODE_STATE_DECODE);
268 assert(dc->pipe != NULL);
269 assert(length % audio_format_frame_size(&dc->in_audio_format) == 0);
271 decoder_lock(dc);
272 cmd = dc->command;
273 decoder_unlock(dc);
275 if (cmd == DECODE_COMMAND_STOP || cmd == DECODE_COMMAND_SEEK ||
276 length == 0)
277 return cmd;
279 /* send stream tags */
281 if (update_stream_tag(decoder, is)) {
282 if (decoder->decoder_tag != NULL) {
283 /* merge with tag from decoder plugin */
284 struct tag *tag;
286 tag = tag_merge(decoder->decoder_tag,
287 decoder->stream_tag);
288 cmd = do_send_tag(decoder, is, tag);
289 tag_free(tag);
290 } else
291 /* send only the stream tag */
292 cmd = do_send_tag(decoder, is, decoder->stream_tag);
294 if (cmd != DECODE_COMMAND_NONE)
295 return cmd;
298 if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) {
299 data = pcm_convert(&decoder->conv_state,
300 &dc->in_audio_format, data, length,
301 &dc->out_audio_format, &length,
302 &error);
303 if (data == NULL) {
304 /* the PCM conversion has failed - stop
305 playback, since we have no better way to
306 bail out */
307 g_warning("%s", error->message);
308 return DECODE_COMMAND_STOP;
312 while (length > 0) {
313 struct music_chunk *chunk;
314 char *dest;
315 size_t nbytes;
316 bool full;
318 chunk = decoder_get_chunk(decoder, is);
319 if (chunk == NULL) {
320 assert(dc->command != DECODE_COMMAND_NONE);
321 return dc->command;
324 dest = music_chunk_write(chunk, &dc->out_audio_format,
325 decoder->timestamp -
326 dc->song->start_ms / 1000.0,
327 kbit_rate, &nbytes);
328 if (dest == NULL) {
329 /* the chunk is full, flush it */
330 decoder_flush_chunk(decoder);
331 player_lock_signal();
332 continue;
335 assert(nbytes > 0);
337 if (nbytes > length)
338 nbytes = length;
340 /* copy the buffer */
342 memcpy(dest, data, nbytes);
344 /* expand the music pipe chunk */
346 full = music_chunk_expand(chunk, &dc->out_audio_format, nbytes);
347 if (full) {
348 /* the chunk is full, flush it */
349 decoder_flush_chunk(decoder);
350 player_lock_signal();
353 data += nbytes;
354 length -= nbytes;
356 decoder->timestamp += (double)nbytes /
357 audio_format_time_to_size(&dc->out_audio_format);
359 if (dc->song->end_ms > 0 &&
360 decoder->timestamp >= dc->song->end_ms / 1000.0)
361 /* the end of this range has been reached:
362 stop decoding */
363 return DECODE_COMMAND_STOP;
366 return DECODE_COMMAND_NONE;
369 enum decoder_command
370 decoder_tag(G_GNUC_UNUSED struct decoder *decoder, struct input_stream *is,
371 const struct tag *tag)
373 G_GNUC_UNUSED const struct decoder_control *dc = decoder->dc;
374 enum decoder_command cmd;
376 assert(dc->state == DECODE_STATE_DECODE);
377 assert(dc->pipe != NULL);
378 assert(tag != NULL);
380 /* save the tag */
382 if (decoder->decoder_tag != NULL)
383 tag_free(decoder->decoder_tag);
384 decoder->decoder_tag = tag_dup(tag);
386 /* check for a new stream tag */
388 update_stream_tag(decoder, is);
390 /* send tag to music pipe */
392 if (decoder->stream_tag != NULL) {
393 /* merge with tag from input stream */
394 struct tag *merged;
396 merged = tag_merge(decoder->stream_tag, decoder->decoder_tag);
397 cmd = do_send_tag(decoder, is, merged);
398 tag_free(merged);
399 } else
400 /* send only the decoder tag */
401 cmd = do_send_tag(decoder, is, tag);
403 return cmd;
406 void
407 decoder_replay_gain(struct decoder *decoder,
408 const struct replay_gain_info *replay_gain_info)
410 assert(decoder != NULL);
412 if (replay_gain_info != NULL) {
413 static unsigned serial;
414 if (++serial == 0)
415 serial = 1;
417 decoder->replay_gain_info = *replay_gain_info;
418 decoder->replay_gain_serial = serial;
420 if (decoder->chunk != NULL) {
421 /* flush the current chunk because the new
422 replay gain values affect the following
423 samples */
424 decoder_flush_chunk(decoder);
425 player_lock_signal();
427 } else
428 decoder->replay_gain_serial = 0;
431 void
432 decoder_mixramp(struct decoder *decoder,
433 char *mixramp_start, char *mixramp_end)
435 assert(decoder != NULL);
436 struct decoder_control *dc = decoder->dc;
437 assert(dc != NULL);
439 dc_mixramp_start(dc, mixramp_start);
440 dc_mixramp_end(dc, mixramp_end);