#ifndef AUDIOPIPELINE_AUDIO_DEMUXER_MP3_H__INCLUDED
#define AUDIOPIPELINE_AUDIO_DEMUXER_MP3_H__INCLUDED

#include <stdbool.h>
#include "audio.h"

/**
 * Creates a demuxer element for MP3 files
 *
 * This demuxer just searches for MP3 sync markers, decodes the header to calculate frame length
 * and then will forward the MP3 data to the next element in the chain in packets of 10 frames.
 * The maximum size of data buffered before calling the decoder is around 14 kB.
 * The theoretical maximum decoded data from this packet is 11520 samples which could be
 * up to 45kB of data and between 0.96 and 1.44 seconds of audio playback time.
 *
 * Formula: FrameSize = (144 * BitRate / SampleRate) + PaddingBit
 *
 * @returns Initialized `AudioPipelineElement` that can be used in call to `audio_pipeline_assemble`
 */
AudioPipelineElement *audio_demuxer_mp3(void);


typedef enum {
    MPEGVersion2_5 = 0,
    MPEGVersionReserved = 1,
    MPEGVersion2 = 2,
    MPEGVersion1 = 3
} MPEGVersion;

typedef enum {
    MPEGLayerReserved = 0,
    MPEGLayer3 = 1,
    MPEGLayer2 = 2,
    MPEGLayer1 = 3
} MPEGLayer;

typedef enum {
    MPEGChannelModeStereo = 0,
    MPEGChannelModeJointStereo = 1,
    MPEGChannelModeDualChannel = 2,
    MPEGChannelModeMono = 3
} MPEGChannelMode;

typedef enum {
    MPEGEmphasisNone = 0,
    MPEGEmphasis50_15 = 1,
    MPEGEmphasisReserved = 2,
    MPEGEmphasisCCITJ17 = 3
} MPEGEmphasis;

typedef struct _MP3Header {
    MPEGVersion version;
    MPEGLayer layer;
    uint8_t bitrateIndex;
    uint8_t sampleRateIndex;
    MPEGChannelMode channelMode;
    uint8_t jointStereoModeExtension;
    MPEGEmphasis emphasis;

    bool has_crc;
    bool has_padding;
    bool has_copyright;
    bool is_original;
    bool is_private;

    bool valid;
    uint32_t bitrate;
    uint32_t sampleRate;
    uint16_t packetLength;
} MP3Header;

MP3Header demuxer_mp3_decode_header(uint8_t data[4]);

#endif /* AUDIOPIPELINE_AUDIO_DEMUXER_MP3_H__INCLUDED */