| 1 | /**************************************************************************/ |
| 2 | /* movie_writer_ogv.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "servers/audio/audio_server.h" |
| 34 | #include "servers/movie_writer/movie_writer.h" |
| 35 | |
| 36 | #include <theora/theoraenc.h> |
| 37 | #include <vorbis/codec.h> |
| 38 | #include <vorbis/vorbisenc.h> |
| 39 | |
| 40 | class MovieWriterOGV : public MovieWriter { |
| 41 | GDCLASS(MovieWriterOGV, MovieWriter) |
| 42 | |
| 43 | uint32_t mix_rate = 48000; |
| 44 | AudioServer::SpeakerMode speaker_mode = AudioServer::SPEAKER_MODE_STEREO; |
| 45 | String base_path; |
| 46 | uint32_t frame_count = 0; |
| 47 | uint32_t fps = 0; |
| 48 | uint32_t audio_ch = 0; |
| 49 | uint32_t audio_frames = 0; |
| 50 | |
| 51 | Ref<FileAccess> f; |
| 52 | |
| 53 | // Vorbis quality -0.1 to 1 (-0.1 yields smallest files but lowest fidelity; 1 yields highest fidelity but large files. '0.2' is a reasonable default). |
| 54 | float audio_quality = 0.5; |
| 55 | |
| 56 | // Bitrate target for Theora video. |
| 57 | int video_bitrate = 0; |
| 58 | |
| 59 | // Theora quality selector from 0 to 1.0 (0 yields smallest files but lowest video quality. 1.0 yields highest fidelity but large files). |
| 60 | float video_quality = 0.75; |
| 61 | |
| 62 | // Video stream keyframe frequency (one every N frames). |
| 63 | ogg_uint32_t keyframe_frequency = 64; |
| 64 | |
| 65 | // Sets the encoder speed level. Higher speed levels favor quicker encoding over better quality per bit. Depending on the encoding |
| 66 | // mode, and the internal algorithms used, quality may actually improve with higher speeds, but in this case bitrate will also |
| 67 | // likely increase. The maximum value, and the meaning of each value, are implementation-specific and may change depending on the |
| 68 | // current encoding mode. |
| 69 | int speed = 4; |
| 70 | |
| 71 | // Take physical pages, weld into a logical stream of packets. |
| 72 | ogg_stream_state to; |
| 73 | |
| 74 | // Take physical pages, weld into a logical stream of packets. |
| 75 | ogg_stream_state vo; |
| 76 | |
| 77 | // Theora encoding context. |
| 78 | th_enc_ctx *td; |
| 79 | |
| 80 | // Theora bitstream information. |
| 81 | th_info ti; |
| 82 | |
| 83 | // Theora comment information. |
| 84 | th_comment tc; |
| 85 | |
| 86 | // Vorbis bitstream information. |
| 87 | vorbis_info vi; |
| 88 | |
| 89 | // Vorbis comment information. |
| 90 | vorbis_comment vc; |
| 91 | |
| 92 | // Central working state for the packet->PCM decoder. |
| 93 | vorbis_dsp_state vd; |
| 94 | |
| 95 | // Local working space for packet->PCM decode. |
| 96 | vorbis_block vb; |
| 97 | |
| 98 | // Video buffer. |
| 99 | uint8_t *y, *u, *v; |
| 100 | th_ycbcr_buffer ycbcr; |
| 101 | |
| 102 | bool audio_flag = false; |
| 103 | bool video_flag = false; |
| 104 | ogg_page audio_page; |
| 105 | ogg_page video_page; |
| 106 | ogg_page backup_page; |
| 107 | unsigned int backup_page_size = 0; |
| 108 | unsigned char *backup_page_data = nullptr; |
| 109 | |
| 110 | void write_to_file(bool p_finish = false); |
| 111 | void push_audio(const int32_t *p_audio_data); |
| 112 | void push_video(const Ref<Image> &p_image); |
| 113 | void pull_audio(bool p_last = false); |
| 114 | void pull_video(bool p_last = false); |
| 115 | void save_page(ogg_page page); |
| 116 | void restore_page(ogg_page *page); |
| 117 | |
| 118 | inline int ilog(unsigned _v) { |
| 119 | int ret; |
| 120 | for (ret = 0; _v; ret++) { |
| 121 | _v >>= 1; |
| 122 | } |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | protected: |
| 127 | virtual uint32_t get_audio_mix_rate() const override; |
| 128 | virtual AudioServer::SpeakerMode get_audio_speaker_mode() const override; |
| 129 | virtual void get_supported_extensions(List<String> *r_extensions) const override; |
| 130 | |
| 131 | virtual Error write_begin(const Size2i &p_movie_size, uint32_t p_fps, const String &p_base_path) override; |
| 132 | virtual Error write_frame(const Ref<Image> &p_image, const int32_t *p_audio_data) override; |
| 133 | virtual void write_end() override; |
| 134 | |
| 135 | virtual bool handles_file(const String &p_path) const override; |
| 136 | |
| 137 | public: |
| 138 | MovieWriterOGV(); |
| 139 | }; |