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_(kWaitingForBeginRect), | 21 : state_(kUninitialized), |
22 rect_x_(0), | |
23 rect_y_(0), | |
24 rect_width_(0), | |
25 rect_height_(0), | |
26 updated_rects_(NULL), | |
27 codec_(NULL) { | 22 codec_(NULL) { |
28 } | 23 } |
29 | 24 |
30 DecoderVp8::~DecoderVp8() { | 25 DecoderVp8::~DecoderVp8() { |
31 if (codec_) { | 26 if (codec_) { |
32 vpx_codec_err_t ret = vpx_codec_destroy(codec_); | 27 vpx_codec_err_t ret = vpx_codec_destroy(codec_); |
33 CHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec"; | 28 CHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec"; |
34 } | 29 } |
35 delete codec_; | 30 delete codec_; |
36 } | 31 } |
37 | 32 |
38 bool DecoderVp8::BeginDecode(scoped_refptr<media::VideoFrame> frame, | 33 void DecoderVp8::Initialize(scoped_refptr<media::VideoFrame> frame, |
39 UpdatedRects* updated_rects, | 34 const gfx::Rect& clip, int bytes_per_src_pixel) { |
40 Task* partial_decode_done, | 35 DCHECK_EQ(kUninitialized, state_); |
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; | |
50 | 36 |
51 if (frame->format() != media::VideoFrame::RGB32) { | 37 if (frame->format() != media::VideoFrame::RGB32) { |
52 LOG(INFO) << "DecoderVp8 only supports RGB32 as output"; | 38 LOG(INFO) << "DecoderVp8 only supports RGB32 as output"; |
53 return false; | 39 state_ = kError; |
| 40 return; |
54 } | 41 } |
55 frame_ = frame; | 42 frame_ = frame; |
56 return true; | 43 |
| 44 state_ = kReady; |
57 } | 45 } |
58 | 46 |
59 bool DecoderVp8::PartialDecode(ChromotingHostMessage* message) { | 47 void DecoderVp8::DecodeBytes(const std::string& encoded_bytes) { |
60 scoped_ptr<ChromotingHostMessage> msg_deleter(message); | 48 DCHECK_EQ(kReady, state_); |
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()); | |
103 | 49 |
104 // Initialize the codec as needed. | 50 // Initialize the codec as needed. |
105 if (!codec_) { | 51 if (!codec_) { |
106 codec_ = new vpx_codec_ctx_t(); | 52 codec_ = new vpx_codec_ctx_t(); |
107 vpx_codec_err_t ret = | 53 vpx_codec_err_t ret = |
108 vpx_codec_dec_init( | 54 vpx_codec_dec_init( |
109 codec_, | 55 codec_, |
110 (const vpx_codec_iface_t*)media::GetVp8DxAlgoAddress(), NULL, 0); | 56 (const vpx_codec_iface_t*)media::GetVp8DxAlgoAddress(), NULL, 0); |
111 if (ret != VPX_CODEC_OK) { | 57 if (ret != VPX_CODEC_OK) { |
112 LOG(INFO) << "Cannot initialize codec."; | 58 LOG(INFO) << "Cannot initialize codec."; |
113 delete codec_; | 59 delete codec_; |
114 codec_ = NULL; | 60 codec_ = NULL; |
115 return false; | 61 state_ = kError; |
| 62 return; |
116 } | 63 } |
117 } | 64 } |
118 | 65 |
| 66 LOG(WARNING) << "Decoding " << encoded_bytes.size(); |
| 67 |
119 // Do the actual decoding. | 68 // Do the actual decoding. |
120 vpx_codec_err_t ret = vpx_codec_decode( | 69 vpx_codec_err_t ret = vpx_codec_decode( |
121 codec_, | 70 codec_, reinterpret_cast<const uint8*>(encoded_bytes.data()), |
122 (uint8_t*)message->update_stream_packet().rect_data().data().c_str(), | 71 encoded_bytes.size(), NULL, 0); |
123 message->update_stream_packet().rect_data().data().size(), | |
124 NULL, 0); | |
125 if (ret != VPX_CODEC_OK) { | 72 if (ret != VPX_CODEC_OK) { |
126 LOG(INFO) << "Decoding failed:" | 73 LOG(INFO) << "Decoding failed:" << vpx_codec_err_to_string(ret) << "\n" |
127 << vpx_codec_err_to_string(ret) | 74 << "Details: " << vpx_codec_error(codec_) << "\n" |
128 << "\n" | |
129 << "Details: " | |
130 << vpx_codec_error(codec_) | |
131 << "\n" | |
132 << vpx_codec_error_detail(codec_); | 75 << vpx_codec_error_detail(codec_); |
133 return false; | |
134 } | 76 } |
135 | 77 |
136 // Gets the decoded data. | 78 // Gets the decoded data. |
137 vpx_codec_iter_t iter = NULL; | 79 vpx_codec_iter_t iter = NULL; |
138 vpx_image_t* image = vpx_codec_get_frame(codec_, &iter); | 80 vpx_image_t* image = vpx_codec_get_frame(codec_, &iter); |
139 if (!image) { | 81 if (!image) { |
140 LOG(INFO) << "No video frame decoded"; | 82 LOG(INFO) << "No video frame decoded"; |
141 return false; | |
142 } | 83 } |
143 | 84 |
144 // Perform YUV conversion. | 85 // Perform YUV conversion. |
145 media::ConvertYUVToRGB32(image->planes[0], image->planes[1], image->planes[2], | 86 media::ConvertYUVToRGB32(image->planes[0], image->planes[1], image->planes[2], |
146 frame_->data(media::VideoFrame::kRGBPlane), | 87 frame_->data(media::VideoFrame::kRGBPlane), |
147 rect_width_, rect_height_, | 88 frame_->width(), frame_->height(), |
148 image->stride[0], image->stride[1], | 89 image->stride[0], image->stride[1], |
149 frame_->stride(media::VideoFrame::kRGBPlane), | 90 frame_->stride(media::VideoFrame::kRGBPlane), |
150 media::YV12); | 91 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; | |
157 } | 92 } |
158 | 93 |
159 bool DecoderVp8::HandleEndRect(ChromotingHostMessage* message) { | 94 void DecoderVp8::Reset() { |
160 DCHECK_EQ(kWaitingForRectData, state_); | 95 frame_ = NULL; |
161 state_ = kWaitingForBeginRect; | 96 state_ = kUninitialized; |
162 return true; | 97 } |
| 98 |
| 99 bool DecoderVp8::IsReadyForData() { |
| 100 return state_ == kReady; |
| 101 } |
| 102 |
| 103 VideoPacketFormat::Encoding DecoderVp8::Encoding() { |
| 104 return VideoPacketFormat::ENCODING_VP8; |
163 } | 105 } |
164 | 106 |
165 } // namespace remoting | 107 } // namespace remoting |
OLD | NEW |