1## General Information 2 3### Scope of this document 4 5This document is aimed to provide information about the v4l2\_codec2 project. 6The target readers of this document are the developers and following maintainers 7of this project, and the partners who are willing to use the V4L2 components. 8 9### Introduction 10 11v4l2\_codec2 project provides a component implementation of Codec2 framework, 12the next-generation codec framework. The component implementation delegates the 13request to the driver via the V4L2 API. 14 15## Quick Start Guide 16 17### Prerequisites 18 19* Gralloc support for graphic buffer allocation 20* ION or Gralloc support for linear buffer allocation 21* Kernel v5.3+ or [this patch][1] backported for buffer identification 22* [V4L2 stateful decoding API](https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-decoder.html) 23* [V4L2 encoding API](https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-encoder.html) 24* Widevine DRM for secure playback 25 26### Enable V4L2 Components 27 28Install the build package and files, and set the parameters in device.mk 29 30```makefile 31# Add the folder to the namespace. 32PRODUCT_SOONG_NAMESPACES += external/v4l2_codec2 33 34# Add the build target. 35PRODUCT_PACKAGES += \ 36 [email protected] \ 37 libc2plugin_store 38 39# If a customized allocator is needed, then add this package. 40# See more detail at "Customized allocator" section. 41PRODUCT_PACKAGES += \ 42 libv4l2_codec2_vendor_allocator 43 44# Install media_codecs_c2.xml. 45# The destination is: /vendor/etc/media_codecs_c2.xml 46PRODUCT_COPY_FILES += \ 47 <path_to_file>:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs_c2.xml 48 49# Set the customized property of v4l2_codec2, including: 50# - The maximum concurrent instances for decoder/encoder. 51# It should be the same as "concurrent-instances" at media_codec_c2.xml. 52PRODUCT_PROPERTY_OVERRIDES += \ 53 ro.vendor.v4l2_codec2.decode_concurrent_instances=8 \ 54 ro.vendor.v4l2_codec2.encode_concurrent_instances=8 55 56# Codec2.0 poolMask: 57# ION(16) 58# BUFFERQUEUE(18) 59# BLOB(19) 60# V4L2_BUFFERQUEUE(20) 61# V4L2_BUFFERPOOL(21) 62# SECURE_LINEAR(22) 63# SECURE_GRAPHIC(23) 64# 65# For linear buffer allocation: 66# If ION is chosen, then the mask should be 0xf50000 67# If BLOB is chosen, then the mask should be 0xfc0000 68PRODUCT_PROPERTY_OVERRIDES += \ 69 debug.stagefright.c2-poolmask=0xf50000 70 71# Install extended policy for codec2. 72# The destination is: /vendor/etc/seccomp_policy/codec2.vendor.ext.policy 73PRODUCT_COPY_FILES += \ 74 <path_to_policy>:$(TARGET_COPY_OUT_VENDOR)/etc/seccomp_policy/codec2.vendor.ext.policy 75``` 76 77Add decode and encode components in media\_codecs\_c2.xml 78 79```xml 80<?xml version="1.0" encoding="utf-8" ?> 81<MediaCodecs> 82 <Encoders> 83 <MediaCodec name="c2.v4l2.avc.encoder" type="video/avc"> 84 <Limit name="size" min="32x32" max="1920x1088" /> 85 <Limit name="alignment" value="2x2" /> 86 <Limit name="block-size" value="16x16" /> 87 <Limit name="blocks-per-second" range="1-244800" /> 88 <Limit name="bitrate" range="1-12000000" /> 89 <Limit name="concurrent-instances" max="8" /> 90 <Limit name="performance-point-1280x720" range="30-30" /> 91 </MediaCodec> 92 93 <MediaCodec name="c2.v4l2.vp8.encoder" type="video/x-vnd.on2.vp8"> 94 <Limit name="size" min="32x32" max="1920x1088" /> 95 <Limit name="alignment" value="2x2" /> 96 <Limit name="block-size" value="16x16" /> 97 <Limit name="blocks-per-second" range="1-244800" /> 98 <Limit name="bitrate" range="1-12000000" /> 99 <Limit name="concurrent-instances" max="8" /> 100 <Limit name="performance-point-1280x720" range="30-30" /> 101 </MediaCodec> 102 103 <MediaCodec name="c2.v4l2.vp9.encoder" type="video/x-vnd.on2.vp9"> 104 <Limit name="size" min="32x32" max="1920x1088" /> 105 <Limit name="alignment" value="2x2" /> 106 <Limit name="block-size" value="16x16" /> 107 <Limit name="blocks-per-second" range="1-244800" /> 108 <Limit name="bitrate" range="1-12000000" /> 109 <Limit name="concurrent-instances" max="8" /> 110 <Limit name="performance-point-1280x720" range="30-30" /> 111 </MediaCodec> 112 </Encoders> 113 114 <Decoders> 115 <MediaCodec name="c2.v4l2.avc.decoder" type="video/avc" > 116 <Limit name="size" min="16x16" max="4096x4096" /> 117 <Limit name="alignment" value="2x2" /> 118 <Limit name="block-size" value="16x16" /> 119 <Limit name="blocks-per-second" min="1" max="1879200" /> 120 <Limit name="bitrate" range="1-62500000" /> 121 <Limit name="concurrent-instances" max="8" /> 122 <Limit name="performance-point-3840x2160" range="30-30" /> 123 <Feature name="adaptive-playback" /> 124 </MediaCodec> 125 126 <MediaCodec name="c2.v4l2.vp8.decoder" type="video/x-vnd.on2.vp8" > 127 <Limit name="size" min="16x16" max="4096x4096" /> 128 <Limit name="alignment" value="2x2" /> 129 <Limit name="block-size" value="16x16" /> 130 <Limit name="blocks-per-second" min="1" max="1984500" /> 131 <Limit name="bitrate" range="1-62500000" /> 132 <Limit name="concurrent-instances" max="8" /> 133 <Limit name="performance-point-3840x2160" range="30-30" /> 134 <Feature name="adaptive-playback" /> 135 </MediaCodec> 136 137 <MediaCodec name="c2.v4l2.vp9.decoder" type="video/x-vnd.on2.vp9" > 138 <Limit name="size" min="16x16" max="4096x4096" /> 139 <Limit name="alignment" value="2x2" /> 140 <Limit name="block-size" value="16x16" /> 141 <Limit name="blocks-per-second" min="1" max="2073600" /> 142 <Limit name="bitrate" range="1-62500000" /> 143 <Limit name="concurrent-instances" max="8" /> 144 <Limit name="performance-point-3840x2160" range="30-30" /> 145 <Feature name="adaptive-playback" /> 146 </MediaCodec> 147 148 <MediaCodec name="c2.v4l2.avc.decoder.secure" type="video/avc" > 149 <Limit name="size" min="16x16" max="4096x4096" /> 150 <Limit name="alignment" value="2x2" /> 151 <Limit name="block-size" value="16x16" /> 152 <Limit name="blocks-per-second" min="1" max="1879200" /> 153 <Limit name="bitrate" range="1-62500000" /> 154 <Limit name="concurrent-instances" max="8" /> 155 <Limit name="performance-point-3840x2160" range="30-30" /> 156 <Feature name="adaptive-playback" /> 157 <Feature name="secure-playback" required="true" /> 158 </MediaCodec> 159 160 <MediaCodec name="c2.v4l2.vp8.decoder.secure" type="video/x-vnd.on2.vp8" > 161 <Limit name="size" min="16x16" max="4096x4096" /> 162 <Limit name="alignment" value="2x2" /> 163 <Limit name="block-size" value="16x16" /> 164 <Limit name="blocks-per-second" min="1" max="1984500" /> 165 <Limit name="bitrate" range="1-62500000" /> 166 <Limit name="concurrent-instances" max="8" /> 167 <Limit name="performance-point-3840x2160" range="30-30" /> 168 <Feature name="adaptive-playback" /> 169 <Feature name="secure-playback" required="true" /> 170 </MediaCodec> 171 172 <MediaCodec name="c2.v4l2.vp9.decoder.secure" type="video/x-vnd.on2.vp9" > 173 <Limit name="size" min="16x16" max="4096x4096" /> 174 <Limit name="alignment" value="2x2" /> 175 <Limit name="block-size" value="16x16" /> 176 <Limit name="blocks-per-second" min="1" max="2073600" /> 177 <Limit name="bitrate" range="1-62500000" /> 178 <Limit name="concurrent-instances" max="8" /> 179 <Limit name="performance-point-3840x2160" range="30-30" /> 180 <Feature name="adaptive-playback" /> 181 <Feature name="secure-playback" required="true" /> 182 </MediaCodec> 183 </Decoders> 184</MediaCodecs> 185``` 186 187Set SELinux file policy in sepolicy/file\_contexts 188 189``` 190/vendor/bin/hw/android\.hardware\.media\.c2@1\.0-service-v4l2(.*)? u:object_r:mediacodec_exec:s0 191``` 192 193Add additional permission in codec2.vendor.ext.policy 194 195``` 196_llseek: 1 197epoll_create1: 1 198epoll_ctl: 1 199epoll_pwait: 1 200eventfd2: 1 201fstat64: 1 202fstatat64: 1 203fstatfs64: 1 204getcwd: 1 205getdents64: 1 206getuid32: 1 207mmap2: 1 208pselect6: 1 209statfs64: 1 210sysinfo: 1 211ugetrlimit: 1 212``` 213 214Set file permission in ueventd.rc 215 216``` 217/dev/video* 0600 media media 218``` 219 220### Customized Allocator 221 222The default allocator of the decoder's output buffer is C2AllocatorGralloc. 223However, C2AllocatorGralloc might not fit the requirement for secure playback. 224In this case, we can implement a customized C2Allocator, and load it at 225run-time. Please create a library `libv4l2_codec2_vendor_allocator`, and export 226a function `CreateVendorAllocator() `for creating the customized C2Allocator. 227Here is an example below. 228 229#### Example of Android.bp 230 231```json 232cc_library_shared { 233 name: "libv4l2_codec2_vendor_allocator", 234 vendor: true, 235 236 defaults: [ 237 "libcodec2-impl-defaults", 238 ], 239 240 srcs: [ 241 "C2VendorAllocatorFactory.cpp", 242 ], 243 244 shared_libs: [ 245 "libc2plugin_store", 246 ], 247} 248``` 249 250#### Example of C2VendorAllocatorFactory.cpp 251 252```cpp 253//#define LOG_NDEBUG 0 254#define LOG_TAG "C2VendorAllocatorFactory" 255 256#include <C2AllocatorGralloc.h> 257#include <utils/Log.h> 258#include <v4l2_codec2/plugin_store/V4L2AllocatorId.h> 259 260namespace android { 261 262::C2Allocator* CreateVendorAllocator(::C2Allocator::id_t allocatorId) { 263 ALOGV("%s(%d)", __func__, allocatorId); 264 265 // Change to create vendor-customized implementation. 266 switch (allocatorId) { 267 case V4L2AllocatorId::V4L2_BUFFERQUEUE: 268 return new C2AllocatorGralloc(V4L2AllocatorId::V4L2_BUFFERQUEUE, true); 269 case V4L2AllocatorId::V4L2_BUFFERPOOL: 270 return new C2AllocatorGralloc(V4L2AllocatorId::V4L2_BUFFERPOOL, true); 271 case V4L2AllocatorId::SECURE_LINEAR: 272 return new C2AllocatorGralloc(V4L2AllocatorId::SECURE_LINEAR, true); 273 case V4L2AllocatorId::SECURE_GRAPHIC: 274 return new C2AllocatorGralloc(V4L2AllocatorId::SECURE_GRAPHIC, true); 275 default: 276 ALOGE("%s(): Unknown allocator ID: %d", __func__, allocatorId); 277 } 278 return nullptr; 279} 280 281} // namespace android 282 283extern "C" ::C2Allocator* CreateAllocator(::C2Allocator::id_t allocatorId) { 284 return ::android::CreateVendorAllocator(allocatorId); 285} 286``` 287 288## V4L2 Encoder 289 290### Supported Codecs 291 292Currently the V4L2 encoder has support for the H.264, VP8 and VP9 codecs. Codec 293selection can be done by selecting the encoder with the appropriate name. 294 295- H26: *c2.v4l2.avc.encoder* 296- VP8: *c2.v4l2.vp8.encoder* 297- VP9: *c2.v4l2.vp9.encoder* 298 299### Supported Parameters: 300 301The following parameters are static and can not be changed at run-time: 302 303- *C2_PARAMKEY_PICTURE_SIZE*: This parameter can be used to configure the 304resolution of the encoded video stream. 305- *C2_PARAMKEY_PROFILE_LEVEL*: This parameter can be used to adjust the desired 306profile level. When using the H.264 codec the profile level might be adjusted to 307conform to the minimum requirements for the specified bitrate and framerate. 308- *C2_PARAMKEY_SYNC_FRAME_INTERVAL*: This parameter can be used to configure the 309desired time between subsequent key frames in microseconds. 310- *C2_PARAMKEY_BITRATE_MODE*: This parameter can be used to switch between 311constant (*C2Config::BITRATE_CONST*) and variable (*C2Config::BITRATE_VARIABLE*) 312bitrate modes. When using CBR the encoder will try to maintain a constant 313bitrate. This mode is preferable for video conferencing where maintaining a 314stable bitrate is more important than quality. When using VBR the encoder will 315be allowed to dynamically adjust the bitrate to maintain a constant quality. As 316the mediacodec framework does not provide facilities to configure the peak 317bitrate when using VBR, it is currently always set to 2x the target bitrate. 318 319The following parameters are dynamic, and can be freely adjusted at run-time: 320 321- *C2_PARAMKEY_BITRATE*: Use this parameter to specify the desired bitrate. 322- *C2_PARAMKEY_FRAME_RATE*: This parameter can be used to configure the desired 323framerate. Note that the encoder will automatically try to adjust the framerate 324if the timestamps on the input video frames don't match the configured 325framerate. 326- *C2_PARAMKEY_REQUEST_SYNC_FRAME*: This parameter can be used to request 327additional key frames in addition to the periodic ones requested through the 328*C2_PARAMKEY_SYNC_FRAME_INTERVAL* parameter. 329 330### Supported Input Pixel Formats: 331 332The V4L2 encoder supports various input pixel formats, however frames are 333currently always passed to the V4L2 encoder in the NV12 format. If a video frame 334using a different pixel format is passed to the encoder, format conversion will 335be performed to convert the frame to the NV12 format. 336 337### Additional Features: 338 339To improve the resilience of H.264 video streams when data is missing, SPS and 340PPS NAL units are prepended to IDR frames by enabling the 341*V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR* control. If the V4L2 driver does not 342support this control the encoder will manually cache and prepend SPS and PPS NAL 343units. 344 345[1]: https://android.googlesource.com/kernel/common/+/ed63bb1d1f8469586006a9ca63c42344401aa2ab 346