OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/webm/chromeos/webm_encoder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/file_util.h" | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_generic_obj.h" | |
11 #include "libyuv/convert.h" | |
12 #include "libyuv/video_common.h" | |
13 #include "third_party/skia/include/core/SkBitmap.h" | |
14 | |
15 extern "C" { | |
16 // Getting the right degree of C compatibility has been a constant struggle. | |
17 // - Stroustrup, C++ Report, 12(7), July/August 2000. | |
18 #define private priv | |
19 #include "third_party/libvpx/source/libvpx/libmkv/EbmlIDs.h" | |
20 #include "third_party/libvpx/source/libvpx/libmkv/EbmlWriter.h" | |
21 #undef private | |
22 } | |
23 | |
24 // Number of encoder threads to use. | |
25 static const int kNumEncoderThreads = 2; | |
26 | |
27 // Need a fixed size serializer for the track ID. libmkv provides a 64 bit | |
28 // one, but not a 32 bit one. | |
29 static void Ebml_SerializeUnsigned32(EbmlGlobal* ebml, | |
30 unsigned long class_id, | |
31 uint64_t value) { | |
32 uint8 size_serialized = 4 | 0x80; | |
33 Ebml_WriteID(ebml, class_id); | |
34 Ebml_Serialize(ebml, &size_serialized, sizeof(size_serialized), 1); | |
35 Ebml_Serialize(ebml, &value, sizeof(value), 4); | |
36 } | |
37 | |
38 // Wrapper functor for vpx_codec_destroy(). | |
39 class VpxCodecDestroyHelper { | |
40 public: | |
41 void operator()(vpx_codec_ctx_t* codec) { | |
42 vpx_codec_destroy(codec); | |
43 } | |
44 }; | |
45 | |
46 // Wrapper functor for vpx_img_free(). | |
47 class VpxImgFreeHelper { | |
48 public: | |
49 void operator()(vpx_image_t* image) { | |
50 vpx_img_free(image); | |
51 } | |
52 }; | |
53 | |
54 namespace media { | |
55 | |
56 namespace chromeos { | |
57 | |
58 WebmEncoder::WebmEncoder(const FilePath& output_path, | |
59 int bitrate, | |
60 bool realtime) | |
61 : bitrate_(bitrate), | |
62 deadline_(realtime ? VPX_DL_REALTIME : VPX_DL_GOOD_QUALITY), | |
63 output_path_(output_path) { | |
64 ebml_writer_.write_cb = base::Bind( | |
65 &WebmEncoder::EbmlWrite, base::Unretained(this)); | |
66 ebml_writer_.serialize_cb = base::Bind( | |
67 &WebmEncoder::EbmlSerialize, base::Unretained(this)); | |
68 } | |
69 | |
70 bool WebmEncoder::EncodeFromSprite(const SkBitmap& sprite, | |
71 int fps_n, | |
72 int fps_d) { | |
73 DCHECK(!sprite.isNull()); | |
74 DCHECK(!sprite.empty()); | |
75 | |
76 width_ = sprite.width(); | |
77 height_ = sprite.width(); | |
78 fps_.num = fps_n; | |
79 fps_.den = fps_d; | |
80 | |
81 // Sprite is tiled vertically. | |
82 size_t frame_count = sprite.height() / width_; | |
83 | |
84 vpx_image_t image; | |
85 vpx_img_alloc(&image, VPX_IMG_FMT_I420, width_, height_, 16); | |
86 // Ensure that image is freed after return. | |
87 ScopedGenericObj<vpx_image_t*, VpxImgFreeHelper> image_ptr(&image); | |
88 | |
89 const vpx_codec_iface_t* codec_iface = vpx_codec_vp8_cx(); | |
90 DCHECK(codec_iface); | |
scherkus (not reviewing)
2012/07/23 22:23:43
should these be errors be handled?
Ivan Korotkov
2012/07/23 22:57:48
These 2 shouldn't normally happen and denote a pro
| |
91 vpx_codec_err_t ret = vpx_codec_enc_config_default(codec_iface, &config_, 0); | |
92 DCHECK_EQ(VPX_CODEC_OK, ret); | |
scherkus (not reviewing)
2012/07/23 22:23:43
ditto?
| |
93 | |
94 config_.rc_target_bitrate = bitrate_; | |
95 config_.g_w = width_; | |
96 config_.g_h = height_; | |
97 config_.g_pass = VPX_RC_ONE_PASS; | |
98 config_.g_profile = 0; // Default profile. | |
99 config_.g_threads = kNumEncoderThreads; | |
100 config_.rc_min_quantizer = 0; | |
101 config_.rc_max_quantizer = 63; // Maximum possible range. | |
102 config_.g_timebase.num = fps_.den; | |
103 config_.g_timebase.den = fps_.num; | |
104 config_.kf_mode = VPX_KF_AUTO; // Auto key frames. | |
105 | |
106 vpx_codec_ctx_t codec; | |
107 ret = vpx_codec_enc_init(&codec, codec_iface, &config_, 0); | |
108 if (VPX_CODEC_OK != ret) | |
scherkus (not reviewing)
2012/07/23 22:23:43
nit: flip comparison -- we don't compare against a
Ivan Korotkov
2012/07/23 22:57:48
Done.
| |
109 return false; | |
110 // Ensure that codec context is freed after return. | |
111 ScopedGenericObj<vpx_codec_ctx_t*, VpxCodecDestroyHelper> codec_ptr(&codec); | |
112 | |
113 SkAutoLockPixels lock_sprite(sprite); | |
114 | |
115 const uint8* src = reinterpret_cast<const uint8*>(sprite.getAddr32(0, 0)); | |
116 size_t src_frame_size = sprite.getSize(); | |
117 int crop_y = 0; | |
118 | |
119 if (!WriteWebmHeader()) | |
120 return false; | |
121 | |
122 for (size_t frame = 0; frame < frame_count; ++frame) { | |
123 int res = libyuv::ConvertToI420( | |
124 src, src_frame_size, | |
125 image.planes[VPX_PLANE_Y], image.stride[VPX_PLANE_Y], | |
126 image.planes[VPX_PLANE_U], image.stride[VPX_PLANE_U], | |
127 image.planes[VPX_PLANE_V], image.stride[VPX_PLANE_V], | |
128 0, crop_y, // src origin | |
129 width_, sprite.height(), // src size | |
130 width_, height_, // dest size | |
131 libyuv::kRotate0, | |
132 libyuv::FOURCC_ARGB); | |
133 if (res) | |
134 return false; | |
135 crop_y += height_; | |
136 | |
137 ret = vpx_codec_encode(&codec, &image, frame, 1, 0, deadline_); | |
138 if (VPX_CODEC_OK != ret) | |
scherkus (not reviewing)
2012/07/23 22:23:43
ditto
Ivan Korotkov
2012/07/23 22:57:48
Done.
| |
139 return false; | |
140 | |
141 vpx_codec_iter_t iter = NULL; | |
142 const vpx_codec_cx_pkt_t* packet; | |
143 while ((packet = vpx_codec_get_cx_data(&codec, &iter))) { | |
144 if (packet->kind == VPX_CODEC_CX_FRAME_PKT) | |
145 WriteWebmBlock(packet); | |
146 } | |
147 } | |
148 | |
149 return WriteWebmFooter(); | |
150 } | |
151 | |
152 bool WebmEncoder::WriteWebmHeader() { | |
153 output_ = file_util::OpenFile(output_path_, "wb"); | |
154 if (!output_) | |
155 return false; | |
156 | |
157 // Global header. | |
158 StartSubElement(EBML); | |
159 { | |
160 Ebml_SerializeUnsigned(&ebml_writer_, EBMLVersion, 1); | |
161 Ebml_SerializeUnsigned(&ebml_writer_, EBMLReadVersion, 1); | |
162 Ebml_SerializeUnsigned(&ebml_writer_, EBMLMaxIDLength, 4); | |
163 Ebml_SerializeUnsigned(&ebml_writer_, EBMLMaxSizeLength, 8); | |
164 Ebml_SerializeString(&ebml_writer_, DocType, "webm"); | |
165 Ebml_SerializeUnsigned(&ebml_writer_, DocTypeVersion, 2); | |
166 Ebml_SerializeUnsigned(&ebml_writer_, DocTypeReadVersion, 2); | |
167 } | |
168 EndSubElement(); // EBML | |
169 | |
170 // Single segment with a video track. | |
171 StartSubElement(Segment); | |
172 { | |
173 StartSubElement(Info); | |
174 { | |
175 // All timecodes in the segment will be expressed in milliseconds. | |
176 Ebml_SerializeUnsigned(&ebml_writer_, TimecodeScale, 1000000); | |
177 } | |
178 EndSubElement(); // Info | |
179 | |
180 StartSubElement(Tracks); | |
181 { | |
182 StartSubElement(TrackEntry); | |
183 { | |
184 Ebml_SerializeUnsigned(&ebml_writer_, TrackNumber, 1); | |
185 Ebml_SerializeUnsigned32(&ebml_writer_, TrackUID, 1); | |
186 Ebml_SerializeUnsigned(&ebml_writer_, TrackType, 1); // Video | |
187 Ebml_SerializeString(&ebml_writer_, CodecID, "V_VP8"); | |
188 | |
189 StartSubElement(Video); | |
190 { | |
191 Ebml_SerializeUnsigned(&ebml_writer_, PixelWidth, width_); | |
192 Ebml_SerializeUnsigned(&ebml_writer_, PixelHeight, height_); | |
193 Ebml_SerializeUnsigned(&ebml_writer_, StereoMode, 0); // Mono | |
194 float fps = static_cast<float>(fps_.num) / fps_.den; | |
195 Ebml_SerializeFloat(&ebml_writer_, FrameRate, fps); | |
196 } | |
197 EndSubElement(); // Video | |
198 } | |
199 EndSubElement(); // TrackEntry | |
200 } | |
201 EndSubElement(); // Tracks | |
202 | |
203 StartSubElement(Cluster); { | |
204 Ebml_SerializeUnsigned(&ebml_writer_, Timecode, 0); | |
205 } // Cluster left open. | |
206 } // Segment left open. | |
207 | |
208 return true; | |
209 } | |
210 | |
211 void WebmEncoder::WriteWebmBlock(const vpx_codec_cx_pkt_t* packet) { | |
212 bool is_keyframe = packet->data.frame.flags & VPX_FRAME_IS_KEY; | |
213 int64_t pts_ms = 1000 * packet->data.frame.pts * fps_.den / fps_.num; | |
214 | |
215 DVLOG(1) << "Video packet @" << pts_ms << " ms " | |
216 << packet->data.frame.sz << " bytes " | |
217 << (is_keyframe ? "K" : ""); | |
218 | |
219 Ebml_WriteID(&ebml_writer_, SimpleBlock); | |
220 | |
221 uint32 block_length = (packet->data.frame.sz + 4) | 0x10000000; | |
222 EbmlSerializeHelper(&block_length, 4); | |
223 | |
224 uint8 track_number = 1 | 0x80; | |
225 EbmlSerializeHelper(&track_number, 1); | |
226 | |
227 EbmlSerializeHelper(&pts_ms, 2); | |
228 | |
229 uint8 flags = 0; | |
230 if (is_keyframe) | |
231 flags |= 0x80; | |
232 if (packet->data.frame.flags & VPX_FRAME_IS_INVISIBLE) | |
233 flags |= 0x08; | |
234 EbmlSerializeHelper(&flags, 1); | |
235 | |
236 EbmlWrite(packet->data.frame.buf, packet->data.frame.sz); | |
237 } | |
238 | |
239 bool WebmEncoder::WriteWebmFooter() { | |
240 EndSubElement(); // Cluster | |
241 EndSubElement(); // Segment | |
242 DCHECK(ebml_sub_elements_.empty()); | |
243 return file_util::CloseFile(output_); | |
244 } | |
245 | |
246 void WebmEncoder::StartSubElement(unsigned long class_id) { | |
247 Ebml_WriteID(&ebml_writer_, class_id); | |
248 ebml_sub_elements_.push(ftell(output_)); | |
249 static const uint64_t kUnknownLen = 0x01FFFFFFFFFFFFFFLLU; | |
250 EbmlSerializeHelper(&kUnknownLen, 8); | |
251 } | |
252 | |
253 void WebmEncoder::EndSubElement() { | |
254 DCHECK(!ebml_sub_elements_.empty()); | |
255 | |
256 long int end_pos = ftell(output_); | |
257 long int start_pos = ebml_sub_elements_.top(); | |
258 ebml_sub_elements_.pop(); | |
259 | |
260 uint64_t size = (end_pos - start_pos - 8) | 0x0100000000000000ULL; | |
261 // Seek to the beginning of the sub-element and patch in the calculated size. | |
262 if (fseek(output_, start_pos, SEEK_SET)) | |
263 LOG(ERROR) << "Error writing to " << output_path_.value(); | |
264 EbmlSerializeHelper(&size, 8); | |
265 | |
266 // Restore write position. | |
267 if (fseek(output_, end_pos, SEEK_SET)) | |
268 LOG(ERROR) << "Error writing to " << output_path_.value(); | |
269 } | |
270 | |
271 void WebmEncoder::EbmlWrite(const void* buffer, | |
272 unsigned long len) { | |
273 if (fwrite(buffer, 1, len, output_) != len) | |
274 LOG(ERROR) << "Error writing to " << output_path_.value(); | |
275 } | |
276 | |
277 template <class T> | |
278 void WebmEncoder::EbmlSerializeHelper(const T* buffer, unsigned long len) { | |
279 for (int i = len - 1; i >= 0; i--) { | |
280 uint8 c = *buffer >> (i * CHAR_BIT); | |
281 EbmlWrite(&c, 1); | |
282 } | |
283 } | |
284 | |
285 void WebmEncoder::EbmlSerialize(const void* buffer, | |
286 int buffer_size, | |
287 unsigned long len) { | |
288 switch (buffer_size) { | |
289 case 1: | |
290 return EbmlSerializeHelper(static_cast<const int8_t*>(buffer), len); | |
291 case 2: | |
292 return EbmlSerializeHelper(static_cast<const int16_t*>(buffer), len); | |
293 case 4: | |
294 return EbmlSerializeHelper(static_cast<const int32_t*>(buffer), len); | |
295 case 8: | |
296 return EbmlSerializeHelper(static_cast<const int64_t*>(buffer), len); | |
297 default: | |
298 NOTREACHED() << "Invalid EbmlSerialize length: " << len; | |
299 } | |
300 } | |
301 | |
302 } // namespace chromeos | |
303 | |
304 } // namespace media | |
OLD | NEW |