int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, const char *mime, FFAMediaFormat *format) { int ret = 0; int status; s->first_buffer_at = av_gettime(); s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, avctx->width, avctx->height, avctx); if (!s->codec_name) { ret = AVERROR_EXTERNAL; goto fail; } av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name); s->codec = ff_AMediaCodec_createCodecByName(s->codec_name); if (!s->codec) { av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name); ret = AVERROR_EXTERNAL; goto fail; } status = ff_AMediaCodec_configure(s->codec, format, NULL, NULL, 0); if (status < 0) { char *desc = ff_AMediaFormat_toString(format); av_log(avctx, AV_LOG_ERROR, "Failed to configure codec (status = %d) with format %s\n", status, desc); av_freep(&desc); ret = AVERROR_EXTERNAL; goto fail; } status = ff_AMediaCodec_start(s->codec); if (status < 0) { char *desc = ff_AMediaFormat_toString(format); av_log(avctx, AV_LOG_ERROR, "Failed to start codec (status = %d) with format %s\n", status, desc); av_freep(&desc); ret = AVERROR_EXTERNAL; goto fail; } s->format = ff_AMediaCodec_getOutputFormat(s->codec); if (s->format) { if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to configure context\n"); goto fail; } } av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec); return 0; fail: av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec); ff_mediacodec_dec_close(avctx, s); return ret; }
int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, const char *mime, FFAMediaFormat *format) { int ret = 0; int status; int profile; enum AVPixelFormat pix_fmt; static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MEDIACODEC, AV_PIX_FMT_NONE, }; s->first_buffer_at = av_gettime(); s->refcount = 1; pix_fmt = ff_get_format(avctx, pix_fmts); if (pix_fmt == AV_PIX_FMT_MEDIACODEC) { AVMediaCodecContext *user_ctx = avctx->hwaccel_context; if (user_ctx && user_ctx->surface) { s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx); av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface); } } profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx); if (profile < 0) { av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile"); } s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx); if (!s->codec_name) { ret = AVERROR_EXTERNAL; goto fail; } av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name); s->codec = ff_AMediaCodec_createCodecByName(s->codec_name); if (!s->codec) { av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name); ret = AVERROR_EXTERNAL; goto fail; } status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0); if (status < 0) { char *desc = ff_AMediaFormat_toString(format); av_log(avctx, AV_LOG_ERROR, "Failed to configure codec (status = %d) with format %s\n", status, desc); av_freep(&desc); ret = AVERROR_EXTERNAL; goto fail; } status = ff_AMediaCodec_start(s->codec); if (status < 0) { char *desc = ff_AMediaFormat_toString(format); av_log(avctx, AV_LOG_ERROR, "Failed to start codec (status = %d) with format %s\n", status, desc); av_freep(&desc); ret = AVERROR_EXTERNAL; goto fail; } s->format = ff_AMediaCodec_getOutputFormat(s->codec); if (s->format) { if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to configure context\n"); goto fail; } } av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec); return 0; fail: av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec); ff_mediacodec_dec_close(avctx, s); return ret; }