/*
 * 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.h,v 1.17 2004/01/23 09:41:32 rob Exp $
 */

#pragma once
#include "Mp3Frame.h"
#include "Mp3Stream.h"
#include "Mp3Synth.h"


constexpr char MP3_DATA_ERROR[] = "mp3 data error";


namespace kilipili::Audio
{

enum Mp3FlowCtl {
	MAD_FLOW_CONTINUE = 0x0000, // continue normally
	MAD_FLOW_STOP	  = 0x0010, // stop decoding file
	MAD_FLOW_BREAK	  = 0x0011, // stop decoding and signal an error: throw Error instead!
	MAD_FLOW_IGNORE	  = 0x0020	// ignore and skip the current frame
};

struct Mp3Decoder
{
	using input_fu	 = Mp3FlowCtl (*)(void*, Mp3Stream*);
	using header_fu	 = Mp3FlowCtl (*)(void*, const Mp3Header*);
	using filter_fu	 = Mp3FlowCtl (*)(void*, const Mp3Stream*, Mp3Frame*);
	using output_fu	 = Mp3FlowCtl (*)(void*, const Mp3Header*, PcmBuffer*);
	using error_fu	 = Mp3FlowCtl (*)(void*, Mp3Stream*, Mp3Frame*);
	using message_fu = Mp3FlowCtl (*)(void*, void*, uint*);

	Mp3Decoder(void* callback_data, input_fu, header_fu, filter_fu, output_fu, error_fu, message_fu) noexcept;
	~Mp3Decoder() noexcept = default;

	Error run() noexcept;
	void  setOptions(int opts) noexcept { options = opts; }

	int options {0};

	struct Sync
	{
		Mp3Stream stream;
		Mp3Frame  frame;
		Mp3Synth  synth;
	};
	Sync* sync {nullptr};

	void* cb_data;

	Mp3FlowCtl (*input_func)(void*, Mp3Stream*);
	Mp3FlowCtl (*header_func)(void*, const Mp3Header*);
	Mp3FlowCtl (*filter_func)(void*, const Mp3Stream*, Mp3Frame*);
	Mp3FlowCtl (*output_func)(void*, const Mp3Header*, PcmBuffer*);
	Mp3FlowCtl (*error_func)(void*, Mp3Stream*, Mp3Frame*);
	Mp3FlowCtl (*message_func)(void*, void*, uint*);

private:
	void run_sync();
};


} // namespace kilipili::Audio
