You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.9KB

  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. return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
  76. }
  77. return 0;
  78. }
  79. #else
  80. #include <stdlib.h>
  81. AVMediaCodecContext *av_mediacodec_alloc_context(void)
  82. {
  83. return NULL;
  84. }
  85. int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
  86. {
  87. return AVERROR(ENOSYS);
  88. }
  89. void av_mediacodec_default_free(AVCodecContext *avctx)
  90. {
  91. }
  92. int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
  93. {
  94. return AVERROR(ENOSYS);
  95. }
  96. #endif