History

lavfi/framepool: add audio support

Matthieu Bouron committed 9 years ago

Commit eb336811 parent b1f68f02 files · +144 −1Browse files

Unified · Split

2 files
  1. libavfilter/framepool.c +109 −0
  2. libavfilter/framepool.h +35 −1
libavfilter/framepool.c+109−0
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020
2121 #include "framepool.h"
2222 #include "libavutil/avassert.h"
23+#include "libavutil/avutil.h"
2324 #include "libavutil/buffer.h"
2425 #include "libavutil/frame.h"
2526 #include "libavutil/imgutils.h"
@@ -28,8 +29,18 @@ · 2 unmodified lines
2829
2930 struct FFFramePool {
3031
32+ enum AVMediaType type;
33+
34+ /* video */
3135 int width;
3236 int height;
37+
38+ /* audio */
39+ int planes;
40+ int channels;
41+ int nb_samples;
42+
43+ /* common */
3344 int format;
3445 int align;
3546 int linesize[4];
@@ -54,6 +65,7 @@ FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size), · 18 unmodified lines
5465 if (!pool)
5566 return NULL;
5667
68+ pool->type = AVMEDIA_TYPE_VIDEO;
5769 pool->width = width;
5870 pool->height = height;
5971 pool->format = format;
@@ -104,6 +116,44 @@ fail: · 44 unmodified lines
104116 return NULL;
105117 }
106118
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+
107157 int ff_frame_pool_get_video_config(FFFramePool *pool,
108158 int *width,
109159 int *height,
@@ -113,6 +163,8 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, · 3 unmodified lines
113163 if (!pool)
114164 return AVERROR(EINVAL);
115165
166+ av_assert0(pool->type == AVMEDIA_TYPE_VIDEO);
167+
116168 *width = pool->width;
117169 *height = pool->height;
118170 *format = pool->format;
@@ -121,6 +173,24 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, · 2 unmodified lines
121173 return 0;
122174 }
123175
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+}
124194
125195 AVFrame *ff_frame_pool_get(FFFramePool *pool)
126196 {
@@ -133,6 +203,8 @@ AVFrame *ff_frame_pool_get(FFFramePool *pool) · 6 unmodified lines
133203 return NULL;
134204 }
135205
206+ switch(pool->type) {
207+ case AVMEDIA_TYPE_VIDEO:
136208 desc = av_pix_fmt_desc_get(pool->format);
137209 if (!desc) {
138210 goto fail;
@@ -167,6 +239,43 @@ AVFrame *ff_frame_pool_get(FFFramePool *pool) · 28 unmodified lines
167239 }
168240
169241 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+ }
170279
171280 return frame;
172281 fail:
libavfilter/framepool.h+35−1
@@ -41,7 +41,7 @@ typedef struct FFFramePool FFFramePool; · 40 unmodified lines
4141 * @param height height of each frame in this pool
4242 * @param format format of each frame in this pool
4343 * @param align buffers alignement of each frame in this pool
44− * @return newly created frame pool on success, NULL on error.
44+ * @return newly created video frame pool on success, NULL on error.
4545 */
4646 FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size),
4747 int width,
@@ -49,6 +49,24 @@ FFFramePool *ff_frame_pool_video_init(AVBufferRef* (*alloc)(int size), · 1 unmodified line
4949 enum AVPixelFormat format,
5050 int align);
5151
52+/**
53+ * Allocate and initialize an audio frame pool.
54+ *
55+ * @param alloc a function that will be used to allocate new frame buffers when
56+ * the pool is empty. May be NULL, then the default allocator will be used
57+ * (av_buffer_alloc()).
58+ * @param channels channels of each frame in this pool
59+ * @param nb_samples number of samples of each frame in this pool
60+ * @param format format of each frame in this pool
61+ * @param align buffers alignement of each frame in this pool
62+ * @return newly created audio frame pool on success, NULL on error.
63+ */
64+FFFramePool *ff_frame_pool_audio_init(AVBufferRef* (*alloc)(int size),
65+ int channels,
66+ int samples,
67+ enum AVSampleFormat format,
68+ int align);
69+
5270 /**
5371 * Deallocate the frame pool. It is safe to call this function while
5472 * some of the allocated frame are still in use.
@@ -72,6 +90,22 @@ int ff_frame_pool_get_video_config(FFFramePool *pool, · 17 unmodified lines
7290 enum AVPixelFormat *format,
7391 int *align);
7492
93+/**
94+ * Get the audio frame pool configuration.
95+ *
96+ * @param channels channels of each frame in this pool
97+ * @param nb_samples number of samples of each frame in this pool
98+ * @param format format of each frame in this pool
99+ * @param align buffers alignement of each frame in this pool
100+ * @return 0 on success, a negative AVERROR otherwise.
101+ */
102+int ff_frame_pool_get_audio_config(FFFramePool *pool,
103+ int *channels,
104+ int *nb_samples,
105+ enum AVSampleFormat *format,
106+ int *align);
107+
108+
75109 /**
76110 * Allocate a new AVFrame, reussing old buffers from the pool when available.
77111 * This function may be called simultaneously from multiple threads.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t