Working Directly with VA API for Linux*¶
Intel® Media Software Development Kit takes care of all memory and synchronization related operations in the VA API. The application may need to extend Intel® Media Software Development Kit functionality by working directly with the VA API for Linux*, for example to implement a customized external allocator. This section describes basic memory management and synchronization techniques.
To create the VA surface pool, the application should call the vaCreateSurfaces function:
1 2 3 4 5 6 7 8 9 10 11
const int num_surfaces = 5; VASurfaceID surfaces[num_surfaces]; VASurfaceAttrib attrib; attrib.type = VASurfaceAttribPixelFormat; attrib.value.type = VAGenericValueTypeInteger; attrib.value.value.i = VA_FOURCC_NV12; attrib.flags = VA_SURFACE_ATTRIB_SETTABLE; vaCreateSurfaces(va_display, VA_RT_FORMAT_YUV420, width, height, surfaces, num_surfaces, &attrib, 1);
To destroy the surface pool, the application should call the vaDestroySurfaces function:
vaDestroySurfaces(va_display, surfaces, num_surfaces);
If the application works with hardware acceleration through Intel® Media Software Development Kit, then it can
access surface data immediately after successful completion of
MFXVideoCORE_SyncOperation() call. If the application works with
hardware acceleration directly, then it must check surface status before
accessing data in video memory. This check can be done asynchronously by calling
the vaQuerySurfaceStatus function or synchronously by calling the vaSyncSurface
After successful synchronization, the application can access surface data. Accessing surface data is performed in two steps:
Create VAImage from surface.
Map image buffer to system memory.
After mapping, the VAImage.offsets array holds offsets to each color plain in a mapped buffer and the VAImage.pitches array holds color plain pitches in bytes. For packed data formats, only first entries in these arrays are valid. The following example shows how to access data in a NV12 surface:
1 2 3 4 5 6 7 8 9 10 11
VAImage image; unsigned char *Y, *U, *V; void* buffer; vaDeriveImage(va_display, surfaceToMap, &image); vaMapBuffer(va_display, image.buf, &buffer); /* NV12 */ Y = (unsigned char*)buffer + image.offsets; U = (unsigned char*)buffer + image.offsets; V = U + 1;
After processing data in a VA surface, the application should release resources allocated for the mapped buffer and VAImage object:
vaUnmapBuffer(va_display, image.buf); vaDestroyImage(va_display, image.image_id);
In some cases, in order to retrieve encoded bitstream data from video memory, the application must use the VABuffer to store data. The following example shows how to create, use, and destroy the VABuffer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
VABufferID buf_id; size_t size; uint32_t offset; void *buf; /* create buffer */ vaCreateBuffer(va_display, va_context, VAEncCodedBufferType, buf_size, 1, NULL, & buf_id); /* encode frame */ // ... /* map buffer */ VACodedBufferSegment *coded_buffer_segment; vaMapBuffer(va_display, buf_id, (void **)(&coded_buffer_segment)); size = coded_buffer_segment->size; offset = coded_buffer_segment->bit_offset; buf = coded_buffer_segment->buf; /* retrieve encoded data*/ // ... /* unmap and destroy buffer */ vaUnmapBuffer(va_display, buf_id); vaDestroyBuffer(va_display, buf_id);
Note that the vaMapBuffer function returns pointers to different objects depending on the mapped buffer type. The VAImage is a plain data buffer and the encoded bitstream is a VACodedBufferSegment structure. The application cannot use VABuffer for synchronization. If encoding, it is recommended to synchronize using the VA surface as described above.