-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFFmpegFormatCtx.cpp
More file actions
101 lines (82 loc) · 2.23 KB
/
Copy pathFFmpegFormatCtx.cpp
File metadata and controls
101 lines (82 loc) · 2.23 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
#include "FFmpegFormatCtx.hpp"
FFmpegFormatCtx::FFmpegFormatCtx()
{
_format_ctx = avformat_alloc_context();
}
bool FFmpegFormatCtx::Open(std::string url)
{
int ret = avformat_open_input(&_format_ctx, url.c_str(), NULL, NULL);
if (ret != 0)
{
char buff[512];
_format_ctx = nullptr;
_format_ctx = _format_ctx = avformat_alloc_context();
av_make_error_string(buff, 512, ret);
return false;
}
return true;
}
bool FFmpegFormatCtx::Open(FFmpegIOCtx::Ptr avio, int buff_size)
{
_buff_size = buff_size;
_avio = std::move(avio);
_avio_weak = _avio;
_avio->Init(_buff_size, _avio_weak);
_format_ctx->pb = _avio->get();
int ret = avformat_open_input(&_format_ctx, NULL, NULL, NULL);
if (ret != 0)
{
char buff[512];
_format_ctx = nullptr;
_format_ctx = _format_ctx = avformat_alloc_context();
av_make_error_string(buff, 512, ret);
return false;
}
return true;
}
std::vector<unsigned int> FFmpegFormatCtx::FindVideoStreamIDX()
{
std::vector<unsigned int> result;
for (unsigned int i = 0; i < _format_ctx->nb_streams; i++)
{
if (_format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
if (_format_ctx->streams[i])
if (_format_ctx->streams[i]->codecpar)
result.push_back(i);
}
}
return result;
}
AVCodecID FFmpegFormatCtx::GetCodecID(unsigned int stream_idx)
{
if (stream_idx < _format_ctx->nb_streams)
{
if (_format_ctx->streams[stream_idx])
if (_format_ctx->streams[stream_idx]->codecpar)
return _format_ctx->streams[stream_idx]->codecpar->codec_id;
}
return AV_CODEC_ID_NONE;
}
void FFmpegFormatCtx::TurnOnRealTimeMode()
{
av_read_play(_format_ctx);
}
void FFmpegFormatCtx::TurnOffRealTimeMode()
{
av_read_pause(_format_ctx);
}
int FFmpegFormatCtx::ReadPacket(FFmpegPacket::Ptr &packet)
{
packet = std::make_shared<FFmpegPacket>();
return av_read_frame(_format_ctx, packet->get());
}
void FFmpegFormatCtx::DropEOS()
{
_format_ctx->pb->eof_reached = 0;
}
FFmpegFormatCtx::~FFmpegFormatCtx()
{
// _avio = nullptr;
avformat_close_input(&_format_ctx);
}