aboutsummaryrefslogtreecommitdiff
path: root/pdp2gem.cpp
blob: dcbb3aeaafe163f0f55feac0f96881f5fa072965 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 *  pdp2gem : pdp to gem bridge
 *
 *  Holds the contents of a PDP packet and introduce it in the GEM rendering chain
 *
 *  Copyright (c) 2003-2005 Yves Degoyon
 *  Copyright (c) 2004-2005 James Tittle
 *
 */


#include "pdp2gem.h"
#include "Base/GemState.h"
#include "Base/GemVersion.h"
#include "yuv.h"

CPPEXTERN_NEW_WITH_ONE_ARG(pdp2gem, t_symbol *, A_DEFSYM)

pdp2gem :: pdp2gem(t_symbol *colorspace)
		 : m_colorspace(GL_YUV422_GEM), m_data(NULL), m_xsize(0), m_ysize(0),
		 m_mutex(NULL), m_packet0(-1), m_dropped(0), m_pdpdata(NULL)
{
  
  // csMess allows us to select what colorspace to convert
  //  the YV12 pixels into while handing it over to GEM
  csMess(colorspace->s_name);

  // initialize the pix block data
  m_pixBlock.image.data = NULL;
  m_pixBlock.image.xsize = 0;
  m_pixBlock.image.ysize = 0;

  if ( m_colorspace == GL_RGB ){
  m_pixBlock.image.csize = 3;
  m_pixBlock.image.format = GL_RGB;
  m_pixBlock.image.type = GL_UNSIGNED_BYTE;
	m_format = GL_RGB;  
	m_csize = 3;
  }else if ( m_colorspace == GL_YUV422_GEM ){
	m_pixBlock.image.csize = 2;
	m_pixBlock.image.format = GL_YUV422_GEM;
#ifdef __APPLE__
	m_pixBlock.image.type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
#else
	m_pixBlock.image.type = GL_UNSIGNED_BYTE;
#endif
	m_format = GL_YUV422_GEM;
	m_csize = 2;
  }else if ( m_colorspace == GL_RGBA || m_colorspace == GL_BGRA_EXT){
	m_pixBlock.image.csize = 4;
	m_csize = 4;
#ifdef __APPLE__
	m_pixBlock.image.format = GL_BGRA_EXT;
	m_format = GL_BGRA_EXT;
#else
	m_pixBlock.image.format = GL_RGBA;
	m_format = GL_RGBA;
#endif
	m_pixBlock.image.type = GL_UNSIGNED_BYTE;
	post("pdp2gem:  RGBA conversion not yet implemented");
  }else if ( m_colorspace == GL_LUMINANCE ){
	post("pdp2gem:  Gray conversion not yet implemented");
	m_pixBlock.image.csize = 1;
	m_pixBlock.image.format = GL_LUMINANCE;
	m_pixBlock.image.type = GL_UNSIGNED_BYTE;
	m_format = GL_RGB;
	m_csize = 1;
  }else{
	post("pdp2gem:  needs to know what colorspace to import to:  YUV, RGB, RGBA, or Gray");
  }

  m_mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
  if ( pthread_mutex_init(m_mutex, NULL) < 0 )
  {
       perror("pdp2gem : couldn't create mutex");
  }
  else
  {
       post("pdp2gem : created mutex");
  }
}

pdp2gem :: ~pdp2gem()
{
  if ( m_mutex ) 
  {
    pthread_mutex_destroy(m_mutex);
    free(m_mutex);
    post("pdp2gem : destroyed mutex");
  }

  deleteBuffer();
  colorspace = NULL;
}

void pdp2gem :: deleteBuffer()
{
  if ( m_data )
  {
    delete [] m_data;
  }
  m_pixBlock.image.data=NULL;
}

