aboutsummaryrefslogtreecommitdiff
path: root/modules/pdp_yqt.c
blob: 8c5b5807e9f90375dd42b65d76cff739d4f8f76c (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
/*
 *   PiDiP module.
 *   Copyright (c) by Yves Degoyon (ydegoyon@free.fr)
 *
 *   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.h"
#include "pdp_llconv.h"
#include "time.h"
#include "sys/time.h"
#include "pidip_config.h"
#ifdef QUICKTIME_NEWER
#include <lqt/lqt.h>
#include <lqt/colormodels.h>
#else
#include <quicktime/lqt.h>
#include <quicktime/colormodels.h>
#endif

#define     MIN_AUDIO_INPUT           1024  /* we must have at least n chunks to play a steady sound */
#define     OUTPUT_BUFFER_SIZE        128*1024  /* audio output buffer : 128k */
#define     DECODE_PACKET_SIZE        16*1024  /* size of audio data decoded in one call */

typedef struct pdp_yqt_struct
{
    t_object x_obj;
    t_float x_f;

    t_outlet *x_outlet0;
    t_outlet *x_curframe;
    t_outlet *x_nbframes;
    t_outlet *x_framerate;
    t_outlet *x_ol;   /* audio left channel  */
    t_outlet *x_or;   /* audio right channel */

    int packet0;
    bool initialized;

    unsigned int x_vwidth;
    unsigned int x_vheight;
    int x_cursec;
    int x_framescount;

    bool loop;

    unsigned char * qt_rows[3];

    unsigned char *qt_frame;
    quicktime_t *qt;
    int qt_cmodel;

    int    x_audio;             /* indicates the existence of an audio track */
    int    x_audio_channels;	  /* number of audio channels of first track   */
    int    x_mono;              /* indicates a mono audio track              */
    int    x_audio_rate;        /* audio rate                                */
    int    x_resampling_factor; /* resampling factor                         */

    t_float *x_outbuffer;         /* buffer to store audio decoded data        */
    int    x_outwriteposition;
    int    x_outreadposition;
    int    x_outunread;
    int    x_outbuffersize;
    t_float  *x_outl;
    t_float  *x_outr;
    t_float  **x_outs;

} t_pdp_yqt;



static void pdp_yqt_close(t_pdp_yqt *x)
{
    if (x->initialized){
	quicktime_close(x->qt);
	free(x->qt_frame);
	x->initialized = false;
    }

}

static void pdp_yqt_open(t_pdp_yqt *x, t_symbol *name)
{
    unsigned int size;

    post("pdp_yqt: opening %s", name->s_name);

    pdp_yqt_close(x);

    x->qt = quicktime_open(name->s_name, 1, 0);

    if (!(x->qt)){
	post("pdp_yqt: error opening qt file");
	x->initialized = false;
	return;
    }

    if (!quicktime_has_video(x->qt)) {
	post("pdp_yqt: no video stream");
	quicktime_close(x->qt);
	x->initialized = false;
	return;
	
    }
    else if (!quicktime_supported_video(x->qt,0)) {
	post("pdp_yqt: unsupported video codec\n");
	quicktime_close(x->qt);
	x->initialized = false;
	return;
    }
    else 
    {
	x->qt_cmodel = BC_YUV420P;
	x->x_vwidth  = quicktime_video_width(x->qt,0);
	x->x_vheight = quicktime_video_height(x->qt,0);
	x->qt_frame = (unsigned char*)malloc(x->x_vwidth*x->x_vheight*4);
	size = x->x_vwidth * x->x_vheight;
	x->qt_rows[0] = &x->qt_frame[0];
	x->qt_rows[2] = &x->qt_frame[size];
	x->qt_rows[1] = &x->qt_frame[size + (size>>2)];
    
	quicktime_set_cmodel(x->qt, x->qt_cmodel);
	x->initialized = true;
	outlet_float(x->x_nbframes, (float)quicktime_video_length(x->qt,0));

    }

    if (!quicktime_has_audio(x->qt)) {
	post("pdp_yqt: warning : no audio stream");
        x->x_audio = 0;
        return;
    }
    
    if ( quicktime_audio_tracks(x->qt) > 1 )
    {
	post("pdp_yqt: warning : more that one audio track, using first one");
    }

    if ( ( x->x_audio_channels = quicktime_track_channels(x->qt, 0) ) != 2 ) {
	x->x_mono=0;
        post("pdp_yqt: track 0 has %d channels", x->x_audio_channels );
        post("pdp_yqt: warning : not a stereo audio track ( audio channels : %d )", x->x_audio_channels ); 
        if ( x->x_audio_channels == 1 ) x->x_mono = 1;
        else x->x_audio_channels = 2;
    } else {
        post("pdp_yqt: track 0 has %d channels", x->x_audio_channels );
    }

    if (!quicktime_supported_audio(x->qt,0)) {
        post("pdp_yqt: warning : audio not supported" ); 
	x->x_audio = 0;
    } else {
	x->x_audio = 1;
    }
   
    if ( x->x_audio )
    {
       post("pdp_yqt: using audio track 0 with %d channels", x->x_audio_channels );
       post("pdp_yqt: audio data is %d bytes, %d kHz compressed with %s", 
		quicktime_audio_bits(x->qt, 0),
		x->x_audio_rate = quicktime_sample_rate(x->qt, 0),
		quicktime_audio_compressor(x->qt, 0) );
       x->x_resampling_factor = ( sys_getsr() / x->x_audio_rate );
       x->x_outreadposition = 0;
       x->x_outwriteposition = 0;
       x->x_outunread = 0;
    }

}


static void pdp_yqt_bang(t_pdp_yqt *x)
{
  unsigned int w, h, nbpixels, packet_size;
  int object, length, pos, i, j;
  short int* data;
  t_pdp* header;
  struct timeval etime;

    if (!(x->initialized)){
	//post("pdp_yqt: no qt file opened");
	return;
    }

    w = x->x_vwidth;
    h = x->x_vheight;
    nbpixels = w * h;
    packet_size = (nbpixels + (nbpixels >> 1)) << 1;

    object = pdp_packet_new_image_YCrCb( x->x_vwidth, x->x_vheight );
    header = pdp_packet_header(object);
    data = (short int *) pdp_packet_data(object);

    header->info.image.encoding = PDP_IMAGE_YV12;
    header->info.image.width = w;
    header->info.image.height = h;

    length = quicktime_video_length(x->qt,0);
    pos = quicktime_video_position(x->qt,0);
    // post("pdp_yqt : video position : %d length =%d", pos, length );

    if (pos >= length){
	pos = (x->loop) ? 0 : length - 1;
        // post("pdp_yqt : setting video position to %d", pos);
	quicktime_set_video_position(x->qt, pos, 0);
	if (x->loop) 
        {
           // post("pdp_yqt : resetting audio position");
           if ( x->x_audio ) quicktime_set_audio_position(x->qt, 0, 0);
           x->x_outreadposition = 0;
           x->x_outwriteposition = 0;
           x->x_outunread = 0;
        }
    }

    lqt_decode_video(x->qt, x->qt_rows, 0);

    switch(x->qt_cmodel){
    case BC_YUV420P:
        pdp_llconv(x->qt_frame, RIF_YVU__P411_U8, data, RIF_YVU__P411_S16, x->x_vwidth, x->x_vheight);
        break;

    case BC_YUV422:
        pdp_llconv(x->qt_frame, RIF_YUYV_P____U8, data, RIF_YVU__P411_S16, x->x_vwidth, x->x_vheight);
        break;

    case BC_RGB888:
        pdp_llconv(x->qt_frame, RIF_RGB__P____U8, data, RIF_YVU__P411_S16, x->x_vwidth, x->x_vheight);
        break;

    default:
        post("pdp_yqt : error on decode: unkown colour model");
        break;
    }

    if ( gettimeofday(&etime, NULL) == -1)
    {
        post("pdp_yqt : could not get time" );
    }
    if ( etime.tv_sec != x->x_cursec )
    {
       x->x_cursec = etime.tv_sec;
       outlet_float(x->x_framerate, (float)x->x_framescount);
       x->x_framescount = 0;
    }
    x->x_framescount++;
    
    outlet_float(x->x_curframe, (float)pos);

    pdp_packet_pass_if_valid(x->x_outlet0, &object);

}

static void pdp_yqt_loop(t_pdp_yqt *x, t_floatarg loop)
{
    int loopi = (int)loop;
    x->loop = !(loopi == 0);
}

static void pdp_yqt_frame_cold(t_pdp_yqt *x, t_floatarg frameindex)
{
    int frame = (int)frameindex;
    int length, sample;

    if (!(x->initialized)) return;

    length = quicktime_video_length(x->qt,0);

    frame = (frame >= length) ? length-1 : frame;
    frame = (frame < 0) ? 0 : frame;

    // post("pdp_yqt : frame cold : setting video position to : %d", frame );
    quicktime_set_video_position(x->qt, frame, 0);
    if ( x->x_audio )
    {
      sample = x->x_audio_rate*((float)frame/(float)quicktime_frame_rate (x->qt, 0));
      quicktime_set_audio_position(x->qt, sample, 0 );
      x->x_outreadposition = 0;
      x->x_outwriteposition = 0;
      x->x_outunread = 0;
    }
}

static void pdp_yqt_frame(t_pdp_yqt *x, t_floatarg frameindex)
{
    // pdp_yqt_frame_cold(x, frameindex);
    pdp_yqt_bang(x);
}

static void pdp_yqt_free(t_pdp_yqt *x)
{
  
    pdp_yqt_close(x);

    freebytes(x->x_outbuffer, OUTPUT_BUFFER_SIZE*sizeof(t_float));
    freebytes(x->x_outl, DECODE_PACKET_SIZE*sizeof(t_float));
    freebytes(x->x_outr, DECODE_PACKET_SIZE*sizeof(t_float));
}

t_class *pdp_yqt_class;

void *pdp_yqt_new(void)
{
    t_pdp_yqt *x = (t_pdp_yqt *)pd_new(pdp_yqt_class);

    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("frame_cold"));

    x->x_outlet0 = outlet_new(&x->x_obj, &s_anything);
    x->x_curframe = outlet_new(&x->x_obj, &s_float);
    x->x_nbframes = outlet_new(&x->x_obj, &s_float);
    x->x_framerate = outlet_new(&x->x_obj, &s_float);

    x->x_ol = outlet_new(&x->x_obj, &s_signal);   /* audio left channel  */
    x->x_or = outlet_new(&x->x_obj, &s_signal);   /* audio right channel */

    x->packet0 = -1;

    x->initialized = false;

    x->loop = false;

    // allocate audio buffers
    x->x_outbuffersize = OUTPUT_BUFFER_SIZE;
    x->x_outl = (t_float*) getbytes(DECODE_PACKET_SIZE*sizeof(t_float));
    x->x_outr = (t_float*) getbytes(DECODE_PACKET_SIZE*sizeof(t_float));
    x->x_outs = (t_float**) getbytes( 2*sizeof(t_float*));
    x->x_outs[0] = x->x_outl;
    x->x_outs[1] = x->x_outr;
    x->x_outbuffer = (t_float*) getbytes(OUTPUT_BUFFER_SIZE*sizeof(t_float));

    if ( !x->x_outl || !x->x_outr || !x->x_outbuffer || !x->x_outs )
    {
       post( "pdp_yqt: not allocate buffers" );
       return NULL;
    }
    memset( x->x_outl, 0x0, DECODE_PACKET_SIZE*sizeof(t_float) );
    memset( x->x_outr, 0x0, DECODE_PACKET_SIZE*sizeof(t_float) );
    memset( x->x_outbuffer, 0x0, OUTPUT_BUFFER_SIZE*sizeof(t_float) );

    x->x_outreadposition = 0;
    x->x_outwriteposition = 0;
    x->x_outunread = 0;

    return (void *)x;
}

