OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 const int64_t frame_input_time_ms); | 149 const int64_t frame_input_time_ms); |
150 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, | 150 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, |
151 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); | 151 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); |
152 bool EncodeTextureOnCodecThread(JNIEnv* jni, | 152 bool EncodeTextureOnCodecThread(JNIEnv* jni, |
153 bool key_frame, const webrtc::VideoFrame& frame); | 153 bool key_frame, const webrtc::VideoFrame& frame); |
154 | 154 |
155 int32_t RegisterEncodeCompleteCallbackOnCodecThread( | 155 int32_t RegisterEncodeCompleteCallbackOnCodecThread( |
156 webrtc::EncodedImageCallback* callback); | 156 webrtc::EncodedImageCallback* callback); |
157 int32_t ReleaseOnCodecThread(); | 157 int32_t ReleaseOnCodecThread(); |
158 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); | 158 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); |
159 void OnDroppedFrameOnCodecThread(); | |
160 | 159 |
161 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. | 160 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. |
162 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); | 161 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); |
163 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); | 162 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); |
164 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); | 163 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); |
165 jlong GetOutputBufferInfoPresentationTimestampUs( | 164 jlong GetOutputBufferInfoPresentationTimestampUs( |
166 JNIEnv* jni, jobject j_output_buffer_info); | 165 JNIEnv* jni, jobject j_output_buffer_info); |
167 | 166 |
168 // Deliver any outputs pending in the MediaCodec to our |callback_| and return | 167 // Deliver any outputs pending in the MediaCodec to our |callback_| and return |
169 // true on success. | 168 // true on success. |
170 bool DeliverPendingOutputs(JNIEnv* jni); | 169 bool DeliverPendingOutputs(JNIEnv* jni); |
171 | 170 |
172 // Search for H.264 start codes. | 171 // Search for H.264 start codes. |
173 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); | 172 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); |
174 | 173 |
| 174 QualityScaler::Settings GetQPThresholds() const override; |
| 175 |
175 // Displays encoder statistics. | 176 // Displays encoder statistics. |
176 void LogStatistics(bool force_log); | 177 void LogStatistics(bool force_log); |
177 | 178 |
178 // Type of video codec. | 179 // Type of video codec. |
179 VideoCodecType codecType_; | 180 VideoCodecType codecType_; |
180 | 181 |
181 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to | 182 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to |
182 // |codec_thread_| synchronously. | 183 // |codec_thread_| synchronously. |
183 webrtc::EncodedImageCallback* callback_; | 184 webrtc::EncodedImageCallback* callback_; |
184 | 185 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // |input_frame_infos_|. | 251 // |input_frame_infos_|. |
251 int64_t output_render_time_ms_; // Last output frame render time from | 252 int64_t output_render_time_ms_; // Last output frame render time from |
252 // |input_frame_infos_|. | 253 // |input_frame_infos_|. |
253 webrtc::VideoRotation output_rotation_; // Last output frame rotation from | 254 webrtc::VideoRotation output_rotation_; // Last output frame rotation from |
254 // |input_frame_infos_|. | 255 // |input_frame_infos_|. |
255 // Frame size in bytes fed to MediaCodec. | 256 // Frame size in bytes fed to MediaCodec. |
256 int yuv_size_; | 257 int yuv_size_; |
257 // True only when between a callback_->Encoded() call return a positive value | 258 // True only when between a callback_->Encoded() call return a positive value |
258 // and the next Encode() call being ignored. | 259 // and the next Encode() call being ignored. |
259 bool drop_next_input_frame_; | 260 bool drop_next_input_frame_; |
| 261 bool scale_; |
260 // Global references; must be deleted in Release(). | 262 // Global references; must be deleted in Release(). |
261 std::vector<jobject> input_buffers_; | 263 std::vector<jobject> input_buffers_; |
262 QualityScaler quality_scaler_; | |
263 // Dynamic resolution change, off by default. | |
264 bool scale_; | |
265 | |
266 // H264 bitstream parser, used to extract QP from encoded bitstreams. | |
267 webrtc::H264BitstreamParser h264_bitstream_parser_; | 264 webrtc::H264BitstreamParser h264_bitstream_parser_; |
268 | 265 |
269 // VP9 variables to populate codec specific structure. | 266 // VP9 variables to populate codec specific structure. |
270 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for | 267 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for |
271 // non-flexible VP9 mode. | 268 // non-flexible VP9 mode. |
272 uint8_t tl0_pic_idx_; | 269 uint8_t tl0_pic_idx_; |
273 size_t gof_idx_; | 270 size_t gof_idx_; |
274 | 271 |
275 // EGL context - owned by factory, should not be allocated/destroyed | 272 // EGL context - owned by factory, should not be allocated/destroyed |
276 // by MediaCodecVideoEncoder. | 273 // by MediaCodecVideoEncoder. |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 scale_ = false; | 384 scale_ = false; |
388 if (codecType_ == kVideoCodecVP8) { | 385 if (codecType_ == kVideoCodecVP8) { |
389 scale_ = codec_settings->codecSpecific.VP8.automaticResizeOn; | 386 scale_ = codec_settings->codecSpecific.VP8.automaticResizeOn; |
390 } else if (codecType_ != kVideoCodecVP9) { | 387 } else if (codecType_ != kVideoCodecVP9) { |
391 scale_ = true; | 388 scale_ = true; |
392 } | 389 } |
393 | 390 |
394 ALOGD << "InitEncode request: " << init_width << " x " << init_height; | 391 ALOGD << "InitEncode request: " << init_width << " x " << init_height; |
395 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); | 392 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); |
396 | 393 |
397 if (scale_) { | |
398 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecH264) { | |
399 quality_scaler_.Init(codecType_, codec_settings->startBitrate, | |
400 codec_settings->width, codec_settings->height, | |
401 codec_settings->maxFramerate); | |
402 } else { | |
403 // When adding codec support to additional hardware codecs, also configure | |
404 // their QP thresholds for scaling. | |
405 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; | |
406 scale_ = false; | |
407 } | |
408 QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); | |
409 init_width = res.width; | |
410 init_height = res.height; | |
411 ALOGD << "Scaled resolution: " << init_width << " x " << init_height; | |
412 } | |
413 | |
414 return codec_thread_->Invoke<int32_t>( | 394 return codec_thread_->Invoke<int32_t>( |
415 RTC_FROM_HERE, | 395 RTC_FROM_HERE, |
416 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, | 396 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, |
417 init_height, codec_settings->startBitrate, | 397 init_height, codec_settings->startBitrate, |
418 codec_settings->maxFramerate, | 398 codec_settings->maxFramerate, |
419 codec_settings->expect_encode_from_texture)); | 399 codec_settings->expect_encode_from_texture)); |
420 } | 400 } |
421 | 401 |
422 int32_t MediaCodecVideoEncoder::Encode( | 402 int32_t MediaCodecVideoEncoder::Encode( |
423 const webrtc::VideoFrame& frame, | 403 const webrtc::VideoFrame& frame, |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 << ". TS: " << (int)(current_timestamp_us_ / 1000) | 617 << ". TS: " << (int)(current_timestamp_us_ / 1000) |
638 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ | 618 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ |
639 << ". Kbps: " << last_set_bitrate_kbps_; | 619 << ". Kbps: " << last_set_bitrate_kbps_; |
640 } | 620 } |
641 | 621 |
642 if (drop_next_input_frame_) { | 622 if (drop_next_input_frame_) { |
643 ALOGW << "Encoder drop frame - failed callback."; | 623 ALOGW << "Encoder drop frame - failed callback."; |
644 drop_next_input_frame_ = false; | 624 drop_next_input_frame_ = false; |
645 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 625 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
646 frames_dropped_media_encoder_++; | 626 frames_dropped_media_encoder_++; |
647 OnDroppedFrameOnCodecThread(); | |
648 return WEBRTC_VIDEO_CODEC_OK; | 627 return WEBRTC_VIDEO_CODEC_OK; |
649 } | 628 } |
650 | 629 |
651 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; | 630 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; |
652 | 631 |
653 // Check if we accumulated too many frames in encoder input buffers and drop | 632 // Check if we accumulated too many frames in encoder input buffers and drop |
654 // frame if so. | 633 // frame if so. |
655 if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) { | 634 if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) { |
656 ALOGD << "Already " << input_frame_infos_.size() | 635 ALOGD << "Already " << input_frame_infos_.size() |
657 << " frames in the queue, dropping" | 636 << " frames in the queue, dropping" |
658 << ". TS: " << (int)(current_timestamp_us_ / 1000) | 637 << ". TS: " << (int)(current_timestamp_us_ / 1000) |
659 << ". Fps: " << last_set_fps_ | 638 << ". Fps: " << last_set_fps_ |
660 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; | 639 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; |
661 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 640 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
662 consecutive_full_queue_frame_drops_++; | 641 consecutive_full_queue_frame_drops_++; |
663 if (consecutive_full_queue_frame_drops_ >= | 642 if (consecutive_full_queue_frame_drops_ >= |
664 ENCODER_STALL_FRAMEDROP_THRESHOLD) { | 643 ENCODER_STALL_FRAMEDROP_THRESHOLD) { |
665 ALOGE << "Encoder got stuck. Reset."; | 644 ALOGE << "Encoder got stuck. Reset."; |
666 ResetCodecOnCodecThread(); | 645 ResetCodecOnCodecThread(); |
667 return WEBRTC_VIDEO_CODEC_ERROR; | 646 return WEBRTC_VIDEO_CODEC_ERROR; |
668 } | 647 } |
669 frames_dropped_media_encoder_++; | 648 frames_dropped_media_encoder_++; |
670 OnDroppedFrameOnCodecThread(); | |
671 return WEBRTC_VIDEO_CODEC_OK; | 649 return WEBRTC_VIDEO_CODEC_OK; |
672 } | 650 } |
673 consecutive_full_queue_frame_drops_ = 0; | 651 consecutive_full_queue_frame_drops_ = 0; |
674 | 652 |
675 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( | 653 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( |
676 frame.video_frame_buffer()); | 654 frame.video_frame_buffer()); |
677 if (scale_) { | |
678 // Check framerate before spatial resolution change. | |
679 quality_scaler_.OnEncodeFrame(frame.width(), frame.height()); | |
680 const webrtc::QualityScaler::Resolution scaled_resolution = | |
681 quality_scaler_.GetScaledResolution(); | |
682 if (scaled_resolution.width != frame.width() || | |
683 scaled_resolution.height != frame.height()) { | |
684 if (input_buffer->native_handle() != nullptr) { | |
685 input_buffer = static_cast<AndroidTextureBuffer*>(input_buffer.get()) | |
686 ->CropScaleAndRotate(frame.width(), frame.height(), | |
687 0, 0, | |
688 scaled_resolution.width, | |
689 scaled_resolution.height, | |
690 webrtc::kVideoRotation_0); | |
691 } else { | |
692 input_buffer = quality_scaler_.GetScaledBuffer(input_buffer); | |
693 } | |
694 } | |
695 } | |
696 | 655 |
697 VideoFrame input_frame(input_buffer, frame.timestamp(), | 656 VideoFrame input_frame(input_buffer, frame.timestamp(), |
698 frame.render_time_ms(), frame.rotation()); | 657 frame.render_time_ms(), frame.rotation()); |
699 | 658 |
700 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { | 659 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { |
701 ALOGE << "Failed to reconfigure encoder."; | 660 ALOGE << "Failed to reconfigure encoder."; |
702 return WEBRTC_VIDEO_CODEC_ERROR; | 661 return WEBRTC_VIDEO_CODEC_ERROR; |
703 } | 662 } |
704 | 663 |
705 const bool key_frame = | 664 const bool key_frame = |
706 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; | 665 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; |
707 bool encode_status = true; | 666 bool encode_status = true; |
708 if (!input_frame.video_frame_buffer()->native_handle()) { | 667 if (!input_frame.video_frame_buffer()->native_handle()) { |
709 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, | 668 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, |
710 j_dequeue_input_buffer_method_); | 669 j_dequeue_input_buffer_method_); |
711 CHECK_EXCEPTION(jni); | 670 CHECK_EXCEPTION(jni); |
712 if (j_input_buffer_index == -1) { | 671 if (j_input_buffer_index == -1) { |
713 // Video codec falls behind - no input buffer available. | 672 // Video codec falls behind - no input buffer available. |
714 ALOGW << "Encoder drop frame - no input buffers available"; | 673 ALOGW << "Encoder drop frame - no input buffers available"; |
715 if (frames_received_ > 1) { | 674 if (frames_received_ > 1) { |
716 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 675 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
717 frames_dropped_media_encoder_++; | 676 frames_dropped_media_encoder_++; |
718 OnDroppedFrameOnCodecThread(); | |
719 } else { | 677 } else { |
720 // Input buffers are not ready after codec initialization, HW is still | 678 // Input buffers are not ready after codec initialization, HW is still |
721 // allocating thme - this is expected and should not result in drop | 679 // allocating thme - this is expected and should not result in drop |
722 // frame report. | 680 // frame report. |
723 frames_received_ = 0; | 681 frames_received_ = 0; |
724 } | 682 } |
725 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. | 683 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. |
726 } else if (j_input_buffer_index == -2) { | 684 } else if (j_input_buffer_index == -2) { |
727 ResetCodecOnCodecThread(); | 685 ResetCodecOnCodecThread(); |
728 return WEBRTC_VIDEO_CODEC_ERROR; | 686 return WEBRTC_VIDEO_CODEC_ERROR; |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 | 833 |
876 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, | 834 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, |
877 uint32_t frame_rate) { | 835 uint32_t frame_rate) { |
878 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 836 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
879 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? | 837 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? |
880 frame_rate : MAX_ALLOWED_VIDEO_FPS; | 838 frame_rate : MAX_ALLOWED_VIDEO_FPS; |
881 if (last_set_bitrate_kbps_ == new_bit_rate && | 839 if (last_set_bitrate_kbps_ == new_bit_rate && |
882 last_set_fps_ == frame_rate) { | 840 last_set_fps_ == frame_rate) { |
883 return WEBRTC_VIDEO_CODEC_OK; | 841 return WEBRTC_VIDEO_CODEC_OK; |
884 } | 842 } |
885 if (scale_) { | |
886 quality_scaler_.ReportFramerate(frame_rate); | |
887 } | |
888 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 843 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
889 ScopedLocalRefFrame local_ref_frame(jni); | 844 ScopedLocalRefFrame local_ref_frame(jni); |
890 if (new_bit_rate > 0) { | 845 if (new_bit_rate > 0) { |
891 last_set_bitrate_kbps_ = new_bit_rate; | 846 last_set_bitrate_kbps_ = new_bit_rate; |
892 } | 847 } |
893 if (frame_rate > 0) { | 848 if (frame_rate > 0) { |
894 last_set_fps_ = frame_rate; | 849 last_set_fps_ = frame_rate; |
895 } | 850 } |
896 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 851 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
897 j_set_rates_method_, | 852 j_set_rates_method_, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 std::unique_ptr<webrtc::EncodedImage> image( | 935 std::unique_ptr<webrtc::EncodedImage> image( |
981 new webrtc::EncodedImage(payload, payload_size, payload_size)); | 936 new webrtc::EncodedImage(payload, payload_size, payload_size)); |
982 image->_encodedWidth = width_; | 937 image->_encodedWidth = width_; |
983 image->_encodedHeight = height_; | 938 image->_encodedHeight = height_; |
984 image->_timeStamp = output_timestamp_; | 939 image->_timeStamp = output_timestamp_; |
985 image->capture_time_ms_ = output_render_time_ms_; | 940 image->capture_time_ms_ = output_render_time_ms_; |
986 image->rotation_ = output_rotation_; | 941 image->rotation_ = output_rotation_; |
987 image->_frameType = | 942 image->_frameType = |
988 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); | 943 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); |
989 image->_completeFrame = true; | 944 image->_completeFrame = true; |
990 image->adapt_reason_.quality_resolution_downscales = | |
991 scale_ ? quality_scaler_.downscale_shift() : -1; | |
992 | |
993 webrtc::CodecSpecificInfo info; | 945 webrtc::CodecSpecificInfo info; |
994 memset(&info, 0, sizeof(info)); | 946 memset(&info, 0, sizeof(info)); |
995 info.codecType = codecType_; | 947 info.codecType = codecType_; |
996 if (codecType_ == kVideoCodecVP8) { | 948 if (codecType_ == kVideoCodecVP8) { |
997 info.codecSpecific.VP8.pictureId = picture_id_; | 949 info.codecSpecific.VP8.pictureId = picture_id_; |
998 info.codecSpecific.VP8.nonReference = false; | 950 info.codecSpecific.VP8.nonReference = false; |
999 info.codecSpecific.VP8.simulcastIdx = 0; | 951 info.codecSpecific.VP8.simulcastIdx = 0; |
1000 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; | 952 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; |
1001 info.codecSpecific.VP8.layerSync = false; | 953 info.codecSpecific.VP8.layerSync = false; |
1002 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; | 954 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; |
(...skipping 26 matching lines...) Expand all Loading... |
1029 | 981 |
1030 // Generate a header describing a single fragment. | 982 // Generate a header describing a single fragment. |
1031 webrtc::RTPFragmentationHeader header; | 983 webrtc::RTPFragmentationHeader header; |
1032 memset(&header, 0, sizeof(header)); | 984 memset(&header, 0, sizeof(header)); |
1033 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { | 985 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { |
1034 header.VerifyAndAllocateFragmentationHeader(1); | 986 header.VerifyAndAllocateFragmentationHeader(1); |
1035 header.fragmentationOffset[0] = 0; | 987 header.fragmentationOffset[0] = 0; |
1036 header.fragmentationLength[0] = image->_length; | 988 header.fragmentationLength[0] = image->_length; |
1037 header.fragmentationPlType[0] = 0; | 989 header.fragmentationPlType[0] = 0; |
1038 header.fragmentationTimeDiff[0] = 0; | 990 header.fragmentationTimeDiff[0] = 0; |
1039 if (codecType_ == kVideoCodecVP8 && scale_) { | 991 if (codecType_ == kVideoCodecVP8) { |
1040 int qp; | 992 int qp; |
1041 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { | 993 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { |
1042 current_acc_qp_ += qp; | 994 current_acc_qp_ += qp; |
1043 quality_scaler_.ReportQP(qp); | |
1044 image->qp_ = qp; | 995 image->qp_ = qp; |
1045 } | 996 } |
1046 } | 997 } |
1047 } else if (codecType_ == kVideoCodecH264) { | 998 } else if (codecType_ == kVideoCodecH264) { |
1048 if (scale_) { | 999 h264_bitstream_parser_.ParseBitstream(payload, payload_size); |
1049 h264_bitstream_parser_.ParseBitstream(payload, payload_size); | 1000 int qp; |
1050 int qp; | 1001 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { |
1051 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { | 1002 current_acc_qp_ += qp; |
1052 current_acc_qp_ += qp; | 1003 image->qp_ = qp; |
1053 quality_scaler_.ReportQP(qp); | |
1054 } | |
1055 } | 1004 } |
1056 // For H.264 search for start codes. | 1005 // For H.264 search for start codes. |
1057 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; | 1006 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; |
1058 int32_t scPositionsLength = 0; | 1007 int32_t scPositionsLength = 0; |
1059 int32_t scPosition = 0; | 1008 int32_t scPosition = 0; |
1060 while (scPositionsLength < MAX_NALUS_PERFRAME) { | 1009 while (scPositionsLength < MAX_NALUS_PERFRAME) { |
1061 int32_t naluPosition = NextNaluPosition( | 1010 int32_t naluPosition = NextNaluPosition( |
1062 payload + scPosition, payload_size - scPosition); | 1011 payload + scPosition, payload_size - scPosition); |
1063 if (naluPosition < 0) { | 1012 if (naluPosition < 0) { |
1064 break; | 1013 break; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 ". QP: " << (current_acc_qp_ / current_frames_divider) << | 1097 ". QP: " << (current_acc_qp_ / current_frames_divider) << |
1149 " for last " << statistic_time_ms << " ms."; | 1098 " for last " << statistic_time_ms << " ms."; |
1150 stat_start_time_ms_ = rtc::TimeMillis(); | 1099 stat_start_time_ms_ = rtc::TimeMillis(); |
1151 current_frames_ = 0; | 1100 current_frames_ = 0; |
1152 current_bytes_ = 0; | 1101 current_bytes_ = 0; |
1153 current_acc_qp_ = 0; | 1102 current_acc_qp_ = 0; |
1154 current_encoding_time_ms_ = 0; | 1103 current_encoding_time_ms_ = 0; |
1155 } | 1104 } |
1156 } | 1105 } |
1157 | 1106 |
| 1107 QualityScaler::Settings MediaCodecVideoEncoder::GetQPThresholds() const { |
| 1108 return QualityScaler::Settings(scale_); |
| 1109 } |
| 1110 |
1158 int32_t MediaCodecVideoEncoder::NextNaluPosition( | 1111 int32_t MediaCodecVideoEncoder::NextNaluPosition( |
1159 uint8_t *buffer, size_t buffer_size) { | 1112 uint8_t *buffer, size_t buffer_size) { |
1160 if (buffer_size < H264_SC_LENGTH) { | 1113 if (buffer_size < H264_SC_LENGTH) { |
1161 return -1; | 1114 return -1; |
1162 } | 1115 } |
1163 uint8_t *head = buffer; | 1116 uint8_t *head = buffer; |
1164 // Set end buffer pointer to 4 bytes before actual buffer end so we can | 1117 // Set end buffer pointer to 4 bytes before actual buffer end so we can |
1165 // access head[1], head[2] and head[3] in a loop without buffer overrun. | 1118 // access head[1], head[2] and head[3] in a loop without buffer overrun. |
1166 uint8_t *end = buffer + buffer_size - H264_SC_LENGTH; | 1119 uint8_t *end = buffer + buffer_size - H264_SC_LENGTH; |
1167 | 1120 |
(...skipping 13 matching lines...) Expand all Loading... |
1181 if (head[3] != 0x01) { // got 000000xx | 1134 if (head[3] != 0x01) { // got 000000xx |
1182 head++; // xx != 1, continue searching. | 1135 head++; // xx != 1, continue searching. |
1183 continue; | 1136 continue; |
1184 } | 1137 } |
1185 return (int32_t)(head - buffer); | 1138 return (int32_t)(head - buffer); |
1186 } | 1139 } |
1187 return -1; | 1140 return -1; |
1188 } | 1141 } |
1189 | 1142 |
1190 void MediaCodecVideoEncoder::OnDroppedFrame() { | 1143 void MediaCodecVideoEncoder::OnDroppedFrame() { |
1191 // Methods running on the codec thread should call OnDroppedFrameOnCodecThread | |
1192 // directly. | |
1193 RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); | 1144 RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); |
1194 codec_thread_->Invoke<void>( | |
1195 RTC_FROM_HERE, | |
1196 Bind(&MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread, this)); | |
1197 } | |
1198 | |
1199 void MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread() { | |
1200 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
1201 // Report dropped frame to quality_scaler_. | |
1202 if (scale_) | |
1203 quality_scaler_.ReportDroppedFrame(); | |
1204 } | 1145 } |
1205 | 1146 |
1206 const char* MediaCodecVideoEncoder::ImplementationName() const { | 1147 const char* MediaCodecVideoEncoder::ImplementationName() const { |
1207 return "MediaCodec"; | 1148 return "MediaCodec"; |
1208 } | 1149 } |
1209 | 1150 |
1210 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() | 1151 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() |
1211 : egl_context_(nullptr) { | 1152 : egl_context_(nullptr) { |
1212 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 1153 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
1213 ScopedLocalRefFrame local_ref_frame(jni); | 1154 ScopedLocalRefFrame local_ref_frame(jni); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1290 return supported_codecs_; | 1231 return supported_codecs_; |
1291 } | 1232 } |
1292 | 1233 |
1293 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1234 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
1294 webrtc::VideoEncoder* encoder) { | 1235 webrtc::VideoEncoder* encoder) { |
1295 ALOGD << "Destroy video encoder."; | 1236 ALOGD << "Destroy video encoder."; |
1296 delete encoder; | 1237 delete encoder; |
1297 } | 1238 } |
1298 | 1239 |
1299 } // namespace webrtc_jni | 1240 } // namespace webrtc_jni |
OLD | NEW |