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