1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
media / gpu / vaapi / vaapi_video_encoder_delegate.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/gpu/vaapi/vaapi_video_encoder_delegate.h"
#include <va/va.h>
#include "base/memory/ref_counted_memory.h"
#include "base/trace_event/trace_event.h"
#include "media/base/media_util.h"
#include "media/base/video_frame.h"
#include "media/gpu/codec_picture.h"
#include "media/gpu/gpu_video_encode_accelerator_helpers.h"
#include "media/gpu/macros.h"
#include "media/gpu/vaapi/vaapi_utils.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "media/video/video_encode_accelerator.h"
namespace media {
VaapiVideoEncoderDelegate::EncodeJob::EncodeJob(
bool keyframe,
base::TimeDelta timestamp,
uint8_t spatial_index,
bool end_of_picture,
VASurfaceID input_surface_id,
scoped_refptr<CodecPicture> picture,
std::unique_ptr<ScopedVABuffer> coded_buffer)
: keyframe_(keyframe),
timestamp_(timestamp),
spatial_index_(spatial_index),
end_of_picture_(end_of_picture),
input_surface_id_(input_surface_id),
picture_(std::move(picture)),
coded_buffer_(std::move(coded_buffer)) {
DCHECK(picture_);
DCHECK(coded_buffer_);
}
VaapiVideoEncoderDelegate::EncodeJob::~EncodeJob() = default;
VaapiVideoEncoderDelegate::EncodeResult
VaapiVideoEncoderDelegate::EncodeJob::CreateEncodeResult(
const BitstreamBufferMetadata& metadata) && {
return EncodeResult(std::move(coded_buffer_), metadata);
}
base::TimeDelta VaapiVideoEncoderDelegate::EncodeJob::timestamp() const {
return timestamp_;
}
uint8_t VaapiVideoEncoderDelegate::EncodeJob::spatial_index() const {
return spatial_index_;
}
bool VaapiVideoEncoderDelegate::EncodeJob::end_of_picture() const {
return end_of_picture_;
}
VABufferID VaapiVideoEncoderDelegate::EncodeJob::coded_buffer_id() const {
CHECK(coded_buffer_);
return coded_buffer_->id();
}
VASurfaceID VaapiVideoEncoderDelegate::EncodeJob::input_surface_id() const {
return input_surface_id_;
}
const scoped_refptr<CodecPicture>&
VaapiVideoEncoderDelegate::EncodeJob::picture() const {
return picture_;
}
VaapiVideoEncoderDelegate::EncodeResult::EncodeResult(
std::unique_ptr<ScopedVABuffer> coded_buffer,
const BitstreamBufferMetadata& metadata)
: coded_buffer_(std::move(coded_buffer)), metadata_(metadata) {}
VaapiVideoEncoderDelegate::EncodeResult::~EncodeResult() = default;
VaapiVideoEncoderDelegate::EncodeResult::EncodeResult(EncodeResult&&) = default;
VaapiVideoEncoderDelegate::EncodeResult&
VaapiVideoEncoderDelegate::EncodeResult::operator=(EncodeResult&&) = default;
VABufferID VaapiVideoEncoderDelegate::EncodeResult::coded_buffer_id() const {
CHECK(coded_buffer_);
return coded_buffer_->id();
}
const BitstreamBufferMetadata&
VaapiVideoEncoderDelegate::EncodeResult::metadata() const {
return metadata_;
}
VaapiVideoEncoderDelegate::VaapiVideoEncoderDelegate(
scoped_refptr<VaapiWrapper> vaapi_wrapper,
base::RepeatingClosure error_cb)
: vaapi_wrapper_(vaapi_wrapper), error_cb_(error_cb) {}
VaapiVideoEncoderDelegate::~VaapiVideoEncoderDelegate() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
size_t VaapiVideoEncoderDelegate::GetBitstreamBufferSize() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetEncodeBitstreamBufferSize(GetCodedSize());
}
void VaapiVideoEncoderDelegate::BitrateControlUpdate(
const BitstreamBufferMetadata& metadata) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
bool VaapiVideoEncoderDelegate::Encode(EncodeJob& encode_job) {
TRACE_EVENT0("media,gpu", "VAVEDelegate::Encode");
PrepareEncodeJobResult result = PrepareEncodeJob(encode_job);
if (result == PrepareEncodeJobResult::kFail) {
VLOGF(1) << "Failed preparing an encode job";
return false;
}
if (result == PrepareEncodeJobResult::kDrop) {
// An encoder must not drop a keyframe.
CHECK(!encode_job.IsKeyframeRequested());
DVLOGF(3) << "Drop frame";
encode_job.DropFrame();
return true;
}
if (!vaapi_wrapper_->ExecuteAndDestroyPendingBuffers(
encode_job.input_surface_id())) {
VLOGF(1) << "Failed to execute encode";
return false;
}
return true;
}
std::optional<VaapiVideoEncoderDelegate::EncodeResult>
VaapiVideoEncoderDelegate::GetEncodeResult(
std::unique_ptr<EncodeJob> encode_job) {
TRACE_EVENT0("media,gpu", "VAVEDelegate::GetEncodeResult");
if (encode_job->IsFrameDropped()) {
return std::make_optional<EncodeResult>(
nullptr, BitstreamBufferMetadata::CreateForDropFrame(
encode_job->timestamp(), encode_job->spatial_index(),
encode_job->end_of_picture()));
}
const VASurfaceID va_surface_id = encode_job->input_surface_id();
const uint64_t encoded_chunk_size = vaapi_wrapper_->GetEncodedChunkSize(
encode_job->coded_buffer_id(), va_surface_id);
if (encoded_chunk_size == 0) {
VLOGF(1) << "Invalid encoded chunk size";
return std::nullopt;
}
auto metadata = GetMetadata(*encode_job, encoded_chunk_size);
BitrateControlUpdate(metadata);
return std::make_optional<EncodeResult>(
std::move(*encode_job).CreateEncodeResult(metadata));
}
} // namespace media