summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/chromeos/pixel_format_utils.cc
blob: 73237bc7569f20482fbf5194613ab171105ba3d2 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/capture/video/chromeos/pixel_format_utils.h"

#include <drm_fourcc.h>

namespace media {

namespace {

struct SupportedFormat {
  cros::mojom::HalPixelFormat hal_format;
  ChromiumPixelFormat cr_format;
} const kSupportedFormats[] = {
    // The Android camera HAL v3 has three types of mandatory pixel formats:
    //
    //   1. HAL_PIXEL_FORMAT_YCbCr_420_888 (YUV flexible format).
    //   2. HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED (platform-specific format).
    //   3. HAL_PIXEL_FORMAT_BLOB (for JPEG).
    //
    // We can't use HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED as it is highly
    // platform specific and there is no way for Chrome to query the exact
    // pixel layout of the implementation-defined buffer.
    //
    // On Android the framework requests the preview stream with the
    // implementation-defined format, and as a result some camera HALs support
    // only implementation-defined preview buffers.  We should use the video
    // capture stream in Chrome VCD as it is mandatory for the camera HAL to
    // support YUV flexbile format video streams.
    {cros::mojom::HalPixelFormat::HAL_PIXEL_FORMAT_YCbCr_420_888,
     {PIXEL_FORMAT_NV12, gfx::BufferFormat::YUV_420_BIPLANAR}},
    // FIXME(jcliang): MJPEG is not accurate; we should have BLOB or JPEG
    {cros::mojom::HalPixelFormat::HAL_PIXEL_FORMAT_BLOB,
     {PIXEL_FORMAT_MJPEG, gfx::BufferFormat::R_8}},
    // Add more mappings when we have more devices.
};

}  // namespace

std::vector<ChromiumPixelFormat> PixFormatHalToChromium(
    cros::mojom::HalPixelFormat from) {
  std::vector<ChromiumPixelFormat> ret;
  for (const auto& it : kSupportedFormats) {
    if (it.hal_format == from) {
      ret.push_back(it.cr_format);
    }
  }
  return ret;
}

uint32_t PixFormatVideoToDrm(VideoPixelFormat from) {
  switch (from) {
    case PIXEL_FORMAT_NV12:
      return DRM_FORMAT_NV12;
    case PIXEL_FORMAT_MJPEG:
      return DRM_FORMAT_R8;
    default:
      // Unsupported format.
      return 0;
  }
}

absl::optional<gfx::BufferFormat> PixFormatVideoToGfx(
    VideoPixelFormat pixel_format) {
  switch (pixel_format) {
    case PIXEL_FORMAT_MJPEG:
      return gfx::BufferFormat::R_8;
    case PIXEL_FORMAT_NV12:
      return gfx::BufferFormat::YUV_420_BIPLANAR;
    default:
      return absl::nullopt;
  }
}

}  // namespace media