📄 bf_sdk.cpp
字号:
#include "EJ_BBS.h"
#include "utility.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define DO_CYCLE_COUNTS
#include <cycle_count.h>
#include "install_codec_heap.h"
#include "jpeg.h"
cycle_t g_s[15];
cycle_t g_x[15];
#define DMA_CHANNEL_NUM (5) //one for ppi, 4 for mem dma
#define SEC_HANDLER_NUM (0)
#define DCB_CALLBACK_NUM (0)
#define DEV_NUM (1)
//#define ENTIRE_FIELD_MODE
#define ACTIVE_FIELD_MODE // uncomment for Active Field
#if defined ACTIVE_FIELD_MODE
#define FRAME_SIZE (2 * ADI_ITU656_PAL_WIDTH * ADI_ITU656_PAL_ACTIVE_FLINES) // only get one field , 2 bytes per pixel for uyvy
#else
#define FRAME_SIZE (ADI_ITU656_PAL_LINE_WIDTH * ADI_ITU656_PAL_HEIGHT)
#endif
//extern "asm" int ldf_slow_heap_space;
extern "asm" int ldf_slow_heap_space;
extern "asm" int ldf_L1_heap_space, ldf_L1_heap_length;
extern "asm" int ldf_L2_heap_space, ldf_L2_heap_length;
u8 *pField = (u8*)NULL; // in ppi, this var set to ppi dst mem pool
u8 *pFrame_U = (u8*)NULL;
u8 *pFrame_Y = (u8*)NULL;
u8 *pFrame_V = (u8*)NULL;
ADI_DMA_2D_TRANSFER src_info_Y_1 = {0}, dst_info_Y_1 = {0};
ADI_DMA_2D_TRANSFER src_info_Y_2 = {0}, dst_info_Y_2 = {0};
ADI_DMA_2D_TRANSFER src_info_U = {0}, dst_info_U = {0};
ADI_DMA_2D_TRANSFER src_info_V = {0}, dst_info_V = {0};
bool g_field_in = false;
bool g_Y = false;
bool g_UV = false;
// This is the callback function for the DMA.
static void Memory_Callback_Y(
void *AppHandle,
u32 Event,
void *pArg)
{
// watch for errors
if (Event == ADI_DMA_EVENT_ERROR_INTERRUPT) {
Event = Event;
}
g_Y = true;
}
static void Memory_Callback_V(
void *AppHandle,
u32 Event,
void *pArg)
{
// watch for errors
if (Event == ADI_DMA_EVENT_ERROR_INTERRUPT) {
Event = Event;
}
g_UV = true;
}
static void Memory_Callback_U(
void *AppHandle,
u32 Event,
void *pArg)
{
// watch for errors
if (Event == ADI_DMA_EVENT_ERROR_INTERRUPT) {
Event = Event;
}
#if defined(__ADSP_TETON__)
((EJ_BBS *)AppHandle)->start_memdma(ADI_DMA_MDMA1_1, &dst_info_V, &src_info_V, 1, Memory_Callback_V);
#else
#endif
}
static void ppiCallback(
void *AppHandle,
u32 Event,
void *pArg)
{
static unsigned int Counter = 0; // count the number of input buffers processed
// CASEOF (event type)
switch (Event) {
// CASE (buffer processed)
case ADI_DEV_EVENT_BUFFER_PROCESSED:
//((EJ_BBS *)AppHandle)->set_dev_stop(DEV_PPI1);
// point to the buffer
if((u32)pArg == 0x1)
{
g_field_in = true;
((EJ_BBS *)AppHandle)->start_memdma(ADI_DMA_MDMA1_0, &dst_info_Y_1, &src_info_Y_1, 1, (ADI_DCB_CALLBACK_FN)Memory_Callback_Y);
((EJ_BBS *)AppHandle)->start_memdma(ADI_DMA_MDMA1_1, &dst_info_U, &src_info_U, 1, Memory_Callback_U);
}
if((u32)pArg == 0x2)
{
g_field_in = true;
}
// increment our counter
//Counter++;
break;
// CASE (an error)
case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:
case ADI_PPI_EVENT_ERROR_INTERRUPT:
// turn on all LEDs and wait for help
// ezTurnOnAllLEDs();
while (1) ;
// ENDCASE
}
// return
}
/*
unsigned long file_size(FILE *fp)
{
unsigned long pos_start, pos_end, pos_cur;
pos_cur = ftell(fp);
fseek(fp,0,SEEK_SET);
pos_start = ftell(fp);
fseek(fp,0,SEEK_END);
pos_end = ftell(fp);
fseek(fp, pos_cur, SEEK_SET);
return (pos_end - pos_start);
}
*/
void main(void)
{
u8 *mem_sys = (u8*)NULL;
u32 sysMemUsed = 0,
ddrMemUsed = 0;
RESULT rst = EJ_ERR_NONE;
FILE *fp ;
int heap_space ;
int heap_L3, heap_L2, heap_L1;
// table of configuration values for the PPI on input
ADI_DEV_CMD_VALUE_PAIR InboundConfigurationTable [] = {
{ ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void *)ADI_DEV_MODE_CHAINED },
{ ADI_PPI_CMD_SET_CONTROL_REG, (void *)0x00c0 },
{ ADI_PPI_CMD_SET_LINES_PER_FRAME_REG, (void *)288 },
{ ADI_DEV_CMD_END, NULL },
};
ADI_DEV_2D_BUFFER buffer2D;
EJ_BBS bbs ;
HANDLE session = NULL;
bbs.init_core();
mem_sys = (u8 *)malloc(1024);
heap_space = heap_space_unused(0);
rst = bbs.init_system_service(mem_sys, &sysMemUsed, DMA_CHANNEL_NUM, SEC_HANDLER_NUM, DCB_CALLBACK_NUM);
if(EJ_ERR_NONE != rst)
return;
rst = bbs.init_device_driver(mem_sys + sysMemUsed, &ddrMemUsed, DEV_NUM);
if(EJ_ERR_NONE != rst)
return;
// creat my own heap
heap_L3 = bbs.creat_alternate_heap((void*)&ldf_slow_heap_space, 0x00400000); // 4M bytes
heap_L2 = bbs.creat_alternate_heap((void*)&ldf_L2_heap_space, 0x1a000); // 104K
heap_L1 = bbs.creat_alternate_heap((void*)&ldf_L1_heap_space, 0x4000); // 16K
//
// allocate video_in frame memory from my own heap
//pField = (u8*)heap_malloc(heap_L3, 4*720*288);
pField = (u8*)heap_malloc(heap_L3, 2*720*288);
// pFiled2 = (u8*)heap_malloc(heap_index1, 2*720*288);
pFrame_U = (u8*)heap_malloc(heap_L3, 3*360*288);
pFrame_Y = pFrame_U + 360 * 288;
pFrame_V = pFrame_Y + 360 * 288;
heap_space = heap_space_unused(heap_L3);
//prepqre buffer for the ppi
// field buffer 1
buffer2D.Data = (void*)pField;
buffer2D.ElementWidth = 2;
#ifdef ENTIRE_FIELD_MODE
buffer2D.XCount = ADI_ITU656_PAL_LINE_WIDTH / buffer2D.ElementWidth;
buffer2D.YCount = ADI_ITU656_PAL_HEIGHT;
#endif
#ifdef ACTIVE_FIELD_MODE //one field
buffer2D.XCount = ADI_ITU656_PAL_WIDTH*2 / buffer2D.ElementWidth;
buffer2D.YCount = 576; //ADI_ITU656_PAL_ACTIVE_FLINES;
#endif
buffer2D.XModify = 2;
buffer2D.YModify = 2;
buffer2D.CallbackParameter = (void *)0x1; // never set CallbackParameter to 0, sys will take for granted that no need to make callback
buffer2D.ProcessedFlag = FALSE;
buffer2D.ProcessedElementCount = 0;
buffer2D.pNext = (adi_dev_2d_buffer *)NULL;//&buffer2D[1];
buffer2D.pAdditionalInfo = NULL;
// configuration the ppi device
#if defined(__ADSP_TETON__)
rst = bbs.config_device(DEV_PPI1, InboundConfigurationTable, ppiCallback);
#else
rst = bbs.config_device(DEV_PPI, InboundConfigurationTable, ppiCallback);
#endif
if(EJ_ERR_NONE != rst)
return;
// set the buffer to ppi
#if defined(__ADSP_TETON__)
rst = bbs.set_dev_buffer(DEV_PPI1, (ADI_DEV_BUFFER *)(&buffer2D), ADI_DEV_2D);
#else
rst = bbs.set_dev_buffer(DEV_PPI, (ADI_DEV_BUFFER *)(&buffer2D), ADI_DEV_2D);
#endif
if(EJ_ERR_NONE != rst)
return;
// config and open memory dma stream
#if defined(__ADSP_TETON__)
rst = bbs.config_memdma(ADI_DMA_MDMA1_0);
rst = bbs.config_memdma(ADI_DMA_MDMA1_1);
#else
rst = bbs.config_memdma(ADI_DMA_MDMA_0);
#endif
if(EJ_ERR_NONE != rst)
return;
// set the memory dma buffer
// IYU2_Y
src_info_Y_1.StartAddress = (void *)((u8* )pField + 1);
src_info_Y_1.XCount = 360 ;
src_info_Y_1.XModify = 4;
src_info_Y_1.YCount = 288;
src_info_Y_1.YModify = 4;
dst_info_Y_1.StartAddress = (void *)pFrame_Y;
dst_info_Y_1.XCount = 360;
dst_info_Y_1.XModify = 1;
dst_info_Y_1.YCount = 288;
dst_info_Y_1.YModify = 1;
// IYU2_U
src_info_U.StartAddress = (void *)((u8* )pField);
src_info_U.XCount = 360 ;
src_info_U.XModify = 4;
src_info_U.YCount = 288;
src_info_U.YModify = 4;
dst_info_U.StartAddress = (void*)pFrame_U;
dst_info_U.XCount = 360;
dst_info_U.XModify = 1;
dst_info_U.YCount = 288;
dst_info_U.YModify = 1;
// IYU2_U
src_info_V.StartAddress = (void *)((u8* )pField + 2);
src_info_V.XCount = 360 ;
src_info_V.XModify = 4;
src_info_V.YCount = 288;
src_info_V.YModify = 4;
dst_info_V.StartAddress = (void*)pFrame_V;
dst_info_V.XCount = 360;
dst_info_V.XModify = 1;
dst_info_V.YCount = 288;
dst_info_V.YModify = 1;
fp = fopen( "encode_jpeg.jpg", "wb+" );
// start ppi data transfer
#if defined(__ADSP_TETON__)
bbs.set_dev_start(DEV_PPI1);
#else
bbs.set_dev_start(DEV_PPI);
#endif
// g_field_in = true;
while(1)
{
g_field_in = g_field_in;
if(g_UV && g_Y)
{
g_field_in = false;
g_UV = false;
g_Y = false;
session = init_jpeg_enc(fp, pFrame_U, IYU2, 360, 288, heap_L1,heap_L2,heap_L3);
if(session == NULL)
return;
START_CYCLE_COUNT(g_s[0]);
jpg_encode();//test
STOP_CYCLE_COUNT(g_x[0],g_s[0]);
fclose(fp);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -