28
Embedded Linux Conference Europe 2018 Supporting Hardware Codecs in a Linux system Maxime Ripard [email protected] © Copyright 2004-2018, Bootlin. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! embedded Linux and kernel engineering - Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/28

SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Embedded Linux Conference Europe 2018

Supporting HardwareCodecs in a LinuxsystemMaxime [email protected]

© Copyright 2004-2018, Bootlin.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

embedded Linux and kernel engineering

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/28

Page 2: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Maxime Ripard

I Embedded Linux engineer and trainer at BootlinI Embedded Linux development: kernel and driver development, system integration,

boot time and power consumption optimization, consulting, etc.I Embedded Linux, Linux driver development, Yocto Project / OpenEmbedded and

Buildroot training, with materials freely available under a Creative Commons license.I https://bootlin.com

I ContributionsI Co-maintainer of the sunXi SoCs from Allwinner in Linux and U-BootI Co-maintainer of drm-misc in LinuxI Contributor to a couple of other open-source projects, Buildroot, U-Boot, Barebox

I Living in Toulouse, south west of France

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/28

Page 3: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Supporting Hardware Codecs in a Linux system

Introduction to Video Decoding

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/28

Page 4: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Video Decoding

I An encoded video is basically two things:I A container, which will organize the video and audio streams, and the metadataI A codec, which will encode a given audio or video stream

I We’re only interested in the (video) codec hereI With a codec, the video stream is encoded into a bitstream

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/28

Page 5: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Bitstream

I A bitstream is a sequence of bits. – WikipediaI In the context of codecs, it refers to the compressed output of the encoder, the

at-rest dataI It’s mainly composed of a few things:

I Separators between encoded dataI Metadata holding the compression parametersI Slices, buffers holding the compressed data

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/28

Page 6: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Decoder

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/28

Page 7: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Stateful Codec

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/28

Page 8: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Supporting Hardware Codecs in a Linux system

Video Decoding in Linux

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/28

Page 9: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

V4L2

I Introduced in 2002, in 2.5.46I Supports a wide range of devices

I Video Capture (camera, tuners)I Memory to memory devices (hardware codecs, scalers, deinterlacers)I Radio receivers and transceiversI SDR

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/28

Page 10: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

V4L2: A Dumb M2M Pipeline

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/28

Page 11: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

V4L2: Stateful Codec Support

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/28

Page 12: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Supporting Hardware Codecs in a Linux system

And then, everything falls apart...

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/28

Page 13: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Stateless Codec

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/28

Page 14: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Request API

I An API that let’s you tie controls with buffersI First RFC sent in 2015 by Hans VerkuilI Taken over in 2016 by Laurent Pinchart and Sakari AilusI Taken over in 2017 by Alexandre CourbotI Taken over in 2018 by Hans VerkuilI Merged in 4.20 (Yay!)

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/28

Page 15: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Stateless Codec Stack

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/28

Page 16: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Supporting Hardware Codecs in a Linux system

Enters Allwinner

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/28

Page 17: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Allwinner

I Produces multimedia SoCs, targeted at tablets and STBsI Found in a number of cheap SBCsI Just like many multimedia SoCs, it has a hardware codec…I …a stateless codec that is

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/28

Page 18: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Allwinner BSP Stack

I Linux 3.4, or 3.10 these daysI Not using v4l2, but a stack split in two parts:

I A kernel driver that manages the resources, and provides access to the codecregisters

I A userspace stack implementing the logic to decode and encode videos, that used tobe closed source

I Obviously not compatible with mainline kernel

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 18/28

Page 19: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Reverse-Engineering Effort

I Reverse-engineering done by the Cedrus effort in order to have an opensourcestack

I MPEG2, MPEG4, H264, H265 and VP8 Decoding and H264 EncodingI Targeted at Allwinner’s BSP kernelI Kernel obviously not maintained, and seriously outdated nowI But very functional, supports many codecs, decoding and encoding, pretty much

doneI libvdpau implementation to hook into popular media players

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/28

Page 20: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

First attempt at a mainline driver

I Summer Internship in 2016, by Florent RevestI Worked on an RFC, to do a mainline-based driverI Quite successful:

I MPEG2 implementationI libva implementation to integrate in the popular media players

I But was still a prototype:I Still had bugsI Slow, and could only play videos at any other resolution than the displayI Who cares about MPEG2 anymore?

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/28

Page 21: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Kickstarter Campaign

I Huge effort to bring it into a production-ready stateI Let’s fund it through a Kickstarter campaign!I The goals we achieved were to develop the driver for the most used SoCs, with

H264 and H265 decoding supportI Funded a full-time 6-month internship, and a part-time engineerI Built on top of the prototype

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/28

Page 22: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Cedrus Stack

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/28

Page 23: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Supporting Hardware Codecs in a Linux system

Displaying is hard

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 23/28

Page 24: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

In an ideal world...

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 24/28

Page 25: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Except that...

I The decoded frame is in a proprietary format...I ... that the display engine can understand

I The decoded frame is at the video resolution, not the window resolution...I ... and the display engine can upscale or downscale it at will

I But X11 doesn’t let you do that easily

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 25/28

Page 26: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Possible solutions

I X doesn’t have any knowledge about proprietary formatsI Let’s convert it in software!

I Reaaaaaally slow, even without scalingI Hardware video playback is supposed to be more efficient

I The X Video Extension is supposed to let you do thatI But it’s completely deprecatedI Only one video would be playable at the same time (on our platform)I Possible glitches

I You could import the decoded frame in the GPU, and do the format conversionand scaling thereI But the OpenGL blob has a lot of constraintsI And memory bandwidth is a sparse resource

I Wayland!I You have to patch all Wayland compositors to deal with your proprietary formatI And you leave out all the users that are stuck with X11

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 26/28

Page 27: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Current State

I The Request API is merged in 4.20I Libva implementation working on top of it, with MPEG2, H264 and H265 decoding

supportI Test tool to simulate some video decoding (v4l2-request-test)

I Our Cedrus driver is merged in 4.20 as well (but in staging)I Only MPEG2 for nowI H264 and H265 patches have been sent

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 27/28

Page 28: SupportingHardware CodecsinaLinux embedded Linux and ...MaximeRipard I EmbeddedLinuxengineerandtraineratBootlin I EmbeddedLinuxdevelopment:kernelanddriverdevelopment,systemintegration

Questions? Suggestions? Comments?

Maxime [email protected]

Slides under CC-BY-SA 3.0https://bootlin.com/pub/conferences/2018/elce/ripard-v4l2-codec

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 28/28