Skip to content

Commit

Permalink
WIP: 002
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Mar 15, 2024
1 parent f5ace9f commit 89b5e56
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 79 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ if(BUILD_TOXAV)
message(${NO_TOXAV_ERROR_TYPE} "Option BUILD_TOXAV is enabled but required library X264 was not found.")
set(BUILD_TOXAV OFF)
endif()
if(NOT X265_FOUND)
message(${NO_TOXAV_ERROR_TYPE} "Option BUILD_TOXAV is enabled but required library X265 was not found.")
set(BUILD_TOXAV OFF)
endif()
if(NOT LIBAVCODEC_FOUND)
message(${NO_TOXAV_ERROR_TYPE} "Option BUILD_TOXAV is enabled but required library LIBAVCODEC was not found.")
set(BUILD_TOXAV OFF)
Expand Down Expand Up @@ -369,7 +373,7 @@ if(BUILD_TOXAV)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)

set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES} ${X264_LIBRARIES} ${LIBAVCODEC_LIBRARIES} ${LIBAVUTIL_LIBRARIES})
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES} ${X264_LIBRARIES} ${X265_LIBRARIES} ${LIBAVCODEC_LIBRARIES} ${LIBAVUTIL_LIBRARIES})
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} opus vpx x264 libavcodec libavutil)
endif()

Expand Down
1 change: 1 addition & 0 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pkg_use_module(LIBSODIUM libsodium )
pkg_use_module(OPUS "opus;Opus" )
pkg_use_module(VPX "vpx;libvpx" )
pkg_use_module(X264 x264 )
pkg_use_module(X265 x265 )
pkg_use_module(LIBAVCODEC libavcodec )
pkg_use_module(LIBAVUTIL libavutil )

Expand Down
14 changes: 12 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,16 @@ if test "x$BUILD_AV" = "xyes"; then
)
fi

if test "x$BUILD_AV" = "xyes"; then
PKG_CHECK_MODULES([X265], [x265],
[],
[
AC_MSG_WARN([disabling AV support $X265_PKG_ERRORS])
BUILD_AV="no"
]
)
fi

