From 1005f542b20cb152514c9496689148fc4456f438 Mon Sep 17 00:00:00 2001 From: Corey Hickey Date: Thu, 30 Mar 2006 04:33:05 +0000 Subject: [PATCH] - Add new file internal.h for common internal-use-only functions. - Add new function av_tempfile() for creating temporary files; contains workaround for MinGW. - Make XviD stuff use av_tempfile(). Originally committed as revision 5245 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/internal.h | 12 ++++++++++++ libavcodec/utils.c | 39 +++++++++++++++++++++++++++++++++++++++ libavcodec/xvid_rc.c | 10 +++++----- libavcodec/xvidff.c | 38 ++------------------------------------ 4 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 libavcodec/internal.h diff --git a/libavcodec/internal.h b/libavcodec/internal.h new file mode 100644 index 0000000000..189090eaad --- /dev/null +++ b/libavcodec/internal.h @@ -0,0 +1,12 @@ +#ifndef INTERNAL_H +#define INTERNAL_H + +/** + * @file internal.h + * common functions for internal libavcodec use + */ + + +int av_tempfile(char *prefix, char **filename); + +#endif /* INTERNAL_H */ diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 7e5d3b0372..3a26c6f94a 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -33,6 +33,9 @@ #include #include #include +#ifdef CONFIG_WIN32 +#include +#endif const uint8_t ff_reverse[256]={ 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0, @@ -1349,3 +1352,39 @@ unsigned int av_xiphlacing(unsigned char *s, unsigned int v) n++; return n; } + +/* Wrapper to work around the lack of mkstemp() on mingw/cygin. + * Also, tries to create file in /tmp first, if possible. + * *prefix can be a character constant; *filename will be allocated internally. + * Returns file descriptor of opened file (or -1 on error) + * and opened file name in **filename. */ +int av_tempfile(char *prefix, char **filename) { + int fd=-1; +#ifdef CONFIG_WIN32 + *filename = tempnam(".", prefix); +#else + size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */ + *filename = av_malloc(len * sizeof(char)); +#endif + /* -----common section-----*/ + if (*filename == NULL) { + av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n"); + return -1; + } +#ifdef CONFIG_WIN32 + fd = open(*filename, _O_RDWR | _O_BINARY | _O_CREAT, 0444); +#else + snprintf(*filename, len, "/tmp/%sXXXXXX", prefix); + fd = mkstemp(*filename); + if (fd < 0) { + snprintf(*filename, len, "./%sXXXXXX", prefix); + fd = mkstemp(*filename); + } +#endif + /* -----common section-----*/ + if (fd < 0) { + av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename); + return -1; + } + return fd; /* success */ +} diff --git a/libavcodec/xvid_rc.c b/libavcodec/xvid_rc.c index 829ad8e5bf..ed9bec39cb 100644 --- a/libavcodec/xvid_rc.c +++ b/libavcodec/xvid_rc.c @@ -21,6 +21,7 @@ #include #include #include "avcodec.h" +#include "internal.h" //#include "dsputil.h" #include "mpegvideo.h" @@ -37,11 +38,10 @@ int ff_xvid_rate_control_init(MpegEncContext *s){ //xvid_debug=-1; - tmp_name= av_strdup("/tmp/xvidrc.XXXXXX"); - fd = mkstemp(tmp_name); - if(fd < 0){ - strcpy(tmp_name, "./xvidrc.XXXXXX"); - fd = mkstemp(tmp_name); + fd=av_tempfile("xvidrc.", &tmp_name); + if (fd == -1) { + av_log(NULL, AV_LOG_ERROR, "Can't create temporary pass2 file.\n"); + return -1; } for(i=0; irc_context.num_entries; i++){ diff --git a/libavcodec/xvidff.c b/libavcodec/xvidff.c index d44fedb2c8..c765891d35 100644 --- a/libavcodec/xvidff.c +++ b/libavcodec/xvidff.c @@ -27,9 +27,7 @@ #include #include "common.h" #include "avcodec.h" -#ifdef CONFIG_WIN32 -#include -#endif +#include "internal.h" /** * Buffer management macros. @@ -229,39 +227,7 @@ int ff_xvid_encode_init(AVCodecContext *avctx) { rc2pass2.version = XVID_VERSION; rc2pass2.bitrate = avctx->bit_rate; -#ifdef CONFIG_WIN32 /* Ugly work around */ - { - char *tempname; - - tempname = tempnam(".", "xvidff"); - fd = -1; - if( tempname && - (fd = open(tempname, _O_RDWR | _O_BINARY)) != -1 ) { - x->twopassfile = av_strdup(tempname); -#undef free - free(tempname); -#define free please_use_av_free - if( x->twopassfile == NULL ) { - av_log(avctx, AV_LOG_ERROR, - "XviD: Cannot allocate 2-pass buffer\n"); - return -1; - } - } - } -#else - x->twopassfile = av_malloc(BUFFER_SIZE); - if( x->twopassfile == NULL ) { - av_log(avctx, AV_LOG_ERROR, - "XviD: Cannot allocate 2-pass buffer\n"); - return -1; - } - strcpy(x->twopassfile, "/tmp/xvidff.XXXXXX"); - fd = mkstemp(x->twopassfile); - if(fd < 0){ - strcpy(x->twopassfile, "./xvidff.XXXXXX"); - fd = mkstemp(x->twopassfile); - } -#endif + fd = av_tempfile("xvidff.", &(x->twopassfile)); if( fd == -1 ) { av_log(avctx, AV_LOG_ERROR, "XviD: Cannot write 2-pass pipe\n");