1// Copyright 2013 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 "media/cdm/ppapi/external_clear_key/fake_cdm_video_decoder.h"
6
7#include "base/logging.h"
8
9namespace media {
10
11FakeCdmVideoDecoder::FakeCdmVideoDecoder(cdm::Host* host)
12    : is_initialized_(false),
13      host_(host) {
14}
15
16FakeCdmVideoDecoder::~FakeCdmVideoDecoder() {
17  Deinitialize();
18}
19
20bool FakeCdmVideoDecoder::Initialize(const cdm::VideoDecoderConfig& config) {
21  DVLOG(1) << "Initialize()";
22
23  video_size_ = config.coded_size;
24  is_initialized_ = true;
25  return true;
26}
27
28void FakeCdmVideoDecoder::Deinitialize() {
29  DVLOG(1) << "Deinitialize()";
30  is_initialized_ = false;
31}
32
33void FakeCdmVideoDecoder::Reset() {
34  DVLOG(1) << "Reset()";
35}
36
37// Creates a YV12 video frame.
38cdm::Status FakeCdmVideoDecoder::DecodeFrame(const uint8_t* compressed_frame,
39                                             int32_t compressed_frame_size,
40                                             int64_t timestamp,
41                                             cdm::VideoFrame* decoded_frame) {
42  DVLOG(1) << "DecodeFrame()";
43
44  // The fake decoder does not buffer any frames internally. So if the input is
45  // empty (EOS), just return kNeedMoreData.
46  if (!decoded_frame)
47    return cdm::kNeedMoreData;
48
49  // Choose non-zero alignment and padding on purpose for testing.
50  const int kAlignment = 8;
51  const int kPadding = 16;
52  const int kPlanePadding = 128;
53
54  int width = video_size_.width;
55  int height = video_size_.height;
56  DCHECK_EQ(width % 2, 0);
57  DCHECK_EQ(height % 2, 0);
58
59  int y_stride = (width + kAlignment - 1) / kAlignment * kAlignment + kPadding;
60  int uv_stride =
61      (width / 2 + kAlignment - 1) / kAlignment * kAlignment + kPadding;
62  int y_rows = height;
63  int uv_rows = height / 2;
64  int y_offset = 0;
65  int v_offset = y_stride * y_rows + kPlanePadding;
66  int u_offset = v_offset + uv_stride * uv_rows + kPlanePadding;
67  int frame_size = u_offset + uv_stride * uv_rows + kPlanePadding;
68
69  decoded_frame->SetFrameBuffer(host_->Allocate(frame_size));
70  decoded_frame->FrameBuffer()->SetSize(frame_size);
71
72  decoded_frame->SetFormat(cdm::kYv12);
73  decoded_frame->SetSize(video_size_);
74  decoded_frame->SetPlaneOffset(cdm::VideoFrame::kYPlane, y_offset);
75  decoded_frame->SetPlaneOffset(cdm::VideoFrame::kVPlane, v_offset);
76  decoded_frame->SetPlaneOffset(cdm::VideoFrame::kUPlane, u_offset);
77  decoded_frame->SetStride(cdm::VideoFrame::kYPlane, y_stride);
78  decoded_frame->SetStride(cdm::VideoFrame::kVPlane, uv_stride);
79  decoded_frame->SetStride(cdm::VideoFrame::kUPlane, uv_stride);
80  decoded_frame->SetTimestamp(timestamp);
81
82  static unsigned char color = 0;
83  color += 10;
84
85  memset(reinterpret_cast<void*>(decoded_frame->FrameBuffer()->Data()),
86         color, frame_size);
87
88  return cdm::kSuccess;
89}
90
91}  // namespace media
92