FFmpegfs Fuse Multi Media Filesystem 2.16
Classes | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Private Attributes | Static Private Attributes | List of all members
FFmpeg_Transcoder Class Reference

The FFmpeg_Transcoder class. More...

#include <ffmpeg_transcoder.h>

Inheritance diagram for FFmpeg_Transcoder:
Inheritance graph
[legend]
Collaboration diagram for FFmpeg_Transcoder:
Collaboration graph
[legend]

Classes

struct  BUFFER_DATA
 Buffer structure, used in FFmpeg_Transcoder::read_packet. More...
 
struct  INPUTFILE
 Input file definition. More...
 
struct  OUTPUTFILE
 Output file definition. More...
 
struct  PRORES_BITRATE
 Predicted bitrates for Apple Prores, see https://www.apple.com/final-cut-pro/docs/Apple_ProRes_White_Paper.pdf. More...
 
class  StreamRef
 In/output stream reference data. More...
 

Public Member Functions

 FFmpeg_Transcoder ()
 
virtual ~FFmpeg_Transcoder ()
 
bool is_open () const
 
int open_input_file (LPVIRTUALFILE virtualfile, std::shared_ptr< FileIO > fio=nullptr)
 
int open_output_file (Buffer *buffer)
 Open output file. Data will actually be written to buffer and copied by FUSE when accessed. More...
 
int process_single_fr (DECODER_STATUS *status)
 
int encode_finish ()
 
void closeio ()
 Close transcoder, free all ressources. More...
 
time_t mtime () const
 Get last modification time of file. More...
 
int64_t duration () const
 Get the file duration. More...
 
size_t predicted_filesize () const
 Try to predict the recoded file size. This may (better will surely) be inaccurate. More...
 
uint32_t video_frame_count () const
 Get the number of video frames in file. More...
 
uint32_t segment_count () const
 Get the number of HLS segments of file. More...
 
const ID3v1id3v1tag () const
 Assemble an ID3v1 file tag. More...
 
virtual const char * filename () const override
 Return source filename. More...
 
virtual const char * destname () const override
 Return destination filename. More...
 
virtual const char * virtname () const override
 Return virtual filename. Same as destination filename, but with virtual (mount) path.. More...
 
bool close_output_file ()
 Closes the output file if open and reports lost packets. Can safely be called again after the file was already closed or if the file was never open. More...
 
bool close_input_file ()
 Closes the input file if open. Can safely be called again after the file was already closed or if the file was never open. More...
 
int stack_seek_frame (uint32_t frame_no)
 Seek to a specific frame. Does not actually perform the seek, this is done asynchronously by the transcoder thread. More...
 
int stack_seek_segment (uint32_t segment_no)
 Seek to a specific HLS segment. Does not actually perform the seek, this is done asynchronously by the transcoder thread. More...
 
bool is_multiformat () const
 Check for an export frame format. More...
 
bool is_frameset () const
 Check for an export frame format. More...
 
bool is_hls () const
 Check for HLS format. More...
 
bool have_seeked () const
 Check if we made a seek operation. More...
 
void flush_buffers ()
 Flush FFmpeg's input buffers. More...
 
int flush_delayed_audio ()
 Flush delayed audio packets, if there are any. More...
 
int flush_delayed_video ()
 Flush delayed video packets, if there are any. More...
 
int flush_delayed_subtitles ()
 Flush delayed subtitle packets, if there are any. More...
 
int64_t pts () const
 Get PTS (presentation time stamp) of decoded audio/video so far. More...
 
uint32_t last_seek_frame_no () const
 Current seek frame if available. More...
 
- Public Member Functions inherited from FFmpeg_Base
 FFmpeg_Base ()
 Construct FFmpeg_Base object. More...
 
virtual ~FFmpeg_Base ()=default
 Destruct FFmpeg_Base object.
 

Static Public Member Functions

static bool audio_size (size_t *filesize, AVCodecID codec_id, BITRATE bit_rate, int64_t duration, int channels, int sample_rate, AVSampleFormat sample_format)
 Predict audio file size. This may (better will surely) be inaccurate. More...
 
static bool video_size (size_t *filesize, AVCodecID codec_id, BITRATE bit_rate, int64_t duration, int width, int height, bool interleaved, const AVRational &framerate)
 Predict video file size. This may (better will surely) be inaccurate. More...
 
static bool total_overhead (size_t *filesize, FILETYPE filetype)
 Predict overhead in file size. This may (better will surely) be inaccurate. More...
 

Protected Types

enum class  HWACCELMODE { NONE , ENABLED , FALLBACK }
 Currently active hardware acceleration mode. More...
 
typedef struct FFmpeg_Transcoder::BUFFER_DATA BUFFER_DATA
 Buffer structure, used in FFmpeg_Transcoder::read_packet.
 
typedef struct FFmpeg_Transcoder::PRORES_BITRATE PRORES_BITRATE
 Predicted bitrates for Apple Prores, see https://www.apple.com/final-cut-pro/docs/Apple_ProRes_White_Paper.pdf. More...
 
typedef struct FFmpeg_Transcoder::PRORES_BITRATELPPRORES_BITRATE
 Pointer version of PRORES_BITRATE.
 
typedef PRORES_BITRATE const * LPCPRORES_BITRATE
 Pointer to const version of PRORES_BITRATE. More...
 
typedef std::map< int, StreamRefStreamRef_map
 Map stream index to StreamRef. More...
 
typedef std::map< AVHWDeviceType, AVPixelFormat > DEVICETYPE_MAP
 Map device types to pixel formats. More...
 
typedef std::variant< FFmpeg_Frame, FFmpeg_SubtitleMULTIFRAME
 Combined audio/videoframe and subtitle. More...
 
typedef std::multimap< int64_t, MULTIFRAMEMULTIFRAME_MAP
 Audio frame/video frame/subtitle buffer. More...
 
typedef std::map< int, int > STREAM_MAP
 Map input subtitle stream to output stream. More...
 

Protected Member Functions

int copy_audio_to_frame_buffer (int *finished)
 Copy data from audio FIFO to frame buffer. Divides WAV data into proper chunks to be fed into the encoder. More...
 
int open_bestmatch_decoder (AVFormatContext *format_ctx, AVCodecContext **codec_ctx, int *stream_idx, AVMediaType type)
 Find best match stream and open codec context for it. More...
 
int open_bestmatch_video ()
 Open the best match video stream, if present in input file. More...
 
int open_bestmatch_audio ()
 Open the best match audio stream. More...
 
int open_subtitles ()
 Open all subtitles streams, if present in input file and if supported by output file. The input and output codec type must also match: Can only transcode bitmap subtitles into bitmap subtitles or text to text. More...
 
int open_albumarts ()
 open_albumarts More...
 
AVPixelFormat get_hw_pix_fmt (const AVCodec *codec, AVHWDeviceType dev_type, bool use_device_ctx) const
 Determine the hardware pixel format for the codec, if applicable. More...
 
int open_decoder (AVFormatContext *format_ctx, AVCodecContext **codec_ctx, int stream_idx, const AVCodec *input_codec, AVMediaType mediatype)
 Open codec context for stream_idx. More...
 
int open_output_frame_set (Buffer *buffer)
 Open output frame set. Data will actually be written to buffer and copied by FUSE when accessed. More...
 
int open_output (Buffer *buffer)
 Open output file. Data will actually be written to buffer and copied by FUSE when accessed. More...
 
int process_output ()
 Process headers of output file Write file header, process meta data and add album arts. More...
 
bool is_video () const
 
int update_codec (void *opt, const PROFILE_OPTION_VEC &profile_option_vec) const
 Prepare codec options. More...
 
int prepare_codec (void *opt, FILETYPE filetype) const
 Prepare codec options for a file type. More...
 
int add_stream (AVCodecID codec_id)
 Add new stream to output file. More...
 
int add_subtitle_stream (AVCodecID codec_id, StreamRef &input_streamref, const std::optional< std::string > &language=std::nullopt)
 Add new subtitle stream to output file. More...
 
int add_stream_copy (AVCodecID codec_id, AVMediaType codec_type)
 Add new stream copy to output file. More...
 
int add_albumart_stream (const AVCodecContext *input_codec_ctx)
 Add a stream for an album art. More...
 
int add_albumart_frame (AVStream *output_stream, AVPacket *pkt_in)
 Add album art to stream. More...
 
int open_output_filestreams (Buffer *buffer)
 Open an output file and the required encoder. Also set some basic encoder parameters. Some of these parameters are based on the input file's parameters. More...
 
template<size_t size>
const char * tagcpy (char(&out)[size], const std::string &in) const
 Safely copy a tag to a target buffer. If the input buffer size is larger than output the data will be truncated to avoid overruns. The function never appends a /0 terminator. More...
 
template<class T >
const T & tagcpy (T &out, const std::string &in) const
 Safely copy a tag to a target buffer. If the input buffer size is larger than output the data will be truncated to avoid overruns. The function never appends a /0 terminator. More...
 
void copy_metadata (AVDictionary **metadata_out, const AVDictionary *metadata_in, bool contentstream=true)
 Process the metadata in the FFmpeg file. This should be called at the beginning, before reading audio data. The set_text_tag() and set_picture_tag() methods of the given Encoder will be used to set the metadata, with results going into the given Buffer. This function will also read the actual PCM stream parameters. More...
 
int process_metadata ()
 Copy metadata from source to target. More...
 
int process_albumarts ()
 Copy all album arts from source to target. More...
 
int init_resampler ()
 Initialize the audio resampler based on the input and output codec settings. If the input and output sample formats differ, a conversion is required libswresample takes care of this, but requires initialization. More...
 
int init_audio_fifo ()
 Initialise a FIFO buffer for the audio samples to be encoded. More...
 
int update_format (AVDictionary **dict, const PROFILE_OPTION_VEC &option_vec) const
 Update format options. More...
 
int prepare_format (AVDictionary **dict, FILETYPE filetype) const
 Prepare format optimisations. More...
 
int write_output_file_header ()
 Write the header of the output file container. More...
 
int store_packet (AVPacket *pkt, AVMediaType mediatype)
 Store packet in output stream. More...
 
int decode_audio_frame (AVPacket *pkt, int *decoded)
 Decode one audio frame. More...
 
int decode_video_frame (AVPacket *pkt, int *decoded)
 Decode one video frame. More...
 
int decode_subtitle (AVPacket *pkt, int *decoded)
 Decode one subtitle. More...
 
int decode_subtitle (AVCodecContext *codec_ctx, AVPacket *pkt, int *decoded, int out_stream_idx)
 Decode one subtitle. More...
 
void make_pts (AVPacket *pkt, int64_t *cur_ts) const
 Create PTS/DTS and update the packet. If the update packet lacks time stamps, create a fictitious PTS or DTS and update it. If the packet already has valid time stamps, nothing is changed. More...
 
int decode_frame (AVPacket *pkt)
 Decode one frame. More...
 
int init_converted_samples (uint8_t ***converted_input_samples, int frame_size)
 Initialise a temporary storage for the specified number of audio samples. The conversion requires temporary storage due to the different format. The number of audio samples to be allocated is specified in frame_size. More...
 
int convert_samples (uint8_t **input_data, int in_samples, uint8_t **converted_data, int *out_samples)
 Convert the input audio samples into the output sample format. The conversion happens on a per-frame basis, the size of which is specified by frame_size. More...
 
int add_samples_to_fifo (uint8_t **converted_input_samples, int frame_size)
 Add converted input audio samples to the FIFO buffer for later processing. More...
 
int flush_frames_all (bool use_flush_packet)
 Flush the remaining frames for all streams. More...
 
int flush_frames_single (int stream_idx, bool use_flush_packet)
 Flush the remaining frames. More...
 
int read_decode_convert_and_store (int *finished)
 Read frame from source file, decode and store in FIFO. More...
 
int init_audio_output_frame (AVFrame *frame, int frame_size) const
 Initialise one input frame for writing to the output file. The frame will be exactly frame_size samples large. More...
 
int alloc_picture (AVFrame *frame, AVPixelFormat pix_fmt, int width, int height) const
 Allocate memory for one picture. More...
 
void produce_audio_dts (AVPacket *pkt)
 Produce audio dts/pts. This is required because the target codec usually has a different frame size than the source, so the number of packets will not match 1:1. More...
 
int decode (AVCodecContext *codec_ctx, AVFrame *frame, int *got_frame, const AVPacket *pkt) const
 
int create_audio_frame (int frame_size)
 Load one audio frame from the FIFO buffer and store in frame buffer. More...
 
int encode_audio_frame (const AVFrame *frame, int *data_present)
 Create one frame worth of audio to the output file. More...
 
int encode_video_frame (const AVFrame *frame, int *data_present)
 Encode one frame worth of video to the output file. More...
 
int encode_subtitle (const AVSubtitle *sub, int out_stream_idx, int *data_present)
 Encode one subtitle frame to the output file. More...
 
int encode_image_frame (const AVFrame *frame, int *data_present)
 Encode frame to image. More...
 
int write_output_file_trailer ()
 Write the trailer of the output file container. More...
 
size_t calculate_predicted_filesize () const
 Try to predict final file size. More...
 
bool get_video_size (int *output_width, int *output_height) const
 Get the size of the output video based on user selection and apsect ratio. More...
 
bool get_aspect_ratio (int width, int height, const AVRational &sar, AVRational *ar) const
 Calculate aspect ratio for width/height and sample aspect ratio (sar). More...
 
int init_deinterlace_filters (AVCodecContext *codec_ctx, AVPixelFormat pix_fmt, const AVRational &avg_frame_rate, const AVRational &time_base)
 Initialise video filters. More...
 
int send_filters (FFmpeg_Frame *srcframe, int &ret)
 Send video frame to the filters. More...
 
void free_filters ()
 Free filter sinks. More...
 
bool can_copy_stream (const AVStream *stream) const
 Check if stream can be copied from input to output (AUTOCOPY option). More...
 
bool close_resample ()
 Close and free the resampler context. More...
 
int init_rescaler (AVPixelFormat in_pix_fmt, int in_width, int in_height, AVPixelFormat out_pix_fmt, int out_width, int out_height)
 Init image size rescaler and pixel format converter. More...
 
int purge_audio_fifo ()
 Purge all samples in audio FIFO. More...
 
size_t purge_multiframe_map ()
 Purge all frames in buffer. More...
 
size_t purge_hls_fifo ()
 Purge all packets in HLS FIFO buffer. More...
 
void purge ()
 Purge FIFO and map buffers and report lost packets/frames/samples. More...
 
int do_seek_frame (uint32_t frame_no)
 Actually perform seek for frame. This function ensures that it is positioned at a key frame, so the resulting position may be different from the requested. If e.g. frame no. 24 is a key frame, and frame_no is set to 28, the actual position will be at frame 24. More...
 
int skip_decoded_frames (uint32_t frame_no, bool forced_seek)
 Skip decoded frames or force seek to frame_no. More...
 
void get_pix_formats (AVPixelFormat *in_pix_fmt, AVPixelFormat *out_pix_fmt, AVCodecContext *output_codec_ctx=nullptr) const
 Get correct input and output pixel format. More...
 
enum AVPixelFormat get_format (AVCodecContext *input_codec_ctx, const enum AVPixelFormat *pix_fmts) const
 
int hwdevice_ctx_create (AVBufferRef **hwaccel_enc_device_ctx, AVHWDeviceType dev_type, const std::string &device) const
 
int hwdevice_ctx_add_ref (AVCodecContext *input_codec_ctx)
 Add reference to hardware device context. More...
 
void hwdevice_ctx_free (AVBufferRef **hwaccel_device_ctx)
 Free (remove reference) to hardware device context. More...
 
int hwframe_ctx_set (AVCodecContext *output_codec_ctx, AVCodecContext *input_codec_ctx, AVBufferRef *hw_device_ctx) const
 Adds a reference to an existing decoder hardware frame context or allocates a new AVHWFramesContext tied to the given hardware device context if if the decoder runs in software. More...
 
int hwframe_copy_from_hw (AVCodecContext *output_codec_ctx, FFmpeg_Frame *sw_frame, const AVFrame *hw_frame) const
 
int hwframe_copy_to_hw (AVCodecContext *output_codec_ctx, FFmpeg_Frame *hw_frame, const AVFrame *sw_frame) const
 
int get_hw_decoder_name (AVCodecID codec_id, std::string *codec_name=nullptr) const
 Get the hardware codec name as string. This is required, because e.g. the name for the software codec is libx264, but for hardware it is h264_vaapi under VAAPI. More...
 
