summaryrefslogtreecommitdiff
path: root/core/java/android/hardware/camera2/params/DynamicRangeProfiles.java
blob: 34c83366e42c61e2201d18f9488b16e42ee4cfbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.camera2.params;

import android.annotation.LongDef;
import android.annotation.NonNull;

import android.hardware.camera2.CameraMetadata;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Immutable class with information about supported 10-bit dynamic range profiles.
 *
 * <p>An instance of this class can be queried by retrieving the value of
 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES}.
 * </p>
 *
 * <p>All camera devices supporting the
 * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT}
 * capability must advertise the supported 10-bit dynamic range profiles in
 * {@link #getSupportedProfiles}</p>
 *
 * <p>Some devices may not be able to support 8-bit and/or 10-bit output with different dynamic
 * range profiles within the same capture request. Such device specific constraints can be queried
 * by calling {@link #getProfileCaptureRequestConstraints}. Do note that unsupported
 * combinations will result in {@link IllegalArgumentException} when trying to submit a capture
 * request. Capture requests that only reference outputs configured using the same dynamic range
 * profile value will never fail due to such constraints.</p>
 *
 * @see OutputConfiguration#setDynamicRangeProfile
 */
public final class DynamicRangeProfiles {
    /**
     * This the default 8-bit standard profile that will be used in case where camera clients do not
     * explicitly configure a supported dynamic range profile by calling
     * {@link OutputConfiguration#setDynamicRangeProfile}.
     */
    public static final long STANDARD =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;

    /**
     * 10-bit pixel samples encoded using the Hybrid log-gamma transfer function
     *
     * <p>All 10-bit output capable devices are required to support this profile.</p>
     */
    public static final long HLG10  =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HLG10;

    /**
     * 10-bit pixel samples encoded using the SMPTE ST 2084 transfer function.
     *
     * <p>This profile utilizes internal static metadata to increase the quality
     * of the capture.</p>
     */
    public static final long HDR10  =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10;

    /**
     * 10-bit pixel samples encoded using the SMPTE ST 2084 transfer function.
     *
     * <p>In contrast to HDR10, this profile uses internal per-frame metadata
     * to further enhance the quality of the capture.</p>
     */
    public static final long HDR10_PLUS =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10_PLUS;

    /**
     * <p>This is a camera mode for Dolby Vision capture optimized for a more scene
     * accurate capture. This would typically differ from what a specific device
     * might want to tune for a consumer optimized Dolby Vision general capture.</p>
     */
    public static final long DOLBY_VISION_10B_HDR_REF =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_REF;

    /**
     * <p>This is the power optimized mode for 10-bit Dolby Vision HDR Reference Mode.</p>
     */
    public static final long DOLBY_VISION_10B_HDR_REF_PO =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_REF_PO;

    /**
     * <p>This is the camera mode for the default Dolby Vision capture mode for the
     * specific device. This would be tuned by each specific device for consumer
     * pleasing results that resonate with their particular audience. We expect
     * that each specific device would have a different look for their default
     * Dolby Vision capture.</p>
     */
    public static final long DOLBY_VISION_10B_HDR_OEM =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_OEM;

    /**
     * <p>This is the power optimized mode for 10-bit Dolby Vision HDR device specific capture
     * Mode.</p>
     */
    public static final long DOLBY_VISION_10B_HDR_OEM_PO =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_OEM_PO;

    /**
     * <p>This is the 8-bit version of the Dolby Vision reference capture mode optimized
     * for scene accuracy.</p>
     */
    public static final long DOLBY_VISION_8B_HDR_REF =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_REF;

    /**
     * <p>This is the power optimized mode for 8-bit Dolby Vision HDR Reference Mode.</p>
     */
    public static final long DOLBY_VISION_8B_HDR_REF_PO =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_REF_PO;

    /**
     * <p>This is the 8-bit version of device specific tuned and optimized Dolby Vision
     * capture mode.</p>
     */
    public static final long DOLBY_VISION_8B_HDR_OEM =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_OEM;

    /**
     * <p>This is the power optimized mode for 8-bit Dolby Vision HDR device specific
     * capture Mode.</p>
     */
    public static final long DOLBY_VISION_8B_HDR_OEM_PO =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_OEM_PO;

    /*
     * @hide
     */
    public static final long PUBLIC_MAX =
            CameraMetadata.REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX;

     /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @LongDef(prefix = {"PROFILE_"}, value =
            {STANDARD,
             HLG10,
             HDR10,
             HDR10_PLUS,
             DOLBY_VISION_10B_HDR_REF,
             DOLBY_VISION_10B_HDR_REF_PO,
             DOLBY_VISION_10B_HDR_OEM,
             DOLBY_VISION_10B_HDR_OEM_PO,
             DOLBY_VISION_8B_HDR_REF,
             DOLBY_VISION_8B_HDR_REF_PO,
             DOLBY_VISION_8B_HDR_OEM,
             DOLBY_VISION_8B_HDR_OEM_PO})
    public @interface Profile {
    }

    private final HashMap<Long, Set<Long>> mProfileMap = new HashMap<>();
    private final HashMap<Long, Boolean> mLookahedLatencyMap = new HashMap<>();

    /**
     * Create a new immutable DynamicRangeProfiles instance.
     *
     * <p>This constructor takes over the array; do not write to the array afterwards.</p>
     *
     * <p>Do note that the constructor is available for testing purposes only!
     * Camera clients must always retrieve the value of
     * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES}.
     * for a given camera id in order to retrieve the device capabilities.</p>
     *
     * @param elements
     *          An array of elements describing the map. It contains two elements per entry which
     *          describe the supported dynamic range profile value in the first element and in the
     *          second element a bitmap of concurrently supported dynamic range profiles within the
     *          same capture request. Bitmap values of 0 indicate that there are no constraints.
     *
     * @throws IllegalArgumentException
     *            if the {@code elements} array length is invalid, not divisible by 2 or contains
     *            invalid element values
     * @throws NullPointerException
     *            if {@code elements} is {@code null}
     *
     */
    public DynamicRangeProfiles(@NonNull final long[] elements) {
        if ((elements.length % 3) != 0) {
            throw new IllegalArgumentException("Dynamic range profile map length " +
                    elements.length + " is not even!");
        }

        for (int i = 0; i < elements.length; i += 3) {
            checkProfileValue(elements[i]);
            // STANDARD is not expected to be included
            if (elements[i] == STANDARD) {
                throw new IllegalArgumentException("Dynamic range profile map must not include a"
                        + " STANDARD profile entry!");
            }
            HashSet<Long> profiles = new HashSet<>();

            if (elements[i+1] != 0) {
                for (long profile = STANDARD; profile < PUBLIC_MAX; profile <<= 1) {
                    if ((elements[i+1] & profile) != 0) {
                        profiles.add(profile);
                    }
                }
            }

            mProfileMap.put(elements[i], profiles);
            mLookahedLatencyMap.put(elements[i], elements[i+2] != 0L);
        }

        // Build the STANDARD constraints depending on the advertised 10-bit limitations
        HashSet<Long> standardConstraints = new HashSet<>();
        standardConstraints.add(STANDARD);
        for(Long profile : mProfileMap.keySet()) {
            if (mProfileMap.get(profile).isEmpty() || mProfileMap.get(profile).contains(STANDARD)) {
                standardConstraints.add(profile);
            }
        }

        mProfileMap.put(STANDARD, standardConstraints);
        mLookahedLatencyMap.put(STANDARD, false);
    }


    /**
     * @hide
     */
    public static void checkProfileValue(long profile) {
        if (profile == STANDARD || profile == HLG10 || profile == HDR10 || profile == HDR10_PLUS
                || profile == DOLBY_VISION_10B_HDR_REF || profile == DOLBY_VISION_10B_HDR_REF_PO
                || profile == DOLBY_VISION_10B_HDR_OEM || profile == DOLBY_VISION_10B_HDR_OEM_PO
                || profile == DOLBY_VISION_8B_HDR_REF || profile == DOLBY_VISION_8B_HDR_REF_PO
                || profile == DOLBY_VISION_8B_HDR_OEM
                || profile == DOLBY_VISION_8B_HDR_OEM_PO) {//No-op
        } else {
            throw new IllegalArgumentException("Unknown profile " + profile);
        }
    }

    /**
     * Return a set of supported dynamic range profiles.
     *
     * @return non-modifiable set of dynamic range profiles
     */
     public @NonNull Set<Long> getSupportedProfiles() {
         return Collections.unmodifiableSet(mProfileMap.keySet());
     }

    /**
     * Return a list of supported dynamic range profiles that
     * can be referenced in a single capture request along with a given
     * profile.
     *
     * <p>For example if assume that a particular 10-bit output capable device
     * returns ({@link #STANDARD}, {@link #HLG10}, {@link #HDR10}) as result from calling
     * {@link #getSupportedProfiles()} and {@link #getProfileCaptureRequestConstraints}
     * returns ({@link #STANDARD}, {@link #HLG10}) when given an argument of {@link #STANDARD}.
     * This means that the corresponding camera device will only accept and process capture requests
     * that reference outputs configured using {@link #HDR10} dynamic profile or alternatively
     * some combination of {@link #STANDARD} and {@link #HLG10}. However trying to
     * queue capture requests to outputs that reference both {@link #HDR10} and
     * {@link #STANDARD}/{@link #HLG10} will result in {@link IllegalArgumentException}.</p>
     *
     * <p>The list will be empty in case there are no constraints for the given
     * profile.</p>
     *
     * @return non-modifiable set of dynamic range profiles
     * @throws IllegalArgumentException   If the profile argument is not
     *                                    within the list returned by
     *                                    getSupportedProfiles()
     *
     * @see OutputConfiguration#setDynamicRangeProfile
     */
     public @NonNull Set<Long> getProfileCaptureRequestConstraints(@Profile long profile) {
         Set<Long> ret = mProfileMap.get(profile);
         if (ret == null) {
             throw new IllegalArgumentException("Unsupported profile!");
         }

         return Collections.unmodifiableSet(ret);
     }

    /**
     * Check whether a given dynamic range profile is suitable for latency sensitive use cases.
     *
     * <p>Due to internal lookahead logic, camera outputs configured with some dynamic range
     * profiles may experience additional latency greater than 3 buffers. Using camera outputs
     * with such profiles for latency sensitive use cases such as camera preview is not
     * recommended. Profiles that have such extra streaming delay are typically utilized for
     * scenarios such as offscreen video recording.</p>
     *
     * @return true if the given profile is not suitable for latency sensitive use cases, false
     *         otherwise
     * @throws IllegalArgumentException   If the profile argument is not
     *                                    within the list returned by
     *                                    getSupportedProfiles()
     *
     * @see OutputConfiguration#setDynamicRangeProfile
     */
    public boolean isExtraLatencyPresent(@Profile long profile) {
        Boolean ret = mLookahedLatencyMap.get(profile);
        if (ret == null) {
            throw new IllegalArgumentException("Unsupported profile!");
        }

        return ret;
    }
}