merging trunk 17221:17300
[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 #include "BKE_writeffmpeg.h" /* for silence_log_ffmpeg */
37
38 #include "imbuf.h"
39 #include "imbuf_patch.h"
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
42
43 #include "IMB_targa.h"
44 #include "IMB_png.h"
45
46 #ifdef WITH_DDS
47 #include "dds/dds_api.h"
48 #endif
49
50 #include "IMB_bmp.h"
51 #include "IMB_tiff.h"
52 #include "IMB_radiance_hdr.h"
53 #include "IMB_dpxcineon.h"
54
55 #include "IMB_anim.h"
56
57 #ifdef WITH_OPENEXR
58 #include "openexr/openexr_api.h"
59 #endif
60
61 #ifdef WITH_QUICKTIME
62 #include "quicktime_import.h"
63 #endif
64
65 #ifdef WITH_FFMPEG
66 #include <ffmpeg/avcodec.h>
67 #include <ffmpeg/avformat.h>
68 //#include <ffmpeg/avdevice.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 extern void do_init_ffmpeg();
235 void do_init_ffmpeg()
236 {
237         static int ffmpeg_init = 0;
238         if (!ffmpeg_init) {
239                 ffmpeg_init = 1;
240                 av_register_all();
241                 //avdevice_register_all();
242                 
243                 if ((G.f & G_DEBUG) == 0)
244                 {
245                         silence_log_ffmpeg(1);
246                 }
247         }
248 }
249
250 #ifdef FFMPEG_CODEC_IS_POINTER
251 static AVCodecContext* get_codec_from_stream(AVStream* stream)
252 {
253         return stream->codec;
254 }
255 #else
256 static AVCodecContext* get_codec_from_stream(AVStream* stream)
257 {
258         return &stream->codec;
259 }
260 #endif
261
262
263 static int isffmpeg (char *filename) {
264         AVFormatContext *pFormatCtx;
265         int            i, videoStream;
266         AVCodec *pCodec;
267         AVCodecContext *pCodecCtx;
268
269         do_init_ffmpeg();
270
271         if( BLI_testextensie(filename, ".swf") ||
272                 BLI_testextensie(filename, ".jpg") ||
273                 BLI_testextensie(filename, ".png") ||
274                 BLI_testextensie(filename, ".dds") ||
275                 BLI_testextensie(filename, ".tga") ||
276                 BLI_testextensie(filename, ".bmp") ||
277                 BLI_testextensie(filename, ".exr") ||
278                 BLI_testextensie(filename, ".cin") ||
279                 BLI_testextensie(filename, ".wav")) return 0;
280
281         if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
282                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
283                 return 0;
284         }
285
286         if(av_find_stream_info(pFormatCtx)<0) {
287                 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
288                 av_close_input_file(pFormatCtx);
289                 return 0;
290         }
291
292         if(UTIL_DEBUG) dump_format(pFormatCtx, 0, filename, 0);
293
294
295         /* Find the first video stream */
296         videoStream=-1;
297         for(i=0; i<pFormatCtx->nb_streams; i++)
298                 if(pFormatCtx->streams[i] &&
299                    get_codec_from_stream(pFormatCtx->streams[i]) && 
300                   (get_codec_from_stream(pFormatCtx->streams[i])->codec_type==CODEC_TYPE_VIDEO))
301                 {
302                         videoStream=i;
303                         break;
304                 }
305
306         if(videoStream==-1) {
307                 av_close_input_file(pFormatCtx);
308                 return 0;
309         }
310
311         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
312
313         /* Find the decoder for the video stream */
314         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
315         if(pCodec==NULL) {
316                 av_close_input_file(pFormatCtx);
317                 return 0;
318         }
319
320         if(avcodec_open(pCodecCtx, pCodec)<0) {
321                 av_close_input_file(pFormatCtx);
322                 return 0;
323         }
324
325         avcodec_close(pCodecCtx);
326         av_close_input_file(pFormatCtx);
327
328         return 1;
329 }
330 #endif
331
332 #ifdef WITH_REDCODE
333 static int isredcode(char * filename)
334 {
335         struct redcode_handle * h = redcode_open(filename);
336         if (!h) {
337                 return 0;
338         }
339         redcode_close(h);
340         return 1;
341 }
342
343 #endif
344
345 int imb_get_anim_type(char * name) {
346         int type;
347         struct stat st;
348
349         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
350
351 #ifndef _WIN32
352 #       ifdef WITH_FFMPEG
353         /* stat test below fails on large files > 4GB */
354         if (isffmpeg(name)) return (ANIM_FFMPEG);
355 #       endif
356         if (ib_stat(name,&st) == -1) return(0);
357         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
358
359         if (isavi(name)) return (ANIM_AVI);
360
361         if (ismovie(name)) return (ANIM_MOVIE);
362 #       ifdef WITH_QUICKTIME
363         if (isqtime(name)) return (ANIM_QTIME);
364 #       endif
365 #else
366         if (ib_stat(name,&st) == -1) return(0);
367         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
368
369         if (isavi(name)) return (ANIM_AVI);
370
371         if (ismovie(name)) return (ANIM_MOVIE);
372 #       ifdef WITH_QUICKTIME
373         if (isqtime(name)) return (ANIM_QTIME);
374 #       endif
375 #       ifdef WITH_FFMPEG
376         if (isffmpeg(name)) return (ANIM_FFMPEG);
377 #       endif
378 #endif
379 #ifdef WITH_REDCODE
380         if (isredcode(name)) return (ANIM_REDCODE);
381 #endif
382         type = IMB_ispic(name);
383         if (type == ANIM) return (ANIM_ANIM5);
384         if (type) return(ANIM_SEQUENCE);
385         return(0);
386 }
387  
388 int IMB_isanim(char *filename) {
389         int type;
390         
391         if(U.uiflag & USER_FILTERFILEEXTS) {
392                 if (G.have_quicktime){
393                         if(             BLI_testextensie(filename, ".avi")
394                                 ||      BLI_testextensie(filename, ".flc")
395                                 ||      BLI_testextensie(filename, ".dv")
396                                 ||      BLI_testextensie(filename, ".r3d")
397                                 ||      BLI_testextensie(filename, ".mov")
398                                 ||      BLI_testextensie(filename, ".movie")
399                                 ||      BLI_testextensie(filename, ".mv")) {
400                                 type = imb_get_anim_type(filename);
401                         } else {
402                                 return(FALSE);                  
403                         }
404                 } else { // no quicktime
405                         if(             BLI_testextensie(filename, ".avi")
406                                 ||      BLI_testextensie(filename, ".dv")
407                                 ||      BLI_testextensie(filename, ".r3d")
408                                 ||      BLI_testextensie(filename, ".mv")) {
409                                 type = imb_get_anim_type(filename);
410                         }
411                         else  {
412                                 return(FALSE);
413                         }
414                 }
415         } else { // no FILTERFILEEXTS
416                 type = imb_get_anim_type(filename);
417         }
418         
419         return (type && type!=ANIM_SEQUENCE);
420 }