build options to disable image formats WITH_CINEON, WITH_HDR.
[blender-staging.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 #ifdef WITH_HDR
122                                 ||      BLI_testextensie(filename, ".hdr")
123 #endif
124                                 ||      BLI_testextensie(filename, ".tga")
125                                 ||      BLI_testextensie(filename, ".rgb")
126                                 ||      BLI_testextensie(filename, ".bmp")
127                                 ||      BLI_testextensie(filename, ".png")
128 #ifdef WITH_DDS
129                                 ||      BLI_testextensie(filename, ".dds")
130 #endif
131                                 ||      BLI_testextensie(filename, ".iff")
132                                 ||      BLI_testextensie(filename, ".lbm")
133                                 ||      BLI_testextensie(filename, ".gif")
134                                 ||      BLI_testextensie(filename, ".psd")
135                                 ||      BLI_testextensie(filename, ".pct")
136                                 ||      BLI_testextensie(filename, ".pict")
137                                 ||      BLI_testextensie(filename, ".pntg") //macpaint
138                                 ||      BLI_testextensie(filename, ".qtif")
139 #ifdef WITH_CINEON                              
140                                 ||      BLI_testextensie(filename, ".dpx")
141                                 ||      BLI_testextensie(filename, ".cin")
142 #endif
143 #ifdef WITH_BF_OPENEXR
144                                 ||      BLI_testextensie(filename, ".exr")
145 #endif
146 #ifdef WITH_BF_OPENJPEG
147                                 ||      BLI_testextensie(filename, ".jp2")
148 #endif
149                                 ||      BLI_testextensie(filename, ".sgi")) {
150                                 return IMB_ispic_name(filename);
151                         } else {
152                                 return(FALSE);                  
153                         }
154                 } else { /* no quicktime */
155                         if(             BLI_testextensie(filename, ".jpg")
156                                 ||      BLI_testextensie(filename, ".jpeg")
157 #ifdef WITH_TIFF
158                                 ||      BLI_testextensie(filename, ".tif")
159                                 ||      BLI_testextensie(filename, ".tiff")
160                                 ||      BLI_testextensie(filename, ".tx")
161 #endif
162 #ifdef WITH_HDR
163                                 ||      BLI_testextensie(filename, ".hdr")
164 #endif
165                                 ||      BLI_testextensie(filename, ".tga")
166                                 ||      BLI_testextensie(filename, ".rgb")
167                                 ||      BLI_testextensie(filename, ".bmp")
168                                 ||      BLI_testextensie(filename, ".png")
169 #ifdef WITH_CINEON
170                                 ||      BLI_testextensie(filename, ".cin")
171                                 ||      BLI_testextensie(filename, ".dpx")
172 #endif
173 #ifdef WITH_DDS
174                                 ||      BLI_testextensie(filename, ".dds")
175 #endif
176 #ifdef WITH_BF_OPENEXR
177                                 ||      BLI_testextensie(filename, ".exr")
178 #endif
179 #ifdef WITH_BF_OPENJPEG
180                                 ||      BLI_testextensie(filename, ".jp2")
181 #endif
182                                 ||      BLI_testextensie(filename, ".iff")
183                                 ||      BLI_testextensie(filename, ".lbm")
184                                 ||      BLI_testextensie(filename, ".sgi")) {
185                                 return IMB_ispic_name(filename);
186                         }
187                         else  {
188                                 return(FALSE);
189                         }
190                 }
191         } else { /* no FILTERFILEEXTS */
192                 return IMB_ispic_name(filename);
193         }
194 }
195
196
197
198 static int isavi (char *name) {
199         return AVI_is_avi (name);
200 }
201
202 #ifdef WITH_QUICKTIME
203 static int isqtime (char *name) {
204         return anim_is_quicktime (name);
205 }
206 #endif
207
208 #ifdef WITH_FFMPEG
209
210 void silence_log_ffmpeg(int quiet)
211 {
212         if (quiet)
213         {
214                 av_log_set_level(AV_LOG_QUIET);
215         }
216         else
217         {
218                 av_log_set_level(AV_LOG_INFO);
219         }
220 }
221
222 extern void do_init_ffmpeg();
223 void do_init_ffmpeg()
224 {
225         static int ffmpeg_init = 0;
226         if (!ffmpeg_init) {
227                 ffmpeg_init = 1;
228                 av_register_all();
229                 avdevice_register_all();
230                 
231                 if ((G.f & G_DEBUG) == 0)
232                 {
233                         silence_log_ffmpeg(1);
234                 }
235         }
236 }
237
238 #ifdef FFMPEG_CODEC_IS_POINTER
239 static AVCodecContext* get_codec_from_stream(AVStream* stream)
240 {
241         return stream->codec;
242 }
243 #else
244 static AVCodecContext* get_codec_from_stream(AVStream* stream)
245 {
246         return &stream->codec;
247 }
248 #endif
249
250
251 static int isffmpeg (char *filename) {
252         AVFormatContext *pFormatCtx;
253         unsigned int i;
254         int videoStream;
255         AVCodec *pCodec;
256         AVCodecContext *pCodecCtx;
257
258         do_init_ffmpeg();
259
260         if( BLI_testextensie(filename, ".swf") ||
261                 BLI_testextensie(filename, ".jpg") ||
262                 BLI_testextensie(filename, ".png") ||
263                 BLI_testextensie(filename, ".dds") ||
264                 BLI_testextensie(filename, ".tga") ||
265                 BLI_testextensie(filename, ".bmp") ||
266                 BLI_testextensie(filename, ".exr") ||
267                 BLI_testextensie(filename, ".cin") ||
268                 BLI_testextensie(filename, ".wav")) return 0;
269
270         if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
271                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
272                 return 0;
273         }
274
275         if(av_find_stream_info(pFormatCtx)<0) {
276                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
277                 av_close_input_file(pFormatCtx);
278                 return 0;
279         }
280
281         if(UTIL_DEBUG) dump_format(pFormatCtx, 0, filename, 0);
282
283
284                 /* Find the first video stream */
285         videoStream=-1;
286         for(i=0; i<pFormatCtx->nb_streams; i++)
287                 if(pFormatCtx->streams[i] &&
288                    get_codec_from_stream(pFormatCtx->streams[i]) && 
289                   (get_codec_from_stream(pFormatCtx->streams[i])->codec_type==CODEC_TYPE_VIDEO))
290                 {
291                         videoStream=i;
292                         break;
293                 }
294
295         if(videoStream==-1) {
296                 av_close_input_file(pFormatCtx);
297                 return 0;
298         }
299
300         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
301
302                 /* Find the decoder for the video stream */
303         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
304         if(pCodec==NULL) {
305                 av_close_input_file(pFormatCtx);
306                 return 0;
307         }
308
309         if(avcodec_open(pCodecCtx, pCodec)<0) {
310                 av_close_input_file(pFormatCtx);
311                 return 0;
312         }
313
314         avcodec_close(pCodecCtx);
315         av_close_input_file(pFormatCtx);
316
317         return 1;
318 }
319 #endif
320
321 #ifdef WITH_REDCODE
322 static int isredcode(char * filename)
323 {
324         struct redcode_handle * h = redcode_open(filename);
325         if (!h) {
326                 return 0;
327         }
328         redcode_close(h);
329         return 1;
330 }
331
332 #endif
333
334 int imb_get_anim_type(char * name) {
335         int type;
336         struct stat st;
337
338         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
339
340 #ifndef _WIN32
341 #       ifdef WITH_QUICKTIME
342         if (isqtime(name)) return (ANIM_QTIME);
343 #       endif
344 #       ifdef WITH_FFMPEG
345         /* stat test below fails on large files > 4GB */
346         if (isffmpeg(name)) return (ANIM_FFMPEG);
347 #       endif
348         if (stat(name,&st) == -1) return(0);
349         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
350
351         if (isavi(name)) return (ANIM_AVI);
352
353         if (ismovie(name)) return (ANIM_MOVIE);
354 #else
355         if (stat(name,&st) == -1) return(0);
356         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
357
358         if (ismovie(name)) return (ANIM_MOVIE);
359 #       ifdef WITH_QUICKTIME
360         if (isqtime(name)) return (ANIM_QTIME);
361 #       endif
362 #       ifdef WITH_FFMPEG
363         if (isffmpeg(name)) return (ANIM_FFMPEG);
364 #       endif
365         if (isavi(name)) return (ANIM_AVI);
366 #endif
367 #ifdef WITH_REDCODE
368         if (isredcode(name)) return (ANIM_REDCODE);
369 #endif
370         type = IMB_ispic(name);
371         if (type) return(ANIM_SEQUENCE);
372         return(0);
373 }
374  
375 int IMB_isanim(char *filename) {
376         int type;
377         
378         if(U.uiflag & USER_FILTERFILEEXTS) {
379                 if (G.have_quicktime){
380                         if(             BLI_testextensie(filename, ".avi")
381                                 ||      BLI_testextensie(filename, ".flc")
382                                 ||      BLI_testextensie(filename, ".dv")
383                                 ||      BLI_testextensie(filename, ".r3d")
384                                 ||      BLI_testextensie(filename, ".mov")
385                                 ||      BLI_testextensie(filename, ".movie")
386                                 ||      BLI_testextensie(filename, ".mv")) {
387                                 type = imb_get_anim_type(filename);
388                         } else {
389                                 return(FALSE);                  
390                         }
391                 } else { // no quicktime
392                         if(             BLI_testextensie(filename, ".avi")
393                                 ||      BLI_testextensie(filename, ".dv")
394                                 ||      BLI_testextensie(filename, ".r3d")
395                                 ||      BLI_testextensie(filename, ".mv")) {
396                                 type = imb_get_anim_type(filename);
397                         }
398                         else  {
399                                 return(FALSE);
400                         }
401                 }
402         } else { // no FILTERFILEEXTS
403                 type = imb_get_anim_type(filename);
404         }
405         
406         return (type && type!=ANIM_SEQUENCE);
407 }