LCOV - code coverage report
Current view: top level - src/media/video - accel.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 1 6 16.7 %
Date: 2024-12-21 08:56:24 Functions: 1 6 16.7 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #pragma once
      18             : 
      19             : #include "libav_deps.h"
      20             : #include "media_codec.h"
      21             : 
      22             : #include <memory>
      23             : #include <string>
      24             : #include <vector>
      25             : #include <list>
      26             : 
      27             : extern "C" {
      28             : #include <libavutil/hwcontext.h>
      29             : }
      30             : 
      31             : namespace jami {
      32             : namespace video {
      33             : 
      34             : 
      35             : enum class DeviceState {
      36             :     NOT_TESTED,
      37             :     USABLE,
      38             :     NOT_USABLE
      39             : };
      40             : 
      41             : /**
      42             :  * @brief Provides an abstraction layer to the hardware acceleration APIs in FFmpeg.
      43             :  */
      44             : class HardwareAccel
      45             : {
      46             : public:
      47             :     /**
      48             :      * @brief Transfers hardware frame to main memory.
      49             :      *
      50             :      * Transfers a hardware decoded frame back to main memory. Should be called after
      51             :      * the frame is decoded using avcodec_send_packet/avcodec_receive_frame.
      52             :      *
      53             :      * If @frame is software, this is a no-op.
      54             :      *
      55             :      * @param frame Refrerence to the decoded hardware frame.
      56             :      * @param desiredFormat Software pixel format that the hardware outputs.
      57             :      * @returns Software frame.
      58             :      */
      59             :     static std::unique_ptr<VideoFrame> transferToMainMemory(const VideoFrame& frame,
      60             :                                                             AVPixelFormat desiredFormat);
      61             : 
      62             :     /**
      63             :      * @brief Constructs a HardwareAccel object
      64             :      *
      65             :      * Made public so std::unique_ptr can access it. Should not be called.
      66             :      */
      67             :     HardwareAccel(AVCodecID id,
      68             :                   const std::string& name,
      69             :                   AVHWDeviceType hwType,
      70             :                   AVPixelFormat format,
      71             :                   AVPixelFormat swFormat,
      72             :                   CodecType type,
      73             :                   bool dynBitrate);
      74             : 
      75             :     /**
      76             :      * @brief Dereferences hardware contexts.
      77             :      */
      78             :     ~HardwareAccel();
      79             : 
      80             :     /**
      81             :      * @brief Codec that is being accelerated.
      82             :      */
      83           0 :     AVCodecID getCodecId() const { return id_; };
      84             : 
      85             :     /**
      86             :      * @brief Name of the hardware layer/API being used.
      87             :      */
      88           0 :     const std::string& getName() const { return name_; };
      89             : 
      90             :     /**
      91             :      * @brief Hardware format.
      92             :      */
      93           0 :     AVPixelFormat getFormat() const { return format_; };
      94             : 
      95             :     /**
      96             :      * @brief Software format.
      97             :      *
      98             :      * For encoding it is the format expected by the hardware. For decoding
      99             :      * it is the format output by the hardware.
     100             :      */
     101           2 :     AVPixelFormat getSoftwareFormat() const { return swFormat_; }
     102             : 
     103             :     /**
     104             :      * @brief Gets the name of the codec.
     105             :      *
     106             :      * Decoding: avcodec_get_name(id_)
     107             :      * Encoding: avcodec_get_name(id_) + '_' + name_
     108             :      */
     109             :     std::string getCodecName() const;
     110             : 
     111             :     /**
     112             :      * @brief If hardware decoder can feed hardware encoder directly.
     113             :      *
     114             :      * Returns whether or not the decoder is linked to an encoder or vice-versa. Being linked
     115             :      * means an encoder can directly use the decoder's hardware frame, without first
     116             :      * transferring it to main memory.
     117             :      */
     118           0 :     bool isLinked() const { return linked_; }
     119             : 
     120             :     /**
     121             :      * @brief Set some extra details in the codec context.
     122             :      *
     123             :      * Should be called after a successful
     124             :      * setup (setupDecoder or setupEncoder).
     125             :      * For decoding, sets the hw_device_ctx and get_format callback. If the decoder has
     126             :      * a frames context, mark as linked.
     127             :      * For encoding, sets hw_device_ctx and hw_frames_ctx, and may set some hardware
     128             :      * codec options.
     129             :      */
     130             :     void setDetails(AVCodecContext* codecCtx);
     131             : 
     132             :     /**
     133             :      * @brief Transfers a frame to/from the GPU memory.
     134             :      *
     135             :      * Transfers a hardware decoded frame back to main memory. Should be called after
     136             :      * the frame is decoded using avcodec_send_packet/avcodec_receive_frame or before
     137             :      * the frame is encoded using avcodec_send_frame/avcodec_receive_packet.
     138             :      *
     139             :      * @param frame Hardware frame when decoding, software frame when encoding.
     140             :      * @returns Software frame when decoding, hardware frame when encoding.
     141             :      */
     142             :     std::unique_ptr<VideoFrame> transfer(const VideoFrame& frame);
     143             : 
     144             :     /**
     145             :      * @brief Links this HardwareAccel's frames context with the passed in context.
     146             :      *
     147             :      * This serves to skip transferring a decoded frame back to main memory before encoding.
     148             :      */
     149             :     bool linkHardware(AVBufferRef* framesCtx);
     150             : 
     151             :     static std::list<HardwareAccel> getCompatibleAccel(AVCodecID id,
     152             :                                                        int width,
     153             :                                                        int height,
     154             :                                                        CodecType type);
     155             :     int initAPI(bool linkable, AVBufferRef* framesCtx);
     156           0 :     bool dynBitrate() { return dynBitrate_; }
     157             : 
     158             : private:
     159             :     bool initDevice(const std::string& device);
     160             :     bool initFrame();
     161             : 
     162             :     AVCodecID id_ {AV_CODEC_ID_NONE};
     163             :     std::string name_;
     164             :     AVHWDeviceType hwType_ {AV_HWDEVICE_TYPE_NONE};
     165             :     AVPixelFormat format_ {AV_PIX_FMT_NONE};
     166             :     AVPixelFormat swFormat_ {AV_PIX_FMT_NONE};
     167             :     CodecType type_ {CODEC_NONE};
     168             :     bool linked_ {false};
     169             :     int width_ {0};
     170             :     int height_ {0};
     171             :     bool dynBitrate_ {false};
     172             : 
     173             :     AVBufferRef* deviceCtx_ {nullptr};
     174             :     AVBufferRef* framesCtx_ {nullptr};
     175             : 
     176             :     int init_device(const char* name, const char* device, int flags);
     177             :     int init_device_type(std::string& dev);
     178             : 
     179             :     std::list<std::pair<std::string, DeviceState>>* possible_devices_;
     180             : };
     181             : 
     182             : } // namespace video
     183             : } // namespace jami

Generated by: LCOV version 1.14