summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/decoder/mpgPlugin.cpp
blob: fd7170c8c77a290dafc32216134c07d6d41aaaf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
  mpg I video/audio player plugin
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "mpgPlugin.h"

#include "../mpegplay/mpegSystemStream.h"
#include "../mpgplayer/mpegStreamPlayer.h"
#include "../mpegplay/mpegVideoLength.h"

#include "splayPlugin.h"
#include "mpegPlugin.h"

#include <iostream>

using namespace std;


MpgPlugin::MpgPlugin() {

  mpegSystemHeader=NULL;
  mpegSystemStream=NULL;
  mpegStreamPlayer=NULL;
  mpegVideoLength=NULL;

  // make runtime config easier, no NULL check necessary
  mpegSystemHeader=new MpegSystemHeader();
  timeStamp=new TimeStamp();
  lMono=false;
  lDown=false;
  lWriteStreams=false;
  lDoLength=true;
}


MpgPlugin::~MpgPlugin() {
  delete mpegSystemHeader;
  delete timeStamp;
}


 
void MpgPlugin::decoder_loop() {

  if (input == NULL) {
    cout << "MPGPlugin::decoder_loop input is NULL"<<endl;
    exit(0);
  }
  if (output == NULL) {
    cout << "MPGPlugin::decoder_loop output is NULL"<<endl;
    exit(0);
  }

  // decoder
  SplayPlugin* splayPlugin=new SplayPlugin();
  splayPlugin->config("-c","true",NULL);
  if (lMono) {
    splayPlugin->config("-m","true",NULL);
  }
  if (lDown) {
    splayPlugin->config("-2","true",NULL);
  }
   
  MpegPlugin* mpegplayPlugin=new MpegPlugin();

  mpegSystemStream=new MpegSystemStream(input);
  mpegStreamPlayer=new MpegStreamPlayer(input,output,
					splayPlugin,mpegplayPlugin);
  mpegStreamPlayer->setWriteToDisk(lWriteStreams);
  mpegVideoLength=new MpegVideoLength(input);

 
  int lSysLayer=false;
  int lHasLength=false;

  while(runCheck()) {
    switch(streamState) {
    case _STREAM_STATE_FIRST_INIT :
      if ((lHasLength==false) && (lDoLength)) {
	if (mpegVideoLength->firstInitialize()==false) {
	  continue;
	}
	lHasLength=true;
      }
      if (mpegSystemStream->firstInitialize(mpegSystemHeader)==false) {
	//cout << "still initializing system stream"<<endl;
      } else {
	// if we have found a raw stream,
	// make sure we pass as first argument a SEQ_START_CODE
	if (mpegSystemHeader->getLayer() == _PACKET_SYSLAYER) {
	  lSysLayer=true;
	  mpegStreamPlayer->processSystemHeader(mpegSystemHeader);
	} else {
	  //output->config("-s","false");
	  unsigned int startCodeRaw=htonl(_SEQ_START_CODE);
	  mpegStreamPlayer->
	    insertVideoDataRaw((unsigned char*)&startCodeRaw,4,timeStamp);
	}
	pluginInfo->setLength(mpegVideoLength->getLength());
	output->writeInfo(pluginInfo);
	setStreamState(_STREAM_STATE_INIT);
	
	if (mpegSystemHeader->getMPEG2()==true) {
	  cout << "this plugin does not support MPEG2/VOB/DVD"<<endl;
	  lDecoderLoop=false;
	  if (lWriteStreams == true) {
	    cout << "demux is supported"<<endl;
	    lDecoderLoop=true;
	    
	  }
	  break;
	}

      }
      break;
    case _STREAM_STATE_INIT :
      setStreamState(_STREAM_STATE_PLAY);
      break;
    case _STREAM_STATE_PLAY :
      // syslayer

      if (mpegSystemStream->nextPacket(mpegSystemHeader) == false) {

	break;
      }

      if (mpegStreamPlayer->processSystemHeader(mpegSystemHeader) == false) {
        mpegSystemStream->reset();
	setStreamState(_STREAM_STATE_INIT);
      }
      
      break;
    case _STREAM_STATE_WAIT_FOR_END:
      if (mpegStreamPlayer->hasEnd()==true) {
	// exit while loop
	lDecoderLoop=false;
      }
      TimeWrapper::usleep(100000);
      break;
    default:
      cout << "unknown stream state:"<<streamState<<endl;
    }
  }

  delete mpegStreamPlayer;
  delete mpegSystemStream;
  delete mpegVideoLength;
  delete mpegplayPlugin;
  delete splayPlugin;

  mpegSystemStream=NULL;
  mpegStreamPlayer=NULL;
  mpegVideoLength=NULL;


  output->audioFlush();
  output->flushWindow();

}



// here we can config our decoder with special flags
void MpgPlugin::config(const char* key,const char* value,void* user_data) {
  if(strcmp("VideoLayer", key) == 0) {
    int videoLayerSelect=atoi(value);
    mpegSystemHeader->setVideoLayerSelect(videoLayerSelect);
  }

  if(strcmp("AudioLayer", key) == 0) {
    int audioLayerSelect=atoi(value);
    mpegSystemHeader->setAudioLayerSelect(audioLayerSelect);
  }
  if (strcmp(key,"-2")==0) {
    lDown=true;
  }
  if (strcmp(key,"-m")==0) {
    lMono=true;
  }
  if (strcmp(key,"-c")==0) {
    lDoLength=false;
  }
  if (strcmp(key,"-w")==0) {
    if (strcmp(value,"true")==0) {
      lWriteStreams=true;
    } else {
      lWriteStreams=true;
    }
  }
  // now try to set it if stream is running:
  shutdownLock();
  if (mpegStreamPlayer != NULL) {
    mpegStreamPlayer->setWriteToDisk(lWriteStreams);
  }
  shutdownUnlock();  
    
  
  DecoderPlugin::config(key,value,user_data);
}




int MpgPlugin::getTotalLength() {
  shutdownLock();
  int back=0;
  if (mpegVideoLength != NULL) {
    back=mpegVideoLength->getLength();
  } else {
    cout << "cannot report total length, plugin not initialized"<<endl;
  }
  shutdownUnlock();

  return back;
}





int MpgPlugin::processThreadCommand(Command* command) {

  int id=command->getID();
  int arg;
  switch(id) {
  case _COMMAND_SEEK: {
    //
    // mapping from second to bytePosition
    //
    if (mpegStreamPlayer->isInit()==false) {
      command->print("MPGPLUGIN:ignore command in _STREAM_STATE_FIRST_INIT");
    } else {
      arg=command->getIntArg();
      int pos=mpegVideoLength->getSeekPos(arg);
      Command seek(_COMMAND_SEEK,pos);
      // insert correct values
      mpegStreamPlayer->processThreadCommand(&seek);
    }
    return _RUN_CHECK_CONTINUE;
  }
  
  default:
    // we forward the command to the mpegStreamPlayer
    mpegStreamPlayer->processThreadCommand(command);
  }

  // use default
  return (DecoderPlugin::processThreadCommand(command));
}