framehook.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Video processing hooks
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <errno.h>
  22. #include "config.h"
  23. #include "avformat.h"
  24. #include "framehook.h"
  25. #if HAVE_DLFCN_H
  26. #include <dlfcn.h>
  27. #endif
  28. typedef struct FrameHookEntry {
  29. struct FrameHookEntry *next;
  30. FrameHookConfigureFn Configure;
  31. FrameHookProcessFn Process;
  32. FrameHookReleaseFn Release;
  33. void *ctx;
  34. } FrameHookEntry;
  35. static FrameHookEntry *first_hook;
  36. /* Returns 0 on OK */
  37. int frame_hook_add(int argc, char *argv[])
  38. {
  39. void *loaded;
  40. FrameHookEntry *fhe, **fhep;
  41. if (argc < 1) {
  42. return ENOENT;
  43. }
  44. loaded = dlopen(argv[0], RTLD_NOW);
  45. if (!loaded) {
  46. av_log(NULL, AV_LOG_ERROR, "%s\n", dlerror());
  47. return -1;
  48. }
  49. fhe = av_mallocz(sizeof(*fhe));
  50. if (!fhe) {
  51. return AVERROR(ENOMEM);
  52. }
  53. fhe->Configure = dlsym(loaded, "Configure");
  54. fhe->Process = dlsym(loaded, "Process");
  55. fhe->Release = dlsym(loaded, "Release"); /* Optional */
  56. if (!fhe->Process) {
  57. av_log(NULL, AV_LOG_ERROR, "Failed to find Process entrypoint in %s\n", argv[0]);
  58. return AVERROR(ENOENT);
  59. }
  60. if (!fhe->Configure && argc > 1) {
  61. av_log(NULL, AV_LOG_ERROR, "Failed to find Configure entrypoint in %s\n", argv[0]);
  62. return AVERROR(ENOENT);
  63. }
  64. if (argc > 1 || fhe->Configure) {
  65. if (fhe->Configure(&fhe->ctx, argc, argv)) {
  66. av_log(NULL, AV_LOG_ERROR, "Failed to Configure %s\n", argv[0]);
  67. return AVERROR(EINVAL);
  68. }
  69. }
  70. for (fhep = &first_hook; *fhep; fhep = &((*fhep)->next)) {
  71. }
  72. *fhep = fhe;
  73. return 0;
  74. }
  75. void frame_hook_process(AVPicture *pict, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  76. {
  77. if (first_hook) {
  78. FrameHookEntry *fhe;
  79. for (fhe = first_hook; fhe; fhe = fhe->next) {
  80. fhe->Process(fhe->ctx, pict, pix_fmt, width, height, pts);
  81. }
  82. }
  83. }
  84. void frame_hook_release(void)
  85. {
  86. FrameHookEntry *fhe;
  87. FrameHookEntry *fhenext;
  88. for (fhe = first_hook; fhe; fhe = fhenext) {
  89. fhenext = fhe->next;
  90. if (fhe->Release)
  91. fhe->Release(fhe->ctx);
  92. av_free(fhe);
  93. }
  94. first_hook = NULL;
  95. }