static void mediacodec_buffer_release(void *opaque, uint8_t *data) { AVMediaCodecBuffer *buffer = opaque; MediaCodecDecContext *ctx = buffer->ctx; int released = avpriv_atomic_int_get(&buffer->released); if (!released) { ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0); } ff_mediacodec_dec_unref(ctx); av_freep(&buffer); }
static void mediacodec_buffer_release(void *opaque, uint8_t *data) { AVMediaCodecBuffer *buffer = opaque; MediaCodecDecContext *ctx = buffer->ctx; int released = atomic_load(&buffer->released); if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) { atomic_fetch_sub(&ctx->hw_buffer_count, 1); av_log(ctx->avctx, AV_LOG_DEBUG, "Releasing output buffer %zd (%p) ts=%"PRId64" on free() [%d pending]\n", buffer->index, buffer, buffer->pts, atomic_load(&ctx->hw_buffer_count)); ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0); } if (ctx->delay_flush) ff_mediacodec_dec_unref(ctx); av_freep(&buffer); }
static int mediacodec_wrap_buffer(AVCodecContext *avctx, MediaCodecDecContext *s, uint8_t *data, size_t size, ssize_t index, FFAMediaCodecBufferInfo *info, AVFrame *frame) { int ret = 0; int status = 0; frame->width = avctx->width; frame->height = avctx->height; frame->format = avctx->pix_fmt; /* MediaCodec buffers needs to be copied to our own refcounted buffers * because the flush command invalidates all input and output buffers. */ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n"); goto done; } /* Override frame->pkt_pts as ff_get_buffer will override its value based * on the last avpacket received which is not in sync with the frame: * * N avpackets can be pushed before 1 frame is actually returned * * 0-sized avpackets are pushed to flush remaining frames at EOS */ frame->pkt_pts = info->presentationTimeUs; av_log(avctx, AV_LOG_DEBUG, "Frame: width=%d stride=%d height=%d slice-height=%d " "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n" "destination linesizes=%d,%d,%d\n" , avctx->width, s->stride, avctx->height, s->slice_height, s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name, frame->linesize[0], frame->linesize[1], frame->linesize[2]); switch (s->color_format) { case COLOR_FormatYUV420Planar: ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame); break; case COLOR_FormatYUV420SemiPlanar: case COLOR_QCOM_FormatYUV420SemiPlanar: case COLOR_QCOM_FormatYUV420SemiPlanar32m: ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame); break; case COLOR_TI_FormatYUV420PackedSemiPlanar: case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced: ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame); break; case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka: ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame); break; default: av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n", s->color_format, s->color_format); ret = AVERROR(EINVAL); goto done; } ret = 0; done: status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0); if (status < 0) { av_log(NULL, AV_LOG_ERROR, "Failed to release output buffer\n"); ret = AVERROR_EXTERNAL; } return ret; }
int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s, AVFrame *frame, int *got_frame, AVPacket *pkt) { int ret; int offset = 0; int need_draining = 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 (s->flushing) { av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer " "until all output buffers have been released\n"); return AVERROR_EXTERNAL; } if (pkt->size == 0) { need_draining = 1; } if (s->draining && s->eos) { return 0; } while (offset < pkt->size || (need_draining && !s->draining)) { 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_draining) { int64_t pts = pkt->pts; uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec); if (s->surface) { pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000)); } av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n"); status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, 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->draining = 1; break; } else { int64_t pts = pkt->pts; size = FFMIN(pkt->size - offset, size); memcpy(data, pkt->data + offset, size); offset += size; if (s->surface && avctx->pkt_timebase.num && avctx->pkt_timebase.den) { pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000)); } status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status); return AVERROR_EXTERNAL; } } } if (need_draining || 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->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); 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; } } *got_frame = 1; s->dequeued_buffer_nb++; } 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_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; }
static int mediacodec_wrap_hw_buffer(AVCodecContext *avctx, MediaCodecDecContext *s, ssize_t index, FFAMediaCodecBufferInfo *info, AVFrame *frame) { int ret = 0; int status = 0; AVMediaCodecBuffer *buffer = NULL; frame->buf[0] = NULL; frame->width = avctx->width; frame->height = avctx->height; frame->format = avctx->pix_fmt; if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) { frame->pts = av_rescale_q(info->presentationTimeUs, av_make_q(1, 1000000), avctx->pkt_timebase); } else { frame->pts = info->presentationTimeUs; } #if FF_API_PKT_PTS FF_DISABLE_DEPRECATION_WARNINGS frame->pkt_pts = frame->pts; FF_ENABLE_DEPRECATION_WARNINGS #endif frame->pkt_dts = AV_NOPTS_VALUE; buffer = av_mallocz(sizeof(AVMediaCodecBuffer)); if (!buffer) { ret = AVERROR(ENOMEM); goto fail; } buffer->released = 0; frame->buf[0] = av_buffer_create(NULL, 0, mediacodec_buffer_release, buffer, AV_BUFFER_FLAG_READONLY); if (!frame->buf[0]) { ret = AVERROR(ENOMEM); goto fail; } buffer->ctx = s; ff_mediacodec_dec_ref(s); buffer->index = index; buffer->pts = info->presentationTimeUs; frame->data[3] = (uint8_t *)buffer; return 0; fail: av_freep(buffer); av_buffer_unref(&frame->buf[0]); status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0); if (status < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n"); ret = AVERROR_EXTERNAL; } 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); }