~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/imbuf/intern/util.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
 
35
35
#ifdef _WIN32
36
 
#include <io.h>
37
 
#define open _open
38
 
#define read _read
39
 
#define close _close
 
36
#  include <io.h>
 
37
#  define open _open
 
38
#  define read _read
 
39
#  define close _close
40
40
#endif
41
41
 
42
 
#include "BLI_blenlib.h"
 
42
#include <stdlib.h>
 
43
 
 
44
#include "BLI_path_util.h"
43
45
#include "BLI_fileops.h"
 
46
#include "BLI_utildefines.h"
 
47
#include "BLI_string.h"
44
48
 
45
49
#include "DNA_userdef_types.h"
46
50
#include "BKE_global.h"
79
83
#endif
80
84
#ifdef WITH_OPENJPEG
81
85
        ".jp2",
 
86
        ".j2c",
82
87
#endif
83
88
#ifdef WITH_HDR
84
89
        ".hdr",
93
98
#ifdef WITH_OPENEXR
94
99
        ".exr",
95
100
#endif
96
 
        NULL};
 
101
        NULL
 
102
};
97
103
 
98
104
const char *imb_ext_image_qt[] = {
99
105
        ".gif",
101
107
        ".pct", ".pict",
102
108
        ".pntg",
103
109
        ".qtif",
104
 
        NULL};
 
110
        NULL
 
111
};
105
112
 
106
113
const char *imb_ext_movie[] = {
107
114
        ".avi",
128
135
        ".divx",
129
136
        ".xvid",
130
137
        ".mxf",
131
 
        NULL};
 
138
        NULL
 
139
};
132
140
 
133
141
/* sort of wrong being here... */
134
142
const char *imb_ext_audio[] = {
145
153
        ".aif",
146
154
        ".aiff",
147
155
        ".m4a",
148
 
        NULL};
 
156
        NULL
 
157
};
149
158
 