int get_hw_encoder_name (AVCodecID codec_id, std::string *codec_name=nullptr) const
 Get the hardware codec name as string. This is required, because e.g. the name for the software codec is libx264, but for hardware it is h264_vaapi under VAAPI. More...
 
int get_hw_vaapi_codec_name (AVCodecID codec_id, std::string *codec_name) const
 Determine VAAPI codec name. More...
 
int get_hw_mmal_decoder_name (AVCodecID codec_id, std::string *codec_name) const
 Determine MMAL decoder codec name. More...
 
int get_hw_omx_encoder_name (AVCodecID codec_id, std::string *codec_name) const
 Determine OMX encoder codec name. More...
 
int get_hw_v4l2m2m_encoder_name (AVCodecID codec_id, std::string *codec_name) const
 Determine video for linux encoder codec name. More...
 
uint32_t get_next_segment (int64_t pos) const
 Calculate next HLS segment from position. More...
 
bool goto_next_segment (uint32_t next_segment) const
 Check if segment number is next designated segment. More...
 
int create_fake_wav_header () const
 Create a fake WAV header Create a fake WAV header. Inserts predicted file sizes to allow playback to start directly. More...
 
int create_fake_aiff_header () const
 Create a fake AIFF header Create a fake AIFF header. Inserts predicted file sizes to allow playback to start directly. More...
 
int read_aiff_chunk (Buffer *buffer, size_t *buffoffset, const char *ID, uint8_t *chunk, size_t *size) const
 Read AIFF chunk. More...
 
bool is_audio_stream (int stream_idx) const
 Check for audio stream. More...
 
bool is_video_stream (int stream_idx) const
 Check for video stream. More...
 
bool is_subtitle_stream (int stream_idx) const
 Check for subtitle stream. More...
 
StreamRefget_out_subtitle_stream (int stream_idx)
 Get subtitle stream for the stream index. More...
 
bool stream_exists (int stream_idx) const
 Check if stream exists. More...
 
void add_stream_map (int in_stream_idx, int out_stream_idx)
 Add entry to input stream to output stream map. More...
 
int map_in_to_out_stream (int in_stream_idx) const
 Map input stream index to output stream index. More...
 
int add_subtitle_streams ()
 Add all subtitle streams. Already existing streams are not added again. More...
 
int seek_frame ()
 Frame sets only: perform seek to a certain frame. More...
 
int start_new_segment ()
 HLS only: start a new HLS segment. More...
 
int add_external_subtitle_streams ()
 Scan for external subtitle files. More...
 
int add_external_subtitle_stream (const std::string &subtitle_file, const std::optional< std::string > &language)
 add_external_subtitle_stream More...
 
int foreach_subtitle_file (const std::string &search_path, const std::regex &regex, int depth, const std::function< int(const std::string &, const std::optional< std::string > &)> &f)
 foreach_subititle_file More...
 
- Protected Member Functions inherited from FFmpeg_Base
void video_stream_setup (AVCodecContext *output_codec_ctx, AVStream *output_stream, AVCodecContext *input_codec_ctx, AVRational framerate, AVPixelFormat enc_hw_pix_fmt) const
 Set up a video stream. More...
 
int dict_set_with_check (AVDictionary **pm, const char *key, const char *value, int flags, const char *filename=nullptr, bool nodelete=false) const
 Call av_dict_set and check the result code. It displays an error message if appropriate. More...
 
int dict_set_with_check (AVDictionary **pm, const char *key, int64_t value, int flags, const char *filename=nullptr, bool nodelete=false) const
 Call av_dict_set and check the result code. It displays an error message if appropriate. More...
 
int opt_set_with_check (void *obj, const char *key, const char *value, int flags, const char *filename=nullptr) const
 Call av_opt_set and check result code. Displays an error message if appropriate. More...
 
void video_info (bool out_file, const AVFormatContext *format_ctx, const AVStream *stream) const
 Print data from the video stream to a log. More...
 
void audio_info (bool out_file, const AVFormatContext *format_ctx, const AVStream *stream) const
 Print data from the audio stream to log. More...
 
void subtitle_info (bool out_file, const AVFormatContext *format_ctx, const AVStream *stream) const
 Print data from the subtitle stream to log. More...
 
virtual const char * filename () const =0
 Return source filename. Must be implemented in child class. More...
 
virtual const char * destname () const =0
 Return destination filename. Must be implemented in child class. More...
 
virtual const char * virtname () const =0
 Return virtual filename. Must be implemented in child class. More...
 
uint32_t pts_to_frame (AVStream *stream, int64_t pts) const
 Convert PTS value to frame number. More...
 
int64_t frame_to_pts (AVStream *stream, uint32_t frame_no) const
 Convert frame number to PTS value. More...
 
int get_channels (const AVCodecParameters *codecpar) const
 Get the number of channels from AVCodecParameters. More...
 
void set_channels (AVCodecParameters *codecpar_out, const AVCodecParameters *codecpar_in) const
 Set the number of channels from AVCodecParameters. More...
 
int get_channels (const AVCodecContext *codec_ctx) const
 Get the number of channels from AVCodecContext. More...
 
void set_channels (AVCodecContext *codec_ctx_out, const AVCodecContext *codec_ctx_in) const
 Set the number of channels from AVCodecContext. More...
 
void set_channels (AVCodecContext *codec_ctx_out, int channels) const
 Set the number of channels from AVCodecContext. More...
 
int get_script_info (AVCodecContext *codec_ctx, int play_res_x, int play_res_y, const char *font, int font_size, int primary_color, int secondary_color, int outline_color, int back_color, int bold, int italic, int underline, int border_style, int alignment) const
 Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS. Nicked from the FFmpeg API function ff_ass_subtitle_header_full(). :) More...
 

Static Protected Member Functions

static int input_read (void *opaque, unsigned char *data, int size)
 Custom read function for FFmpeg. More...
 
static int output_write (void *opaque, const uint8_t *data, int size)
 Custom write function for FFmpeg. More...
 
static int64_t seek (void *opaque, int64_t offset, int whence)
 Custom seek function for FFmpeg. More...
 
static BITRATE get_prores_bitrate (int width, int height, const AVRational &framerate, bool interleaved, PRORESLEVEL profile)
 Calculate the appropriate bitrate for a ProRes file given several parameters. More...
 
static bool get_output_sample_rate (int input_sample_rate, int max_sample_rate, int *output_sample_rate=nullptr)
 Calculate output sample rate based on user option. More...
 
static bool get_output_bit_rate (BITRATE input_bit_rate, BITRATE max_bit_rate, BITRATE *output_bit_rate=nullptr)
 Calculate output bit rate based on user option. More...
 
static enum AVPixelFormat get_format_static (AVCodecContext *input_codec_ctx, const enum AVPixelFormat *pix_fmts)
 
static AVPixelFormat find_sw_fmt_by_hw_type (AVHWDeviceType type)
 Get the software pixel format for the given hardware acceleration. More...
 
static int read_packet (void *opaque, uint8_t *buf, int buf_size)
 FFmpeg_Transcoder::read_packet. More...
 
- Static Protected Member Functions inherited from FFmpeg_Base
static std::string get_pix_fmt_name (AVPixelFormat pix_fmt)
 Calls av_get_pix_fmt_name and returns a std::string with the pix format name. More...
 
static std::string get_sample_fmt_name (AVSampleFormat sample_fmt)
 Calls av_get_sample_fmt_name and returns a std::string with the format name. More...
 
static std::string get_channel_layout_name (const AVChannelLayout *ch_layout)
 Calls av_channel_layout_describe and returns a std::string with the channel layout. More...
 

Private Attributes

std::shared_ptr< FileIOm_fileio
 FileIO object of input file. More...
 
time_t m_mtime
 Modified time of input file. More...
 
std::recursive_mutex m_seek_to_fifo_mutex
 Access mutex for seek FIFO. More...
 
std::queue< uint32_t > m_seek_to_fifo
 Stack of seek requests. Will be processed FIFO. More...
 
std::atomic_uint32_t m_last_seek_frame_no
 If not 0, this is the last frame that we seeked to. Video sources only. More...
 
bool m_have_seeked
 After seek operations this is set to make sure the trancoding result is marked RESULTCODE_INCOMPLETE to start transcoding over next access to fill the gaps. More...
 
bool m_skip_next_frame
 After seek, skip next video frame. More...
 
bool m_is_video
 true if input is a video file More...
 
MULTIFRAME_MAP m_frame_map
 Audio/video/subtitle frame map. More...
 
AVSampleFormat m_cur_sample_fmt
 Currently selected audio sample format. More...
 
int m_cur_sample_rate
 Currently selected audio sample rate. More...
 
AVChannelLayout m_cur_ch_layout
 Currently selected audio channel layout. More...
 
SwrContext * m_audio_resample_ctx
 SwResample context for audio resampling. More...
 
AVAudioFifo * m_audio_fifo
 Audio sample FIFO. More...
 
SwsContext * m_sws_ctx
 Context for video filtering. More...
 
AVFilterContext * m_buffer_sink_context
 Video filter sink context. More...
 
AVFilterContext * m_buffer_source_context
 Video filter source context. More...
 
AVFilterGraph * m_filter_graph
 Video filter graph. More...
 
int64_t m_pts
 Generated PTS. More...
 
int64_t m_pos
 Generated position. More...
 
INPUTFILE m_in
 Input file information. More...
 
OUTPUTFILE m_out
 Output file information. More...
 
STREAM_MAP m_stream_map
 Input stream to output stream map. More...
 
uint32_t m_current_segment
 HLS only: Segment file number currently being encoded. More...
 
bool m_insert_keyframe
 HLS only: Allow insertion of 1 keyframe. More...
 
bool m_copy_audio
 If true, copy audio stream from source to target (just remux, no recode). More...
 
bool m_copy_video
 If true, copy video stream from source to target (just remux, no recode). More...
 
int64_t m_cur_audio_ts
 If the audio stream is copied and the time stamps are absent from the input stream, we have to generate them. More...
 
int64_t m_cur_video_ts
 If the video stream is copied and the time stamps are absent from the input stream, we have to generate them. More...
 
const FFmpegfs_Formatm_current_format
 Currently used output format(s) More...
 
Bufferm_buffer
 Pointer to cache buffer object. More...
 
uint32_t m_reset_pts
 We have to reset audio/video pts to the new position. More...
 
uint32_t m_fake_frame_no
 The MJEPG codec requires monotonically growing PTS values so we fake some to avoid them going backwards after seeks. More...
 
HWACCELMODE m_hwaccel_enc_mode
 Current hardware acceleration mode for encoder. More...
 
HWACCELMODE m_hwaccel_dec_mode
 Current hardware acceleration mode for decoder. More...
 
bool m_hwaccel_enable_enc_buffering
 Enable hardware acceleration frame buffers for encoder. More...
 
bool m_hwaccel_enable_dec_buffering
 Enable hardware acceleration frame buffers for decoder. More...
 
AVBufferRef * m_hwaccel_enc_device_ctx
 Hardware acceleration device context for encoder. More...
 
AVBufferRef * m_hwaccel_dec_device_ctx
 Hardware acceleration device context for decoder. More...
 
AVPixelFormat m_enc_hw_pix_fmt
 Requested encoder hardware pixel format. More...
 
AVPixelFormat m_dec_hw_pix_fmt
 Requested decoder hardware pixel format. More...
 
uint32_t m_active_stream_msk
 HLS: Currently active streams bit mask. Set FFMPEGFS_AUDIO and/or FFMPEGFS_VIDEO. More...
 
uint32_t m_inhibit_stream_msk
 HLS: Currently inhibited streams bit mask. Packets temporarly go to m_hls_packet_fifo and will be prepended to next segment. Set FFMPEGFS_AUDIO and/or FFMPEGFS_VIDEO. More...
 
std::queue< AVPacket * > m_hls_packet_fifo
 HLS packet FIFO. More...
 

Static Private Attributes

static const std::vector< PRORES_BITRATEm_prores_bitrate
 ProRes bitrate table. Used for file size prediction. More...
 
static const DEVICETYPE_MAP m_devicetype_map
 List of AVPixelFormats mapped to hardware acceleration types. More...
 
- Static Private Attributes inherited from FFmpeg_Profiles
static const PROFILE_LIST_VEC m_profile
 List of profile options. More...
 

Additional Inherited Members

- Protected Attributes inherited from FFmpeg_Base
VIRTUALFILEm_virtualfile
 Underlying virtual file object. More...
 
- Private Types inherited from FFmpeg_Profiles
typedef struct FFmpeg_Profiles::PROFILE_OPTION PROFILE_OPTION
 Profiles options. More...
 
typedef PROFILE_OPTIONLPPROFILE_OPTION
 Pointer version of PROFILE_OPTION. More...
 
typedef PROFILE_OPTION const * LPCPROFILE_OPTION
 Pointer to const version of PROFILE_OPTION. More...
 
typedef std::vector< PROFILE_OPTIONPROFILE_OPTION_VEC
 PROFILE_OPTION array. More...
 
typedef struct FFmpeg_Profiles::PROFILE_LIST PROFILE_LIST
 List of profiles. More...
 
typedef PROFILE_LISTLPPROFILE_LIST
 Pointer version of PROFILE_LIST. More...
 
typedef PROFILE_LIST const * LPCPROFILE_LIST
 Pointer to const version of PROFILE_LIST. More...
 
typedef std::vector< PROFILE_LISTPROFILE_LIST_VEC
 PROFILE_LIST array. More...
 
- Private Member Functions inherited from FFmpeg_Profiles
 FFmpeg_Profiles ()=default
 Construct a FFmpeg_Profiles object.
 
virtual ~FFmpeg_Profiles ()=default
 Destruct a FFmpeg_Profiles object.
 

Detailed Description

The FFmpeg_Transcoder class.

Definition at line 75 of file ffmpeg_transcoder.h.

Member Typedef Documentation

◆ DEVICETYPE_MAP

typedef std::map<AVHWDeviceType, AVPixelFormat> FFmpeg_Transcoder::DEVICETYPE_MAP
protected

Map device types to pixel formats.

Definition at line 178 of file ffmpeg_transcoder.h.

◆ LPCPRORES_BITRATE

Pointer to const version of PRORES_BITRATE.

Definition at line 112 of file ffmpeg_transcoder.h.

◆ MULTIFRAME

Combined audio/videoframe and subtitle.

Definition at line 187 of file ffmpeg_transcoder.h.

◆ MULTIFRAME_MAP

typedef std::multimap<int64_t, MULTIFRAME> FFmpeg_Transcoder::MULTIFRAME_MAP
protected

Audio frame/video frame/subtitle buffer.

Definition at line 188 of file ffmpeg_transcoder.h.

◆ PRORES_BITRATE

Predicted bitrates for Apple Prores, see https://www.apple.com/final-cut-pro/docs/Apple_ProRes_White_Paper.pdf.

< List of ProRes bit rates

◆ STREAM_MAP

typedef std::map<int, int> FFmpeg_Transcoder::STREAM_MAP
protected

Map input subtitle stream to output stream.

Definition at line 189 of file ffmpeg_transcoder.h.

◆ StreamRef_map

typedef std::map<int, StreamRef> FFmpeg_Transcoder::StreamRef_map
protected

Map stream index to StreamRef.

Definition at line 138 of file ffmpeg_transcoder.h.

Member Enumeration Documentation

◆ HWACCELMODE

enum class FFmpeg_Transcoder::HWACCELMODE
strongprotected

Currently active hardware acceleration mode.

<

Enumerator
NONE 

Hardware acceleration not active.

ENABLED 

Hardware acceleration is active.

FALLBACK 

Hardware acceleration selected, but fell back to software.

Definition at line 180 of file ffmpeg_transcoder.h.

Constructor & Destructor Documentation

◆ FFmpeg_Transcoder()

FFmpeg_Transcoder::FFmpeg_Transcoder ( )
explicit

◆ ~FFmpeg_Transcoder()

FFmpeg_Transcoder::~FFmpeg_Transcoder ( )
virtual

Destroy FFMPEG_Transcoder object Close and free all internal structures.

Definition at line 269 of file ffmpeg_transcoder.cc.

References closeio(), and Logging::trace().

Member Function Documentation

◆ add_albumart_frame()

int FFmpeg_Transcoder::add_albumart_frame ( AVStream *  output_stream,
AVPacket *  pkt_in 
)
protected

Add album art to stream.

Parameters
[in]output_stream- Output stream.
[in]pkt_in- Packet with album art.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2536 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), store_packet(), Logging::trace(), and virtname().

