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.

136 lines
4.6KB

  1. /*
  2. * Null Video Hook
  3. * Copyright (c) 2002 Philip Gladstone
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <stdio.h>
  20. #include "framehook.h"
  21. #include "swscale.h"
  22. static int sws_flags = SWS_BICUBIC;
  23. typedef struct {
  24. int dummy;
  25. // This vhook first converts frame to RGB ...
  26. struct SwsContext *toRGB_convert_ctx;
  27. // ... and later converts back frame from RGB to initial format
  28. struct SwsContext *fromRGB_convert_ctx;
  29. enum PixelFormat sws_pix_fmt; // Sws_Context is opaque, we need to save
  30. int sws_width, sws_height; // this to check if we can re-use contexts
  31. // Contexts will be re-used 99% of time,
  32. // I know of width/height changes only in DVB broadcasts
  33. } ContextInfo;
  34. void Release(void *ctx)
  35. {
  36. ContextInfo *ci;
  37. ci = (ContextInfo *) ctx;
  38. if (ctx) {
  39. sws_freeContext(ci->toRGB_convert_ctx);
  40. sws_freeContext(ci->fromRGB_convert_ctx);
  41. av_free(ctx);
  42. }
  43. }
  44. int Configure(void **ctxp, int argc, char *argv[])
  45. {
  46. fprintf(stderr, "Called with argc=%d\n", argc);
  47. *ctxp = av_mallocz(sizeof(ContextInfo));
  48. return 0;
  49. }
  50. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  51. {
  52. ContextInfo *ci = (ContextInfo *) ctx;
  53. char *buf = 0;
  54. AVPicture picture1;
  55. AVPicture *pict = picture;
  56. (void) ci;
  57. if (pix_fmt != PIX_FMT_RGB24) {
  58. int size;
  59. size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  60. buf = av_malloc(size);
  61. avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
  62. // if we already got a SWS context, let's realloc if is not re-useable
  63. if (ci->toRGB_convert_ctx != NULL) {
  64. if ((ci->sws_pix_fmt != pix_fmt) ||
  65. (ci->sws_width != width) || (ci->sws_height != height)) {
  66. sws_freeContext(ci->toRGB_convert_ctx);
  67. ci->toRGB_convert_ctx = NULL;
  68. sws_freeContext(ci->fromRGB_convert_ctx);
  69. ci->fromRGB_convert_ctx = NULL;
  70. }
  71. }
  72. if (ci->toRGB_convert_ctx == NULL) {
  73. ci->sws_pix_fmt = pix_fmt;
  74. ci->sws_width = width;
  75. ci->sws_height = height;
  76. ci->toRGB_convert_ctx = sws_getContext(
  77. ci->sws_width, ci->sws_height,
  78. ci->sws_pix_fmt,
  79. ci->sws_width, ci->sws_height,
  80. PIX_FMT_RGB24,
  81. sws_flags, NULL, NULL, NULL);
  82. if (ci->toRGB_convert_ctx == NULL) {
  83. av_log(NULL, AV_LOG_ERROR,
  84. "Cannot initialize the toRGB conversion context\n");
  85. exit(1);
  86. }
  87. ci->fromRGB_convert_ctx = sws_getContext(
  88. ci->sws_width, ci->sws_height,
  89. PIX_FMT_RGB24,
  90. ci->sws_width, ci->sws_height,
  91. ci->sws_pix_fmt,
  92. sws_flags, NULL, NULL, NULL);
  93. if (ci->fromRGB_convert_ctx == NULL) {
  94. av_log(NULL, AV_LOG_ERROR,
  95. "Cannot initialize the fromRGB conversion context\n");
  96. exit(1);
  97. }
  98. }
  99. // img_convert parameters are 2 first destination, then 4 source
  100. // sws_scale parameters are context, 4 first source, then 2 destination
  101. sws_scale(ci->toRGB_convert_ctx,
  102. picture->data, picture->linesize, 0, ci->sws_height,
  103. picture1.data, picture1.linesize);
  104. pict = &picture1;
  105. }
  106. /* Insert filter code here */
  107. if (pix_fmt != PIX_FMT_RGB24) {
  108. sws_scale(ci->fromRGB_convert_ctx,
  109. picture1.data, picture1.linesize, 0, ci->sws_height,
  110. picture->data, picture->linesize);
  111. }
  112. av_free(buf);
  113. }