History

qsvenc: add an API for allocating opaque surfaces

Anton Khirnov committed 11 years ago

Commit dc923bc1 parent 2ec96b64 files · +111 −5Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavcodec/qsv.h +47 −0
  3. libavcodec/qsvenc.c +53 −4
  4. libavcodec/qsvenc.h +8 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2015-08-28 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2015-xx-xx - xxxxxxx - lavc 57.0.0 - qsv.h
17+ Add an API for allocating opaque surfaces.
18+
1619 2015-xx-xx - xxxxxxx - lavu 55.2.0 - dict.h
1720 Change return type of av_dict_copy() from void to int, so that a proper
1821 error code can be reported.
libavcodec/qsv.h+47−0
@@ -23,6 +23,8 @@ · 22 unmodified lines
2323
2424 #include <mfx/mfxvideo.h>
2525
26+#include "libavutil/buffer.h"
27+
2628 /**
2729 * This struct is used for communicating QSV parameters between libavcodec and
2830 * the caller. It is managed by the caller and must be assigned to
@@ -48,6 +50,51 @@ typedef struct AVQSVContext { · 19 unmodified lines
4850 */
4951 mfxExtBuffer **ext_buffers;
5052 int nb_ext_buffers;
53+
54+ /**
55+ * Encoding only. If this field is set to non-zero by the caller, libavcodec
56+ * will create an mfxExtOpaqueSurfaceAlloc extended buffer and pass it to
57+ * the encoder initialization. This only makes sense if iopattern is also
58+ * set to MFX_IOPATTERN_IN_OPAQUE_MEMORY.
59+ *
60+ * The number of allocated opaque surfaces will be the sum of the number
61+ * required by the encoder and the user-provided value nb_opaque_surfaces.
62+ * The array of the opaque surfaces will be exported to the caller through
63+ * the opaque_surfaces field.
64+ */
65+ int opaque_alloc;
66+
67+ /**
68+ * Encoding only, and only if opaque_alloc is set to non-zero. Before
69+ * calling avcodec_open2(), the caller should set this field to the number
70+ * of extra opaque surfaces to allocate beyond what is required by the
71+ * encoder.
72+ *
73+ * On return from avcodec_open2(), this field will be set by libavcodec to
74+ * the total number of allocated opaque surfaces.
75+ */
76+ int nb_opaque_surfaces;
77+
78+ /**
79+ * Encoding only, and only if opaque_alloc is set to non-zero. On return
80+ * from avcodec_open2(), this field will be used by libavcodec to export the
81+ * array of the allocated opaque surfaces to the caller, so they can be
82+ * passed to other parts of the pipeline.
83+ *
84+ * The buffer reference exported here is owned and managed by libavcodec,
85+ * the callers should make their own reference with av_buffer_ref() and free
86+ * it with av_buffer_unref() when it is no longer needed.
87+ *
88+ * The buffer data is an nb_opaque_surfaces-sized array of mfxFrameSurface1.
89+ */
90+ AVBufferRef *opaque_surfaces;
91+
92+ /**
93+ * Encoding only, and only if opaque_alloc is set to non-zero. On return
94+ * from avcodec_open2(), this field will be set to the surface type used in
95+ * the opaque allocation request.
96+ */
97+ int opaque_alloc_type;
5198 } AVQSVContext;
5299
53100 /**
libavcodec/qsvenc.c+53−4
@@ -134,7 +134,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) · 133 unmodified lines
134134 q->extco.CAVLC = avctx->coder_type == FF_CODER_TYPE_VLC ?
135135 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
136136
137− q->extparam_internal[0] = (mfxExtBuffer *)&q->extco;
137+ q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco;
138138 }
139139
140140 return 0;
@@ -187,8 +187,46 @@ static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q) · 46 unmodified lines
187187 return 0;
188188 }
189189
190+static int qsv_init_opaque_alloc(AVCodecContext *avctx, QSVEncContext *q)
191+{
192+ AVQSVContext *qsv = avctx->hwaccel_context;
193+ mfxFrameSurface1 *surfaces;
194+ int nb_surfaces, i;
195+
196+ nb_surfaces = qsv->nb_opaque_surfaces + q->req.NumFrameSuggested + q->async_depth;
197+
198+ q->opaque_alloc_buf = av_buffer_allocz(sizeof(*surfaces) * nb_surfaces);
199+ if (!q->opaque_alloc_buf)
200+ return AVERROR(ENOMEM);
201+
202+ q->opaque_surfaces = av_malloc_array(nb_surfaces, sizeof(*q->opaque_surfaces));
203+ if (!q->opaque_surfaces)
204+ return AVERROR(ENOMEM);
205+
206+ surfaces = (mfxFrameSurface1*)q->opaque_alloc_buf->data;
207+ for (i = 0; i < nb_surfaces; i++) {
208+ surfaces[i].Info = q->req.Info;
209+ q->opaque_surfaces[i] = surfaces + i;
210+ }
211+
212+ q->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
213+ q->opaque_alloc.Header.BufferSz = sizeof(q->opaque_alloc);
214+ q->opaque_alloc.In.Surfaces = q->opaque_surfaces;
215+ q->opaque_alloc.In.NumSurface = nb_surfaces;
216+ q->opaque_alloc.In.Type = q->req.Type;
217+
218+ q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->opaque_alloc;
219+
220+ qsv->nb_opaque_surfaces = nb_surfaces;
221+ qsv->opaque_surfaces = q->opaque_alloc_buf;
222+ qsv->opaque_alloc_type = q->req.Type;
223+
224+ return 0;
225+}
226+
190227 int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
191228 {
229+ int opaque_alloc = 0;
192230 int ret;
193231
194232 q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
@@ -204,6 +242,8 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) · 9 unmodified lines
204242
205243 q->session = qsv->session;
206244 q->param.IOPattern = qsv->iopattern;
245+
246+ opaque_alloc = qsv->opaque_alloc;
207247 }
208248
209249 if (!q->session) {
@@ -225,11 +265,17 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) · 15 unmodified lines
225265 return ff_qsv_error(ret);
226266 }
227267
268+ if (opaque_alloc) {
269+ ret = qsv_init_opaque_alloc(avctx, q);
270+ if (ret < 0)
271+ return ret;
272+ }
273+
228274 if (avctx->hwaccel_context) {
229275 AVQSVContext *qsv = avctx->hwaccel_context;
230276 int i, j;
231277
232− q->extparam = av_mallocz_array(qsv->nb_ext_buffers + FF_ARRAY_ELEMS(q->extparam_internal),
278+ q->extparam = av_mallocz_array(qsv->nb_ext_buffers + q->nb_extparam_internal,
233279 sizeof(*q->extparam));
234280 if (!q->extparam)
235281 return AVERROR(ENOMEM);
@@ -239,7 +285,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) · 3 unmodified lines
239285 q->param.ExtParam[i] = qsv->ext_buffers[i];
240286 q->param.NumExtParam = qsv->nb_ext_buffers;
241287
242− for (i = 0; i < FF_ARRAY_ELEMS(q->extparam_internal); i++) {
288+ for (i = 0; i < q->nb_extparam_internal; i++) {
243289 for (j = 0; j < qsv->nb_ext_buffers; j++) {
244290 if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
245291 break;
@@ -251,7 +297,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) · 5 unmodified lines
251297 }
252298 } else {
253299 q->param.ExtParam = q->extparam_internal;
254− q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam_internal);
300+ q->param.NumExtParam = q->nb_extparam_internal;
255301 }
256302
257303 ret = MFXVideoENCODE_Init(q->session, &q->param);
@@ -537,6 +583,9 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) · 279 unmodified lines
537583 av_fifo_free(q->async_fifo);
538584 q->async_fifo = NULL;
539585
586+ av_freep(&q->opaque_surfaces);
587+ av_buffer_unref(&q->opaque_alloc_buf);
588+
540589 av_freep(&q->extparam);
541590
542591 return 0;
libavcodec/qsvenc.h+8−1
@@ -49,7 +49,14 @@ typedef struct QSVEncContext { · 48 unmodified lines
4949 mfxFrameAllocRequest req;
5050
5151 mfxExtCodingOption extco;
52− mfxExtBuffer *extparam_internal[1];
52+
53+ mfxExtOpaqueSurfaceAlloc opaque_alloc;
54+ mfxFrameSurface1 **opaque_surfaces;
55+ AVBufferRef *opaque_alloc_buf;
56+
57+ mfxExtBuffer *extparam_internal[2];
58+ int nb_extparam_internal;
59+
5360 mfxExtBuffer **extparam;
5461
5562 AVFifoBuffer *async_fifo;

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