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 #ifndef MEDIA_WEBM_CHROMEOS_WEBM_ENCODER_H_ | |
6 #define MEDIA_WEBM_CHROMEOS_WEBM_ENCODER_H_ | |
7 | |
8 #include <stack> | |
9 #include <stdio.h> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "media/base/media_export.h" | |
13 #include "media/webm/chromeos/ebml_writer.h" | |
14 | |
15 extern "C" { | |
16 #define VPX_CODEC_DISABLE_COMPAT 1 | |
17 #include "third_party/libvpx/libvpx.h" | |
18 } | |
19 | |
20 class FilePath; | |
21 class SkBitmap; | |
22 | |
23 namespace media { | |
24 | |
25 namespace chromeos { | |
26 | |
27 // WebM encoder using libvpx. Currently only supports one-pass, constant bitrate | |
28 // encoding of short files consisting of a single video track. Seek info and | |
29 // cues are not supported, so generated .webm file does not strictly adhere to | |
30 // WebM standard (http://www.webmproject.org/code/specs/container/). | |
31 class MEDIA_EXPORT WebmEncoder { | |
32 public: | |
33 // Create new instance for writing to |output_path|. If |realtime| is |true|, | |
34 // uses realtime deadline, otherwise - "good quality" deadline. | |
35 WebmEncoder(const FilePath& output_path, int bitrate, bool realtime); | |
36 | |
37 // Encodes video from a Nx(N*M) sprite, having M frames of size NxN with FPS | |
38 // |fps_n/fps_d|. Must be called on a thread that allows disk IO. | |
39 // Returns |true| iff encoding and writing to file is successful. | |
40 bool EncodeFromSprite(const SkBitmap& sprite, int fps_n, int fps_d); | |
41 | |
42 private: | |
43 // Writes global WebM header and starts a single video track. Returns |false| | |
44 // if there was an error opening file for writing. | |
45 bool WriteWebmHeader(); | |
46 | |
47 // Writes VPX packet to output file. | |
48 void WriteWebmBlock(const vpx_codec_cx_pkt_t* packet); | |
49 | |
50 // Finishes video track and closes output file. Returns |false| if there were | |
51 // any error during encoding/writing file. | |
52 bool WriteWebmFooter(); | |
53 | |
54 // Starts a new WebM sub-element of given type. Those can be nested. | |
55 void StartSubElement(unsigned long class_id); | |
56 | |
57 // Closes current top-level sub-element. | |
58 void EndSubElement(); | |
59 | |
60 // libmkv callbacks. | |
61 void EbmlWrite(const void* buffer, unsigned long len); | |
62 void EbmlSerialize(const void* buffer, int buffer_size, unsigned long len); | |
63 | |
64 template <typename T> | |
65 void EbmlSerializeHelper(const T* buffer, unsigned long len); | |
66 | |
67 // Video dimensions and FPS. | |
68 size_t width_; | |
69 size_t height_; | |
70 vpx_rational_t fps_; | |
71 | |
72 // VPX config in use. | |
73 vpx_codec_enc_cfg_t config_; | |
74 | |
75 // VPX parameters. | |
76 int bitrate_; | |
77 unsigned long deadline_; | |
78 | |
79 // EbmlWriter context. | |
80 EbmlGlobal ebml_writer_; | |
81 | |
82 // Stack with start offsets of currently open sub-elements. | |
83 std::stack<long int> ebml_sub_elements_; | |
84 | |
85 FilePath output_path_; | |
86 FILE* output_; | |
87 | |
88 // True if an error occured while encoding/writing to file. | |
89 bool has_errors_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(WebmEncoder); | |
92 }; | |
93 | |
94 } // namespace chromeos | |
95 | |
96 } // namespace media | |
97 | |
98 #endif // MEDIA_WEBM_CHROMEOS_WEBM_ENCODER_H_ | |
OLD | NEW |