23#include "config_components.h"
77#if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
78static int h2645_ps_to_nalu(
const uint8_t *
src,
int src_size, uint8_t **
out,
int *
out_size)
83 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
95 *
out_size =
sizeof(nalu_header) + src_size;
97 memcpy(p, nalu_header,
sizeof(nalu_header));
98 memcpy(p +
sizeof(nalu_header),
src, src_size);
131#if CONFIG_H264_MEDIACODEC_DECODER
141 int nal_length_size = 0;
144 &ps, &is_avc, &nal_length_size, 0, avctx);
169 if ((ret = h2645_ps_to_nalu(
sps->data,
sps->data_size, &
data, &data_size)) < 0) {
175 if ((ret = h2645_ps_to_nalu(
pps->data,
pps->data_size, &
data, &data_size)) < 0) {
181 const int warn = is_avc && (avctx->
codec_tag ==
MKTAG(
'a',
'v',
'c',
'1') ||
184 "Could not extract PPS/SPS from extradata\n");
195#if CONFIG_HEVC_MEDIACODEC_DECODER
208 int nal_length_size = 0;
210 uint8_t *vps_data =
NULL;
211 uint8_t *sps_data =
NULL;
212 uint8_t *pps_data =
NULL;
213 int vps_data_size = 0;
214 int sps_data_size = 0;
215 int pps_data_size = 0;
218 &ps, &
sei, &is_nalff, &nal_length_size, 0, 1, avctx);
247 avctx->
profile =
sps->ptl.general_ptl.profile_idc;
248 avctx->
level =
sps->ptl.general_ptl.level_idc;
250 if ((ret = h2645_ps_to_nalu(
vps->data,
vps->data_size, &vps_data, &vps_data_size)) < 0 ||
251 (ret = h2645_ps_to_nalu(
sps->data,
sps->data_size, &sps_data, &sps_data_size)) < 0 ||
252 (ret = h2645_ps_to_nalu(
pps->data,
pps->data_size, &pps_data, &pps_data_size)) < 0) {
256 data_size = vps_data_size + sps_data_size + pps_data_size;
263 memcpy(
data , vps_data, vps_data_size);
264 memcpy(
data + vps_data_size , sps_data, sps_data_size);
265 memcpy(
data + vps_data_size + sps_data_size, pps_data, pps_data_size);
271 const int warn = is_nalff && avctx->
codec_tag ==
MKTAG(
'h',
'v',
'c',
'1');
273 "Could not extract VPS/PPS/SPS from extradata\n");
289#if CONFIG_MPEG2_MEDIACODEC_DECODER || \
290 CONFIG_MPEG4_MEDIACODEC_DECODER || \
291 CONFIG_VP8_MEDIACODEC_DECODER || \
292 CONFIG_VP9_MEDIACODEC_DECODER || \
293 CONFIG_AV1_MEDIACODEC_DECODER || \
294 CONFIG_AAC_MEDIACODEC_DECODER || \
295 CONFIG_AMRNB_MEDIACODEC_DECODER || \
296 CONFIG_AMRWB_MEDIACODEC_DECODER || \
297 CONFIG_MP3_MEDIACODEC_DECODER
315 const char *codec_mime =
NULL;
320 if (
s->use_ndk_codec < 0)
331#if CONFIG_AV1_MEDIACODEC_DECODER
333 codec_mime =
"video/av01";
335 ret = common_set_extradata(avctx,
format);
340#if CONFIG_H264_MEDIACODEC_DECODER
342 codec_mime =
"video/avc";
344 ret = h264_set_extradata(avctx,
format);
349#if CONFIG_HEVC_MEDIACODEC_DECODER
351 codec_mime =
"video/hevc";
353 ret = hevc_set_extradata(avctx,
format);
358#if CONFIG_MPEG2_MEDIACODEC_DECODER
360 codec_mime =
"video/mpeg2";
362 ret = common_set_extradata(avctx,
format);
367#if CONFIG_MPEG4_MEDIACODEC_DECODER
369 codec_mime =
"video/mp4v-es",
371 ret = common_set_extradata(avctx,
format);
376#if CONFIG_VP8_MEDIACODEC_DECODER
378 codec_mime =
"video/x-vnd.on2.vp8";
380 ret = common_set_extradata(avctx,
format);
385#if CONFIG_VP9_MEDIACODEC_DECODER
387 codec_mime =
"video/x-vnd.on2.vp9";
389 ret = common_set_extradata(avctx,
format);
394#if CONFIG_AAC_MEDIACODEC_DECODER
396 codec_mime =
"audio/mp4a-latm";
398 ret = common_set_extradata(avctx,
format);
403#if CONFIG_AMRNB_MEDIACODEC_DECODER
405 codec_mime =
"audio/3gpp";
407 ret = common_set_extradata(avctx,
format);
412#if CONFIG_AMRWB_MEDIACODEC_DECODER
414 codec_mime =
"audio/amr-wb";
416 ret = common_set_extradata(avctx,
format);
421#if CONFIG_MP3_MEDIACODEC_DECODER
423 codec_mime =
"audio/mpeg";
425 ret = common_set_extradata(avctx,
format);
443 if (
s->operating_rate > 0)
453 s->ctx->delay_flush =
s->delay_flush;
454 s->ctx->use_ndk_codec =
s->use_ndk_codec;
462 "MediaCodec started successfully: codec = %s, ret = %d\n",
463 s->ctx->codec_name, ret);
472 if (sdk_int > 0 && sdk_int <= 23 &&
473 strcmp(
s->ctx->codec_name,
"OMX.amlogic.mpeg2.decoder.awesome") == 0) {
475 s->ctx->codec_name, sdk_int);
476 s->amlogic_mpeg2_api23_workaround = 1;
512 if (
s->ctx->current_input_buffer < 0 && !
s->ctx->draining) {
528 s->ctx->current_input_buffer =
index;
532 if (
s->buffered_pkt.size > 0) {
535 s->buffered_pkt.size -= ret;
536 s->buffered_pkt.data += ret;
537 if (
s->buffered_pkt.size <= 0) {
541 "could not send entire packet in single input buffer (%d < %d)\n",
542 ret,
s->buffered_pkt.size+ret);
544 }
else if (ret < 0 && ret !=
AVERROR(EAGAIN)) {
548 if (
s->amlogic_mpeg2_api23_workaround &&
s->buffered_pkt.size <= 0) {
564 }
else if (ret ==
AVERROR(EAGAIN) &&
s->ctx->current_input_buffer < 0) {
566 }
else if (ret < 0) {
596#define OFFSET(x) offsetof(MediaCodecContext, x)
597#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
599 {
"delay_flush",
"Delay flush until hw output buffers are returned to the decoder",
601 {
"ndk_codec",
"Use MediaCodec from NDK",
603 {
"operating_rate",
"The desired operating rate that the codec will need to operate at, zero for unspecified",
608#define DECLARE_MEDIACODEC_VCLASS(short_name) \
609static const AVClass ff_##short_name##_mediacodec_dec_class = { \
610 .class_name = #short_name "_mediacodec", \
611 .item_name = av_default_item_name, \
612 .option = ff_mediacodec_vdec_options, \
613 .version = LIBAVUTIL_VERSION_INT, \
616#define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
617DECLARE_MEDIACODEC_VCLASS(short_name) \
618const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
619 .p.name = #short_name "_mediacodec", \
620 CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
621 .p.type = AVMEDIA_TYPE_VIDEO, \
623 .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
624 .priv_data_size = sizeof(MediaCodecContext), \
625 .init = mediacodec_decode_init, \
626 FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
627 .flush = mediacodec_decode_flush, \
628 .close = mediacodec_decode_close, \
629 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
630 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
632 .hw_configs = mediacodec_hw_configs, \
633 .p.wrapper_name = "mediacodec", \
636#if CONFIG_H264_MEDIACODEC_DECODER
640#if CONFIG_HEVC_MEDIACODEC_DECODER
644#if CONFIG_MPEG2_MEDIACODEC_DECODER
648#if CONFIG_MPEG4_MEDIACODEC_DECODER
652#if CONFIG_VP8_MEDIACODEC_DECODER
656#if CONFIG_VP9_MEDIACODEC_DECODER
660#if CONFIG_AV1_MEDIACODEC_DECODER
664#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
666 {
"ndk_codec",
"Use MediaCodec from NDK",
668 {
"operating_rate",
"The desired operating rate that the codec will need to operate at, zero for unspecified",
673#define DECLARE_MEDIACODEC_ACLASS(short_name) \
674static const AVClass ff_##short_name##_mediacodec_dec_class = { \
675 .class_name = #short_name "_mediacodec", \
676 .item_name = av_default_item_name, \
677 .option = ff_mediacodec_adec_options, \
678 .version = LIBAVUTIL_VERSION_INT, \
681#define DECLARE_MEDIACODEC_ADEC(short_name, full_name, codec_id, bsf) \
682DECLARE_MEDIACODEC_ACLASS(short_name) \
683const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
684 .p.name = #short_name "_mediacodec", \
685 CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
686 .p.type = AVMEDIA_TYPE_AUDIO, \
688 .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
689 .priv_data_size = sizeof(MediaCodecContext), \
690 .init = mediacodec_decode_init, \
691 FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
692 .flush = mediacodec_decode_flush, \
693 .close = mediacodec_decode_close, \
694 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE, \
695 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
697 .p.wrapper_name = "mediacodec", \
700#if CONFIG_AAC_MEDIACODEC_DECODER
704#if CONFIG_AMRNB_MEDIACODEC_DECODER
708#if CONFIG_AMRWB_MEDIACODEC_DECODER
712#if CONFIG_MP3_MEDIACODEC_DECODER
static const char *const format[]
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define i(width, name, range_min, range_max)
static int FUNC sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static int FUNC vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
common internal and external API header
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_EOF
End of file.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ff_h264_get_profile(const SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps, int *is_avc, int *nal_length_size, int err_recognition, void *logctx)
H.264 decoder/parser shared code.
void ff_h264_ps_uninit(H264ParamSets *ps)
Uninit H264 param sets structure.
H.264 parameter set handling.
int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps, HEVCSEI *sei, int *is_nalff, int *nal_length_size, int err_recognition, int apply_defdispwin, void *logctx)
void ff_hevc_ps_uninit(HEVCParamSets *ps)
static void ff_hevc_reset_sei(HEVCSEI *sei)
Reset SEI values that are stored on the Context.
@ AV_HWDEVICE_TYPE_MEDIACODEC
void * av_jni_get_java_vm(void *log_ctx)
common internal api header.
common internal API header
#define MKTAG(a, b, c, d)
Memory handling functions.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
int width
picture width / height.
AVChannelLayout ch_layout
Audio channel layout.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
enum AVMediaType codec_type
int level
Encoding level descriptor.
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
const PPS * pps_list[MAX_PPS_COUNT]
RefStruct references.
const SPS * sps_list[MAX_SPS_COUNT]
RefStruct references.
const HEVCVPS * vps_list[HEVC_MAX_VPS_COUNT]
RefStruct references.
const HEVCPPS * pps_list[HEVC_MAX_PPS_COUNT]
RefStruct references.
const HEVCSPS * sps_list[HEVC_MAX_SPS_COUNT]
RefStruct references.
MediaCodecDecContext * ctx
int amlogic_mpeg2_api23_workaround