2016-01-21 10:29:39 +02:00
|
|
|
/*
|
2017-06-05 22:22:58 +02:00
|
|
|
* Android MediaCodec MPEG-2 / H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
|
2016-01-21 10:29:39 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#include <stdint.h>
|
2016-01-21 10:29:39 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2016-01-21 10:29:39 +02:00
|
|
|
#include "libavutil/common.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2016-10-12 14:44:08 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2016-01-21 10:29:39 +02:00
|
|
|
#include "libavutil/pixfmt.h"
|
2018-03-02 13:04:30 +02:00
|
|
|
#include "libavutil/internal.h"
|
2016-01-21 10:29:39 +02:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2017-12-29 03:33:14 +02:00
|
|
|
#include "decode.h"
|
2016-10-12 14:44:08 +02:00
|
|
|
#include "h264_parse.h"
|
2022-01-24 20:39:31 +02:00
|
|
|
#include "h264_ps.h"
|
2016-10-12 14:44:08 +02:00
|
|
|
#include "hevc_parse.h"
|
2020-04-13 17:33:14 +02:00
|
|
|
#include "hwconfig.h"
|
2016-01-21 10:29:39 +02:00
|
|
|
#include "internal.h"
|
2022-10-16 19:07:27 +02:00
|
|
|
#include "jni.h"
|
2016-01-21 10:29:39 +02:00
|
|
|
#include "mediacodec_wrapper.h"
|
2016-10-12 14:44:08 +02:00
|
|
|
#include "mediacodecdec_common.h"
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
typedef struct MediaCodecH264DecContext {
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-03-06 23:07:32 +02:00
|
|
|
AVClass *avclass;
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
MediaCodecDecContext *ctx;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2017-06-10 00:41:07 +02:00
|
|
|
AVPacket buffered_pkt;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-03-06 23:07:32 +02:00
|
|
|
int delay_flush;
|
2018-04-26 20:51:15 +02:00
|
|
|
int amlogic_mpeg2_api23_workaround;
|
2018-03-06 23:07:32 +02:00
|
|
|
|
2022-10-16 19:07:27 +02:00
|
|
|
int use_ndk_codec;
|
2016-10-12 14:44:08 +02:00
|
|
|
} MediaCodecH264DecContext;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
|
2016-01-21 10:29:39 +02:00
|
|
|
{
|
2016-10-12 14:44:08 +02:00
|
|
|
MediaCodecH264DecContext *s = avctx->priv_data;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ff_mediacodec_dec_close(avctx, s->ctx);
|
|
|
|
s->ctx = NULL;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2017-06-10 00:41:07 +02:00
|
|
|
av_packet_unref(&s->buffered_pkt);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
return 0;
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
|
|
|
|
static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
|
2016-03-11 18:21:04 +02:00
|
|
|
{
|
2016-10-12 14:44:08 +02:00
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
uint8_t *p = NULL;
|
|
|
|
static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (!out || !out_size) {
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
p = av_malloc(sizeof(nalu_header) + src_size);
|
|
|
|
if (!p) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
*out = p;
|
|
|
|
*out_size = sizeof(nalu_header) + src_size;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
memcpy(p, nalu_header, sizeof(nalu_header));
|
|
|
|
memcpy(p + sizeof(nalu_header), src, src_size);
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
/* Escape 0x00, 0x00, 0x0{0-3} pattern */
|
|
|
|
for (i = 4; i < *out_size; i++) {
|
|
|
|
if (i < *out_size - 3 &&
|
|
|
|
p[i + 0] == 0 &&
|
|
|
|
p[i + 1] == 0 &&
|
|
|
|
p[i + 2] <= 3) {
|
|
|
|
uint8_t *new;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
*out_size += 1;
|
|
|
|
new = av_realloc(*out, *out_size);
|
|
|
|
if (!new) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*out = p = new;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
i = i + 2;
|
|
|
|
memmove(p + i + 1, p + i, *out_size - (i + 1));
|
|
|
|
p[i] = 0x03;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(out);
|
|
|
|
*out_size = 0;
|
2016-03-11 18:21:04 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
return ret;
|
2016-03-11 18:21:04 +02:00
|
|
|
}
|
2016-10-07 12:55:50 +02:00
|
|
|
#endif
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_H264_MEDIACODEC_DECODER
|
|
|
|
static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
H264ParamSets ps;
|
|
|
|
const PPS *pps = NULL;
|
|
|
|
const SPS *sps = NULL;
|
|
|
|
int is_avc = 0;
|
|
|
|
int nal_length_size = 0;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
memset(&ps, 0, sizeof(ps));
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
|
|
|
|
&ps, &is_avc, &nal_length_size, 0, avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto done;
|
2016-03-11 18:21:04 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
for (i = 0; i < MAX_PPS_COUNT; i++) {
|
|
|
|
if (ps.pps_list[i]) {
|
2022-08-04 05:18:58 +02:00
|
|
|
pps = ps.pps_list[i];
|
2016-10-12 14:44:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-03-11 18:21:04 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (pps) {
|
|
|
|
if (ps.sps_list[pps->sps_id]) {
|
2022-08-04 05:18:58 +02:00
|
|
|
sps = ps.sps_list[pps->sps_id];
|
2016-10-12 14:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (pps && sps) {
|
|
|
|
uint8_t *data = NULL;
|
|
|
|
int data_size = 0;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2021-12-13 22:56:05 +02:00
|
|
|
avctx->profile = ff_h264_get_profile(sps);
|
|
|
|
avctx->level = sps->level_idc;
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
|
|
|
|
av_freep(&data);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
|
|
|
|
av_freep(&data);
|
|
|
|
} else {
|
2021-02-13 00:47:46 +02:00
|
|
|
const int warn = is_avc && (avctx->codec_tag == MKTAG('a','v','c','1') ||
|
|
|
|
avctx->codec_tag == MKTAG('a','v','c','2'));
|
|
|
|
av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
|
|
|
|
"Could not extract PPS/SPS from extradata\n");
|
|
|
|
ret = 0;
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2016-10-12 14:44:08 +02:00
|
|
|
ff_h264_ps_uninit(&ps);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-10-12 14:44:08 +02:00
|
|
|
#endif
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_HEVC_MEDIACODEC_DECODER
|
|
|
|
static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
|
2016-01-21 10:29:39 +02:00
|
|
|
{
|
2016-10-12 14:44:08 +02:00
|
|
|
int i;
|
|
|
|
int ret;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
HEVCParamSets ps;
|
2017-10-31 17:27:57 +02:00
|
|
|
HEVCSEI sei;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
const HEVCVPS *vps = NULL;
|
|
|
|
const HEVCPPS *pps = NULL;
|
|
|
|
const HEVCSPS *sps = NULL;
|
|
|
|
int is_nalff = 0;
|
|
|
|
int nal_length_size = 0;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
uint8_t *vps_data = NULL;
|
|
|
|
uint8_t *sps_data = NULL;
|
|
|
|
uint8_t *pps_data = NULL;
|
|
|
|
int vps_data_size = 0;
|
|
|
|
int sps_data_size = 0;
|
|
|
|
int pps_data_size = 0;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
memset(&ps, 0, sizeof(ps));
|
2017-04-30 18:05:52 +02:00
|
|
|
memset(&sei, 0, sizeof(sei));
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
|
2017-04-30 18:05:52 +02:00
|
|
|
&ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
goto done;
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:27:48 +02:00
|
|
|
for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ps.vps_list[i]) {
|
2022-08-04 15:39:00 +02:00
|
|
|
vps = ps.vps_list[i];
|
2016-10-12 14:44:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:27:48 +02:00
|
|
|
for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ps.pps_list[i]) {
|
2022-08-04 15:39:00 +02:00
|
|
|
pps = ps.pps_list[i];
|
2016-10-12 14:44:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (pps) {
|
|
|
|
if (ps.sps_list[pps->sps_id]) {
|
2022-08-04 15:39:00 +02:00
|
|
|
sps = ps.sps_list[pps->sps_id];
|
2016-10-12 14:44:08 +02:00
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (vps && pps && sps) {
|
|
|
|
uint8_t *data;
|
|
|
|
int data_size;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2021-12-13 22:56:05 +02:00
|
|
|
avctx->profile = sps->ptl.general_ptl.profile_idc;
|
|
|
|
avctx->level = sps->ptl.general_ptl.level_idc;
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
|
|
|
|
(ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
|
|
|
|
(ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
data_size = vps_data_size + sps_data_size + pps_data_size;
|
|
|
|
data = av_mallocz(data_size);
|
|
|
|
if (!data) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto done;
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
memcpy(data , vps_data, vps_data_size);
|
|
|
|
memcpy(data + vps_data_size , sps_data, sps_data_size);
|
|
|
|
memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
av_freep(&data);
|
|
|
|
} else {
|
2021-02-13 00:47:46 +02:00
|
|
|
const int warn = is_nalff && avctx->codec_tag == MKTAG('h','v','c','1');
|
|
|
|
av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
|
|
|
|
"Could not extract VPS/PPS/SPS from extradata\n");
|
|
|
|
ret = 0;
|
2016-10-12 14:44:08 +02:00
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
done:
|
2018-01-20 21:55:17 +02:00
|
|
|
ff_hevc_ps_uninit(&ps);
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
av_freep(&vps_data);
|
|
|
|
av_freep(&sps_data);
|
|
|
|
av_freep(&pps_data);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2018-03-02 11:36:08 +02:00
|
|
|
#if CONFIG_MPEG2_MEDIACODEC_DECODER || \
|
|
|
|
CONFIG_MPEG4_MEDIACODEC_DECODER || \
|
|
|
|
CONFIG_VP8_MEDIACODEC_DECODER || \
|
2022-12-19 14:45:40 +02:00
|
|
|
CONFIG_VP9_MEDIACODEC_DECODER || \
|
|
|
|
CONFIG_AV1_MEDIACODEC_DECODER
|
2018-03-02 11:36:08 +02:00
|
|
|
static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
|
2016-01-21 10:29:39 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (avctx->extradata) {
|
|
|
|
ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
|
|
|
|
}
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
int ret;
|
2018-04-26 20:51:15 +02:00
|
|
|
int sdk_int;
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
const char *codec_mime = NULL;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
FFAMediaFormat *format = NULL;
|
|
|
|
MediaCodecH264DecContext *s = avctx->priv_data;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2022-10-16 19:07:27 +02:00
|
|
|
if (s->use_ndk_codec < 0)
|
|
|
|
s->use_ndk_codec = !av_jni_get_java_vm(avctx);
|
|
|
|
|
|
|
|
format = ff_AMediaFormat_new(s->use_ndk_codec);
|
2016-10-12 14:44:08 +02:00
|
|
|
if (!format) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
|
2016-01-21 10:29:39 +02:00
|
|
|
ret = AVERROR_EXTERNAL;
|
2016-10-12 14:44:08 +02:00
|
|
|
goto done;
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
switch (avctx->codec_id) {
|
2022-12-19 14:45:40 +02:00
|
|
|
#if CONFIG_AV1_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_AV1:
|
|
|
|
codec_mime = "video/av01";
|
|
|
|
|
|
|
|
ret = common_set_extradata(avctx, format);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_H264_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_H264:
|
|
|
|
codec_mime = "video/avc";
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ret = h264_set_extradata(avctx, format);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_HEVC_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_HEVC:
|
|
|
|
codec_mime = "video/hevc";
|
|
|
|
|
|
|
|
ret = hevc_set_extradata(avctx, format);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
2017-06-05 22:22:58 +02:00
|
|
|
#if CONFIG_MPEG2_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_MPEG2VIDEO:
|
|
|
|
codec_mime = "video/mpeg2";
|
|
|
|
|
2018-03-02 11:36:08 +02:00
|
|
|
ret = common_set_extradata(avctx, format);
|
2017-06-05 22:22:58 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_MPEG4_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_MPEG4:
|
|
|
|
codec_mime = "video/mp4v-es",
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-03-02 11:36:08 +02:00
|
|
|
ret = common_set_extradata(avctx, format);
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_VP8_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_VP8:
|
|
|
|
codec_mime = "video/x-vnd.on2.vp8";
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-03-02 11:36:08 +02:00
|
|
|
ret = common_set_extradata(avctx, format);
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_VP9_MEDIACODEC_DECODER
|
|
|
|
case AV_CODEC_ID_VP9:
|
|
|
|
codec_mime = "video/x-vnd.on2.vp9";
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-03-02 11:36:08 +02:00
|
|
|
ret = common_set_extradata(avctx, format);
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
av_assert0(0);
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ff_AMediaFormat_setString(format, "mime", codec_mime);
|
|
|
|
ff_AMediaFormat_setInt32(format, "width", avctx->width);
|
|
|
|
ff_AMediaFormat_setInt32(format, "height", avctx->height);
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
s->ctx = av_mallocz(sizeof(*s->ctx));
|
|
|
|
if (!s->ctx) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto done;
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-03-06 23:07:32 +02:00
|
|
|
s->ctx->delay_flush = s->delay_flush;
|
2022-10-16 19:07:27 +02:00
|
|
|
s->ctx->use_ndk_codec = s->use_ndk_codec;
|
2018-03-06 23:07:32 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
|
|
|
|
s->ctx = NULL;
|
|
|
|
goto done;
|
2016-03-11 18:21:04 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 20:51:15 +02:00
|
|
|
av_log(avctx, AV_LOG_INFO,
|
|
|
|
"MediaCodec started successfully: codec = %s, ret = %d\n",
|
|
|
|
s->ctx->codec_name, ret);
|
|
|
|
|
|
|
|
sdk_int = ff_Build_SDK_INT(avctx);
|
2023-01-10 13:35:46 +02:00
|
|
|
/* ff_Build_SDK_INT can fail when target API < 24 and JVM isn't available.
|
|
|
|
* If we don't check sdk_int > 0, the workaround might be enabled by
|
|
|
|
* mistake.
|
|
|
|
* JVM is required to make the workaround works reliably. On the other hand,
|
|
|
|
* missing a workaround should not be a serious issue, we do as best we can.
|
|
|
|
*/
|
|
|
|
if (sdk_int > 0 && sdk_int <= 23 &&
|
2018-04-26 20:51:15 +02:00
|
|
|
strcmp(s->ctx->codec_name, "OMX.amlogic.mpeg2.decoder.awesome") == 0) {
|
|
|
|
av_log(avctx, AV_LOG_INFO, "Enabling workaround for %s on API=%d\n",
|
|
|
|
s->ctx->codec_name, sdk_int);
|
|
|
|
s->amlogic_mpeg2_api23_workaround = 1;
|
|
|
|
}
|
2016-10-12 14:44:08 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (format) {
|
|
|
|
ff_AMediaFormat_delete(format);
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
mediacodec_decode_close(avctx);
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2017-12-29 03:33:14 +02:00
|
|
|
static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
|
2016-10-12 14:44:08 +02:00
|
|
|
{
|
|
|
|
MediaCodecH264DecContext *s = avctx->priv_data;
|
|
|
|
int ret;
|
2018-04-24 21:40:38 +02:00
|
|
|
ssize_t index;
|
2016-10-12 14:44:08 +02:00
|
|
|
|
2018-04-24 04:21:19 +02:00
|
|
|
/* In delay_flush mode, wait until the user has released or rendered
|
|
|
|
all retained frames. */
|
|
|
|
if (s->delay_flush && ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
|
2016-10-12 14:44:08 +02:00
|
|
|
if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
|
2017-12-29 03:33:14 +02:00
|
|
|
return AVERROR(EAGAIN);
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:40:38 +02:00
|
|
|
/* poll for new frame */
|
|
|
|
ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, false);
|
2018-02-16 05:52:14 +02:00
|
|
|
if (ret != AVERROR(EAGAIN))
|
2017-12-29 03:33:14 +02:00
|
|
|
return ret;
|
|
|
|
|
2018-04-24 21:40:38 +02:00
|
|
|
/* feed decoder */
|
|
|
|
while (1) {
|
|
|
|
if (s->ctx->current_input_buffer < 0) {
|
|
|
|
/* poll for input space */
|
|
|
|
index = ff_AMediaCodec_dequeueInputBuffer(s->ctx->codec, 0);
|
|
|
|
if (index < 0) {
|
|
|
|
/* no space, block for an output frame to appear */
|
2022-10-19 04:38:41 +02:00
|
|
|
ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
|
|
|
|
/* Try again if both input port and output port return EAGAIN.
|
|
|
|
* If no data is consumed and no frame in output, it can make
|
|
|
|
* both avcodec_send_packet() and avcodec_receive_frame()
|
|
|
|
* return EAGAIN, which violate the design.
|
|
|
|
*/
|
|
|
|
if (ff_AMediaCodec_infoTryAgainLater(s->ctx->codec, index) &&
|
|
|
|
ret == AVERROR(EAGAIN))
|
|
|
|
continue;
|
|
|
|
return ret;
|
2018-04-24 21:40:38 +02:00
|
|
|
}
|
|
|
|
s->ctx->current_input_buffer = index;
|
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2018-04-24 21:40:38 +02:00
|
|
|
/* try to flush any buffered packet data */
|
|
|
|
if (s->buffered_pkt.size > 0) {
|
|
|
|
ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt, false);
|
|
|
|
if (ret >= 0) {
|
|
|
|
s->buffered_pkt.size -= ret;
|
|
|
|
s->buffered_pkt.data += ret;
|
2019-09-05 20:04:09 +02:00
|
|
|
if (s->buffered_pkt.size <= 0) {
|
2018-04-24 21:40:38 +02:00
|
|
|
av_packet_unref(&s->buffered_pkt);
|
2019-09-05 20:04:09 +02:00
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"could not send entire packet in single input buffer (%d < %d)\n",
|
|
|
|
ret, s->buffered_pkt.size+ret);
|
|
|
|
}
|
2018-04-24 21:40:38 +02:00
|
|
|
} else if (ret < 0 && ret != AVERROR(EAGAIN)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-26 20:51:15 +02:00
|
|
|
if (s->amlogic_mpeg2_api23_workaround && s->buffered_pkt.size <= 0) {
|
|
|
|
/* fallthrough to fetch next packet regardless of input buffer space */
|
|
|
|
} else {
|
|
|
|
/* poll for space again */
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-24 21:40:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch new packet or eof */
|
|
|
|
ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
AVPacket null_pkt = { 0 };
|
|
|
|
ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt, true);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2019-04-24 09:59:28 +02:00
|
|
|
return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
|
2018-04-24 21:40:38 +02:00
|
|
|
} else if (ret == AVERROR(EAGAIN) && s->ctx->current_input_buffer < 0) {
|
|
|
|
return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
|
|
|
|
} else if (ret < 0) {
|
2016-01-21 10:29:39 +02:00
|
|
|
return ret;
|
2018-04-24 21:40:38 +02:00
|
|
|
}
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
2017-06-10 00:41:07 +02:00
|
|
|
|
2018-04-24 21:40:38 +02:00
|
|
|
return AVERROR(EAGAIN);
|
2016-01-21 10:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
static void mediacodec_decode_flush(AVCodecContext *avctx)
|
2016-01-21 10:29:39 +02:00
|
|
|
{
|
2016-10-12 14:44:08 +02:00
|
|
|
MediaCodecH264DecContext *s = avctx->priv_data;
|
2016-01-21 10:29:39 +02:00
|
|
|
|
2017-06-10 00:41:07 +02:00
|
|
|
av_packet_unref(&s->buffered_pkt);
|
2016-03-11 18:21:04 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
ff_mediacodec_dec_flush(avctx, s->ctx);
|
2016-03-11 18:21:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 20:51:15 +02:00
|
|
|
static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = {
|
2017-10-26 01:18:40 +02:00
|
|
|
&(const AVCodecHWConfigInternal) {
|
|
|
|
.public = {
|
|
|
|
.pix_fmt = AV_PIX_FMT_MEDIACODEC,
|
2017-12-04 03:32:22 +02:00
|
|
|
.methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
|
|
|
|
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
|
|
|
|
.device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
|
2017-10-26 01:18:40 +02:00
|
|
|
},
|
|
|
|
.hwaccel = NULL,
|
|
|
|
},
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-06 23:07:32 +02:00
|
|
|
#define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
|
|
|
|
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption ff_mediacodec_vdec_options[] = {
|
|
|
|
{ "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
|
|
|
|
OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
|
2022-10-16 19:07:27 +02:00
|
|
|
{ "ndk_codec", "Use MediaCodec from NDK",
|
|
|
|
OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VD },
|
2018-03-06 23:07:32 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECLARE_MEDIACODEC_VCLASS(short_name) \
|
|
|
|
static const AVClass ff_##short_name##_mediacodec_dec_class = { \
|
|
|
|
.class_name = #short_name "_mediacodec", \
|
2024-01-19 14:33:28 +02:00
|
|
|
.item_name = av_default_item_name, \
|
2018-03-06 23:07:32 +02:00
|
|
|
.option = ff_mediacodec_vdec_options, \
|
|
|
|
.version = LIBAVUTIL_VERSION_INT, \
|
|
|
|
};
|
|
|
|
|
2018-03-02 13:06:19 +02:00
|
|
|
#define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
|
2018-03-06 23:07:32 +02:00
|
|
|
DECLARE_MEDIACODEC_VCLASS(short_name) \
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
|
|
|
|
.p.name = #short_name "_mediacodec", \
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO, \
|
|
|
|
.p.id = codec_id, \
|
|
|
|
.p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
|
2018-03-02 13:06:19 +02:00
|
|
|
.priv_data_size = sizeof(MediaCodecH264DecContext), \
|
|
|
|
.init = mediacodec_decode_init, \
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
|
2018-03-02 13:06:19 +02:00
|
|
|
.flush = mediacodec_decode_flush, \
|
|
|
|
.close = mediacodec_decode_close, \
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
|
2023-07-09 21:17:21 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
|
2018-03-02 13:06:19 +02:00
|
|
|
.bsfs = bsf, \
|
|
|
|
.hw_configs = mediacodec_hw_configs, \
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.wrapper_name = "mediacodec", \
|
2018-03-02 13:06:19 +02:00
|
|
|
}; \
|
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_H264_MEDIACODEC_DECODER
|
2018-03-02 13:06:19 +02:00
|
|
|
DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
|
2016-10-12 14:44:08 +02:00
|
|
|
#endif
|
2016-09-05 17:08:41 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_HEVC_MEDIACODEC_DECODER
|
2018-03-02 13:06:19 +02:00
|
|
|
DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
|
2017-06-05 22:22:58 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_MPEG2_MEDIACODEC_DECODER
|
2018-03-02 13:06:19 +02:00
|
|
|
DECLARE_MEDIACODEC_VDEC(mpeg2, "MPEG-2", AV_CODEC_ID_MPEG2VIDEO, NULL)
|
2016-10-12 14:44:08 +02:00
|
|
|
#endif
|
2016-10-04 16:02:59 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_MPEG4_MEDIACODEC_DECODER
|
2018-03-02 13:06:19 +02:00
|
|
|
DECLARE_MEDIACODEC_VDEC(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4, NULL)
|
2016-10-12 14:44:08 +02:00
|
|
|
#endif
|
2016-10-04 17:47:44 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_VP8_MEDIACODEC_DECODER
|
2018-03-02 13:06:19 +02:00
|
|
|
DECLARE_MEDIACODEC_VDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL)
|
2016-10-12 14:44:08 +02:00
|
|
|
#endif
|
2016-10-04 16:02:59 +02:00
|
|
|
|
2016-10-12 14:44:08 +02:00
|
|
|
#if CONFIG_VP9_MEDIACODEC_DECODER
|
2018-03-02 13:06:19 +02:00
|
|
|
DECLARE_MEDIACODEC_VDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL)
|
2016-10-12 14:44:08 +02:00
|
|
|
#endif
|
2022-12-19 14:45:40 +02:00
|
|
|
|
|
|
|
#if CONFIG_AV1_MEDIACODEC_DECODER
|
|
|
|
DECLARE_MEDIACODEC_VDEC(av1, "AV1", AV_CODEC_ID_AV1, NULL)
|
|
|
|
#endif
|