WIP: VP9 encoder.
[dcpomatic.git] / src / lib / vp9_pcm_block.cc
1 /*
2     Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "constants.h"
23 #include "dcpomatic_assert.h"
24 #include "util.h"
25 #include "vp9_pcm_block.h"
26
27
28 using std::vector;
29
30
31 auto constexpr pcm_block_size = SIGN_LANGUAGE_AUDIO_FRAME_RATE * AUDIO_BYTES_PER_SAMPLE * SIGN_LANGUAGE_CHUNK_LENGTH_IN_SECONDS;
32
33
34 VP9PCMBlock::VP9PCMBlock(uint8_t const* vp9_segment, int vp9_segment_size, vector<uint8_t> const& ebml_header)
35         : _data(pcm_block_size)
36 {
37         uint32_t const ebml_header_size = ebml_header.size();
38
39         DCPOMATIC_ASSERT(vp9_segment_size < static_cast<int>((pcm_block_size - 20 - ebml_header_size)));
40
41         // H_1
42         _data[0] = 0xff;
43         _data[1] = 0xff;
44         _data[2] = 0xff;
45         _data[3] = 0xff;
46         // Length of VP9 segment, big-endian, unsigned
47         _data[4] = (vp9_segment_size >> 24) & 0xff;
48         _data[5] = (vp9_segment_size >> 16) & 0xff;
49         _data[6] = (vp9_segment_size >> 8) & 0xff;
50         _data[7] = (vp9_segment_size >> 0) & 0xff;
51         // Length of PCM block, big-endian, unsigned
52         _data[8] = (pcm_block_size >> 24) & 0xff;
53         _data[9] = (pcm_block_size >> 16) & 0xff;
54         _data[10] = (pcm_block_size >> 8) & 0xff;
55         _data[11] = (pcm_block_size >> 0) & 0xff;
56         // Length of VP9 EBML header, big-endian, unsigned
57         _data[12] = (ebml_header_size >> 24) & 0xff;
58         _data[13] = (ebml_header_size >> 16) & 0xff;
59         _data[14] = (ebml_header_size >> 8) & 0xff;
60         _data[15] = (ebml_header_size >> 0) & 0xff;
61         // H_2
62         _data[16] = 0xff;
63         _data[17] = 0xff;
64         _data[18] = 0xff;
65         _data[19] = 0xff;
66         // VP9 EBML header
67         std::copy(ebml_header.begin(), ebml_header.end(), _data.begin() + 20);
68         // VP9 payload
69         memcpy(_data.data() + 20 + ebml_header.size(), vp9_segment, vp9_segment_size);
70 }
71