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