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.

138 lines
4.6KB

  1. /*
  2. * Null Video Hook
  3. * Copyright (c) 2002 Philip Gladstone
  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 <stdio.h>
  22. #include "framehook.h"
  23. #include "swscale.h"
  24. static int sws_flags = SWS_BICUBIC;
  25. typedef struct {
  26. int dummy;
  27. // This vhook first converts frame to RGB ...
  28. struct SwsContext *toRGB_convert_ctx;
  29. // ... and later converts back frame from RGB to initial format
  30. struct SwsContext *fromRGB_convert_ctx;
  31. enum PixelFormat sws_pix_fmt; // Sws_Context is opaque, we need to save
  32. int sws_width, sws_height; // this to check if we can re-use contexts
  33. // Contexts will be re-used 99% of time,
  34. // I know of width/height changes only in DVB broadcasts
  35. } ContextInfo;
  36. void Release(void *ctx)
  37. {
  38. ContextInfo *ci;
  39. ci = (ContextInfo *) ctx;
  40. if (ctx) {
  41. sws_freeContext(ci->toRGB_convert_ctx);
  42. sws_freeContext(ci->fromRGB_convert_ctx);
  43. av_free(ctx);
  44. }
  45. }
  46. int Configure(void **ctxp, int argc, char *argv[])
  47. {
  48. fprintf(stderr, "Called with argc=%d\n", argc);
  49. *ctxp = av_mallocz(sizeof(ContextInfo));
  50. return 0;
  51. }
  52. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  53. {
  54. ContextInfo *ci = (ContextInfo *) ctx;
  55. char *buf = 0;
  56. AVPicture picture1;
  57. AVPicture *pict = picture;
  58. (void) ci;
  59. if (pix_fmt != PIX_FMT_RGB24) {
  60. int size;
  61. size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  62. buf = av_malloc(size);
  63. avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
  64. // if we already got a SWS context, let's realloc if is not re-useable
  65. if (ci->toRGB_convert_ctx != NULL) {
  66. if ((ci->sws_pix_fmt != pix_fmt) ||
  67. (ci->sws_width != width) || (ci->sws_height != height)) {
  68. sws_freeContext(ci->toRGB_convert_ctx);
  69. ci->toRGB_convert_ctx = NULL;
  70. sws_freeContext(ci->fromRGB_convert_ctx);
  71. ci->fromRGB_convert_ctx = NULL;
  72. }
  73. }
  74. if (ci->toRGB_convert_ctx == NULL) {
  75. ci->sws_pix_fmt = pix_fmt;
  76. ci->sws_width = width;
  77. ci->sws_height = height;
  78. ci->toRGB_convert_ctx = sws_getContext(
  79. ci->sws_width, ci->sws_height,
  80. ci->sws_pix_fmt,
  81. ci->sws_width, ci->sws_height,
  82. PIX_FMT_RGB24,
  83. sws_flags, NULL, NULL, NULL);
  84. if (ci->toRGB_convert_ctx == NULL) {
  85. av_log(NULL, AV_LOG_ERROR,
  86. "Cannot initialize the toRGB conversion context\n");
  87. exit(1);
  88. }
  89. ci->fromRGB_convert_ctx = sws_getContext(
  90. ci->sws_width, ci->sws_height,
  91. PIX_FMT_RGB24,
  92. ci->sws_width, ci->sws_height,
  93. ci->sws_pix_fmt,
  94. sws_flags, NULL, NULL, NULL);
  95. if (ci->fromRGB_convert_ctx == NULL) {
  96. av_log(NULL, AV_LOG_ERROR,
  97. "Cannot initialize the fromRGB conversion context\n");
  98. exit(1);
  99. }
  100. }
  101. // img_convert parameters are 2 first destination, then 4 source
  102. // sws_scale parameters are context, 4 first source, then 2 destination
  103. sws_scale(ci->toRGB_convert_ctx,
  104. picture->data, picture->linesize, 0, ci->sws_height,
  105. picture1.data, picture1.linesize);
  106. pict = &picture1;
  107. }
  108. /* Insert filter code here */
  109. if (pix_fmt != PIX_FMT_RGB24) {
  110. sws_scale(ci->fromRGB_convert_ctx,
  111. picture1.data, picture1.linesize, 0, ci->sws_height,
  112. picture->data, picture->linesize);
  113. }
  114. av_free(buf);
  115. }