8#include "libmythbase/mythconfig.h"
29#include "libavcodec/defs.h"
30#include "libavutil/opt.h"
33#define LOC QString("V4L2_M2M: ")
53 const AVCodec **Codec,
58 bool decodeonly =
Decoder ==
"v4l2-dec";
63 if (!
Decoder.startsWith(
"v4l2"))
91 if (!standard.contains(mythprofile))
94 if (!requests.contains(mythprofile))
105 if ((*Codec)->id == AV_CODEC_ID_H264)
107 if ((*Context)->profile == AV_PROFILE_H264_HIGH_10 ||
108 (*Context)->profile == AV_PROFILE_H264_HIGH_10_INTRA)
117 (*Context)->pix_fmt = AV_PIX_FMT_DRM_PRIME;
122 success, failure,
"v4l2m2m",
123 decodeonly ? (*Context)->pix_fmt : AV_PIX_FMT_DRM_PRIME);
158 DirectRendering =
false;
164 DirectRendering =
false;
193 if (!(Context && Codec))
196 if (!(Codec->priv_class && Context->priv_data))
202 LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"Setting number of capture buffers to 6");
203 av_opt_set_int(Context->priv_data,
"num_capture_buffers", 6, 0);
215 if (!Context || !AvFrame || !
Frame)
221 const auto * supported =
Frame->m_renderFormats;
222 auto found =
std::find(supported->cbegin(), supported->cend(),
type);
224 if (found == supported->end())
228 if ((
Frame->m_type !=
type) || (
Frame->m_width != AvFrame->width) || (
Frame->m_height != AvFrame->height))
234 for (
uint plane = 0; plane < count; ++plane)
237 AvFrame->data[plane], AvFrame->linesize[plane],
245#ifndef V4L2_PIX_FMT_HEVC
246#define V4L2_PIX_FMT_HEVC v4l2_fourcc('H', 'E', 'V', 'C')
249#ifndef V4L2_PIX_FMT_VP9
250#define V4L2_PIX_FMT_VP9 v4l2_fourcc('V', 'P', '9', '0')
253#ifndef V4L2_PIX_FMT_NV12_COL128
254#define V4L2_PIX_FMT_NV12_COL128 v4l2_fourcc('N', 'C', '1', '2')
257#ifndef V4L2_PIX_FMT_NV12_10_COL128
258#define V4L2_PIX_FMT_NV12_10_COL128 v4l2_fourcc('N', 'C', '3', '0')
263 static const std::vector<V4L2Mapping> s_map
276 static QRecursiveMutex lock;
277 static bool s_initialised =
false;
280 QMutexLocker locker(&lock);
283 s_initialised =
true;
289 static const std::vector<uint32_t> s_formats
291 V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV420M,
292 V4L2_PIX_FMT_YVU420M, V4L2_PIX_FMT_NV12, V4L2_PIX_FMT_NV12M,
299 const QString root(
"/dev/");
301 QStringList namefilters;
302 namefilters.append(
"video*");
303 auto devices = dir.entryList(namefilters, QDir::Files |QDir::System);
304 for (
const QString& device : std::as_const(devices))
308 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Device: %1 Driver: '%2' Capabilities: 0x%3")
313 bool mplanar = ((caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE)) != 0U) &&
314 ((caps & V4L2_CAP_STREAMING) != 0U);
315 bool mplanarm2m = (caps & V4L2_CAP_VIDEO_M2M_MPLANE) != 0U;
316 bool splanar = ((caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT)) != 0U) &&
317 ((caps & V4L2_CAP_STREAMING) != 0U);
318 bool splanarm2m = (caps & V4L2_CAP_VIDEO_M2M) != 0U;
320 if (!(mplanar || mplanarm2m || splanar || splanarm2m))
323 v4l2_buf_type capturetype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
324 v4l2_buf_type outputtype = V4L2_BUF_TYPE_VIDEO_OUTPUT;
326 if (mplanar || mplanarm2m)
328 capturetype = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
329 outputtype = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
335 for (
const auto &
profile : Profiles)
338 uint32_t v4l2pixfmt =
profile.first;
339 auto mythprofile =
profile.second;
340 struct v4l2_fmtdesc fdesc {};
341 memset(&fdesc, 0,
sizeof(fdesc));
344 fdesc.type = outputtype;
347 int res = ioctl(v4l2dev.
FD(), VIDIOC_ENUM_FMT, &fdesc);
350 if (fdesc.pixelformat == v4l2pixfmt)
357 QStringList pixformats;
358 bool foundfmt =
false;
360 memset(&fdesc, 0,
sizeof(fdesc));
361 fdesc.type = capturetype;
364 int res = ioctl(v4l2dev.
FD(), VIDIOC_ENUM_FMT, &fdesc);
367 pixformats.append(
fourcc_str(
static_cast<int>(fdesc.pixelformat)));
368 if (
std::find(s_formats.cbegin(), s_formats.cend(), fdesc.pixelformat) != s_formats.cend())
370 if (!result.contains(mythprofile))
371 result.append(mythprofile);
380 if (pixformats.isEmpty())
381 pixformats.append(
"None");
382 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"Codec '%1' has no supported formats (Supported: %2)")
395 if (!profiles.isEmpty())
398 Decoders.append(
"V4L2:");
404 if (!requests.isEmpty())
407 Decoders.append(
"V4L2 Request:");
416 static QRecursiveMutex lock;
417 QMutexLocker locker(&lock);
418 static bool s_checked =
false;
419 static bool s_available =
false;
421 if (s_checked && !Reinit)
427 if (standard.isEmpty() && request.isEmpty())
429 LOG(VB_GENERAL, LOG_INFO,
LOC +
"No V4L2 decoders found");
433 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Supported/available V4L2 decoders:");
436 for (
auto profile : std::as_const(standard))
438 for (
auto profile : std::as_const(request))
443#ifndef V4L2_PIX_FMT_MPEG2_SLICE
444#define V4L2_PIX_FMT_MPEG2_SLICE v4l2_fourcc('M', 'G', '2', 'S')
447#ifndef V4L2_PIX_FMT_H264_SLICE
448#define V4L2_PIX_FMT_H264_SLICE v4l2_fourcc('S', '2', '6', '4')
451#ifndef V4L2_PIX_FMT_VP8_FRAME
452#define V4L2_PIX_FMT_VP8_FRAME v4l2_fourcc('V', 'P', '8', 'F')
455#ifndef V4L2_PIX_FMT_VP9_FRAME
456#define V4L2_PIX_FMT_VP9_FRAME v4l2_fourcc('V', 'P', '9', 'F')
459#ifndef V4L2_PIX_FMT_HEVC_SLICE
460#define V4L2_PIX_FMT_HEVC_SLICE v4l2_fourcc('S', '2', '6', '5')
465 static const std::vector<V4L2Mapping> s_map
474 static QRecursiveMutex lock;
475 static bool s_initialised =
false;
478 QMutexLocker locker(&lock);
481 s_initialised =
true;
487 while (*PixFmt != AV_PIX_FMT_NONE)
489 if (*PixFmt == AV_PIX_FMT_DRM_PRIME)
492 "V4L2 request context creation") >= 0)
494 return AV_PIX_FMT_DRM_PRIME;
499 return AV_PIX_FMT_NONE;
513 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