### Configure Encoding/Decoding Threads in C Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code demonstrates how to configure the number of threads used by libvmx for encoding and decoding operations. It shows how to check the default thread count, set a specific number of threads (e.g., 8 for 4K encoding), and verify the change. The example includes encoding a 4K frame to illustrate the multi-threading capability. ```c VMX_SIZE dimensions = { 3840, 2160 }; // 4K resolution VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // Check default thread count int defaultThreads = VMX_GetThreads(encoder); printf("Default threads: %d\n", defaultThreads); // Set to use 8 threads for 4K encoding VMX_SetThreads(encoder, 8); // Verify setting int currentThreads = VMX_GetThreads(encoder); printf("Now using %d threads\n", currentThreads); // Encode 4K frame with multi-threading BYTE* frame4K = (BYTE*)malloc(dimensions.width * dimensions.height * 4); // ... populate frame ... VMX_EncodeBGRX(encoder, frame4K, dimensions.width * 4, 0); BYTE* output = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int outputSize = VMX_SaveTo(encoder, output, dimensions.width * dimensions.height * 4); printf("4K frame encoded to %d bytes using %d threads\n", outputSize, currentThreads); free(frame4K); free(output); VMX_Destroy(encoder); ``` -------------------------------- ### Set Encoding Quality and Bitrate Management in C Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code demonstrates how to set the encoding quality (0-100) and configure automatic bitrate targeting for VMX_INSTANCE. It defines minimum and maximum target bitrates per frame and a minimum quality level. The example encodes multiple frames, showing how the quality adjusts automatically based on the output size. It also shows how to retrieve the current encoding parameters. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_SQ, VMX_COLORSPACE_BT709); // Set initial quality (0-100, where 100 is highest quality) VMX_SetQuality(encoder, 85); // Configure bitrate targeting // Target 5-8 MB per frame for high quality int targetMin = 5 * 1024 * 1024; // 5 MB minimum int targetMax = 8 * 1024 * 1024; // 8 MB maximum int minQuality = 50; // Don't drop below quality 50 int dcShift = 0; // 11-bit DC precision VMX_SetEncodingParameters(encoder, targetMin, targetMax, minQuality, dcShift); // Encode multiple frames - quality adjusts automatically for (int frame = 0; frame < 100; frame++) { BYTE* frameData = /* ... get next frame ... */ NULL; int stride = dimensions.width * 4; VMX_EncodeBGRX(encoder, frameData, stride, 0); BYTE* compressed = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int compressedSize = VMX_SaveTo(encoder, compressed, dimensions.width * dimensions.height * 4); // Quality automatically adjusted based on output size int currentQuality = VMX_GetQuality(encoder); printf("Frame %d: %d bytes, quality: %d\n", frame, compressedSize, currentQuality); free(compressed); } // Query current parameters int frameMin, frameMax, minQ, dcS; VMX_GetEncodingParameters(encoder, &frameMin, &frameMax, &minQ, &dcS); printf("Current settings: min=%d max=%d minQ=%d dcShift=%d\n", frameMin, frameMax, minQ, dcS); VMX_Destroy(encoder); ``` -------------------------------- ### Convert BGRX to UYVY Color Format in C Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code provides a helper function, VMX_BGRXToUYVY, for converting image data from BGRX to UYVY color formats. It emphasizes the importance of allocating 64-byte aligned buffers using _mm_malloc for optimal SIMD performance. The example shows the function call with source and destination buffers, strides, and dimensions. ```c VMX_SIZE dimensions = { 1920, 1080 }; // Allocate 64-byte aligned buffers for SIMD operations BYTE* bgrxSource = (BYTE*)_mm_malloc(dimensions.width * dimensions.height * 4, 64); BYTE* uyvyDest = (BYTE*)_mm_malloc(dimensions.width * dimensions.height * 2, 64); // Fill BGRX source with image data // ... populate bgrxSource ... int bgrxStride = dimensions.width * 4; int uyvyStride = dimensions.width * 2; // Convert BGRX to UYVY using optimized conversion VMX_BGRXToUYVY(bgrxSource, bgrxStride, uyvyDest, uyvyStride, dimensions); // uyvyDest now contains converted UYVY data // ... use uyvyDest for encoding or output ... _mm_free(bgrxSource); _mm_free(uyvyDest); ``` -------------------------------- ### Initialize and Destroy VMX Codec Instance (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code snippet demonstrates how to create a VMX codec instance with specified dimensions, quality profile, and color space. It also shows how to configure the number of threads for optimal performance and the essential step of destroying the instance to free resources. Ensure 'vmxcodec.h' is included. ```c #include "vmxcodec.h" // Create a 1920x1080 progressive codec instance VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create( dimensions, VMX_PROFILE_HQ, // High quality profile VMX_COLORSPACE_BT709 // HD color space ); if (encoder == NULL) { // Handle creation failure return -1; } // Configure threading for optimal performance VMX_SetThreads(encoder, 4); // When finished, destroy the instance VMX_Destroy(encoder); ``` -------------------------------- ### Generate Low-Resolution Preview in C Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code snippet illustrates how to generate a low-resolution preview (1/8th resolution) from compressed video data. It uses VMX_LoadFrom to load the compressed frame and VMX_DecodePreviewBGRX for a faster partial decode suitable for thumbnails. The code calculates the necessary preview buffer size and dimensions, decodes the preview, and handles potential errors. ```c BYTE* compressedFrame = /* ... compressed data ... */; int compressedSize = /* ... */; VMX_SIZE fullSize = { 1920, 1080 }; VMX_INSTANCE* decoder = VMX_Create(fullSize, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // Load compressed frame VMX_LoadFrom(decoder, compressedFrame, compressedSize); // Get preview length needed for partial decode int previewDataLength = VMX_GetEncodedPreviewLength(decoder); printf("Preview needs only first %d bytes of %d\n", previewDataLength, compressedSize); // Calculate preview dimensions (1/8 resolution) // 1920x1080 becomes 240x135 for progressive int previewWidth = (fullSize.width / 8) & ~1; // Ensure divisible by 2 int previewHeight = fullSize.height / 8; // Allocate preview buffer int previewStride = previewWidth * 4; BYTE* previewBuffer = (BYTE*)malloc(previewWidth * previewHeight * 4); // Decode preview (much faster than full decode) VMX_ERR result = VMX_DecodePreviewBGRX(decoder, previewBuffer, previewStride); if (result == VMX_ERR_OK) { // Use preview for thumbnail or quick display printf("Preview decoded: %dx%d\n", previewWidth, previewHeight); // ... display or save preview ... } free(previewBuffer); VMX_Destroy(decoder); ``` -------------------------------- ### Interlaced Video Encoding (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code snippet illustrates how to encode interlaced video frames, often used in broadcast systems. It shows the creation of a `VMX_INSTANCE` for encoding, population of an interlaced frame buffer, and the call to `VMX_EncodeBGRX` with the interlaced flag set to 1. The process includes saving the compressed output and managing allocated memory. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT601); // Interlaced frame (1080i) int stride = dimensions.width * 4; BYTE* interlacedFrame = (BYTE*)malloc(dimensions.width * dimensions.height * 4); // ... populate interlacedFrame with field-based data ... // Encode as interlaced (pass 1 for interlaced parameter) VMX_ERR result = VMX_EncodeBGRX(encoder, interlacedFrame, stride, 1); if (result == VMX_ERR_OK) { BYTE* output = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int size = VMX_SaveTo(encoder, output, dimensions.width * dimensions.height * 4); printf("Interlaced frame compressed: %d bytes\n", size); free(output); } free(interlacedFrame); VMX_Destroy(encoder); ``` -------------------------------- ### PSNR Quality Assessment (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code calculates the Peak Signal-to-Noise Ratio (PSNR) to assess video compression quality. It demonstrates creating encoder and decoder instances, encoding an original BGRX frame, saving it, loading it into the decoder, decoding it, and then calculating the PSNR between the original and decoded frames using `VMX_CalculatePSNR`. Memory management with `malloc` and `free`, along with instance destruction using `VMX_Destroy`, is included. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_SQ, VMX_COLORSPACE_BT709); VMX_INSTANCE* decoder = VMX_Create(dimensions, VMX_PROFILE_SQ, VMX_COLORSPACE_BT709); // Original frame int stride = dimensions.width * 4; BYTE* originalFrame = (BYTE*)malloc(dimensions.width * dimensions.height * 4); // ... populate with original image ... // Encode VMX_EncodeBGRX(encoder, originalFrame, stride, 0); BYTE* compressed = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int compressedSize = VMX_SaveTo(encoder, compressed, dimensions.width * dimensions.height * 4); // Decode VMX_LoadFrom(decoder, compressed, compressedSize); BYTE* decodedFrame = (BYTE*)malloc(dimensions.width * dimensions.height * 4); VMX_DecodeBGRX(decoder, decodedFrame, stride); // Calculate PSNR to measure quality loss float psnr = VMX_CalculatePSNR( originalFrame, decodedFrame, stride, 4, // 4 bytes per pixel (BGRX) dimensions ); printf("Compression PSNR: %.2f dB\n", psnr); printf("Compressed size: %d bytes (%.2f%% of original)\n", compressedSize, (compressedSize * 100.0) / (dimensions.width * dimensions.height * 4)); // PSNR interpretation: // > 40 dB: Excellent quality // 30-40 dB: Good quality // 20-30 dB: Acceptable quality // < 20 dB: Poor quality free(originalFrame); free(compressed); free(decodedFrame); VMX_Destroy(encoder); VMX_Destroy(decoder); ``` -------------------------------- ### Encode 10-bit PA16 with Alpha using VMX Source: https://context7.com/openmediatransport/libvmx/llms.txt Encodes a 10-bit 4:2:2:4 PA16 format frame with a 10-bit alpha channel for high-quality transparency. This function requires the VMX_Create, VMX_EncodePA16, and VMX_SaveTo functions from the libvmx library. It takes frame data as input and outputs compressed data. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // PA16: 16-bit Y + 16-bit UV + 16-bit Alpha int stride = dimensions.width * 2; int ySize = dimensions.width * dimensions.height * 2; int uvSize = dimensions.width * dimensions.height * 2; int alphaSize = dimensions.width * dimensions.height * 2; int totalSize = ySize + uvSize + alphaSize; BYTE* pa16Frame = (BYTE*)malloc(totalSize); unsigned short* y = (unsigned short*)pa16Frame; unsigned short* uv = (unsigned short*)(pa16Frame + ySize); unsigned short* alpha = (unsigned short*)(pa16Frame + ySize + uvSize); // Populate with 10-bit data (shifted left 6 bits) // ... fill y, uv, alpha planes ... VMX_ERR result = VMX_EncodePA16(encoder, pa16Frame, stride, 0); if (result == VMX_ERR_OK) { BYTE* output = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int outputSize = VMX_SaveTo(encoder, output, dimensions.width * dimensions.height * 4); printf("10-bit PA16 with alpha: %d bytes\n", outputSize); free(output); } free(pa16Frame); VMX_Destroy(encoder); ``` -------------------------------- ### Conditional Frame Conversion (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code snippet demonstrates how to convert BGRX frames to UYVY only when pixel data has changed. It utilizes `_mm_malloc` for aligned memory allocation and includes a conditional conversion function `VMX_BGRXToUYVYConditional`. The snippet shows buffer allocation, population, conditional conversion, and buffer deallocation. ```c VMX_SIZE dimensions = { 1920, 1080 }; // Allocate aligned buffers BYTE* currentFrame = (BYTE*)_mm_malloc(dimensions.width * dimensions.height * 4, 64); BYTE* previousFrame = (BYTE*)_mm_malloc(dimensions.width * dimensions.height * 4, 64); BYTE* uyvyOutput = (BYTE*)_mm_malloc(dimensions.width * dimensions.height * 2, 64); int stride = dimensions.width * 4; int uyvyStride = dimensions.width * 2; // Fill previous frame // ... populate previousFrame ... // Main processing loop for (int frame = 0; frame < 1000; frame++) { // Get current frame data // ... populate currentFrame ... // Convert only if changed int changed = VMX_BGRXToUYVYConditional( currentFrame, previousFrame, stride, uyvyOutput, uyvyStride, dimensions ); if (changed) { printf("Frame %d: Pixels changed, converted to UYVY\n", frame); // ... encode or process uyvyOutput ... } else { printf("Frame %d: No change, skipping conversion\n", frame); // Skip encoding for static frame } // Swap frame buffers BYTE* temp = previousFrame; previousFrame = currentFrame; currentFrame = temp; } _mm_free(currentFrame); _mm_free(previousFrame); _mm_free(uyvyOutput); ``` -------------------------------- ### Encode UYVY Format (4:2:2) using libvmx Source: https://context7.com/openmediatransport/libvmx/llms.txt Encodes a video frame in UYVY 4:2:2 format, commonly used in video capture. This function requires a VMX_INSTANCE created with appropriate dimensions and profile. It takes raw UYVY frame data and its stride as input, producing compressed output. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // UYVY uses 2 bytes per pixel (4:2:2 subsampling) int stride = dimensions.width * 2; BYTE* uyvyFrame = (BYTE*)malloc(dimensions.width * dimensions.height * 2); // Fill with UYVY data from capture device // ... populate uyvyFrame ... // Encode the frame VMX_ERR result = VMX_EncodeUYVY(encoder, uyvyFrame, stride, 0); if (result == VMX_ERR_OK) { BYTE* compressed = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int size = VMX_SaveTo(encoder, compressed, dimensions.width * dimensions.height * 4); printf("UYVY frame compressed to %d bytes\n", size); free(compressed); } free(uyvyFrame); VMX_Destroy(encoder); ``` -------------------------------- ### C: VMX Codec Encoding Error Handling Source: https://context7.com/openmediatransport/libvmx/llms.txt Demonstrates how to handle potential errors during the VMX_EncodeBGRX function. It checks for specific VMX_ERR return codes to identify issues like invalid instances, parameters, buffer overflows, or codec format problems. Ensures proper cleanup of allocated resources. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); if (encoder == NULL) { fprintf(stderr, "Failed to create encoder instance\n"); return -1; } BYTE* sourceFrame = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int stride = dimensions.width * 4; // Encode frame VMX_ERR encodeResult = VMX_EncodeBGRX(encoder, sourceFrame, stride, 0); switch (encodeResult) { case VMX_ERR_OK: printf("Encoding successful\n"); break; case VMX_ERR_INVALID_INSTANCE: fprintf(stderr, "Invalid encoder instance\n"); break; case VMX_ERR_INVALID_PARAMETERS: fprintf(stderr, "Invalid encoding parameters\n"); break; case VMX_ERR_BUFFER_OVERFLOW: fprintf(stderr, "Buffer overflow during encoding\n"); break; case VMX_ERR_INVALID_CODEC_FORMAT: fprintf(stderr, "Invalid codec format\n"); break; case VMX_ERR_INVALID_SLICE_COUNT: fprintf(stderr, "Invalid slice count\n"); break; default: fprintf(stderr, "Unknown error: %d\n", encodeResult); break; } // Always clean up free(sourceFrame); VMX_Destroy(encoder); ``` -------------------------------- ### Encode UYVA Format with Alpha Channel using libvmx Source: https://context7.com/openmediatransport/libvmx/llms.txt Encodes video frames in UYVA format, which includes a separate alpha channel for transparency. The function requires a VMX_INSTANCE and the frame data, which consists of UYVY data followed by an alpha plane. The stride for the UYVY portion is passed to the encoding function. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // UYVA layout: UYVY data followed by alpha plane (half stride) // Total size = width * height * 2 + width * height / 2 int uyvyStride = dimensions.width * 2; int totalSize = dimensions.width * dimensions.height * 2 + (dimensions.width * dimensions.height / 2); BYTE* uyvaFrame = (BYTE*)malloc(totalSize); // Fill UYVY portion BYTE* uyvy = uyvaFrame; // Fill alpha plane (starts after UYVY data) BYTE* alpha = uyvaFrame + (dimensions.width * dimensions.height * 2); // ... populate uyvy and alpha data ... // Encode with alpha VMX_ERR result = VMX_EncodeUYVA(encoder, uyvaFrame, uyvyStride, 0); if (result == VMX_ERR_OK) { BYTE* output = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int outputSize = VMX_SaveTo(encoder, output, dimensions.width * dimensions.height * 4); printf("UYVA encoded to %d bytes\n", outputSize); free(output); } free(uyvaFrame); VMX_Destroy(encoder); ``` -------------------------------- ### Decode to UYVY Format using libvmx Source: https://context7.com/openmediatransport/libvmx/llms.txt Decodes a compressed VMX frame back into UYVY 4:2:2 format. This function is useful for outputting video to hardware or for further processing. It requires the compressed data, its size, and dimensions to initialize the decoder. ```c BYTE* compressedData = /* ... */; int compressedSize = /* ... */; VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* decoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // Load compressed frame VMX_ERR result = VMX_LoadFrom(decoder, compressedData, compressedSize); if (result != VMX_ERR_OK) { VMX_Destroy(decoder); return -1; } // Decode to UYVY (2 bytes per pixel) int stride = dimensions.width * 2; BYTE* uyvyOutput = (BYTE*)malloc(dimensions.width * dimensions.height * 2); result = VMX_DecodeUYVY(decoder, uyvyOutput, stride); if (result == VMX_ERR_OK) { // Send to video output device // ... use uyvyOutput ... } free(uyvyOutput); VMX_Destroy(decoder); ``` -------------------------------- ### Decode Compressed VMX Frame to BGRA (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code demonstrates how to decode a VMX compressed frame back into BGRA format, preserving the alpha channel. It involves creating a decoder instance, loading the compressed data, allocating a buffer for the decoded frame, and performing the decoding operation. Remember to destroy the decoder instance and free allocated memory. ```c // Assume we have compressed data from encoding BYTE* compressedFrame = /* ... compressed data ... */; int compressedLength = /* ... size in bytes ... */; // Create decoder instance VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* decoder = VMX_Create( dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709 ); // Load compressed frame VMX_ERR loadResult = VMX_LoadFrom(decoder, compressedFrame, compressedLength); if (loadResult != VMX_ERR_OK) { VMX_Destroy(decoder); return -1; } // Allocate destination buffer for decoded frame int stride = dimensions.width * 4; BYTE* decodedFrame = (BYTE*)malloc(dimensions.width * dimensions.height * 4); // Decode to BGRA format VMX_ERR decodeResult = VMX_DecodeBGRA(decoder, decodedFrame, stride); if (decodeResult != VMX_ERR_OK) { free(decodedFrame); VMX_Destroy(decoder); return -1; } // Use decoded frame // ... process decodedFrame ... free(decodedFrame); VMX_Destroy(decoder); ``` -------------------------------- ### Encode BGRX Frame (No Alpha) to VMX (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code snippet illustrates encoding a BGRX (RGB32) image frame, which does not utilize the alpha channel, for more efficient compression when transparency is not required. It covers creating the encoder, preparing the BGRX frame buffer, performing the encoding, and saving the compressed output. Proper memory management for allocated buffers is essential. ```c // Create encoder for 1920x1080 VMX_SIZE size = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(size, VMX_PROFILE_SQ, VMX_COLORSPACE_BT709); // Prepare BGRX frame (4 bytes per pixel, alpha ignored) int stride = size.width * 4; BYTE* bgrxFrame = (BYTE*)malloc(size.width * size.height * 4); // Fill with image data // ... populate bgrxFrame ... // Encode progressive frame VMX_ERR result = VMX_EncodeBGRX(encoder, bgrxFrame, stride, 0); if (result == VMX_ERR_OK) { // Save to buffer BYTE* output = (BYTE*)malloc(size.width * size.height * 4); int outputSize = VMX_SaveTo(encoder, output, size.width * size.height * 4); printf("Compressed size: %d bytes\n", outputSize); free(output); } free(bgrxFrame); VMX_Destroy(encoder); ``` -------------------------------- ### Encode BGRA Frame to VMX Compressed Data (C) Source: https://context7.com/openmediatransport/libvmx/llms.txt This C code shows how to encode a BGRA image frame (including alpha channel) into VMX compressed format. It covers allocating the source frame buffer, filling it with image data, performing the encoding, and saving the compressed data to a buffer. Error handling for encoding and saving is included. The source frame and compressed data buffers must be freed after use. ```c // Allocate source BGRA frame buffer (4 bytes per pixel) int width = 1920; int height = 1080; int stride = width * 4; BYTE* bgraFrame = (BYTE*)malloc(width * height * 4); // Fill bgraFrame with your image data (BGRA format) // ... populate frame data ... // Encode the frame VMX_ERR result = VMX_EncodeBGRA(encoder, bgraFrame, stride, 0); if (result != VMX_ERR_OK) { // Handle encoding error free(bgraFrame); return -1; } // Save compressed frame to buffer int maxCompressedSize = width * height * 4; BYTE* compressedData = (BYTE*)malloc(maxCompressedSize); int compressedSize = VMX_SaveTo(encoder, compressedData, maxCompressedSize); if (compressedSize <= 0) { // Handle save error free(bgraFrame); free(compressedData); return -1; } printf("Encoded frame size: %d bytes\n", compressedSize); free(bgraFrame); free(compressedData); ``` -------------------------------- ### Encode 10-bit P216 Format using libvmx Source: https://context7.com/openmediatransport/libvmx/llms.txt Encodes video frames in 10-bit 4:2:2 P216 format, suitable for high color depth applications. P216 uses 16-bit values where the 10 most significant bits contain the actual data. The function requires a VMX_INSTANCE and the frame data, which includes separate Y and interleaved UV planes. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_HQ, VMX_COLORSPACE_BT709); // P216: 16-bit Y plane + 16-bit interleaved UV plane // Stride for 16-bit data is width * 2 (bytes) int stride = dimensions.width * 2; int yPlaneSize = dimensions.width * dimensions.height * 2; int uvPlaneSize = dimensions.width * dimensions.height * 2; // 4:2:2 UV int totalSize = yPlaneSize + uvPlaneSize; BYTE* p216Frame = (BYTE*)malloc(totalSize); unsigned short* yPlane = (unsigned short*)p216Frame; unsigned short* uvPlane = (unsigned short*)(p216Frame + yPlaneSize); // Convert 10-bit data to 16-bit by shifting left 6 bits for (int i = 0; i < dimensions.width * dimensions.height; i++) { unsigned short value10bit = /* ... your 10-bit Y value ... */ 0; yPlane[i] = value10bit << 6; } // ... populate UV plane similarly ... // Encode 10-bit frame VMX_ERR result = VMX_EncodeP216(encoder, p216Frame, stride, 0); if (result == VMX_ERR_OK) { BYTE* compressed = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int size = VMX_SaveTo(encoder, compressed, dimensions.width * dimensions.height * 4); printf("10-bit P216 compressed to %d bytes\n", size); free(compressed); } free(p216Frame); VMX_Destroy(encoder); ``` -------------------------------- ### Encode YV12 Planar Format using VMX Source: https://context7.com/openmediatransport/libvmx/llms.txt Encodes video frames in YV12 (or I420) format, which uses three separate planes for the Y, U, and V color components. This function relies on VMX_Create, VMX_EncodeYV12, and VMX_SaveTo from the libvmx library. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_SQ, VMX_COLORSPACE_BT709); // YV12: Full Y plane + quarter resolution U and V planes int yStride = dimensions.width; int ySize = dimensions.width * dimensions.height; BYTE* yPlane = (BYTE*)malloc(ySize); int uvStride = dimensions.width / 2; int uvSize = (dimensions.width / 2) * (dimensions.height / 2); BYTE* uPlane = (BYTE*)malloc(uvSize); BYTE* vPlane = (BYTE*)malloc(uvSize); // Populate planes // ... fill yPlane, uPlane, vPlane ... // Encode YV12 VMX_ERR result = VMX_EncodeYV12( encoder, yPlane, yStride, uPlane, uvStride, vPlane, uvStride, 0 // progressive ); if (result == VMX_ERR_OK) { BYTE* output = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int outputSize = VMX_SaveTo(encoder, output, dimensions.width * dimensions.height * 4); printf("YV12 compressed to %d bytes\n", outputSize); free(output); } free(yPlane); free(uPlane); free(vPlane); VMX_Destroy(encoder); ``` -------------------------------- ### Encode NV12 Planar Format using VMX Source: https://context7.com/openmediatransport/libvmx/llms.txt Encodes video frames in NV12 format, characterized by a separate Y plane and an interleaved UV plane. This is a common format for modern video codecs and hardware encoders. The function utilizes VMX_Create, VMX_EncodeNV12, and VMX_SaveTo from the libvmx library. ```c VMX_SIZE dimensions = { 1920, 1080 }; VMX_INSTANCE* encoder = VMX_Create(dimensions, VMX_PROFILE_SQ, VMX_COLORSPACE_BT709); // NV12: Full resolution Y plane + half resolution interleaved UV plane int yStride = dimensions.width; int ySize = dimensions.width * dimensions.height; BYTE* yPlane = (BYTE*)malloc(ySize); int uvStride = dimensions.width; // UV pairs, so width is maintained int uvSize = dimensions.width * (dimensions.height / 2); BYTE* uvPlane = (BYTE*)malloc(uvSize); // Fill Y plane // ... populate yPlane ... // Fill UV plane (interleaved U and V) // ... populate uvPlane ... // Encode NV12 frame VMX_ERR result = VMX_EncodeNV12( encoder, yPlane, yStride, uvPlane, uvStride, 0 // progressive ); if (result == VMX_ERR_OK) { BYTE* compressed = (BYTE*)malloc(dimensions.width * dimensions.height * 4); int size = VMX_SaveTo(encoder, compressed, dimensions.width * dimensions.height * 4); printf("NV12 compressed to %d bytes\n", size); free(compressed); } free(yPlane); free(uvPlane); VMX_Destroy(encoder); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.