History

avutil/hwcontext_videotoolbox: create real buffer pool

vt_get_buffer shouldn't do buffer pool's job.

Zhao Zhili committed 4 years ago

Commit c3f00da1 parent 301141b1 file · +34 −37Browse files

Unified · Split

libavutil/hwcontext_videotoolbox.c+34−37
@@ -210,9 +210,36 @@ static int vt_pool_alloc(AVHWFramesContext *ctx) · 209 unmodified lines
210210 return AVERROR_EXTERNAL;
211211 }
212212
213−static AVBufferRef *vt_dummy_pool_alloc(void *opaque, size_t size)
213+static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
214+{
215+ CVPixelBufferRelease((CVPixelBufferRef)data);
216+}
217+
218+static AVBufferRef *vt_pool_alloc_buffer(void *opaque, size_t size)
214219 {
215− return NULL;
220+ CVPixelBufferRef pixbuf;
221+ AVBufferRef *buf;
222+ CVReturn err;
223+ AVHWFramesContext *ctx = opaque;
224+ VTFramesContext *fctx = ctx->internal->priv;
225+
226+ err = CVPixelBufferPoolCreatePixelBuffer(
227+ NULL,
228+ fctx->pool,
229+ &pixbuf
230+ );
231+ if (err != kCVReturnSuccess) {
232+ av_log(ctx, AV_LOG_ERROR, "Failed to create pixel buffer from pool: %d\n", err);
233+ return NULL;
234+ }
235+
236+ buf = av_buffer_create((uint8_t *)pixbuf, size,
237+ videotoolbox_buffer_release, NULL, 0);
238+ if (!buf) {
239+ CVPixelBufferRelease(pixbuf);
240+ return NULL;
241+ }
242+ return buf;
216243 }
217244
218245 static void vt_frames_uninit(AVHWFramesContext *ctx)
@@ -238,9 +265,9 @@ static int vt_frames_init(AVHWFramesContext *ctx) · 19 unmodified lines
238265 return AVERROR(ENOSYS);
239266 }
240267
241− // create a dummy pool so av_hwframe_get_buffer doesn't EINVAL
242268 if (!ctx->pool) {
243− ctx->internal->pool_internal = av_buffer_pool_init2(0, ctx, vt_dummy_pool_alloc, NULL);
269+ ctx->internal->pool_internal = av_buffer_pool_init2(
270+ sizeof(CVPixelBufferRef), ctx, vt_pool_alloc_buffer, NULL);
244271 if (!ctx->internal->pool_internal)
245272 return AVERROR(ENOMEM);
246273 }
@@ -252,41 +279,11 @@ static int vt_frames_init(AVHWFramesContext *ctx) · 5 unmodified lines
252279 return 0;
253280 }
254281
255−static void videotoolbox_buffer_release(void *opaque, uint8_t *data)
256−{
257− CVPixelBufferRelease((CVPixelBufferRef)data);
258−}
259−
260282 static int vt_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
261283 {
262− VTFramesContext *fctx = ctx->internal->priv;
263−
264− if (ctx->pool && ctx->pool->size != 0) {
265− frame->buf[0] = av_buffer_pool_get(ctx->pool);
266− if (!frame->buf[0])
267− return AVERROR(ENOMEM);
268− } else {
269− CVPixelBufferRef pixbuf;
270− AVBufferRef *buf = NULL;
271− CVReturn err;
272−
273− err = CVPixelBufferPoolCreatePixelBuffer(
274− NULL,
275− fctx->pool,
276− &pixbuf
277− );
278− if (err != kCVReturnSuccess) {
279− av_log(ctx, AV_LOG_ERROR, "Failed to create pixel buffer from pool: %d\n", err);
280− return AVERROR_EXTERNAL;
281− }
282−
283− buf = av_buffer_create((uint8_t *)pixbuf, 1, videotoolbox_buffer_release, NULL, 0);
284− if (!buf) {
285− CVPixelBufferRelease(pixbuf);
286− return AVERROR(ENOMEM);
287− }
288− frame->buf[0] = buf;
289− }
284+ frame->buf[0] = av_buffer_pool_get(ctx->pool);
285+ if (!frame->buf[0])
286+ return AVERROR(ENOMEM);
290287
291288 frame->data[3] = frame->buf[0]->data;
292289 frame->format = AV_PIX_FMT_VIDEOTOOLBOX;

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