void pdp2gem :: createBuffer()
{
  const int neededXSize = m_xsize;
  const int neededYSize = m_ysize;
  int dataSize;

  deleteBuffer();

  m_pixBlock.image.xsize = neededXSize;
  m_pixBlock.image.ysize = neededYSize;
  m_pixBlock.image.csize = m_csize;
  m_pixBlock.image.format= m_format;
  m_pixBlock.image.upsidedown=1;

  // +4 from MPEG 
  dataSize = (m_pixBlock.image.xsize * m_pixBlock.image.ysize * m_pixBlock.image.csize)+4; 
  m_data = new unsigned char[dataSize];
  memset(m_data, 0, dataSize);

  m_pixBlock.image.data = m_data;

  post("pdp2gem : created buffer %d x %d", m_xsize, m_ysize );
}

void pdp2gem :: pdpMess(t_symbol *action, int pcktno)
{
 t_int psize;
  short int *pY, *pY2, *pU, *pV;
 unsigned char r,g,b;
  unsigned char y,y2,u,v;
 t_int cpt, px, py;

  if (action == gensym("register_ro"))  
  {
	m_dropped = pdp_packet_copy_ro_or_drop((int*)&m_packet0, pcktno);
  }
  // post("pdp2gem : got pdp packet #%d : dropped : %d", pcktno, m_dropped );

  m_header = pdp_packet_header(m_packet0);

  if ((action == gensym("process")) && (-1 != m_packet0) && (!m_dropped))
  {
    if ( PDP_IMAGE == m_header->type )
    {
	  if (pdp_packet_header(m_packet0)->info.image.encoding == PDP_IMAGE_YV12 
	  				&& (m_colorspace == GL_RGB || m_colorspace == GL_BGR_EXT) )
	  {
		if ( ( m_xsize != (int)m_header->info.image.width ) ||
			 ( m_ysize != (int)m_header->info.image.height ) )
		{
		  m_xsize = m_header->info.image.width;
		  m_ysize = m_header->info.image.height;
		  createBuffer();
		}
		m_pdpdata = (short int *)pdp_packet_data(m_packet0);

		// lock mutex
		pthread_mutex_lock(m_mutex);
		
		// convert pixels from yv12 to RGB/BGR
		m_pixBlock.image.fromYV12(m_pdpdata);
		
		// unlock mutex
		pthread_mutex_unlock(m_mutex);

		// free PDP packet
		pdp_packet_mark_unused(m_packet0);
		m_packet0 = -1;
		
	} else if (pdp_packet_header(m_packet0)->info.image.encoding == PDP_IMAGE_YV12 
	  				&& (m_colorspace == GL_RGBA || m_colorspace == GL_BGRA_EXT) )
	{
		if ( ( m_xsize != (int)m_header->info.image.width ) ||
			 ( m_ysize != (int)m_header->info.image.height ) )
		{
		  m_xsize = m_header->info.image.width;
		  m_ysize = m_header->info.image.height;
		  createBuffer();
		}
		m_pdpdata = (short int *)pdp_packet_data(m_packet0);

		// lock mutex
		pthread_mutex_lock(m_mutex);
		
		// convert pixels from yv12 to RGBA/BGRA
		m_pixBlock.image.fromYV12(m_pdpdata);
		
		// unlock mutex
		pthread_mutex_unlock(m_mutex);

		// free PDP packet
		pdp_packet_mark_unused(m_packet0);
		m_packet0 = -1;
		
	} else if (pdp_packet_header(m_packet0)->info.image.encoding == PDP_IMAGE_YV12 
									&& m_colorspace == GL_YUV422_GEM)
	{
		  if ( ( m_xsize != (int)m_header->info.image.width ) ||
			 ( m_ysize != (int)m_header->info.image.height ) )
		  {
		    m_xsize = m_header->info.image.width;
		    m_ysize = m_header->info.image.height;
		    createBuffer();
		  }
		m_pdpdata = (short int *)pdp_packet_data(m_packet0);

 		// lock mutex
 		pthread_mutex_lock(m_mutex);
 		
 		// convert pixels from yv12 to uyvy
 		m_pixBlock.image.fromYV12(m_pdpdata);

		// unlock mutex
		pthread_mutex_unlock(m_mutex);

		// free PDP packet
		pdp_packet_mark_unused(m_packet0);
		m_packet0 = -1;       
	  }
	  else
	  {
		post("pdp2gem : unsupported image type %i", m_header->info.image.encoding );
	  }
	}
  }
}

