Merging r39717 through r39983 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenkernel / intern / movieclip.c
index 6d6c6c8d715cdd91975df70b2fec977765384559..220a050fcc03a2f8eb9e98586173d60ee651bce6 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * The Original Code is Copyright (C) 2011 Blender Foundation.
  * All rights reserved.
  *
  * Contributor(s): Blender Foundation,
@@ -215,8 +215,8 @@ static ImBuf *movieclip_load_movie_file(MovieClip *clip, int framenr, int flag)
                else
                        BLI_path_abs(str, G.main->name);
 
-               clip->anim= openanim(str, IB_rect);
-
+               /* FIXME: make several stream accessible in image editor, too */
+               clip->anim= openanim(str, IB_rect, 0);
 
                if(clip->anim) {
                        if(clip->flag&MCLIP_USE_PROXY_CUSTOM_DIR) {
@@ -341,13 +341,14 @@ static MovieClip *movieclip_alloc(const char *name)
        clip->aspx= clip->aspy= 1.0f;
 
        clip->tracking.camera.sensor_width= 35.0f;
-       clip->tracking.camera.sensor_height= 18.0f;
+       clip->tracking.camera.pixel_aspect= 1.0f;
        clip->tracking.camera.units= CAMERA_UNITS_MM;
 
-       clip->tracking.settings.frames_limit= 20;
+       clip->tracking.settings.frames_limit= 0;
        clip->tracking.settings.keyframe1= 1;
        clip->tracking.settings.keyframe2= 30;
        clip->tracking.settings.dist= 1;
+       clip->tracking.settings.corr= 0.75;
 
        clip->tracking.stabilization.scaleinf= 1.f;
        clip->tracking.stabilization.locinf= 1.f;
@@ -483,17 +484,30 @@ ImBuf *BKE_movieclip_acquire_ibuf_flag(MovieClip *clip, MovieClipUser *user, int
        ImBuf *ibuf= NULL;
        int framenr= user?user->framenr:clip->lastframe;
 
-       if(clip->source==MCLIP_SRC_SEQUENCE) {
-               ibuf= movieclip_load_sequence_file(clip, framenr, flag);
-       } else {
-               /* loading of movies can't happen from concurent threads */
-               BLI_lock_thread(LOCK_MOVIECLIP);
+       /* cache isn't threadsafe itself and also loading of movies
+          can't happen from concurent threads that's why we use lock here */
+       BLI_lock_thread(LOCK_MOVIECLIP);
 
-               ibuf= movieclip_load_movie_file(clip, framenr, flag);
+       /* cache is supposed to be threadsafe */
+       ibuf= get_imbuf_cache(clip, user, flag);
 
-               BLI_unlock_thread(LOCK_MOVIECLIP);
+       if(!ibuf) {
+               if(clip->source==MCLIP_SRC_SEQUENCE) {
+                       ibuf= movieclip_load_sequence_file(clip, framenr, flag);
+               } else {
+                       ibuf= movieclip_load_movie_file(clip, framenr, flag);
+               }
+
+               if(ibuf) {
+                       int bits= MCLIP_USE_PROXY|MCLIP_USE_PROXY_CUSTOM_DIR;
+
+                       if((flag&bits)==(clip->flag&bits))
+                               put_imbuf_cache(clip, user, ibuf, clip->flag);
+               }
        }
 
+       BLI_unlock_thread(LOCK_MOVIECLIP);
+
        return ibuf;
 }
 
@@ -586,7 +600,7 @@ void BKE_movieclip_aspect(MovieClip *clip, float *aspx, float *aspy)
        *aspx= *aspy= 1.0;
 
        /* x is always 1 */
-       *aspy = clip->aspy/clip->aspx;
+       *aspy = clip->aspy/clip->aspx/clip->tracking.camera.pixel_aspect;
 }
 
 /* get segments of cached frames. useful for debugging cache policies */
@@ -659,40 +673,15 @@ void BKE_movieclip_select_track(MovieClip *clip, MovieTrackingTrack *track, int
                        cur= cur->next;
                }
        }
-
-       if(!TRACK_SELECTED(track))
-               BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
 }
 
 void BKE_movieclip_deselect_track(MovieClip *clip, MovieTrackingTrack *track, int area)
 {
        BKE_tracking_track_flag(track, area, SELECT, 1);
-
-       if(!TRACK_SELECTED(track))
-               BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
-}
-
-void BKE_movieclip_set_selection(MovieClip *clip, int type, void *sel)
-{
-       clip->sel_type= type;
-
-       if(type == MCLIP_SEL_NONE) clip->last_sel= NULL;
-       else clip->last_sel= sel;
-}
-
-void BKE_movieclip_last_selection(MovieClip *clip, int *type, void **sel)
-{
-       *type= clip->sel_type;
-
-       if(clip->sel_type == MCLIP_SEL_NONE) *sel= NULL;
-       else *sel= clip->last_sel;
 }
 
 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
 {
-       void *sel;
-       int sel_type;
-
        if(scopes->ok) return;
 
        if(scopes->track_preview) {
@@ -704,10 +693,8 @@ void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClip
        scopes->track= NULL;
 
        if(clip) {
-               BKE_movieclip_last_selection(clip, &sel_type, &sel);
-
-               if(sel_type==MCLIP_SEL_TRACK) {
-                       MovieTrackingTrack *track= (MovieTrackingTrack*)sel;
+               if(clip->tracking.act_track) {
+                       MovieTrackingTrack *track= clip->tracking.act_track;
                        MovieTrackingMarker *marker= BKE_tracking_get_marker(track, user->framenr);
 
                        if(marker->flag&MARKER_DISABLED) {
@@ -751,7 +738,7 @@ void BKE_movieclip_build_proxy_frame(MovieClip *clip, int cfra, int proxy_render
 {
        char name[FILE_MAXFILE+FILE_MAXDIR];
        int quality, rectx, recty, size;
-       struct ImBuf * ibuf;
+       ImBuf *ibuf;
        MovieClipUser user;
 
        size= rendersize_to_number(proxy_render_size);
@@ -761,22 +748,37 @@ void BKE_movieclip_build_proxy_frame(MovieClip *clip, int cfra, int proxy_render
 
        ibuf= BKE_movieclip_acquire_ibuf_flag(clip, &user, 0);
 
-       rectx= ibuf->x*size/100.f;
-       recty= ibuf->y*size/100.f;
+       if(ibuf) {
+               ImBuf *scaleibuf= ibuf;
 
-       if (ibuf->x != rectx || ibuf->y != recty) {
-               IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
-       }
+               rectx= ibuf->x*size/100.f;
+               recty= ibuf->y*size/100.f;
 
-       quality= clip->proxy.quality;
-       ibuf->ftype= JPG | quality;
+               if (ibuf->x != rectx || ibuf->y != recty) {
+                       if(ibuf->refcounter)    /* means buffer came from cache and shouldn't be modified */
+                               scaleibuf= IMB_dupImBuf(ibuf);
 
-       BLI_make_existing_file(name);
+                       IMB_scalefastImBuf(scaleibuf, (short)rectx, (short)recty);
+               }
+
+               quality= clip->proxy.quality;
+               scaleibuf->ftype= JPG | quality;
+
+               /* unsupported feature only confuses other s/w */
+               if(scaleibuf->depth==32)
+                       scaleibuf->depth= 24;
+
+               BLI_lock_thread(LOCK_MOVIECLIP);
 
-       if(IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)==0)
-               perror(name);
+               BLI_make_existing_file(name);
+               if(IMB_saveiff(scaleibuf, name, IB_rect)==0)
+                       perror(name);
 
-       IMB_freeImBuf(ibuf);
+               BLI_unlock_thread(LOCK_MOVIECLIP);
+
+               IMB_freeImBuf(ibuf);
+               if(scaleibuf!=ibuf) IMB_freeImBuf(scaleibuf);
+       }
 }
 
 void free_movieclip(MovieClip *clip)