/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef _NMM_MPEGCHECKFORMAT_HPP_
#define _NMM_MPEGCHECKFORMAT_HPP_

#include "nmm/nmm_prefix.hpp"

#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef __FreeBSD__
typedef uint8_t u_int8_t;
typedef uint16_t u_int16_t;
typedef uint32_t u_int32_t;
#endif

struct mpeg_file_format_type {
    bool   pes_stream;
    bool   mpeg_audio;
    bool   ac3_audio;
    bool   mpeg2_video;
    bool   mpeg1_video;
    int    width;
    int    height;
    int    frame_rate_code;
    double frame_rate;
    int    ratio_x;
    int    ratio_y;
};

typedef struct mpeg_file_format_type mpeg_file_format;

namespace NMM {
  class MPEGCheckFormat {

  protected:
    int INIT_BUFFER_SIZE;

    u_int8_t code;
    u_int8_t chunk_buffer[65536];
    u_int8_t *chunk_ptr;
    u_int32_t shift;
    char *aspect_ratio_information_str[8];
    char *frame_rate_str[16];
    int mpeg1_skip_table[16];

    int max_packets;
    bool found_pes_stream;
    bool found_mpeg_audio;
    bool found_ac3_audio;
    bool found_mpeg2_video;
    bool found_mpeg1_video;

    bool found_sequence;
    int horizontal_size;
    int vertical_size;
    int aspect_ratio_information;
    int frame_rate_code;

    // check and synchronize mpeg2 video packet
    void mpeg2_info(u_int8_t *current, u_int8_t *end);

    // if "code" is a sequence_header_code then format will be extracted
    // this function is used by mpeg2_info
    void check_sequence(int code, u_int8_t * buffer);

    // zu ueberladende Funktion, um Buffer zu fuellen
    virtual int MPEG_Read_Buffer( unsigned char *buf, int size ) = 0;

    // zu ueberladende Funktion, um an Anfangsposition zu springen
    virtual void MPEG_Seek_Begin() = 0;

  public:

    // reads max_packets from a mpeg2 program stream file and tries to find out the audio and video format
    bool CheckFormat( mpeg_file_format *form );
    void NumPackets( int p ) { max_packets = p; }
    void SetBufferSize( int s ) { INIT_BUFFER_SIZE = s; }


    MPEGCheckFormat();
    virtual ~MPEGCheckFormat();
  };
}

#include "nmm/nmm_suffix.hpp"

#endif
