hunting/utils/MediaBase/src/FfmpegDecoderV2.h
2024-07-25 02:04:40 +08:00

74 lines
2.5 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FFMPEG_DECODER_V2_H
#define FFMPEG_DECODER_V2_H
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavcodec/packet.h>
#include <libavformat/avformat.h>
#include <libavutil/avassert.h>
#include <libavutil/avutil.h>
#include <libavutil/channel_layout.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/opt.h>
#include <libavutil/timestamp.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
#include <functional>
// constexpr int DECODER_UNSUPORTED = 0;
class FfmpegDecoderV2
{
public:
/**
* @brief When decoding a video stream, you need to use this constructor to provide the required parameters.
* @param codecId Video stream format
* @param width Video height
* @param height Video width
*/
FfmpegDecoderV2(const enum AVCodecID &codecId, const AVPixelFormat &decodePixelFormat, const int &width,
const int &height);
virtual ~FfmpegDecoderV2() = default;
bool Init(void);
bool UnInit(void);
void DecodeData(const void *data, const size_t &size, const unsigned long long &pts,
std::function<void(AVFrame *frame)> callback);
private:
void AVParseData(const void *data, const size_t &size, std::function<void(AVFrame *frame)> callback);
void AVDecodeData(AVPacket *pkt, std::function<void(AVFrame *frame)> callback);
private:
static int select_sample_rate(const AVCodec *codec);
static int select_channel_layout(const AVCodec *codec, AVChannelLayout *dst);
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt);
private:
const enum AVCodecID mCodecId;
AVCodec *mCodec;
AVCodecContext *mCodecCtx;
AVFrame *mFrame;
AVPacket *mPacket;
AVCodecParserContext *mParser;
const int mVideoWidth;
const int mVideoHeight;
const AVPixelFormat mDecodePixelFormat;
};
#endif