/*
 * mpeg2.h
 * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
 *
 * mpeg2dec 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.
 *
 * mpeg2dec 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
 *
 * Modifications for this RISCOS port, Copyright (c) 2002 P.Everett
 * <peter@everett9981.freeserve.co.uk>
 */

#ifndef MPEG2_H
#define MPEG2_H

#include "inttypes.h"
#include "ka_error.h"

#define SEQ_FLAG_MPEG2 1
#define SEQ_FLAG_CONSTRAINED_PARAMETERS 2
#define SEQ_FLAG_PROGRESSIVE_SEQUENCE 4
#define SEQ_FLAG_LOW_DELAY 8
#define SEQ_FLAG_COLOUR_DESCRIPTION 16

#define SEQ_MASK_VIDEO_FORMAT 0xe0
#define SEQ_VIDEO_FORMAT_COMPONENT 0
#define SEQ_VIDEO_FORMAT_PAL 0x20
#define SEQ_VIDEO_FORMAT_NTSC 0x40
#define SEQ_VIDEO_FORMAT_SECAM 0x60
#define SEQ_VIDEO_FORMAT_MAC 0x80
#define SEQ_VIDEO_FORMAT_UNSPECIFIED 0xa0

// chroma_format
#define SEQ_CHROMA_420 1
#define SEQ_CHROMA_422 2
#define SEQ_CHROMA_444 3

typedef struct mpeg2_sequence_s {
    uint32_t width, height;                 // size rounded to upper block size
    uint32_t chroma_width, chroma_height;   // corresponding chroma size
    uint32_t chroma_wshift, chroma_hshift;  // mappping between lumi and chroma
    uint32_t chroma_type;                   // chroma format
    uint32_t byte_rate;                     // in 1/400 Kb/s
    uint32_t vbv_buffer_size;               // min. chunk buffer size in bytes
    uint32_t flags;                         // flags

    uint32_t picture_width, picture_height; // primary display size
    uint32_t display_width, display_height; // secondary display size
    uint32_t pixel_width, pixel_height;     // aspect ratio
    uint32_t frame_period;                  // in 1/(90 KHz)

    uint8_t profile_level_id;
    uint8_t colour_primaries;
    uint8_t transfer_characteristics;
    uint8_t matrix_coefficients;
} mpeg2_sequence_t;

#define PIC_MASK_CODING_TYPE 7
#define PIC_FLAG_CODING_TYPE_I 1
#define PIC_FLAG_CODING_TYPE_P 2
#define PIC_FLAG_CODING_TYPE_B 3
#define PIC_FLAG_CODING_TYPE_D 4

#define PIC_FLAG_TOP_FIELD_FIRST 8
#define PIC_FLAG_PROGRESSIVE_FRAME 16
#define PIC_FLAG_COMPOSITE_DISPLAY 32
#define PIC_FLAG_SKIP 64
#define PIC_FLAG_PTS 128
#define PIC_MASK_COMPOSITE_DISPLAY 0xfffff000

typedef struct
{
	uint8_t*	y;
	uint8_t*	cb;
	uint8_t*	cr;
} yuv_bufs;

typedef struct mpeg2_info_s {
    mpeg2_sequence_t sequence;
    uint32_t         sequence_ref; // increases with sequence changes
} mpeg2_info_t;

typedef struct mpeg2dec_s mpeg2dec_t;

typedef struct
{
    yuv_bufs base;              // pointer to 3 planes (y,u,v)

    int coding_type;            // D/I/P/B_TYPE
    int temporal_reference;
    uint32_t pts;               // time stamp
    uint32_t duration;          // delay till next frame (cf. repeats)
    int display_type;           // 0 bottom first, 1 top first, 2 both fields
    int valid;                  // 0 if dropped
    int inuse;                  // 0 if in use by decoder/player
    uint32_t sequence_ref;      // increasing signaling change in sequence info
    uint32_t section;           // kinoamp section
} mpeg2_frame_t;

mpeg2dec_t * mpeg2_init (ka_error_t* pErrorBlock, uint32_t hardware);
const mpeg2_info_t * mpeg2_info (mpeg2dec_t * mpeg2dec);
void mpeg2_close (mpeg2dec_t * mpeg2dec);

#define MPEG2_OPT_MONOCHROME 0x01
void mpeg2_setoptions(mpeg2dec_t* mpeg2dec, int32_t mask, int32_t value);

void mpeg2_decode_reset(mpeg2dec_t* mpeg2dec);

#define MPEG2_DECODE_BLOCKEND 0
#define MPEG2_DECODE_NEW_SEQUENCE -1
#define MPEG2_DECODE_WAIT_FREEFRAME -2
#define MPEG2_DECODE_START_FIRST_PICTURE -3
#define MPEG2_DECODE_START_SECOND_PICTURE -4
#define MPEG2_DECODE_END_FRAME -5
#define MPEG2_DECODE_ERROR -6

int mpeg2_decode_videoblock(mpeg2dec_t* mpeg2dec, ka_error_t* pErrorBlock
			, const uint8_t** data_start, const uint8_t* data_end, uint32_t pts);
int mpeg2_decode_complete(mpeg2dec_t* mpeg2dec, ka_error_t* pErrorBlock);

mpeg2_frame_t* mpeg2_currentframe(mpeg2dec_t* mpeg2dec);
mpeg2_frame_t* mpeg2_displayframe(mpeg2dec_t* mpeg2dec);
void mpeg2_releaseframe(mpeg2dec_t * mpeg2dec, mpeg2_frame_t* frame);

void* mpeg2_malloc(ka_error_t* pErrorBlock, unsigned size);
void* mpeg2_calloc(ka_error_t* pErrorBlock, unsigned size);
void mpeg2_free (void * buf);

#endif /* MPEG2_H */
