2 #include <QtAndroidExtras>
3 #include <QAndroidJniEnvironment>
16 #include "libavutil/pixfmt.h"
17 #include "libavutil/hwcontext_mediacodec.h"
18 #include "libavcodec/mediacodec.h"
19 #include "libavcodec/avcodec.h"
22 #define LOC QString("MediaCodec: ")
25 #define MC_MPEG2_SIMPLE (0x0)
26 #define MC_MPEG2_MAIN (0x1)
27 #define MC_MPEG2_422 (0x2)
28 #define MC_MPEG2_SNR (0x3)
29 #define MC_MPEG2_SPATIAL (0x4)
30 #define MC_MPEG2_HIGH (0x5)
31 #define MC_MPEG4_SIMPLE (0x0001)
32 #define MC_MPEG4_SIMPLE_SCALEABLE (0x0002)
33 #define MC_MPEG4_CORE (0x0004)
34 #define MC_MPEG4_MAIN (0x0008)
35 #define MC_MPEG4_NBIT (0x0010)
36 #define MC_MPEG4_SCALEABLE_TEX (0x0020)
37 #define MC_MPEG4_SIMPLE_FACE (0x0040)
38 #define MC_MPEG4_SIMPLE_FBA (0x0080)
39 #define MC_MPEG4_BASIC_ANIMATED (0x0100)
40 #define MC_MPEG4_HYBRID (0x0200)
41 #define MC_MPEG4_ADV_REALTIME (0x0400)
42 #define MC_MPEG4_CORE_SCALEABLE (0x0800)
43 #define MC_MPEG4_ADV_CODING (0x1000)
44 #define MC_MPEG4_ADV_CORE (0x2000)
45 #define MC_MPEG4_ADV_SCALEABLE (0x4000)
46 #define MC_MPEG4_ADV_SIMPLE (0x8000)
47 #define MC_H264_BASELINE (0x00001)
48 #define MC_H264_MAIN (0x00002)
49 #define MC_H264_EXTENDED (0x00004)
50 #define MC_H264_HIGH (0x00008)
51 #define MC_H264_HIGH10 (0x00010)
52 #define MC_H264_HIGH422 (0x00020)
53 #define MC_H264_HIGH444 (0x00040)
54 #define MC_H264_CONST_BASELINE (0x10000)
55 #define MC_H264_CONST_HIGH (0x80000)
56 #define MC_HEVC_MAIN (0x0001)
57 #define MC_HEVC_MAIN10 (0x0002)
58 #define MC_HEVC_MAIN_STILL (0x0004)
59 #define MC_HEVC_MAIN10HDR10 (0x1000)
60 #define MC_HEVC_MMAIN10HDR10PLUS (0x2000)
61 #define MC_VP8_MAIN (0x0001)
62 #define MC_VP9_0 (0x0001)
63 #define MC_VP9_1 (0x0002)
64 #define MC_VP9_2 (0x0004)
65 #define MC_VP9_3 (0x0008)
66 #define MC_VP9_2HDR (0x1000)
67 #define MC_VP9_3HDR (0x2000)
68 #define MC_VP9_2HDRPLUS (0x4000)
69 #define MC_VP9_3HDRPLUS (0x8000)
181 QSize size(Context->width, Context->height);
185 if (!interop->GetSurface())
192 AVBufferRef *hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_MEDIACODEC);
193 AVHWDeviceContext *hwdevicectx =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
195 hwdevicectx->user_opaque = interop;
196 AVMediaCodecDeviceContext *hwctx =
reinterpret_cast<AVMediaCodecDeviceContext*
>(hwdevicectx->hwctx);
197 hwctx->surface = interop->GetSurface();
198 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
200 LOG(VB_GENERAL, LOG_ERR,
LOC +
"av_hwdevice_ctx_init failed");
201 av_buffer_unref(&hwdeviceref);
205 Context->hw_device_ctx = hwdeviceref;
206 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Created MediaCodec hardware device context");
216 bool decodeonly =
Decoder ==
"mediacodec-dec";
220 if (!
Decoder.startsWith(
"mediacodec"))
234 for (
auto profile : qAsConst(profiles))
236 if (
profile.first == mythprofile &&
237 profile.second.width() >= (*Context)->width &&
238 profile.second.height() >= (*Context)->height)
247 if (found && decoder)
249 QString decodername = QString((*Codec)->name) +
"_mediacodec";
250 if (decodername ==
"mpeg2video_mediacodec")
251 decodername =
"mpeg2_mediacodec";
252 AVCodec *newCodec = avcodec_find_decoder_by_name (decodername.toLocal8Bit());
256 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"HW device type '%1' supports decoding '%2' (%3)")
257 .
arg(av_hwdevice_get_type_name(AV_HWDEVICE_TYPE_MEDIACODEC)).
arg((*Codec)->name).arg(profilestr));
260 (*Context)->pix_fmt = AV_PIX_FMT_MEDIACODEC;
265 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"HW device type '%1' does not support decoding '%2' (%3)")
266 .
arg(av_hwdevice_get_type_name(AV_HWDEVICE_TYPE_MEDIACODEC)).
arg((*Codec)->name).arg(profilestr));
280 Context->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
281 DirectRendering =
false;
299 if (AvFrame->format != AV_PIX_FMT_MEDIACODEC)
308 while (*PixFmt != AV_PIX_FMT_NONE)
310 if (*PixFmt == AV_PIX_FMT_MEDIACODEC)
311 return AV_PIX_FMT_MEDIACODEC;
314 return AV_PIX_FMT_NONE;
327 Frame->m_deinterlaceInuse2x =
false;
328 Frame->m_interlaced = 0;
329 Frame->m_interlacedReverse =
false;
330 Frame->m_topFieldFirst =
false;
332 Frame->m_alreadyDeinterlaced =
true;
348 static QMutex lock(QMutex::Recursive);
349 static bool s_initialised =
false;
352 QMutexLocker locker(&lock);
355 s_initialised =
true;
357 static const QPair<QString,QPair<MythCodecContext::CodecProfile, QList<int> > > mimetypes[] =
386 QAndroidJniEnvironment env;
387 QAndroidJniObject list(
"android/media/MediaCodecList",
"(I)V", 0);
391 QAndroidJniObject qtcodecs = list.callObjectMethod(
"getCodecInfos",
"()[Landroid/media/MediaCodecInfo;");
392 if (!qtcodecs.isValid())
396 jobjectArray codecs = qtcodecs.object<jobjectArray>();
397 jsize codeccount = env->GetArrayLength(codecs);
398 for (jsize i = 0; i < codeccount; ++i)
400 QAndroidJniObject codec(env->GetObjectArrayElement(codecs, i));
401 if (!codec.isValid())
405 if (codec.callMethod<jboolean>(
"isEncoder"))
409 QString name = codec.callObjectMethod<jstring>(
"getName").
toString();
410 if (name.contains(
"OMX.google", Qt::CaseInsensitive))
414 QAndroidJniObject qttypes = codec.callObjectMethod(
"getSupportedTypes",
"()[Ljava/lang/String;");
415 jobjectArray
types = qttypes.object<jobjectArray>();
416 jsize typecount = env->GetArrayLength(
types);
417 for (jsize j = 0; j < typecount; ++j)
419 QAndroidJniObject
type(env->GetObjectArrayElement(
types, j));
424 QString typestr =
type.toString();
425 for (
auto mimetype : mimetypes)
427 if (mimetype.first != typestr)
431 QAndroidJniObject caps = codec.callObjectMethod(
"getCapabilitiesForType",
432 "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;",
433 type.object<jstring>());
438 QAndroidJniObject videocaps = caps.callObjectMethod(
"getVideoCapabilities",
439 "()Landroid/media/MediaCodecInfo$VideoCapabilities;");
440 QAndroidJniObject widthrange = videocaps.callObjectMethod(
"getSupportedWidths",
441 "()Landroid/util/Range;");
442 QAndroidJniObject heightrange = videocaps.callObjectMethod(
"getSupportedHeights",
443 "()Landroid/util/Range;");
445 QAndroidJniObject widthqt = widthrange.callObjectMethod(
"getUpper",
"()Ljava/lang/Comparable;");
446 QAndroidJniObject heightqt = heightrange.callObjectMethod(
"getUpper",
"()Ljava/lang/Comparable;");
447 int width = widthqt.callMethod<jint>(
"intValue",
"()I");
448 int height = heightqt.callMethod<jint>(
"intValue",
"()I");
451 QAndroidJniObject profiles = caps.getObjectField(
"profileLevels",
452 "[Landroid/media/MediaCodecInfo$CodecProfileLevel;");
453 jobjectArray profilearr = profiles.object<jobjectArray>();
454 jsize profilecount = env->GetArrayLength(profilearr);
455 if (profilecount < 1)
457 s_profiles.append(QPair<MythCodecContext::CodecProfile,QSize>(mimetype.second.first, QSize(width, height)));
461 for (jsize k = 0; k < profilecount; ++k)
463 jobject
profile = env->GetObjectArrayElement(profilearr, k);
464 jclass objclass = env->GetObjectClass(
profile);
465 jfieldID
id = env->GetFieldID(objclass,
"profile",
"I");
466 int value =
static_cast<int>(env->GetIntField(
profile,
id));
467 QList<int>& mcprofiles = mimetype.second.second;
468 auto sameprof = [value](
auto mcprofile) {
return value == mcprofile; };
469 if (std::any_of(mcprofiles.cbegin(), mcprofiles.cend(), sameprof))
472 s_profiles.append(QPair<MythCodecContext::CodecProfile,QSize>(
p, QSize(width, height)));
475 s_profiles.append(QPair<MythCodecContext::CodecProfile,QSize>(mimetype.second.first, QSize(width, height)));
487 if (profiles.isEmpty())
490 Decoders.append(
"MediaCodec:");
491 for (
auto profile : qAsConst(profiles))
497 static QMutex lock(QMutex::Recursive);
498 static bool s_initialised =
false;
499 static bool s_available =
false;
501 QMutexLocker locker(&lock);
502 if (!s_initialised || Reinit)
505 if (profiles.isEmpty())
507 LOG(VB_GENERAL, LOG_INFO,
LOC +
"No MediaCodec decoders found");
512 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Supported/available MediaCodec decoders:");
513 for (
auto profile : qAsConst(profiles))
515 LOG(VB_GENERAL, LOG_INFO,
LOC +
520 s_initialised =
true;