Revision e98b8e2f libavcore/samplefmt.c

View differences:

libavcore/samplefmt.c
68 68
    return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
69 69
        0 : sample_fmt_info[sample_fmt].bits;
70 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
}

Also available in: Unified diff