Video Texture: remove support for capture device, the linux ffmpeg repository is...
[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 #include "BLI_blenlib.h"
33
34 #include "DNA_userdef_types.h"
35 #include "BKE_global.h"
36
37 #include "imbuf.h"
38 #include "imbuf_patch.h"
39 #include "IMB_imbuf_types.h"
40 #include "IMB_imbuf.h"
41
42 #include "IMB_targa.h"
43 #include "IMB_png.h"
44
45 #ifdef WITH_DDS
46 #include "dds/dds_api.h"
47 #endif
48
49 #include "IMB_bmp.h"
50 #include "IMB_tiff.h"
51 #include "IMB_radiance_hdr.h"
52 #include "IMB_dpxcineon.h"
53
54 #include "IMB_anim.h"
55
56 #ifdef WITH_OPENEXR
57 #include "openexr/openexr_api.h"
58 #endif
59
60 #ifdef WITH_QUICKTIME
61 #include "quicktime_import.h"
62 #endif
63
64 #ifdef WITH_FFMPEG
65 #include <ffmpeg/avcodec.h>
66 #include <ffmpeg/avformat.h>
67 //#include <ffmpeg/avdevice.h>
68
69 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
70 #define FFMPEG_OLD_FRAME_RATE 1
71 #else
72 #define FFMPEG_CODEC_IS_POINTER 1
73 #endif
74
75 #endif
76
77 #define UTIL_DEBUG 0
78
79 /* from misc_util: flip the bytes from x  */
80 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
81
82 /* this one is only def-ed once, strangely... */
83 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
84
85 static int IMB_ispic_name(char *name)
86 {
87         struct stat st;
88         int fp, buf[10];
89         int ofs = 0;
90
91         if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
92         
93         if (ib_stat(name,&st) == -1) return(0);
94         if (((st.st_mode) & S_IFMT) == S_IFREG){
95                 if ((fp = open(name,O_BINARY|O_RDONLY)) >= 0){
96                         if (read(fp,buf,32)==32){
97                                 close(fp);
98                                 if (buf[ofs] == CAT) ofs += 3;
99                                 if (buf[ofs] == FORM){
100                                         if (buf[ofs + 2] == ILBM) return(AMI);
101                                         if (buf[ofs + 2] == ANIM){
102                                                 if (buf[ofs + 3] == FORM){
103                                                         return(ANIM);
104                                                 }else{
105                                                         return(Anim);
106                                                 }
107                                         }
108                                 } else {
109                                         if (GS(buf) == IMAGIC) return(IMAGIC);
110                                         if (GSS(buf) == IMAGIC) return(IMAGIC);
111                                         if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return(JPG);
112
113                                         /* at windows there are ".ffl" files with the same magic numnber... 
114                                            besides that,  tim images are not really important anymore! */
115                                         /* if ((BIG_LONG(buf[0]) == 0x10000000) && ((BIG_LONG(buf[1]) & 0xf0ffffff) == 0)) return(TIM); */
116
117                                 }
118                                 if (imb_is_a_png(buf)) return(PNG);
119 #ifdef WITH_DDS
120                                 if (imb_is_a_dds((uchar *)buf)) return(DDS);
121 #endif
122                                 if (imb_is_a_targa(buf)) return(TGA);
123 #ifdef WITH_OPENEXR
124                                 if (imb_is_a_openexr((uchar *)buf)) return(OPENEXR);
125 #endif
126                                 if (imb_is_a_tiff(buf)) return(TIF);
127                                 if (imb_is_dpx(buf)) return (DPX);
128                                 if (imb_is_cineon(buf)) return(CINEON);
129                                 /* radhdr: check if hdr format */
130                                 if (imb_is_a_hdr(buf)) return(RADHDR);
131
132 /*
133                                 if (imb_is_a_bmp(buf)) return(BMP);
134 */
135
136 #ifdef WITH_QUICKTIME
137 #if defined(_WIN32) || defined(__APPLE__)
138                                 if(G.have_quicktime) {
139                                         if (imb_is_a_quicktime(name)) return(QUICKTIME);
140                                 }
141 #endif
142 #endif
143
144                                 return(FALSE);
145                         }
146                         close(fp);
147                 }
148         }
149         return(FALSE);
150 }
151
152
153
154 int IMB_ispic(char *filename)
155 {
156         if(U.uiflag & USER_FILTERFILEEXTS) {
157                 if (G.have_libtiff && (BLI_testextensie(filename, ".tif")
158                                 ||      BLI_testextensie(filename, ".tiff"))) {
159                                 return IMB_ispic_name(filename);
160                 }
161                 if (G.have_quicktime){
162                         if(             BLI_testextensie(filename, ".jpg")
163                                 ||      BLI_testextensie(filename, ".jpeg")
164                                 ||      BLI_testextensie(filename, ".tif")
165                                 ||      BLI_testextensie(filename, ".tiff")
166                                 ||      BLI_testextensie(filename, ".hdr")
167                                 ||      BLI_testextensie(filename, ".tga")
168                                 ||      BLI_testextensie(filename, ".rgb")
169                                 ||      BLI_testextensie(filename, ".bmp")
170                                 ||      BLI_testextensie(filename, ".png")
171 #ifdef WITH_DDS
172                                 ||      BLI_testextensie(filename, ".dds")
173 #endif
174                                 ||      BLI_testextensie(filename, ".iff")
175                                 ||      BLI_testextensie(filename, ".lbm")
176                                 ||      BLI_testextensie(filename, ".gif")
177                                 ||      BLI_testextensie(filename, ".psd")
178                                 ||      BLI_testextensie(filename, ".pct")
179                                 ||      BLI_testextensie(filename, ".pict")
180                                 ||      BLI_testextensie(filename, ".pntg") //macpaint
181                                 ||      BLI_testextensie(filename, ".qtif")
182                                 ||      BLI_testextensie(filename, ".cin")
183 #ifdef WITH_BF_OPENEXR
184                                 ||      BLI_testextensie(filename, ".exr")
185 #endif
186                                 ||      BLI_testextensie(filename, ".sgi")) {
187                                 return IMB_ispic_name(filename);
188                         } else {
189                                 return(FALSE);                  
190                         }
191                 } else { /* no quicktime or libtiff */
192                         if(             BLI_testextensie(filename, ".jpg")
193                                 ||      BLI_testextensie(filename, ".jpeg")
194                                 ||      BLI_testextensie(filename, ".hdr")
195                                 ||      BLI_testextensie(filename, ".tga")
196                                 ||      BLI_testextensie(filename, ".rgb")
197                                 ||      BLI_testextensie(filename, ".bmp")
198                                 ||      BLI_testextensie(filename, ".png")
199                                 ||      BLI_testextensie(filename, ".cin")
200 #ifdef WITH_DDS
201                                 ||      BLI_testextensie(filename, ".dds")
202 #endif
203 #ifdef WITH_BF_OPENEXR
204                                 ||      BLI_testextensie(filename, ".exr")
205 #endif
206                                 ||      BLI_testextensie(filename, ".iff")
207                                 ||      BLI_testextensie(filename, ".lbm")
208                                 ||      BLI_testextensie(filename, ".sgi")) {
209                                 return IMB_ispic_name(filename);
210                         }
211                         else  {
212                                 return(FALSE);
213                         }
214                 }
215         } else { /* no FILTERFILEEXTS */
216                 return IMB_ispic_name(filename);
217         }
218 }
219
220
221
222 static int isavi (char *name) {
223         return AVI_is_avi (name);
224 }
225
226 #ifdef WITH_QUICKTIME
227 static int isqtime (char *name) {
228         return anim_is_quicktime (name);
229 }
230 #endif
231
232 #ifdef WITH_FFMPEG
233 extern void do_init_ffmpeg();
234 void do_init_ffmpeg()
235 {
236         static int ffmpeg_init = 0;
237         if (!ffmpeg_init) {
238                 ffmpeg_init = 1;
239                 av_register_all();
240                 //avdevice_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(pFormatCtx->streams[i] &&
293                    get_codec_from_stream(pFormatCtx->streams[i]) && 
294                   (get_codec_from_stream(pFormatCtx->streams[i])->codec_type==CODEC_TYPE_VIDEO))
295                 {
296                         videoStream=i;
297                         break;
298                 }
299
300         if(videoStream==-1) {
301                 av_close_input_file(pFormatCtx);
302                 return 0;
303         }
304
305         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
306
307         /* Find the decoder for the video stream */
308         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
309         if(pCodec==NULL) {
310                 av_close_input_file(pFormatCtx);
311                 return 0;
312         }
313
314         if(avcodec_open(pCodecCtx, pCodec)<0) {
315                 av_close_input_file(pFormatCtx);
316                 return 0;
317         }
318
319         avcodec_close(pCodecCtx);
320         av_close_input_file(pFormatCtx);
321
322         return 1;
323 }
324 #endif
325
326 #ifdef WITH_REDCODE
327 static int isredcode(char * filename)
328 {
329         struct redcode_handle * h = redcode_open(filename);
330         if (!h) {
331                 return 0;
332         }
333         redcode_close(h);
334         return 1;
335 }
336
337 #endif
338
339 int imb_get_anim_type(char * name) {
340         int type;
341         struct stat st;
342
343         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
344
345 #ifndef _WIN32
346 #       ifdef WITH_FFMPEG
347         /* stat test below fails on large files > 4GB */
348         if (isffmpeg(name)) return (ANIM_FFMPEG);
349 #       endif
350         if (ib_stat(name,&st) == -1) return(0);
351         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
352
353         if (isavi(name)) return (ANIM_AVI);
354
355         if (ismovie(name)) return (ANIM_MOVIE);
356 #       ifdef WITH_QUICKTIME
357         if (isqtime(name)) return (ANIM_QTIME);
358 #       endif
359 #else
360         if (ib_stat(name,&st) == -1) return(0);
361         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
362
363         if (isavi(name)) return (ANIM_AVI);
364
365         if (ismovie(name)) return (ANIM_MOVIE);
366 #       ifdef WITH_QUICKTIME
367         if (isqtime(name)) return (ANIM_QTIME);
368 #       endif
369 #       ifdef WITH_FFMPEG
370         if (isffmpeg(name)) return (ANIM_FFMPEG);
371 #       endif
372 #endif
373 #ifdef WITH_REDCODE
374         if (isredcode(name)) return (ANIM_REDCODE);
375 #endif
376         type = IMB_ispic(name);
377         if (type == ANIM) return (ANIM_ANIM5);
378         if (type) return(ANIM_SEQUENCE);
379         return(0);
380 }
381  
382 int IMB_isanim(char *filename) {
383         int type;
384         
385         if(U.uiflag & USER_FILTERFILEEXTS) {
386                 if (G.have_quicktime){
387                         if(             BLI_testextensie(filename, ".avi")
388                                 ||      BLI_testextensie(filename, ".flc")
389                                 ||      BLI_testextensie(filename, ".dv")
390                                 ||      BLI_testextensie(filename, ".r3d")
391                                 ||      BLI_testextensie(filename, ".mov")
392                                 ||      BLI_testextensie(filename, ".movie")
393                                 ||      BLI_testextensie(filename, ".mv")) {
394                                 type = imb_get_anim_type(filename);
395                         } else {
396                                 return(FALSE);                  
397                         }
398                 } else { // no quicktime
399                         if(             BLI_testextensie(filename, ".avi")
400                                 ||      BLI_testextensie(filename, ".dv")
401                                 ||      BLI_testextensie(filename, ".r3d")
402                                 ||      BLI_testextensie(filename, ".mv")) {
403                                 type = imb_get_anim_type(filename);
404                         }
405                         else  {
406                                 return(FALSE);
407                         }
408                 }
409         } else { // no FILTERFILEEXTS
410                 type = imb_get_anim_type(filename);
411         }
412         
413         return (type && type!=ANIM_SEQUENCE);
414 }