1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <linux/videodev2.h>
6
7#include "base/numerics/safe_conversions.h"
8#include "content/common/gpu/media/exynos_v4l2_video_device.h"
9#include "content/common/gpu/media/tegra_v4l2_video_device.h"
10
11namespace content {
12
13V4L2Device::~V4L2Device() {}
14
15// static
16scoped_ptr<V4L2Device> V4L2Device::Create(Type type) {
17  DVLOG(3) << __PRETTY_FUNCTION__;
18
19  scoped_ptr<ExynosV4L2Device> exynos_device(new ExynosV4L2Device(type));
20  if (exynos_device->Initialize())
21    return exynos_device.PassAs<V4L2Device>();
22
23  scoped_ptr<TegraV4L2Device> tegra_device(new TegraV4L2Device(type));
24  if (tegra_device->Initialize())
25    return tegra_device.PassAs<V4L2Device>();
26
27  DLOG(ERROR) << "Failed to create V4L2Device";
28  return scoped_ptr<V4L2Device>();
29}
30
31// static
32media::VideoFrame::Format V4L2Device::V4L2PixFmtToVideoFrameFormat(
33    uint32 pix_fmt) {
34  switch (pix_fmt) {
35    case V4L2_PIX_FMT_NV12:
36    case V4L2_PIX_FMT_NV12M:
37      return media::VideoFrame::NV12;
38
39    case V4L2_PIX_FMT_YUV420:
40    case V4L2_PIX_FMT_YUV420M:
41      return media::VideoFrame::I420;
42
43    default:
44      LOG(FATAL) << "Add more cases as needed";
45      return media::VideoFrame::UNKNOWN;
46  }
47}
48
49// static
50uint32 V4L2Device::VideoFrameFormatToV4L2PixFmt(
51    media::VideoFrame::Format format) {
52  switch (format) {
53    case media::VideoFrame::NV12:
54      return V4L2_PIX_FMT_NV12M;
55
56    case media::VideoFrame::I420:
57      return V4L2_PIX_FMT_YUV420M;
58
59    default:
60      LOG(FATAL) << "Add more cases as needed";
61      return 0;
62  }
63}
64
65// static
66uint32 V4L2Device::VideoCodecProfileToV4L2PixFmt(
67    media::VideoCodecProfile profile) {
68  if (profile >= media::H264PROFILE_MIN &&
69      profile <= media::H264PROFILE_MAX) {
70    return V4L2_PIX_FMT_H264;
71  } else if (profile >= media::VP8PROFILE_MIN &&
72             profile <= media::VP8PROFILE_MAX) {
73    return V4L2_PIX_FMT_VP8;
74  } else {
75    LOG(FATAL) << "Add more cases as needed";
76    return 0;
77  }
78}
79
80// static
81gfx::Size V4L2Device::CodedSizeFromV4L2Format(struct v4l2_format format) {
82  gfx::Size coded_size;
83  gfx::Size visible_size;
84  media::VideoFrame::Format frame_format = media::VideoFrame::UNKNOWN;
85  int bytesperline = 0;
86  int sizeimage = 0;
87
88  if (V4L2_TYPE_IS_MULTIPLANAR(format.type) &&
89      format.fmt.pix_mp.num_planes > 0) {
90    bytesperline =
91        base::checked_cast<int>(format.fmt.pix_mp.plane_fmt[0].bytesperline);
92    sizeimage =
93        base::checked_cast<int>(format.fmt.pix_mp.plane_fmt[0].sizeimage);
94    visible_size.SetSize(base::checked_cast<int>(format.fmt.pix_mp.width),
95                         base::checked_cast<int>(format.fmt.pix_mp.height));
96    frame_format =
97        V4L2Device::V4L2PixFmtToVideoFrameFormat(format.fmt.pix_mp.pixelformat);
98  } else {
99    bytesperline = base::checked_cast<int>(format.fmt.pix.bytesperline);
100    sizeimage = base::checked_cast<int>(format.fmt.pix.sizeimage);
101    visible_size.SetSize(base::checked_cast<int>(format.fmt.pix.width),
102                         base::checked_cast<int>(format.fmt.pix.height));
103    frame_format =
104        V4L2Device::V4L2PixFmtToVideoFrameFormat(format.fmt.pix.pixelformat);
105  }
106
107  int horiz_bpp =
108      media::VideoFrame::PlaneHorizontalBitsPerPixel(frame_format, 0);
109  DVLOG(3) << __func__ << ": bytesperline=" << bytesperline
110           << ", sizeimage=" << sizeimage
111           << ", visible_size=" << visible_size.ToString() << ", frame_format="
112           << media::VideoFrame::FormatToString(frame_format)
113           << ", horiz_bpp=" << horiz_bpp;
114  if (sizeimage == 0 || bytesperline == 0 || horiz_bpp == 0 ||
115      (bytesperline * 8) % horiz_bpp != 0) {
116    DLOG(ERROR) << "Invalid format provided";
117    return coded_size;
118  }
119
120  // Round up sizeimage to full bytesperlines. sizeimage does not have to be
121  // a multiple of bytesperline, as in V4L2 terms it's just a byte size of
122  // the buffer, unrelated to its payload.
123  sizeimage = ((sizeimage + bytesperline - 1) / bytesperline) * bytesperline;
124
125  coded_size.SetSize(bytesperline * 8 / horiz_bpp, sizeimage / bytesperline);
126  DVLOG(3) << "coded_size=" << coded_size.ToString();
127
128  // Sanity checks. Calculated coded size has to contain given visible size
129  // and fulfill buffer byte size requirements for each plane.
130  DCHECK(gfx::Rect(coded_size).Contains(gfx::Rect(visible_size)));
131
132  if (V4L2_TYPE_IS_MULTIPLANAR(format.type)) {
133    for (size_t i = 0; i < format.fmt.pix_mp.num_planes; ++i) {
134      DCHECK_EQ(format.fmt.pix_mp.plane_fmt[i].bytesperline,
135                base::checked_cast<__u32>(media::VideoFrame::RowBytes(
136                    i, frame_format, coded_size.width())));
137    }
138  }
139
140  return coded_size;
141}
142
143}  //  namespace content
144