forked from MESAI/FFmpegRTSPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFmpegDecoder.cpp
More file actions
148 lines (116 loc) · 3.91 KB
/
FFmpegDecoder.cpp
File metadata and controls
148 lines (116 loc) · 3.91 KB
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
//
// FFmpegDecoder.cpp
// FFmpegRTSPServer
//
// Created by Mina Saad on 9/22/15.
// Copyright (c) 2015 Mina Saad. All rights reserved.
//
#include "FFmpegDecoder.h"
namespace MESAI
{
FFmpegDecoder::FFmpegDecoder(std::string path)
{
this->path = path;
}
void FFmpegDecoder::intialize()
{
// Intialize FFmpeg enviroment
av_register_all();
avdevice_register_all();
avcodec_register_all();
avformat_network_init();
const char *filenameSrc = path.c_str();
pFormatCtx = avformat_alloc_context();
AVCodec * pCodec;
if(avformat_open_input(&pFormatCtx,filenameSrc,NULL,NULL) != 0)
{
//exception
return;
}
if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
//exception
return;
}
av_dump_format(pFormatCtx, 0, filenameSrc, 0);
videoStream = -1;
for(int i=0; i < pFormatCtx->nb_streams; i++)
{
AVStream *st = pFormatCtx->streams[i];
enum AVMediaType type = st->codec->codec_type;
if (videoStream == -1)
if (avformat_match_stream_specifier(pFormatCtx, st, "vst") > 0)
videoStream = i;
}
videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO,videoStream, -1, NULL, 0);
if(videoStream == -1)
{
//exception
return;
}
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec =avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
//exception
return;
}
pCodecCtx->codec_id = pCodec->id;
pCodecCtx->workaround_bugs = 1;
if(avcodec_open2(pCodecCtx,pCodec,NULL) < 0)
{
//exception
return;
}
pFrameRGB = av_frame_alloc();
AVPixelFormat pFormat = AV_PIX_FMT_BGR24;
uint8_t *fbuffer;
int numBytes;
numBytes = avpicture_get_size(pFormat,pCodecCtx->width,pCodecCtx->height) ; //AV_PIX_FMT_RGB24
fbuffer = (uint8_t *) av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameRGB,fbuffer,pFormat,pCodecCtx->width,pCodecCtx->height);
img_convert_ctx = sws_getCachedContext(NULL,pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL,NULL);
height = pCodecCtx->height;
width = pCodecCtx->width;
bitrate =pCodecCtx->bit_rate;
GOP = pCodecCtx->gop_size;
frameRate = (int )pFormatCtx->streams[videoStream]->avg_frame_rate.num/pFormatCtx->streams[videoStream]->avg_frame_rate.den;
}
void FFmpegDecoder::setOnframeCallbackFunction(std::function<void(uint8_t *)> func)
{
onFrame = func;
}
void FFmpegDecoder::playMedia()
{
AVPacket packet;
AVFrame * pFrame;
while((av_read_frame(pFormatCtx,&packet)>=0))
{
if(packet.buf != NULL & packet.stream_index == videoStream)
{
pFrame = av_frame_alloc();
int frameFinished;
int decode_ret = avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);
av_free_packet(&packet);
if(frameFinished)
{
sws_scale(img_convert_ctx, ((AVPicture*)pFrame)->data, ((AVPicture*)pFrame)->linesize, 0, pCodecCtx->height, ((AVPicture *)pFrameRGB)->data, ((AVPicture *)pFrameRGB)->linesize);
onFrame(((AVPicture *)pFrameRGB)->data[0]);
}
av_frame_unref(pFrame);
av_free(pFrame);
}
usleep(((double)(1.0/frameRate))*1000000);
}
av_free_packet(&packet);
}
void FFmpegDecoder::finalize()
{
sws_freeContext(img_convert_ctx);
av_freep(&(pFrameRGB->data[0]));
av_frame_unref(pFrameRGB);
av_free(pFrameRGB);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}
}