Referenced by decode_frame(), and process_albumarts().

◆ add_albumart_stream()

int FFmpeg_Transcoder::add_albumart_stream ( const AVCodecContext *  input_codec_ctx)
protected

◆ add_external_subtitle_stream()

int FFmpeg_Transcoder::add_external_subtitle_stream ( const std::string &  subtitle_file,
const std::optional< std::string > &  language 
)
protected

add_external_subtitle_stream

Parameters
[in]subtitle_file- Name of subtitle fule
[in]language- Language or subtitle file, or std::nullopt if unknown.
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 7636 of file ffmpeg_transcoder.cc.

References add_subtitle_stream(), Logging::debug(), decode_subtitle(), Logging::error(), ffmpeg_geterror(), filename(), INVALID_STREAM, m_current_format, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, open_bestmatch_decoder(), FFmpeg_Transcoder::BUFFER_DATA::ptr, read_packet(), FFmpeg_Transcoder::StreamRef::set_codec_ctx(), FFmpeg_Transcoder::BUFFER_DATA::size, and FFmpegfs_Format::subtitle_codec().

◆ add_external_subtitle_streams()

int FFmpeg_Transcoder::add_external_subtitle_streams ( )
protected

Scan for external subtitle files.

Returns
0 on success, a negative AVERROR code on failure.

Definition at line 7778 of file ffmpeg_transcoder.cc.

References Logging::error(), filename(), foreach_subtitle_file(), and regex_escape().

Referenced by open_output_filestreams().

◆ add_samples_to_fifo()

int FFmpeg_Transcoder::add_samples_to_fifo ( uint8_t **  converted_input_samples,
int  frame_size 
)
protected

Add converted input audio samples to the FIFO buffer for later processing.

Parameters
[in]converted_input_samples- Samples to add.
[in]frame_size- Frame size
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3986 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), m_audio_fifo, and virtname().

Referenced by decode_audio_frame().

◆ add_stream()

int FFmpeg_Transcoder::add_stream ( AVCodecID  codec_id)
protected

Add new stream to output file.

Parameters
[in]codec_id- Codec for this stream.
Returns
On success, returns index of new stream [0...n]; On error, negative AVERROR value.
Todo:
FFmpeg actually can transcode while presevering the SAR. FFmpegfs rescales to fix that problem. Need to find out what I am doing wrong here...

Definition at line 1631 of file ffmpeg_transcoder.cc.

