| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "content/common/gpu/media/vp8_decoder.h" | |
| 6 #include "media/base/limits.h" | |
| 7 | |
| 8 namespace content { | |
| 9 | |
| 10 VP8Decoder::VP8Accelerator::VP8Accelerator() { | |
| 11 } | |
| 12 | |
| 13 VP8Decoder::VP8Accelerator::~VP8Accelerator() { | |
| 14 } | |
| 15 | |
| 16 VP8Decoder::VP8Decoder(VP8Accelerator* accelerator) | |
| 17 : state_(kNeedStreamMetadata), | |
| 18 curr_frame_start_(nullptr), | |
| 19 frame_size_(0), | |
| 20 accelerator_(accelerator) { | |
| 21 DCHECK(accelerator_); | |
| 22 } | |
| 23 | |
| 24 VP8Decoder::~VP8Decoder() { | |
| 25 } | |
| 26 | |
| 27 bool VP8Decoder::Flush() { | |
| 28 DVLOG(2) << "Decoder flush"; | |
| 29 Reset(); | |
| 30 return true; | |
| 31 } | |
| 32 | |
| 33 void VP8Decoder::SetStream(const uint8_t* ptr, size_t size) { | |
| 34 DCHECK(ptr); | |
| 35 DCHECK(size); | |
| 36 | |
| 37 curr_frame_start_ = ptr; | |
| 38 frame_size_ = size; | |
| 39 DVLOG(4) << "New input stream at: " << (void*)ptr << " size: " << size; | |
| 40 } | |
| 41 | |
| 42 void VP8Decoder::Reset() { | |
| 43 curr_pic_ = nullptr; | |
| 44 curr_frame_hdr_ = nullptr; | |
| 45 curr_frame_start_ = nullptr; | |
| 46 frame_size_ = 0; | |
| 47 | |
| 48 last_frame_ = nullptr; | |
| 49 golden_frame_ = nullptr; | |
| 50 alt_frame_ = nullptr; | |
| 51 | |
| 52 if (state_ == kDecoding) | |
| 53 state_ = kAfterReset; | |
| 54 } | |
| 55 | |
| 56 VP8Decoder::DecodeResult VP8Decoder::Decode() { | |
| 57 if (!curr_frame_start_ || frame_size_ == 0) | |
| 58 return kRanOutOfStreamData; | |
| 59 | |
| 60 if (!curr_frame_hdr_) { | |
| 61 curr_frame_hdr_.reset(new media::Vp8FrameHeader()); | |
| 62 if (!parser_.ParseFrame(curr_frame_start_, frame_size_, | |
| 63 curr_frame_hdr_.get())) { | |
| 64 DVLOG(1) << "Error during decode"; | |
| 65 state_ = kError; | |
| 66 return kDecodeError; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 if (curr_frame_hdr_->IsKeyframe()) { | |
| 71 gfx::Size new_pic_size(curr_frame_hdr_->width, curr_frame_hdr_->height); | |
| 72 if (new_pic_size.IsEmpty()) | |
| 73 return kDecodeError; | |
| 74 | |
| 75 if (new_pic_size != pic_size_) { | |
| 76 DVLOG(2) << "New resolution: " << new_pic_size.ToString(); | |
| 77 pic_size_ = new_pic_size; | |
| 78 return kAllocateNewSurfaces; | |
| 79 } | |
| 80 | |
| 81 state_ = kDecoding; | |
| 82 } else { | |
| 83 if (state_ != kDecoding) { | |
| 84 // Need a resume point. | |
| 85 curr_frame_hdr_.reset(); | |
| 86 return kRanOutOfStreamData; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 curr_pic_ = accelerator_->CreateVP8Picture(); | |
| 91 if (!curr_pic_) | |
| 92 return kRanOutOfSurfaces; | |
| 93 | |
| 94 if (!DecodeAndOutputCurrentFrame()) | |
| 95 return kDecodeError; | |
| 96 | |
| 97 return kRanOutOfStreamData; | |
| 98 } | |
| 99 | |
| 100 void VP8Decoder::RefreshReferenceFrames() { | |
| 101 if (curr_frame_hdr_->IsKeyframe()) { | |
| 102 last_frame_ = curr_pic_; | |
| 103 golden_frame_ = curr_pic_; | |
| 104 alt_frame_ = curr_pic_; | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 // Save current golden since we overwrite it here, | |
| 109 // but may have to use it to update alt below. | |
| 110 scoped_refptr<VP8Picture> curr_golden = golden_frame_; | |
| 111 | |
| 112 if (curr_frame_hdr_->refresh_golden_frame) { | |
| 113 golden_frame_ = curr_pic_; | |
| 114 } else { | |
| 115 switch (curr_frame_hdr_->copy_buffer_to_golden) { | |
| 116 case media::Vp8FrameHeader::COPY_LAST_TO_GOLDEN: | |
| 117 DCHECK(last_frame_); | |
| 118 golden_frame_ = last_frame_; | |
| 119 break; | |
| 120 | |
| 121 case media::Vp8FrameHeader::COPY_ALT_TO_GOLDEN: | |
| 122 DCHECK(alt_frame_); | |
| 123 golden_frame_ = alt_frame_; | |
| 124 break; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 if (curr_frame_hdr_->refresh_alternate_frame) { | |
| 129 alt_frame_ = curr_pic_; | |
| 130 } else { | |
| 131 switch (curr_frame_hdr_->copy_buffer_to_alternate) { | |
| 132 case media::Vp8FrameHeader::COPY_LAST_TO_ALT: | |
| 133 DCHECK(last_frame_); | |
| 134 alt_frame_ = last_frame_; | |
| 135 break; | |
| 136 | |
| 137 case media::Vp8FrameHeader::COPY_GOLDEN_TO_ALT: | |
| 138 DCHECK(curr_golden); | |
| 139 alt_frame_ = curr_golden; | |
| 140 break; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 if (curr_frame_hdr_->refresh_last) | |
| 145 last_frame_ = curr_pic_; | |
| 146 } | |
| 147 | |
| 148 bool VP8Decoder::DecodeAndOutputCurrentFrame() { | |
| 149 DCHECK(!pic_size_.IsEmpty()); | |
| 150 DCHECK(curr_pic_); | |
| 151 DCHECK(curr_frame_hdr_); | |
| 152 | |
| 153 if (curr_frame_hdr_->IsKeyframe()) { | |
| 154 horizontal_scale_ = curr_frame_hdr_->horizontal_scale; | |
| 155 vertical_scale_ = curr_frame_hdr_->vertical_scale; | |
| 156 } else { | |
| 157 // Populate fields from decoder state instead. | |
| 158 curr_frame_hdr_->width = pic_size_.width(); | |
| 159 curr_frame_hdr_->height = pic_size_.height(); | |
| 160 curr_frame_hdr_->horizontal_scale = horizontal_scale_; | |
| 161 curr_frame_hdr_->vertical_scale = vertical_scale_; | |
| 162 } | |
| 163 | |
| 164 if (!accelerator_->SubmitDecode(curr_pic_, curr_frame_hdr_.get(), last_frame_, | |
| 165 golden_frame_, alt_frame_)) | |
| 166 return false; | |
| 167 | |
| 168 if (!accelerator_->OutputPicture(curr_pic_)) | |
| 169 return false; | |
| 170 | |
| 171 RefreshReferenceFrames(); | |
| 172 | |
| 173 curr_pic_ = nullptr; | |
| 174 curr_frame_hdr_ = nullptr; | |
| 175 curr_frame_start_ = nullptr; | |
| 176 frame_size_ = 0; | |
| 177 return true; | |
| 178 } | |
| 179 | |
| 180 size_t VP8Decoder::GetRequiredNumOfPictures() const { | |
| 181 const size_t kVP8NumFramesActive = 4; | |
| 182 const size_t kPicsInPipeline = media::limits::kMaxVideoFrames + 2; | |
| 183 return kVP8NumFramesActive + kPicsInPipeline; | |
| 184 } | |
| 185 | |
| 186 } // namespace content | |
| OLD | NEW |