Example programs
================
This is a collection of example programs demonstrating how to use Netint hardware
transcoding solutions through libavcodec.
Note, libavcodec API examples not supported on FFmpeg versions below 3.4.2.

###
Building example programs

These libavcodec api example programs were written for use with FFmpeg-n4.2.1.
Use 'make build_info' to see a list of supported examples for the FFmpeg build.
FFmpeg must be build with shared libraries to link to these api example programs.
For example, from the FFmpeg directory, run:
    sudo make clean; bash build_ffmpeg.sh --shared && sudo make install && sudo ldconfig

To build libavcodec api example programs, from this directory run:
    sudo make clean; make

###
ni_demuxing_decoding

This example program uses libavformat to demux an input video, then uses Netint HW decoder
through libavcodec to decode it to YUV.

Usage example:
    sudo ./ni_demuxing_decoding input_file.h264 output_file.yuv


###
ni_encode_video

This example program take in a YUV input file and encodes it with Netint's HW encoder
through libavcodec. Can set encoding parameters as described in the Codensity T408
Integration & Programming Guide.

Usage example:
    sudo ./ni_encode_video -i input.yuv --size 426x240 -r 25 -c h265_ni_logan_enc -x gopPresetIdx=5:intraPeriod=120:RcEnable=1 -b 100000 -o output.h265

Parameters:
    -s | --size                  Size of input yuv in [width]x[height].
    -x | --xcoder_params         Xcoder parameters. See Integration and Programing Guide for details.
    -i | --input                 Input yuv file.
    -o | --output                Output video file.
    -c | --codec                 Codec name. [h264_ni_logan_enc, h265_ni_logan_enc].
    -r | --fps                   FPS of output.
    -b | --bitrate               Bitrate in bits/second.
    -e | --enc                   Encoder device index to use. See ni_rsrc_mon_logan.
    -l | --loop                  Loop read input count.
    -g | --log_level             Log level for messages. [NONE, FATAL, ERROR, INFO, DEBUG, TRACE]
    -h | --help                  Help info.

    Testing intermittent idle of input:
    -w | --inst_restart_count    Number of times to restart encoder instance and re-read input.
    -a | --interval_frame        Number of frames between intervals of test activity.
                                 (if 0, use random interval).
    -v | --interval_time         Number of seconds to sleep at each interval_frame.
                                 (if 0, random between 1s - 20s).
    -k | --force_key_frame       Flag to force key frame at each interval (must set interval_frame).

    Testing Region of Interest:
    -z | --roi                   Apply demo ROI mode. Options: 1 -> Border ROI, 2 -> Center ROI.

    Testing Bitrate reconfiguration:
    -m | --bitrate_reconfig      Use side data to reconfigure bitrate target throughout video.
                                 The --bitrate parameter must be set. Use the format
                                 bitrate_min:bitrate_max:bitrate_step:frame_interval to cause target
                                 bitrate to sweep in a saw-tooth pattern between bitrate_min and
                                 bitrate_max in increments of bitrate_step every frame_interval frames.


###
ni_encode_video_dynamic_resolution

This example program shows how to change the output resolution dynamically. The new resolution can be entered
with the commandline parameter. The resolution change location is hard coded to frame 100. It take in a YUV input
file and encodes it with Netint's HW encoder through libavcodec. First 99 frames are encoded with the input resolution and
the rest are encoded with the changed resolution.

Usage example:
    sudo ./ni_encode_video_dynamic_resolution -i input.yuv -s 1920x1080 -n 1280x720 -r 25 -c h265_ni_logan_enc -x gopPresetIdx=5:intraPeriod=120:RcEnable=1 -b 2000000 -o output.h265

Parameters:
    -s | --size                Size of input yuv in [width]x[height].
    -n | --new_size            Size of yuv for resolution change in [width]x[height].
    -x | --xcoder_params       Xcoder parameters. See Integration and Programing Guide for details.
    -i | --input               Input yuv file.
    -o | --output              Output video file.
    -c | --codec               Codec name. [h264_ni_logan_enc, h265_ni_logan_enc].
    -r | --fps                 FPS of output.
    -b | --bitrate             Bitrate in bits/second.
    -e | --enc                 Encoder device index to use. See ni_rsrc_mon_logan.
    -g | --log_level           Log level for messages. [NONE, FATAL, ERROR, INFO, DEBUG, TRACE]
    -h | --help                Help info.


