0373-BCM2835-V4L2-Correct-handling-for-BGR24-vs-RGB24.patch 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. From 8d2b378035000b56c9ac7964c7000e9cd7679c01 Mon Sep 17 00:00:00 2001
  2. From: Dave Stevenson <6by9@users.noreply.github.com>
  3. Date: Wed, 25 May 2016 23:25:36 +0100
  4. Subject: [PATCH] BCM2835-V4L2: Correct handling for BGR24 vs RGB24.
  5. There was a bug in the GPU firmware that had reversed these
  6. two formats.
  7. Detect the old firmware, and reverse the formats if necessary.
  8. Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
  9. ---
  10. drivers/media/platform/bcm2835/bcm2835-camera.c | 69 ++++++++++++++++++-------
  11. drivers/media/platform/bcm2835/bcm2835-camera.h | 1 +
  12. 2 files changed, 52 insertions(+), 18 deletions(-)
  13. --- a/drivers/media/platform/bcm2835/bcm2835-camera.c
  14. +++ b/drivers/media/platform/bcm2835/bcm2835-camera.c
  15. @@ -115,7 +115,7 @@ static struct mmal_fmt formats[] = {
  16. .name = "RGB24 (LE)",
  17. .fourcc = V4L2_PIX_FMT_RGB24,
  18. .flags = 0,
  19. - .mmal = MMAL_ENCODING_BGR24,
  20. + .mmal = MMAL_ENCODING_RGB24,
  21. .depth = 24,
  22. .mmal_component = MMAL_COMPONENT_CAMERA,
  23. .ybbp = 3,
  24. @@ -187,7 +187,7 @@ static struct mmal_fmt formats[] = {
  25. .name = "RGB24 (BE)",
  26. .fourcc = V4L2_PIX_FMT_BGR24,
  27. .flags = 0,
  28. - .mmal = MMAL_ENCODING_RGB24,
  29. + .mmal = MMAL_ENCODING_BGR24,
  30. .depth = 24,
  31. .mmal_component = MMAL_COMPONENT_CAMERA,
  32. .ybbp = 3,
  33. @@ -1059,6 +1059,13 @@ static int mmal_setup_components(struct
  34. else
  35. camera_port->format.encoding = mfmt->mmal;
  36. + if (dev->rgb_bgr_swapped) {
  37. + if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
  38. + camera_port->format.encoding = MMAL_ENCODING_BGR24;
  39. + else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
  40. + camera_port->format.encoding = MMAL_ENCODING_RGB24;
  41. + }
  42. +
  43. camera_port->format.encoding_variant = 0;
  44. camera_port->es.video.width = f->fmt.pix.width;
  45. camera_port->es.video.height = f->fmt.pix.height;
  46. @@ -1569,12 +1576,17 @@ static int set_camera_parameters(struct
  47. return ret;
  48. }
  49. +#define MAX_SUPPORTED_ENCODINGS 20
  50. +
  51. /* MMAL instance and component init */
  52. static int __init mmal_init(struct bm2835_mmal_dev *dev)
  53. {
  54. int ret;
  55. struct mmal_es_format *format;
  56. u32 bool_true = 1;
  57. + u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
  58. + int param_size;
  59. + struct vchiq_mmal_component *camera;
  60. ret = vchiq_mmal_init(&dev->instance);
  61. if (ret < 0)
  62. @@ -1586,21 +1598,48 @@ static int __init mmal_init(struct bm283
  63. if (ret < 0)
  64. goto unreg_mmal;
  65. - if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  66. - MMAL_CAMERA_PORT_COUNT) {
  67. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  68. + if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
  69. ret = -EINVAL;
  70. goto unreg_camera;
  71. }
  72. ret = set_camera_parameters(dev->instance,
  73. - dev->component[MMAL_COMPONENT_CAMERA],
  74. + camera,
  75. dev);
  76. if (ret < 0)
  77. goto unreg_camera;
  78. - format =
  79. - &dev->component[MMAL_COMPONENT_CAMERA]->
  80. - output[MMAL_CAMERA_PORT_PREVIEW].format;
  81. + /* There was an error in the firmware that meant the camera component
  82. + * produced BGR instead of RGB.
  83. + * This is now fixed, but in order to support the old firmwares, we
  84. + * have to check.
  85. + */
  86. + dev->rgb_bgr_swapped = true;
  87. + param_size = sizeof(supported_encodings);
  88. + ret = vchiq_mmal_port_parameter_get(dev->instance,
  89. + &camera->output[MMAL_CAMERA_PORT_CAPTURE],
  90. + MMAL_PARAMETER_SUPPORTED_ENCODINGS,
  91. + &supported_encodings,
  92. + &param_size);
  93. + if (ret == 0) {
  94. + int i;
  95. +
  96. + for (i = 0; i < param_size/sizeof(u32); i++) {
  97. + if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
  98. + /* Found BGR24 first - old firmware. */
  99. + break;
  100. + }
  101. + if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
  102. + /* Found RGB24 first
  103. + * new firmware, so use RGB24.
  104. + */
  105. + dev->rgb_bgr_swapped = false;
  106. + break;
  107. + }
  108. + }
  109. + }
  110. + format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
  111. format->encoding = MMAL_ENCODING_OPAQUE;
  112. format->encoding_variant = MMAL_ENCODING_I420;
  113. @@ -1614,9 +1653,7 @@ static int __init mmal_init(struct bm283
  114. format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  115. format->es->video.frame_rate.den = 1;
  116. - format =
  117. - &dev->component[MMAL_COMPONENT_CAMERA]->
  118. - output[MMAL_CAMERA_PORT_VIDEO].format;
  119. + format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
  120. format->encoding = MMAL_ENCODING_OPAQUE;
  121. format->encoding_variant = MMAL_ENCODING_I420;
  122. @@ -1631,14 +1668,11 @@ static int __init mmal_init(struct bm283
  123. format->es->video.frame_rate.den = 1;
  124. vchiq_mmal_port_parameter_set(dev->instance,
  125. - &dev->component[MMAL_COMPONENT_CAMERA]->
  126. - output[MMAL_CAMERA_PORT_VIDEO],
  127. + &camera->output[MMAL_CAMERA_PORT_VIDEO],
  128. MMAL_PARAMETER_NO_IMAGE_PADDING,
  129. &bool_true, sizeof(bool_true));
  130. - format =
  131. - &dev->component[MMAL_COMPONENT_CAMERA]->
  132. - output[MMAL_CAMERA_PORT_CAPTURE].format;
  133. + format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
  134. format->encoding = MMAL_ENCODING_OPAQUE;
  135. @@ -1660,8 +1694,7 @@ static int __init mmal_init(struct bm283
  136. dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  137. vchiq_mmal_port_parameter_set(dev->instance,
  138. - &dev->component[MMAL_COMPONENT_CAMERA]->
  139. - output[MMAL_CAMERA_PORT_CAPTURE],
  140. + &camera->output[MMAL_CAMERA_PORT_CAPTURE],
  141. MMAL_PARAMETER_NO_IMAGE_PADDING,
  142. &bool_true, sizeof(bool_true));
  143. --- a/drivers/media/platform/bcm2835/bcm2835-camera.h
  144. +++ b/drivers/media/platform/bcm2835/bcm2835-camera.h
  145. @@ -109,6 +109,7 @@ struct bm2835_mmal_dev {
  146. unsigned int camera_num;
  147. unsigned int max_width;
  148. unsigned int max_height;
  149. + unsigned int rgb_bgr_swapped;
  150. };
  151. int bm2835_mmal_init_controls(