if test "x$BUILD_AV" = "xyes"; then
PKG_CHECK_MODULES([OPUS], [opus],
[],
Expand All @@ -514,10 +524,10 @@ fi
if test "x$BUILD_AV" = "xyes"; then
# toxcore lib needs an global?
# So far this works okay
AV_LIBS="$OPUS_LIBS $VPX_LIBS $LIBAVCODEC_LIBS $X264_LIBS"
AV_LIBS="$OPUS_LIBS $VPX_LIBS $LIBAVCODEC_LIBS $X264_LIBS $X265_LIBS"
AC_SUBST(AV_LIBS)

AV_CFLAGS="$OPUS_CFLAGS $VPX_CFLAGS $LIBAVCODEC_CFLAGS $X264_CFLAGS"
AV_CFLAGS="$OPUS_CFLAGS $VPX_CFLAGS $LIBAVCODEC_CFLAGS $X264_CFLAGS $X265_CFLAGS"
AC_SUBST(AV_CFLAGS)
fi

Expand Down
105 changes: 32 additions & 73 deletions toxav/codecs/h264/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1927,85 +1927,36 @@ void vc_kill_h264(VCSession *vc)
// --------- H265 ---------
// --------- H265 ---------


VCSession *vc_new_h265(Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data,
VCSession *vc)
{
#ifdef HAVE_H265_ENCODER
// ENCODER -------
x265_param *param = x265_param_alloc();
if (x265_param_default_preset(param, "ultrafast", "zerolatency,fastdecode") < 0) {
// goto fail;
}

// ------ ffmpeg encoder --------------------------------------------------------------
// ------ ffmpeg encoder --------------------------------------------------------------
// ------ ffmpeg encoder --------------------------------------------------------------
// ------ ffmpeg encoder --------------------------------------------------------------
// ------ ffmpeg encoder --------------------------------------------------------------

AVCodec *codec2 = NULL;
vc->h265_encoder2 = NULL;


#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
avcodec_register_all();
#endif

codec2 = NULL;
codec2 = avcodec_find_encoder_by_name("libx265");
LOGGER_API_ERROR(av->tox, "FOUND: H265 encoder");

vc->h265_encoder2 = avcodec_alloc_context3(codec2);
vc->h265_out_pic2 = av_packet_alloc();

av_opt_set(vc->h265_encoder2->priv_data, "annex_b", "1", 0);
av_opt_set(vc->h265_encoder2->priv_data, "repeat_headers", "1", 0);
av_opt_set_int(vc->h265_encoder2->priv_data, "b", VIDEO_BITRATE_INITIAL_VALUE_H264 * 1000, 0);
av_opt_set_int(vc->h265_encoder2->priv_data, "bitrate", VIDEO_BITRATE_INITIAL_VALUE_H264 * 1000, 0);

av_opt_set_int(vc->h265_encoder2->priv_data, "delay", 0, 0);
av_opt_set(vc->h265_encoder2->priv_data, "preset", "ultrafast", 0);

av_opt_set_int(vc->h265_encoder2->priv_data, "zerolatency", 1, AV_OPT_SEARCH_CHILDREN);

av_opt_set(vc->h265_encoder2->priv_data, "2pass", "false", 0);

av_opt_set_int(vc->h265_encoder2->priv_data, "threads", 3, 0);

/* put sample parameters */
vc->h265_encoder2->bit_rate = VIDEO_BITRATE_INITIAL_VALUE_H264 * 1000;
vc->h264_enc_bitrate = VIDEO_BITRATE_INITIAL_VALUE_H264 * 1000;

/* resolution must be a multiple of two */
vc->h265_encoder2->width = 1920;
vc->h265_encoder2->height = 1080;

vc->h264_enc_width = vc->h265_encoder2->width;
vc->h264_enc_height = vc->h265_encoder2->height;

vc->h265_encoder2->max_b_frames = 0;
vc->h265_encoder2->pix_fmt = AV_PIX_FMT_YUV420P;

vc->h265_encoder2->time_base.num = 1;
vc->h265_encoder2->time_base.den = 1000;

// without these it won't work !! ---------------------
vc->h265_encoder2->time_base = (AVRational) {
1, 30
};
vc->h265_encoder2->framerate = (AVRational) {
30, 1
};
// without these it won't work !! ---------------------

// vc->h264_encoder2->flags2 |= AV_CODEC_FLAG2_LOCAL_HEADER;
// vc->h264_encoder2->flags2 |= AV_CODEC_FLAG2_FAST;

AVDictionary *opts = NULL;
/* Configure non-default params */
// param.i_bitdepth = 8;
/*
param.i_csp = X264_CSP_I420;
param.i_width = 1920;
param.i_height = 1080;
*/
vc->h264_enc_width = param.i_width;
vc->h264_enc_height = param.i_height;

if (x265_picture_alloc(vc->h265_in_pic) < 0) {
// goto fail;
}

if (avcodec_open2(vc->h265_encoder2, codec2, &opts) < 0) {
LOGGER_API_ERROR(av->tox, "could not open codec H264 on encoder");
}

av_dict_free(&opts);
x265_picture_init(param, vc->h265_in_pic);

vc->h265_encoder = x265_encoder_open(param);
x265_param_free(param);
// ENCODER -------
#endif


// DECODER -------
Expand Down Expand Up @@ -2079,6 +2030,8 @@ int vc_reconfigure_encoder_h265(Logger *log, VCSession *vc, uint32_t bit_rate,
uint16_t width, uint16_t height,
int16_t kf_max_dist)
{
#ifdef HAVE_H265_ENCODER
#endif
return 0;
}

Expand All @@ -2101,6 +2054,8 @@ uint32_t encode_frame_h265(ToxAV *av, uint32_t friend_number, uint16_t width, ui
x264_nal_t **nal,
int *i_frame_size)
{
#ifdef HAVE_H265_ENCODER
#endif
return 0;
}

Expand All @@ -2113,14 +2068,18 @@ uint32_t send_frames_h265(ToxAV *av, uint32_t friend_number, uint16_t width, uin
int *i_frame_size,
TOXAV_ERR_SEND_FRAME *rc)
{
#ifdef HAVE_H265_ENCODER
#endif
return 0;
}

void vc_kill_h265(VCSession *vc)
{
#ifdef HAVE_H265_ENCODER
// encoder
av_packet_free(&(vc->h264_out_pic2));
avcodec_free_context(&(vc->h265_encoder2));
x265_encoder_close(vc->h265_encoder);
x265_picture_free(vc->h265_in_pic);
#endif

// decoder
if (vc->h265_decoder->extradata) {
Expand Down
10 changes: 7 additions & 3 deletions toxav/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,16 @@ typedef struct VCSession_s {
int h264_enc_height;
uint32_t h264_enc_bitrate;

#ifdef HAVE_H265_ENCODER
// ------ h265 encoder ------
x265_encoder *h265_encoder;
x265_picture *h265_in_pic;
// ------ h265 encoder ------
#endif

// ------ ffmpeg encoder ------
AVCodecContext *h264_encoder2;
AVPacket *h264_out_pic2;

AVCodecContext *h265_encoder2;
AVPacket *h265_out_pic2;
// ------ ffmpeg encoder ------

char *encoder_codec_used_name;
Expand Down

0 comments on commit 89b5e56

Please sign in to comment.