/*
 * Emunation Plugin for MD-API 
 *
 * This code 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 code 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.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */
 
#include "pmtparser.h"
#include "tools.h"
#include "dvbsectiondata.h"

#include "..\\main.h"
#include "misc.h"
//#define WRITE(x){m_DLLInstance.x;}
#define WRITE(x){;}

cPMTParser::cPMTParser()
:cDataParser()
{
}

cPMTParser::cPMTParser(unsigned char* buffer, int length)
:cDataParser(buffer,length)
{
}

cPMTParser::~cPMTParser()
{
}
char* cPMTParser::GetStreamType(int type)
{
    switch(type){
        case 0x00:  return "ITU-T | ISO-IE Reserved";
        case 0x01:  return "ISO/IEC 11172 Video";
        case 0x02:  return " ISO/IEC 13818-2 Video ";
        case 0x03:  return "ISO/IEC 11172 Audio";
        case 0x04:  return "ISO/IEC 13818-3 Audio";
        default:
            return "Unknown stream type!";
    };
}
void cPMTParser::GetMPEG_VideoStream(unsigned char* b)
{
    cDVBVideoStreamDescriptor d;
    d.descriptor_tag		 = b[0];
    d.descriptor_length       	 = b[1];

    d.multiple_frame_rate_flag	 = GetBits ( 0, 16, 1);
    d.frame_rate_code		 = GetBits ( 0, 17, 4);
    d.MPEG_1_only_flag		 = GetBits ( 0, 21, 1);
    d.constrained_parameter_flag	 = GetBits ( 0, 22, 1);
    d.still_picture_flag		 = GetBits ( 0, 23, 1);
    
    if (d.MPEG_1_only_flag == 1) 
    {
        d.profile_and_level_indication	= GetBits ( 0, 24, 8);
        d.chroma_format			= GetBits ( 0, 32, 2);
        d.frame_rate_extension_flag		= GetBits ( 0, 34, 1);
        d.reserved_1			= GetBits ( 0, 35, 5);
    }
}

void cPMTParser::GetMPEG_AudioStream(unsigned char* b)
{
    cDVBAudioStreamDescriptor d;


    d.descriptor_tag		 = b[0];
    d.descriptor_length       	 = b[1];

    d.free_format_flag		 = GetBits ( 0, 16, 1);
    d.ID				 = GetBits ( 0, 17, 1);
    d.layer			 = GetBits ( 0, 18, 2);
    d.variable_rate_audio_indicator = GetBits ( 0, 20, 1);
    d.reserved_1			 = GetBits ( 0, 21, 3);
}

int cPMTParser::GetProvID(int ca_sys_id,unsigned char* priv_data,int len)
{
    int prov_id=0;
    switch(ca_sys_id)
    {
        case 0x0100://Seca
            WRITE(Log("Seca len:%d\n",len));
            prov_id=(priv_data[0]<<8)|priv_data[1];
        break;
        case 0x0500://Via
            prov_id=(priv_data[len-3]<<16)|(priv_data[len-2]<<8)|0x00;
        break;
        default:    
            prov_id=0;
        break;
    }
    return prov_id;
    
}

