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