CedarX_InAndOut.c 6.0 KB
/*

    编码测试程序

*/

#include <stdio.h>
#include "JZsdkLib.h"
#include "./cedarX_venc/cedarX_venc.h"
#include "./cedarX_vdec/cedarX_vdec.h"
#include "version_choose.h"
#include <pthread.h>

#if ALLWINNER_CEDAR == VERSION_SWITCH_ON
#define VIDEO_FRAME_MAX_COUNT 18000

int h264_to_nv12(int width, int height)
{
    char *FrameInfoString = NULL; //帧信息
    int ret;

    FrameInfoString = (char *)malloc(VIDEO_FRAME_MAX_COUNT * 1024);
    if (FrameInfoString == NULL) {
        printf("帧信息字符串内存注册失败\n");
        return -1;
    }
    memset(FrameInfoString, 0, VIDEO_FRAME_MAX_COUNT * 1024);

    char ffmpegCmdStr[256];
    //snprintf(ffmpegCmdStr, 256, "ffprobe -show_packets /root/sdcard/test.h264 2>/dev/null");
    snprintf(ffmpegCmdStr, 256, "ffprobe -show_packets /root/sdcard/centerTest.h264 2>/dev/null");

    FILE *fp;

    fp = popen(ffmpegCmdStr, "r");
    if (fp == NULL) {
            printf("空\n");
    }

    ret = (long) fread(FrameInfoString, 1, VIDEO_FRAME_MAX_COUNT * 1024, fp);

    FrameInfoString[ret] = '\0';
    printf("ret:%d\n",ret);

    //show_packets读取的信息
    // [PACKET]
    // codec_type=video
    // stream_index=0
    // pts=N/A
    // pts_time=N/A
    // dts=N/A
    // dts_time=N/A
    // duration=48000
    // duration_time=0.040000
    // size=52632
    // pos=7902242
    // flags=K_
    // [/PACKET]
    const char *FrameKeyHead =    "[PACKET]";         //帧头  
    char *FrameKeyDurationTime =   "duration_time";                //当前帧的时间长度
    char *FrameKeyPos          =    "pos"     ;                        //当前帧的开头的视频长度 从0开始
    char *FrameKeySize            =    "size"  ;                        //当前帧的长度

    char *FrameLocation = NULL;
    char *FrameLocalDurationTime    = NULL; 
    char *FrameLocalPos =   NULL;
    char *FrameLocalSize =  NULL;
    char frameParameterFormat[50] = {0};
    int frameDurationTimeS = 0;
    //解析FrameInfoString
    FrameLocation = FrameInfoString;


    //打开h264文件
    FILE *h264_Input =  fopen("/root/sdcard/centerTest.h264", "rb");
    //FILE *h264_Input =  fopen("/root/sdcard/PSDK_0005.h264", "rb");
    FILE *yuv_Output =  fopen("/root/sdcard/centerTest.yuv", "wb");

    int FrameLength;
    
    unsigned char *videodata = NULL;
    int videodata_lenth = 0;
    videodata_lenth = 1440 * 1088 * 1.5;
    //videodata = (unsigned char *)malloc(videodata_lenth);

    unsigned char *outputVideoData = (unsigned char *)malloc(1440*1088*3/2);
    if (outputVideoData == NULL)
    {
        printf("输出数组申请内存失败\n");
        return -1;
    }
    
    int OutputdataLen;

    while (1)
    {
        //1、寻找帧头
        FrameLocation = strstr(FrameLocation, FrameKeyHead);    //返回第一次出现FrameKeyChar的地址
        if (FrameLocation == NULL) {
            printf("已到达视频尾\n");
            break;
        }

        //2、寻找此帧的一帧时间长度 的地址
        FrameLocalDurationTime = strstr(FrameLocation, FrameKeyDurationTime);    //返回第一次出现FrameKeyDurationTime
        if (FrameLocalDurationTime == NULL) {
            printf("无法获取当前帧的时间长度\n");
        }
        //3、寻找当前帧的位置 的地址
        FrameLocalPos = strstr(FrameLocation, FrameKeyPos);    //返回第一次出现FrameKeyPos的地址
        if (FrameLocalPos == NULL) {
            printf("无法获取当前帧的位置\n");
        }
    
        //4、寻找当前帧的长度   的地址
        FrameLocalSize = strstr(FrameLocation, FrameKeySize);    //返回第一次出现FrameKeySize的地址
        if (FrameLocalPos == NULL) {
            printf("无法获取当前帧的长度\n");
        }

        //解析参数
        //帧时间长度
        //snprintf(frameParameterFormat, sizeof(frameParameterFormat), "%s=%%f", FrameKeyDurationTime);
        //sscanf(FrameLocalDurationTime, frameParameterFormat, &VideoFrameInfo[FrameNumber].DurationTime);
        //printf("帧时间长度:%f\n",VideoFrameInfo[FrameNumber].DurationTime);
        //当前帧的字节位
        //snprintf(frameParameterFormat, sizeof(frameParameterFormat), "%s=%%d", FrameKeyPos);
        //sscanf(FrameLocalPos, frameParameterFormat, &VideoFrameInfo[FrameNumber].pos);
        //printf("帧字节位:%d\n",VideoFrameInfo[FrameNumber].pos);
        //当前帧长度
        snprintf(frameParameterFormat, sizeof(frameParameterFormat), "%s=%%d", FrameKeySize);
        sscanf(FrameLocalSize, frameParameterFormat, &FrameLength);
        //printf("帧长度:%d\n",FrameLength);

        FrameLocation += strlen(FrameKeyHead);

        videodata = (unsigned char *)malloc(FrameLength);
        memset(videodata, 0, FrameLength);

        //memset(videodata, 0, videodata_lenth);
        fread(videodata, 1, FrameLength, h264_Input);
        //printf("解码\n");
        //CedarX_H264_TO_NV21_DecodeOneFrame(videodata, FrameLength,outputVideoData, &OutputdataLen);
        fwrite(outputVideoData, 1, OutputdataLen, yuv_Output);
        //printf("释放\n");
        free(videodata);
    }
    
    free(FrameInfoString);
    fclose(h264_Input);
    fclose(yuv_Output);
}


void* threadFunction(void* arg) {
    //已知M3 输入格式为
    //1440 X 1080的h264 色彩格式yuv420    
    //1、初始解码器
    CedarX_vdec_init(1440,1080,1,30);

    //2、输入视频进解码器
    h264_to_nv12(1440,1080);    
    printf("解码完成\n");
}

int CedarX_Test()
{
    // pthread_t thread;
    // int result = pthread_create(&thread, NULL, threadFunction, NULL);
    // if (result != 0) {
    //     perror("线程创建失败");
    //     return 1;
    // }

    CedarX_vdec_init(1920,1080,2,30);  //得到480*272
}

/**
 * 
 *  cedar解码
 * 
 * ****/
int CedarX_DecodeOneFrame(unsigned char *InputBuffer, int InputBufferLenth)
{
    CedarX_H264_TO_NV21_DecodeOneFrame(InputBuffer, InputBufferLenth);
}

#endif