Skip to content

Commit

Permalink
update for ffmpeg 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
youling257 committed May 21, 2022
1 parent f6c1728 commit a6685d8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 57 deletions.
4 changes: 1 addition & 3 deletions common.mk
Expand Up @@ -16,8 +16,6 @@

include $(CLEAR_VARS)

include external/$(AV_CODEC_LIB)/android/$(AV_CODEC_LIB).mk

# put the libraries to /vendor
LOCAL_PROPRIETARY_MODULE := true

Expand All @@ -44,7 +42,7 @@ endif
LOCAL_CFLAGS += -D__STDC_CONSTANT_MACROS=1 -D__STDINT_LIMITS=1

# Quiet some noise
LOCAL_CFLAGS += -Wno-deprecated-declarations -Wno-unused-parameter
LOCAL_CFLAGS += -Wno-deprecated-declarations -Wno-unused-parameter -Wno-reserved-user-defined-literal
LOCAL_CLANG_CFLAGS += -Wno-unknown-attributes

LOCAL_MODULE_TAGS := optional
1 change: 1 addition & 0 deletions omx/SoftFFmpegVideo.cpp
Expand Up @@ -100,6 +100,7 @@ void SoftFFmpegVideo::setDefaultCtx(AVCodecContext *avctx, const AVCodec *codec)
avctx->skip_idct = AVDISCARD_DEFAULT;
avctx->skip_loop_filter = AVDISCARD_DEFAULT;
avctx->error_concealment = 3;
avctx->thread_count = 0;

if (fast) avctx->flags2 |= AV_CODEC_FLAG2_FAST;
#ifdef CODEC_FLAG_EMU_EDGE
Expand Down
171 changes: 117 additions & 54 deletions omx/ffmpeg_hwaccel.c
Expand Up @@ -8,39 +8,76 @@
#include "libavutil/hwcontext.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#ifdef LIBAV_CONFIG_H
#include "avtools/avconv.h"
#else
#include "ffmpeg.h"
#endif
#include "fftools/ffmpeg.h"

/* BEGIN: Extracted from ffmpeg_opt.c */

const HWAccel hwaccels[] = {
#if HAVE_VDPAU_X11
{ "vdpau", vdpau_init, HWACCEL_VDPAU, AV_PIX_FMT_VDPAU },
#endif
#if CONFIG_VDA
{ "vda", videotoolbox_init, HWACCEL_VDA, AV_PIX_FMT_VDA },
#if CONFIG_VIDEOTOOLBOX
{ "videotoolbox", videotoolbox_init, HWACCEL_VIDEOTOOLBOX, AV_PIX_FMT_VIDEOTOOLBOX },
#endif
#if CONFIG_LIBMFX
{ "qsv", qsv_init, HWACCEL_QSV, AV_PIX_FMT_QSV },
#endif
#if CONFIG_VAAPI
#ifdef LIBAV_CONFIG_H
{ "vaapi", hwaccel_decode_init, HWACCEL_VAAPI, AV_PIX_FMT_VAAPI, AV_HWDEVICE_TYPE_VAAPI },
#else
{ "vaapi", vaapi_decode_init, HWACCEL_VAAPI, AV_PIX_FMT_VAAPI },
#endif
#endif
#if CONFIG_CUVID
{ "cuvid", cuvid_init, HWACCEL_CUVID, AV_PIX_FMT_CUDA },
{ "qsv", qsv_init, HWACCEL_QSV, AV_PIX_FMT_QSV },
#endif
{ 0, 0, HWACCEL_NONE, AV_PIX_FMT_NONE },
};

/* END: Extracted from ffmpeg_opt.c */

/* BEGIN: Extracted from ffmpeg_hw.c */
static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
{
InputStream *ist = avctx->opaque;
AVFrame *output = NULL;
enum AVPixelFormat output_format = ist->hwaccel_output_format;
int err;

if (input->format == output_format) {
// Nothing to do.
return 0;
}

output = av_frame_alloc();
if (!output)
return AVERROR(ENOMEM);

output->format = output_format;

err = av_hwframe_transfer_data(output, input, 0);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
"output frame: %d.\n", err);
goto fail;
}

err = av_frame_copy_props(output, input);
if (err < 0) {
av_frame_unref(output);
goto fail;
}

