home *** CD-ROM | disk | FTP | other *** search
- /*
- * StreamDetector.cpp: implementation of the CStreamDetector class.
- *
- * Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
- *
- * This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
- *
- * FlasKMPEG 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, or (at your option)
- * any later version.
- *
- * FlasKMPEG 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 GNU Make; see the file COPYING. If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- */
- #include <windows.h>
- #include <commctrl.h>
- #include "..\Misc\selectordialog.h"
- #include "StreamDetector.h"
- #include "..\flaskmpeg.h"
- #include "..\runstate.h"
- #include "..\resource.h"
- #include "..\error.h"
- #include "..\demux\demux.h"
-
- #define BtoMB(A) ((double)A/1048576)
-
- extern TRunState rs;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- CStreamDetector::CStreamDetector()
- {
- inp=NULL;
- }
-
- CStreamDetector::~CStreamDetector()
- {
- if(inp)
- delete inp;
- }
-
-
-
- #define SWAP_INT32(x) ((((ui8*)&x)[0] << 24) | \
- (((ui8*)&x)[1] << 16) | \
- (((ui8*)&x)[2] << 8) | \
- ((ui8*)&x)[3])
- #define INPUT_NOEXIST 1
- #define IS_TRANSPORTSTREAM 2
- #define NOT_RECOGNIZED 3
- #define NO_AUDIOVIDEO 4
- #define PACK_START_CODE 0x000001BA
-
- int CStreamDetector::RecognizeType()
- {
- ui32 i;
-
- if(!inp)
- return INPUT_NOEXIST;
-
- inp->SetStreamPos(0);
- //Recognize type
- inp->Read((ui8 *)&i, 4); //Read first 32 bits
-
- if (SWAP_INT32(i) != PACK_START_CODE)
- {
- DebugPrint("File is not an MPEG Program Stream",0);
- return NOT_RECOGNIZED;
- }
-
-
- /* //Rewind file
- SeekFirst();
- if(!GetStreams())
- return NO_AUDIOVIDEO;
- */
- return 0; //Everything OK
-
- }
-
-
- extern HINSTANCE hInst;
- extern HWND hMainWnd;
- HWND hDlgParser, hSelectStream;
- unsigned int videoFound, audioFound;
- TStreamDetector *gSt;
- fpos_t pos;
-
- LRESULT CALLBACK DlgParser(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
-
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
-
-
-
- int CStreamDetector::GetStreams(TStreamDetector *st, int lurk_size)
- {
- int porciento,i;
- char szTemp[256];
- unsigned char *dummy;
- PESinfo PES;
- bool stream_ids[256], substream_ids[256];
- TStreamIdentifier mySt;
-
- gSt = st;
-
- if(!st)
- return 0;
-
- hDlgParser= CreateDialog(hInst,(LPCTSTR)(IDD_PARSER), hMainWnd ,(DLGPROC)DlgParser );
- ShowWindow(hDlgParser, SW_SHOW);
-
- CDemux *demux=NULL;
- demux=new CDemux();
- if(demux){
- if(!demux->SetInput(inp)){
- delete demux;
- return 0;
- }
- }
- else
- return 0;
-
-
- //Initialize Tstream detector
- for(i=0;i<256;i++){
- stream_ids[i]= false;
- substream_ids[i]= false;
- }
-
- st->n_audio_streams =0;
- st->n_subpic_streams=0;
- st->n_video_streams =0;
- st->audio =NULL;
- st->video =NULL;
- st->subpic =NULL;
-
- st->video_selected=0;
- for(i=0; i<MAX_AUDIO_STREAMS; i++)
- st->audio_selected[i]=0;
- st->subpic_selected=0;
-
-
-
- //START
- inp->SetStreamPos(0);
-
-
- pos=inp->GetStreamPos();
-
-
- while(pos<(lurk_size*1024) && demux->ReadPES(&dummy,&PES)){
- if(stream_ids[PES.streamID]==false){ //if this stream is a new one
- if(PES.streamID >= 0xE0 && PES.streamID <= 0xEF){ //if it was a video stream
- mySt.aux =0;
- mySt.streamID =PES.streamID;
- mySt.subStreamID =0;
- sprintf(szTemp, "MPEG %s 0x%02X ", GS(FST_VIDEOTRACK) ,PES.streamID);
- strcpy(mySt.id, szTemp);
-
- st->AddST(&mySt);
-
-
- sprintf( szTemp, "%s %d", GS(FST_VIDEOFOUND),st->n_video_streams);
- SetDlgItemText( hDlgParser, IDC_VIDEO, szTemp);
- stream_ids[PES.streamID]=true;
- }
- if(PES.streamID >= 0xC0 && PES.streamID <= 0xDF ){//if it was an audio stream
- mySt.aux =0;
- mySt.streamID =PES.streamID;
- mySt.subStreamID =0;
- sprintf(szTemp, "MPEG %s 0x%02X ", GS(FST_AUDIOTRACK),PES.streamID);
- strcpy(mySt.id, szTemp);
-
- st->AddST(&mySt);
-
-
-
-
- sprintf( szTemp, "%s %d", GS(FST_AUDIOFOUND),st->n_audio_streams);
- SetDlgItemText( hDlgParser, IDC_AUDIO, szTemp);
- stream_ids[PES.streamID]=true;
-
- }
- if(substream_ids[PES.subStreamID]==false && PES.streamID == 0xBD && PES.subStreamID >= 0x80 && PES.subStreamID <= 0x87){//if it was an audio stream
- mySt.aux =0;
- mySt.streamID =PES.streamID;
- mySt.subStreamID =PES.subStreamID;
- sprintf(szTemp, "AC3 %s 0x%02X %s 0x%02X", GS(FST_AUDIOTRACK),PES.subStreamID,GS(FST_MAINTRACK), PES.streamID);
- strcpy(mySt.id, szTemp);
-
- st->AddST(&mySt);
-
-
-
- sprintf( szTemp, "%s %d", GS(FST_AUDIOFOUND),st->n_audio_streams);
- SetDlgItemText( hDlgParser, IDC_AUDIO, szTemp);
- stream_ids[PES.streamID] =false;
- substream_ids[PES.subStreamID]=true;
-
- }
-
- }
- pos=inp->GetStreamPos();
-
- porciento= (int)(((double)pos/(double)(lurk_size*1024))*100.0);
- SendDlgItemMessage( hDlgParser, IDC_PROGRESS, PBM_SETPOS, porciento , 0);
-
- }
-
- // We've finished the lurking
- if(st->n_video_streams<1){
- delete demux;
- DestroyWindow(hDlgParser);
- return 0; //No video stream found. CHUNGO
- }
- if(st->n_video_streams==1 && st->n_audio_streams==1){
- st->video_selected = 0;
- st->audio_selected[0] = 0;
- }
- else if(st->n_video_streams==1 && st->n_audio_streams==0){
- st->video_selected = 0;
- }
- else if(st->n_video_streams>1 || st->n_audio_streams>0){
- //DialogBox(hInst,(LPCTSTR)(IDD_STRSELECTOR), hMainWnd ,(DLGPROC)DlgSelector);
- TSelectorDialog *sd= (TSelectorDialog *) new TSelectorDialog;
-
- sd->lateral_text = GS(FST_LATERAL);
- sd->tittle = GS(FST_TITLE);
- sd->button_text = GS(FST_BUTTON);
- sd->sections_titles[0]=GS(FST_VIDEOFOUND);
- sd->section_mode[0] =SINGLE_SELECT | MUST_SELECT;
- sd->section_mode[1] =SINGLE_SELECT | MUST_SELECT ;
- sd->sections_titles[1]=GS(FST_AUDIOFOUND);
- sd->section_count = 2;
-
-
- sd->strings[0].SetArraySize(st->n_video_streams);
- for(i=0; i<st->n_video_streams; i++)
- sd->strings[0][i] = st->video[i].id;
-
- sd->strings[1].SetArraySize(st->n_audio_streams);
- for(i=0; i<st->n_audio_streams; i++)
- sd->strings[1][i] = st->audio[i].id;
-
- // ResetSelections MUST be called before OpenSelectorDialog or setting default selections
- // in order to initialize selections.
- ResetSelections(sd);
- // Now set default selections
- sd->selected[0][0] = 1; //First section first video
- sd->selected[1][0] = 1; //Second section First audio
-
- OpenSelectorDialog(hMainWnd, hInst, sd);
-
- // FIXME: Video is mandatory but that is likely to change in future versions
- st->video_selected = 0; // Select the first stream
- // FIXME: When multiaudio is available check this one
- i=0;
- while( i < sd->selected[1].GetCount() ){
- if(sd->selected[1][i]==1)
- break;
- i++;
- }
-
- st->audio_selected[0] = i;
-
- delete sd;
-
-
- }
-
-
- delete demux;
- DestroyWindow(hDlgParser);
- return 1;
-
- }
-
- int CStreamDetector::SetFile(char *inputfile)
- {
- inp=new CinputStream;
- if(inp)
- return inp->OpenStream(inputfile, DVD_MODE|VERBOSE_MODE);
- else
- return 0;
- }
-
- TStreamDetector::TStreamDetector(){
- video= NULL;
- audio= NULL;
- subpic=NULL;
- }
-
- TStreamDetector::~TStreamDetector(){
- if(video)
- free(video);
- if(audio)
- free(audio);
- if(subpic)
- free(subpic);
- }
-
- int TStreamDetector::AddST(TStreamIdentifier *si)
- {
- if(si->streamID >= 0xE0 && si->streamID <= 0xEF){
- n_video_streams++;
- //resize the SIs buffer
- video=(TStreamIdentifier *) realloc(video, n_video_streams*sizeof(TStreamIdentifier));
- memcpy((void *)&video[n_video_streams-1], (void *)si, sizeof(TStreamIdentifier));
- }
- else if((si->streamID >= 0xC0 && si->streamID <= 0xDF) || \
- (si->streamID == 0xBD && si->subStreamID >= 0x80 && si->subStreamID <= 0x87)){
- n_audio_streams++;
- //resize the SIs buffer
- audio=(TStreamIdentifier *) realloc(audio, n_audio_streams*sizeof(TStreamIdentifier));
- memcpy((void *)&audio[n_audio_streams-1], (void *)si, sizeof(TStreamIdentifier));
- }
- return 1;
- }
-