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