av_frame_unref(input);
av_frame_move_ref(input, output);
av_frame_free(&output);

return 0;

fail:
av_frame_free(&output);
return err;
}

int hwaccel_decode_init(AVCodecContext *avctx)
{
InputStream *ist = avctx->opaque;

ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;

return 0;
}

/* END: Extracted from ffmpeg_hw.c */

int ffmpeg_hwaccel_get_frame(AVCodecContext *avctx, AVFrame *frame)
{
int err;
Expand All @@ -61,60 +98,86 @@ int ffmpeg_hwaccel_get_frame(AVCodecContext *avctx, AVFrame *frame)

/* BEGIN: Extracted from ffmpeg.c */

static const HWAccel *get_hwaccel(enum AVPixelFormat pix_fmt)
{
int i;
for (i = 0; hwaccels[i].name; i++)
if (hwaccels[i].pix_fmt == pix_fmt)
return &hwaccels[i];
return NULL;
}

static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
{
InputStream *ist = s->opaque;
const enum AVPixelFormat *p;
int ret = -1;
int ret;

for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
ALOGD_IF(DEBUG_HWACCEL, "check %td pix_fmts=%d", p - pix_fmts, *p);
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
const HWAccel *hwaccel;
const AVCodecHWConfig *config = NULL;
int i;

if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
break;

hwaccel = get_hwaccel(*p);
if (!hwaccel ||
(ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
(ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
continue;
if (ist->hwaccel_id == HWACCEL_GENERIC ||
ist->hwaccel_id == HWACCEL_AUTO) {
for (i = 0;; i++) {
config = avcodec_get_hw_config(s->codec, i);
if (!config)
break;
if (!(config->methods &
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
continue;
if (config->pix_fmt == *p)
break;
}
}
if (config) {
if (config->device_type != ist->hwaccel_device_type) {
// Different hwaccel offered, ignore.
continue;
}

ret = hwaccel_decode_init(s);
if (ret < 0) {
if (ist->hwaccel_id == HWACCEL_GENERIC) {
av_log(NULL, AV_LOG_FATAL,
"%s hwaccel requested for input stream #%d:%d, "
"but cannot be initialized.\n",
av_hwdevice_get_type_name(config->device_type),
ist->file_index, ist->st->index);
return AV_PIX_FMT_NONE;
}
continue;
}
} else {
const HWAccel *hwaccel = NULL;
int i;
for (i = 0; hwaccels[i].name; i++) {
if (hwaccels[i].pix_fmt == *p) {
hwaccel = &hwaccels[i];
break;
}
}
if (!hwaccel) {
// No hwaccel supporting this pixfmt.
continue;
}
if (hwaccel->id != ist->hwaccel_id) {
// Does not match requested hwaccel.
continue;
}

ret = hwaccel->init(s);
if (ret < 0) {
if (ist->hwaccel_id == hwaccel->id) {
ret = hwaccel->init(s);
if (ret < 0) {
av_log(NULL, AV_LOG_FATAL,
"%s hwaccel requested for input stream #%d, "
"but cannot be initialized.\n", hwaccel->name, ist->file_index);
"%s hwaccel requested for input stream #%d:%d, "
"but cannot be initialized.\n", hwaccel->name,
ist->file_index, ist->st->index);
return AV_PIX_FMT_NONE;
}
continue;
}

if (ist->hw_frames_ctx) {
s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);
if (!s->hw_frames_ctx)
return AV_PIX_FMT_NONE;
}
ist->active_hwaccel_id = hwaccel->id;

ist->hwaccel_pix_fmt = *p;
s->thread_count = 1;
#ifdef LIBAV_CONFIG_H
ist->dec = s->codec;
ist->dec_ctx = avcodec_alloc_context3(ist->dec);
avcodec_copy_context(ist->dec_ctx, s);
ret = hw_device_setup_for_decode(ist);
s->hw_device_ctx = ist->dec_ctx->hw_device_ctx;
ALOGD_IF(DEBUG_HWACCEL, "hw_device_setup_for_decode: %d ctx=%p hw_device_ctx=%p pix_fmts=%d", ret, s, s->hw_device_ctx, *p);
#endif
break;
}

Expand Down

0 comments on commit a6685d8

Please sign in to comment.