150
159
static int IMB_ispic_name(const char *name)
151
160
{
155
164
 
156
165
        if (UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
157
166
        
158
 
        if (stat(name,&st) == -1)
 
167
        if (stat(name, &st) == -1)
159
168
                return FALSE;
160
169
        if (((st.st_mode) & S_IFMT) != S_IFREG)
161
170
                return FALSE;
162
171
 
163
 
        if ((fp = BLI_open(name,O_BINARY|O_RDONLY, 0)) < 0)
 
172
        if ((fp = BLI_open(name, O_BINARY | O_RDONLY, 0)) < 0)
164
173
                return FALSE;
165
174
 
166
175
        if (read(fp, buf, 32) != 32) {
174
183
        if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0)
175
184
                return JPG;
176
185
 
177
 
        for (type=IMB_FILE_TYPES; type->is_a; type++)
178
 
                if (type->is_a((uchar*)buf))
 
186
        for (type = IMB_FILE_TYPES; type->is_a; type++)
 
187
                if (type->is_a((uchar *)buf))
179
188
                        return type->filetype;
180
189
 
181
190
        return FALSE;
184
193
int IMB_ispic(const char *filename)
185
194
{
186
195
        if (U.uiflag & USER_FILTERFILEEXTS) {
187
 
                if (    (BLI_testextensie_array(filename, imb_ext_image)) ||
188
 
                        (G.have_quicktime && BLI_testextensie_array(filename, imb_ext_image_qt))
189
 
                ) {
 
196
                if ((BLI_testextensie_array(filename, imb_ext_image)) ||
 
197
                    (G.have_quicktime && BLI_testextensie_array(filename, imb_ext_image_qt)))
 
198
                {
190
199
                        return IMB_ispic_name(filename);
191
200
                }
192
201
                else {
200
209
 
201
210
 
202
211
 
203
 
static int isavi (const char *name)
 
212
static int isavi(const char *name)
204
213
{
205
 
        return AVI_is_avi (name);
 
214
#ifdef WITH_AVI
 
215
        return AVI_is_avi(name);
 
216
#else
 
217
        (void)name;
 
218
        return FALSE;
 
219
#endif
206
220
}
207
221
 
208
222
#ifdef WITH_QUICKTIME
209
 
static int isqtime (const char *name)
 
223
static int isqtime(const char *name)
210
224
{
211
 
        return anim_is_quicktime (name);
 
225
        return anim_is_quicktime(name);
212
226
}
213
227
#endif
214
228
 
215
229
#ifdef WITH_FFMPEG
216
230
 
217
 
void silence_log_ffmpeg(int quiet)
218
 
{
219
 
        if (quiet) {
220
 
                av_log_set_level(AV_LOG_QUIET);
221
 
        }
222
 
        else {
223
 
                av_log_set_level(AV_LOG_DEBUG);
224
 
        }
225
 
}
226
 
 
227
 
extern void do_init_ffmpeg(void);
228
 
void do_init_ffmpeg(void)
229
 
{
230
 
        static int ffmpeg_init = 0;
231
 
        if (!ffmpeg_init) {
232
 
                ffmpeg_init = 1;
233
 
                av_register_all();
234
 
                avdevice_register_all();
235
 
                if ((G.debug & G_DEBUG_FFMPEG) == 0) {
236
 
                        silence_log_ffmpeg(1);
237
 
                }
238
 
                else {
239
 
                        silence_log_ffmpeg(0);
240
 
                }
241
 
        }
242
 
}
243
 
 
244
 
static int isffmpeg (const char *filename)
245
 
{
246
 
        AVFormatContext *pFormatCtx;
 
231
#ifdef _MSC_VER
 
232
#define va_copy(dst, src) ((dst) = (src))
 
233
#endif
 
234
 
 
235
/* BLI_vsnprintf in ffmpeg_log_callback() causes invalid warning */
 
236
#ifdef __GNUC__
 
237
#  pragma GCC diagnostic push
 
238
#  pragma GCC diagnostic ignored "-Wmissing-format-attribute"
 
239
#endif
 
240
 
 
241
static char ffmpeg_last_error[1024];
 
242
 
 
243
static void ffmpeg_log_callback(void *ptr, int level, const char *format, va_list arg)
 
244
{
 
245
        if (ELEM(level, AV_LOG_FATAL, AV_LOG_ERROR)) {
 
246
                size_t n;
 
247
                va_list arg2;
 
248
 
 
249
                va_copy(arg2, arg);
 
250
 
 
251
                n = BLI_vsnprintf(ffmpeg_last_error, sizeof(ffmpeg_last_error), format, arg2);
 
252
 
 
253
                /* strip trailing \n */
 
254
                ffmpeg_last_error[n - 1] = '\0';
 
255
        }
 
256
 
 
257
        if (G.debug & G_DEBUG_FFMPEG) {
 
258
                /* call default logger to print all message to console */
 
259
                av_log_default_callback(ptr, level, format, arg);
 
260
        }
 
261
}
 
262
 
 
263
#ifdef __GNUC__
 
264
#  pragma GCC diagnostic pop
 
265
#endif
 
266
 
 
267
void IMB_ffmpeg_init(void)
 
268
{
 
269
        av_register_all();
 
270
        avdevice_register_all();
 
271
 
 
272
        ffmpeg_last_error[0] = '\0';
 
273
 
 
274
        /* set own callback which could store last error to report to UI */
 
275
        av_log_set_callback(ffmpeg_log_callback);
 
276
}
 
277
 
 
278
const char *IMB_ffmpeg_last_error(void)
 
279
{
 
280
        return ffmpeg_last_error;
 
281
}
 
282
 
 
283
static int isffmpeg(const char *filename)
 
284
{
 
285
        AVFormatContext *pFormatCtx = NULL;
247
286
        unsigned int i;
248
287
        int videoStream;
249
288
        AVCodec *pCodec;
250
289
        AVCodecContext *pCodecCtx;
251
290
 
252
 
        do_init_ffmpeg();
253
 
 
254
 
        if ( BLI_testextensie(filename, ".swf") ||
255
 
                BLI_testextensie(filename, ".jpg") ||
256
 
                BLI_testextensie(filename, ".png") ||
257
 
                BLI_testextensie(filename, ".dds") ||
258
 
                BLI_testextensie(filename, ".tga") ||
259
 
                BLI_testextensie(filename, ".bmp") ||
260
 
                BLI_testextensie(filename, ".exr") ||
261
 
                BLI_testextensie(filename, ".cin") ||
262
 
                BLI_testextensie(filename, ".wav")) return 0;
263
 
 
264
 
        if (av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
 
291
        if (BLI_testextensie(filename, ".swf") ||
 
292
            BLI_testextensie(filename, ".jpg") ||
 
293
            BLI_testextensie(filename, ".png") ||
 
294
            BLI_testextensie(filename, ".dds") ||
 
295
            BLI_testextensie(filename, ".tga") ||
 
296
            BLI_testextensie(filename, ".bmp") ||
 
297
            BLI_testextensie(filename, ".exr") ||
 
298
            BLI_testextensie(filename, ".cin") ||
 
299
            BLI_testextensie(filename, ".wav"))
 
300
        {
 
301
                return 0;
 
302
        }
 
303
 
 
304
        if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) {
265
305
                if (UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_open_input_file failed\n");
266
306
                return 0;
267
307
        }
268
308
 
269
 
        if (av_find_stream_info(pFormatCtx)<0) {
270
 
                if (UTIL_DEBUG) fprintf(stderr, "isffmpeg: av_find_stream_info failed\n");
 
309
        if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
 
310
                if (UTIL_DEBUG) fprintf(stderr, "isffmpeg: avformat_find_stream_info failed\n");
271
311
                av_close_input_file(pFormatCtx);
272
312
                return 0;
273
313
        }
275
315
        if (UTIL_DEBUG) av_dump_format(pFormatCtx, 0, filename, 0);
276
316
 
277
317
 
278
 
                /* Find the first video stream */
279
 
        videoStream=-1;
280
 
        for (i=0; i<pFormatCtx->nb_streams; i++)
 
318
        /* Find the first video stream */
 
319
        videoStream = -1;
 
320
        for (i = 0; i < pFormatCtx->nb_streams; i++)
281
321
                if (pFormatCtx->streams[i] &&
282
 
                   pFormatCtx->streams[i]->codec && 
283
 
                  (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO))
 
322
                    pFormatCtx->streams[i]->codec &&
 
323
                    (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
284
324
                {
285
 
                        videoStream=i;
 
325
                        videoStream = i;
286
326
                        break;
287
327
                }
288
328
 
289
 
        if (videoStream==-1) {
 
329
        if (videoStream == -1) {
290
330
                av_close_input_file(pFormatCtx);
291
331
                return 0;
292
332
        }
293
333
 
294
334
        pCodecCtx = pFormatCtx->streams[videoStream]->codec;
295
335
 
296
 
                /* Find the decoder for the video stream */
297
 
        pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
298
 
        if (pCodec==NULL) {
 
336
        /* Find the decoder for the video stream */
 
337
        pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
 
338
        if (pCodec == NULL) {
299
339
                av_close_input_file(pFormatCtx);
300
340
                return 0;
301
341
        }
302
342
 
303
 
        if (avcodec_open(pCodecCtx, pCodec)<0) {
 
343
        if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
304
344
                av_close_input_file(pFormatCtx);
305
345
                return 0;
306
346
        }
313
353
#endif
314
354
 
315
355
#ifdef WITH_REDCODE
316
 
static int isredcode(const char * filename)
 
356
static int isredcode(const char *filename)
317
357
{
318
 
        struct redcode_handle * h = redcode_open(filename);
 
358
        struct redcode_handle *h = redcode_open(filename);
319
359
        if (!h) {
320
360
                return 0;
321
361
        }
325
365
 
326
366
#endif
327
367
 
328
 
int imb_get_anim_type(const char * name)
 
368
int imb_get_anim_type(const char *name)
329
369
{
330
370
        int type;
331
371
        struct stat st;
333
373
        if (UTIL_DEBUG) printf("in getanimtype: %s\n", name);
334
374
 
335
375
#ifndef _WIN32
336
 
#       ifdef WITH_QUICKTIME
 
376
#   ifdef WITH_QUICKTIME
337
377
        if (isqtime(name)) return (ANIM_QTIME);
338
 
#       endif
339
 
#       ifdef WITH_FFMPEG
 
378
#   endif
 
379
#   ifdef WITH_FFMPEG
340
380
        /* stat test below fails on large files > 4GB */
341
381
        if (isffmpeg(name)) return (ANIM_FFMPEG);
342
 
#       endif
 
382
#   endif
343
383
        if (BLI_stat(name, &st) == -1) return(0);
344
384
        if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
345
385
 
351
391
        if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
352
392
 
353
393
        if (ismovie(name)) return (ANIM_MOVIE);
354
 
#       ifdef WITH_QUICKTIME
 
394
#   ifdef WITH_QUICKTIME
355
395
        if (isqtime(name)) return (ANIM_QTIME);
356
 
#       endif
357
 
#       ifdef WITH_FFMPEG
 
396
#   endif
 
397
#   ifdef WITH_FFMPEG
358
398
        if (isffmpeg(name)) return (ANIM_FFMPEG);
359
 
#       endif
 
399
#   endif
360
400
 
361
401
 
362
402
        if (isavi(name)) return (ANIM_AVI);
389
429
                                type = imb_get_anim_type(filename);
390
430
                        }
391
431
                        else {
392
 
                                return(FALSE);                  
 
432
                                return(FALSE);
393
433
                        }
394
434
                }
395
435
                else { /* no quicktime */
409
449
                type = imb_get_anim_type(filename);
410
450
        }
411
451
        
412
 
        return (type && type!=ANIM_SEQUENCE);
 
452
        return (type && type != ANIM_SEQUENCE);
413
453
}