mediacodec.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Android MediaCodec public API functions
  3. *
  4. * Copyright (c) 2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "libavutil/error.h"
  24. #include "mediacodec.h"
  25. #if CONFIG_MEDIACODEC
  26. #include <jni.h>
  27. #include "libavcodec/avcodec.h"
  28. #include "libavutil/mem.h"
  29. #include "ffjni.h"
  30. #include "mediacodecdec_common.h"
  31. #include "version.h"
  32. AVMediaCodecContext *av_mediacodec_alloc_context(void)
  33. {
  34. return av_mallocz(sizeof(AVMediaCodecContext));
  35. }
  36. int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
  37. {
  38. int ret = 0;
  39. JNIEnv *env = NULL;
  40. env = ff_jni_get_env(avctx);
  41. if (!env) {
  42. return AVERROR_EXTERNAL;
  43. }
  44. ctx->surface = (*env)->NewGlobalRef(env, surface);
  45. if (ctx->surface) {
  46. avctx->hwaccel_context = ctx;
  47. } else {
  48. av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
  49. ret = AVERROR_EXTERNAL;
  50. }
  51. return ret;
  52. }
  53. void av_mediacodec_default_free(AVCodecContext *avctx)
  54. {
  55. JNIEnv *env = NULL;
  56. AVMediaCodecContext *ctx = avctx->hwaccel_context;
  57. if (!ctx) {
  58. return;
  59. }
  60. env = ff_jni_get_env(avctx);
  61. if (!env) {
  62. return;
  63. }
  64. if (ctx->surface) {
  65. (*env)->DeleteGlobalRef(env, ctx->surface);
  66. ctx->surface = NULL;
  67. }
  68. av_freep(&avctx->hwaccel_context);
  69. }
  70. int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
  71. {
  72. MediaCodecDecContext *ctx = buffer->ctx;
  73. int released = atomic_fetch_add(&buffer->released, 1);
  74. if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
  75. atomic_fetch_sub(&ctx->hw_buffer_count, 1);
  76. av_log(ctx->avctx, AV_LOG_DEBUG,
  77. "Releasing output buffer %zd (%p) ts=%"PRId64" with render=%d [%d pending]\n",
  78. buffer->index, buffer, buffer->pts, render, atomic_load(&ctx->hw_buffer_count));
  79. return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
  80. }
  81. return 0;
  82. }
  83. int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
  84. {
  85. MediaCodecDecContext *ctx = buffer->ctx;
  86. int released = atomic_fetch_add(&buffer->released, 1);
  87. if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
  88. atomic_fetch_sub(&ctx->hw_buffer_count, 1);
  89. av_log(ctx->avctx, AV_LOG_DEBUG,
  90. "Rendering output buffer %zd (%p) ts=%"PRId64" with time=%"PRId64" [%d pending]\n",
  91. buffer->index, buffer, buffer->pts, time, atomic_load(&ctx->hw_buffer_count));
  92. return ff_AMediaCodec_releaseOutputBufferAtTime(ctx->codec, buffer->index, time);
  93. }
  94. return 0;
  95. }
  96. #else
  97. #include <stdlib.h>
  98. AVMediaCodecContext *av_mediacodec_alloc_context(void)
  99. {
  100. return NULL;
  101. }
  102. int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
  103. {
  104. return AVERROR(ENOSYS);
  105. }
  106. void av_mediacodec_default_free(AVCodecContext *avctx)
  107. {
  108. }
  109. int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
  110. {
  111. return AVERROR(ENOSYS);
  112. }
  113. int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
  114. {
  115. return AVERROR(ENOSYS);
  116. }
  117. #endif