| @@ -20,6 +20,7 @@ · 19 unmodified lines |
| 20 | 20 | |
| 21 | 21 | #include "framepool.h" |
| 22 | 22 | #include "libavutil/avassert.h" |
| 23 | +#include "libavutil/avutil.h" |
| 23 | 24 | #include "libavutil/buffer.h" |
| 24 | 25 | #include "libavutil/frame.h" |
| 25 | 26 | #include "libavutil/imgutils.h" |
| @@ -28,8 +29,18 @@ · 2 unmodified lines |
| 28 | 29 | |
| 29 | 30 | struct FFFramePool { |
| 30 | 31 | |
| 32 | + enum AVMediaType type; |
| 33 | + |
| 34 | + /* video */ |
| 31 | 35 | int width; |
| 32 | 36 | int height; |
| 37 | + |
| 38 | + /* audio */ |
| 39 | + int planes; |
| 40 | + int channels; |
| 41 | + int nb_samples; |
| 42 | + |
| 43 | + /* common */ |
| 33 | 44 | int format; |
| 34 | 45 | int align; |
| 35 | 46 | int linesize[4]; |
| @@ -54,6 +65,7 @@ FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size), · 18 unmodified lines |
| 54 | 65 | if (!pool) |
| 55 | 66 | return NULL; |
| 56 | 67 | |
| 68 | + pool->type = AVMEDIA_TYPE_VIDEO; |
| 57 | 69 | pool->width = width; |
| 58 | 70 | pool->height = height; |
| 59 | 71 | pool->format = format; |
| @@ -104,6 +116,44 @@ fail: · 44 unmodified lines |
| 104 | 116 | return NULL; |
| 105 | 117 | } |
| 106 | 118 | |
| 119 | +FFFramePool *ff_frame_pool_audio_init(AVBufferRef* (*alloc)(int size), |
| 120 | + int channels, |
| 121 | + int nb_samples, |
| 122 | + enum AVSampleFormat format, |
| 123 | + int align) |
| 124 | +{ |
| 125 | + int ret, planar; |
| 126 | + FFFramePool *pool; |
| 127 | + |
| 128 | + pool = av_mallocz(sizeof(FFFramePool)); |
| 129 | + if (!pool) |
| 130 | + return NULL; |
| 131 | + |
| 132 | + planar = av_sample_fmt_is_planar(format); |
| 133 | + |
| 134 | + pool->type = AVMEDIA_TYPE_AUDIO; |
| 135 | + pool->planes = planar ? channels : 1; |
| 136 | + pool->channels = channels; |
| 137 | + pool->nb_samples = nb_samples; |
| 138 | + pool->format = format; |
| 139 | + pool->align = align; |
| 140 | + |
| 141 | + ret = av_samples_get_buffer_size(&pool->linesize[0], channels, |
| 142 | + nb_samples, format, 0); |
| 143 | + if (ret < 0) |
| 144 | + goto fail; |
| 145 | + |
| 146 | + pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL); |
| 147 | + if (!pool->pools[0]) |
| 148 | + goto fail; |
| 149 | + |
| 150 | + return pool; |
| 151 | + |
| 152 | +fail: |
| 153 | + ff_frame_pool_uninit(&pool); |
| 154 | + return NULL; |
| 155 | +} |
| 156 | + |
| 107 | 157 | int ff_frame_pool_get_video_config(FFFramePool *pool, |
| 108 | 158 | int *width, |
| 109 | 159 | int *height, |
| @@ -113,6 +163,8 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, · 3 unmodified lines |
| 113 | 163 | if (!pool) |
| 114 | 164 | return AVERROR(EINVAL); |
| 115 | 165 | |
| 166 | + av_assert0(pool->type == AVMEDIA_TYPE_VIDEO); |
| 167 | + |
| 116 | 168 | *width = pool->width; |
| 117 | 169 | *height = pool->height; |
| 118 | 170 | *format = pool->format; |
| @@ -121,6 +173,24 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, · 2 unmodified lines |
| 121 | 173 | return 0; |
| 122 | 174 | } |
| 123 | 175 | |
| 176 | +int ff_frame_pool_get_audio_config(FFFramePool *pool, |
| 177 | + int *channels, |
| 178 | + int *nb_samples, |
| 179 | + enum AVSampleFormat *format, |
| 180 | + int *align) |
| 181 | +{ |
| 182 | + if (!pool) |
| 183 | + return AVERROR(EINVAL); |
| 184 | + |
| 185 | + av_assert0(pool->type == AVMEDIA_TYPE_AUDIO); |
| 186 | + |
| 187 | + *channels = pool->channels; |
| 188 | + *nb_samples = pool->nb_samples; |
| 189 | + *format = pool->format; |
| 190 | + *align = pool->align; |
| 191 | + |
| 192 | + return 0; |
| 193 | +} |
| 124 | 194 | |
| 125 | 195 | AVFrame *ff_frame_pool_get(FFFramePool *pool) |
| 126 | 196 | { |
| @@ -133,6 +203,8 @@ AVFrame *ff_frame_pool_get(FFFramePool *pool) · 6 unmodified lines |
| 133 | 203 | return NULL; |
| 134 | 204 | } |
| 135 | 205 | |
| 206 | + switch(pool->type) { |
| 207 | + case AVMEDIA_TYPE_VIDEO: |
| 136 | 208 | desc = av_pix_fmt_desc_get(pool->format); |
| 137 | 209 | if (!desc) { |
| 138 | 210 | goto fail; |
| @@ -167,6 +239,43 @@ AVFrame *ff_frame_pool_get(FFFramePool *pool) · 28 unmodified lines |
| 167 | 239 | } |
| 168 | 240 | |
| 169 | 241 | frame->extended_data = frame->data; |
| 242 | + break; |
| 243 | + case AVMEDIA_TYPE_AUDIO: |
| 244 | + frame->nb_samples = pool->nb_samples; |
| 245 | + av_frame_set_channels(frame, pool->channels); |
| 246 | + frame->format = pool->format; |
| 247 | + frame->linesize[0] = pool->linesize[0]; |
| 248 | + |
| 249 | + if (pool->planes > AV_NUM_DATA_POINTERS) { |
| 250 | + frame->extended_data = av_mallocz_array(pool->planes, |
| 251 | + sizeof(*frame->extended_data)); |
| 252 | + frame->nb_extended_buf = pool->planes - AV_NUM_DATA_POINTERS; |
| 253 | + frame->extended_buf = av_mallocz_array(frame->nb_extended_buf, |
| 254 | + sizeof(*frame->extended_buf)); |
| 255 | + if (!frame->extended_data || !frame->extended_buf) |
| 256 | + goto fail; |
| 257 | + } else { |
| 258 | + frame->extended_data = frame->data; |
| 259 | + av_assert0(frame->nb_extended_buf == 0); |
| 260 | + } |
| 261 | + |
| 262 | + for (i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) { |
| 263 | + frame->buf[i] = av_buffer_pool_get(pool->pools[0]); |
| 264 | + if (!frame->buf[i]) |
| 265 | + goto fail; |
| 266 | + frame->extended_data[i] = frame->data[i] = frame->buf[i]->data; |
| 267 | + } |
| 268 | + for (i = 0; i < frame->nb_extended_buf; i++) { |
| 269 | + frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]); |
| 270 | + if (!frame->extended_buf[i]) |
| 271 | + goto fail; |
| 272 | + frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data; |
| 273 | + } |
| 274 | + |
| 275 | + break; |
| 276 | + default: |
| 277 | + av_assert0(0); |
| 278 | + } |
| 170 | 279 | |
| 171 | 280 | return frame; |
| 172 | 281 | fail: |