Blender V2.61 - r43446

util.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * util.c
00027  *
00028  */
00029 
00035 #ifdef _WIN32
00036 #include <io.h>
00037 #define open _open
00038 #define read _read
00039 #define close _close
00040 #endif
00041 
00042 #include "BLI_blenlib.h"
00043 
00044 #include "DNA_userdef_types.h"
00045 #include "BKE_global.h"
00046 
00047 #include "imbuf.h"
00048 #include "IMB_imbuf_types.h"
00049 #include "IMB_imbuf.h"
00050 #include "IMB_filetype.h"
00051 
00052 #include "IMB_anim.h"
00053 
00054 #ifdef WITH_QUICKTIME
00055 #include "quicktime_import.h"
00056 #endif
00057 
00058 #ifdef WITH_FFMPEG
00059 #include <libavcodec/avcodec.h>
00060 #include <libavformat/avformat.h>
00061 #include <libavdevice/avdevice.h>
00062 #include <libavutil/log.h>
00063 
00064 #include "ffmpeg_compat.h"
00065 
00066 #endif
00067 
00068 #define UTIL_DEBUG 0
00069 
00070 const char *imb_ext_image[] = {
00071     ".png",
00072     ".tga",
00073     ".bmp",
00074     ".jpg", ".jpeg",
00075     ".sgi", ".rgb", ".rgba",
00076 #ifdef WITH_TIFF
00077     ".tif", ".tiff", ".tx",
00078 #endif
00079 #ifdef WITH_OPENJPEG
00080     ".jp2",
00081 #endif
00082 #ifdef WITH_HDR
00083     ".hdr",
00084 #endif
00085 #ifdef WITH_DDS
00086     ".dds",
00087 #endif
00088 #ifdef WITH_CINEON
00089     ".dpx",
00090     ".cin",
00091 #endif
00092 #ifdef WITH_OPENEXR
00093     ".exr",
00094 #endif
00095     NULL};
00096 
00097 const char *imb_ext_image_qt[] = {
00098     ".gif",
00099     ".psd",
00100     ".pct", ".pict",
00101     ".pntg",
00102     ".qtif",
00103     NULL};
00104 
00105 const char *imb_ext_movie[] = {
00106     ".avi",
00107     ".flc",
00108     ".mov",
00109     ".movie",
00110     ".mp4",
00111     ".m4v",
00112     ".m2v",
00113     ".m2t",
00114     ".m2ts",
00115     ".mts",
00116     ".mv",
00117     ".avs",
00118     ".wmv",
00119     ".ogv",
00120     ".dv",
00121     ".mpeg",
00122     ".mpg",
00123     ".mpg2",
00124     ".vob",
00125     ".mkv",
00126     ".flv",
00127     ".divx",
00128     ".xvid",
00129     ".mxf",
00130     NULL};
00131 
00132 /* sort of wrong being here... */
00133 const char *imb_ext_audio[] = {
00134     ".wav",
00135     ".ogg",
00136     ".oga",
00137     ".mp3",
00138     ".mp2",
00139     ".ac3",
00140     ".aac",
00141     ".flac",
00142     ".wma",
00143     ".eac3",
00144     ".aif",
00145     ".aiff",
00146     ".m4a",
00147     NULL};
00148 
00149 static int IMB_ispic_name(const char *name)
00150 {
00151     ImFileType *type;
00152     struct stat st;
00153     int fp, buf[10];
00154 
00155     if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
00156     
00157     if(stat(name,&st) == -1)
00158         return FALSE;
00159     if(((st.st_mode) & S_IFMT) != S_IFREG)
00160         return FALSE;
00161 
00162     if((fp = open(name,O_BINARY|O_RDONLY)) < 0)
00163         return FALSE;
00164 
00165     if(read(fp, buf, 32) != 32) {
00166         close(fp);
00167         return FALSE;
00168     }
00169 
00170     close(fp);
00171 
00172     /* XXX move this exception */
00173     if((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0)
00174         return JPG;
00175 
00176     for(type=IMB_FILE_TYPES; type->is_a; type++)
00177         if(type->is_a((uchar*)buf))
00178             return type->filetype;
00179 
00180     return FALSE;
00181 }
00182 
00183 int IMB_ispic(const char *filename)
00184 {
00185     if(U.uiflag & USER_FILTERFILEEXTS) {
00186         if( (BLI_testextensie_array(filename, imb_ext_image)) ||
00187             (G.have_quicktime && BLI_testextensie_array(filename, imb_ext_image_qt))
00188         ) {
00189             return IMB_ispic_name(filename);
00190         }
00191         else  {
00192             return FALSE;
00193         }
00194     }
00195     else { /* no FILTERFILEEXTS */
00196         return IMB_ispic_name(filename);
00197     }
00198 }
00199 
00200 
00201 
00202 static int isavi (const char *name)
00203 {
00204     return AVI_is_avi (name);
00205 }
00206 
00207 #ifdef WITH_QUICKTIME
00208 static int isqtime (const char *name)
00209 {
00210     return anim_is_quicktime (name);
00211 }
00212 #endif
00213 
00214 #ifdef WITH_FFMPEG
00215 
00216 void silence_log_ffmpeg(int quiet)
00217 {
00218     if (quiet)
00219     {
00220         av_log_set_level(AV_LOG_QUIET);
00221     }
00222     else
00223     {
00224         av_log_set_level(AV_LOG_DEBUG);
00225     }
00226 }
00227 
00228 extern void do_init_ffmpeg(void);
00229 void do_init_ffmpeg(void)
00230 {
00231     static int ffmpeg_init = 0;
00232     if (!ffmpeg_init) {
00233         ffmpeg_init = 1;
00234         av_register_all();
00235         avdevice_register_all();
00236         
00237         if ((G.f & G_DEBUG) == 0) {
00238             silence_log_ffmpeg(1);
00239         } else {
00240             silence_log_ffmpeg(0);
00241         }
00242     }
00243 }
00244 
00245 static int isffmpeg (const char *filename)
00246 {
00247     AVFormatContext *pFormatCtx;
00248     unsigned int i;
00249     int videoStream;
00250     AVCodec *pCodec;
00251     AVCodecContext *pCodecCtx;
00252 
00253     do_init_ffmpeg();
00254 
00255     if( BLI_testextensie(filename, ".swf") ||
00256         BLI_testextensie(filename, ".jpg") ||
00257         BLI_testextensie(filename, ".png") ||
00258         BLI_testextensie(filename, ".dds") ||
00259         BLI_testextensie(filename, ".tga") ||
00260         BLI_testextensie(filename, ".bmp") ||
00261         BLI_testextensie(filename, ".exr") ||
00262         BLI_testextensie(filename, ".cin") ||
00263         BLI_testextensie(filename, ".wav")) return 0;
00264 
00265     if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
00266         if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
00267         return 0;
00268     }
00269 
00270     if(av_find_stream_info(pFormatCtx)<0) {
00271         if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
00272         av_close_input_file(pFormatCtx);
00273         return 0;
00274     }
00275 
00276     if(UTIL_DEBUG) av_dump_format(pFormatCtx, 0, filename, 0);
00277 
00278 
00279         /* Find the first video stream */
00280     videoStream=-1;
00281     for(i=0; i<pFormatCtx->nb_streams; i++)
00282         if(pFormatCtx->streams[i] &&
00283            pFormatCtx->streams[i]->codec && 
00284           (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO))
00285         {
00286             videoStream=i;
00287             break;
00288         }
00289 
00290     if(videoStream==-1) {
00291         av_close_input_file(pFormatCtx);
00292         return 0;
00293     }
00294 
00295     pCodecCtx = pFormatCtx->streams[videoStream]->codec;
00296 
00297         /* Find the decoder for the video stream */
00298     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
00299     if(pCodec==NULL) {
00300         av_close_input_file(pFormatCtx);
00301         return 0;
00302     }
00303 
00304     if(avcodec_open(pCodecCtx, pCodec)<0) {
00305         av_close_input_file(pFormatCtx);
00306         return 0;
00307     }
00308 
00309     avcodec_close(pCodecCtx);
00310     av_close_input_file(pFormatCtx);
00311 
00312     return 1;
00313 }
00314 #endif
00315 
00316 #ifdef WITH_REDCODE
00317 static int isredcode(const char * filename)
00318 {
00319     struct redcode_handle * h = redcode_open(filename);
00320     if (!h) {
00321         return 0;
00322     }
00323     redcode_close(h);
00324     return 1;
00325 }
00326 
00327 #endif
00328 
00329 int imb_get_anim_type(const char * name)
00330 {
00331     int type;
00332     struct stat st;
00333 
00334     if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
00335 
00336 #ifndef _WIN32
00337 #   ifdef WITH_QUICKTIME
00338     if (isqtime(name)) return (ANIM_QTIME);
00339 #   endif
00340 #   ifdef WITH_FFMPEG
00341     /* stat test below fails on large files > 4GB */
00342     if (isffmpeg(name)) return (ANIM_FFMPEG);
00343 #   endif
00344     if (stat(name,&st) == -1) return(0);
00345     if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
00346 
00347     if (isavi(name)) return (ANIM_AVI);
00348 
00349     if (ismovie(name)) return (ANIM_MOVIE);
00350 #else
00351     if (stat(name,&st) == -1) return(0);
00352     if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
00353 
00354     if (ismovie(name)) return (ANIM_MOVIE);
00355 #   ifdef WITH_QUICKTIME
00356     if (isqtime(name)) return (ANIM_QTIME);
00357 #   endif
00358 #   ifdef WITH_FFMPEG
00359     if (isffmpeg(name)) return (ANIM_FFMPEG);
00360 #   endif
00361     if (isavi(name)) return (ANIM_AVI);
00362 #endif
00363 #ifdef WITH_REDCODE
00364     if (isredcode(name)) return (ANIM_REDCODE);
00365 #endif
00366     type = IMB_ispic(name);
00367     if (type) return(ANIM_SEQUENCE);
00368     return(0);
00369 }
00370  
00371 int IMB_isanim(const char *filename)
00372 {
00373     int type;
00374     
00375     if(U.uiflag & USER_FILTERFILEEXTS) {
00376         if (G.have_quicktime){
00377             if(     BLI_testextensie(filename, ".avi")
00378                 ||  BLI_testextensie(filename, ".flc")
00379                 ||  BLI_testextensie(filename, ".dv")
00380                 ||  BLI_testextensie(filename, ".r3d")
00381                 ||  BLI_testextensie(filename, ".mov")
00382                 ||  BLI_testextensie(filename, ".movie")
00383                 ||  BLI_testextensie(filename, ".mv")) {
00384                 type = imb_get_anim_type(filename);
00385             } else {
00386                 return(FALSE);          
00387             }
00388         } else { // no quicktime
00389             if(     BLI_testextensie(filename, ".avi")
00390                 ||  BLI_testextensie(filename, ".dv")
00391                 ||  BLI_testextensie(filename, ".r3d")
00392                 ||  BLI_testextensie(filename, ".mv")) {
00393                 type = imb_get_anim_type(filename);
00394             }
00395             else  {
00396                 return(FALSE);
00397             }
00398         }
00399     } else { // no FILTERFILEEXTS
00400         type = imb_get_anim_type(filename);
00401     }
00402     
00403     return (type && type!=ANIM_SEQUENCE);
00404 }