/*
   CLE266 MPEG-2 decoder

   (c) Copyright 2003 Andreas Robinson

   All rights reserved.

   Written by Andreas Robinson <andro134 at student.liu.se>
              Colin Paton <ac2crp at blueyonder.co.uk>
              Laurence Abbott <laz at club-burniston.co.uk>
              Martin Wache <wachm at users.berlios.de>
              Mark Adams <mark147m at gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   This library 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
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#ifndef CLE266MPEGDEC_H
#define CLE266MPEGDEC_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

#define LIBCLE266MPEGDEC_VERSION_INT ((0<<8)+(5<<0))
#define LIBCLE266MPEGDEC_VERSION     0.5

    typedef struct
    {
        /* sequence information */
        int width;                  /* width */
        int height;                 /* height */
        int aspect_ratio_info;      /* aspect ratio */
        int progressive_sequence;   /* no interlaced frames this sequence */
        int frame_duration;         /* 90000 / frame_rate */

        /* picture information for the frame to be displayed */
        int dtg_active_format;      /* dtg active format */
        int progressive_frame;      /* non-zero if frame is progressive */
        int top_field_first;        /* field order of interlaced frame */
        int repeat_first_field;     /* non-zero requests field be repeated */
    } cle266_decoder_state_t;

    /**
     * Must be called at startup - mmaps IO registers etc.
     * 
     * @param device Used frame buffer device
     * 
     * @return '1' if all ok, else '0'
     */
    int CLE266MPEGInitialise(const char *device);

    /**
     * Set bytes/line in the framebuffer
     * 
     * @param y_stride  Bytes per line, luminosity.
     * @param uv_stride Bytes per line, chroma. Typically half of y_stride
     */
    void CLE266MPEGSetStride(int y_stride,
        int uv_stride);

    /**
     * Set the framebuffer
     * 
     * @param buffer Framebuffer number, 0 - 3
     * @param y_off  Start of Y plane
     * @param u_off  Start of U plane
     * @param v_off  Start of V plane
     */
    void CLE266MPEGSetFrameBuffer(int buffer,
        unsigned int y_off,
        unsigned int u_off,
        unsigned int v_off);

    /*
     * Determine the currently unused framebuffer
     */
    int CLE266MPEGGetIdleFrameBuffer(void);

    /**
     * Ask the decoder to skip some frames
     * 
     * @param count  Number of frames to skip
     */
    void CLE266MPEGSkipFrames(int count);

    /**
     * Reset the decoder
     */
    void CLE266MPEGReset(void);

    /**
     * Close the decoder - call at exit
     */
    void CLE266MPEGClose(void);

    /**
     * Pass elementary stream data to the decoder.
     * 
     * @param buffer Stream data buffer
     * @param length The number of bytes decoded is returned
     * @param pts    pts input: the pts which corresponds to the
     *               frame which starts in *buffer
     *               pts output: the pts which corresponds to the
     *               decoded frame
     * 
     * @return '-1' if no picture present, or an integer '0-3'
     *         indicating which framebuffer contains the newly
     *         decoded picture if a picture is present
     */
    int CLE266MPEGDecodeData(unsigned char* buffer,
        int* length,
        int64_t *pts);

    /**
     * Return the decoder state
     * 
     * @return Decoder state struct
     */
    cle266_decoder_state_t CLE266MPEGGetDecoderState(void);

#ifdef __cplusplus
}
#endif

#endif /* CLE266MPEGDEC_H */

