New: Import/Export of Cineon and DPX image files. The first is Kodak's
[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                                 ||      BLI_testextensie(filename, ".sgi")) {
175                                 return IMB_ispic_name(filename);
176                         } else {
177                                 return(FALSE);                  
178                         }
179                 } else { /* no quicktime or libtiff */
180                         if(             BLI_testextensie(filename, ".jpg")
181                                 ||      BLI_testextensie(filename, ".jpeg")
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                                 ||  BLI_testextensie(filename, ".cin")
188                                 ||      BLI_testextensie(filename, ".iff")
189                                 ||      BLI_testextensie(filename, ".lbm")
190                                 ||      BLI_testextensie(filename, ".sgi")) {
191                                 return IMB_ispic_name(filename);
192                         }
193                         else  {
194                                 return(FALSE);
195                         }
196                 }
197         } else { /* no FILTERFILEEXTS */
198                 return IMB_ispic_name(filename);
199         }
200 }
201
202
203
204 static int isavi (char *name) {
205         return AVI_is_avi (name);
206 }
207
208 #ifdef WITH_QUICKTIME
209 static int isqtime (char *name) {
210         return anim_is_quicktime (name);
211 }
212 #endif
213
214 #ifdef WITH_FFMPEG
215 void do_init_ffmpeg()
216 {
217         static int ffmpeg_init = 0;
218         if (!ffmpeg_init) {
219                 ffmpeg_init = 1;
220                 av_register_all();
221         }
222 }
223
224 #ifdef FFMPEG_CODEC_IS_POINTER
225 static AVCodecContext* get_codec_from_stream(AVStream* stream)
226 {
227         return stream->codec;
228 }
229 #else
230 static AVCodecContext* get_codec_from_stream(AVStream* stream)
231 {
232         return &stream->codec;
233 }
234 #endif
235
236
237 static int isffmpeg (char *filename) {
238         AVFormatContext *pFormatCtx;
239         int            i, videoStream;
240         AVCodec *pCodec;
241         AVCodecContext *pCodecCtx;
242
243         do_init_ffmpeg();
244
245         if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
246                 fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
247                 return 0;
248         }
249
250         if(av_find_stream_info(pFormatCtx)<0) {
251                 fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
252                 av_close_input_file(pFormatCtx);
253                 return 0;
254         }
255
256         dump_format(pFormatCtx, 0, filename, 0);
257
258
259         /* Find the first video stream */
260         videoStream=-1;
261         for(i=0; i<pFormatCtx->nb_streams; i++)
262                 if(get_codec_from_stream(pFormatCtx->streams[i])
263                    ->codec_type==CODEC_TYPE_VIDEO)
264                 {
265                         videoStream=i;
266                         break;
267                 }
268
269         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
270
271         if(videoStream==-1) {
272                 avcodec_close(pCodecCtx);
273                 av_close_input_file(pFormatCtx);
274                 return 0;
275         }
276
277
278         /* Find the decoder for the video stream */
279         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
280         if(pCodec==NULL) {
281                 avcodec_close(pCodecCtx);
282                 av_close_input_file(pFormatCtx);
283                 return 0;
284         }
285
286         if(avcodec_open(pCodecCtx, pCodec)<0) {
287                 avcodec_close(pCodecCtx);
288                 av_close_input_file(pFormatCtx);
289                 return 0;
290         }
291
292         avcodec_close(pCodecCtx);
293         av_close_input_file(pFormatCtx);
294
295         return 1;
296 }
297 #endif
298
299 int imb_get_anim_type(char * name) {
300         int type;
301         struct stat st;
302
303         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
304
305 #ifdef WITH_FFMPEG
306         /* stat test below fails on large files > 4GB */
307         if (isffmpeg(name)) return (ANIM_FFMPEG);
308 #endif
309
310         if (ib_stat(name,&st) == -1) return(0);
311         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
312
313         if (isavi(name)) return (ANIM_AVI);
314
315         if (ismovie(name)) return (ANIM_MOVIE);
316 #ifdef WITH_QUICKTIME
317         if (isqtime(name)) return (ANIM_QTIME);
318 #endif
319         type = IMB_ispic(name);
320         if (type == ANIM) return (ANIM_ANIM5);
321         if (type) return(ANIM_SEQUENCE);
322         return(0);
323 }
324  
325 int IMB_isanim(char *filename) {
326         int type;
327         
328         if(U.uiflag & USER_FILTERFILEEXTS) {
329                 if (G.have_quicktime){
330                         if(             BLI_testextensie(filename, ".avi")
331                                 ||      BLI_testextensie(filename, ".flc")
332                                 ||      BLI_testextensie(filename, ".dv")
333                                 ||      BLI_testextensie(filename, ".mov")
334                                 ||      BLI_testextensie(filename, ".movie")
335                                 ||      BLI_testextensie(filename, ".mv")) {
336                                 type = imb_get_anim_type(filename);
337                         } else {
338                                 return(FALSE);                  
339                         }
340                 } else { // no quicktime
341                         if(             BLI_testextensie(filename, ".avi")
342                                 ||      BLI_testextensie(filename, ".dv")
343                                 ||      BLI_testextensie(filename, ".mv")) {
344                                 type = imb_get_anim_type(filename);
345                         }
346                         else  {
347                                 return(FALSE);
348                         }
349                 }
350         } else { // no FILTERFILEEXTS
351                 type = imb_get_anim_type(filename);
352         }
353         
354         return (type && type!=ANIM_SEQUENCE);
355 }