Camera tracking: initial commit of dopesheet view for clip editor:
[blender.git] / source / blender / editors / space_clip / clip_editor.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/clip_editor.c
29  *  \ingroup spclip
30  */
31
32 #include <stddef.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BKE_main.h"
37 #include "BKE_movieclip.h"
38 #include "BKE_context.h"
39 #include "BKE_tracking.h"
40 #include "DNA_object_types.h"   /* SELECT */
41
42 #include "BLI_utildefines.h"
43 #include "BLI_math.h"
44
45 #include "GPU_extensions.h"
46
47 #include "IMB_imbuf_types.h"
48 #include "IMB_imbuf.h"
49
50 #include "ED_screen.h"
51 #include "ED_clip.h"
52
53 #include "BIF_gl.h"
54 #include "BIF_glutil.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "UI_view2d.h"
60
61 #include "clip_intern.h"        // own include
62
63 int ED_space_clip_poll(bContext *C)
64 {
65         SpaceClip *sc = CTX_wm_space_clip(C);
66
67         if (sc && sc->clip)
68                 return TRUE;
69
70         return FALSE;
71 }
72
73 void ED_space_clip_set(bContext *C, bScreen *screen, SpaceClip *sc, MovieClip *clip)
74 {
75         MovieClip *old_clip;
76
77         if (!screen && C)
78                 screen = CTX_wm_screen(C);
79
80         old_clip = sc->clip;
81         sc->clip = clip;
82
83         if (sc->clip && sc->clip->id.us==0)
84                 sc->clip->id.us = 1;
85
86         if (screen) {
87                 ScrArea *area;
88                 SpaceLink *sl;
89
90                 for (area = screen->areabase.first; area; area = area->next) {
91                         for (sl = area->spacedata.first; sl; sl = sl->next) {
92                                 if (sl->spacetype == SPACE_CLIP) {
93                                         SpaceClip *cur_sc = (SpaceClip *) sl;
94
95                                         if (cur_sc != sc) {
96                                                 if (cur_sc->clip == old_clip || cur_sc->clip == NULL) {
97                                                         cur_sc->clip = clip;
98                                                 }
99                                         }
100                                 }
101                         }
102                 }
103         }
104
105         if (C)
106                 WM_event_add_notifier(C, NC_MOVIECLIP|NA_SELECTED, sc->clip);
107 }
108
109 MovieClip *ED_space_clip(SpaceClip *sc)
110 {
111         return sc->clip;
112 }
113
114 ImBuf *ED_space_clip_get_buffer(SpaceClip *sc)
115 {
116         if (sc->clip) {
117                 ImBuf *ibuf;
118
119                 ibuf = BKE_movieclip_get_postprocessed_ibuf(sc->clip, &sc->user, sc->postproc_flag);
120
121                 if (ibuf && (ibuf->rect || ibuf->rect_float))
122                         return ibuf;
123
124                 if (ibuf)
125                         IMB_freeImBuf(ibuf);
126         }
127
128         return NULL;
129 }
130
131 ImBuf *ED_space_clip_get_stable_buffer(SpaceClip *sc, float loc[2], float *scale, float *angle)
132 {
133         if (sc->clip) {
134                 ImBuf *ibuf;
135
136                 ibuf = BKE_movieclip_get_stable_ibuf(sc->clip, &sc->user, loc, scale, angle, sc->postproc_flag);
137
138                 if (ibuf && (ibuf->rect || ibuf->rect_float))
139                         return ibuf;
140
141                 if (ibuf)
142                         IMB_freeImBuf(ibuf);
143         }
144
145         return NULL;
146 }
147
148 void ED_space_clip_size(SpaceClip *sc, int *width, int *height)
149 {
150         if (!sc->clip) {
151                 *width = *height = 0;
152         }
153         else {
154                 BKE_movieclip_get_size(sc->clip, &sc->user, width, height);
155         }
156 }
157
158 void ED_space_clip_zoom(SpaceClip *sc, ARegion *ar, float *zoomx, float *zoomy)
159 {
160         int width, height;
161
162         ED_space_clip_size(sc, &width, &height);
163
164         *zoomx = (float)(ar->winrct.xmax - ar->winrct.xmin + 1)/(float)((ar->v2d.cur.xmax - ar->v2d.cur.xmin)*width);
165         *zoomy = (float)(ar->winrct.ymax - ar->winrct.ymin + 1)/(float)((ar->v2d.cur.ymax - ar->v2d.cur.ymin)*height);
166 }
167
168 void ED_space_clip_aspect(SpaceClip *sc, float *aspx, float *aspy)
169 {
170         MovieClip *clip = ED_space_clip(sc);
171
172         if (clip)
173                 BKE_movieclip_aspect(clip, aspx, aspy);
174         else
175                 *aspx = *aspy = 1.0f;
176 }
177
178 void ED_clip_update_frame(const Main *mainp, int cfra)
179 {
180         wmWindowManager *wm;
181         wmWindow *win;
182
183         /* image window, compo node users */
184         for (wm = mainp->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
185                 for (win = wm->windows.first; win; win = win->next) {
186                         ScrArea *sa;
187
188                         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
189                                 if (sa->spacetype == SPACE_CLIP) {
190                                         SpaceClip *sc = sa->spacedata.first;
191
192                                         sc->scopes.ok = FALSE;
193
194                                         BKE_movieclip_user_set_frame(&sc->user, cfra);
195                                 }
196                         }
197                 }
198         }
199 }
200
201 static int selected_boundbox(SpaceClip *sc, float min[2], float max[2])
202 {
203         MovieClip *clip = ED_space_clip(sc);
204         MovieTrackingTrack *track;
205         int width, height, ok = FALSE;
206         ListBase *tracksbase= BKE_tracking_get_tracks(&clip->tracking);
207
208         INIT_MINMAX2(min, max);
209
210         ED_space_clip_size(sc, &width, &height);
211
212         track = tracksbase->first;
213         while (track) {
214                 if (TRACK_VIEW_SELECTED(sc, track)) {
215                         MovieTrackingMarker *marker = BKE_tracking_get_marker(track, sc->user.framenr);
216
217                         if (marker) {
218                                 float pos[3];
219
220                                 pos[0] = marker->pos[0] + track->offset[0];
221                                 pos[1] = marker->pos[1] + track->offset[1];
222                                 pos[2] = 0.0f;
223
224                                 /* undistortion happens for normalized coords */
225                                 if (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
226                                         /* undistortion happens for normalized coords */
227                                         ED_clip_point_undistorted_pos(sc, pos, pos);
228                                 }
229
230                                 pos[0] *= width;
231                                 pos[1] *= height;
232
233                                 mul_v3_m4v3(pos, sc->stabmat, pos);
234
235                                 DO_MINMAX2(pos, min, max);
236
237                                 ok = TRUE;
238                         }
239                 }
240
241                 track = track->next;
242         }
243
244         return ok;
245 }
246
247 int ED_clip_view_selection(SpaceClip *sc, ARegion *ar, int fit)
248 {
249         int w, h, frame_width, frame_height;
250         float min[2], max[2];
251
252         ED_space_clip_size(sc, &frame_width, &frame_height);
253
254         if (frame_width == 0 || frame_height == 0)
255                 return FALSE;
256
257         if (!selected_boundbox(sc, min, max))
258                 return FALSE;
259
260         /* center view */
261         clip_view_center_to_point(sc, (max[0]+min[0])/(2*frame_width), (max[1]+min[1])/(2*frame_height));
262
263         w = max[0] - min[0];
264         h = max[1] - min[1];
265
266         /* set zoom to see all selection */
267         if (w > 0 && h > 0) {
268                 int width, height;
269                 float zoomx, zoomy, newzoom, aspx, aspy;
270
271                 ED_space_clip_aspect(sc, &aspx, &aspy);
272
273                 width = ar->winrct.xmax - ar->winrct.xmin + 1;
274                 height = ar->winrct.ymax - ar->winrct.ymin + 1;
275
276                 zoomx = (float)width / w / aspx;
277                 zoomy = (float)height / h / aspy;
278
279                 newzoom = 1.0f / power_of_2(1.0f / MIN2(zoomx, zoomy));
280
281                 if (fit || sc->zoom>newzoom)
282                         sc->zoom = newzoom;
283         }
284
285         return TRUE;
286 }
287
288 void ED_clip_point_undistorted_pos(SpaceClip *sc, float co[2], float nco[2])
289 {
290         copy_v2_v2(nco, co);
291
292         if (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
293                 MovieClip *clip = ED_space_clip(sc);
294                 float aspy = 1.0f / clip->tracking.camera.pixel_aspect;
295                 int width, height;
296
297                 ED_space_clip_size(sc, &width, &height);
298
299                 nco[0] *= width;
300                 nco[1] *= height * aspy;
301
302                 BKE_tracking_invert_intrinsics(&clip->tracking, nco, nco);
303
304                 nco[0] /= width;
305                 nco[1] /= height * aspy;
306         }
307 }
308
309 void ED_clip_point_stable_pos(bContext *C, float x, float y, float *xr, float *yr)
310 {
311         ARegion *ar = CTX_wm_region(C);
312         SpaceClip *sc = CTX_wm_space_clip(C);
313         int sx, sy, width, height;
314         float zoomx, zoomy, pos[3] = {0.0f, 0.0f, 0.0f}, imat[4][4];
315
316         ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
317         ED_space_clip_size(sc, &width, &height);
318
319         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &sx, &sy);
320
321         pos[0] = (x - sx) / zoomx;
322         pos[1] = (y - sy) / zoomy;
323
324         invert_m4_m4(imat, sc->stabmat);
325         mul_v3_m4v3(pos, imat, pos);
326
327         *xr = pos[0] / width;
328         *yr = pos[1] / height;
329
330         if (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
331                 MovieClip *clip = ED_space_clip(sc);
332                 MovieTracking *tracking = &clip->tracking;
333                 float aspy = 1.0f / tracking->camera.pixel_aspect;
334                 float tmp[2] = {*xr * width, *yr * height * aspy};
335
336                 BKE_tracking_apply_intrinsics(tracking, tmp, tmp);
337
338                 *xr = tmp[0] / width;
339                 *yr = tmp[1] / (height * aspy);
340         }
341 }
342
343 void ED_clip_mouse_pos(bContext *C, wmEvent *event, float co[2])
344 {
345         ED_clip_point_stable_pos(C, event->mval[0], event->mval[1], &co[0], &co[1]);
346 }
347
348 /* OpenGL draw context */
349
350 typedef struct SpaceClipDrawContext {
351         int support_checked, buffers_supported;
352
353         GLuint texture;                 /* OGL texture ID */
354         short texture_allocated;        /* flag if texture was allocated by glGenTextures */
355         struct ImBuf *texture_ibuf;     /* image buffer for which texture was created */
356         int image_width, image_height;  /* image width and height for which texture was created */
357         unsigned last_texture;          /* ID of previously used texture, so it'll be restored after clip drawing */
358         int framenr;
359 } SpaceClipDrawContext;
360
361 int ED_space_clip_texture_buffer_supported(SpaceClip *sc)
362 {
363         SpaceClipDrawContext *context = sc->draw_context;
364
365         if (!context) {
366                 context = MEM_callocN(sizeof(SpaceClipDrawContext), "SpaceClipDrawContext");
367                 sc->draw_context = context;
368         }
369
370         if (!context->support_checked) {
371                 context->support_checked = TRUE;
372                 context->buffers_supported = GPU_non_power_of_two_support();
373         }
374
375         return context->buffers_supported;
376 }
377
378 int ED_space_clip_load_movieclip_buffer(SpaceClip *sc, ImBuf *ibuf)
379 {
380         SpaceClipDrawContext *context = sc->draw_context;
381         MovieClip *clip = ED_space_clip(sc);
382         int need_rebind = 0;
383
384         context->last_texture = glaGetOneInteger(GL_TEXTURE_2D);
385
386         /* image texture need to be rebinded if displaying another image buffer
387          * assuming displaying happens of footage frames only on which painting doesn't heppen.
388          * so not changed image buffer pointer means unchanged image content */
389         need_rebind |= context->texture_ibuf != ibuf;
390         need_rebind |= context->framenr != sc->user.framenr;
391
392         if (need_rebind) {
393                 int width = ibuf->x, height = ibuf->y;
394                 float *frect = NULL, *fscalerect = NULL;
395                 unsigned int *rect = NULL, *scalerect = NULL;
396                 int need_recreate = 0;
397
398                 if (width > GL_MAX_TEXTURE_SIZE || height > GL_MAX_TEXTURE_SIZE)
399                         return 0;
400
401                 rect = ibuf->rect;
402                 frect = ibuf->rect_float;
403
404                 /* if image resolution changed (e.g. switched to proxy display) texture need to be recreated */
405                 need_recreate = context->image_width != ibuf->x || context->image_height != ibuf->y;
406
407                 if (context->texture_ibuf && need_recreate) {
408                         glDeleteTextures(1, &context->texture);
409                         context->texture_allocated = 0;
410                 }
411
412 #if 0
413                 /* disabled for now because current tracking users have got NPOT textures
414                  * working smoothly on their computers and forcing re-scaling during playback
415                  * slows down playback a lot */
416
417                 /* if videocard doesn't support NPOT textures, need to do rescaling */
418                 if (!GPU_non_power_of_two_support()) {
419                         if (!is_power_of_2_i(width) || !is_power_of_2_i(height)) {
420                                 width = power_of_2_max_i(width);
421                                 height = power_of_2_max_i(height);
422
423                                 if (ibuf->x != width || ibuf->y != height) {
424                                         if (frect) {
425                                                 fscalerect= MEM_mallocN(width*width*sizeof(*fscalerect)*4, "fscalerect");
426                                                 gluScaleImage(GL_RGBA, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float, width, height, GL_FLOAT, fscalerect);
427
428                                                 frect = fscalerect;
429                                         }
430                                         else {
431                                                 scalerect= MEM_mallocN(width*height*sizeof(*scalerect), "scalerect");
432                                                 gluScaleImage(GL_RGBA, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect, width, height, GL_UNSIGNED_BYTE, scalerect);
433
434                                                 rect = scalerect;
435                                         }
436                                 }
437                         }
438                 }
439 #endif
440
441                 if (need_recreate || !context->texture_allocated) {
442                         /* texture doesn't exist yet or need to be re-allocated because of changed dimensions */
443                         int filter = GL_LINEAR;
444
445                         /* non-scaled proxy shouldn;t use diltering */
446                         if ((clip->flag & MCLIP_USE_PROXY) == 0 ||
447                             ELEM(sc->user.render_size, MCLIP_PROXY_RENDER_SIZE_FULL, MCLIP_PROXY_RENDER_SIZE_100))
448                         {
449                                 filter = GL_NEAREST;
450                         }
451
452                         glGenTextures(1, &context->texture);
453                         glBindTexture(GL_TEXTURE_2D, context->texture);
454                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
455                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
456                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
457                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
458                 }
459                 else {
460                         /* if texture doesn't need to be reallocated itself, just bind it so
461                          * loading of image will happen to a proper texture */
462                         glBindTexture(GL_TEXTURE_2D, context->texture);
463                 }
464
465                 if (frect)
466                         glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA16,  width, height, 0, GL_RGBA, GL_FLOAT, frect);
467                 else
468                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rect);
469
470                 /* store settings */
471                 context->texture_allocated = 1;
472                 context->texture_ibuf = ibuf;
473                 context->image_width = ibuf->x;
474                 context->image_height = ibuf->y;
475                 context->framenr = sc->user.framenr;
476
477                 if (fscalerect)
478                         MEM_freeN(fscalerect);
479                 if (scalerect)
480                         MEM_freeN(scalerect);
481         }
482         else {
483                 /* displaying exactly the same image which was loaded t oa texture,
484                  * just bint texture in this case */
485                 glBindTexture(GL_TEXTURE_2D, context->texture);
486         }
487
488         glEnable(GL_TEXTURE_2D);
489
490         return 1;
491 }
492
493 void ED_space_clip_unload_movieclip_buffer(SpaceClip *sc)
494 {
495         SpaceClipDrawContext *context = sc->draw_context;
496
497         glBindTexture(GL_TEXTURE_2D, context->last_texture);
498         glDisable(GL_TEXTURE_2D);
499 }
500
501 void ED_space_clip_free_texture_buffer(SpaceClip *sc)
502 {
503         SpaceClipDrawContext *context = sc->draw_context;
504
505         if (context) {
506                 glDeleteTextures(1, &context->texture);
507
508                 MEM_freeN(context);
509         }
510 }