Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: webrtc/api/android/jni/androidmediaencoder_jni.cc

Issue 2398963003: Move usage of QualityScaler to ViEEncoder. (Closed)
Patch Set: fix videotoolbox overridden QP thresholds Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 13 matching lines...) Expand all
24 #include "webrtc/api/android/jni/native_handle_impl.h" 24 #include "webrtc/api/android/jni/native_handle_impl.h"
25 #include "webrtc/base/bind.h" 25 #include "webrtc/base/bind.h"
26 #include "webrtc/base/checks.h" 26 #include "webrtc/base/checks.h"
27 #include "webrtc/base/logging.h" 27 #include "webrtc/base/logging.h"
28 #include "webrtc/base/thread.h" 28 #include "webrtc/base/thread.h"
29 #include "webrtc/base/thread_checker.h" 29 #include "webrtc/base/thread_checker.h"
30 #include "webrtc/base/timeutils.h" 30 #include "webrtc/base/timeutils.h"
31 #include "webrtc/common_types.h" 31 #include "webrtc/common_types.h"
32 #include "webrtc/modules/video_coding/include/video_codec_interface.h" 32 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
33 #include "webrtc/modules/video_coding/utility/h264_bitstream_parser.h" 33 #include "webrtc/modules/video_coding/utility/h264_bitstream_parser.h"
34 #include "webrtc/modules/video_coding/utility/quality_scaler.h"
35 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" 34 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h"
36 #include "webrtc/system_wrappers/include/field_trial.h" 35 #include "webrtc/system_wrappers/include/field_trial.h"
37 #include "webrtc/system_wrappers/include/logcat_trace_context.h" 36 #include "webrtc/system_wrappers/include/logcat_trace_context.h"
38 37
39 using rtc::Bind; 38 using rtc::Bind;
40 using rtc::Thread; 39 using rtc::Thread;
41 using rtc::ThreadManager; 40 using rtc::ThreadManager;
42 41
43 using webrtc::CodecSpecificInfo; 42 using webrtc::CodecSpecificInfo;
44 using webrtc::EncodedImage; 43 using webrtc::EncodedImage;
45 using webrtc::VideoFrame; 44 using webrtc::VideoFrame;
46 using webrtc::RTPFragmentationHeader; 45 using webrtc::RTPFragmentationHeader;
47 using webrtc::VideoCodec; 46 using webrtc::VideoCodec;
48 using webrtc::VideoCodecType; 47 using webrtc::VideoCodecType;
49 using webrtc::kVideoCodecH264; 48 using webrtc::kVideoCodecH264;
50 using webrtc::kVideoCodecVP8; 49 using webrtc::kVideoCodecVP8;
51 using webrtc::kVideoCodecVP9; 50 using webrtc::kVideoCodecVP9;
52 using webrtc::QualityScaler;
53 51
54 namespace webrtc_jni { 52 namespace webrtc_jni {
55 53
56 // H.264 start code length. 54 // H.264 start code length.
57 #define H264_SC_LENGTH 4 55 #define H264_SC_LENGTH 4
58 // Maximum allowed NALUs in one output frame. 56 // Maximum allowed NALUs in one output frame.
59 #define MAX_NALUS_PERFRAME 32 57 #define MAX_NALUS_PERFRAME 32
60 // Maximum supported HW video encoder resolution. 58 // Maximum supported HW video encoder resolution.
61 #define MAX_VIDEO_WIDTH 1280 59 #define MAX_VIDEO_WIDTH 1280
62 #define MAX_VIDEO_HEIGHT 1280 60 #define MAX_VIDEO_HEIGHT 1280
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 const int64_t frame_input_time_ms); 147 const int64_t frame_input_time_ms);
150 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, 148 bool EncodeByteBufferOnCodecThread(JNIEnv* jni,
151 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); 149 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index);
152 bool EncodeTextureOnCodecThread(JNIEnv* jni, 150 bool EncodeTextureOnCodecThread(JNIEnv* jni,
153 bool key_frame, const webrtc::VideoFrame& frame); 151 bool key_frame, const webrtc::VideoFrame& frame);
154 152
155 int32_t RegisterEncodeCompleteCallbackOnCodecThread( 153 int32_t RegisterEncodeCompleteCallbackOnCodecThread(
156 webrtc::EncodedImageCallback* callback); 154 webrtc::EncodedImageCallback* callback);
157 int32_t ReleaseOnCodecThread(); 155 int32_t ReleaseOnCodecThread();
158 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); 156 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate);
159 void OnDroppedFrameOnCodecThread();
160 157
161 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. 158 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members.
162 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); 159 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info);
163 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); 160 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info);
164 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); 161 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info);
165 jlong GetOutputBufferInfoPresentationTimestampUs( 162 jlong GetOutputBufferInfoPresentationTimestampUs(
166 JNIEnv* jni, jobject j_output_buffer_info); 163 JNIEnv* jni, jobject j_output_buffer_info);
167 164
168 // Deliver any outputs pending in the MediaCodec to our |callback_| and return 165 // Deliver any outputs pending in the MediaCodec to our |callback_| and return
169 // true on success. 166 // true on success.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 // |input_frame_infos_|. 249 // |input_frame_infos_|.
253 webrtc::VideoRotation output_rotation_; // Last output frame rotation from 250 webrtc::VideoRotation output_rotation_; // Last output frame rotation from
254 // |input_frame_infos_|. 251 // |input_frame_infos_|.
255 // Frame size in bytes fed to MediaCodec. 252 // Frame size in bytes fed to MediaCodec.
256 int yuv_size_; 253 int yuv_size_;
257 // True only when between a callback_->Encoded() call return a positive value 254 // True only when between a callback_->Encoded() call return a positive value
258 // and the next Encode() call being ignored. 255 // and the next Encode() call being ignored.
259 bool drop_next_input_frame_; 256 bool drop_next_input_frame_;
260 // Global references; must be deleted in Release(). 257 // Global references; must be deleted in Release().
261 std::vector<jobject> input_buffers_; 258 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_; 259 webrtc::H264BitstreamParser h264_bitstream_parser_;
268 260
269 // VP9 variables to populate codec specific structure. 261 // VP9 variables to populate codec specific structure.
270 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for 262 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for
271 // non-flexible VP9 mode. 263 // non-flexible VP9 mode.
272 uint8_t tl0_pic_idx_; 264 uint8_t tl0_pic_idx_;
273 size_t gof_idx_; 265 size_t gof_idx_;
274 266
275 // EGL context - owned by factory, should not be allocated/destroyed 267 // EGL context - owned by factory, should not be allocated/destroyed
276 // by MediaCodecVideoEncoder. 268 // by MediaCodecVideoEncoder.
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; 367 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
376 } 368 }
377 // Factory should guard against other codecs being used with us. 369 // Factory should guard against other codecs being used with us.
378 RTC_CHECK(codec_settings->codecType == codecType_) 370 RTC_CHECK(codec_settings->codecType == codecType_)
379 << "Unsupported codec " << codec_settings->codecType << " for " 371 << "Unsupported codec " << codec_settings->codecType << " for "
380 << codecType_; 372 << codecType_;
381 373
382 codec_mode_ = codec_settings->mode; 374 codec_mode_ = codec_settings->mode;
383 int init_width = codec_settings->width; 375 int init_width = codec_settings->width;
384 int init_height = codec_settings->height; 376 int init_height = codec_settings->height;
385 // Scaling is disabled for VP9, but optionally enabled for VP8.
386 // TODO(pbos): Extract automaticResizeOn out of VP8 settings.
387 scale_ = false;
388 if (codecType_ == kVideoCodecVP8) {
389 scale_ = codec_settings->codecSpecific.VP8.automaticResizeOn;
390 } else if (codecType_ != kVideoCodecVP9) {
391 scale_ = true;
392 }
393
394 ALOGD << "InitEncode request: " << init_width << " x " << init_height; 377 ALOGD << "InitEncode request: " << init_width << " x " << init_height;
395 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled");
396
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 378
414 return codec_thread_->Invoke<int32_t>( 379 return codec_thread_->Invoke<int32_t>(
415 RTC_FROM_HERE, 380 RTC_FROM_HERE,
416 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, 381 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width,
417 init_height, codec_settings->startBitrate, 382 init_height, codec_settings->startBitrate,
418 codec_settings->maxFramerate, 383 codec_settings->maxFramerate,
419 codec_settings->expect_encode_from_texture)); 384 codec_settings->expect_encode_from_texture));
420 } 385 }
421 386
422 int32_t MediaCodecVideoEncoder::Encode( 387 int32_t MediaCodecVideoEncoder::Encode(
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 << ". TS: " << (int)(current_timestamp_us_ / 1000) 602 << ". TS: " << (int)(current_timestamp_us_ / 1000)
638 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ 603 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_
639 << ". Kbps: " << last_set_bitrate_kbps_; 604 << ". Kbps: " << last_set_bitrate_kbps_;
640 } 605 }
641 606
642 if (drop_next_input_frame_) { 607 if (drop_next_input_frame_) {
643 ALOGW << "Encoder drop frame - failed callback."; 608 ALOGW << "Encoder drop frame - failed callback.";
644 drop_next_input_frame_ = false; 609 drop_next_input_frame_ = false;
645 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; 610 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_;
646 frames_dropped_media_encoder_++; 611 frames_dropped_media_encoder_++;
647 OnDroppedFrameOnCodecThread();
648 return WEBRTC_VIDEO_CODEC_OK; 612 return WEBRTC_VIDEO_CODEC_OK;
649 } 613 }
650 614
651 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; 615 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count";
652 616
653 // Check if we accumulated too many frames in encoder input buffers and drop 617 // Check if we accumulated too many frames in encoder input buffers and drop
654 // frame if so. 618 // frame if so.
655 if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) { 619 if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) {
656 ALOGD << "Already " << input_frame_infos_.size() 620 ALOGD << "Already " << input_frame_infos_.size()
657 << " frames in the queue, dropping" 621 << " frames in the queue, dropping"
658 << ". TS: " << (int)(current_timestamp_us_ / 1000) 622 << ". TS: " << (int)(current_timestamp_us_ / 1000)
659 << ". Fps: " << last_set_fps_ 623 << ". Fps: " << last_set_fps_
660 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; 624 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_;
661 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; 625 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_;
662 consecutive_full_queue_frame_drops_++; 626 consecutive_full_queue_frame_drops_++;
663 if (consecutive_full_queue_frame_drops_ >= 627 if (consecutive_full_queue_frame_drops_ >=
664 ENCODER_STALL_FRAMEDROP_THRESHOLD) { 628 ENCODER_STALL_FRAMEDROP_THRESHOLD) {
665 ALOGE << "Encoder got stuck. Reset."; 629 ALOGE << "Encoder got stuck. Reset.";
666 ResetCodecOnCodecThread(); 630 ResetCodecOnCodecThread();
667 return WEBRTC_VIDEO_CODEC_ERROR; 631 return WEBRTC_VIDEO_CODEC_ERROR;
668 } 632 }
669 frames_dropped_media_encoder_++; 633 frames_dropped_media_encoder_++;
670 OnDroppedFrameOnCodecThread();
671 return WEBRTC_VIDEO_CODEC_OK; 634 return WEBRTC_VIDEO_CODEC_OK;
672 } 635 }
673 consecutive_full_queue_frame_drops_ = 0; 636 consecutive_full_queue_frame_drops_ = 0;
674 637
675 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( 638 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer(
676 frame.video_frame_buffer()); 639 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 640
697 VideoFrame input_frame(input_buffer, frame.timestamp(), 641 VideoFrame input_frame(input_buffer, frame.timestamp(),
698 frame.render_time_ms(), frame.rotation()); 642 frame.render_time_ms(), frame.rotation());
699 643
700 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { 644 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) {
701 ALOGE << "Failed to reconfigure encoder."; 645 ALOGE << "Failed to reconfigure encoder.";
702 return WEBRTC_VIDEO_CODEC_ERROR; 646 return WEBRTC_VIDEO_CODEC_ERROR;
703 } 647 }
704 648
705 const bool key_frame = 649 const bool key_frame =
706 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; 650 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame;
707 bool encode_status = true; 651 bool encode_status = true;
708 if (!input_frame.video_frame_buffer()->native_handle()) { 652 if (!input_frame.video_frame_buffer()->native_handle()) {
709 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, 653 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_,
710 j_dequeue_input_buffer_method_); 654 j_dequeue_input_buffer_method_);
711 CHECK_EXCEPTION(jni); 655 CHECK_EXCEPTION(jni);
712 if (j_input_buffer_index == -1) { 656 if (j_input_buffer_index == -1) {
713 // Video codec falls behind - no input buffer available. 657 // Video codec falls behind - no input buffer available.
714 ALOGW << "Encoder drop frame - no input buffers available"; 658 ALOGW << "Encoder drop frame - no input buffers available";
715 if (frames_received_ > 1) { 659 if (frames_received_ > 1) {
716 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; 660 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_;
717 frames_dropped_media_encoder_++; 661 frames_dropped_media_encoder_++;
718 OnDroppedFrameOnCodecThread();
719 } else { 662 } else {
720 // Input buffers are not ready after codec initialization, HW is still 663 // Input buffers are not ready after codec initialization, HW is still
721 // allocating thme - this is expected and should not result in drop 664 // allocating thme - this is expected and should not result in drop
722 // frame report. 665 // frame report.
723 frames_received_ = 0; 666 frames_received_ = 0;
724 } 667 }
725 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. 668 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887.
726 } else if (j_input_buffer_index == -2) { 669 } else if (j_input_buffer_index == -2) {
727 ResetCodecOnCodecThread(); 670 ResetCodecOnCodecThread();
728 return WEBRTC_VIDEO_CODEC_ERROR; 671 return WEBRTC_VIDEO_CODEC_ERROR;
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 818
876 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, 819 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate,
877 uint32_t frame_rate) { 820 uint32_t frame_rate) {
878 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); 821 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
879 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? 822 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ?
880 frame_rate : MAX_ALLOWED_VIDEO_FPS; 823 frame_rate : MAX_ALLOWED_VIDEO_FPS;
881 if (last_set_bitrate_kbps_ == new_bit_rate && 824 if (last_set_bitrate_kbps_ == new_bit_rate &&
882 last_set_fps_ == frame_rate) { 825 last_set_fps_ == frame_rate) {
883 return WEBRTC_VIDEO_CODEC_OK; 826 return WEBRTC_VIDEO_CODEC_OK;
884 } 827 }
885 if (scale_) {
886 quality_scaler_.ReportFramerate(frame_rate);
887 }
888 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 828 JNIEnv* jni = AttachCurrentThreadIfNeeded();
889 ScopedLocalRefFrame local_ref_frame(jni); 829 ScopedLocalRefFrame local_ref_frame(jni);
890 if (new_bit_rate > 0) { 830 if (new_bit_rate > 0) {
891 last_set_bitrate_kbps_ = new_bit_rate; 831 last_set_bitrate_kbps_ = new_bit_rate;
892 } 832 }
893 if (frame_rate > 0) { 833 if (frame_rate > 0) {
894 last_set_fps_ = frame_rate; 834 last_set_fps_ = frame_rate;
895 } 835 }
896 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, 836 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
897 j_set_rates_method_, 837 j_set_rates_method_,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 std::unique_ptr<webrtc::EncodedImage> image( 920 std::unique_ptr<webrtc::EncodedImage> image(
981 new webrtc::EncodedImage(payload, payload_size, payload_size)); 921 new webrtc::EncodedImage(payload, payload_size, payload_size));
982 image->_encodedWidth = width_; 922 image->_encodedWidth = width_;
983 image->_encodedHeight = height_; 923 image->_encodedHeight = height_;
984 image->_timeStamp = output_timestamp_; 924 image->_timeStamp = output_timestamp_;
985 image->capture_time_ms_ = output_render_time_ms_; 925 image->capture_time_ms_ = output_render_time_ms_;
986 image->rotation_ = output_rotation_; 926 image->rotation_ = output_rotation_;
987 image->_frameType = 927 image->_frameType =
988 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); 928 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta);
989 image->_completeFrame = true; 929 image->_completeFrame = true;
990 image->adapt_reason_.quality_resolution_downscales =
perkj_webrtc 2016/10/07 07:41:15 does this cl take stats into account?
kthelgason 2016/10/07 09:34:48 Nope, that's still to be addressed. In any case th
991 scale_ ? quality_scaler_.downscale_shift() : -1;
992
993 webrtc::CodecSpecificInfo info; 930 webrtc::CodecSpecificInfo info;
994 memset(&info, 0, sizeof(info)); 931 memset(&info, 0, sizeof(info));
995 info.codecType = codecType_; 932 info.codecType = codecType_;
996 if (codecType_ == kVideoCodecVP8) { 933 if (codecType_ == kVideoCodecVP8) {
997 info.codecSpecific.VP8.pictureId = picture_id_; 934 info.codecSpecific.VP8.pictureId = picture_id_;
998 info.codecSpecific.VP8.nonReference = false; 935 info.codecSpecific.VP8.nonReference = false;
999 info.codecSpecific.VP8.simulcastIdx = 0; 936 info.codecSpecific.VP8.simulcastIdx = 0;
1000 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; 937 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx;
1001 info.codecSpecific.VP8.layerSync = false; 938 info.codecSpecific.VP8.layerSync = false;
1002 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; 939 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx;
(...skipping 26 matching lines...) Expand all
1029 966
1030 // Generate a header describing a single fragment. 967 // Generate a header describing a single fragment.
1031 webrtc::RTPFragmentationHeader header; 968 webrtc::RTPFragmentationHeader header;
1032 memset(&header, 0, sizeof(header)); 969 memset(&header, 0, sizeof(header));
1033 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { 970 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) {
1034 header.VerifyAndAllocateFragmentationHeader(1); 971 header.VerifyAndAllocateFragmentationHeader(1);
1035 header.fragmentationOffset[0] = 0; 972 header.fragmentationOffset[0] = 0;
1036 header.fragmentationLength[0] = image->_length; 973 header.fragmentationLength[0] = image->_length;
1037 header.fragmentationPlType[0] = 0; 974 header.fragmentationPlType[0] = 0;
1038 header.fragmentationTimeDiff[0] = 0; 975 header.fragmentationTimeDiff[0] = 0;
1039 if (codecType_ == kVideoCodecVP8 && scale_) { 976 if (codecType_ == kVideoCodecVP8) {
1040 int qp; 977 int qp;
1041 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { 978 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) {
1042 current_acc_qp_ += qp; 979 current_acc_qp_ += qp;
1043 quality_scaler_.ReportQP(qp);
1044 image->qp_ = qp; 980 image->qp_ = qp;
1045 } 981 }
1046 } 982 }
1047 } else if (codecType_ == kVideoCodecH264) { 983 } else if (codecType_ == kVideoCodecH264) {
1048 if (scale_) { 984 h264_bitstream_parser_.ParseBitstream(payload, payload_size);
1049 h264_bitstream_parser_.ParseBitstream(payload, payload_size); 985 int qp;
1050 int qp; 986 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) {
1051 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { 987 current_acc_qp_ += qp;
1052 current_acc_qp_ += qp; 988 image->qp_ = qp;
1053 quality_scaler_.ReportQP(qp);
1054 }
1055 } 989 }
1056 // For H.264 search for start codes. 990 // For H.264 search for start codes.
1057 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; 991 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {};
1058 int32_t scPositionsLength = 0; 992 int32_t scPositionsLength = 0;
1059 int32_t scPosition = 0; 993 int32_t scPosition = 0;
1060 while (scPositionsLength < MAX_NALUS_PERFRAME) { 994 while (scPositionsLength < MAX_NALUS_PERFRAME) {
1061 int32_t naluPosition = NextNaluPosition( 995 int32_t naluPosition = NextNaluPosition(
1062 payload + scPosition, payload_size - scPosition); 996 payload + scPosition, payload_size - scPosition);
1063 if (naluPosition < 0) { 997 if (naluPosition < 0) {
1064 break; 998 break;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 if (head[3] != 0x01) { // got 000000xx 1115 if (head[3] != 0x01) { // got 000000xx
1182 head++; // xx != 1, continue searching. 1116 head++; // xx != 1, continue searching.
1183 continue; 1117 continue;
1184 } 1118 }
1185 return (int32_t)(head - buffer); 1119 return (int32_t)(head - buffer);
1186 } 1120 }
1187 return -1; 1121 return -1;
1188 } 1122 }
1189 1123
1190 void MediaCodecVideoEncoder::OnDroppedFrame() { 1124 void MediaCodecVideoEncoder::OnDroppedFrame() {
1191 // Methods running on the codec thread should call OnDroppedFrameOnCodecThread
1192 // directly.
1193 RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); 1125 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 } 1126 }
1205 1127
1206 const char* MediaCodecVideoEncoder::ImplementationName() const { 1128 const char* MediaCodecVideoEncoder::ImplementationName() const {
1207 return "MediaCodec"; 1129 return "MediaCodec";
1208 } 1130 }
1209 1131
1210 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() 1132 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory()
1211 : egl_context_(nullptr) { 1133 : egl_context_(nullptr) {
1212 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 1134 JNIEnv* jni = AttachCurrentThreadIfNeeded();
1213 ScopedLocalRefFrame local_ref_frame(jni); 1135 ScopedLocalRefFrame local_ref_frame(jni);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1290 return supported_codecs_; 1212 return supported_codecs_;
1291 } 1213 }
1292 1214
1293 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( 1215 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder(
1294 webrtc::VideoEncoder* encoder) { 1216 webrtc::VideoEncoder* encoder) {
1295 ALOGD << "Destroy video encoder."; 1217 ALOGD << "Destroy video encoder.";
1296 delete encoder; 1218 delete encoder;
1297 } 1219 }
1298 1220
1299 } // namespace webrtc_jni 1221 } // namespace webrtc_jni
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698