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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| #include "xsdl.h" #include "xvideo_view.h" #include <iostream>
using namespace std;
extern "C" { #include <libavcodec/avcodec.h> }
#pragma comment(lib, "avutil.lib")
bool XVideoView::Init(AVCodecParameters *para) { if (!para) return false; auto fmt = (Format)para->format; switch (para->format) { case AV_PIX_FMT_YUV420P: case AV_PIX_FMT_YUVJ420P: fmt = YUV420P; break; default: break; } return Init(para->width, para->height, fmt); }
AVFrame* XVideoView::Read() { if (width_ <= 0 || height_ <= 0 || !ifs_) return nullptr; if (frame_) { if (frame_->width != width_ || frame_->height != height_ || frame_->format != fmt_) { av_frame_free(&frame_); } }
if (!frame_) { frame_ = av_frame_alloc(); frame_->width = width_; frame_->height = height_; frame_->format = fmt_; frame_->linesize[0] = width_ * 4; if (frame_->format == AV_PIX_FMT_YUV420P) { frame_->linesize[0] = width_; frame_->linesize[1] = width_ / 2; frame_->linesize[2] = width_ / 2; }
auto re = av_frame_get_buffer(frame_, 0);
if (re != 0) { char buf[1024] = { 0 }; av_strerror(re, buf, sizeof(buf) - 1); cout << buf << endl; av_frame_free(&frame_); return NULL; } }
if (!frame_) return NULL;
if (frame_->format == AV_PIX_FMT_YUV420P) { ifs_.read((char*)frame_->data[0], frame_->linesize[0] * height_); ifs_.read((char*)frame_->data[1], frame_->linesize[1] * height_ / 2); ifs_.read((char*)frame_->data[2], frame_->linesize[2] * height_ / 2); } else { ifs_.read((char*)frame_->data[0], frame_->linesize[0] * height_); }
if (ifs_.gcount() == 0) return NULL; return frame_; }
bool XVideoView::Open(std::string filepath) { if (ifs_.is_open()) { ifs_.close(); } ifs_.open(filepath, ios::binary);
return ifs_.is_open(); }
XVideoView* XVideoView::Create(RenderType type) { switch (type) { case XVideoView::SDL: return new XSDL(); break; default: break; }
return nullptr; }
XVideoView::~XVideoView() { if (cache_) delete cache_; cache_ = nullptr; }
bool XVideoView::DrawFrame(AVFrame* frame) { if (!frame || !frame->data[0]) return false; count_++; if (beg_ms_ <= 0) { beg_ms_ = clock(); } else if((clock() - beg_ms_) / (CLOCKS_PER_SEC / 1000) >= 1000) { render_fps_ = count_; count_ = 0; beg_ms_ = clock(); }
int linesize = 0; switch (frame->format) { case AV_PIX_FMT_YUV420P: return Draw(frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2] ); case AV_PIX_FMT_YUVJ420P: return Draw(frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2] ); case AV_PIX_FMT_NV12: if (!cache_) { cache_ = new unsigned char[4092 * 2160 * 1.5]; } linesize = frame->width;
if (frame->linesize[0] == frame->width) { memcpy(cache_, frame->data[0], frame->linesize[0] * frame->height); memcpy(cache_ + frame->linesize[0] * frame->height, frame->data[1], frame->linesize[1] * frame->height / 2); } else { for (int i = 0; i < frame->height; i++) { memcpy(cache_ + i * frame->width, frame->data[0] + i * frame->linesize[0], frame->width ); } for (int i = 0; i < frame->height; i++) { auto p = cache_ + frame->height * frame->width; memcpy(p + i * frame->width, frame->data[1] + i * frame->linesize[1], frame->width ); } }
return Draw(cache_, linesize); break; case AV_PIX_FMT_BGRA: return Draw(frame->data[0], frame->linesize[0]); case AV_PIX_FMT_ARGB: return Draw(frame->data[0], frame->linesize[0]); case AV_PIX_FMT_RGBA: return Draw(frame->data[0], frame->linesize[0]); default: break; } return false; }
|