static t_int *pdp_yqt_perform(t_int *w)
{
     t_pdp_yqt *x = (t_pdp_yqt*) (w[1]);
     t_float *out1 = (t_float *)(w[2]);
     t_float *out2 = (t_float *)(w[3]);
     int n = (int)(w[4]);
     int ret;
     int i = 0;

    // fills in the audio buffer with a chunk if necessary
    if ( (x->initialized) && (quicktime_video_position(x->qt,0)>0) && x->x_audio && ( x->x_outunread < n ) )
    {
      int csize, rsize, i, j;

       // watch remaining size
       rsize = (int ) ( quicktime_audio_length(x->qt, 0) - quicktime_audio_position(x->qt, 0) );
       csize = ( rsize < DECODE_PACKET_SIZE ) ? rsize : DECODE_PACKET_SIZE;

       // post("pdp_yqt : decode one chunk (size=%d)", csize );
       if ( ( lqt_decode_audio(x->qt, NULL, x->x_outs, csize) <0 ) )
       {
	   post("pdp_yqt : could not decode audio data" );
       } else {
         for ( i=0; i<csize; i++ )
         {
            if ( x->x_outunread >= x->x_outbuffersize-2 )
            {
                post( "pdp_yqt: decode audio : too much input ... ignored" );
                continue;
            }
            for ( j=0; j<x->x_resampling_factor; j++ )
            {
               *(x->x_outbuffer+x->x_outwriteposition) = x->x_outl[i];
               x->x_outwriteposition = (x->x_outwriteposition + 1)%x->x_outbuffersize;
               *(x->x_outbuffer+x->x_outwriteposition) =
                   ((x->x_mono)? x->x_outl[i] : x->x_outr[i] );
               x->x_outwriteposition = (x->x_outwriteposition + 1)%x->x_outbuffersize;
               x->x_outunread+=2;
            }
         }
       }
    }

     while( n-- )
     {
        if ( x->x_audio && x->x_outunread >= 2 )
        {
          *out1++=*(x->x_outbuffer+x->x_outreadposition);
          x->x_outreadposition = (x->x_outreadposition + 1)%x->x_outbuffersize;
          *out2++=*(x->x_outbuffer+x->x_outreadposition);
          x->x_outreadposition = (x->x_outreadposition + 1)%x->x_outbuffersize;
          x->x_outunread-=2;
        }
        else
        {
          // should not happen 
          // post( "pdp_yqt: null audio samples" );
          *out1++=0.;
          // *out1++=*(x->x_outbuffer+x->x_outreadposition);
          *out2++=0.;
          // *out2++=*(x->x_outbuffer+x->x_outreadposition);
        }
     }

     return (w+5);
}

