OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
AVVideoDecoder Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/AVVideoDecoder.hpp>

Collaboration diagram for AVVideoDecoder:

Public Member Functions

 AVVideoDecoder ()
 ~AVVideoDecoder ()
int open (Bambu_StreamInfo const &info)
int open (AVCodecParameters const &parameters)
int decode (Bambu_Sample const &sample)
int decode (AVPacket const &packet)
int flush ()
void close ()
bool toWxImage (wxImage &image, wxSize const &size)
bool toWxBitmap (wxBitmap &bitmap, wxSize const &size)
wxSize decoded_frame_size () const

Private Attributes

AVCodecContext * codec_ctx_ = nullptr
AVFrame * frame_ = nullptr
SwsContext * sws_ctx_ = nullptr
bool got_frame_ = false
int width_ { 0 }
std::vector< uint8_t > bits_

Detailed Description

Constructor & Destructor Documentation

◆ AVVideoDecoder()

AVVideoDecoder::AVVideoDecoder ( )
20{
21 codec_ctx_ = avcodec_alloc_context3(nullptr);
22}
AVCodecContext * codec_ctx_
Definition AVVideoDecoder.hpp:52

References codec_ctx_.

◆ ~AVVideoDecoder()

AVVideoDecoder::~AVVideoDecoder ( )
25{
26 if (sws_ctx_)
27 sws_freeContext(sws_ctx_);
28 if (frame_)
29 av_frame_free(&frame_);
30 if (codec_ctx_)
31 avcodec_free_context(&codec_ctx_);
32}
AVFrame * frame_
Definition AVVideoDecoder.hpp:53
SwsContext * sws_ctx_
Definition AVVideoDecoder.hpp:54

References codec_ctx_, frame_, and sws_ctx_.

Member Function Documentation

◆ close()

void AVVideoDecoder::close ( )
117{
118}

◆ decode() [1/2]

int AVVideoDecoder::decode ( AVPacket const & packet)
102{
103 int ret = avcodec_send_packet(codec_ctx_, &packet);
104 if (ret == 0)
105 got_frame_ = avcodec_receive_frame(codec_ctx_, frame_) == 0;
106 return ret;
107}
bool got_frame_
Definition AVVideoDecoder.hpp:55

References codec_ctx_, frame_, and got_frame_.

◆ decode() [2/2]

int AVVideoDecoder::decode ( Bambu_Sample const & sample)
76{
77 int ret = -1;
78 AVPacket *pkt = av_packet_alloc();
79 if (!pkt) {
80 return ret;
81 }
82
83 ret = av_new_packet(pkt, sample.size);
84 if (ret != 0) {
85 av_packet_free(&pkt);
86 return ret;
87 }
88
89 memcpy(pkt->data, sample.buffer, size_t(sample.size));
90
91 ret = avcodec_send_packet(codec_ctx_, pkt);
92 if (ret == 0) {
93 got_frame_ = avcodec_receive_frame(codec_ctx_, frame_) == 0;
94 }
95
96 av_packet_unref(pkt);
97 av_packet_free(&pkt);
98 return ret;
99}

References __Bambu_Sample::buffer, codec_ctx_, frame_, got_frame_, and __Bambu_Sample::size.

Referenced by wxMediaCtrl3::PlayFfmpeg(), and wxMediaCtrl3::PlayThread().

Here is the caller graph for this function:

◆ decoded_frame_size()

wxSize AVVideoDecoder::decoded_frame_size ( ) const
inline
47 {
48 return got_frame_ && frame_ ? wxSize{frame_->width, frame_->height} : wxSize{};
49 }

References frame_, and got_frame_.

Referenced by wxMediaCtrl3::PlayFfmpeg().

Here is the caller graph for this function:

◆ flush()

int AVVideoDecoder::flush ( )
110{
111 int ret = avcodec_send_packet(codec_ctx_, nullptr);
112 got_frame_ = avcodec_receive_frame(codec_ctx_, frame_) == 0;
113 return ret;
114}

References codec_ctx_, frame_, and got_frame_.

◆ open() [1/2]

int AVVideoDecoder::open ( AVCodecParameters const & parameters)
57{
58 if (avcodec_parameters_to_context(codec_ctx_, &parameters) < 0)
59 return -1;
60
61 auto codec = avcodec_find_decoder(codec_ctx_->codec_id);
62 if (codec == nullptr) {
63 fprintf(stderr, "AVVideoDecoder: unsupported codec!\n");
64 return -1;
65 }
66 if (avcodec_open2(codec_ctx_, codec, nullptr) < 0) {
67 fprintf(stderr, "AVVideoDecoder: could not open codec\n");
68 return -1;
69 }
70
71 frame_ = av_frame_alloc();
72 return frame_ == nullptr ? -1 : 0;
73}

References codec_ctx_, and frame_.

◆ open() [2/2]

int AVVideoDecoder::open ( Bambu_StreamInfo const & info)
35{
36 auto codec_id = info.sub_type == AVC1 ? AV_CODEC_ID_H264 : AV_CODEC_ID_MJPEG;
37 auto codec = avcodec_find_decoder(codec_id);
38 if (codec == nullptr) {
39 fprintf(stderr, "AVVideoDecoder: unsupported codec!\n");
40 return -1; // Codec not found
41 }
42 /* open the coderc */
43 if (avcodec_open2(codec_ctx_, codec, nullptr) < 0) {
44 fprintf(stderr, "AVVideoDecoder: could not open codec\n");
45 return -1;
46 }
47
48 // Allocate an AVFrame structure
49 frame_ = av_frame_alloc();
50 if (frame_ == nullptr)
51 return -1;
52
53 return 0;
54}
@ AVC1
Definition BambuTunnel.h:48

