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.

124 lines
2.8KB

  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. #if CONFIG_H264_MEDIACODEC_HWACCEL
  24. #include <jni.h>
  25. #include "libavcodec/avcodec.h"
  26. #include "libavutil/atomic.h"
  27. #include "libavutil/mem.h"
  28. #include "ffjni.h"
  29. #include "mediacodec.h"
  30. #include "mediacodecdec.h"
  31. AVMediaCodecContext *av_mediacodec_alloc_context(void)
  32. {
  33. return av_mallocz(sizeof(AVMediaCodecContext));
  34. }
  35. int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
  36. {
  37. int ret = 0;
  38. JNIEnv *env = NULL;
  39. env = ff_jni_get_env(avctx);
  40. if (!env) {
  41. return AVERROR_EXTERNAL;
  42. }
  43. ctx->surface = (*env)->NewGlobalRef(env, surface);
  44. if (ctx->surface) {
  45. avctx->hwaccel_context = ctx;
  46. } else {
  47. av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
  48. ret = AVERROR_EXTERNAL;
  49. }
  50. return ret;
  51. }
  52. void av_mediacodec_default_free(AVCodecContext *avctx)
  53. {
  54. JNIEnv *env = NULL;
  55. AVMediaCodecContext *ctx = avctx->hwaccel_context;
  56. if (!ctx) {
  57. return;
  58. }
  59. env = ff_jni_get_env(avctx);
  60. if (!env) {
  61. return;
  62. }
  63. if (ctx->surface) {
  64. (*env)->DeleteGlobalRef(env, ctx->surface);
  65. ctx->surface = NULL;
  66. }
  67. av_freep(&avctx->hwaccel_context);
  68. }
  69. int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
  70. {
  71. MediaCodecDecContext *ctx = buffer->ctx;
  72. int released = avpriv_atomic_int_add_and_fetch(&buffer->released, 1);
  73. if (released == 1) {
  74. return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
  75. }
  76. return 0;
  77. }
  78. #else
  79. #include <stdlib.h>
  80. #include "mediacodec.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 0;
  88. }
  89. void av_mediacodec_default_free(AVCodecContext *avctx)
  90. {
  91. }
  92. int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
  93. {
  94. return 0;
  95. }
  96. #endif