References add_stream_map(), BITRATE, Logging::debug(), FFmpeg_Base::dict_set_with_check(), ENABLED, Logging::error(), FALLBACK, ffmpeg_rescale_q(), format_bitrate(), format_samplerate(), FFmpeg_Base::get_channels(), get_hw_encoder_name(), get_hw_pix_fmt(), get_hwaccel_API_text(), get_output_bit_rate(), get_output_sample_rate(), get_video_size(), hwframe_ctx_set(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFMPEGFS_PARAMS::m_audiobitrate, FFMPEGFS_PARAMS::m_audiochannels, FFMPEGFS_PARAMS::m_audiosamplerate, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_current_format, m_enc_hw_pix_fmt, FFmpeg_Transcoder::INPUTFILE::m_filetype, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_hwaccel_enable_enc_buffering, FFMPEGFS_PARAMS::m_hwaccel_enc_API, m_hwaccel_enc_device_ctx, FFMPEGFS_PARAMS::m_hwaccel_enc_device_type, m_hwaccel_enc_mode, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, FFMPEGFS_PARAMS::m_videobitrate, params, FFmpegfs_Format::sample_format(), FFmpeg_Base::set_channels(), FFmpeg_Transcoder::StreamRef::set_codec_ctx(), Logging::trace(), FFmpeg_Base::video_stream_setup(), and virtname().

Referenced by open_output_filestreams().

◆ add_stream_copy()

int FFmpeg_Transcoder::add_stream_copy ( AVCodecID  codec_id,
AVMediaType  codec_type 
)
protected

Add new stream copy to output file.

Parameters
[in]codec_id- Codec for this stream.
[in]codec_type- Codec type: audio or video.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2349 of file ffmpeg_transcoder.cc.

References add_stream_map(), Logging::error(), ffmpeg_geterror(), ffmpeg_rescale_q(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, print_stream_info(), FFmpeg_Transcoder::StreamRef::set_codec_ctx(), and virtname().

Referenced by open_output_filestreams().

◆ add_stream_map()

void FFmpeg_Transcoder::add_stream_map ( int  in_stream_idx,
int  out_stream_idx 
)
protected

Add entry to input stream to output stream map.

Parameters
[in]in_stream_idx- Index of input stream
[in]out_stream_idx- Index of output stream

Definition at line 7550 of file ffmpeg_transcoder.cc.

References INVALID_STREAM, and m_stream_map.

Referenced by add_stream(), add_stream_copy(), and add_subtitle_stream().

◆ add_subtitle_stream()

int FFmpeg_Transcoder::add_subtitle_stream ( AVCodecID  codec_id,
StreamRef input_streamref,
const std::optional< std::string > &  language = std::nullopt 
)
protected

◆ add_subtitle_streams()

int FFmpeg_Transcoder::add_subtitle_streams ( )
protected

Add all subtitle streams. Already existing streams are not added again.

Returns
0 on success, a negative AVERROR code on failure.

Definition at line 2733 of file ffmpeg_transcoder.cc.

References add_subtitle_stream(), INVALID_STREAM, m_current_format, m_in, FFmpeg_Transcoder::INPUTFILE::m_subtitle, map_in_to_out_stream(), and FFmpegfs_Format::subtitle_codec().

Referenced by open_output_filestreams().

◆ alloc_picture()

int FFmpeg_Transcoder::alloc_picture ( AVFrame *  frame,
AVPixelFormat  pix_fmt,
int  width,
int  height 
) const
protected

Allocate memory for one picture.

Parameters
[in]frame- Frame to prepare
[in]pix_fmt- Pixel format
[in]width- Picture width
[in]height- Picture height
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3149 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), and virtname().

Referenced by decode_video_frame().

◆ audio_size()

bool FFmpeg_Transcoder::audio_size ( size_t *  filesize,
AVCodecID  codec_id,
BITRATE  bit_rate,
int64_t  duration,
int  channels,
int  sample_rate,
AVSampleFormat  sample_format 
)
static

Predict audio file size. This may (better will surely) be inaccurate.

Parameters
[out]filesize- Predicted file size in bytes, including audio stream size.
[in]codec_id- Target codec ID.
[in]bit_rate- Target bit rate.
[in]duration- File duration.
[in]channels- Number of channels in target file.
[in]sample_rate- Sample rate of target file.
[in]sample_format- Selected sample format
Returns
On success, returns true; on failure, returns false.

Definition at line 5734 of file ffmpeg_transcoder.cc.

References BITRATE, duration(), get_output_bit_rate(), get_output_sample_rate(), ID3V1_TAG_LENGTH, FFMPEGFS_PARAMS::m_audiobitrate, FFMPEGFS_PARAMS::m_audiosamplerate, and params.

Referenced by calculate_predicted_filesize(), and transcoder_set_filesize().

◆ calculate_predicted_filesize()

size_t FFmpeg_Transcoder::calculate_predicted_filesize ( ) const
protected

◆ can_copy_stream()

bool FFmpeg_Transcoder::can_copy_stream ( const AVStream *  stream) const
protected

Check if stream can be copied from input to output (AUTOCOPY option).

Parameters
[in]stream- Input stream to check.
Returns
Returns true if stream can be copied; false if not.

Definition at line 784 of file ffmpeg_transcoder.cc.

References FFmpegfs_Format::audio_codec(), BITRATE, Logging::debug(), get_output_bit_rate(), Logging::info(), FFMPEGFS_PARAMS::m_audiobitrate, FFMPEGFS_PARAMS::m_autocopy, m_current_format, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, FFMPEGFS_PARAMS::m_videobitrate, MATCH, MATCHLIMIT, OFF, params, STRICT, STRICTLIMIT, FFmpegfs_Format::video_codec(), and virtname().

Referenced by open_output_filestreams().

◆ close_input_file()

bool FFmpeg_Transcoder::close_input_file ( )

Closes the input file if open. Can safely be called again after the file was already closed or if the file was never open.

Returns
Returns true if anything has been closed; false if not.

Definition at line 6435 of file ffmpeg_transcoder.cc.

References free_filters(), FFmpeg_Transcoder::INPUTFILE::m_album_art, FFmpeg_Transcoder::INPUTFILE::m_audio, m_fileio, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, FFmpeg_Transcoder::INPUTFILE::m_subtitle, FFmpeg_Transcoder::INPUTFILE::m_video, and FFmpeg_Transcoder::StreamRef::reset().

Referenced by closeio().

◆ close_output_file()

bool FFmpeg_Transcoder::close_output_file ( )

Closes the output file if open and reports lost packets. Can safely be called again after the file was already closed or if the file was never open.

Returns
Returns true if anything has been closed; false if not.

Definition at line 6392 of file ffmpeg_transcoder.cc.

References close_resample(), FFmpeg_Transcoder::INPUTFILE::m_album_art, FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_out, FFmpeg_Transcoder::INPUTFILE::m_subtitle, m_sws_ctx, FFmpeg_Transcoder::INPUTFILE::m_video, purge(), and FFmpeg_Transcoder::StreamRef::reset().

Referenced by closeio(), open_output(), and start_new_segment().

◆ close_resample()

bool FFmpeg_Transcoder::close_resample ( )
protected

Close and free the resampler context.

Returns
If an open context was closed, returns true; if nothing had been done returns false.

Definition at line 6309 of file ffmpeg_transcoder.cc.

References m_audio_resample_ctx.

Referenced by close_output_file(), and init_resampler().

◆ closeio()

void FFmpeg_Transcoder::closeio ( )

◆ convert_samples()

int FFmpeg_Transcoder::convert_samples ( uint8_t **  input_data,
int  in_samples,
uint8_t **  converted_data,
int *  out_samples 
)
protected

Convert the input audio samples into the output sample format. The conversion happens on a per-frame basis, the size of which is specified by frame_size.

Parameters
[in]input_data- Input data.
[in]in_samples- Number of input samples.
[out]converted_data- Converted data.
[out]out_samples- Number of output samples
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3946 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), FFmpeg_Base::get_channels(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_audio_resample_ctx, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_in, m_out, and virtname().

Referenced by decode_audio_frame().

◆ copy_audio_to_frame_buffer()

int FFmpeg_Transcoder::copy_audio_to_frame_buffer ( int *  finished)
protected

Copy data from audio FIFO to frame buffer. Divides WAV data into proper chunks to be fed into the encoder.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 5187 of file ffmpeg_transcoder.cc.

References AV_CODEC_CAP_VARIABLE_FRAME_SIZE, create_audio_frame(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_audio_fifo, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_out, and read_decode_convert_and_store().

Referenced by process_single_fr().

◆ copy_metadata()

void FFmpeg_Transcoder::copy_metadata ( AVDictionary **  metadata_out,
const AVDictionary *  metadata_in,
bool  contentstream = true 
)
protected

Process the metadata in the FFmpeg file. This should be called at the beginning, before reading audio data. The set_text_tag() and set_picture_tag() methods of the given Encoder will be used to set the metadata, with results going into the given Buffer. This function will also read the actual PCM stream parameters.

Parameters
[in]metadata_out- Dictionary of output file. Metadata will be copied into it.
[in]metadata_in- Dictionary of input file. Metadata will be copied out of it.
[in]contentstream- True if this is a content stream, i.e, audio or video. False for album arts or sub titles.

Definition at line 4901 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::dict_set_with_check(), VIRTUALFILE::CUESHEET_TRACK::m_artist, VIRTUALFILE::m_cuesheet_track, FFmpeg_Transcoder::INPUTFILE::m_filetype, VIRTUALFILE::m_flags, m_out, VIRTUALFILE::CUESHEET_TRACK::m_title, VIRTUALFILE::CUESHEET_TRACK::m_trackno, FFmpeg_Base::m_virtualfile, strcasecmp(), strsprintf(), virtname(), and VIRTUALFLAG_CUESHEET.

Referenced by process_metadata().

◆ create_audio_frame()

int FFmpeg_Transcoder::create_audio_frame ( int  frame_size)
protected

Load one audio frame from the FIFO buffer and store in frame buffer.

Parameters
[in]frame_size- Size of frame.
Returns
On success, returns 0. On error, returns a negative AVERROR value.

Definition at line 4787 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), ffmpeg_rescale_q(), ffmpeg_rescale_q_rnd(), init_audio_output_frame(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_audio_fifo, FFmpeg_Transcoder::OUTPUTFILE::m_audio_pts, m_frame_map, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_start_time, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Frame::res(), and virtname().

Referenced by copy_audio_to_frame_buffer().

◆ create_fake_aiff_header()

int FFmpeg_Transcoder::create_fake_aiff_header ( ) const
protected

Create a fake AIFF header Create a fake AIFF header. Inserts predicted file sizes to allow playback to start directly.

Returns
0 on success, a negative AVERROR code on failure.

Definition at line 3051 of file ffmpeg_transcoder.cc.

References AIFF_COMMONID, AIFF_FORMID, AIFF_SOUNDATAID, AIFF_FORMCHUNK::m_ckSize, AIFF_SOUNDDATACHUNK::m_ckSize, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_out, predicted_filesize(), read_aiff_chunk(), Buffer::seek(), Buffer::tell(), and Buffer::writeio().

◆ create_fake_wav_header()

int FFmpeg_Transcoder::create_fake_wav_header ( ) const
protected

Create a fake WAV header Create a fake WAV header. Inserts predicted file sizes to allow playback to start directly.

Returns
0 on success, a negative AVERROR code on failure.

Definition at line 2948 of file ffmpeg_transcoder.cc.

References Buffer::copy(), WAV_HEADER::m_audio_format, WAV_FACT::m_chunk_id, WAV_LIST_HEADER::m_data_bytes, WAV_DATA_HEADER::m_data_bytes, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_out, WAV_HEADER::m_wav_size, predicted_filesize(), Buffer::seek(), Buffer::tell(), and Buffer::writeio().

◆ decode()

int FFmpeg_Transcoder::decode ( AVCodecContext *  codec_ctx,
AVFrame *  frame,
int *  got_frame,
const AVPacket *  pkt 
) const
protected

This does not quite work like avcodec_decode_audio4/avcodec_decode_video2. There is the following difference: if you got a frame, you must call it again with pkt=nullptr. pkt==nullptr is treated differently from pkt->size==0 (pkt==nullptr means get more output, pkt->size==0 is a flush/drain packet)

Parameters
[in]codec_ctx- AVCodecContext of input stream.
[in]frame- Decoded frame
[out]got_frame- 1 if a frame was decoded, 0 if not
[in]pkt- Packet to decode
Returns
On success, returns 0. On error, returns a negative AVERROR value.
Note
Only after the first hardware decoded video packet arrived we have a
hardware frame context.
We should create the output stream now, open a codec etc. and call
hwframe_ctx_set.

Definition at line 3168 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), ffmpeg_rescale_q(), filename(), is_audio_stream(), is_video_stream(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, and stream_exists().

Referenced by decode_audio_frame(), and decode_video_frame().

◆ decode_audio_frame()

int FFmpeg_Transcoder::decode_audio_frame ( AVPacket *  pkt,
int *  decoded 
)
protected

Decode one audio frame.

Parameters
[in]pkt- Packet to decode.
[in]decoded- 1 if packet was decoded, 0 if it did not contain data.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3222 of file ffmpeg_transcoder.cc.

References add_samples_to_fifo(), convert_samples(), decode(), Logging::error(), ffmpeg_geterror(), filename(), init_converted_samples(), init_resampler(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_audio_resample_ctx, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream_idx, and FFmpeg_Frame::res().

Referenced by decode_frame(), and flush_frames_single().

◆ decode_frame()

int FFmpeg_Transcoder::decode_frame ( AVPacket *  pkt)
protected

Decode one frame.

Parameters
[in]pkt- Packet to decode.
Returns
On success, returns 0; on error, a negative AVERROR value.
Todo:
Calling decode_video_frame until all data has been used, but for DVDs only. Can someone tell me why this seems required??? If this is not done some videos become garbled. But only for DVDs...

With fix: all DVDs OK, some Blurays (e.g. Phil Collins) not...
With fix: all DVDs shitty, but Blurays OK.

Applying fix for DVDs only.

Definition at line 3745 of file ffmpeg_transcoder.cc.

References add_albumart_frame(), Logging::debug(), decode_audio_frame(), decode_subtitle(), decode_video_frame(), DVD, Logging::error(), ffmpeg_geterror(), ffmpeg_rescale_q(), FFMPEGFS_AUDIO, FFMPEGFS_VIDEO, filename(), format_duration(), is_audio_stream(), is_frameset(), is_subtitle_stream(), is_video_stream(), FFmpeg_Transcoder::INPUTFILE::m_album_art, FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::OUTPUTFILE::m_audio_pts, m_copy_audio, m_copy_video, m_cur_audio_ts, m_cur_video_ts, FFMPEGFS_PARAMS::m_decoding_errors, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, m_out, m_reset_pts, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, VIRTUALFILE::m_type, FFmpeg_Transcoder::INPUTFILE::m_video, FFmpeg_Transcoder::OUTPUTFILE::m_video_pts, FFmpeg_Base::m_virtualfile, make_pts(), params, store_packet(), stream_exists(), and virtname().

Referenced by read_decode_convert_and_store().

◆ decode_subtitle() [1/2]

int FFmpeg_Transcoder::decode_subtitle ( AVCodecContext *  codec_ctx,
AVPacket *  pkt,
int *  decoded,
int  out_stream_idx 
)
protected

Decode one subtitle.

Parameters
[in]codec_ctx- AVCodecContext object of output codec context.
[in]pkt- Packet to decode.
[in]decoded- 1 if packet was decoded, 0 if it did not contain data.
[in]out_stream_idx- Output stream index.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3574 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), filename(), m_frame_map, and FFmpeg_Subtitle::res().

◆ decode_subtitle() [2/2]

int FFmpeg_Transcoder::decode_subtitle ( AVPacket *  pkt,
int *  decoded 
)
protected

Decode one subtitle.

Parameters
[in]pkt- Packet to decode.
[in]decoded- 1 if packet was decoded, 0 if it did not contain data.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3542 of file ffmpeg_transcoder.cc.

References decode_subtitle(), Logging::error(), ffmpeg_geterror(), filename(), INVALID_STREAM, m_in, FFmpeg_Transcoder::INPUTFILE::m_subtitle, map_in_to_out_stream(), and virtname().

Referenced by add_external_subtitle_stream(), decode_frame(), and decode_subtitle().

◆ decode_video_frame()

int FFmpeg_Transcoder::decode_video_frame ( AVPacket *  pkt,
int *  decoded 
)
protected

◆ destname()

const char * FFmpeg_Transcoder::destname ( ) const
overridevirtual

Return destination filename.

Returns
Returns filename.

Implements FFmpeg_Base.

Definition at line 6502 of file ffmpeg_transcoder.cc.

References VIRTUALFILE::m_destfile, and FFmpeg_Base::m_virtualfile.

◆ do_seek_frame()

int FFmpeg_Transcoder::do_seek_frame ( uint32_t  frame_no)
protected

Actually perform seek for frame. This function ensures that it is positioned at a key frame, so the resulting position may be different from the requested. If e.g. frame no. 24 is a key frame, and frame_no is set to 28, the actual position will be at frame 24.

Parameters
[in]frame_no- Frame number 1...n to seek to.
Returns
Returns 0 if OK, or negative AVERROR value.

Definition at line 5046 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::frame_to_pts(), FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_have_seeked, m_in, m_skip_next_frame, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, and FFmpeg_Transcoder::INPUTFILE::m_video.

Referenced by skip_decoded_frames().

◆ duration()

int64_t FFmpeg_Transcoder::duration ( ) const

Get the file duration.

Returns
Returns the duration of the file in AV_TIME_BASE units.

Definition at line 6159 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::m_virtualfile, and SAFE_VALUE.

Referenced by audio_size(), transcode(), transcode_finish(), transcoder_predict_filesize(), and video_size().

◆ encode_audio_frame()

int FFmpeg_Transcoder::encode_audio_frame ( const AVFrame *  frame,
int *  data_present 
)
protected

Create one frame worth of audio to the output file.

Parameters
[in]frame- Audio frame to encode
[in]data_present- 1 if frame contained data that could be encoded, 0 if not.
Returns
On success, returns 0. On error, returns a negative AVERROR value.

Definition at line 4256 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_out, FFmpeg_Transcoder::StreamRef::m_stream_idx, produce_audio_dts(), store_packet(), and virtname().

Referenced by flush_delayed_audio(), and process_single_fr().

◆ encode_finish()

int FFmpeg_Transcoder::encode_finish ( )

Encode any remaining PCM data to the given buffer. This should be called after all input data has already been passed to encode_pcm_data().

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 6179 of file ffmpeg_transcoder.cc.

References Buffer::buffer_watermark(), FFMPEGFS_PARAMS::current_format(), FFmpegfs_Format::fileext(), find_file(), Buffer::finished_segment(), is_frameset(), is_hls(), m_buffer, m_current_segment, VIRTUALFILE::m_destfile, VIRTUALFILE::m_predicted_size, VIRTUALFILE::m_st, FFmpeg_Base::m_virtualfile, make_filename(), params, stat_set_size(), FileIO::virtualfile(), and write_output_file_trailer().

Referenced by start_new_segment(), and transcode_finish().

◆ encode_image_frame()

int FFmpeg_Transcoder::encode_image_frame ( const AVFrame *  frame,
int *  data_present 
)
protected

◆ encode_subtitle()

int FFmpeg_Transcoder::encode_subtitle ( const AVSubtitle *  sub,
int  out_stream_idx,
int *  data_present 
)
protected

Encode one subtitle frame to the output file.

Parameters
[in]sub- Subtitle frame to encode
[in]out_stream_idx- Index of stream to encode to.
[in]data_present- 1 if frame contained data that could be encoded, 0 if not.
Returns
On success, returns 0. On error, returns a negative AVERROR value.

Definition at line 4649 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), ffmpeg_rescale_q(), get_out_subtitle_stream(), FFmpeg_Transcoder::StreamRef::m_codec_ctx, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, store_packet(), and virtname().

Referenced by process_single_fr().

◆ encode_video_frame()

int FFmpeg_Transcoder::encode_video_frame ( const AVFrame *  frame,
int *  data_present 
)
protected

◆ filename()

const char * FFmpeg_Transcoder::filename ( ) const
overridevirtual

◆ find_sw_fmt_by_hw_type()

AVPixelFormat FFmpeg_Transcoder::find_sw_fmt_by_hw_type ( AVHWDeviceType  type)
staticprotected

Get the software pixel format for the given hardware acceleration.

Parameters
[in]type- Selected hardware acceleration.
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 7459 of file ffmpeg_transcoder.cc.

References m_devicetype_map.

Referenced by get_pix_formats(), and hwframe_ctx_set().

◆ flush_buffers()

void FFmpeg_Transcoder::flush_buffers ( )

◆ flush_delayed_audio()

int FFmpeg_Transcoder::flush_delayed_audio ( )

◆ flush_delayed_subtitles()

int FFmpeg_Transcoder::flush_delayed_subtitles ( )

Flush delayed subtitle packets, if there are any.

Todo:
Implement flush_delayed_subtitles() if required

Definition at line 5182 of file ffmpeg_transcoder.cc.

Referenced by process_single_fr().

◆ flush_delayed_video()

int FFmpeg_Transcoder::flush_delayed_video ( )

◆ flush_frames_all()

int FFmpeg_Transcoder::flush_frames_all ( bool  use_flush_packet)
protected

Flush the remaining frames for all streams.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 4018 of file ffmpeg_transcoder.cc.

References flush_frames_single(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_in, FFmpeg_Transcoder::StreamRef::m_stream_idx, and FFmpeg_Transcoder::INPUTFILE::m_video.

Referenced by read_decode_convert_and_store().

◆ flush_frames_single()

int FFmpeg_Transcoder::flush_frames_single ( int  stream_idx,
bool  use_flush_packet 
)
protected

Flush the remaining frames.

Parameters
[in]stream_idx- Stream index to flush.
[in]use_flush_packet- If true, use flush packet. Otherwise pass nullptr to avcodec_receive_frame.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 4043 of file ffmpeg_transcoder.cc.

References decode_audio_frame(), decode_video_frame(), is_audio_stream(), is_frameset(), is_video_stream(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_copy_audio, m_copy_video, m_out, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, and stream_exists().

Referenced by flush_frames_all().

◆ foreach_subtitle_file()

int FFmpeg_Transcoder::foreach_subtitle_file ( const std::string &  search_path,
const std::regex &  regex,
int  depth,
const std::function< int(const std::string &, const std::optional< std::string > &)> &  f 
)
inlineprotected

foreach_subititle_file

Parameters
[in]search_path- Directory with subtitle files
[in]regex- Regular expression to select subtitle files
[in]depth- Recursively scan for subtitles, should be 0.
[in]f- Funtion to be called for each file found
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 7570 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), filename(), and foreach_subtitle_file().

Referenced by add_external_subtitle_streams(), and foreach_subtitle_file().

◆ free_filters()

void FFmpeg_Transcoder::free_filters ( )
protected

Free filter sinks.

Definition at line 6712 of file ffmpeg_transcoder.cc.

References m_buffer_sink_context, m_buffer_source_context, and m_filter_graph.

Referenced by close_input_file().

◆ get_aspect_ratio()

bool FFmpeg_Transcoder::get_aspect_ratio ( int  width,
int  height,
const AVRational &  sar,
AVRational *  ar 
) const
protected

Calculate aspect ratio for width/height and sample aspect ratio (sar).

Parameters
[in]width- Video width in pixels.
[in]height- Video height in pixels.
[in]sar- Aspect ratio of input video.
[in]ar- Calulcated aspect ratio, if computeable.
Returns
On success, returns true; if false is returned ar may not be used.

Definition at line 1457 of file ffmpeg_transcoder.cc.

Referenced by get_video_size().

◆ get_format()

enum AVPixelFormat FFmpeg_Transcoder::get_format ( AVCodecContext *  input_codec_ctx,
const enum AVPixelFormat *  pix_fmts 
) const
protected

Callback to negotiate the pixelFormat

Parameters
[in]input_codec_ctx- Input codec context
[in]pix_fmtsis the list of formats which are supported by the codec, it is terminated by -1 as 0 is a valid format, the formats are ordered by quality. The first is always the native one.
Note
The callback may be called again immediately if initialization for the selected (hardware-accelerated) pixel format failed.
Warning
Behavior is undefined if the callback returns a value not in the fmt list of formats.
Returns
the chosen format
  • encoding: unused
  • decoding: Set by user, if not set the native format will be chosen.

Definition at line 6813 of file ffmpeg_transcoder.cc.

References Logging::error(), filename(), FFmpeg_Base::get_pix_fmt_name(), m_dec_hw_pix_fmt, FFMPEGFS_PARAMS::m_hwaccel_dec_device_type, and params.

Referenced by get_format_static().

◆ get_format_static()

enum AVPixelFormat FFmpeg_Transcoder::get_format_static ( AVCodecContext *  input_codec_ctx,
const enum AVPixelFormat *  pix_fmts 
)
staticprotected

Callback to negotiate the pixelFormat

Parameters
[in]input_codec_ctx- Input codec context
[in]pix_fmtsis the list of formats which are supported by the codec, it is terminated by -1 as 0 is a valid format, the formats are ordered by quality. The first is always the native one.
Note
The callback may be called again immediately if initialization for the selected (hardware-accelerated) pixel format failed.
Warning
Behavior is undefined if the callback returns a value not in the fmt list of formats.
Returns
the chosen format
  • encoding: unused
  • decoding: Set by user, if not set the native format will be chosen.

Definition at line 6807 of file ffmpeg_transcoder.cc.

References get_format().

Referenced by hwdevice_ctx_add_ref().

◆ get_hw_decoder_name()

int FFmpeg_Transcoder::get_hw_decoder_name ( AVCodecID  codec_id,
std::string *  codec_name = nullptr 
) const
protected

Get the hardware codec name as string. This is required, because e.g. the name for the software codec is libx264, but for hardware it is h264_vaapi under VAAPI.

Parameters
[in]codec_id- Id of encoder/decoder codec
[out]codec_name- Returns the name of the codec, may be nullptr if not requitred.
Returns
0 on success, a negative AVERROR code on failure.
Todo:
HWACCEL - Supported formats

Tested and working: VA-API, MMAL and OMX.

Some VA-API formats do not yet work, see "fixit"

V4LM2M: implemented, but untested NIVIDA/CUDA: implemented, but untested

Definition at line 7060 of file ffmpeg_transcoder.cc.

References get_hw_mmal_decoder_name(), get_hw_vaapi_codec_name(), FFMPEGFS_PARAMS::m_hwaccel_dec_API, MMAL, params, and VAAPI.

◆ get_hw_encoder_name()

int FFmpeg_Transcoder::get_hw_encoder_name ( AVCodecID  codec_id,
std::string *  codec_name = nullptr 
) const
protected

Get the hardware codec name as string. This is required, because e.g. the name for the software codec is libx264, but for hardware it is h264_vaapi under VAAPI.

Parameters
[in]codec_id- Id of encoder/decoder codec
[out]codec_name- Returns the name of the codec, may be nullptr if not requitred.
Returns
0 on success, AVERROR_DECODER_NOT_FOUND if no codec available.

Definition at line 7105 of file ffmpeg_transcoder.cc.

References get_hw_omx_encoder_name(), get_hw_vaapi_codec_name(), FFMPEGFS_PARAMS::m_hwaccel_enc_API, OMX, params, and VAAPI.

Referenced by add_stream(), and open_bestmatch_video().

◆ get_hw_mmal_decoder_name()

int FFmpeg_Transcoder::get_hw_mmal_decoder_name ( AVCodecID  codec_id,
std::string *  codec_name 
) const
protected

Determine MMAL decoder codec name.

Parameters
[in]codec_id- Id of encoder/decoder codec
[out]codec_name- Name of the codec.
Returns
0 on success, AVERROR_DECODER_NOT_FOUND if no codec available.

*** MMAL decoder ***

h264_mmal h264 (mmal) (codec h264) mpeg2_mmal mpeg2 (mmal) (codec mpeg2video) mpeg4_mmal mpeg4 (mmal) (codec mpeg4) vc1_mmal vc1 (mmal) (codec vc1)

Todo:
mmal MPEG1 hardware acceleration not working. Probably because I have not bought a key...

INFO : [/root/test/in/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).mpeg] Transcoding to ts.
INFO : [/root/test/in/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).mpeg] Hardware decoder acceleration active using codec 'mpeg2_mmal'.
INFO : [/root/test/in/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).mpeg] Hardware decoder acceleration enabled. Codec 'mpeg2_mmal'.
mmal: mmal_vc_port_info_set: failed to set port info (2:0): EINVAL
mmal: mmal_vc_port_set_format: mmal_vc_port_info_set failed 0x6b985440 (EINVAL)
mmal: mmal_port_disable: port vc.ril.video_decode:in:0(MP2V)(0x6b985440) is not enabled
mmal: mmal_port_disable: port vc.ril.video_decode:out:0(0x6b985890) is not enabled
mmal: mmal_port_disable: port vc.ril.video_decode:ctr:0(0x6b94db90) is not enabled
ERROR : [/root/test/in/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).mpeg] Failed to open video input codec for stream video (error '0').
ERROR : [/root/test/in/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).mpeg] Failed to open video codec (error 'Unknown error occurred').
ERROR : [/root/test/out/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).ts] Transcoding exited with error.
ERROR : [/root/test/out/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).ts] System error: (5) Input/output error
ERROR : [/root/test/out/En Vogue - Don-t Let Go (Love) (Official Music Video) (mpeg2).ts] FFMpeg error: (-1313558101) Unknown error occurred
Todo:
mmal VC1 hardware acceleration not working. Probably because I have not bought a key...

INFO : [/root/test/in/Test_1440x576_WVC1_6Mbps.wmv] Hardware decoder acceleration active using codec 'vc1_mmal'.
INFO : [/root/test/in/Test_1440x576_WVC1_6Mbps.wmv] Hardware decoder acceleration enabled. Codec 'vc1_mmal'.
mmal: mmal_vc_port_info_set: failed to set port info (2:0): EINVAL
mmal: mmal_vc_port_set_format: mmal_vc_port_info_set failed 0x6e54c560 (EINVAL)
mmal: mmal_port_disable: port vc.ril.video_decode:in:0(WVC1)(0x6e54c560) is not enabled
mmal: mmal_port_disable: port vc.ril.video_decode:out:0(0x6e546660) is not enabled
mmal: mmal_port_disable: port vc.ril.video_decode:ctr:0(0x6e54c240) is not enabled
ERROR : [/root/test/in/Test_1440x576_WVC1_6Mbps.wmv] Failed to open video input codec for stream video (error '0').
ERROR : [/root/test/in/Test_1440x576_WVC1_6Mbps.wmv] Failed to open video codec (error 'Unknown error occurred').

