summaryrefslogtreecommitdiff
path: root/modules/camera/3_4/static_properties.cpp
blob: 5be9dcdd543f10c6894c95a9a9c6e1cd393714ed (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * Copyright (C) 2016 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.
 */

#include "static_properties.h"

// #define LOG_NDEBUG 0
#define LOG_TAG "StaticProperties"
#include <cutils/log.h>
#include <hardware/camera3.h>
#include <system/camera.h>

#include "metadata/metadata_reader.h"

namespace default_camera_hal {

// Build stream capabilities from configs + stall durations.
static bool ConstructStreamCapabilities(
    const std::vector<StreamConfiguration>& configs,
    const std::vector<StreamStallDuration>& stalls,
    StaticProperties::CapabilitiesMap* capabilities) {
  // Extract directional capabilities from the configs.
  for (const auto& config : configs) {
    switch (config.direction) {
      case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT:
        (*capabilities)[config.spec].output_supported = true;
        break;
      case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT:
        (*capabilities)[config.spec].input_supported = true;
        break;
      default:
        // Should never happen when using the MetadataReader;
        // it should validate directions.
        ALOGE("%s: Unrecognized stream config direction %d.",
              __func__,
              config.direction);
        return false;
    }
  }

  // Extract stall durations from the stalls.
  for (const auto& stall : stalls) {
    (*capabilities)[stall.spec].stall_duration = stall.duration;
  }

  return true;
}

// Check that each output config has a valid corresponding stall duration
// (extra durations not matching any output config are ignored).
static bool ValidateStreamCapabilities(
    StaticProperties::CapabilitiesMap capabilities) {
  for (const auto& spec_capabilities : capabilities) {
    // Only non-negative stall durations are valid. This should only happen
    // due to output streams without an associated stall duration, as
    // MetadataReader validates the metadata stall durations.
    if (spec_capabilities.second.output_supported &&
        spec_capabilities.second.stall_duration < 0) {
      ALOGE(
          "%s: Static metadata does not have a stall duration for "
          "each output configuration. ",
          __func__);
      return false;
    }
  }
  return true;
}

// Validate that the input/output formats map matches up with
// the capabilities listed for all formats.
bool ValidateReprocessFormats(
    const StaticProperties::CapabilitiesMap& capabilities,
    const ReprocessFormatMap& reprocess_map) {
  // Get input formats.
  std::set<int32_t> all_input_formats;
  std::set<int32_t> all_output_formats;
  for (const auto& spec_capabilities : capabilities) {
    if (spec_capabilities.second.input_supported) {
      all_input_formats.insert(spec_capabilities.first.format);
    }
    if (spec_capabilities.second.output_supported) {
      all_output_formats.insert(spec_capabilities.first.format);
    }
  }

  // Must be at least one input format.
  if (all_input_formats.size() < 1) {
    ALOGE("%s: No input formats, reprocessing can't be supported.", __func__);
    return false;
  }

  // Check that the reprocess map input formats are exactly all available
  // input formats (check size here, then checking for actual value
  // matches will happen as part of the loop below).
  if (all_input_formats.size() != reprocess_map.size()) {
    ALOGE(
        "%s: Stream configuration input formats do not match "
        "input/output format map input formats.",
        __func__);
    return false;
  }

  // Check that each input format has at least one matching output format.
  for (const auto& input_format : all_input_formats) {
    const auto input_outputs_iterator = reprocess_map.find(input_format);
    if (input_outputs_iterator == reprocess_map.end()) {
      ALOGE(
          "%s: No output formats for input format %d.", __func__, input_format);
      return false;
    }
    // No need to check that the output formats vector is non-empty;
    // MetadataReader validates this. Instead just check that
    // all outputs are actually output formats.
    for (const auto& output_format : input_outputs_iterator->second) {
      if (all_output_formats.count(output_format) < 1) {
        ALOGE(
            "%s: Output format %d for input format %d "
            "is not a supported output format.",
            __func__,
            input_format,
            output_format);
        return false;
      }
    }
  }

  return true;
}

StaticProperties* StaticProperties::NewStaticProperties(
    std::unique_ptr<const MetadataReader> metadata_reader) {
  int facing = 0;
  int orientation = 0;
  int32_t max_input_streams = 0;
  int32_t max_raw_output_streams = 0;
  int32_t max_non_stalling_output_streams = 0;
  int32_t max_stalling_output_streams = 0;
  std::set<uint8_t> request_capabilities;
  std::vector<StreamConfiguration> configs;
  std::vector<StreamStallDuration> stalls;
  CapabilitiesMap stream_capabilities;
  ReprocessFormatMap reprocess_map;

  // If reading any data returns an error, something is wrong.
  if (metadata_reader->Facing(&facing) ||
      metadata_reader->Orientation(&orientation) ||
      metadata_reader->MaxInputStreams(&max_input_streams) ||
      metadata_reader->MaxOutputStreams(&max_raw_output_streams,
                                        &max_non_stalling_output_streams,
                                        &max_stalling_output_streams) ||
      metadata_reader->RequestCapabilities(&request_capabilities) ||
      metadata_reader->StreamConfigurations(&configs) ||
      metadata_reader->StreamStallDurations(&stalls) ||
      !ConstructStreamCapabilities(configs, stalls, &stream_capabilities) ||
      // MetadataReader validates configs and stall seperately,
      // but not that they match.
      !ValidateStreamCapabilities(stream_capabilities) ||
      // Reprocessing metadata only necessary if input streams are allowed.
      (max_input_streams > 0 &&
       (metadata_reader->ReprocessFormats(&reprocess_map) ||
        // MetadataReader validates configs and the reprocess map seperately,
        // but not that they match.
        !ValidateReprocessFormats(stream_capabilities, reprocess_map)))) {
    return nullptr;
  }

  return new StaticProperties(std::move(metadata_reader),
                              facing,
                              orientation,
                              max_input_streams,
                              max_raw_output_streams,
                              max_non_stalling_output_streams,
                              max_stalling_output_streams,
                              std::move(request_capabilities),
                              std::move(stream_capabilities),
                              std::move(reprocess_map));
}

StaticProperties::StaticProperties(
    std::unique_ptr<const MetadataReader> metadata_reader,
    int facing,
    int orientation,
    int32_t max_input_streams,
    int32_t max_raw_output_streams,
    int32_t max_non_stalling_output_streams,
    int32_t max_stalling_output_streams,
    std::set<uint8_t> request_capabilities,
    CapabilitiesMap stream_capabilities,
    ReprocessFormatMap supported_reprocess_outputs)
    : metadata_reader_(std::move(metadata_reader)),
      facing_(facing),
      orientation_(orientation),
      max_input_streams_(max_input_streams),
      max_raw_output_streams_(max_raw_output_streams),
      max_non_stalling_output_streams_(max_non_stalling_output_streams),
      max_stalling_output_streams_(max_stalling_output_streams),
      request_capabilities_(std::move(request_capabilities)),
      stream_capabilities_(std::move(stream_capabilities)),
      supported_reprocess_outputs_(std::move(supported_reprocess_outputs)) {}

bool StaticProperties::TemplateSupported(int type) {
  uint8_t required_capability = 0;
  switch (type) {
    case CAMERA3_TEMPLATE_PREVIEW:
      // Preview is always supported.
      return true;
    case CAMERA3_TEMPLATE_MANUAL:
      required_capability =
          ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR;
      break;
    case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
      required_capability =
          ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING;
      break;
    default:
      required_capability =
          ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE;
      return true;
  }

  return request_capabilities_.count(required_capability) > 0;
}

// Helper functions for checking stream properties when verifying support.
static bool IsInputType(int stream_type) {
  return stream_type == CAMERA3_STREAM_INPUT ||
         stream_type == CAMERA3_STREAM_BIDIRECTIONAL;
}

static bool IsOutputType(int stream_type) {
  return stream_type == CAMERA3_STREAM_OUTPUT ||
         stream_type == CAMERA3_STREAM_BIDIRECTIONAL;
}

static bool IsRawFormat(int format) {
  return format == HAL_PIXEL_FORMAT_RAW10 || format == HAL_PIXEL_FORMAT_RAW12 ||
         format == HAL_PIXEL_FORMAT_RAW16 ||
         format == HAL_PIXEL_FORMAT_RAW_OPAQUE;
}

bool StaticProperties::StreamConfigurationSupported(
    const camera3_stream_configuration_t* stream_config) {
  return SanityCheckStreamConfiguration(stream_config) &&
         InputStreamsSupported(stream_config) &&
         OutputStreamsSupported(stream_config) &&
         OperationModeSupported(stream_config);
}

bool StaticProperties::SanityCheckStreamConfiguration(
    const camera3_stream_configuration_t* stream_config) {
  // Check for null/empty values.
  if (stream_config == nullptr) {
    ALOGE("%s: NULL stream configuration array", __func__);
    return false;
  } else if (stream_config->num_streams == 0) {
    ALOGE("%s: Empty stream configuration array", __func__);
    return false;
  } else if (stream_config->streams == nullptr) {
    ALOGE("%s: NULL stream configuration streams", __func__);
    return false;
  }

  // Check that all streams are either inputs or outputs (or both).
  for (size_t i = 0; i < stream_config->num_streams; ++i) {
    const camera3_stream_t* stream = stream_config->streams[i];
    if (stream == nullptr) {
      ALOGE("%s: Stream %d is null", __func__, i);
      return false;
    } else if (!IsInputType(stream->stream_type) &&
               !IsOutputType(stream->stream_type)) {
      ALOGE("%s: Stream %d type %d is neither an input nor an output type",
            __func__,
            i,
            stream->stream_type);
      return false;
    }
  }

  return true;
}

bool StaticProperties::InputStreamsSupported(
    const camera3_stream_configuration_t* stream_config) {
  // Find the input stream(s).
  size_t num_input_streams = 0;
  int input_format = -1;
  for (size_t i = 0; i < stream_config->num_streams; ++i) {
    const camera3_stream_t* stream = stream_config->streams[i];
    if (IsInputType(stream->stream_type)) {
      // Check that this stream is valid as an input.
      const auto capabilities_iterator = stream_capabilities_.find(stream);
      if (capabilities_iterator == stream_capabilities_.end() ||
          !capabilities_iterator->second.input_supported) {
        ALOGE("%s: %d x %d stream of format %d is not a supported input setup.",
              __func__,
              stream->width,
              stream->height,
              stream->format);
        return false;
      }

      // Valid input stream; count it.
      ++num_input_streams;
      input_format = stream->format;
    }
  }

  // Check the count.
  if (num_input_streams > max_input_streams_) {
    ALOGE(
        "%s: Requested number of input streams %d is greater than "
        "the maximum number supported by the device (%d).",
        __func__,
        num_input_streams,
        max_input_streams_);
    return false;
  }
  if (num_input_streams > 1) {
    ALOGE("%s: Camera HAL 3.4 only supports 1 input stream max.", __func__);
    return false;
  }

  // If there's an input stream, the configuration must have at least one
  // supported output format for reprocessing that input.
  if (num_input_streams > 0) {
    const auto input_output_formats_iterator =
        supported_reprocess_outputs_.find(input_format);
    if (input_output_formats_iterator == supported_reprocess_outputs_.end()) {
      // Should never happen; factory should verify that all valid inputs
      // have one or more valid outputs.
      ALOGE("%s: No valid output formats for input format %d.",
            __func__,
            input_format);
      return false;
    }
    bool match_found = false;
    // Go through outputs looking for a supported one.
    for (size_t i = 0; i < stream_config->num_streams; ++i) {
      const camera3_stream_t* stream = stream_config->streams[i];
      if (IsOutputType(stream->stream_type)) {
        if (input_output_formats_iterator->second.count(stream->format) > 0) {
          match_found = true;
          break;
        }
      }
    }
    if (!match_found) {
      ALOGE("%s: No supported output format provided for input format %d.",
            __func__,
            input_format);
      return false;
    }
  }

  return true;
}

bool StaticProperties::OutputStreamsSupported(
    const camera3_stream_configuration_t* stream_config) {
  // Find and count output streams.
  size_t num_raw = 0;
  size_t num_stalling = 0;
  size_t num_non_stalling = 0;
  for (int i = 0; i < stream_config->num_streams; ++i) {
    const camera3_stream_t* stream = stream_config->streams[i];
    if (IsOutputType(stream->stream_type)) {
      // Check that this stream is valid as an output.
      const auto capabilities_iterator = stream_capabilities_.find(stream);
      if (capabilities_iterator == stream_capabilities_.end() ||
          !capabilities_iterator->second.output_supported) {
        ALOGE(
            "%s: %d x %d stream of format %d "
            "is not a supported output setup.",
            __func__,
            stream->width,
            stream->height,
            stream->format);
        return false;
      }

      // Valid output; count it.
      if (IsRawFormat(stream->format)) {
        ++num_raw;
      } else if (capabilities_iterator->second.stall_duration > 0) {
        ++num_stalling;
      } else {
        ++num_non_stalling;
      }
    }
  }

  // Check that the counts are within bounds.
  if (num_raw > max_raw_output_streams_) {
    ALOGE(
        "%s: Requested stream configuration exceeds maximum supported "
        "raw output streams %d (requested %d).",
        __func__,
        max_raw_output_streams_,
        num_raw);
    return false;
  } else if (num_stalling > max_stalling_output_streams_) {
    ALOGE(
        "%s: Requested stream configuration exceeds maximum supported "
        "stalling output streams %d (requested %d).",
        __func__,
        max_stalling_output_streams_,
        num_stalling);
    return false;
  } else if (num_non_stalling > max_non_stalling_output_streams_) {
    ALOGE(
        "%s: Requested stream configuration exceeds maximum supported "
        "non-stalling output streams %d (requested %d).",
        __func__,
        max_non_stalling_output_streams_,
        num_non_stalling);
    return false;
  }

  return true;
}

bool StaticProperties::OperationModeSupported(
    const camera3_stream_configuration_t* stream_config) {
  switch (stream_config->operation_mode) {
    case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
      return true;
    case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
      // TODO(b/31370792): Check metadata for high speed support,
      // check that requested streams have support for high speed.
      ALOGE("%s: Support for CONSTRAINED_HIGH_SPEED not implemented", __func__);
      return false;
    default:
      ALOGE("%s: Unrecognized stream configuration mode: %d",
            __func__,
            stream_config->operation_mode);
      return false;
  }
}

bool StaticProperties::ReprocessingSupported(
    const camera3_stream_t* input_stream,
    const std::set<const camera3_stream_t*>& output_streams) {
  // There must be an input.
  if (!input_stream) {
    ALOGE("%s: No input stream.", __func__);
    return false;
  }
  // There must be an output.
  if (output_streams.size() < 1) {
    ALOGE("%s: No output stream.", __func__);
    return false;
  }

  const auto input_output_formats =
      supported_reprocess_outputs_.find(input_stream->format);
  if (input_output_formats == supported_reprocess_outputs_.end()) {
    // Should never happen for a valid input stream.
    ALOGE("%s: Input format %d does not support any output formats.",
          __func__,
          input_stream->format);
    return false;
  }

  // Check that all output streams can be outputs for the input stream.
  const std::set<int32_t>& supported_output_formats =
      input_output_formats->second;
  for (const auto output_stream : output_streams) {
    if (supported_output_formats.count(output_stream->format) < 1) {
      ALOGE(
          "%s: Output format %d is not a supported output "
          "for request input format %d.",
          __func__,
          output_stream->format,
          input_stream->format);
      return false;
    }
  }

  return true;
}

}  // namespace default_camera_hal