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