Definition at line 7238 of file ffmpeg_transcoder.cc.

Referenced by get_hw_decoder_name().

◆ get_hw_omx_encoder_name()

int FFmpeg_Transcoder::get_hw_omx_encoder_name ( AVCodecID  codec_id,
std::string *  codec_name 
) const
protected

Determine OMX encoder codec name.

Parameters
[in]codec_id- Id of encoder/decoder codec
[out]codec_name- Name of the codec.
Returns
0 on success, AVERROR_DECODER_NOT_FOUND if no codec available.

*** Openmax encoder ***

h264_omx OpenMAX IL H.264 video encoder (codec h264)

Definition at line 7385 of file ffmpeg_transcoder.cc.

Referenced by get_hw_encoder_name().

◆ get_hw_pix_fmt()

AVPixelFormat FFmpeg_Transcoder::get_hw_pix_fmt ( const AVCodec *  codec,
AVHWDeviceType  dev_type,
bool  use_device_ctx 
) const
protected

Determine the hardware pixel format for the codec, if applicable.

Parameters
[in]codec- Input codec used
[in]dev_type- Hardware device type
[in]use_device_ctx- If true checks for pix format if using a hardware device context, for a pix format using a hardware frames context otherwise.
Returns
Returns hardware pixel format, or AV_PIX_FMT_NONE if not applicable.

Definition at line 917 of file ffmpeg_transcoder.cc.

References Logging::debug(), Logging::error(), and hwdevice_get_type_name().

Referenced by add_stream().

◆ get_hw_v4l2m2m_encoder_name()

int FFmpeg_Transcoder::get_hw_v4l2m2m_encoder_name ( AVCodecID  codec_id,
std::string *  codec_name 
) const
protected

Determine video for linux encoder codec name.

Parameters
[in]codec_id- Id of encoder/decoder codec
[out]codec_name- Name of the codec.
Returns
0 on success, AVERROR_DECODER_NOT_FOUND if no codec available.
  • *** v4l2m2m (Video2linux) encoder ***

h263_v4l2m2m V4L2 mem2mem H.263 encoder wrapper (codec h263) h264_v4l2m2m V4L2 mem2mem H.264 encoder wrapper (codec h264) hevc_v4l2m2m V4L2 mem2mem HEVC encoder wrapper (codec hevc) mpeg4_v4l2m2m V4L2 mem2mem MPEG4 encoder wrapper (codec mpeg4) vp8_v4l2m2m V4L2 mem2mem VP8 encoder wrapper (codec vp8)

Definition at line 7410 of file ffmpeg_transcoder.cc.

◆ get_hw_vaapi_codec_name()

int FFmpeg_Transcoder::get_hw_vaapi_codec_name ( AVCodecID  codec_id,
std::string *  codec_name 
) const
protected

Determine VAAPI codec name.

Parameters
[in]codec_id- Id of encoder/decoder codec
[out]codec_name- Name of the codec.
Returns
0 on success, AVERROR_DECODER_NOT_FOUND if no codec available.

*** Intel VAAPI de/encoder ***

h264_vaapi H.264/AVC (VAAPI) (codec h264) hevc_vaapi H.265/HEVC (VAAPI) (codec hevc) mjpeg_vaapi MJPEG (VAAPI) (codec mjpeg) mpeg2_vaapi MPEG-2 (VAAPI) (codec mpeg2video) vp1_vaapi VC1 (VAAPI) (codec vc1) seems to be possible on my hardware vp8_vaapi VP8 (VAAPI) (codec vp8) vp9_vaapi VP9 (VAAPI) (codec vp9)

Todo:
fixit, MPEG-1 decoding does not work...

Program terminated with signal SIGSEGV, Segmentation fault. #0 __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:383 383 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: Datei oder Verzeichnis nicht gefunden. [Current thread is 1 (Thread 0x7f95a24d4700 (LWP 16179))] (gdb) bt #0 __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:383 #1 0x00007f95903c4e26 in ?? () from /usr/lib/x86_64-linux-gnu/dri/iHD_drv_video.so #2 0x00007f95aaae80b8 in vaGetImage () from /lib/x86_64-linux-gnu/libva.so.2 #3 0x00007f95af524bb5 in ?? () from /lib/x86_64-linux-gnu/libavutil.so.56 #4 0x00007f95af5250fb in ?? () from /lib/x86_64-linux-gnu/libavutil.so.56 #5 0x00007f95af51c37f in av_hwframe_transfer_data () from /lib/x86_64-linux-gnu/libavutil.so.56 #6 0x00007f95af51c406 in av_hwframe_transfer_data () from /lib/x86_64-linux-gnu/libavutil.so.56 #7 0x0000555da4fde146 in FFmpeg_Transcoder::decode_video_frame (this=0x7f9598002e90, pkt=0x7f95a24d2f90, decoded=0x7f95a24d2ec4) at ffmpeg_transcoder.cc:2655 #8 0x0000555da4fde5cd in FFmpeg_Transcoder::decode_frame (this=0x7f9598002e90, pkt=0x7f95a24d2f90) at ffmpeg_transcoder.cc:2852 #9 0x0000555da4fdea4b in FFmpeg_Transcoder::read_decode_convert_and_store (this=0x7f9598002e90, finished=0x7f95a24d3030) at ffmpeg_transcoder.cc:3189 #10 0x0000555da4fdfa73 in FFmpeg_Transcoder::process_single_fr (this=this@entry=0x7f9598002e90, status=@0x7f95a24d3134: 0) at ffmpeg_transcoder.cc:3987 #11 0x0000555da4f8c997 in transcoder_thread (arg=optimized out) at transcode.cc:874 #12 0x0000555da4fc54ef in thread_pool::loop_function (this=0x7f959c002b40) at thread_pool.cc:78 #13 0x00007f95aeaf4c10 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6 #14 0x00007f95ae9f0ea7 in start_thread (arg=optimized out) at pthread_create.c:477 #15 0x00007f95ae920d4f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Definition at line 7150 of file ffmpeg_transcoder.cc.

Referenced by get_hw_decoder_name(), and get_hw_encoder_name().

◆ get_next_segment()

uint32_t FFmpeg_Transcoder::get_next_segment ( int64_t  pos) const
protected

Calculate next HLS segment from position.

Parameters
[in]pos- Current transcoder position in AV_TIMEBASE fractional seconds.
Returns
Number of next segment

Definition at line 7506 of file ffmpeg_transcoder.cc.

References FFMPEGFS_PARAMS::m_segment_duration, and params.

Referenced by decode_video_frame(), process_single_fr(), and store_packet().

◆ get_out_subtitle_stream()

FFmpeg_Transcoder::StreamRef * FFmpeg_Transcoder::get_out_subtitle_stream ( int  stream_idx)
protected

Get subtitle stream for the stream index.

Parameters
[in]stream_idx- Stream index to get subtitle stream for
Returns
Pointer to subbtitle stream or nullptr if not found

Definition at line 7533 of file ffmpeg_transcoder.cc.

References m_out, and FFmpeg_Transcoder::INPUTFILE::m_subtitle.

Referenced by encode_subtitle(), open_output(), and store_packet().

◆ get_output_bit_rate()

bool FFmpeg_Transcoder::get_output_bit_rate ( BITRATE  input_bit_rate,
BITRATE  max_bit_rate,
BITRATE output_bit_rate = nullptr 
)
staticprotected

Calculate output bit rate based on user option.

Parameters
[in]input_bit_rate- Bit rate from input file.
[in]max_bit_rate- Max. bit rate if set by user.
[in]output_bit_rate- Selected output bit rate.
Returns
Returns true if bit rate was changed; false if not.

Definition at line 1437 of file ffmpeg_transcoder.cc.

Referenced by add_stream(), audio_size(), can_copy_stream(), and video_size().

◆ get_output_sample_rate()

bool FFmpeg_Transcoder::get_output_sample_rate ( int  input_sample_rate,
int  max_sample_rate,
int *  output_sample_rate = nullptr 
)
staticprotected

Calculate output sample rate based on user option.

Parameters
[in]input_sample_rate- Sample rate from input file.
[in]max_sample_rate- Max. sample rate if set by user
[in]output_sample_rate- Selected output sample rate.
Returns
Returns true if sample rate was changed; false if not.

Definition at line 1417 of file ffmpeg_transcoder.cc.

Referenced by add_stream(), and audio_size().

◆ get_pix_formats()

void FFmpeg_Transcoder::get_pix_formats ( AVPixelFormat *  in_pix_fmt,
AVPixelFormat *  out_pix_fmt,
AVCodecContext *  output_codec_ctx = nullptr 
) const
protected

Get correct input and output pixel format.

Parameters
[in]output_codec_ctx- Output codec context.
[out]in_pix_fmt- Input pixel format.
[out]out_pix_fmt- Output pixel format.

Definition at line 7471 of file ffmpeg_transcoder.cc.

References find_sw_fmt_by_hw_type(), FFmpeg_Transcoder::StreamRef::m_codec_ctx, FFMPEGFS_PARAMS::m_hwaccel_dec_device_type, m_hwaccel_enable_dec_buffering, m_hwaccel_enable_enc_buffering, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::INPUTFILE::m_video, and params.

Referenced by open_output_frame_set().

◆ get_prores_bitrate()

BITRATE FFmpeg_Transcoder::get_prores_bitrate ( int  width,
int  height,
const AVRational &  framerate,
bool  interleaved,
PRORESLEVEL  profile 
)
staticprotected

Calculate the appropriate bitrate for a ProRes file given several parameters.

Parameters
[in]width- Video width in pixels.
[in]height- Video height in pixels.
[in]framerate- Video frame rate.
[in]interleaved- If true, video is interleaved; false if not.
[in]profile- Selected ProRes profile.
Returns
Bitrate in bit/s.

Definition at line 5659 of file ffmpeg_transcoder.cc.

References m_prores_bitrate, and MAX_PRORES_FRAMERATE.

Referenced by video_size().

◆ get_video_size()

bool FFmpeg_Transcoder::get_video_size ( int *  output_width,
int *  output_height 
) const
protected

Get the size of the output video based on user selection and apsect ratio.

Parameters
[in]output_width- Output video width.
[in]output_height- Output video height.
Returns
Returns true if video height/width was reduces; false if not.

Definition at line 1501 of file ffmpeg_transcoder.cc.

References get_aspect_ratio(), m_in, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::INPUTFILE::m_video, FFMPEGFS_PARAMS::m_videoheight, FFMPEGFS_PARAMS::m_videowidth, and params.

Referenced by add_stream().

◆ goto_next_segment()

bool FFmpeg_Transcoder::goto_next_segment ( uint32_t  next_segment) const
protected

Check if segment number is next designated segment.

Parameters
[in]next_segment- Number next current segment
Returns
Returns true if next segment should start, false if not.

Definition at line 7511 of file ffmpeg_transcoder.cc.

References m_current_segment.

Referenced by decode_video_frame(), process_single_fr(), and store_packet().

◆ have_seeked()

bool FFmpeg_Transcoder::have_seeked ( ) const

Check if we made a seek operation.

Returns
Returns true if a seek was done, false if not.

Definition at line 6802 of file ffmpeg_transcoder.cc.

References m_have_seeked.

Referenced by transcode_finish(), and transcoder_thread().

◆ hwdevice_ctx_add_ref()

int FFmpeg_Transcoder::hwdevice_ctx_add_ref ( AVCodecContext *  input_codec_ctx)
protected

Add reference to hardware device context.

Parameters
[in]input_codec_ctx- Input codec context
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 6856 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), get_format_static(), m_hwaccel_dec_device_ctx, and virtname().

◆ hwdevice_ctx_create()

int FFmpeg_Transcoder::hwdevice_ctx_create ( AVBufferRef **  hwaccel_enc_device_ctx,
AVHWDeviceType  dev_type,
const std::string &  device 
) const
protected

Open a device of the specified type and create an AVHWDeviceContext for it.

This is a convenience function intended to cover the simple cases. Callers who need to fine-tune device creation/management should open the device manually and then wrap it in an AVHWDeviceContext using av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().

The returned context is already initialized and ready for use, the caller should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of the created AVHWDeviceContext are set by this function and should not be touched by the caller.

Parameters
[out]hwaccel_enc_device_ctx- On success, a reference to the newly-created device context will be written here.
[in]dev_type- The type of the device to create.
[in]device- A type-specific string identifying the device to open.
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 6837 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), hwdevice_get_type_name(), and virtname().

Referenced by open_bestmatch_video().

◆ hwdevice_ctx_free()

void FFmpeg_Transcoder::hwdevice_ctx_free ( AVBufferRef **  hwaccel_device_ctx)
protected

Free (remove reference) to hardware device context.

Parameters
[in,out]hwaccel_device_ctx- Hardware device context to free

Definition at line 6879 of file ffmpeg_transcoder.cc.

Referenced by closeio(), and open_output().

◆ hwframe_copy_from_hw()

int FFmpeg_Transcoder::hwframe_copy_from_hw ( AVCodecContext *  output_codec_ctx,
FFmpeg_Frame sw_frame,
const AVFrame *  hw_frame 
) const
protected

Copy data hardware surface to software.

Parameters
[in]output_codec_ctx- Codec context
[in,out]sw_frame- AVFrame to copy data to
[in]hw_frame- AVFrame to copy data from
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 6993 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), and filename().

◆ hwframe_copy_to_hw()

int FFmpeg_Transcoder::hwframe_copy_to_hw ( AVCodecContext *  output_codec_ctx,
FFmpeg_Frame hw_frame,
const AVFrame *  sw_frame 
) const
protected

Copy data software to a hardware surface.

Parameters
[in]output_codec_ctx- Codec context
[in,out]hw_frame- AVFrame to copy data to
[in]sw_frame- AVFrame to copy data from
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 7014 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), and virtname().

Referenced by encode_video_frame().

◆ hwframe_ctx_set()

int FFmpeg_Transcoder::hwframe_ctx_set ( AVCodecContext *  output_codec_ctx,
AVCodecContext *  input_codec_ctx,
AVBufferRef *  hw_device_ctx 
) const
protected

Adds a reference to an existing decoder hardware frame context or allocates a new AVHWFramesContext tied to the given hardware device context if if the decoder runs in software.

Parameters
[in]output_codec_ctx- Encoder codexc context
[in]input_codec_ctx- Decoder codexc context
[in]hw_device_ctx- Existing hardware device context
Returns
0 on success, a negative AVERROR code on failure.

Definition at line 6888 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), find_sw_fmt_by_hw_type(), m_enc_hw_pix_fmt, FFMPEGFS_PARAMS::m_hwaccel_enc_device_type, params, and virtname().

Referenced by add_stream().

◆ id3v1tag()

const ID3v1 * FFmpeg_Transcoder::id3v1tag ( ) const

Assemble an ID3v1 file tag.

Returns
Returns an ID3v1 file tag.

Definition at line 6215 of file ffmpeg_transcoder.cc.

References FFmpeg_Transcoder::OUTPUTFILE::m_id3v1, and m_out.

Referenced by transcode().

◆ init_audio_fifo()

int FFmpeg_Transcoder::init_audio_fifo ( )
protected

Initialise a FIFO buffer for the audio samples to be encoded.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2884 of file ffmpeg_transcoder.cc.

