FFmpeg Examples using Software Filters

Examples shown in this section describe how to use FFmpeg combining both AMD accelerated functions and software filters. In all these examples, an AMA compatible device, e.g., MA35D, is used to decode the input stream, the decoded frames are transfered back to the host using the hwdownload_ama filter, one or more software filters are applied to the decoded frames and the filtered frames are transfered back to the device using hwupload_ama for encoding. It should be noted that, when possible, zero-copy filters such as split and fps should be used, to minimize the traffic between the host CPU and target device.

In the following sections, description of command line options is done in an accumulative manner, i.e., previously described options are not explained further.

Environment Setup

  1. The AMD AMA Video SDK version of FFmpeg, Gstreamer and XMA applications can be found in the /opt/amd/ama/ma35/bin folder of your system. If this folder is not present, install the required packages:

    See On Premises

  2. Configure the environment to use the AMD AMA Video SDK. This a mandatory step for all applications:

    source /opt/amd/ama/ma35/scripts/setup.sh
    

The setup script exports important environment variables and ensures proper execution environment for AMD AMA Video SDK.

Sourcing the setup script should be performed each time you open a new terminal on your system. This is required for the environment to be correctly configured.

Video Rotation

The following example demonstrates how to generate multiple flip-over operations on the host CPU and to encode the resultant stream on a target AMA device:

ffmpeg -hwaccel ama -hwaccel_device /dev/ama_transcoder0 -c:v h264_ama  -out_fmt yuv420p -i <INPUT> \
 -filter_complex "split=2[a][b]; \
  [a]hwdownload_ama,transpose=dir=1:passthrough=none,hwupload_ama[a1]; \
  [b]hwdownload_ama,transpose=dir=2:passthrough=none,hwupload_ama[b1]" \
 -map "[a1]" -c:v h264_ama -b:v 4M -f mp4 <OUT DIR>/90_flip.mp4 \
 -map "[b1]" -c:v h264_ama -b:v 4M -f mp4 <OUT DIR>/270_flip.mp4

, where INPUT is the path to the input AVC clip and OUT DIR is the name of the output directory. See FFmpeg's filters for details on passthrough and split filters.

Logo Overlay

The following example demonstrates how to overlay a yuv420p image on a decoded video, through host CPU, and to encode the resultant stream on a target AMA device:

ffmpeg -i <y4M LOGO> -hwaccel ama -hwaccel_device /dev/ama_transcoder0 -c:v h264_ama  -out_fmt yuv420p -i <INPUT> \
 -filter_complex "[1]hwdownload_ama[vid],[vid][0]overlay=x=0:y=0[l],[l]format=yuv420p[lay],[lay]hwupload_ama" \
 -c:v h264_ama -b:v 4M  -f mp4 <OUT DIR>/overlay.mp4

Note that input to accelerated encoder, h264_ama, is in yuv420p format. See FFmpeg's overlay filter for detail usage.

Crop and Shift

The following example demonstrates how to crop a decoded video, at a given x and y offsets, through host CPU, and to encode the resultant stream on a target AMA device:

ffmpeg -hwaccel ama -hwaccel_device /dev/ama_transcoder0 -c:v h264_ama  -out_fmt yuv420p -i <INPUT> \
 -filter_complex "hwdownload_ama,crop=640:480:(in_w-800):(in_h-out_h)/2,hwupload_ama" \
 -c:v h264_ama -b:v 4M  -f mp4 <OUT DIR>/crop.mp4

Video Composition

The following example demonstrates how to compose a tiled video on the host CPU, and to encode the resultant stream on a target AMA device:

ffmpeg -hide_banner -hwaccel ama -c:v h264_ama -out_fmt yuv420p -i <INPUT_1> -c:v h264_ama -out_fmt yuv420p -i <INPUT_2> \
 -c:v h264_ama -out_fmt yuv420p -i <INPUT_3> -c:v h264_ama -out_fmt yuv420p -i <INPUT_4> \
 -filter_complex "[0]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload_ama[00];[1]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload_ama[11];\
  [2]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload_ama[22];[3]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload_ama[33]; [00][11]hstack[top];\
  [22][33]hstack[bot]; [top][bot] vstack,hwupload_ama" \
 -c:v h264_ama -b:v 15M -vframes 600 -f mp4 <OUT DIR>/compose.mp4

Note

For 10-bit support, the following changes are required to the provided examples:

  1. Replace -out_fmt yuv420p with -out_fmt yuv420p10le

  2. Add format=yuv420p10le to the filter graph.

As an example, the following command shows 10-bit crop and shift:

ffmpeg -hwaccel ama -hwaccel_device /dev/ama_transcoder0 -c:v h264_ama  -out_fmt yuv420p10le -i <INPUT> \
 -filter_complex "hwdownload_ama,format=yuv420p10le,crop=640:480:(in_w-800):(in_h-out_h)/2,hwupload_ama" \
 -c:v h264_ama  -b:v 4M -f mp4 <OUT DIR>/crop_10b.mp4

Chroma Subsampling

The following example demonstrates how to perform 4:2:2 to 4:2:0 chroma subsampling on the host CPU, and to encode the resultant stream on a target AMA device:

ffmpeg -hwaccel ama -i <INPUT>  -vf "format=yuv420p,hwupload_ama" -c:v h264_ama -b:v 4M -f mp4 <OUTPUT>

, where <INPUT> is path of the 442 input raw file and <OUTPUT> is the encoded video file.

Decoding and De-interlacing

Given that AMD AMA SDK neither accepts interlaced contents nor performs de-interlacing, such operations need to be performed on the host CPU. The following example demonstrates how to perform interlaced to progressive transcoding:

ffmpeg -hwaccel ama -c:v h264 -i <INPUT>  -filter_complex  "yadif=0,hwupload_ama" -c:v h264_ama -b:v 4M -f mp4 <OUTPUT>

, where <INPUT> is the path to the AVC interlaced content and <OUTPUT> is the encoded video file.

Text Insertion

The following example demonstrates how to insert a text into encode stream:

ffmpeg -y -hwaccel ama -hwaccel_device /dev/ama_transcoder0 -re \
-f lavfi -i testsrc=duration=60:size=1920x1080:rate=60,format=yuv420p -f rawvideo \
-filter_complex "drawtext=text='Hello:y=10:fontcolor=yellow:fontsize=36',format=yuv420p, hwupload_ama" \
-c:v h264_ama -b:v 4M -f mp4 <OUTPUT>