static void pdp_yqt_dsp(t_pdp_yqt *x, t_signal **sp)
{
    dsp_add(pdp_yqt_perform, 4, x, sp[1]->s_vec, sp[2]->s_vec, sp[1]->s_n);
}


#ifdef __cplusplus
extern "C"
{
#endif


void pdp_yqt_setup(void)
{
    pdp_yqt_class = class_new(gensym("pdp_yqt"), (t_newmethod)pdp_yqt_new,
    	(t_method)pdp_yqt_free, sizeof(t_pdp_yqt), 0, A_NULL);

    class_addmethod(pdp_yqt_class, (t_method)pdp_yqt_bang, gensym("bang"), A_NULL);
    class_addmethod(pdp_yqt_class, (t_method)pdp_yqt_close, gensym("close"), A_NULL);
    class_addmethod(pdp_yqt_class, (t_method)pdp_yqt_open, gensym("open"), A_SYMBOL, A_NULL);
    class_addmethod(pdp_yqt_class, (t_method)pdp_yqt_loop, gensym("loop"), A_DEFFLOAT, A_NULL);
    class_addfloat (pdp_yqt_class, (t_method)pdp_yqt_frame);
    class_addmethod(pdp_yqt_class, (t_method)pdp_yqt_frame_cold, gensym("frame_cold"), A_FLOAT, A_NULL);
    class_addmethod(pdp_yqt_class, nullfn, gensym("signal"), 0);
    class_addmethod(pdp_yqt_class, (t_method)pdp_yqt_dsp, gensym("dsp"), 0);
    class_sethelpsymbol( pdp_yqt_class, gensym("pdp_yqt.pd") );

}

#ifdef __cplusplus
}
#endif