aboutsummaryrefslogtreecommitdiff
path: root/modules/pdp_fqt.c
blob: ccbaf572b970c155fab33e7bfe623d2b319fc37b (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
/*
 *   Pure Data Packet 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 <quicktime/lqt.h>
#include <quicktime/colormodels.h>

typedef struct pdp_fqt_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;

    int packet0;
    bool initialized;

    t_int x_vwidth;
    t_int x_vheight;
    t_int x_size;
    t_int x_fsize; // frames size
    t_int x_length;
    t_int x_current_frame;
    t_int x_cursec;
    t_int x_framescount;

    unsigned char * qt_rows[3];

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

    unsigned int** x_frames;
    t_int* x_fsizes;

} t_pdp_fqt;



static void pdp_fqt_close(t_pdp_fqt *x)
{
  t_int fi;

    if (x->initialized){
	quicktime_close(x->qt);
	if ( x->qt_frame ) freebytes(x->qt_frame, x->x_size*3/2);
        for ( fi=0; fi<x->x_length; fi++ )
        {
          if ( x->x_frames[fi] ) freebytes( x->x_frames[fi], x->x_fsizes[fi] );
        }
        if ( x->x_frames ) freebytes( x->x_frames, x->x_length*sizeof(unsigned int*) );
	x->initialized = false;
    }

}

static void pdp_fqt_open(t_pdp_fqt *x, t_symbol *name)
{
  t_int fi;

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

    pdp_fqt_close(x);

    x->qt = quicktime_open(name->s_name, 1, 0); // read=yes, write=no 

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

    if (!quicktime_has_video(x->qt)) {
	post("pdp_fqt: no video stream");
	quicktime_close(x->qt);
	x->initialized = false;
	return;
	
    }
    else if (!quicktime_supported_video(x->qt,0)) {
	post("pdp_fqt: 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->x_size = x->x_vwidth * x->x_vheight;
	x->qt_frame = (unsigned char*)getbytes(x->x_size*3/2);
	x->qt_rows[0] = &x->qt_frame[0];
	x->qt_rows[2] = &x->qt_frame[x->x_size];
	x->qt_rows[1] = &x->qt_frame[x->x_size + (x->x_size>>2)];
    
	quicktime_set_cmodel(x->qt, x->qt_cmodel);
	x->initialized = true;
        x->x_length = quicktime_video_length(x->qt,0);
	outlet_float(x->x_nbframes, (float)x->x_length);

    }

    // read all frames
    x->x_current_frame = 0;
    x->x_frames = (unsigned int**) getbytes( x->x_length*sizeof(unsigned int*) );
    x->x_fsizes = (t_int*) getbytes( x->x_length*sizeof(t_int) );
    x->x_fsize = 0;
    if ( !x->x_frames )
    {
      post("pdp_fqt: couldn't allocate memory for frames" );
      quicktime_close(x->qt);
      x->initialized = false;
      return;
    }

    for ( fi=0; fi<x->x_length; fi++ )
    {
       x->x_fsizes[fi] = ((x->x_size)+(x->x_size>>1))<<1;
       x->x_fsize += x->x_fsizes[fi];
       x->x_frames[fi] = (unsigned int*) getbytes( x->x_fsizes[fi] );
       if ( !x->x_frames[fi] )
       {
         post("pdp_fqt: couldn't allocate memory for frames" );
         quicktime_close(x->qt);
         x->initialized = false;
         return;
       }

       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, x->x_frames[fi], RIF_YVU__P411_S16, 
                            x->x_vwidth, x->x_vheight);
           break;

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

    post("pdp_fqt: allocated memory for %d frames (size=%db %dM)", 
                   x->x_length, x->x_fsize, x->x_fsize/(1024*1024) );
}


static void pdp_fqt_bang(t_pdp_fqt *x)
{
  int object;
  short int* data;
  t_pdp* header;
  struct timeval etime;

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

    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 = x->x_vwidth;
    header->info.image.height = x->x_vheight;

    x->x_current_frame = ( x->x_current_frame + 1 ) % x->x_length;
    // post( "pdp_fqt : current frame : %d", x->x_current_frame );

    memcpy( data, x->x_frames[x->x_current_frame], x->x_fsizes[x->x_current_frame] );

    if ( gettimeofday(&etime, NULL) == -1)
    {
        post("pdp_fcqt : 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)x->x_current_frame);
    pdp_packet_pass_if_valid(x->x_outlet0, &object);

}

static void pdp_fqt_frame_cold(t_pdp_fqt *x, t_floatarg frameindex)
{
    int frame = (int)frameindex;
    int length;


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

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

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

    // post("pdp_fqt : frame cold : setting video position to : %d", frame );
    quicktime_set_video_position(x->qt, frame, 0);
}

static void pdp_fqt_frame(t_pdp_fqt *x, t_floatarg frameindex)
{
    // pdp_fqt_frame_cold(x, frameindex);
    pdp_fqt_bang(x);
}

static void pdp_fqt_free(t_pdp_fqt *x)
{
    pdp_fqt_close(x);
}

t_class *pdp_fqt_class;

void *pdp_fqt_new(void)
{
    t_pdp_fqt *x = (t_pdp_fqt *)pd_new(pdp_fqt_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->packet0 = -1;

    x->initialized = false;

    return (void *)x;
}

#ifdef __cplusplus
extern "C"
{
#endif


void pdp_fqt_setup(void)
{
    pdp_fqt_class = class_new(gensym("pdp_fqt"), (t_newmethod)pdp_fqt_new,
    	(t_method)pdp_fqt_free, sizeof(t_pdp_fqt), 0, A_NULL);

    class_addmethod(pdp_fqt_class, (t_method)pdp_fqt_bang, gensym("bang"), A_NULL);
    class_addmethod(pdp_fqt_class, (t_method)pdp_fqt_close, gensym("close"), A_NULL);
    class_addmethod(pdp_fqt_class, (t_method)pdp_fqt_open, gensym("open"), A_SYMBOL, A_NULL);
    class_addfloat (pdp_fqt_class, (t_method)pdp_fqt_frame);
    class_addmethod(pdp_fqt_class, (t_method)pdp_fqt_frame_cold, gensym("frame_cold"), A_FLOAT, A_NULL);
    class_addmethod(pdp_fqt_class, nullfn, gensym("signal"), 0);
    class_sethelpsymbol( pdp_fqt_class, gensym("pdp_fqt.pd") );

}

#ifdef __cplusplus
}
#endif