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