Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / media / formats / webm / chromeos / webm_encoder.h
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.
4
5 #ifndef MEDIA_FORMATS_WEBM_CHROMEOS_WEBM_ENCODER_H_
6 #define MEDIA_FORMATS_WEBM_CHROMEOS_WEBM_ENCODER_H_
7
8 #include <stdio.h>
9 #include <stack>
10
11 #include "base/files/file_path.h"
12 #include "media/base/media_export.h"
13 #include "media/formats/webm/chromeos/ebml_writer.h"
14
15 extern "C" {
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"
19 }
20
21 class SkBitmap;
22
23 namespace base {
24 class FilePath;
25 }
26
27 namespace media {
28
29 namespace chromeos {
30
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 {
36  public:
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);
40   ~WebmEncoder();
41
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);
46
47  private:
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();
51
52   // Writes VPX packet to output file.
53   void WriteWebmBlock(const vpx_codec_cx_pkt_t* packet);
54
55   // Finishes video track and closes output file. Returns |false| if there were
56   // any error during encoding/writing file.
57   bool WriteWebmFooter();
58
59   // Starts a new WebM sub-element of given type. Those can be nested.
60   void StartSubElement(unsigned long class_id);
61
62   // Closes current top-level sub-element.
63   void EndSubElement();
64
65   // libmkv callbacks.
66   void EbmlWrite(const void* buffer, unsigned long len);
67   void EbmlSerialize(const void* buffer, int buffer_size, unsigned long len);
68
69   template <typename T>
70   void EbmlSerializeHelper(const T* buffer, unsigned long len);
71
72   // Video dimensions and FPS.
73   size_t width_;
74   size_t height_;
75   vpx_rational_t fps_;
76
77   // Number of frames in video.
78   size_t frame_count_;
79
80   // VPX config in use.
81   vpx_codec_enc_cfg_t config_;
82
83   // VPX parameters.
84   int bitrate_;
85   unsigned long deadline_;
86
87   // EbmlWriter context.
88   EbmlGlobal ebml_writer_;
89
90   // Stack with start offsets of currently open sub-elements.
91   std::stack<long int> ebml_sub_elements_;
92
93   base::FilePath output_path_;
94   FILE* output_;
95
96   // True if an error occured while encoding/writing to file.
97   bool has_errors_;
98
99   DISALLOW_COPY_AND_ASSIGN(WebmEncoder);
100 };
101
102 }  // namespace chromeos
103
104 }  // namespace media
105
106 #endif  // MEDIA_FORMATS_WEBM_CHROMEOS_WEBM_ENCODER_H_