Revision 0328b9ea

View differences:

configure
1042 1042
    malloc_h
1043 1043
    memalign
1044 1044
    mkstemp
1045
    mmap
1045 1046
    pld
1046 1047
    posix_memalign
1047 1048
    round
......
2674 2675
check_func  isatty
2675 2676
check_func  ${malloc_prefix}memalign            && enable memalign
2676 2677
check_func  mkstemp
2678
check_func  mmap
2677 2679
check_func  ${malloc_prefix}posix_memalign      && enable posix_memalign
2678 2680
check_func  setrlimit
2679 2681
check_func  strerror_r
doc/APIchanges
13 13

  
14 14
API changes, most recent first:
15 15

  
16
2010-12-22 - r26073 - lavutil 50.36.0 - file.h
17
  Add functions av_file_map() and av_file_unmap() in file.h.
18

  
16 19
2010-12-19 - r26056 - lavutil 50.35.0 - error.h
17 20
  Add "not found" error codes:
18 21
  AVERROR_DEMUXER_NOT_FOUND
libavutil/Makefile
15 15
          error.h                                                       \
16 16
          eval.h                                                        \
17 17
          fifo.h                                                        \
18
          file.h                                                        \
18 19
          intfloat_readwrite.h                                          \
19 20
          intreadwrite.h                                                \
20 21
          lfg.h                                                         \
......
42 43
       error.o                                                          \
43 44
       eval.o                                                           \
44 45
       fifo.o                                                           \
46
       file.o                                                           \
45 47
       intfloat_readwrite.o                                             \
46 48
       inverse.o                                                        \
47 49
       lfg.o                                                            \
libavutil/avutil.h
40 40
#define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
41 41

  
42 42
#define LIBAVUTIL_VERSION_MAJOR 50
43
#define LIBAVUTIL_VERSION_MINOR 35
43
#define LIBAVUTIL_VERSION_MINOR 36
44 44
#define LIBAVUTIL_VERSION_MICRO  0
45 45

  
46 46
#define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
libavutil/file.c
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18

  
19
#include "file.h"
20
#include <fcntl.h>
21
#include <sys/stat.h>
22
#include <unistd.h>
23
#if HAVE_MMAP
24
#include <sys/mman.h>
25
#endif
26

  
27
typedef struct {
28
    const AVClass *class;
29
    int   log_offset;
30
    void *log_ctx;
31
} FileLogContext;
32

  
33
static const AVClass file_log_ctx_class = {
34
    "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
35
    offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
36
};
37

  
38
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
39
                int log_offset, void *log_ctx)
40
{
41
    FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
42
    int err, fd = open(filename, O_RDONLY);
43
    struct stat st;
44
    av_unused void *ptr;
45
    off_t off_size;
46
    char errbuf[128];
47
    size_t max_size = HAVE_MMAP ? SIZE_MAX : FF_INTERNAL_MEM_TYPE_MAX_VALUE;
48
    *bufptr = NULL;
49

  
50
    if (fd < 0) {
51
        err = AVERROR(errno);
52
        av_strerror(err, errbuf, sizeof(errbuf));
53
        av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
54
        return err;
55
    }
56

  
57
    if (lstat(filename, &st) < 0) {
58
        err = AVERROR(errno);
59
        av_strerror(err, errbuf, sizeof(errbuf));
60
        av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in lstat(): %s\n", errbuf);
61
        close(fd);
62
        return err;
63
    }
64

  
65
    off_size = st.st_size;
66
    if (off_size > max_size) {
67
        av_log(&file_log_ctx, AV_LOG_ERROR,
68
               "File size for file '%s' is too big\n", filename);
69
        close(fd);
70
        return AVERROR(EINVAL);
71
    }
72
    *size = off_size;
73

  
74
#if HAVE_MMAP
75
    ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
76
    if ((int)(ptr) == -1) {
77
        err = AVERROR(errno);
78
        av_strerror(err, errbuf, sizeof(errbuf));
79
        av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
80
        close(fd);
81
        return err;
82
    }
83
    *bufptr = ptr;
84
#else
85
    *bufptr = av_malloc(*size);
86
    if (!*bufptr) {
87
        av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
88
        close(fd);
89
        return AVERROR(ENOMEM);
90
    }
91
    read(fd, *bufptr, *size);
92
#endif
93

  
94
    close(fd);
95
    return 0;
96
}
97

  
98
void av_file_unmap(uint8_t *bufptr, size_t size)
99
{
100
#if HAVE_MMAP
101
    munmap(bufptr, size);
102
#else
103
    av_free(bufptr);
104
#endif
105
}
106

  
107
#ifdef TEST
108

  
109
#undef printf
110

  
111
int main(void)
112
{
113
    uint8_t *buf;
114
    size_t size;
115
    if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
116
        return 1;
117

  
118
    buf[0] = 's';
119
    printf("%s", buf);
120
    av_file_unmap(buf, size);
121
    return 0;
122
}
123
#endif
libavutil/file.h
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18

  
19
#ifndef AVUTIL_FILE_H
20
#define AVUTIL_FILE_H
21

  
22
#include "avutil.h"
23

  
24
/**
25
 * @file misc file utilities
26
 */
27

  
28
/**
29
 * Read the file with name filename, and put its content in a newly
30
 * allocated buffer or map it with mmap() when available.
31
 * In case of success set *bufptr to the read or mmapped buffer, and
32
 * *size to the size in bytes of the buffer in *bufptr.
33
 * The returned buffer must be released with av_file_unmap().
34
 *
35
 * @param log_offset loglevel offset used for logging
36
 * @param log_ctx context used for logging
37
 * @return a non negative number in case of success, a negative value
38
 * corresponding to an AVERROR error code in case of failure
39
 */
40
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
41
                int log_offset, void *log_ctx);
42

  
43
/**
44
 * Unmap or free the buffer bufptr created by av_file_map().
45
 *
46
 * @param size size in bytes of bufptr, must be the same as returned
47
 * by av_file_map()
48
 */
49
void av_file_unmap(uint8_t *bufptr, size_t size);
50

  
51
#endif /* AVUTIL_FILE_H */
libavutil/mem.h
64 64

  
65 65
#if LIBAVUTIL_VERSION_MAJOR < 51
66 66
#   define FF_INTERNAL_MEM_TYPE unsigned int
67
#   define FF_INTERNAL_MEM_TYPE_MAX_VALUE UINT_MAX
67 68
#else
68 69
#   define FF_INTERNAL_MEM_TYPE size_t
70
#   define FF_INTERNAL_MEM_TYPE_MAX_VALUE SIZE_MAX
69 71
#endif
70 72

  
71 73
/**

Also available in: Unified diff