v4l2m2m/sim encoder: move hostencoder to x264encoder

Signed-off-by: shizhenghui <shizhenghui@xiaomi.com>
This commit is contained in:
shizhenghui 2024-07-04 20:55:35 +08:00 committed by Alan Carvalho de Assis
parent 7248ba0293
commit 61f79757f7
5 changed files with 50 additions and 57 deletions

View File

@ -256,9 +256,9 @@ ifeq ($(CONFIG_SIM_VIDEO_DECODER),y)
endif endif
ifeq ($(CONFIG_SIM_VIDEO_ENCODER),y) ifeq ($(CONFIG_SIM_VIDEO_ENCODER),y)
HOSTSRCS += sim_hostencoder.c
CSRCS += sim_encoder.c CSRCS += sim_encoder.c
STDLIBS += -lx264 CSRCS += sim_x264encoder.c
CFLAGS += -D_STDINT_H
endif endif
COBJS = $(CSRCS:.c=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT))

View File

@ -161,9 +161,8 @@ if(CONFIG_SIM_VIDEO_DECODER)
endif() endif()
if(CONFIG_SIM_VIDEO_ENCODER) if(CONFIG_SIM_VIDEO_ENCODER)
list(APPEND HOSTSRCS sim_hostencoder.c)
list(APPEND SRCS sim_encoder.c) list(APPEND SRCS sim_encoder.c)
list(APPEND STDLIBS x264) list(APPEND SRCS sim_x264encoder.c)
endif() endif()
if(CONFIG_SPINLOCK) if(CONFIG_SPINLOCK)

View File

