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.

183 lines
6.1KB

  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. static int match_codec(const void *a, const void *b)
  109. {
  110. if (*(enum AVCodecID *)a == ((struct fmt_conversion *)b)->avcodec)
  111. return 0;
  112. return 1;
  113. }
  114. uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)
  115. {
  116. size_t len = FF_ARRAY_ELEMS(fmt_map);
  117. struct fmt_conversion *item;
  118. item = lfind(&avcodec, fmt_map, &len, sizeof(fmt_map[0]), match_codec);
  119. if (item)
  120. return item->v4l2_fmt;
  121. return 0;
  122. }
  123. static int match_fmt(const void *a, const void *b)
  124. {
  125. if ( *(enum AVPixelFormat *)a == ((struct fmt_conversion *)b)->avfmt)
  126. return 0;
  127. return 1;
  128. }
  129. uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)
  130. {
  131. size_t len = FF_ARRAY_ELEMS(fmt_map);
  132. struct fmt_conversion *item;
  133. item = lfind(&avfmt, fmt_map, &len, sizeof(fmt_map[0]), match_fmt);
  134. if (item)
  135. return item->v4l2_fmt;
  136. return 0;
  137. }
  138. struct v4l2fmt_avcodec_pair {
  139. enum AVCodecID avcodec;
  140. uint32_t v4l2_fmt;
  141. };
  142. static int match_codecfmt(const void *a, const void *b)
  143. {
  144. struct v4l2fmt_avcodec_pair *key = (struct v4l2fmt_avcodec_pair *) a;
  145. struct fmt_conversion *item = (struct fmt_conversion *) b;
  146. if (key->avcodec == item->avcodec && key->v4l2_fmt == item->v4l2_fmt)
  147. return 0;
  148. return 1;
  149. }
  150. enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
  151. {
  152. struct v4l2fmt_avcodec_pair const key = {
  153. .v4l2_fmt = v4l2_fmt,
  154. .avcodec = avcodec,
  155. };
  156. size_t len = FF_ARRAY_ELEMS(fmt_map);
  157. struct fmt_conversion *item;
  158. item = lfind(&key, fmt_map, &len, sizeof(fmt_map[0]), match_codecfmt);
  159. if (item)
  160. return item->avfmt;
  161. return AV_PIX_FMT_NONE;
  162. }