23#include "config_components.h"
76#if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
77static int h2645_ps_to_nalu(
const uint8_t *
src,
int src_size, uint8_t **
out,
int *
out_size)
82 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
94 *
out_size =
sizeof(nalu_header) + src_size;
96 memcpy(p, nalu_header,
sizeof(nalu_header));
97 memcpy(p +
sizeof(nalu_header),
src, src_size);
130#if CONFIG_H264_MEDIACODEC_DECODER
140 int nal_length_size = 0;
143 &ps, &is_avc, &nal_length_size, 0, avctx);
168 if ((ret = h2645_ps_to_nalu(
sps->data,
sps->data_size, &
data, &data_size)) < 0) {
174 if ((ret = h2645_ps_to_nalu(
pps->data,
pps->data_size, &
data, &data_size)) < 0) {
180 const int warn = is_avc && (avctx->
codec_tag ==
MKTAG(
'a',
'v',
'c',
'1') ||
183 "Could not extract PPS/SPS from extradata\n");
194#if CONFIG_HEVC_MEDIACODEC_DECODER
207 int nal_length_size = 0;
209 uint8_t *vps_data =
NULL;
210 uint8_t *sps_data =
NULL;
211 uint8_t *pps_data =
NULL;
212 int vps_data_size = 0;
213 int sps_data_size = 0;
214 int pps_data_size = 0;
217 &ps, &
sei, &is_nalff, &nal_length_size, 0, 1, avctx);
246 avctx->
profile =
sps->ptl.general_ptl.profile_idc;
247 avctx->
level =
sps->ptl.general_ptl.level_idc;
249 if ((ret = h2645_ps_to_nalu(
vps->data,
vps->data_size, &vps_data, &vps_data_size)) < 0 ||
250 (ret = h2645_ps_to_nalu(
sps->data,
sps->data_size, &sps_data, &sps_data_size)) < 0 ||
251 (ret = h2645_ps_to_nalu(
pps->data,
pps->data_size, &pps_data, &pps_data_size)) < 0) {
255 data_size = vps_data_size + sps_data_size + pps_data_size;
262 memcpy(
data , vps_data, vps_data_size);
263 memcpy(
data + vps_data_size , sps_data, sps_data_size);
264 memcpy(
data + vps_data_size + sps_data_size, pps_data, pps_data_size);
270 const int warn = is_nalff && avctx->
codec_tag ==
MKTAG(
'h',
'v',
'c',
'1');
272 "Could not extract VPS/PPS/SPS from extradata\n");
287#if CONFIG_MPEG2_MEDIACODEC_DECODER || \
288 CONFIG_MPEG4_MEDIACODEC_DECODER || \
289 CONFIG_VP8_MEDIACODEC_DECODER || \
290 CONFIG_VP9_MEDIACODEC_DECODER || \
291 CONFIG_AV1_MEDIACODEC_DECODER || \
292 CONFIG_AAC_MEDIACODEC_DECODER || \
293 CONFIG_AMRNB_MEDIACODEC_DECODER || \
294 CONFIG_AMRWB_MEDIACODEC_DECODER || \
295 CONFIG_MP3_MEDIACODEC_DECODER
313 const char *codec_mime =
NULL;
318 if (
s->use_ndk_codec < 0)
329#if CONFIG_AV1_MEDIACODEC_DECODER
331 codec_mime =
"video/av01";
333 ret = common_set_extradata(avctx,
format);
338#if CONFIG_H264_MEDIACODEC_DECODER
340 codec_mime =
"video/avc";
342 ret = h264_set_extradata(avctx,
format);
347#if CONFIG_HEVC_MEDIACODEC_DECODER
349 codec_mime =
"video/hevc";
351 ret = hevc_set_extradata(avctx,
format);
356#if CONFIG_MPEG2_MEDIACODEC_DECODER
358 codec_mime =
"video/mpeg2";
360 ret = common_set_extradata(avctx,
format);
365#if CONFIG_MPEG4_MEDIACODEC_DECODER
367 codec_mime =
"video/mp4v-es",
369 ret = common_set_extradata(avctx,
format);
374#if CONFIG_VP8_MEDIACODEC_DECODER
376 codec_mime =
"video/x-vnd.on2.vp8";
378 ret = common_set_extradata(avctx,
format);
383#if CONFIG_VP9_MEDIACODEC_DECODER
385 codec_mime =
"video/x-vnd.on2.vp9";
387 ret = common_set_extradata(avctx,
format);
392#if CONFIG_AAC_MEDIACODEC_DECODER
394 codec_mime =
"audio/mp4a-latm";
396 ret = common_set_extradata(avctx,
format);
401#if CONFIG_AMRNB_MEDIACODEC_DECODER
403 codec_mime =
"audio/3gpp";
405 ret = common_set_extradata(avctx,
format);
410#if CONFIG_AMRWB_MEDIACODEC_DECODER
412 codec_mime =
"audio/amr-wb";
414 ret = common_set_extradata(avctx,
format);
419#if CONFIG_MP3_MEDIACODEC_DECODER
421 codec_mime =
"audio/mpeg";
423 ret = common_set_extradata(avctx,
format);
441 if (
s->operating_rate > 0)
451 s->ctx->delay_flush =
s->delay_flush;
452 s->ctx->use_ndk_codec =
s->use_ndk_codec;
460 "MediaCodec started successfully: codec = %s, ret = %d\n",
461 s->ctx->codec_name, ret);
470 if (sdk_int > 0 && sdk_int <= 23 &&
471 strcmp(
s->ctx->codec_name,
"OMX.amlogic.mpeg2.decoder.awesome") == 0) {
473 s->ctx->codec_name, sdk_int);
474 s->amlogic_mpeg2_api23_workaround = 1;
510 if (
s->ctx->current_input_buffer < 0 && !
s->ctx->draining) {
526 s->ctx->current_input_buffer =
index;
530 if (
s->buffered_pkt.size > 0) {
533 s->buffered_pkt.size -= ret;
534 s->buffered_pkt.data += ret;
535 if (
s->buffered_pkt.size <= 0) {
539 "could not send entire packet in single input buffer (%d < %d)\n",
540 ret,
s->buffered_pkt.size+ret);
542 }
else if (ret < 0 && ret !=
AVERROR(EAGAIN)) {
546 if (
s->amlogic_mpeg2_api23_workaround &&
s->buffered_pkt.size <= 0) {
562 }
else if (ret ==
AVERROR(EAGAIN) &&
s->ctx->current_input_buffer < 0) {
564 }
else if (ret < 0) {
594#define OFFSET(x) offsetof(MediaCodecContext, x)
595#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
597 {
"delay_flush",
"Delay flush until hw output buffers are returned to the decoder",
599 {
"ndk_codec",
"Use MediaCodec from NDK",
601 {
"operating_rate",
"The desired operating rate that the codec will need to operate at, zero for unspecified",
606#define DECLARE_MEDIACODEC_VCLASS(short_name) \
607static const AVClass ff_##short_name##_mediacodec_dec_class = { \
608 .class_name = #short_name "_mediacodec", \
609 .item_name = av_default_item_name, \
610 .option = ff_mediacodec_vdec_options, \
611 .version = LIBAVUTIL_VERSION_INT, \
614#define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
615DECLARE_MEDIACODEC_VCLASS(short_name) \
616const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
617 .p.name = #short_name "_mediacodec", \
618 CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
619 .p.type = AVMEDIA_TYPE_VIDEO, \
621 .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
622 .priv_data_size = sizeof(MediaCodecContext), \
623 .init = mediacodec_decode_init, \
624 FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
625 .flush = mediacodec_decode_flush, \
626 .close = mediacodec_decode_close, \
627 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
628 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
630 .hw_configs = mediacodec_hw_configs, \
631 .p.wrapper_name = "mediacodec", \
634#if CONFIG_H264_MEDIACODEC_DECODER
638#if CONFIG_HEVC_MEDIACODEC_DECODER
642#if CONFIG_MPEG2_MEDIACODEC_DECODER
646#if CONFIG_MPEG4_MEDIACODEC_DECODER
650#if CONFIG_VP8_MEDIACODEC_DECODER
654#if CONFIG_VP9_MEDIACODEC_DECODER
658#if CONFIG_AV1_MEDIACODEC_DECODER
662#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
664 {
"ndk_codec",
"Use MediaCodec from NDK",
666 {
"operating_rate",
"The desired operating rate that the codec will need to operate at, zero for unspecified",
671#define DECLARE_MEDIACODEC_ACLASS(short_name) \
672static const AVClass ff_##short_name##_mediacodec_dec_class = { \
673 .class_name = #short_name "_mediacodec", \
674 .item_name = av_default_item_name, \
675 .option = ff_mediacodec_adec_options, \
676 .version = LIBAVUTIL_VERSION_INT, \
679#define DECLARE_MEDIACODEC_ADEC(short_name, full_name, codec_id, bsf) \
680DECLARE_MEDIACODEC_ACLASS(short_name) \
681const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
682 .p.name = #short_name "_mediacodec", \
683 CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
684 .p.type = AVMEDIA_TYPE_AUDIO, \
686 .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
687 .priv_data_size = sizeof(MediaCodecContext), \
688 .init = mediacodec_decode_init, \
689 FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
690 .flush = mediacodec_decode_flush, \
691 .close = mediacodec_decode_close, \
692 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE, \
693 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
695 .p.wrapper_name = "mediacodec", \
698#if CONFIG_AAC_MEDIACODEC_DECODER
702#if CONFIG_AMRNB_MEDIACODEC_DECODER
706#if CONFIG_AMRWB_MEDIACODEC_DECODER
710#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)
@ 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