References Logging::error(), FFmpeg_Base::get_channels(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_audio_fifo, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_out, and virtname().

Referenced by open_output().

◆ init_audio_output_frame()

int FFmpeg_Transcoder::init_audio_output_frame ( AVFrame *  frame,
int  frame_size 
) const
protected

Initialise one input frame for writing to the output file. The frame will be exactly frame_size samples large.

Parameters
[in]frame- Newly initialised frame.
[in]frame_size- Size of new frame.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 4173 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_out, and virtname().

Referenced by create_audio_frame().

◆ init_converted_samples()

int FFmpeg_Transcoder::init_converted_samples ( uint8_t ***  converted_input_samples,
int  frame_size 
)
protected

Initialise a temporary storage for the specified number of audio samples. The conversion requires temporary storage due to the different format. The number of audio samples to be allocated is specified in frame_size.

Parameters
[out]converted_input_samples- Memory for input samples.
[in]frame_size- Size of one frame.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3914 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), FFmpeg_Base::get_channels(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_out, and virtname().

Referenced by decode_audio_frame().

◆ init_deinterlace_filters()

int FFmpeg_Transcoder::init_deinterlace_filters ( AVCodecContext *  codec_ctx,
AVPixelFormat  pix_fmt,
const AVRational &  avg_frame_rate,
const AVRational &  time_base 
)
protected

Initialise video filters.

Parameters
[in]codec_ctx- AVCodecContext object of output video.
[in]pix_fmt- Output stream pixel format.
[in]avg_frame_rate- Average output stream frame rate.
[in]time_base- Output stream time base.
Returns
Returns 0 if OK, or negative AVERROR value.

Definition at line 6513 of file ffmpeg_transcoder.cc.

References Logging::debug(), Logging::error(), ffmpeg_geterror(), m_buffer_sink_context, m_buffer_source_context, m_filter_graph, strsprintf(), and virtname().

Referenced by open_output_filestreams(), and open_output_frame_set().

◆ init_resampler()

int FFmpeg_Transcoder::init_resampler ( )
protected

Initialize the audio resampler based on the input and output codec settings. If the input and output sample formats differ, a conversion is required libswresample takes care of this, but requires initialization.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2764 of file ffmpeg_transcoder.cc.

References close_resample(), Logging::debug(), Logging::error(), ffmpeg_geterror(), format_samplerate(), FFmpeg_Base::get_channel_layout_name(), FFmpeg_Base::get_sample_fmt_name(), LAVU_DEP_OLD_CHANNEL_LAYOUT, FFmpeg_Transcoder::INPUTFILE::m_audio, m_audio_resample_ctx, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_cur_ch_layout, m_cur_sample_fmt, m_cur_sample_rate, m_in, m_out, and virtname().

Referenced by decode_audio_frame().

◆ init_rescaler()

int FFmpeg_Transcoder::init_rescaler ( AVPixelFormat  in_pix_fmt,
int  in_width,
int  in_height,
AVPixelFormat  out_pix_fmt,
int  out_width,
int  out_height 
)
protected

Init image size rescaler and pixel format converter.

Parameters
[in]in_pix_fmt- Input pixel format
[in]in_width- Input image width
[in]in_height- Input image height
[in]out_pix_fmt- Output pixel format
[in]out_width- Output image width
[in]out_height- Output pixel format
Returns
Returns 0 if OK, or negative AVERROR value.

Definition at line 1594 of file ffmpeg_transcoder.cc.

References Logging::debug(), Logging::error(), FFmpeg_Base::get_pix_fmt_name(), m_sws_ctx, Logging::trace(), and virtname().

Referenced by open_output_frame_set().

◆ input_read()

int FFmpeg_Transcoder::input_read ( void *  opaque,
unsigned char *  data,
int  size 
)
staticprotected

Custom read function for FFmpeg.

Read from virtual files, may be a physical file but also a DVD, VCD or Blu-ray chapter.

Parameters
[in]opaque- Payload given to FFmpeg, basically the FileIO object
[in]data- Returned data read from file.
[in]size- Size of data buffer.
Returns
On success, returns bytes read. May be less than size or even 0. On error, returns a negative AVERROR value.

Definition at line 6220 of file ffmpeg_transcoder.cc.

References FileIO::eof(), FileIO::error(), Logging::error(), and FileIO::readio().

Referenced by open_input_file().

◆ is_audio_stream()

bool FFmpeg_Transcoder::is_audio_stream ( int  stream_idx) const
protected

Check for audio stream.

Parameters
[in]stream_idx- ID of stream to check
Returns
Returns true if stream is an audio stream, false if not.

Definition at line 7516 of file ffmpeg_transcoder.cc.

References FFmpeg_Transcoder::INPUTFILE::m_audio, m_in, FFmpeg_Transcoder::StreamRef::m_stream_idx, and stream_exists().

Referenced by decode(), decode_frame(), flush_frames_single(), process_metadata(), and read_decode_convert_and_store().

◆ is_frameset()

bool FFmpeg_Transcoder::is_frameset ( ) const

Check for an export frame format.

Returns
Returns true for formats that export all frames as images.

Definition at line 6778 of file ffmpeg_transcoder.cc.

References FFmpegfs_Format::is_frameset(), and m_current_format.

Referenced by decode_frame(), encode_finish(), flush_delayed_video(), flush_frames_single(), open_output_file(), process_single_fr(), and transcode().

◆ is_hls()

bool FFmpeg_Transcoder::is_hls ( ) const

Check for HLS format.

Returns
Returns true for formats that create an HLS set including the m3u file.

Definition at line 6790 of file ffmpeg_transcoder.cc.

References FFmpegfs_Format::is_hls(), and m_current_format.

Referenced by decode_video_frame(), encode_finish(), open_output(), process_single_fr(), store_packet(), and transcode().

◆ is_multiformat()

bool FFmpeg_Transcoder::is_multiformat ( ) const

Check for an export frame format.

Returns
Returns true for formats that export all frames as images.

Definition at line 6765 of file ffmpeg_transcoder.cc.

References FFmpegfs_Format::is_multiformat(), and m_current_format.

Referenced by transcode_finish().

◆ is_open()

bool FFmpeg_Transcoder::is_open ( ) const

Check if input file is already open.

Returns
true if open; false if closed

Definition at line 289 of file ffmpeg_transcoder.cc.

References FFmpeg_Transcoder::INPUTFILE::m_format_ctx, and m_in.

Referenced by open_input_file().

◆ is_subtitle_stream()

bool FFmpeg_Transcoder::is_subtitle_stream ( int  stream_idx) const
protected

Check for subtitle stream.

Parameters
[in]stream_idx- ID of stream to check
Returns
Returns true if stream is a subtitle stream, false if not.

Definition at line 7526 of file ffmpeg_transcoder.cc.

References m_in, and FFmpeg_Transcoder::INPUTFILE::m_subtitle.

Referenced by decode_frame().

◆ is_video()

bool FFmpeg_Transcoder::is_video ( ) const
protected

FFmpeg handles cover arts like video streams. Try to find out if we have a video stream or a cover art.

Returns
Return true if file contains a video stream.

Definition at line 277 of file ffmpeg_transcoder.cc.

References is_album_art(), is_video(), FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_in, FFmpeg_Transcoder::StreamRef::m_stream, and FFmpeg_Transcoder::INPUTFILE::m_video.

Referenced by is_video(), and open_bestmatch_video().

◆ is_video_stream()

bool FFmpeg_Transcoder::is_video_stream ( int  stream_idx) const
protected

Check for video stream.

Parameters
[in]stream_idx- ID of stream to check
Returns
Returns true if stream is a video stream, false if not.

Definition at line 7521 of file ffmpeg_transcoder.cc.

References m_in, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, and stream_exists().

Referenced by decode(), decode_frame(), flush_frames_single(), and process_metadata().

◆ last_seek_frame_no()

uint32_t FFmpeg_Transcoder::last_seek_frame_no ( ) const

Current seek frame if available.

Returns
Returns the current seek frame; 0 if none available.

Definition at line 7851 of file ffmpeg_transcoder.cc.

References m_last_seek_frame_no.

Referenced by transcoder_thread().

◆ make_pts()

void FFmpeg_Transcoder::make_pts ( AVPacket *  pkt,
int64_t *  cur_ts 
) const
protected

Create PTS/DTS and update the packet. If the update packet lacks time stamps, create a fictitious PTS or DTS and update it. If the packet already has valid time stamps, nothing is changed.

Parameters
[in,out]pkt- Audio/video packet to update.
[in,out]cur_ts- Current time stamp, will be updated to the next position.

Definition at line 3718 of file ffmpeg_transcoder.cc.

Referenced by decode_frame().

◆ map_in_to_out_stream()

int FFmpeg_Transcoder::map_in_to_out_stream ( int  in_stream_idx) const
protected

Map input stream index to output stream index.

Parameters
[in]in_stream_idx- Index of input stream
Returns
Returns output stream index or INVALID_STREAM if no match

Definition at line 7558 of file ffmpeg_transcoder.cc.

References INVALID_STREAM, and m_stream_map.

Referenced by add_subtitle_streams(), decode_subtitle(), and open_output().

◆ mtime()

time_t FFmpeg_Transcoder::mtime ( ) const

Get last modification time of file.

Returns
Modification time (seconds since epoch)

Definition at line 4877 of file ffmpeg_transcoder.cc.

References m_mtime.

◆ open_albumarts()

int FFmpeg_Transcoder::open_albumarts ( )
protected

◆ open_bestmatch_audio()

int FFmpeg_Transcoder::open_bestmatch_audio ( )
protected

◆ open_bestmatch_decoder()

int FFmpeg_Transcoder::open_bestmatch_decoder ( AVFormatContext *  format_ctx,
AVCodecContext **  codec_ctx,
int *  stream_idx,
AVMediaType  type 
)
protected

Find best match stream and open codec context for it.

Parameters
[in]format_ctx- Output format context
[out]codec_ctx,-Newly created codec context
[in]stream_idx- Stream index of new stream.
[in]type- Type of media: audio or video.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 889 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), filename(), get_media_type_string(), INVALID_STREAM, and open_decoder().

Referenced by add_external_subtitle_stream(), open_bestmatch_audio(), and open_bestmatch_video().

◆ open_bestmatch_video()

int FFmpeg_Transcoder::open_bestmatch_video ( )
protected

◆ open_decoder()

int FFmpeg_Transcoder::open_decoder ( AVFormatContext *  format_ctx,
AVCodecContext **  codec_ctx,
int  stream_idx,
const AVCodec *  input_codec,
AVMediaType  mediatype 
)
protected

Open codec context for stream_idx.

Parameters
[in]format_ctx- Output format context
[out]codec_ctx- Newly created codec context
[in]stream_idx- Stream index of new stream.
[in]input_codec- Decoder codec to open, may be nullptr. Will open a matching codec automatically.
[in]mediatype- Type of media: audio or video.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 950 of file ffmpeg_transcoder.cc.

References check_hwaccel_dec_blocked(), Logging::debug(), Logging::error(), ffmpeg_geterror(), get_codec_name(), get_hwaccel_API_text(), get_media_type_string(), Logging::info(), FFMPEGFS_PARAMS::m_hwaccel_dec_API, FFMPEGFS_PARAMS::m_hwaccel_dec_device, FFMPEGFS_PARAMS::m_hwaccel_dec_device_type, params, and Logging::trace().

Referenced by open_albumarts(), open_bestmatch_decoder(), and open_subtitles().

◆ open_input_file()

int FFmpeg_Transcoder::open_input_file ( LPVIRTUALFILE  virtualfile,
std::shared_ptr< FileIO fio = nullptr 
)

Open the given FFmpeg file and prepare for decoding. Collect information for the file (duration, bitrate, etc.). After this function, the other methods can be used to process the file.

Parameters
[in,out]virtualfile- Virtualfile object for desired file. May be a physical file, a DVD, Blu-ray or video CD
[in,out]fio- Pass an already open fileio object. Normally the file is opened, but if this parameter is not nullptr the already existing object is used.
Returns
On success, returns 0; on error, a negative AVERROR value.
Bug:
Fix memory leak: Probably in FFmpeg API av_probe_input_buffer2(), the av_reallocp is missing a matching free() call...

102,400 bytes in 1 blocks are definitely lost in loss record 248 of 249
in FFmpeg_Transcoder::open_input_file(VIRTUALFILE*, FileIO*) in /home/norbert/dev/prj/ffmpegfs/src/ffmpeg_transcoder.cc:368
1: realloc in ./coregrind/m_replacemalloc/vg_replace_malloc.c:834
2: av_realloc_f in /usr/lib/x86_64-linux-gnu/libavutil.so.56.51.100
3: /usr/lib/x86_64-linux-gnu/libavformat.so.58.45.100
4: av_probe_input_buffer2 in /usr/lib/x86_64-linux-gnu/libavformat.so.58.45.100
5: avformat_open_input in /usr/lib/x86_64-linux-gnu/libavformat.so.58.45.100
6: FFmpeg_Transcoder::open_input_file(VIRTUALFILE*, FileIO*) in /home/norbert/dev/prj/ffmpegfs/src/ffmpeg_transcoder.cc:368
7: transcoder_predict_filesize(VIRTUALFILE*, Cache_Entry*) in /home/norbert/dev/prj/ffmpegfs/src/transcode.cc:320
8: transcoder_new(VIRTUALFILE*, bool) in /home/norbert/dev/prj/ffmpegfs/src/transcode.cc:425
9: ffmpegfs_getattr(char const*, stat*) in /home/norbert/dev/prj/ffmpegfs/src/fuseops.cc:1323
10: /usr/lib/x86_64-linux-gnu/libfuse.so.2.9.9
11: /usr/lib/x86_64-linux-gnu/libfuse.so.2.9.9
12: /usr/lib/x86_64-linux-gnu/libfuse.so.2.9.9
13: /usr/lib/x86_64-linux-gnu/libfuse.so.2.9.9
14: start_thread in ./nptl/pthread_create.c:477
15: clone in ./misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Definition at line 294 of file ffmpeg_transcoder.cc.

References FileIO::alloc(), BLURAY, calculate_predicted_filesize(), FFMPEGFS_PARAMS::current_format(), Logging::debug(), FFmpeg_Base::dict_set_with_check(), DVD, Logging::error(), FF_INPUT_BUFFER_PADDING_SIZE, ffmpeg_geterror(), ffmpeg_rescale_q(), filename(), get_filetype_from_list(), input_read(), is_open(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, VIRTUALFILE::m_cuesheet_track, m_current_format, VIRTUALFILE::m_duration, m_fileio, FFmpeg_Transcoder::INPUTFILE::m_filetype, VIRTUALFILE::m_flags, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, VIRTUALFILE::m_framerate, VIRTUALFILE::m_height, m_in, m_mtime, FFMPEGFS_PARAMS::m_no_subtitles, VIRTUALFILE::m_predicted_size, VIRTUALFILE::m_st, VIRTUALFILE::CUESHEET_TRACK::m_start, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, VIRTUALFILE::m_type, FFmpeg_Transcoder::INPUTFILE::m_video, VIRTUALFILE::m_video_frame_count, FFmpeg_Base::m_virtualfile, VIRTUALFILE::m_width, open_albumarts(), open_bestmatch_audio(), open_bestmatch_video(), open_subtitles(), params, seek(), stream_exists(), VCD, VIRTUALFLAG_CUESHEET, and Logging::warning().

Referenced by transcode(), and transcoder_predict_filesize().

◆ open_output()

int FFmpeg_Transcoder::open_output ( Buffer buffer)
protected

◆ open_output_file()

int FFmpeg_Transcoder::open_output_file ( Buffer buffer)

Open output file. Data will actually be written to buffer and copied by FUSE when accessed.

Parameters
[in]buffer- Cache buffer to be written.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 856 of file ffmpeg_transcoder.cc.

References Logging::debug(), FFmpegfs_Format::desttype(), Logging::error(), FFmpegfs_Format::filetype(), is_frameset(), FFmpeg_Transcoder::INPUTFILE::m_audio, m_current_format, FFmpeg_Transcoder::INPUTFILE::m_filetype, VIRTUALFILE::m_flags, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Base::m_virtualfile, open_output(), open_output_frame_set(), stream_exists(), FFmpegfs_Format::video_codec(), virtname(), and VIRTUALFLAG_HIDDEN.

Referenced by transcode().

◆ open_output_filestreams()

int FFmpeg_Transcoder::open_output_filestreams ( Buffer buffer)
protected

Open an output file and the required encoder. Also set some basic encoder parameters. Some of these parameters are based on the input file's parameters.

Parameters
[in]buffer- Stream buffer to operate on
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2563 of file ffmpeg_transcoder.cc.

References add_albumart_stream(), add_external_subtitle_streams(), add_stream(), add_stream_copy(), add_subtitle_streams(), FFmpegfs_Format::albumart_supported(), FFmpegfs_Format::audio_codec(), can_copy_stream(), Logging::debug(), FFmpegfs_Format::desttype(), Logging::error(), FF_INPUT_BUFFER_PADDING_SIZE, FFMPEGFS_AUDIO, FFMPEGFS_VIDEO, FFmpegfs_Format::filetype(), FFmpegfs_Format::format_name(), Logging::info(), init_deinterlace_filters(), INVALID_STREAM, m_active_stream_msk, FFmpeg_Transcoder::INPUTFILE::m_album_art, FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_copy_audio, m_copy_video, m_current_format, FFMPEGFS_PARAMS::m_deinterlace, FFmpeg_Transcoder::INPUTFILE::m_filetype, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, m_inhibit_stream_msk, m_is_video, FFMPEGFS_PARAMS::m_noalbumarts, m_out, FFmpeg_Transcoder::INPUTFILE::m_pix_fmt, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_subtitle, FFmpeg_Transcoder::INPUTFILE::m_video, output_write(), params, seek(), stream_exists(), FFmpegfs_Format::video_codec(), and virtname().

Referenced by open_output().

◆ open_output_frame_set()

int FFmpeg_Transcoder::open_output_frame_set ( Buffer buffer)
protected

◆ open_subtitles()

int FFmpeg_Transcoder::open_subtitles ( )
protected

Open all subtitles streams, if present in input file and if supported by output file. The input and output codec type must also match: Can only transcode bitmap subtitles into bitmap subtitles or text to text.

Todo:
Add text to bitmap conversion.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 705 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), filename(), m_current_format, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_subtitle, open_decoder(), FFmpeg_Transcoder::StreamRef::set_codec_ctx(), FFmpegfs_Format::subtitle_codec(), and FFmpeg_Base::subtitle_info().

