[FFmpeg-devel] [PATCH 4/5] file: Add 'blocksize' option

Nicolas George nicolas.george at normalesup.org
Sat Jul 13 13:48:14 CEST 2013


Le tridi 23 messidor, an CCXXI, Andrey Utkin a écrit :
> Interruptibility of file operations is strongly desirable in case of
> slow storage access, e.g. mounted network share.
> This commit introduces possibility to limit data quantity transferred by
> 'file' protocol at once. By default, old behaviour is preserved and data
> is still tried to be transferred without block size limitation.
> 
> Note that file I/O operation still may block (or even freeze) inside of
> single read(2) or write(2) operation. There is no API for
> asynchronous file I/O, but hopefully it will appear in future.

There is an API for asynchronous file I/O operation, see aio.h. Its internal
implementation in GNU/Linux is to create a thread, though, so it is not
really a satisfactory solution.

> ---
>  libavformat/file.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/libavformat/file.c b/libavformat/file.c
> index e09a64b..bbc474f 100644
> --- a/libavformat/file.c
> +++ b/libavformat/file.c
> @@ -49,10 +49,12 @@ typedef struct FileContext {
>      const AVClass *class;
>      int fd;
>      int trunc;
> +    int blocksize;
>  } FileContext;
>  
>  static const AVOption file_options[] = {
>      { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
> +    { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },

Why not set the default value to INT_MAX and the minimum value to 1? That
way...

>      { NULL }
>  };
>  
> @@ -66,14 +68,20 @@ static const AVClass file_class = {
>  static int file_read(URLContext *h, unsigned char *buf, int size)
>  {
>      FileContext *c = h->priv_data;
> -    int r = read(c->fd, buf, size);
> +    int r;
> +    if (c->blocksize && (size > c->blocksize))
> +        size = c->blocksize;


... you could just write "size = FFMIN(size, c->blocksize)" here instead of
special-casing 0.

> +    r = read(c->fd, buf, size);
>      return (-1 == r)?AVERROR(errno):r;
>  }
>  
>  static int file_write(URLContext *h, const unsigned char *buf, int size)
>  {
>      FileContext *c = h->priv_data;
> -    int r = write(c->fd, buf, size);
> +    int r;
> +    if (c->blocksize && (size > c->blocksize))
> +        size = c->blocksize;
> +    r = write(c->fd, buf, size);
>      return (-1 == r)?AVERROR(errno):r;
>  }

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130713/0eb821a3/attachment.asc>


More information about the ffmpeg-devel mailing list