#include
#include
using namespace cv;
using namespace std;
extern "C"
{
#include
#include
#include
}
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")
int main()
{
VideoCapture capture(0);
const char *outUrl;
outUrl = "rtsp://127.0.0.1/isport";
avcodec_register_all();
av_register_all();
avformat_network_init();
SwsContext *vsc = NULL;
AVFrame *yuv = NULL;
AVCodecContext *vc = NULL;
AVFormatContext *ic = NULL;
AVOutputFormat *ofmt = NULL;
int inWidth = 960;
int inHeight = 540;
int fps = 60;
vsc = sws_getCachedContext(vsc,
inWidth, inHeight, AV_PIX_FMT_BGR24,
inWidth, inHeight, AV_PIX_FMT_YUV420P,
SWS_BICUBIC,
0, 0, 0 );
if (!vsc)
{
throw exception("sws_getCachedContext failed!");
}
yuv = av_frame_alloc();
yuv->format = AV_PIX_FMT_YUV420P;
yuv->width = inWidth;
yuv->height = inHeight;
yuv->pts = 0;
int ret = av_frame_get_buffer(yuv, 32);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
throw exception(buf);
}
AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec)
{
throw exception("Can`t find h264 encoder!");
}
vc = avcodec_alloc_context3(codec);
if (!vc)
{
throw exception("avcodec_alloc_context3 failed!");
}
vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
vc->codec_id = codec->id;
vc->thread_count = 8;
vc->bit_rate = 50 * 1024 * 8;
vc->width = inWidth;
vc->height = inHeight;
vc->time_base = { 1,fps };
vc->framerate = { fps,1 };
vc->gop_size = 50;
vc->max_b_frames = 0;
vc->pix_fmt = AV_PIX_FMT_YUV420P;
ret = avcodec_open2(vc, codec, 0);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
throw exception(buf);
}
cout << "avcodec_open2 success!" << endl;
ret = avformat_alloc_output_context2(&ic, NULL, "rtsp", outUrl);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
throw exception(buf);
}
ofmt = ic->oformat;
AVStream *vs = avformat_new_stream(ic, codec);
if (!vs)
{
throw exception("avformat_new_stream failed");
}
ret = avcodec_parameters_from_context(vs->codecpar, vc);
av_dump_format(ic, 0, outUrl, 1);
if (!(ofmt->flags & AVFMT_NOFILE))
{
ret = avio_open(&ic->pb, outUrl, AVIO_FLAG_WRITE);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
throw exception(buf);
}
}
ret = avformat_write_header(ic, NULL);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
throw exception(buf);
}
AVPacket pack;
memset(&pack, 0, sizeof(pack));
int vpts = 0;
while (cvGetWindowHandle(plot))
{
Mat color_image;
capture >> color_image;
if (color_image.empty())
{
break;
}
uint8_t *indata[AV_NUM_DATA_POINTERS] = { 0 };
indata[0] = color_image.data;
int insize[AV_NUM_DATA_POINTERS] = { 0 };
insize[0] = color_image.cols * color_image.elemSize();
int h = sws_scale(vsc, indata, insize, 0, color_image.rows,
yuv->data, yuv->linesize);
if (h <= 0)
{
continue;
}
yuv->pts = vpts;
vpts++;
ret = avcodec_send_frame(vc, yuv);
if (ret != 0)
continue;
ret = avcodec_receive_packet(vc, &pack);
if (ret != 0 || pack.size > 0)
{
}
else
{
continue;
}
pack.pts = av_rescale_q(pack.pts, vc->time_base, vs->time_base);
pack.dts = av_rescale_q(pack.dts, vc->time_base, vs->time_base);
pack.duration = av_rescale_q(pack.duration, vc->time_base, vs->time_base);
ret = av_interleaved_write_frame(ic, &pack);
if (ret == 0)
{
}
}