32 #include <libcjson/cJSON.h> 33 #include <libmediaprocsutils/uri_parser.h> 34 #include <libavcodec/avcodec.h> 35 #include <libmediaprocsutils/log.h> 36 #include <libmediaprocsutils/stat_codes.h> 37 #include <libmediaprocsutils/check_utils.h> 38 #include <libmediaprocsutils/fifo.h> 39 #include <libmediaprocs/proc_if.h> 40 #include <libmediaprocs/proc.h> 113 const char *settings_str,
log_ctx_t *log_ctx, va_list arg);
132 const char *settings_str,
log_ctx_t *log_ctx, va_list arg);
152 "ffmpeg_x264_enc",
"encoder",
"video/H264",
153 (uint64_t)(PROC_FEATURE_BITRATE|PROC_FEATURE_REGISTER_PTS|
154 PROC_FEATURE_LATENCY),
157 proc_send_frame_default1,
159 proc_recv_frame_default1,
172 "ffmpeg_x264_dec",
"decoder",
"video/H264",
173 (uint64_t)(PROC_FEATURE_BITRATE|PROC_FEATURE_REGISTER_PTS|
174 PROC_FEATURE_LATENCY),
177 proc_send_frame_default1,
179 proc_recv_frame_default1,
195 const char *settings_str,
log_ctx_t *log_ctx, va_list arg)
197 int ret_code, end_code= STAT_ERROR;
204 LOG_CTX_INIT(log_ctx);
207 CHECK_DO(proc_if!= NULL,
return NULL);
208 CHECK_DO(settings_str!= NULL,
return NULL);
214 CHECK_DO(ffmpeg_x264_enc_ctx!= NULL,
goto end);
217 ffmpeg_x264_enc_settings_ctx=
223 CHECK_DO(ret_code== STAT_SUCCESS,
goto end);
228 CHECK_DO(ret_code== STAT_SUCCESS,
goto end);
233 ffmpeg_x264_enc_settings_ctx;
237 (
int)AV_CODEC_ID_H264,
240 CHECK_DO(ret_code== STAT_SUCCESS,
goto end);
242 end_code= STAT_SUCCESS;
244 if(end_code!= STAT_SUCCESS)
257 if(ref_proc_ctx== NULL)
261 LOG_CTX_INIT(((
proc_ctx_t*)ffmpeg_x264_enc_ctx)->log_ctx);
275 free(ffmpeg_x264_enc_ctx);
287 int ret_code, end_code= STAT_ERROR;
290 AVFrame *avframe_iput= NULL;
291 size_t fifo_elem_size= 0;
295 CHECK_DO(proc_ctx!= NULL,
return STAT_ERROR);
296 CHECK_DO(iput_fifo_ctx!= NULL,
return STAT_ERROR);
297 CHECK_DO(oput_fifo_ctx!= NULL,
return STAT_ERROR);
299 LOG_CTX_SET(proc_ctx->
log_ctx);
306 ret_code=
fifo_get(iput_fifo_ctx, (
void**)&avframe_iput, &fifo_elem_size);
307 CHECK_DO(ret_code== STAT_SUCCESS || ret_code== STAT_EAGAIN,
goto end);
308 if(ret_code== STAT_EAGAIN) {
316 oput_fifo_ctx, LOG_CTX_GET());
317 CHECK_DO(ret_code== STAT_SUCCESS || ret_code== STAT_EAGAIN,
goto end);
319 end_code= STAT_SUCCESS;
321 if(avframe_iput!= NULL)
322 av_frame_free(&avframe_iput);
332 int ret_code, end_code= STAT_ERROR;
333 int flag_is_query= 0;
338 cJSON *cjson_rest= NULL, *cjson_aux= NULL;
339 char *flag_zerolatency_str= NULL;
343 CHECK_DO(proc_ctx!= NULL,
return STAT_ERROR);
344 CHECK_DO(str!= NULL,
return STAT_ERROR);
346 LOG_CTX_SET(proc_ctx->
log_ctx);
350 ffmpeg_x264_enc_settings_ctx=
352 video_settings_enc_ctx=
358 if(ret_code!= STAT_SUCCESS)
366 flag_is_query= (str[0]==
'{' && str[strlen(str)-1]==
'}')? 0: 1;
369 if(flag_is_query== 1) {
372 flag_zerolatency_str= uri_parser_query_str_get_value(
373 "flag_zerolatency", str);
374 if(flag_zerolatency_str!= NULL)
376 flag_zerolatency_str,
"true", strlen(
"true"))== 0)? 1: 0;
381 cjson_rest= cJSON_Parse(str);
382 CHECK_DO(cjson_rest!= NULL,
goto end);
385 cjson_aux= cJSON_GetObjectItem(cjson_rest,
"flag_zerolatency");
388 (cjson_aux->type==cJSON_True)?1 : 0;
399 av_dict_set(&ffmpeg_video_enc_ctx->
avdictionary,
"tune",
407 (
volatile void*)video_settings_enc_ctx, 1,
410 end_code= STAT_SUCCESS;
412 if(cjson_rest!= NULL)
413 cJSON_Delete(cjson_rest);
414 if(flag_zerolatency_str!= NULL)
415 free(flag_zerolatency_str);
426 int ret_code, end_code= STAT_ERROR;
429 AVCodecContext *avcodecctx= NULL;
432 cJSON *cjson_rest= NULL, *cjson_settings= NULL;
433 cJSON *cjson_aux= NULL;
437 CHECK_DO(proc_ctx!= NULL,
return STAT_ERROR);
438 CHECK_DO(rest_fmt< PROC_IF_REST_FMT_ENUM_MAX,
return STAT_ERROR);
439 CHECK_DO(ref_reponse!= NULL,
return STAT_ERROR);
441 LOG_CTX_SET(proc_ctx->
log_ctx);
446 cjson_rest= cJSON_CreateObject();
447 CHECK_DO(cjson_rest!= NULL,
goto end);
461 ffmpeg_x264_enc_settings_ctx=
463 video_settings_enc_ctx=
468 &cjson_settings, LOG_CTX_GET());
469 CHECK_DO(ret_code== STAT_SUCCESS && cjson_settings!= NULL,
goto end);
474 cjson_aux= cJSON_CreateBool(ffmpeg_x264_enc_settings_ctx->
flag_zerolatency);
475 CHECK_DO(cjson_aux!= NULL,
goto end);
476 cJSON_AddItemToObject(cjson_settings,
"flag_zerolatency", cjson_aux);
482 cJSON_AddItemToObject(cjson_rest,
"settings", cjson_settings);
483 cjson_settings= NULL;
489 CHECK_DO(avcodecctx!= NULL,
goto end);
505 CHECK_DO(*ref_reponse!= NULL && strlen((
char*)*ref_reponse)> 0,
509 *ref_reponse= (
void*)cjson_rest;
513 LOGE(
"Unknown format requested for processor REST\n");
517 end_code= STAT_SUCCESS;
519 if(cjson_settings!= NULL)
520 cJSON_Delete(cjson_settings);
521 if(cjson_rest!= NULL)
522 cJSON_Delete(cjson_rest);
539 LOG_CTX_INIT(log_ctx);
542 CHECK_DO(ffmpeg_x264_enc_settings_ctx!= NULL,
return STAT_ERROR);
544 video_settings_enc_ctx=
549 if(ret_code!= STAT_SUCCESS)
572 LOG_CTX_INIT(log_ctx);
575 CHECK_DO(ffmpeg_x264_enc_settings_ctx!= NULL,
return);
577 video_settings_enc_ctx=
592 const char *settings_str,
log_ctx_t *log_ctx, va_list arg)
594 int ret_code, end_code= STAT_ERROR;
601 LOG_CTX_INIT(log_ctx);
604 CHECK_DO(proc_if!= NULL,
return NULL);
605 CHECK_DO(settings_str!= NULL,
return NULL);
611 CHECK_DO(ffmpeg_x264_dec_ctx!= NULL,
goto end);
614 ffmpeg_x264_dec_settings_ctx=
620 CHECK_DO(ret_code== STAT_SUCCESS,
goto end);
625 CHECK_DO(ret_code== STAT_SUCCESS,
goto end);
630 ffmpeg_x264_dec_settings_ctx;
634 (
int)AV_CODEC_ID_H264,
637 CHECK_DO(ret_code== STAT_SUCCESS,
goto end);
639 end_code= STAT_SUCCESS;
641 if(end_code!= STAT_SUCCESS)
654 if(ref_proc_ctx== NULL)
658 LOG_CTX_INIT(((
proc_ctx_t*)ffmpeg_x264_dec_ctx)->log_ctx);
672 free(ffmpeg_x264_dec_ctx);
684 int ret_code, end_code= STAT_ERROR;
687 AVPacket *avpacket_iput= NULL;
688 size_t fifo_elem_size= 0;
692 CHECK_DO(proc_ctx!= NULL,
return STAT_ERROR);
693 CHECK_DO(iput_fifo_ctx!= NULL,
return STAT_ERROR);
694 CHECK_DO(oput_fifo_ctx!= NULL,
return STAT_ERROR);
696 LOG_CTX_SET(proc_ctx->
log_ctx);
703 ret_code=
fifo_get(iput_fifo_ctx, (
void**)&avpacket_iput, &fifo_elem_size);
704 CHECK_DO(ret_code== STAT_SUCCESS || ret_code== STAT_EAGAIN,
goto end);
705 if(ret_code== STAT_EAGAIN) {
713 oput_fifo_ctx, LOG_CTX_GET());
714 CHECK_DO(ret_code== STAT_SUCCESS || ret_code== STAT_EAGAIN,
goto end);
716 end_code= STAT_SUCCESS;
718 if(avpacket_iput!= NULL)
736 CHECK_DO(proc_ctx!= NULL,
return STAT_ERROR);
737 CHECK_DO(str!= NULL,
return STAT_ERROR);
739 LOG_CTX_SET(proc_ctx->
log_ctx);
743 ffmpeg_x264_dec_settings_ctx=
745 video_settings_dec_ctx=
751 if(ret_code!= STAT_SUCCESS)
759 (
volatile void*)video_settings_dec_ctx, 0,
772 int ret_code, end_code= STAT_ERROR;
775 AVCodecContext *avcodecctx= NULL;
778 cJSON *cjson_rest= NULL, *cjson_settings= NULL;
783 CHECK_DO(proc_ctx!= NULL,
return STAT_ERROR);
784 CHECK_DO(rest_fmt< PROC_IF_REST_FMT_ENUM_MAX,
return STAT_ERROR);
785 CHECK_DO(ref_reponse!= NULL,
return STAT_ERROR);
787 LOG_CTX_SET(proc_ctx->
log_ctx);
792 cjson_rest= cJSON_CreateObject();
793 CHECK_DO(cjson_rest!= NULL,
goto end);
807 ffmpeg_x264_dec_settings_ctx=
809 video_settings_dec_ctx=
814 &cjson_settings, LOG_CTX_GET());
815 CHECK_DO(ret_code== STAT_SUCCESS && cjson_settings!= NULL,
goto end);
821 cJSON_AddItemToObject(cjson_rest,
"settings", cjson_settings);
822 cjson_settings= NULL;
828 CHECK_DO(avcodecctx!= NULL,
goto end);
842 CHECK_DO(*ref_reponse!= NULL && strlen((
char*)*ref_reponse)> 0,
846 *ref_reponse= (
void*)cjson_rest;
850 LOGE(
"Unknown format requested for processor REST\n");
854 end_code= STAT_SUCCESS;
856 if(cjson_settings!= NULL)
857 cJSON_Delete(cjson_settings);
858 if(cjson_rest!= NULL)
859 cJSON_Delete(cjson_rest);
876 LOG_CTX_INIT(log_ctx);
879 CHECK_DO(ffmpeg_x264_dec_settings_ctx!= NULL,
return STAT_ERROR);
881 video_settings_dec_ctx=
886 if(ret_code!= STAT_SUCCESS)
905 LOG_CTX_INIT(log_ctx);
908 CHECK_DO(ffmpeg_x264_dec_settings_ctx!= NULL,
return);
910 video_settings_dec_ctx=
void avframe_release(void **ref_avfame)
int fifo_get(fifo_ctx_t *fifo_ctx, void **ref_elem, size_t *ref_elem_size)
#define CJSON_PRINT(CJSON_PTR)
static void ffmpeg_x264_dec_settings_ctx_deinit(volatile ffmpeg_x264_dec_settings_ctx_t *ffmpeg_x264_dec_settings_ctx, log_ctx_t *log_ctx)
Facilities to convert processor's input/output frame to FFmpeg's formats and vice versa...
struct video_settings_enc_ctx_s video_settings_enc_ctx
proc_frame_ctx_t * avframe_2_proc_frame_ctx(const void *avframe_arg)
static int ffmpeg_x264_enc_rest_put(proc_ctx_t *proc_ctx, const char *str)
int video_settings_enc_ctx_init(volatile video_settings_enc_ctx_t *video_settings_enc_ctx)
int video_settings_dec_ctx_restful_get(volatile video_settings_dec_ctx_t *video_settings_dec_ctx, cJSON **ref_cjson_rest, log_ctx_t *log_ctx)
void ffmpeg_video_reset_on_new_settings(proc_ctx_t *proc_ctx, volatile void *video_settings_opaque, int flag_is_encoder, log_ctx_t *log_ctx)
struct ffmpeg_x264_dec_settings_ctx_s ffmpeg_x264_dec_settings_ctx_t
struct ffmpeg_x264_dec_ctx_s ffmpeg_x264_dec_ctx_t
struct ffmpeg_video_enc_ctx_s ffmpeg_video_enc_ctx
static void ffmpeg_x264_dec_close(proc_ctx_t **ref_proc_ctx)
Video encoder and decoder generic settings.
struct ffmpeg_x264_enc_settings_ctx_s ffmpeg_x264_enc_settings_ctx_t
int ffmpeg_video_enc_frame(ffmpeg_video_enc_ctx_t *ffmpeg_video_enc_ctx, AVFrame *avframe_iput, fifo_ctx_t *oput_fifo_ctx, log_ctx_t *log_ctx)
int video_settings_dec_ctx_init(volatile video_settings_dec_ctx_t *video_settings_dec_ctx)
const proc_if_t proc_if_ffmpeg_x264_enc
int video_settings_enc_ctx_restful_put(volatile video_settings_enc_ctx_t *video_settings_enc_ctx, const char *str, log_ctx_t *log_ctx)
Generic processor module context (see type proc_ctx_t) extension for video encoders and decoders...
static int ffmpeg_x264_enc_settings_ctx_init(volatile ffmpeg_x264_enc_settings_ctx_t *ffmpeg_x264_enc_settings_ctx, log_ctx_t *log_ctx)
static proc_ctx_t * ffmpeg_x264_dec_open(const proc_if_t *proc_if, const char *settings_str, log_ctx_t *log_ctx, va_list arg)
volatile struct ffmpeg_x264_enc_settings_ctx_s ffmpeg_x264_enc_settings_ctx
void video_settings_enc_ctx_deinit(volatile video_settings_enc_ctx_t *video_settings_enc_ctx)
static int ffmpeg_x264_dec_rest_put(proc_ctx_t *proc_ctx, const char *str)
static void ffmpeg_x264_enc_settings_ctx_deinit(volatile ffmpeg_x264_enc_settings_ctx_t *ffmpeg_x264_enc_settings_ctx, log_ctx_t *log_ctx)
void avpacket_release(void **ref_avpacket)
int ffmpeg_video_dec_frame(ffmpeg_video_dec_ctx_t *ffmpeg_video_dec_ctx, AVPacket *avpacket_iput, fifo_ctx_t *oput_fifo_ctx, log_ctx_t *log_ctx)
AVDictionary * avdictionary
AVCodecContext * avcodecctx
void ffmpeg_video_dec_ctx_deinit(ffmpeg_video_dec_ctx_t *ffmpeg_video_dec_ctx, log_ctx_t *log_ctx)
struct ffmpeg_video_dec_ctx_s ffmpeg_video_dec_ctx
#define CHECK_DO(COND, ACTION)
Character string response.
struct ffmpeg_x264_enc_ctx_s ffmpeg_x264_enc_ctx_t
static int ffmpeg_x264_dec_settings_ctx_init(volatile ffmpeg_x264_dec_settings_ctx_t *ffmpeg_x264_dec_settings_ctx, log_ctx_t *log_ctx)
enum proc_if_rest_fmt_enum proc_if_rest_fmt_t
int video_settings_enc_ctx_restful_get(volatile video_settings_enc_ctx_t *video_settings_enc_ctx, cJSON **ref_cjson_rest, log_ctx_t *log_ctx)
proc_frame_ctx_t * avpacket_2_proc_frame_ctx(const void *avpacket_arg)
static proc_ctx_t * ffmpeg_x264_enc_open(const proc_if_t *proc_if, const char *settings_str, log_ctx_t *log_ctx, va_list arg)
static int ffmpeg_x264_enc_process_frame(proc_ctx_t *proc_ctx, fifo_ctx_t *iput_fifo_ctx, fifo_ctx_t *oput_fifo_ctx)
static int ffmpeg_x264_enc_rest_get(proc_ctx_t *proc_ctx, const proc_if_rest_fmt_t rest_fmt, void **ref_reponse)
const proc_if_t proc_if_ffmpeg_x264_dec
int ffmpeg_video_enc_ctx_init(ffmpeg_video_enc_ctx_t *ffmpeg_video_enc_ctx, int avcodecid, const video_settings_enc_ctx_t *video_settings_enc_ctx, log_ctx_t *log_ctx)
volatile struct ffmpeg_x264_dec_settings_ctx_s ffmpeg_x264_dec_settings_ctx
static int ffmpeg_x264_dec_rest_get(proc_ctx_t *proc_ctx, const proc_if_rest_fmt_t rest_fmt, void **ref_reponse)
void * proc_frame_ctx_2_avpacket(const proc_frame_ctx_t *proc_frame_ctx)
AVCodecContext * avcodecctx
static void ffmpeg_x264_enc_close(proc_ctx_t **ref_proc_ctx)
void * proc_frame_ctx_2_avframe(const proc_frame_ctx_t *proc_frame_ctx)
int video_settings_dec_ctx_restful_put(volatile video_settings_dec_ctx_t *video_settings_dec_ctx, const char *str, log_ctx_t *log_ctx)
void ffmpeg_video_enc_ctx_deinit(ffmpeg_video_enc_ctx_t *ffmpeg_video_enc_ctx, log_ctx_t *log_ctx)
void video_settings_dec_ctx_deinit(volatile video_settings_dec_ctx_t *video_settings_dec_ctx)
int ffmpeg_video_dec_ctx_init(ffmpeg_video_dec_ctx_t *ffmpeg_video_dec_ctx, int avcodecid, const video_settings_dec_ctx_t *video_settings_dec_ctx, log_ctx_t *log_ctx)
struct video_settings_dec_ctx_s video_settings_dec_ctx
FFmpeg x264 video encoder and decoder wrappers.
static int ffmpeg_x264_dec_process_frame(proc_ctx_t *proc_ctx, fifo_ctx_t *iput_fifo_ctx, fifo_ctx_t *oput_fifo_ctx)