Files
Payload-SDK/samples/sample_c/module_sample/camera_emu/ffmpeg_tc.c

177 lines
5.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by tangchao on 2021/11/16.
//
#include "ffmpeg_tc.h"
char *getsystemtime()
{
//获取系统时间
time_t timer;//time_t就是long int 类型
struct tm *tblock;
timer = time(NULL);//返回秒数(精度为秒)从1970-1-1,00:00:00 可以当成整型输出或用于其它函数
tblock = localtime(&timer);
//printf("Local time is: %s\n", asctime(tblock));
//格式化时间为需要的格式
char fileNameTmp[256] = { 0 };
char dirNameTmp[256] = { 0 };
strftime(fileNameTmp, sizeof(fileNameTmp), "%Y%m%d_%H%M%S", tblock);
return fileNameTmp;
}
AVFrame *get_video_frame(IntputDev* input)//tc改动
{
clock_t start,finish;
start = clock(); // 设置开始clock
int ret;
AVFrame * ret_frame=NULL;
if(av_read_frame(input->v_ifmtCtx, input->in_packet)>=0)
{
if(input->in_packet->stream_index==input->videoindex)
{
ret = avcodec_send_packet(input->pCodecCtx, input->in_packet);
if (ret < 0) {
fprintf(stderr, "Error sending a packet for decoding\n");
exit(1);
}
ret = avcodec_receive_frame(input->pCodecCtx, input->pFrame);
if(ret<0)
{
printf("Decode Error.\n");
av_packet_unref(input->in_packet);
return NULL;
}
sws_scale(input->img_convert_ctx, (const unsigned char* const*)input->pFrame->data, input->pFrame->linesize, 0, input->pCodecCtx->height, input->pFrameYUV->data, input->pFrameYUV->linesize);
input->pFrameYUV->pts=input->next_pts++;
ret_frame= input->pFrameYUV;
}
av_packet_unref(input->in_packet);
}
finish = clock();// 设置结束clock
double duration = (double)(finish - start) / CLOCKS_PER_SEC;//转换浮点型
// printf( "采集视频帧时间:%lf seconds\n", duration );
return ret_frame;
}
//
//static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt)
//{
// int ret;
//
// /* send the frame to the encoder */
// if (frame)
// printf("Send frame %3"PRId64"\n", frame->pts);
//
// ret = avcodec_send_frame(enc_ctx, frame);//返回-21?????????????????????????????????????????????
// if (ret < 0) {
// fprintf(stderr, "Error sending a frame for encoding\n");
// exit(1);
// }
//
// while (ret >= 0) {
// ret = avcodec_receive_packet(enc_ctx, pkt);
// if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
// return;
// else if (ret < 0) {
// fprintf(stderr, "Error during encoding\n");
// exit(1);
// }
//
//// printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
//// fwrite(pkt->data, 1, pkt->size, outfile);
// av_packet_unref(pkt);
// }
//}
void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, char **data, int *datasize)
{
int ret;
/* send the frame to the encoder */
// if (frame)
// printf("Send frame %3"PRId64"\n", frame->pts);
ret = avcodec_send_frame(enc_ctx, frame);//返回-21?????????????????????????????????????????????
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
printf("11111111111111111111\n");
return;
}
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
// printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
// fwrite(pkt->data, 1, pkt->size, outfile);
//tc
char *dataBuffer = calloc(pkt->size, 1);
memcpy(dataBuffer,pkt->data,pkt->size);
*data = dataBuffer;
*datasize = pkt->size;
av_packet_unref(pkt);
}
printf("22222222222222222\n");
}
char * encode2(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, int *datasize)
{
clock_t start,finish;
start = clock(); // 设置开始clock
int ret;
char *dataBuffer;
ret = avcodec_send_frame(enc_ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
printf("11111111111111111111\n");
return NULL;
}
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
// printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
// fwrite(pkt->data, 1, pkt->size, outfile);
//tc
dataBuffer = calloc(pkt->size, 1);
memcpy(dataBuffer,pkt->data,pkt->size);
*datasize = pkt->size;
av_packet_unref(pkt);
finish = clock();// 设置结束clock
double duration = (double)(finish - start) / CLOCKS_PER_SEC;//转换浮点型
// printf( "编码视频帧时间:%lf seconds\n", duration );
return dataBuffer;
}
}