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
filter, one or more software filters are applied to the decoded frames and the filtered frames are transfered back to the device using hwupload
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¶
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
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,transpose=dir=1:passthrough=none,hwupload[a1]; \
[b]hwdownload,transpose=dir=2:passthrough=none,hwupload[b1]" \
-map "[a1]" -c:v h264_ama -f mp4 <OUT DIR>/90_flip.mp4 \
-map "[b1]" -c:v h264_ama -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[vid],[vid][0]overlay=x=0:y=0[l],[l]format=yuv420p[lay],[lay]hwupload" \
-c:v h264_ama -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,crop=640:480:(in_w-800):(in_h-out_h)/2,hwupload" \
-c:v h264_ama -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[00];[1]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload[11];\
[2]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload[22];[3]scaler_ama=outputs=1:out_res=(1920x1080|yuv420p),hwdownload[33]; [00][11]hstack[top];\
[22][33]hstack[bot]; [top][bot] vstack,hwupload" \
-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:
Replace
-out_fmt yuv420p
with-out_fmt yuv420p10le
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,format=yuv420p10le,crop=640:480:(in_w-800):(in_h-out_h)/2,hwupload" \
-c:v h264_ama -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" -c:v h264_ama -b:v 1M -f mp4 <OUTPUT>
, where <INPUT>
is path to the 442 input raw file and <OUTPUT>
is the encoded video file.