Referenced by open_input_file().

◆ output_write()

int FFmpeg_Transcoder::output_write ( void *  opaque,
const uint8_t *  data,
int  size 
)
staticprotected

Custom write function for FFmpeg.

Parameters
[in]opaque- Payload given to FFmpeg, basically the FileIO object
[in]data- Data to be written
[in]size- Size of data block.
Returns
On success, returns bytes written. On error, returns a negative AVERROR value.

Definition at line 6248 of file ffmpeg_transcoder.cc.

References Logging::error(), and Buffer::writeio().

Referenced by open_output_filestreams().

◆ predicted_filesize()

size_t FFmpeg_Transcoder::predicted_filesize ( ) const

Try to predict the recoded file size. This may (better will surely) be inaccurate.

Returns
Predicted file size in bytes.

Definition at line 6164 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::m_virtualfile, and SAFE_VALUE.

Referenced by create_fake_aiff_header(), create_fake_wav_header(), open_output(), open_output_frame_set(), transcode(), and transcoder_predict_filesize().

◆ prepare_codec()

int FFmpeg_Transcoder::prepare_codec ( void *  opt,
FILETYPE  filetype 
) const
protected

Prepare codec options for a file type.

Parameters
[in]opt- Codec private data.
[in]filetype- File type: MP3, MP4 etc.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 1578 of file ffmpeg_transcoder.cc.

References FFmpeg_Profiles::m_profile, FFMPEGFS_PARAMS::m_profile, params, and update_codec().

◆ prepare_format()

int FFmpeg_Transcoder::prepare_format ( AVDictionary **  dict,
FILETYPE  filetype 
) const
protected

Prepare format optimisations.

Parameters
[in]dict- Dictionary to update.
[in]filetype- File type: MP3, MP4 etc.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2925 of file ffmpeg_transcoder.cc.

References FFmpeg_Profiles::m_profile, FFMPEGFS_PARAMS::m_profile, params, and update_format().

Referenced by write_output_file_header().

◆ process_albumarts()

int FFmpeg_Transcoder::process_albumarts ( )
protected

Copy all album arts from source to target.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 5011 of file ffmpeg_transcoder.cc.

References add_albumart_frame(), FFmpeg_Transcoder::INPUTFILE::m_album_art, m_in, and m_out.

Referenced by process_output().

◆ process_metadata()

int FFmpeg_Transcoder::process_metadata ( )
protected

◆ process_output()

int FFmpeg_Transcoder::process_output ( )
protected

Process headers of output file Write file header, process meta data and add album arts.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 1396 of file ffmpeg_transcoder.cc.

References process_albumarts(), process_metadata(), and write_output_file_header().

Referenced by open_output(), and start_new_segment().

◆ process_single_fr()

int FFmpeg_Transcoder::process_single_fr ( DECODER_STATUS status)

Process a single frame of audio data. The encode_pcm_data() method of the Encoder will be used to process the resulting audio data, with the result going into the given Buffer.

Parameters
[out]status- On success, returns DECODER_SUCCESS; if at EOF, returns DECODER_EOF; on error, returns DECODER_ERROR
Returns
On success, returns 0; on error, a negative AVERROR value. If EOF is reached, it returns 1.

Definition at line 5251 of file ffmpeg_transcoder.cc.

References copy_audio_to_frame_buffer(), DEC_EOF, DEC_ERROR, DEC_SUCCESS, encode_audio_frame(), encode_image_frame(), encode_subtitle(), encode_video_frame(), Logging::error(), flush_delayed_audio(), flush_delayed_subtitles(), flush_delayed_video(), get_next_segment(), goto_next_segment(), is_frameset(), is_hls(), m_active_stream_msk, FFmpeg_Transcoder::INPUTFILE::m_audio, m_copy_audio, m_frame_map, m_in, m_inhibit_stream_msk, m_out, FFMPEGFS_PARAMS::m_segment_duration, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Frame::m_stream_idx, FFmpeg_Subtitle::m_stream_idx, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, params, read_decode_convert_and_store(), seek_frame(), start_new_segment(), stream_exists(), and virtname().

Referenced by transcode().

◆ produce_audio_dts()

void FFmpeg_Transcoder::produce_audio_dts ( AVPacket *  pkt)
protected

Produce audio dts/pts. This is required because the target codec usually has a different frame size than the source, so the number of packets will not match 1:1.

Parameters
[in]pkt- Packet to add dts/pts to.

Definition at line 4212 of file ffmpeg_transcoder.cc.

References FFmpegfs_Format::filetype(), FFmpeg_Transcoder::INPUTFILE::m_audio, FFmpeg_Transcoder::StreamRef::m_codec_ctx, m_current_format, and m_out.

Referenced by encode_audio_frame().

◆ pts()

int64_t FFmpeg_Transcoder::pts ( ) const

Get PTS (presentation time stamp) of decoded audio/video so far.

Returns
Returns the PTS (presentation time stamp) of decoded audio/video so far in AV_TIME_BASE units.

Definition at line 7837 of file ffmpeg_transcoder.cc.

References m_frame_map.

Referenced by transcode().

◆ purge()

void FFmpeg_Transcoder::purge ( )
protected

Purge FIFO and map buffers and report lost packets/frames/samples.

Definition at line 6359 of file ffmpeg_transcoder.cc.

References FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_out, VIRTUALFILE::m_virtfile, FFmpeg_Base::m_virtualfile, purge_audio_fifo(), purge_hls_fifo(), purge_multiframe_map(), and Logging::warning().

Referenced by close_output_file().

◆ purge_audio_fifo()

int FFmpeg_Transcoder::purge_audio_fifo ( )
protected

Purge all samples in audio FIFO.

Returns
Number of samples that have been purged. Function never fails.

Definition at line 6321 of file ffmpeg_transcoder.cc.

References m_audio_fifo.

Referenced by purge().

◆ purge_hls_fifo()

size_t FFmpeg_Transcoder::purge_hls_fifo ( )
protected

Purge all packets in HLS FIFO buffer.

Returns
Number of Packets that have been purged. Function never fails.

Definition at line 6344 of file ffmpeg_transcoder.cc.

References m_hls_packet_fifo.

Referenced by purge(), and start_new_segment().

◆ purge_multiframe_map()

size_t FFmpeg_Transcoder::purge_multiframe_map ( )
protected

Purge all frames in buffer.

Returns
Number of frames that have been purged. Function never fails.

Definition at line 6335 of file ffmpeg_transcoder.cc.

References m_frame_map.

Referenced by purge().

◆ read_aiff_chunk()

int FFmpeg_Transcoder::read_aiff_chunk ( Buffer buffer,
size_t *  buffoffset,
const char *  ID,
uint8_t *  chunk,
size_t *  size 
) const
protected

Read AIFF chunk.

Parameters
[in]buffer- Cache buffer to read from
[in,out]buffoffset- Byte offset into buffer. Upon return holds offset to the position of the chunk.
[in]ID- Chunk ID (fourCC)
[out]chunk- Buffer for chunk
[in,out]size- Size of chunk. Buffer for chunk must be large enough to hold it. Upon return holds the actual size of the chunk read.
Returns
Returns 0 if successful or -1 On error, or end of file. Check buffer->eof().

Definition at line 3006 of file ffmpeg_transcoder.cc.

References AIFF_FORMID, Buffer::copy(), Buffer::eof(), AIFF_CHUNK::m_ckID, and AIFF_CHUNK::m_ckSize.

Referenced by create_fake_aiff_header().

◆ read_decode_convert_and_store()

int FFmpeg_Transcoder::read_decode_convert_and_store ( int *  finished)
protected

Read frame from source file, decode and store in FIFO.

Parameters
[in]finished- 1 if at EOF.
Returns
On success, returns 0; on error, a negative AVERROR value.

<*

Todo:
Cue sheet track: Must check video stream, too and end if both all video and audio packets arrived. Discard packets exceeding duration.

Definition at line 4102 of file ffmpeg_transcoder.cc.

References decode_frame(), Logging::error(), ffmpeg_geterror(), ffmpeg_rescale_q(), flush_frames_all(), is_audio_stream(), FFmpeg_Transcoder::INPUTFILE::m_audio, VIRTUALFILE::m_cuesheet_track, VIRTUALFILE::CUESHEET_TRACK::m_duration, VIRTUALFILE::m_flags, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, VIRTUALFILE::CUESHEET_TRACK::m_start, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Base::m_virtualfile, Logging::trace(), virtname(), and VIRTUALFLAG_CUESHEET.

Referenced by copy_audio_to_frame_buffer(), and process_single_fr().

◆ read_packet()

int FFmpeg_Transcoder::read_packet ( void *  opaque,
uint8_t *  buf,
int  buf_size 
)
staticprotected

FFmpeg_Transcoder::read_packet.

Parameters
[in]opaque
[in]buf
[in]buf_size
Returns

Definition at line 7618 of file ffmpeg_transcoder.cc.

References FFmpeg_Transcoder::BUFFER_DATA::ptr, and FFmpeg_Transcoder::BUFFER_DATA::size.

Referenced by add_external_subtitle_stream().

◆ seek()

int64_t FFmpeg_Transcoder::seek ( void *  opaque,
int64_t  offset,
int  whence 
)
staticprotected

Custom seek function for FFmpeg.

Write to virtual files, currently only physical files.

Parameters
[in]opaque- Payload given to FFmpeg, basically the FileIO object
[in]offset- Offset to seek to.
[in]whence- One of the regular seek() constants like SEEK_SET/SEEK_END. Additionally FFmpeg constants like AVSEEK_SIZE are supported.
Returns
On successs returns 0. On error, returns -1 and sets errno accordingly.

Definition at line 6274 of file ffmpeg_transcoder.cc.

References Logging::error(), FileIO::seek(), and FileIO::size().

Referenced by open_input_file(), and open_output_filestreams().

◆ seek_frame()

int FFmpeg_Transcoder::seek_frame ( )
protected

Frame sets only: perform seek to a certain frame.

Returns
0 on success, a negative AVERROR code on failure.

Definition at line 5474 of file ffmpeg_transcoder.cc.

References Buffer::have_frame(), m_buffer, m_last_seek_frame_no, m_seek_to_fifo, m_seek_to_fifo_mutex, and skip_decoded_frames().

Referenced by process_single_fr().

◆ segment_count()

uint32_t FFmpeg_Transcoder::segment_count ( ) const

Get the number of HLS segments of file.

Returns
On success, returns the number of segments; on error, returns 0 (calculation failed).

Definition at line 6174 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::m_virtualfile, and SAFE_VALUE.

Referenced by stack_seek_segment(), transcode(), transcode_finish(), and transcoder_predict_filesize().

◆ send_filters()

int FFmpeg_Transcoder::send_filters ( FFmpeg_Frame srcframe,
int &  ret 
)
protected

Send video frame to the filters.

Parameters
[in,out]srcframe- On input video frame to process, on output video frame that was filtered.
[in]ret- 0 if OK, or negative AVERROR value.
Returns
Returns 0 if OK, or negative AVERROR value.

Definition at line 6655 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), m_buffer_sink_context, m_buffer_source_context, FFmpeg_Frame::m_stream_idx, FFmpeg_Frame::res(), virtname(), and Logging::warning().

Referenced by decode_video_frame().

◆ skip_decoded_frames()

int FFmpeg_Transcoder::skip_decoded_frames ( uint32_t  frame_no,
bool  forced_seek 
)
protected

Skip decoded frames or force seek to frame_no.

Parameters
[in]frame_no- Frame to seek to.
[in]forced_seek- Force seek even if np frames skipped.
Returns
Returns 0 if OK, or negative AVERROR value.

Definition at line 5067 of file ffmpeg_transcoder.cc.

References do_seek_frame(), Logging::error(), ffmpeg_geterror(), FRAME_SEEK_THRESHOLD, Buffer::have_frame(), m_buffer, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_in, m_out, FFmpeg_Transcoder::StreamRef::m_stream, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, VIRTUALFILE::m_video_frame_count, FFmpeg_Transcoder::OUTPUTFILE::m_video_pts, FFmpeg_Base::m_virtualfile, FFmpeg_Base::pts_to_frame(), and virtname().

Referenced by seek_frame().

◆ stack_seek_frame()

int FFmpeg_Transcoder::stack_seek_frame ( uint32_t  frame_no)

Seek to a specific frame. Does not actually perform the seek, this is done asynchronously by the transcoder thread.

Parameters
[in]frame_no- Frame number to seek 1...n
Returns
On success, returns 0; On error, negative AVERROR value and sets errno to EINVAL.

Definition at line 6733 of file ffmpeg_transcoder.cc.

References Logging::error(), m_seek_to_fifo, m_seek_to_fifo_mutex, video_frame_count(), and virtname().

Referenced by transcode().

◆ stack_seek_segment()

int FFmpeg_Transcoder::stack_seek_segment ( uint32_t  segment_no)

Seek to a specific HLS segment. Does not actually perform the seek, this is done asynchronously by the transcoder thread.

Parameters
[in]segment_no- Segment number to seek 1...n
Returns
On success, returns 0; On error, negative AVERROR value and sets errno to EINVAL.

Definition at line 6749 of file ffmpeg_transcoder.cc.

References Logging::error(), m_seek_to_fifo, m_seek_to_fifo_mutex, segment_count(), video_frame_count(), and virtname().

Referenced by transcode().

◆ start_new_segment()

int FFmpeg_Transcoder::start_new_segment ( )
protected

◆ store_packet()

int FFmpeg_Transcoder::store_packet ( AVPacket *  pkt,
AVMediaType  mediatype 
)
protected

◆ stream_exists()

bool FFmpeg_Transcoder::stream_exists ( int  stream_idx) const
protected

Check if stream exists.

Parameters
[in]stream_idx- ID of stream to check
Returns
Returns 0 if stream exists, false if not.

Definition at line 7545 of file ffmpeg_transcoder.cc.

References INVALID_STREAM.

Referenced by calculate_predicted_filesize(), decode(), decode_frame(), flush_frames_single(), is_audio_stream(), is_video_stream(), open_bestmatch_audio(), open_bestmatch_video(), open_input_file(), open_output(), open_output_file(), open_output_filestreams(), process_single_fr(), start_new_segment(), and update_format().

◆ tagcpy() [1/2]

template<size_t size>
const char * FFmpeg_Transcoder::tagcpy ( char(&)  out[size],
const std::string &  in 
) const
protected

Safely copy a tag to a target buffer. If the input buffer size is larger than output the data will be truncated to avoid overruns. The function never appends a /0 terminator.

Parameters
[out]out- Target buffer
[in]in- Input buffer
Returns
Constant pointer to target buffer.

Definition at line 4883 of file ffmpeg_transcoder.cc.

◆ tagcpy() [2/2]

template<class T >
const T & FFmpeg_Transcoder::tagcpy ( T &  out,
const std::string &  in 
) const
protected

Safely copy a tag to a target buffer. If the input buffer size is larger than output the data will be truncated to avoid overruns. The function never appends a /0 terminator.

Parameters
[out]out- Target buffer
[in]in- Input buffer
Returns
Constant pointer to target buffer.

Definition at line 4891 of file ffmpeg_transcoder.cc.

◆ total_overhead()

bool FFmpeg_Transcoder::total_overhead ( size_t *  filesize,
FILETYPE  filetype 
)
static

Predict overhead in file size. This may (better will surely) be inaccurate.

