2024-10-08 02:29:00 +00:00
|
|
|
#ifndef ENCODER_H
|
|
|
|
#define ENCODER_H
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
#include <cstdint>
|
2024-11-02 02:19:01 +00:00
|
|
|
#include <filesystem>
|
2024-12-03 05:22:07 +00:00
|
|
|
#include <vector>
|
2024-11-02 02:19:01 +00:00
|
|
|
|
2024-10-10 07:23:13 +00:00
|
|
|
extern "C" {
|
2024-12-03 05:22:07 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2024-10-08 02:29:00 +00:00
|
|
|
#include <libavformat/avformat.h>
|
2024-11-17 00:00:00 +00:00
|
|
|
#include <libavutil/pixdesc.h>
|
2024-10-10 07:23:13 +00:00
|
|
|
}
|
2024-10-08 02:29:00 +00:00
|
|
|
|
2024-12-03 05:22:07 +00:00
|
|
|
#include "fsutils.h"
|
|
|
|
|
|
|
|
// Encoder configurations
|
|
|
|
struct EncoderConfig {
|
|
|
|
// Non-AVCodecContext options
|
|
|
|
AVCodecID codec;
|
|
|
|
bool copy_streams;
|
|
|
|
|
|
|
|
// Basic video options
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int frm_rate_mul;
|
|
|
|
AVPixelFormat pix_fmt;
|
|
|
|
|
|
|
|
// Rate control and compression
|
|
|
|
int64_t bit_rate;
|
|
|
|
int rc_buffer_size;
|
|
|
|
int rc_min_rate;
|
|
|
|
int rc_max_rate;
|
|
|
|
int qmin;
|
|
|
|
int qmax;
|
|
|
|
|
|
|
|
// GOP and frame structure
|
|
|
|
int gop_size;
|
|
|
|
int max_b_frames;
|
|
|
|
int keyint_min;
|
|
|
|
int refs;
|
|
|
|
|
|
|
|
// Performance and threading
|
|
|
|
int thread_count;
|
|
|
|
|
|
|
|
// Latency and buffering
|
|
|
|
int delay;
|
|
|
|
|
|
|
|
// Extra AVOptions
|
|
|
|
std::vector<std::pair<StringType, StringType>> extra_opts;
|
|
|
|
};
|
2024-11-17 00:00:00 +00:00
|
|
|
|
|
|
|
class Encoder {
|
|
|
|
public:
|
|
|
|
Encoder();
|
|
|
|
~Encoder();
|
|
|
|
|
|
|
|
int init(
|
|
|
|
AVBufferRef *hw_ctx,
|
|
|
|
const std::filesystem::path &out_fpath,
|
|
|
|
AVFormatContext *ifmt_ctx,
|
|
|
|
AVCodecContext *dec_ctx,
|
2024-12-02 07:24:30 +00:00
|
|
|
EncoderConfig &enc_cfg,
|
2024-11-17 00:00:00 +00:00
|
|
|
int in_vstream_idx
|
|
|
|
);
|
|
|
|
|
|
|
|
int write_frame(AVFrame *frame, int64_t frame_idx);
|
|
|
|
int flush();
|
|
|
|
|
|
|
|
AVCodecContext *get_encoder_context() const;
|
|
|
|
AVFormatContext *get_format_context() const;
|
|
|
|
int *get_stream_map() const;
|
|
|
|
int get_output_video_stream_index() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
AVFormatContext *ofmt_ctx_;
|
|
|
|
AVCodecContext *enc_ctx_;
|
|
|
|
int out_vstream_idx_;
|
|
|
|
int *stream_map_;
|
|
|
|
};
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
#endif // ENCODER_H
|