⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plugin.c

📁 这是著名的TCPMP播放器在WINDWOWS,和WINCE下编译通过的源程序.笔者对其中的LIBMAD库做了针对ARM MPU的优化. 并增加了词幕功能.
💻 C
📖 第 1 页 / 共 2 页
字号:

void FLAC_XMMS__cleanup()
{
	decoder_func_table_ -> safe_decoder_delete(decoder_);
	decoder_ = 0;
}

void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
{
	FLAC__StreamMetadata streaminfo;

	if(0 == filename)
		filename = "";

	if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
		/* @@@ how to report the error? */
		if(title) {
			if (source_to_decoder_type (filename) == DECODER_FILE) {
				static const char *errtitle = "Invalid FLAC File: ";
				*title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
				sprintf(*title, "%s\"%s\"", errtitle, filename);
			} else {
				*title = NULL;
			}
		}
		if(length_in_msec)
			*length_in_msec = -1;
		return;
	}

	if(title) {
		*title = flac_format_song_title(filename);
	}
	if(length_in_msec)
		*length_in_msec = (unsigned)((double)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5);
}

/***********************************************************************
 * local routines
 **********************************************************************/

void *play_loop_(void *arg)
{
	unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
	FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;

	(void)arg;

	while(file_info_.is_playing) {
		if(!file_info_.eof) {
			while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
				unsigned s;

				s = sample_buffer_last_ - sample_buffer_first_;
				if(decoder_func_table_ -> is_eof(decoder_)) {
					file_info_.eof = true;
					break;
				}
				else if (!decoder_func_table_ -> process_single(decoder_)) {
					/*@@@ this should probably be a dialog */
					fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
					file_info_.eof = true;
					break;
				}
				blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
				decode_position_frame_last = decode_position_frame;
				if(!decoder_func_table_->seekable || !FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
					decode_position_frame = 0;
			}
			if(sample_buffer_last_ - sample_buffer_first_ > 0) {
				const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
				int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
				FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
				unsigned written_time, bh_index_w;
				FLAC__uint64 decode_position;

				sample_buffer_first_ += n;
				flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
				while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
					xmms_usleep(10000);
				if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
					flac_ip.output->write_audio(sample_buffer_start, bytes);

				/* compute current bitrate */

				written_time = flac_ip.output->written_time();
				bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
				if(bh_index_w != bh_index_last_w) {
					bh_index_last_w = bh_index_w;
					decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
					bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
						decode_position > decode_position_last && written_time > written_time_last ?
							8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
							file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
					decode_position_last = decode_position;
					written_time_last = written_time;
				}
			}
			else {
				file_info_.eof = true;
				xmms_usleep(10000);
			}
		}
		else
			xmms_usleep(10000);
		if(decoder_func_table_->seekable && file_info_.seek_to_in_sec != -1) {
			const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
			unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
			if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
				flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
				bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
				if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
					decode_position_frame = 0;
				file_info_.seek_to_in_sec = -1;
				file_info_.eof = false;
				sample_buffer_first_ = sample_buffer_last_ = 0;
			}
		}
		else {
			/* display the right bitrate from history */
			unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
			if(bh_index_o != bh_index_last_o && bh_index_o != bh_index_last_w && bh_index_o != (bh_index_last_w + 1) % BITRATE_HIST_SIZE) {
				bh_index_last_o = bh_index_o;
				flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
			}
		}
	}

	decoder_func_table_ -> safe_decoder_finish(decoder_);

	/* are these two calls necessary? */
	flac_ip.output->buffer_free();
	flac_ip.output->buffer_free();

	g_free(file_info_.title);

	pthread_exit(NULL);
	return 0; /* to silence the compiler warning about not returning a value */
}

/*********** File decoder functions */

static FLAC__bool file_decoder_init (void *decoder)
{
	return FLAC__file_decoder_init( (FLAC__FileDecoder*) decoder) == FLAC__FILE_DECODER_OK;
}

static void file_decoder_safe_decoder_finish_(void *decoder)
{
	if(decoder && FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
		FLAC__file_decoder_finish((FLAC__FileDecoder *) decoder);
}

static void file_decoder_safe_decoder_delete_(void *decoder)
{
	if(decoder) {
		file_decoder_safe_decoder_finish_(decoder);
		FLAC__file_decoder_delete( (FLAC__FileDecoder *) decoder);
	}
}

static FLAC__bool file_decoder_is_eof(void *decoder)
{
	return FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) == FLAC__FILE_DECODER_END_OF_FILE;
}

