/*
#
# header for MPEG2Parser class
#
# $Id: MPEG2Parser.h,v 1.9 2003/08/22 03:54:31 nemies Exp $
#
# Copyright (C) 2001-2003 Kees Cook
# kees@outflux.net, http://outflux.net/
# 
# 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.
# http://www.gnu.org/copyleft/gpl.html
#
*/

#ifndef _MPEG2PARSER_H_
#define _MPEG2PARSER_H_

#include "GOPchop.h"
// FIXME: shouldn't I use config.h macros somewhere?
// open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// everything
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
// mmap
#include <sys/mman.h>
// strlen
#include <string.h>

#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif

#include "Parser.h"
#include "List.h"

#include "mpeg2structs.h"

#include "Pack.h"
#include "GroupOfPictures.h"
#include "ElementStream.h"



// Markers
extern uint8_t stuffing_byte[1];
extern uint8_t sequence_start_code[4];
extern uint8_t picture_start_code[4];
extern uint8_t group_start_code[4];
extern uint8_t MPEG_program_end_code[4];
extern uint8_t pack_start_code[4];
extern uint8_t system_header_start_code[4];
extern uint8_t packet_start_code_prefix[3];

// picture frame types
extern char *frame_type[4];

class MPEG2Parser:public Parser
{
  public:
    MPEG2Parser();
    virtual ~ MPEG2Parser();

    // bind to parent
    int parse(char ignore_errors = FALSE);

    // MPEG2
    List *getGOPs();
    List *getPackets();
    List *getVideo();
    List *getAudio();
    List *getPictures();

    uint32_t numGOPs();

    // searches a vector for a certain byte string
    Vector *findCode(Vector * area, uint8_t * code, size_t size);
  protected:
    // MPEG2
    List * GOP_list;          // all GOPs in MPEG2

    // functional parsing globals
    off_t pack_start;           // where packet started
    off_t GOP_start;            // where GOP started
    off_t picture_start;        // where picture started

    List *packet_list;          // All packets in MPEG2
    List *picture_list;         // All pictures in MPEG2
    List *video_list;           // All video ES fragments
    List *audio_list;           // All audio ES fragments

    // section parsers
    void parse_PES_packet(void);
    void parse_system_header(void);
    void parse_pack_header(void);
    void parse_pack(void);
    void parse_MPEG2_program_stream(void);

    void buildGOPs(void);
};

#endif // _MPEG2PARSER_H_

/* vi:set ai ts=4 sw=4 expandtab: */
