magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import static java.lang.Math.abs; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 14 | |
sakal | 79ede03 | 2016-06-14 05:33:18 -0700 | [diff] [blame] | 15 | import android.graphics.ImageFormat; |
sakal | 9d1315a | 2016-11-10 06:35:17 -0800 | [diff] [blame] | 16 | import java.util.ArrayList; |
| 17 | import java.util.Arrays; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.Comparator; |
| 20 | import java.util.List; |
| 21 | |
| 22 | @SuppressWarnings("deprecation") |
| 23 | public class CameraEnumerationAndroid { |
| 24 | private final static String TAG = "CameraEnumerationAndroid"; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 25 | |
sakal | 9d1315a | 2016-11-10 06:35:17 -0800 | [diff] [blame] | 26 | static final ArrayList<Size> COMMON_RESOLUTIONS = new ArrayList<Size>(Arrays.asList( |
| 27 | // 0, Unknown resolution |
| 28 | new Size(160, 120), // 1, QQVGA |
| 29 | new Size(240, 160), // 2, HQVGA |
| 30 | new Size(320, 240), // 3, QVGA |
| 31 | new Size(400, 240), // 4, WQVGA |
| 32 | new Size(480, 320), // 5, HVGA |
| 33 | new Size(640, 360), // 6, nHD |
| 34 | new Size(640, 480), // 7, VGA |
| 35 | new Size(768, 480), // 8, WVGA |
| 36 | new Size(854, 480), // 9, FWVGA |
| 37 | new Size(800, 600), // 10, SVGA |
| 38 | new Size(960, 540), // 11, qHD |
| 39 | new Size(960, 640), // 12, DVGA |
| 40 | new Size(1024, 576), // 13, WSVGA |
| 41 | new Size(1024, 600), // 14, WVSGA |
| 42 | new Size(1280, 720), // 15, HD |
| 43 | new Size(1280, 1024), // 16, SXGA |
| 44 | new Size(1920, 1080), // 17, Full HD |
| 45 | new Size(1920, 1440), // 18, Full HD 4:3 |
| 46 | new Size(2560, 1440), // 19, QHD |
| 47 | new Size(3840, 2160) // 20, UHD |
| 48 | )); |
| 49 | |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 50 | public static class CaptureFormat { |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 51 | // Class to represent a framerate range. The framerate varies because of lightning conditions. |
| 52 | // The values are multiplied by 1000, so 1000 represents one frame per second. |
| 53 | public static class FramerateRange { |
| 54 | public int min; |
| 55 | public int max; |
| 56 | |
| 57 | public FramerateRange(int min, int max) { |
| 58 | this.min = min; |
| 59 | this.max = max; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String toString() { |
| 64 | return "[" + (min / 1000.0f) + ":" + (max / 1000.0f) + "]"; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean equals(Object other) { |
| 69 | if (!(other instanceof FramerateRange)) { |
| 70 | return false; |
| 71 | } |
| 72 | final FramerateRange otherFramerate = (FramerateRange) other; |
| 73 | return min == otherFramerate.min && max == otherFramerate.max; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public int hashCode() { |
| 78 | // Use prime close to 2^16 to avoid collisions for normal values less than 2^16. |
| 79 | return 1 + 65537 * min + max; |
| 80 | } |
| 81 | } |
| 82 | |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 83 | public final int width; |
| 84 | public final int height; |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 85 | public final FramerateRange framerate; |
sakal | e6c9e88 | 2016-06-17 01:02:04 -0700 | [diff] [blame] | 86 | |
Magnus Jedvert | 5e7834e | 2016-02-12 17:05:29 +0100 | [diff] [blame] | 87 | // TODO(hbos): If VideoCapturer.startCapture is updated to support other image formats then this |
| 88 | // needs to be updated and VideoCapturer.getSupportedFormats need to return CaptureFormats of |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 89 | // all imageFormats. |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 90 | public final int imageFormat = ImageFormat.NV21; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 91 | |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 92 | public CaptureFormat(int width, int height, int minFramerate, int maxFramerate) { |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 93 | this.width = width; |
| 94 | this.height = height; |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 95 | this.framerate = new FramerateRange(minFramerate, maxFramerate); |
| 96 | } |
| 97 | |
| 98 | public CaptureFormat(int width, int height, FramerateRange framerate) { |
| 99 | this.width = width; |
| 100 | this.height = height; |
| 101 | this.framerate = framerate; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Calculates the frame size of this capture format. |
| 105 | public int frameSize() { |
| 106 | return frameSize(width, height, imageFormat); |
| 107 | } |
| 108 | |
| 109 | // Calculates the frame size of the specified image format. Currently only |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 110 | // supporting ImageFormat.NV21. |
| 111 | // The size is width * height * number of bytes per pixel. |
| 112 | // http://developer.android.com/reference/android/hardware/Camera.html#addCallbackBuffer(byte[]) |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 113 | public static int frameSize(int width, int height, int imageFormat) { |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 114 | if (imageFormat != ImageFormat.NV21) { |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 115 | throw new UnsupportedOperationException("Don't know how to calculate " |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 116 | + "the frame size of non-NV21 image formats."); |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 117 | } |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 118 | return (width * height * ImageFormat.getBitsPerPixel(imageFormat)) / 8; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public String toString() { |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 123 | return width + "x" + height + "@" + framerate; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 124 | } |
| 125 | |
sakal | e6c9e88 | 2016-06-17 01:02:04 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public boolean equals(Object other) { |
| 128 | if (!(other instanceof CaptureFormat)) { |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
sakal | e6c9e88 | 2016-06-17 01:02:04 -0700 | [diff] [blame] | 131 | final CaptureFormat otherFormat = (CaptureFormat) other; |
| 132 | return width == otherFormat.width && height == otherFormat.height |
| 133 | && framerate.equals(otherFormat.framerate); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public int hashCode() { |
| 138 | return 1 + (width * 65497 + height) * 251 + framerate.hashCode(); |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 142 | // Helper class for finding the closest supported format for the two functions below. It creates a |
| 143 | // comparator based on the difference to some requested parameters, where the element with the |
| 144 | // minimum difference is the element that is closest to the requested parameters. |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 145 | private static abstract class ClosestComparator<T> implements Comparator<T> { |
| 146 | // Difference between supported and requested parameter. |
| 147 | abstract int diff(T supportedParameter); |
| 148 | |
| 149 | @Override |
| 150 | public int compare(T t1, T t2) { |
| 151 | return diff(t1) - diff(t2); |
| 152 | } |
| 153 | } |
| 154 | |
magjed | ce17e01 | 2016-06-01 00:43:59 -0700 | [diff] [blame] | 155 | // Prefer a fps range with an upper bound close to |framerate|. Also prefer a fps range with a low |
| 156 | // lower bound, to allow the framerate to fluctuate based on lightning conditions. |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 157 | public static CaptureFormat.FramerateRange getClosestSupportedFramerateRange( |
| 158 | List<CaptureFormat.FramerateRange> supportedFramerates, final int requestedFps) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 159 | return Collections.min( |
| 160 | supportedFramerates, new ClosestComparator<CaptureFormat.FramerateRange>() { |
magjed | ce17e01 | 2016-06-01 00:43:59 -0700 | [diff] [blame] | 161 | // Progressive penalty if the upper bound is further away than |MAX_FPS_DIFF_THRESHOLD| |
| 162 | // from requested. |
| 163 | private static final int MAX_FPS_DIFF_THRESHOLD = 5000; |
| 164 | private static final int MAX_FPS_LOW_DIFF_WEIGHT = 1; |
| 165 | private static final int MAX_FPS_HIGH_DIFF_WEIGHT = 3; |
| 166 | |
| 167 | // Progressive penalty if the lower bound is bigger than |MIN_FPS_THRESHOLD|. |
| 168 | private static final int MIN_FPS_THRESHOLD = 8000; |
| 169 | private static final int MIN_FPS_LOW_VALUE_WEIGHT = 1; |
| 170 | private static final int MIN_FPS_HIGH_VALUE_WEIGHT = 4; |
| 171 | |
| 172 | // Use one weight for small |value| less than |threshold|, and another weight above. |
| 173 | private int progressivePenalty(int value, int threshold, int lowWeight, int highWeight) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 174 | return (value < threshold) ? value * lowWeight |
| 175 | : threshold * lowWeight + (value - threshold) * highWeight; |
magjed | ce17e01 | 2016-06-01 00:43:59 -0700 | [diff] [blame] | 176 | } |
magjed | e38a936 | 2016-05-30 04:00:18 -0700 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | int diff(CaptureFormat.FramerateRange range) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 180 | final int minFpsError = progressivePenalty( |
| 181 | range.min, MIN_FPS_THRESHOLD, MIN_FPS_LOW_VALUE_WEIGHT, MIN_FPS_HIGH_VALUE_WEIGHT); |
magjed | ce17e01 | 2016-06-01 00:43:59 -0700 | [diff] [blame] | 182 | final int maxFpsError = progressivePenalty(Math.abs(requestedFps * 1000 - range.max), |
| 183 | MAX_FPS_DIFF_THRESHOLD, MAX_FPS_LOW_DIFF_WEIGHT, MAX_FPS_HIGH_DIFF_WEIGHT); |
| 184 | return minFpsError + maxFpsError; |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 185 | } |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 186 | }); |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 187 | } |
| 188 | |
sakal | e6c9e88 | 2016-06-17 01:02:04 -0700 | [diff] [blame] | 189 | public static Size getClosestSupportedSize( |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 190 | List<Size> supportedSizes, final int requestedWidth, final int requestedHeight) { |
| 191 | return Collections.min(supportedSizes, new ClosestComparator<Size>() { |
| 192 | @Override |
| 193 | int diff(Size size) { |
| 194 | return abs(requestedWidth - size.width) + abs(requestedHeight - size.height); |
| 195 | } |
| 196 | }); |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 197 | } |
sakal | 9d1315a | 2016-11-10 06:35:17 -0800 | [diff] [blame] | 198 | |
| 199 | // Helper method for camera classes. |
| 200 | static void reportCameraResolution(Histogram histogram, Size resolution) { |
| 201 | int index = COMMON_RESOLUTIONS.indexOf(resolution); |
| 202 | // 0 is reserved for unknown resolution, so add 1. |
| 203 | // indexOf returns -1 for unknown resolutions so it becomes 0 automatically. |
| 204 | histogram.addSample(index + 1); |
| 205 | } |
magjed | 6813ec8 | 2015-08-28 05:22:19 -0700 | [diff] [blame] | 206 | } |