9#include "libmythbase/mythconfig.h"
30#include "libavcodec/defs.h"
31#include "libavutil/opt.h"
34#define LOC QString("V4L2_M2M: ")
54 const AVCodec **Codec,
59 bool decodeonly =
Decoder ==
"v4l2-dec";
64 if (!
Decoder.startsWith(
"v4l2"))
92 if (!standard.contains(mythprofile))
95 if (!requests.contains(mythprofile))
106 if ((*Codec)->id == AV_CODEC_ID_H264)
108 if ((*Context)->profile == AV_PROFILE_H264_HIGH_10 ||
109 (*Context)->profile == AV_PROFILE_H264_HIGH_10_INTRA)
118 (*Context)->pix_fmt = AV_PIX_FMT_DRM_PRIME;
123 success, failure,
"v4l2m2m",
124 decodeonly ? (*Context)->pix_fmt : AV_PIX_FMT_DRM_PRIME);
159 DirectRendering =
false;
165 DirectRendering =
false;
194 if (!(Context && Codec))
197 if (!(Codec->priv_class && Context->priv_data))
203 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"Setting number of capture buffers to 6");
204 av_opt_set_int(Context->priv_data,
"num_capture_buffers", 6, 0);
216 if (!Context || !AvFrame || !
Frame)
222 const auto * supported =
Frame->m_renderFormats;
224#ifdef __cpp_lib_ranges_contains
225 if (!std::ranges::contains(*supported,
type))
228 if (found == supported->end())
233 if ((
Frame->m_type !=
type) || (
Frame->m_width != AvFrame->width) || (
Frame->m_height != AvFrame->height))
239 for (
uint plane = 0; plane < count; ++plane)
242 AvFrame->data[plane], AvFrame->linesize[plane],
250#ifndef V4L2_PIX_FMT_HEVC
251#define V4L2_PIX_FMT_HEVC v4l2_fourcc('H', 'E', 'V', 'C')
254#ifndef V4L2_PIX_FMT_VP9
255#define V4L2_PIX_FMT_VP9 v4l2_fourcc('V', 'P', '9', '0')
258#ifndef V4L2_PIX_FMT_NV12_COL128
259#define V4L2_PIX_FMT_NV12_COL128 v4l2_fourcc('N', 'C', '1', '2')
262#ifndef V4L2_PIX_FMT_NV12_10_COL128
263#define V4L2_PIX_FMT_NV12_10_COL128 v4l2_fourcc('N', 'C', '3', '0')
268 static const std::vector<V4L2Mapping> s_map
281 static QRecursiveMutex lock;
282 static bool s_initialised =
false;
285 QMutexLocker locker(&lock);
288 s_initialised =
true;
294 static const std::vector<uint32_t> s_formats
296 V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV420M,
297 V4L2_PIX_FMT_YVU420M, V4L2_PIX_FMT_NV12, V4L2_PIX_FMT_NV12M,
304 const QString root(
"/dev/");
306 QStringList namefilters;
307 namefilters.append(
"video*");
308 auto devices = dir.entryList(namefilters, QDir::Files |QDir::System);
309 for (
const QString& device : std::as_const(devices))
313 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Device: %1 Driver: '%2' Capabilities: 0x%3")
318 bool mplanar = ((caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE)) != 0U) &&
319 ((caps & V4L2_CAP_STREAMING) != 0U);
320 bool mplanarm2m = (caps & V4L2_CAP_VIDEO_M2M_MPLANE) != 0U;
321 bool splanar = ((caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT)) != 0U) &&
322 ((caps & V4L2_CAP_STREAMING) != 0U);
323 bool splanarm2m = (caps & V4L2_CAP_VIDEO_M2M) != 0U;
325 if (!(mplanar || mplanarm2m || splanar || splanarm2m))
328 v4l2_buf_type capturetype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
329 v4l2_buf_type outputtype = V4L2_BUF_TYPE_VIDEO_OUTPUT;
331 if (mplanar || mplanarm2m)
333 capturetype = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
334 outputtype = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
340 for (
const auto &
profile : Profiles)
343 uint32_t v4l2pixfmt =
profile.first;
344 auto mythprofile =
profile.second;
345 struct v4l2_fmtdesc fdesc {};
346 memset(&fdesc, 0,
sizeof(fdesc));
349 fdesc.type = outputtype;
352 int res = ioctl(v4l2dev.
FD(), VIDIOC_ENUM_FMT, &fdesc);
355 if (fdesc.pixelformat == v4l2pixfmt)
362 QStringList pixformats;
363 bool foundfmt =
false;
365 memset(&fdesc, 0,
sizeof(fdesc));
366 fdesc.type = capturetype;
369 int res = ioctl(v4l2dev.
FD(), VIDIOC_ENUM_FMT, &fdesc);
372 pixformats.append(
fourcc_str(
static_cast<int>(fdesc.pixelformat)));
373#ifdef __cpp_lib_ranges_contains
374 if (std::ranges::contains(s_formats, fdesc.pixelformat))
379 if (!result.contains(mythprofile))
380 result.append(mythprofile);
389 if (pixformats.isEmpty())
390 pixformats.append(
"None");
391 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Codec '%1' has no supported formats (Supported: %2)")
404 if (!profiles.isEmpty())
407 Decoders.append(
"V4L2:");
413 if (!requests.isEmpty())
416 Decoders.append(
"V4L2 Request:");
425 static QRecursiveMutex lock;
426 QMutexLocker locker(&lock);
427 static bool s_checked =
false;
428 static bool s_available =
false;
430 if (s_checked && !Reinit)
436 if (standard.isEmpty() && request.isEmpty())
438 LOG(VB_GENERAL, LOG_INFO,
LOC +
"No V4L2 decoders found");
442 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Supported/available V4L2 decoders:");
445 for (
auto profile : std::as_const(standard))
447 for (
auto profile : std::as_const(request))
452#ifndef V4L2_PIX_FMT_MPEG2_SLICE
453#define V4L2_PIX_FMT_MPEG2_SLICE v4l2_fourcc('M', 'G', '2', 'S')
456#ifndef V4L2_PIX_FMT_H264_SLICE
457#define V4L2_PIX_FMT_H264_SLICE v4l2_fourcc('S', '2', '6', '4')
460#ifndef V4L2_PIX_FMT_VP8_FRAME
461#define V4L2_PIX_FMT_VP8_FRAME v4l2_fourcc('V', 'P', '8', 'F')
464#ifndef V4L2_PIX_FMT_VP9_FRAME
465#define V4L2_PIX_FMT_VP9_FRAME v4l2_fourcc('V', 'P', '9', 'F')
468#ifndef V4L2_PIX_FMT_HEVC_SLICE
469#define V4L2_PIX_FMT_HEVC_SLICE v4l2_fourcc('S', '2', '6', '5')
474 static const std::vector<V4L2Mapping> s_map
483 static QRecursiveMutex lock;
484 static bool s_initialised =
false;
487 QMutexLocker locker(&lock);
490 s_initialised =
true;
496 while (*PixFmt != AV_PIX_FMT_NONE)
498 if (*PixFmt == AV_PIX_FMT_DRM_PRIME)
501 "V4L2 request context creation") >= 0)
503 return AV_PIX_FMT_DRM_PRIME;
508 return AV_PIX_FMT_NONE;
522 Context->hw_device_ctx = hwdeviceref;
static VideoFrameType PixelFormatToFrameType(AVPixelFormat Fmt)
static bool FrameTypeIsSupported(AVCodecContext *Context, VideoFrameType Format)
static AVBufferRef * CreateDevice(AVHWDeviceType Type, MythInteropGPU *Interop, const QString &Device=QString())
static int InitialiseDecoder2(AVCodecContext *Context, CreateHWDecoder Callback, const QString &Debug)
Initialise a hardware decoder that is NOT expected to use AVHWFramesContext.
static QString GetProfileDescription(CodecProfile Profile, QSize Size, VideoFrameType Format=FMT_NONE, uint ColorDepth=0)
A generic context handler for codecs that return AV_PIX_FMT_DRM_PRIME frames.
void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering) override
bool GetDRMBuffer(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame, int)
static MythCodecID GetPrimeCodec(AVCodecContext **Context, const AVCodec **Codec, AVStream *Stream, MythCodecID Successs, MythCodecID Failure, const QString &CodecName, AVPixelFormat Format)
int HwDecoderInit(AVCodecContext *Context) override
bool RetrieveFrame(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame) override
static bool CheckCodecSize(int Width, int Height, MythCodecContext::CodecProfile Profile)
static MythCodecID GetSupportedCodec(AVCodecContext **Context, const AVCodec **Codec, const QString &Decoder, AVStream *Stream, uint StreamType)
static int InitialiseV4L2RequestContext(AVCodecContext *Context)
void SetDecoderOptions(AVCodecContext *Context, const AVCodec *Codec) override
Reduce the number of capture buffers.
static const V4L2Profiles & GetRequestProfiles()
static V4L2Profiles GetProfiles(const std::vector< V4L2Mapping > &Profiles)
static enum AVPixelFormat GetV4L2RequestFormat(AVCodecContext *Context, const AVPixelFormat *PixFmt)
static bool HaveV4L2Codecs(bool Reinit=false)
static void GetDecoderList(QStringList &Decoders)
bool RetrieveFrame(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame) override
void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering) override
static const V4L2Profiles & GetStandardProfiles()
bool DecoderWillResetOnFlush() override
static bool GetBuffer(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame, int)
Retrieve a frame from CPU memory.
MythV4L2M2MContext(DecoderBase *Parent, MythCodecID CodecID)
int HwDecoderInit(AVCodecContext *Context) override
static uint GetNumPlanes(VideoFrameType Type)
static int GetPitchForPlane(VideoFrameType Type, int Width, uint Plane)
static int GetHeightForPlane(VideoFrameType Type, int Height, uint Plane)
static void CopyPlane(uint8_t *To, int ToPitch, const uint8_t *From, int FromPitch, int PlaneWidth, int PlaneHeight)
QString GetDriverName(void) const
QString GetDeviceName(void) const
uint32_t GetCapabilities(void) const
static bool ReinitBuffer(MythVideoFrame *Frame, VideoFrameType Type, MythCodecID CodecID, int Width, int Height)
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)
static const char * fourcc_str(int i)
static bool codec_is_v4l2_dec(MythCodecID id)
static bool codec_is_v4l2(MythCodecID id)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
#define V4L2_PIX_FMT_VP8_FRAME
#define V4L2_PIX_FMT_H264_SLICE
#define V4L2_PIX_FMT_HEVC_SLICE
#define V4L2_PIX_FMT_VP9_FRAME
#define V4L2_PIX_FMT_NV12_COL128
#define V4L2_PIX_FMT_MPEG2_SLICE
#define V4L2_PIX_FMT_NV12_10_COL128
#define V4L2_PIX_FMT_HEVC
QList< MythCodecContext::CodecProfile > V4L2Profiles