Merge trunk with branch
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "BLI_blenlib.h"
33
34 #include "DNA_userdef_types.h"
35 #include "BKE_global.h"
36
37 #include "imbuf.h"
38 #include "imbuf_patch.h"
39 #include "IMB_imbuf_types.h"
40 #include "IMB_imbuf.h"
41
42 #include "IMB_targa.h"
43 #include "IMB_png.h"
44
45 #ifdef WITH_DDS
46 #include "dds/dds_api.h"
47 #endif
48
49 #include "IMB_bmp.h"
50 #include "IMB_tiff.h"
51 #include "IMB_radiance_hdr.h"
52 #include "IMB_dpxcineon.h"
53
54 #include "IMB_anim.h"
55
56 #ifdef WITH_OPENEXR
57 #include "openexr/openexr_api.h"
58 #endif
59
60 #ifdef WITH_QUICKTIME
61 #include "quicktime_import.h"
62 #endif
63
64 #ifdef WITH_FFMPEG
65 #include <ffmpeg/avcodec.h>
66 #include <ffmpeg/avformat.h>
67
68 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
69 #define FFMPEG_OLD_FRAME_RATE 1
70 #else
71 #define FFMPEG_CODEC_IS_POINTER 1
72 #endif
73
74 #endif
75
76 #define UTIL_DEBUG 0
77
78 /* from misc_util: flip the bytes from x  */
79 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
80
81 /* this one is only def-ed once, strangely... */
82 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
83
84 static int IMB_ispic_name(char *name)
85 {
86         struct stat st;
87         int fp, buf[10];
88         int ofs = 0;
89
90         if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
91         
92         if (ib_stat(name,&st) == -1) return(0);
93         if (((st.st_mode) & S_IFMT) == S_IFREG){
94                 if ((fp = open(name,O_BINARY|O_RDONLY)) >= 0){
95                         if (read(fp,buf,32)==32){
96                                 close(fp);
97                                 if (buf[ofs] == CAT) ofs += 3;
98                                 if (buf[ofs] == FORM){
99                                         if (buf[ofs + 2] == ILBM) return(AMI);
100                                         if (buf[ofs + 2] == ANIM){
101                                                 if (buf[ofs + 3] == FORM){
102                                                         return(ANIM);
103                                                 }else{
104                                                         return(Anim);
105                                                 }
106                                         }
107                                 } else {
108                                         if (GS(buf) == IMAGIC) return(IMAGIC);
109                                         if (GSS(buf) == IMAGIC) return(IMAGIC);
110                                         if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return(JPG);
111
112                                         /* at windows there are ".ffl" files with the same magic numnber... 
113                                            besides that,  tim images are not really important anymore! */
114                                         /* if ((BIG_LONG(buf[0]) == 0x10000000) && ((BIG_LONG(buf[1]) & 0xf0ffffff) == 0)) return(TIM); */
115
116                                 }
117                                 if (imb_is_a_png(buf)) return(PNG);
118 #ifdef WITH_DDS
119                                 if (imb_is_a_dds((uchar *)buf)) return(DDS);
120 #endif
121                                 if (imb_is_a_targa(buf)) return(TGA);
122 #ifdef WITH_OPENEXR
123                                 if (imb_is_a_openexr((uchar *)buf)) return(OPENEXR);
124 #endif
125                                 if (imb_is_a_tiff(buf)) return(TIF);
126                                 if (imb_is_dpx(buf)) return (DPX);
127                                 if (imb_is_cineon(buf)) return(CINEON);
128                                 /* radhdr: check if hdr format */
129                                 if (imb_is_a_hdr(buf)) return(RADHDR);
130
131 /*
132                                 if (imb_is_a_bmp(buf)) return(BMP);
133 */
134
135 #ifdef WITH_QUICKTIME
136 #if defined(_WIN32) || defined(__APPLE__)
137                                 if(G.have_quicktime) {
138                                         if (imb_is_a_quicktime(name)) return(QUICKTIME);
139                                 }
140 #endif
141 #endif
142
143                                 return(FALSE);
144                         }
145                         close(fp);
146                 }
147         }
148         return(FALSE);
149 }
150
151
152
153 int IMB_ispic(char *filename)
154 {
155         if(U.uiflag & USER_FILTERFILEEXTS) {
156                 if (G.have_libtiff && (BLI_testextensie(filename, ".tif")
157                                 ||      BLI_testextensie(filename, ".tiff"))) {
158                                 return IMB_ispic_name(filename);
159                 }
160                 if (G.have_quicktime){
161                         if(             BLI_testextensie(filename, ".jpg")
162                                 ||      BLI_testextensie(filename, ".jpeg")
163                                 ||      BLI_testextensie(filename, ".tif")
164                                 ||      BLI_testextensie(filename, ".tiff")
165                                 ||      BLI_testextensie(filename, ".hdr")
166                                 ||      BLI_testextensie(filename, ".tga")
167                                 ||      BLI_testextensie(filename, ".rgb")
168                                 ||      BLI_testextensie(filename, ".bmp")
169                                 ||      BLI_testextensie(filename, ".png")
170 #ifdef WITH_DDS
171                                 ||      BLI_testextensie(filename, ".dds")
172 #endif
173                                 ||      BLI_testextensie(filename, ".iff")
174                                 ||      BLI_testextensie(filename, ".lbm")
175                                 ||      BLI_testextensie(filename, ".gif")
176                                 ||      BLI_testextensie(filename, ".psd")
177                                 ||      BLI_testextensie(filename, ".pct")
178                                 ||      BLI_testextensie(filename, ".pict")
179                                 ||      BLI_testextensie(filename, ".pntg") //macpaint
180                                 ||      BLI_testextensie(filename, ".qtif")
181                                 ||      BLI_testextensie(filename, ".cin")
182 #ifdef WITH_BF_OPENEXR
183                                 ||      BLI_testextensie(filename, ".exr")
184 #endif
185                                 ||      BLI_testextensie(filename, ".sgi")) {
186                                 return IMB_ispic_name(filename);
187                         } else {
188                                 return(FALSE);                  
189                         }
190                 } else { /* no quicktime or libtiff */
191                         if(             BLI_testextensie(filename, ".jpg")
192                                 ||      BLI_testextensie(filename, ".jpeg")
193                                 ||      BLI_testextensie(filename, ".hdr")
194                                 ||      BLI_testextensie(filename, ".tga")
195                                 ||      BLI_testextensie(filename, ".rgb")
196                                 ||      BLI_testextensie(filename, ".bmp")
197                                 ||      BLI_testextensie(filename, ".png")
198                                 ||      BLI_testextensie(filename, ".cin")
199 #ifdef WITH_DDS
200                                 ||      BLI_testextensie(filename, ".dds")
201 #endif
202 #ifdef WITH_BF_OPENEXR
203                                 ||      BLI_testextensie(filename, ".exr")
204 #endif
205                                 ||      BLI_testextensie(filename, ".iff")
206                                 ||      BLI_testextensie(filename, ".lbm")
207                                 ||      BLI_testextensie(filename, ".sgi")) {
208                                 return IMB_ispic_name(filename);
209                         }
210                         else  {
211                                 return(FALSE);
212                         }
213                 }
214         } else { /* no FILTERFILEEXTS */
215                 return IMB_ispic_name(filename);
216         }
217 }
218
219
220
221 static int isavi (char *name) {
222         return AVI_is_avi (name);
223 }
224
225 #ifdef WITH_QUICKTIME
226 static int isqtime (char *name) {
227         return anim_is_quicktime (name);
228 }
229 #endif
230
231 #ifdef WITH_FFMPEG
232 extern void do_init_ffmpeg();
233 void do_init_ffmpeg()
234 {
235         static int ffmpeg_init = 0;
236         if (!ffmpeg_init) {
237                 ffmpeg_init = 1;
238                 av_register_all();
239         }
240 }
241
242 #ifdef FFMPEG_CODEC_IS_POINTER
243 static AVCodecContext* get_codec_from_stream(AVStream* stream)
244 {
245         return stream->codec;
246 }
247 #else
248 static AVCodecContext* get_codec_from_stream(AVStream* stream)
249 {
250         return &stream->codec;
251 }
252 #endif
253
254
255 static int isffmpeg (char *filename) {
256         AVFormatContext *pFormatCtx;
257         int            i, videoStream;
258         AVCodec *pCodec;
259         AVCodecContext *pCodecCtx;
260
261         do_init_ffmpeg();
262
263         if( BLI_testextensie(filename, ".swf") ||
264                 BLI_testextensie(filename, ".jpg") ||
265                 BLI_testextensie(filename, ".png") ||
266                 BLI_testextensie(filename, ".dds") ||
267                 BLI_testextensie(filename, ".tga") ||
268                 BLI_testextensie(filename, ".bmp") ||
269                 BLI_testextensie(filename, ".exr") ||
270                 BLI_testextensie(filename, ".cin") ||
271                 BLI_testextensie(filename, ".wav")) return 0;
272
273         if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
274                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
275                 return 0;
276         }
277
278         if(av_find_stream_info(pFormatCtx)<0) {
279                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
280                 av_close_input_file(pFormatCtx);
281                 return 0;
282         }
283
284         if(UTIL_DEBUG) dump_format(pFormatCtx, 0, filename, 0);
285
286
287         /* Find the first video stream */
288         videoStream=-1;
289         for(i=0; i<pFormatCtx->nb_streams; i++)
290                 if(pFormatCtx->streams[i] &&
291                    get_codec_from_stream(pFormatCtx->streams[i]) && 
292                   (get_codec_from_stream(pFormatCtx->streams[i])->codec_type==CODEC_TYPE_VIDEO))
293                 {
294                         videoStream=i;
295                         break;
296                 }
297
298         if(videoStream==-1) {
299                 av_close_input_file(pFormatCtx);
300                 return 0;
301         }
302
303         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
304
305         /* Find the decoder for the video stream */
306         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
307         if(pCodec==NULL) {
308                 avcodec_close(pCodecCtx);
309                 av_close_input_file(pFormatCtx);
310                 return 0;
311         }
312
313         if(avcodec_open(pCodecCtx, pCodec)<0) {
314                 avcodec_close(pCodecCtx);
315                 av_close_input_file(pFormatCtx);
316                 return 0;
317         }
318
319         avcodec_close(pCodecCtx);
320         av_close_input_file(pFormatCtx);
321
322         return 1;
323 }
324 #endif
325
326 int imb_get_anim_type(char * name) {
327         int type;
328         struct stat st;
329
330         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
331
332 #ifndef _WIN32
333 #       ifdef WITH_FFMPEG
334         /* stat test below fails on large files > 4GB */
335         if (isffmpeg(name)) return (ANIM_FFMPEG);
336 #       endif
337         if (ib_stat(name,&st) == -1) return(0);
338         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
339
340         if (isavi(name)) return (ANIM_AVI);
341
342         if (ismovie(name)) return (ANIM_MOVIE);
343 #       ifdef WITH_QUICKTIME
344         if (isqtime(name)) return (ANIM_QTIME);
345 #       endif
346 #else
347         if (ib_stat(name,&st) == -1) return(0);
348         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
349
350         if (isavi(name)) return (ANIM_AVI);
351
352         if (ismovie(name)) return (ANIM_MOVIE);
353 #       ifdef WITH_QUICKTIME
354         if (isqtime(name)) return (ANIM_QTIME);
355 #       endif
356 #       ifdef WITH_FFMPEG
357         if (isffmpeg(name)) return (ANIM_FFMPEG);
358 #       endif
359 #endif
360         type = IMB_ispic(name);
361         if (type == ANIM) return (ANIM_ANIM5);
362         if (type) return(ANIM_SEQUENCE);
363         return(0);
364 }
365  
366 int IMB_isanim(char *filename) {
367         int type;
368         
369         if(U.uiflag & USER_FILTERFILEEXTS) {
370                 if (G.have_quicktime){
371                         if(             BLI_testextensie(filename, ".avi")
372                                 ||      BLI_testextensie(filename, ".flc")
373                                 ||      BLI_testextensie(filename, ".dv")
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, ".mv")) {
385                                 type = imb_get_anim_type(filename);
386                         }
387                         else  {
388                                 return(FALSE);
389                         }
390                 }
391         } else { // no FILTERFILEEXTS
392                 type = imb_get_anim_type(filename);
393         }
394         
395         return (type && type!=ANIM_SEQUENCE);
396 }