MythTV master
mythv4l2m2mcontext.cpp
Go to the documentation of this file.
1// Sys
2#include <sys/ioctl.h>
3
4// Qt
5#include <QDir>
6
7// MythTV
8#include "libmythbase/mythconfig.h"
12
13#include "avformatdecoder.h"
14#include "fourcc.h"
15#include "mythplayerui.h"
16#include "v4l2util.h"
17
18#if CONFIG_EGL
20#endif
22
23#if CONFIG_MMAL
25#endif
26
27// FFmpeg
28extern "C" {
29#include "libavutil/opt.h"
30}
31
32#define LOC QString("V4L2_M2M: ")
33
42 : MythDRMPRIMEContext(Parent, CodecID)
43{
44}
45
47{
49}
50
52 const AVCodec **Codec,
53 const QString &Decoder,
54 AVStream *Stream,
55 uint StreamType)
56{
57 bool decodeonly = Decoder == "v4l2-dec";
58 auto success = static_cast<MythCodecID>((decodeonly ? kCodec_MPEG1_V4L2_DEC : kCodec_MPEG1_V4L2) + (StreamType - 1));
59 auto failure = static_cast<MythCodecID>(kCodec_MPEG1 + (StreamType - 1));
60
61 // not us
62 if (!Decoder.startsWith("v4l2"))
63 return failure;
64
65 if (!decodeonly)
66 if (!FrameTypeIsSupported(*Context, FMT_DRMPRIME))
67 return failure;
68
69 // supported by device driver?
71 switch ((*Codec)->id)
72 {
73 case AV_CODEC_ID_MPEG1VIDEO: mythprofile = MythCodecContext::MPEG1; break;
74 case AV_CODEC_ID_MPEG2VIDEO: mythprofile = MythCodecContext::MPEG2; break;
75 case AV_CODEC_ID_MPEG4: mythprofile = MythCodecContext::MPEG4; break;
76 case AV_CODEC_ID_H263: mythprofile = MythCodecContext::H263; break;
77 case AV_CODEC_ID_H264: mythprofile = MythCodecContext::H264; break;
78 case AV_CODEC_ID_VC1: mythprofile = MythCodecContext::VC1; break;
79 case AV_CODEC_ID_VP8: mythprofile = MythCodecContext::VP8; break;
80 case AV_CODEC_ID_VP9: mythprofile = MythCodecContext::VP9; break;
81 case AV_CODEC_ID_HEVC: mythprofile = MythCodecContext::HEVC; break;
82 default: break;
83 }
84
85 if (mythprofile == MythCodecContext::NoProfile)
86 return failure;
87
88 bool request = false;
89 const auto & standard = MythV4L2M2MContext::GetStandardProfiles();
90 if (!standard.contains(mythprofile))
91 {
93 if (!requests.contains(mythprofile))
94 return failure;
95 request = true;
96 }
97
98#if CONFIG_MMAL
99 // If MMAL is available, assume this is a Raspberry Pi and check the supported
100 // video sizes
101 if (!MythMMALContext::CheckCodecSize((*Context)->width, (*Context)->height, mythprofile))
102 return failure;
103 // As for MMAL, don't try and decode 10bit H264
104 if ((*Codec)->id == AV_CODEC_ID_H264)
105 {
106 if ((*Context)->profile == FF_PROFILE_H264_HIGH_10 ||
107 (*Context)->profile == FF_PROFILE_H264_HIGH_10_INTRA)
108 {
109 return failure;
110 }
111 }
112#endif
113
114 if (request)
115 {
116 (*Context)->pix_fmt = AV_PIX_FMT_DRM_PRIME;
117 return success;
118 }
119
120 return MythDRMPRIMEContext::GetPrimeCodec(Context, Codec, Stream,
121 success, failure, "v4l2m2m",
122 decodeonly ? (*Context)->pix_fmt : AV_PIX_FMT_DRM_PRIME);
123}
124
125int MythV4L2M2MContext::HwDecoderInit(AVCodecContext *Context)
126{
127 if (!Context)
128 return -1;
129
131 return 0;
132
134}
135
136void MythV4L2M2MContext::InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering)
137{
138 // Fairly circular check of whether our codec id is using the request API.
139 // N.B. As for other areas of this class, this assumes there is no overlap
140 // between standard and request API codec support - though both can be used
141 // but for different codecs (as is expected on the Pi 4)
143 switch (m_codecID)
144 {
145 case kCodec_MPEG2_V4L2: profile = MPEG2; break;
146 case kCodec_H264_V4L2: profile = H264; break;
147 case kCodec_VP8_V4L2: profile = VP8; break;
148 case kCodec_VP9_V4L2: profile = VP9; break;
149 case kCodec_HEVC_V4L2: profile = HEVC; break;
150 default: break;
151 }
152
154
156 {
157 DirectRendering = false;
158 return;
159 }
160
162 {
163 DirectRendering = false; // Surely true ?? And then an issue for regular V4L2 as well
164 Context->get_format = MythV4L2M2MContext::GetV4L2RequestFormat;
165 return;
166 }
167
168 MythDRMPRIMEContext::InitVideoCodec(Context, SelectedStream, DirectRendering);
169}
170
171bool MythV4L2M2MContext::RetrieveFrame(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame)
172{
174 return GetBuffer(Context, Frame, AvFrame, 0);
175
176 if (m_request)
177 return MythDRMPRIMEContext::GetDRMBuffer(Context, Frame, AvFrame, 0);
178
179 return MythDRMPRIMEContext::RetrieveFrame(Context, Frame, AvFrame);
180}
181
187void MythV4L2M2MContext::SetDecoderOptions(AVCodecContext* Context, const AVCodec* Codec)
188{
189 if (m_request)
190 return;
191
192 if (!(Context && Codec))
193 return;
194
195 if (!(Codec->priv_class && Context->priv_data))
196 return;
197
198 // best guess currently - this matches the number of capture buffers to the
199 // number of output buffers - and hence to the number of video buffers for
200 // direct rendering
201 LOG(VB_PLAYBACK, LOG_INFO, LOC + "Setting number of capture buffers to 6");
202 av_opt_set_int(Context->priv_data, "num_capture_buffers", 6, 0);
203}
204
211bool MythV4L2M2MContext::GetBuffer(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame, int /*Flags*/)
212{
213 // Sanity checks
214 if (!Context || !AvFrame || !Frame)
215 return false;
216
217 // Ensure we can render this format
218 auto * decoder = static_cast<AvFormatDecoder*>(Context->opaque);
219 auto type = MythAVUtil::PixelFormatToFrameType(static_cast<AVPixelFormat>(AvFrame->format));
220 const auto * supported = Frame->m_renderFormats;
221 auto found = std::find(supported->cbegin(), supported->cend(), type);
222 // No fallback currently (unlikely)
223 if (found == supported->end())
224 return false;
225
226 // Re-allocate if necessary
227 if ((Frame->m_type != type) || (Frame->m_width != AvFrame->width) || (Frame->m_height != AvFrame->height))
228 if (!VideoBuffers::ReinitBuffer(Frame, type, decoder->GetVideoCodecID(), AvFrame->width, AvFrame->height))
229 return false;
230
231 // Copy data
232 uint count = MythVideoFrame::GetNumPlanes(Frame->m_type);
233 for (uint plane = 0; plane < count; ++plane)
234 {
235 MythVideoFrame::CopyPlane(Frame->m_buffer + Frame->m_offsets[plane],Frame->m_pitches[plane],
236 AvFrame->data[plane], AvFrame->linesize[plane],
237 MythVideoFrame::GetPitchForPlane(Frame->m_type, AvFrame->width, plane),
238 MythVideoFrame::GetHeightForPlane(Frame->m_type, AvFrame->height, plane));
239 }
240
241 return true;
242}
243
244#ifndef V4L2_PIX_FMT_HEVC
245#define V4L2_PIX_FMT_HEVC v4l2_fourcc('H', 'E', 'V', 'C')
246#endif
247
248#ifndef V4L2_PIX_FMT_VP9
249#define V4L2_PIX_FMT_VP9 v4l2_fourcc('V', 'P', '9', '0')
250#endif
251
252#ifndef V4L2_PIX_FMT_NV12_COL128
253#define V4L2_PIX_FMT_NV12_COL128 v4l2_fourcc('N', 'C', '1', '2')
254#endif
255
256#ifndef V4L2_PIX_FMT_NV12_10_COL128
257#define V4L2_PIX_FMT_NV12_10_COL128 v4l2_fourcc('N', 'C', '3', '0')
258#endif
259
261{
262 static const std::vector<V4L2Mapping> s_map
263 {{
264 { V4L2_PIX_FMT_MPEG1, MythCodecContext::MPEG1 },
265 { V4L2_PIX_FMT_MPEG2, MythCodecContext::MPEG2 },
266 { V4L2_PIX_FMT_MPEG4, MythCodecContext::MPEG4 },
267 { V4L2_PIX_FMT_H263, MythCodecContext::H263 },
268 { V4L2_PIX_FMT_H264, MythCodecContext::H264 },
269 { V4L2_PIX_FMT_VC1_ANNEX_G, MythCodecContext::VC1 },
270 { V4L2_PIX_FMT_VP8, MythCodecContext::VP8 },
273 }};
274
275 static QRecursiveMutex lock;
276 static bool s_initialised = false;
277 static V4L2Profiles s_profiles;
278
279 QMutexLocker locker(&lock);
280 if (!s_initialised)
281 s_profiles = GetProfiles(s_map);
282 s_initialised = true;
283 return s_profiles;
284}
285
286V4L2Profiles MythV4L2M2MContext::GetProfiles(const std::vector<V4L2Mapping>& Profiles)
287{
288 static const std::vector<uint32_t> s_formats
289 {
290 V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV420M,
291 V4L2_PIX_FMT_YVU420M, V4L2_PIX_FMT_NV12, V4L2_PIX_FMT_NV12M,
292 V4L2_PIX_FMT_NV21, V4L2_PIX_FMT_NV21M, V4L2_PIX_FMT_NV12_COL128,
294 };
295
296 V4L2Profiles result;
297
298 const QString root("/dev/");
299 QDir dir(root);
300 QStringList namefilters;
301 namefilters.append("video*");
302 auto devices = dir.entryList(namefilters, QDir::Files |QDir::System);
303 for (const QString& device : std::as_const(devices))
304 {
305 V4L2util v4l2dev(root + device);
306 uint32_t caps = v4l2dev.GetCapabilities();
307 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Device: %1 Driver: '%2' Capabilities: 0x%3")
308 .arg(v4l2dev.GetDeviceName(), v4l2dev.GetDriverName(), QString::number(caps, 16)));
309
310 // check capture and output support
311 // these mimic the device checks in v4l2_m2m.c
312 bool mplanar = ((caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE)) != 0U) &&
313 ((caps & V4L2_CAP_STREAMING) != 0U);
314 bool mplanarm2m = (caps & V4L2_CAP_VIDEO_M2M_MPLANE) != 0U;
315 bool splanar = ((caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT)) != 0U) &&
316 ((caps & V4L2_CAP_STREAMING) != 0U);
317 bool splanarm2m = (caps & V4L2_CAP_VIDEO_M2M) != 0U;
318
319 if (!(mplanar || mplanarm2m || splanar || splanarm2m))
320 continue;
321
322 v4l2_buf_type capturetype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323 v4l2_buf_type outputtype = V4L2_BUF_TYPE_VIDEO_OUTPUT;
324
325 if (mplanar || mplanarm2m)
326 {
327 capturetype = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
328 outputtype = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
329 }
330
331 // check codec support
332 QSize dummy{0, 0};
333
334 for (const auto & profile : Profiles)
335 {
336 bool found = false;
337 uint32_t v4l2pixfmt = profile.first;
338 auto mythprofile = profile.second;
339 struct v4l2_fmtdesc fdesc {};
340 memset(&fdesc, 0, sizeof(fdesc));
341
342 // check output first
343 fdesc.type = outputtype;
344 while (!found)
345 {
346 int res = ioctl(v4l2dev.FD(), VIDIOC_ENUM_FMT, &fdesc);
347 if (res)
348 break;
349 if (fdesc.pixelformat == v4l2pixfmt)
350 found = true;
351 fdesc.index++;
352 }
353
354 if (found)
355 {
356 QStringList pixformats;
357 bool foundfmt = false;
358 // check capture
359 memset(&fdesc, 0, sizeof(fdesc));
360 fdesc.type = capturetype;
361 while (true)
362 {
363 int res = ioctl(v4l2dev.FD(), VIDIOC_ENUM_FMT, &fdesc);
364 if (res)
365 break;
366 pixformats.append(fourcc_str(static_cast<int>(fdesc.pixelformat)));
367 if (std::find(s_formats.cbegin(), s_formats.cend(), fdesc.pixelformat) != s_formats.cend())
368 {
369 if (!result.contains(mythprofile))
370 result.append(mythprofile);
371 foundfmt = true;
372 break;
373 }
374 fdesc.index++;
375 }
376
377 if (!foundfmt)
378 {
379 if (pixformats.isEmpty())
380 pixformats.append("None");
381 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Codec '%1' has no supported formats (Supported: %2)")
382 .arg(MythCodecContext::GetProfileDescription(mythprofile, dummy), pixformats.join((","))));
383 }
384 }
385 }
386 }
387
388 return result;
389}
390
391void MythV4L2M2MContext::GetDecoderList(QStringList &Decoders)
392{
393 const auto & profiles = MythV4L2M2MContext::GetStandardProfiles();
394 if (!profiles.isEmpty())
395 {
396 QSize size(0, 0);
397 Decoders.append("V4L2:");
399 Decoders.append(MythCodecContext::GetProfileDescription(profile, size));
400 }
401
403 if (!requests.isEmpty())
404 {
405 QSize size(0, 0);
406 Decoders.append("V4L2 Request:");
408 Decoders.append(MythCodecContext::GetProfileDescription(profile, size));
409 }
410
411}
412
413bool MythV4L2M2MContext::HaveV4L2Codecs(bool Reinit /*=false*/)
414{
415 static QRecursiveMutex lock;
416 QMutexLocker locker(&lock);
417 static bool s_checked = false;
418 static bool s_available = false;
419
420 if (s_checked && !Reinit)
421 return s_available;
422 s_checked = true;
423
424 const auto & standard = MythV4L2M2MContext::GetStandardProfiles();
425 const auto & request = MythV4L2M2MContext::GetRequestProfiles();
426 if (standard.isEmpty() && request.isEmpty())
427 {
428 LOG(VB_GENERAL, LOG_INFO, LOC + "No V4L2 decoders found");
429 return s_available;
430 }
431
432 LOG(VB_GENERAL, LOG_INFO, LOC + "Supported/available V4L2 decoders:");
433 s_available = true;
434 QSize size {0, 0};
435 for (auto profile : std::as_const(standard))
436 LOG(VB_GENERAL, LOG_INFO, LOC + MythCodecContext::GetProfileDescription(profile, size));
437 for (auto profile : std::as_const(request))
438 LOG(VB_GENERAL, LOG_INFO, LOC + MythCodecContext::GetProfileDescription(profile, size) + "(Request)");
439 return s_available;
440}
441
442#ifndef V4L2_PIX_FMT_MPEG2_SLICE
443#define V4L2_PIX_FMT_MPEG2_SLICE v4l2_fourcc('M', 'G', '2', 'S')
444#endif
445
446#ifndef V4L2_PIX_FMT_H264_SLICE
447#define V4L2_PIX_FMT_H264_SLICE v4l2_fourcc('S', '2', '6', '4')
448#endif
449
450#ifndef V4L2_PIX_FMT_VP8_FRAME
451#define V4L2_PIX_FMT_VP8_FRAME v4l2_fourcc('V', 'P', '8', 'F')
452#endif
453
454#ifndef V4L2_PIX_FMT_VP9_FRAME
455#define V4L2_PIX_FMT_VP9_FRAME v4l2_fourcc('V', 'P', '9', 'F')
456#endif
457
458#ifndef V4L2_PIX_FMT_HEVC_SLICE
459#define V4L2_PIX_FMT_HEVC_SLICE v4l2_fourcc('S', '2', '6', '5')
460#endif
461
463{
464 static const std::vector<V4L2Mapping> s_map
465 {{
471 }};
472
473 static QRecursiveMutex lock;
474 static bool s_initialised = false;
475 static V4L2Profiles s_profiles;
476
477 QMutexLocker locker(&lock);
478 if (!s_initialised)
479 s_profiles = GetProfiles(s_map);
480 s_initialised = true;
481 return s_profiles;
482}
483
484AVPixelFormat MythV4L2M2MContext::GetV4L2RequestFormat(AVCodecContext *Context, const AVPixelFormat *PixFmt)
485{
486 while (*PixFmt != AV_PIX_FMT_NONE)
487 {
488 if (*PixFmt == AV_PIX_FMT_DRM_PRIME)
489 {
491 "V4L2 request context creation") >= 0)
492 {
493 return AV_PIX_FMT_DRM_PRIME;
494 }
495 }
496 PixFmt++;
497 }
498 return AV_PIX_FMT_NONE;
499}
500
502{
503 if (!Context || !gCoreContext->IsUIThread())
504 return -1;
505
506 // N.B. Interop support should already have been checked
507 // Create the device context
508 auto * hwdeviceref = MythCodecContext::CreateDevice(AV_HWDEVICE_TYPE_DRM, nullptr);
509 if (!hwdeviceref)
510 return -1;
511
512 Context->hw_device_ctx = hwdeviceref;
513 return 0;
514}
AVFrame AVFrame
A decoder for media files.
static VideoFrameType PixelFormatToFrameType(AVPixelFormat Fmt)
Definition: mythavutil.cpp:72
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)
MythCodecID m_codecID
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)
Definition: mythframe.h:213
static int GetPitchForPlane(VideoFrameType Type, int Width, uint Plane)
Definition: mythframe.h:303
static int GetHeightForPlane(VideoFrameType Type, int Height, uint Plane)
Definition: mythframe.h:257
static void CopyPlane(uint8_t *To, int ToPitch, const uint8_t *From, int FromPitch, int PlaneWidth, int PlaneHeight)
Definition: mythframe.cpp:188
QString GetDriverName(void) const
Definition: v4l2util.cpp:684
int FD(void) const
Definition: v4l2util.h:28
QString GetDeviceName(void) const
Definition: v4l2util.cpp:679
uint32_t GetCapabilities(void) const
Definition: v4l2util.cpp:674
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)
Definition: fourcc.h:26
unsigned int uint
Definition: freesurround.h:24
static bool codec_is_v4l2_dec(MythCodecID id)
Definition: mythcodecid.h:360
MythCodecID
Definition: mythcodecid.h:14
@ kCodec_MPEG1_V4L2
Definition: mythcodecid.h:216
@ kCodec_VP9_V4L2
Definition: mythcodecid.h:224
@ kCodec_MPEG1
Definition: mythcodecid.h:24
@ kCodec_H264_V4L2
Definition: mythcodecid.h:220
@ kCodec_HEVC_V4L2
Definition: mythcodecid.h:225
@ kCodec_MPEG1_V4L2_DEC
Definition: mythcodecid.h:232
@ kCodec_VP8_V4L2
Definition: mythcodecid.h:223
@ kCodec_MPEG2_V4L2
Definition: mythcodecid.h:217
static bool codec_is_v4l2(MythCodecID id)
Definition: mythcodecid.h:358
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
@ FMT_DRMPRIME
Definition: mythframe.h:63
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
#define V4L2_PIX_FMT_VP8_FRAME
#define V4L2_PIX_FMT_H264_SLICE
#define LOC
#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
#define V4L2_PIX_FMT_VP9
QList< MythCodecContext::CodecProfile > V4L2Profiles