[FFmpeg-cvslog] Move av_tempfile() into libavutil, it is a generically usefull thing and its small.

Stefano Sabatini stefasab at gmail.com
Sun Oct 16 21:08:26 CEST 2011


On date Sunday 2011-10-16 17:21:22 +0200, Michael Niedermayer wrote:
> ffmpeg | branch: master | Michael Niedermayer <michaelni at gmx.at> | Sun Oct 16 15:21:58 2011 +0200| [885158c887384c6da247cc061458f2e53367e6b5] | committer: Michael Niedermayer
> 
> Move av_tempfile() into libavutil, it is a generically usefull thing and its small.
> 
> Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
> 
> > http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=885158c887384c6da247cc061458f2e53367e6b5
> ---
> 
>  libavcodec/libxvid_rc.c |    3 ++-
>  libavcodec/libxvidff.c  |   42 ++----------------------------------------
>  libavutil/file.c        |   31 +++++++++++++++++++++++++++++++
>  libavutil/file.h        |    9 +++++++++
>  4 files changed, 44 insertions(+), 41 deletions(-)
> 
> diff --git a/libavcodec/libxvid_rc.c b/libavcodec/libxvid_rc.c
> index dbf7b0b..37716ac 100644
> --- a/libavcodec/libxvid_rc.c
> +++ b/libavcodec/libxvid_rc.c
> @@ -22,6 +22,7 @@
>  
>  #include <xvid.h>
>  #include <unistd.h>
> +#include "libavutil/file.h"
>  #include "avcodec.h"
>  #include "libxvid_internal.h"
>  //#include "dsputil.h"
> @@ -40,7 +41,7 @@ int ff_xvid_rate_control_init(MpegEncContext *s){
>  
>  //xvid_debug=-1;
>  
> -    fd=ff_tempfile("xvidrc.", &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;
> diff --git a/libavcodec/libxvidff.c b/libavcodec/libxvidff.c
> index d8d44bf..ba950ed 100644
> --- a/libavcodec/libxvidff.c
> +++ b/libavcodec/libxvidff.c
> @@ -28,13 +28,11 @@
>  #include <xvid.h>
>  #include <unistd.h>
>  #include "avcodec.h"
> +#include "libavutil/file.h"
>  #include "libavutil/cpu.h"
>  #include "libavutil/intreadwrite.h"
>  #include "libavutil/mathematics.h"
>  #include "libxvid_internal.h"
> -#if !HAVE_MKSTEMP
> -#include <fcntl.h>
> -#endif
>  
>  /**
>   * Buffer management macros.
> @@ -77,42 +75,6 @@ int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned
>  int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
>  void xvid_correct_framerate(AVCodecContext *avctx);
>  
> -/* Wrapper to work around the lack of mkstemp() on mingw.
> - * Also, tries to create file in /tmp first, if possible.
> - * *prefix can be a character constant; *filename will be allocated internally.
> - * @return file descriptor of opened file (or -1 on error)
> - * and opened file name in **filename. */
> -int ff_tempfile(const char *prefix, char **filename) {
> -    int fd=-1;
> -#if !HAVE_MKSTEMP
> -    *filename = tempnam(".", prefix);
> -#else
> -    size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
> -    *filename = av_malloc(len);
> -#endif
> -    /* -----common section-----*/
> -    if (*filename == NULL) {
> -        av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
> -        return -1;
> -    }
> -#if !HAVE_MKSTEMP
> -    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 */
> -}
> -
>  #if CONFIG_LIBXVID_ENCODER
>  
>  /**
> @@ -270,7 +232,7 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx)  {
>          rc2pass2.version = XVID_VERSION;
>          rc2pass2.bitrate = avctx->bit_rate;
>  
> -        fd = ff_tempfile("xvidff.", &(x->twopassfile));
> +        fd = av_tempfile("xvidff.", &(x->twopassfile));
>          if( fd == -1 ) {
>              av_log(avctx, AV_LOG_ERROR,
>                  "Xvid: Cannot write 2-pass pipe\n");
> diff --git a/libavutil/file.c b/libavutil/file.c
> index 31a3b75..4d14292 100644
> --- a/libavutil/file.c
> +++ b/libavutil/file.c
> @@ -130,6 +130,37 @@ void av_file_unmap(uint8_t *bufptr, size_t size)
>  #endif
>  }
>  
> +int av_tempfile(const char *prefix, char **filename) {

I suggest:
int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)

so you can provide a log_ctx rather than use NULL (check for example
av_file_map()).

maybe av_get_tempfile() but I'm not strong about it.

> +    int fd=-1;
> +#if !HAVE_MKSTEMP
> +    *filename = tempnam(".", prefix);
> +#else
> +    size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
> +    *filename = av_malloc(len);
> +#endif
> +    /* -----common section-----*/
> +    if (*filename == NULL) {
> +        av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
> +        return -1;

AVERROR(ENOMEM)

> +    }
> +#if !HAVE_MKSTEMP
> +    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;
> +    }

err = AVERROR(errno);
av_log(...);
return err; 



More information about the ffmpeg-cvslog mailing list