1 // Copyright 2014 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.
5 #ifndef MEDIA_FORMATS_WEBM_CHROMEOS_WEBM_ENCODER_H_
6 #define MEDIA_FORMATS_WEBM_CHROMEOS_WEBM_ENCODER_H_
11 #include "base/files/file_path.h"
12 #include "media/base/media_export.h"
13 #include "media/formats/webm/chromeos/ebml_writer.h"
16 #define VPX_CODEC_DISABLE_COMPAT 1
17 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"
18 #include "third_party/libvpx/source/libvpx/vpx/vp8cx.h"
31 // WebM encoder using libvpx. Currently only supports one-pass, constant bitrate
32 // encoding of short files consisting of a single video track. Seek info and
33 // cues are not supported, so generated .webm file does not strictly adhere to
34 // WebM standard (http://www.webmproject.org/code/specs/container/).
35 class MEDIA_EXPORT WebmEncoder {
37 // Create new instance for writing to |output_path|. If |realtime| is |true|,
38 // uses realtime deadline, otherwise - "good quality" deadline.
39 WebmEncoder(const base::FilePath& output_path, int bitrate, bool realtime);
42 // Encodes video from a Nx(N*M) sprite, having M frames of size NxN with FPS
43 // |fps_n/fps_d|. Must be called on a thread that allows disk IO.
44 // Returns |true| iff encoding and writing to file is successful.
45 bool EncodeFromSprite(const SkBitmap& sprite, int fps_n, int fps_d);
48 // Writes global WebM header and starts a single video track. Returns |false|
49 // if there was an error opening file for writing.
50 bool WriteWebmHeader();
52 // Writes VPX packet to output file.
53 void WriteWebmBlock(const vpx_codec_cx_pkt_t* packet);
55 // Finishes video track and closes output file. Returns |false| if there were
56 // any error during encoding/writing file.
57 bool WriteWebmFooter();
59 // Starts a new WebM sub-element of given type. Those can be nested.
60 void StartSubElement(unsigned long class_id);
62 // Closes current top-level sub-element.
66 void EbmlWrite(const void* buffer, unsigned long len);
67 void EbmlSerialize(const void* buffer, int buffer_size, unsigned long len);
70 void EbmlSerializeHelper(const T* buffer, unsigned long len);
72 // Video dimensions and FPS.
77 // Number of frames in video.
81 vpx_codec_enc_cfg_t config_;
85 unsigned long deadline_;
87 // EbmlWriter context.
88 EbmlGlobal ebml_writer_;
90 // Stack with start offsets of currently open sub-elements.
91 std::stack<long int> ebml_sub_elements_;
93 base::FilePath output_path_;
96 // True if an error occured while encoding/writing to file.
99 DISALLOW_COPY_AND_ASSIGN(WebmEncoder);
102 } // namespace chromeos
106 #endif // MEDIA_FORMATS_WEBM_CHROMEOS_WEBM_ENCODER_H_