v4l2_m2m.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * V4L2 mem2mem helper functions
  3. *
  4. * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
  5. * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #ifndef AVCODEC_V4L2_M2M_H
  24. #define AVCODEC_V4L2_M2M_H
  25. #include <semaphore.h>
  26. #include <unistd.h>
  27. #include <dirent.h>
  28. #include <linux/videodev2.h>
  29. #include "libavcodec/avcodec.h"
  30. #include "v4l2_context.h"
  31. #define container_of(ptr, type, member) ({ \
  32. const __typeof__(((type *)0)->member ) *__mptr = (ptr); \
  33. (type *)((char *)__mptr - offsetof(type,member) );})
  34. #define V4L_M2M_DEFAULT_OPTS \
  35. { "num_output_buffers", "Number of buffers in the output context",\
  36. OFFSET(num_output_buffers), AV_OPT_TYPE_INT, { .i64 = 16 }, 6, INT_MAX, FLAGS }
  37. typedef struct V4L2m2mContext {
  38. char devname[PATH_MAX];
  39. int fd;
  40. /* the codec context queues */
  41. V4L2Context capture;
  42. V4L2Context output;
  43. /* dynamic stream reconfig */
  44. AVCodecContext *avctx;
  45. sem_t refsync;
  46. atomic_uint refcount;
  47. int reinit;
  48. /* null frame/packet received */
  49. int draining;
  50. AVPacket buf_pkt;
  51. /* Reference to a frame. Only used during encoding */
  52. AVFrame *frame;
  53. /* Reference to self; only valid while codec is active. */
  54. AVBufferRef *self_ref;
  55. /* reference back to V4L2m2mPriv */
  56. void *priv;
  57. } V4L2m2mContext;
  58. typedef struct V4L2m2mPriv {
  59. AVClass *class;
  60. V4L2m2mContext *context;
  61. AVBufferRef *context_ref;
  62. int num_output_buffers;
  63. int num_capture_buffers;
  64. } V4L2m2mPriv;
  65. /**
  66. * Allocate a new context and references for a V4L2 M2M instance.
  67. *
  68. * @param[in] ctx The V4L2m2mPriv instantiated by the encoder/decoder.
  69. * @param[out] ctx The V4L2m2mContext.
  70. *
  71. * @returns 0 in success, a negative error code otherwise.
  72. */
  73. int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s);
  74. /**
  75. * Probes the video nodes looking for the required codec capabilities.
  76. *
  77. * @param[in] ctx The V4L2m2mPriv instantiated by the encoder/decoder.
  78. *
  79. * @returns 0 if a driver is found, a negative number otherwise.
  80. */
  81. int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv);
  82. /**
  83. * Releases all the codec resources if all AVBufferRefs have been returned to the
  84. * ctx. Otherwise keep the driver open.
  85. *
  86. * @param[in] The V4L2m2mPriv instantiated by the encoder/decoder.
  87. *
  88. * @returns 0
  89. *
  90. */
  91. int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv);
  92. /**
  93. * Reinitializes the V4L2m2mContext when the driver cannot continue processing
  94. * with the capture parameters.
  95. *
  96. * @param[in] ctx The V4L2m2mContext instantiated by the encoder/decoder.
  97. *
  98. * @returns 0 in case of success, negative number otherwise
  99. */
  100. int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *ctx);
  101. /**
  102. * Reinitializes the V4L2m2mContext when the driver cannot continue processing
  103. * with the any of the current V4L2Contexts (ie, changes in output and capture).
  104. *
  105. * @param[in] ctx The V4L2m2mContext instantiated by the encoder/decoder.
  106. *
  107. * @returns 0 in case of success, negative number otherwise
  108. */
  109. int ff_v4l2_m2m_codec_full_reinit(V4L2m2mContext *ctx);
  110. #endif /* AVCODEC_V4L2_M2M_H */