Statistics
| Branch: | Revision:

ffmpeg / libavcore / samplefmt.c @ e98b8e2f

History | View | Annotate | Download (3.95 KB)

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 "avcore.h"
20
#include "samplefmt.h"
21

    
22
typedef struct SampleFmtInfo {
23
    const char *name;
24
    int bits;
25
} SampleFmtInfo;
26

    
27
/** this table gives more information about formats */
28
static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
29
    [AV_SAMPLE_FMT_U8]  = { .name = "u8",  .bits = 8 },
30
    [AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16 },
31
    [AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32 },
32
    [AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32 },
33
    [AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64 },
34
};
35

    
36
const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
37
{
38
    if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
39
        return NULL;
40
    return sample_fmt_info[sample_fmt].name;
41
}
42

    
43
enum AVSampleFormat av_get_sample_fmt(const char *name)
44
{
45
    int i;
46

    
47
    for (i = 0; i < AV_SAMPLE_FMT_NB; i++)
48
        if (!strcmp(sample_fmt_info[i].name, name))
49
            return i;
50
    return AV_SAMPLE_FMT_NONE;
51
}
52

    
53
char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
54
{
55
    /* print header */
56
    if (sample_fmt < 0)
57
        snprintf(buf, buf_size, "name  " " depth");
58
    else if (sample_fmt < AV_SAMPLE_FMT_NB) {
59
        SampleFmtInfo info = sample_fmt_info[sample_fmt];
60
        snprintf (buf, buf_size, "%-6s" "   %2d ", info.name, info.bits);
61
    }
62

    
63
    return buf;
64
}
65

    
66
int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
67
{
68
    return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
69
        0 : sample_fmt_info[sample_fmt].bits;
70
}
71

    
72
int av_samples_fill_arrays(uint8_t *pointers[8], int linesizes[8],
73
                           uint8_t *buf, int nb_channels, int nb_samples,
74
                           enum AVSampleFormat sample_fmt, int planar, int align)
75
{
76
    int i, step_size = 0;
77
    int sample_size = av_get_bits_per_sample_fmt(sample_fmt) >> 3;
78
    int channel_step = planar ? FFALIGN(nb_samples*sample_size, align) : sample_size;
79

    
80
    if(nb_channels * (uint64_t)nb_samples * sample_size >= INT_MAX - align*(uint64_t)nb_channels)
81
        return AVERROR(EINVAL);
82

    
83
    if (pointers) {
84
        pointers[0] = buf;
85
        for (i = 0; i < nb_channels; i++) {
86
            pointers[i] = buf + step_size;
87
            step_size += channel_step;
88
        }
89
        memset(&pointers[nb_channels], 0, (8-nb_channels) * sizeof(pointers[0]));
90
    }
91

    
92
    if (linesizes) {
93
        linesizes[0] = planar ?  sample_size : nb_channels*sample_size;
94
        memset(&linesizes[1], 0, (8-1) * sizeof(linesizes[0]));
95
    }
96

    
97
    return planar ? channel_step * nb_channels : FFALIGN(nb_channels*sample_size*nb_samples, align);
98
}
99

    
100
int av_samples_alloc(uint8_t *pointers[8], int linesizes[8],
101
                     int nb_samples, int nb_channels,
102
                     enum AVSampleFormat sample_fmt, int planar,
103
                     int align)
104
{
105
    uint8_t *buf;
106
    int size = av_samples_fill_arrays(NULL, NULL,
107
                                      NULL, nb_channels, nb_samples,
108
                                      sample_fmt, planar, align);
109

    
110
    buf = av_mallocz(size);
111
    if (!buf)
112
        return AVERROR(ENOMEM);
113

    
114
    return av_samples_fill_arrays(pointers, linesizes,
115
                                  buf, nb_channels, nb_samples,
116
                                  sample_fmt, planar, align);
117
}