###
ni_multi_thread_transcoding

This example would start multiple threads specified at once.
In each thread, it opens a decoder AVCodecContext instance, an encoder
AVCodecContext instance and simple source-to-sink FilterContext.
The output file is named "output-%{thread index}".%{mux name} and stored
in output directory specified by parameter. when there is a sequence change,
the encoder would be flushed, closed and re-opened for frames with
the new sequence.

The main thread calculates and prints fps in the front until
there is an ctrl-c interrupt or all the transcoding threads are done.

Usage example:
    ./multi_thread_transcoding -x "intraPeriod=120:RcEnable=1" \
    -i /path/to/mp4 -e h264_ni_logan_enc -d h264_ni_logan_dec -t 8 -s mp4

Note, to transcode the video clip in the LGXCOD-2155:
    ./ni_multi_thread_transcoding -r -s flv -i input.flv -d h264 -e h264_ni_logan_enc -x \
    "RcEnable=1:hvsQPEnable=1:bitrate=670000:gopPresetIdx=8:intraPeriod=0:repeatHeaders=1:GenHdrs=1"

Parameters:
    -x | --encoder-params    encoder parameters.
    -i | --input             input file path.
    -o | --output_dir        output directory path.
    -e | --encoder           encoder name.
    -d | --decoder           decoder name.
    -t | --threads           number of codec threads.
    -b | --bitrate           bitrate.
    -n | --devid             device id.
    -s | --suffix            file extension of output. Use 'null' for no output.
    -l | --loop              numbers of loop to go.
    -f | --filter            decoded frames passing through filters.
    -r | --repeat-header     force repeating headers for IDR frames.
    -v | --loglevel          available debug level: warning, info, debug, trace.
    -k | --force-keyframe    encode a keyframe whenever input source has a keyframe.
    -c | --rescale-ts        rescale timestamp and timebase to default timebase of file type in --suffix.
    -a | --disable-audio     disable audio streams.
    -h | --help              print this help information.


###
ni_multi_thread_encode_video

This example would start multiple threads specified at once.
In each thread, it creates and opens an AVCodecContext intance,
and runs encoder with YUV files as the input source. The output
file is named "output-%{thread index}".%{codec} and stored in
the directory specified by parameter.

The main thread calculates and prints fps in the front until
there is an ctrl-c interrupt or all the encode threads are done.

Usage example:
    ./multi_thread_encode_video -x "intraPeriod=120:RcEnable=1" \
    -i /path/to/YUV -c h264_ni_logan_enc -p 1920x1080 -d 0 -t 8 -s 100

Parameters:
    -x - NetInt encoder parameters.
    -i - the input file path.
    -o - the output directory.
    -c - encoder name from "ffmpeg -encoders".
    -b - bitrate
    -p - pixels format, must be WidthxHeight.
    -d - for ni encoder, which card to operate on.
    -t - how many threads to create.
    -s - how many encode loops to run.


###
ni_transcode_filter

This sample program demonstrates the transcoding example of the general filter, such as scale, overlay, etc. 
But this example only supports one input stream and one output stream.

Usage examples:
	./ni_transcode_filter -i ../../libxcoder_logan/test/1280x720p_Basketball.264 -o outputd.h264 -d h264_ni_logan_dec -e h264_ni_logan_enc -n 0 -f "scale=640:640"  -p "RcEnable=1:bitrate=1024000" -b 1024000

	./ni_transcode_filter -i ../../libxcoder_logan/test/1280x720p_Basketball.264 -o outtest.h264 -d h264_ni_logan_dec -e h264_ni_logan_enc -n 0 -f "movie=test.png[watermask];[in][watermask]overlay=0:0[out]"  -p "roiEnable=1:RcEnable=1:bitrate=1024000" -b 1024000

Parameters:
	-x | --decoder-params    decoder parameters
	-i | --input             input file path
	-o | --output_file       output file path
	-d | --decoder           decoder name [h264_ni_logan_dec | h265_ni_logan_dec]
	-n | --devid             device id
	-v | --loglevel          available debug level: warning, info, debug, trace
	-f | --filter_dsc        filter set params
	-b | --bitrate           set bitrate
	-e | --encode-name       encode name [h264_ni_logan_enc | h265_ni_logan_enc]
	-p | --encode_params     encode params
	-h | --help              print this help information
