OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "media/gpu/android_video_encode_accelerator.h" | 5 #include "media/gpu/android_video_encode_accelerator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <set> | 8 #include <set> |
9 #include <tuple> | 9 #include <tuple> |
10 | 10 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 if (supported_codec.codec == kCodecVP8 && | 114 if (supported_codec.codec == kCodecVP8 && |
115 !MediaCodecUtil::IsVp8EncoderAvailable()) { | 115 !MediaCodecUtil::IsVp8EncoderAvailable()) { |
116 continue; | 116 continue; |
117 } | 117 } |
118 | 118 |
119 if (supported_codec.codec == kCodecH264 && | 119 if (supported_codec.codec == kCodecH264 && |
120 !MediaCodecUtil::IsH264EncoderAvailable()) { | 120 !MediaCodecUtil::IsH264EncoderAvailable()) { |
121 continue; | 121 continue; |
122 } | 122 } |
123 | 123 |
124 if (VideoCodecBridge::IsKnownUnaccelerated(supported_codec.codec, | 124 if (MediaCodecUtil::IsKnownUnaccelerated(supported_codec.codec, |
125 MEDIA_CODEC_ENCODER)) { | 125 MediaCodecDirection::ENCODER)) { |
126 continue; | 126 continue; |
127 } | 127 } |
128 | 128 |
129 SupportedProfile profile; | 129 SupportedProfile profile; |
130 profile.profile = supported_codec.profile; | 130 profile.profile = supported_codec.profile; |
131 // It would be nice if MediaCodec exposes the maximum capabilities of | 131 // It would be nice if MediaCodec exposes the maximum capabilities of |
132 // the encoder. Hard-code some reasonable defaults as workaround. | 132 // the encoder. Hard-code some reasonable defaults as workaround. |
133 profile.max_resolution.SetSize(kMaxEncodeFrameWidth, kMaxEncodeFrameHeight); | 133 profile.max_resolution.SetSize(kMaxEncodeFrameWidth, kMaxEncodeFrameHeight); |
134 profile.max_framerate_numerator = kMaxFramerateNumerator; | 134 profile.max_framerate_numerator = kMaxFramerateNumerator; |
135 profile.max_framerate_denominator = kMaxFramerateDenominator; | 135 profile.max_framerate_denominator = kMaxFramerateDenominator; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 frame_input_count = 30; | 180 frame_input_count = 30; |
181 i_frame_interval = IFRAME_INTERVAL_H264; | 181 i_frame_interval = IFRAME_INTERVAL_H264; |
182 } else { | 182 } else { |
183 return false; | 183 return false; |
184 } | 184 } |
185 | 185 |
186 frame_size_ = input_visible_size; | 186 frame_size_ = input_visible_size; |
187 last_set_bitrate_ = initial_bitrate; | 187 last_set_bitrate_ = initial_bitrate; |
188 | 188 |
189 // Only consider using MediaCodec if it's likely backed by hardware. | 189 // Only consider using MediaCodec if it's likely backed by hardware. |
190 if (VideoCodecBridge::IsKnownUnaccelerated(codec, MEDIA_CODEC_ENCODER)) { | 190 if (MediaCodecUtil::IsKnownUnaccelerated(codec, |
| 191 MediaCodecDirection::ENCODER)) { |
191 DLOG(ERROR) << "No HW support"; | 192 DLOG(ERROR) << "No HW support"; |
192 return false; | 193 return false; |
193 } | 194 } |
194 | 195 |
195 PixelFormat pixel_format = COLOR_FORMAT_YUV420_SEMIPLANAR; | 196 PixelFormat pixel_format = COLOR_FORMAT_YUV420_SEMIPLANAR; |
196 if (!GetSupportedColorFormatForMime(mime_type, &pixel_format)) { | 197 if (!GetSupportedColorFormatForMime(mime_type, &pixel_format)) { |
197 DLOG(ERROR) << "No color format support."; | 198 DLOG(ERROR) << "No color format support."; |
198 return false; | 199 return false; |
199 } | 200 } |
200 media_codec_.reset(VideoCodecBridge::CreateEncoder( | 201 media_codec_ = MediaCodecBridgeImpl::CreateVideoEncoder( |
201 codec, input_visible_size, initial_bitrate, INITIAL_FRAMERATE, | 202 codec, input_visible_size, initial_bitrate, INITIAL_FRAMERATE, |
202 i_frame_interval, pixel_format)); | 203 i_frame_interval, pixel_format); |
203 | 204 |
204 if (!media_codec_) { | 205 if (!media_codec_) { |
205 DLOG(ERROR) << "Failed to create/start the codec: " | 206 DLOG(ERROR) << "Failed to create/start the codec: " |
206 << input_visible_size.ToString(); | 207 << input_visible_size.ToString(); |
207 return false; | 208 return false; |
208 } | 209 } |
209 | 210 |
210 // Conservative upper bound for output buffer size: decoded size + 2KB. | 211 // Conservative upper bound for output buffer size: decoded size + 2KB. |
211 const size_t output_buffer_capacity = | 212 const size_t output_buffer_capacity = |
212 VideoFrame::AllocationSize(format, input_visible_size) + 2048; | 213 VideoFrame::AllocationSize(format, input_visible_size) + 2048; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 } | 304 } |
304 | 305 |
305 void AndroidVideoEncodeAccelerator::QueueInput() { | 306 void AndroidVideoEncodeAccelerator::QueueInput() { |
306 if (error_occurred_ || pending_frames_.empty()) | 307 if (error_occurred_ || pending_frames_.empty()) |
307 return; | 308 return; |
308 | 309 |
309 int input_buf_index = 0; | 310 int input_buf_index = 0; |
310 MediaCodecStatus status = | 311 MediaCodecStatus status = |
311 media_codec_->DequeueInputBuffer(NoWaitTimeOut(), &input_buf_index); | 312 media_codec_->DequeueInputBuffer(NoWaitTimeOut(), &input_buf_index); |
312 if (status != MEDIA_CODEC_OK) { | 313 if (status != MEDIA_CODEC_OK) { |
313 DCHECK(status == MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER || | 314 DCHECK(status == MEDIA_CODEC_TRY_AGAIN_LATER || |
314 status == MEDIA_CODEC_ERROR); | 315 status == MEDIA_CODEC_ERROR); |
315 RETURN_ON_FAILURE(status != MEDIA_CODEC_ERROR, "MediaCodec error", | 316 RETURN_ON_FAILURE(status != MEDIA_CODEC_ERROR, "MediaCodec error", |
316 kPlatformFailureError); | 317 kPlatformFailureError); |
317 return; | 318 return; |
318 } | 319 } |
319 | 320 |
320 const PendingFrames::value_type& input = pending_frames_.front(); | 321 const PendingFrames::value_type& input = pending_frames_.front(); |
321 bool is_key_frame = std::get<1>(input); | 322 bool is_key_frame = std::get<1>(input); |
322 if (is_key_frame) { | 323 if (is_key_frame) { |
323 // Ideally MediaCodec would honor BUFFER_FLAG_SYNC_FRAME so we could | 324 // Ideally MediaCodec would honor BUFFER_FLAG_SYNC_FRAME so we could |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 | 378 |
378 int32_t buf_index = 0; | 379 int32_t buf_index = 0; |
379 size_t offset = 0; | 380 size_t offset = 0; |
380 size_t size = 0; | 381 size_t size = 0; |
381 bool key_frame = false; | 382 bool key_frame = false; |
382 | 383 |
383 MediaCodecStatus status = | 384 MediaCodecStatus status = |
384 media_codec_->DequeueOutputBuffer(NoWaitTimeOut(), &buf_index, &offset, | 385 media_codec_->DequeueOutputBuffer(NoWaitTimeOut(), &buf_index, &offset, |
385 &size, nullptr, nullptr, &key_frame); | 386 &size, nullptr, nullptr, &key_frame); |
386 switch (status) { | 387 switch (status) { |
387 case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER: | 388 case MEDIA_CODEC_TRY_AGAIN_LATER: |
388 return; | 389 return; |
389 | 390 |
390 case MEDIA_CODEC_ERROR: | 391 case MEDIA_CODEC_ERROR: |
391 RETURN_ON_FAILURE(false, "Codec error", kPlatformFailureError); | 392 RETURN_ON_FAILURE(false, "Codec error", kPlatformFailureError); |
392 // Unreachable because of previous statement, but included for clarity. | 393 // Unreachable because of previous statement, but included for clarity. |
393 return; | 394 return; |
394 | 395 |
395 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: | 396 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: |
396 return; | 397 return; |
397 | 398 |
(...skipping 26 matching lines...) Expand all Loading... |
424 --num_buffers_at_codec_; | 425 --num_buffers_at_codec_; |
425 | 426 |
426 base::ThreadTaskRunnerHandle::Get()->PostTask( | 427 base::ThreadTaskRunnerHandle::Get()->PostTask( |
427 FROM_HERE, | 428 FROM_HERE, |
428 base::Bind(&VideoEncodeAccelerator::Client::BitstreamBufferReady, | 429 base::Bind(&VideoEncodeAccelerator::Client::BitstreamBufferReady, |
429 client_ptr_factory_->GetWeakPtr(), bitstream_buffer.id(), size, | 430 client_ptr_factory_->GetWeakPtr(), bitstream_buffer.id(), size, |
430 key_frame, base::Time::Now() - base::Time())); | 431 key_frame, base::Time::Now() - base::Time())); |
431 } | 432 } |
432 | 433 |
433 } // namespace media | 434 } // namespace media |
OLD | NEW |