summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/decoder/mpegPlugin.cpp
blob: e230adb5c61b0590fb4c2e64522482aed1d6d3bc (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
/*
  mpeg 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 "mpegPlugin.h"

#include "../mpegplay/mpegVideoStream.h"
#include "../mpegplay/proto.h"
#include "../mpegplay/mpegVideoHeader.h"

using namespace std;


MpegPlugin::MpegPlugin() {
  init();
}


MpegPlugin::~MpegPlugin() {
}


void MpegPlugin::init() {
  lCalcLength=false;
  mpegVideoHeader=NULL;
  mpegVideoStream=NULL;
}


void MpegPlugin::decoder_loop() {


  VideoDecoder* video=NULL;
  if (input == NULL) {
    cout << "MpegPlugin::decoder_loop input is NULL"<<endl;
    exit(0);
  }
  if (output == NULL) {
    cout << "MpegPlugin::decoder_loop output is NULL"<<endl;
    exit(0);
  }

  mpegVideoHeader=new MpegVideoHeader();
  mpegVideoStream=new MpegVideoStream(input);

  PictureArray* pictureArray;
  YUVPicture* pic;
  int skipMode=_SYNC_TO_NONE;
  // decode loop

  while(runCheck()) {
    switch(streamState) {
    case _STREAM_STATE_FIRST_INIT :
      if (mpegVideoStream->firstInitialize(mpegVideoHeader)==false) {

      } else {
	pluginInfo->setLength(getSongLength());
	
	// now create pictureArray from the sequence
	int width=mpegVideoHeader->getMB_Width()*16;
	int height=mpegVideoHeader->getMB_Height()*16;

	output->openWindow(width,height,(char*)"kmpg");
	video=new VideoDecoder(mpegVideoStream,mpegVideoHeader);
	setStreamState(_STREAM_STATE_INIT);
      }
      break;
    case _STREAM_STATE_INIT :
      //      cout << "mpeg _STREAM_STATE_INI"<<endl;
      if (skipMode==_SYNC_TO_GOP) {
	if (mpegVideoStream->nextGOP()==false) {
	  continue;
	}
	video->resyncToI_Frame();
      }
      if (skipMode==_SYNC_TO_PIC) {
	if (mpegVideoStream->nextPIC()==false) {
	  continue;
	}
      }
      skipMode=_SYNC_TO_NONE;
      setStreamState(_STREAM_STATE_PLAY);
      break;
    case _STREAM_STATE_PLAY :
      pictureArray=output->lockPictureArray();
      skipMode=video->mpegVidRsrc(pictureArray);

      if (skipMode != _SYNC_TO_NONE) {
	setStreamState(_STREAM_STATE_INIT);
      }
      pic=pictureArray->getYUVPictureCallback();
      if (pic == NULL) {
	// nothin to display
	break;
      }

      output->unlockPictureArray(pictureArray);
      pictureArray->setYUVPictureCallback(NULL);
      break;
    case _STREAM_STATE_WAIT_FOR_END:
      // exit while loop
      lDecoderLoop=false;
      break;
    default:
      cout << "unknown stream state:"<<streamState<<endl;
    }
  }

  output->flushWindow();
  // copy sequence back if needed
  if (video != NULL) {
    delete video;
  }
  delete mpegVideoStream;
  delete mpegVideoHeader;
  mpegVideoStream=NULL;
  mpegVideoHeader=NULL;
}





// here we can config our decoder with special flags
void MpegPlugin::config(const char* key,const char* value,void* user_data) {
  if (strcmp(key,"-c")==0) {
    lCalcLength=false;
  }

  if (strcmp(key,"decode")==0) {
    if (strcmp(value,"true")==0) {
      lDecode=true;
    } else {
      lDecode=false;
    }
  }    
  DecoderPlugin::config(key,value,user_data);
}



int MpegPlugin::getSongLength() {
  int back=0;
  return back;
}