2
0

v4l2_fmt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * V4L2 format 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. #include <linux/videodev2.h>
  24. #include <search.h>
  25. #include "v4l2_fmt.h"
  26. #define V4L2_FMT(x) V4L2_PIX_FMT_##x
  27. #define AV_CODEC(x) AV_CODEC_ID_##x
  28. #define AV_FMT(x) AV_PIX_FMT_##x
  29. static const struct fmt_conversion {
  30. enum AVPixelFormat avfmt;
  31. enum AVCodecID avcodec;
  32. uint32_t v4l2_fmt;
  33. } fmt_map[] = {
  34. { AV_FMT(RGB555LE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB555) },
  35. { AV_FMT(RGB555BE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB555X) },
  36. { AV_FMT(RGB565LE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB565) },
  37. { AV_FMT(RGB565BE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB565X) },
  38. { AV_FMT(BGR24), AV_CODEC(RAWVIDEO), V4L2_FMT(BGR24) },
  39. { AV_FMT(RGB24), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB24) },
  40. { AV_FMT(BGR0), AV_CODEC(RAWVIDEO), V4L2_FMT(BGR32) },
  41. { AV_FMT(0RGB), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB32) },
  42. { AV_FMT(GRAY8), AV_CODEC(RAWVIDEO), V4L2_FMT(GREY) },
  43. { AV_FMT(YUV420P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV420) },
  44. { AV_FMT(YUYV422), AV_CODEC(RAWVIDEO), V4L2_FMT(YUYV) },
  45. { AV_FMT(UYVY422), AV_CODEC(RAWVIDEO), V4L2_FMT(UYVY) },
  46. { AV_FMT(YUV422P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV422P) },
  47. { AV_FMT(YUV411P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV411P) },
  48. { AV_FMT(YUV410P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV410) },
  49. { AV_FMT(YUV410P), AV_CODEC(RAWVIDEO), V4L2_FMT(YVU410) },
  50. { AV_FMT(NV12), AV_CODEC(RAWVIDEO), V4L2_FMT(NV12) },
  51. { AV_FMT(NONE), AV_CODEC(MJPEG), V4L2_FMT(MJPEG) },
  52. { AV_FMT(NONE), AV_CODEC(MJPEG), V4L2_FMT(JPEG) },
  53. #ifdef V4L2_PIX_FMT_SRGGB8
  54. { AV_FMT(BAYER_BGGR8), AV_CODEC(RAWVIDEO), V4L2_FMT(SBGGR8) },
  55. { AV_FMT(BAYER_GBRG8), AV_CODEC(RAWVIDEO), V4L2_FMT(SGBRG8) },
  56. { AV_FMT(BAYER_GRBG8), AV_CODEC(RAWVIDEO), V4L2_FMT(SGRBG8) },
  57. { AV_FMT(BAYER_RGGB8), AV_CODEC(RAWVIDEO), V4L2_FMT(SRGGB8) },
  58. #endif
  59. #ifdef V4L2_PIX_FMT_Y16
  60. { AV_FMT(GRAY16LE), AV_CODEC(RAWVIDEO), V4L2_FMT(Y16) },
  61. #endif
  62. #ifdef V4L2_PIX_FMT_NV12M
  63. { AV_FMT(NV12), AV_CODEC(RAWVIDEO), V4L2_FMT(NV12M) },
  64. #endif
  65. #ifdef V4L2_PIX_FMT_NV21M
  66. { AV_FMT(NV21), AV_CODEC(RAWVIDEO), V4L2_FMT(NV21M) },
  67. #endif
  68. #ifdef V4L2_PIX_FMT_YUV420M
  69. { AV_FMT(YUV420P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV420M) },
  70. #endif
  71. #ifdef V4L2_PIX_FMT_NV16M
  72. { AV_FMT(NV16), AV_CODEC(RAWVIDEO), V4L2_FMT(NV16M) },
  73. #endif
  74. #ifdef V4L2_PIX_FMT_H263
  75. { AV_FMT(NONE), AV_CODEC(H263), V4L2_FMT(H263) },
  76. #endif
  77. #ifdef V4L2_PIX_FMT_H264
  78. { AV_FMT(NONE), AV_CODEC(H264), V4L2_FMT(H264) },
  79. #endif
  80. #ifdef V4L2_PIX_FMT_MPEG4
  81. { AV_FMT(NONE), AV_CODEC(MPEG4), V4L2_FMT(MPEG4) },
  82. #endif
  83. #ifdef V4L2_PIX_FMT_CPIA1
  84. { AV_FMT(NONE), AV_CODEC(CPIA), V4L2_FMT(CPIA1) },
  85. #endif
  86. #ifdef V4L2_PIX_FMT_DV
  87. { AV_FMT(NONE), AV_CODEC(DVVIDEO), V4L2_FMT(DV) },
  88. #endif
  89. #ifdef V4L2_PIX_FMT_MPEG1
  90. { AV_FMT(NONE), AV_CODEC(MPEG1VIDEO), V4L2_FMT(MPEG1) },
  91. #endif
  92. #ifdef V4L2_PIX_FMT_MPEG2
  93. { AV_FMT(NONE), AV_CODEC(MPEG2VIDEO), V4L2_FMT(MPEG2) },
  94. #endif
  95. #ifdef V4L2_PIX_FMT_VP8
  96. { AV_FMT(NONE), AV_CODEC(VP8), V4L2_FMT(VP8) },
  97. #endif
  98. #ifdef V4L2_PIX_FMT_VP9
  99. { AV_FMT(NONE), AV_CODEC(VP9), V4L2_FMT(VP9) },
  100. #endif
  101. #ifdef V4L2_PIX_FMT_HEVC
  102. { AV_FMT(NONE), AV_CODEC(HEVC), V4L2_FMT(HEVC) },
  103. #endif
  104. #ifdef V4L2_PIX_FMT_VC1_ANNEX_G
  105. { AV_FMT(NONE), AV_CODEC(VC1), V4L2_FMT(VC1_ANNEX_G) },
  106. #endif
  107. };
  108. uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)
  109. {
  110. int i;
  111. for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) {
  112. if (fmt_map[i].avcodec == avcodec)
  113. return fmt_map[i].v4l2_fmt;
  114. }
  115. return 0;
  116. }
  117. uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)
  118. {
  119. int i;
  120. for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) {
  121. if (fmt_map[i].avfmt == avfmt)
  122. return fmt_map[i].v4l2_fmt;
  123. }
  124. return 0;
  125. }
  126. enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
  127. {
  128. int i;
  129. for (i = 0; i < FF_ARRAY_ELEMS(fmt_map); i++) {
  130. if (fmt_map[i].avcodec == avcodec &&
  131. fmt_map[i].v4l2_fmt == v4l2_fmt)
  132. return fmt_map[i].avfmt;
  133. }
  134. return AV_PIX_FMT_NONE;
  135. }