References AVC1, codec_ctx_, frame_, and __Bambu_StreamInfo::sub_type.

Referenced by wxMediaCtrl3::PlayFfmpeg(), and wxMediaCtrl3::PlayThread().

Here is the caller graph for this function:

◆ toWxBitmap()

bool AVVideoDecoder::toWxBitmap ( wxBitmap & bitmap,
wxSize const & size )
165{
166 if (!got_frame_)
167 return false;
168
169 auto size1 = size2;
170 if (!size1.IsFullySpecified())
171 size1 = {frame_->width, frame_->height };
172 auto size = size1;
173 if (size.GetWidth() & 0x0f) {
174 size.SetWidth((size.GetWidth() & ~0x0f) + 0x10);
175 if (size.GetWidth() != width_) {
176 std::fill(bits_.begin(), bits_.end(), 0);
177 width_ = size.GetWidth();
178 }
179 }
180 AVPixelFormat wxFmt = AV_PIX_FMT_RGB32;
181 sws_ctx_ = sws_getCachedContext(sws_ctx_,
182 frame_->width, frame_->height, AVPixelFormat(frame_->format),
183 size1.GetWidth(), size1.GetHeight(), wxFmt,
184 SWS_GAUSS,
185 nullptr, nullptr, nullptr);
186 if (sws_ctx_ == nullptr)
187 return false;
188 int length = size.GetWidth() * size.GetHeight() * 4;
189 if (bits_.size() < length)
190 bits_.resize(length);
191 uint8_t *datas[] = { bits_.data() };
192 int strides[] = { size.GetWidth() * 4 };
193 int result_h = sws_scale(sws_ctx_, frame_->data, frame_->linesize, 0, frame_->height, datas, strides);
194 if (result_h != size.GetHeight()) {
195 fprintf(stderr, "AVVideoDecoder: result_h %d %d\n", result_h, size.GetHeight());
196 return false;
197 }
198 bitmap = wxBitmap((char const *) bits_.data(), size.GetWidth(), size.GetHeight(), 32);
199 assert(bitmap.IsOk());
200 if (!bitmap.IsOk()) {
201 fprintf(stderr, "AVVideoDecoder: bitmap not ok %dx%d\n", size.GetWidth(), size.GetHeight());
202 return false;
203 }
204 return true;
205}
std::vector< uint8_t > bits_
Definition AVVideoDecoder.hpp:57
int width_
Definition AVVideoDecoder.hpp:56
double length(const Points &pts)
Definition MultiPoint.hpp:181

References bits_, frame_, got_frame_, sws_ctx_, and width_.

Referenced by wxMediaCtrl3::PlayFfmpeg(), and wxMediaCtrl3::PlayThread().

Here is the caller graph for this function:

◆ toWxImage()

bool AVVideoDecoder::toWxImage ( wxImage & image,
wxSize const & size )
121{
122 if (!got_frame_)
123 return false;
124
125 auto size1 = size2;
126 if (!size1.IsFullySpecified())
127 size1 = {frame_->width, frame_->height };
128 auto size = size1;
129 if (size.GetWidth() & 0x0f) {
130 size.SetWidth((size.GetWidth() & ~0x0f) + 0x10);
131 if (size.GetWidth() != width_) {
132 std::fill(bits_.begin(), bits_.end(), 0);
133 width_ = size.GetWidth();
134 }
135 }
136 AVPixelFormat wxFmt = AV_PIX_FMT_RGB24;
137 sws_ctx_ = sws_getCachedContext(sws_ctx_,
138 frame_->width, frame_->height, AVPixelFormat(frame_->format),
139 size1.GetWidth(), size1.GetHeight(), wxFmt,
140 SWS_GAUSS,
141 nullptr, nullptr, nullptr);
142 if (sws_ctx_ == nullptr)
143 return false;
144 int length = size.GetWidth() * size.GetHeight() * 3;
145 if (bits_.size() < length)
146 bits_.resize(length);
147 uint8_t * datas[] = { bits_.data() };
148 int strides[] = { size.GetWidth() * 3 };
149 int result_h = sws_scale(sws_ctx_, frame_->data, frame_->linesize, 0, frame_->height, datas, strides);
150 if (result_h != size.GetHeight()) {
151 return false;
152 }
153 // Copy: the frame outlives this decoder and is painted by the GUI thread while the
154 // next sws_scale is already overwriting bits_, so it must own its pixels. The Windows
155 // path below needs no equivalent, wxBitmap copies the bits into GDI.
156 image = wxImage(size.GetWidth(), size.GetHeight(), bits_.data(), true).Copy();
157 if (!image.IsOk()) {
158 fprintf(stderr, "AVVideoDecoder: image not ok %dx%d\n", size.GetWidth(), size.GetHeight());
159 return false;
160 }
161 return true;
162}

References bits_, frame_, got_frame_, sws_ctx_, and width_.

Referenced by wxMediaCtrl3::PlayFfmpeg(), and wxMediaCtrl3::PlayThread().

Here is the caller graph for this function:

Member Data Documentation

◆ bits_

std::vector<uint8_t> AVVideoDecoder::bits_
private

Referenced by toWxBitmap(), and toWxImage().

◆ codec_ctx_

AVCodecContext* AVVideoDecoder::codec_ctx_ = nullptr
private

◆ frame_

AVFrame* AVVideoDecoder::frame_ = nullptr
private

◆ got_frame_

bool AVVideoDecoder::got_frame_ = false
private

◆ sws_ctx_

SwsContext* AVVideoDecoder::sws_ctx_ = nullptr
private

◆ width_

int AVVideoDecoder::width_ { 0 }
private
56{ 0 }; // scale result width

Referenced by toWxBitmap(), and toWxImage().


The documentation for this class was generated from the following files: