You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
8.6 KiB
278 lines
8.6 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd. 2009-2019. All rights reserved.
|
|
* Description: Defines avplay common api
|
|
* Author: Hisilicon
|
|
* Create: 2009-12-21
|
|
* History:
|
|
*/
|
|
#ifndef __AVPLAY_COMMON_H__
|
|
#define __AVPLAY_COMMON_H__
|
|
|
|
#include <securec.h>
|
|
#include <pthread.h>
|
|
#include <limits.h>
|
|
#include "td_type.h"
|
|
#include "soc_errno.h"
|
|
#include "soc_log.h"
|
|
#include "soc_module.h"
|
|
#include "mpi_stat_ext.h"
|
|
#include "uapi_memory.h"
|
|
|
|
#ifdef __cplusplus
|
|
#if __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
#endif
|
|
|
|
#define MAX_THREAD_PRIORITY 99
|
|
#define TIME_RATION 1000
|
|
#define AVPLAY_TYPE_RESERVED (-1)
|
|
#define AVPLAY_DEFAULT_STRING "***"
|
|
|
|
#define DEMUX_MAX_PID 0x1fff
|
|
#ifndef DEMUX_INVALID_PID
|
|
#define DEMUX_INVALID_PID (DEMUX_MAX_PID + 1)
|
|
#endif
|
|
|
|
#define TIME_IS_S64
|
|
|
|
#ifdef TIME_IS_U32
|
|
typedef td_u32 avplay_time_t;
|
|
#define time_to_u32(time) (time)
|
|
#define time_to_u64(time) (td_u64)(time)
|
|
#define time_to_s64(time) (td_s64)(time)
|
|
#elif (defined TIME_IS_S64)
|
|
typedef td_s64 avplay_time_t;
|
|
#define time_to_u32(time) (td_u32)(time)
|
|
#define time_to_u64(time) (td_u64)(time)
|
|
#define time_to_s64(time) (time)
|
|
#endif
|
|
|
|
#define u64_to_time(val) (avplay_time_t)(val)
|
|
#define s64_to_time(val) (avplay_time_t)(val)
|
|
|
|
#ifdef AVPLAY_SUPPORT_DEBUGGER
|
|
#define soc_assert_avplay(expr) soc_assert(expr)
|
|
#else
|
|
#define soc_assert_avplay(expr)
|
|
#endif
|
|
|
|
#define ext_fatal_avplay(fmt...) soc_trace(SOC_TRACE_LEVEL_FATAL, SOC_ID_AVPLAY, fmt)
|
|
#define ext_err_avplay(fmt...) soc_trace(SOC_TRACE_LEVEL_ERROR, SOC_ID_AVPLAY, fmt)
|
|
#define ext_warn_avplay(fmt...) soc_trace(SOC_TRACE_LEVEL_WARN, SOC_ID_AVPLAY, fmt)
|
|
#define ext_info_avplay(fmt...) soc_trace(SOC_TRACE_LEVEL_INFO, SOC_ID_AVPLAY, fmt)
|
|
#define ext_dbg_avplay(fmt...) soc_trace(SOC_TRACE_LEVEL_DBG, SOC_ID_AVPLAY, fmt)
|
|
#define soc_trace_avplay(fmt...) soc_trace(SOC_TRACE_LEVEL_TRACE, SOC_ID_AVPLAY, fmt)
|
|
|
|
#define tag_call(tag) tag
|
|
#define line_literal(x) #x
|
|
#define line_call(x) line_literal(x)
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#ifndef likely
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
#endif
|
|
#ifndef unlikely
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#define log_when_error(ret, description) do { \
|
|
if (unlikely((ret) != TD_SUCCESS)) { \
|
|
ext_err_avplay("[%s] failed, errcode = 0x%x\n", line_call(description), (ret)); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define log_when_check_fail(condition) do { \
|
|
if (unlikely(!(condition))) { \
|
|
ext_err_avplay("Check [%s] failed\n", line_call(condition)); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define return_when_error(ret, description) do { \
|
|
if (unlikely((ret) != TD_SUCCESS)) { \
|
|
ext_err_avplay("[%s] failed, return 0x%x\n", line_call(description), (ret)); \
|
|
return (ret); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define return_when_check_fail(condition, ret) do { \
|
|
if (unlikely(!(condition))) { \
|
|
ext_err_avplay("Check [%s] failed, return 0x%x\n", line_call(condition), (ret)); \
|
|
return (ret); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define goto_when_error(ret, description, tag) do { \
|
|
if (unlikely((ret) != TD_SUCCESS)) { \
|
|
ext_err_avplay("[%s] failed, errcode = 0x%x, goto [%s]\n", line_call(description), (ret), line_call(tag)); \
|
|
goto tag_call(tag); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define goto_when_ctx_is_null(ctx, ret, tag) do { \
|
|
if (unlikely((ctx) == TD_NULL)) { \
|
|
ext_err_avplay("Avplay context is NULL, maybe has been destoryed\n"); \
|
|
(ret) = SOC_ERR_AVPLAY_NULL_PTR; \
|
|
goto tag_call(tag); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define return_when_not_eok(func, ret, description) do { \
|
|
if (unlikely((func) != EOK)) { \
|
|
ext_err_avplay("Execute %s failed!\n", line_call(description)); \
|
|
return (ret); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define get_array_size(array) (sizeof(array) / sizeof((array)[0]))
|
|
|
|
#define set_bit_enable(who, bit) ((who) | (1LL << ((td_u32)(bit))))
|
|
#define get_bit(who, bit) (((who) >> (bit)) & (0x1))
|
|
#define is_bit_enable(who, bit) (get_bit(who, bit) != 0)
|
|
|
|
#define has_flag(who, flag) (((td_u32)(who) & (td_u32)(flag)) != 0)
|
|
#define set_flag(who, flag) do { (who) = (td_u32)(who) | (td_u32)(flag); } while (0)
|
|
#define clr_flag(who, flag) do { (who) = (td_u32)(who) & (~(td_u32)(flag)); } while (0)
|
|
#define clr_flag64(who, flag) do { (who) = (td_u64)(who) & (~(td_u64)(flag)); } while (0)
|
|
|
|
#define ptr2str(ptr) ((ptr) == TD_NULL ? "NULL" : (ptr))
|
|
#define bool2str(flag) ((flag) ? "true" : "false")
|
|
#define errcode2str(ret) (((ret) == TD_SUCCESS) ? "SUCCESS" : "FAIL")
|
|
#define check_param(condition) return_when_check_fail(condition, SOC_ERR_AVPLAY_INVALID_PARA)
|
|
#define check_null(pointer) return_when_check_fail(((pointer) != NULL), SOC_ERR_AVPLAY_NULL_PTR)
|
|
#define check_dev(dev) return_when_check_fail(((dev) >= 0), SOC_ERR_AVPLAY_DEV_NO_INIT)
|
|
|
|
#ifndef get_abs
|
|
#define get_abs(v) abs(v)
|
|
#endif
|
|
|
|
#ifndef unused
|
|
#define unused(param) (void)(param)
|
|
#endif
|
|
|
|
#define avplay_max(a, b) ((a) > (b)) ? (a) : (b)
|
|
#define avplay_min(a, b) ((a) > (b)) ? (b) : (a)
|
|
|
|
#define avplay_calc_percent(cur, total) ((total) != 0 ? ((cur) * 100LL / (total)) : 0)
|
|
#define byte_rate_to_kbps(byte_rate) ((byte_rate) * 8 / 1024)
|
|
|
|
/*
|
|
Function: Compile fails when the condition is false
|
|
Note: The condition must be pre-compiled
|
|
*/
|
|
#define avplay_build_check(condition) ((void)sizeof(char[2 * !!(condition) - 1]))
|
|
|
|
typedef pthread_mutex_t avplay_mutex_t;
|
|
typedef struct {
|
|
const td_char *name;
|
|
td_u8 priority;
|
|
td_u32 tid;
|
|
pthread_t pid;
|
|
|
|
td_s32 (*loop)(void *);
|
|
void *private;
|
|
} avplay_thread_info;
|
|
|
|
typedef struct {
|
|
pthread_cond_t cond;
|
|
const td_char *name;
|
|
} avplay_cond_info;
|
|
|
|
typedef struct {
|
|
td_u8 *buf;
|
|
td_u32 size;
|
|
td_u32 rd;
|
|
td_u32 wr;
|
|
td_u32 evt_cnt;
|
|
} avplay_efifo_status;
|
|
|
|
typedef struct {
|
|
td_u64 total_size;
|
|
td_u64 start_ts;
|
|
td_u64 byte_rate; /* average byte rate */
|
|
|
|
td_u64 instant_size;
|
|
td_u64 instant_start_ts;
|
|
td_u64 instant_byte_rate; /* instant byte rate every 1s */
|
|
} avplay_bps_info;
|
|
|
|
typedef enum {
|
|
AVPLAY_STEP_ACQ_FRM = 0,
|
|
AVPLAY_STEP_QUE_FRM,
|
|
AVPLAY_STEP_MAX,
|
|
} avplay_step;
|
|
|
|
static inline td_bool avplay_check_int_multip(td_s32 multiplier, td_s32 val, td_s32 *result)
|
|
{
|
|
td_s64 temp = (td_s64)(multiplier * val);
|
|
if (result != NULL) {
|
|
*result = (td_s32)temp;
|
|
}
|
|
return (temp < INT_MAX && temp > INT_MIN);
|
|
}
|
|
|
|
static inline td_bool avplay_check_unint_multip(td_u32 multiplier, td_u32 val, td_u32 *result)
|
|
{
|
|
td_s64 temp = (td_s64)(multiplier * val);
|
|
if (result != NULL) {
|
|
*result = (td_u32)temp;
|
|
}
|
|
return (temp < UINT_MAX);
|
|
}
|
|
|
|
td_u64 avplay_gettime_real(td_bool millisecond);
|
|
td_u64 avplay_gettime_mono(td_bool millisecond);
|
|
td_u32 avplay_gettime_system(void);
|
|
|
|
void avplay_mutex_init(avplay_mutex_t *mutex);
|
|
void avplay_mutex_lock(avplay_mutex_t *mutex);
|
|
td_s32 avplay_mutex_trylock(avplay_mutex_t *mutex);
|
|
void avplay_mutex_unlock(avplay_mutex_t *mutex);
|
|
|
|
void avplay_cond_init(avplay_cond_info *cond, const td_char *name);
|
|
void avplay_cond_signal(avplay_cond_info *cond);
|
|
void avplay_cond_broadcast(avplay_cond_info *cond);
|
|
/* If timeout < 0, then wait until signal; if timeout == 0, then return immediately. */
|
|
/* timeout ms */
|
|
td_s32 avplay_cond_wait(avplay_cond_info *cond, avplay_mutex_t *mutex, td_s64 timeout);
|
|
|
|
void avplay_sleep(td_u64 millisecond);
|
|
td_u32 avplay_gettid(void);
|
|
td_u32 avplay_getpid(void);
|
|
|
|
td_s32 avplay_thread_create(avplay_thread_info *thread);
|
|
td_s32 avplay_thread_setname(const avplay_thread_info *thread);
|
|
td_s32 avplay_thread_setpriority(avplay_thread_info *thread);
|
|
td_s32 avplay_thread_wait(const avplay_thread_info *thread);
|
|
|
|
#define avplay_atomic_inc(var) __sync_fetch_and_add(&(var), 1)
|
|
#define avplay_atomic_dec(var) __sync_fetch_and_sub(&(var), 1)
|
|
#define avplay_atomic_add(var, v) __sync_fetch_and_add(&(var), (v))
|
|
#define avplay_atomic_sub(var, v) __sync_fetch_and_sub(&(var), (v))
|
|
|
|
#define avplay_atomic_set(var, v) do { __sync_lock_test_and_set(&(var), v); } while (0)
|
|
#define avplay_atomic_read(var) __sync_add_and_fetch(&(var), 0)
|
|
|
|
td_bool avplay_object_equal(const void *dst, const void *src, size_t size);
|
|
|
|
td_s32 avplay_efifo_init(avplay_efifo_status *efifo, td_u32 size);
|
|
td_s32 avplay_efifo_deinit(avplay_efifo_status *efifo);
|
|
td_s32 avplay_efifo_write(avplay_efifo_status *efifo, td_u32 id, const void *param, td_u32 param_size);
|
|
/* param_size in && out */
|
|
td_s32 avplay_efifo_read(avplay_efifo_status *efifo, td_u32 *id, void *param, td_u32 *param_size);
|
|
|
|
void avplay_bps_calc(avplay_bps_info *info, td_u64 add_size);
|
|
|
|
td_void avplay_stat_info_notify(avplay_step step, td_u64 index, td_s64 pts, td_s64 delta);
|
|
|
|
#ifdef __cplusplus
|
|
#if __cplusplus
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
#endif
|