3 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 #include "BLI_blenlib.h"
34 #include "DNA_userdef_types.h"
35 #include "BKE_global.h"
36 #include "BKE_writeffmpeg.h" /* for silence_log_ffmpeg */
39 #include "imbuf_patch.h"
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
43 #include "IMB_targa.h"
47 #include "dds/dds_api.h"
52 #include "IMB_radiance_hdr.h"
53 #include "IMB_dpxcineon.h"
58 #include "openexr/openexr_api.h"
62 #include "quicktime_import.h"
66 #include <ffmpeg/avcodec.h>
67 #include <ffmpeg/avformat.h>
68 //#include <ffmpeg/avdevice.h>
70 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
71 #define FFMPEG_OLD_FRAME_RATE 1
73 #define FFMPEG_CODEC_IS_POINTER 1
80 /* from misc_util: flip the bytes from x */
81 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
83 /* this one is only def-ed once, strangely... */
84 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
86 static int IMB_ispic_name(char *name)
92 if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
94 if (ib_stat(name,&st) == -1) return(0);
95 if (((st.st_mode) & S_IFMT) == S_IFREG){
96 if ((fp = open(name,O_BINARY|O_RDONLY)) >= 0){
97 if (read(fp,buf,32)==32){
99 if (buf[ofs] == CAT) ofs += 3;
100 if (buf[ofs] == FORM){
101 if (buf[ofs + 2] == ILBM) return(AMI);
102 if (buf[ofs + 2] == ANIM){
103 if (buf[ofs + 3] == FORM){
110 if (GS(buf) == IMAGIC) return(IMAGIC);
111 if (GSS(buf) == IMAGIC) return(IMAGIC);
112 if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return(JPG);
114 /* at windows there are ".ffl" files with the same magic numnber...
115 besides that, tim images are not really important anymore! */
116 /* if ((BIG_LONG(buf[0]) == 0x10000000) && ((BIG_LONG(buf[1]) & 0xf0ffffff) == 0)) return(TIM); */
119 if (imb_is_a_png(buf)) return(PNG);
121 if (imb_is_a_dds((uchar *)buf)) return(DDS);
123 if (imb_is_a_targa(buf)) return(TGA);
125 if (imb_is_a_openexr((uchar *)buf)) return(OPENEXR);
127 if (imb_is_a_tiff(buf)) return(TIF);
128 if (imb_is_dpx(buf)) return (DPX);
129 if (imb_is_cineon(buf)) return(CINEON);
130 /* radhdr: check if hdr format */
131 if (imb_is_a_hdr(buf)) return(RADHDR);
134 if (imb_is_a_bmp(buf)) return(BMP);
137 #ifdef WITH_QUICKTIME
138 #if defined(_WIN32) || defined(__APPLE__)
139 if(G.have_quicktime) {
140 if (imb_is_a_quicktime(name)) return(QUICKTIME);
155 int IMB_ispic(char *filename)
157 if(U.uiflag & USER_FILTERFILEEXTS) {
158 if (G.have_libtiff && (BLI_testextensie(filename, ".tif")
159 || BLI_testextensie(filename, ".tiff"))) {
160 return IMB_ispic_name(filename);
162 if (G.have_quicktime){
163 if( BLI_testextensie(filename, ".jpg")
164 || BLI_testextensie(filename, ".jpeg")
165 || BLI_testextensie(filename, ".tif")
166 || BLI_testextensie(filename, ".tiff")
167 || BLI_testextensie(filename, ".hdr")
168 || BLI_testextensie(filename, ".tga")
169 || BLI_testextensie(filename, ".rgb")
170 || BLI_testextensie(filename, ".bmp")
171 || BLI_testextensie(filename, ".png")
173 || BLI_testextensie(filename, ".dds")
175 || BLI_testextensie(filename, ".iff")
176 || BLI_testextensie(filename, ".lbm")
177 || BLI_testextensie(filename, ".gif")
178 || BLI_testextensie(filename, ".psd")
179 || BLI_testextensie(filename, ".pct")
180 || BLI_testextensie(filename, ".pict")
181 || BLI_testextensie(filename, ".pntg") //macpaint
182 || BLI_testextensie(filename, ".qtif")
183 || BLI_testextensie(filename, ".cin")
184 #ifdef WITH_BF_OPENEXR
185 || BLI_testextensie(filename, ".exr")
187 || BLI_testextensie(filename, ".sgi")) {
188 return IMB_ispic_name(filename);
192 } else { /* no quicktime or libtiff */
193 if( BLI_testextensie(filename, ".jpg")
194 || BLI_testextensie(filename, ".jpeg")
195 || BLI_testextensie(filename, ".hdr")
196 || BLI_testextensie(filename, ".tga")
197 || BLI_testextensie(filename, ".rgb")
198 || BLI_testextensie(filename, ".bmp")
199 || BLI_testextensie(filename, ".png")
200 || BLI_testextensie(filename, ".cin")
202 || BLI_testextensie(filename, ".dds")
204 #ifdef WITH_BF_OPENEXR
205 || BLI_testextensie(filename, ".exr")
207 || BLI_testextensie(filename, ".iff")
208 || BLI_testextensie(filename, ".lbm")
209 || BLI_testextensie(filename, ".sgi")) {
210 return IMB_ispic_name(filename);
216 } else { /* no FILTERFILEEXTS */
217 return IMB_ispic_name(filename);
223 static int isavi (char *name) {
224 return AVI_is_avi (name);
227 #ifdef WITH_QUICKTIME
228 static int isqtime (char *name) {
229 return anim_is_quicktime (name);
234 extern void do_init_ffmpeg();
235 void do_init_ffmpeg()
237 static int ffmpeg_init = 0;
241 //avdevice_register_all();
243 if ((G.f & G_DEBUG) == 0)
245 silence_log_ffmpeg(1);
250 #ifdef FFMPEG_CODEC_IS_POINTER
251 static AVCodecContext* get_codec_from_stream(AVStream* stream)
253 return stream->codec;
256 static AVCodecContext* get_codec_from_stream(AVStream* stream)
258 return &stream->codec;
263 static int isffmpeg (char *filename) {
264 AVFormatContext *pFormatCtx;
267 AVCodecContext *pCodecCtx;
271 if( BLI_testextensie(filename, ".swf") ||
272 BLI_testextensie(filename, ".jpg") ||
273 BLI_testextensie(filename, ".png") ||
274 BLI_testextensie(filename, ".dds") ||
275 BLI_testextensie(filename, ".tga") ||
276 BLI_testextensie(filename, ".bmp") ||
277 BLI_testextensie(filename, ".exr") ||
278 BLI_testextensie(filename, ".cin") ||
279 BLI_testextensie(filename, ".wav")) return 0;
281 if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
282 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
286 if(av_find_stream_info(pFormatCtx)<0) {
287 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
288 av_close_input_file(pFormatCtx);
292 if(UTIL_DEBUG) dump_format(pFormatCtx, 0, filename, 0);
295 /* Find the first video stream */
297 for(i=0; i<pFormatCtx->nb_streams; i++)
298 if(pFormatCtx->streams[i] &&
299 get_codec_from_stream(pFormatCtx->streams[i]) &&
300 (get_codec_from_stream(pFormatCtx->streams[i])->codec_type==CODEC_TYPE_VIDEO))
306 if(videoStream==-1) {
307 av_close_input_file(pFormatCtx);
311 pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
313 /* Find the decoder for the video stream */
314 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
316 av_close_input_file(pFormatCtx);
320 if(avcodec_open(pCodecCtx, pCodec)<0) {
321 av_close_input_file(pFormatCtx);
325 avcodec_close(pCodecCtx);
326 av_close_input_file(pFormatCtx);
333 static int isredcode(char * filename)
335 struct redcode_handle * h = redcode_open(filename);
345 int imb_get_anim_type(char * name) {
349 if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
353 /* stat test below fails on large files > 4GB */
354 if (isffmpeg(name)) return (ANIM_FFMPEG);
356 if (ib_stat(name,&st) == -1) return(0);
357 if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
359 if (isavi(name)) return (ANIM_AVI);
361 if (ismovie(name)) return (ANIM_MOVIE);
362 # ifdef WITH_QUICKTIME
363 if (isqtime(name)) return (ANIM_QTIME);
366 if (ib_stat(name,&st) == -1) return(0);
367 if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
369 if (isavi(name)) return (ANIM_AVI);
371 if (ismovie(name)) return (ANIM_MOVIE);
372 # ifdef WITH_QUICKTIME
373 if (isqtime(name)) return (ANIM_QTIME);
376 if (isffmpeg(name)) return (ANIM_FFMPEG);
380 if (isredcode(name)) return (ANIM_REDCODE);
382 type = IMB_ispic(name);
383 if (type == ANIM) return (ANIM_ANIM5);
384 if (type) return(ANIM_SEQUENCE);
388 int IMB_isanim(char *filename) {
391 if(U.uiflag & USER_FILTERFILEEXTS) {
392 if (G.have_quicktime){
393 if( BLI_testextensie(filename, ".avi")
394 || BLI_testextensie(filename, ".flc")
395 || BLI_testextensie(filename, ".dv")
396 || BLI_testextensie(filename, ".r3d")
397 || BLI_testextensie(filename, ".mov")
398 || BLI_testextensie(filename, ".movie")
399 || BLI_testextensie(filename, ".mv")) {
400 type = imb_get_anim_type(filename);
404 } else { // no quicktime
405 if( BLI_testextensie(filename, ".avi")
406 || BLI_testextensie(filename, ".dv")
407 || BLI_testextensie(filename, ".r3d")
408 || BLI_testextensie(filename, ".mv")) {
409 type = imb_get_anim_type(filename);
415 } else { // no FILTERFILEEXTS
416 type = imb_get_anim_type(filename);
419 return (type && type!=ANIM_SEQUENCE);