null.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "libavformat/framehook.h"
  23. #include "libswscale/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. } ContextInfo;
  32. void Release(void *ctx)
  33. {
  34. ContextInfo *ci;
  35. ci = (ContextInfo *) ctx;
  36. if (ctx) {
  37. sws_freeContext(ci->toRGB_convert_ctx);
  38. sws_freeContext(ci->fromRGB_convert_ctx);
  39. av_free(ctx);
  40. }
  41. }
  42. int Configure(void **ctxp, int argc, char *argv[])
  43. {
  44. av_log(NULL, AV_LOG_DEBUG, "Called with argc=%d\n", argc);
  45. *ctxp = av_mallocz(sizeof(ContextInfo));
  46. return 0;
  47. }
  48. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  49. {
  50. ContextInfo *ci = (ContextInfo *) ctx;
  51. char *buf = 0;
  52. AVPicture picture1;
  53. AVPicture *pict = picture;
  54. (void) ci;
  55. if (pix_fmt != PIX_FMT_RGB24) {
  56. int size;
  57. size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  58. buf = av_malloc(size);
  59. avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
  60. // if we already got a SWS context, let's realloc if is not re-useable
  61. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  62. width, height, pix_fmt,
  63. width, height, PIX_FMT_RGB24,
  64. sws_flags, NULL, NULL, NULL);
  65. if (ci->toRGB_convert_ctx == NULL) {
  66. av_log(NULL, AV_LOG_ERROR,
  67. "Cannot initialize the toRGB conversion context\n");
  68. return;
  69. }
  70. // img_convert parameters are 2 first destination, then 4 source
  71. // sws_scale parameters are context, 4 first source, then 2 destination
  72. sws_scale(ci->toRGB_convert_ctx,
  73. picture->data, picture->linesize, 0, height,
  74. picture1.data, picture1.linesize);
  75. pict = &picture1;
  76. }
  77. /* Insert filter code here */
  78. if (pix_fmt != PIX_FMT_RGB24) {
  79. ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
  80. width, height, PIX_FMT_RGB24,
  81. width, height, pix_fmt,
  82. sws_flags, NULL, NULL, NULL);
  83. if (ci->fromRGB_convert_ctx == NULL) {
  84. av_log(NULL, AV_LOG_ERROR,
  85. "Cannot initialize the fromRGB conversion context\n");
  86. return;
  87. }
  88. // img_convert parameters are 2 first destination, then 4 source
  89. // sws_scale parameters are context, 4 first source, then 2 destination
  90. sws_scale(ci->fromRGB_convert_ctx,
  91. picture1.data, picture1.linesize, 0, height,
  92. picture->data, picture->linesize);
  93. }
  94. av_free(buf);
  95. }