summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/render/yuvPicture.cpp
blob: e79d3bde3ed1030902cf993294a3d55bc4ee75a5 (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
/*
  describes a picture in yuv format
  Copyright (C) 2000 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 "yuvPicture.h"

#include <iostream>

using namespace std;

static int instanceCnt=0;

YUVPicture::YUVPicture(int width,int height) {
  this->width=width;
  this->height=height;

  instance=instanceCnt;
  instanceCnt++;
  imagePtr=NULL;

  setImageType(PICTURE_YUVMODE_CR_CB);


  startTimeStamp=new TimeStamp();
  waitTime=new TimeStamp();
  earlyTime=new TimeStamp();
  mpegType=-1;

}


YUVPicture::~YUVPicture() {
  delete imagePtr;
  delete earlyTime;
  delete startTimeStamp;
  delete waitTime;
}



int YUVPicture::getHeight() {
  return height;
}


int YUVPicture::getWidth() {
  return width;
}



int YUVPicture::getLumLength() {
  return lumLength;
}


int YUVPicture::getColorLength() {
  return colorLength;
}

int YUVPicture::getImageSize() {
  return imageSize;
}
  






void YUVPicture::print(const char* title) {
  cout << title <<":";
  printf(" instance:%d  ",instance);
  printf(" width:%d  ",width);
  printf(" height:%d  ",height);
  cout <<" picPerSec:"<<picPerSec;
  switch(mpegType) {
  case 1:
    printf("I_FRAME  ");
    break;
  case 2:
    printf("P_FRAME  ");
    break;
  case 3:
    printf("B_FRAME  ");
    break;
  case 4:
    printf("D_FRAME  ");
    break;  
  default:
    printf("<unknown>  ");
  }
  printf("\n");
  
  
}


void YUVPicture::setPicturePerSecond(float val) {
  this->picPerSec=val;
}


float YUVPicture::getPicturePerSecond() {
  return picPerSec;
}


void YUVPicture::setStartTimeStamp(TimeStamp* aStamp) {
  aStamp->copyTo(startTimeStamp);
}


TimeStamp* YUVPicture::getStartTimeStamp() {
  return startTimeStamp;
}

void YUVPicture::setWaitTime(TimeStamp* aStamp) {
  aStamp->copyTo(waitTime);
}


TimeStamp* YUVPicture::getWaitTime() {
  return waitTime;
}

void YUVPicture::setEarlyTime(TimeStamp* earlyTime) {
  this->earlyTime=earlyTime;
}


TimeStamp* YUVPicture::getEarlyTime() {
  return earlyTime;
}



void YUVPicture::setMpegPictureType(int type) {
  this->mpegType=type;
}


int YUVPicture::getMpegPictureType() {
  return mpegType;
}


void YUVPicture::setImageType(int imageType) {


  //
  // Reset everything
  //

  if (imagePtr != NULL) {
    delete [] imagePtr;
    imagePtr=NULL;
  }
  this->imageType=imageType;

  lumLength=0;
  colorLength=0;
  Cr_mode=NULL;
  Cb_mode=NULL;
  luminance_mode=NULL;


  //
  // YUV Images
  //
  if ( (imageType == PICTURE_YUVMODE_CR_CB) ||
       (imageType == PICTURE_YUVMODE_CB_CR) ) {


    lumLength=width * height;
    colorLength=width * height / 4;
    imageSize=lumLength+colorLength+colorLength;

    // the 64 is some "security" space
    imagePtr=new unsigned char[imageSize+64];

    if (imagePtr == NULL) {
      cout << "cannot create image"<<endl;
      exit(0);
    }

    // now caculate pointers to start addresses of lum/Cr/Cb blocks
    // we need the yuvPtr for direct dither in hardware
    // this should save a memcpy

    luminance = imagePtr;
    Cr = imagePtr+lumLength;
    Cb = imagePtr+lumLength+colorLength;
    
   
    if ( (luminance == NULL) ||
	 (Cr == NULL) ||
	 (Cb == NULL) ) {
      cout << "allocation luminance/Cr/Cb error"<<endl;
      exit(0);
    }

    switch(imageType) {
    case PICTURE_YUVMODE_CR_CB:
      Cr_mode=Cr;
      Cb_mode=Cb;
      luminance_mode=imagePtr;
      break;
    case PICTURE_YUVMODE_CB_CR:
      Cr_mode=Cb;
      Cb_mode=Cr;
      luminance_mode=imagePtr;
      break;
    default:
      cout << "unknown yuv mode:"<<imageType<<endl;
    }
  }
  else if ( (imageType == PICTURE_YUVMODE_YUY2) ||
       (imageType == PICTURE_YUVMODE_UYVY) ) {
    // these yuv-modes are packed

    imageSize=width * height * 2;

    // the 64 is some "security" space
    imagePtr=new unsigned char[imageSize+64];

    if (imagePtr == NULL) {
      cout << "cannot create image"<<endl;
      exit(0);
    }
  }

  //
  // RGB Imcdages
  //
  
  if ( (imageType == PICTURE_RGB) ||
       (imageType == PICTURE_RGB_FLIPPED) ){
    imageSize=width*height*4;
    imagePtr=new unsigned char[imageSize];
  }
  memset(imagePtr,0,imageSize);
}