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
| #include "VideoDecoder.h" #include <iostream>
VideoDecoder::VideoDecoder(const std::string& filePath) : filePath(filePath), pFormatCtx(nullptr), pCodecCtx(nullptr), pFrame(nullptr), pFrameRGB(nullptr), swsCtx(nullptr), buffer(nullptr), videoStream(-1) {}
VideoDecoder::~VideoDecoder() { av_free(buffer); av_frame_free(&pFrameRGB); av_frame_free(&pFrame); avcodec_free_context(&pCodecCtx); avformat_close_input(&pFormatCtx); sws_freeContext(swsCtx); }
bool VideoDecoder::initialize() { pFormatCtx = avformat_alloc_context(); if (avformat_open_input(&pFormatCtx, filePath.c_str(), nullptr, nullptr) != 0) { return false; }
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) { return false; } for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { return false; }
AVCodecParameters* pCodecPar = pFormatCtx->streams[videoStream]->codecpar; const AVCodec* pCodec = avcodec_find_decoder(pCodecPar->codec_id); if (pCodec == nullptr) { return false; }
pCodecCtx = avcodec_alloc_context3(pCodec); if (!pCodecCtx) { std::cerr << "error1" << std::endl; return false; }
if (avcodec_parameters_to_context(pCodecCtx, pCodecPar) < 0) { std::cerr << "error2" << std::endl; return false; }
if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { std::cerr << "error3" << std::endl; return false; }
pFrame = av_frame_alloc(); pFrameRGB = av_frame_alloc(); if (pFrameRGB == nullptr || pFrame == nullptr) { std::cerr << "分配帧失败" << std::endl; return false; }
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 32); buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t)); av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
swsCtx = sws_getContext( pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr );
return true; }
bool VideoDecoder::decodeFrame() { AVPacket packet; if (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { int ret = avcodec_send_packet(pCodecCtx, &packet); if (ret >= 0) { ret = avcodec_receive_frame(pCodecCtx, pFrame); av_packet_unref(&packet); if (ret >= 0) { sws_scale( swsCtx, (uint8_t const* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize ); return true; } } } av_packet_unref(&packet); } return false; }
void VideoDecoder::printVideoInfo() const { std::cout << "Video Information:" << std::endl; std::cout << "Format: " << pFormatCtx->iformat->long_name << std::endl; std::cout << "Duration: " << pFormatCtx->duration / AV_TIME_BASE << " seconds" << std::endl; std::cout << "Width: " << pCodecCtx->width << std::endl; std::cout << "Height: " << pCodecCtx->height << std::endl; std::cout << "Frame Rate: " << av_q2d(pFormatCtx->streams[videoStream]->avg_frame_rate) << " fps" << std::endl; std::cout << "Codec: " << pCodecCtx->codec->long_name << std::endl; }
uint8_t* VideoDecoder::getFrameData() { return pFrameRGB->data[0]; }
int VideoDecoder::getWidth() const { return pCodecCtx->width; }
int VideoDecoder::getHeight() const { return pCodecCtx->height; }
double VideoDecoder::getFrameRate() const { return av_q2d(pFormatCtx->streams[videoStream]->avg_frame_rate); }
|