static const decoder_funcs_t FILE_DECODER_FUNCTIONS = {
	true,
	(void* (*) (void)) FLAC__file_decoder_new,
	(FLAC__bool (*) (void *, FLAC__bool)) FLAC__file_decoder_set_md5_checking,
	(FLAC__bool (*) (void *, const char*)) FLAC__file_decoder_set_filename,
	(FLAC__bool (*) (void *)) FLAC__file_decoder_set_metadata_ignore_all,
	(FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__file_decoder_set_metadata_respond,
	(FLAC__bool (*) (void *, WriteCallback)) FLAC__file_decoder_set_write_callback,
	(FLAC__bool (*) (void *, MetadataCallback)) FLAC__file_decoder_set_metadata_callback,
	(FLAC__bool (*) (void *, ErrorCallback)) FLAC__file_decoder_set_error_callback,
	(FLAC__bool (*) (void *, void *)) FLAC__file_decoder_set_client_data,
	(FLAC__bool (*) (void *)) file_decoder_init,
	(void (*) (void *)) file_decoder_safe_decoder_finish_,
	(void (*) (void *)) file_decoder_safe_decoder_delete_,
	(FLAC__bool (*) (void *)) FLAC__file_decoder_process_until_end_of_metadata,
	(FLAC__bool (*) (void *)) FLAC__file_decoder_process_single,
	file_decoder_is_eof
};

/*********** HTTP decoder functions */

static gchar *url_;

static FLAC__bool http_decoder_set_md5_checking (void *decoder, FLAC__bool value)
{
	(void) value;
	// operation unsupported
	return FLAC__stream_decoder_get_state ((const FLAC__StreamDecoder *) decoder) ==
		FLAC__STREAM_DECODER_UNINITIALIZED;
}

static FLAC__bool http_decoder_set_url (void *decoder, const char* url)
{
	(void) decoder;
	url_ = g_strdup (url);
	return true;
}

static FLAC__StreamDecoderReadStatus http_decoder_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
{
	(void) decoder;
	(void) client_data;
	*bytes = flac_http_read (buffer, *bytes);
	return *bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}

static FLAC__bool http_decoder_init (void *decoder)
{
	flac_http_open (url_, 0);
	g_free (url_);
	FLAC__stream_decoder_set_read_callback (decoder, http_decoder_read_callback);
	return FLAC__stream_decoder_init( (FLAC__StreamDecoder*) decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
}

static void http_decoder_safe_decoder_finish_(void *decoder)
{
	if(decoder && FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
		FLAC__stream_decoder_finish((FLAC__StreamDecoder *) decoder);
		flac_http_close();
	}
}

static void http_decoder_safe_decoder_delete_(void *decoder)
{
	if(decoder) {
		http_decoder_safe_decoder_finish_(decoder);
		FLAC__stream_decoder_delete( (FLAC__StreamDecoder *) decoder);
	}
}

static FLAC__bool http_decoder_is_eof(void *decoder)
{
	return FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) == FLAC__STREAM_DECODER_END_OF_STREAM;
}

static const decoder_funcs_t HTTP_DECODER_FUNCTIONS = {
	false,
	(void* (*) (void)) FLAC__stream_decoder_new,
	http_decoder_set_md5_checking,
	(FLAC__bool (*) (void *, const char*)) http_decoder_set_url,
	(FLAC__bool (*) (void *)) FLAC__stream_decoder_set_metadata_ignore_all,
	(FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__stream_decoder_set_metadata_respond,
	(FLAC__bool (*) (void *, WriteCallback)) FLAC__stream_decoder_set_write_callback,
	(FLAC__bool (*) (void *, MetadataCallback)) FLAC__stream_decoder_set_metadata_callback,
	(FLAC__bool (*) (void *, ErrorCallback)) FLAC__stream_decoder_set_error_callback,
	(FLAC__bool (*) (void *, void *)) FLAC__stream_decoder_set_client_data,
	(FLAC__bool (*) (void *)) http_decoder_init,
	(void (*) (void *)) http_decoder_safe_decoder_finish_,
	(void (*) (void *)) http_decoder_safe_decoder_delete_,
	(FLAC__bool (*) (void *)) FLAC__stream_decoder_process_until_end_of_metadata,
	(FLAC__bool (*) (void *)) FLAC__stream_decoder_process_single,
	http_decoder_is_eof
};

static decoder_funcs_t const *decoder_func_table_;

static void init_decoder_func_tables()
{
	DECODER_FUNCS [DECODER_FILE] = & FILE_DECODER_FUNCTIONS;
	DECODER_FUNCS [DECODER_HTTP] = & HTTP_DECODER_FUNCTIONS;
}

static decoder_t source_to_decoder_type (const char *source)
{
	return strncasecmp(source, "http://", 7) ? DECODER_FILE : DECODER_HTTP;
}

static void change_decoder_if_needed (decoder_t new_decoder_type, void **decoderp, decoder_funcs_t const ** fntabp)
{
	const decoder_funcs_t *new_fn_table = DECODER_FUNCS [new_decoder_type];
	if (*fntabp != new_fn_table) {
		(*fntabp)->safe_decoder_delete(*decoderp);
		*fntabp = new_fn_table;
		*decoderp = new_fn_table -> new_decoder();
	}
}

FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fntabp)
{
	if(decoderp == 0 || *decoderp == 0)
		return false;

	(*fntabp)->safe_decoder_finish(*decoderp);

	change_decoder_if_needed(source_to_decoder_type(filename), decoderp, fntabp);

	{
		decoder_funcs_t const *fntab = *fntabp;
		void *decoder = *decoderp;

		decoder = *decoderp;
		fntab = *fntabp;

		fntab -> set_md5_checking(decoder, false);
		fntab -> set_source(decoder, filename);
		fntab -> set_metadata_ignore_all(decoder);
		fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
		fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
		fntab -> set_write_callback(decoder, write_callback_);
		fntab -> set_metadata_callback(decoder, metadata_callback_);
		fntab -> set_error_callback(decoder, error_callback_);
		fntab -> set_client_data(decoder, &file_info_);
		if(!fntab -> decoder_init(decoder))
			return false;

		if(!fntab -> process_until_end_of_metadata(decoder))
			return false;
	}

	return true;
}

FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
	file_info_struct *file_info = (file_info_struct *)client_data;
	const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
	const unsigned bits_per_sample = file_info->bits_per_sample;
	FLAC__byte *sample_buffer_start;

	(void)decoder;

	if(file_info->abort_flag)
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

	if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
		memmove(sample_buffer_, sample_buffer_ + sample_buffer_first_ * channels * file_info->sample_format_bytes_per_sample, (sample_buffer_last_ - sample_buffer_first_) * channels * file_info->sample_format_bytes_per_sample);
		sample_buffer_last_ -= sample_buffer_first_;
		sample_buffer_first_ = 0;
	}
	sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
	if(file_info->has_replaygain && flac_cfg.output.replaygain.enable) {
		FLAC__replaygain_synthesis__apply_gain(
				sample_buffer_start,
				!is_big_endian_host_,
				file_info->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
				buffer,
				wide_samples,
				channels,
				bits_per_sample,
				file_info->sample_format_bytes_per_sample * 8,
				file_info->replay_scale,
				flac_cfg.output.replaygain.hard_limit,
				flac_cfg.output.resolution.replaygain.dither,
				&file_info->dither_context
		);
	}
	else if(is_big_endian_host_) {
		FLAC__plugin_common__pack_pcm_signed_big_endian(
			sample_buffer_start,
			buffer,
			wide_samples,
			channels,
			bits_per_sample,
			file_info->sample_format_bytes_per_sample * 8
		);
	}
	else {
		FLAC__plugin_common__pack_pcm_signed_little_endian(
			sample_buffer_start,
			buffer,
			wide_samples,
			channels,
			bits_per_sample,
			file_info->sample_format_bytes_per_sample * 8
		);
	}

	sample_buffer_last_ += wide_samples;

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
	file_info_struct *file_info = (file_info_struct *)client_data;
	(void)decoder;
	if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
		FLAC__ASSERT(metadata->data.stream_info.total_samples < FLAC__U64L(0x100000000)); /* this plugin can only handle < 4 gigasamples */
		file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
		file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
		file_info->channels = metadata->data.stream_info.channels;
		file_info->sample_rate = metadata->data.stream_info.sample_rate;
		file_info->length_in_msec = (unsigned)((double)file_info->total_samples / (double)file_info->sample_rate * 1000.0 + 0.5);
	}
	else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
		double gain, peak;
		if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
			file_info->has_replaygain = true;
			file_info->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)flac_cfg.output.replaygain.preamp, /*prevent_clipping=*/!flac_cfg.output.replaygain.hard_limit);
		}
	}
}

void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
	file_info_struct *file_info = (file_info_struct *)client_data;
	(void)decoder;
	if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
		file_info->abort_flag = true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -