Skip to content

Commit

Permalink
MediaCodec: Updates
Browse files Browse the repository at this point in the history
- merge all functionality into MythMediaCodecContext and remove
MediaCodecContext
- if'def out MediaCodec only OpenGL code
  • Loading branch information
mark-kendall committed May 30, 2019
1 parent 853f521 commit 794ab2b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 186 deletions.
42 changes: 2 additions & 40 deletions mythtv/libs/libmythtv/decoders/avformatdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ using namespace std;
#endif

#ifdef USING_MEDIACODEC
#include "mediacodeccontext.h"
#include "mythmediacodeccontext.h"
extern "C" {
#include "libavcodec/jni.h"
Expand Down Expand Up @@ -1499,23 +1498,6 @@ static enum AVPixelFormat get_format_vaapi2(struct AVCodecContext */*avctx*/,
}
#endif

#ifdef USING_MEDIACODEC
static enum AVPixelFormat get_format_mediacodec(struct AVCodecContext */*avctx*/,
const enum AVPixelFormat *valid_fmts)
{
enum AVPixelFormat ret = AV_PIX_FMT_NONE;
while (*valid_fmts != AV_PIX_FMT_NONE) {
if (*valid_fmts == AV_PIX_FMT_MEDIACODEC)
{
ret = AV_PIX_FMT_MEDIACODEC;
break;
}
valid_fmts++;
}
return ret;
}
#endif

void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
bool selectedStream)
{
Expand Down Expand Up @@ -1600,10 +1582,7 @@ void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
#ifdef USING_MEDIACODEC
if (CODEC_IS_MEDIACODEC(codec1))
{
if (codec_is_mediacodec(m_video_codec_id))
enc->get_format = MythMediaCodecContext::GetFormat;
else
enc->get_format = get_format_mediacodec;
enc->get_format = MythMediaCodecContext::GetFormat;
enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
}
else
Expand Down Expand Up @@ -2566,31 +2545,14 @@ int AvFormatDecoder::ScanStreams(bool novideo)
mediacodecmcid = MythMediaCodecContext::GetBestSupportedCodec(enc, &codec,
dec, mpeg_version(enc->codec_id), pixfmt);

if (codec_is_mediacodec(mediacodecmcid))
if (codec_is_mediacodec(mediacodecmcid) || codec_is_mediacodec_dec(mediacodecmcid))
{
gCodecMap->freeCodecContext(m_ic->streams[selTrack]);
enc = gCodecMap->getCodecContext(m_ic->streams[selTrack], codec);
m_video_codec_id = mediacodecmcid;
foundgpudecoder = true;
}
}

if (!foundgpudecoder)
{
MythCodecID mediacodec_mcid;
AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
mediacodec_mcid = MediaCodecContext::GetBestSupportedCodec(
&codec, dec, mpeg_version(enc->codec_id),
pix_fmt);

if (codec_is_mediacodec_dec(mediacodec_mcid))
{
gCodecMap->freeCodecContext(m_ic->streams[selTrack]);
enc = gCodecMap->getCodecContext(m_ic->streams[selTrack], codec);
m_video_codec_id = mediacodec_mcid;
foundgpudecoder = true;
}
}
#endif // USING_MEDIACODEC
#ifdef USING_NVDEC
if (!foundgpudecoder)
Expand Down
5 changes: 2 additions & 3 deletions mythtv/libs/libmythtv/decoders/mythcodeccontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ MythCodecContext *MythCodecContext::createMythCodecContext(MythCodecID codec)
mctx = new MythVTBContext(codec);
#endif
#ifdef USING_MEDIACODEC
if (codec_is_mediacodec(codec))
mctx = new MythMediaCodecContext();
if (codec_is_mediacodec(codec) || codec_is_mediacodec_dec(codec))
mctx = new MythMediaCodecContext(codec);
#endif
// In case neither was defined
Q_UNUSED(codec);

if (!mctx)
Expand Down
2 changes: 0 additions & 2 deletions mythtv/libs/libmythtv/libmythtv.pro
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,6 @@ using_frontend {

using_mediacodec {
DEFINES += USING_MEDIACODEC
HEADERS += mediacodeccontext.h
SOURCES += mediacodeccontext.cpp
HEADERS += mythmediacodeccontext.h mythmediacodecinterop.h
SOURCES += mythmediacodeccontext.cpp mythmediacodecinterop.cpp
}
Expand Down
73 changes: 0 additions & 73 deletions mythtv/libs/libmythtv/mediacodeccontext.cpp

This file was deleted.

41 changes: 0 additions & 41 deletions mythtv/libs/libmythtv/mediacodeccontext.h

This file was deleted.

47 changes: 25 additions & 22 deletions mythtv/libs/libmythtv/mythmediacodeccontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,44 @@ MythCodecID MythMediaCodecContext::GetBestSupportedCodec(AVCodecContext*,
uint StreamType,
AVPixelFormat &PixFmt)
{
AVPixelFormat format = AV_PIX_FMT_NONE;
if (Decoder == "mediacodec")
{
QString decodername = QString((*Codec)->name) + "_mediacodec";
if (decodername == "mpeg2video_mediacodec")
decodername = "mpeg2_mediacodec";
AVCodec *newCodec = avcodec_find_decoder_by_name (decodername.toLocal8Bit());
if (newCodec)
{
*Codec = newCodec;
format = AV_PIX_FMT_MEDIACODEC;
}
}
bool decodeonly = Decoder == "mediacodec-dec";
MythCodecID success = static_cast<MythCodecID>((decodeonly ? kCodec_MPEG1_MEDIACODEC_DEC : kCodec_MPEG1_MEDIACODEC) + (StreamType - 1));
MythCodecID failure = static_cast<MythCodecID>(kCodec_MPEG1 + (StreamType - 1));

if ((Decoder != "mediacodec") && (Decoder != "mediacodec-dec"))
return failure;

if (format == AV_PIX_FMT_NONE)
QString decodername = QString((*Codec)->name) + "_mediacodec";
if (decodername == "mpeg2video_mediacodec")
decodername = "mpeg2_mediacodec";
AVCodec *newCodec = avcodec_find_decoder_by_name (decodername.toLocal8Bit());
if (newCodec)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("HW device type '%1' does not support decoding '%2'")
*Codec = newCodec;
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("HW device type '%1' supports decoding '%2'")
.arg(av_hwdevice_get_type_name(AV_HWDEVICE_TYPE_MEDIACODEC)).arg((*Codec)->name));
return (MythCodecID)(kCodec_MPEG1 + (StreamType - 1));
PixFmt = AV_PIX_FMT_MEDIACODEC;
return success;
}

LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("HW device type '%1' supports decoding '%2'")
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("HW device type '%1' does not support decoding '%2'")
.arg(av_hwdevice_get_type_name(AV_HWDEVICE_TYPE_MEDIACODEC)).arg((*Codec)->name));
PixFmt = AV_PIX_FMT_MEDIACODEC;
return (MythCodecID)(kCodec_MPEG1_MEDIACODEC + (StreamType - 1));
return failure;
}

MythMediaCodecContext::MythMediaCodecContext()
: MythCodecContext()
MythMediaCodecContext::MythMediaCodecContext(MythCodecID CodecID)
: MythCodecContext(),
m_codecID(CodecID)
{
}

int MythMediaCodecContext::HwDecoderInit(AVCodecContext *Context)
{
return MythHWContext::InitialiseDecoder2(Context, MythMediaCodecContext::InitialiseDecoder, "Create MediaCodec decoder");
if (codec_is_mediacodec_dec(m_codecID))
return 0;
else if (codec_is_mediacodec_dec(m_codecID))
return MythHWContext::InitialiseDecoder2(Context, MythMediaCodecContext::InitialiseDecoder, "Create MediaCodec decoder");
return -1;
}

AVPixelFormat MythMediaCodecContext::GetFormat(AVCodecContext*, const AVPixelFormat *PixFmt)
Expand Down
8 changes: 4 additions & 4 deletions mythtv/libs/libmythtv/mythmediacodeccontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class MythMediaCodecContext : public MythCodecContext
{
public:
// MythCodecContext
MythMediaCodecContext();
MythMediaCodecContext(MythCodecID CodecID);
int HwDecoderInit(AVCodecContext *Context) override;

static MythCodecID GetBestSupportedCodec(AVCodecContext *AvCtx,
static MythCodecID GetBestSupportedCodec(AVCodecContext*,
AVCodec **Codec,
const QString &Decoder,
uint StreamType,
AVPixelFormat &PixFmt);
static AVPixelFormat GetFormat (AVCodecContext *AvCtx,
const AVPixelFormat *PixFmt);
static AVPixelFormat GetFormat (AVCodecContext*, const AVPixelFormat *PixFmt);

private:
static int InitialiseDecoder (AVCodecContext *Context);
MythCodecID m_codecID;
};

#endif // MYTHMEDIACODECCONTEXT_H
2 changes: 2 additions & 0 deletions mythtv/libs/libmythtv/mythvideotexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class MythVideoTexture : public MythGLTexture
VideoFrameType m_frameFormat { FMT_NONE };
uint m_plane { 0 };
uint m_planeCount { 0 };
#ifdef USING_MEDIACODEC
QMatrix4x4 *m_transform { nullptr };
#endif
MythAVCopy *m_copyContext { nullptr };

protected:
Expand Down
5 changes: 4 additions & 1 deletion mythtv/libs/libmythtv/openglvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ bool OpenGLVideo::CreateVideoShader(VideoShaderType Type, MythDeintType Deint)
if ((Default == Type) || (!format_is_yuv(m_outputType)))
{
fragment = DefaultFragmentShader;
#ifdef USING_MEDIACODEC
if (FMT_MEDIACODEC == m_inputType)
vertex = MediaCodecVertexShader;
#endif
}
// no interlaced shaders yet (i.e. interlaced chroma upsampling - not deinterlacers)
else if ((Progressive == Type) || (Interlaced == Type) || (Deint == DEINT_NONE))
Expand Down Expand Up @@ -578,6 +580,7 @@ void OpenGLVideo::PrepareFrame(VideoFrame *Frame, bool TopFieldFirst, FrameScanT
SetupFrameFormat(newsourcetype, newtargettype, newsize, newtargettexture);
}

#ifdef USING_MEDIACODEC
// Set the texture transform for mediacodec
if (FMT_MEDIACODEC == m_inputType)
{
Expand All @@ -587,7 +590,7 @@ void OpenGLVideo::PrepareFrame(VideoFrame *Frame, bool TopFieldFirst, FrameScanT
m_shaders[Default]->setUniformValue("u_transform", *inputtextures[0]->m_transform);
}
}

#endif
// Enable deinterlacing for NVDEC and VTB
if (Frame && format_is_hwyuv(Frame->codec))
AddDeinterlacer(Frame, Scan, DEINT_SHADER | DEINT_CPU); // pickup shader or cpu prefs
Expand Down
2 changes: 2 additions & 0 deletions mythtv/libs/libmythtv/openglvideoshaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static const QString DefaultFragmentShader =
" gl_FragColor = vec4(color.rgb, 1.0);\n"
"}\n";

#ifdef USING_MEDIACODEC
static const QString MediaCodecVertexShader =
"attribute highp vec2 a_position;\n"
"attribute highp vec2 a_texcoord0;\n"
Expand All @@ -34,6 +35,7 @@ static const QString MediaCodecVertexShader =
" gl_Position = u_projection * vec4(a_position, 0.0, 1.0);\n"
" v_texcoord0 = (u_transform * vec4(a_texcoord0, 0.0, 1.0)).xy;\n"
"}\n";
#endif

static const QString SampleNV12 =
"highp vec3 sampleNV12(in sampler2D texture1, in sampler2D texture2, highp vec2 texcoord)\n"
Expand Down

0 comments on commit 794ab2b

Please sign in to comment.