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