void pdp2gem :: startRendering()
{
  m_pixBlock.newimage = 1;
}

void pdp2gem :: render(GemState *state)
{
  if ( m_data && state )
  {
    // lock mutex
    pthread_mutex_lock(m_mutex);

    m_pixBlock.newimage = 1;
#if GEM_VERSION_MINOR >= 93
    state->set(GemState::_PIX, &m_pixBlock);
#else
    state->image = &m_pixBlock;
#endif
  }
}

void pdp2gem :: postrender(GemState *state)
{
  m_pixBlock.newimage = 0;

  // unlock mutex
  pthread_mutex_unlock(m_mutex);
}

/////////////////////////////////////////////////////////
// colorspaceMess
//
/////////////////////////////////////////////////////////
void pdp2gem :: csMess(char* format)
{
    if (!strcmp(format, "YUV")){
      m_colorspace = GL_YUV422_GEM;
      post("pdp2gem: colorspace is YUV %d",m_colorspace);
      m_csize = 2;
      m_format = GL_YUV422_GEM;
#ifdef __APPLE__
      m_pixBlock.image.type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
#else
      m_pixBlock.image.type = GL_UNSIGNED_BYTE;
#endif
      createBuffer();
      return;
    } else
    
    if (!strcmp(format, "RGB")){
      m_colorspace = GL_RGB;
      post("pdp2gem: colorspace is GL_RGB %d",m_colorspace);
      m_csize = 3;
      m_pixBlock.image.type = GL_UNSIGNED_BYTE;
#ifdef __APPLE__
	  m_format = GL_BGR_EXT;
#else
      m_format = GL_RGB;
#endif
      createBuffer();
      return;
    } else
    
    if (!strcmp(format, "RGBA")){
      m_colorspace = GL_RGBA;
      post("pdp2gem: colorspace is GL_RGBA %d",m_colorspace);
      m_csize = 4;
#ifdef __APPLE__
      m_format = GL_BGRA_EXT; m_pixBlock.image.format = GL_UNSIGNED_INT_8_8_8_8_REV;
#else
      m_format = GL_RGBA; m_pixBlock.image.format = GL_UNSIGNED_BYTE;
#endif
      createBuffer();
      return;
    } else {
    
    //post("pdp2gem: colorspace is unknown %d",m_colorspace);
    post("pdp2gem: using default colorspace:  YUV");
    m_csize = 2;
    m_colorspace = GL_YUV422_GEM;
    m_format = GL_YUV422_GEM;
    createBuffer();
    }
}

void pdp2gem :: obj_setupCallback(t_class *classPtr)
{
  ::post( "pdp2gem : a bridge between PDP/PiDiP and GEM v"GEM2PDP_VERSION" (ydegoyon@free.fr & tigital@mac.com)" );
  class_addmethod(classPtr, (t_method)&pdp2gem::pdpCallback, 
				  gensym("pdp"),  A_SYMBOL, A_DEFFLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pdp2gem::csMessCallback,
				  gensym("colorspace"), A_DEFSYMBOL, A_NULL);
  class_sethelpsymbol( classPtr, gensym("pdp2gem.pd") );
}

void pdp2gem :: pdpCallback(void *data, t_symbol *action, t_floatarg fpcktno)
{
  //post("pdp2gem : callback : action : %s : no : %f\n", action->s_name, fpcktno );
  GetMyClass(data)->pdpMess(action, (int)fpcktno);
}
void pdp2gem :: csMessCallback (void *data, t_symbol *colorspace)
{
  GetMyClass(data)->csMess((char*)colorspace->s_name);
}