forked from sophgo/sophon-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_decode.h
134 lines (99 loc) · 4.29 KB
/
stream_decode.h
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
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2022 Sophgo Technologies Inc. All rights reserved.
//
// SOPHON-PIPELINE is licensed under the 2-Clause BSD License except for the
// third-party components.
//
//===----------------------------------------------------------------------===//
#ifndef BMUTILITY_STREAM_DECODE_H
#define BMUTILITY_STREAM_DECODE_H
#include "stream_demuxer.h"
namespace bm {
#if LIBAVCODEC_VERSION_MAJOR <= 56
static AVPacket *av_packet_alloc() {
AVPacket* pkt = new AVPacket;
av_init_packet(pkt);
return pkt;
}
static void av_packet_free(AVPacket** pkt){
av_free_packet(*pkt);
av_freep(pkt);
}
#endif
struct StreamDecoderEvents {
virtual ~StreamDecoderEvents() {}
virtual void on_decoded_avframe(const AVPacket *pkt, const AVFrame *pFrame) = 0;
virtual void on_decoded_sei_info(const uint8_t *sei_data, int sei_data_len, uint64_t pts, int64_t pkt_pos){};
virtual void on_stream_eof() {};
};
class StreamDecoder : public StreamDemuxerEvents {
StreamDecoderEvents *m_observer;
using OnDecodedFrameCallback = std::function<void(const AVPacket *pkt, const AVFrame *pFrame)>;
using OnDecodedSEICallback =std::function<void(const uint8_t *sei_data, int sei_data_len, uint64_t pts, int64_t pkt_pos)>;
using OnStreamEofCallback = std::function<void()>;
OnDecodedFrameCallback m_OnDecodedFrameFunc;
OnDecodedSEICallback m_OnDecodedSEIFunc;
StreamDemuxer::OnAVFormatOpenedFunc m_pfnOnAVFormatOpened;
StreamDemuxer::OnAVFormatClosedFunc m_pfnOnAVFormatClosed;
StreamDemuxer::OnReadFrameFunc m_pfnOnReadFrame;
StreamDemuxer::OnReadEofFunc m_pfnOnReadEof;
protected:
std::list<AVPacket *> m_list_packets;
AVCodecContext *m_dec_ctx{nullptr};
AVCodecContext *m_external_dec_ctx {nullptr};
int m_video_stream_index{0};
int m_frame_decoded_num{0};
StreamDemuxer m_demuxer;
AVDictionary *m_opts_decoder{nullptr};
bool m_is_waiting_iframe{true};
int m_id{0};
AVRational m_timebase;
//Functions
int create_video_decoder(AVFormatContext *ifmt_ctx);
int put_packet(AVPacket *pkt);
AVPacket *get_packet();
void clear_packets();
int decode_frame(AVPacket *pkt, AVFrame *pFrame);
int get_video_stream_index(AVFormatContext *ifmt_ctx);
bool is_key_frame(AVPacket *pkt);
//
//Overload StreamDemuxerEvents Interface.
//
virtual void on_avformat_opened(AVFormatContext *ifmt_ctx) override;
virtual void on_avformat_closed() override;
virtual int on_read_frame(AVPacket *pkt) override;
virtual void on_read_eof(AVPacket *pkt) override;
public:
StreamDecoder(int id, AVCodecContext *decoder=nullptr);
virtual ~StreamDecoder();
int set_observer(StreamDecoderEvents *observer);
void set_decoded_frame_callback(OnDecodedFrameCallback func)
{
m_OnDecodedFrameFunc = func;
}
void set_decoded_sei_info_callback(OnDecodedSEICallback func)
{
m_OnDecodedSEIFunc = func;
}
void set_avformat_opend_callback(StreamDemuxer::OnAVFormatOpenedFunc func) {
m_pfnOnAVFormatOpened = func;
}
void set_avformat_closed_callback(StreamDemuxer::OnAVFormatClosedFunc func){
m_pfnOnAVFormatClosed = func;
}
void set_read_Frame_callback(StreamDemuxer::OnReadFrameFunc func){
m_pfnOnReadFrame = func;
}
void set_read_eof_callback(StreamDemuxer::OnReadEofFunc func){
m_pfnOnReadEof = func;
}
int open_stream(std::string url, bool repeat = true, AVDictionary *opts=nullptr);
int close_stream(bool is_waiting = true);
AVCodecID get_video_codec_id();
//External utilities
static AVPacket* ffmpeg_packet_alloc();
static AVCodecContext* ffmpeg_create_decoder(enum AVCodecID id, AVDictionary **opts=nullptr);
};
}
#endif //BM_FFMPEG_DECODE_TEST_STREAM_DECODE_H