OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/base/decoder_vp8.h" | 5 #include "remoting/base/decoder_vp8.h" |
6 | 6 |
7 #include "media/base/media.h" | 7 #include "media/base/media.h" |
8 #include "media/base/yuv_convert.h" | 8 #include "media/base/yuv_convert.h" |
9 #include "remoting/base/util.h" | 9 #include "remoting/base/util.h" |
10 | 10 |
11 extern "C" { | 11 extern "C" { |
12 #define VPX_CODEC_DISABLE_COMPAT 1 | 12 #define VPX_CODEC_DISABLE_COMPAT 1 |
13 #include "third_party/libvpx/include/vpx/vpx_codec.h" | 13 #include "third_party/libvpx/include/vpx/vpx_codec.h" |
14 #include "third_party/libvpx/include/vpx/vpx_decoder.h" | 14 #include "third_party/libvpx/include/vpx/vpx_decoder.h" |
15 #include "third_party/libvpx/include/vpx/vp8dx.h" | 15 #include "third_party/libvpx/include/vpx/vp8dx.h" |
16 } | 16 } |
17 | 17 |
18 namespace remoting { | 18 namespace remoting { |
19 | 19 |
20 DecoderVp8::DecoderVp8() | 20 DecoderVp8::DecoderVp8() |
21 : state_(kUninitialized), | 21 : state_(kWaitingForBeginRect), |
| 22 rect_x_(0), |
| 23 rect_y_(0), |
| 24 rect_width_(0), |
| 25 rect_height_(0), |
| 26 updated_rects_(NULL), |
22 codec_(NULL) { | 27 codec_(NULL) { |
23 } | 28 } |
24 | 29 |
25 DecoderVp8::~DecoderVp8() { | 30 DecoderVp8::~DecoderVp8() { |
26 if (codec_) { | 31 if (codec_) { |
27 vpx_codec_err_t ret = vpx_codec_destroy(codec_); | 32 vpx_codec_err_t ret = vpx_codec_destroy(codec_); |
28 CHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec"; | 33 CHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec"; |
29 } | 34 } |
30 delete codec_; | 35 delete codec_; |
31 } | 36 } |
32 | 37 |
33 void DecoderVp8::Initialize(scoped_refptr<media::VideoFrame> frame, | 38 bool DecoderVp8::BeginDecode(scoped_refptr<media::VideoFrame> frame, |
34 const gfx::Rect& clip, int bytes_per_src_pixel) { | 39 UpdatedRects* updated_rects, |
35 DCHECK_EQ(kUninitialized, state_); | 40 Task* partial_decode_done, |
| 41 Task* decode_done) { |
| 42 DCHECK(!partial_decode_done_.get()); |
| 43 DCHECK(!decode_done_.get()); |
| 44 DCHECK(!updated_rects_); |
| 45 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 46 |
| 47 partial_decode_done_.reset(partial_decode_done); |
| 48 decode_done_.reset(decode_done); |
| 49 updated_rects_ = updated_rects; |
36 | 50 |
37 if (frame->format() != media::VideoFrame::RGB32) { | 51 if (frame->format() != media::VideoFrame::RGB32) { |
38 LOG(INFO) << "DecoderVp8 only supports RGB32 as output"; | 52 LOG(INFO) << "DecoderVp8 only supports RGB32 as output"; |
39 state_ = kError; | 53 return false; |
40 return; | |
41 } | 54 } |
42 frame_ = frame; | 55 frame_ = frame; |
43 | 56 return true; |
44 state_ = kReady; | |
45 } | 57 } |
46 | 58 |
47 void DecoderVp8::DecodeBytes(const std::string& encoded_bytes) { | 59 bool DecoderVp8::PartialDecode(ChromotingHostMessage* message) { |
48 DCHECK_EQ(kReady, state_); | 60 scoped_ptr<ChromotingHostMessage> msg_deleter(message); |
| 61 DCHECK(message->has_update_stream_packet()); |
| 62 |
| 63 bool ret = true; |
| 64 if (message->update_stream_packet().has_begin_rect()) |
| 65 ret = HandleBeginRect(message); |
| 66 if (ret && message->update_stream_packet().has_rect_data()) |
| 67 ret = HandleRectData(message); |
| 68 if (ret && message->update_stream_packet().has_end_rect()) |
| 69 ret = HandleEndRect(message); |
| 70 return ret; |
| 71 } |
| 72 |
| 73 void DecoderVp8::EndDecode() { |
| 74 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 75 decode_done_->Run(); |
| 76 |
| 77 partial_decode_done_.reset(); |
| 78 decode_done_.reset(); |
| 79 frame_ = NULL; |
| 80 updated_rects_ = NULL; |
| 81 } |
| 82 |
| 83 bool DecoderVp8::HandleBeginRect(ChromotingHostMessage* message) { |
| 84 DCHECK_EQ(kWaitingForBeginRect, state_); |
| 85 state_ = kWaitingForRectData; |
| 86 |
| 87 rect_width_ = message->update_stream_packet().begin_rect().width(); |
| 88 rect_height_ = message->update_stream_packet().begin_rect().height(); |
| 89 rect_x_ = message->update_stream_packet().begin_rect().x(); |
| 90 rect_y_ = message->update_stream_packet().begin_rect().y(); |
| 91 |
| 92 PixelFormat pixel_format = |
| 93 message->update_stream_packet().begin_rect().pixel_format(); |
| 94 if (pixel_format != PixelFormatYv12) |
| 95 return false; |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool DecoderVp8::HandleRectData(ChromotingHostMessage* message) { |
| 100 DCHECK_EQ(kWaitingForRectData, state_); |
| 101 DCHECK_EQ(0, |
| 102 message->update_stream_packet().rect_data().sequence_number()); |
49 | 103 |
50 // Initialize the codec as needed. | 104 // Initialize the codec as needed. |
51 if (!codec_) { | 105 if (!codec_) { |
52 codec_ = new vpx_codec_ctx_t(); | 106 codec_ = new vpx_codec_ctx_t(); |
53 vpx_codec_err_t ret = | 107 vpx_codec_err_t ret = |
54 vpx_codec_dec_init( | 108 vpx_codec_dec_init( |
55 codec_, | 109 codec_, |
56 (const vpx_codec_iface_t*)media::GetVp8DxAlgoAddress(), NULL, 0); | 110 (const vpx_codec_iface_t*)media::GetVp8DxAlgoAddress(), NULL, 0); |
57 if (ret != VPX_CODEC_OK) { | 111 if (ret != VPX_CODEC_OK) { |
58 LOG(INFO) << "Cannot initialize codec."; | 112 LOG(INFO) << "Cannot initialize codec."; |
59 delete codec_; | 113 delete codec_; |
60 codec_ = NULL; | 114 codec_ = NULL; |
61 state_ = kError; | 115 return false; |
62 return; | |
63 } | 116 } |
64 } | 117 } |
65 | 118 |
66 LOG(WARNING) << "Decoding " << encoded_bytes.size(); | |
67 | |
68 // Do the actual decoding. | 119 // Do the actual decoding. |
69 vpx_codec_err_t ret = vpx_codec_decode( | 120 vpx_codec_err_t ret = vpx_codec_decode( |
70 codec_, reinterpret_cast<const uint8*>(encoded_bytes.data()), | 121 codec_, |
71 encoded_bytes.size(), NULL, 0); | 122 (uint8_t*)message->update_stream_packet().rect_data().data().c_str(), |
| 123 message->update_stream_packet().rect_data().data().size(), |
| 124 NULL, 0); |
72 if (ret != VPX_CODEC_OK) { | 125 if (ret != VPX_CODEC_OK) { |
73 LOG(INFO) << "Decoding failed:" << vpx_codec_err_to_string(ret) << "\n" | 126 LOG(INFO) << "Decoding failed:" |
74 << "Details: " << vpx_codec_error(codec_) << "\n" | 127 << vpx_codec_err_to_string(ret) |
| 128 << "\n" |
| 129 << "Details: " |
| 130 << vpx_codec_error(codec_) |
| 131 << "\n" |
75 << vpx_codec_error_detail(codec_); | 132 << vpx_codec_error_detail(codec_); |
| 133 return false; |
76 } | 134 } |
77 | 135 |
78 // Gets the decoded data. | 136 // Gets the decoded data. |
79 vpx_codec_iter_t iter = NULL; | 137 vpx_codec_iter_t iter = NULL; |
80 vpx_image_t* image = vpx_codec_get_frame(codec_, &iter); | 138 vpx_image_t* image = vpx_codec_get_frame(codec_, &iter); |
81 if (!image) { | 139 if (!image) { |
82 LOG(INFO) << "No video frame decoded"; | 140 LOG(INFO) << "No video frame decoded"; |
| 141 return false; |
83 } | 142 } |
84 | 143 |
85 // Perform YUV conversion. | 144 // Perform YUV conversion. |
86 media::ConvertYUVToRGB32(image->planes[0], image->planes[1], image->planes[2], | 145 media::ConvertYUVToRGB32(image->planes[0], image->planes[1], image->planes[2], |
87 frame_->data(media::VideoFrame::kRGBPlane), | 146 frame_->data(media::VideoFrame::kRGBPlane), |
88 frame_->width(), frame_->height(), | 147 rect_width_, rect_height_, |
89 image->stride[0], image->stride[1], | 148 image->stride[0], image->stride[1], |
90 frame_->stride(media::VideoFrame::kRGBPlane), | 149 frame_->stride(media::VideoFrame::kRGBPlane), |
91 media::YV12); | 150 media::YV12); |
| 151 |
| 152 updated_rects_->clear(); |
| 153 updated_rects_->push_back(gfx::Rect(rect_x_, rect_y_, |
| 154 rect_width_, rect_height_)); |
| 155 partial_decode_done_->Run(); |
| 156 return true; |
92 } | 157 } |
93 | 158 |
94 void DecoderVp8::Reset() { | 159 bool DecoderVp8::HandleEndRect(ChromotingHostMessage* message) { |
95 frame_ = NULL; | 160 DCHECK_EQ(kWaitingForRectData, state_); |
96 state_ = kUninitialized; | 161 state_ = kWaitingForBeginRect; |
97 } | 162 return true; |
98 | |
99 bool DecoderVp8::IsReadyForData() { | |
100 return state_ == kReady; | |
101 } | |
102 | |
103 VideoPacketFormat::Encoding DecoderVp8::Encoding() { | |
104 return VideoPacketFormat::ENCODING_VP8; | |
105 } | 163 } |
106 | 164 |
107 } // namespace remoting | 165 } // namespace remoting |
OLD | NEW |