/*
 * libmad - MPEG audio decoder library
 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: decoder.c,v 1.22 2004/01/23 09:41:32 rob Exp $
 */

//#include <new>
//#ifdef HAVE_CONFIG_H
//  #include "config.h"
//#endif
//
//#include "global.h"
//
//#ifdef HAVE_SYS_TYPES_H
//  #include <sys/types.h>
//#endif
//
//#ifdef HAVE_SYS_WAIT_H
//  #include <sys/wait.h>
//#endif
//
//#ifdef HAVE_UNISTD_H
//  #include <unistd.h>
//#endif
//
//#ifdef HAVE_FCNTL_H
//  #include <fcntl.h>
//#endif
//
//#include <stdlib.h>
//
//#ifdef HAVE_ERRNO_H
//  #include <errno.h>
//#endif

#include "Mp3Decoder.h"
#include "Mp3Frame.h"
#include "Mp3Stream.h"
#include "Mp3Synth.h"
#include "common/Trace.h"
#include "common/cdefs.h"
#include "common/standard_types.h"
#include <new>

namespace kilipili::Audio
{

Mp3Decoder::Mp3Decoder(
	void* cb_data, input_fu i, header_fu h, filter_fu f, output_fu o, error_fu e, message_fu m) noexcept :
	cb_data(cb_data),
	input_func(i),
	header_func(h),
	filter_func(f),
	output_func(o),
	error_func(e),
	message_func(m)
{
	assert(i); // input function is required
	assert(o); // output function is required
}

static Mp3FlowCtl error_default(void* data, Mp3Stream* stream, Mp3Frame* frame)
{
	int* bad_last_frame = intptr(data);

	switch (stream->error)
	{
	case MAD_ERROR_BADCRC:
		if (*bad_last_frame) frame->mute();
		else *bad_last_frame = 1;

		return MAD_FLOW_IGNORE;

	default: return MAD_FLOW_CONTINUE;
	}
}

void Mp3Decoder::run_sync()
{
	trace(__func__);

	error_fu error_func;
	void*	 error_data;
	int		 bad_last_frame = 0;

	if (this->error_func)
	{
		error_func = this->error_func;
		error_data = this->cb_data;
	}
	else
	{
		error_func = error_default;
		error_data = &bad_last_frame;
	}

	Mp3Stream* stream = &this->sync->stream;
	Mp3Frame*  frame  = &this->sync->frame;
	Mp3Synth*  synth  = &this->sync->synth;

	mad_stream_options(stream, this->options);

	do {
		switch (this->input_func(this->cb_data, stream))
		{
		case MAD_FLOW_STOP: return;
		case MAD_FLOW_BREAK: throw MP3_DATA_ERROR;
		case MAD_FLOW_IGNORE: continue;
		case MAD_FLOW_CONTINUE: break;
		}

		while (1)
		{
			if (this->header_func)
			{
				if (mad_header_decode(&frame->header, stream) == -1)
				{
					if (!MAD_RECOVERABLE(stream->error)) throw "mp3: non-recoverable data error";

					switch (error_func(error_data, stream, frame))
					{
					case MAD_FLOW_STOP: return;
					case MAD_FLOW_BREAK: throw MP3_DATA_ERROR;
					case MAD_FLOW_IGNORE:
					case MAD_FLOW_CONTINUE:
					default: continue;
					}
				}

				switch (this->header_func(this->cb_data, &frame->header))
				{
				case MAD_FLOW_STOP: return;
				case MAD_FLOW_BREAK: throw MP3_DATA_ERROR;
				case MAD_FLOW_IGNORE: continue;
				case MAD_FLOW_CONTINUE: break;
				}
			}

			if (mad_frame_decode(frame, stream) == -1)
			{
				if (!MAD_RECOVERABLE(stream->error)) throw "mp3: non-recoverable data error";

				switch (error_func(error_data, stream, frame))
				{
				case MAD_FLOW_STOP: return;
				case MAD_FLOW_BREAK: throw MP3_DATA_ERROR;
				case MAD_FLOW_IGNORE: break;
				case MAD_FLOW_CONTINUE:
				default: continue;
				}
			}
			else bad_last_frame = 0;

			if (this->filter_func)
			{
				switch (this->filter_func(this->cb_data, stream, frame))
				{
				case MAD_FLOW_STOP: return;
				case MAD_FLOW_BREAK: throw MP3_DATA_ERROR;
				case MAD_FLOW_IGNORE: continue;
				case MAD_FLOW_CONTINUE: break;
				}
			}

			synth->synthesizeFrame(frame);

			if (this->output_func)
			{
				switch (this->output_func(this->cb_data, &frame->header, &synth->pcm))
				{
				case MAD_FLOW_STOP: return;
				case MAD_FLOW_BREAK: throw MP3_DATA_ERROR;
				case MAD_FLOW_IGNORE:
				case MAD_FLOW_CONTINUE: break;
				}
			}
		}
	}
	while (stream->error == MAD_ERROR_BUFLEN);

	throw "MP3 unexpected stream->error";
}

Error Mp3Decoder::run() noexcept
{
	trace(__func__);

	sync = new (std::nothrow) Sync;
	if (sync == nullptr) return OUT_OF_MEMORY;

	cstr result = nullptr;

	try
	{
		run_sync();
	}
	catch (Error e)
	{
		result = e;
	}

	delete sync;
	sync = nullptr;

	return result;
}

} // namespace kilipili::Audio

/*
































*/