Parameters
[out]filesize- Predicted file size in bytes, including overhead.
[in]filetype- File type: MP3, TS etc.
Returns
On success, returns true; on failure, returns false.

Definition at line 6010 of file ffmpeg_transcoder.cc.

Referenced by calculate_predicted_filesize(), and transcoder_set_filesize().

◆ update_codec()

int FFmpeg_Transcoder::update_codec ( void *  opt,
const PROFILE_OPTION_VEC profile_option_vec 
) const
protected

Prepare codec options.

Parameters
[in]opt- Codec private data.
[in]profile_option_vec- Selected profile option.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 1556 of file ffmpeg_transcoder.cc.

References m_hwaccel_enable_enc_buffering, OPT_HW_ONLY, FFmpeg_Base::opt_set_with_check(), OPT_SW_ONLY, Logging::trace(), and virtname().

Referenced by prepare_codec().

◆ update_format()

int FFmpeg_Transcoder::update_format ( AVDictionary **  dict,
const PROFILE_OPTION_VEC option_vec 
) const
protected

Update format options.

Parameters
[in]dict- Dictionary to update.
[in]option_vec- Profile option to set.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 2896 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::dict_set_with_check(), m_out, FFmpeg_Transcoder::StreamRef::m_stream_idx, FFmpeg_Transcoder::INPUTFILE::m_video, OPT_AUDIO, OPT_VIDEO, stream_exists(), Logging::trace(), and virtname().

Referenced by prepare_format().

◆ video_frame_count()

uint32_t FFmpeg_Transcoder::video_frame_count ( ) const

Get the number of video frames in file.

Returns
On success, returns the number of frames; on error, returns 0 (calculation failed or no video source file).

Definition at line 6169 of file ffmpeg_transcoder.cc.

References FFmpeg_Base::m_virtualfile, and SAFE_VALUE.

Referenced by open_output_frame_set(), stack_seek_frame(), stack_seek_segment(), transcode(), transcode_finish(), and transcoder_predict_filesize().

◆ video_size()

bool FFmpeg_Transcoder::video_size ( size_t *  filesize,
AVCodecID  codec_id,
BITRATE  bit_rate,
int64_t  duration,
int  width,
int  height,
bool  interleaved,
const AVRational &  framerate 
)
static

Predict video file size. This may (better will surely) be inaccurate.

Parameters
[out]filesize- Predicted file size in bytes, including video stream size.
[in]codec_id- Target codec ID.
[in]bit_rate- Target bit rate.
[in]duration- File duration.
[in]width- Target video width.
[in]height-Target video height.
[in]interleaved- True if target video is interleaved, false if not.
[in]framerate- Frame rate of target video.
Returns
On success, returns true; on failure, returns false.
Todo:
AV1 prediction has not been tested yet

Definition at line 5926 of file ffmpeg_transcoder.cc.

References BITRATE, duration(), get_output_bit_rate(), get_prores_bitrate(), FFMPEGFS_PARAMS::m_level, FFMPEGFS_PARAMS::m_videobitrate, and params.

Referenced by calculate_predicted_filesize(), and transcoder_set_filesize().

◆ virtname()

const char * FFmpeg_Transcoder::virtname ( ) const
overridevirtual

◆ write_output_file_header()

int FFmpeg_Transcoder::write_output_file_header ( )
protected

Write the header of the output file container.

Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 3110 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), FFmpegfs_Format::filetype(), m_current_format, FFmpeg_Transcoder::INPUTFILE::m_filetype, FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_out, prepare_format(), and virtname().

Referenced by process_output().

◆ write_output_file_trailer()

int FFmpeg_Transcoder::write_output_file_trailer ( )
protected

Write the trailer of the output file container.

Returns
On success, returns 0. On error, returns a negative AVERROR value.

Definition at line 4863 of file ffmpeg_transcoder.cc.

References Logging::error(), ffmpeg_geterror(), FFmpeg_Transcoder::INPUTFILE::m_format_ctx, m_out, and virtname().

Referenced by encode_finish().

Member Data Documentation

◆ m_active_stream_msk

uint32_t FFmpeg_Transcoder::m_active_stream_msk
private

HLS: Currently active streams bit mask. Set FFMPEGFS_AUDIO and/or FFMPEGFS_VIDEO.

Definition at line 1299 of file ffmpeg_transcoder.h.

Referenced by open_output_filestreams(), and process_single_fr().

◆ m_audio_fifo

AVAudioFifo* FFmpeg_Transcoder::m_audio_fifo
private

◆ m_audio_resample_ctx

SwrContext* FFmpeg_Transcoder::m_audio_resample_ctx
private

SwResample context for audio resampling.

Definition at line 1248 of file ffmpeg_transcoder.h.

Referenced by close_resample(), convert_samples(), decode_audio_frame(), and init_resampler().

◆ m_buffer

Buffer* FFmpeg_Transcoder::m_buffer
private

◆ m_buffer_sink_context

AVFilterContext* FFmpeg_Transcoder::m_buffer_sink_context
private

Video filter sink context.

Definition at line 1253 of file ffmpeg_transcoder.h.

Referenced by free_filters(), init_deinterlace_filters(), and send_filters().

◆ m_buffer_source_context

AVFilterContext* FFmpeg_Transcoder::m_buffer_source_context
private

Video filter source context.

Definition at line 1254 of file ffmpeg_transcoder.h.

Referenced by free_filters(), init_deinterlace_filters(), and send_filters().

◆ m_copy_audio

bool FFmpeg_Transcoder::m_copy_audio
private

If true, copy audio stream from source to target (just remux, no recode).

Definition at line 1268 of file ffmpeg_transcoder.h.

Referenced by decode_frame(), flush_frames_single(), open_output_filestreams(), and process_single_fr().

◆ m_copy_video

bool FFmpeg_Transcoder::m_copy_video
private

If true, copy video stream from source to target (just remux, no recode).

Definition at line 1269 of file ffmpeg_transcoder.h.

Referenced by decode_frame(), flush_frames_single(), and open_output_filestreams().

◆ m_cur_audio_ts

int64_t FFmpeg_Transcoder::m_cur_audio_ts
private

If the audio stream is copied and the time stamps are absent from the input stream, we have to generate them.

Definition at line 1272 of file ffmpeg_transcoder.h.

Referenced by decode_frame().

◆ m_cur_ch_layout

AVChannelLayout FFmpeg_Transcoder::m_cur_ch_layout
private

Currently selected audio channel layout.

Definition at line 1244 of file ffmpeg_transcoder.h.

Referenced by FFmpeg_Transcoder(), and init_resampler().

◆ m_cur_sample_fmt

AVSampleFormat FFmpeg_Transcoder::m_cur_sample_fmt
private

Currently selected audio sample format.

Definition at line 1241 of file ffmpeg_transcoder.h.

Referenced by calculate_predicted_filesize(), and init_resampler().

◆ m_cur_sample_rate

int FFmpeg_Transcoder::m_cur_sample_rate
private

Currently selected audio sample rate.

Definition at line 1242 of file ffmpeg_transcoder.h.

Referenced by init_resampler().

◆ m_cur_video_ts

int64_t FFmpeg_Transcoder::m_cur_video_ts
private

If the video stream is copied and the time stamps are absent from the input stream, we have to generate them.

Definition at line 1273 of file ffmpeg_transcoder.h.

Referenced by decode_frame().

◆ m_current_format

const FFmpegfs_Format* FFmpeg_Transcoder::m_current_format
private

◆ m_current_segment

uint32_t FFmpeg_Transcoder::m_current_segment
private

HLS only: Segment file number currently being encoded.

Definition at line 1264 of file ffmpeg_transcoder.h.

Referenced by encode_finish(), goto_next_segment(), open_output(), and start_new_segment().

◆ m_dec_hw_pix_fmt

AVPixelFormat FFmpeg_Transcoder::m_dec_hw_pix_fmt
private

Requested decoder hardware pixel format.

Definition at line 1293 of file ffmpeg_transcoder.h.

Referenced by get_format().

◆ m_devicetype_map

const FFmpeg_Transcoder::DEVICETYPE_MAP FFmpeg_Transcoder::m_devicetype_map
staticprivate
Initial value:
=
{
{ AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_NV12 },
}

List of AVPixelFormats mapped to hardware acceleration types.

Definition at line 1285 of file ffmpeg_transcoder.h.

Referenced by find_sw_fmt_by_hw_type().

◆ m_enc_hw_pix_fmt

AVPixelFormat FFmpeg_Transcoder::m_enc_hw_pix_fmt
private

Requested encoder hardware pixel format.

Definition at line 1292 of file ffmpeg_transcoder.h.

Referenced by add_stream(), hwframe_ctx_set(), and open_output().

◆ m_fake_frame_no

uint32_t FFmpeg_Transcoder::m_fake_frame_no
private

The MJEPG codec requires monotonically growing PTS values so we fake some to avoid them going backwards after seeks.

Definition at line 1280 of file ffmpeg_transcoder.h.

Referenced by encode_image_frame().

◆ m_fileio

std::shared_ptr<FileIO> FFmpeg_Transcoder::m_fileio
private

FileIO object of input file.

Definition at line 1229 of file ffmpeg_transcoder.h.

Referenced by calculate_predicted_filesize(), close_input_file(), and open_input_file().

◆ m_filter_graph

AVFilterGraph* FFmpeg_Transcoder::m_filter_graph
private

Video filter graph.

Definition at line 1255 of file ffmpeg_transcoder.h.

Referenced by free_filters(), and init_deinterlace_filters().

◆ m_frame_map

MULTIFRAME_MAP FFmpeg_Transcoder::m_frame_map
private

Audio/video/subtitle frame map.

Definition at line 1238 of file ffmpeg_transcoder.h.

Referenced by create_audio_frame(), decode_subtitle(), decode_video_frame(), process_single_fr(), pts(), and purge_multiframe_map().

◆ m_have_seeked

bool FFmpeg_Transcoder::m_have_seeked
private

After seek operations this is set to make sure the trancoding result is marked RESULTCODE_INCOMPLETE to start transcoding over next access to fill the gaps.

Definition at line 1234 of file ffmpeg_transcoder.h.

Referenced by do_seek_frame(), have_seeked(), open_output_frame_set(), and start_new_segment().

◆ m_hls_packet_fifo

std::queue<AVPacket*> FFmpeg_Transcoder::m_hls_packet_fifo
private

HLS packet FIFO.

Definition at line 1301 of file ffmpeg_transcoder.h.

Referenced by purge_hls_fifo(), start_new_segment(), and store_packet().

◆ m_hwaccel_dec_device_ctx

AVBufferRef* FFmpeg_Transcoder::m_hwaccel_dec_device_ctx
private

Hardware acceleration device context for decoder.

Definition at line 1291 of file ffmpeg_transcoder.h.

Referenced by closeio(), and hwdevice_ctx_add_ref().

◆ m_hwaccel_dec_mode

HWACCELMODE FFmpeg_Transcoder::m_hwaccel_dec_mode
private

Current hardware acceleration mode for decoder.

Definition at line 1287 of file ffmpeg_transcoder.h.

◆ m_hwaccel_enable_dec_buffering

bool FFmpeg_Transcoder::m_hwaccel_enable_dec_buffering
private

Enable hardware acceleration frame buffers for decoder.

Definition at line 1289 of file ffmpeg_transcoder.h.

Referenced by decode_video_frame(), and get_pix_formats().

◆ m_hwaccel_enable_enc_buffering

bool FFmpeg_Transcoder::m_hwaccel_enable_enc_buffering
private

Enable hardware acceleration frame buffers for encoder.

Definition at line 1288 of file ffmpeg_transcoder.h.

Referenced by add_stream(), encode_video_frame(), get_pix_formats(), open_bestmatch_video(), open_output(), and update_codec().

◆ m_hwaccel_enc_device_ctx

AVBufferRef* FFmpeg_Transcoder::m_hwaccel_enc_device_ctx
private

Hardware acceleration device context for encoder.

Definition at line 1290 of file ffmpeg_transcoder.h.

Referenced by add_stream(), closeio(), open_bestmatch_video(), and open_output().

◆ m_hwaccel_enc_mode

HWACCELMODE FFmpeg_Transcoder::m_hwaccel_enc_mode
private

Current hardware acceleration mode for encoder.

Definition at line 1286 of file ffmpeg_transcoder.h.

Referenced by add_stream(), and open_output().

◆ m_in

INPUTFILE FFmpeg_Transcoder::m_in
private

◆ m_inhibit_stream_msk

uint32_t FFmpeg_Transcoder::m_inhibit_stream_msk
private

HLS: Currently inhibited streams bit mask. Packets temporarly go to m_hls_packet_fifo and will be prepended to next segment. Set FFMPEGFS_AUDIO and/or FFMPEGFS_VIDEO.

Definition at line 1300 of file ffmpeg_transcoder.h.

Referenced by open_output_filestreams(), process_single_fr(), start_new_segment(), and store_packet().

◆ m_insert_keyframe

bool FFmpeg_Transcoder::m_insert_keyframe
private

HLS only: Allow insertion of 1 keyframe.

Definition at line 1265 of file ffmpeg_transcoder.h.

Referenced by decode_video_frame(), open_output(), and start_new_segment().

◆ m_is_video

bool FFmpeg_Transcoder::m_is_video
private

true if input is a video file

Definition at line 1236 of file ffmpeg_transcoder.h.

Referenced by calculate_predicted_filesize(), open_bestmatch_video(), and open_output_filestreams().

◆ m_last_seek_frame_no

std::atomic_uint32_t FFmpeg_Transcoder::m_last_seek_frame_no
private

If not 0, this is the last frame that we seeked to. Video sources only.

Definition at line 1233 of file ffmpeg_transcoder.h.

Referenced by encode_image_frame(), last_seek_frame_no(), and seek_frame().

◆ m_mtime

time_t FFmpeg_Transcoder::m_mtime
private

Modified time of input file.

Definition at line 1230 of file ffmpeg_transcoder.h.

Referenced by FFmpeg_Transcoder(), mtime(), and open_input_file().

◆ m_out

OUTPUTFILE FFmpeg_Transcoder::m_out
private

◆ m_pos

int64_t FFmpeg_Transcoder::m_pos
private

Generated position.

Definition at line 1257 of file ffmpeg_transcoder.h.

Referenced by decode_video_frame().

◆ m_prores_bitrate

const std::vector< FFmpeg_Transcoder::PRORES_BITRATE > FFmpeg_Transcoder::m_prores_bitrate
staticprivate

ProRes bitrate table. Used for file size prediction.

Definition at line 1282 of file ffmpeg_transcoder.h.

Referenced by get_prores_bitrate().

◆ m_pts

int64_t FFmpeg_Transcoder::m_pts
private

Generated PTS.

Definition at line 1256 of file ffmpeg_transcoder.h.

Referenced by decode_video_frame().

◆ m_reset_pts

uint32_t FFmpeg_Transcoder::m_reset_pts
private

We have to reset audio/video pts to the new position.

Definition at line 1279 of file ffmpeg_transcoder.h.

Referenced by decode_frame(), and start_new_segment().

◆ m_seek_to_fifo

std::queue<uint32_t> FFmpeg_Transcoder::m_seek_to_fifo
private

Stack of seek requests. Will be processed FIFO.

Definition at line 1232 of file ffmpeg_transcoder.h.

Referenced by open_output_frame_set(), seek_frame(), stack_seek_frame(), stack_seek_segment(), and start_new_segment().

◆ m_seek_to_fifo_mutex

std::recursive_mutex FFmpeg_Transcoder::m_seek_to_fifo_mutex
private

Access mutex for seek FIFO.

Definition at line 1231 of file ffmpeg_transcoder.h.

Referenced by open_output_frame_set(), seek_frame(), stack_seek_frame(), and stack_seek_segment().

◆ m_skip_next_frame

bool FFmpeg_Transcoder::m_skip_next_frame
private

After seek, skip next video frame.

Definition at line 1235 of file ffmpeg_transcoder.h.

Referenced by do_seek_frame(), and encode_image_frame().

◆ m_stream_map

STREAM_MAP FFmpeg_Transcoder::m_stream_map
private

Input stream to output stream map.

Definition at line 1262 of file ffmpeg_transcoder.h.

Referenced by add_stream_map(), and map_in_to_out_stream().

◆ m_sws_ctx

SwsContext* FFmpeg_Transcoder::m_sws_ctx
private

Context for video filtering.

Definition at line 1252 of file ffmpeg_transcoder.h.

Referenced by close_output_file(), decode_video_frame(), and init_rescaler().


The documentation for this class was generated from the following files: