| | |
| | | #include "../ffmpeg/bridge/cvbridge.hpp" |
| | | #include "../ffmpeg/format/FormatIn.hpp" |
| | | #include "../ffmpeg/data/CodedData.hpp" |
| | | #include "../ffmpeg/data/FrameData.hpp" |
| | | #include "../ffmpeg/log/log.hpp" |
| | | |
| | | extern "C"{ |
| | |
| | | ,conv_h_(h) |
| | | ,conv_flag_(f) |
| | | ,decRef_(dec) |
| | | ,thread_(nullptr) |
| | | ,stop_{false} |
| | | {} |
| | | |
| | | decoder::~decoder(){ |
| | | if (thread_){ |
| | | stop_.store(true); |
| | | thread_->join(); |
| | | } |
| | | |
| | | if (conv_){ |
| | | delete conv_; |
| | | } |
| | | |
| | | |
| | | { |
| | | std::lock_guard<std::mutex> l(mutex_pkt_); |
| | | list_pkt_.clear(); |
| | | } |
| | | |
| | | { |
| | |
| | | return list_pic_.size(); |
| | | } |
| | | |
| | | void decoder::Start(){ |
| | | if (thread_) return; |
| | | thread_.reset(new std::thread([&]{ |
| | | if (initDecoder() != 0) { |
| | | return; |
| | | } |
| | | |
| | | while(!stop_.load()){ |
| | | |
| | | std::unique_lock<std::mutex> locker(mutex_pkt_); |
| | | cv_.wait(locker, [&]{ |
| | | return !list_pkt_.empty() || stop_.load(); |
| | | }); |
| | | if (stop_.load()){ |
| | | break; |
| | | } |
| | | |
| | | auto pkt = list_pkt_.front(); |
| | | list_pkt_.pop_front(); |
| | | |
| | | AVFrame *frame = av_frame_alloc(); |
| | | AVPacket np(pkt.data->getAVPacket()); |
| | | av_copy_packet(&np, &pkt.data->getAVPacket()); |
| | | |
| | | auto ret = decRef_->decode(frame, &np); |
| | | av_packet_unref(&np); |
| | | |
| | | if (ret == 0){ |
| | | saveFrame(frame, pkt.id); |
| | | } |
| | | av_frame_free(&frame); |
| | | } |
| | | |
| | | })); |
| | | } |
| | | |
| | | int decoder::SetFrame(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){ |
| | | |
| | | if (!data) return -1; |
| | | if (decRef_->isAudioPkt(&data->getAVPacket())) return -2; |
| | | |
| | | // if (!thread_){ |
| | | // if (initDecoder() != 0) return -3; |
| | | // Start(); |
| | | // } |
| | | |
| | | // std::lock_guard<std::mutex> l(mutex_pkt_); |
| | | // list_pkt_.push_back({data, id}); |
| | | // cv_.notify_one(); |
| | | // return list_pkt_.size(); |
| | | |
| | | if (!conv_){ |
| | | if (initDecoder() != 0) return -3; |
| | |
| | | saveFrame(frame, id); |
| | | } |
| | | av_frame_free(&frame); |
| | | |
| | | } |
| | | |
| | | void decoder::GetFrame(unsigned char **data, int *w, int *h, int64_t *id){ |