aboutsummaryrefslogtreecommitdiff
path: root/modules/pdp_ieee1394l.c
blob: 6b9492460064575e473e7d8ee0f834af015cf70c (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 *   Pure Data Packet module.
 *   Copyright (c) by Lluis Gomez i Bigorda <lluisgomez@hangar.org>
 *
 *   This program 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 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include "pdp_config.h"
#include "pdp.h"
#include "pdp_llconv.h"
#include "pdp_imageproc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>             /* getopt_long() */
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <linux/types.h>
#include <sched.h>
#include <pthread.h>

#include "dv1394.h"
#include <libdv/dv.h>

#define N_BUF 2 /*DV1394_MAX_FRAMES/4*/
#define PAL 0
#define NTSC 1


typedef struct pdp_ieee1394_struct
{
  t_object x_obj;
  
  t_outlet *x_outlet0;

  int x_width;
  int x_height;

  int dvfd;
  unsigned char *videobuf;
  unsigned char *decodedbuf;
  bool x_frame_ready;
  int  x_frame, x_lastframe;
  bool x_continue_thread;
  pthread_t x_thread_id;

  int x_framesize;
  unsigned char *x_mmapbuf;

  dv_decoder_t *x_decoder;

  bool x_haveVideo;
  bool x_capturing;
  bool x_norm;
  char* x_devicename;
  int x_devicenum;

} t_pdp_ieee1394;



static void
process_image                   (t_pdp_ieee1394 *x)
{
	
    unsigned int w,h;
    int object,length,pos,i,encoding;
    t_pdp* header;
    t_image* image;
    short int * data;
	
    //fputc ('.', stdout);
    //fflush (stdout);
	
	
	
    /* create new packet */
    w = x->x_width;
    h = x->x_height;


    object = pdp_packet_new_image(PDP_IMAGE_YV12, w, h);
    header = pdp_packet_header(object);
    image = pdp_packet_image_info(object);

    if (!header){
	post("pdp_v4l: ERROR: can't allocate packet");
	return;
    }

    data = (short int *) pdp_packet_data(object);
	
    /* convert data to pdp packet */
    //pdp_llconv(x->decodedbuf, RIF_YVYU_P____U8, data, RIF_YVU__P411_S16, w, h); 
    pdp_llconv(x->decodedbuf, RIF_RGB__P____U8, data, RIF_YVU__P411_S16, w, h); //


    pdp_packet_pass_if_valid(x->x_outlet0, &object);
}

static int pdp_ieee1394_read_frame(t_pdp_ieee1394 *x)
{

  if (!x->x_decoder)return 0;
  if (!x->x_frame_ready) {
	//x->x_image.newimage = 0;
  }
  else {
    dv_parse_header(x->x_decoder, x->videobuf);
    dv_parse_packs (x->x_decoder, x->videobuf);
    if(dv_frame_changed(x->x_decoder)) {
      int pitches[3] = {0,0,0};
      //      pitches[0]=x_decoder->width*3; // rgb
      //      pitches[0]=x_decoder->width*((x_reqFormat==GL_RGBA)?3:2);
      pitches[0]=x->x_decoder->width*3;
      x->x_height=x->x_decoder->height;
      x->x_width=x->x_decoder->width;
      
      /* decode the DV-data to something we can handle and that is similar to the wanted format */
      //      dv_report_video_error(x_decoder, videobuf);  // do we need this ?
      // gosh, this(e_dv_color_rgb) is expansive:: the decoding is done in software only...
      //      dv_decode_full_frame(x_decoder, videobuf, ((x_reqFormat==GL_RGBA)?e_dv_color_rgb:e_dv_color_yuv), &decodedbuf, pitches);
      dv_decode_full_frame(x->x_decoder, x->videobuf, e_dv_color_rgb, &x->decodedbuf, pitches);

      //     post("sampling %d", x_decoder->sampling);

      /* convert the colour-space to the one we want */
      /*
       * btw. shouldn't this be done in [pix_video] rather than here ?
       * no because [pix_video] knows nothing about the possible colourspaces in here
       */

      // letting the library do the conversion to RGB and then doing the conversion to RGBA
      // is really stupid.
      // let's do it all ourselfes:
      //      if (x_reqFormat==GL_RGBA)x_image.image.fromRGB(decodedbuf); else
      //x_image.image.fromYVYU(decodedbuf);
    	process_image (x);
	
    }

    x->x_frame_ready = false;
  }
	
  return 1;
}


static void *pdp_ieee1394_thread(void *voidx)
{
    t_pdp_ieee1394 *x = ((t_pdp_ieee1394 *)voidx);

  int fd=x->dvfd;
  int framesize = x->x_framesize;
  struct dv1394_status dvst;
  int n_frames = N_BUF;
  unsigned char* mmapbuf = x->x_mmapbuf;

  /* this will hang if no ieee1394-device is present, what to do about it ??? */
  x->x_haveVideo=false;
  if(ioctl(fd, DV1394_WAIT_FRAMES, 1)) {
    perror("error: ioctl WAIT_FRAMES");
    x->x_capturing=false; return NULL;
  }
  if (ioctl(fd, DV1394_GET_STATUS, &dvst))   {
    perror("ioctl GET_STATUS");
    x->x_capturing=false; return NULL;
  }
  x->x_haveVideo=true;
  x->x_capturing=true;

  //fprintf(stderr,"aqui1");
  while(x->x_continue_thread){
  //fprintf(stderr,"aqui2");
    if(ioctl(fd, DV1394_WAIT_FRAMES, n_frames - 1)) {
      perror("error: ioctl WAIT_FRAMES");
      x->x_capturing=false; return NULL;
    }
    if (ioctl(fd, DV1394_GET_STATUS, &dvst))   {
      perror("ioctl GET_STATUS");
      x->x_capturing=false; return NULL;
    }
  //fprintf(stderr,"aqui3");
    /*
      dvst.init
      dvst.active_frame
      dvst.first_clear_frame
      dvst.n_clear_frames
      dvst.dropped_frames
    */	
    if (dvst.dropped_frames > 0) {
      verbose(1,"dv1394: dropped at least %d frames", dvst.dropped_frames);
    }
    /*
      memcpy( g_current_frame->data, 
      (g_dv1394_map + (dvst.first_clear_frame * DV1394_PAL_FRAME_SIZE)),
      DV1394_PAL_FRAME_SIZE );
    */
    x->videobuf = mmapbuf + (dvst.first_clear_frame * framesize);

    //post("thread %d\t%x %x", me->frame, me->tvfd, me->vmmap);
    if (ioctl(fd, DV1394_RECEIVE_FRAMES, 1) < 0)    {
      perror("receiving...");
    }
    x->x_lastframe=x->x_frame;
    x->x_frame++;
    x->x_frame%=N_BUF;
    x->x_frame_ready = true;
  }
  x->x_capturing=false;

  //process_image  (x, x->videobuf);

    return 0;
}

static void
close_device                    (t_pdp_ieee1394 *x)
{
  if(x->x_mmapbuf!=NULL)munmap(x->x_mmapbuf, N_BUF*x->x_framesize);
  if(x->dvfd>=0)close(x->dvfd);
  x->x_haveVideo=false;

}


static int
startTransfer (t_pdp_ieee1394 *x)
{
  //if ((x->dvfd=openDevice(format))<0){
  //  verbose(1, "DV4L: closed");
  //  return(0);
  //}
  //x->x_image.newimage=0;
  //x->x_image.image.data=0;
  //x->x_image.image.xsize=720;
  //x->x_image.image.ysize=576;
  //x->x_image.image.setCsizeByFormat(x->x_reqFormat);
  //x->x_image.image.reallocate();
  x->videobuf=NULL;

  x->x_frame_ready = false; 

  if(x->x_decoder!=NULL)dv_decoder_free(x->x_decoder);
  if (!(x->x_decoder=dv_decoder_new(1, 1, 1))){
    //error("DV4L: unable to create DV-decoder...closing");
    close_device(x);
    return(0);
  }
  //x->x_decoder->quality=x->x_quality;
  x->x_decoder->quality = DV_QUALITY_BEST;
  verbose(1, "DV4L: DV decoding quality %d ", x->x_decoder->quality);
  //fprintf(stderr,"before");
  x->x_continue_thread = true;
  pthread_create(&x->x_thread_id, 0, pdp_ieee1394_thread, x);
  return 1;
}

static int
stopTransfer                   (t_pdp_ieee1394 *x)
{
  /* close the dv4l device and dealloc buffer */
  /* terminate thread if there is one */
  x->x_continue_thread=false;
  int i=0;
  if(x->x_haveVideo){
    while(x->x_capturing){
      struct timeval sleep;
      sleep.tv_sec=0;  sleep.tv_usec=10; /* 10us */
      select(0,0,0,0,&sleep);
      i++;
    }
    verbose(1, "DV4L: shutting down dv1394 after %d usec", i*10);
    ioctl(x->dvfd, DV1394_SHUTDOWN);
  }
  close_device(x);
  return(1);
}


static void pdp_ieee1394_close(t_pdp_ieee1394 *x)
{
  /* close the v4l device and dealloc buffer */

    void *dummy;
    //x->x_initialized = false;
    /* terminate thread if there is one */
    if(x->x_continue_thread){
	x->x_continue_thread = 0;
	pthread_join (x->x_thread_id, &dummy);
    }


        //stop_capturing (x);

        //uninit_device (x);

        close_device (x);
	
	if (-1 == close (x->dvfd))
	        post ("close");

        x->dvfd = -1;
}


static int pdp_ieee1394_open(t_pdp_ieee1394 *x, t_symbol *name)
{
  x->x_devicename = name->s_name;

  if(x->x_haveVideo){
    verbose(1, "Stream already going on. Doing some clean-up...");
    stopTransfer(x);
  }

  /*
  All of the errors in this method return -1 anyhow, so fd should be 0 to allow
  successful open if everything goes ok.

  Ico Bukvic ico@vt.edu 2-18-07
  */
  int fd = 0; 
  struct dv1394_init init = {
    DV1394_API_VERSION, // api version 
    0x63,              // isochronous transmission channel
    N_BUF,             // number of frames in ringbuffer
    (x->x_norm==NTSC)?DV1394_NTSC:DV1394_PAL,         // PAL or NTSC
    //DV1394_PAL,         // PAL or NTSC
    0, 0 , 0                // default packet rate
  };

  x->x_framesize=(x->x_norm==NTSC)?DV1394_NTSC_FRAME_SIZE:DV1394_PAL_FRAME_SIZE;
  //x->x_framesize=DV1394_PAL_FRAME_SIZE;

  if(x->x_devicename){
    if ((fd = open(x->x_devicename, O_RDWR)) < 0) {
        perror(x->x_devicename);
        return -1;
    }
  } else {
    signed char devnum=(x->x_devicenum<0)?0:(signed char)x->x_devicenum;
    char buf[256];
    buf[255]=0;buf[32]=0;buf[33]=0;
    if (devnum<0)devnum=0;
    snprintf(buf, 32, "/dev/ieee1394/dv/host%d/%s/in", devnum, (x->x_norm==NTSC)?"NTSC":"PAL");
    //snprintf(buf, 32, "/dev/ieee1394/dv/host%d/%s/in", devnum, "PAL");
    if ((fd = open(buf, O_RDWR)) < 0)    {
      snprintf(buf, 32, "/dev/dv1394/%d", devnum);
      if ((fd = open(buf, O_RDWR)) < 0) {
	if ((fd=open("/dev/dv1394", O_RDWR)) < 0)    {
	  perror(buf);
	  return -1;
	}
      }
    }
  }
  if (ioctl(fd, DV1394_INIT, &init) < 0)    {
    perror("initializing");
    close(fd);
    return -1;
  }
  
  x->x_mmapbuf = (unsigned char *) mmap( NULL, N_BUF*x->x_framesize,
				       PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if(x->x_mmapbuf == MAP_FAILED) {
    perror("mmap frame buffers");
    close(fd);
    return -1;
  }
  
  if(ioctl(fd, DV1394_START_RECEIVE, NULL)) {
    perror("dv1394 START_RECEIVE ioctl");
    close(fd);
    return -1;
  }
  /*Extra verbosity never hurt anyone...

  Ico Bukvic ico@vt.edu 2-18-07
  */
  post("DV4L: Successfully opened...");
  startTransfer(x);
  x->dvfd=fd;

  return 1;
	
}


static int pdp_ieee1394_norm(t_pdp_ieee1394 *x, t_symbol *s)
{
  int inorm = x->x_norm;
  char* norm=s->s_name;
  switch(norm[0]){
  case 'N': case 'n':
    inorm=NTSC;
    break;
  case 'P': case 'p':
    inorm=PAL;
    break;
  }
  if (inorm==x->x_norm)return 0;
  x->x_norm=inorm;
  return 0;
}


static void pdp_ieee1394_bang(t_pdp_ieee1394 *x)
{
   
  /* if initialized, grab a frame and output it */



    /* convert data to pdp packet */
/*
    switch(x->x_v4l_palette){
    case  VIDEO_PALETTE_YUV420P:
	pdp_llconv(newimage, RIF_YUV__P411_U8, data, RIF_YVU__P411_S16, w, h); 
	break;*/
	
	/* long live standards. v4l's rgb is in fact ogl's bgr */
/*    case  VIDEO_PALETTE_RGB24:
	pdp_llconv(newimage, RIF_BGR__P____U8, data, RIF_YVU__P411_S16, w, h); 
	break;

    case  VIDEO_PALETTE_RGB32:
	pdp_llconv(newimage, RIF_BGRA_P____U8, data, RIF_YVU__P411_S16, w, h); 
	break;

    case  VIDEO_PALETTE_YUV422:
	pdp_llconv(newimage, RIF_YUYV_P____U8, data, RIF_YVU__P411_S16, w, h); 
	break;*/


    /*default:
	post("pdp_ieee1394: unsupported palette");
	break;
    }*/

/*
    if (PDP_IMAGE_YV12 == x->x_pdp_image_type){
	pixel_unpack_u8s16_y(&newimage[0], data, nbpixels>>3, x->x_state_data->gain);
	pixel_unpack_u8s16_uv(&newimage[plane1], &data[plane2], nbpixels>>5, x->x_state_data->gain);
	pixel_unpack_u8s16_uv(&newimage[plane2], &data[plane1], nbpixels>>5, x->x_state_data->gain);
    }
*/
    //x->x_v4l_palette = VIDEO_PALETTE_YUV420P;
    //x->x_v4l_palette = VIDEO_PALETTE_RGB24;

/*

    else if(PDP_IMAGE_GREY == x->x_pdp_image_type){
	pixel_unpack_u8s16_y(&newimage[0], data, nbpixels>>3, x->x_state_data->gain);
    }
*/
    //post("pdp_ieee1394: mark unused %d", object);

    /*pdp_packet_pass_if_valid(x->x_outlet0, &object);*/

}



static void pdp_ieee1394_free(t_pdp_ieee1394 *x)
{
  //pdp_ieee1394_close(x);
  if(x->x_haveVideo)stopTransfer(x);
  //if(x->decodedbuf)delete[]decodedbuf;
  if(x->x_decoder!=NULL)dv_decoder_free(x->x_decoder);
}

t_class *pdp_ieee1394_class;



void *pdp_ieee1394_new(t_symbol *vdef, t_symbol *format)
{
    t_pdp_ieee1394 *x = (t_pdp_ieee1394 *)pd_new(pdp_ieee1394_class);

    x->x_outlet0 = outlet_new(&x->x_obj, &s_anything);

    //x->x_channel = 0;//0x63;
    x->x_devicenum  = 0;
    x->x_norm = PAL;
    x->x_decoder=NULL;
    x->x_frame_ready=false;
    x->x_width=720;
    x->x_height=576;
    x->x_framesize=DV1394_PAL_FRAME_SIZE;
    //x->x_quality = DV_QUALITY_BEST;
    //x->decodedbuf = new unsigned char[720*576*3];
    x->decodedbuf = malloc (720*576*3*sizeof(unsigned char));
    x->x_haveVideo=false;

    return (void *)x;
}


#ifdef __cplusplus
extern "C"
{
#endif


void pdp_ieee1394_setup(void)
{
    // post( " pdp_ieee1394 : linux dv interface by Lluis Gomez i Bigorda (lluisgomez@hangar.org)" );

    pdp_ieee1394_class = class_new(gensym("pdp_ieee1394"), (t_newmethod)pdp_ieee1394_new,
    	(t_method)pdp_ieee1394_free, sizeof(t_pdp_ieee1394), 0, A_DEFSYMBOL, A_DEFSYMBOL, A_NULL);


    class_addmethod(pdp_ieee1394_class, (t_method)pdp_ieee1394_read_frame, gensym("bang"), A_NULL);
    class_addmethod(pdp_ieee1394_class, (t_method)pdp_ieee1394_close, gensym("close"), A_NULL);
    class_addmethod(pdp_ieee1394_class, (t_method)pdp_ieee1394_open, gensym("open"), A_SYMBOL, A_NULL);
    class_addmethod(pdp_ieee1394_class, (t_method)pdp_ieee1394_norm, gensym("norm"), A_SYMBOL, A_NULL);

}

#ifdef __cplusplus
}
#endif