QML Video Shader Effects Example

Applying shader effects on video and camera viewfinder content.

Running the Example

To run the example from Qt Creator , open the Welcome mode and select the example from Examples . For more information, visit Building and Running an Example.

Overview

QML Video Shader Effects demonstrates how a ShaderEffect can be used to apply postprocessing effects, expressed in GLSL, to QML VideoOutput type.

It also shows how native code can be combined with QML to implement more advanced functionality - in this case, C++ code is used to calculate the QML frame rate. This value is rendered in QML in a semi-transparent item overlaid on the video content.

The following screenshots show shader effects being applied. In each case, the effect is implemented using a fragment shader.

Here we see an edge detection algorithm being applied to a video clip ( Sintel from blender.org ).

../_images/qmlvideofx-video-edgedetection.jpg

This image shows a page curl effect, applied to the same video clip.

../_images/qmlvideofx-video-pagecurl.jpg

Here we see a ‘glow’ effect (edge detection plus colour quantization) being applied to the camera viewfinder.

../_images/qmlvideofx-camera-glow.jpg

This image shows a ‘wobble’ effect applied to the viewfinder.

../_images/qmlvideofx-camera-wobble.jpg

The application includes many more effects than the ones shown here - look for Effect*.qml files in the list of files below to see the full range.

Application Structure

Shader effects can be applied to video or viewfinder content using ShaderEffect , as shown in the following example, which applies a wiggly effect to the content:

In this application, the usage of the ShaderEffect and VideoOutput types is a bit more complicated, for the following reasons:

  • Each effect can be applied to either a VideoOutput or an Image item, so the type of the source item must be abstracted away from the effect implementation

  • For some effects (such as the edge detection and glow examples shown in the screenshots above), the transformation is applied only to pixels to the left of a dividing line - this allows the effect to be easily compared with the untransformed image on the right

  • Most effects have one or more parameters which can be modified by the user - these are controlled by sliders in the UI which are connected to uniform values passed into the GLSL code

The abstraction of source item type is achieved by the Content , which uses a Loader to create either a MediaPlayer , Camera , or an Image :

...
...

Each effect is implemented as a QML item which is based on the Effect , which in turn is based on the ShaderEffect :

The interface of Effect allows for derived effects to specify the number of parameters which they support (and therefore the number of sliders which should be displayed), and whether a vertical dividing line should be drawn between transformed and untransformed image regions. As an example, here is the implementation of the pixelation effect. As you can see, the pixelation effect supports one parameter (which controls the pixelation granularity), and states that the divider should be displayed.

The main.qml file shows a FileOpen item, which allows the user to select the input source and an EffectSelectionList item, which lists each of the available shader effects. As described above, a Content item is used to load the appropriate input and effect type. A Divider item draws the vertical dividing line, which can be dragged left or right by the user. Finally, a ParameterPanel item renders the sliders corresponding to each effect parameter.

../_images/qmlvideofx-effects-menu.jpg

Calculating and Displaying QML Painting Rate

The QML painting rate is calculated by the FrequencyMonitor class, which turns a stream of events (received via the notify() slot), into an instantaneous and an averaged frequency:

...

The FrequencyMonitor class is exposed to QML like this

and its data is displayed by defining a QML item called FrequencyItem, like this:

...

The result looks like this:

../_images/video-qml-paint-rate.png

All that remains is to connect the afterRendering() signal of the QQuickView object to a JavaScript function, which will eventually call frequencyItem.notify():

...
...

Example project @ code.qt.io