-
Notifications
You must be signed in to change notification settings - Fork 16
/
ffmpegdecoder.cpp
162 lines (128 loc) · 3.99 KB
/
ffmpegdecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// ffmpegdecoder.h
// ffmpeg RTSP Client
//
// Created by Yonghye Kwon on 9/14/18.
// Copyright (c) Freeeeeeeeeeeeeee
//
#include "ffmpegdecoder.h"
using namespace std;
FFmpegDecoder::FFmpegDecoder(std::string path)
:bConnected(false)
{
this->path = path;
}
FFmpegDecoder::~FFmpegDecoder()
{
destroy();
}
void FFmpegDecoder::connect()
{
avformat_network_init();
av_register_all();
pFormatCtx = avformat_alloc_context();
AVDictionary *avdic=NULL;
char option_key[]="rtsp_transport";
char option_value[]="tcp";
av_dict_set(&avdic,option_key,option_value,0);
char option_key2[]="max_delay";
char option_value2[]="100";
av_dict_set(&avdic,option_key2,option_value2,0);
if (avformat_open_input(&pFormatCtx, path.c_str(), NULL, &avdic) != 0)
{
std::cout << "can't open the file." << std::endl;
bConnected = false;
return ;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
std::cout << "can't find stream infomation" << std::endl;
bConnected = false;
return ;
}
videoStream = -1;
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
}
}
if (videoStream == -1)
{
std::cout << "can't find a video stream" << std::endl;
bConnected = false;
return ;
}
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
pCodecCtx->bit_rate = 0;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 10;
pCodecCtx->frame_number = 1;
if (pCodec == NULL)
{
std::cout << "can't find a codec" << std::endl;
bConnected = false;
return ;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
std::cout << "can't open a codec" << std::endl;
bConnected = false;
return ;
}
pFrame = av_frame_alloc();
pFrameBGR = av_frame_alloc();
imgConvertCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
int numBytes = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);
outBuffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameBGR, outBuffer, AV_PIX_FMT_BGR24,
pCodecCtx->width, pCodecCtx->height);
packet = (AVPacket *) malloc(sizeof(AVPacket));
av_new_packet(packet, pCodecCtx->width * pCodecCtx->height);
bConnected = true;
}
void FFmpegDecoder::decode()
{
std::chrono::milliseconds duration(5);
while (av_read_frame(pFormatCtx, packet) >= 0)
{
if (packet->stream_index == videoStream)
{
int got_picture = 0;
int ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,packet);
if (ret < 0)
{
std::cout << "decode error.\n" << std::endl;
break ;
}
if (got_picture)
{
sws_scale(imgConvertCtx,
(uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameBGR->data,
pFrameBGR->linesize);
cv::Mat img(pFrame->height,
pFrame->width,
CV_8UC3,
pFrameBGR->data[0]);
mtx.lock();
decodedImgBuf.push_back(img);
mtx.unlock();
}
}
av_free_packet(packet);
std::this_thread::sleep_for(duration);
}
bConnected = false;
}
void FFmpegDecoder::destroy()
{
av_free(outBuffer);
av_free(pFrameBGR);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}