Skip to content

Commit

Permalink
ppapi: VideoEncoder: augment precision of the IVF container
Browse files Browse the repository at this point in the history
Use millisecond timestamps in the IVF container rather than having
1.0/30 units of time. This prevents a drift in the replay of the
video because the recorder might not schedule frames at exactly
33.3333ms.

BUG=503153
TEST=record a 2 minutes vp8 video using the video encoder NaCl SDK and verify the produced video last exactly 2 minutes

Review URL: https://codereview.chromium.org/1226203010

Cr-Commit-Position: refs/heads/master@{#338694}
  • Loading branch information
llandwerlin-intel authored and Commit bot committed Jul 14, 2015
1 parent 0c5a55a commit 0141e78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
17 changes: 12 additions & 5 deletions native_client_sdk/src/examples/api/video_encode/video_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <sstream>
Expand Down Expand Up @@ -88,7 +89,7 @@ uint32_t IVFWriter::WriteFileHeader(uint8_t* mem,
PutLE32(mem + 8, fourcc('V', 'P', '8', '0')); // fourcc
PutLE16(mem + 12, static_cast<uint16_t>(width)); // width
PutLE16(mem + 14, static_cast<uint16_t>(height)); // height
PutLE32(mem + 16, 30); // rate
PutLE32(mem + 16, 1000); // rate
PutLE32(mem + 20, 1); // scale
PutLE32(mem + 24, 0xffffffff); // length
PutLE32(mem + 28, 0); // unused
Expand Down Expand Up @@ -180,6 +181,8 @@ class VideoEncoderInstance : public pp::Instance {
pp::Size encoder_size_;
uint32_t encoded_frames_;

std::deque<uint64_t> frames_timestamps_;

pp::VideoFrame current_track_frame_;

IVFWriter ivf_writer_;
Expand Down Expand Up @@ -308,6 +311,7 @@ void VideoEncoderInstance::OnEncoderProbed(

void VideoEncoderInstance::StartEncoder() {
video_encoder_ = pp::VideoEncoder(this);
frames_timestamps_.clear();

int32_t error = video_encoder_.Initialize(
frame_format_, frame_size_, video_profile_, 2000000,
Expand Down Expand Up @@ -425,6 +429,8 @@ int32_t VideoEncoderInstance::CopyVideoFrame(pp::VideoFrame dest,
}

void VideoEncoderInstance::EncodeFrame(const pp::VideoFrame& frame) {
frames_timestamps_.push_back(
static_cast<uint64_t>(frame.GetTimestamp() * 1000));
video_encoder_.Encode(
frame, PP_FALSE,
callback_factory_.NewCallback(&VideoEncoderInstance::OnEncodeDone));
Expand Down Expand Up @@ -552,10 +558,11 @@ void VideoEncoderInstance::PostDataMessage(const void* buffer, uint32_t size) {
size + ivf_writer_.GetFrameHeaderSize());
data_ptr = static_cast<uint8_t*>(array_buffer.Map());
}
data_offset = frame_offset +
ivf_writer_.WriteFrameHeader(data_ptr + frame_offset,
encoded_frames_,
size);
uint64_t timestamp = frames_timestamps_.front();
frames_timestamps_.pop_front();
data_offset =
frame_offset +
ivf_writer_.WriteFrameHeader(data_ptr + frame_offset, timestamp, size);
} else {
array_buffer = pp::VarArrayBuffer(size);
data_ptr = static_cast<uint8_t*>(array_buffer.Map());
Expand Down
17 changes: 12 additions & 5 deletions ppapi/examples/video_encode/video_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <sstream>
Expand Down Expand Up @@ -86,7 +87,7 @@ uint32_t IVFWriter::WriteFileHeader(uint8_t* mem,
PutLE32(mem + 8, fourcc('V', 'P', '8', '0')); // fourcc
PutLE16(mem + 12, static_cast<uint16_t>(width)); // width
PutLE16(mem + 14, static_cast<uint16_t>(height)); // height
PutLE32(mem + 16, 30); // rate
PutLE32(mem + 16, 1000); // rate
PutLE32(mem + 20, 1); // scale
PutLE32(mem + 24, 0xffffffff); // length
PutLE32(mem + 28, 0); // unused
Expand Down Expand Up @@ -179,6 +180,8 @@ class VideoEncoderInstance : public pp::Instance {
pp::Size encoder_size_;
uint32_t encoded_frames_;

std::deque<uint64_t> frames_timestamps_;

pp::VideoFrame current_track_frame_;

IVFWriter ivf_writer_;
Expand Down Expand Up @@ -311,6 +314,7 @@ void VideoEncoderInstance::OnEncoderProbed(

void VideoEncoderInstance::StartEncoder() {
video_encoder_ = pp::VideoEncoder(this);
frames_timestamps_.clear();

int32_t error = video_encoder_.Initialize(
frame_format_, frame_size_, video_profile_, 2000000,
Expand Down Expand Up @@ -428,6 +432,8 @@ int32_t VideoEncoderInstance::CopyVideoFrame(pp::VideoFrame dest,
}

void VideoEncoderInstance::EncodeFrame(const pp::VideoFrame& frame) {
frames_timestamps_.push_back(
static_cast<uint64_t>(frame.GetTimestamp() * 1000));
video_encoder_.Encode(
frame, PP_FALSE,
callback_factory_.NewCallback(&VideoEncoderInstance::OnEncodeDone));
Expand Down Expand Up @@ -555,10 +561,11 @@ void VideoEncoderInstance::PostDataMessage(const void* buffer, uint32_t size) {
size + ivf_writer_.GetFrameHeaderSize());
data_ptr = static_cast<uint8_t*>(array_buffer.Map());
}
data_offset = frame_offset +
ivf_writer_.WriteFrameHeader(data_ptr + frame_offset,
encoded_frames_,
size);
uint64_t timestamp = frames_timestamps_.front();
frames_timestamps_.pop_front();
data_offset =
frame_offset +
ivf_writer_.WriteFrameHeader(data_ptr + frame_offset, timestamp, size);
} else {
array_buffer = pp::VarArrayBuffer(size);
data_ptr = static_cast<uint8_t*>(array_buffer.Map());
Expand Down

0 comments on commit 0141e78

Please sign in to comment.