int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s, AVFrame *frame, int *got_frame, AVPacket *pkt) { int ret; int offset = 0; int need_flushing = 0; uint8_t *data; ssize_t index; size_t size; FFAMediaCodec *codec = s->codec; FFAMediaCodecBufferInfo info = { 0 }; int status; int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US; int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US; if (pkt->size == 0) { need_flushing = 1; } if (s->flushing && need_flushing && s->queued_buffer_nb <= 0) { return 0; } while (offset < pkt->size || (need_flushing && !s->flushing)) { int size; index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us); if (ff_AMediaCodec_infoTryAgainLater(codec, index)) { break; } if (index < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index); return AVERROR_EXTERNAL; } data = ff_AMediaCodec_getInputBuffer(codec, index, &size); if (!data) { av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n"); return AVERROR_EXTERNAL; } if (need_flushing) { uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec); av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n"); status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pkt->pts, flags); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status); return AVERROR_EXTERNAL; } s->flushing = 1; break; } else { size = FFMIN(pkt->size - offset, size); memcpy(data, pkt->data + offset, size); offset += size; status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pkt->pts, 0); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status); return AVERROR_EXTERNAL; } s->queued_buffer_nb++; if (s->queued_buffer_nb > s->queued_buffer_max) s->queued_buffer_max = s->queued_buffer_nb; } } if (s->flushing) { /* If the codec is flushing, block for a fair amount of time to * ensure we got a frame */ output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US; } else if (s->dequeued_buffer_nb == 0) { /* If the codec hasn't produced any frames, do not block so we * can push data to it as fast as possible, and get the first * frame */ output_dequeue_timeout_us = 0; } index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us); if (index >= 0) { int ret; if (!s->first_buffer++) { av_log(avctx, AV_LOG_DEBUG, "Got first buffer after %fms\n", (av_gettime() - s->first_buffer_at) / 1000); } av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd" " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64 " flags=%" PRIu32 "\n", index, info.offset, info.size, info.presentationTimeUs, info.flags); data = ff_AMediaCodec_getOutputBuffer(codec, index, &size); if (!data) { av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n"); return AVERROR_EXTERNAL; } if ((ret = mediacodec_wrap_buffer(avctx, s, data, size, index, &info, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n"); return ret; } *got_frame = 1; s->queued_buffer_nb--; s->dequeued_buffer_nb++; } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) { char *format = NULL; if (s->format) { status = ff_AMediaFormat_delete(s->format); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format); } } s->format = ff_AMediaCodec_getOutputFormat(codec); if (!s->format) { av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n"); return AVERROR_EXTERNAL; } format = ff_AMediaFormat_toString(s->format); if (!format) { return AVERROR_EXTERNAL; } av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format); av_freep(&format); if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) { return ret; } } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) { ff_AMediaCodec_cleanOutputBuffers(codec); } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) { if (s->flushing) { av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms " "while flushing remaining frames, output will probably lack last %d frames\n", output_dequeue_timeout_us / 1000, s->queued_buffer_nb); } else { av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n"); } } else { av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index); return AVERROR_EXTERNAL; } return offset; }
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; }
static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s) { int width = 0; int height = 0; int32_t value = 0; char *format = NULL; if (!s->format) { av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n"); return AVERROR(EINVAL); } format = ff_AMediaFormat_toString(s->format); if (!format) { return AVERROR_EXTERNAL; } av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format); av_freep(&format); /* Mandatory fields */ if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) { format = ff_AMediaFormat_toString(s->format); av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format); av_freep(&format); return AVERROR_EXTERNAL; } s->width = value; if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) { format = ff_AMediaFormat_toString(s->format); av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format); av_freep(&format); return AVERROR_EXTERNAL; } s->height = value; if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) { format = ff_AMediaFormat_toString(s->format); av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format); av_freep(&format); return AVERROR_EXTERNAL; } s->stride = value > 0 ? value : s->width; if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) { format = ff_AMediaFormat_toString(s->format); av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format); av_freep(&format); return AVERROR_EXTERNAL; } s->slice_height = value > 0 ? value : s->height; if (strstr(s->codec_name, "OMX.Nvidia.")) { s->slice_height = FFALIGN(s->height, 16); } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) { s->slice_height = avctx->height; s->stride = avctx->width; } if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) { format = ff_AMediaFormat_toString(s->format); av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format); av_freep(&format); return AVERROR_EXTERNAL; } s->color_format = value; s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value); if (avctx->pix_fmt == AV_PIX_FMT_NONE) { av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n"); return AVERROR(EINVAL); } /* Optional fields */ if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value)) s->crop_top = value; if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value)) s->crop_bottom = value; if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value)) s->crop_left = value; if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value)) s->crop_right = value; width = s->crop_right + 1 - s->crop_left; height = s->crop_bottom + 1 - s->crop_top; av_log(avctx, AV_LOG_INFO, "Output crop parameters top=%d bottom=%d left=%d right=%d, " "resulting dimensions width=%d height=%d\n", s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, width, height); return ff_set_dimensions(avctx, width, height); }
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; }
int ff_mediacodec_dec_receive(AVCodecContext *avctx, MediaCodecDecContext *s, AVFrame *frame, bool wait) { int ret; uint8_t *data; ssize_t index; size_t size; FFAMediaCodec *codec = s->codec; FFAMediaCodecBufferInfo info = { 0 }; int status; int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US; if (s->draining && s->eos) { return AVERROR_EOF; } if (s->draining) { /* If the codec is flushing or need to be flushed, block for a fair * amount of time to ensure we got a frame */ output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US; } else if (s->output_buffer_count == 0 || !wait) { /* If the codec hasn't produced any frames, do not block so we * can push data to it as fast as possible, and get the first * frame */ output_dequeue_timeout_us = 0; } index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us); if (index >= 0) { av_log(avctx, AV_LOG_TRACE, "Got output buffer %zd" " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64 " flags=%" PRIu32 "\n", index, info.offset, info.size, info.presentationTimeUs, info.flags); if (info.flags & ff_AMediaCodec_getBufferFlagEndOfStream(codec)) { s->eos = 1; } if (info.size) { if (s->surface) { if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n"); return ret; } } else { data = ff_AMediaCodec_getOutputBuffer(codec, index, &size); if (!data) { av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n"); return AVERROR_EXTERNAL; } if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n"); return ret; } } s->output_buffer_count++; return 0; } else { status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n"); } } } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) { char *format = NULL; if (s->format) { status = ff_AMediaFormat_delete(s->format); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format); } } s->format = ff_AMediaCodec_getOutputFormat(codec); if (!s->format) { av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n"); return AVERROR_EXTERNAL; } format = ff_AMediaFormat_toString(s->format); if (!format) { return AVERROR_EXTERNAL; } av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format); av_freep(&format); if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) { return ret; } } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) { ff_AMediaCodec_cleanOutputBuffers(codec); } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) { if (s->draining) { av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms " "while draining remaining frames, output will probably lack frames\n", output_dequeue_timeout_us / 1000); } else { av_log(avctx, AV_LOG_TRACE, "No output buffer available, try again later\n"); } } else { av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index); return AVERROR_EXTERNAL; } return AVERROR(EAGAIN); }
static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s) { int ret = 0; int width = 0; int height = 0; char *format = NULL; if (!s->format) { av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n"); return AVERROR(EINVAL); } format = ff_AMediaFormat_toString(s->format); if (!format) { return AVERROR_EXTERNAL; } av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format); /* Mandatory fields */ AMEDIAFORMAT_GET_INT32(s->width, "width", 1); AMEDIAFORMAT_GET_INT32(s->height, "height", 1); AMEDIAFORMAT_GET_INT32(s->stride, "stride", 1); s->stride = s->stride > 0 ? s->stride : s->width; AMEDIAFORMAT_GET_INT32(s->slice_height, "slice-height", 1); s->slice_height = s->slice_height > 0 ? s->slice_height : s->height; if (strstr(s->codec_name, "OMX.Nvidia.")) { s->slice_height = FFALIGN(s->height, 16); } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) { s->slice_height = avctx->height; s->stride = avctx->width; } AMEDIAFORMAT_GET_INT32(s->color_format, "color-format", 1); avctx->pix_fmt = mcdec_map_color_format(avctx, s, s->color_format); if (avctx->pix_fmt == AV_PIX_FMT_NONE) { av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n"); ret = AVERROR(EINVAL); goto fail; } /* Optional fields */ AMEDIAFORMAT_GET_INT32(s->crop_top, "crop-top", 0); AMEDIAFORMAT_GET_INT32(s->crop_bottom, "crop-bottom", 0); AMEDIAFORMAT_GET_INT32(s->crop_left, "crop-left", 0); AMEDIAFORMAT_GET_INT32(s->crop_right, "crop-right", 0); width = s->crop_right + 1 - s->crop_left; height = s->crop_bottom + 1 - s->crop_top; AMEDIAFORMAT_GET_INT32(s->display_width, "display-width", 0); AMEDIAFORMAT_GET_INT32(s->display_height, "display-height", 0); if (s->display_width && s->display_height) { AVRational sar = av_div_q( (AVRational){ s->display_width, s->display_height }, (AVRational){ width, height }); ff_set_sar(avctx, sar); } av_log(avctx, AV_LOG_INFO, "Output crop parameters top=%d bottom=%d left=%d right=%d, " "resulting dimensions width=%d height=%d\n", s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, width, height); av_freep(&format); return ff_set_dimensions(avctx, width, height); fail: av_freep(&format); return ret; }