Blender TIFF support
[blender.git] / source / blender / imbuf / intern / util.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * util.c
28  *
29  * $Id$
30  */
31
32 #ifdef _WIN32
33 #include <io.h>
34 #define open _open
35 #define read _read
36 #define close _close
37 #endif
38
39 #include "BLI_blenlib.h"
40
41 #include "DNA_userdef_types.h"
42 #include "BKE_global.h"
43
44 #include "imbuf.h"
45 #include "IMB_imbuf_types.h"
46 #include "IMB_imbuf.h"
47 #include "IMB_filetype.h"
48
49 #include "IMB_anim.h"
50
51 #ifdef WITH_QUICKTIME
52 #include "quicktime_import.h"
53 #endif
54
55 #ifdef WITH_FFMPEG
56 #include <libavcodec/avcodec.h>
57 #include <libavformat/avformat.h>
58 #include <libavdevice/avdevice.h>
59 #include <libavutil/log.h>
60
61 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
62 #define FFMPEG_OLD_FRAME_RATE 1
63 #else
64 #define FFMPEG_CODEC_IS_POINTER 1
65 #endif
66
67 #endif
68
69 #define UTIL_DEBUG 0
70
71 static int IMB_ispic_name(char *name)
72 {
73         ImFileType *type;
74         struct stat st;
75         int fp, buf[10];
76
77         if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
78         
79         if(stat(name,&st) == -1)
80                 return FALSE;
81         if(((st.st_mode) & S_IFMT) != S_IFREG)
82                 return FALSE;
83
84         if((fp = open(name,O_BINARY|O_RDONLY)) < 0)
85                 return FALSE;
86
87         if(read(fp, buf, 32) != 32) {
88                 close(fp);
89                 return FALSE;
90         }
91
92         close(fp);
93
94         /* XXX move this exception */
95         if((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0)
96                 return JPG;
97
98         for(type=IMB_FILE_TYPES; type->is_a; type++)
99                 if(type->is_a((uchar*)buf))
100                         return type->filetype;
101
102         return FALSE;
103 }
104
105 int IMB_ispic(char *filename)
106 {
107         if(U.uiflag & USER_FILTERFILEEXTS) {
108                 if (BLI_testextensie(filename, ".tif")
109                                 ||      BLI_testextensie(filename, ".tiff")
110                                 ||      BLI_testextensie(filename, ".tx")) {
111                                 return IMB_ispic_name(filename);
112                 }
113                 if (G.have_quicktime){
114                         if(             BLI_testextensie(filename, ".jpg")
115                                 ||      BLI_testextensie(filename, ".jpeg")
116 #ifdef WITH_TIFF
117                                 ||      BLI_testextensie(filename, ".tif")
118                                 ||      BLI_testextensie(filename, ".tiff")
119                                 ||      BLI_testextensie(filename, ".tx")
120 #endif
121                                 ||      BLI_testextensie(filename, ".hdr")
122                                 ||      BLI_testextensie(filename, ".tga")
123                                 ||      BLI_testextensie(filename, ".rgb")
124                                 ||      BLI_testextensie(filename, ".bmp")
125                                 ||      BLI_testextensie(filename, ".png")
126 #ifdef WITH_DDS
127                                 ||      BLI_testextensie(filename, ".dds")
128 #endif
129                                 ||      BLI_testextensie(filename, ".iff")
130                                 ||      BLI_testextensie(filename, ".lbm")
131                                 ||      BLI_testextensie(filename, ".gif")
132                                 ||      BLI_testextensie(filename, ".psd")
133                                 ||      BLI_testextensie(filename, ".pct")
134                                 ||      BLI_testextensie(filename, ".pict")
135                                 ||      BLI_testextensie(filename, ".pntg") //macpaint
136                                 ||      BLI_testextensie(filename, ".qtif")
137                                 ||      BLI_testextensie(filename, ".cin")
138 #ifdef WITH_BF_OPENEXR
139                                 ||      BLI_testextensie(filename, ".exr")
140 #endif
141 #ifdef WITH_BF_OPENJPEG
142                                 ||      BLI_testextensie(filename, ".jp2")
143 #endif
144                                 ||      BLI_testextensie(filename, ".sgi")) {
145                                 return IMB_ispic_name(filename);
146                         } else {
147                                 return(FALSE);                  
148                         }
149                 } else { /* no quicktime */
150                         if(             BLI_testextensie(filename, ".jpg")
151                                 ||      BLI_testextensie(filename, ".jpeg")
152 #ifdef WITH_TIFF
153                                 ||      BLI_testextensie(filename, ".tif")
154                                 ||      BLI_testextensie(filename, ".tiff")
155                                 ||      BLI_testextensie(filename, ".tx")
156 #endif
157                                 ||      BLI_testextensie(filename, ".hdr")
158                                 ||      BLI_testextensie(filename, ".tga")
159                                 ||      BLI_testextensie(filename, ".rgb")
160                                 ||      BLI_testextensie(filename, ".bmp")
161                                 ||      BLI_testextensie(filename, ".png")
162                                 ||      BLI_testextensie(filename, ".cin")
163 #ifdef WITH_DDS
164                                 ||      BLI_testextensie(filename, ".dds")
165 #endif
166 #ifdef WITH_BF_OPENEXR
167                                 ||      BLI_testextensie(filename, ".exr")
168 #endif
169 #ifdef WITH_BF_OPENJPEG
170                                 ||      BLI_testextensie(filename, ".jp2")
171 #endif
172                                 ||      BLI_testextensie(filename, ".iff")
173                                 ||      BLI_testextensie(filename, ".lbm")
174                                 ||      BLI_testextensie(filename, ".sgi")) {
175                                 return IMB_ispic_name(filename);
176                         }
177                         else  {
178                                 return(FALSE);
179                         }
180                 }
181         } else { /* no FILTERFILEEXTS */
182                 return IMB_ispic_name(filename);
183         }
184 }
185
186
187
188 static int isavi (char *name) {
189         return AVI_is_avi (name);
190 }
191
192 #ifdef WITH_QUICKTIME
193 static int isqtime (char *name) {
194         return anim_is_quicktime (name);
195 }
196 #endif
197
198 #ifdef WITH_FFMPEG
199
200 void silence_log_ffmpeg(int quiet)
201 {
202         if (quiet)
203         {
204                 av_log_set_level(AV_LOG_QUIET);
205         }
206         else
207         {
208                 av_log_set_level(AV_LOG_INFO);
209         }
210 }
211
212 extern void do_init_ffmpeg();
213 void do_init_ffmpeg()
214 {
215         static int ffmpeg_init = 0;
216         if (!ffmpeg_init) {
217                 ffmpeg_init = 1;
218                 av_register_all();
219                 avdevice_register_all();
220                 
221                 if ((G.f & G_DEBUG) == 0)
222                 {
223                         silence_log_ffmpeg(1);
224                 }
225         }
226 }
227
228 #ifdef FFMPEG_CODEC_IS_POINTER
229 static AVCodecContext* get_codec_from_stream(AVStream* stream)
230 {
231         return stream->codec;
232 }
233 #else
234 static AVCodecContext* get_codec_from_stream(AVStream* stream)
235 {
236         return &stream->codec;
237 }
238 #endif
239
240
241 static int isffmpeg (char *filename) {
242         AVFormatContext *pFormatCtx;
243         unsigned int i;
244         int videoStream;
245         AVCodec *pCodec;
246         AVCodecContext *pCodecCtx;
247
248         do_init_ffmpeg();
249
250         if( BLI_testextensie(filename, ".swf") ||
251                 BLI_testextensie(filename, ".jpg") ||
252                 BLI_testextensie(filename, ".png") ||
253                 BLI_testextensie(filename, ".dds") ||
254                 BLI_testextensie(filename, ".tga") ||
255                 BLI_testextensie(filename, ".bmp") ||
256                 BLI_testextensie(filename, ".exr") ||
257                 BLI_testextensie(filename, ".cin") ||
258                 BLI_testextensie(filename, ".wav")) return 0;
259
260         if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
261                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
262                 return 0;
263         }
264
265         if(av_find_stream_info(pFormatCtx)<0) {
266                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
267                 av_close_input_file(pFormatCtx);
268                 return 0;
269         }
270
271         if(UTIL_DEBUG) dump_format(pFormatCtx, 0, filename, 0);
272
273
274                 /* Find the first video stream */
275         videoStream=-1;
276         for(i=0; i<pFormatCtx->nb_streams; i++)
277                 if(pFormatCtx->streams[i] &&
278                    get_codec_from_stream(pFormatCtx->streams[i]) && 
279                   (get_codec_from_stream(pFormatCtx->streams[i])->codec_type==CODEC_TYPE_VIDEO))
280                 {
281                         videoStream=i;
282                         break;
283                 }
284
285         if(videoStream==-1) {
286                 av_close_input_file(pFormatCtx);
287                 return 0;
288         }
289
290         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
291
292                 /* Find the decoder for the video stream */
293         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
294         if(pCodec==NULL) {
295                 av_close_input_file(pFormatCtx);
296                 return 0;
297         }
298
299         if(avcodec_open(pCodecCtx, pCodec)<0) {
300                 av_close_input_file(pFormatCtx);
301                 return 0;
302         }
303
304         avcodec_close(pCodecCtx);
305         av_close_input_file(pFormatCtx);
306
307         return 1;
308 }
309 #endif
310
311 #ifdef WITH_REDCODE
312 static int isredcode(char * filename)
313 {
314         struct redcode_handle * h = redcode_open(filename);
315         if (!h) {
316                 return 0;
317         }
318         redcode_close(h);
319         return 1;
320 }
321
322 #endif
323
324 int imb_get_anim_type(char * name) {
325         int type;
326         struct stat st;
327
328         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
329
330 #ifndef _WIN32
331 #       ifdef WITH_QUICKTIME
332         if (isqtime(name)) return (ANIM_QTIME);
333 #       endif
334 #       ifdef WITH_FFMPEG
335         /* stat test below fails on large files > 4GB */
336         if (isffmpeg(name)) return (ANIM_FFMPEG);
337 #       endif
338         if (stat(name,&st) == -1) return(0);
339         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
340
341         if (isavi(name)) return (ANIM_AVI);
342
343         if (ismovie(name)) return (ANIM_MOVIE);
344 #else
345         if (stat(name,&st) == -1) return(0);
346         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
347
348         if (ismovie(name)) return (ANIM_MOVIE);
349 #       ifdef WITH_QUICKTIME
350         if (isqtime(name)) return (ANIM_QTIME);
351 #       endif
352 #       ifdef WITH_FFMPEG
353         if (isffmpeg(name)) return (ANIM_FFMPEG);
354 #       endif
355         if (isavi(name)) return (ANIM_AVI);
356 #endif
357 #ifdef WITH_REDCODE
358         if (isredcode(name)) return (ANIM_REDCODE);
359 #endif
360         type = IMB_ispic(name);
361         if (type) return(ANIM_SEQUENCE);
362         return(0);
363 }
364  
365 int IMB_isanim(char *filename) {
366         int type;
367         
368         if(U.uiflag & USER_FILTERFILEEXTS) {
369                 if (G.have_quicktime){
370                         if(             BLI_testextensie(filename, ".avi")
371                                 ||      BLI_testextensie(filename, ".flc")
372                                 ||      BLI_testextensie(filename, ".dv")
373                                 ||      BLI_testextensie(filename, ".r3d")
374                                 ||      BLI_testextensie(filename, ".mov")
375                                 ||      BLI_testextensie(filename, ".movie")
376                                 ||      BLI_testextensie(filename, ".mv")) {
377                                 type = imb_get_anim_type(filename);
378                         } else {
379                                 return(FALSE);                  
380                         }
381                 } else { // no quicktime
382                         if(             BLI_testextensie(filename, ".avi")
383                                 ||      BLI_testextensie(filename, ".dv")
384                                 ||      BLI_testextensie(filename, ".r3d")
385                                 ||      BLI_testextensie(filename, ".mv")) {
386                                 type = imb_get_anim_type(filename);
387                         }
388                         else  {
389                                 return(FALSE);
390                         }
391                 }
392         } else { // no FILTERFILEEXTS
393                 type = imb_get_anim_type(filename);
394         }
395         
396         return (type && type!=ANIM_SEQUENCE);
397 }