int cPMTParser::GetMPEG_CA(unsigned char* b)
{

    cMPEGCADescriptor d;
    int     len;
    d.descriptor_tag = b[0];
    d.descriptor_length = b[1];
    d.CA_system_ID = GetBits ( 0, 16, 16);
    d.reserved = GetBits ( 0, 32, 3);
    d.CA_PID = GetBits ( 0, 35, 13);
    len = d.descriptor_length-4;

//    WRITE(Log("CA System ID:%d PID:%d\n",d.CA_system_ID,d.CA_PID));
    if (len > 0) {
        char * txt= new char[len*2+1];
        HexStr(txt,b+6,len);
       WRITE(Log("ID:%d Pid%d: Data:%s\n",d.CA_system_ID,d.CA_PID,txt));
        delete txt;
    }

    d.prov_id=GetProvID(d.CA_system_ID,b+6,len);
    if(m_data)
    {
        bool isExist=false;
        cPMT* pmt_data=(cPMT*)m_data;
        for(int i=0;i<m_data->Count();i++)
            if(d.Compare(*(cMPEGCADescriptor*)pmt_data->Get(i))==0)
            {
                isExist=true;
                break;
            }
        if(!isExist)
        {
            m_data->Add(new cMPEGCADescriptor(d.CA_system_ID,d.CA_PID,d.prov_id));
        }
    }
    
    if(d.CA_system_ID==0x0100&&m_data&&len>13)
    {
        //Seca
        int dlen=len-13;
        unsigned char* b1=b+19;
        while(dlen>0)
        {
            d.CA_system_ID=0x0100;
            d.CA_PID=(b1[0]<<8|b1[1])&0x0FFF;
            d.prov_id=b1[2]<<8|b1[3];
            dlen-=15;
            b1+=15;
            if(d.CA_PID==0)continue;
            {
                bool isExist=false;
                cPMT* pmt_data=(cPMT*)m_data;
                for(int i=0;i<m_data->Count();i++)
                    if(d.Compare(*(cMPEGCADescriptor*)pmt_data->Get(i))==0)
                    {
                        isExist=true;
                        break;
                    }
                if(!isExist)
                {
                    m_data->Add(new cMPEGCADescriptor(d.CA_system_ID,d.CA_PID,d.prov_id));
                }
            }
        }
        
    }
    return len;
}
int cPMTParser::GetMPEGDescriptor(unsigned char* b)
{
    int len;
    int id;
    id  =  (int) b[0];
    len = ((int) b[1]) + 2;
    if (b[1] == 0)
        return len;  
    //HEX Print
    switch (b[0]) 
    {
        case 0x02:  GetMPEG_VideoStream  (b);  break;
        //case 0x03:  GetMPEG_AudioStream  (b);  break;
        case 0x09:  GetMPEG_CA  (b);  break;
        default:
         break;
    } 

  return len;   // (descriptor total length)
}
int cPMTParser::GetDescriptor(unsigned char* b, int type)
{
    int len;
    int id;
    id  =  (int)b[0];  
    len = ((int)b[1]) + 2;
    switch(type)
    {
        
        case 0:
        default:
            if(id<0x40)GetMPEGDescriptor(b);
        break;
    }
    return len;
}
bool cPMTParser::Parse()
{
    if(m_buf)
    {
        if(m_data) delete m_data;
        cPMT* p=new cPMT();
        m_data=p;

        cPMT_ProgramInfoList p2;
        int  len1,len2; 
        p->table_id 			 = m_buf[0];
        p->section_syntax_indicator	 = GetBits ( 0, 8, 1);
        p->b_null			 = GetBits ( 0, 9, 1);
        p->reserved_1 			 = GetBits ( 0, 10, 2);
        p->section_length		 = GetBits ( 0, 12, 12);
        p->program_number		 = GetBits ( 0, 24, 16);
        p->reserved_2 			 = GetBits ( 0, 40, 2);
        p->version_number 		 = GetBits ( 0, 42, 5);
        p->current_next_indicator	 = GetBits ( 0, 47, 1);
        p->section_number 		 = GetBits ( 0, 48, 8);
        p->last_section_number 		 = GetBits ( 0, 56, 8);
        p->reserved_3	 		 = GetBits ( 0, 64, 3);
        p->pcr_pid	 		 = GetBits ( 0, 67, 13);
        p->reserved_4	 		 = GetBits ( 0, 80, 4);
        p->program_info_length 		 = GetBits ( 0, 84, 12);

        if (p->table_id != 0x02) 
        {
            return false;
        }

        len1 = p->section_length - 9;
        m_buf   += 9+3;
        len2 = p->program_info_length;

        while (len2 > 0) {
            int x;
            x = GetDescriptor (m_buf, 0);
            len2 -= x;
            m_buf += x;
            len1 -= x;
        }

        while (len1 > 4) 
        {
            p2.stream_type		 = GetBits ( 0,  0,  8);
            p2.reserved_1		 = GetBits ( 0,  8,  3);
            p2.elementary_PID		 = GetBits ( 0, 11, 13);
            p2.reserved_2		 = GetBits ( 0, 24,  4);
            p2.ES_info_length		 = GetBits ( 0, 28, 12);
            m_buf    += 5;
            len1 -= 5;
            len2 = p2.ES_info_length;
            while (len2 > 0) 
            {
                int x;
                x = GetDescriptor (m_buf, 0);
                len2 -= x;
                len1 -= x;
                m_buf += x;
            }

        } // while len1
        return true;
    }
    return false;
}


