Revision e98b8e2f

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
}
libavcore/samplefmt.h
69 69
 */
70 70
int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt);
71 71

  
72
/**
73
 * Fill channel data pointers and linesizes for samples with sample
74
 * format sample_fmt.
75
 *
76
 * The pointers array is filled with the pointers to the samples data:
77
 * data[c] points to the first sample of channel c.
78
 * data[c] + linesize[0] points to the second sample of channel c
79
 *
80
 * @param pointers array to be filled with the pointer for each plane, may be NULL
81
 * @param linesizes array to be filled with the linesize, may be NULL
82
 * @param buf the pointer to a buffer containing the samples
83
 * @param nb_samples the number of samples in a single channel
84
 * @param planar 1 if the samples layout is planar, 0 if it is packed
85
 * @param nb_channels the number of channels
86
 * @return the total size of the buffer, a negative
87
 * error code in case of failure
88
 */
89
int av_samples_fill_arrays(uint8_t *pointers[8], int linesizes[8],
90
                           uint8_t *buf, int nb_channels, int nb_samples,
91
                           enum AVSampleFormat sample_fmt, int planar, int align);
92

  
93
/**
94
 * Allocate a samples buffer for nb_samples samples, and
95
 * fill pointers and linesizes accordingly.
96
 * The allocated samples buffer has to be freed by using
97
 * av_freep(&pointers[0]).
98
 *
99
 * @param nb_samples number of samples per channel
100
 * @param planar 1 if the samples layout is planar, 0 if packed,
101
 * @param align the value to use for buffer size alignment
102
 * @return the size in bytes required for the samples buffer, a negative
103
 * error code in case of failure
104
 * @see av_samples_fill_arrays()
105
 */
106
int av_samples_alloc(uint8_t *pointers[8], int linesizes[8],
107
                     int nb_samples, int nb_channels,
108
                     enum AVSampleFormat sample_fmt, int planar,
109
                     int align);
110

  
72 111
#endif /* AVCORE_SAMPLEFMT_H */

Also available in: Unified diff