#include "stdafx.h"
#include "CounterDemo.h"
#include "CounterDemoDlg.h"
#include "videotransform.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#ifdef __cplusplus
}
#endif
bool bStop;
int ISDK_DEV_VideoTransform(void *inParam, void *outParam, int inType, int outType)
{
if (NULL == inParam || NULL == outParam)
{
return SDK_ERROR_PARAM;
}
if (VIDEO_TRANSFORM_SRC_RTSP == inType && strnicmp((const char *)inParam, "rtsp", strlen("rtsp")))
{
return SDK_ERROR_PARAM;
}
AVFormatContext *i_fmt_ctx;
AVStream *i_video_stream;
AVFormatContext *o_fmt_ctx;
AVStream *o_video_stream;
//avcodec_register_all();
av_register_all();
avformat_network_init();
/* should set to NULL so that avformat_open_input() allocate a new one */
i_fmt_ctx = NULL;
if (avformat_open_input(&i_fmt_ctx, (const char *)inParam, NULL, NULL)!=0)
{
fprintf(stderr, "could not open input file\n");
return SDK_ERROR_PARAM;
}
if (avformat_find_stream_info(i_fmt_ctx, NULL)<0)
{
fprintf(stderr, "could not find stream info\n");
return SDK_ERROR_PARAM;
}
//av_dump_format(i_fmt_ctx, 0, argv[1], 0);
/* find first video stream */
for (unsigned i=0; i<i_fmt_ctx->nb_streams; i++)
{
if (i_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
i_video_stream = i_fmt_ctx->streams[i];
break;
}
}
if (i_video_stream == NULL)
{
fprintf(stderr, "didn't find any video stream\n");
return -1;
}
avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, (const char *)outParam);
/*
* since all input files are supposed to be identical (framerate, dimension, color format, ...)
* we can safely set output codec values from first input file
*/
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
if (NULL == o_video_stream)
{
return SDK_ERROR_PARAM;
}
else
{
avcodec_copy_context(o_video_stream->codec, i_video_stream->codec);
o_video_stream->codec->extradata = i_video_stream->codec->extradata;
o_video_stream->codec->extradata_size = i_video_stream->codec->extradata_size;
}
avio_open(&o_fmt_ctx->pb, (const char *)outParam, AVIO_FLAG_WRITE);
avformat_write_header(o_fmt_ctx, NULL);
int last_pts = 0;
int last_dts = 0;
int64_t pts, dts;
int num = 0, have_update = 0;
while (bStop)
{
if ((num++) < 25 && 1 != have_update)
{
have_update = 1;
::PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_UPDATERECORDSTATUS, 0, num);
}
else if(num > 25 && num < 50 && 2 != have_update)
{
have_update = 2;
::PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_UPDATERECORDSTATUS, 0, num);
}
else if(num > 50 && num <= 75 && 3 != have_update)
{
have_update = 3;
::PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_UPDATERECORDSTATUS, 0, num);
}
if (75 == num)
num = 0;
AVStream *in_stream, *out_stream;
AVPacket pkt;
//获取一个AVPacket(Get an AVPacket)
if (av_read_frame(i_fmt_ctx, &pkt) < 0)
break;
in_stream = i_fmt_ctx->streams[pkt.stream_index];
out_stream = o_fmt_ctx->streams[pkt.stream_index];
/* copy packet */
//转换PTS/DTS(Convert PTS/DTS)
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
//写入(Write)
if (av_interleaved_write_frame(o_fmt_ctx, &pkt) < 0) {
printf( "Error muxing packet\n");
break;
}
//printf("Write %8d frames to output file\n",frame_index);
av_free_packet(&pkt);
}
avformat_close_input(&i_fmt_ctx);
av_write_trailer(o_fmt_ctx);
avcodec_close(o_fmt_ctx->streams[0]->codec);
/* close output */
if (o_fmt_ctx && !(o_fmt_ctx->oformat->flags & AVFMT_NOFILE))
avio_close(o_fmt_ctx->pb);
av_freep(&o_fmt_ctx->streams[0]->codec);
av_freep(&o_fmt_ctx->streams[0]);
//avio_close(o_fmt_ctx->pb);
av_free(o_fmt_ctx);
::PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_UPDATERECORDSTATUS, 0, 30008);
return 1;
}
- 1
- 2
前往页