[FFmpeg-devel] [PATCH 1/2] Add av_fd_dump(): read from fd into buffer up to EOF
Michael Niedermayer
michaelni at gmx.at
Tue Jun 24 17:25:47 CEST 2014
On Tue, Jun 24, 2014 at 04:27:27PM +0300, Andrey Utkin wrote:
> ---
> libavutil/file.c | 32 ++++++++++++++++++++++++++++++++
> libavutil/file.h | 30 ++++++++++++++++++++++++++++++
> 2 files changed, 62 insertions(+)
>
> diff --git a/libavutil/file.c b/libavutil/file.c
> index 45fe853..1c664f2 100644
> --- a/libavutil/file.c
> +++ b/libavutil/file.c
> @@ -184,6 +184,38 @@ int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_c
> return fd; /* success */
> }
>
> +int av_fd_dump(int fd, uint8_t **bufptr, size_t *size,
> + size_t spare_trailing_bytes, size_t buf_incr)
this needs a minor versio bump and APIchanges update
> +{
> + int ret;
> + int should_free_on_failure;
> + int bufsize;
> + assert(bufptr && size);
av_assert() of some kind or check, error message and error return
> + should_free_on_failure = (*bufptr == NULL);
> + /* We don't pass explicitly current allocated size, but it is safe to
> + * assume it equal to the offset to start, i.e. *size */
> + bufsize = *size;
> + while (1) {
> + if (bufsize - *size <= spare_trailing_bytes) {
> + bufsize += buf_incr;
> + ret = av_reallocp(bufptr, bufsize);
> + if (ret)
> + break;
> + }
> + ret = read(fd, *bufptr + *size, bufsize - *size - spare_trailing_bytes);
> + if (!ret) {
> + break;
> + } else if (ret == -1) {
> + ret = AVERROR(errno);
> + break;
> + }
> + *size += ret;
> + }
> + if (ret && should_free_on_failure)
> + av_reallocp(*bufptr, 0);
maybe reallocate to the exact size at the end to maybe free some
unneeded memory (also document this if user buffers are made smaller)
> + return ret;
> +}
> +
> #ifdef TEST
>
> #undef printf
> diff --git a/libavutil/file.h b/libavutil/file.h
> index a7364fe..db585d4 100644
> --- a/libavutil/file.h
> +++ b/libavutil/file.h
> @@ -63,4 +63,34 @@ void av_file_unmap(uint8_t *bufptr, size_t size);
> */
> int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx);
>
> +/**
> + * Reads from fd up to EOF, saves data in *bufptr and updates *size.
> + *
> + * @note You MUST initialize *bufptr and *size to valid values. If you set them
> + * to NULL and 0, it will allocate the array for you and fill it with fd
> + * product. Otherwise you can set it to an address of some previously
> + * av_realloc()-ed buffer and the offset to proceed writing, and it will
> + * effectively append fd product to existing content of array. Array will get
> + * extended by av_reallocp() if needed.
> + *
> + * @note According to av_realloc() doc, it may work wrong if supplied with
> + * av_malloc()-ed buffer. Use av_realloc() to create pre-allocated buffer.
> + *
> + * @note fd is not closed by this function
> + *
> + * @param fd file descriptor to read from
> + * @param bufptr pointer to pointer to buffer, it must be initialized by caller.
> + * @param size pointer to amount of data written to buffer, it must be
> + * initialized by caller.
> + * @param spare_trailing_bytes how many allocated bytes to hold for later
> + * buffer finalization. Set to 0 if not needed, set to 1 if you need to
> + * null-terminate the buffer.
> + * @param buf_incr amount of bytes to increase the buffer size when needed
its probably better to increase the buffer by some factor like
make it 10% bigger each time
a constant addition is harder to choose when the file size isnt
known
> + *
> + * @return zero on success, negative error code on failure
> + *
> + */
> +int av_fd_dump(int fd, uint8_t **bufptr, size_t *size,
> + size_t spare_trailing_bytes, size_t buf_incr);
> +
> #endif /* AVUTIL_FILE_H */
> --
> 1.8.3.2
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
The real ebay dictionary, page 2
"100% positive feedback" - "All either got their money back or didnt complain"
"Best seller ever, very honest" - "Seller refunded buyer after failed scam"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140624/acbabd22/attachment.asc>
More information about the ffmpeg-devel
mailing list