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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/imbuf/intern/util.c
44 #include "BLI_blenlib.h"
46 #include "DNA_userdef_types.h"
47 #include "BKE_global.h"
50 #include "IMB_imbuf_types.h"
51 #include "IMB_imbuf.h"
52 #include "IMB_filetype.h"
57 #include "quicktime_import.h"
61 #include <libavcodec/avcodec.h>
62 #include <libavformat/avformat.h>
63 #include <libavdevice/avdevice.h>
64 #include <libavutil/log.h>
66 #if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
67 #define FFMPEG_HAVE_AV_DUMP_FORMAT 1
70 #ifndef FFMPEG_HAVE_AV_DUMP_FORMAT
71 #define av_dump_format dump_format
78 const char *imb_ext_image[] = {
83 ".sgi", ".rgb", ".rgba",
85 ".tif", ".tiff", ".tx",
105 const char *imb_ext_image_qt[] = {
113 const char *imb_ext_movie[] = {
139 /* sort of wrong being here... */
140 const char *imb_ext_audio[] = {
153 static int IMB_ispic_name(const char *name)
159 if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
161 if(stat(name,&st) == -1)
163 if(((st.st_mode) & S_IFMT) != S_IFREG)
166 if((fp = open(name,O_BINARY|O_RDONLY)) < 0)
169 if(read(fp, buf, 32) != 32) {
176 /* XXX move this exception */
177 if((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0)
180 for(type=IMB_FILE_TYPES; type->is_a; type++)
181 if(type->is_a((uchar*)buf))
182 return type->filetype;
187 int IMB_ispic(const char *filename)
189 if(U.uiflag & USER_FILTERFILEEXTS) {
190 if( (BLI_testextensie_array(filename, imb_ext_image)) ||
191 (G.have_quicktime && BLI_testextensie_array(filename, imb_ext_image_qt))
193 return IMB_ispic_name(filename);
199 else { /* no FILTERFILEEXTS */
200 return IMB_ispic_name(filename);
206 static int isavi (const char *name) {
207 return AVI_is_avi (name);
210 #ifdef WITH_QUICKTIME
211 static int isqtime (const char *name) {
212 return anim_is_quicktime (name);
218 void silence_log_ffmpeg(int quiet)
222 av_log_set_level(AV_LOG_QUIET);
226 av_log_set_level(AV_LOG_INFO);
230 extern void do_init_ffmpeg(void);
231 void do_init_ffmpeg(void)
233 static int ffmpeg_init = 0;
237 avdevice_register_all();
239 if ((G.f & G_DEBUG) == 0)
241 silence_log_ffmpeg(1);
246 static int isffmpeg (const char *filename) {
247 AVFormatContext *pFormatCtx;
251 AVCodecContext *pCodecCtx;
255 if( BLI_testextensie(filename, ".swf") ||
256 BLI_testextensie(filename, ".jpg") ||
257 BLI_testextensie(filename, ".png") ||
258 BLI_testextensie(filename, ".dds") ||
259 BLI_testextensie(filename, ".tga") ||
260 BLI_testextensie(filename, ".bmp") ||
261 BLI_testextensie(filename, ".exr") ||
262 BLI_testextensie(filename, ".cin") ||
263 BLI_testextensie(filename, ".wav")) return 0;
265 if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
266 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
270 if(av_find_stream_info(pFormatCtx)<0) {
271 if(UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
272 av_close_input_file(pFormatCtx);
276 if(UTIL_DEBUG) av_dump_format(pFormatCtx, 0, filename, 0);
279 /* Find the first video stream */
281 for(i=0; i<pFormatCtx->nb_streams; i++)
282 if(pFormatCtx->streams[i] &&
283 pFormatCtx->streams[i]->codec &&
284 (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO))
290 if(videoStream==-1) {
291 av_close_input_file(pFormatCtx);
295 pCodecCtx = pFormatCtx->streams[videoStream]->codec;
297 /* Find the decoder for the video stream */
298 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
300 av_close_input_file(pFormatCtx);
304 if(avcodec_open(pCodecCtx, pCodec)<0) {
305 av_close_input_file(pFormatCtx);
309 avcodec_close(pCodecCtx);
310 av_close_input_file(pFormatCtx);
317 static int isredcode(const char * filename)
319 struct redcode_handle * h = redcode_open(filename);
329 int imb_get_anim_type(const char * name) {
333 if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
336 # ifdef WITH_QUICKTIME
337 if (isqtime(name)) return (ANIM_QTIME);
340 /* stat test below fails on large files > 4GB */
341 if (isffmpeg(name)) return (ANIM_FFMPEG);
343 if (stat(name,&st) == -1) return(0);
344 if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
346 if (isavi(name)) return (ANIM_AVI);
348 if (ismovie(name)) return (ANIM_MOVIE);
350 if (stat(name,&st) == -1) return(0);
351 if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
353 if (ismovie(name)) return (ANIM_MOVIE);
354 # ifdef WITH_QUICKTIME
355 if (isqtime(name)) return (ANIM_QTIME);
358 if (isffmpeg(name)) return (ANIM_FFMPEG);
360 if (isavi(name)) return (ANIM_AVI);
363 if (isredcode(name)) return (ANIM_REDCODE);
365 type = IMB_ispic(name);
366 if (type) return(ANIM_SEQUENCE);
370 int IMB_isanim(const char *filename) {
373 if(U.uiflag & USER_FILTERFILEEXTS) {
374 if (G.have_quicktime){
375 if( BLI_testextensie(filename, ".avi")
376 || BLI_testextensie(filename, ".flc")
377 || BLI_testextensie(filename, ".dv")
378 || BLI_testextensie(filename, ".r3d")
379 || BLI_testextensie(filename, ".mov")
380 || BLI_testextensie(filename, ".movie")
381 || BLI_testextensie(filename, ".mv")) {
382 type = imb_get_anim_type(filename);
386 } else { // no quicktime
387 if( BLI_testextensie(filename, ".avi")
388 || BLI_testextensie(filename, ".dv")
389 || BLI_testextensie(filename, ".r3d")
390 || BLI_testextensie(filename, ".mv")) {
391 type = imb_get_anim_type(filename);
397 } else { // no FILTERFILEEXTS
398 type = imb_get_anim_type(filename);
401 return (type && type!=ANIM_SEQUENCE);