@ -27,7 +27,7 @@
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/video/v4l2_m2m.h> #include <nuttx/video/v4l2_m2m.h>
#include "sim_hostencoder.h" #include "sim_x264encoder.h"
#include "sim_internal.h" #include "sim_internal.h"
/**************************************************************************** /****************************************************************************
@ -42,7 +42,7 @@
struct sim_encoder_s struct sim_encoder_s
{ {
struct host_encoder_s *encoder; struct x264_wrapper_s *encoder;
struct v4l2_format output_fmt; struct v4l2_format output_fmt;
struct v4l2_format capture_fmt; struct v4l2_format capture_fmt;
struct work_s work; struct work_s work;
@ -140,7 +140,7 @@ static struct codec_s g_sim_codec_encoder =
static int sim_encoder_open(void *cookie, void **priv) static int sim_encoder_open(void *cookie, void **priv)
{ {
sim_encoder_t *sim_encoder; sim_encoder_t *sim_encoder;
struct host_encoder_s *encoder; struct x264_wrapper_s *encoder;
sim_encoder = kmm_zalloc(sizeof(struct sim_encoder_s)); sim_encoder = kmm_zalloc(sizeof(struct sim_encoder_s));
if (sim_encoder == NULL) if (sim_encoder == NULL)
@ -148,7 +148,7 @@ static int sim_encoder_open(void *cookie, void **priv)
return -ENOMEM; return -ENOMEM;
} }
encoder = host_encoder_open(); encoder = x264_wrapper_open();
if (encoder == NULL) if (encoder == NULL)
{ {
kmm_free(sim_encoder); kmm_free(sim_encoder);
@ -166,7 +166,7 @@ static int sim_encoder_close(void *priv)
{ {
sim_encoder_t *sim_encoder = priv; sim_encoder_t *sim_encoder = priv;
host_encoder_close(sim_encoder->encoder); x264_wrapper_close(sim_encoder->encoder);
kmm_free(sim_encoder); kmm_free(sim_encoder);
return 0; return 0;
@ -187,7 +187,7 @@ static int sim_encoder_output_streamon(void *priv)
{ {
sim_encoder_t *sim_encoder = priv; sim_encoder_t *sim_encoder = priv;
return host_encoder_streamon(sim_encoder->encoder, return x264_wrapper_streamon(sim_encoder->encoder,
sim_encoder->output_fmt.fmt.pix.width, sim_encoder->output_fmt.fmt.pix.width,
sim_encoder->output_fmt.fmt.pix.height, sim_encoder->output_fmt.fmt.pix.height,
sim_encoder->fps ? sim_encoder->fps : 30, sim_encoder->fps ? sim_encoder->fps : 30,
@ -227,7 +227,7 @@ static int sim_encoder_capture_streamoff(void *priv)
sim_encoder_t *sim_encoder = priv; sim_encoder_t *sim_encoder = priv;
sim_encoder->capture_on = false; sim_encoder->capture_on = false;
return host_encoder_streamoff(sim_encoder->encoder); return x264_wrapper_streamoff(sim_encoder->encoder);
} }
static int sim_encoder_output_streamoff(void *priv) static int sim_encoder_output_streamoff(void *priv)
@ -475,7 +475,7 @@ static int sim_encoder_process(sim_encoder_t *sim_encoder,
src_buf->timestamp.tv_usec; src_buf->timestamp.tv_usec;
} }
ret = host_encoder_enqueue(sim_encoder->encoder, ret = x264_wrapper_enqueue(sim_encoder->encoder,
src_data, src_size, src_pts); src_data, src_size, src_pts);
if (ret >= 0 && src_buf != NULL) if (ret >= 0 && src_buf != NULL)
{ {
@ -487,7 +487,7 @@ static int sim_encoder_process(sim_encoder_t *sim_encoder,
return ret; return ret;
} }
ret = host_encoder_dequeue(sim_encoder->encoder, ret = x264_wrapper_dequeue(sim_encoder->encoder,
(uint8_t *)dst_buf->m.userptr, (uint8_t *)dst_buf->m.userptr,
&dst_buf->bytesused, &dst_buf->bytesused,
&dst_pts, &dst_pts,

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* arch/sim/src/sim/sim_hostencoder.c * arch/sim/src/sim/sim_x264encoder.c
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
@ -30,14 +30,13 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <x264.h> #include <x264.h>
#include "sim_hostencoder.h" #include "sim_x264encoder.h"
#include "sim_internal.h"
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
struct host_encoder_s struct x264_wrapper_s
{ {
x264_t *enc_ctx; x264_t *enc_ctx;
x264_picture_t pic_in; x264_picture_t pic_in;
@ -52,28 +51,27 @@ struct host_encoder_s
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
struct host_encoder_s *host_encoder_open(void) struct x264_wrapper_s *x264_wrapper_open(void)
{ {
return calloc(1, sizeof(struct host_encoder_s)); return calloc(1, sizeof(struct x264_wrapper_s));
} }
int host_encoder_close(struct host_encoder_s *encoder) int x264_wrapper_close(struct x264_wrapper_s *encoder)
{ {
free(encoder); free(encoder);
return 0; return 0;
} }
int host_encoder_streamon(struct host_encoder_s *encoder, int x264_wrapper_streamon(struct x264_wrapper_s *encoder,
int width, int height, int fps, int bframe) int width, int height, int fps, int bframe)
{ {
int ret; int ret;
memset(&encoder->param, 0, sizeof(x264_param_t)); memset(&encoder->param, 0, sizeof(x264_param_t));
ret = host_uninterruptible(x264_param_default_preset, ret = x264_param_default_preset(&encoder->param,
&encoder->param, "fast",
"fast", "zerolatency");
"zerolatency");
if (ret < 0) if (ret < 0)
{ {
return ret; return ret;
@ -89,37 +87,35 @@ int host_encoder_streamon(struct host_encoder_s *encoder,
encoder->param.i_keyint_min = 25; encoder->param.i_keyint_min = 25;
encoder->param.i_bframe = bframe; encoder->param.i_bframe = bframe;
ret = host_uninterruptible(x264_picture_alloc, ret = x264_picture_alloc(&encoder->pic_in,
&encoder->pic_in, X264_CSP_I420,
X264_CSP_I420, width,
width, height);
height);
if (ret < 0) if (ret < 0)
{ {
return ret; return ret;
} }
encoder->enc_ctx = host_uninterruptible(x264_encoder_open, encoder->enc_ctx = x264_encoder_open(&encoder->param);
&encoder->param);
if (!encoder->enc_ctx) if (!encoder->enc_ctx)
{ {
host_uninterruptible_no_return(x264_picture_clean, &encoder->pic_in); x264_picture_clean(&encoder->pic_in);
return -EINVAL; return -EINVAL;
} }
return 0; return 0;
} }
int host_encoder_streamoff(struct host_encoder_s *encoder) int x264_wrapper_streamoff(struct x264_wrapper_s *encoder)
{ {
host_uninterruptible_no_return(x264_encoder_close, encoder->enc_ctx); x264_encoder_close(encoder->enc_ctx);
host_uninterruptible_no_return(x264_picture_clean, &encoder->pic_in); x264_picture_clean(&encoder->pic_in);
encoder->remaining_frames = 0; encoder->remaining_frames = 0;
return 0; return 0;
} }
int host_encoder_enqueue(struct host_encoder_s *encoder, int x264_wrapper_enqueue(struct x264_wrapper_s *encoder,
uint8_t *data, uint32_t size, int64_t pts) uint8_t *data, uint32_t size, int64_t pts)
{ {
int ret; int ret;
@ -137,18 +133,16 @@ int host_encoder_enqueue(struct host_encoder_s *encoder,
width * height / 4); width * height / 4);
} }
ret = host_uninterruptible(x264_encoder_encode, ret = x264_encoder_encode(encoder->enc_ctx,
encoder->enc_ctx, &encoder->nal,
&encoder->nal, &encoder->i_nal,
&encoder->i_nal, (data != NULL ? &encoder->pic_in : NULL),
(data != NULL ? &encoder->pic_in : NULL), &encoder->pic_out);
&encoder->pic_out);
if (data == NULL) if (data == NULL)
{ {
encoder->remaining_frames = encoder->remaining_frames =
host_uninterruptible(x264_encoder_delayed_frames, x264_encoder_delayed_frames(encoder->enc_ctx);
encoder->enc_ctx);
} }
if (ret >= 0) if (ret >= 0)
@ -159,7 +153,7 @@ int host_encoder_enqueue(struct host_encoder_s *encoder,
return ret; return ret;
} }
int host_encoder_dequeue(struct host_encoder_s *encoder, int x264_wrapper_dequeue(struct x264_wrapper_s *encoder,
uint8_t *data, uint32_t *size, uint8_t *data, uint32_t *size,
int64_t *pts, uint32_t *flags) int64_t *pts, uint32_t *flags)
{ {

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* arch/sim/src/sim/sim_hostencoder.h * arch/sim/src/sim/sim_x264encoder.h
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
@ -18,8 +18,8 @@
* *
****************************************************************************/ ****************************************************************************/
#ifndef __ARCH_SIM_SRC_SIM_SIM_HOSTENCODER_H #ifndef __ARCH_SIM_SRC_SIM_SIM_X264ENCODER_H
#define __ARCH_SIM_SRC_SIM_SIM_HOSTENCODER_H #define __ARCH_SIM_SRC_SIM_SIM_X264ENCODER_H
/**************************************************************************** /****************************************************************************
* Included Files * Included Files
@ -31,21 +31,21 @@
* Public Types * Public Types
****************************************************************************/ ****************************************************************************/
struct host_encoder_s; struct x264_wrapper_s;
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
struct host_encoder_s *host_encoder_open(void); struct x264_wrapper_s *x264_wrapper_open(void);
int host_encoder_close(struct host_encoder_s *encoder); int x264_wrapper_close(struct x264_wrapper_s *encoder);
int host_encoder_streamon(struct host_encoder_s *encoder, int x264_wrapper_streamon(struct x264_wrapper_s *encoder,
int width, int height, int fps, int bframe); int width, int height, int fps, int bframe);
int host_encoder_streamoff(struct host_encoder_s *encoder); int x264_wrapper_streamoff(struct x264_wrapper_s *encoder);
int host_encoder_enqueue(struct host_encoder_s *encoder, int x264_wrapper_enqueue(struct x264_wrapper_s *encoder,
uint8_t *data, uint32_t size, int64_t pts); uint8_t *data, uint32_t size, int64_t pts);
int host_encoder_dequeue(struct host_encoder_s *encoder, int x264_wrapper_dequeue(struct x264_wrapper_s *encoder,
uint8_t *data, uint32_t *size, uint8_t *data, uint32_t *size,
int64_t *pts, uint32_t *flags); int64_t *pts, uint32_t *flags);
#endif /* __ARCH_SIM_SRC_SIM_SIM_HOSTENCODER_H */ #endif /* __ARCH_SIM_SRC_SIM_SIM_X264ENCODER_H */