0010-libcamera-add-framerate-for-request-pad-src.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. From d93fba5044e5688df377b8f1087d22b0357c36ba Mon Sep 17 00:00:00 2001
  2. From: chenhb3487 <chenhb3487@thundersoft.com>
  3. Date: Tue, 11 Jan 2022 10:24:26 +0800
  4. Subject: [PATCH] libcamera: add framerate for request pad src.
  5. ---
  6. .../0010-add-framerate-from-v4l2.patch | 234 ++++++++++++++++++
  7. 1 file changed, 234 insertions(+)
  8. create mode 100644 package/libcamera/0010-add-framerate-from-v4l2.patch
  9. diff --git a/package/libcamera/0010-add-framerate-from-v4l2.patch b/package/libcamera/0010-add-framerate-from-v4l2.patch
  10. new file mode 100644
  11. index 00000000..b2a558aa
  12. --- /dev/null
  13. +++ b/package/libcamera/0010-add-framerate-from-v4l2.patch
  14. @@ -0,0 +1,234 @@
  15. +diff -Naur a/include/libcamera/internal/v4l2_videodevice.h libcamera-d6f4abeead1e86d89dc376e8a303849bdb98d5fd/include/libcamera/internal/v4l2_videodevice.h
  16. +--- a/include/libcamera/internal/v4l2_videodevice.h 2022-01-10 22:03:08.131869823 +0800
  17. ++++ b/include/libcamera/internal/v4l2_videodevice.h 2022-01-11 10:12:46.551963256 +0800
  18. +@@ -35,6 +35,11 @@
  19. + class MediaDevice;
  20. + class MediaEntity;
  21. +
  22. ++struct V4L2Framerate {
  23. ++ uint32_t num = 0;
  24. ++ uint32_t denom = 0;
  25. ++};
  26. ++
  27. + struct V4L2Capability final : v4l2_capability {
  28. + const char *driver() const
  29. + {
  30. +@@ -215,7 +220,7 @@
  31. +
  32. + static std::unique_ptr<V4L2VideoDevice>
  33. + fromEntityName(const MediaDevice *media, const std::string &entity);
  34. +-
  35. ++ std::vector<V4L2Framerate> getFramerates(uint32_t pixelformat, uint32_t width, uint32_t height);
  36. + protected:
  37. + std::string logPrefix() const override;
  38. +
  39. +diff -Naur a/include/libcamera/stream.h b/include/libcamera/stream.h
  40. +--- a/include/libcamera/stream.h 2022-01-10 22:03:08.131869823 +0800
  41. ++++ b/include/libcamera/stream.h 2022-01-11 10:12:56.020027708 +0800
  42. +@@ -36,6 +36,11 @@
  43. + std::map<PixelFormat, std::vector<SizeRange>> formats_;
  44. + };
  45. +
  46. ++struct Framerate {
  47. ++ uint32_t num = 0;
  48. ++ uint32_t denom = 0;
  49. ++};
  50. ++
  51. + struct StreamConfiguration {
  52. + StreamConfiguration();
  53. + StreamConfiguration(const StreamFormats &formats);
  54. +@@ -51,11 +56,15 @@
  55. + void setStream(Stream *stream) { stream_ = stream; }
  56. + const StreamFormats &formats() const { return formats_; }
  57. +
  58. ++ void setframeRates(std::vector<Framerate> framerates) { rates = framerates; }
  59. ++ const std::vector<Framerate> getframeRates() const { return rates; }
  60. ++
  61. + std::string toString() const;
  62. +
  63. + private:
  64. + Stream *stream_;
  65. + StreamFormats formats_;
  66. ++ std::vector<Framerate> rates;
  67. + };
  68. +
  69. + enum StreamRole {
  70. +diff -Naur a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp
  71. +--- a/src/gstreamer/gstlibcamera-utils.cpp 2022-01-10 22:03:08.135869864 +0800
  72. ++++ b/src/gstreamer/gstlibcamera-utils.cpp 2022-01-11 10:13:11.952135908 +0800
  73. +@@ -140,10 +140,18 @@
  74. + "height", G_TYPE_INT, stream_cfg.size.height,
  75. + nullptr);
  76. +
  77. +- // Add framerate negotiation support
  78. +- // the range will be [ 0/1, 2147483647/1 ] as there is not any args
  79. +- // required from driver for the time being.
  80. +- gst_structure_set(s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
  81. ++ std::vector<libcamera::Framerate> rates = stream_cfg.getframeRates();
  82. ++ guint index = 0;
  83. ++ for (libcamera::Framerate it : rates) {
  84. ++ libcamera::Framerate rate = it;
  85. ++ gst_structure_set (s, "framerate", GST_TYPE_FRACTION, rate.denom, rate.num, NULL);
  86. ++ //truncate rates. only remain the first one.
  87. ++ if ( index == 0)
  88. ++ {
  89. ++ break;
  90. ++ }
  91. ++ index++;
  92. ++ }
  93. +
  94. + gst_caps_append_structure(caps, s);
  95. +
  96. +diff -Naur a/src/libcamera/pipeline/starfive/starfive.cpp b/src/libcamera/pipeline/starfive/starfive.cpp
  97. +--- a/src/libcamera/pipeline/starfive/starfive.cpp 2022-01-10 22:03:08.139869906 +0800
  98. ++++ b/src/libcamera/pipeline/starfive/starfive.cpp 2022-01-11 10:13:31.200266219 +0800
  99. +@@ -44,6 +44,9 @@
  100. +
  101. + #define PIPELINE_CONFIG_FILENAME "/etc/starfive/sensors_pipeline.yaml"
  102. +
  103. ++#define DEFAULT_FRAMERATE_NUM 25;
  104. ++#define DEFAULT_FRAMERATE_DENOM 1;
  105. ++
  106. + namespace {
  107. +
  108. + typedef enum {
  109. +@@ -226,6 +229,8 @@
  110. + std::vector<SizeRange> sensorSizes() const;
  111. + std::vector<PixelFormat> sensorFormats() const;
  112. + std::vector<PixelFormat> videoFormats() const;
  113. ++ std::vector<Framerate> videoFrameRates(unsigned int pixelformat, unsigned int width, unsigned int height) const;
  114. ++
  115. + void paramsFilled(unsigned int id){}
  116. + int ispLoadFW(const char *filename);
  117. +
  118. +@@ -270,6 +275,24 @@
  119. + return formats;
  120. + }
  121. +
  122. ++std::vector<Framerate> StarFiveCameraData::videoFrameRates(unsigned int pixelformat, unsigned int width, unsigned int height) const
  123. ++{
  124. ++ if (!video_)
  125. ++ return {};
  126. ++
  127. ++ std::vector<Framerate> framerates;
  128. ++ for (auto it : video_->getFramerates(pixelformat, width, height)) {
  129. ++ V4L2Framerate v4l2_framerate = (V4L2Framerate)it;
  130. ++ libcamera::Framerate rate;
  131. ++ rate.num = v4l2_framerate.num;
  132. ++ rate.denom = v4l2_framerate.denom;
  133. ++ LOG(STARFIVE, Debug) << "videoFrameRates framerate: rate.num=" << rate.num << " rate.denom= " << rate.denom;
  134. ++ framerates.push_back(rate);
  135. ++ }
  136. ++
  137. ++ return framerates;
  138. ++}
  139. ++
  140. + std::vector<PixelFormat> StarFiveCameraData::sensorFormats() const
  141. + {
  142. + if (!sensor_)
  143. +@@ -481,6 +504,20 @@
  144. + cfg.size.height = std::max(OUTPUT_MIN_SIZE.height,
  145. + std::min(OUTPUT_MAX_SIZE.height, cfg.size.height));
  146. +
  147. ++ std::vector<Framerate> rates;
  148. ++ rates = data_->videoFrameRates(cfg.pixelFormat,size.width, size.height);
  149. ++ if (rates.size() > 0){
  150. ++ cfg.setframeRates(rates);
  151. ++ } else {
  152. ++ LOG(STARFIVE, Debug)
  153. ++ << "fail to obtian framerate, use default one ";
  154. ++ Framerate rate;
  155. ++ rate.num = DEFAULT_FRAMERATE_NUM;
  156. ++ rate.denom = DEFAULT_FRAMERATE_DENOM;
  157. ++ rates.emplace_back(rate);
  158. ++ cfg.setframeRates(rates);
  159. ++ }
  160. ++
  161. + if (cfg.size != size) {
  162. + LOG(STARFIVE, Debug)
  163. + << "Adjusting size to " << cfg.size.toString();
  164. +diff -Naur a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
  165. +--- a/src/libcamera/v4l2_videodevice.cpp 2022-01-10 22:03:08.135869864 +0800
  166. ++++ b/src/libcamera/v4l2_videodevice.cpp 2022-01-11 10:13:42.444342141 +0800
  167. +@@ -31,6 +31,7 @@
  168. + #include "libcamera/internal/framebuffer.h"
  169. + #include "libcamera/internal/media_device.h"
  170. + #include "libcamera/internal/media_object.h"
  171. ++#include <limits.h>
  172. +
  173. + /**
  174. + * \file v4l2_videodevice.h
  175. +@@ -1847,6 +1848,73 @@
  176. + return std::make_unique<V4L2VideoDevice>(mediaEntity);
  177. + }
  178. +
  179. ++std::vector<V4L2Framerate> V4L2VideoDevice::getFramerates(uint32_t pixelformat, uint32_t width, uint32_t height)
  180. ++{
  181. ++ struct v4l2_frmivalenum ival;
  182. ++ uint32_t num, denom;
  183. ++
  184. ++ memset (&ival, 0, sizeof (struct v4l2_frmivalenum));
  185. ++ ival.index = 0;
  186. ++ ival.pixel_format = pixelformat;
  187. ++ ival.width = width;
  188. ++ ival.height = height;
  189. ++ std::vector<V4L2Framerate> rates;
  190. ++ LOG(V4L2, Debug) << "getFramerates: width: " << width << " height: " << height << " pixelformat " << pixelformat;
  191. ++
  192. ++ if (ioctl (VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0){
  193. ++ LOG(V4L2, Error) << "fail to ioctl: VIDIOC_ENUM_FRAMEINTERVALS";
  194. ++ goto enum_frameintervals_failed;
  195. ++ }
  196. ++
  197. ++ if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
  198. ++ struct V4L2Framerate rate;
  199. ++ do {
  200. ++ num = ival.discrete.numerator;
  201. ++ denom = ival.discrete.denominator;
  202. ++
  203. ++ if (num > INT_MAX || denom > INT_MAX) {
  204. ++ /* let us hope we don't get here... */
  205. ++ LOG(V4L2, Error) << "num or denom is beyond INT_MAX";
  206. ++ goto enum_frameintervals_failed;
  207. ++ }
  208. ++
  209. ++ // /* swap to get the framerate */
  210. ++ // gst_value_set_fraction (&rate, denom, num);
  211. ++ rate.denom = denom;
  212. ++ rate.num = num;
  213. ++ LOG(V4L2, Debug) << "gstFramerates adding discrete framerate: " << denom << "/" << num;
  214. ++ rates.emplace_back(rate);
  215. ++ ival.index++;
  216. ++ } while (ioctl (VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
  217. ++ } else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE){
  218. ++ //TODO
  219. ++ goto enum_frameintervals_unimplemented;
  220. ++ } else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
  221. ++ num = ival.stepwise.min.numerator;
  222. ++ denom = ival.stepwise.min.denominator;
  223. ++ if (num > INT_MAX || denom > INT_MAX) {
  224. ++ LOG(V4L2, Error) << "continuous frame interval: num or denom is beyond INT_MAX";
  225. ++ goto enum_frameintervals_failed;
  226. ++ }
  227. ++
  228. ++ struct V4L2Framerate rate;
  229. ++ rate.denom = denom;
  230. ++ rate.num = num;
  231. ++ LOG(V4L2, Debug) << "gstFramerates continuous frame interval: " << denom << "/" <<num;
  232. ++ rates.emplace_back(rate);
  233. ++ }
  234. ++
  235. ++ return rates;
  236. ++
  237. ++enum_frameintervals_failed:
  238. ++enum_frameintervals_unimplemented:
  239. ++ {
  240. ++ LOG(V4L2, Error) << "fail to obtain framerate !!!";
  241. ++ return rates;
  242. ++ }
  243. ++
  244. ++}
  245. ++
  246. + /**
  247. + * \class V4L2M2MDevice
  248. + * \brief Memory-to-Memory video device
  249. --
  250. 2.25.1