rknn_usb_rtsp-master/ 这是RTSP推流的代码,用了rockcip硬件推流

This commit is contained in:
zhangpeng
2025-04-28 14:32:50 +08:00
parent c0b0c3e87b
commit 837c870f18
79 changed files with 15161 additions and 1 deletions

0
rknn_usb_rtsp-master/.DS_Store vendored Normal file
View File

1
rknn_usb_rtsp-master/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build

View File

@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"3rdparty/rga/RK3588/include",
"/usr/include/opencv4",
"mk_api/include",
"mpp_api/include",
"/usr/local/include/eigen3"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-arm64"
}
],
"version": 4
}

View File

@@ -0,0 +1,32 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "CMake调试",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/usb_to_rtsp", // 编译后的程序需要结合CMakeLists.txt中的add_executable()函数
"args": [
"/dev/video0",
"264"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "CMake编译"
}
]
}

View File

@@ -0,0 +1,74 @@
{
"files.associations": {
"vector": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"bitset": "cpp",
"regex": "cpp",
"core": "cpp",
"csignal": "cpp",
"unordered_set": "cpp",
"hash_map": "cpp",
"future": "cpp",
"valarray": "cpp"
},
}

58
rknn_usb_rtsp-master/.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,58 @@
{
"version": "2.0.0",
"tasks": [
{
// cmake配置
"type": "cppbuild",
"label": "CMake配置",
"command": "cmake", // cmake命令
"args": [
"-S .", // 源码目录
"-B build", // 编译目录
"-DCMAKE_BUILD_TYPE=Debug" // 编译类型
],
"options": {
"cwd": "${workspaceFolder}" // 工作目录
},
"problemMatcher": [
"$gcc"
],
"group": "build",
},
{
// cmake编译
"type": "cppbuild",
"label": "CMake编译",
"command": "cmake", // cmake命令
"args": [
"--build", // 编译
"build", // 编译目录
],
"options": {
"cwd": "${workspaceFolder}" // 工作目录
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"dependsOn": [
"CMake配置" // 依赖CMake配置先执行CMake配置
]
},
{
// 删除build目录
"type": "shell",
"label": "删除build目录",
"command": "rm -rf build",
"options": {
"cwd": "${workspaceFolder}" // 工作目录
},
"problemMatcher": [
"$gcc"
],
"group": "build",
}
]
}

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rk_graphic_buffer_h_
#define _rk_graphic_buffer_h_
#ifdef ANDROID
#include <stdint.h>
#include <vector>
#include <sys/types.h>
#include <system/graphics.h>
#include <utils/Thread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <android/log.h>
#include <utils/Log.h>
#include <log/log_main.h>
#include "drmrga.h"
#include "rga.h"
// -------------------------------------------------------------------------------
int RkRgaGetHandleFd(buffer_handle_t handle, int *fd);
int RkRgaGetHandleAttributes(buffer_handle_t handle,
std::vector<int> *attrs);
int RkRgaGetHandleMapAddress(buffer_handle_t handle,
void **buf);
#endif //Android
#endif //_rk_graphic_buffer_h_

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rockchip_rga_c_h_
#define _rockchip_rga_c_h_
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include "drmrga.h"
#include "rga.h"
#ifdef __cplusplus
extern "C"{
#endif
/*
* Compatible with the old version of C interface.The new
* version of the C interface no longer requires users to
* initialize rga, so RgaInit and RgaDeInit are just for
* compatibility with the old C interface, so please do
* not use ctx, because it is usually a NULL.
*/
#define RgaInit(ctx) ({ \
int ret = 0; \
ret = c_RkRgaInit(); \
c_RkRgaGetContext(ctx); \
ret;\
})
#define RgaDeInit(ctx) { \
(void)ctx; /* unused */ \
c_RkRgaDeInit(); \
}
#define RgaBlit(...) c_RkRgaBlit(__VA_ARGS__)
#define RgaCollorFill(...) c_RkRgaColorFill(__VA_ARGS__)
#define RgaFlush() c_RkRgaFlush()
int c_RkRgaInit();
void c_RkRgaDeInit();
void c_RkRgaGetContext(void **ctx);
int c_RkRgaBlit(rga_info_t *src, rga_info_t *dst, rga_info_t *src1);
int c_RkRgaColorFill(rga_info_t *dst);
int c_RkRgaFlush();
#ifndef ANDROID /* linux */
int c_RkRgaGetAllocBuffer(bo_t *bo_info, int width, int height, int bpp);
int c_RkRgaGetAllocBufferCache(bo_t *bo_info, int width, int height, int bpp);
int c_RkRgaGetMmap(bo_t *bo_info);
int c_RkRgaUnmap(bo_t *bo_info);
int c_RkRgaFree(bo_t *bo_info);
int c_RkRgaGetBufferFd(bo_t *bo_info, int *fd);
#endif /* #ifndef ANDROID */
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _rockchip_rga_c_h_ */

View File

@@ -0,0 +1,193 @@
/*
* Copyright (C) 2020 Rockchip Electronics Co., Ltd.
* Authors:
* PutinLee <putin.lee@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBS_RGA_MUTEX_H
#define _LIBS_RGA_MUTEX_H
#ifndef ANDROID
#include <stdint.h>
#include <sys/types.h>
#include <time.h>
#include <pthread.h>
// Enable thread safety attributes only with clang.
// The attributes can be safely erased when compiling with other compilers.
#if defined(__clang__) && (!defined(SWIG))
#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
#else
#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
#endif
#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
#define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
#define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
#define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
#define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
#define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
#define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
#define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
#define TRY_ACQUIRE_SHARED(...) \
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
#define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
class Condition;
/*
* NOTE: This class is for code that builds on Win32. Its usage is
* deprecated for code which doesn't build for Win32. New code which
* doesn't build for Win32 should use std::mutex and std::lock_guard instead.
*
* Simple mutex class. The implementation is system-dependent.
*
* The mutex must be unlocked by the thread that locked it. They are not
* recursive, i.e. the same thread can't lock it multiple times.
*/
class CAPABILITY("mutex") Mutex {
public:
enum {
PRIVATE = 0,
SHARED = 1
};
Mutex();
explicit Mutex(const char* name);
explicit Mutex(int type, const char* name = nullptr);
~Mutex();
// lock or unlock the mutex
int32_t lock() ACQUIRE();
void unlock() RELEASE();
// lock if possible; returns 0 on success, error otherwise
int32_t tryLock() TRY_ACQUIRE(0);
int32_t timedLock(int64_t timeoutNs) TRY_ACQUIRE(0);
// Manages the mutex automatically. It'll be locked when Autolock is
// constructed and released when Autolock goes out of scope.
class SCOPED_CAPABILITY Autolock {
public:
inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) {
mLock.lock();
}
inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) {
mLock.lock();
}
inline ~Autolock() RELEASE() {
mLock.unlock();
}
private:
Mutex& mLock;
// Cannot be copied or moved - declarations only
Autolock(const Autolock&);
Autolock& operator=(const Autolock&);
};
private:
friend class Condition;
// A mutex cannot be copied
Mutex(const Mutex&);
Mutex& operator=(const Mutex&);
pthread_mutex_t mMutex;
};
// ---------------------------------------------------------------------------
inline Mutex::Mutex() {
pthread_mutex_init(&mMutex, nullptr);
}
inline Mutex::Mutex(__attribute__((unused)) const char* name) {
pthread_mutex_init(&mMutex, nullptr);
}
inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
if (type == SHARED) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&mMutex, &attr);
pthread_mutexattr_destroy(&attr);
} else {
pthread_mutex_init(&mMutex, nullptr);
}
}
inline Mutex::~Mutex() {
pthread_mutex_destroy(&mMutex);
}
inline int32_t Mutex::lock() {
return -pthread_mutex_lock(&mMutex);
}
inline void Mutex::unlock() {
pthread_mutex_unlock(&mMutex);
}
inline int32_t Mutex::tryLock() {
return -pthread_mutex_trylock(&mMutex);
}
inline int32_t Mutex::timedLock(int64_t timeoutNs) {
timespec now;
clock_gettime(CLOCK_REALTIME, &now);
timeoutNs += now.tv_sec*1000000000 + now.tv_nsec;
const struct timespec ts = {
/* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
/* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
};
return -pthread_mutex_timedlock(&mMutex, &ts);
}
// ---------------------------------------------------------------------------
/*
* Automatic mutex. Declare one of these at the top of a function.
* When the function returns, it will go out of scope, and release the
* mutex.
*/
typedef Mutex::Autolock AutoMutex;
#endif // __ANDROID_VNDK__
#endif // _LIBS_RGA_MUTEX_H

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBS_RGA_SINGLETON_H
#define _LIBS_RGA_SINGLETON_H
#ifndef ANDROID
#include "RgaMutex.h"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundefined-var-template"
#endif
template <typename TYPE>
class Singleton {
public:
static TYPE& getInstance() {
Mutex::Autolock _l(sLock);
TYPE* instance = sInstance;
if (instance == nullptr) {
instance = new TYPE();
sInstance = instance;
}
return *instance;
}
static bool hasInstance() {
Mutex::Autolock _l(sLock);
return sInstance != nullptr;
}
protected:
~Singleton() { }
Singleton() { }
private:
Singleton(const Singleton&);
Singleton& operator = (const Singleton&);
static Mutex sLock;
static TYPE* sInstance;
};
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#define RGA_SINGLETON_STATIC_INSTANCE(TYPE) \
template<> ::Mutex \
(::Singleton< TYPE >::sLock)(::Mutex::PRIVATE); \
template<> TYPE* ::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \
template class ::Singleton< TYPE >;
#endif //ANDROID
#endif //_LIBS_RGA_SINGLETON_H

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rga_utils_h_
#define _rga_utils_h_
// -------------------------------------------------------------------------------
float get_bpp_from_format(int format);
int get_perPixel_stride_from_format(int format);
int get_buf_from_file(void *buf, int f, int sw, int sh, int index);
int output_buf_data_to_file(void *buf, int f, int sw, int sh, int index);
const char *translate_format_str(int format);
int get_buf_from_file_FBC(void *buf, int f, int sw, int sh, int index);
int output_buf_data_to_file_FBC(void *buf, int f, int sw, int sh, int index);
#endif

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rockchip_rga_h_
#define _rockchip_rga_h_
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include "drmrga.h"
#include "GrallocOps.h"
#include "RgaUtils.h"
#include "rga.h"
//////////////////////////////////////////////////////////////////////////////////
#ifndef ANDROID
#include "RgaSingleton.h"
#endif
#ifdef ANDROID
#include <utils/Singleton.h>
#include <utils/Thread.h>
#include <hardware/hardware.h>
namespace android {
#endif
class RockchipRga :public Singleton<RockchipRga> {
public:
static inline RockchipRga& get() {
return getInstance();
}
int RkRgaInit();
void RkRgaDeInit();
void RkRgaGetContext(void **ctx);
#ifndef ANDROID /* LINUX */
int RkRgaAllocBuffer(int drm_fd /* input */, bo_t *bo_info,
int width, int height, int bpp, int flags);
int RkRgaFreeBuffer(int drm_fd /* input */, bo_t *bo_info);
int RkRgaGetAllocBuffer(bo_t *bo_info, int width, int height, int bpp);
int RkRgaGetAllocBufferExt(bo_t *bo_info, int width, int height, int bpp, int flags);
int RkRgaGetAllocBufferCache(bo_t *bo_info, int width, int height, int bpp);
int RkRgaGetMmap(bo_t *bo_info);
int RkRgaUnmap(bo_t *bo_info);
int RkRgaFree(bo_t *bo_info);
int RkRgaGetBufferFd(bo_t *bo_info, int *fd);
#else
int RkRgaGetBufferFd(buffer_handle_t handle, int *fd);
int RkRgaGetHandleMapCpuAddress(buffer_handle_t handle, void **buf);
#endif
int RkRgaBlit(rga_info *src, rga_info *dst, rga_info *src1);
int RkRgaCollorFill(rga_info *dst);
int RkRgaCollorPalette(rga_info *src, rga_info *dst, rga_info *lut);
int RkRgaFlush();
void RkRgaSetLogOnceFlag(int log) {
mLogOnce = log;
}
void RkRgaSetAlwaysLogFlag(bool log) {
mLogAlways = log;
}
void RkRgaLogOutRgaReq(struct rga_req rgaReg);
int RkRgaLogOutUserPara(rga_info *rgaInfo);
inline bool RkRgaIsReady() {
return mSupportRga;
}
RockchipRga();
~RockchipRga();
private:
bool mSupportRga;
int mLogOnce;
int mLogAlways;
void * mContext;
friend class Singleton<RockchipRga>;
};
#ifdef ANDROID
}; // namespace android
#endif
#endif

View File

@@ -0,0 +1,358 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rk_drm_rga_
#define _rk_drm_rga_
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include "rga.h"
#ifdef ANDROID
#define DRMRGA_HARDWARE_MODULE_ID "librga"
#include <hardware/gralloc.h>
#include <hardware/hardware.h>
#include <system/graphics.h>
#include <cutils/native_handle.h>
#ifdef ANDROID_12
#include <hardware/hardware_rockchip.h>
#endif
#endif
#define RGA_BLIT_SYNC 0x5017
#define RGA_BLIT_ASYNC 0x5018
#ifndef ANDROID /* LINUX */
/* flip source image horizontally (around the vertical axis) */
#define HAL_TRANSFORM_FLIP_H 0x01
/* flip source image vertically (around the horizontal axis)*/
#define HAL_TRANSFORM_FLIP_V 0x02
/* rotate source image 90 degrees clockwise */
#define HAL_TRANSFORM_ROT_90 0x04
/* rotate source image 180 degrees */
#define HAL_TRANSFORM_ROT_180 0x03
/* rotate source image 270 degrees clockwise */
#define HAL_TRANSFORM_ROT_270 0x07
#endif
#define HAL_TRANSFORM_FLIP_H_V 0x08
/*****************************************************************************/
/* for compatibility */
#define DRM_RGA_MODULE_API_VERSION HWC_MODULE_API_VERSION_0_1
#define DRM_RGA_DEVICE_API_VERSION HWC_DEVICE_API_VERSION_0_1
#define DRM_RGA_API_VERSION HWC_DEVICE_API_VERSION
#define DRM_RGA_TRANSFORM_ROT_MASK 0x0000000F
#define DRM_RGA_TRANSFORM_ROT_0 0x00000000
#define DRM_RGA_TRANSFORM_ROT_90 HAL_TRANSFORM_ROT_90
#define DRM_RGA_TRANSFORM_ROT_180 HAL_TRANSFORM_ROT_180
#define DRM_RGA_TRANSFORM_ROT_270 HAL_TRANSFORM_ROT_270
#define DRM_RGA_TRANSFORM_FLIP_MASK 0x00000003
#define DRM_RGA_TRANSFORM_FLIP_H HAL_TRANSFORM_FLIP_H
#define DRM_RGA_TRANSFORM_FLIP_V HAL_TRANSFORM_FLIP_V
enum {
AWIDTH = 0,
AHEIGHT,
ASTRIDE,
AFORMAT,
ASIZE,
ATYPE,
};
/*****************************************************************************/
#ifndef ANDROID /* LINUX */
/* memory type definitions. */
enum drm_rockchip_gem_mem_type {
/* Physically Continuous memory and used as default. */
ROCKCHIP_BO_CONTIG = 1 << 0,
/* cachable mapping. */
ROCKCHIP_BO_CACHABLE = 1 << 1,
/* write-combine mapping. */
ROCKCHIP_BO_WC = 1 << 2,
ROCKCHIP_BO_SECURE = 1 << 3,
ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE |
ROCKCHIP_BO_WC | ROCKCHIP_BO_SECURE
};
typedef struct bo {
int fd;
void *ptr;
size_t size;
size_t offset;
size_t pitch;
unsigned handle;
} bo_t;
#endif
/*
@value size: user not need care about.For avoid read/write out of memory
*/
typedef struct rga_rect {
int xoffset;
int yoffset;
int width;
int height;
int wstride;
int hstride;
int format;
int size;
} rga_rect_t;
typedef struct rga_nn {
int nn_flag;
int scale_r;
int scale_g;
int scale_b;
int offset_r;
int offset_g;
int offset_b;
} rga_nn_t;
typedef struct rga_dither {
int enable;
int mode;
int lut0_l;
int lut0_h;
int lut1_l;
int lut1_h;
} rga_dither_t;
struct rga_mosaic_info {
uint8_t enable;
uint8_t mode;
};
struct rga_pre_intr_info {
uint8_t enable;
uint8_t read_intr_en;
uint8_t write_intr_en;
uint8_t read_hold_en;
uint32_t read_threshold;
uint32_t write_start;
uint32_t write_step;
};
/* MAX(min, (max - channel_value)) */
struct rga_osd_invert_factor {
uint8_t alpha_max;
uint8_t alpha_min;
uint8_t yg_max;
uint8_t yg_min;
uint8_t crb_max;
uint8_t crb_min;
};
struct rga_color {
union {
struct {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
uint32_t value;
};
};
struct rga_osd_bpp2 {
uint8_t ac_swap; // ac swap flag
// 0: CA
// 1: AC
uint8_t endian_swap; // rgba2bpp endian swap
// 0: Big endian
// 1: Little endian
struct rga_color color0;
struct rga_color color1;
};
struct rga_osd_mode_ctrl {
uint8_t mode; // OSD cal mode:
// 0b'1: statistics mode
// 1b'1: auto inversion overlap mode
uint8_t direction_mode; // horizontal or vertical
// 0: horizontal
// 1: vertical
uint8_t width_mode; // using @fix_width or LUT width
// 0: fix width
// 1: LUT width
uint16_t block_fix_width; // OSD block fixed width
// real width = (fix_width + 1) * 2
uint8_t block_num; // OSD block num
uint16_t flags_index; // auto invert flags index
/* invertion config */
uint8_t color_mode; // selete color
// 0: src1 color
// 1: config data color
uint8_t invert_flags_mode; // invert flag selete
// 0: use RAM flag
// 1: usr last result
uint8_t default_color_sel; // default color mode
// 0: default is bright
// 1: default is dark
uint8_t invert_enable; // invert channel enable
// 1 << 0: aplha enable
// 1 << 1: Y/G disable
// 1 << 2: C/RB disable
uint8_t invert_mode; // invert cal mode
// 0: normal(max-data)
// 1: swap
uint8_t invert_thresh; // if luma > thresh, osd_flag to be 1
uint8_t unfix_index; // OSD width config index
};
struct rga_osd_info {
uint8_t enable;
struct rga_osd_mode_ctrl mode_ctrl;
struct rga_osd_invert_factor cal_factor;
struct rga_osd_bpp2 bpp2_info;
union {
struct {
uint32_t last_flags1;
uint32_t last_flags0;
};
uint64_t last_flags;
};
union {
struct {
uint32_t cur_flags1;
uint32_t cur_flags0;
};
uint64_t cur_flags;
};
};
/*
@value fd: use fd to share memory, it can be ion shard fd,and dma fd.
@value virAddr:userspace address
@value phyAddr:use phy address
@value hnd: use buffer_handle_t
*/
typedef struct rga_info {
int fd;
void *virAddr;
void *phyAddr;
#ifndef ANDROID /* LINUX */
unsigned hnd;
#else /* Android */
buffer_handle_t hnd;
#endif
int format;
rga_rect_t rect;
unsigned int blend;
int bufferSize;
int rotation;
int color;
int testLog;
int mmuFlag;
int colorkey_en;
int colorkey_mode;
int colorkey_max;
int colorkey_min;
int scale_mode;
int color_space_mode;
int sync_mode;
rga_nn_t nn;
rga_dither_t dither;
int rop_code;
int rd_mode;
unsigned short is_10b_compact;
unsigned short is_10b_endian;
int in_fence_fd;
int out_fence_fd;
int core;
int priority;
unsigned short enable;
int handle;
struct rga_mosaic_info mosaic_info;
struct rga_osd_info osd_info;
struct rga_pre_intr_info pre_intr;
int mpi_mode;
union {
int ctx_id;
int job_handle;
};
char reserve[402];
} rga_info_t;
typedef struct drm_rga {
rga_rect_t src;
rga_rect_t dst;
} drm_rga_t;
/*
@fun rga_set_rect:For use to set the rects esayly
@param rect:The rect user want to set,like setting the src rect:
drm_rga_t rects;
rga_set_rect(rects.src,0,0,1920,1080,1920,NV12);
mean to set the src rect to the value.
*/
static inline int rga_set_rect(rga_rect_t *rect,
int x, int y, int w, int h, int sw, int sh, int f) {
if (!rect)
return -EINVAL;
rect->xoffset = x;
rect->yoffset = y;
rect->width = w;
rect->height = h;
rect->wstride = sw;
rect->hstride = sh;
rect->format = f;
return 0;
}
#ifndef ANDROID /* LINUX */
static inline void rga_set_rotation(rga_info_t *info, int angle) {
if (angle == 90)
info->rotation = HAL_TRANSFORM_ROT_90;
else if (angle == 180)
info->rotation = HAL_TRANSFORM_ROT_180;
else if (angle == 270)
info->rotation = HAL_TRANSFORM_ROT_270;
}
#endif
/*****************************************************************************/
#endif

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2020 Rockchip Electronics Co., Ltd.
* Authors:
* PutinLee <putin.lee@rock-chips.com>
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_h_
#define _im2d_h_
#include "im2d_version.h"
#include "im2d_type.h"
#include "im2d_common.h"
#include "im2d_buffer.h"
#include "im2d_single.h"
#include "im2d_task.h"
#include "im2d_mpi.h"
#endif /* #ifndef _im2d_h_ */

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2020 Rockchip Electronics Co., Ltd.
* Authors:
* PutinLee <putin.lee@rock-chips.com>
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_hpp_
#define _im2d_hpp_
#include "im2d.h"
#include "im2d_expand.h"
#endif /* #ifndef _im2d_hpp_ */

View File

@@ -0,0 +1,179 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_buffer_h_
#define _im2d_buffer_h_
#include "im2d_type.h"
/**
* Import external buffers into RGA driver.
*
* @param fd/va/pa
* Select dma_fd/virtual_address/physical_address by buffer type
* @param size
* Describes the size of the image buffer
*
* @return rga_buffer_handle_t
*/
#ifdef __cplusplus
IM_API rga_buffer_handle_t importbuffer_fd(int fd, int size);
IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, int size);
IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, int size);
#endif
/**
* Import external buffers into RGA driver.
*
* @param fd/va/pa
* Select dma_fd/virtual_address/physical_address by buffer type
* @param width
* Describes the pixel width stride of the image buffer
* @param height
* Describes the pixel height stride of the image buffer
* @param format
* Describes the pixel format of the image buffer
*
* @return rga_buffer_handle_t
*/
#ifdef __cplusplus
IM_API rga_buffer_handle_t importbuffer_fd(int fd, int width, int height, int format);
IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, int width, int height, int format);
IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, int width, int height, int format);
#endif
/**
* Import external buffers into RGA driver.
*
* @param fd/va/pa
* Select dma_fd/virtual_address/physical_address by buffer type
* @param param
* Configure buffer parameters
*
* @return rga_buffer_handle_t
*/
IM_EXPORT_API rga_buffer_handle_t importbuffer_fd(int fd, im_handle_param_t *param);
IM_EXPORT_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, im_handle_param_t *param);
IM_EXPORT_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, im_handle_param_t *param);
/**
* Import external buffers into RGA driver.
*
* @param handle
* rga buffer handle
*
* @return success or else negative error code.
*/
IM_EXPORT_API IM_STATUS releasebuffer_handle(rga_buffer_handle_t handle);
/**
* Wrap image Parameters.
*
* @param handle/virtualaddr/physicaladdr/fd
* RGA buffer handle/virtualaddr/physicaladdr/fd.
* @param width
* Width of image manipulation area.
* @param height
* Height of image manipulation area.
* @param wstride
* Width pixel stride, default (width = wstride).
* @param hstride
* Height pixel stride, default (height = hstride).
* @param format
* Image format.
*
* @return rga_buffer_t
*/
#define wrapbuffer_handle(handle, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_handle_t(handle, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_handle_t(handle, width, height, __args[0], __args[1], format); \
} else { \
memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
#define wrapbuffer_virtualaddr(vir_addr, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, __args[0], __args[1], format); \
} else { \
memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
#define wrapbuffer_physicaladdr(phy_addr, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, __args[0], __args[1], format); \
} else { \
memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
#define wrapbuffer_fd(fd, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, __args[0], __args[1], format); \
} else { \
memset(&im2d_api_buffer, 0x0, sizeof(im2d_api_buffer)); \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
/* Symbols for define *_t functions */
IM_C_API rga_buffer_t wrapbuffer_handle_t(rga_buffer_handle_t handle, int width, int height, int wstride, int hstride, int format);
IM_C_API rga_buffer_t wrapbuffer_virtualaddr_t(void* vir_addr, int width, int height, int wstride, int hstride, int format);
IM_C_API rga_buffer_t wrapbuffer_physicaladdr_t(void* phy_addr, int width, int height, int wstride, int hstride, int format);
IM_C_API rga_buffer_t wrapbuffer_fd_t(int fd, int width, int height, int wstride, int hstride, int format);
#ifdef __cplusplus
#undef wrapbuffer_handle
IM_API rga_buffer_t wrapbuffer_handle(rga_buffer_handle_t handle,
int width, int height, int format);
IM_API rga_buffer_t wrapbuffer_handle(rga_buffer_handle_t handle,
int width, int height, int format,
int wstride, int hstride);
#endif
#endif /* #ifndef _im2d_buffer_h_ */

View File

@@ -0,0 +1,151 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_common_h_
#define _im2d_common_h_
#include "im2d_type.h"
/**
* Query RGA basic information, supported resolution, supported format, etc.
*
* @param name
* RGA_VENDOR
* RGA_VERSION
* RGA_MAX_INPUT
* RGA_MAX_OUTPUT
* RGA_INPUT_FORMAT
* RGA_OUTPUT_FORMAT
* RGA_EXPECTED
* RGA_ALL
*
* @returns a string describing properties of RGA.
*/
IM_EXPORT_API const char* querystring(int name);
/**
* String to output the error message
*
* @param status
* process result value.
*
* @returns error message.
*/
#define imStrError(...) \
({ \
const char* im2d_api_err; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_err = imStrError_t(IM_STATUS_INVALID_PARAM); \
} else if (__argc == 1){ \
im2d_api_err = imStrError_t((IM_STATUS)__args[0]); \
} else { \
im2d_api_err = ("Fatal error, imStrError() too many parameters\n"); \
printf("Fatal error, imStrError() too many parameters\n"); \
} \
im2d_api_err; \
})
IM_C_API const char* imStrError_t(IM_STATUS status);
/**
* check im2d api header file
*
* @param header_version
* Default is RGA_CURRENT_API_HEADER_VERSION, no need to change if there are no special cases.
*
* @returns no error or else negative error code.
*/
#ifdef __cplusplus
IM_API IM_STATUS imcheckHeader(im_api_version_t header_version = RGA_CURRENT_API_HEADER_VERSION);
#endif
/**
* check RGA basic information, supported resolution, supported format, etc.
*
* @param src
* @param dst
* @param pat
* @param src_rect
* @param dst_rect
* @param pat_rect
* @param mode_usage
*
* @returns no error or else negative error code.
*/
#define imcheck(src, dst, src_rect, dst_rect, ...) \
({ \
IM_STATUS __ret = IM_STATUS_NOERROR; \
rga_buffer_t __pat; \
im_rect __pat_rect; \
memset(&__pat, 0, sizeof(rga_buffer_t)); \
memset(&__pat_rect, 0, sizeof(im_rect)); \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcheck_t(src, dst, __pat, src_rect, dst_rect, __pat_rect, 0); \
} else if (__argc == 1){ \
__ret = imcheck_t(src, dst, __pat, src_rect, dst_rect, __pat_rect, __args[0]); \
} else { \
__ret = IM_STATUS_FAILED; \
printf("check failed\n"); \
} \
__ret; \
})
#define imcheck_composite(src, dst, pat, src_rect, dst_rect, pat_rect, ...) \
({ \
IM_STATUS __ret = IM_STATUS_NOERROR; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, 0); \
} else if (__argc == 1){ \
__ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, __args[0]); \
} else { \
__ret = IM_STATUS_FAILED; \
printf("check failed\n"); \
} \
__ret; \
})
IM_C_API IM_STATUS imcheck_t(const rga_buffer_t src, const rga_buffer_t dst, const rga_buffer_t pat,
const im_rect src_rect, const im_rect dst_rect, const im_rect pat_rect, const int mode_usage);
/* Compatible with the legacy symbol */
IM_C_API void rga_check_perpare(rga_buffer_t *src, rga_buffer_t *dst, rga_buffer_t *pat,
im_rect *src_rect, im_rect *dst_rect, im_rect *pat_rect, int mode_usage);
/**
* block until all execution is complete
*
* @param release_fence_fd
* RGA job release fence fd
*
* @returns success or else negative error code.
*/
IM_EXPORT_API IM_STATUS imsync(int release_fence_fd);
/**
* config
*
* @param name
* enum IM_CONFIG_NAME
* @param value
*
* @returns success or else negative error code.
*/
IM_EXPORT_API IM_STATUS imconfig(IM_CONFIG_NAME name, uint64_t value);
#endif /* #ifndef _im2d_common_h_ */

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_expand_h_
#define _im2d_expand_h_
#ifdef __cplusplus
#include "im2d_type.h"
// #if ANDROID
// #include <ui/GraphicBuffer.h>
// using namespace android;
// IM_API rga_buffer_handle_t importbuffer_GraphicBuffer_handle(buffer_handle_t hnd);
// IM_API rga_buffer_handle_t importbuffer_GraphicBuffer(sp<GraphicBuffer> buf);
// IM_API rga_buffer_t wrapbuffer_handle(buffer_handle_t hnd);
// IM_API rga_buffer_t wrapbuffer_GraphicBuffer(sp<GraphicBuffer> buf);
// #if USE_AHARDWAREBUFFER
// #include <android/hardware_buffer.h>
// IM_API rga_buffer_handle_t importbuffer_AHardwareBuffer(AHardwareBuffer *buf);
// IM_API rga_buffer_t wrapbuffer_AHardwareBuffer(AHardwareBuffer *buf);
// #endif /* #if USE_AHARDWAREBUFFER */
// #endif /* #if ANDROID */
#endif /* #ifdef __cplusplus */
#endif

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_mpi_hpp_
#define _im2d_mpi_hpp_
#include "im2d_type.h"
/**
* Create and config an rga ctx for rockit-ko
*
* @param flags
* Some configuration flags for this job
*
* @returns job id.
*/
IM_EXPORT_API im_ctx_id_t imbegin(uint32_t flags);
/**
* Cancel and delete an rga ctx for rockit-ko
*
* @param flags
* Some configuration flags for this job
*
* @returns success or else negative error code.
*/
IM_EXPORT_API IM_STATUS imcancel(im_ctx_id_t id);
/**
* process for rockit-ko
*
* @param src
* The input source image and is also the foreground image in blend.
* @param dst
* The output destination image and is also the foreground image in blend.
* @param pat
* The foreground image, or a LUT table.
* @param srect
* The rectangle on the src channel image that needs to be processed.
* @param drect
* The rectangle on the dst channel image that needs to be processed.
* @param prect
* The rectangle on the pat channel image that needs to be processed.
* @param acquire_fence_fd
* @param release_fence_fd
* @param opt
* The image processing options configuration.
* @param usage
* The image processing usage.
* @param ctx_id
* ctx id
*
* @returns success or else negative error code.
*/
#ifdef __cplusplus
IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect,
int acquire_fence_fd, int *release_fence_fd,
im_opt_t *opt, int usage, im_ctx_id_t ctx_id);
#endif
IM_EXPORT_API IM_STATUS improcess_ctx(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect,
int acquire_fence_fd, int *release_fence_fd,
im_opt_t *opt, int usage, im_ctx_id_t ctx_id);
#endif /* #ifndef _im2d_mpi_hpp_ */

View File

@@ -0,0 +1,940 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_single_h_
#define _im2d_single_h_
#include "im2d_type.h"
#ifdef __cplusplus
/**
* copy
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcopy(const rga_buffer_t src, rga_buffer_t dst, int sync = 1, int *release_fence_fd = NULL);
/**
* Resize
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param fx
* X-direction resize factor.
* @param fy
* X-direction resize factor.
* @param interpolation
* Interpolation formula(Only RGA1 support).
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imresize(const rga_buffer_t src, rga_buffer_t dst, double fx = 0, double fy = 0, int interpolation = 0, int sync = 1, int *release_fence_fd = NULL);
/**
* Crop
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be cropped.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcrop(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync = 1, int *release_fence_fd = NULL);
/**
* translate
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param x
* Output the coordinates of the starting point in the X-direction of the destination image.
* @param y
* Output the coordinates of the starting point in the Y-direction of the destination image.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imtranslate(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync = 1, int *release_fence_fd = NULL);
/**
* format convert
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param sfmt
* The source image format.
* @param dfmt
* The destination image format.
* @param mode
* color space mode:
* IM_YUV_TO_RGB_BT601_LIMIT
* IM_YUV_TO_RGB_BT601_FULL
* IM_YUV_TO_RGB_BT709_LIMIT
* IM_RGB_TO_YUV_BT601_FULL
* IM_RGB_TO_YUV_BT601_LIMIT
* IM_RGB_TO_YUV_BT709_LIMIT
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcvtcolor(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode = IM_COLOR_SPACE_DEFAULT, int sync = 1, int *release_fence_fd = NULL);
/**
* rotation
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param rotation
* IM_HAL_TRANSFORM_ROT_90
* IM_HAL_TRANSFORM_ROT_180
* IM_HAL_TRANSFORM_ROT_270
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrotate(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync = 1, int *release_fence_fd = NULL);
/**
* flip
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param mode
* IM_HAL_TRANSFORM_FLIP_H
* IM_HAL_TRANSFORM_FLIP_V
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imflip(const rga_buffer_t src, rga_buffer_t dst, int mode, int sync = 1, int *release_fence_fd = NULL);
/**
* 2-channel blend (SRC + DST -> DST or SRCA + SRCB -> DST)
*
* @param fg_image
* The foreground image.
* @param bg_image
* The background image, which is also the output destination image.
* @param mode
* Port-Duff mode:
* IM_ALPHA_BLEND_SRC
* IM_ALPHA_BLEND_DST
* IM_ALPHA_BLEND_SRC_OVER
* IM_ALPHA_BLEND_DST_OVER
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imblend(const rga_buffer_t fd_image, rga_buffer_t bg_image, int mode = IM_ALPHA_BLEND_SRC_OVER, int sync = 1, int *release_fence_fd = NULL);
/**
* 3-channel blend (SRC + DST -> DST or SRCA + SRCB -> DST)
*
* @param fg_image
* The foreground image.
* @param bg_image
* The background image.
* @param output_image
* The output destination image.
* @param mode
* Port-Duff mode:
* IM_ALPHA_BLEND_SRC
* IM_ALPHA_BLEND_DST
* IM_ALPHA_BLEND_SRC_OVER
* IM_ALPHA_BLEND_DST_OVER
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
* @param release_fence_fd
* When 'sync == 0', the fence_fd used to identify the current job state
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcomposite(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode = IM_ALPHA_BLEND_SRC_OVER, int sync = 1, int *release_fence_fd = NULL);
/**
* color key
*
* @param fg_image
* The foreground image.
* @param bg_image
* The background image, which is also the output destination image.
* @param colorkey_range
* The range of color key.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcolorkey(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode = IM_ALPHA_COLORKEY_NORMAL, int sync = 1, int *release_fence_fd = NULL);
/**
* OSD
*
* @param osd
* The osd text block.
* @param dst
* The background image.
* @param osd_rect
* The rectangle on the source image that needs to be OSD.
* @param osd_config
* osd mode configuration.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imosd(const rga_buffer_t osd,const rga_buffer_t dst,
const im_rect osd_rect, im_osd_t *osd_config,
int sync = 1, int *release_fence_fd = NULL);
/**
* nn quantize
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param nninfo
* nn configuration
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imquantize(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync = 1, int *release_fence_fd = NULL);
/**
* ROP
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param rop_code
* The ROP opcode.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrop(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync = 1, int *release_fence_fd = NULL);
/**
* fill/reset/draw
*
* @param dst
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be filled with color.
* @param color
* The fill color value.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imfill(rga_buffer_t dst, im_rect rect, int color, int sync = 1, int *release_fence_fd = NULL);
/**
* fill array
*
* @param dst
* The output destination image.
* @param rect_array
* The rectangle arrays on the source image that needs to be filled with color.
* @param array_size
* The size of rectangular area arrays.
* @param color
* The fill color value.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imfillArray(rga_buffer_t dst, im_rect *rect_array, int array_size, uint32_t color, int sync = 1, int *release_fence_fd = NULL);
/**
* fill rectangle
*
* @param dst
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be filled with color.
* @param color
* The fill color value.
* @param thickness
* Thickness of lines that make up the rectangle. Negative values, like -1,
* mean that the function has to draw a filled rectangle.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrectangle(rga_buffer_t dst, im_rect rect,
uint32_t color, int thickness,
int sync = 1, int *release_fence_fd = NULL);
/**
* fill rectangle array
*
* @param dst
* The output destination image.
* @param rect_array
* The rectangle arrays on the source image that needs to be filled with color.
* @param array_size
* The size of rectangular area arrays.
* @param color
* The fill color value.
* @param thickness
* Thickness of lines that make up the rectangle. Negative values, like -1,
* mean that the function has to draw a filled rectangle.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrectangleArray(rga_buffer_t dst, im_rect *rect_array, int array_size,
uint32_t color, int thickness,
int sync = 1, int *release_fence_fd = NULL);
/**
* MOSAIC
*
* @param image
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be mosaicked.
* @param mosaic_mode
* mosaic block width configuration:
* IM_MOSAIC_8
* IM_MOSAIC_16
* IM_MOSAIC_32
* IM_MOSAIC_64
* IM_MOSAIC_128
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immosaic(const rga_buffer_t image, im_rect rect, int mosaic_mode, int sync = 1, int *release_fence_fd = NULL);
/**
* MOSAIC array
*
* @param image
* The output destination image.
* @param rect_array
* The rectangle arrays on the source image that needs to be filled with color.
* @param array_size
* The size of rectangular area arrays.
* @param mosaic_mode
* mosaic block width configuration:
* IM_MOSAIC_8
* IM_MOSAIC_16
* IM_MOSAIC_32
* IM_MOSAIC_64
* IM_MOSAIC_128
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immosaicArray(const rga_buffer_t image, im_rect *rect_array, int array_size, int mosaic_mode, int sync = 1, int *release_fence_fd = NULL);
/**
* palette
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param lut
* The LUT table.
* @param sync
* When 'sync == 1', wait for the operation to complete and return, otherwise return directly.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS impalette(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync = 1, int *release_fence_fd = NULL);
/**
* process for single task mode
*
* @param src
* The input source image and is also the foreground image in blend.
* @param dst
* The output destination image and is also the foreground image in blend.
* @param pat
* The foreground image, or a LUT table.
* @param srect
* The rectangle on the src channel image that needs to be processed.
* @param drect
* The rectangle on the dst channel image that needs to be processed.
* @param prect
* The rectangle on the pat channel image that needs to be processed.
* @param opt
* The image processing options configuration.
* @param usage
* The image processing usage.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect,
int acquire_fence_fd, int *release_fence_fd,
im_opt_t *opt_ptr, int usage);
/**
* make border
*
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param top
* the top pixels
* @param bottom
* the bottom pixels
* @param left
* the left pixels
* @param right
* the right pixels
* @param border_type
* Border type.
* @param value
* The pixel value at which the border is filled.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immakeBorder(rga_buffer_t src, rga_buffer_t dst,
int top, int bottom, int left, int right,
int border_type, int value = 0,
int sync = 1, int acquir_fence_fd = -1, int *release_fence_fd = NULL);
#endif /* #ifdef __cplusplus */
IM_C_API IM_STATUS immosaic(const rga_buffer_t image, im_rect rect, int mosaic_mode, int sync);
IM_C_API IM_STATUS imosd(const rga_buffer_t osd,const rga_buffer_t dst,
const im_rect osd_rect, im_osd_t *osd_config, int sync);
IM_C_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect, int usage);
/* Start: Symbols reserved for compatibility with macro functions */
IM_C_API IM_STATUS imcopy_t(const rga_buffer_t src, rga_buffer_t dst, int sync);
IM_C_API IM_STATUS imresize_t(const rga_buffer_t src, rga_buffer_t dst, double fx, double fy, int interpolation, int sync);
IM_C_API IM_STATUS imcrop_t(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync);
IM_C_API IM_STATUS imtranslate_t(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync);
IM_C_API IM_STATUS imcvtcolor_t(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode, int sync);
IM_C_API IM_STATUS imrotate_t(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync);
IM_C_API IM_STATUS imflip_t (const rga_buffer_t src, rga_buffer_t dst, int mode, int sync);
IM_C_API IM_STATUS imblend_t(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode, int sync);
IM_C_API IM_STATUS imcolorkey_t(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode, int sync);
IM_C_API IM_STATUS imquantize_t(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync);
IM_C_API IM_STATUS imrop_t(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync);
IM_C_API IM_STATUS imfill_t(rga_buffer_t dst, im_rect rect, int color, int sync);
IM_C_API IM_STATUS impalette_t(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync);
/* End: Symbols reserved for compatibility with macro functions */
#ifndef __cplusplus
#define RGA_GET_MIN(n1, n2) ((n1) < (n2) ? (n1) : (n2))
/**
* copy
*
* @param src
* @param dst
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcopy(src, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcopy_t(src, dst, 1); \
} else if (__argc == 1){ \
__ret = imcopy_t(src, dst, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* Resize
*
* @param src
* @param dst
* @param fx
* @param fy
* @param interpolation
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imresize(src, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
double __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(double); \
if (__argc == 0) { \
__ret = imresize_t(src, dst, 0, 0, INTER_LINEAR, 1); \
} else if (__argc == 2){ \
__ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], INTER_LINEAR, 1); \
} else if (__argc == 3){ \
__ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], (int)__args[RGA_GET_MIN(__argc, 2)], 1); \
} else if (__argc == 4){ \
__ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], (int)__args[RGA_GET_MIN(__argc, 2)], (int)__args[RGA_GET_MIN(__argc, 3)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define impyramid(src, dst, direction) \
imresize_t(src, \
dst, \
direction == IM_UP_SCALE ? 0.5 : 2, \
direction == IM_UP_SCALE ? 0.5 : 2, \
INTER_LINEAR, 1)
/**
* format convert
*
* @param src
* @param dst
* @param sfmt
* @param dfmt
* @param mode
* color space mode: IM_COLOR_SPACE_MODE
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcvtcolor(src, dst, sfmt, dfmt, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcvtcolor_t(src, dst, sfmt, dfmt, IM_COLOR_SPACE_DEFAULT, 1); \
} else if (__argc == 1){ \
__ret = imcvtcolor_t(src, dst, sfmt, dfmt, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imcvtcolor_t(src, dst, sfmt, dfmt, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* Crop
*
* @param src
* @param dst
* @param rect
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcrop(src, dst, rect, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcrop_t(src, dst, rect, 1); \
} else if (__argc == 1){ \
__ret = imcrop_t(src, dst, rect, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* translate
*
* @param src
* @param dst
* @param x
* @param y
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imtranslate(src, dst, x, y, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imtranslate_t(src, dst, x, y, 1); \
} else if (__argc == 1){ \
__ret = imtranslate_t(src, dst, x, y, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* rotation
*
* @param src
* @param dst
* @param rotation
* IM_HAL_TRANSFORM_ROT_90
* IM_HAL_TRANSFORM_ROT_180
* IM_HAL_TRANSFORM_ROT_270
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imrotate(src, dst, rotation, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imrotate_t(src, dst, rotation, 1); \
} else if (__argc == 1){ \
__ret = imrotate_t(src, dst, rotation, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* flip
*
* @param src
* @param dst
* @param mode
* IM_HAL_TRANSFORM_FLIP_H
* IM_HAL_TRANSFORM_FLIP_V
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imflip(src, dst, mode, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imflip_t(src, dst, mode, 1); \
} else if (__argc == 1){ \
__ret = imflip_t(src, dst, mode, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* blend (SRC + DST -> DST or SRCA + SRCB -> DST)
*
* @param srcA
* @param srcB can be NULL.
* @param dst
* @param mode
* IM_ALPHA_BLEND_MODE
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imblend(srcA, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
rga_buffer_t srcB; \
memset(&srcB, 0x00, sizeof(rga_buffer_t)); \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \
} else if (__argc == 1){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define imcomposite(srcA, srcB, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \
} else if (__argc == 1){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* color key
*
* @param src
* @param dst
* @param colorkey_range
* max color
* min color
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcolorkey(src, dst, range, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcolorkey_t(src, dst, range, IM_ALPHA_COLORKEY_NORMAL, 1); \
} else if (__argc == 1){ \
__ret = imcolorkey_t(src, dst, range, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imcolorkey_t(src, dst, range, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* nn quantize
*
* @param src
* @param dst
* @param nninfo
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imquantize(src, dst, nn_info, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imquantize_t(src, dst, nn_info, 1); \
} else if (__argc == 1){ \
__ret = imquantize_t(src, dst, nn_info, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* ROP
*
* @param src
* @param dst
* @param rop_code
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imrop(src, dst, rop_code, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imrop_t(src, dst, rop_code, 1); \
} else if (__argc == 1){ \
__ret = imrop_t(src, dst, rop_code, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* fill/reset/draw
*
* @param src
* @param dst
* @param rect
* @param color
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imfill(buf, rect, color, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imfill_t(buf, rect, color, 1); \
} else if (__argc == 1){ \
__ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define imreset(buf, rect, color, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imfill_t(buf, rect, color, 1); \
} else if (__argc == 1){ \
__ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define imdraw(buf, rect, color, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imfill_t(buf, rect, color, 1); \
} else if (__argc == 1){ \
__ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/**
* palette
*
* @param src
* @param dst
* @param lut
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define impalette(src, dst, lut, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = impalette_t(src, dst, lut, 1); \
} else if (__argc == 1){ \
__ret = impalette_t(src, dst, lut, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
/* End define IM2D macro API */
#endif
#endif /* #ifndef _im2d_single_h_ */

View File

@@ -0,0 +1,497 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_task_h_
#define _im2d_task_h_
#include "im2d_type.h"
#ifdef __cplusplus
/**
* Create an rga job
*
* @param flags
* Some configuration flags for this job
*
* @returns job handle.
*/
IM_API im_job_handle_t imbeginJob(uint64_t flags = 0);
/**
* Submit and run an rga job
*
* @param job_handle
* This is the job handle that will be submitted.
* @param sync_mode
* run mode:
* IM_SYNC
* IM_ASYNC
* @param acquire_fence_fd
* @param release_fence_fd
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imendJob(im_job_handle_t job_handle,
int sync_mode = IM_SYNC,
int acquire_fence_fd = 0, int *release_fence_fd = NULL);
/**
* Cancel and delete an rga job
*
* @param job_handle
* This is the job handle that will be cancelled.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcancelJob(im_job_handle_t job_handle);
/**
* Add copy task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcopyTask(im_job_handle_t job_handle, const rga_buffer_t src, rga_buffer_t dst);
/**
* Add resize task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param fx
* X-direction resize factor.
* @param fy
* X-direction resize factor.
* @param interpolation
* Interpolation formula(Only RGA1 support).
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imresizeTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst,
double fx = 0, double fy = 0,
int interpolation = 0);
/**
* Add crop task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be cropped.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcropTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst, im_rect rect);
/**
* Add translate task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param x
* Output the coordinates of the starting point in the X-direction of the destination image.
* @param y
* Output the coordinates of the starting point in the Y-direction of the destination image.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imtranslateTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst, int x, int y);
/**
* Add format convert task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param sfmt
* The source image format.
* @param dfmt
* The destination image format.
* @param mode
* color space mode:
* IM_YUV_TO_RGB_BT601_LIMIT
* IM_YUV_TO_RGB_BT601_FULL
* IM_YUV_TO_RGB_BT709_LIMIT
* IM_RGB_TO_YUV_BT601_FULL
* IM_RGB_TO_YUV_BT601_LIMIT
* IM_RGB_TO_YUV_BT709_LIMIT
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcvtcolorTask(im_job_handle_t job_handle,
rga_buffer_t src, rga_buffer_t dst,
int sfmt, int dfmt, int mode = IM_COLOR_SPACE_DEFAULT);
/**
* Add rotation task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param rotation
* IM_HAL_TRANSFORM_ROT_90
* IM_HAL_TRANSFORM_ROT_180
* IM_HAL_TRANSFORM_ROT_270
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrotateTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst, int rotation);
/**
* Add flip task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param mode
* IM_HAL_TRANSFORM_FLIP_H
* IM_HAL_TRANSFORM_FLIP_V
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imflipTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst, int mode);
/**
* Add blend(SRC + DST -> DST) task
*
* @param job_handle
* Insert the task into the job handle.
* @param fg_image
* The foreground image.
* @param bg_image
* The background image, which is also the output destination image.
* @param mode
* Port-Duff mode:
* IM_ALPHA_BLEND_SRC
* IM_ALPHA_BLEND_DST
* IM_ALPHA_BLEND_SRC_OVER
* IM_ALPHA_BLEND_DST_OVER
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imblendTask(im_job_handle_t job_handle,
const rga_buffer_t fg_image, rga_buffer_t bg_image,
int mode = IM_ALPHA_BLEND_SRC_OVER);
/**
* Add composite(SRCA + SRCB -> DST) task
*
* @param job_handle
* Insert the task into the job handle.
* @param fg_image
* The foreground image.
* @param bg_image
* The background image.
* @param output_image
* The output destination image.
* @param mode
* Port-Duff mode:
* IM_ALPHA_BLEND_SRC
* IM_ALPHA_BLEND_DST
* IM_ALPHA_BLEND_SRC_OVER
* IM_ALPHA_BLEND_DST_OVER
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcompositeTask(im_job_handle_t job_handle,
const rga_buffer_t fg_image, const rga_buffer_t bg_image,
rga_buffer_t output_image,
int mode = IM_ALPHA_BLEND_SRC_OVER);
/**
* Add color key task
*
* @param job_handle
* Insert the task into the job handle.
* @param fg_image
* The foreground image.
* @param bg_image
* The background image, which is also the output destination image.
* @param colorkey_range
* The range of color key.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imcolorkeyTask(im_job_handle_t job_handle,
const rga_buffer_t fg_image, rga_buffer_t bg_image,
im_colorkey_range range, int mode = IM_ALPHA_COLORKEY_NORMAL);
/**
* Add OSD task
*
* @param job_handle
* Insert the task into the job handle.
* @param osd
* The osd text block.
* @param dst
* The background image.
* @param osd_rect
* The rectangle on the source image that needs to be OSD.
* @param osd_config
* osd mode configuration.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imosdTask(im_job_handle_t job_handle,
const rga_buffer_t osd,const rga_buffer_t bg_image,
const im_rect osd_rect, im_osd_t *osd_config);
/**
* Add nn quantize task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param nninfo
* nn configuration
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imquantizeTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info);
/**
* Add ROP task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param rop_code
* The ROP opcode.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imropTask(im_job_handle_t job_handle,
const rga_buffer_t src, rga_buffer_t dst, int rop_code);
/**
* Add color fill task
*
* @param job_handle
* Insert the task into the job handle.
* @param dst
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be filled with color.
* @param color
* The fill color value.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imfillTask(im_job_handle_t job_handle, rga_buffer_t dst, im_rect rect, uint32_t color);
/**
* Add color fill task array
*
* @param job_handle
* Insert the task into the job handle.
* @param dst
* The output destination image.
* @param rect_array
* The rectangle arrays on the source image that needs to be filled with color.
* @param array_size
* The size of rectangular area arrays.
* @param color
* The fill color value.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imfillTaskArray(im_job_handle_t job_handle,
rga_buffer_t dst,
im_rect *rect_array, int array_size, uint32_t color);
/**
* Add fill rectangle task
*
* @param job_handle
* Insert the task into the job handle.
* @param dst
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be filled with color.
* @param color
* The fill color value.
* @param thickness
* Thickness of lines that make up the rectangle. Negative values, like -1,
* mean that the function has to draw a filled rectangle.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrectangleTask(im_job_handle_t job_handle,
rga_buffer_t dst,
im_rect rect,
uint32_t color, int thickness);
/**
* Add fill rectangle task array
*
* @param job_handle
* Insert the task into the job handle.
* @param dst
* The output destination image.
* @param rect_array
* The rectangle arrays on the source image that needs to be filled with color.
* @param array_size
* The size of rectangular area arrays.
* @param color
* The fill color value.
* @param thickness
* Thickness of lines that make up the rectangle. Negative values, like -1,
* mean that the function has to draw a filled rectangle.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imrectangleTaskArray(im_job_handle_t job_handle,
rga_buffer_t dst,
im_rect *rect_array, int array_size,
uint32_t color, int thickness);
/**
* Add mosaic task
*
* @param job_handle
* Insert the task into the job handle.
* @param image
* The output destination image.
* @param rect
* The rectangle on the source image that needs to be mosaicked.
* @param mosaic_mode
* mosaic block width configuration:
* IM_MOSAIC_8
* IM_MOSAIC_16
* IM_MOSAIC_32
* IM_MOSAIC_64
* IM_MOSAIC_128
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immosaicTask(im_job_handle_t job_handle,
const rga_buffer_t image, im_rect rect, int mosaic_mode);
/**
* Add mosaic task
*
* @param job_handle
* Insert the task into the job handle.
* @param image
* The output destination image.
* @param rect_array
* The rectangle arrays on the source image that needs to be filled with color.
* @param array_size
* The size of rectangular area arrays.
* @param mosaic_mode
* mosaic block width configuration:
* IM_MOSAIC_8
* IM_MOSAIC_16
* IM_MOSAIC_32
* IM_MOSAIC_64
* IM_MOSAIC_128
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immosaicTaskArray(im_job_handle_t job_handle,
const rga_buffer_t image,
im_rect *rect_array, int array_size, int mosaic_mode);
/**
* Add palette task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image.
* @param dst
* The output destination image.
* @param lut
* The LUT table.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS impaletteTask(im_job_handle_t job_handle,
rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut);
/**
* Add process task
*
* @param job_handle
* Insert the task into the job handle.
* @param src
* The input source image and is also the foreground image in blend.
* @param dst
* The output destination image and is also the foreground image in blend.
* @param pat
* The foreground image, or a LUT table.
* @param srect
* The rectangle on the src channel image that needs to be processed.
* @param drect
* The rectangle on the dst channel image that needs to be processed.
* @param prect
* The rectangle on the pat channel image that needs to be processed.
* @param opt
* The image processing options configuration.
* @param usage
* The image processing usage.
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS improcessTask(im_job_handle_t job_handle,
rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect,
im_opt_t *opt_ptr, int usage);
#endif /* #ifdef __cplusplus */
#endif /* #ifndef _im2d_task_h_ */

View File

@@ -0,0 +1,436 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_IM2D_TYPE_H_
#define _RGA_IM2D_TYPE_H_
#include <stdint.h>
#include "rga.h"
#define IM_API /* define API export as needed */
#ifdef __cplusplus
#define IM_C_API extern "C"
#define IM_EXPORT_API extern "C"
#else
#define IM_C_API
#define IM_EXPORT_API
#endif
#ifdef __cplusplus
#define DEFAULT_INITIALIZER(x) = x
#else
#define DEFAULT_INITIALIZER(x)
#endif
typedef uint32_t im_api_version_t;
typedef uint32_t im_job_handle_t;
typedef uint32_t im_ctx_id_t;
typedef uint32_t rga_buffer_handle_t;
typedef enum {
/* Rotation */
IM_HAL_TRANSFORM_ROT_90 = 1 << 0,
IM_HAL_TRANSFORM_ROT_180 = 1 << 1,
IM_HAL_TRANSFORM_ROT_270 = 1 << 2,
IM_HAL_TRANSFORM_FLIP_H = 1 << 3,
IM_HAL_TRANSFORM_FLIP_V = 1 << 4,
IM_HAL_TRANSFORM_FLIP_H_V = 1 << 5,
IM_HAL_TRANSFORM_MASK = 0x3f,
/*
* Blend
* Additional blend usage, can be used with both source and target configs.
* If none of the below is set, the default "SRC over DST" is applied.
*/
IM_ALPHA_BLEND_SRC_OVER = 1 << 6, /* Default, Porter-Duff "SRC over DST" */
IM_ALPHA_BLEND_SRC = 1 << 7, /* Porter-Duff "SRC" */
IM_ALPHA_BLEND_DST = 1 << 8, /* Porter-Duff "DST" */
IM_ALPHA_BLEND_SRC_IN = 1 << 9, /* Porter-Duff "SRC in DST" */
IM_ALPHA_BLEND_DST_IN = 1 << 10, /* Porter-Duff "DST in SRC" */
IM_ALPHA_BLEND_SRC_OUT = 1 << 11, /* Porter-Duff "SRC out DST" */
IM_ALPHA_BLEND_DST_OUT = 1 << 12, /* Porter-Duff "DST out SRC" */
IM_ALPHA_BLEND_DST_OVER = 1 << 13, /* Porter-Duff "DST over SRC" */
IM_ALPHA_BLEND_SRC_ATOP = 1 << 14, /* Porter-Duff "SRC ATOP" */
IM_ALPHA_BLEND_DST_ATOP = 1 << 15, /* Porter-Duff "DST ATOP" */
IM_ALPHA_BLEND_XOR = 1 << 16, /* Xor */
IM_ALPHA_BLEND_MASK = 0x1ffc0,
IM_ALPHA_COLORKEY_NORMAL = 1 << 17,
IM_ALPHA_COLORKEY_INVERTED = 1 << 18,
IM_ALPHA_COLORKEY_MASK = 0x60000,
IM_SYNC = 1 << 19,
IM_CROP = 1 << 20, /* Unused */
IM_COLOR_FILL = 1 << 21,
IM_COLOR_PALETTE = 1 << 22,
IM_NN_QUANTIZE = 1 << 23,
IM_ROP = 1 << 24,
IM_ALPHA_BLEND_PRE_MUL = 1 << 25,
IM_ASYNC = 1 << 26,
IM_MOSAIC = 1 << 27,
IM_OSD = 1 << 28,
IM_PRE_INTR = 1 << 29,
} IM_USAGE;
typedef enum {
IM_RASTER_MODE = 1 << 0,
IM_FBC_MODE = 1 << 1,
IM_TILE_MODE = 1 << 2,
} IM_RD_MODE;
typedef enum {
IM_SCHEDULER_RGA3_CORE0 = 1 << 0,
IM_SCHEDULER_RGA3_CORE1 = 1 << 1,
IM_SCHEDULER_RGA2_CORE0 = 1 << 2,
IM_SCHEDULER_RGA3_DEFAULT = IM_SCHEDULER_RGA3_CORE0,
IM_SCHEDULER_RGA2_DEFAULT = IM_SCHEDULER_RGA2_CORE0,
IM_SCHEDULER_MASK = 0x7,
IM_SCHEDULER_DEFAULT = 0,
} IM_SCHEDULER_CORE;
typedef enum {
IM_ROP_AND = 0x88,
IM_ROP_OR = 0xee,
IM_ROP_NOT_DST = 0x55,
IM_ROP_NOT_SRC = 0x33,
IM_ROP_XOR = 0xf6,
IM_ROP_NOT_XOR = 0xf9,
} IM_ROP_CODE;
typedef enum {
IM_MOSAIC_8 = 0x0,
IM_MOSAIC_16 = 0x1,
IM_MOSAIC_32 = 0x2,
IM_MOSAIC_64 = 0x3,
IM_MOSAIC_128 = 0x4,
} IM_MOSAIC_MODE;
typedef enum {
IM_BORDER_CONSTANT = 0, /* iiiiii|abcdefgh|iiiiiii with some specified value 'i' */
IM_BORDER_REFLECT = 2, /* fedcba|abcdefgh|hgfedcb */
IM_BORDER_WRAP = 3, /* cdefgh|abcdefgh|abcdefg */
} IM_BORDER_TYPE;
/* Status codes, returned by any blit function */
typedef enum {
IM_YUV_TO_RGB_BT601_LIMIT = 1 << 0,
IM_YUV_TO_RGB_BT601_FULL = 2 << 0,
IM_YUV_TO_RGB_BT709_LIMIT = 3 << 0,
IM_YUV_TO_RGB_MASK = 3 << 0,
IM_RGB_TO_YUV_BT601_FULL = 1 << 2,
IM_RGB_TO_YUV_BT601_LIMIT = 2 << 2,
IM_RGB_TO_YUV_BT709_LIMIT = 3 << 2,
IM_RGB_TO_YUV_MASK = 3 << 2,
IM_RGB_TO_Y4 = 1 << 4,
IM_RGB_TO_Y4_DITHER = 2 << 4,
IM_RGB_TO_Y1_DITHER = 3 << 4,
IM_Y4_MASK = 3 << 4,
IM_RGB_FULL = 1 << 8,
IM_RGB_CLIP = 2 << 8,
IM_YUV_BT601_LIMIT_RANGE = 3 << 8,
IM_YUV_BT601_FULL_RANGE = 4 << 8,
IM_YUV_BT709_LIMIT_RANGE = 5 << 8,
IM_YUV_BT709_FULL_RANGE = 6 << 8,
IM_FULL_CSC_MASK = 0xf << 8,
IM_COLOR_SPACE_DEFAULT = 0,
} IM_COLOR_SPACE_MODE;
typedef enum {
IM_UP_SCALE,
IM_DOWN_SCALE,
} IM_SCALE;
typedef enum {
INTER_NEAREST,
INTER_LINEAR,
INTER_CUBIC,
} IM_SCALE_MODE;
typedef enum {
IM_CONFIG_SCHEDULER_CORE,
IM_CONFIG_PRIORITY,
IM_CONFIG_CHECK,
} IM_CONFIG_NAME;
typedef enum {
IM_OSD_MODE_STATISTICS = 0x1 << 0,
IM_OSD_MODE_AUTO_INVERT = 0x1 << 1,
} IM_OSD_MODE;
typedef enum {
IM_OSD_INVERT_CHANNEL_NONE = 0x0,
IM_OSD_INVERT_CHANNEL_Y_G = 0x1 << 0,
IM_OSD_INVERT_CHANNEL_C_RB = 0x1 << 1,
IM_OSD_INVERT_CHANNEL_ALPHA = 0x1 << 2,
IM_OSD_INVERT_CHANNEL_COLOR = IM_OSD_INVERT_CHANNEL_Y_G |
IM_OSD_INVERT_CHANNEL_C_RB,
IM_OSD_INVERT_CHANNEL_BOTH = IM_OSD_INVERT_CHANNEL_COLOR |
IM_OSD_INVERT_CHANNEL_ALPHA,
} IM_OSD_INVERT_CHANNEL;
typedef enum {
IM_OSD_FLAGS_INTERNAL = 0,
IM_OSD_FLAGS_EXTERNAL,
} IM_OSD_FLAGS_MODE;
typedef enum {
IM_OSD_INVERT_USE_FACTOR,
IM_OSD_INVERT_USE_SWAP,
} IM_OSD_INVERT_MODE;
typedef enum {
IM_OSD_BACKGROUND_DEFAULT_BRIGHT = 0,
IM_OSD_BACKGROUND_DEFAULT_DARK,
} IM_OSD_BACKGROUND_DEFAULT;
typedef enum {
IM_OSD_BLOCK_MODE_NORMAL = 0,
IM_OSD_BLOCK_MODE_DIFFERENT,
} IM_OSD_BLOCK_WIDTH_MODE;
typedef enum {
IM_OSD_MODE_HORIZONTAL,
IM_OSD_MODE_VERTICAL,
} IM_OSD_DIRECTION;
typedef enum {
IM_OSD_COLOR_PIXEL,
IM_OSD_COLOR_EXTERNAL,
} IM_OSD_COLOR_MODE;
typedef enum {
IM_INTR_READ_INTR = 1 << 0,
IM_INTR_READ_HOLD = 1 << 1,
IM_INTR_WRITE_INTR = 1 << 2,
} IM_PRE_INTR_FLAGS;
typedef enum {
IM_CONTEXT_NONE = 0x0,
IM_CONTEXT_SRC_FIX_ENABLE = 0x1 << 0, // Enable kernel to modify the image parameters of the channel.
IM_CONTEXT_SRC_CACHE_INFO = 0x1 << 1, // It will replace the parameters in ctx with the modified parameters.
IM_CONTEXT_SRC1_FIX_ENABLE = 0x1 << 2,
IM_CONTEXT_SRC1_CACHE_INFO = 0x1 << 3,
IM_CONTEXT_DST_FIX_ENABLE = 0x1 << 4,
IM_CONTEXT_DST_CACHE_INFO = 0x1 << 5,
} IM_CONTEXT_FLAGS;
/* Get RGA basic information index */
typedef enum {
RGA_VENDOR = 0,
RGA_VERSION,
RGA_MAX_INPUT,
RGA_MAX_OUTPUT,
RGA_BYTE_STRIDE,
RGA_SCALE_LIMIT,
RGA_INPUT_FORMAT,
RGA_OUTPUT_FORMAT,
RGA_FEATURE,
RGA_EXPECTED,
RGA_ALL,
} IM_INFORMATION;
/* Status codes, returned by any blit function */
typedef enum {
IM_STATUS_NOERROR = 2,
IM_STATUS_SUCCESS = 1,
IM_STATUS_NOT_SUPPORTED = -1,
IM_STATUS_OUT_OF_MEMORY = -2,
IM_STATUS_INVALID_PARAM = -3,
IM_STATUS_ILLEGAL_PARAM = -4,
IM_STATUS_ERROR_VERSION = -5,
IM_STATUS_FAILED = 0,
} IM_STATUS;
/* Rectangle definition */
typedef struct {
int x; /* upper-left x */
int y; /* upper-left y */
int width; /* width */
int height; /* height */
} im_rect;
typedef struct {
int max; /* The Maximum value of the color key */
int min; /* The minimum value of the color key */
} im_colorkey_range;
typedef struct im_nn {
int scale_r; /* scaling factor on R channal */
int scale_g; /* scaling factor on G channal */
int scale_b; /* scaling factor on B channal */
int offset_r; /* offset on R channal */
int offset_g; /* offset on G channal */
int offset_b; /* offset on B channal */
} im_nn_t;
/* im_info definition */
typedef struct {
void* vir_addr; /* virtual address */
void* phy_addr; /* physical address */
int fd; /* shared fd */
int width; /* width */
int height; /* height */
int wstride; /* wstride */
int hstride; /* hstride */
int format; /* format */
int color_space_mode; /* color_space_mode */
int global_alpha; /* global_alpha */
int rd_mode;
/* legarcy */
int color; /* color, used by color fill */
im_colorkey_range colorkey_range; /* range value of color key */
im_nn_t nn;
int rop_code;
rga_buffer_handle_t handle; /* buffer handle */
} rga_buffer_t;
typedef struct im_color {
union {
struct {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
uint32_t value;
};
} im_color_t;
typedef struct im_osd_invert_factor {
uint8_t alpha_max;
uint8_t alpha_min;
uint8_t yg_max;
uint8_t yg_min;
uint8_t crb_max;
uint8_t crb_min;
} im_osd_invert_factor_t;
typedef struct im_osd_bpp2 {
uint8_t ac_swap; // ac swap flag
// 0: CA
// 1: AC
uint8_t endian_swap; // rgba2bpp endian swap
// 0: Big endian
// 1: Little endian
im_color_t color0;
im_color_t color1;
} im_osd_bpp2_t;
typedef struct im_osd_block {
int width_mode; // normal or different
// IM_OSD_BLOCK_MODE_NORMAL
// IM_OSD_BLOCK_MODE_DIFFERENT
union {
int width; // normal_mode block width
int width_index; // different_mode block width index in RAM
};
int block_count; // block count
int background_config; // background config is bright or dark
// IM_OSD_BACKGROUND_DEFAULT_BRIGHT
// IM_OSD_BACKGROUND_DEFAULT_DARK
int direction; // osd block direction
// IM_OSD_MODE_HORIZONTAL
// IM_OSD_MODE_VERTICAL
int color_mode; // using src1 color or config color
// IM_OSD_COLOR_PIXEL
// IM_OSD_COLOR_EXTERNAL
im_color_t normal_color; // config color: normal
im_color_t invert_color; // config color: invert
} im_osd_block_t;
typedef struct im_osd_invert {
int invert_channel; // invert channel config:
// IM_OSD_INVERT_CHANNEL_NONE
// IM_OSD_INVERT_CHANNEL_Y_G
// IM_OSD_INVERT_CHANNEL_C_RB
// IM_OSD_INVERT_CHANNEL_ALPHA
// IM_OSD_INVERT_CHANNEL_COLOR
// IM_OSD_INVERT_CHANNEL_BOTH
int flags_mode; // use external or inertnal RAM invert flags
// IM_OSD_FLAGS_EXTERNAL
// IM_OSD_FLAGS_INTERNAL
int flags_index; // flags index when using internal RAM invert flags
uint64_t invert_flags; // external invert flags
uint64_t current_flags; // current flags
int invert_mode; // invert use swap or factor
// IM_OSD_INVERT_USE_FACTOR
// IM_OSD_INVERT_USE_SWAP
im_osd_invert_factor_t factor;
int threash;
} im_osd_invert_t;
typedef struct im_osd {
int osd_mode; // osd mode: statistics or auto_invert
// IM_OSD_MODE_STATISTICS
// IM_OSD_MODE_AUTO_INVERT
im_osd_block_t block_parm; // osd block info config
im_osd_invert_t invert_config;
im_osd_bpp2_t bpp2_info;
} im_osd_t;
typedef struct im_intr_config {
uint32_t flags;
int read_threshold;
int write_start;
int write_step;
} im_intr_config_t;
typedef struct im_opt {
im_api_version_t version DEFAULT_INITIALIZER(RGA_CURRENT_API_HEADER_VERSION);
int color; /* color, used by color fill */
im_colorkey_range colorkey_range; /* range value of color key */
im_nn_t nn;
int rop_code;
int priority;
int core;
int mosaic_mode;
im_osd_t osd_config;
im_intr_config_t intr_config;
char reserve[128];
} im_opt_t;
typedef struct im_handle_param {
uint32_t width;
uint32_t height;
uint32_t format;
} im_handle_param_t;
#endif /* _RGA_IM2D_TYPE_H_ */

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_IM2D_VERSION_H_
#define _RGA_IM2D_VERSION_H_
#define RGA_VERSION_STR_HELPER(x) #x
#define RGA_VERSION_STR(x) RGA_VERSION_STR_HELPER(x)
/* RGA im2d api verison */
#define RGA_API_MAJOR_VERSION 1
#define RGA_API_MINOR_VERSION 9
#define RGA_API_REVISION_VERSION 1
#define RGA_API_BUILD_VERSION 4
#define RGA_API_VERSION \
RGA_VERSION_STR(RGA_API_MAJOR_VERSION) "." \
RGA_VERSION_STR(RGA_API_MINOR_VERSION) "." \
RGA_VERSION_STR(RGA_API_REVISION_VERSION) "_[" \
RGA_VERSION_STR(RGA_API_BUILD_VERSION) "]"
#define RGA_API_FULL_VERSION "rga_api version " RGA_API_VERSION
/* For header file version verification */
#define RGA_CURRENT_API_VERSION (\
(RGA_API_MAJOR_VERSION & 0xff) << 24 | \
(RGA_API_MINOR_VERSION & 0xff) << 16 | \
(RGA_API_REVISION_VERSION & 0xff) << 8 | \
(RGA_API_BUILD_VERSION & 0xff)\
)
#define RGA_CURRENT_API_HEADER_VERSION RGA_CURRENT_API_VERSION
#endif /* _RGA_IM2D_VERSION_H_ */

View File

@@ -0,0 +1,139 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_DRIVER_H_
#define _RGA_DRIVER_H_
#ifndef ENABLE
#define ENABLE 1
#endif
#ifndef DISABLE
#define DISABLE 0
#endif
#ifdef __cplusplus
extern "C"
{
#endif
/* In order to be compatible with RK_FORMAT_XX and HAL_PIXEL_FORMAT_XX,
* RK_FORMAT_XX is shifted to the left by 8 bits to distinguish. */
typedef enum _Rga_SURF_FORMAT {
RK_FORMAT_RGBA_8888 = 0x0 << 8,
RK_FORMAT_RGBX_8888 = 0x1 << 8,
RK_FORMAT_RGB_888 = 0x2 << 8,
RK_FORMAT_BGRA_8888 = 0x3 << 8,
RK_FORMAT_RGB_565 = 0x4 << 8,
RK_FORMAT_RGBA_5551 = 0x5 << 8,
RK_FORMAT_RGBA_4444 = 0x6 << 8,
RK_FORMAT_BGR_888 = 0x7 << 8,
RK_FORMAT_YCbCr_422_SP = 0x8 << 8,
RK_FORMAT_YCbCr_422_P = 0x9 << 8,
RK_FORMAT_YCbCr_420_SP = 0xa << 8,
RK_FORMAT_YCbCr_420_P = 0xb << 8,
RK_FORMAT_YCrCb_422_SP = 0xc << 8,
RK_FORMAT_YCrCb_422_P = 0xd << 8,
RK_FORMAT_YCrCb_420_SP = 0xe << 8,
RK_FORMAT_YCrCb_420_P = 0xf << 8,
RK_FORMAT_BPP1 = 0x10 << 8,
RK_FORMAT_BPP2 = 0x11 << 8,
RK_FORMAT_BPP4 = 0x12 << 8,
RK_FORMAT_BPP8 = 0x13 << 8,
RK_FORMAT_Y4 = 0x14 << 8,
RK_FORMAT_YCbCr_400 = 0x15 << 8,
RK_FORMAT_BGRX_8888 = 0x16 << 8,
RK_FORMAT_YVYU_422 = 0x18 << 8,
RK_FORMAT_YVYU_420 = 0x19 << 8,
RK_FORMAT_VYUY_422 = 0x1a << 8,
RK_FORMAT_VYUY_420 = 0x1b << 8,
RK_FORMAT_YUYV_422 = 0x1c << 8,
RK_FORMAT_YUYV_420 = 0x1d << 8,
RK_FORMAT_UYVY_422 = 0x1e << 8,
RK_FORMAT_UYVY_420 = 0x1f << 8,
RK_FORMAT_YCbCr_420_SP_10B = 0x20 << 8,
RK_FORMAT_YCrCb_420_SP_10B = 0x21 << 8,
RK_FORMAT_YCbCr_422_SP_10B = 0x22 << 8,
RK_FORMAT_YCrCb_422_SP_10B = 0x23 << 8,
/* For compatibility with misspellings */
RK_FORMAT_YCbCr_422_10b_SP = RK_FORMAT_YCbCr_422_SP_10B,
RK_FORMAT_YCrCb_422_10b_SP = RK_FORMAT_YCrCb_422_SP_10B,
RK_FORMAT_BGR_565 = 0x24 << 8,
RK_FORMAT_BGRA_5551 = 0x25 << 8,
RK_FORMAT_BGRA_4444 = 0x26 << 8,
RK_FORMAT_ARGB_8888 = 0x28 << 8,
RK_FORMAT_XRGB_8888 = 0x29 << 8,
RK_FORMAT_ARGB_5551 = 0x2a << 8,
RK_FORMAT_ARGB_4444 = 0x2b << 8,
RK_FORMAT_ABGR_8888 = 0x2c << 8,
RK_FORMAT_XBGR_8888 = 0x2d << 8,
RK_FORMAT_ABGR_5551 = 0x2e << 8,
RK_FORMAT_ABGR_4444 = 0x2f << 8,
RK_FORMAT_RGBA2BPP = 0x30 << 8,
RK_FORMAT_UNKNOWN = 0x100 << 8,
} RgaSURF_FORMAT;
enum {
yuv2rgb_mode0 = 0x0, /* BT.601 MPEG */
yuv2rgb_mode1 = 0x1, /* BT.601 JPEG */
yuv2rgb_mode2 = 0x2, /* BT.709 */
rgb2yuv_601_full = 0x1 << 8,
rgb2yuv_709_full = 0x2 << 8,
yuv2yuv_601_limit_2_709_limit = 0x3 << 8,
yuv2yuv_601_limit_2_709_full = 0x4 << 8,
yuv2yuv_709_limit_2_601_limit = 0x5 << 8,
yuv2yuv_709_limit_2_601_full = 0x6 << 8, //not support
yuv2yuv_601_full_2_709_limit = 0x7 << 8,
yuv2yuv_601_full_2_709_full = 0x8 << 8, //not support
yuv2yuv_709_full_2_601_limit = 0x9 << 8, //not support
yuv2yuv_709_full_2_601_full = 0xa << 8, //not support
full_csc_mask = 0xf00,
};
enum {
RGA3_SCHEDULER_CORE0 = 1 << 0,
RGA3_SCHEDULER_CORE1 = 1 << 1,
RGA2_SCHEDULER_CORE0 = 1 << 2,
};
/* RGA3 rd_mode */
enum
{
raster_mode = 0x1 << 0,
fbc_mode = 0x1 << 1,
tile_mode = 0x1 << 2,
};
#ifdef __cplusplus
}
#endif
#endif /*_RK29_IPP_DRIVER_H_*/

View File

@@ -0,0 +1,74 @@
# 设置最低版本号
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
# 设置项目名称
project(rk3588-demo VERSION 0.0.1 LANGUAGES CXX)
# 输出系统信息
message(STATUS "System: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}")
# 设置编译器
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 设置库架构
set(LIB_ARCH "aarch64")
set(DEVICE_NAME "RK3588")
link_directories(
mpp_api
mk_api
)
# 寻找OpenCV库使用自定义的OpenCV_DIR
set(3RDPARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty)
set(RGA_DIR ${3RDPARTY_PATH}/rga/${DEVICE_NAME})
set(RGA_LIB ${RGA_DIR}/lib/Linux/${LIB_ARCH}/librga.so)
# set(OpenCV_DIR ${3RDPARTY_PATH}/opencv/opencv-linux-${LIB_ARCH}/share/OpenCV)
find_package(OpenCV REQUIRED)
# 输出OpenCV信息
message(STATUS "include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "${OpenCV_VERSION}")
# 用来搜索头文件的目录
include_directories(
${OpenCV_INCLUDE_DIRS}
${RKNN_API_INCLUDE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/src
${RGA_DIR}/include
${CMAKE_CURRENT_LIST_DIR}/mpp_api/include
${CMAKE_CURRENT_LIST_DIR}/mk_api/include
)
# 测试usb_to_rtsp
add_executable(usb_to_rtsp
src/usb_to_rtsp.cpp
src/rkmedia/utils/mpp_decoder.cpp
src/rkmedia/utils/mpp_encoder.cpp
)
# 链接库
target_link_libraries(usb_to_rtsp
rockchip_mpp
utils
mk_api
${OpenCV_LIBS}
${RGA_LIB}
)
# 测试yolov8_video_track
add_executable(rtsp_to_rtsp
src/rtsp_to_rtsp.cpp
src/rkmedia/utils/mpp_decoder.cpp
src/rkmedia/utils/mpp_encoder.cpp
)
# 链接库
target_link_libraries(rtsp_to_rtsp
rockchip_mpp
utils
mk_api
${OpenCV_LIBS}
${RGA_LIB}
)

View File

@@ -0,0 +1,194 @@
木兰宽松许可证第2版
木兰宽松许可证第2版
2020年1月 http://license.coscl.org.cn/MulanPSL2
您对“软件”的复制、使用、修改及分发受木兰宽松许可证第2版“本许可证”的如下条款的约束
0. 定义
“软件” 是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
“贡献” 是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
“贡献者” 是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
“法人实体” 是指提交贡献的机构及其“关联实体”。
“关联实体” 是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是
指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
1. 授予版权许可
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可
以复制、使用、修改、分发其“贡献”,不论修改与否。
2. 授予专利许可
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定
撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡
献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软
件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“
关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或
其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权
行动之日终止。
3. 无商标许可
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可但您为满足第4条规定
的声明义务而必须使用除外。
4. 分发限制
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“
本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
5. 免责声明与责任限制
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对
任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于
何种法律理论,即使其曾被建议有此种损失的可能性。
6. 语言
“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文
版为准。
条款结束
如何将木兰宽松许可证第2版应用到您的软件
如果您希望将木兰宽松许可证第2版应用到您的新软件为了方便接收者查阅建议您完成如下三步
1 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
2 请您在软件包的一级目录下创建以“LICENSE”为名的文件将整个许可证文本放入该文件中
3 请将如下声明文本放入每个源文件的头部注释中。
Copyright (c) [Year] [name of copyright holder]
[Software Name] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
Mulan Permissive Software LicenseVersion 2
Mulan Permissive Software LicenseVersion 2 (Mulan PSL v2)
January 2020 http://license.coscl.org.cn/MulanPSL2
Your reproduction, use, modification and distribution of the Software shall
be subject to Mulan PSL v2 (this License) with the following terms and
conditions:
0. Definition
Software means the program and related documents which are licensed under
this License and comprise all Contribution(s).
Contribution means the copyrightable work licensed by a particular
Contributor under this License.
Contributor means the Individual or Legal Entity who licenses its
copyrightable work under this License.
Legal Entity means the entity making a Contribution and all its
Affiliates.
Affiliates means entities that control, are controlled by, or are under
common control with the acting entity under this License, control means
direct or indirect ownership of at least fifty percent (50%) of the voting
power, capital or other securities of controlled or commonly controlled
entity.
1. Grant of Copyright License
Subject to the terms and conditions of this License, each Contributor hereby
grants to you a perpetual, worldwide, royalty-free, non-exclusive,
irrevocable copyright license to reproduce, use, modify, or distribute its
Contribution, with modification or not.
2. Grant of Patent License
Subject to the terms and conditions of this License, each Contributor hereby
grants to you a perpetual, worldwide, royalty-free, non-exclusive,
irrevocable (except for revocation under this Section) patent license to
make, have made, use, offer for sale, sell, import or otherwise transfer its
Contribution, where such patent license is only limited to the patent claims
owned or controlled by such Contributor now or in future which will be
necessarily infringed by its Contribution alone, or by combination of the
Contribution with the Software to which the Contribution was contributed.
The patent license shall not apply to any modification of the Contribution,
and any other combination which includes the Contribution. If you or your
Affiliates directly or indirectly institute patent litigation (including a
cross claim or counterclaim in a litigation) or other patent enforcement
activities against any individual or entity by alleging that the Software or
any Contribution in it infringes patents, then any patent license granted to
you under this License for the Software shall terminate as of the date such
litigation or activity is filed or taken.
3. No Trademark License
No trademark license is granted to use the trade names, trademarks, service
marks, or product names of Contributor, except as required to fulfill notice
requirements in section 4.
4. Distribution Restriction
You may distribute the Software in any medium with or without modification,
whether in source or executable forms, provided that you provide recipients
with a copy of this License and retain copyright, patent, trademark and
disclaimer statements in the Software.
5. Disclaimer of Warranty and Limitation of Liability
THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR
COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT
LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING
FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO
MATTER HOW ITS CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGES.
6. Language
THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION
AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF
DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION
SHALL PREVAIL.
END OF THE TERMS AND CONDITIONS
How to Apply the Mulan Permissive Software LicenseVersion 2
(Mulan PSL v2) to Your Software
To apply the Mulan PSL v2 to your work, for easy identification by
recipients, you are suggested to complete following three steps:
i. Fill in the blanks in following statement, including insert your software
name, the year of the first publication of your software, and your name
identified as the copyright owner;
ii. Create a file named "LICENSE" which contains the whole context of this
License in the first directory of your software package;
iii. Attach the statement to the appropriate annotated syntax at the
beginning of each source file.
Copyright (c) [Year] [name of copyright holder]
[Software Name] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.

View File

@@ -0,0 +1 @@
这是一个可以在RK3588上运行的yolov8-demo项目项目自带有量化后的官方模型可以进行测试使用。

View File

@@ -0,0 +1,720 @@
/****************************************************************************
*
* Copyright (c) 2017 - 2022 by Rockchip Corp. All rights reserved.
*
* The material in this file is confidential and contains trade secrets
* of Rockchip Corporation. This is proprietary information owned by
* Rockchip Corporation. No part of this work may be disclosed,
* reproduced, copied, transmitted, or used in any way for any purpose,
* without the express written permission of Rockchip Corporation.
*
*****************************************************************************/
#ifndef _RKNN_API_H
#define _RKNN_API_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/*
Definition of extended flag for rknn_init.
*/
/* set high priority context. */
#define RKNN_FLAG_PRIOR_HIGH 0x00000000
/* set medium priority context */
#define RKNN_FLAG_PRIOR_MEDIUM 0x00000001
/* set low priority context. */
#define RKNN_FLAG_PRIOR_LOW 0x00000002
/* asynchronous mode.
when enable, rknn_outputs_get will not block for too long because it directly retrieves the result of
the previous frame which can increase the frame rate on single-threaded mode, but at the cost of
rknn_outputs_get not retrieves the result of the current frame.
in multi-threaded mode you do not need to turn this mode on. */
#define RKNN_FLAG_ASYNC_MASK 0x00000004
/* collect performance mode.
when enable, you can get detailed performance reports via rknn_query(ctx, RKNN_QUERY_PERF_DETAIL, ...),
but it will reduce the frame rate. */
#define RKNN_FLAG_COLLECT_PERF_MASK 0x00000008
/* allocate all memory in outside, includes weight/internal/inputs/outputs */
#define RKNN_FLAG_MEM_ALLOC_OUTSIDE 0x00000010
/* weight sharing with the same network structure */
#define RKNN_FLAG_SHARE_WEIGHT_MEM 0x00000020
/* send fence fd from outside */
#define RKNN_FLAG_FENCE_IN_OUTSIDE 0x00000040
/* get fence fd from inside */
#define RKNN_FLAG_FENCE_OUT_OUTSIDE 0x00000080
/* dummy init flag: could only get total_weight_size and total_internal_size by rknn_query*/
#define RKNN_FLAG_COLLECT_MODEL_INFO_ONLY 0x00000100
/* set GPU as the preferred execution backend When the operator is not supported by the NPU */
#define RKNN_FLAG_EXECUTE_FALLBACK_PRIOR_DEVICE_GPU 0x00000400
/* allocate internal memory in outside */
#define RKNN_FLAG_INTERNAL_ALLOC_OUTSIDE 0x00000200
/*
Error code returned by the RKNN API.
*/
#define RKNN_SUCC 0 /* execute succeed. */
#define RKNN_ERR_FAIL -1 /* execute failed. */
#define RKNN_ERR_TIMEOUT -2 /* execute timeout. */
#define RKNN_ERR_DEVICE_UNAVAILABLE -3 /* device is unavailable. */
#define RKNN_ERR_MALLOC_FAIL -4 /* memory malloc fail. */
#define RKNN_ERR_PARAM_INVALID -5 /* parameter is invalid. */
#define RKNN_ERR_MODEL_INVALID -6 /* model is invalid. */
#define RKNN_ERR_CTX_INVALID -7 /* context is invalid. */
#define RKNN_ERR_INPUT_INVALID -8 /* input is invalid. */
#define RKNN_ERR_OUTPUT_INVALID -9 /* output is invalid. */
#define RKNN_ERR_DEVICE_UNMATCH -10 /* the device is unmatch, please update rknn sdk
and npu driver/firmware. */
#define RKNN_ERR_INCOMPATILE_PRE_COMPILE_MODEL -11 /* This RKNN model use pre_compile mode, but not compatible with current driver. */
#define RKNN_ERR_INCOMPATILE_OPTIMIZATION_LEVEL_VERSION -12 /* This RKNN model set optimization level, but not compatible with current driver. */
#define RKNN_ERR_TARGET_PLATFORM_UNMATCH -13 /* This RKNN model set target platform, but not compatible with current platform. */
/*
Definition for tensor
*/
#define RKNN_MAX_DIMS 16 /* maximum dimension of tensor. */
#define RKNN_MAX_NUM_CHANNEL 15 /* maximum channel number of input tensor. */
#define RKNN_MAX_NAME_LEN 256 /* maximum name lenth of tensor. */
#define RKNN_MAX_DYNAMIC_SHAPE_NUM 512 /* maximum number of dynamic shape for each input. */
#ifdef __arm__
typedef uint32_t rknn_context;
#else
typedef uint64_t rknn_context;
#endif
/*
The query command for rknn_query
*/
typedef enum _rknn_query_cmd {
RKNN_QUERY_IN_OUT_NUM = 0, /* query the number of input & output tensor. */
RKNN_QUERY_INPUT_ATTR = 1, /* query the attribute of input tensor. */
RKNN_QUERY_OUTPUT_ATTR = 2, /* query the attribute of output tensor. */
RKNN_QUERY_PERF_DETAIL = 3, /* query the detail performance, need set
RKNN_FLAG_COLLECT_PERF_MASK when call rknn_init,
this query needs to be valid after rknn_outputs_get. */
RKNN_QUERY_PERF_RUN = 4, /* query the time of run,
this query needs to be valid after rknn_outputs_get. */
RKNN_QUERY_SDK_VERSION = 5, /* query the sdk & driver version */
RKNN_QUERY_MEM_SIZE = 6, /* query the weight & internal memory size */
RKNN_QUERY_CUSTOM_STRING = 7, /* query the custom string */
RKNN_QUERY_NATIVE_INPUT_ATTR = 8, /* query the attribute of native input tensor. */
RKNN_QUERY_NATIVE_OUTPUT_ATTR = 9, /* query the attribute of native output tensor. */
RKNN_QUERY_NATIVE_NC1HWC2_INPUT_ATTR = 8, /* query the attribute of native input tensor. */
RKNN_QUERY_NATIVE_NC1HWC2_OUTPUT_ATTR = 9, /* query the attribute of native output tensor. */
RKNN_QUERY_NATIVE_NHWC_INPUT_ATTR = 10, /* query the attribute of native input tensor. */
RKNN_QUERY_NATIVE_NHWC_OUTPUT_ATTR = 11, /* query the attribute of native output tensor. */
RKNN_QUERY_DEVICE_MEM_INFO = 12, /* query the attribute of rknn memory information. */
RKNN_QUERY_INPUT_DYNAMIC_RANGE = 13, /* query the dynamic shape range of rknn input tensor. */
RKNN_QUERY_CURRENT_INPUT_ATTR = 14, /* query the current shape of rknn input tensor, only valid for dynamic rknn model*/
RKNN_QUERY_CURRENT_OUTPUT_ATTR = 15, /* query the current shape of rknn output tensor, only valid for dynamic rknn model*/
RKNN_QUERY_CURRENT_NATIVE_INPUT_ATTR = 16, /* query the current native shape of rknn input tensor, only valid for dynamic rknn model*/
RKNN_QUERY_CURRENT_NATIVE_OUTPUT_ATTR = 17, /* query the current native shape of rknn output tensor, only valid for dynamic rknn model*/
RKNN_QUERY_CMD_MAX
} rknn_query_cmd;
/*
the tensor data type.
*/
typedef enum _rknn_tensor_type {
RKNN_TENSOR_FLOAT32 = 0, /* data type is float32. */
RKNN_TENSOR_FLOAT16, /* data type is float16. */
RKNN_TENSOR_INT8, /* data type is int8. */
RKNN_TENSOR_UINT8, /* data type is uint8. */
RKNN_TENSOR_INT16, /* data type is int16. */
RKNN_TENSOR_UINT16, /* data type is uint16. */
RKNN_TENSOR_INT32, /* data type is int32. */
RKNN_TENSOR_UINT32, /* data type is uint32. */
RKNN_TENSOR_INT64, /* data type is int64. */
RKNN_TENSOR_BOOL,
RKNN_TENSOR_TYPE_MAX
} rknn_tensor_type;
inline static const char* get_type_string(rknn_tensor_type type)
{
switch(type) {
case RKNN_TENSOR_FLOAT32: return "FP32";
case RKNN_TENSOR_FLOAT16: return "FP16";
case RKNN_TENSOR_INT8: return "INT8";
case RKNN_TENSOR_UINT8: return "UINT8";
case RKNN_TENSOR_INT16: return "INT16";
case RKNN_TENSOR_UINT16: return "UINT16";
case RKNN_TENSOR_INT32: return "INT32";
case RKNN_TENSOR_UINT32: return "UINT32";
case RKNN_TENSOR_INT64: return "INT64";
case RKNN_TENSOR_BOOL: return "BOOL";
default: return "UNKNOW";
}
}
/*
the quantitative type.
*/
typedef enum _rknn_tensor_qnt_type {
RKNN_TENSOR_QNT_NONE = 0, /* none. */
RKNN_TENSOR_QNT_DFP, /* dynamic fixed point. */
RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC, /* asymmetric affine. */
RKNN_TENSOR_QNT_MAX
} rknn_tensor_qnt_type;
inline static const char* get_qnt_type_string(rknn_tensor_qnt_type type)
{
switch(type) {
case RKNN_TENSOR_QNT_NONE: return "NONE";
case RKNN_TENSOR_QNT_DFP: return "DFP";
case RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC: return "AFFINE";
default: return "UNKNOW";
}
}
/*
the tensor data format.
*/
typedef enum _rknn_tensor_format {
RKNN_TENSOR_NCHW = 0, /* data format is NCHW. */
RKNN_TENSOR_NHWC, /* data format is NHWC. */
RKNN_TENSOR_NC1HWC2, /* data format is NC1HWC2. */
RKNN_TENSOR_UNDEFINED,
RKNN_TENSOR_FORMAT_MAX
} rknn_tensor_format;
/*
the mode of running on target NPU core.
*/
typedef enum _rknn_core_mask {
RKNN_NPU_CORE_AUTO = 0, /* default, run on NPU core randomly. */
RKNN_NPU_CORE_0 = 1, /* run on NPU core 0. */
RKNN_NPU_CORE_1 = 2, /* run on NPU core 1. */
RKNN_NPU_CORE_2 = 4, /* run on NPU core 2. */
RKNN_NPU_CORE_0_1 = RKNN_NPU_CORE_0 | RKNN_NPU_CORE_1, /* run on NPU core 0 and core 1. */
RKNN_NPU_CORE_0_1_2 = RKNN_NPU_CORE_0_1 | RKNN_NPU_CORE_2, /* run on NPU core 0 and core 1 and core 2. */
RKNN_NPU_CORE_UNDEFINED,
} rknn_core_mask;
inline static const char* get_format_string(rknn_tensor_format fmt)
{
switch(fmt) {
case RKNN_TENSOR_NCHW: return "NCHW";
case RKNN_TENSOR_NHWC: return "NHWC";
case RKNN_TENSOR_NC1HWC2: return "NC1HWC2";
case RKNN_TENSOR_UNDEFINED: return "UNDEFINED";
default: return "UNKNOW";
}
}
/*
the information for RKNN_QUERY_IN_OUT_NUM.
*/
typedef struct _rknn_input_output_num {
uint32_t n_input; /* the number of input. */
uint32_t n_output; /* the number of output. */
} rknn_input_output_num;
/*
the information for RKNN_QUERY_INPUT_ATTR / RKNN_QUERY_OUTPUT_ATTR.
*/
typedef struct _rknn_tensor_attr {
uint32_t index; /* input parameter, the index of input/output tensor,
need set before call rknn_query. */
uint32_t n_dims; /* the number of dimensions. */
uint32_t dims[RKNN_MAX_DIMS]; /* the dimensions array. */
char name[RKNN_MAX_NAME_LEN]; /* the name of tensor. */
uint32_t n_elems; /* the number of elements. */
uint32_t size; /* the bytes size of tensor. */
rknn_tensor_format fmt; /* the data format of tensor. */
rknn_tensor_type type; /* the data type of tensor. */
rknn_tensor_qnt_type qnt_type; /* the quantitative type of tensor. */
int8_t fl; /* fractional length for RKNN_TENSOR_QNT_DFP. */
int32_t zp; /* zero point for RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC. */
float scale; /* scale for RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC. */
uint32_t w_stride; /* the stride of tensor along the width dimention of input,
Note: it is read-only, 0 means equal to width. */
uint32_t size_with_stride; /* the bytes size of tensor with stride. */
uint8_t pass_through; /* pass through mode, for rknn_set_io_mem interface.
if TRUE, the buf data is passed directly to the input node of the rknn model
without any conversion. the following variables do not need to be set.
if FALSE, the buf data is converted into an input consistent with the model
according to the following type and fmt. so the following variables
need to be set.*/
uint32_t h_stride; /* the stride along the height dimention of input,
Note: it is write-only, if it was set to 0, h_stride = height. */
} rknn_tensor_attr;
typedef struct _rknn_input_range {
uint32_t index; /* input parameter, the index of input/output tensor,
need set before call rknn_query. */
uint32_t shape_number; /* the number of shape. */
rknn_tensor_format fmt; /* the data format of tensor. */
char name[RKNN_MAX_NAME_LEN]; /* the name of tensor. */
uint32_t dyn_range[RKNN_MAX_DYNAMIC_SHAPE_NUM][RKNN_MAX_DIMS]; /* the dynamic input dimensions range. */
uint32_t n_dims; /* the number of dimensions. */
} rknn_input_range;
/*
the information for RKNN_QUERY_PERF_DETAIL.
*/
typedef struct _rknn_perf_detail {
char* perf_data; /* the string pointer of perf detail. don't need free it by user. */
uint64_t data_len; /* the string length. */
} rknn_perf_detail;
/*
the information for RKNN_QUERY_PERF_RUN.
*/
typedef struct _rknn_perf_run {
int64_t run_duration; /* real inference time (us) */
} rknn_perf_run;
/*
the information for RKNN_QUERY_SDK_VERSION.
*/
typedef struct _rknn_sdk_version {
char api_version[256]; /* the version of rknn api. */
char drv_version[256]; /* the version of rknn driver. */
} rknn_sdk_version;
/*
the information for RKNN_QUERY_MEM_SIZE.
*/
typedef struct _rknn_mem_size {
uint32_t total_weight_size; /* the weight memory size */
uint32_t total_internal_size; /* the internal memory size, exclude inputs/outputs */
uint64_t total_dma_allocated_size; /* total dma memory allocated size */
uint32_t total_sram_size; /* total system sram size reserved for rknn */
uint32_t free_sram_size; /* free system sram size reserved for rknn */
uint32_t reserved[10]; /* reserved */
} rknn_mem_size;
/*
the information for RKNN_QUERY_CUSTOM_STRING.
*/
typedef struct _rknn_custom_string {
char string[1024]; /* the string of custom, lengths max to 1024 bytes */
} rknn_custom_string;
/*
The flags of rknn_tensor_mem.
*/
typedef enum _rknn_tensor_mem_flags {
RKNN_TENSOR_MEMORY_FLAGS_ALLOC_INSIDE = 1, /*Used to mark in rknn_destroy_mem() whether it is necessary to release the "mem" pointer itself.
If the flag RKNN_TENSOR_MEMORY_FLAGS_ALLOC_INSIDE is set, rknn_destroy_mem() will call free(mem).*/
RKNN_TENSOR_MEMORY_FLAGS_FROM_FD = 2, /*Used to mark in rknn_create_mem_from_fd() whether it is necessary to release the "mem" pointer itself.
If the flag RKNN_TENSOR_MEMORY_FLAGS_FROM_FD is set, rknn_destroy_mem() will call free(mem).*/
RKNN_TENSOR_MEMORY_FLAGS_FROM_PHYS = 3, /*Used to mark in rknn_create_mem_from_phys() whether it is necessary to release the "mem" pointer itself.
If the flag RKNN_TENSOR_MEMORY_FLAGS_FROM_PHYS is set, rknn_destroy_mem() will call free(mem).*/
RKNN_TENSOR_MEMORY_FLAGS_UNKNOWN
} rknn_tensor_mem_flags;
/*
the memory information of tensor.
*/
typedef struct _rknn_tensor_memory {
void* virt_addr; /* the virtual address of tensor buffer. */
uint64_t phys_addr; /* the physical address of tensor buffer. */
int32_t fd; /* the fd of tensor buffer. */
int32_t offset; /* indicates the offset of the memory. */
uint32_t size; /* the size of tensor buffer. */
uint32_t flags; /* the flags of tensor buffer, reserved */
void * priv_data; /* the private data of tensor buffer. */
} rknn_tensor_mem;
/*
the input information for rknn_input_set.
*/
typedef struct _rknn_input {
uint32_t index; /* the input index. */
void* buf; /* the input buf for index. */
uint32_t size; /* the size of input buf. */
uint8_t pass_through; /* pass through mode.
if TRUE, the buf data is passed directly to the input node of the rknn model
without any conversion. the following variables do not need to be set.
if FALSE, the buf data is converted into an input consistent with the model
according to the following type and fmt. so the following variables
need to be set.*/
rknn_tensor_type type; /* the data type of input buf. */
rknn_tensor_format fmt; /* the data format of input buf.
currently the internal input format of NPU is NCHW by default.
so entering NCHW data can avoid the format conversion in the driver. */
} rknn_input;
/*
the output information for rknn_outputs_get.
*/
typedef struct _rknn_output {
uint8_t want_float; /* want transfer output data to float */
uint8_t is_prealloc; /* whether buf is pre-allocated.
if TRUE, the following variables need to be set.
if FALSE, the following variables do not need to be set. */
uint32_t index; /* the output index. */
void* buf; /* the output buf for index.
when is_prealloc = FALSE and rknn_outputs_release called,
this buf pointer will be free and don't use it anymore. */
uint32_t size; /* the size of output buf. */
} rknn_output;
/*
the extend information for rknn_init.
*/
typedef struct _rknn_init_extend {
rknn_context ctx; /* rknn context */
int32_t real_model_offset; /* real rknn model file offset, only valid when init context with rknn file path */
uint32_t real_model_size; /* real rknn model file size, only valid when init context with rknn file path */
uint8_t reserved[120]; /* reserved */
} rknn_init_extend;
/*
the extend information for rknn_run.
*/
typedef struct _rknn_run_extend {
uint64_t frame_id; /* output parameter, indicate current frame id of run. */
int32_t non_block; /* block flag of run, 0 is block else 1 is non block */
int32_t timeout_ms; /* timeout for block mode, in milliseconds */
int32_t fence_fd; /* fence fd from other unit */
} rknn_run_extend;
/*
the extend information for rknn_outputs_get.
*/
typedef struct _rknn_output_extend {
uint64_t frame_id; /* output parameter, indicate the frame id of outputs, corresponds to
struct rknn_run_extend.frame_id.*/
} rknn_output_extend;
/* rknn_init
initial the context and load the rknn model.
input:
rknn_context* context the pointer of context handle.
void* model if size > 0, pointer to the rknn model, if size = 0, filepath to the rknn model.
uint32_t size the size of rknn model.
uint32_t flag extend flag, see the define of RKNN_FLAG_XXX_XXX.
rknn_init_extend* extend the extend information of init.
return:
int error code.
*/
int rknn_init(rknn_context* context, void* model, uint32_t size, uint32_t flag, rknn_init_extend* extend);
/* rknn_dup_context
initial the context and load the rknn model.
input:
rknn_context* context_in the pointer of context in handle.
rknn_context* context_out the pointer of context out handle.
return:
int error code.
*/
int rknn_dup_context(rknn_context* context_in, rknn_context* context_out);
/* rknn_destroy
unload the rknn model and destroy the context.
input:
rknn_context context the handle of context.
return:
int error code.
*/
int rknn_destroy(rknn_context context);
/* rknn_query
query the information about model or others. see rknn_query_cmd.
input:
rknn_context context the handle of context.
rknn_query_cmd cmd the command of query.
void* info the buffer point of information.
uint32_t size the size of information.
return:
int error code.
*/
int rknn_query(rknn_context context, rknn_query_cmd cmd, void* info, uint32_t size);
/* rknn_inputs_set
set inputs information by input index of rknn model.
inputs information see rknn_input.
input:
rknn_context context the handle of context.
uint32_t n_inputs the number of inputs.
rknn_input inputs[] the arrays of inputs information, see rknn_input.
return:
int error code
*/
int rknn_inputs_set(rknn_context context, uint32_t n_inputs, rknn_input inputs[]);
/*
rknn_set_batch_core_num
set rknn batch core_num.
input:
rknn_context context the handle of context.
int core_num the core number.
return:
int error code.
*/
int rknn_set_batch_core_num(rknn_context context, int core_num);
/* rknn_set_core_mask
set rknn core mask.(only supported on RK3588 now)
RKNN_NPU_CORE_AUTO: auto mode, default value
RKNN_NPU_CORE_0: core 0 mode
RKNN_NPU_CORE_1: core 1 mode
RKNN_NPU_CORE_2: core 2 mode
RKNN_NPU_CORE_0_1: combine core 0/1 mode
RKNN_NPU_CORE_0_1_2: combine core 0/1/2 mode
input:
rknn_context context the handle of context.
rknn_core_mask core_mask the core mask.
return:
int error code.
*/
int rknn_set_core_mask(rknn_context context, rknn_core_mask core_mask);
/* rknn_run
run the model to execute inference.
input:
rknn_context context the handle of context.
rknn_run_extend* extend the extend information of run.
return:
int error code.
*/
int rknn_run(rknn_context context, rknn_run_extend* extend);
/* rknn_wait
wait the model after execute inference.
input:
rknn_context context the handle of context.
rknn_run_extend* extend the extend information of run.
return:
int error code.
*/
int rknn_wait(rknn_context context, rknn_run_extend* extend);
/* rknn_outputs_get
wait the inference to finish and get the outputs.
this function will block until inference finish.
the results will set to outputs[].
input:
rknn_context context the handle of context.
uint32_t n_outputs the number of outputs.
rknn_output outputs[] the arrays of output, see rknn_output.
rknn_output_extend* the extend information of output.
return:
int error code.
*/
int rknn_outputs_get(rknn_context context, uint32_t n_outputs, rknn_output outputs[], rknn_output_extend* extend);
/* rknn_outputs_release
release the outputs that get by rknn_outputs_get.
after called, the rknn_output[x].buf get from rknn_outputs_get will
also be free when rknn_output[x].is_prealloc = FALSE.
input:
rknn_context context the handle of context.
uint32_t n_ouputs the number of outputs.
rknn_output outputs[] the arrays of output.
return:
int error code
*/
int rknn_outputs_release(rknn_context context, uint32_t n_ouputs, rknn_output outputs[]);
/* new api for zero copy */
/* rknn_create_mem_from_phys (memory allocated outside)
initialize tensor memory from physical address.
input:
rknn_context ctx the handle of context.
uint64_t phys_addr physical address.
void *virt_addr virtual address.
uint32_t size the size of tensor buffer.
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem_from_phys(rknn_context ctx, uint64_t phys_addr, void *virt_addr, uint32_t size);
/* rknn_create_mem_from_fd (memory allocated outside)
initialize tensor memory from file description.
input:
rknn_context ctx the handle of context.
int32_t fd file description.
void *virt_addr virtual address.
uint32_t size the size of tensor buffer.
int32_t offset indicates the offset of the memory (virt_addr without offset).
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem_from_fd(rknn_context ctx, int32_t fd, void *virt_addr, uint32_t size, int32_t offset);
/* rknn_create_mem_from_mb_blk (memory allocated outside)
create tensor memory from mb_blk.
input:
rknn_context ctx the handle of context.
void *mb_blk mb_blk allocate from system api.
int32_t offset indicates the offset of the memory.
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem_from_mb_blk(rknn_context ctx, void *mb_blk, int32_t offset);
/* rknn_create_mem (memory allocated inside)
create tensor memory.
input:
rknn_context ctx the handle of context.
uint32_t size the size of tensor buffer.
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem(rknn_context ctx, uint32_t size);
/* rknn_destroy_mem (support allocate inside and outside)
destroy tensor memory.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the pointer of tensor memory information.
return:
int error code
*/
int rknn_destroy_mem(rknn_context ctx, rknn_tensor_mem *mem);
/* rknn_set_weight_mem
set the weight memory.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the array of tensor memory information
return:
int error code.
*/
int rknn_set_weight_mem(rknn_context ctx, rknn_tensor_mem *mem);
/* rknn_set_internal_mem
set the internal memory.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the array of tensor memory information
return:
int error code.
*/
int rknn_set_internal_mem(rknn_context ctx, rknn_tensor_mem *mem);
/* rknn_set_io_mem
set the input and output tensors buffer.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the array of tensor memory information.
rknn_tensor_attr *attr the attribute of input or output tensor buffer.
return:
int error code.
*/
int rknn_set_io_mem(rknn_context ctx, rknn_tensor_mem *mem, rknn_tensor_attr *attr);
/* rknn_set_input_shape(deprecated)
set the input tensor shape (only valid for dynamic shape rknn model).
input:
rknn_context ctx the handle of context.
rknn_tensor_attr *attr the attribute of input or output tensor buffer.
return:
int error code.
*/
int rknn_set_input_shape(rknn_context ctx, rknn_tensor_attr* attr);
/* rknn_set_input_shapes
set all the input tensor shapes. graph will run under current set of input shapes after rknn_set_input_shapes.(only valid for dynamic shape rknn model).
input:
rknn_context ctx the handle of context.
uint32_t n_inputs the number of inputs.
rknn_tensor_attr attr[] the attribute array of all input tensors.
return:
int error code.
*/
int rknn_set_input_shapes(rknn_context ctx, uint32_t n_inputs, rknn_tensor_attr attr[]);
#ifdef __cplusplus
} //extern "C"
#endif
#endif //_RKNN_API_H

View File

@@ -0,0 +1,261 @@
/****************************************************************************
*
* Copyright (c) 2017 - 2018 by Rockchip Corp. All rights reserved.
*
* The material in this file is confidential and contains trade secrets
* of Rockchip Corporation. This is proprietary information owned by
* Rockchip Corporation. No part of this work may be disclosed,
* reproduced, copied, transmitted, or used in any way for any purpose,
* without the express written permission of Rockchip Corporation.
*
*****************************************************************************/
#ifndef _RKNN_MATMUL_API_H
#define _RKNN_MATMUL_API_H
#ifdef __cplusplus
extern "C" {
#endif
#include "rknn_api.h"
typedef rknn_context rknn_matmul_ctx;
typedef struct _rknn_matmul_tensor_attr
{
char name[RKNN_MAX_NAME_LEN];
// indicate A(M, K) or B(K, N) or C(M, N)
uint32_t n_dims;
uint32_t dims[RKNN_MAX_DIMS];
// matmul tensor size
uint32_t size;
// matmul tensor data type
// int8 : A, B
// int32: C
rknn_tensor_type type;
} rknn_matmul_tensor_attr;
typedef struct _rknn_matmul_io_attr
{
// indicate A(M, K) or B(K, N) or C(M, N)
rknn_matmul_tensor_attr A;
rknn_matmul_tensor_attr B;
rknn_matmul_tensor_attr C;
} rknn_matmul_io_attr;
/*
matmul information struct
*/
typedef struct rknn_matmul_info_t
{
int32_t M;
int32_t K; // limit: rk356x: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte;
// rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 32byte;
int32_t N; // limit: rk356x: int8 type must be aligned with 16byte, float16 type must be aligned with 8byte;
// rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte;
// matmul data type
// int8: int8(A) x int8(B) -> int32(C)
// float16: float16(A) x float16(B) -> float32(C)
rknn_tensor_type type;
// matmul native layout for B
// 0: normal layout
// 1: native layout
int32_t native_layout;
// matmul perf layout for A and C
// 0: normal layout
// 1: perf layout
int32_t perf_layout;
} rknn_matmul_info;
/* rknn_matmul_create
params:
rknn_matmul_ctx *ctx the handle of context.
rknn_matmul_info *info the matmal information.
rknn_matmul_io_attr *io_attr inputs/output attribute
return:
int error code
*/
int rknn_matmul_create(rknn_matmul_ctx* ctx, rknn_matmul_info* info, rknn_matmul_io_attr* io_attr);
/* rknn_matmul_set_io_mem
params:
rknn_matmul_ctx ctx the handle of context.
rknn_tensor_mem *mem the pointer of tensor memory information.
rknn_matmul_tensor_attr *attr the attribute of input or output tensor buffer.
return:
int error code.
formula:
C = A * B,
limit:
K <= 4096
K limit: rk356x: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte;
rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 32byte;
N limit: rk356x: int8 type must be aligned with 16byte, float16 type must be aligned with 8byte;
rk3588: int8 type must be aligned with 32byte, float16 type must be aligned with 16byte;
A shape: M x K
normal layout: (M, K)
[M1K1, M1K2, ..., M1Kk,
M2K1, M2K2, ..., M2Kk,
...
MmK1, MmK2, ..., MmKk]
for rk356x
int8:
perf layout: (K / 8, M, 8)
[K1M1, K2M1, ..., K8M1,
K9M2, K10M2, ..., K16M2,
...
K(k-7)Mm, K(k-6)Mm, ..., KkMm]
float16:
perf layout: (K / 4, M, 4)
[K1M1, K2M1, ..., K4M1,
K9M2, K10M2, ..., K8M2,
...
K(k-3)Mm, K(k-2)Mm, ..., KkMm]
for rk3588
int8:
perf layout: (K / 16, M, 16)
[K1M1, K2M1, ..., K16M1,
K9M2, K10M2, ..., K32M2,
...
K(k-15)Mm, K(k-14)Mm, ..., KkMm]
float16:
perf layout: (K / 8, M, 8)
[K1M1, K2M1, ..., K8M1,
K9M2, K10M2, ..., K16M2,
...
K(k-7)Mm, K(k-6)Mm, ..., KkMm]
B shape: K x N
normal layout: (K, N)
[K1N1, K1N2, ..., K1Nn,
K2N1, K2N2, ..., K2Nn,
...
KkN1, KkN2, ..., KkNn]
for rk356x
int8:
native layout: (N / 16, K / 32, 16, 32)
[K1N1, K2N1, ..., K32N1,
K1N2, K2N2, ..., K32N2,
...
K1N16, K2N16, ..., K32N16,
K33N1, K34N1, ..., K64N1,
K33N2, K34N2, ..., K64N2,
...
K(k-31)N16, K(k-30)N16, ..., KkN16,
K1N17, K2N17, ..., K32N17,
K1N18, K2N18, ..., K32N18,
...
K(k-31)Nn, K(k-30)Nn, ..., KkNn]
float16:
native layout: (N / 8, K / 16, 8, 16)
[K1N1, K2N1, ..., K16N1,
K1N2, K2N2, ..., K16N2,
...
K1N8, K2N8, ..., K16N8,
K17N1, K18N1, ..., K32N1,
K17N2, K18N2, ..., K32N2,
...
K(k-15)N8, K(k-30)N8, ..., KkN8,
K1N9, K2N9, ..., K16N9,
K1N10, K2N10, ..., K16N10,
...
K(k-15)Nn, K(k-14)Nn, ..., KkNn]
for rk3588
int8:
native layout: (N / 32, K / 32, 32, 32)
[K1N1, K2N1, ..., K32N1,
K1N2, K2N2, ..., K32N2,
...
K1N32, K2N32, ..., K32N32,
K33N1, K34N1, ..., K64N1,
K33N2, K34N2, ..., K64N2,
...
K(k-31)N32, K(k-30)N32, ..., KkN32,
K1N33, K2N33, ..., K32N33,
K1N34, K2N34, ..., K32N34,
...
K(k-31)Nn, K(k-30)Nn, ..., KkNn]
float16:
native layout: (N / 16, K / 32, 16, 32)
[K1N1, K2N1, ..., K32N1,
K1N2, K2N2, ..., K32N2,
...
K1N16, K2N16, ..., K32N16,
K33N1, K34N1, ..., K64N1,
K33N2, K34N2, ..., K64N2,
...
K(k-31)N16, K(k-30)N16, ..., KkN16,
K1N17, K2N17, ..., K32N17,
K1N18, K2N18, ..., K32N18,
...
K(k-31)Nn, K(k-30)Nn, ..., KkNn]
C shape: M x N
normal layout: (M, N)
[M1N1, M1N2, ..., M1Nn,
M2N1, M2N2, ..., M2Nn,
...
MmN1, MmN2, ..., MmNn]
perf layout: (N / 4, M, 4)
[N1M1, N2M1, ..., N4M1,
N5M2, N6M2, ..., N8M2,
...
N(n-3)Mm, N(n-2)Mm, ..., NnMm]
*/
int rknn_matmul_set_io_mem(rknn_matmul_ctx ctx, rknn_tensor_mem* mem, rknn_matmul_tensor_attr* attr);
/* rknn_matmul_set_core_mask
set rknn core mask.(only support rk3588 in current)
RKNN_NPU_CORE_AUTO: auto mode, default value
RKNN_NPU_CORE_0: core 0 mode
RKNN_NPU_CORE_1: core 1 mode
RKNN_NPU_CORE_2: core 2 mode
RKNN_NPU_CORE_0_1: combine core 0/1 mode
RKNN_NPU_CORE_0_1_2: combine core 0/1/2 mode
input:
rknn_matmul_ctx context the handle of context.
rknn_core_mask core_mask the core mask.
return:
int error code.
*/
int rknn_matmul_set_core_mask(rknn_matmul_ctx context, rknn_core_mask core_mask);
/* rknn_matmul_run
run the matmul in blocking mode
params:
rknn_matmul_ctx ctx the handle of context.
return:
int error code.
*/
int rknn_matmul_run(rknn_matmul_ctx ctx);
/* rknn_matmul_destroy
destroy the matmul context
params:
rknn_matmul_ctx ctx the handle of context.
return:
int error code.
*/
int rknn_matmul_destroy(rknn_matmul_ctx ctx);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // _RKNN_MATMUL_API_H

View File

@@ -0,0 +1,212 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_COMMON_H
#define MK_COMMON_H
#include <stdint.h>
#include <stddef.h>
#if defined(GENERATE_EXPORT)
#include "mk_export.h"
#endif
#if defined(_WIN32) && defined(_MSC_VER)
# define API_CALL __cdecl
#else
# define API_CALL
#endif
#if defined(_WIN32) && defined(_MSC_VER)
# if !defined(GENERATE_EXPORT)
# if defined(MediaKitApi_EXPORTS)
# define API_EXPORT __declspec(dllexport)
# else
# define API_EXPORT __declspec(dllimport)
# endif
# endif
#elif !defined(GENERATE_EXPORT)
# define API_EXPORT __attribute__((visibility("default")))
#endif
#ifdef __cplusplus
extern "C" {
#endif
//输出日志到shell
#define LOG_CONSOLE (1 << 0)
//输出日志到文件
#define LOG_FILE (1 << 1)
//输出日志到回调函数(mk_events::on_mk_log)
#define LOG_CALLBACK (1 << 2)
//回调user_data回调函数
typedef void(API_CALL *on_user_data_free)(void *user_data);
typedef struct {
// 线程数
int thread_num;
// 日志级别,支持0~4
int log_level;
//控制日志输出的掩模请查看LOG_CONSOLE、LOG_FILE、LOG_CALLBACK等宏
int log_mask;
//文件日志保存路径,路径可以不存在(内部可以创建文件夹)设置为NULL关闭日志输出至文件
const char *log_file_path;
//文件日志保存天数,设置为0关闭日志文件
int log_file_days;
// 配置文件是内容还是路径
int ini_is_path;
// 配置文件内容或路径可以为NULL,如果该文件不存在,那么将导出默认配置至该文件
const char *ini;
// ssl证书是内容还是路径
int ssl_is_path;
// ssl证书内容或路径可以为NULL
const char *ssl;
// 证书密码可以为NULL
const char *ssl_pwd;
} mk_config;
/**
* 初始化环境,调用该库前需要先调用此函数
* @param cfg 库运行相关参数
*/
API_EXPORT void API_CALL mk_env_init(const mk_config *cfg);
/**
* 关闭所有服务器请在main函数退出时调用
*/
API_EXPORT void API_CALL mk_stop_all_server();
/**
* 基础类型参数版本的mk_env_init为了方便其他语言调用
* @param thread_num 线程数
* @param log_level 日志级别,支持0~4
* @param log_mask 日志输出方式掩模请查看LOG_CONSOLE、LOG_FILE、LOG_CALLBACK等宏
* @param log_file_path 文件日志保存路径,路径可以不存在(内部可以创建文件夹)设置为NULL关闭日志输出至文件
* @param log_file_days 文件日志保存天数,设置为0关闭日志文件
* @param ini_is_path 配置文件是内容还是路径
* @param ini 配置文件内容或路径可以为NULL,如果该文件不存在,那么将导出默认配置至该文件
* @param ssl_is_path ssl证书是内容还是路径
* @param ssl ssl证书内容或路径可以为NULL
* @param ssl_pwd 证书密码可以为NULL
*/
API_EXPORT void API_CALL mk_env_init1(int thread_num,
int log_level,
int log_mask,
const char *log_file_path,
int log_file_days,
int ini_is_path,
const char *ini,
int ssl_is_path,
const char *ssl,
const char *ssl_pwd);
/**
* 设置日志文件
* @param file_max_size 单个切片文件大小(MB)
* @param file_max_count 切片文件个数
*/
API_EXPORT void API_CALL mk_set_log(int file_max_size, int file_max_count);
/**
* 设置配置项
* @deprecated 请使用mk_ini_set_option替代
* @param key 配置项名
* @param val 配置项值
*/
API_EXPORT void API_CALL mk_set_option(const char *key, const char *val);
/**
* 获取配置项的值
* @deprecated 请使用mk_ini_get_option替代
* @param key 配置项名
*/
API_EXPORT const char * API_CALL mk_get_option(const char *key);
/**
* 创建http[s]服务器
* @param port htt监听端口推荐80传入0则随机分配
* @param ssl 是否为ssl类型服务器
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_http_server_start(uint16_t port, int ssl);
/**
* 创建rtsp[s]服务器
* @param port rtsp监听端口推荐554传入0则随机分配
* @param ssl 是否为ssl类型服务器
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_rtsp_server_start(uint16_t port, int ssl);
/**
* 创建rtmp[s]服务器
* @param port rtmp监听端口推荐1935传入0则随机分配
* @param ssl 是否为ssl类型服务器
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_rtmp_server_start(uint16_t port, int ssl);
/**
* 创建rtp服务器
* @param port rtp监听端口(包括udp/tcp)
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_rtp_server_start(uint16_t port);
/**
* 创建rtc服务器
* @param port rtc监听端口
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_rtc_server_start(uint16_t port);
//获取webrtc answer sdp回调函数
typedef void(API_CALL *on_mk_webrtc_get_answer_sdp)(void *user_data, const char *answer, const char *err);
/**
* webrtc交换sdp根据offer sdp生成answer sdp
* @param user_data 回调用户指针
* @param cb 回调函数
* @param type webrtc插件类型支持echo,play,push
* @param offer webrtc offer sdp
* @param url rtc url, 例如 rtc://__defaultVhost/app/stream?key1=val1&key2=val2
*/
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url);
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url);
/**
* 创建srt服务器
* @param port srt监听端口
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_srt_server_start(uint16_t port);
/**
* 创建shell服务器
* @param port shell监听端口
* @return 0:失败,非0:端口号
*/
API_EXPORT uint16_t API_CALL mk_shell_server_start(uint16_t port);
#ifdef __cplusplus
}
#endif
#endif /* MK_COMMON_H */

View File

@@ -0,0 +1,193 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_EVENTS_H
#define MK_EVENTS_H
#include "mk_common.h"
#include "mk_events_objects.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
/**
* 注册或反注册MediaSource事件广播
* @param regist 注册为1注销为0
* @param sender 该MediaSource对象
*/
void (API_CALL *on_mk_media_changed)(int regist,
const mk_media_source sender);
/**
* 收到rtsp/rtmp推流事件广播通过该事件控制推流鉴权
* @see mk_publish_auth_invoker_do
* @param url_info 推流url相关信息
* @param invoker 执行invoker返回鉴权结果
* @param sender 该tcp客户端相关信息
*/
void (API_CALL *on_mk_media_publish)(const mk_media_info url_info,
const mk_publish_auth_invoker invoker,
const mk_sock_info sender);
/**
* 播放rtsp/rtmp/http-flv/hls事件广播通过该事件控制播放鉴权
* @see mk_auth_invoker_do
* @param url_info 播放url相关信息
* @param invoker 执行invoker返回鉴权结果
* @param sender 播放客户端相关信息
*/
void (API_CALL *on_mk_media_play)(const mk_media_info url_info,
const mk_auth_invoker invoker,
const mk_sock_info sender);
/**
* 未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了
* @param url_info 播放url相关信息
* @param sender 播放客户端相关信息
* @return 1 直接关闭
* 0 等待流注册
*/
int (API_CALL *on_mk_media_not_found)(const mk_media_info url_info,
const mk_sock_info sender);
/**
* 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑
* @param sender 该MediaSource对象
*/
void (API_CALL *on_mk_media_no_reader)(const mk_media_source sender);
/**
* 收到http api请求广播(包括GET/POST)
* @param parser http请求内容对象
* @param invoker 执行该invoker返回http回复
* @param consumed 置1则说明我们要处理该事件
* @param sender http客户端相关信息
*/
void (API_CALL *on_mk_http_request)(const mk_parser parser,
const mk_http_response_invoker invoker,
int *consumed,
const mk_sock_info sender);
/**
* 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限
* @param parser http请求内容对象
* @param path 文件绝对路径
* @param is_dir path是否为文件夹
* @param invoker 执行invoker返回本次访问文件的结果
* @param sender http客户端相关信息
*/
void (API_CALL *on_mk_http_access)(const mk_parser parser,
const char *path,
int is_dir,
const mk_http_access_path_invoker invoker,
const mk_sock_info sender);
/**
* 在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射
* 在该事件中通过自行覆盖path参数可以做到譬如根据虚拟主机或者app选择不同http根目录的目的
* @param parser http请求内容对象
* @param path 文件绝对路径,覆盖之可以重定向到其他文件
* @param sender http客户端相关信息
*/
void (API_CALL *on_mk_http_before_access)(const mk_parser parser,
char *path,
const mk_sock_info sender);
/**
* 该rtsp流是否需要认证是的话调用invoker并传入realm,否则传入空的realm
* @param url_info 请求rtsp url相关信息
* @param invoker 执行invoker返回是否需要rtsp专属认证
* @param sender rtsp客户端相关信息
*/
void (API_CALL *on_mk_rtsp_get_realm)(const mk_media_info url_info,
const mk_rtsp_get_realm_invoker invoker,
const mk_sock_info sender);
/**
* 请求认证用户密码事件user_name为用户名must_no_encrypt如果为true则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败
* 获取到密码后请调用invoker并输入对应类型的密码和密码类型invoker执行时会匹配密码
* @param url_info 请求rtsp url相关信息
* @param realm rtsp认证realm
* @param user_name rtsp认证用户名
* @param must_no_encrypt 如果为true则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败
* @param invoker 执行invoker返回rtsp专属认证的密码
* @param sender rtsp客户端信息
*/
void (API_CALL *on_mk_rtsp_auth)(const mk_media_info url_info,
const char *realm,
const char *user_name,
int must_no_encrypt,
const mk_rtsp_auth_invoker invoker,
const mk_sock_info sender);
/**
* 录制mp4分片文件成功后广播
*/
void (API_CALL *on_mk_record_mp4)(const mk_mp4_info mp4);
/**
* shell登录鉴权
*/
void (API_CALL *on_mk_shell_login)(const char *user_name,
const char *passwd,
const mk_auth_invoker invoker,
const mk_sock_info sender);
/**
* 停止rtsp/rtmp/http-flv会话后流量汇报事件广播
* @param url_info 播放url相关信息
* @param total_bytes 耗费上下行总流量,单位字节数
* @param total_seconds 本次tcp会话时长单位秒
* @param is_player 客户端是否为播放器
*/
void (API_CALL *on_mk_flow_report)(const mk_media_info url_info,
size_t total_bytes,
size_t total_seconds,
int is_player,
const mk_sock_info sender);
/**
* 日志输出广播
* @param level 日志级别
* @param file 源文件名
* @param line 源文件行
* @param function 源文件函数名
* @param message 日志内容
*/
void (API_CALL *on_mk_log)(int level, const char *file, int line, const char *function, const char *message);
/**
* 发送rtp流失败回调适用于mk_media_source_start_send_rtp/mk_media_start_send_rtp接口触发的rtp发送
* @param vhost 虚拟主机
* @param app 应用名
* @param stream 流id
* @param ssrc ssrc的10进制打印通过atoi转换为整型
* @param err 错误代码
* @param msg 错误提示
*/
void(API_CALL *on_mk_media_send_rtp_stop)(const char *vhost, const char *app, const char *stream, const char *ssrc, int err, const char *msg);
} mk_events;
/**
* 监听ZLMediaKit里面的事件
* @param events 各个事件的结构体,这个对象在内部会再拷贝一次可以设置为null以便取消监听
*/
API_EXPORT void API_CALL mk_events_listen(const mk_events *events);
#ifdef __cplusplus
}
#endif
#endif //MK_EVENTS_H

View File

@@ -0,0 +1,355 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_EVENT_OBJECTS_H
#define MK_EVENT_OBJECTS_H
#include "mk_common.h"
#include "mk_tcp.h"
#include "mk_track.h"
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////MP4Info/////////////////////////////////////////////
//MP4Info对象的C映射
typedef struct mk_mp4_info_t *mk_mp4_info;
// GMT 标准时间,单位秒
API_EXPORT uint64_t API_CALL mk_mp4_info_get_start_time(const mk_mp4_info ctx);
// 录像长度,单位秒
API_EXPORT float API_CALL mk_mp4_info_get_time_len(const mk_mp4_info ctx);
// 文件大小,单位 BYTE
API_EXPORT size_t API_CALL mk_mp4_info_get_file_size(const mk_mp4_info ctx);
// 文件路径
API_EXPORT const char* API_CALL mk_mp4_info_get_file_path(const mk_mp4_info ctx);
// 文件名称
API_EXPORT const char* API_CALL mk_mp4_info_get_file_name(const mk_mp4_info ctx);
// 文件夹路径
API_EXPORT const char* API_CALL mk_mp4_info_get_folder(const mk_mp4_info ctx);
// 播放路径
API_EXPORT const char* API_CALL mk_mp4_info_get_url(const mk_mp4_info ctx);
// 应用名称
API_EXPORT const char* API_CALL mk_mp4_info_get_vhost(const mk_mp4_info ctx);
// 流 ID
API_EXPORT const char* API_CALL mk_mp4_info_get_app(const mk_mp4_info ctx);
// 虚拟主机
API_EXPORT const char* API_CALL mk_mp4_info_get_stream(const mk_mp4_info ctx);
///////////////////////////////////////////Parser/////////////////////////////////////////////
//Parser对象的C映射
typedef struct mk_parser_t *mk_parser;
//Parser::Method(),获取命令字譬如GET/POST
API_EXPORT const char* API_CALL mk_parser_get_method(const mk_parser ctx);
//Parser::Url(),获取HTTP的访问url(不包括?后面的参数)
API_EXPORT const char* API_CALL mk_parser_get_url(const mk_parser ctx);
//Parser::Params(),?后面的参数字符串
API_EXPORT const char* API_CALL mk_parser_get_url_params(const mk_parser ctx);
//Parser::getUrlArgs()["key"],获取?后面的参数中的特定参数
API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,const char *key);
//Parser::Tail(),获取协议相关信息,譬如 HTTP/1.1
API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx);
//Parser::getValues()["key"],获取HTTP头中特定字段
API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key);
//Parser::Content(),获取HTTP body
API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_t *length);
///////////////////////////////////////////MediaInfo/////////////////////////////////////////////
//MediaInfo对象的C映射
typedef struct mk_media_info_t *mk_media_info;
//MediaInfo::param_strs
API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx);
//MediaInfo::schema
API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx);
//MediaInfo::vhost
API_EXPORT const char* API_CALL mk_media_info_get_vhost(const mk_media_info ctx);
//MediaInfo::app
API_EXPORT const char* API_CALL mk_media_info_get_app(const mk_media_info ctx);
//MediaInfo::stream
API_EXPORT const char* API_CALL mk_media_info_get_stream(const mk_media_info ctx);
//MediaInfo::host
API_EXPORT const char* API_CALL mk_media_info_get_host(const mk_media_info ctx);
//MediaInfo::port
API_EXPORT uint16_t API_CALL mk_media_info_get_port(const mk_media_info ctx);
///////////////////////////////////////////MediaSource/////////////////////////////////////////////
//MediaSource对象的C映射
typedef struct mk_media_source_t *mk_media_source;
//查找MediaSource的回调函数
typedef void(API_CALL *on_mk_media_source_find_cb)(void *user_data, const mk_media_source ctx);
//MediaSource::getSchema()
API_EXPORT const char* API_CALL mk_media_source_get_schema(const mk_media_source ctx);
//MediaSource::getVhost()
API_EXPORT const char* API_CALL mk_media_source_get_vhost(const mk_media_source ctx);
//MediaSource::getApp()
API_EXPORT const char* API_CALL mk_media_source_get_app(const mk_media_source ctx);
//MediaSource::getId()
API_EXPORT const char* API_CALL mk_media_source_get_stream(const mk_media_source ctx);
//MediaSource::readerCount()
API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source ctx);
//MediaSource::totalReaderCount()
API_EXPORT int API_CALL mk_media_source_get_total_reader_count(const mk_media_source ctx);
// get track count from MediaSource
API_EXPORT int API_CALL mk_media_source_get_track_count(const mk_media_source ctx);
// copy track reference by index from MediaSource, please use mk_track_unref to release it
API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx, int index);
// MediaSource::broadcastMessage
API_EXPORT int API_CALL mk_media_source_broadcast_msg(const mk_media_source ctx, const char *msg, size_t len);
/**
* 直播源在ZLMediaKit中被称作为MediaSource
* 目前支持3种分别是RtmpMediaSource、RtspMediaSource、HlsMediaSource
* 源的产生有被动和主动方式:
* 被动方式分别是rtsp/rtmp/rtp推流、mp4点播
* 主动方式包括mk_media_create创建的对象(DevChannel)、mk_proxy_player_create创建的对象(PlayerProxy)
* 被动方式你不用做任何处理ZLMediaKit已经默认适配了MediaSource::close()事件,都会关闭直播流
* 主动方式你要设置这个事件的回调,你要自己选择删除对象
* 通过mk_proxy_player_set_on_close、mk_media_set_on_close函数可以设置回调,
* 请在回调中删除对象来完成媒体的关闭否则又为什么要调用mk_media_source_close函数
* @param ctx 对象
* @param force 是否强制关闭,如果强制关闭,在有人观看的情况下也会关闭
* @return 0代表失败1代表成功
*/
API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int force);
//MediaSource::seekTo()
API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32_t stamp);
/**
* rtp推流成功与否的回调(第一次成功后,后面将一直重试)
*/
typedef void(API_CALL *on_mk_media_source_send_rtp_result)(void *user_data, uint16_t local_port, int err, const char *msg);
//MediaSource::startSendRtp,请参考mk_media_start_send_rtp,注意ctx参数类型不一样
API_EXPORT void API_CALL mk_media_source_start_send_rtp(const mk_media_source ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_source_send_rtp_result cb, void *user_data);
API_EXPORT void API_CALL mk_media_source_start_send_rtp2(const mk_media_source ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_source_send_rtp_result cb, void *user_data, on_user_data_free user_data_free);
//MediaSource::stopSendRtp请参考mk_media_stop_send_rtp,注意ctx参数类型不一样
API_EXPORT int API_CALL mk_media_source_stop_send_rtp(const mk_media_source ctx);
//MediaSource::find()
API_EXPORT void API_CALL mk_media_source_find(const char *schema,
const char *vhost,
const char *app,
const char *stream,
int from_mp4,
void *user_data,
on_mk_media_source_find_cb cb);
API_EXPORT const mk_media_source API_CALL mk_media_source_find2(const char *schema,
const char *vhost,
const char *app,
const char *stream,
int from_mp4);
//MediaSource::for_each_media()
API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb, const char *schema,
const char *vhost, const char *app, const char *stream);
///////////////////////////////////////////HttpBody/////////////////////////////////////////////
//HttpBody对象的C映射
typedef struct mk_http_body_t *mk_http_body;
/**
* 生成HttpStringBody
* @param str 字符串指针
* @param len 字符串长度为0则用strlen获取
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_string(const char *str,size_t len);
/**
* 生成HttpBufferBody
* @param buffer mk_buffer对象
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_buffer(mk_buffer buffer);
/**
* 生成HttpFileBody
* @param file_path 文件完整路径
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_file(const char *file_path);
/**
* 生成HttpMultiFormBody
* @param key_val 参数key-value
* @param file_path 文件完整路径
*/
API_EXPORT mk_http_body API_CALL mk_http_body_from_multi_form(const char *key_val[],const char *file_path);
/**
* 销毁HttpBody
*/
API_EXPORT void API_CALL mk_http_body_release(mk_http_body ctx);
///////////////////////////////////////////HttpResponseInvoker/////////////////////////////////////////////
//HttpSession::HttpResponseInvoker对象的C映射
typedef struct mk_http_response_invoker_t *mk_http_response_invoker;
/**
* HttpSession::HttpResponseInvoker(const string &codeOut, const StrCaseMap &headerOut, const HttpBody::Ptr &body);
* @param response_code 譬如200
* @param response_header 返回的http头譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾
* @param response_body body对象
*/
API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx,
int response_code,
const char **response_header,
const mk_http_body response_body);
/**
* HttpSession::HttpResponseInvoker(const string &codeOut, const StrCaseMap &headerOut, const string &body);
* @param response_code 譬如200
* @param response_header 返回的http头譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾
* @param response_content 返回的content部分譬如一个网页内容
*/
API_EXPORT void API_CALL mk_http_response_invoker_do_string(const mk_http_response_invoker ctx,
int response_code,
const char **response_header,
const char *response_content);
/**
* HttpSession::HttpResponseInvoker(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const string &filePath);
* @param request_parser 请求事件中的mk_parser对象用于提取其中http头中的Range字段通过该字段先fseek然后再发送文件部分片段
* @param response_header 返回的http头譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾
* @param response_file_path 返回的content部分譬如/path/to/html/file
*/
API_EXPORT void API_CALL mk_http_response_invoker_do_file(const mk_http_response_invoker ctx,
const mk_parser request_parser,
const char *response_header[],
const char *response_file_path);
/**
* 克隆mk_http_response_invoker对象通过克隆对象为堆对象可以实现跨线程异步执行mk_http_response_invoker_do
* 如果是同步执行mk_http_response_invoker_do那么没必要克隆对象
*/
API_EXPORT mk_http_response_invoker API_CALL mk_http_response_invoker_clone(const mk_http_response_invoker ctx);
/**
* 销毁堆上的克隆对象
*/
API_EXPORT void API_CALL mk_http_response_invoker_clone_release(const mk_http_response_invoker ctx);
///////////////////////////////////////////HttpAccessPathInvoker/////////////////////////////////////////////
//HttpSession::HttpAccessPathInvoker对象的C映射
typedef struct mk_http_access_path_invoker_t *mk_http_access_path_invoker;
/**
* HttpSession::HttpAccessPathInvoker(const string &errMsg,const string &accessPath, int cookieLifeSecond);
* @param err_msg 如果为空,则代表鉴权通过,否则为错误提示,可以为null
* @param access_path 运行或禁止访问的根目录,可以为null
* @param cookie_life_second 鉴权cookie有效期
**/
API_EXPORT void API_CALL mk_http_access_path_invoker_do(const mk_http_access_path_invoker ctx,
const char *err_msg,
const char *access_path,
int cookie_life_second);
/**
* 克隆mk_http_access_path_invoker对象通过克隆对象为堆对象可以实现跨线程异步执行mk_http_access_path_invoker_do
* 如果是同步执行mk_http_access_path_invoker_do那么没必要克隆对象
*/
API_EXPORT mk_http_access_path_invoker API_CALL mk_http_access_path_invoker_clone(const mk_http_access_path_invoker ctx);
/**
* 销毁堆上的克隆对象
*/
API_EXPORT void API_CALL mk_http_access_path_invoker_clone_release(const mk_http_access_path_invoker ctx);
///////////////////////////////////////////RtspSession::onGetRealm/////////////////////////////////////////////
//RtspSession::onGetRealm对象的C映射
typedef struct mk_rtsp_get_realm_invoker_t *mk_rtsp_get_realm_invoker;
/**
* 执行RtspSession::onGetRealm
* @param realm 该rtsp流是否需要开启rtsp专属鉴权至null或空字符串则不鉴权
*/
API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_do(const mk_rtsp_get_realm_invoker ctx,
const char *realm);
/**
* 克隆mk_rtsp_get_realm_invoker对象通过克隆对象为堆对象可以实现跨线程异步执行mk_rtsp_get_realm_invoker_do
* 如果是同步执行mk_rtsp_get_realm_invoker_do那么没必要克隆对象
*/
API_EXPORT mk_rtsp_get_realm_invoker API_CALL mk_rtsp_get_realm_invoker_clone(const mk_rtsp_get_realm_invoker ctx);
/**
* 销毁堆上的克隆对象
*/
API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_clone_release(const mk_rtsp_get_realm_invoker ctx);
///////////////////////////////////////////RtspSession::onAuth/////////////////////////////////////////////
//RtspSession::onAuth对象的C映射
typedef struct mk_rtsp_auth_invoker_t *mk_rtsp_auth_invoker;
/**
* 执行RtspSession::onAuth
* @param encrypted 为true是则表明是md5加密的密码否则是明文密码, 在请求明文密码时如果提供md5密码者则会导致认证失败
* @param pwd_or_md5 明文密码或者md5加密的密码
*/
API_EXPORT void API_CALL mk_rtsp_auth_invoker_do(const mk_rtsp_auth_invoker ctx,
int encrypted,
const char *pwd_or_md5);
/**
* 克隆mk_rtsp_auth_invoker对象通过克隆对象为堆对象可以实现跨线程异步执行mk_rtsp_auth_invoker_do
* 如果是同步执行mk_rtsp_auth_invoker_do那么没必要克隆对象
*/
API_EXPORT mk_rtsp_auth_invoker API_CALL mk_rtsp_auth_invoker_clone(const mk_rtsp_auth_invoker ctx);
/**
* 销毁堆上的克隆对象
*/
API_EXPORT void API_CALL mk_rtsp_auth_invoker_clone_release(const mk_rtsp_auth_invoker ctx);
///////////////////////////////////////////Broadcast::PublishAuthInvoker/////////////////////////////////////////////
//Broadcast::PublishAuthInvoker对象的C映射
typedef struct mk_publish_auth_invoker_t *mk_publish_auth_invoker;
/**
* 执行Broadcast::PublishAuthInvoker
* @param err_msg 为空或null则代表鉴权成功
* @param enable_hls 是否允许转换hls
* @param enable_mp4 是否运行MP4录制
*/
API_EXPORT void API_CALL mk_publish_auth_invoker_do(const mk_publish_auth_invoker ctx,
const char *err_msg,
int enable_hls,
int enable_mp4);
/**
* 克隆mk_publish_auth_invoker对象通过克隆对象为堆对象可以实现跨线程异步执行mk_publish_auth_invoker_do
* 如果是同步执行mk_publish_auth_invoker_do那么没必要克隆对象
*/
API_EXPORT mk_publish_auth_invoker API_CALL mk_publish_auth_invoker_clone(const mk_publish_auth_invoker ctx);
/**
* 销毁堆上的克隆对象
*/
API_EXPORT void API_CALL mk_publish_auth_invoker_clone_release(const mk_publish_auth_invoker ctx);
///////////////////////////////////////////Broadcast::AuthInvoker/////////////////////////////////////////////
//Broadcast::AuthInvoker对象的C映射
typedef struct mk_auth_invoker_t *mk_auth_invoker;
/**
* 执行Broadcast::AuthInvoker
* @param err_msg 为空或null则代表鉴权成功
*/
API_EXPORT void API_CALL mk_auth_invoker_do(const mk_auth_invoker ctx, const char *err_msg);
/**
* 克隆mk_auth_invoker对象通过克隆对象为堆对象可以实现跨线程异步执行mk_auth_invoker_do
* 如果是同步执行mk_auth_invoker_do那么没必要克隆对象
*/
API_EXPORT mk_auth_invoker API_CALL mk_auth_invoker_clone(const mk_auth_invoker ctx);
/**
* 销毁堆上的克隆对象
*/
API_EXPORT void API_CALL mk_auth_invoker_clone_release(const mk_auth_invoker ctx);
#ifdef __cplusplus
}
#endif
#endif //MK_EVENT_OBJECTS_H

View File

@@ -0,0 +1,226 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef ZLMEDIAKIT_MK_FRAME_H
#define ZLMEDIAKIT_MK_FRAME_H
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
//是否为关键帧
#define MK_FRAME_FLAG_IS_KEY (1 << 0)
//是否为配置帧(sps/pps/vps等)
#define MK_FRAME_FLAG_IS_CONFIG (1 << 1)
//是否可丢弃的帧(sei/aud)
#define MK_FRAME_FLAG_DROP_ABLE (1 << 2)
//是否不可单独解码的帧(多slice的非vcl帧)
#define MK_FRAME_FLAG_NOT_DECODE_ABLE (1 << 3)
//codec id常量定义
API_EXPORT extern const int MKCodecH264;
API_EXPORT extern const int MKCodecH265;
API_EXPORT extern const int MKCodecAAC;
API_EXPORT extern const int MKCodecG711A;
API_EXPORT extern const int MKCodecG711U;
API_EXPORT extern const int MKCodecOpus;
API_EXPORT extern const int MKCodecL16;
API_EXPORT extern const int MKCodecVP8;
API_EXPORT extern const int MKCodecVP9;
API_EXPORT extern const int MKCodecAV1;
API_EXPORT extern const int MKCodecJPEG;
typedef struct mk_frame_t *mk_frame;
// 用户自定义free回调函数
typedef void(API_CALL *on_mk_frame_data_release)(void *user_data, char *ptr);
/**
* 创建frame对象并返回其引用
* @param codec_id 编解码类型请参考MKCodecXXX定义
* @param dts 解码时间戳,单位毫秒
* @param pts 显示时间戳,单位毫秒
* @param data 单帧数据
* @param size 单帧数据长度
* @param cb data指针free释放回调, 如果为空,内部会拷贝数据
* @param user_data data指针free释放回调用户指针
* @return frame对象引用
*/
API_EXPORT mk_frame API_CALL mk_frame_create(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size,
on_mk_frame_data_release cb, void *user_data);
API_EXPORT mk_frame API_CALL mk_frame_create2(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size,
on_mk_frame_data_release cb, void *user_data, on_user_data_free user_data_free);
/**
* 减引用frame对象
* @param frame 帧对象引用
*/
API_EXPORT void API_CALL mk_frame_unref(mk_frame frame);
/**
* 引用frame对象
* @param frame 被引用的frame对象
* @return 新的对象引用
*/
API_EXPORT mk_frame API_CALL mk_frame_ref(mk_frame frame);
/**
* 获取frame 编码codec类型请参考MKCodecXXX定义
*/
API_EXPORT int API_CALL mk_frame_codec_id(mk_frame frame);
/**
* 获取帧编码codec名称
*/
API_EXPORT const char* API_CALL mk_frame_codec_name(mk_frame frame);
/**
* 帧是否为视频
*/
API_EXPORT int API_CALL mk_frame_is_video(mk_frame frame);
/**
* 获取帧数据指针
*/
API_EXPORT const char* API_CALL mk_frame_get_data(mk_frame frame);
/**
* 获取帧数据指针长度
*/
API_EXPORT size_t API_CALL mk_frame_get_data_size(mk_frame frame);
/**
* 返回帧数据前缀长度譬如H264/H265前缀一般是0x00 00 00 01,那么本函数返回4
*/
API_EXPORT size_t API_CALL mk_frame_get_data_prefix_size(mk_frame frame);
/**
* 获取解码时间戳,单位毫秒
*/
API_EXPORT uint64_t API_CALL mk_frame_get_dts(mk_frame frame);
/**
* 获取显示时间戳,单位毫秒
*/
API_EXPORT uint64_t API_CALL mk_frame_get_pts(mk_frame frame);
/**
* 获取帧flag请参考 MK_FRAME_FLAG
*/
API_EXPORT uint32_t API_CALL mk_frame_get_flags(mk_frame frame);
//////////////////////////////////////////////////////////////////////
typedef struct mk_buffer_t *mk_buffer;
typedef struct mk_frame_merger_t *mk_frame_merger;
/**
* 创建帧合并器
* @param type 起始头类型0: none, 1: h264_prefix/AnnexB(0x 00 00 00 01), 2: mp4_nal_size(avcC)
* @return 帧合并器
*/
API_EXPORT mk_frame_merger API_CALL mk_frame_merger_create(int type);
/**
* 销毁帧合并器
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_frame_merger_release(mk_frame_merger ctx);
/**
* 清空merger对象缓冲方便复用
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_frame_merger_clear(mk_frame_merger ctx);
/**
* 合并帧回调函数
* @param user_data 用户数据指针
* @param dts 解码时间戳
* @param pts 显示时间戳
* @param buffer 合并后数据buffer对象
* @param have_key_frame 合并后数据中是否包含关键帧
*/
typedef void(API_CALL *on_mk_frame_merger)(void *user_data, uint64_t dts, uint64_t pts, mk_buffer buffer, int have_key_frame);
/**
* 输入frame到merger对象并合并
* @param ctx 对象指针
* @param frame 帧数据
* @param cb 帧合并回调函数
* @param user_data 帧合并回调函数用户数据指针
*/
API_EXPORT void API_CALL mk_frame_merger_input(mk_frame_merger ctx, mk_frame frame, on_mk_frame_merger cb, void *user_data);
/**
* 强制flush merger对象缓冲调用此api前需要确保先调用mk_frame_merger_input函数并且回调参数有效
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_frame_merger_flush(mk_frame_merger ctx);
//////////////////////////////////////////////////////////////////////
typedef struct mk_mpeg_muxer_t *mk_mpeg_muxer;
/**
* mpeg-ps/ts 打包器输出回调函数
* @param user_data 设置回调时的用户数据指针
* @param muxer 对象
* @param frame 帧数据
* @param size 帧数据长度
* @param timestamp 时间戳
* @param key_pos 是否关键帧
*/
typedef void(API_CALL *on_mk_mpeg_muxer_frame)(void *user_data, mk_mpeg_muxer muxer, const char *frame, size_t size, uint64_t timestamp, int key_pos);
/**
* mpeg-ps/ts 打包器
* @param cb 打包回调函数
* @param user_data 回调用户数据指针
* @param is_ps 是否是ps
* @return 打包器对象
*/
API_EXPORT mk_mpeg_muxer API_CALL mk_mpeg_muxer_create(on_mk_mpeg_muxer_frame cb, void *user_data, int is_ps);
/**
* 删除mpeg-ps/ts 打包器
* @param ctx 打包器
*/
API_EXPORT void API_CALL mk_mpeg_muxer_release(mk_mpeg_muxer ctx);
/**
* 添加音视频track
* @param ctx mk_mpeg_muxer对象
* @param track mk_track对象音视频轨道
*/
API_EXPORT void API_CALL mk_mpeg_muxer_init_track(mk_mpeg_muxer ctx, void* track);
/**
* 初始化track完毕后调用此函数
* 在单track(只有音频或视频)时因为ZLMediaKit不知道后续是否还要添加track所以会多等待3秒钟
* 如果产生的流是单Track类型请调用此函数以便加快流生成速度当然不调用该函数影响也不大(会多等待3秒)
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_mpeg_muxer_init_complete(mk_mpeg_muxer ctx);
/**
* 输入frame对象
* @param ctx mk_mpeg_muxer对象
* @param frame 帧对象
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_mpeg_muxer_input_frame(mk_mpeg_muxer ctx, mk_frame frame);
#ifdef __cplusplus
}
#endif
#endif //ZLMEDIAKIT_MK_FRAME_H

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef ZLMEDIAKIT_MK_H264_SPLITTER_H
#define ZLMEDIAKIT_MK_H264_SPLITTER_H
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mk_h264_splitter_t *mk_h264_splitter;
/**
* h264 分帧器输出回调函数
* @param user_data 设置回调时的用户数据指针
* @param splitter 对象
* @param frame 帧数据
* @param size 帧数据长度
*/
typedef void(API_CALL *on_mk_h264_splitter_frame)(void *user_data, mk_h264_splitter splitter, const char *frame, int size);
/**
* 创建h264分帧器
* @param cb 分帧回调函数
* @param user_data 回调用户数据指针
* @param is_h265 是否是265
* @return 分帧器对象
*/
API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create(on_mk_h264_splitter_frame cb, void *user_data, int is_h265);
API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create2(on_mk_h264_splitter_frame cb, void *user_data, on_user_data_free user_data_free, int is_h265);
/**
* 删除h264分帧器
* @param ctx 分帧器
*/
API_EXPORT void API_CALL mk_h264_splitter_release(mk_h264_splitter ctx);
/**
* 输入数据并分帧
* @param ctx 分帧器
* @param data h264/h265数据
* @param size 数据长度
*/
API_EXPORT void API_CALL mk_h264_splitter_input_data(mk_h264_splitter ctx, const char *data, int size);
#ifdef __cplusplus
}
#endif
#endif //ZLMEDIAKIT_MK_H264_SPLITTER_H

View File

@@ -0,0 +1,159 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_HTTPCLIENT_H_
#define MK_HTTPCLIENT_H_
#include "mk_common.h"
#include "mk_events_objects.h"
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////HttpDownloader/////////////////////////////////////////////
typedef struct mk_http_downloader_t *mk_http_downloader;
/**
* @param user_data 用户数据指针
* @param code 错误代码0代表成功
* @param err_msg 错误提示
* @param file_path 文件保存路径
*/
typedef void(API_CALL *on_mk_download_complete)(void *user_data, int code, const char *err_msg, const char *file_path);
/**
* 创建http[s]下载器
* @return 下载器指针
*/
API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create();
/**
* 销毁http[s]下载器
* @param ctx 下载器指针
*/
API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx);
/**
* 开始http[s]下载
* @param ctx 下载器指针
* @param url http[s]下载url
* @param file 文件保存路径
* @param cb 回调函数
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data);
API_EXPORT void API_CALL mk_http_downloader_start2(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data, on_user_data_free user_data_free);
///////////////////////////////////////////HttpRequester/////////////////////////////////////////////
typedef struct mk_http_requester_t *mk_http_requester;
/**
* http请求结果回调
* 在code == 0时代表本次http会话是完整的收到了http回复
* 用户应该通过user_data获取到mk_http_requester对象
* 然后通过mk_http_requester_get_response等函数获取相关回复数据
* 在回调结束时应该通过mk_http_requester_release函数销毁该对象
* 或者调用mk_http_requester_clear函数后再复用该对象
* @param user_data 用户数据指针
* @param code 错误代码0代表成功
* @param err_msg 错误提示
*/
typedef void(API_CALL *on_mk_http_requester_complete)(void *user_data, int code, const char *err_msg);
/**
* 创建HttpRequester
*/
API_EXPORT mk_http_requester API_CALL mk_http_requester_create();
/**
* 在复用mk_http_requester对象时才需要用到此方法
*/
API_EXPORT void API_CALL mk_http_requester_clear(mk_http_requester ctx);
/**
* 销毁HttpRequester
* 如果调用了mk_http_requester_start函数且正在等待http回复
* 也可以调用mk_http_requester_release方法取消本次http请求
*/
API_EXPORT void API_CALL mk_http_requester_release(mk_http_requester ctx);
/**
* 设置HTTP方法譬如GET/POST
*/
API_EXPORT void API_CALL mk_http_requester_set_method(mk_http_requester ctx,const char *method);
/**
* 批量设置设置HTTP头
* @param header 譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾
*/
API_EXPORT void API_CALL mk_http_requester_set_header(mk_http_requester ctx, const char *header[]);
/**
* 添加HTTP头
* @param key 譬如Content-Type
* @param value 譬如 text/html
* @param force 如果已经存在该key是否强制替换
*/
API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,const char *key,const char *value,int force);
/**
* 设置消息体,
* @param body mk_http_body对象通过mk_http_body_from_string等函数生成使用完毕后请调用mk_http_body_release释放之
*/
API_EXPORT void API_CALL mk_http_requester_set_body(mk_http_requester ctx, mk_http_body body);
/**
* 在收到HTTP回复后可调用该方法获取状态码
* @return 譬如 200 OK
*/
API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx);
/**
* 在收到HTTP回复后可调用该方法获取响应HTTP头
* @param key HTTP头键名
* @return HTTP头键值
*/
API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key);
/**
* 在收到HTTP回复后可调用该方法获取响应HTTP body
* @param length 返回body长度,可以为null
* @return body指针
*/
API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requester ctx, size_t *length);
/**
* 在收到HTTP回复后可调用该方法获取响应
* @return 响应对象
*/
API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx);
/**
* 设置回调函数
* @param cb 回调函数,不能为空
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data);
API_EXPORT void API_CALL mk_http_requester_set_cb2(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data, on_user_data_free user_data_free);
/**
* 开始url请求
* @param url 请求url支持http/https
* @param timeout_second 最大超时时间
*/
API_EXPORT void API_CALL mk_http_requester_start(mk_http_requester ctx,const char *url, float timeout_second);
#ifdef __cplusplus
}
#endif
#endif /* MK_HTTPCLIENT_H_ */

View File

@@ -0,0 +1,297 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_MEDIA_H_
#define MK_MEDIA_H_
#include "mk_common.h"
#include "mk_track.h"
#include "mk_frame.h"
#include "mk_events_objects.h"
#include "mk_thread.h"
#include "mk_util.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mk_media_t *mk_media;
/**
* 创建一个媒体源
* @param vhost 虚拟主机名一般为__defaultVhost__
* @param app 应用名推荐为live
* @param stream 流id例如camera
* @param duration 时长(单位秒)直播则为0
* @param hls_enabled 是否生成hls
* @param mp4_enabled 是否生成mp4
* @return 对象指针
*/
API_EXPORT mk_media API_CALL mk_media_create(const char *vhost, const char *app, const char *stream,
float duration, int hls_enabled, int mp4_enabled);
/**
* 创建一个媒体源
* @param vhost 虚拟主机名一般为__defaultVhost__
* @param app 应用名推荐为live
* @param stream 流id例如camera
* @param duration 时长(单位秒)直播则为0
* @param option ProtocolOption相关配置
* @return 对象指针
*/
API_EXPORT mk_media API_CALL mk_media_create2(const char *vhost, const char *app, const char *stream, float duration, mk_ini option);
/**
* 销毁媒体源
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_media_release(mk_media ctx);
/**
* 添加音视频track
* @param ctx mk_media对象
* @param track mk_track对象音视频轨道
*/
API_EXPORT void API_CALL mk_media_init_track(mk_media ctx, mk_track track);
/**
* 添加视频轨道请改用mk_media_init_track方法
* @param ctx 对象指针
* @param codec_id 0:CodecH264/1:CodecH265
* @param width 视频宽度; 在编码时才有效
* @param height 视频高度; 在编码时才有效
* @param fps 视频fps; 在编码时才有效
* @param bit_rate 视频比特率,单位bps; 在编码时才有效
* @param width 视频宽度
* @param height 视频高度
* @param fps 视频fps
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps, int bit_rate);
/**
* 添加音频轨道请改用mk_media_init_track方法
* @param ctx 对象指针
* @param codec_id 2:CodecAAC/3:CodecG711A/4:CodecG711U/5:OPUS
* @param channel 通道数
* @param sample_bit 采样位数只支持16
* @param sample_rate 采样率
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sample_rate, int channels, int sample_bit);
/**
* 初始化h264/h265/aac完毕后调用此函数
* 在单track(只有音频或视频)时因为ZLMediaKit不知道后续是否还要添加track所以会多等待3秒钟
* 如果产生的流是单Track类型请调用此函数以便加快流生成速度当然不调用该函数影响也不大(会多等待3秒)
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx);
/**
* 输入frame对象
* @param ctx mk_media对象
* @param frame 帧对象
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_input_frame(mk_media ctx, mk_frame frame);
/**
* 输入单帧H264视频帧起始字节00 00 01,00 00 00 01均可请改用mk_media_input_frame方法
* @param ctx 对象指针
* @param data 单帧H264数据
* @param len 单帧H264数据字节数
* @param dts 解码时间戳,单位毫秒
* @param pts 播放时间戳,单位毫秒
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint64_t dts, uint64_t pts);
/**
* 输入单帧H265视频帧起始字节00 00 01,00 00 00 01均可请改用mk_media_input_frame方法
* @param ctx 对象指针
* @param data 单帧H265数据
* @param len 单帧H265数据字节数
* @param dts 解码时间戳,单位毫秒
* @param pts 播放时间戳,单位毫秒
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint64_t dts, uint64_t pts);
/**
* 输入YUV视频数据
* @param ctx 对象指针
* @param yuv yuv420p数据
* @param linesize yuv420p linesize
* @param cts 视频采集时间戳,单位毫秒
*/
API_EXPORT void API_CALL mk_media_input_yuv(mk_media ctx, const char *yuv[3], int linesize[3], uint64_t cts);
/**
* 输入单帧AAC音频(单独指定adts头)请改用mk_media_input_frame方法
* @param ctx 对象指针
* @param data 不包含adts头的单帧AAC数据adts头7个字节
* @param len 单帧AAC数据字节数
* @param dts 时间戳,毫秒
* @param adts adts头可以为null
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint64_t dts, void *adts);
/**
* 输入单帧PCM音频,启用ENABLE_FAAC编译时该函数才有效
* @param ctx 对象指针
* @param data 单帧PCM数据
* @param len 单帧PCM数据字节数
* @param dts 时间戳,毫秒
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_input_pcm(mk_media ctx, void *data, int len, uint64_t pts);
/**
* 输入单帧OPUS/G711音频帧请改用mk_media_input_frame方法
* @param ctx 对象指针
* @param data 单帧音频数据
* @param len 单帧音频数据字节数
* @param dts 时间戳,毫秒
* @return 1代表成功0失败
*/
API_EXPORT int API_CALL mk_media_input_audio(mk_media ctx, const void* data, int len, uint64_t dts);
/**
* MediaSource.close()回调事件
* 在选择关闭一个关联的MediaSource时将会最终触发到该回调
* 你应该通过该事件调用mk_media_release函数并且释放其他资源
* 如果你不调用mk_media_release函数那么MediaSource.close()操作将无效
* @param user_data 用户数据指针通过mk_media_set_on_close函数设置
*/
typedef void(API_CALL *on_mk_media_close)(void *user_data);
/**
* 监听MediaSource.close()事件
* 在选择关闭一个关联的MediaSource时将会最终触发到该回调
* 你应该通过该事件调用mk_media_release函数并且释放其他资源
* @param ctx 对象指针
* @param cb 回调指针
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data);
API_EXPORT void API_CALL mk_media_set_on_close2(mk_media ctx, on_mk_media_close cb, void *user_data, on_user_data_free user_data_free);
/**
* 收到客户端的seek请求时触发该回调
* @param user_data 用户数据指针,通过mk_media_set_on_seek设置
* @param stamp_ms seek至的时间轴位置单位毫秒
* @return 1代表将处理seek请求0代表忽略该请求
*/
typedef int(API_CALL *on_mk_media_seek)(void *user_data,uint32_t stamp_ms);
/**
* 收到客户端的pause或resume请求时触发该回调
* @param user_data 用户数据指针,通过mk_media_set_on_pause设置
* @param pause 1:暂停, 0: 恢复
*/
typedef int(API_CALL* on_mk_media_pause)(void* user_data, int pause);
/**
* 收到客户端的speed请求时触发该回调
* @param user_data 用户数据指针,通过mk_media_set_on_pause设置
* @param speed 0.5 1.0 2.0
*/
typedef int(API_CALL* on_mk_media_speed)(void* user_data, float speed);
/**
* 监听播放器seek请求事件
* @param ctx 对象指针
* @param cb 回调指针
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, void *user_data);
API_EXPORT void API_CALL mk_media_set_on_seek2(mk_media ctx, on_mk_media_seek cb, void *user_data, on_user_data_free user_data_free);
/**
* 监听播放器pause请求事件
* @param ctx 对象指针
* @param cb 回调指针
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause cb, void *user_data);
API_EXPORT void API_CALL mk_media_set_on_pause2(mk_media ctx, on_mk_media_pause cb, void *user_data, on_user_data_free user_data_free);
/**
* 监听播放器pause请求事件
* @param ctx 对象指针
* @param cb 回调指针
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_media_set_on_speed(mk_media ctx, on_mk_media_speed cb, void *user_data);
API_EXPORT void API_CALL mk_media_set_on_speed2(mk_media ctx, on_mk_media_speed cb, void *user_data, on_user_data_free user_data_free);
/**
* 获取总的观看人数
* @param ctx 对象指针
* @return 观看人数
*/
API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx);
/**
* 生成的MediaSource注册或注销事件
* @param user_data 设置回调时的用户数据指针
* @param sender 生成的MediaSource对象
* @param regist 1为注册事件0为注销事件
*/
typedef void(API_CALL *on_mk_media_source_regist)(void *user_data, mk_media_source sender, int regist);
/**
* 设置MediaSource注册或注销事件回调函数
* @param ctx 对象指针
* @param cb 回调指针
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data);
API_EXPORT void API_CALL mk_media_set_on_regist2(mk_media ctx, on_mk_media_source_regist cb, void *user_data, on_user_data_free user_data_free);
/**
* rtp推流成功与否的回调(第一次成功后,后面将一直重试)
*/
typedef on_mk_media_source_send_rtp_result on_mk_media_send_rtp_result;
/**
* 开始发送一路ps-rtp流(通过ssrc区分多路)此api线程安全
* @param ctx 对象指针
* @param dst_url 目标ip或域名
* @param dst_port 目标端口
* @param ssrc rtp的ssrc10进制的字符串打印
* @param is_udp 是否为udp
* @param cb 启动成功或失败回调
* @param user_data 回调用户指针
*/
API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data);
API_EXPORT void API_CALL mk_media_start_send_rtp2(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data, on_user_data_free user_data_free);
/**
* 停止某路或全部ps-rtp发送此api线程安全
* @param ctx 对象指针
* @param ssrc rtp的ssrc10进制的字符串打印如果为null或空字符串则停止所有rtp推流
*/
API_EXPORT void API_CALL mk_media_stop_send_rtp(mk_media ctx, const char *ssrc);
/**
* 获取所属线程
* @param ctx 对象指针
*/
API_EXPORT mk_thread API_CALL mk_media_get_owner_thread(mk_media ctx);
#ifdef __cplusplus
}
#endif
#endif /* MK_MEDIA_H_ */

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_API_H_
#define MK_API_H_
#include "mk_common.h"
#include "mk_httpclient.h"
#include "mk_media.h"
#include "mk_proxyplayer.h"
#include "mk_recorder.h"
#include "mk_player.h"
#include "mk_pusher.h"
#include "mk_events.h"
#include "mk_tcp.h"
#include "mk_util.h"
#include "mk_thread.h"
#include "mk_rtp_server.h"
#include "mk_h264_splitter.h"
#include "mk_frame.h"
#include "mk_track.h"
#include "mk_transcode.h"
#endif /* MK_API_H_ */

View File

@@ -0,0 +1,136 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_PLAYER_H_
#define MK_PLAYER_H_
#include "mk_common.h"
#include "mk_frame.h"
#include "mk_track.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mk_player_t *mk_player;
/**
* 播放结果或播放中断事件的回调
* @param user_data 用户数据指针
* @param err_code 错误代码0为成功
* @param err_msg 错误提示
* @param tracks track列表
* @param track_count track个数
*/
typedef void(API_CALL *on_mk_play_event)(void *user_data, int err_code, const char *err_msg, mk_track tracks[],
int track_count);
/**
* 创建一个播放器,支持rtmp[s]/rtsp[s]
* @return 播放器指针
*/
API_EXPORT mk_player API_CALL mk_player_create();
/**
* 销毁播放器
* @param ctx 播放器指针
*/
API_EXPORT void API_CALL mk_player_release(mk_player ctx);
/**
* 设置播放器配置选项
* @param ctx 播放器指针
* @param key 配置项键,支持 net_adapter/rtp_type/rtsp_user/rtsp_pwd/protocol_timeout_ms/media_timeout_ms/beat_interval_ms/wait_track_ready
* @param val 配置项值,如果是整形需要转换成统一转换成string
*/
API_EXPORT void API_CALL mk_player_set_option(mk_player ctx, const char *key, const char *val);
/**
* 开始播放url
* @param ctx 播放器指针
* @param url rtsp[s]/rtmp[s] url
*/
API_EXPORT void API_CALL mk_player_play(mk_player ctx, const char *url);
/**
* 暂停或恢复播放,仅对点播有用
* @param ctx 播放器指针
* @param pause 1:暂停播放0恢复播放
*/
API_EXPORT void API_CALL mk_player_pause(mk_player ctx, int pause);
/**
* 倍数播放,仅对点播有用
* @param ctx 播放器指针
* @param speed 0.5 1.0 2.0
*/
API_EXPORT void API_CALL mk_player_speed(mk_player ctx, float speed);
/**
* 设置点播进度条
* @param ctx 对象指针
* @param progress 取值范围未 0.01.0
*/
API_EXPORT void API_CALL mk_player_seekto(mk_player ctx, float progress);
/**
* 设置点播进度条
* @param ctx 对象指针
* @param seek_pos 取值范围 相对于开始时间增量 单位秒
*/
API_EXPORT void API_CALL mk_player_seekto_pos(mk_player ctx, int seek_pos);
/**
* 设置播放器开启播放结果回调函数
* @param ctx 播放器指针
* @param cb 回调函数指针,设置null立即取消回调
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data);
API_EXPORT void API_CALL mk_player_set_on_result2(mk_player ctx, on_mk_play_event cb, void *user_data, on_user_data_free user_data_free);
/**
* 设置播放被异常中断的回调
* @param ctx 播放器指针
* @param cb 回调函数指针,设置null立即取消回调
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_event cb, void *user_data);
API_EXPORT void API_CALL mk_player_set_on_shutdown2(mk_player ctx, on_mk_play_event cb, void *user_data, on_user_data_free user_data_free);
///////////////////////////获取音视频相关信息接口在播放成功回调触发后才有效///////////////////////////////
/**
* 获取点播节目时长如果是直播返回0否则返回秒数
*/
API_EXPORT float API_CALL mk_player_duration(mk_player ctx);
/**
* 获取点播播放进度,取值范围 0.01.0
*/
API_EXPORT float API_CALL mk_player_progress(mk_player ctx);
/**
* 获取点播播放进度位置,取值范围 相对于开始时间增量 单位秒
*/
API_EXPORT int API_CALL mk_player_progress_pos(mk_player ctx);
/**
* 获取丢包率rtsp时有效
* @param ctx 对象指针
* @param track_type 0视频1音频
*/
API_EXPORT float API_CALL mk_player_loss_rate(mk_player ctx, int track_type);
#ifdef __cplusplus
}
#endif
#endif /* MK_PLAYER_H_ */

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_PROXY_PLAYER_H_
#define MK_PROXY_PLAYER_H_
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mk_proxy_player_t *mk_proxy_player;
/**
* 创建一个代理播放器
* @param vhost 虚拟主机名一般为__defaultVhost__
* @param app 应用名
* @param stream 流名
* @param rtp_type rtsp播放方式:RTP_TCP = 0, RTP_UDP = 1, RTP_MULTICAST = 2
* @param hls_enabled 是否生成hls
* @param mp4_enabled 是否生成mp4
* @return 对象指针
*/
API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create(const char *vhost, const char *app, const char *stream, int hls_enabled, int mp4_enabled);
/**
* 销毁代理播放器
* @param ctx 对象指针
*/
API_EXPORT void API_CALL mk_proxy_player_release(mk_proxy_player ctx);
/**
* 设置代理播放器配置选项
* @param ctx 代理播放器指针
* @param key 配置项键,支持 net_adapter/rtp_type/rtsp_user/rtsp_pwd/protocol_timeout_ms/media_timeout_ms/beat_interval_ms
* @param val 配置项值,如果是整形需要转换成统一转换成string
*/
API_EXPORT void API_CALL mk_proxy_player_set_option(mk_proxy_player ctx, const char *key, const char *val);
/**
* 开始播放
* @param ctx 对象指针
* @param url 播放url,支持rtsp/rtmp
*/
API_EXPORT void API_CALL mk_proxy_player_play(mk_proxy_player ctx, const char *url);
/**
* MediaSource.close()回调事件
* 在选择关闭一个关联的MediaSource时将会最终触发到该回调
* 你应该通过该事件调用mk_proxy_player_release函数并且释放其他资源
* 如果你不调用mk_proxy_player_release函数那么MediaSource.close()操作将无效
* @param user_data 用户数据指针通过mk_proxy_player_set_on_close函数设置
*/
typedef void(API_CALL *on_mk_proxy_player_close)(void *user_data, int err, const char *what, int sys_err);
/**
* 监听MediaSource.close()事件
* 在选择关闭一个关联的MediaSource时将会最终触发到该回调
* 你应该通过该事件调用mk_proxy_player_release函数并且释放其他资源
* @param ctx 对象指针
* @param cb 回调指针
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_proxy_player_set_on_close(mk_proxy_player ctx, on_mk_proxy_player_close cb, void *user_data);
API_EXPORT void API_CALL mk_proxy_player_set_on_close2(mk_proxy_player ctx, on_mk_proxy_player_close cb, void *user_data, on_user_data_free user_data_free);
/**
* 获取总的观看人数
* @param ctx 对象指针
* @return 观看人数
*/
API_EXPORT int API_CALL mk_proxy_player_total_reader_count(mk_proxy_player ctx);
#ifdef __cplusplus
}
#endif
#endif /* MK_PROXY_PLAYER_H_ */

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_PUSHER_H
#define MK_PUSHER_H
#include "mk_common.h"
#include "mk_events_objects.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mk_pusher_t *mk_pusher;
/**
* 推流结果或推流中断事件的回调
* @param user_data 用户数据指针
* @param err_code 错误代码0为成功
* @param err_msg 错误提示
*/
typedef void(API_CALL *on_mk_push_event)(void *user_data,int err_code,const char *err_msg);
/**
* 绑定的MediaSource对象并创建rtmp[s]/rtsp[s]推流器
* MediaSource通过mk_media_create或mk_proxy_player_create或推流生成
* 该MediaSource对象必须已注册
*
* @param schema 绑定的MediaSource对象所属协议支持rtsp/rtmp
* @param vhost 绑定的MediaSource对象的虚拟主机一般为__defaultVhost__
* @param app 绑定的MediaSource对象的应用名一般为live
* @param stream 绑定的MediaSource对象的流id
* @return 对象指针
*/
API_EXPORT mk_pusher API_CALL mk_pusher_create(const char *schema,const char *vhost,const char *app, const char *stream);
/**
* 绑定的MediaSource对象并创建rtmp[s]/rtsp[s]推流器
* MediaSource通过mk_media_create或mk_proxy_player_create或推流生成
* 该MediaSource对象必须已注册
*
* @param src MediaSource对象
* @return 对象指针
*/
API_EXPORT mk_pusher API_CALL mk_pusher_create_src(mk_media_source src);
/**
* 释放推流器
* @param ctx 推流器指针
*/
API_EXPORT void API_CALL mk_pusher_release(mk_pusher ctx);
/**
* 设置推流器配置选项
* @param ctx 推流器指针
* @param key 配置项键,支持 net_adapter/rtp_type/rtsp_user/rtsp_pwd/protocol_timeout_ms/media_timeout_ms/beat_interval_ms
* @param val 配置项值,如果是整形需要转换成统一转换成string
*/
API_EXPORT void API_CALL mk_pusher_set_option(mk_pusher ctx, const char *key, const char *val);
/**
* 开始推流
* @param ctx 推流器指针
* @param url 推流地址支持rtsp[s]/rtmp[s]
*/
API_EXPORT void API_CALL mk_pusher_publish(mk_pusher ctx,const char *url);
/**
* 设置推流器推流结果回调函数
* @param ctx 推流器指针
* @param cb 回调函数指针,不得为null
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_pusher_set_on_result(mk_pusher ctx, on_mk_push_event cb, void *user_data);
API_EXPORT void API_CALL mk_pusher_set_on_result2(mk_pusher ctx, on_mk_push_event cb, void *user_data, on_user_data_free user_data_free);
/**
* 设置推流被异常中断的回调
* @param ctx 推流器指针
* @param cb 回调函数指针,不得为null
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_pusher_set_on_shutdown(mk_pusher ctx, on_mk_push_event cb, void *user_data);
API_EXPORT void API_CALL mk_pusher_set_on_shutdown2(mk_pusher ctx, on_mk_push_event cb, void *user_data, on_user_data_free user_data_free);
#ifdef __cplusplus
}
#endif
#endif //MK_PUSHER_H

View File

@@ -0,0 +1,85 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_RECORDER_API_H_
#define MK_RECORDER_API_H_
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////flv录制/////////////////////////////////////////////
typedef struct mk_flv_recorder_t *mk_flv_recorder;
/**
* 创建flv录制器
* @return
*/
API_EXPORT mk_flv_recorder API_CALL mk_flv_recorder_create();
/**
* 释放flv录制器
* @param ctx
*/
API_EXPORT void API_CALL mk_flv_recorder_release(mk_flv_recorder ctx);
/**
* 开始录制flv
* @param ctx flv录制器
* @param vhost 虚拟主机
* @param app 绑定的RtmpMediaSource的 app名
* @param stream 绑定的RtmpMediaSource的 stream名
* @param file_path 文件存放地址
* @return 0:开始超过,-1:失败,打开文件失败或该RtmpMediaSource不存在
*/
API_EXPORT int API_CALL mk_flv_recorder_start(mk_flv_recorder ctx, const char *vhost, const char *app, const char *stream, const char *file_path);
///////////////////////////////////////////hls/mp4录制/////////////////////////////////////////////
/**
* 获取录制状态
* @param type 0:hls,1:MP4
* @param vhost 虚拟主机
* @param app 应用名
* @param stream 流id
* @return 录制状态,0:未录制, 1:正在录制
*/
API_EXPORT int API_CALL mk_recorder_is_recording(int type, const char *vhost, const char *app, const char *stream);
/**
* 开始录制
* @param type 0:hls-ts,1:MP4,2:hls-fmp4,3:http-fmp4,4:http-ts
* @param vhost 虚拟主机
* @param app 应用名
* @param stream 流id
* @param customized_path 录像文件保存自定义目录默认为空或null则自动生成
* @param max_second mp4录制最大切片时间单位秒置0则采用配置文件配置
* @return 1代表成功0代表失败
*/
API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream, const char *customized_path, size_t max_second);
/**
* 停止录制
* @param type 0:hls-ts,1:MP4,2:hls-fmp4,3:http-fmp4,4:http-ts
* @param vhost 虚拟主机
* @param app 应用名
* @param stream 流id
* @return 1:成功0失败
*/
API_EXPORT int API_CALL mk_recorder_stop(int type, const char *vhost, const char *app, const char *stream);
#ifdef __cplusplus
}
#endif
#endif /* MK_RECORDER_API_H_ */

View File

@@ -0,0 +1,75 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mk_rtp_server_t *mk_rtp_server;
/**
* 创建GB28181 RTP 服务器
* @param port 监听端口0则为随机
* @param tcp_mode tcp模式(0: 不监听端口 1: 监听端口 2: 主动连接到服务端)
* @param stream_id 该端口绑定的流id
* @return
*/
API_EXPORT mk_rtp_server API_CALL mk_rtp_server_create(uint16_t port, int tcp_mode, const char *stream_id);
/**
* TCP 主动模式时连接到服务器是否成功的回调
*/
typedef void(API_CALL *on_mk_rtp_server_connected)(void *user_data, int err, const char *what, int sys_err);
/**
* TCP 主动模式时连接到服务器
* @param @param ctx 服务器对象
* @param dst_url 服务端地址
* @param dst_port 服务端端口
* @param cb 连接到服务器是否成功的回调
* @param user_data 用户数据指针
* @return
*/
API_EXPORT void API_CALL mk_rtp_server_connect(mk_rtp_server ctx, const char *dst_url, uint16_t dst_port, on_mk_rtp_server_connected cb, void *user_data);
API_EXPORT void API_CALL mk_rtp_server_connect2(mk_rtp_server ctx, const char *dst_url, uint16_t dst_port, on_mk_rtp_server_connected cb, void *user_data, on_user_data_free user_data_free);
/**
* 销毁GB28181 RTP 服务器
* @param ctx 服务器对象
*/
API_EXPORT void API_CALL mk_rtp_server_release(mk_rtp_server ctx);
/**
* 获取本地监听的端口号
* @param ctx 服务器对象
* @return 端口号
*/
API_EXPORT uint16_t API_CALL mk_rtp_server_port(mk_rtp_server ctx);
/**
* GB28181 RTP 服务器接收流超时时触发
* @param user_data 用户数据指针
*/
typedef void(API_CALL *on_mk_rtp_server_detach)(void *user_data);
/**
* 监听B28181 RTP 服务器接收流超时事件
* @param ctx 服务器对象
* @param cb 回调函数
* @param user_data 回调函数用户数据指针
*/
API_EXPORT void API_CALL mk_rtp_server_set_on_detach(mk_rtp_server ctx, on_mk_rtp_server_detach cb, void *user_data);
API_EXPORT void API_CALL mk_rtp_server_set_on_detach2(mk_rtp_server ctx, on_mk_rtp_server_detach cb, void *user_data, on_user_data_free user_data_free);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,268 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_TCP_H
#define MK_TCP_H
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////Buffer::Ptr/////////////////////////////////////////////
typedef struct mk_buffer_t *mk_buffer;
typedef void(API_CALL *on_mk_buffer_free)(void *user_data, void *data);
/**
* 创建buffer对象
* @param data 数据指针
* @param len 数据长度
* @param cb 数据指针free回调函数该参数置空时内部会拷贝数据
* @param user_data 数据指针free回调函数on_mk_buffer_free第一个参数
* @return buffer对象
*/
API_EXPORT mk_buffer API_CALL mk_buffer_from_char(const char *data, size_t len, on_mk_buffer_free cb, void *user_data);
API_EXPORT mk_buffer API_CALL mk_buffer_from_char2(const char *data, size_t len, on_mk_buffer_free cb, void *user_data, on_user_data_free user_data_free);
API_EXPORT mk_buffer API_CALL mk_buffer_ref(mk_buffer buffer);
API_EXPORT void API_CALL mk_buffer_unref(mk_buffer buffer);
API_EXPORT const char* API_CALL mk_buffer_get_data(mk_buffer buffer);
API_EXPORT size_t API_CALL mk_buffer_get_size(mk_buffer buffer);
///////////////////////////////////////////SockInfo/////////////////////////////////////////////
//SockInfo对象的C映射
typedef struct mk_sock_info_t *mk_sock_info;
//SockInfo::get_peer_ip()
API_EXPORT const char* API_CALL mk_sock_info_peer_ip(const mk_sock_info ctx, char *buf);
//SockInfo::get_local_ip()
API_EXPORT const char* API_CALL mk_sock_info_local_ip(const mk_sock_info ctx, char *buf);
//SockInfo::get_peer_port()
API_EXPORT uint16_t API_CALL mk_sock_info_peer_port(const mk_sock_info ctx);
//SockInfo::get_local_port()
API_EXPORT uint16_t API_CALL mk_sock_info_local_port(const mk_sock_info ctx);
#ifndef SOCK_INFO_API_RENAME
#define SOCK_INFO_API_RENAME
//mk_tcp_session对象转换成mk_sock_info对象后再获取网络相关信息
#define mk_tcp_session_peer_ip(x,buf) mk_sock_info_peer_ip(mk_tcp_session_get_sock_info(x),buf)
#define mk_tcp_session_local_ip(x,buf) mk_sock_info_local_ip(mk_tcp_session_get_sock_info(x),buf)
#define mk_tcp_session_peer_port(x) mk_sock_info_peer_port(mk_tcp_session_get_sock_info(x))
#define mk_tcp_session_local_port(x) mk_sock_info_local_port(mk_tcp_session_get_sock_info(x))
//mk_tcp_client对象转换成mk_sock_info对象后再获取网络相关信息
#define mk_tcp_client_peer_ip(x,buf) mk_sock_info_peer_ip(mk_tcp_client_get_sock_info(x),buf)
#define mk_tcp_client_local_ip(x,buf) mk_sock_info_local_ip(mk_tcp_client_get_sock_info(x),buf)
#define mk_tcp_client_peer_port(x) mk_sock_info_peer_port(mk_tcp_client_get_sock_info(x))
#define mk_tcp_client_local_port(x) mk_sock_info_local_port(mk_tcp_client_get_sock_info(x))
#endif
///////////////////////////////////////////TcpSession/////////////////////////////////////////////
//TcpSession对象的C映射
typedef struct mk_tcp_session_t *mk_tcp_session;
typedef struct mk_tcp_session_ref_t *mk_tcp_session_ref;
//获取基类指针以便获取其网络相关信息
API_EXPORT mk_sock_info API_CALL mk_tcp_session_get_sock_info(const mk_tcp_session ctx);
//TcpSession::safeShutdown()
API_EXPORT void API_CALL mk_tcp_session_shutdown(const mk_tcp_session ctx,int err,const char *err_msg);
//TcpSession::send()
API_EXPORT void API_CALL mk_tcp_session_send(const mk_tcp_session ctx, const char *data, size_t len);
API_EXPORT void API_CALL mk_tcp_session_send_buffer(const mk_tcp_session ctx, mk_buffer buffer);
//切换到该对象所在线程后再TcpSession::send()
API_EXPORT void API_CALL mk_tcp_session_send_safe(const mk_tcp_session ctx, const char *data, size_t len);
API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ctx, mk_buffer buffer);
//创建mk_tcp_session的强引用
API_EXPORT mk_tcp_session_ref API_CALL mk_tcp_session_ref_from(const mk_tcp_session ctx);
//删除mk_tcp_session的强引用
API_EXPORT void mk_tcp_session_ref_release(const mk_tcp_session_ref ref);
//根据强引用获取mk_tcp_session
API_EXPORT mk_tcp_session mk_tcp_session_from_ref(const mk_tcp_session_ref ref);
///////////////////////////////////////////自定义tcp服务/////////////////////////////////////////////
typedef struct {
/**
* 收到mk_tcp_session创建对象
* @param server_port 服务器端口号
* @param session 会话处理对象
*/
void (API_CALL *on_mk_tcp_session_create)(uint16_t server_port,mk_tcp_session session);
/**
* 收到客户端发过来的数据
* @param server_port 服务器端口号
* @param session 会话处理对象
* @param buffer 数据
*/
void (API_CALL *on_mk_tcp_session_data)(uint16_t server_port,mk_tcp_session session, mk_buffer buffer);
/**
* 每隔2秒的定时器用于管理超时等任务
* @param server_port 服务器端口号
* @param session 会话处理对象
*/
void (API_CALL *on_mk_tcp_session_manager)(uint16_t server_port,mk_tcp_session session);
/**
* 一般由于客户端断开tcp触发
* @param server_port 服务器端口号
* @param session 会话处理对象
* @param code 错误代码
* @param msg 错误提示
*/
void (API_CALL *on_mk_tcp_session_disconnect)(uint16_t server_port,mk_tcp_session session,int code,const char *msg);
} mk_tcp_session_events;
typedef enum {
//普通的tcp
mk_type_tcp = 0,
//ssl类型的tcp
mk_type_ssl = 1,
//基于websocket的连接
mk_type_ws = 2,
//基于ssl websocket的连接
mk_type_wss = 3
}mk_tcp_type;
/**
* tcp会话对象附着用户数据
* 该函数只对mk_tcp_server_server_start启动的服务类型有效
* @param session 会话对象
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_tcp_session_set_user_data(mk_tcp_session session, void *user_data);
API_EXPORT void API_CALL mk_tcp_session_set_user_data2(mk_tcp_session session, void *user_data, on_user_data_free user_data_free);
/**
* 获取tcp会话对象上附着的用户数据
* 该函数只对mk_tcp_server_server_start启动的服务类型有效
* @param session tcp会话对象
* @return 用户数据指针
*/
API_EXPORT void* API_CALL mk_tcp_session_get_user_data(mk_tcp_session session);
/**
* 开启tcp服务器
* @param port 监听端口号0则为随机
* @param type 服务器类型
*/
API_EXPORT uint16_t API_CALL mk_tcp_server_start(uint16_t port, mk_tcp_type type);
/**
* 监听tcp服务器事件
*/
API_EXPORT void API_CALL mk_tcp_server_events_listen(const mk_tcp_session_events *events);
///////////////////////////////////////////自定义tcp客户端/////////////////////////////////////////////
typedef struct mk_tcp_client_t *mk_tcp_client;
//获取基类指针以便获取其网络相关信息
API_EXPORT mk_sock_info API_CALL mk_tcp_client_get_sock_info(const mk_tcp_client ctx);
typedef struct {
/**
* tcp客户端连接服务器成功或失败回调
* @param client tcp客户端
* @param code 0为连接成功否则为失败原因
* @param msg 连接失败错误提示
*/
void (API_CALL *on_mk_tcp_client_connect)(mk_tcp_client client,int code,const char *msg);
/**
* tcp客户端与tcp服务器之间断开回调
* 一般是eof事件导致
* @param client tcp客户端
* @param code 错误代码
* @param msg 错误提示
*/
void (API_CALL *on_mk_tcp_client_disconnect)(mk_tcp_client client,int code,const char *msg);
/**
* 收到tcp服务器发来的数据
* @param client tcp客户端
* @param buffer 数据
*/
void (API_CALL *on_mk_tcp_client_data)(mk_tcp_client client, mk_buffer buffer);
/**
* 每隔2秒的定时器用于管理超时等任务
* @param client tcp客户端
*/
void (API_CALL *on_mk_tcp_client_manager)(mk_tcp_client client);
} mk_tcp_client_events;
/**
* 创建tcp客户端
* @param events 回调函数结构体
* @param user_data 用户数据指针
* @param type 客户端类型
* @return 客户端对象
*/
API_EXPORT mk_tcp_client API_CALL mk_tcp_client_create(mk_tcp_client_events *events, mk_tcp_type type);
/**
* 释放tcp客户端
* @param ctx 客户端对象
*/
API_EXPORT void API_CALL mk_tcp_client_release(mk_tcp_client ctx);
/**
* 发起连接
* @param ctx 客户端对象
* @param host 服务器ip或域名
* @param port 服务器端口号
* @param time_out_sec 超时时间
*/
API_EXPORT void API_CALL mk_tcp_client_connect(mk_tcp_client ctx, const char *host, uint16_t port, float time_out_sec);
/**
* 非线程安全的发送数据
* 开发者如果能确保在本对象网络线程内,可以调用此此函数
* @param ctx 客户端对象
* @param data 数据指针
* @param len 数据长度等于0时内部通过strlen获取
*/
API_EXPORT void API_CALL mk_tcp_client_send(mk_tcp_client ctx, const char *data, int len);
API_EXPORT void API_CALL mk_tcp_client_send_buffer(mk_tcp_client ctx, mk_buffer buffer);
/**
* 切换到本对象的网络线程后再发送数据
* @param ctx 客户端对象
* @param data 数据指针
* @param len 数据长度等于0时内部通过strlen获取
*/
API_EXPORT void API_CALL mk_tcp_client_send_safe(mk_tcp_client ctx, const char *data, int len);
API_EXPORT void API_CALL mk_tcp_client_send_buffer_safe(mk_tcp_client ctx, mk_buffer buffer);
/**
* 客户端附着用户数据
* @param ctx 客户端对象
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_tcp_client_set_user_data(mk_tcp_client ctx, void *user_data);
API_EXPORT void API_CALL mk_tcp_client_set_user_data2(mk_tcp_client ctx, void *user_data, on_user_data_free user_data_free);
/**
* 获取客户端对象上附着的用户数据
* @param ctx 客户端对象
* @return 用户数据指针
*/
API_EXPORT void* API_CALL mk_tcp_client_get_user_data(mk_tcp_client ctx);
#ifdef __cplusplus
}
#endif
#endif //MK_TCP_H

View File

@@ -0,0 +1,164 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_THREAD_H
#define MK_THREAD_H
#include <assert.h>
#include "mk_common.h"
#include "mk_tcp.h"
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////事件线程/////////////////////////////////////////////
typedef struct mk_thread_t *mk_thread;
/**
* 获取tcp会话对象所在事件线程
* @param ctx tcp会话对象
* @return 对象所在事件线程
*/
API_EXPORT mk_thread API_CALL mk_thread_from_tcp_session(mk_tcp_session ctx);
/**
* 获取tcp客户端对象所在事件线程
* @param ctx tcp客户端
* @return 对象所在事件线程
*/
API_EXPORT mk_thread API_CALL mk_thread_from_tcp_client(mk_tcp_client ctx);
/**
* 根据负载均衡算法,从事件线程池中随机获取一个事件线程
* 如果在事件线程内执行此函数将返回本事件线程
* 事件线程指的是定时器、网络io事件线程
* @return 事件线程
*/
API_EXPORT mk_thread API_CALL mk_thread_from_pool();
/**
* 根据负载均衡算法,从后台线程池中随机获取一个线程
* 后台线程本质与事件线程相同,只是优先级更低,同时可以执行短时间的阻塞任务
* ZLMediaKit中后台线程用于dns解析、mp4点播时的文件解复用
* @return 后台线程
*/
API_EXPORT mk_thread API_CALL mk_thread_from_pool_work();
typedef struct mk_thread_pool_t *mk_thread_pool;
/**
* 创建线程池
* @param name 线程池名称,方便调试
* @param n_thread 线程个数0时为cpu个数
* @param priority 线程优先级分为PRIORITY_LOWEST = 0,PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_HIGHEST
* @return 线程池
*/
API_EXPORT mk_thread_pool API_CALL mk_thread_pool_create(const char *name, size_t n_thread, int priority);
/**
* 销毁线程池
* @param pool 线程池
* @return 0:成功
*/
API_EXPORT int API_CALL mk_thread_pool_release(mk_thread_pool pool);
/**
* 从线程池获取一个线程
* @param pool 线程池
* @return 线程
*/
API_EXPORT mk_thread API_CALL mk_thread_from_thread_pool(mk_thread_pool pool);
///////////////////////////////////////////线程切换/////////////////////////////////////////////
typedef void (API_CALL *on_mk_async)(void *user_data);
/**
* 切换到事件线程并异步执行
* @param ctx 事件线程
* @param cb 回调函数
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_async_do(mk_thread ctx, on_mk_async cb, void *user_data);
API_EXPORT void API_CALL mk_async_do2(mk_thread ctx, on_mk_async cb, void *user_data, on_user_data_free user_data_free);
/**
* 切换到事件线程并延时执行
* @param ctx 事件线程
* @param ms 延时时间,单位毫秒
* @param cb 回调函数
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_async_do_delay(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data);
API_EXPORT void API_CALL mk_async_do_delay2(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data, on_user_data_free user_data_free);
/**
* 切换到事件线程并同步执行
* @param ctx 事件线程
* @param cb 回调函数
* @param user_data 用户数据指针
*/
API_EXPORT void API_CALL mk_sync_do(mk_thread ctx, on_mk_async cb, void *user_data);
///////////////////////////////////////////定时器/////////////////////////////////////////////
typedef struct mk_timer_t *mk_timer;
/**
* 定时器触发事件
* @return 下一次触发延时(单位毫秒)返回0则不再重复
*/
typedef uint64_t (API_CALL *on_mk_timer)(void *user_data);
/**
* 创建定时器
* @param ctx 线程对象
* @param delay_ms 执行延时,单位毫秒
* @param cb 回调函数
* @param user_data 用户数据指针
* @return 定时器对象
*/
API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data);
API_EXPORT mk_timer API_CALL mk_timer_create2(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data, on_user_data_free user_data_free);
/**
* 销毁和取消定时器
* @param ctx 定时器对象
*/
API_EXPORT void API_CALL mk_timer_release(mk_timer ctx);
///////////////////////////////////////////信号量/////////////////////////////////////////////
typedef struct mk_sem_t *mk_sem;
/**
* 创建信号量
*/
API_EXPORT mk_sem API_CALL mk_sem_create();
/**
* 销毁信号量
*/
API_EXPORT void API_CALL mk_sem_release(mk_sem sem);
/**
* 信号量加n
*/
API_EXPORT void API_CALL mk_sem_post(mk_sem sem, size_t n);
/**
* 信号量减1
* @param sem
*/
API_EXPORT void API_CALL mk_sem_wait(mk_sem sem);
#ifdef __cplusplus
}
#endif
#endif //MK_THREAD_H

View File

@@ -0,0 +1,136 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef ZLMEDIAKIT_MK_TRACK_H
#define ZLMEDIAKIT_MK_TRACK_H
#include "mk_common.h"
#include "mk_frame.h"
#ifdef __cplusplus
extern "C" {
#endif
//音视频轨道
typedef struct mk_track_t *mk_track;
//输出frame回调
typedef void(API_CALL *on_mk_frame_out)(void *user_data, mk_frame frame);
//track创建参数
typedef union {
struct {
int width;
int height;
int fps;
} video;
struct {
int channels;
int sample_rate;
} audio;
} codec_args;
/**
* 创建track对象引用
* @param codec_id 请参考MKCodecXXX 常量定义
* @param args 视频或音频参数
* @return track对象引用
*/
API_EXPORT mk_track API_CALL mk_track_create(int codec_id, codec_args *args);
/**
* 减引用track对象
* @param track track对象
*/
API_EXPORT void API_CALL mk_track_unref(mk_track track);
/**
* 引用track对象
* @param track track对象
* @return 新的track引用对象
*/
API_EXPORT mk_track API_CALL mk_track_ref(mk_track track);
/**
* 获取track 编码codec类型请参考MKCodecXXX定义
*/
API_EXPORT int API_CALL mk_track_codec_id(mk_track track);
/**
* 获取编码codec名称
*/
API_EXPORT const char* API_CALL mk_track_codec_name(mk_track track);
/**
* 获取比特率信息
*/
API_EXPORT int API_CALL mk_track_bit_rate(mk_track track);
/**
* 监听frame输出事件
* @param track track对象
* @param cb frame输出回调
* @param user_data frame输出回调用户指针参数
*/
API_EXPORT void *API_CALL mk_track_add_delegate(mk_track track, on_mk_frame_out cb, void *user_data);
API_EXPORT void *API_CALL mk_track_add_delegate2(mk_track track, on_mk_frame_out cb, void *user_data, on_user_data_free user_data_free);
/**
* 取消frame输出事件监听
* @param track track对象
* @param tag mk_track_add_delegate返回值
*/
API_EXPORT void API_CALL mk_track_del_delegate(mk_track track, void *tag);
/**
* 输入frame到track通常你不需要调用此api
*/
API_EXPORT void API_CALL mk_track_input_frame(mk_track track, mk_frame frame);
/**
* track是否为视频
*/
API_EXPORT int API_CALL mk_track_is_video(mk_track track);
/**
* 获取视频宽度
*/
API_EXPORT int API_CALL mk_track_video_width(mk_track track);
/**
* 获取视频高度
*/
API_EXPORT int API_CALL mk_track_video_height(mk_track track);
/**
* 获取视频帧率
*/
API_EXPORT int API_CALL mk_track_video_fps(mk_track track);
/**
* 获取音频采样率
*/
API_EXPORT int API_CALL mk_track_audio_sample_rate(mk_track track);
/**
* 获取音频通道数
*/
API_EXPORT int API_CALL mk_track_audio_channel(mk_track track);
/**
* 获取音频位数一般为16bit
*/
API_EXPORT int API_CALL mk_track_audio_sample_bit(mk_track track);
#ifdef __cplusplus
}
#endif
#endif //ZLMEDIAKIT_MK_TRACK_H

View File

@@ -0,0 +1,188 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef ZLMEDIAKIT_MK_TRANSCODE_H
#define ZLMEDIAKIT_MK_TRANSCODE_H
#include "mk_common.h"
#include "mk_track.h"
#include "mk_tcp.h"
#ifdef __cplusplus
extern "C" {
#endif
//解码器对象
typedef struct mk_decoder_t *mk_decoder;
//解码后的frame
typedef struct mk_frame_pix_t *mk_frame_pix;
//SwsContext的包装
typedef struct mk_swscale_t *mk_swscale;
//FFmpeg原始解码帧对象
typedef struct AVFrame AVFrame;
//FFmpeg编解码器对象
typedef struct AVCodecContext AVCodecContext;
//解码输出回调
typedef void(API_CALL *on_mk_decode)(void *user_data, mk_frame_pix frame);
/**
* 创建解码器
* @param track track对象
* @param thread_num 解码线程数0时为自动
* @return 返回解码器对象NULL代表失败
*/
API_EXPORT mk_decoder API_CALL mk_decoder_create(mk_track track, int thread_num);
/**
* 创建解码器
* @param track track对象
* @param thread_num 解码线程数0时为自动
* @param codec_name_list 偏好的ffmpeg codec name列表以NULL结尾譬如{"libopenh264", "h264_nvdec", NULL};
* 在数组中越前,优先级越高;如果指定的codec不存在或跟mk_track_codec_id类型不匹配时则使用内部默认codec列表
* @return 返回解码器对象NULL代表失败
*/
API_EXPORT mk_decoder API_CALL mk_decoder_create2(mk_track track, int thread_num, const char *codec_name_list[]);
/**
* 销毁解码器
* @param ctx 解码器对象
* @param flush_frame 是否等待所有帧解码成功
*/
API_EXPORT void API_CALL mk_decoder_release(mk_decoder ctx, int flush_frame);
/**
* 解码音视频帧
* @param ctx 解码器
* @param frame 帧对象
* @param async 是否异步解码
* @param enable_merge 是否合并帧解码有些情况下需要把时间戳相同的slice合并输入到解码器才能解码
*/
API_EXPORT void API_CALL mk_decoder_decode(mk_decoder ctx, mk_frame frame, int async, int enable_merge);
/**
* 设置异步解码最大帧缓存积压数限制
*/
API_EXPORT void API_CALL mk_decoder_set_max_async_frame_size(mk_decoder ctx, size_t size);
/**
* 设置解码输出回调
* @param ctx 解码器
* @param cb 回调函数
* @param user_data 回调函数用户指针参数
*/
API_EXPORT void API_CALL mk_decoder_set_cb(mk_decoder ctx, on_mk_decode cb, void *user_data);
API_EXPORT void API_CALL mk_decoder_set_cb2(mk_decoder ctx, on_mk_decode cb, void *user_data, on_user_data_free user_data_free);
/**
* 获取FFmpeg原始AVCodecContext对象
* @param ctx 解码器
*/
API_EXPORT const AVCodecContext* API_CALL mk_decoder_get_context(mk_decoder ctx);
/////////////////////////////////////////////////////////////////////////////////////////////
/**
* 创建解码帧mk_frame_pix新引用
* @param frame 原始引用
* @return 新引用
*/
API_EXPORT mk_frame_pix API_CALL mk_frame_pix_ref(mk_frame_pix frame);
/**
* 解码帧mk_frame_pix减引用
* @param frame 原始引用
*/
API_EXPORT void API_CALL mk_frame_pix_unref(mk_frame_pix frame);
/**
* 从FFmpeg AVFrame转换为mk_frame_pix
* @param frame FFmpeg AVFrame
* @return mk_frame_pix对象
*/
API_EXPORT mk_frame_pix API_CALL mk_frame_pix_from_av_frame(AVFrame *frame);
/**
* 可无内存拷贝的创建mk_frame_pix对象
* @param plane_data 多个平面数据, 通过mk_buffer_get_data获取其数据指针
* @param line_size 平面数据line size
* @param plane 数据平面个数
* @return mk_frame_pix对象
*/
API_EXPORT mk_frame_pix API_CALL mk_frame_pix_from_buffer(mk_buffer plane_data[], int line_size[], int plane);
/**
* 获取FFmpeg AVFrame对象
* @param frame 解码帧mk_frame_pix
* @return FFmpeg AVFrame对象
*/
API_EXPORT AVFrame* API_CALL mk_frame_pix_get_av_frame(mk_frame_pix frame);
/////////////////////////////////////////////////////////////////////////////////////////////
/**
* 创建ffmpeg SwsContext wrapper实例
* @param output AVPixelFormat类型AV_PIX_FMT_BGR24==3
* @param width 目标宽度置0时则与输入时一致
* @param height 目标高度置0时则与输入时一致
* @return SwsContext wrapper 实例
*/
API_EXPORT mk_swscale mk_swscale_create(int output, int width, int height);
/**
* 释放ffmpeg SwsContext wrapper实例
* @param ctx SwsContext wrapper实例
*/
API_EXPORT void mk_swscale_release(mk_swscale ctx);
/**
* 使用SwsContext转换pix format
* @param ctx SwsContext wrapper实例
* @param frame pix frame
* @param out 转换后存放的数据指针,用户需要确保提前申请并大小足够
* @return sws_scale()返回值the height of the output slice
*/
API_EXPORT int mk_swscale_input_frame(mk_swscale ctx, mk_frame_pix frame, uint8_t *out);
/**
* 使用SwsContext转换pix format
* @param ctx SwsContext wrapper实例
* @param frame pix frame
* @return 新的pix frame对象需要使用mk_frame_pix_unref销毁
*/
API_EXPORT mk_frame_pix mk_swscale_input_frame2(mk_swscale ctx, mk_frame_pix frame);
/////////////////////////////////////////////////////////////////////////////////////////////
API_EXPORT uint8_t **API_CALL mk_get_av_frame_data(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_data(AVFrame *frame, uint8_t *data, int plane);
API_EXPORT int *API_CALL mk_get_av_frame_line_size(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_line_size(AVFrame *frame, int line_size, int plane);
API_EXPORT int64_t API_CALL mk_get_av_frame_dts(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_dts(AVFrame *frame, int64_t dts);
API_EXPORT int64_t API_CALL mk_get_av_frame_pts(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_pts(AVFrame *frame, int64_t pts);
API_EXPORT int API_CALL mk_get_av_frame_width(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_width(AVFrame *frame, int width);
API_EXPORT int API_CALL mk_get_av_frame_height(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_height(AVFrame *frame, int height);
API_EXPORT int API_CALL mk_get_av_frame_format(AVFrame *frame);
API_EXPORT void API_CALL mk_set_av_frame_format(AVFrame *frame, int format);
#ifdef __cplusplus
}
#endif
#endif //ZLMEDIAKIT_MK_TRANSCODE_H

View File

@@ -0,0 +1,156 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MK_UTIL_H
#define MK_UTIL_H
#include <stdlib.h>
#include "mk_common.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* 释放mk api内部malloc的资源
*/
API_EXPORT void API_CALL mk_free(void *ptr);
/**
* 获取本程序可执行文件路径
* @return 文件路径使用完后需要自己mk_free
*/
API_EXPORT char* API_CALL mk_util_get_exe_path();
/**
* 获取本程序可执行文件相同目录下文件的绝对路径
* @param relative_path 同目录下文件的路径相对,可以为null
* @return 文件路径使用完后需要自己mk_free
*/
API_EXPORT char* API_CALL mk_util_get_exe_dir(const char *relative_path);
/**
* 获取unix标准的系统时间戳
* @return 当前系统时间戳
*/
API_EXPORT uint64_t API_CALL mk_util_get_current_millisecond();
/**
* 获取时间字符串
* @param fmt 时间格式,譬如%Y-%m-%d %H:%M:%S
* @return 时间字符串使用完后需要自己mk_free
*/
API_EXPORT char* API_CALL mk_util_get_current_time_string(const char *fmt);
/**
* 打印二进制为字符串
* @param buf 二进制数据
* @param len 数据长度
* @return 可打印的调试信息使用完后需要自己mk_free
*/
API_EXPORT char* API_CALL mk_util_hex_dump(const void *buf, int len);
///////////////////////////////////////////mk ini/////////////////////////////////////////////
typedef struct mk_ini_t *mk_ini;
/**
* 创建ini配置对象
*/
API_EXPORT mk_ini API_CALL mk_ini_create();
/**
* 返回全局默认ini配置
* @return 全局默认ini配置请勿用mk_ini_release释放它
*/
API_EXPORT mk_ini API_CALL mk_ini_default();
/**
* 加载ini配置文件内容
* @param ini ini对象
* @param str 配置文件内容
*/
API_EXPORT void API_CALL mk_ini_load_string(mk_ini ini, const char *str);
/**
* 加载ini配置文件
* @param ini ini对象
* @param file 配置文件路径
*/
API_EXPORT void API_CALL mk_ini_load_file(mk_ini ini, const char *file);
/**
* 销毁ini配置对象
*/
API_EXPORT void API_CALL mk_ini_release(mk_ini ini);
/**
* 添加或覆盖配置项
* @param ini 配置对象
* @param key 配置名两段式field.key
* @param value 配置值
*/
API_EXPORT void API_CALL mk_ini_set_option(mk_ini ini, const char *key, const char *value);
API_EXPORT void API_CALL mk_ini_set_option_int(mk_ini ini, const char *key, int value);
/**
* 获取配置项
* @param ini 配置对象
* @param key 配置名两段式field.key
* @return 配置不存在返回NULL否则返回配置值
*/
API_EXPORT const char *API_CALL mk_ini_get_option(mk_ini ini, const char *key);
/**
* 删除配置项
* @param ini 配置对象
* @param key 配置名两段式field.key
* @return 1: 成功0: 该配置不存在
*/
API_EXPORT int API_CALL mk_ini_del_option(mk_ini ini, const char *key);
/**
* 导出为配置文件内容
* @param ini 配置对象
* @return 配置文件内容字符串用完后需要自行mk_free
*/
API_EXPORT char *API_CALL mk_ini_dump_string(mk_ini ini);
/**
* 导出配置文件到文件
* @param ini 配置对象
* @param file 配置文件路径
*/
API_EXPORT void API_CALL mk_ini_dump_file(mk_ini ini, const char *file);
///////////////////////////////////////////日志/////////////////////////////////////////////
/**
* 打印日志
* @param level 日志级别,支持0~4
* @param file __FILE__
* @param function __FUNCTION__
* @param line __LINE__
* @param fmt printf类型的格式控制字符串
* @param ... 不定长参数
*/
API_EXPORT void API_CALL mk_log_printf(int level, const char *file, const char *function, int line, const char *fmt, ...);
// 以下宏可以替换printf使用
#define log_printf(lev, ...) mk_log_printf(lev, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define log_trace(...) log_printf(0, ##__VA_ARGS__)
#define log_debug(...) log_printf(1, ##__VA_ARGS__)
#define log_info(...) log_printf(2, ##__VA_ARGS__)
#define log_warn(...) log_printf(3, ##__VA_ARGS__)
#define log_error(...) log_printf(4, ##__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif //MK_UTIL_H

View File

@@ -0,0 +1,327 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_BUFFER_H__
#define __MPP_BUFFER_H__
#include "rk_type.h"
#include "mpp_err.h"
/*
* MppBuffer module has several functions:
*
* 1. buffer get / put / reference management / external commit / get info.
* this part is the basic user interface for MppBuffer.
*
* function:
*
* mpp_buffer_get
* mpp_buffer_put
* mpp_buffer_inc_ref
* mpp_buffer_commit
* mpp_buffer_info_get
*
* 2. user buffer working flow control abstraction.
* buffer should attach to certain group, and buffer mode control the buffer usage flow.
* this part is also a part of user interface.
*
* function:
*
* mpp_buffer_group_get
* mpp_buffer_group_normal_get
* mpp_buffer_group_limit_get
* mpp_buffer_group_put
* mpp_buffer_group_limit_config
*
* 3. buffer allocator management
* this part is for allocator on different os, it does not have user interface
* it will support normal buffer, Android ion buffer, Linux v4l2 vb2 buffer
* user can only use MppBufferType to choose.
*
*/
/*
* mpp buffer group support two work flow mode:
*
* normal flow: all buffer are generated by MPP
* under this mode, buffer pool is maintained internally
*
* typical call flow:
*
* mpp_buffer_group_get() return A
* mpp_buffer_get(A) return a ref +1 -> used
* mpp_buffer_inc_ref(a) ref +1
* mpp_buffer_put(a) ref -1
* mpp_buffer_put(a) ref -1 -> unused
* mpp_buffer_group_put(A)
*
* commit flow: all buffer are commited out of MPP
* under this mode, buffers is commit by external api.
* normally MPP only use it but not generate it.
*
* typical call flow:
*
* ==== external allocator ====
* mpp_buffer_group_get() return A
* mpp_buffer_commit(A, x)
* mpp_buffer_commit(A, y)
*
* ======= internal user ======
* mpp_buffer_get(A) return a
* mpp_buffer_get(A) return b
* mpp_buffer_put(a)
* mpp_buffer_put(b)
*
* ==== external allocator ====
* mpp_buffer_group_put(A)
*
* NOTE: commit interface required group handle to record group information
*/
/*
* mpp buffer group has two buffer limit mode: normal and limit
*
* normal mode: allows any buffer size and always general new buffer is no unused buffer
* is available.
* This mode normally use with normal flow and is used for table / stream buffer
*
* limit mode : restrict the buffer's size and count in the buffer group. if try to calloc
* buffer with different size or extra count it will fail.
* This mode normally use with commit flow and is used for frame buffer
*/
/*
* NOTE: normal mode is recommanded to work with normal flow, working with limit mode is not.
* limit mode is recommanded to work with commit flow, working with normal mode is not.
*/
typedef enum {
MPP_BUFFER_INTERNAL,
MPP_BUFFER_EXTERNAL,
MPP_BUFFER_MODE_BUTT,
} MppBufferMode;
/*
* the mpp buffer has serval types:
*
* normal : normal malloc buffer for unit test or hardware simulation
* ion : use ion device under Android/Linux, MppBuffer will encapsulte ion file handle
* ext_dma : the DMABUF(DMA buffers) come from the application
* drm : use the drm device interface for memory management
*/
typedef enum {
MPP_BUFFER_TYPE_NORMAL,
MPP_BUFFER_TYPE_ION,
MPP_BUFFER_TYPE_EXT_DMA,
MPP_BUFFER_TYPE_DRM,
MPP_BUFFER_TYPE_DMA_HEAP,
MPP_BUFFER_TYPE_BUTT,
} MppBufferType;
#define MPP_BUFFER_TYPE_MASK 0x0000FFFF
/*
* MPP_BUFFER_FLAGS cooperate with MppBufferType
* 16 high bits of MppBufferType are used in flags
*
* eg:
* DRM CMA buffer : MPP_BUFFER_TYPE_DRM | MPP_BUFFER_FLAGS_CONTIG
* = 0x00010003
* DRM SECURE buffer: MPP_BUFFER_TYPE_DRM | MPP_BUFFER_FLAGS_SECURE
* = 0x00080003
*
* The dma buffer source can also be set by format: flags | type.
* dma buffer source flags:
* MPP_BUFFER_FLAGS_CONTIG means cma
* MPP_BUFFER_FLAGS_CACHABLE means cachable
* MPP_BUFFER_FLAGS_DMA32 means dma32
*
* flags originate from drm_rockchip_gem_mem_type
*/
#define MPP_BUFFER_FLAGS_MASK 0x003f0000 //ROCKCHIP_BO_MASK << 16
#define MPP_BUFFER_FLAGS_CONTIG 0x00010000 //ROCKCHIP_BO_CONTIG << 16
#define MPP_BUFFER_FLAGS_CACHABLE 0x00020000 //ROCKCHIP_BO_CACHABLE << 16
#define MPP_BUFFER_FLAGS_WC 0x00040000 //ROCKCHIP_BO_WC << 16
#define MPP_BUFFER_FLAGS_SECURE 0x00080000 //ROCKCHIP_BO_SECURE << 16
#define MPP_BUFFER_FLAGS_ALLOC_KMAP 0x00100000 //ROCKCHIP_BO_ALLOC_KMAP << 16
#define MPP_BUFFER_FLAGS_DMA32 0x00200000 //ROCKCHIP_BO_DMA32 << 16
/*
* MppBufferInfo variable's meaning is different in different MppBufferType
*
* Common
* index - the buffer index used to track buffer in buffer pool
* size - the buffer size
*
* MPP_BUFFER_TYPE_NORMAL
*
* ptr - virtual address of normal malloced buffer
* fd - unused and set to -1, the allocator would return its
* internal buffer counter number
*
* MPP_BUFFER_TYPE_ION
*
* ptr - virtual address of ion buffer in user space
* hnd - ion handle in user space
* fd - ion buffer file handle for map / unmap
*
*/
typedef struct MppBufferInfo_t {
MppBufferType type;
size_t size;
void *ptr;
void *hnd;
int fd;
int index;
} MppBufferInfo;
#define BUFFER_GROUP_SIZE_DEFAULT (SZ_1M*80)
/*
* mpp_buffer_import_with_tag(MppBufferGroup group, MppBufferInfo *info, MppBuffer *buffer)
*
* 1. group - specified the MppBuffer to be attached to.
* group can be NULL then this buffer will attached to default legecy group
* Default to NULL on mpp_buffer_import case
*
* 2. info - input information for the output MppBuffer
* info can NOT be NULL. It must contain at least one of ptr/fd.
*
* 3. buffer - generated MppBuffer from MppBufferInfo.
* buffer can be NULL then the buffer is commit to group with unused status.
* Otherwise generated buffer will be directly got and ref_count increased.
* Default to NULL on mpp_buffer_commit case
*
* mpp_buffer_commit usage:
*
* Add a external buffer info to group. This buffer will be on unused status.
* Typical usage is on Android. MediaPlayer gralloc Graphic buffer then commit these buffer
* to decoder's buffer group. Then decoder will recycle these buffer and return buffer reference
* to MediaPlayer for display.
*
* mpp_buffer_import usage:
*
* Transfer a external buffer info to MppBuffer but it is not expected to attached to certain
* buffer group. So the group is set to NULL. Then this buffer can be used for MppFrame/MppPacket.
* Typical usage is for image processing. Image processing normally will be a oneshot operation
* It does not need complicated group management. But in other hand mpp still need to know the
* imported buffer is leak or not and trace its usage inside mpp process. So we attach this kind
* of buffer to default misc buffer group for management.
*/
#define mpp_buffer_commit(group, info) \
mpp_buffer_import_with_tag(group, info, NULL, MODULE_TAG, __FUNCTION__)
#define mpp_buffer_import(buffer, info) \
mpp_buffer_import_with_tag(NULL, info, buffer, MODULE_TAG, __FUNCTION__)
#define mpp_buffer_get(group, buffer, size) \
mpp_buffer_get_with_tag(group, buffer, size, MODULE_TAG, __FUNCTION__)
#define mpp_buffer_put(buffer) \
mpp_buffer_put_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_inc_ref(buffer) \
mpp_buffer_inc_ref_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_info_get(buffer, info) \
mpp_buffer_info_get_with_caller(buffer, info, __FUNCTION__)
#define mpp_buffer_read(buffer, offset, data, size) \
mpp_buffer_read_with_caller(buffer, offset, data, size, __FUNCTION__)
#define mpp_buffer_write(buffer, offset, data, size) \
mpp_buffer_write_with_caller(buffer, offset, data, size, __FUNCTION__)
#define mpp_buffer_get_ptr(buffer) \
mpp_buffer_get_ptr_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_get_fd(buffer) \
mpp_buffer_get_fd_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_get_size(buffer) \
mpp_buffer_get_size_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_get_index(buffer) \
mpp_buffer_get_index_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_set_index(buffer, index) \
mpp_buffer_set_index_with_caller(buffer, index, __FUNCTION__)
#define mpp_buffer_get_offset(buffer) \
mpp_buffer_get_offset_with_caller(buffer, __FUNCTION__)
#define mpp_buffer_set_offset(buffer, offset) \
mpp_buffer_set_offset_with_caller(buffer, offset, __FUNCTION__)
#define mpp_buffer_group_get_internal(group, type, ...) \
mpp_buffer_group_get(group, type, MPP_BUFFER_INTERNAL, MODULE_TAG, __FUNCTION__)
#define mpp_buffer_group_get_external(group, type, ...) \
mpp_buffer_group_get(group, type, MPP_BUFFER_EXTERNAL, MODULE_TAG, __FUNCTION__)
#ifdef __cplusplus
extern "C" {
#endif
/*
* MppBuffer interface
* these interface will change value of group and buffer so before calling functions
* parameter need to be checked.
*
* IMPORTANT:
* mpp_buffer_import_with_tag - compounded interface for commit and import
*
*/
MPP_RET mpp_buffer_import_with_tag(MppBufferGroup group, MppBufferInfo *info, MppBuffer *buffer,
const char *tag, const char *caller);
MPP_RET mpp_buffer_get_with_tag(MppBufferGroup group, MppBuffer *buffer, size_t size,
const char *tag, const char *caller);
MPP_RET mpp_buffer_put_with_caller(MppBuffer buffer, const char *caller);
MPP_RET mpp_buffer_inc_ref_with_caller(MppBuffer buffer, const char *caller);
MPP_RET mpp_buffer_info_get_with_caller(MppBuffer buffer, MppBufferInfo *info, const char *caller);
MPP_RET mpp_buffer_read_with_caller(MppBuffer buffer, size_t offset, void *data, size_t size, const char *caller);
MPP_RET mpp_buffer_write_with_caller(MppBuffer buffer, size_t offset, void *data, size_t size, const char *caller);
void *mpp_buffer_get_ptr_with_caller(MppBuffer buffer, const char *caller);
int mpp_buffer_get_fd_with_caller(MppBuffer buffer, const char *caller);
size_t mpp_buffer_get_size_with_caller(MppBuffer buffer, const char *caller);
int mpp_buffer_get_index_with_caller(MppBuffer buffer, const char *caller);
MPP_RET mpp_buffer_set_index_with_caller(MppBuffer buffer, int index, const char *caller);
size_t mpp_buffer_get_offset_with_caller(MppBuffer buffer, const char *caller);
MPP_RET mpp_buffer_set_offset_with_caller(MppBuffer buffer, size_t offset, const char *caller);
MPP_RET mpp_buffer_group_get(MppBufferGroup *group, MppBufferType type, MppBufferMode mode,
const char *tag, const char *caller);
MPP_RET mpp_buffer_group_put(MppBufferGroup group);
MPP_RET mpp_buffer_group_clear(MppBufferGroup group);
RK_S32 mpp_buffer_group_unused(MppBufferGroup group);
size_t mpp_buffer_group_usage(MppBufferGroup group);
MppBufferMode mpp_buffer_group_mode(MppBufferGroup group);
MppBufferType mpp_buffer_group_type(MppBufferGroup group);
/*
* size : 0 - no limit, other - max buffer size
* count : 0 - no limit, other - max buffer count
*/
MPP_RET mpp_buffer_group_limit_config(MppBufferGroup group, size_t size, RK_S32 count);
RK_U32 mpp_buffer_total_now();
RK_U32 mpp_buffer_total_max();
#ifdef __cplusplus
}
#endif
#endif /*__MPP_BUFFER_H__*/

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2021 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_COMPAT_H__
#define __MPP_COMPAT_H__
#include "rk_type.h"
#include "mpp_err.h"
typedef enum MppCompatId_e {
MPP_COMPAT_INC_FBC_BUF_SIZE,
MPP_COMPAT_ENC_ASYNC_INPUT,
MPP_COMPAT_DEC_FBC_HDR_256_ODD,
MPP_COMPAT_BUTT,
} MppCompatId;
typedef enum MppCompatType_e {
MPP_COMPAT_BOOL,
MPP_COMPAT_S32,
MPP_COMPAT_TYPE_BUTT,
} MppCompatType;
typedef struct MppCompat_t MppCompat;
/* external user can only update value_ext to notify mpp to change its behavior */
struct MppCompat_t {
const MppCompatId feature_id;
const MppCompatType feature_type;
const RK_S32 value_mpp;
RK_S32 value_usr;
const char *name;
MppCompat * const next;
};
#ifdef __cplusplus
extern "C" {
#endif
MppCompat *mpp_compat_query(void);
MppCompat *mpp_compat_query_by_id(MppCompatId id);
MPP_RET mpp_compat_update(MppCompat *compat, RK_S32 value);
void mpp_compat_show(void);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_COMPAT_H__*/

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_ERR_H__
#define __MPP_ERR_H__
#define RK_OK 0
#define RK_SUCCESS 0
typedef enum {
MPP_SUCCESS = RK_SUCCESS,
MPP_OK = RK_OK,
MPP_NOK = -1,
MPP_ERR_UNKNOW = -2,
MPP_ERR_NULL_PTR = -3,
MPP_ERR_MALLOC = -4,
MPP_ERR_OPEN_FILE = -5,
MPP_ERR_VALUE = -6,
MPP_ERR_READ_BIT = -7,
MPP_ERR_TIMEOUT = -8,
MPP_ERR_PERM = -9,
MPP_ERR_BASE = -1000,
/* The error in stream processing */
MPP_ERR_LIST_STREAM = MPP_ERR_BASE - 1,
MPP_ERR_INIT = MPP_ERR_BASE - 2,
MPP_ERR_VPU_CODEC_INIT = MPP_ERR_BASE - 3,
MPP_ERR_STREAM = MPP_ERR_BASE - 4,
MPP_ERR_FATAL_THREAD = MPP_ERR_BASE - 5,
MPP_ERR_NOMEM = MPP_ERR_BASE - 6,
MPP_ERR_PROTOL = MPP_ERR_BASE - 7,
MPP_FAIL_SPLIT_FRAME = MPP_ERR_BASE - 8,
MPP_ERR_VPUHW = MPP_ERR_BASE - 9,
MPP_EOS_STREAM_REACHED = MPP_ERR_BASE - 11,
MPP_ERR_BUFFER_FULL = MPP_ERR_BASE - 12,
MPP_ERR_DISPLAY_FULL = MPP_ERR_BASE - 13,
} MPP_RET;
#endif /*__MPP_ERR_H__*/

View File

@@ -0,0 +1,431 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_FRAME_H__
#define __MPP_FRAME_H__
#include "mpp_buffer.h"
#include "mpp_meta.h"
/*
* bit definition for mode flag in MppFrame
*/
/* progressive frame */
#define MPP_FRAME_FLAG_FRAME (0x00000000)
/* top field only */
#define MPP_FRAME_FLAG_TOP_FIELD (0x00000001)
/* bottom field only */
#define MPP_FRAME_FLAG_BOT_FIELD (0x00000002)
/* paired field */
#define MPP_FRAME_FLAG_PAIRED_FIELD (MPP_FRAME_FLAG_TOP_FIELD|MPP_FRAME_FLAG_BOT_FIELD)
/* paired field with field order of top first */
#define MPP_FRAME_FLAG_TOP_FIRST (0x00000004)
/* paired field with field order of bottom first */
#define MPP_FRAME_FLAG_BOT_FIRST (0x00000008)
/* paired field with unknown field order (MBAFF) */
#define MPP_FRAME_FLAG_DEINTERLACED (MPP_FRAME_FLAG_TOP_FIRST|MPP_FRAME_FLAG_BOT_FIRST)
#define MPP_FRAME_FLAG_FIELD_ORDER_MASK (0x0000000C)
// for multiview stream
#define MPP_FRAME_FLAG_VIEW_ID_MASK (0x000000f0)
#define MPP_FRAME_FLAG_IEP_DEI_MASK (0x00000f00)
#define MPP_FRAME_FLAG_IEP_DEI_I2O1 (0x00000100)
#define MPP_FRAME_FLAG_IEP_DEI_I4O2 (0x00000200)
#define MPP_FRAME_FLAG_IEP_DEI_I4O1 (0x00000300)
/*
* MPEG vs JPEG YUV range.
*/
typedef enum {
MPP_FRAME_RANGE_UNSPECIFIED = 0,
MPP_FRAME_RANGE_MPEG = 1, ///< the normal 219*2^(n-8) "MPEG" YUV ranges
MPP_FRAME_RANGE_JPEG = 2, ///< the normal 2^n-1 "JPEG" YUV ranges
MPP_FRAME_RANGE_NB, ///< Not part of ABI
} MppFrameColorRange;
typedef enum {
MPP_FRAME_VIDEO_FMT_COMPONEMT = 0,
MPP_FRAME_VIDEO_FMT_PAL = 1,
MPP_FRAME_VIDEO_FMT_NTSC = 2,
MPP_FRAME_VIDEO_FMT_SECAM = 3,
MPP_FRAME_VIDEO_FMT_MAC = 4,
MPP_FRAME_VIDEO_FMT_UNSPECIFIED = 5,
MPP_FRAME_VIDEO_FMT_RESERVED0 = 6,
MPP_FRAME_VIDEO_FMT_RESERVED1 = 7,
} MppFrameVideoFormat;
/*
* Chromaticity coordinates of the source primaries.
*/
typedef enum {
MPP_FRAME_PRI_RESERVED0 = 0,
MPP_FRAME_PRI_BT709 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
MPP_FRAME_PRI_UNSPECIFIED = 2,
MPP_FRAME_PRI_RESERVED = 3,
MPP_FRAME_PRI_BT470M = 4, ///< also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
MPP_FRAME_PRI_BT470BG = 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
MPP_FRAME_PRI_SMPTE170M = 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC/SMPTE ST 170 (2004)
MPP_FRAME_PRI_SMPTE240M = 7, ///< functionally identical to above/SMPTE ST 240
MPP_FRAME_PRI_FILM = 8, ///< colour filters using Illuminant C
MPP_FRAME_PRI_BT2020 = 9, ///< ITU-R BT2020 / ITU-R BT.2100-2
MPP_FRAME_PRI_SMPTEST428_1 = 10, ///< SMPTE ST 428-1 (CIE 1931 XYZ)
MPP_FRAME_PRI_SMPTE431 = 11, ///< SMPTE ST 431-2 (2011) / DCI P3
MPP_FRAME_PRI_SMPTE432 = 12, ///< SMPTE ST 432-1 (2010) / P3 D65 / Display P3
MPP_FRAME_PRI_JEDEC_P22 = 22, ///< JEDEC P22 phosphors
MPP_FRAME_PRI_NB, ///< Not part of ABI
} MppFrameColorPrimaries;
/*
* Color Transfer Characteristic.
*/
typedef enum {
MPP_FRAME_TRC_RESERVED0 = 0,
MPP_FRAME_TRC_BT709 = 1, ///< also ITU-R BT1361
MPP_FRAME_TRC_UNSPECIFIED = 2,
MPP_FRAME_TRC_RESERVED = 3,
MPP_FRAME_TRC_GAMMA22 = 4, ///< also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
MPP_FRAME_TRC_GAMMA28 = 5, ///< also ITU-R BT470BG
MPP_FRAME_TRC_SMPTE170M = 6, ///< also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
MPP_FRAME_TRC_SMPTE240M = 7,
MPP_FRAME_TRC_LINEAR = 8, ///< "Linear transfer characteristics"
MPP_FRAME_TRC_LOG = 9, ///< "Logarithmic transfer characteristic (100:1 range)"
MPP_FRAME_TRC_LOG_SQRT = 10, ///< "Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
MPP_FRAME_TRC_IEC61966_2_4 = 11, ///< IEC 61966-2-4
MPP_FRAME_TRC_BT1361_ECG = 12, ///< ITU-R BT1361 Extended Colour Gamut
MPP_FRAME_TRC_IEC61966_2_1 = 13, ///< IEC 61966-2-1 (sRGB or sYCC)
MPP_FRAME_TRC_BT2020_10 = 14, ///< ITU-R BT2020 for 10 bit system
MPP_FRAME_TRC_BT2020_12 = 15, ///< ITU-R BT2020 for 12 bit system
MPP_FRAME_TRC_SMPTEST2084 = 16, ///< SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
MPP_FRAME_TRC_SMPTEST428_1 = 17, ///< SMPTE ST 428-1
MPP_FRAME_TRC_ARIB_STD_B67 = 18, ///< ARIB STD-B67, known as "Hybrid log-gamma"
MPP_FRAME_TRC_NB, ///< Not part of ABI
} MppFrameColorTransferCharacteristic;
/*
* YUV colorspace type.
*/
typedef enum {
MPP_FRAME_SPC_RGB = 0, ///< order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
MPP_FRAME_SPC_BT709 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
MPP_FRAME_SPC_UNSPECIFIED = 2,
MPP_FRAME_SPC_RESERVED = 3,
MPP_FRAME_SPC_FCC = 4, ///< FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
MPP_FRAME_SPC_BT470BG = 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
MPP_FRAME_SPC_SMPTE170M = 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
MPP_FRAME_SPC_SMPTE240M = 7,
MPP_FRAME_SPC_YCOCG = 8, ///< Used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
MPP_FRAME_SPC_BT2020_NCL = 9, ///< ITU-R BT2020 non-constant luminance system
MPP_FRAME_SPC_BT2020_CL = 10, ///< ITU-R BT2020 constant luminance system
MPP_FRAME_SPC_SMPTE2085 = 11, ///< SMPTE 2085, Y'D'zD'x
MPP_FRAME_SPC_CHROMA_DERIVED_NCL = 12, ///< Chromaticity-derived non-constant luminance system
MPP_FRAME_SPC_CHROMA_DERIVED_CL = 13, ///< Chromaticity-derived constant luminance system
MPP_FRAME_SPC_ICTCP = 14, ///< ITU-R BT.2100-0, ICtCp
MPP_FRAME_SPC_NB, ///< Not part of ABI
} MppFrameColorSpace;
/*
* Location of chroma samples.
*
* Illustration showing the location of the first (top left) chroma sample of the
* image, the left shows only luma, the right
* shows the location of the chroma sample, the 2 could be imagined to overlay
* each other but are drawn separately due to limitations of ASCII
*
* 1st 2nd 1st 2nd horizontal luma sample positions
* v v v v
* ______ ______
*1st luma line > |X X ... |3 4 X ... X are luma samples,
* | |1 2 1-6 are possible chroma positions
*2nd luma line > |X X ... |5 6 X ... 0 is undefined/unknown position
*/
typedef enum {
MPP_CHROMA_LOC_UNSPECIFIED = 0,
MPP_CHROMA_LOC_LEFT = 1, ///< mpeg2/4 4:2:0, h264 default for 4:2:0
MPP_CHROMA_LOC_CENTER = 2, ///< mpeg1 4:2:0, jpeg 4:2:0, h263 4:2:0
MPP_CHROMA_LOC_TOPLEFT = 3, ///< ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2
MPP_CHROMA_LOC_TOP = 4,
MPP_CHROMA_LOC_BOTTOMLEFT = 5,
MPP_CHROMA_LOC_BOTTOM = 6,
MPP_CHROMA_LOC_NB, ///< Not part of ABI
} MppFrameChromaLocation;
#define MPP_FRAME_FMT_MASK (0x000fffff)
#define MPP_FRAME_FMT_COLOR_MASK (0x000f0000)
#define MPP_FRAME_FMT_YUV (0x00000000)
#define MPP_FRAME_FMT_RGB (0x00010000)
#define MPP_FRAME_FBC_MASK (0x00f00000)
#define MPP_FRAME_FBC_NONE (0x00000000)
#define MPP_FRAME_HDR_MASK (0x0c000000)
#define MPP_FRAME_HDR_NONE (0x00000000)
#define MPP_FRAME_HDR (0x04000000)
/*
* AFBC_V1 is for ISP output.
* It has default payload offset to be calculated * from width and height:
* Payload offset = MPP_ALIGN(MPP_ALIGN(width, 16) * MPP_ALIGN(height, 16) / 16, SZ_4K)
*/
#define MPP_FRAME_FBC_AFBC_V1 (0x00100000)
/*
* AFBC_V2 is for video decoder output.
* It stores payload offset in first 32-bit in header address
* Payload offset is always set to zero.
*/
#define MPP_FRAME_FBC_AFBC_V2 (0x00200000)
#define MPP_FRAME_FMT_LE_MASK (0x01000000)
#define MPP_FRAME_FMT_IS_YUV(fmt) (((fmt & MPP_FRAME_FMT_COLOR_MASK) == MPP_FRAME_FMT_YUV) && \
((fmt & MPP_FRAME_FMT_MASK) < MPP_FMT_YUV_BUTT))
#define MPP_FRAME_FMT_IS_YUV_10BIT(fmt) ((fmt & MPP_FRAME_FMT_MASK) == MPP_FMT_YUV420SP_10BIT || \
(fmt & MPP_FRAME_FMT_MASK) == MPP_FMT_YUV422SP_10BIT)
#define MPP_FRAME_FMT_IS_RGB(fmt) (((fmt & MPP_FRAME_FMT_COLOR_MASK) == MPP_FRAME_FMT_RGB) && \
((fmt & MPP_FRAME_FMT_MASK) < MPP_FMT_RGB_BUTT))
/*
* For MPP_FRAME_FBC_AFBC_V1 the 16byte aligned stride is used.
*/
#define MPP_FRAME_FMT_IS_FBC(fmt) (fmt & MPP_FRAME_FBC_MASK)
#define MPP_FRAME_FMT_IS_HDR(fmt) (fmt & MPP_FRAME_HDR_MASK)
#define MPP_FRAME_FMT_IS_LE(fmt) ((fmt & MPP_FRAME_FMT_LE_MASK) == MPP_FRAME_FMT_LE_MASK)
#define MPP_FRAME_FMT_IS_BE(fmt) ((fmt & MPP_FRAME_FMT_LE_MASK) == 0)
/* mpp color format index definition */
typedef enum {
MPP_FMT_YUV420SP = (MPP_FRAME_FMT_YUV + 0), /* YYYY... UV... (NV12) */
/*
* A rockchip specific pixel format, without gap between pixel aganist
* the P010_10LE/P010_10BE
*/
MPP_FMT_YUV420SP_10BIT = (MPP_FRAME_FMT_YUV + 1),
MPP_FMT_YUV422SP = (MPP_FRAME_FMT_YUV + 2), /* YYYY... UVUV... (NV16) */
MPP_FMT_YUV422SP_10BIT = (MPP_FRAME_FMT_YUV + 3), ///< Not part of ABI
MPP_FMT_YUV420P = (MPP_FRAME_FMT_YUV + 4), /* YYYY... U...V... (I420) */
MPP_FMT_YUV420SP_VU = (MPP_FRAME_FMT_YUV + 5), /* YYYY... VUVUVU... (NV21) */
MPP_FMT_YUV422P = (MPP_FRAME_FMT_YUV + 6), /* YYYY... UU...VV...(422P) */
MPP_FMT_YUV422SP_VU = (MPP_FRAME_FMT_YUV + 7), /* YYYY... VUVUVU... (NV61) */
MPP_FMT_YUV422_YUYV = (MPP_FRAME_FMT_YUV + 8), /* YUYVYUYV... (YUY2) */
MPP_FMT_YUV422_YVYU = (MPP_FRAME_FMT_YUV + 9), /* YVYUYVYU... (YVY2) */
MPP_FMT_YUV422_UYVY = (MPP_FRAME_FMT_YUV + 10), /* UYVYUYVY... (UYVY) */
MPP_FMT_YUV422_VYUY = (MPP_FRAME_FMT_YUV + 11), /* VYUYVYUY... (VYUY) */
MPP_FMT_YUV400 = (MPP_FRAME_FMT_YUV + 12), /* YYYY... */
MPP_FMT_YUV440SP = (MPP_FRAME_FMT_YUV + 13), /* YYYY... UVUV... */
MPP_FMT_YUV411SP = (MPP_FRAME_FMT_YUV + 14), /* YYYY... UV... */
MPP_FMT_YUV444SP = (MPP_FRAME_FMT_YUV + 15), /* YYYY... UVUVUVUV... */
MPP_FMT_YUV444P = (MPP_FRAME_FMT_YUV + 16), /* YYYY... UUUU... VVVV... */
MPP_FMT_YUV_BUTT,
MPP_FMT_RGB565 = (MPP_FRAME_FMT_RGB + 0), /* 16-bit RGB */
MPP_FMT_BGR565 = (MPP_FRAME_FMT_RGB + 1), /* 16-bit RGB */
MPP_FMT_RGB555 = (MPP_FRAME_FMT_RGB + 2), /* 15-bit RGB */
MPP_FMT_BGR555 = (MPP_FRAME_FMT_RGB + 3), /* 15-bit RGB */
MPP_FMT_RGB444 = (MPP_FRAME_FMT_RGB + 4), /* 12-bit RGB */
MPP_FMT_BGR444 = (MPP_FRAME_FMT_RGB + 5), /* 12-bit RGB */
MPP_FMT_RGB888 = (MPP_FRAME_FMT_RGB + 6), /* 24-bit RGB */
MPP_FMT_BGR888 = (MPP_FRAME_FMT_RGB + 7), /* 24-bit RGB */
MPP_FMT_RGB101010 = (MPP_FRAME_FMT_RGB + 8), /* 30-bit RGB */
MPP_FMT_BGR101010 = (MPP_FRAME_FMT_RGB + 9), /* 30-bit RGB */
MPP_FMT_ARGB8888 = (MPP_FRAME_FMT_RGB + 10), /* 32-bit RGB */
MPP_FMT_ABGR8888 = (MPP_FRAME_FMT_RGB + 11), /* 32-bit RGB */
MPP_FMT_BGRA8888 = (MPP_FRAME_FMT_RGB + 12), /* 32-bit RGB */
MPP_FMT_RGBA8888 = (MPP_FRAME_FMT_RGB + 13), /* 32-bit RGB */
MPP_FMT_RGB_BUTT,
MPP_FMT_BUTT,
} MppFrameFormat;
/**
* Rational number (pair of numerator and denominator).
*/
typedef struct MppFrameRational {
RK_S32 num; ///< Numerator
RK_S32 den; ///< Denominator
} MppFrameRational;
typedef struct MppFrameMasteringDisplayMetadata {
RK_U16 display_primaries[3][2];
RK_U16 white_point[2];
RK_U32 max_luminance;
RK_U32 min_luminance;
} MppFrameMasteringDisplayMetadata;
typedef struct MppFrameContentLightMetadata {
RK_U16 MaxCLL;
RK_U16 MaxFALL;
} MppFrameContentLightMetadata;
typedef struct MppFrameHdrDynamicMeta {
RK_U32 hdr_fmt;
RK_U32 size;
RK_U8 data[];
} MppFrameHdrDynamicMeta;
typedef enum MppFrameError {
/* General error not specified */
MPP_FRAME_ERR_UNKNOW = 0x0001,
/* Critical error for decoder not support error */
MPP_FRAME_ERR_UNSUPPORT = 0x0002,
/*
* Fatal error for decoder can not parse a valid frame for hardware.
* the pixel data is all invalid.
*/
MPP_FRAME_ERR_DEC_INVALID = 0x0010,
/*
* Normal error for decoder found hardware error on decoding.
*/
MPP_FRAME_ERR_DEC_HW_ERR = 0x0100,
/*
* Normal error for decoder found missing reference frame on decoding.
*/
MPP_FRAME_ERR_DEC_MISS_REF = 0x0200,
} MppFrameError;
#ifdef __cplusplus
extern "C" {
#endif
/*
* MppFrame interface
*/
MPP_RET mpp_frame_init(MppFrame *frame);
MPP_RET mpp_frame_deinit(MppFrame *frame);
/*
* normal parameter
*
* offset_x
* <-------->
*
* <---------------+ hor_stride +--------------->
*
* +------------------------------------------------------+ ^ ^
* | | | |
* | | | | offset_y
* | | | |
* | +--------------------------------+ ^ | | v
* | | | | | |
* | | | + | +
* | | | |
* | | valid data area | height | ver_stride
* | | | |
* | | | + | +
* | | | | | |
* | +--------------------------------+ v | |
* | | |
* | <----------+ width +---------> | |
* | | |
* +------------------------------------------------------+ v
*
*/
RK_U32 mpp_frame_get_width(const MppFrame frame);
void mpp_frame_set_width(MppFrame frame, RK_U32 width);
RK_U32 mpp_frame_get_height(const MppFrame frame);
void mpp_frame_set_height(MppFrame frame, RK_U32 height);
RK_U32 mpp_frame_get_hor_stride(const MppFrame frame);
void mpp_frame_set_hor_stride(MppFrame frame, RK_U32 hor_stride);
RK_U32 mpp_frame_get_ver_stride(const MppFrame frame);
void mpp_frame_set_ver_stride(MppFrame frame, RK_U32 ver_stride);
void mpp_frame_set_hor_stride_pixel(MppFrame frame, RK_U32 hor_stride_pixel);
RK_U32 mpp_frame_get_hor_stride_pixel(const MppFrame frame);
void mpp_frame_set_fbc_hdr_stride(MppFrame frame, RK_U32 fbc_hdr_stride);
RK_U32 mpp_frame_get_fbc_hdr_stride(const MppFrame frame);
RK_U32 mpp_frame_get_offset_x(const MppFrame frame);
void mpp_frame_set_offset_x(MppFrame frame, RK_U32 offset_x);
RK_U32 mpp_frame_get_offset_y(const MppFrame frame);
void mpp_frame_set_offset_y(MppFrame frame, RK_U32 offset_y);
RK_U32 mpp_frame_get_mode(const MppFrame frame);
void mpp_frame_set_mode(MppFrame frame, RK_U32 mode);
RK_U32 mpp_frame_get_discard(const MppFrame frame);
void mpp_frame_set_discard(MppFrame frame, RK_U32 discard);
RK_U32 mpp_frame_get_viewid(const MppFrame frame);
void mpp_frame_set_viewid(MppFrame frame, RK_U32 viewid);
RK_U32 mpp_frame_get_poc(const MppFrame frame);
void mpp_frame_set_poc(MppFrame frame, RK_U32 poc);
RK_S64 mpp_frame_get_pts(const MppFrame frame);
void mpp_frame_set_pts(MppFrame frame, RK_S64 pts);
RK_S64 mpp_frame_get_dts(const MppFrame frame);
void mpp_frame_set_dts(MppFrame frame, RK_S64 dts);
RK_U32 mpp_frame_get_errinfo(const MppFrame frame);
void mpp_frame_set_errinfo(MppFrame frame, RK_U32 errinfo);
size_t mpp_frame_get_buf_size(const MppFrame frame);
void mpp_frame_set_buf_size(MppFrame frame, size_t buf_size);
void mpp_frame_set_thumbnail_en(MppFrame frame, RK_U32 thumbnail_en);
RK_U32 mpp_frame_get_thumbnail_en(const MppFrame frame);
/*
* flow control parmeter
*/
RK_U32 mpp_frame_get_eos(const MppFrame frame);
void mpp_frame_set_eos(MppFrame frame, RK_U32 eos);
RK_U32 mpp_frame_get_info_change(const MppFrame frame);
void mpp_frame_set_info_change(MppFrame frame, RK_U32 info_change);
/*
* buffer parameter
*/
MppBuffer mpp_frame_get_buffer(const MppFrame frame);
void mpp_frame_set_buffer(MppFrame frame, MppBuffer buffer);
/*
* meta data parameter
*/
RK_S32 mpp_frame_has_meta(const MppFrame frame);
MppMeta mpp_frame_get_meta(const MppFrame frame);
void mpp_frame_set_meta(MppFrame frame, MppMeta meta);
/*
* color related parameter
*/
MppFrameColorRange mpp_frame_get_color_range(const MppFrame frame);
void mpp_frame_set_color_range(MppFrame frame, MppFrameColorRange color_range);
MppFrameColorPrimaries mpp_frame_get_color_primaries(const MppFrame frame);
void mpp_frame_set_color_primaries(MppFrame frame, MppFrameColorPrimaries color_primaries);
MppFrameColorTransferCharacteristic mpp_frame_get_color_trc(const MppFrame frame);
void mpp_frame_set_color_trc(MppFrame frame, MppFrameColorTransferCharacteristic color_trc);
MppFrameColorSpace mpp_frame_get_colorspace(const MppFrame frame);
void mpp_frame_set_colorspace(MppFrame frame, MppFrameColorSpace colorspace);
MppFrameChromaLocation mpp_frame_get_chroma_location(const MppFrame frame);
void mpp_frame_set_chroma_location(MppFrame frame, MppFrameChromaLocation chroma_location);
MppFrameFormat mpp_frame_get_fmt(MppFrame frame);
void mpp_frame_set_fmt(MppFrame frame, MppFrameFormat fmt);
MppFrameRational mpp_frame_get_sar(const MppFrame frame);
void mpp_frame_set_sar(MppFrame frame, MppFrameRational sar);
MppFrameMasteringDisplayMetadata mpp_frame_get_mastering_display(const MppFrame frame);
void mpp_frame_set_mastering_display(MppFrame frame, MppFrameMasteringDisplayMetadata mastering_display);
MppFrameContentLightMetadata mpp_frame_get_content_light(const MppFrame frame);
void mpp_frame_set_content_light(MppFrame frame, MppFrameContentLightMetadata content_light);
MppFrameHdrDynamicMeta* mpp_frame_get_hdr_dynamic_meta(const MppFrame frame);
void mpp_frame_set_hdr_dynamic_meta(MppFrame frame, MppFrameHdrDynamicMeta *vivi_data);
/*
* HDR parameter
*/
#ifdef __cplusplus
}
#endif
#endif /*__MPP_FRAME_H__*/

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2022 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_LOG_H__
#define __MPP_LOG_H__
#include "rk_type.h"
#include "mpp_log_def.h"
/*
* _c function will add condition check
* _f function will add function name to the log
* _cf function will add both function name and condition check
*/
/*
* mpp runtime log system usage:
* mpp_logf is for fatal logging. For use when aborting
* mpp_loge is for error logging. For use with unrecoverable failures.
* mpp_logw is for warning logging. For use with recoverable failures.
* mpp_logi is for informational logging.
* mpp_logd is for debug logging.
* mpp_logv is for verbose logging
*/
#define mpp_logf(fmt, ...) _mpp_log_l(MPP_LOG_FATAL, MODULE_TAG, fmt, NULL, ## __VA_ARGS__)
#define mpp_loge(fmt, ...) _mpp_log_l(MPP_LOG_ERROR, MODULE_TAG, fmt, NULL, ## __VA_ARGS__)
#define mpp_logw(fmt, ...) _mpp_log_l(MPP_LOG_WARN, MODULE_TAG, fmt, NULL, ## __VA_ARGS__)
#define mpp_logi(fmt, ...) _mpp_log_l(MPP_LOG_INFO, MODULE_TAG, fmt, NULL, ## __VA_ARGS__)
#define mpp_logd(fmt, ...) _mpp_log_l(MPP_LOG_DEBUG, MODULE_TAG, fmt, NULL, ## __VA_ARGS__)
#define mpp_logv(fmt, ...) _mpp_log_l(MPP_LOG_VERBOSE, MODULE_TAG, fmt, NULL, ## __VA_ARGS__)
#define mpp_logf_f(fmt, ...) _mpp_log_l(MPP_LOG_FATAL, MODULE_TAG, fmt, __FUNCTION__, ## __VA_ARGS__)
#define mpp_loge_f(fmt, ...) _mpp_log_l(MPP_LOG_ERROR, MODULE_TAG, fmt, __FUNCTION__, ## __VA_ARGS__)
#define mpp_logw_f(fmt, ...) _mpp_log_l(MPP_LOG_WARN, MODULE_TAG, fmt, __FUNCTION__, ## __VA_ARGS__)
#define mpp_logi_f(fmt, ...) _mpp_log_l(MPP_LOG_INFO, MODULE_TAG, fmt, __FUNCTION__, ## __VA_ARGS__)
#define mpp_logd_f(fmt, ...) _mpp_log_l(MPP_LOG_DEBUG, MODULE_TAG, fmt, __FUNCTION__, ## __VA_ARGS__)
#define mpp_logv_f(fmt, ...) _mpp_log_l(MPP_LOG_VERBOSE, MODULE_TAG, fmt, __FUNCTION__, ## __VA_ARGS__)
#define mpp_logf_c(cond, fmt, ...) do { if (cond) mpp_logf(fmt, ## __VA_ARGS__); } while (0)
#define mpp_loge_c(cond, fmt, ...) do { if (cond) mpp_loge(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logw_c(cond, fmt, ...) do { if (cond) mpp_logw(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logi_c(cond, fmt, ...) do { if (cond) mpp_logi(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logd_c(cond, fmt, ...) do { if (cond) mpp_logd(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logv_c(cond, fmt, ...) do { if (cond) mpp_logv(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logf_cf(cond, fmt, ...) do { if (cond) mpp_logf_f(fmt, ## __VA_ARGS__); } while (0)
#define mpp_loge_cf(cond, fmt, ...) do { if (cond) mpp_loge_f(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logw_cf(cond, fmt, ...) do { if (cond) mpp_logw_f(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logi_cf(cond, fmt, ...) do { if (cond) mpp_logi_f(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logd_cf(cond, fmt, ...) do { if (cond) mpp_logd_f(fmt, ## __VA_ARGS__); } while (0)
#define mpp_logv_cf(cond, fmt, ...) do { if (cond) mpp_logv_f(fmt, ## __VA_ARGS__); } while (0)
/*
* mpp runtime log system usage:
* mpp_err is for error status message, it will print for sure.
* mpp_log is for important message like open/close/reset/flush, it will print too.
*/
#define mpp_log(fmt, ...) mpp_logi(fmt, ## __VA_ARGS__)
#define mpp_err(fmt, ...) mpp_loge(fmt, ## __VA_ARGS__)
#define mpp_log_f(fmt, ...) mpp_logi_f(fmt, ## __VA_ARGS__)
#define mpp_err_f(fmt, ...) mpp_loge_f(fmt, ## __VA_ARGS__)
#define mpp_log_c(cond, fmt, ...) do { if (cond) mpp_log(fmt, ## __VA_ARGS__); } while (0)
#define mpp_log_cf(cond, fmt, ...) do { if (cond) mpp_log_f(fmt, ## __VA_ARGS__); } while (0)
#ifdef __cplusplus
extern "C" {
#endif
void _mpp_log_l(int level, const char *tag, const char *fmt, const char *func, ...);
void mpp_set_log_level(int level);
int mpp_get_log_level(void);
/* deprecated function */
void _mpp_log(const char *tag, const char *fmt, const char *func, ...);
void _mpp_err(const char *tag, const char *fmt, const char *func, ...);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_LOG_H__*/

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2022 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_LOG_DEF_H__
#define __MPP_LOG_DEF_H__
#ifdef __cplusplus
extern "C" {
#endif
#define MPP_LOG_UNKNOWN 0 /* internal use only */
#define MPP_LOG_FATAL 1 /* fatal error on aborting */
#define MPP_LOG_ERROR 2 /* error log on unrecoverable failures */
#define MPP_LOG_WARN 3 /* warning log on recoverable failures */
#define MPP_LOG_INFO 4 /* Informational log */
#define MPP_LOG_DEBUG 5 /* Debug log */
#define MPP_LOG_VERBOSE 6 /* Verbose log */
#define MPP_LOG_SILENT 7 /* internal use only */
#ifdef __cplusplus
}
#endif
#endif /*__MPP_LOG_DEF_H__*/

View File

@@ -0,0 +1,179 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_META_H__
#define __MPP_META_H__
#include <stdint.h>
#include "rk_type.h"
#define FOURCC_META(a, b, c, d) ((RK_U32)(a) << 24 | \
((RK_U32)(b) << 16) | \
((RK_U32)(c) << 8) | \
((RK_U32)(d) << 0))
/*
* Mpp Metadata definition
*
* Metadata is for information transmision in mpp.
* Mpp task will contain two meta data:
*
* 1. Data flow metadata
* This metadata contains information of input / output data flow. For example
* A. decoder input side task the input packet must be defined and output frame
* may not be defined. Then decoder will try malloc or use committed buffer to
* complete decoding.
* B. decoder output side task
*
*
* 2. Flow control metadata
*
*/
typedef enum MppMetaDataType_e {
/*
* mpp meta data of data flow
* reference counter will be used for these meta data type
*/
TYPE_FRAME = FOURCC_META('m', 'f', 'r', 'm'),
TYPE_PACKET = FOURCC_META('m', 'p', 'k', 't'),
TYPE_BUFFER = FOURCC_META('m', 'b', 'u', 'f'),
/* mpp meta data of normal data type */
TYPE_S32 = FOURCC_META('s', '3', '2', ' '),
TYPE_S64 = FOURCC_META('s', '6', '4', ' '),
TYPE_PTR = FOURCC_META('p', 't', 'r', ' '),
} MppMetaType;
typedef enum MppMetaKey_e {
/* data flow key */
KEY_INPUT_FRAME = FOURCC_META('i', 'f', 'r', 'm'),
KEY_INPUT_PACKET = FOURCC_META('i', 'p', 'k', 't'),
KEY_OUTPUT_FRAME = FOURCC_META('o', 'f', 'r', 'm'),
KEY_OUTPUT_PACKET = FOURCC_META('o', 'p', 'k', 't'),
/* output motion information for motion detection */
KEY_MOTION_INFO = FOURCC_META('m', 'v', 'i', 'f'),
KEY_HDR_INFO = FOURCC_META('h', 'd', 'r', ' '),
KEY_HDR_META_OFFSET = FOURCC_META('h', 'd', 'r', 'o'),
KEY_HDR_META_SIZE = FOURCC_META('h', 'd', 'r', 'l'),
/* flow control key */
KEY_INPUT_BLOCK = FOURCC_META('i', 'b', 'l', 'k'),
KEY_OUTPUT_BLOCK = FOURCC_META('o', 'b', 'l', 'k'),
KEY_INPUT_IDR_REQ = FOURCC_META('i', 'i', 'd', 'r'), /* input idr frame request flag */
KEY_OUTPUT_INTRA = FOURCC_META('o', 'i', 'd', 'r'), /* output intra frame indicator */
/* mpp_frame / mpp_packet meta data info key */
KEY_TEMPORAL_ID = FOURCC_META('t', 'l', 'i', 'd'),
KEY_LONG_REF_IDX = FOURCC_META('l', 't', 'i', 'd'),
KEY_ENC_AVERAGE_QP = FOURCC_META('a', 'v', 'g', 'q'),
KEY_ROI_DATA = FOURCC_META('r', 'o', 'i', ' '),
KEY_OSD_DATA = FOURCC_META('o', 's', 'd', ' '),
KEY_OSD_DATA2 = FOURCC_META('o', 's', 'd', '2'),
KEY_USER_DATA = FOURCC_META('u', 's', 'r', 'd'),
KEY_USER_DATAS = FOURCC_META('u', 'r', 'd', 's'),
/*
* For vepu580 roi buffer config mode
* The encoder roi structure is so complex that we should provide a buffer
* tunnel for externl user to config encoder hardware by direct sending
* roi data buffer.
* This way can reduce the config parsing and roi buffer data generating
* overhead in mpp.
*/
KEY_ROI_DATA2 = FOURCC_META('r', 'o', 'i', '2'),
/*
* qpmap for rv1109/1126 encoder qpmap config
* Input data is a MppBuffer which contains an array of 16bit Vepu541RoiCfg.
* And each 16bit represents a 16x16 block qp info.
*
* H.264 - 16x16 block qp is arranged in raster order:
* each value is a 16bit data
* 00 01 02 03 04 05 06 07 -> 00 01 02 03 04 05 06 07
* 10 11 12 13 14 15 16 17 10 11 12 13 14 15 16 17
* 20 21 22 23 24 25 26 27 20 21 22 23 24 25 26 27
* 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37
*
* H.265 - 16x16 block qp is reorder to 64x64/32x32 ctu order then 64x64 / 32x32 ctu raster order
* 64x64 ctu
* 00 01 02 03 04 05 06 07 -> 00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33 04 05 06 07 14 15 16 17 24 25 26 27 34 35 36 37
* 10 11 12 13 14 15 16 17
* 20 21 22 23 24 25 26 27
* 30 31 32 33 34 35 36 37
* 32x32 ctu
* 00 01 02 03 04 05 06 07 -> 00 01 10 11 02 03 12 13 04 05 14 15 06 07 16 17
* 10 11 12 13 14 15 16 17 20 21 30 31 22 23 32 33 24 25 34 35 26 27 36 37
* 20 21 22 23 24 25 26 27
* 30 31 32 33 34 35 36 37
*/
KEY_QPMAP0 = FOURCC_META('e', 'q', 'm', '0'),
/* input motion list for smart p rate control */
KEY_MV_LIST = FOURCC_META('m', 'v', 'l', 't'),
/* frame long-term reference frame operation */
KEY_ENC_MARK_LTR = FOURCC_META('m', 'l', 't', 'r'),
KEY_ENC_USE_LTR = FOURCC_META('u', 'l', 't', 'r'),
/* MLVEC specified encoder feature */
KEY_ENC_FRAME_QP = FOURCC_META('f', 'r', 'm', 'q'),
KEY_ENC_BASE_LAYER_PID = FOURCC_META('b', 'p', 'i', 'd'),
/* Thumbnail info for decoder output frame */
KEY_DEC_TBN_EN = FOURCC_META('t', 'b', 'e', 'n'),
KEY_DEC_TBN_Y_OFFSET = FOURCC_META('t', 'b', 'y', 'o'),
KEY_DEC_TBN_UV_OFFSET = FOURCC_META('t', 'b', 'c', 'o'),
} MppMetaKey;
#define mpp_meta_get(meta) mpp_meta_get_with_tag(meta, MODULE_TAG, __FUNCTION__)
#include "mpp_frame.h"
#include "mpp_packet.h"
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_meta_get_with_tag(MppMeta *meta, const char *tag, const char *caller);
MPP_RET mpp_meta_put(MppMeta meta);
RK_S32 mpp_meta_size(MppMeta meta);
MPP_RET mpp_meta_set_s32(MppMeta meta, MppMetaKey key, RK_S32 val);
MPP_RET mpp_meta_set_s64(MppMeta meta, MppMetaKey key, RK_S64 val);
MPP_RET mpp_meta_set_ptr(MppMeta meta, MppMetaKey key, void *val);
MPP_RET mpp_meta_get_s32(MppMeta meta, MppMetaKey key, RK_S32 *val);
MPP_RET mpp_meta_get_s64(MppMeta meta, MppMetaKey key, RK_S64 *val);
MPP_RET mpp_meta_get_ptr(MppMeta meta, MppMetaKey key, void **val);
MPP_RET mpp_meta_set_frame (MppMeta meta, MppMetaKey key, MppFrame frame);
MPP_RET mpp_meta_set_packet(MppMeta meta, MppMetaKey key, MppPacket packet);
MPP_RET mpp_meta_set_buffer(MppMeta meta, MppMetaKey key, MppBuffer buffer);
MPP_RET mpp_meta_get_frame (MppMeta meta, MppMetaKey key, MppFrame *frame);
MPP_RET mpp_meta_get_packet(MppMeta meta, MppMetaKey key, MppPacket *packet);
MPP_RET mpp_meta_get_buffer(MppMeta meta, MppMetaKey key, MppBuffer *buffer);
MPP_RET mpp_meta_get_s32_d(MppMeta meta, MppMetaKey key, RK_S32 *val, RK_S32 def);
MPP_RET mpp_meta_get_s64_d(MppMeta meta, MppMetaKey key, RK_S64 *val, RK_S64 def);
MPP_RET mpp_meta_get_ptr_d(MppMeta meta, MppMetaKey key, void **val, void *def);
MPP_RET mpp_meta_get_frame_d(MppMeta meta, MppMetaKey key, MppFrame *frame, MppFrame def);
MPP_RET mpp_meta_get_packet_d(MppMeta meta, MppMetaKey key, MppPacket *packet, MppPacket def);
MPP_RET mpp_meta_get_buffer_d(MppMeta meta, MppMetaKey key, MppBuffer *buffer, MppBuffer def);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_META_H__*/

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_PACKET_H__
#define __MPP_PACKET_H__
#include "mpp_meta.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* MppPacket interface
*
* mpp_packet_init = mpp_packet_new + mpp_packet_set_data + mpp_packet_set_size
* mpp_packet_copy_init = mpp_packet_init + memcpy
*/
MPP_RET mpp_packet_new(MppPacket *packet);
MPP_RET mpp_packet_init(MppPacket *packet, void *data, size_t size);
MPP_RET mpp_packet_init_with_buffer(MppPacket *packet, MppBuffer buffer);
MPP_RET mpp_packet_copy_init(MppPacket *packet, const MppPacket src);
MPP_RET mpp_packet_deinit(MppPacket *packet);
/*
* data : ( R/W ) start address of the whole packet memory
* size : ( R/W ) total size of the whole packet memory
* pos : ( R/W ) current access position of the whole packet memory, used for buffer read/write
* length : ( R/W ) the rest length from current position to end of buffer
* NOTE: normally length is updated only by set_pos,
* so set length must be used carefully for special usage
*/
void mpp_packet_set_data(MppPacket packet, void *data);
void mpp_packet_set_size(MppPacket packet, size_t size);
void mpp_packet_set_pos(MppPacket packet, void *pos);
void mpp_packet_set_length(MppPacket packet, size_t size);
void* mpp_packet_get_data(const MppPacket packet);
void* mpp_packet_get_pos(const MppPacket packet);
size_t mpp_packet_get_size(const MppPacket packet);
size_t mpp_packet_get_length(const MppPacket packet);
void mpp_packet_set_pts(MppPacket packet, RK_S64 pts);
RK_S64 mpp_packet_get_pts(const MppPacket packet);
void mpp_packet_set_dts(MppPacket packet, RK_S64 dts);
RK_S64 mpp_packet_get_dts(const MppPacket packet);
void mpp_packet_set_flag(MppPacket packet, RK_U32 flag);
RK_U32 mpp_packet_get_flag(const MppPacket packet);
MPP_RET mpp_packet_set_eos(MppPacket packet);
MPP_RET mpp_packet_clr_eos(MppPacket packet);
RK_U32 mpp_packet_get_eos(MppPacket packet);
MPP_RET mpp_packet_set_extra_data(MppPacket packet);
void mpp_packet_set_buffer(MppPacket packet, MppBuffer buffer);
MppBuffer mpp_packet_get_buffer(const MppPacket packet);
/*
* data access interface
*/
MPP_RET mpp_packet_read(MppPacket packet, size_t offset, void *data, size_t size);
MPP_RET mpp_packet_write(MppPacket packet, size_t offset, void *data, size_t size);
/*
* meta data access interface
*/
RK_S32 mpp_packet_has_meta(const MppPacket packet);
MppMeta mpp_packet_get_meta(const MppPacket packet);
/*
* multi packet sequence interface for slice/split encoding/decoding
* partition - the packet is a part of a while image
* soi - Start Of Image
* eoi - End Of Image
*/
RK_U32 mpp_packet_is_partition(const MppPacket packet);
RK_U32 mpp_packet_is_soi(const MppPacket packet);
RK_U32 mpp_packet_is_eoi(const MppPacket packet);
/*
* packet segement pack info for
* segment number - number of segment
* segment info - base address of segment info
*/
typedef struct MppPktSeg_t MppPktSeg;
struct MppPktSeg_t {
RK_S32 index;
RK_S32 type;
RK_U32 offset;
RK_U32 len;
const MppPktSeg *next;
};
RK_U32 mpp_packet_get_segment_nb(const MppPacket packet);
const MppPktSeg *mpp_packet_get_segment_info(const MppPacket packet);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_PACKET_H__*/

View File

@@ -0,0 +1,262 @@
/*
* Copyright 2016 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_RC_API_H__
#define __MPP_RC_API_H__
#include "mpp_err.h"
#include "rk_venc_rc.h"
#include "mpp_rc_defs.h"
/*
* Mpp rate control has three parts:
*
* 1. MPI user config module
* MppEncRcCfg structure is provided to user for overall rate control config
* Mpp will receive MppEncRcCfg from user, check parameter and set it to
* encoder.
*
* 2. Encoder rate control module
* Encoder will implement the rate control strategy required by users
* including CBR, VBR, AVBR and so on.
* This module only implement the target bit calculation behavior and
* quality restriction. And the quality level will be controlled by hal.
*
* 3. Hal rate control module
* Hal will implement the rate control on hardware. Hal will calculate the
* QP parameter for hardware according to the frame level target bit
* specified by the encoder. And the report the real bitrate and quality to
* encoder.
*
* The header defines the communication interfaces and structures used between
* MPI, encoder and hal.
*/
typedef enum RcMode_e {
RC_VBR,
RC_CBR,
RC_FIXQP,
RC_AVBR,
RC_CVBR,
RC_QVBR,
RC_LEARNING,
RC_MODE_BUTT,
} RcMode;
typedef enum GopMode_e {
NORMAL_P,
SMART_P,
} GopMode;
/*
* frame rate parameters have great effect on rate control
*
* fps_in_flex
* 0 - fix input frame rate
* 1 - variable input frame rate
*
* fps_in_num
* input frame rate numerator, if 0 then default 30
*
* fps_in_denorm
* input frame rate denorminator, if 0 then default 1
*
* fps_out_flex
* 0 - fix output frame rate
* 1 - variable output frame rate
*
* fps_out_num
* output frame rate numerator, if 0 then default 30
*
* fps_out_denorm
* output frame rate denorminator, if 0 then default 1
*/
typedef struct RcFpsCfg_t {
RK_S32 fps_in_flex;
RK_S32 fps_in_num;
RK_S32 fps_in_denorm;
RK_S32 fps_out_flex;
RK_S32 fps_out_num;
RK_S32 fps_out_denorm;
} RcFpsCfg;
typedef struct RcSuperframeCfg_t {
MppEncRcSuperFrameMode super_mode;
RK_U32 super_i_thd;
RK_U32 super_p_thd;
MppEncRcPriority rc_priority;
} RcSuperframeCfg;
typedef struct RcDebreathCfg_t {
RK_U32 enable;
RK_U32 strength;
} RcDebreathCfg;
typedef struct RcHierQPCfg_t {
RK_S32 hier_qp_en;
RK_S32 hier_qp_delta[4];
RK_S32 hier_frame_num[4];
} RcHierQPCfg;
/*
* Control parameter from external config
*
* It will be updated on rc/prep/gopref config changed.
*/
typedef struct RcCfg_s {
/* encode image size */
RK_S32 width;
RK_S32 height;
/* Use rc_mode to find different api */
RcMode mode;
RcFpsCfg fps;
GopMode gop_mode;
/* I frame gop len */
RK_S32 igop;
/* visual gop len */
RK_S32 vgop;
/* bitrate parameter */
RK_S32 bps_min;
RK_S32 bps_target;
RK_S32 bps_max;
RK_S32 stats_time;
/* max I frame bit ratio to P frame bit */
RK_S32 max_i_bit_prop;
RK_S32 min_i_bit_prop;
RK_S32 init_ip_ratio;
/* layer bitrate proportion */
RK_S32 layer_bit_prop[4];
/* quality parameter */
RK_S32 init_quality;
RK_S32 max_quality;
RK_S32 min_quality;
RK_S32 max_i_quality;
RK_S32 min_i_quality;
RK_S32 i_quality_delta;
RK_S32 vi_quality_delta;
RK_S32 fqp_min_i;
RK_S32 fqp_min_p;
RK_S32 fqp_max_i;
RK_S32 fqp_max_p;
/* layer quality proportion */
RK_S32 layer_quality_delta[4];
/* reencode parameter */
RK_S32 max_reencode_times;
/* still / motion desision parameter */
RK_S32 min_still_prop;
RK_S32 max_still_quality;
/*
* vbr parameter
*
* vbr_hi_prop - high proportion bitrate for reduce quality
* vbr_lo_prop - low proportion bitrate for increase quality
*/
RK_S32 vbr_hi_prop;
RK_S32 vbr_lo_prop;
MppEncRcDropFrmMode drop_mode;
RK_U32 drop_thd;
RK_U32 drop_gap;
RcSuperframeCfg super_cfg;
RcDebreathCfg debreath_cfg;
RcHierQPCfg hier_qp_cfg;
RK_U32 refresh_len;
RK_S32 scene_mode;
} RcCfg;
/*
* Different rate control strategy will be implemented by different API config
*/
typedef struct RcImplApi_t {
char *name;
MppCodingType type;
RK_U32 ctx_size;
MPP_RET (*init)(void *ctx, RcCfg *cfg);
MPP_RET (*deinit)(void *ctx);
MPP_RET (*check_drop)(void *ctx, EncRcTask *task);
MPP_RET (*check_reenc)(void *ctx, EncRcTask *task);
/*
* frm_start - frame level rate control frm_start.
* The EncRcTaskInfo will be output to hal for hardware to implement.
* frm_end - frame level rate control frm_end.
* The EncRcTaskInfo is returned for real quality and bitrate.
*/
MPP_RET (*frm_start)(void *ctx, EncRcTask *task);
MPP_RET (*frm_end)(void *ctx, EncRcTask *task);
/*
* hal_start - hardware level rate control start.
* The EncRcTaskInfo will be output to hal for hardware to implement.
* hal_end - hardware level rate control end.
* The EncRcTaskInfo is returned for real quality and bitrate.
*/
MPP_RET (*hal_start)(void *ctx, EncRcTask *task);
MPP_RET (*hal_end)(void *ctx, EncRcTask *task);
} RcImplApi;
/*
* structures for RC API register and query
*/
typedef struct RcApiBrief_t {
const char *name;
MppCodingType type;
} RcApiBrief;
typedef struct RcApiQueryAll_t {
/* input param for query */
RcApiBrief *brief;
RK_S32 max_count;
/* output query count */
RK_S32 count;
} RcApiQueryAll;
typedef struct RcApiQueryType_t {
/* input param for query */
RcApiBrief *brief;
RK_S32 max_count;
MppCodingType type;
/* output query count */
RK_S32 count;
} RcApiQueryType;
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET rc_api_add(const RcImplApi *api);
MPP_RET rc_brief_get_all(RcApiQueryAll *query);
MPP_RET rc_brief_get_by_type(RcApiQueryType *query);
#ifdef __cplusplus
}
#endif
#endif /* __MPP_RC_API_H__ */

View File

@@ -0,0 +1,212 @@
/*
* Copyright 2016 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_RC_DEFS_H__
#define __MPP_RC_DEFS_H__
#include "rk_venc_ref.h"
#define MAX_CPB_REFS (8)
typedef enum EncFrmType_e {
INTER_P_FRAME = 0,
INTER_B_FRAME = 1,
INTRA_FRAME = 2,
INTER_VI_FRAME = 3,
INTRA_RFH_FRAME = 4,
} EncFrmType;
/*
* EncFrmStatus controls record the encoding frame status and also control
* work flow of encoder. It is the communicat channel between encoder implement
* module, rate control module and hardware module.
*
* bit 0 ~ 31 frame status
* 0 ~ 15 current frame status
* 16 ~ 31 reference frame status
* bit 32 ~ 63 encoding flow control
*/
typedef union EncFrmStatus_u {
struct {
/*
* bit 0 ~ 31 frame status
*/
/* status flag */
RK_U32 valid : 1;
/*
* 0 - write the reconstructed frame pixel to memory
* 1 - do not write the reconstructed frame pixel to memory
*/
RK_U32 non_recn : 1;
/*
* 0 - normal frame and normal dpb management
* 1 - save recon frame as first pass extra frame. Used in two pass mode
*/
RK_U32 save_pass1 : 1;
/*
* 0 - use normal input source frame as input
* 1 - use the previously stored first pass recon frame as input frame
*/
RK_U32 use_pass1 : 1;
/* reference status flag */
/*
* 0 - inter frame
* 1 - intra frame
*/
RK_U32 is_intra : 1;
/*
* Valid when is_intra is true
* 0 - normal intra frame
* 1 - IDR frame
*/
RK_U32 is_idr : 1;
/*
* 0 - mark as reference frame
* 1 - mark as non-refernce frame
*/
RK_U32 is_non_ref : 1;
/*
* Valid when is_non_ref is false
* 0 - mark as short-term reference frame
* 1 - mark as long-term refernce frame
*/
RK_U32 is_lt_ref : 1;
/* bit 8 - 15 */
RK_U32 lt_idx : 4;
RK_U32 temporal_id : 4;
/* distance between current frame and reference frame */
MppEncRefMode ref_mode : 6;
RK_S32 ref_arg : 8;
RK_S32 ref_dist : 2;
/*
* bit 32 ~ 63 encoder flow control flags
*/
/*
* 0 - normal frame encoding
* 1 - current frame will be dropped
*/
RK_U32 drop : 1;
/*
* 0 - rate control module does not change frame type parameter
* 1 - rate control module changes frame type parameter reencode is needed
* to reprocess the dpb process. Also this means dpb module will follow
* the frame status parameter provided by rate control module.
*/
RK_U32 re_dpb_proc : 1;
/*
* 0 - current frame encoding is in normal flow
* 1 - current frame encoding is in reencode flow
*/
RK_U32 reencode : 1;
/*
* When true current frame size is super large then the frame should be reencoded.
*/
RK_U32 super_frame : 1;
/*
* When true currnet frame is force to encoded as software skip frame
*/
RK_U32 force_pskip : 1;
/*
* Current frame is intra refresh frame
*/
RK_U32 is_i_refresh : 1;
/*
* Current frame needs add recovery point prefix
*/
RK_U32 is_i_recovery : 1;
RK_U32 reserved1 : 1;
/* reencode times */
RK_U32 reencode_times : 8;
/* sequential index for each frame */
RK_U32 seq_idx : 16;
};
RK_U64 val;
} EncFrmStatus;
typedef struct EncCpbStatus_t {
RK_S32 seq_idx;
EncFrmStatus curr;
EncFrmStatus refr;
/* initial cpb status for current frame encoding */
EncFrmStatus init[MAX_CPB_REFS];
/* final cpb status after current frame encoding */
EncFrmStatus final[MAX_CPB_REFS];
} EncCpbStatus;
#define ENC_RC_FORCE_QP (0x00000001)
typedef struct EncRcForceCfg_t {
RK_U32 force_flag;
RK_S32 force_qp;
RK_U32 reserve[6];
} EncRcForceCfg;
/*
* communication channel between rc / hal / hardware
*
* rc -> hal bit_target / bit_max / bit_min
* hal -> hw quality_target / quality_max / quality_min
* hw -> rc / hal bit_real / quality_real / madi / madp
*/
typedef struct EncRcCommonInfo_t {
EncFrmType frame_type;
/* rc to hal */
RK_S32 bit_target;
RK_S32 bit_max;
RK_S32 bit_min;
RK_S32 quality_target;
RK_S32 quality_max;
RK_S32 quality_min;
/* rc from hardware */
RK_S32 bit_real;
RK_S32 quality_real;
RK_S32 madi;
RK_S32 madp;
RK_U32 iblk4_prop; // scale 256
RK_S32 reserve[15];
} EncRcTaskInfo;
typedef struct EncRcTask_s {
EncCpbStatus cpb;
EncFrmStatus frm;
EncRcTaskInfo info;
EncRcForceCfg force;
MppFrame frame;
} EncRcTask;
#endif /* __MPP_RC_DEFS_H__ */

View File

@@ -0,0 +1,237 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_TASK_H__
#define __MPP_TASK_H__
#include "mpp_meta.h"
/*
* Advanced task flow
* Advanced task flow introduces three concepts: port, task and item
*
* Port is from OpenMAX
* Port has two type: input port and output port which are all for data transaction.
* Port work like a queue. task will be dequeue from or enqueue to one port.
* On input side user will dequeue task from input port, setup task and enqueue task
* back to input port.
* On output side user will dequeue task from output port, get the information from
* and then enqueue task back to output port.
*
* Task indicates one transaction on the port.
* Task has two working mode: async mode and sync mode
* If mpp is work in sync mode on task enqueue function return the task has been done
* If mpp is work in async mode on task enqueue function return the task is just put
* on the task queue for process.
* Task can carry different items. Task just like a container of items
*
* Item indicates MppPacket or MppFrame which is contained in one task
*/
/*
* One mpp task queue has two ports: input and output
*
* The whole picture is:
* Top layer mpp has two ports: mpp_input_port and mpp_output_port
* But internally these two ports belongs to two task queue.
* The mpp_input_port is the mpp_input_task_queue's input port.
* The mpp_output_port is the mpp_output_task_queue's output port.
*
* Each port uses its task queue to communication
*/
typedef enum {
MPP_PORT_INPUT,
MPP_PORT_OUTPUT,
MPP_PORT_BUTT,
} MppPortType;
/*
* Advance task work flow mode:
******************************************************************************
* 1. async mode (default_val)
*
* mpp_init(type, coding, MPP_WORK_ASYNC)
*
* input thread
* a - poll(input)
* b - dequeue(input, *task)
* c - task_set_item(packet/frame)
* d - enqueue(input, task) // when enqueue return the task is not done yet
*
* output thread
* a - poll(output)
* b - dequeue(output, *task)
* c - task_get_item(frame/packet)
* d - enqueue(output, task)
******************************************************************************
* 2. sync mode
*
* mpp_init(type, coding, MPP_WORK_SYNC)
*
* a - poll(input)
* b - dequeue(input, *task)
* c - task_set_item(packet/frame)
* d - enqueue(task) // when enqueue return the task is finished
******************************************************************************
*/
typedef enum {
MPP_TASK_ASYNC,
MPP_TASK_SYNC,
MPP_TASK_WORK_MODE_BUTT,
} MppTaskWorkMode;
/*
* Mpp port poll type
*
* MPP_POLL_BLOCK - for block poll
* MPP_POLL_NON_BLOCK - for non-block poll
* small than MPP_POLL_MAX - for poll with timeout in ms
* small than MPP_POLL_BUTT or larger than MPP_POLL_MAX is invalid value
*/
typedef enum {
MPP_POLL_BUTT = -2,
MPP_POLL_BLOCK = -1,
MPP_POLL_NON_BLOCK = 0,
MPP_POLL_MAX = 8000,
} MppPollType;
/*
* Mpp timeout define
* MPP_TIMEOUT_BLOCK - for block poll
* MPP_TIMEOUT_NON_BLOCK - for non-block poll
* small than MPP_TIMEOUT_MAX - for poll with timeout in ms
* small than MPP_TIMEOUT_BUTT or larger than MPP_TIMEOUT_MAX is invalid value
*/
#define MPP_TIMEOUT_BUTT (-2L)
#define MPP_TIMEOUT_BLOCK (-1L)
#define MPP_TIMEOUT_NON_BLOCK (0L)
#define MPP_TIMEOUT_MAX (8000L)
/*
* MppTask is descriptor of a task which send to mpp for process
* mpp can support different type of work mode, for example:
*
* decoder:
*
* 1. typical decoder mode:
* input - MppPacket (normal cpu buffer, need cpu copy)
* output - MppFrame (ion/drm buffer in external/internal mode)
* 2. secure decoder mode:
* input - MppPacket (externel ion/drm buffer, cpu can not access)
* output - MppFrame (ion/drm buffer in external/internal mode, cpu can not access)
*
* interface usage:
*
* typical flow
* input side:
* task_dequeue(ctx, PORT_INPUT, &task);
* task_put_item(task, MODE_INPUT, packet)
* task_enqueue(ctx, PORT_INPUT, task);
* output side:
* task_dequeue(ctx, PORT_OUTPUT, &task);
* task_get_item(task, MODE_OUTPUT, &frame)
* task_enqueue(ctx, PORT_OUTPUT, task);
*
* secure flow
* input side:
* task_dequeue(ctx, PORT_INPUT, &task);
* task_put_item(task, MODE_INPUT, packet)
* task_put_item(task, MODE_OUTPUT, frame) // buffer will be specified here
* task_enqueue(ctx, PORT_INPUT, task);
* output side:
* task_dequeue(ctx, PORT_OUTPUT, &task);
* task_get_item(task, MODE_OUTPUT, &frame)
* task_enqueue(ctx, PORT_OUTPUT, task);
*
* encoder:
*
* 1. typical encoder mode:
* input - MppFrame (ion/drm buffer in external mode)
* output - MppPacket (normal cpu buffer, need cpu copy)
* 2. user input encoder mode:
* input - MppFrame (normal cpu buffer, need to build hardware table for this buffer)
* output - MppPacket (normal cpu buffer, need cpu copy)
* 3. secure encoder mode:
* input - MppFrame (ion/drm buffer in external mode, cpu can not access)
* output - MppPacket (externel ion/drm buffer, cpu can not access)
*
* typical / user input flow
* input side:
* task_dequeue(ctx, PORT_INPUT, &task);
* task_put_item(task, MODE_INPUT, frame)
* task_enqueue(ctx, PORT_INPUT, task);
* output side:
* task_dequeue(ctx, PORT_OUTPUT, &task);
* task_get_item(task, MODE_OUTPUT, &packet)
* task_enqueue(ctx, PORT_OUTPUT, task);
*
* secure flow
* input side:
* task_dequeue(ctx, PORT_INPUT, &task);
* task_put_item(task, MODE_OUTPUT, packet) // buffer will be specified here
* task_put_item(task, MODE_INPUT, frame)
* task_enqueue(ctx, PORT_INPUT, task);
* output side:
* task_dequeue(ctx, PORT_OUTPUT, &task);
* task_get_item(task, MODE_OUTPUT, &packet)
* task_get_item(task, MODE_OUTPUT, &frame)
* task_enqueue(ctx, PORT_OUTPUT, task);
*
* NOTE: this flow can specify the output frame. User will setup both intput frame and output packet
* buffer at the input side. Then at output side when user gets a finished task user can get the output
* packet and corresponding released input frame.
*
* image processing
*
* 1. typical image process mode:
* input - MppFrame (ion/drm buffer in external mode)
* output - MppFrame (ion/drm buffer in external mode)
*
* typical / user input flow
* input side:
* task_dequeue(ctx, PORT_INPUT, &task);
* task_put_item(task, MODE_INPUT, frame)
* task_enqueue(ctx, PORT_INPUT, task);
* output side:
* task_dequeue(ctx, PORT_OUTPUT, &task);
* task_get_item(task, MODE_OUTPUT, &frame)
* task_enqueue(ctx, PORT_OUTPUT, task);
*/
/* NOTE: use index rather then handle to descripbe task */
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_task_meta_set_s32(MppTask task, MppMetaKey key, RK_S32 val);
MPP_RET mpp_task_meta_set_s64(MppTask task, MppMetaKey key, RK_S64 val);
MPP_RET mpp_task_meta_set_ptr(MppTask task, MppMetaKey key, void *val);
MPP_RET mpp_task_meta_set_frame (MppTask task, MppMetaKey key, MppFrame frame);
MPP_RET mpp_task_meta_set_packet(MppTask task, MppMetaKey key, MppPacket packet);
MPP_RET mpp_task_meta_set_buffer(MppTask task, MppMetaKey key, MppBuffer buffer);
MPP_RET mpp_task_meta_get_s32(MppTask task, MppMetaKey key, RK_S32 *val, RK_S32 default_val);
MPP_RET mpp_task_meta_get_s64(MppTask task, MppMetaKey key, RK_S64 *val, RK_S64 default_val);
MPP_RET mpp_task_meta_get_ptr(MppTask task, MppMetaKey key, void **val, void *default_val);
MPP_RET mpp_task_meta_get_frame (MppTask task, MppMetaKey key, MppFrame *frame);
MPP_RET mpp_task_meta_get_packet(MppTask task, MppMetaKey key, MppPacket *packet);
MPP_RET mpp_task_meta_get_buffer(MppTask task, MppMetaKey key, MppBuffer *buffer);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_QUEUE_H__*/

View File

@@ -0,0 +1,118 @@
/*
* Copyright 2022 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_HDR_META_COM_H__
#define __RK_HDR_META_COM_H__
#include "rk_type.h"
typedef enum HdrCodecType_e {
HDR_AVS2 = 0,
HDR_HEVC = 1,
HDR_H264 = 2,
HDR_AV1 = 3,
HDR_CODEC_BUT,
} HdrCodecType;
typedef enum HdrFormat_e {
HDR_NONE = 0,
HDR10 = 1,
HLG = 2,
// RESERVED3 = 3, //reserved for more future static hdr format
// RESERVED4 = 4, //reserved for more future static hdr format
HDRVIVID = 5,
// RESERVED6 = 6, //reserved for hdr vivid
// RESERVED7 = 7, //reserved for hdr vivid
HDR10PLUS = 8,
// RESERVED9 = 9, //reserved for hdr10+
// RESERVED10 = 10,//reserved for hdr10+
DOLBY = 11,
// RESERVED12 = 12, //reserved for other dynamic hdr format
// RESERVED13 = 13, //reserved for other dynamic hdr format
HDR_FORMAT_MAX,
} HdrFormat;
typedef enum HdrPayloadFormat_e {
STATIC = 0,
DYNAMIC = 1,
HDR_PAYLOAD_FORMAT_MAX,
} HdrPayloadFormat;
typedef struct HdrStaticMeta_t {
RK_U32 color_space;
RK_U32 color_primaries;
RK_U32 color_trc;
RK_U32 red_x;
RK_U32 red_y;
RK_U32 green_x;
RK_U32 green_y;
RK_U32 blue_x;
RK_U32 blue_y;
RK_U32 white_point_x;
RK_U32 white_point_y;
RK_U32 min_luminance;
RK_U32 max_luminance;
RK_U32 max_cll;
RK_U32 max_fall;
RK_U32 reserved[4];
} HdrStaticMeta;
/*
* HDR metadata format from codec
*
* +----------+
* | header1 |
* +----------+
* | |
* | payload |
* | |
* +----------+
* | header2 |
* +----------+
* | |
* | payload |
* | |
* +----------+
* | header3 |
* +----------+
* | |
* | payload |
* | |
* +----------+
*/
typedef struct RkMetaHdrHeader_t {
/* For transmission */
RK_U16 magic; /* magic word for checking overwrite error */
RK_U16 size; /* total header+payload length including header */
RK_U16 message_total; /* total message count in current transmission */
RK_U16 message_index; /* current message index in the transmission */
/* For payload identification */
RK_U16 version; /* payload structure version */
RK_U16 hdr_format; /* HDR protocol: HDR10, HLG, Dolby, HDRVivid ... */
RK_U16 hdr_payload_type; /* HDR data type: static data, dynamic data ... */
RK_U16 video_format; /* video format: H.264, H.265, AVS2 ... */
/* For extenstion usage */
RK_U32 reserve[4];
/* payload data aligned to 32bits */
RK_U32 payload[];
} RkMetaHdrHeader;
void fill_hdr_meta_to_frame(MppFrame frame, HdrCodecType codec_type);
#endif

View File

@@ -0,0 +1,274 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_MPI_H__
#define __RK_MPI_H__
/**
* @addtogroup rk_mpi
* @brief Rockchip Media Process Interface
* @details Media Process Platform(MPP) provides application programming
* interface for the application layer, by which applications can
* call hardware encode and decode. Current MPP fully supports
* chipset RK3288/RK3228/RK3229/RK3399/RK3328/RV1108. Old chipset
* like RK29xx/RK30xx/RK31XX/RK3368 is partly supported due to lack
* of some hardware register generation module.
*/
#include "rk_mpi_cmd.h"
#include "mpp_task.h"
/**
* @ingroup rk_mpi
* @brief MPP main work function set
* @details all api function are seperated into two sets: data io api set
* and control api set
*
* (1). the data api set is for data input/output flow including:
*
* (1.1) simple data api set:
*
* decode : both send video stream packet to decoder and get video frame from
* decoder at the same time.
*
* encode : both send video frame to encoder and get encoded video stream from
* encoder at the same time.
*
* decode_put_packet: send video stream packet to decoder only, async interface
*
* decode_get_frame : get video frame from decoder only, async interface
*
* encode_put_frame : send video frame to encoder only, async interface
*
* encode_get_packet: get encoded video packet from encoder only, async interface
*
* (1.2) advanced task api set:
*
* poll : poll port for dequeue
*
* dequeue : pop a task from mpp task queue
*
* enqueue : push a task to mpp task queue
*
* (2). the control api set is for mpp context control including:
*
* control : similiar to ioctl in kernel driver, setup or get mpp internal parameter
*
* reset : clear all data in mpp context, discard all packet and frame,
* reset all components to initialized status
*/
typedef struct MppApi_t {
/**
* @brief size of struct MppApi
*/
RK_U32 size;
/**
* @brief mpp api version, generated by Git
*/
RK_U32 version;
// simple data flow interface
/**
* @brief both send video stream packet to decoder and get video frame from
* decoder at the same time
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] packet The input video stream, its usage can refer mpp_packet.h.
* @param[out] frame The output picture, its usage can refer mpp_frame.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*decode)(MppCtx ctx, MppPacket packet, MppFrame *frame);
/**
* @brief send video stream packet to decoder only, async interface
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] packet The input video stream, its usage can refer mpp_packet.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*decode_put_packet)(MppCtx ctx, MppPacket packet);
/**
* @brief get video frame from decoder only, async interface
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[out] frame The output picture, its usage can refer mpp_frame.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*decode_get_frame)(MppCtx ctx, MppFrame *frame);
/**
* @brief both send video frame to encoder and get encoded video stream from
* encoder at the same time
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] frame The input video data, its usage can refer mpp_frame.h.
* @param[out] packet The output compressed data, its usage can refer mpp_packet.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*encode)(MppCtx ctx, MppFrame frame, MppPacket *packet);
/**
* @brief send video frame to encoder only, async interface
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] frame The input video data, its usage can refer mpp_frame.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*encode_put_frame)(MppCtx ctx, MppFrame frame);
/**
* @brief get encoded video packet from encoder only, async interface
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[out] packet The output compressed data, its usage can refer mpp_packet.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*encode_get_packet)(MppCtx ctx, MppPacket *packet);
/**
* @brief ISP interface, will be supported in the future.
*/
MPP_RET (*isp)(MppCtx ctx, MppFrame dst, MppFrame src);
/**
* @brief ISP interface, will be supported in the future.
*/
MPP_RET (*isp_put_frame)(MppCtx ctx, MppFrame frame);
/**
* @brief ISP interface, will be supported in the future.
*/
MPP_RET (*isp_get_frame)(MppCtx ctx, MppFrame *frame);
// advance data flow interface
/**
* @brief poll port for dequeue
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] type input port or output port which are both for data transaction
* @param[in] timeout mpp poll type, its usage can refer mpp_task.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*poll)(MppCtx ctx, MppPortType type, MppPollType timeout);
/**
* @brief dequeue MppTask, pop a task from mpp task queue
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] type input port or output port which are both for data transaction
* @param[out] task MppTask popped from mpp task queue, its usage can refer mpp_task.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*dequeue)(MppCtx ctx, MppPortType type, MppTask *task);
/**
* @brief enqueue MppTask, push a task to mpp task queue
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] type input port or output port which are both for data transaction
* @param[in] task MppTask which is sent to mpp for process, its usage can refer mpp_task.h.
* @return 0 and positive for success, negative for failure. The return
* value is an error code. For details, please refer mpp_err.h.
*/
MPP_RET (*enqueue)(MppCtx ctx, MppPortType type, MppTask task);
// control interface
/**
* @brief discard all packet and frame, reset all component,
* for both decoder and encoder
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @return 0 for success, others for failure. The return value is an
* error code. For details, please refer mpp_err.h.
*/
MPP_RET (*reset)(MppCtx ctx);
/**
* @brief control function for mpp property setting
* @param[in] ctx The context of mpp, created by mpp_create() and initiated
* by mpp_init().
* @param[in] cmd The mpi command, its definition can refer rk_mpi_cmd.h.
* @param[in,out] param The mpi command parameter
* @return 0 for success, others for failure. The return value is an
* error code. For details, please refer mpp_err.h.
*/
MPP_RET (*control)(MppCtx ctx, MpiCmd cmd, MppParam param);
/**
* @brief The reserved segment, may be used in the future
*/
RK_U32 reserv[16];
} MppApi;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @ingroup rk_mpi
* @brief Create empty context structure and mpi function pointers.
* Use functions in MppApi to access mpp services.
* @param[in,out] ctx pointer of the mpp context, refer to MpiImpl_t.
* @param[in,out] mpi pointer of mpi function, refer to MppApi.
* @return 0 for success, others for failure. The return value is an
* error code. For details, please refer mpp_err.h.
* @note This interface creates base flow context, all function calls
* are based on it.
*/
MPP_RET mpp_create(MppCtx *ctx, MppApi **mpi);
/**
* @ingroup rk_mpi
* @brief Call after mpp_create to setup mpp type and video format.
* This function will call internal context init function.
* @param[in] ctx The context of mpp, created by mpp_create().
* @param[in] type specify decoder or encoder, refer to MppCtxType.
* @param[in] coding specify video compression coding, refer to MppCodingType.
* @return 0 for success, others for failure. The return value is an
* error code. For details, please refer mpp_err.h.
*/
MPP_RET mpp_init(MppCtx ctx, MppCtxType type, MppCodingType coding);
/**
* @ingroup rk_mpi
* @brief Destroy mpp context and free both context and mpi structure,
* it matches with mpp_init().
* @param[in] ctx The context of mpp, created by mpp_create().
* @return 0 for success, others for failure. The return value is an
* error code. For details, please refer mpp_err.h.
*/
MPP_RET mpp_destroy(MppCtx ctx);
/**
* @ingroup rk_mpi
* @brief judge given format is supported or not by MPP.
* @param[in] type specify decoder or encoder, refer to MppCtxType.
* @param[in] coding specify video compression coding, refer to MppCodingType.
* @return 0 for support, -1 for unsupported.
*/
MPP_RET mpp_check_support_format(MppCtxType type, MppCodingType coding);
/**
* @ingroup rk_mpi
* @brief List all formats supported by MPP
* @param NULL no need to input parameter
* @return No return value. This function just prints format information supported
* by MPP on standard output.
*/
void mpp_show_support_format(void);
void mpp_show_color_format(void);
#ifdef __cplusplus
}
#endif
#endif /*__RK_MPI_H__*/

View File

@@ -0,0 +1,209 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_MPI_CMD_H__
#define __RK_MPI_CMD_H__
/*
* Command id bit usage is defined as follows:
* bit 20 - 23 - module id
* bit 16 - 19 - contex id
* bit 0 - 15 - command id
*/
#define CMD_MODULE_ID_MASK (0x00F00000)
#define CMD_MODULE_OSAL (0x00100000)
#define CMD_MODULE_MPP (0x00200000)
#define CMD_MODULE_CODEC (0x00300000)
#define CMD_MODULE_HAL (0x00400000)
#define CMD_CTX_ID_MASK (0x000F0000)
#define CMD_CTX_ID_DEC (0x00010000)
#define CMD_CTX_ID_ENC (0x00020000)
#define CMD_CTX_ID_ISP (0x00030000)
/* separate encoder / decoder control command to different segment */
#define CMD_CFG_ID_MASK (0x0000FF00)
/* mpp status control command */
#define CMD_STATE_OPS (0x00000100)
/* decoder control command */
#define CMD_DEC_CFG_ALL (0x00000000)
#define CMD_DEC_QUERY (0x00000100)
#define CMD_DEC_CFG (0x00000200)
/* encoder control command */
#define CMD_ENC_CFG_ALL (0x00000000)
#define CMD_ENC_QUERY (0x00000100)
#define CMD_ENC_CFG_RC_API (0x00000200)
#define CMD_ENC_CFG_MISC (0x00008000)
#define CMD_ENC_CFG_SPLIT (0x00008100)
#define CMD_ENC_CFG_REF (0x00008200)
#define CMD_ENC_CFG_ROI (0x00008300)
#define CMD_ENC_CFG_OSD (0x00008400)
typedef enum {
MPP_OSAL_CMD_BASE = CMD_MODULE_OSAL,
MPP_OSAL_CMD_END,
MPP_CMD_BASE = CMD_MODULE_MPP,
MPP_ENABLE_DEINTERLACE,
MPP_SET_INPUT_BLOCK, /* deprecated */
MPP_SET_INTPUT_BLOCK_TIMEOUT, /* deprecated */
MPP_SET_OUTPUT_BLOCK, /* deprecated */
MPP_SET_OUTPUT_BLOCK_TIMEOUT, /* deprecated */
/*
* timeout setup, refer to MPP_TIMEOUT_XXX
* zero - non block
* negative - block with no timeout
* positive - timeout in milisecond
*/
MPP_SET_INPUT_TIMEOUT, /* parameter type RK_S64 */
MPP_SET_OUTPUT_TIMEOUT, /* parameter type RK_S64 */
MPP_SET_DISABLE_THREAD, /* MPP no thread mode and use external thread to decode */
MPP_STATE_CMD_BASE = CMD_MODULE_MPP | CMD_STATE_OPS,
MPP_START,
MPP_STOP,
MPP_PAUSE,
MPP_RESUME,
MPP_CMD_END,
MPP_CODEC_CMD_BASE = CMD_MODULE_CODEC,
MPP_CODEC_GET_FRAME_INFO,
MPP_CODEC_CMD_END,
MPP_DEC_CMD_BASE = CMD_MODULE_CODEC | CMD_CTX_ID_DEC,
MPP_DEC_SET_FRAME_INFO, /* vpu api legacy control for buffer slot dimension init */
MPP_DEC_SET_EXT_BUF_GROUP, /* IMPORTANT: set external buffer group to mpp decoder */
MPP_DEC_SET_INFO_CHANGE_READY,
MPP_DEC_SET_PRESENT_TIME_ORDER, /* use input time order for output */
MPP_DEC_SET_PARSER_SPLIT_MODE, /* Need to setup before init */
MPP_DEC_SET_PARSER_FAST_MODE, /* Need to setup before init */
MPP_DEC_GET_STREAM_COUNT,
MPP_DEC_GET_VPUMEM_USED_COUNT,
MPP_DEC_SET_VC1_EXTRA_DATA,
MPP_DEC_SET_OUTPUT_FORMAT,
MPP_DEC_SET_DISABLE_ERROR, /* When set it will disable sw/hw error (H.264 / H.265) */
MPP_DEC_SET_IMMEDIATE_OUT,
MPP_DEC_SET_ENABLE_DEINTERLACE, /* MPP enable deinterlace by default. Vpuapi can disable it */
MPP_DEC_SET_ENABLE_FAST_PLAY, /* enable idr output immediately */
MPP_DEC_SET_DISABLE_THREAD, /* MPP no thread mode and use external thread to decode */
MPP_DEC_SET_MAX_USE_BUFFER_SIZE,
MPP_DEC_SET_ENABLE_MVC, /* enable MVC decoding*/
MPP_DEC_CMD_QUERY = CMD_MODULE_CODEC | CMD_CTX_ID_DEC | CMD_DEC_QUERY,
/* query decoder runtime information for decode stage */
MPP_DEC_QUERY, /* set and get MppDecQueryCfg structure */
CMD_DEC_CMD_CFG = CMD_MODULE_CODEC | CMD_CTX_ID_DEC | CMD_DEC_CFG,
MPP_DEC_SET_CFG, /* set MppDecCfg structure */
MPP_DEC_GET_CFG, /* get MppDecCfg structure */
MPP_DEC_CMD_END,
MPP_ENC_CMD_BASE = CMD_MODULE_CODEC | CMD_CTX_ID_ENC,
/* basic encoder setup control */
MPP_ENC_SET_CFG, /* set MppEncCfg structure */
MPP_ENC_GET_CFG, /* get MppEncCfg structure */
MPP_ENC_SET_PREP_CFG, /* deprecated set MppEncPrepCfg structure, use MPP_ENC_SET_CFG instead */
MPP_ENC_GET_PREP_CFG, /* deprecated get MppEncPrepCfg structure, use MPP_ENC_GET_CFG instead */
MPP_ENC_SET_RC_CFG, /* deprecated set MppEncRcCfg structure, use MPP_ENC_SET_CFG instead */
MPP_ENC_GET_RC_CFG, /* deprecated get MppEncRcCfg structure, use MPP_ENC_GET_CFG instead */
MPP_ENC_SET_CODEC_CFG, /* deprecated set MppEncCodecCfg structure, use MPP_ENC_SET_CFG instead */
MPP_ENC_GET_CODEC_CFG, /* deprecated get MppEncCodecCfg structure, use MPP_ENC_GET_CFG instead */
/* runtime encoder setup control */
MPP_ENC_SET_IDR_FRAME, /* next frame will be encoded as intra frame */
MPP_ENC_SET_OSD_LEGACY_0, /* deprecated */
MPP_ENC_SET_OSD_LEGACY_1, /* deprecated */
MPP_ENC_SET_OSD_LEGACY_2, /* deprecated */
MPP_ENC_GET_HDR_SYNC, /* get vps / sps / pps which has better sync behavior parameter is MppPacket */
MPP_ENC_GET_EXTRA_INFO, /* deprecated */
MPP_ENC_SET_SEI_CFG, /* SEI: Supplement Enhancemant Information, parameter is MppSeiMode */
MPP_ENC_GET_SEI_DATA, /* SEI: Supplement Enhancemant Information, parameter is MppPacket */
MPP_ENC_PRE_ALLOC_BUFF, /* deprecated */
MPP_ENC_SET_QP_RANGE, /* used for adjusting qp range, the parameter can be 1 or 2 */
MPP_ENC_SET_ROI_CFG, /* set MppEncROICfg structure */
MPP_ENC_SET_CTU_QP, /* for H265 Encoder,set CTU's size and QP */
MPP_ENC_CMD_QUERY = CMD_MODULE_CODEC | CMD_CTX_ID_ENC | CMD_ENC_QUERY,
/* query encoder runtime information for encode stage */
MPP_ENC_QUERY, /* set and get MppEncQueryCfg structure */
/* User define rate control stategy API control */
MPP_ENC_CFG_RC_API = CMD_MODULE_CODEC | CMD_CTX_ID_ENC | CMD_ENC_CFG_RC_API,
/*
* Get RcApiQueryAll structure
* Get all available rate control stategy string and count
*/
MPP_ENC_GET_RC_API_ALL = MPP_ENC_CFG_RC_API + 1,
/*
* Get RcApiQueryType structure
* Get available rate control stategy string with certain type
*/
MPP_ENC_GET_RC_API_BY_TYPE = MPP_ENC_CFG_RC_API + 2,
/*
* Set RcImplApi structure
* Add new or update rate control stategy function pointers
*/
MPP_ENC_SET_RC_API_CFG = MPP_ENC_CFG_RC_API + 3,
/*
* Get RcApiBrief structure
* Get current used rate control stategy brief information (type and name)
*/
MPP_ENC_GET_RC_API_CURRENT = MPP_ENC_CFG_RC_API + 4,
/*
* Set RcApiBrief structure
* Set current used rate control stategy brief information (type and name)
*/
MPP_ENC_SET_RC_API_CURRENT = MPP_ENC_CFG_RC_API + 5,
MPP_ENC_CFG_MISC = CMD_MODULE_CODEC | CMD_CTX_ID_ENC | CMD_ENC_CFG_MISC,
MPP_ENC_SET_HEADER_MODE, /* set MppEncHeaderMode */
MPP_ENC_GET_HEADER_MODE, /* get MppEncHeaderMode */
MPP_ENC_CFG_SPLIT = CMD_MODULE_CODEC | CMD_CTX_ID_ENC | CMD_ENC_CFG_SPLIT,
MPP_ENC_SET_SPLIT, /* set MppEncSliceSplit structure */
MPP_ENC_GET_SPLIT, /* get MppEncSliceSplit structure */
MPP_ENC_CFG_REF = CMD_MODULE_CODEC | CMD_CTX_ID_ENC | CMD_ENC_CFG_REF,
MPP_ENC_SET_REF_CFG, /* set MppEncRefCfg structure */
MPP_ENC_CFG_OSD = CMD_MODULE_CODEC | CMD_CTX_ID_ENC | CMD_ENC_CFG_OSD,
MPP_ENC_SET_OSD_PLT_CFG, /* set OSD palette, parameter should be pointer to MppEncOSDPltCfg */
MPP_ENC_GET_OSD_PLT_CFG, /* get OSD palette, parameter should be pointer to MppEncOSDPltCfg */
MPP_ENC_SET_OSD_DATA_CFG, /* set OSD data with at most 8 regions, parameter should be pointer to MppEncOSDData */
MPP_ENC_CMD_END,
MPP_ISP_CMD_BASE = CMD_MODULE_CODEC | CMD_CTX_ID_ISP,
MPP_ISP_CMD_END,
MPP_HAL_CMD_BASE = CMD_MODULE_HAL,
MPP_HAL_CMD_END,
MPI_CMD_BUTT,
} MpiCmd;
#include "rk_vdec_cmd.h"
#include "rk_vdec_cfg.h"
#include "rk_venc_cmd.h"
#include "rk_venc_cfg.h"
#include "rk_venc_ref.h"
#endif /*__RK_MPI_CMD_H__*/

View File

@@ -0,0 +1,142 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_TYPE_H__
#define __RK_TYPE_H__
#include <stddef.h>
#if defined(_WIN32) && !defined(__MINGW32CE__)
typedef unsigned char RK_U8;
typedef unsigned short RK_U16;
typedef unsigned int RK_U32;
typedef unsigned long RK_ULONG;
typedef unsigned __int64 RK_U64;
typedef signed char RK_S8;
typedef signed short RK_S16;
typedef signed int RK_S32;
typedef signed long RK_LONG;
typedef signed __int64 RK_S64;
#else
typedef unsigned char RK_U8;
typedef unsigned short RK_U16;
typedef unsigned int RK_U32;
typedef unsigned long RK_ULONG;
typedef unsigned long long int RK_U64;
typedef signed char RK_S8;
typedef signed short RK_S16;
typedef signed int RK_S32;
typedef signed long RK_LONG;
typedef signed long long int RK_S64;
#endif
#ifndef MODULE_TAG
#define MODULE_TAG NULL
#endif
/**
* @ingroup rk_mpi
* @brief The type of mpp context
* @details This type is used when calling mpp_init(), which including decoder,
* encoder and Image Signal Process(ISP). So far decoder and encoder
* are supported perfectly, and ISP will be supported in the future.
*/
typedef enum {
MPP_CTX_DEC, /**< decoder */
MPP_CTX_ENC, /**< encoder */
MPP_CTX_ISP, /**< isp */
MPP_CTX_BUTT, /**< undefined */
} MppCtxType;
/**
* @ingroup rk_mpi
* @brief Enumeration used to define the possible video compression codings.
* sync with the omx_video.h
*
* @note This essentially refers to file extensions. If the coding is
* being used to specify the ENCODE type, then additional work
* must be done to configure the exact flavor of the compression
* to be used. For decode cases where the user application can
* not differentiate between MPEG-4 and H.264 bit streams, it is
* up to the codec to handle this.
*/
typedef enum {
MPP_VIDEO_CodingUnused, /**< Value when coding is N/A */
MPP_VIDEO_CodingAutoDetect, /**< Autodetection of coding type */
MPP_VIDEO_CodingMPEG2, /**< AKA: H.262 */
MPP_VIDEO_CodingH263, /**< H.263 */
MPP_VIDEO_CodingMPEG4, /**< MPEG-4 */
MPP_VIDEO_CodingWMV, /**< Windows Media Video (WMV1,WMV2,WMV3)*/
MPP_VIDEO_CodingRV, /**< all versions of Real Video */
MPP_VIDEO_CodingAVC, /**< H.264/AVC */
MPP_VIDEO_CodingMJPEG, /**< Motion JPEG */
MPP_VIDEO_CodingVP8, /**< VP8 */
MPP_VIDEO_CodingVP9, /**< VP9 */
MPP_VIDEO_CodingVC1 = 0x01000000, /**< Windows Media Video (WMV1,WMV2,WMV3)*/
MPP_VIDEO_CodingFLV1, /**< Sorenson H.263 */
MPP_VIDEO_CodingDIVX3, /**< DIVX3 */
MPP_VIDEO_CodingVP6,
MPP_VIDEO_CodingHEVC, /**< H.265/HEVC */
MPP_VIDEO_CodingAVSPLUS, /**< AVS+ */
MPP_VIDEO_CodingAVS, /**< AVS profile=0x20 */
MPP_VIDEO_CodingAVS2, /**< AVS2 */
MPP_VIDEO_CodingAV1, /**< av1 */
MPP_VIDEO_CodingKhronosExtensions = 0x6F000000, /**< Reserved region for introducing Khronos Standard Extensions */
MPP_VIDEO_CodingVendorStartUnused = 0x7F000000, /**< Reserved region for introducing Vendor Extensions */
MPP_VIDEO_CodingMax = 0x7FFFFFFF
} MppCodingType;
/*
* All external interface object list here.
* The interface object is defined as void * for expandability
* The cross include between these objects will introduce extra
* compiling difficulty. So we move them together in this header.
*
* Object interface header list:
*
* MppCtx - rk_mpi.h
* MppParam - rk_mpi.h
*
* MppFrame - mpp_frame.h
* MppPacket - mpp_packet.h
*
* MppBuffer - mpp_buffer.h
* MppBufferGroup - mpp_buffer.h
*
* MppTask - mpp_task.h
* MppMeta - mpp_meta.h
*/
typedef void* MppCtx;
typedef void* MppParam;
typedef void* MppFrame;
typedef void* MppPacket;
typedef void* MppBuffer;
typedef void* MppBufferGroup;
typedef void* MppTask;
typedef void* MppMeta;
#endif /*__RK_TYPE_H__*/

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2020 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_VDEC_CFG_H__
#define __RK_VDEC_CFG_H__
#include "rk_type.h"
#include "mpp_err.h"
typedef void* MppDecCfg;
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_dec_cfg_init(MppDecCfg *cfg);
MPP_RET mpp_dec_cfg_deinit(MppDecCfg cfg);
MPP_RET mpp_dec_cfg_set_s32(MppDecCfg cfg, const char *name, RK_S32 val);
MPP_RET mpp_dec_cfg_set_u32(MppDecCfg cfg, const char *name, RK_U32 val);
MPP_RET mpp_dec_cfg_set_s64(MppDecCfg cfg, const char *name, RK_S64 val);
MPP_RET mpp_dec_cfg_set_u64(MppDecCfg cfg, const char *name, RK_U64 val);
MPP_RET mpp_dec_cfg_set_ptr(MppDecCfg cfg, const char *name, void *val);
MPP_RET mpp_dec_cfg_get_s32(MppDecCfg cfg, const char *name, RK_S32 *val);
MPP_RET mpp_dec_cfg_get_u32(MppDecCfg cfg, const char *name, RK_U32 *val);
MPP_RET mpp_dec_cfg_get_s64(MppDecCfg cfg, const char *name, RK_S64 *val);
MPP_RET mpp_dec_cfg_get_u64(MppDecCfg cfg, const char *name, RK_U64 *val);
MPP_RET mpp_dec_cfg_get_ptr(MppDecCfg cfg, const char *name, void **val);
void mpp_dec_cfg_show(void);
#ifdef __cplusplus
}
#endif
#endif /*__RK_VDEC_CFG_H__*/

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_VDEC_CMD_H__
#define __RK_VDEC_CMD_H__
#include "rk_type.h"
#include "mpp_err.h"
/*
* decoder query interface is only for debug usage
*/
#define MPP_DEC_QUERY_STATUS (0x00000001)
#define MPP_DEC_QUERY_WAIT (0x00000002)
#define MPP_DEC_QUERY_FPS (0x00000004)
#define MPP_DEC_QUERY_BPS (0x00000008)
#define MPP_DEC_QUERY_DEC_IN_PKT (0x00000010)
#define MPP_DEC_QUERY_DEC_WORK (0x00000020)
#define MPP_DEC_QUERY_DEC_OUT_FRM (0x00000040)
#define MPP_DEC_QUERY_ALL (MPP_DEC_QUERY_STATUS | \
MPP_DEC_QUERY_WAIT | \
MPP_DEC_QUERY_FPS | \
MPP_DEC_QUERY_BPS | \
MPP_DEC_QUERY_DEC_IN_PKT | \
MPP_DEC_QUERY_DEC_WORK | \
MPP_DEC_QUERY_DEC_OUT_FRM)
typedef struct MppDecQueryCfg_t {
/*
* 32 bit query flag for query data check
* Each bit represent a query data switch.
* bit 0 - for querying decoder runtime status
* bit 1 - for querying decoder runtime waiting status
* bit 2 - for querying decoder realtime decode fps
* bit 3 - for querying decoder realtime input bps
* bit 4 - for querying decoder input packet count
* bit 5 - for querying decoder start hardware times
* bit 6 - for querying decoder output frame count
*/
RK_U32 query_flag;
/* 64 bit query data output */
RK_U32 rt_status;
RK_U32 rt_wait;
RK_U32 rt_fps;
RK_U32 rt_bps;
RK_U32 dec_in_pkt_cnt;
RK_U32 dec_hw_run_cnt;
RK_U32 dec_out_frm_cnt;
} MppDecQueryCfg;
typedef void* MppExtCbCtx;
typedef MPP_RET (*MppExtCbFunc)(MppExtCbCtx cb_ctx, MppCtx mpp, RK_S32 cmd, void *arg);
#endif /*__RK_VDEC_CMD_H__*/

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_VENC_CFG_H__
#define __RK_VENC_CFG_H__
#include "rk_type.h"
#include "mpp_err.h"
typedef void* MppEncCfg;
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_enc_cfg_init(MppEncCfg *cfg);
MPP_RET mpp_enc_cfg_deinit(MppEncCfg cfg);
MPP_RET mpp_enc_cfg_set_s32(MppEncCfg cfg, const char *name, RK_S32 val);
MPP_RET mpp_enc_cfg_set_u32(MppEncCfg cfg, const char *name, RK_U32 val);
MPP_RET mpp_enc_cfg_set_s64(MppEncCfg cfg, const char *name, RK_S64 val);
MPP_RET mpp_enc_cfg_set_u64(MppEncCfg cfg, const char *name, RK_U64 val);
MPP_RET mpp_enc_cfg_set_ptr(MppEncCfg cfg, const char *name, void *val);
MPP_RET mpp_enc_cfg_set_st(MppEncCfg cfg, const char *name, void *val);
MPP_RET mpp_enc_cfg_get_s32(MppEncCfg cfg, const char *name, RK_S32 *val);
MPP_RET mpp_enc_cfg_get_u32(MppEncCfg cfg, const char *name, RK_U32 *val);
MPP_RET mpp_enc_cfg_get_s64(MppEncCfg cfg, const char *name, RK_S64 *val);
MPP_RET mpp_enc_cfg_get_u64(MppEncCfg cfg, const char *name, RK_U64 *val);
MPP_RET mpp_enc_cfg_get_ptr(MppEncCfg cfg, const char *name, void **val);
MPP_RET mpp_enc_cfg_get_st(MppEncCfg cfg, const char *name, void *val);
void mpp_enc_cfg_show(void);
#ifdef __cplusplus
}
#endif
#endif /*__RK_VENC_CFG_H__*/

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_VENC_RC_H__
#define __RK_VENC_RC_H__
#include "rk_type.h"
#define MPP_ENC_MIN_BPS (SZ_1K)
#define MPP_ENC_MAX_BPS (SZ_1M * 200)
/* Rate control parameter */
typedef enum MppEncRcMode_e {
MPP_ENC_RC_MODE_VBR,
MPP_ENC_RC_MODE_CBR,
MPP_ENC_RC_MODE_FIXQP,
MPP_ENC_RC_MODE_AVBR,
MPP_ENC_RC_MODE_BUTT
} MppEncRcMode;
typedef enum MppEncRcPriority_e {
MPP_ENC_RC_BY_BITRATE_FIRST,
MPP_ENC_RC_BY_FRM_SIZE_FIRST,
MPP_ENC_RC_PRIORITY_BUTT
} MppEncRcPriority;
typedef enum MppEncRcDropFrmMode_e {
MPP_ENC_RC_DROP_FRM_DISABLED,
MPP_ENC_RC_DROP_FRM_NORMAL,
MPP_ENC_RC_DROP_FRM_PSKIP,
MPP_ENC_RC_DROP_FRM_BUTT
} MppEncRcDropFrmMode;
typedef enum MppEncRcSuperFrameMode_t {
MPP_ENC_RC_SUPER_FRM_NONE,
MPP_ENC_RC_SUPER_FRM_DROP,
MPP_ENC_RC_SUPER_FRM_REENC,
MPP_ENC_RC_SUPER_FRM_BUTT
} MppEncRcSuperFrameMode;
typedef enum MppEncRcGopMode_e {
MPP_ENC_RC_NORMAL_P,
MPP_ENC_RC_SMART_P,
MPP_ENC_RC_GOP_MODE_BUTT,
} MppEncRcGopMode;
typedef enum MppEncRcIntraRefreshMode_e {
MPP_ENC_RC_INTRA_REFRESH_ROW = 0,
MPP_ENC_RC_INTRA_REFRESH_COL,
MPP_ENC_RC_INTRA_REFRESH_BUTT
} MppEncRcRefreshMode;
#endif /*__RK_VENC_RC_H__*/

View File

@@ -0,0 +1,242 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __RK_VENC_REF_H__
#define __RK_VENC_REF_H__
#include "rk_type.h"
#include "mpp_err.h"
/*
* MPP reference management system follows the model of H.264/H.265 reference
* frame mangement.
*
* The reference frame is defined into two type: long-term reference frame and
* short-refernce frame (lt_ref and st_ref).
*
* The lt_ref can be only indexed by long-term reference frame index (lt_idx).
* The st_ref can be indexed by its temporal id (tid) and previous count.
*
* MppEncRefMode defined the way for user to reference the required frame.
*
* Normal reference mode without argument
* REF_TO_PREV_REF_FRM - refer to previous reference frame in encode order (No matter Lt or St)
* REF_TO_PREV_ST_REF - refer to previous short-term reference frame
* REF_TO_PREV_LT_REF - refer to previous long-term reference frame
* REF_TO_PREV_INTRA - refer to previous Intra / IDR frame
* REF_TO_ST_REF_SETUP - refer to refernce frame defined in StRefSetup
*
* Normal reference mode with argument
* REF_TO_TEMPORAL_LAYER - refer to previous reference frame with temporal id argument
* REF_TO_LT_REF_IDX - refer to long-term reference frame with lt_ref_idx argument
* REF_TO_ST_PREV_N_REF - refer to short-term reference frame with diff frame_num argument
*
* Long-term reference only mode
* REF_TO_ST_REF_SETUP - use corresponding mode of original short-term reference frame
*
* Short-term reference only mode
* REF_TO_LT_REF_SETUP - indicate that this frame will be overwrited by long-term config
*
* By combining frames with these modes user can define many kinds of reference hierarchy
* structure. But normally user should use simplified preset hierarchy pattern.
*
* The rules for virtual cpb management is similiar to H.264/H.265
* 1. When one frame is marked as long-term reference frame it will be kept in cpb until
* it is replaced by other frame with the same lt_idx or IDR frame.
* 2. When one frame is marked as short-term reference frame it will be inert into cpb when
* there is enough storage space. When the number of total sum of long-term and short-term
* reference frame excess the cpb size limit the oldest short-term frame will be removed.
* This is call sliding window in H.264.
*/
/* max 4 temporal layer */
#define MPP_ENC_MAX_TEMPORAL_LAYER_NUM 4
/* max 4 long-term reference frame */
#define MPP_ENC_MAX_LT_REF_NUM 16
/*
* Group Of Picture (GOP) config is separated into three parts:
*
* 1. Intra / IDR frame config
* igop - the interval of two intra / IDR frames
*
* 2. Long-term reference config (MppEncRefLtFrmCfg)
*
* Setup long-term reference index max lt_idx, loop interval and reference
* mode for auto long-term reference frame generation. The encoder will
* mark frame to be long-term reference frame with given interval.
*
* 2.1 lt_idx
* The long-term reference frame index is unique identifier for a long-term
* reference frame.
* The max long-term reference frame index should NOT larger than
* max_num_ref_frames in sps.
*
* 2.2 lt_gap
* When lt_gap is zero the long-term reference frame generation is disabled.
* When lt_gap is non-zero (usually 2~3 second interval) then the long-term
* reference frame will be generated for error recovery or smart hierarchy.
*
* 2.2 lt_delay
* The lt_delay is the delay time for generation of long-term reference frame.
* The start point of lt_delay is the IDR/intra frame genertaed by igop.
*
* 2.4 ref_mode: Long-term refernce frame reference mode
* NOTE: temporal id of longterm reference frame is always zero.
*
* Examples:
* Sequence has only one lt_ref 0 and setup one long-term reference frame
* every 300 frame.
* {
* .lt_idx = 0,
* .lt_gap = 300,
* .lt_delay = 0,
* }
* result:
* frame 0 ...... 299 300 301 ...... 599 600 601
* lt_idx 0 xxxxxx x 0 x xxxxxx x 0 x
*
* Sequence has lt_ref from 0 to 2 and setup a long-term reference frame
* every 100 frame.
* {
* .lt_idx = 0,
* .lt_gap = 300,
* .lt_delay = 0,
* }
* {
* .lt_idx = 1,
* .lt_gap = 300,
* .lt_delay = 100,
* }
* {
* .lt_idx = 2,
* .lt_gap = 300,
* .lt_delay = 200,
* }
* result:
* frame 0 ... 99 100 101 ... 199 200 201 ... 299 300 301
* lt_idx 0 xxx x 1 x xxx x 2 x xxx x 0 x
*
* 3. Short-term reference config (MppEncStRefSetup)
*
* 3.1 is_non_ref
* The is_non_ref indicated the current frame is reference frame or not.
*
* 3.2 temporal_id
* The temporal id of the current frame configure.
*
* 3.3 ref_mode: short-term refernce frame reference mode
*
* 3.4 repeat
* The repeat time of the short-term reference frame configure.
* The overall frame count with the same config is repeat + 1.
*
* Examples:
*
*/
#define REF_MODE_MODE_MASK (0x1F)
#define REF_MODE_ARG_MASK (0xFFFF0000)
typedef enum MppEncRefMode_e {
/* max 32 mode in 32-bit */
/* for default ref global config */
REF_MODE_GLOBAL,
REF_TO_PREV_REF_FRM = REF_MODE_GLOBAL,
REF_TO_PREV_ST_REF,
REF_TO_PREV_LT_REF,
REF_TO_PREV_INTRA,
/* for global config with args */
REF_MODE_GLOBAL_WITH_ARG = 0x4,
/* with ref arg as temporal layer id */
REF_TO_TEMPORAL_LAYER = REF_MODE_GLOBAL_WITH_ARG,
/* with ref arg as long-term reference picture index */
REF_TO_LT_REF_IDX,
/* with ref arg as short-term reference picture difference frame_num */
REF_TO_ST_PREV_N_REF,
REF_MODE_GLOBAL_BUTT,
/* for lt-ref */
REF_MODE_LT = 0x18,
REF_TO_ST_REF_SETUP,
REF_MODE_LT_BUTT,
/* for st-ref */
REF_MODE_ST = 0x1C,
REF_TO_LT_REF_SETUP,
REF_MODE_ST_BUTT,
} MppEncRefMode;
typedef struct MppEncRefLtFrmCfg_t {
RK_S32 lt_idx; /* lt_idx of the reference frame */
RK_S32 temporal_id; /* temporal_id of the reference frame */
MppEncRefMode ref_mode;
RK_S32 ref_arg;
RK_S32 lt_gap; /* gap between two lt-ref with same lt_idx */
RK_S32 lt_delay; /* delay offset to igop start frame */
} MppEncRefLtFrmCfg;
typedef struct MppEncRefStFrmCfg_t {
RK_S32 is_non_ref;
RK_S32 temporal_id;
MppEncRefMode ref_mode;
RK_S32 ref_arg;
RK_S32 repeat; /* repeat times */
} MppEncRefStFrmCfg;
typedef struct MppEncRefPreset_t {
/* input parameter for query */
const char *name;
RK_S32 max_lt_cnt;
RK_S32 max_st_cnt;
MppEncRefLtFrmCfg *lt_cfg;
MppEncRefStFrmCfg *st_cfg;
/* output parameter */
RK_S32 lt_cnt;
RK_S32 st_cnt;
} MppEncRefPreset;
typedef void* MppEncRefCfg;
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_enc_ref_cfg_init(MppEncRefCfg *ref);
MPP_RET mpp_enc_ref_cfg_deinit(MppEncRefCfg *ref);
MPP_RET mpp_enc_ref_cfg_reset(MppEncRefCfg ref);
MPP_RET mpp_enc_ref_cfg_set_cfg_cnt(MppEncRefCfg ref, RK_S32 lt_cnt, RK_S32 st_cnt);
MPP_RET mpp_enc_ref_cfg_add_lt_cfg(MppEncRefCfg ref, RK_S32 cnt, MppEncRefLtFrmCfg *frm);
MPP_RET mpp_enc_ref_cfg_add_st_cfg(MppEncRefCfg ref, RK_S32 cnt, MppEncRefStFrmCfg *frm);
MPP_RET mpp_enc_ref_cfg_check(MppEncRefCfg ref);
/*
* A new reference configure will restart a new gop and clear cpb by default.
* The keep cpb function will let encoder keeps the current cpb status and do NOT
* reset all the reference frame in cpb.
*/
MPP_RET mpp_enc_ref_cfg_set_keep_cpb(MppEncRefCfg ref, RK_S32 keep);
MPP_RET mpp_enc_ref_cfg_get_preset(MppEncRefPreset *preset);
MPP_RET mpp_enc_ref_cfg_show(MppEncRefCfg ref);
#ifdef __cplusplus
}
#endif
#endif /*__RK_VENC_REF_H__*/

View File

@@ -0,0 +1,123 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __VPU_H__
#define __VPU_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "rk_type.h"
#define VPU_SUCCESS (0)
#define VPU_FAILURE (-1)
#define VPU_HW_WAIT_OK VPU_SUCCESS
#define VPU_HW_WAIT_ERROR VPU_FAILURE
#define VPU_HW_WAIT_TIMEOUT 1
// vpu decoder 60 registers, size 240B
#define VPU_REG_NUM_DEC (60)
// vpu post processor 41 registers, size 164B
#define VPU_REG_NUM_PP (41)
// vpu decoder + post processor 101 registers, size 404B
#define VPU_REG_NUM_DEC_PP (VPU_REG_NUM_DEC+VPU_REG_NUM_PP)
// vpu encoder 96 registers, size 384B
#define VPU_REG_NUM_ENC (96)
typedef enum {
VPU_ENC = 0x0,
VPU_DEC = 0x1,
VPU_PP = 0x2,
VPU_DEC_PP = 0x3,
VPU_DEC_HEVC = 0x4,
VPU_DEC_RKV = 0x5,
VPU_ENC_RKV = 0x6,
VPU_DEC_AVSPLUS = 0x7,
VPU_ENC_VEPU22 = 0x8,
VPU_TYPE_BUTT ,
} VPU_CLIENT_TYPE;
/* Hardware decoder configuration description */
typedef struct VPUHwDecConfig {
RK_U32 maxDecPicWidth; /* Maximum video decoding width supported */
RK_U32 maxPpOutPicWidth; /* Maximum output width of Post-Processor */
RK_U32 h264Support; /* HW supports h.264 */
RK_U32 jpegSupport; /* HW supports JPEG */
RK_U32 mpeg4Support; /* HW supports MPEG-4 */
RK_U32 customMpeg4Support; /* HW supports custom MPEG-4 features */
RK_U32 vc1Support; /* HW supports VC-1 Simple */
RK_U32 mpeg2Support; /* HW supports MPEG-2 */
RK_U32 ppSupport; /* HW supports post-processor */
RK_U32 ppConfig; /* HW post-processor functions bitmask */
RK_U32 sorensonSparkSupport; /* HW supports Sorenson Spark */
RK_U32 refBufSupport; /* HW supports reference picture buffering */
RK_U32 vp6Support; /* HW supports VP6 */
RK_U32 vp7Support; /* HW supports VP7 */
RK_U32 vp8Support; /* HW supports VP8 */
RK_U32 avsSupport; /* HW supports AVS */
RK_U32 jpegESupport; /* HW supports JPEG extensions */
RK_U32 rvSupport; /* HW supports REAL */
RK_U32 mvcSupport; /* HW supports H264 MVC extension */
} VPUHwDecConfig_t;
/* Hardware encoder configuration description */
typedef struct VPUHwEndConfig {
RK_U32 maxEncodedWidth; /* Maximum supported width for video encoding (not JPEG) */
RK_U32 h264Enabled; /* HW supports H.264 */
RK_U32 jpegEnabled; /* HW supports JPEG */
RK_U32 mpeg4Enabled; /* HW supports MPEG-4 */
RK_U32 vsEnabled; /* HW supports video stabilization */
RK_U32 rgbEnabled; /* HW supports RGB input */
RK_U32 reg_size; /* HW bus type in use */
RK_U32 reserv[2];
} VPUHwEncConfig_t;
typedef enum {
// common command
VPU_CMD_REGISTER ,
VPU_CMD_REGISTER_ACK_OK ,
VPU_CMD_REGISTER_ACK_FAIL ,
VPU_CMD_UNREGISTER ,
VPU_SEND_CONFIG ,
VPU_SEND_CONFIG_ACK_OK ,
VPU_SEND_CONFIG_ACK_FAIL ,
VPU_GET_HW_INFO ,
VPU_GET_HW_INFO_ACK_OK ,
VPU_GET_HW_INFO_ACK_FAIL ,
VPU_CMD_BUTT ,
} VPU_CMD_TYPE;
int VPUClientInit(VPU_CLIENT_TYPE type);
RK_S32 VPUClientRelease(int socket);
RK_S32 VPUClientSendReg(int socket, RK_U32 *regs, RK_U32 nregs);
RK_S32 VPUClientSendReg2(RK_S32 socket, RK_S32 offset, RK_S32 size, void *param);
RK_S32 VPUClientWaitResult(int socket, RK_U32 *regs, RK_U32 nregs, VPU_CMD_TYPE *cmd, RK_S32 *len);
RK_S32 VPUClientGetHwCfg(int socket, RK_U32 *cfg, RK_U32 cfg_size);
RK_S32 VPUClientGetIOMMUStatus();
RK_U32 VPUCheckSupportWidth();
#ifdef __cplusplus
}
#endif
#endif /* __VPU_H__ */

View File

@@ -0,0 +1,516 @@
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __VPU_API_H__
#define __VPU_API_H__
#include "rk_type.h"
#include "mpp_err.h"
/**
* @brief rockchip media process interface
*/
#define VPU_API_NOPTS_VALUE (0x8000000000000000LL)
/*
* bit definition of ColorType in structure VPU_FRAME
*/
#define VPU_OUTPUT_FORMAT_TYPE_MASK (0x0000ffff)
#define VPU_OUTPUT_FORMAT_ARGB8888 (0x00000000)
#define VPU_OUTPUT_FORMAT_ABGR8888 (0x00000001)
#define VPU_OUTPUT_FORMAT_RGB888 (0x00000002)
#define VPU_OUTPUT_FORMAT_RGB565 (0x00000003)
#define VPU_OUTPUT_FORMAT_RGB555 (0x00000004)
#define VPU_OUTPUT_FORMAT_YUV420_SEMIPLANAR (0x00000005)
#define VPU_OUTPUT_FORMAT_YUV420_PLANAR (0x00000006)
#define VPU_OUTPUT_FORMAT_YUV422 (0x00000007)
#define VPU_OUTPUT_FORMAT_YUV444 (0x00000008)
#define VPU_OUTPUT_FORMAT_YCH420 (0x00000009)
#define VPU_OUTPUT_FORMAT_BIT_MASK (0x000f0000)
#define VPU_OUTPUT_FORMAT_BIT_8 (0x00000000)
#define VPU_OUTPUT_FORMAT_BIT_10 (0x00010000)
#define VPU_OUTPUT_FORMAT_BIT_12 (0x00020000)
#define VPU_OUTPUT_FORMAT_BIT_14 (0x00030000)
#define VPU_OUTPUT_FORMAT_BIT_16 (0x00040000)
#define VPU_OUTPUT_FORMAT_FBC_MASK (0x00f00000)
#define VPU_OUTPUT_FORMAT_FBC_AFBC_V1 (0x00100000)
#define VPU_OUTPUT_FORMAT_FBC_AFBC_V2 (0x00200000)
#define VPU_OUTPUT_FORMAT_DYNCRANGE_MASK (0x0f000000)
#define VPU_OUTPUT_FORMAT_DYNCRANGE_SDR (0x00000000)
#define VPU_OUTPUT_FORMAT_DYNCRANGE_HDR10 (0x01000000)
#define VPU_OUTPUT_FORMAT_DYNCRANGE_HDR_HLG (0x02000000)
#define VPU_OUTPUT_FORMAT_DYNCRANGE_HDR_DOLBY (0x03000000)
/**
* @brief input picture type
*/
typedef enum {
ENC_INPUT_YUV420_PLANAR = 0, /**< YYYY... UUUU... VVVV */
ENC_INPUT_YUV420_SEMIPLANAR = 1, /**< YYYY... UVUVUV... */
ENC_INPUT_YUV422_INTERLEAVED_YUYV = 2, /**< YUYVYUYV... */
ENC_INPUT_YUV422_INTERLEAVED_UYVY = 3, /**< UYVYUYVY... */
ENC_INPUT_RGB565 = 4, /**< 16-bit RGB */
ENC_INPUT_BGR565 = 5, /**< 16-bit RGB */
ENC_INPUT_RGB555 = 6, /**< 15-bit RGB */
ENC_INPUT_BGR555 = 7, /**< 15-bit RGB */
ENC_INPUT_RGB444 = 8, /**< 12-bit RGB */
ENC_INPUT_BGR444 = 9, /**< 12-bit RGB */
ENC_INPUT_RGB888 = 10, /**< 24-bit RGB */
ENC_INPUT_BGR888 = 11, /**< 24-bit RGB */
ENC_INPUT_RGB101010 = 12, /**< 30-bit RGB */
ENC_INPUT_BGR101010 = 13 /**< 30-bit RGB */
} EncInputPictureType;
typedef enum VPU_API_CMD {
VPU_API_ENC_SETCFG,
VPU_API_ENC_GETCFG,
VPU_API_ENC_SETFORMAT,
VPU_API_ENC_SETIDRFRAME,
VPU_API_ENABLE_DEINTERLACE,
VPU_API_SET_VPUMEM_CONTEXT,
VPU_API_USE_PRESENT_TIME_ORDER,
VPU_API_SET_DEFAULT_WIDTH_HEIGH,
VPU_API_SET_INFO_CHANGE,
VPU_API_USE_FAST_MODE,
VPU_API_DEC_GET_STREAM_COUNT,
VPU_API_GET_VPUMEM_USED_COUNT,
VPU_API_GET_FRAME_INFO,
VPU_API_SET_OUTPUT_BLOCK,
VPU_API_GET_EOS_STATUS,
VPU_API_SET_OUTPUT_MODE,
/* get sps/pps header */
VPU_API_GET_EXTRA_INFO = 0x200,
VPU_API_SET_IMMEDIATE_OUT = 0x1000,
VPU_API_SET_PARSER_SPLIT_MODE, /* NOTE: should control before init */
VPU_API_DEC_OUT_FRM_STRUCT_TYPE,
VPU_API_DEC_EN_THUMBNAIL,
VPU_API_DEC_EN_HDR_META,
VPU_API_DEC_EN_MVC,
VPU_API_DEC_EN_FBC_HDR_256_ODD,
VPU_API_SET_INPUT_BLOCK,
/* set pkt/frm ready callback */
VPU_API_SET_PKT_RDY_CB = 0x1100,
VPU_API_SET_FRM_RDY_CB,
VPU_API_ENC_VEPU22_START = 0x2000,
VPU_API_ENC_SET_VEPU22_CFG,
VPU_API_ENC_GET_VEPU22_CFG,
VPU_API_ENC_SET_VEPU22_CTU_QP,
VPU_API_ENC_SET_VEPU22_ROI,
VPU_API_ENC_MPP = 0x3000,
VPU_API_ENC_MPP_SETCFG,
VPU_API_ENC_MPP_GETCFG,
/* mlvec dynamic configure */
VPU_API_ENC_MLVEC_CFG = 0x4000,
VPU_API_ENC_SET_MAX_TID,
VPU_API_ENC_SET_MARK_LTR,
VPU_API_ENC_SET_USE_LTR,
VPU_API_ENC_SET_FRAME_QP,
VPU_API_ENC_SET_BASE_LAYER_PID,
} VPU_API_CMD;
typedef struct {
RK_U32 TimeLow;
RK_U32 TimeHigh;
} TIME_STAMP;
typedef struct {
RK_U32 CodecType;
RK_U32 ImgWidth;
RK_U32 ImgHeight;
RK_U32 ImgHorStride;
RK_U32 ImgVerStride;
RK_U32 BufSize;
} VPU_GENERIC;
typedef struct VPUMem {
RK_U32 phy_addr;
RK_U32 *vir_addr;
RK_U32 size;
RK_U32 *offset;
} VPUMemLinear_t;
typedef struct tVPU_FRAME {
RK_U32 FrameBusAddr[2]; // 0: Y address; 1: UV address;
RK_U32 FrameWidth; // buffer horizontal stride
RK_U32 FrameHeight; // buffer vertical stride
RK_U32 OutputWidth; // deprecated
RK_U32 OutputHeight; // deprecated
RK_U32 DisplayWidth; // valid width for display
RK_U32 DisplayHeight; // valid height for display
RK_U32 CodingType;
RK_U32 FrameType; // frame; top_field_first; bot_field_first
RK_U32 ColorType;
RK_U32 DecodeFrmNum;
TIME_STAMP ShowTime;
RK_U32 ErrorInfo; // error information
RK_U32 employ_cnt;
VPUMemLinear_t vpumem;
struct tVPU_FRAME *next_frame;
union {
struct {
RK_U32 Res0[2];
struct {
RK_U32 ColorPrimaries : 8;
RK_U32 ColorTransfer : 8;
RK_U32 ColorCoeffs : 8;
RK_U32 ColorRange : 1;
RK_U32 Res1 : 7;
};
RK_U32 Res2;
};
RK_U32 Res[4];
};
} VPU_FRAME;
typedef struct FrameThumbInfo {
RK_U32 enable;
RK_U32 yOffset;
RK_U32 uvOffset;
} FrameThumbInfo_t;
typedef struct FrameHdrInfo {
RK_U32 isHdr;
RK_U32 offset;
RK_U32 size;
} FrameHdrInfo_t;
typedef struct VideoFrame {
VPU_FRAME vpuFrame;
FrameThumbInfo_t thumbInfo;
FrameHdrInfo_t hdrInfo;
RK_U32 viewId;
RK_U32 reserved[16];
} VideoFrame_t;
typedef struct VideoPacket {
RK_S64 pts; /* with unit of us*/
RK_S64 dts; /* with unit of us*/
RK_U8 *data;
RK_S32 size;
RK_U32 capability;
RK_U32 nFlags;
} VideoPacket_t;
typedef struct DecoderOut {
RK_U8 *data;
RK_U32 size;
RK_S64 timeUs;
RK_S32 nFlags;
} DecoderOut_t;
typedef struct ParserOut {
RK_U8 *data;
RK_U32 size;
RK_S64 timeUs;
RK_U32 nFlags;
RK_U32 width;
RK_U32 height;
} ParserOut_t;
typedef struct EncInputStream {
RK_U8 *buf;
RK_S32 size;
RK_U32 bufPhyAddr;
RK_S64 timeUs;
RK_U32 nFlags;
} EncInputStream_t;
typedef struct EncoderOut {
RK_U8 *data;
RK_S32 size;
RK_S64 timeUs;
RK_S32 keyFrame;
} EncoderOut_t;
typedef RK_S32 (*VpuFrmRdyCbFunc)(void *cb_ctx);
typedef struct {
VpuFrmRdyCbFunc cb;
void *cbCtx;
} FrameRdyCB;
/*
* @brief Enumeration used to define the possible video compression codings.
* @note This essentially refers to file extensions. If the coding is
* being used to specify the ENCODE type, then additional work
* must be done to configure the exact flavor of the compression
* to be used. For decode cases where the user application can
* not differentiate between MPEG-4 and H.264 bit streams, it is
* up to the codec to handle this.
*
* sync with the omx_video.h
*/
typedef enum OMX_RK_VIDEO_CODINGTYPE {
OMX_RK_VIDEO_CodingUnused, /**< Value when coding is N/A */
OMX_RK_VIDEO_CodingAutoDetect, /**< Autodetection of coding type */
OMX_RK_VIDEO_CodingMPEG2, /**< AKA: H.262 */
OMX_RK_VIDEO_CodingH263, /**< H.263 */
OMX_RK_VIDEO_CodingMPEG4, /**< MPEG-4 */
OMX_RK_VIDEO_CodingWMV, /**< Windows Media Video (WMV1,WMV2,WMV3)*/
OMX_RK_VIDEO_CodingRV, /**< all versions of Real Video */
OMX_RK_VIDEO_CodingAVC, /**< H.264/AVC */
OMX_RK_VIDEO_CodingMJPEG, /**< Motion JPEG */
OMX_RK_VIDEO_CodingVP8, /**< VP8 */
OMX_RK_VIDEO_CodingVP9, /**< VP9 */
OMX_RK_VIDEO_CodingVC1 = 0x01000000, /**< Windows Media Video (WMV1,WMV2,WMV3)*/
OMX_RK_VIDEO_CodingFLV1, /**< Sorenson H.263 */
OMX_RK_VIDEO_CodingDIVX3, /**< DIVX3 */
OMX_RK_VIDEO_CodingVP6,
OMX_RK_VIDEO_CodingHEVC, /**< H.265/HEVC */
OMX_RK_VIDEO_CodingAVSPLUS, /**< AVS+ profile 0x48 */
OMX_RK_VIDEO_CodingAVS, /**< AVS profile 0x20 */
OMX_RK_VIDEO_CodingAVS2, /**< AVS2 */
OMX_RK_VIDEO_CodingAV1, /**< av1 */
OMX_RK_VIDEO_CodingKhronosExtensions = 0x6F000000, /**< Reserved region for introducing Khronos Standard Extensions */
OMX_RK_VIDEO_CodingVendorStartUnused = 0x7F000000, /**< Reserved region for introducing Vendor Extensions */
OMX_RK_VIDEO_CodingMax = 0x7FFFFFFF
} OMX_RK_VIDEO_CODINGTYPE;
typedef enum CODEC_TYPE {
CODEC_NONE,
CODEC_DECODER,
CODEC_ENCODER,
CODEC_BUTT,
} CODEC_TYPE;
typedef enum VPU_API_ERR {
VPU_API_OK = 0,
VPU_API_ERR_UNKNOW = -1,
VPU_API_ERR_BASE = -1000,
VPU_API_ERR_LIST_STREAM = VPU_API_ERR_BASE - 1,
VPU_API_ERR_INIT = VPU_API_ERR_BASE - 2,
VPU_API_ERR_VPU_CODEC_INIT = VPU_API_ERR_BASE - 3,
VPU_API_ERR_STREAM = VPU_API_ERR_BASE - 4,
VPU_API_ERR_FATAL_THREAD = VPU_API_ERR_BASE - 5,
VPU_API_EOS_STREAM_REACHED = VPU_API_ERR_BASE - 11,
VPU_API_ERR_BUTT,
} VPU_API_ERR;
typedef enum VPU_FRAME_ERR {
VPU_FRAME_ERR_UNKNOW = 0x0001,
VPU_FRAME_ERR_UNSUPPORT = 0x0002,
} VPU_FRAME_ERR;
typedef struct EncParameter {
RK_S32 width;
RK_S32 height;
RK_S32 rc_mode; /* 0 - CQP mode; 1 - CBR mode; 2 - FIXQP mode*/
RK_S32 bitRate; /* target bitrate */
RK_S32 framerate;
RK_S32 qp;
RK_S32 enableCabac;
RK_S32 cabacInitIdc;
RK_S32 format;
RK_S32 intraPicRate;
RK_S32 framerateout;
RK_S32 profileIdc;
RK_S32 levelIdc;
RK_S32 reserved[3];
} EncParameter_t;
typedef struct EXtraCfg {
RK_S32 vc1extra_size;
RK_S32 vp6codeid;
RK_S32 tsformat;
RK_U32 ori_vpu; /* use origin vpu framework */
/* below used in decode */
RK_U32 mpp_mode; /* use mpp framework */
RK_U32 bit_depth; /* 8 or 10 bit */
RK_U32 yuv_format; /* 0:420 1:422 2:444 */
RK_U32 reserved[16];
} EXtraCfg_t;
/**
* @brief vpu function interface
*/
typedef struct VpuCodecContext {
void* vpuApiObj;
CODEC_TYPE codecType;
OMX_RK_VIDEO_CODINGTYPE videoCoding;
RK_U32 width;
RK_U32 height;
void *extradata;
RK_S32 extradata_size;
RK_U8 enableparsing;
RK_S32 no_thread;
EXtraCfg_t extra_cfg;
void* private_data;
/*
** 1: error state(not working) 0: working
*/
RK_S32 decoder_err;
/**
* Allocate and initialize an VpuCodecContext.
*
* @param ctx The context of vpu api, allocated in this function.
* @param extraData The extra data of codec, some codecs need / can
* use extradata like Huffman tables, also live VC1 codec can
* use extradata to initialize itself.
* @param extra_size The size of extra data.
*
* @return 0 for init success, others for failure.
* @note check whether ctx has been allocated success after you do init.
*/
RK_S32 (*init)(struct VpuCodecContext *ctx, RK_U8 *extraData, RK_U32 extra_size);
/**
* @brief both send video stream packet to decoder and get video frame from
* decoder at the same time
* @param ctx The context of vpu codec
* @param pkt[in] Stream to be decoded
* @param aDecOut[out] Decoding frame
* @return 0 for decode success, others for failure.
*/
RK_S32 (*decode)(struct VpuCodecContext *ctx, VideoPacket_t *pkt, DecoderOut_t *aDecOut);
/**
* @brief both send video frame to encoder and get encoded video stream from
* encoder at the same time.
* @param ctx The context of vpu codec
* @param aEncInStrm[in] Frame to be encoded
* @param aEncOut[out] Encoding stream
* @return 0 for encode success, others for failure.
*/
RK_S32 (*encode)(struct VpuCodecContext *ctx, EncInputStream_t *aEncInStrm, EncoderOut_t *aEncOut);
/**
* @brief flush codec while do fast forward playing.
* @param ctx The context of vpu codec
* @return 0 for flush success, others for failure.
*/
RK_S32 (*flush)(struct VpuCodecContext *ctx);
RK_S32 (*control)(struct VpuCodecContext *ctx, VPU_API_CMD cmdType, void* param);
/**
* @brief send video stream packet to decoder only, async interface
* @param ctx The context of vpu codec
* @param pkt Stream to be decoded
* @return 0 for success, others for failure.
*/
RK_S32 (*decode_sendstream)(struct VpuCodecContext *ctx, VideoPacket_t *pkt);
/**
* @brief get video frame from decoder only, async interface
* @param ctx The context of vpu codec
* @param aDecOut Decoding frame
* @return 0 for success, others for failure.
*/
RK_S32 (*decode_getframe)(struct VpuCodecContext *ctx, DecoderOut_t *aDecOut);
/**
* @brief send video frame to encoder only, async interface
* @param ctx The context of vpu codec
* @param aEncInStrm Frame to be encoded
* @return 0 for success, others for failure.
*/
RK_S32 (*encoder_sendframe)(struct VpuCodecContext *ctx, EncInputStream_t *aEncInStrm);
/**
* @brief get encoded video packet from encoder only, async interface
* @param ctx The context of vpu codec
* @param aEncOut Encoding stream
* @return 0 for success, others for failure.
*/
RK_S32 (*encoder_getstream)(struct VpuCodecContext *ctx, EncoderOut_t *aEncOut);
} VpuCodecContext_t;
/* allocated vpu codec context */
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief open context of vpu
* @param ctx pointer of vpu codec context
*/
RK_S32 vpu_open_context(struct VpuCodecContext **ctx);
/**
* @brief close context of vpu
* @param ctx pointer of vpu codec context
*/
RK_S32 vpu_close_context(struct VpuCodecContext **ctx);
#ifdef __cplusplus
}
#endif
/*
* vpu_mem api
*/
#define vpu_display_mem_pool_FIELDS \
RK_S32 (*commit_hdl)(vpu_display_mem_pool *p, RK_S32 hdl, RK_S32 size); \
void* (*get_free)(vpu_display_mem_pool *p); \
RK_S32 (*inc_used)(vpu_display_mem_pool *p, void *hdl); \
RK_S32 (*put_used)(vpu_display_mem_pool *p, void *hdl); \
RK_S32 (*reset)(vpu_display_mem_pool *p); \
RK_S32 (*get_unused_num)(vpu_display_mem_pool *p); \
RK_S32 buff_size;\
float version; \
RK_S32 res[18];
typedef struct vpu_display_mem_pool vpu_display_mem_pool;
struct vpu_display_mem_pool {
vpu_display_mem_pool_FIELDS
};
#ifdef __cplusplus
extern "C"
{
#endif
/*
* vpu memory handle interface
*/
RK_S32 VPUMemJudgeIommu(void);
RK_S32 VPUMallocLinear(VPUMemLinear_t *p, RK_U32 size);
RK_S32 VPUFreeLinear(VPUMemLinear_t *p);
RK_S32 VPUMemDuplicate(VPUMemLinear_t *dst, VPUMemLinear_t *src);
RK_S32 VPUMemLink(VPUMemLinear_t *p);
RK_S32 VPUMemFlush(VPUMemLinear_t *p);
RK_S32 VPUMemClean(VPUMemLinear_t *p);
RK_S32 VPUMemInvalidate(VPUMemLinear_t *p);
RK_S32 VPUMemGetFD(VPUMemLinear_t *p);
RK_S32 VPUMallocLinearFromRender(VPUMemLinear_t *p, RK_U32 size, void *ctx);
/*
* vpu memory allocator and manager interface
*/
vpu_display_mem_pool* open_vpu_memory_pool(void);
void close_vpu_memory_pool(vpu_display_mem_pool *p);
int create_vpu_memory_pool_allocator(vpu_display_mem_pool **ipool, int num, int size);
void release_vpu_memory_pool_allocator(vpu_display_mem_pool *ipool);
#ifdef __cplusplus
}
#endif
#endif /*__VPU_API_H__*/

View File

@@ -0,0 +1,380 @@
#include <stdio.h>
#include <sys/time.h>
#include "mpp_decoder.h"
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#define LOGD printf
// #define LOGD
static unsigned long GetCurrentTimeMS()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
MppDecoder::MppDecoder()
{
}
MppDecoder::~MppDecoder()
{
if (loop_data.packet)
{
mpp_packet_deinit(&loop_data.packet);
loop_data.packet = NULL;
}
if (frame)
{
mpp_frame_deinit(&frame);
frame = NULL;
}
if (mpp_ctx)
{
mpp_destroy(mpp_ctx);
mpp_ctx = NULL;
}
if (loop_data.frm_grp)
{
mpp_buffer_group_put(loop_data.frm_grp);
loop_data.frm_grp = NULL;
}
}
// MPP 解码器初始化
int MppDecoder::Init(int video_type, int fps, void *userdata)
{
MPP_RET ret = MPP_OK;
this->userdata = userdata;
this->fps = fps;
this->last_frame_time_ms = 0;
if (video_type == 264)
{
mpp_type = MPP_VIDEO_CodingAVC;
}
else if (video_type == 265)
{
mpp_type = MPP_VIDEO_CodingHEVC;
}
else
{
LOGD("unsupport video_type %d", video_type);
return -1;
}
LOGD("mpi_dec_test start ");
memset(&loop_data, 0, sizeof(loop_data));
LOGD("mpi_dec_test decoder test start mpp_type %d ", mpp_type);
MppDecCfg cfg = NULL;
MppCtx mpp_ctx = NULL;
ret = mpp_create(&mpp_ctx, &mpp_mpi);
if (MPP_OK != ret)
{
LOGD("mpp_create failed ");
return 0;
}
ret = mpp_init(mpp_ctx, MPP_CTX_DEC, mpp_type);
if (ret)
{
LOGD("%p mpp_init failed ", mpp_ctx);
return -1;
}
mpp_dec_cfg_init(&cfg);
/* get default config from decoder context */
ret = mpp_mpi->control(mpp_ctx, MPP_DEC_GET_CFG, cfg);
if (ret)
{
LOGD("%p failed to get decoder cfg ret %d ", mpp_ctx, ret);
return -1;
}
/*
* split_parse is to enable mpp internal frame spliter when the input
* packet is not aplited into frames.
*/
ret = mpp_dec_cfg_set_u32(cfg, "base:split_parse", need_split);
if (ret)
{
LOGD("%p failed to set split_parse ret %d ", mpp_ctx, ret);
return -1;
}
ret = mpp_mpi->control(mpp_ctx, MPP_DEC_SET_CFG, cfg);
if (ret)
{
LOGD("%p failed to set cfg %p ret %d ", mpp_ctx, cfg, ret);
return -1;
}
mpp_dec_cfg_deinit(cfg);
loop_data.ctx = mpp_ctx;
loop_data.mpi = mpp_mpi;
loop_data.eos = 0;
loop_data.packet_size = packet_size;
loop_data.frame = 0;
loop_data.frame_count = 0;
return 1;
}
int MppDecoder::Reset()
{
if (mpp_mpi != NULL)
{
mpp_mpi->reset(mpp_ctx);
}
return 0;
}
int MppDecoder::Decode(uint8_t *pkt_data, int pkt_size, int pkt_eos)
{
MpiDecLoopData *data = &loop_data;
RK_U32 pkt_done = 0;
RK_U32 err_info = 0;
MPP_RET ret = MPP_OK;
MppCtx ctx = data->ctx;
MppApi *mpi = data->mpi;
size_t read_size = 0;
size_t packet_size = data->packet_size;
//LOGD("receive packet size=%d ", pkt_size);
if (packet == NULL)
{
ret = mpp_packet_init(&packet, NULL, 0);
}
///////////////////////////////////////////////
// ret = mpp_packet_init(&packet, frame_data, frame_size);
mpp_packet_set_data(packet, pkt_data);
mpp_packet_set_size(packet, pkt_size);
mpp_packet_set_pos(packet, pkt_data);
mpp_packet_set_length(packet, pkt_size);
// setup eos flag
if (pkt_eos)
mpp_packet_set_eos(packet);
do
{
RK_S32 times = 5;
// send the packet first if packet is not done
if (!pkt_done)
{
ret = mpi->decode_put_packet(ctx, packet);
if (MPP_OK == ret)
pkt_done = 1;
}
// then get all available frame and release
do
{
RK_S32 get_frm = 0;
RK_U32 frm_eos = 0;
try_again:
ret = mpi->decode_get_frame(ctx, &frame);
if (MPP_ERR_TIMEOUT == ret)
{
if (times > 0)
{
times--;
usleep(2000);
goto try_again;
}
LOGD("decode_get_frame failed too much time ");
}
if (MPP_OK != ret)
{
LOGD("decode_get_frame failed ret %d ", ret);
break;
}
if (frame)
{
RK_U32 hor_stride = mpp_frame_get_hor_stride(frame);
RK_U32 ver_stride = mpp_frame_get_ver_stride(frame);
RK_U32 hor_width = mpp_frame_get_width(frame);
RK_U32 ver_height = mpp_frame_get_height(frame);
RK_U32 buf_size = mpp_frame_get_buf_size(frame);
RK_S64 pts = mpp_frame_get_pts(frame);
RK_S64 dts = mpp_frame_get_dts(frame);
// LOGD("decoder require buffer w:h [%d:%d] stride [%d:%d] buf_size %d pts=%lld dts=%lld ",
// hor_width, ver_height, hor_stride, ver_stride, buf_size, pts, dts);
if (mpp_frame_get_info_change(frame))
{
LOGD("decode_get_frame get info changed found ");
// ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_DRM);
// if (ret) {
// LOGD("get mpp buffer group failed ret %d ", ret);
// break;
// }
// mpi->control(ctx, MPP_DEC_SET_EXT_BUF_GROUP, data->frm_grp);
// mpi->control(ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
if (NULL == data->frm_grp)
{
/* If buffer group is not set create one and limit it */
ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_DRM);
if (ret)
{
LOGD("%p get mpp buffer group failed ret %d ", ctx, ret);
break;
}
/* Set buffer to mpp decoder */
ret = mpi->control(ctx, MPP_DEC_SET_EXT_BUF_GROUP, data->frm_grp);
if (ret)
{
LOGD("%p set buffer group failed ret %d ", ctx, ret);
break;
}
}
else
{
/* If old buffer group exist clear it */
ret = mpp_buffer_group_clear(data->frm_grp);
if (ret)
{
LOGD("%p clear buffer group failed ret %d ", ctx, ret);
break;
}
}
/* Use limit config to limit buffer count to 24 with buf_size */
ret = mpp_buffer_group_limit_config(data->frm_grp, buf_size, 24);
if (ret)
{
LOGD("%p limit buffer group failed ret %d ", ctx, ret);
break;
}
/*
* All buffer group config done. Set info change ready to let
* decoder continue decoding
*/
ret = mpi->control(ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
if (ret)
{
LOGD("%p info change ready failed ret %d ", ctx, ret);
break;
}
this->last_frame_time_ms = GetCurrentTimeMS();
}
else
{
err_info = mpp_frame_get_errinfo(frame) | mpp_frame_get_discard(frame);
if (err_info)
{
LOGD("decoder_get_frame get err info:%d discard:%d. ",
mpp_frame_get_errinfo(frame), mpp_frame_get_discard(frame));
}
data->frame_count++;
struct timeval tv;
gettimeofday(&tv, NULL);
// LOGD("get one frame %ld ", (tv.tv_sec * 1000 + tv.tv_usec / 1000));
// mpp_frame_get_width(frame);
// char *input_data =(char *) mpp_buffer_get_ptr(mpp_frame_get_buffer(frame));
if (callback != nullptr)
{
MppFrameFormat format = mpp_frame_get_fmt(frame);
char *data_vir = (char *)mpp_buffer_get_ptr(mpp_frame_get_buffer(frame));
int fd = mpp_buffer_get_fd(mpp_frame_get_buffer(frame));
// LOGD("data_vir=%p fd=%d ", data_vir, fd);
callback(this->userdata, hor_stride, ver_stride, hor_width, ver_height, format, fd, data_vir);
}
unsigned long cur_time_ms = GetCurrentTimeMS();
long time_gap = 1000 / this->fps - (cur_time_ms - this->last_frame_time_ms);
// LOGD("time_gap=%ld", time_gap);
if (time_gap > 0)
{
usleep(time_gap * 1000);
}
this->last_frame_time_ms = GetCurrentTimeMS();
}
frm_eos = mpp_frame_get_eos(frame);
ret = mpp_frame_deinit(&frame);
frame = NULL;
// if(frame_pre!=NULL)
// {
// mpp_frame_deinit(&frame_pre);
// }
// &frame_pre=&frame;
get_frm = 1;
}
// try get runtime frame memory usage
if (data->frm_grp)
{
size_t usage = mpp_buffer_group_usage(data->frm_grp);
if (usage > data->max_usage)
data->max_usage = usage;
}
// if last packet is send but last frame is not found continue
if (pkt_eos && pkt_done && !frm_eos)
{
usleep(1 * 1000);
continue;
}
if (frm_eos)
{
LOGD("found last frame ");
break;
}
if (data->frame_num > 0 && data->frame_count >= data->frame_num)
{
data->eos = 1;
break;
}
if (get_frm)
continue;
break;
} while (1);
if (data->frame_num > 0 && data->frame_count >= data->frame_num)
{
data->eos = 1;
LOGD("reach max frame number %d ", data->frame_count);
break;
}
if (pkt_done)
break;
/*
* why sleep here:
* mpi->decode_put_packet will failed when packet in internal queue is
* full,waiting the package is consumed .Usually hardware decode one
* frame which resolution is 1080p needs 2 ms,so here we sleep 3ms
* * is enough.
*/
usleep(3 * 1000);
} while (1);
mpp_packet_deinit(&packet);
return ret;
}
int MppDecoder::SetCallback(MppDecoderFrameCallback callback)
{
this->callback = callback;
return 0;
}

View File

@@ -0,0 +1,69 @@
#ifndef __MPP_DECODER_H__
#define __MPP_DECODER_H__
#include <string.h>
#include "rk_mpi.h"
#include "mpp_frame.h"
#include <string.h>
#include <pthread.h>
#define MPI_DEC_STREAM_SIZE (SZ_4K)
#define MPI_DEC_LOOP_COUNT 4
#define MAX_FILE_NAME_LENGTH 256
typedef void (*MppDecoderFrameCallback)(void* userdata, int width_stride, int height_stride, int width, int height, int format, int fd, void* data);
typedef struct
{
MppCtx ctx;
MppApi *mpi;
RK_U32 eos;
char *buf;
MppBufferGroup frm_grp;
MppBufferGroup pkt_grp;
MppPacket packet;
size_t packet_size;
MppFrame frame;
RK_S32 frame_count;
RK_S32 frame_num;
size_t max_usage;
} MpiDecLoopData;
class MppDecoder
{
public:
MppCtx mpp_ctx = NULL;
MppApi *mpp_mpi = NULL;
MppDecoder();
~MppDecoder();
int Init(int video_type, int fps, void* userdata);
int SetCallback(MppDecoderFrameCallback callback);
int Decode(uint8_t* pkt_data, int pkt_size, int pkt_eos);
int Reset();
private:
// base flow context
MpiCmd mpi_cmd = MPP_CMD_BASE;
MppParam mpp_param1 = NULL;
RK_U32 need_split = 1;
RK_U32 width_mpp ;
RK_U32 height_mpp ;
MppCodingType mpp_type;
size_t packet_size = 2400*1300*3/2;
MpiDecLoopData loop_data;
// bool vedio_type;//判断vedio是h264/h265
MppPacket packet = NULL;
MppFrame frame = NULL;
pthread_t th=NULL;
MppDecoderFrameCallback callback;
int fps = -1;
unsigned long last_frame_time_ms = 0;
void* userdata = NULL;
};
size_t mpp_frame_get_buf_size(const MppFrame s);
size_t mpp_buffer_group_usage(MppBufferGroup group);
#endif //__MPP_DECODER_H__

View File

@@ -0,0 +1,864 @@
#include <stdio.h>
#include <string.h>
#include "mpp_encoder.h"
#include "mpp_buffer.h"
#define MPP_ALIGN(x, a) (((x) + (a)-1) & ~((a)-1))
#define SZ_4K 4096
#define LOGD printf
// #define LOGD
#define LOGE printf
int MppEncoder::InitParams(MppEncoderParams &params)
{
memcpy(&enc_params, &params, sizeof(MppEncoderParams));
// get paramter from cmd
if (enc_params.hor_stride == 0)
{
enc_params.hor_stride = MPP_ALIGN(enc_params.width, 16);
}
if (enc_params.ver_stride == 0)
{
enc_params.ver_stride = (MPP_ALIGN(enc_params.height, 16));
}
if (enc_params.fps_in_den == 0)
enc_params.fps_in_den = 1;
if (enc_params.fps_in_num == 0)
enc_params.fps_in_num = 30;
if (enc_params.fps_out_den == 0)
enc_params.fps_out_den = 1;
if (enc_params.fps_out_num == 0)
enc_params.fps_out_num = 30;
if (!enc_params.bps)
enc_params.bps = enc_params.width * enc_params.height / 8 * (enc_params.fps_out_num / enc_params.fps_out_den);
this->mdinfo_size = (MPP_VIDEO_CodingHEVC == enc_params.type) ? (MPP_ALIGN(enc_params.hor_stride, 32) >> 5) *
(MPP_ALIGN(enc_params.ver_stride, 32) >> 5) * 16
: (MPP_ALIGN(enc_params.hor_stride, 64) >> 6) *
(MPP_ALIGN(enc_params.ver_stride, 16) >> 4) * 16;
// update resource parameter
switch (enc_params.fmt & MPP_FRAME_FMT_MASK)
{
case MPP_FMT_YUV420SP:
case MPP_FMT_YUV420P:
{
this->frame_size = MPP_ALIGN(enc_params.hor_stride, 64) * MPP_ALIGN(enc_params.ver_stride, 64) * 3 / 2;
}
break;
case MPP_FMT_YUV422_YUYV:
case MPP_FMT_YUV422_YVYU:
case MPP_FMT_YUV422_UYVY:
case MPP_FMT_YUV422_VYUY:
case MPP_FMT_YUV422P:
case MPP_FMT_YUV422SP:
{
this->frame_size = MPP_ALIGN(enc_params.hor_stride, 64) * MPP_ALIGN(enc_params.ver_stride, 64) * 2;
}
break;
case MPP_FMT_RGB444:
case MPP_FMT_BGR444:
case MPP_FMT_RGB555:
case MPP_FMT_BGR555:
case MPP_FMT_RGB565:
case MPP_FMT_BGR565:
case MPP_FMT_RGB888:
case MPP_FMT_BGR888:
case MPP_FMT_RGB101010:
case MPP_FMT_BGR101010:
case MPP_FMT_ARGB8888:
case MPP_FMT_ABGR8888:
case MPP_FMT_BGRA8888:
case MPP_FMT_RGBA8888:
{
this->frame_size = MPP_ALIGN(enc_params.hor_stride, 64) * MPP_ALIGN(enc_params.ver_stride, 64);
}
break;
default:
{
this->frame_size = MPP_ALIGN(enc_params.hor_stride, 64) * MPP_ALIGN(enc_params.ver_stride, 64) * 4;
}
break;
}
if (MPP_FRAME_FMT_IS_FBC(enc_params.fmt))
{
if ((enc_params.fmt & MPP_FRAME_FBC_MASK) == MPP_FRAME_FBC_AFBC_V1)
this->header_size = MPP_ALIGN(MPP_ALIGN(enc_params.width, 16) * MPP_ALIGN(enc_params.height, 16) / 16, SZ_4K);
else
this->header_size = MPP_ALIGN(enc_params.width, 16) * MPP_ALIGN(enc_params.height, 16) / 16;
}
else
{
this->header_size = 0;
}
return 0;
}
int MppEncoder::SetupEncCfg()
{
MPP_RET ret;
ret = mpp_enc_cfg_init(&cfg);
if (ret)
{
LOGE("mpp_enc_cfg_init failed ret %d\n", ret);
return -1;
}
/* setup default parameter */
if (enc_params.fps_in_den == 0)
enc_params.fps_in_den = 1;
if (enc_params.fps_in_num == 0)
enc_params.fps_in_num = 30;
if (enc_params.fps_out_den == 0)
enc_params.fps_out_den = 1;
if (enc_params.fps_out_num == 0)
enc_params.fps_out_num = 30;
if (!enc_params.bps)
enc_params.bps = enc_params.width * enc_params.height / 8 * (enc_params.fps_out_num / enc_params.fps_out_den);
mpp_enc_cfg_set_s32(cfg, "prep:width", enc_params.width);
mpp_enc_cfg_set_s32(cfg, "prep:height", enc_params.height);
mpp_enc_cfg_set_s32(cfg, "prep:hor_stride", enc_params.hor_stride);
mpp_enc_cfg_set_s32(cfg, "prep:ver_stride", enc_params.ver_stride);
mpp_enc_cfg_set_s32(cfg, "prep:format", enc_params.fmt);
mpp_enc_cfg_set_s32(cfg, "rc:mode", enc_params.rc_mode);
/* fix input / output frame rate */
mpp_enc_cfg_set_s32(cfg, "rc:fps_in_flex", enc_params.fps_in_flex);
mpp_enc_cfg_set_s32(cfg, "rc:fps_in_num", enc_params.fps_in_num);
mpp_enc_cfg_set_s32(cfg, "rc:fps_in_denorm", enc_params.fps_in_den);
mpp_enc_cfg_set_s32(cfg, "rc:fps_out_flex", enc_params.fps_out_flex);
mpp_enc_cfg_set_s32(cfg, "rc:fps_out_num", enc_params.fps_out_num);
mpp_enc_cfg_set_s32(cfg, "rc:fps_out_denorm", enc_params.fps_out_den);
mpp_enc_cfg_set_s32(cfg, "rc:gop", enc_params.gop_len ? enc_params.gop_len : enc_params.fps_out_num * 2);
/* drop frame or not when bitrate overflow */
mpp_enc_cfg_set_u32(cfg, "rc:drop_mode", MPP_ENC_RC_DROP_FRM_DISABLED);
mpp_enc_cfg_set_u32(cfg, "rc:drop_thd", 20); /* 20% of max bps */
mpp_enc_cfg_set_u32(cfg, "rc:drop_gap", 1); /* Do not continuous drop frame */
/* setup bitrate for different rc_mode */
mpp_enc_cfg_set_s32(cfg, "rc:bps_target", enc_params.bps);
switch (enc_params.rc_mode)
{
case MPP_ENC_RC_MODE_FIXQP:
{
/* do not setup bitrate on FIXQP mode */
}
break;
case MPP_ENC_RC_MODE_CBR:
{
/* CBR mode has narrow bound */
mpp_enc_cfg_set_s32(cfg, "rc:bps_max", enc_params.bps_max ? enc_params.bps_max : enc_params.bps * 17 / 16);
mpp_enc_cfg_set_s32(cfg, "rc:bps_min", enc_params.bps_min ? enc_params.bps_min : enc_params.bps * 15 / 16);
}
break;
case MPP_ENC_RC_MODE_VBR:
case MPP_ENC_RC_MODE_AVBR:
{
/* VBR mode has wide bound */
mpp_enc_cfg_set_s32(cfg, "rc:bps_max", enc_params.bps_max ? enc_params.bps_max : enc_params.bps * 17 / 16);
mpp_enc_cfg_set_s32(cfg, "rc:bps_min", enc_params.bps_min ? enc_params.bps_min : enc_params.bps * 1 / 16);
}
break;
default:
{
/* default use CBR mode */
mpp_enc_cfg_set_s32(cfg, "rc:bps_max", enc_params.bps_max ? enc_params.bps_max : enc_params.bps * 17 / 16);
mpp_enc_cfg_set_s32(cfg, "rc:bps_min", enc_params.bps_min ? enc_params.bps_min : enc_params.bps * 15 / 16);
}
break;
}
/* setup qp for different codec and rc_mode */
switch (enc_params.type)
{
case MPP_VIDEO_CodingAVC:
case MPP_VIDEO_CodingHEVC:
{
switch (enc_params.rc_mode)
{
case MPP_ENC_RC_MODE_FIXQP:
{
RK_S32 fix_qp = enc_params.qp_init;
mpp_enc_cfg_set_s32(cfg, "rc:qp_init", fix_qp);
mpp_enc_cfg_set_s32(cfg, "rc:qp_max", fix_qp);
mpp_enc_cfg_set_s32(cfg, "rc:qp_min", fix_qp);
mpp_enc_cfg_set_s32(cfg, "rc:qp_max_i", fix_qp);
mpp_enc_cfg_set_s32(cfg, "rc:qp_min_i", fix_qp);
mpp_enc_cfg_set_s32(cfg, "rc:qp_ip", 0);
}
break;
case MPP_ENC_RC_MODE_CBR:
case MPP_ENC_RC_MODE_VBR:
case MPP_ENC_RC_MODE_AVBR:
{
mpp_enc_cfg_set_s32(cfg, "rc:qp_init", -1);
mpp_enc_cfg_set_s32(cfg, "rc:qp_max", 51);
mpp_enc_cfg_set_s32(cfg, "rc:qp_min", 10);
mpp_enc_cfg_set_s32(cfg, "rc:qp_max_i", 51);
mpp_enc_cfg_set_s32(cfg, "rc:qp_min_i", 10);
mpp_enc_cfg_set_s32(cfg, "rc:qp_ip", 2);
}
break;
default:
{
LOGE("unsupport encoder rc mode %d\n", enc_params.rc_mode);
}
break;
}
}
break;
case MPP_VIDEO_CodingVP8:
{
/* vp8 only setup base qp range */
mpp_enc_cfg_set_s32(cfg, "rc:qp_init", 40);
mpp_enc_cfg_set_s32(cfg, "rc:qp_max", 127);
mpp_enc_cfg_set_s32(cfg, "rc:qp_min", 0);
mpp_enc_cfg_set_s32(cfg, "rc:qp_max_i", 127);
mpp_enc_cfg_set_s32(cfg, "rc:qp_min_i", 0);
mpp_enc_cfg_set_s32(cfg, "rc:qp_ip", 6);
}
break;
case MPP_VIDEO_CodingMJPEG:
{
/* jpeg use special codec config to control qtable */
mpp_enc_cfg_set_s32(cfg, "jpeg:q_factor", 80);
mpp_enc_cfg_set_s32(cfg, "jpeg:qf_max", 99);
mpp_enc_cfg_set_s32(cfg, "jpeg:qf_min", 1);
}
break;
default:
{
}
break;
}
/* setup codec */
mpp_enc_cfg_set_s32(cfg, "codec:type", enc_params.type);
switch (enc_params.type)
{
case MPP_VIDEO_CodingAVC:
{
RK_U32 constraint_set = enc_params.constraint_set;
/*
* H.264 profile_idc parameter
* 66 - Baseline profile
* 77 - Main profile
* 100 - High profile
*/
mpp_enc_cfg_set_s32(cfg, "h264:profile", 100);
/*
* H.264 level_idc parameter
* 10 / 11 / 12 / 13 - qcif@15fps / cif@7.5fps / cif@15fps / cif@30fps
* 20 / 21 / 22 - cif@30fps / half-D1@@25fps / D1@12.5fps
* 30 / 31 / 32 - D1@25fps / 720p@30fps / 720p@60fps
* 40 / 41 / 42 - 1080p@30fps / 1080p@30fps / 1080p@60fps
* 50 / 51 / 52 - 4K@30fps
*/
mpp_enc_cfg_set_s32(cfg, "h264:level", 40);
mpp_enc_cfg_set_s32(cfg, "h264:cabac_en", 1);
mpp_enc_cfg_set_s32(cfg, "h264:cabac_idc", 0);
mpp_enc_cfg_set_s32(cfg, "h264:trans8x8", 1);
if (constraint_set & 0x3f0000)
mpp_enc_cfg_set_s32(cfg, "h264:constraint_set", constraint_set);
}
break;
case MPP_VIDEO_CodingHEVC:
case MPP_VIDEO_CodingMJPEG:
case MPP_VIDEO_CodingVP8:
{
}
break;
default:
{
LOGE("unsupport encoder coding type %d\n", enc_params.type);
}
break;
}
if (enc_params.split_mode)
{
LOGD("%p split mode %d arg %d out %d\n", mpp_ctx,
enc_params.split_mode, enc_params.split_arg, enc_params.split_out);
mpp_enc_cfg_set_s32(cfg, "split:mode", enc_params.split_mode);
mpp_enc_cfg_set_s32(cfg, "split:arg", enc_params.split_arg);
mpp_enc_cfg_set_s32(cfg, "split:out", enc_params.split_out);
}
mpp_enc_cfg_set_s32(cfg, "prep:mirroring", enc_params.mirroring);
mpp_enc_cfg_set_s32(cfg, "prep:rotation", enc_params.rotation);
mpp_enc_cfg_set_s32(cfg, "prep:flip", enc_params.flip);
ret = mpp_mpi->control(mpp_ctx, MPP_ENC_SET_CFG, cfg);
if (ret)
{
LOGE("mpi control enc set cfg failed ret %d\n", ret);
goto RET;
}
#if 0
/* optional */
{
ret = mpp_mpi->control(mpp_ctx, MPP_ENC_SET_SEI_CFG, &enc_params.sei_mode);
if (ret) {
LOGE("mpi control enc set sei cfg failed ret %d\n", ret);
goto RET;
}
}
#endif
if (enc_params.type == MPP_VIDEO_CodingAVC || enc_params.type == MPP_VIDEO_CodingHEVC)
{
enc_params.header_mode = MPP_ENC_HEADER_MODE_EACH_IDR;
ret = mpp_mpi->control(mpp_ctx, MPP_ENC_SET_HEADER_MODE, &enc_params.header_mode);
if (ret)
{
LOGE("mpi control enc set header mode failed ret %d\n", ret);
goto RET;
}
}
#if 0
RK_U32 gop_mode = enc_params.gop_mode;
if (gop_mode) {
MppEncRefCfg ref;
mpp_enc_ref_cfg_init(&ref);
if (enc_params.gop_mode < 4)
mpi_enc_gen_ref_cfg(ref, gop_mode);
else
mpi_enc_gen_smart_gop_ref_cfg(ref, enc_params.gop_len, enc_params.vi_len);
ret = mpp_mpi->control(mpp_ctx, MPP_ENC_SET_REF_CFG, ref);
if (ret) {
LOGE("mpi control enc set ref cfg failed ret %d\n", ret);
goto RET;
}
mpp_enc_ref_cfg_deinit(&ref);
}
if (enc_params.roi_enable) {
mpp_enc_roi_init(&enc_params.roi_mpp_ctx, enc_params.width, enc_params.height, enc_params.type, 4);
mpp_assert(enc_params.roi_mpp_ctx);
}
#endif
RET:
return ret;
}
MppEncoder::MppEncoder()
{
memset(&enc_params, 0, sizeof(MppEncoderParams));
this->mpp_ctx = NULL;
this->mpp_mpi = NULL;
memset(&osd_data, 0, sizeof(MppEncOSDData));
}
MppEncoder::~MppEncoder()
{
if (this->mpp_ctx)
{
mpp_destroy(this->mpp_ctx);
this->mpp_ctx = NULL;
}
if (this->cfg)
{
mpp_enc_cfg_deinit(this->cfg);
this->cfg = NULL;
}
if (this->frm_buf)
{
mpp_buffer_put(this->frm_buf);
this->frm_buf = NULL;
}
if (this->pkt_buf)
{
mpp_buffer_put(this->pkt_buf);
this->pkt_buf = NULL;
}
if (this->md_info)
{
mpp_buffer_put(this->md_info);
this->md_info = NULL;
}
if (this->osd_data.buf)
{
mpp_buffer_put(this->osd_data.buf);
this->osd_data.buf = NULL;
}
if (this->buf_grp)
{
mpp_buffer_group_put(this->buf_grp);
this->buf_grp = NULL;
}
}
int MppEncoder::Init(MppEncoderParams &params, void *userdata)
{
int ret;
MppPollType timeout = MPP_POLL_BLOCK;
this->userdata = userdata;
this->InitParams(params);
ret = mpp_buffer_group_get_internal(&this->buf_grp, MPP_BUFFER_TYPE_DRM);
if (ret)
{
LOGD("failed to get mpp buffer group ret %d\n", ret);
goto MPP_TEST_OUT;
}
ret = mpp_buffer_get(this->buf_grp, &this->pkt_buf, this->frame_size);
if (ret)
{
LOGD("failed to get buffer for output packet ret %d\n", ret);
goto MPP_TEST_OUT;
}
ret = mpp_buffer_get(this->buf_grp, &this->md_info, this->mdinfo_size);
if (ret)
{
LOGD("failed to get buffer for motion info output packet ret %d\n", ret);
goto MPP_TEST_OUT;
}
// encoder demo
ret = mpp_create(&this->mpp_ctx, &this->mpp_mpi);
if (ret)
{
LOGE("mpp_create failed ret %d\n", ret);
goto MPP_TEST_OUT;
}
LOGD("%p encoder test start w %d h %d type %d\n",
this->mpp_ctx, enc_params.width, enc_params.height, enc_params.type);
ret = mpp_mpi->control(mpp_ctx, MPP_SET_OUTPUT_TIMEOUT, &timeout);
if (MPP_OK != ret)
{
LOGE("mpi control set output timeout %d ret %d\n", timeout, ret);
goto MPP_TEST_OUT;
}
ret = mpp_init(mpp_ctx, MPP_CTX_ENC, enc_params.type);
if (ret)
{
LOGE("mpp_init failed ret %d\n", ret);
goto MPP_TEST_OUT;
}
this->SetupEncCfg();
MPP_TEST_OUT:
if (ret)
{
if (this->mpp_ctx)
{
mpp_destroy(this->mpp_ctx);
this->mpp_ctx = NULL;
}
if (this->cfg)
{
mpp_enc_cfg_deinit(this->cfg);
this->cfg = NULL;
}
if (this->frm_buf)
{
mpp_buffer_put(this->frm_buf);
this->frm_buf = NULL;
}
if (this->pkt_buf)
{
mpp_buffer_put(this->pkt_buf);
this->pkt_buf = NULL;
}
if (this->md_info)
{
mpp_buffer_put(this->md_info);
this->md_info = NULL;
}
if (this->osd_data.buf)
{
mpp_buffer_put(this->osd_data.buf);
this->osd_data.buf = NULL;
}
if (this->buf_grp)
{
mpp_buffer_group_put(this->buf_grp);
this->buf_grp = NULL;
}
// if (this->roi_ctx) {
// mpp_enc_roi_deinit(this->roi_ctx);
// this->roi_ctx = NULL;
// }
}
return ret;
}
int MppEncoder::SetCallback(MppEncoderFrameCallback callback)
{
this->callback = callback;
return 0;
}
int MppEncoder::GetHeader(char *enc_buf, int max_size)
{
int ret;
void *out_ptr = enc_buf;
size_t out_len = 0;
if (enc_params.type == MPP_VIDEO_CodingAVC || enc_params.type == MPP_VIDEO_CodingHEVC)
{
MppPacket packet = NULL;
/*
* Can use packet with normal malloc buffer as input not pkt_buf.
* Please refer to vpu_api_legacy.cpp for normal buffer case.
* Using pkt_buf buffer here is just for simplifing demo.
*/
mpp_packet_init_with_buffer(&packet, this->pkt_buf);
/* NOTE: It is important to clear output packet length!! */
mpp_packet_set_length(packet, 0);
ret = mpp_mpi->control(mpp_ctx, MPP_ENC_GET_HDR_SYNC, packet);
if (ret)
{
LOGD("mpi control enc get extra info failed\n");
return -1;
}
else
{
/* get and write sps/pps for H.264 */
void *ptr = mpp_packet_get_pos(packet);
size_t len = mpp_packet_get_length(packet);
memcpy(out_ptr, ptr, len);
out_ptr = (char *)(out_ptr) + len;
out_len += len;
}
mpp_packet_deinit(&packet);
}
return out_len;
}
int MppEncoder::Encode(void *mpp_buf, char *enc_buf, int max_size)
{
MPP_RET ret;
void *out_ptr = enc_buf;
size_t out_len = 0;
MppMeta meta = NULL;
MppFrame frame = NULL;
MppPacket packet = NULL;
// void *buf = mpp_buffer_get_ptr(this->frm_buf);
// RK_S32 cam_frm_idx = -1;
// MppBuffer cam_buf = NULL;
RK_U32 eoi = 1;
RK_U32 frm_eos = 0;
ret = mpp_frame_init(&frame);
if (ret)
{
LOGD("mpp_frame_init failed\n");
return -1;
}
mpp_frame_set_width(frame, enc_params.width);
mpp_frame_set_height(frame, enc_params.height);
mpp_frame_set_hor_stride(frame, enc_params.hor_stride);
mpp_frame_set_ver_stride(frame, enc_params.ver_stride);
mpp_frame_set_fmt(frame, enc_params.fmt);
mpp_frame_set_eos(frame, frm_eos);
mpp_frame_set_buffer(frame, mpp_buf);
meta = mpp_frame_get_meta(frame);
mpp_packet_init_with_buffer(&packet, pkt_buf);
/* NOTE: It is important to clear output packet length!! */
mpp_packet_set_length(packet, 0);
mpp_meta_set_packet(meta, KEY_OUTPUT_PACKET, packet);
mpp_meta_set_buffer(meta, KEY_MOTION_INFO, this->md_info);
#if 0
if (enc_params.osd_enable || enc_params.user_data_enable || enc_params.roi_enable) {
if (enc_params.user_data_enable) {
MppEncUserData user_data;
char *str = "this is user data\n";
if ((enc_params.frame_count & 10) == 0) {
user_data.pdata = str;
user_data.len = strlen(str) + 1;
mpp_meta_set_ptr(meta, KEY_USER_DATA, &user_data);
}
static RK_U8 uuid_debug_info[16] = {
0x57, 0x68, 0x97, 0x80, 0xe7, 0x0c, 0x4b, 0x65,
0xa9, 0x06, 0xae, 0x29, 0x94, 0x11, 0xcd, 0x9a
};
MppEncUserDataSet data_group;
MppEncUserDataFull datas[2];
char *str1 = "this is user data 1\n";
char *str2 = "this is user data 2\n";
data_group.count = 2;
datas[0].len = strlen(str1) + 1;
datas[0].pdata = str1;
datas[0].uuid = uuid_debug_info;
datas[1].len = strlen(str2) + 1;
datas[1].pdata = str2;
datas[1].uuid = uuid_debug_info;
data_group.datas = datas;
mpp_meta_set_ptr(meta, KEY_USER_DATAS, &data_group);
}
if (enc_params.osd_enable) {
/* gen and cfg osd plt */
mpi_enc_gen_osd_plt(&enc_params.osd_plt, enc_params.frame_count);
enc_params.osd_plt_cfg.change = MPP_ENC_OSD_PLT_CFG_CHANGE_ALL;
enc_params.osd_plt_cfg.type = MPP_ENC_OSD_PLT_TYPE_USERDEF;
enc_params.osd_plt_cfg.plt = &enc_params.osd_plt;
ret = mpp_mpi->control(mpp_ctx, MPP_ENC_SET_OSD_PLT_CFG, &enc_params.osd_plt_cfg);
if (ret) {
LOGD("mpi control enc set osd plt failed ret %d\n", ret);
goto RET;
}
/* gen and cfg osd plt */
mpi_enc_gen_osd_data(&enc_params.osd_data, enc_params.buf_grp, enc_params.width,
enc_params.height, enc_params.frame_count);
mpp_meta_set_ptr(meta, KEY_OSD_DATA, (void*)&enc_params.osd_data);
}
if (enc_params.roi_enable) {
RoiRegionCfg *region = &enc_params.roi_region;
/* calculated in pixels */
region->x = MPP_ALIGN(enc_params.width / 8, 16);
region->y = MPP_ALIGN(enc_params.height / 8, 16);
region->w = 128;
region->h = 256;
region->force_intra = 0;
region->qp_mode = 1;
region->qp_val = 24;
mpp_enc_roi_add_region(enc_params.roi_mpp_ctx, region);
region->x = MPP_ALIGN(enc_params.width / 2, 16);
region->y = MPP_ALIGN(enc_params.height / 4, 16);
region->w = 256;
region->h = 128;
region->force_intra = 1;
region->qp_mode = 1;
region->qp_val = 10;
mpp_enc_roi_add_region(enc_params.roi_mpp_ctx, region);
/* send roi info by metadata */
mpp_enc_roi_setup_meta(enc_params.roi_mpp_ctx, meta);
}
}
#endif
/*
* NOTE: in non-block mode the frame can be resent.
* The default input timeout mode is block.
*
* User should release the input frame to meet the requirements of
* resource creator must be the resource destroyer.
*/
ret = mpp_mpi->encode_put_frame(mpp_ctx, frame);
mpp_frame_deinit(&frame);
if (ret)
{
LOGD("chn %d encode put frame failed\n", chn);
return -1;
}
do
{
ret = mpp_mpi->encode_get_packet(mpp_ctx, &packet);
if (ret)
{
LOGD("chn %d encode get packet failed\n", chn);
return -1;
}
// mpp_assert(packet);
if (packet)
{
// write packet to file here
void *ptr = mpp_packet_get_pos(packet);
size_t len = mpp_packet_get_length(packet);
char log_buf[256];
RK_S32 log_size = sizeof(log_buf) - 1;
RK_S32 log_len = 0;
// if (!enc_params.first_pkt)
// enc_params.first_pkt = mpp_time();
RK_U32 pkt_eos = mpp_packet_get_eos(packet);
/* set encode result */
if (this->callback != nullptr)
{
this->callback(this->userdata, (const char *)ptr, len);
}
if (enc_buf != nullptr && max_size > 0)
{
if (out_len + log_len < max_size)
{
memcpy(out_ptr, ptr, len);
out_len += len;
out_ptr = (char *)out_ptr + len;
}
else
{
LOGE("error enc_buf no enought");
}
}
// log_len += snprintf(log_buf + log_len, log_size - log_len,
// "encoded frame %-4d", enc_params.frame_count);
/* for low delay partition encoding */
if (mpp_packet_is_partition(packet))
{
eoi = mpp_packet_is_eoi(packet);
// log_len += snprintf(log_buf + log_len, log_size - log_len,
// " pkt %d", enc_params.frm_pkt_cnt);
// enc_params.frm_pkt_cnt = (eoi) ? (0) : (enc_params.frm_pkt_cnt + 1);
}
log_len += snprintf(log_buf + log_len, log_size - log_len,
" size %-7zu", len);
if (mpp_packet_has_meta(packet))
{
meta = mpp_packet_get_meta(packet);
RK_S32 temporal_id = 0;
RK_S32 lt_idx = -1;
RK_S32 avg_qp = -1;
if (MPP_OK == mpp_meta_get_s32(meta, KEY_TEMPORAL_ID, &temporal_id))
log_len += snprintf(log_buf + log_len, log_size - log_len,
" tid %d", temporal_id);
if (MPP_OK == mpp_meta_get_s32(meta, KEY_LONG_REF_IDX, &lt_idx))
log_len += snprintf(log_buf + log_len, log_size - log_len,
" lt %d", lt_idx);
if (MPP_OK == mpp_meta_get_s32(meta, KEY_ENC_AVERAGE_QP, &avg_qp))
log_len += snprintf(log_buf + log_len, log_size - log_len,
" qp %d", avg_qp);
}
// LOGD("chn %d %s\n", chn, log_buf);
mpp_packet_deinit(&packet);
// enc_params.stream_size += len;
// enc_params.frame_count += eoi;
// if (enc_params.pkt_eos) {
// LOGD("chn %d found last packet\n", chn);
// mpp_assert(enc_params.frm_eos);
// }
}
} while (!eoi);
// if (enc_params.frm_eos && enc_params.pkt_eos)
// break;
return out_len;
}
int MppEncoder::Reset()
{
if (mpp_mpi != NULL)
{
mpp_mpi->reset(mpp_ctx);
}
return 0;
}
size_t MppEncoder::GetFrameSize()
{
return this->frame_size;
}
void *MppEncoder::ImportBuffer(int index, size_t size, int fd, int type)
{
MppBuffer buf;
MppBufferInfo info;
memset(&info, 0, sizeof(MppBufferInfo));
info.type = (MppBufferType)type; // MPP_BUFFER_TYPE_EXT_DMA
info.fd = fd;
info.size = size;
info.index = index;
mpp_buffer_import(&buf, &info);
return buf;
}
void *MppEncoder::GetInputFrameBuffer()
{
int ret;
if (this->frm_buf == nullptr)
{
ret = mpp_buffer_get(this->buf_grp, &this->frm_buf, this->frame_size);
if (ret)
{
LOGD("failed to get buffer for input frame ret %d\n", ret);
return NULL;
}
}
return this->frm_buf;
}
int MppEncoder::GetInputFrameBufferFd(void *mpp_buffer)
{
return mpp_buffer_get_fd(mpp_buffer);
}
void *MppEncoder::GetInputFrameBufferAddr(void *mpp_buffer)
{
return mpp_buffer_get_ptr(mpp_buffer);
}

View File

@@ -0,0 +1,127 @@
#ifndef __MPP_ENCODER_H__
#define __MPP_ENCODER_H__
#include "mpp_frame.h"
#include "rk_mpi.h"
#include <pthread.h>
#include <string.h>
typedef void (*MppEncoderFrameCallback)(void* userdata, const char* data, int size);
typedef struct
{
RK_U32 width;
RK_U32 height;
RK_U32 hor_stride;
RK_U32 ver_stride;
MppFrameFormat fmt;
MppCodingType type;
RK_U32 osd_enable;
RK_U32 osd_mode;
RK_U32 split_mode;
RK_U32 split_arg;
RK_U32 split_out;
RK_U32 user_data_enable;
RK_U32 roi_enable;
// rate control runtime parameter
RK_S32 fps_in_flex;
RK_S32 fps_in_den;
RK_S32 fps_in_num;
RK_S32 fps_out_flex;
RK_S32 fps_out_den;
RK_S32 fps_out_num;
RK_S32 bps;
RK_S32 bps_max;
RK_S32 bps_min;
RK_S32 rc_mode;
RK_S32 gop_mode;
RK_S32 gop_len;
RK_S32 vi_len;
/* general qp control */
RK_S32 qp_init;
RK_S32 qp_max;
RK_S32 qp_max_i;
RK_S32 qp_min;
RK_S32 qp_min_i;
RK_S32 qp_max_step; /* delta qp between each two P frame */
RK_S32 qp_delta_ip; /* delta qp between I and P */
RK_S32 qp_delta_vi; /* delta qp between vi and P */
RK_U32 constraint_set;
RK_U32 rotation;
RK_U32 mirroring;
RK_U32 flip;
MppEncHeaderMode header_mode;
MppEncSeiMode sei_mode;
} MppEncoderParams;
class MppEncoder {
public:
MppEncoder();
~MppEncoder();
int Init(MppEncoderParams& params, void* userdata);
int SetCallback(MppEncoderFrameCallback callback);
int Encode(void* mpp_buf, char* enc_buf, int max_size);
int GetHeader(char* enc_buf, int max_size);
int Reset();
void* ImportBuffer(int index, size_t size, int fd, int type);
size_t GetFrameSize();
void* GetInputFrameBuffer();
int GetInputFrameBufferFd(void* mpp_buffer);
void* GetInputFrameBufferAddr(void* mpp_buffer);
private:
int InitParams(MppEncoderParams& params);
int SetupEncCfg();
MppCtx mpp_ctx = NULL;
MppApi* mpp_mpi = NULL;
RK_S32 chn = 0;
MppEncoderFrameCallback callback = NULL;
// global flow control flag
// RK_U32 frm_eos = 0;
// RK_U32 pkt_eos = 0;
// RK_U32 frm_pkt_cnt = 0;
// RK_S32 frame_num = 0;
// RK_S32 frame_count = 0;
// RK_U64 stream_size = 0;
/* encoder config set */
MppEncCfg cfg = NULL;
MppEncPrepCfg prep_cfg;
MppEncRcCfg rc_cfg;
MppEncCodecCfg codec_cfg;
MppEncSliceSplit split_cfg;
MppEncOSDPltCfg osd_plt_cfg;
MppEncOSDPlt osd_plt;
MppEncOSDData osd_data;
// RoiRegionCfg roi_region;
MppEncROICfg roi_cfg;
// input / output
MppBufferGroup buf_grp = NULL;
MppBuffer frm_buf = NULL;
MppBuffer pkt_buf = NULL;
MppBuffer md_info = NULL;
// MppEncRoiCtx roi_ctx;
// resources
size_t header_size;
size_t frame_size;
size_t mdinfo_size;
/* NOTE: packet buffer may overflow */
size_t packet_size;
MppEncoderParams enc_params;
void* userdata = NULL;
};
#endif //__MPP_ENCODER_H__

View File

@@ -0,0 +1,303 @@
/*-------------------------------------------
Includes
-------------------------------------------*/
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <opencv2/opencv.hpp>
#include "im2d.h"
#include "rga.h"
#include "RgaUtils.h"
#include "rkmedia/utils/mpp_decoder.h"
#include "rkmedia/utils/mpp_encoder.h"
#include "mk_mediakit.h"
typedef struct
{
MppDecoder *decoder;
MppEncoder *encoder;
mk_media media;
mk_pusher pusher;
const char *push_url;
mk_player player;
// Yolov8Custom yolo;
int video_type=264;
int push_rtsp_port;
std::string push_path_first;
std::string push_path_second;
} rknn_app_context_t;
void release_media(mk_media *ptr)
{
if (ptr && *ptr)
{
mk_media_release(*ptr);
*ptr = NULL;
}
}
void release_pusher(mk_pusher *ptr)
{
if (ptr && *ptr)
{
mk_pusher_release(*ptr);
*ptr = NULL;
}
}
// 解码后的数据回调函数
void mpp_decoder_frame_callback(void *userdata, int width_stride, int height_stride, int width, int height, int format, int fd, void *data)
{
rknn_app_context_t *ctx = (rknn_app_context_t *)userdata;
int ret = 0;
// 帧画面计数
static int frame_index = 0;
frame_index++;
void *mpp_frame = NULL;
int mpp_frame_fd = 0;
void *mpp_frame_addr = NULL;
int enc_data_size;
// rga原始数据aiqiyi
rga_buffer_t origin;
rga_buffer_t src;
// 编码器准备
if (ctx->encoder == NULL)
{
MppEncoder *mpp_encoder = new MppEncoder();
MppEncoderParams enc_params;
memset(&enc_params, 0, sizeof(MppEncoderParams));
enc_params.width = width;
enc_params.height = height;
enc_params.hor_stride = width_stride;
enc_params.ver_stride = height_stride;
enc_params.fmt = MPP_FMT_YUV420SP;
enc_params.type = MPP_VIDEO_CodingAVC;
mpp_encoder->Init(enc_params, NULL);
ctx->encoder = mpp_encoder;
}
// 编码
int enc_buf_size = ctx->encoder->GetFrameSize();
char *enc_data = (char *)malloc(enc_buf_size);
// 获取解码后的帧
mpp_frame = ctx->encoder->GetInputFrameBuffer();
// 获取解码后的帧fd
mpp_frame_fd = ctx->encoder->GetInputFrameBufferFd(mpp_frame);
// 获取解码后的帧地址
mpp_frame_addr = ctx->encoder->GetInputFrameBufferAddr(mpp_frame);
// 复制到另一个缓冲区避免修改mpp解码器缓冲区
// 使用的是RK RGA的格式转换YUV420SP -> RGB888
origin = wrapbuffer_fd(fd, width, height, RK_FORMAT_YCbCr_420_SP, width_stride, height_stride);
// 这个是写入解码器的对象和颜色转换没有关系
src = wrapbuffer_fd(mpp_frame_fd, width, height, RK_FORMAT_YCbCr_420_SP, width_stride, height_stride);
// 创建一个等宽高的空对象
cv::Mat origin_mat = cv::Mat::zeros(height, width, CV_8UC3);
rga_buffer_t rgb_img = wrapbuffer_virtualaddr((void *)origin_mat.data, width, height, RK_FORMAT_RGB_888);
imcopy(origin, rgb_img);
// 将当前时间点转换为毫秒级别的时间戳
auto millis = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count();
if (ret != 0)
{
printf("inference model fail\n");
goto RET;
}
imcopy(rgb_img, src);
memset(enc_data, 0, enc_buf_size);
enc_data_size = ctx->encoder->Encode(mpp_frame, enc_data, enc_buf_size);
ret = mk_media_input_h264(ctx->media, enc_data, enc_data_size, millis, millis);
if (ret != 1)
{
printf("mk_media_input_frame failed\n");
}
RET: // tag
if (enc_data != nullptr)
{
free(enc_data);
}
}
void API_CALL on_track_frame_out(void *user_data, mk_frame frame)
{
rknn_app_context_t *ctx = (rknn_app_context_t *)user_data;
const char *data = mk_frame_get_data(frame);
// ctx->dts = mk_frame_get_dts(frame);
// ctx->pts = mk_frame_get_pts(frame);
size_t size = mk_frame_get_data_size(frame);
ctx->decoder->Decode((uint8_t *)data, size, 0);
// mk_media_input_frame(ctx->media, frame);
}
void API_CALL on_mk_push_event_func(void *user_data, int err_code, const char *err_msg)
{
rknn_app_context_t *ctx = (rknn_app_context_t *)user_data;
if (err_code == 0)
{
// push success
log_info("push %s success!", ctx->push_url);
printf("push %s success!\n", ctx->push_url);
}
else
{
log_warn("push %s failed:%d %s", ctx->push_url, err_code, err_msg);
printf("push %s failed:%d %s\n", ctx->push_url, err_code, err_msg);
release_pusher(&(ctx->pusher));
}
}
void API_CALL on_mk_media_source_regist_func(void *user_data, mk_media_source sender, int regist)
{
printf("mk_media_source:%x\n", sender);
rknn_app_context_t *ctx = (rknn_app_context_t *)user_data;
const char *schema = mk_media_source_get_schema(sender);
if (strncmp(schema, ctx->push_url, strlen(schema)) == 0)
{
// 判断是否为推流协议相关的流注册或注销事件
printf("schema: %s\n", schema);
release_pusher(&(ctx->pusher));
if (regist)
{
ctx->pusher = mk_pusher_create_src(sender);
mk_pusher_set_on_result(ctx->pusher, on_mk_push_event_func, ctx);
mk_pusher_set_on_shutdown(ctx->pusher, on_mk_push_event_func, ctx);
// mk_pusher_publish(ctx->pusher, ctx->push_url);
log_info("push started!");
printf("push started!\n");
}
else
{
log_info("push stoped!");
printf("push stoped!\n");
}
printf("push_url:%s\n", ctx->push_url);
}
else
{
printf("unknown schema:%s\n", schema);
}
}
void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[],
int track_count)
{
rknn_app_context_t *ctx = (rknn_app_context_t *)user_data;
if (err_code == 0)
{
// success
printf("play success!");
int i;
ctx->push_url = "rtmp://localhost/live/stream";
ctx->media = mk_media_create("__defaultVhost__", ctx->push_path_first.c_str(), ctx->push_path_second.c_str(), 0, 0, 0);
for (i = 0; i < track_count; ++i)
{
if (mk_track_is_video(tracks[i]))
{
log_info("got video track: %s", mk_track_codec_name(tracks[i]));
// 监听track数据回调
mk_media_init_track(ctx->media, tracks[i]);
mk_track_add_delegate(tracks[i], on_track_frame_out, user_data);
}
}
mk_media_init_complete(ctx->media);
mk_media_set_on_regist(ctx->media, on_mk_media_source_regist_func, ctx);
}
else
{
printf("play failed: %d %s", err_code, err_msg);
}
}
void API_CALL on_mk_shutdown_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[], int track_count)
{
printf("play interrupted: %d %s", err_code, err_msg);
}
int process_video_rtsp(rknn_app_context_t *ctx, const char *url)
{
// MPP 解码器
if (ctx->decoder == NULL)
{
MppDecoder *decoder = new MppDecoder(); // 创建解码器
decoder->Init(ctx->video_type, 30, ctx); // 初始化解码器
decoder->SetCallback(mpp_decoder_frame_callback); // 设置回调函数,用来处理解码后的数据
ctx->decoder = decoder; // 将解码器赋值给上下文
}
mk_player player = mk_player_create();
ctx->player = player;
mk_player_set_on_result(player, on_mk_play_event_func, ctx);
mk_player_set_on_shutdown(player, on_mk_shutdown_func, ctx);
mk_player_play(player, url);
printf("enter any key to exit\n");
getchar();
if (player)
{
mk_player_release(player);
}
return 0;
}
int main(int argc, char **argv)
{
int status = 0;
int ret;
// if (argc != 2)
// {
// printf("Usage: %s<video_path>\n", argv[0]);
// return -1;
// }
// char *stream_url = argv[1]; // 视频流地址
char *stream_url = "rtsp://admin:Tangerine!123@192.168.124.29:554/Streaming/Channels/101";
int video_type = 264;
// 初始化流媒体
mk_config config;
memset(&config, 0, sizeof(mk_config));
config.log_mask = LOG_CONSOLE;
config.thread_num = 4;
mk_env_init(&config);
mk_rtsp_server_start(3554, 0);
rknn_app_context_t app_ctx; // 创建上下文
memset(&app_ctx, 0, sizeof(rknn_app_context_t)); // 初始化上下文
app_ctx.video_type = video_type;
app_ctx.push_path_first = "yunyan-live";
app_ctx.push_path_second = "test";
process_video_rtsp(&app_ctx, stream_url);
printf("waiting finish\n");
usleep(3 * 1000 * 1000);
if (app_ctx.encoder != nullptr)
{
delete (app_ctx.encoder);
app_ctx.encoder = nullptr;
}
return 0;
}

View File

@@ -0,0 +1,283 @@
/*-------------------------------------------
Includes
-------------------------------------------*/
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <opencv2/opencv.hpp>
#include "im2d.h"
#include "rga.h"
#include "RgaUtils.h"
#include "rkmedia/utils/mpp_decoder.h"
#include "rkmedia/utils/mpp_encoder.h"
#include "mk_mediakit.h"
typedef struct
{
MppEncoder *encoder;
mk_media media;
mk_pusher pusher;
const char *push_url;
int video_type=264;
int push_rtsp_port;
std::string push_path_first;
std::string push_path_second;
} rknn_app_context_t;
void release_media(mk_media *ptr)
{
if (ptr && *ptr)
{
mk_media_release(*ptr);
*ptr = NULL;
}
}
void release_pusher(mk_pusher *ptr)
{
if (ptr && *ptr)
{
mk_pusher_release(*ptr);
*ptr = NULL;
}
}
void release_track(mk_track *ptr)
{
if (ptr && *ptr)
{
mk_track_unref(*ptr);
*ptr = NULL;
}
}
// 函数定义
int padToMultipleOf16(int number) {
// 如果number已经是16的倍数则直接返回
if (number % 16 == 0) {
return number;
}
// 否则计算需要添加的额外量即16 - (number % 16)
// 这等价于找到比number大的最小16的倍数并减去number
int extra = 16 - (number % 16);
// 返回扩充后的数
return number + extra;
}
void API_CALL on_mk_push_event_func(void *user_data, int err_code, const char *err_msg)
{
rknn_app_context_t *ctx = (rknn_app_context_t *)user_data;
if (err_code == 0)
{
// push success
log_info("push %s success!", ctx->push_url);
printf("push %s success!\n", ctx->push_url);
}
else
{
log_warn("push %s failed:%d %s", ctx->push_url, err_code, err_msg);
printf("push %s failed:%d %s\n", ctx->push_url, err_code, err_msg);
release_pusher(&(ctx->pusher));
}
}
void API_CALL on_mk_media_source_regist_func(void *user_data, mk_media_source sender, int regist)
{
rknn_app_context_t *ctx = (rknn_app_context_t *)user_data;
const char *schema = mk_media_source_get_schema(sender);
if (strncmp(schema, ctx->push_url, strlen(schema)) == 0)
{
release_pusher(&(ctx->pusher));
if (regist)
{
ctx->pusher = mk_pusher_create_src(sender);
mk_pusher_set_on_result(ctx->pusher, on_mk_push_event_func, ctx);
mk_pusher_set_on_shutdown(ctx->pusher, on_mk_push_event_func, ctx);
log_info("push started!");
printf("push started!\n");
}
else
{
log_info("push stoped!");
printf("push stoped!\n");
}
printf("push_url:%s\n", ctx->push_url);
}
else
{
printf("unknown schema:%s\n", schema);
}
}
void API_CALL on_mk_shutdown_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[], int track_count)
{
printf("play interrupted: %d %s", err_code, err_msg);
}
int process_video_file(rknn_app_context_t *ctx, const char *video_path)
{
// 读取视频
cv::VideoCapture cap(video_path);
if (!cap.isOpened())
{
printf("Failed to open video file: %s", video_path);
}
// 使用前需要使用v4l2-ctl --device=/dev/video0 --list-formats-ext检查一下设备支持范围
cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
// set width
cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
// set height
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
// set fps
cap.set(cv::CAP_PROP_FPS,30);
// 获取视频尺寸、帧率
int cap_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int cap_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
int fps = cap.get(cv::CAP_PROP_FPS);
ctx->push_url = "rtsp://localhost/live/stream";
ctx->media = mk_media_create("__defaultVhost__", ctx->push_path_first.c_str(), ctx->push_path_second.c_str(), 0, 0, 0);
codec_args v_args = {0};
mk_track v_track = mk_track_create(MKCodecH264, &v_args);
mk_media_init_track(ctx->media, v_track);
mk_media_init_complete(ctx->media);
mk_media_set_on_regist(ctx->media, on_mk_media_source_regist_func, ctx);
// 初始化编码器
MppEncoder *mpp_encoder = new MppEncoder();
MppEncoderParams enc_params;
memset(&enc_params, 0, sizeof(MppEncoderParams));
enc_params.width = cap_width;
enc_params.height = cap_height;
enc_params.fmt = MPP_FMT_YUV420SP;
enc_params.type = MPP_VIDEO_CodingAVC;
mpp_encoder->Init(enc_params, ctx);
ctx->encoder = mpp_encoder;
// mpp编码配置
void *mpp_frame = NULL;
int mpp_frame_fd = 0;
void *mpp_frame_addr = NULL;
int enc_data_size;
int frame_index = 0;
int ret = 0;
// 画面
cv::Mat img;
while (true)
{
// 读取视频帧
cap >> img;
if (img.empty())
{
printf("Video end.");
break;
}
frame_index++;
// 结束计时
auto end_time = std::chrono::high_resolution_clock::now();
// 将当前时间点转换为毫秒级别的时间戳
auto millis = std::chrono::time_point_cast<std::chrono::milliseconds>(end_time).time_since_epoch().count();
// 获取解码后的帧
mpp_frame = ctx->encoder->GetInputFrameBuffer();
// 获取解码后的帧fd
mpp_frame_fd = ctx->encoder->GetInputFrameBufferFd(mpp_frame);
// 获取解码后的帧地址
mpp_frame_addr = ctx->encoder->GetInputFrameBufferAddr(mpp_frame);
rga_buffer_t src = wrapbuffer_fd(mpp_frame_fd, cap_width, cap_height, RK_FORMAT_YCbCr_420_SP,padToMultipleOf16(cap_width),padToMultipleOf16(cap_height));
int enc_buf_size = ctx->encoder->GetFrameSize();
char *enc_data = (char *)malloc(enc_buf_size);
rga_buffer_t rgb_img = wrapbuffer_virtualaddr((void *)img.data, cap_width, cap_height, RK_FORMAT_BGR_888);
// 将RGB图像复制到src中
imcopy(rgb_img, src);
if (frame_index == 1)
{
enc_data_size = ctx->encoder->GetHeader(enc_data, enc_buf_size);
}
// 内存初始化
memset(enc_data, 0, enc_buf_size);
enc_data_size = ctx->encoder->Encode(mpp_frame, enc_data, enc_buf_size);
ret = mk_media_input_h264(ctx->media, enc_data, enc_data_size, millis, millis);
if (ret != 1)
{
printf("mk_media_input_frame failed\n");
}
if (enc_data != nullptr)
{
free(enc_data);
}
}
// 释放资源
cap.release();
release_track(&v_track);
release_media(&ctx->media);
}
int main(int argc, char **argv)
{
int status = 0;
int ret;
if (argc != 2)
{
printf("Usage: %s<video_path>\n", argv[0]);
return -1;
}
char *stream_url = argv[1]; // 视频流地址
int video_type = 264;
// 初始化流媒体
mk_config config;
memset(&config, 0, sizeof(mk_config));
config.log_mask = LOG_CONSOLE;
config.thread_num = 4;
mk_env_init(&config);
mk_rtsp_server_start(3554, 0);
rknn_app_context_t app_ctx; // 创建上下文
memset(&app_ctx, 0, sizeof(rknn_app_context_t)); // 初始化上下文
app_ctx.video_type = video_type;
app_ctx.push_path_first = "yunyan-live";
app_ctx.push_path_second = "test";
process_video_file(&app_ctx, stream_url);
printf("waiting finish\n");
usleep(3 * 1000 * 1000);
if (app_ctx.encoder != nullptr)
{
delete (app_ctx.encoder);
app_ctx.encoder = nullptr;
}
return 0;
}