3 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
4 #include <QtAndroidExtras>
5 #include <QAndroidJniEnvironment>
7 #include <QCoreApplication>
8 #include <QJniEnvironment>
10 #define QAndroidJniEnvironment QJniEnvironment
11 #define QAndroidJniObject QJniObject
26 #include "libavutil/pixfmt.h"
27 #include "libavutil/hwcontext_mediacodec.h"
28 #include "libavcodec/mediacodec.h"
29 #include "libavcodec/avcodec.h"
32 #define LOC QString("MediaCodec: ")
35 #define MC_MPEG2_SIMPLE (0x0)
36 #define MC_MPEG2_MAIN (0x1)
37 #define MC_MPEG2_422 (0x2)
38 #define MC_MPEG2_SNR (0x3)
39 #define MC_MPEG2_SPATIAL (0x4)
40 #define MC_MPEG2_HIGH (0x5)
41 #define MC_MPEG4_SIMPLE (0x0001)
42 #define MC_MPEG4_SIMPLE_SCALEABLE (0x0002)
43 #define MC_MPEG4_CORE (0x0004)
44 #define MC_MPEG4_MAIN (0x0008)
45 #define MC_MPEG4_NBIT (0x0010)
46 #define MC_MPEG4_SCALEABLE_TEX (0x0020)
47 #define MC_MPEG4_SIMPLE_FACE (0x0040)
48 #define MC_MPEG4_SIMPLE_FBA (0x0080)
49 #define MC_MPEG4_BASIC_ANIMATED (0x0100)
50 #define MC_MPEG4_HYBRID (0x0200)
51 #define MC_MPEG4_ADV_REALTIME (0x0400)
52 #define MC_MPEG4_CORE_SCALEABLE (0x0800)
53 #define MC_MPEG4_ADV_CODING (0x1000)
54 #define MC_MPEG4_ADV_CORE (0x2000)
55 #define MC_MPEG4_ADV_SCALEABLE (0x4000)
56 #define MC_MPEG4_ADV_SIMPLE (0x8000)
57 #define MC_H264_BASELINE (0x00001)
58 #define MC_H264_MAIN (0x00002)
59 #define MC_H264_EXTENDED (0x00004)
60 #define MC_H264_HIGH (0x00008)
61 #define MC_H264_HIGH10 (0x00010)
62 #define MC_H264_HIGH422 (0x00020)
63 #define MC_H264_HIGH444 (0x00040)
64 #define MC_H264_CONST_BASELINE (0x10000)
65 #define MC_H264_CONST_HIGH (0x80000)
66 #define MC_HEVC_MAIN (0x0001)
67 #define MC_HEVC_MAIN10 (0x0002)
68 #define MC_HEVC_MAIN_STILL (0x0004)
69 #define MC_HEVC_MAIN10HDR10 (0x1000)
70 #define MC_HEVC_MMAIN10HDR10PLUS (0x2000)
71 #define MC_VP8_MAIN (0x0001)
72 #define MC_VP9_0 (0x0001)
73 #define MC_VP9_1 (0x0002)
74 #define MC_VP9_2 (0x0004)
75 #define MC_VP9_3 (0x0008)
76 #define MC_VP9_2HDR (0x1000)
77 #define MC_VP9_3HDR (0x2000)
78 #define MC_VP9_2HDRPLUS (0x4000)
79 #define MC_VP9_3HDRPLUS (0x8000)
191 QSize size(Context->width, Context->height);
195 if (!interop->GetSurface())
202 AVBufferRef *hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_MEDIACODEC);
203 AVHWDeviceContext *hwdevicectx =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
205 hwdevicectx->user_opaque = interop;
206 AVMediaCodecDeviceContext *hwctx =
reinterpret_cast<AVMediaCodecDeviceContext*
>(hwdevicectx->hwctx);
207 hwctx->surface = interop->GetSurface();
208 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
210 LOG(VB_GENERAL, LOG_ERR,
LOC +
"av_hwdevice_ctx_init failed");
211 av_buffer_unref(&hwdeviceref);
215 Context->hw_device_ctx = hwdeviceref;
216 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Created MediaCodec hardware device context");
221 const AVCodec **Codec,
226 bool decodeonly =
Decoder ==
"mediacodec-dec";
230 if (!
Decoder.startsWith(
"mediacodec"))
244 for (
auto profile : std::as_const(profiles))
246 if (
profile.first == mythprofile &&
247 profile.second.width() >= (*Context)->width &&
248 profile.second.height() >= (*Context)->height)
257 if (found && decoder)
259 QString decodername = QString((*Codec)->name) +
"_mediacodec";
260 if (decodername ==
"mpeg2video_mediacodec")
261 decodername =
"mpeg2_mediacodec";
262 const AVCodec *newCodec = avcodec_find_decoder_by_name (decodername.toLocal8Bit());
266 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"HW device type '%1' supports decoding '%2' (%3)")
267 .arg(av_hwdevice_get_type_name(AV_HWDEVICE_TYPE_MEDIACODEC)).arg((*Codec)->name).arg(profilestr));
270 (*Context)->pix_fmt = AV_PIX_FMT_MEDIACODEC;
275 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"HW device type '%1' does not support decoding '%2' (%3)")
276 .arg(av_hwdevice_get_type_name(AV_HWDEVICE_TYPE_MEDIACODEC)).arg((*Codec)->name).arg(profilestr));
290 Context->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
291 DirectRendering =
false;
309 if (AvFrame->format != AV_PIX_FMT_MEDIACODEC)
318 while (*PixFmt != AV_PIX_FMT_NONE)
320 if (*PixFmt == AV_PIX_FMT_MEDIACODEC)
321 return AV_PIX_FMT_MEDIACODEC;
324 return AV_PIX_FMT_NONE;
337 Frame->m_deinterlaceInuse2x =
false;
338 Frame->m_interlaced =
false;
339 Frame->m_interlacedReverse =
false;
340 Frame->m_topFieldFirst =
false;
342 Frame->m_alreadyDeinterlaced =
true;
358 static QRecursiveMutex lock;
359 static bool s_initialised =
false;
362 QMutexLocker locker(&lock);
365 s_initialised =
true;
367 static const QPair<QString,QPair<MythCodecContext::CodecProfile, QList<int> > > mimetypes[] =
401 QAndroidJniObject qtcodecs = list.callObjectMethod(
"getCodecInfos",
"()[Landroid/media/MediaCodecInfo;");
402 if (!qtcodecs.isValid())
406 jobjectArray codecs = qtcodecs.object<jobjectArray>();
407 jsize codeccount = env->GetArrayLength(codecs);
408 for (jsize i = 0; i < codeccount; ++i)
411 if (!codec.isValid())
415 if (codec.callMethod<jboolean>(
"isEncoder"))
419 QString name = codec.callObjectMethod<jstring>(
"getName").
toString();
420 if (name.contains(
"OMX.google", Qt::CaseInsensitive))
424 QAndroidJniObject qttypes = codec.callObjectMethod(
"getSupportedTypes",
"()[Ljava/lang/String;");
425 jobjectArray
types = qttypes.object<jobjectArray>();
426 jsize typecount = env->GetArrayLength(
types);
427 for (jsize j = 0; j < typecount; ++j)
434 QString typestr =
type.toString();
435 for (
auto mimetype : mimetypes)
437 if (mimetype.first != typestr)
442 "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;",
443 type.object<jstring>());
449 "()Landroid/media/MediaCodecInfo$VideoCapabilities;");
451 "()Landroid/util/Range;");
452 QAndroidJniObject heightrange = videocaps.callObjectMethod(
"getSupportedHeights",
453 "()Landroid/util/Range;");
455 QAndroidJniObject widthqt = widthrange.callObjectMethod(
"getUpper",
"()Ljava/lang/Comparable;");
456 QAndroidJniObject heightqt = heightrange.callObjectMethod(
"getUpper",
"()Ljava/lang/Comparable;");
457 int width = widthqt.callMethod<jint>(
"intValue",
"()I");
458 int height = heightqt.callMethod<jint>(
"intValue",
"()I");
462 "[Landroid/media/MediaCodecInfo$CodecProfileLevel;");
463 jobjectArray profilearr = profiles.object<jobjectArray>();
464 jsize profilecount = env->GetArrayLength(profilearr);
465 if (profilecount < 1)
467 s_profiles.append(QPair<MythCodecContext::CodecProfile,QSize>(mimetype.second.first, QSize(width, height)));
471 for (jsize k = 0; k < profilecount; ++k)
473 jobject
profile = env->GetObjectArrayElement(profilearr, k);
474 jclass objclass = env->GetObjectClass(
profile);
475 jfieldID
id = env->GetFieldID(objclass,
"profile",
"I");
476 int value =
static_cast<int>(env->GetIntField(
profile,
id));
477 QList<int>& mcprofiles = mimetype.second.second;
478 auto sameprof = [value](
auto mcprofile) {
return value == mcprofile; };
479 if (std::any_of(mcprofiles.cbegin(), mcprofiles.cend(), sameprof))
482 s_profiles.append(QPair<MythCodecContext::CodecProfile,QSize>(
p, QSize(width, height)));
485 s_profiles.append(QPair<MythCodecContext::CodecProfile,QSize>(mimetype.second.first, QSize(width, height)));
497 if (profiles.isEmpty())
500 Decoders.append(
"MediaCodec:");
501 for (
auto profile : std::as_const(profiles))
507 static QRecursiveMutex lock;
508 static bool s_initialised =
false;
509 static bool s_available =
false;
511 QMutexLocker locker(&lock);
512 if (!s_initialised || Reinit)
515 if (profiles.isEmpty())
517 LOG(VB_GENERAL, LOG_INFO,
LOC +
"No MediaCodec decoders found");
522 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Supported/available MediaCodec decoders:");
523 for (
auto profile : std::as_const(profiles))
525 LOG(VB_GENERAL, LOG_INFO,
LOC +
530 s_initialised =
true;