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