1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Vidtv serves as a reference DVB driver and helps validate the existing APIs
4  * in the media subsystem. It can also aid developers working on userspace
5  * applications.
6  *
7  * This file contains a generic encoder type that can provide data for a stream
8  *
9  * Copyright (C) 2020 Daniel W. S. Almeida
10  */
11 
12 #ifndef VIDTV_ENCODER_H
13 #define VIDTV_ENCODER_H
14 
15 #include <linux/types.h>
16 
17 enum vidtv_encoder_id {
18 	/* add IDs here when implementing new encoders */
19 	S302M,
20 };
21 
22 struct vidtv_access_unit {
23 	u32 num_samples;
24 	u64 pts;
25 	u64 dts;
26 	u32 nbytes;
27 	u32 offset;
28 	struct vidtv_access_unit *next;
29 };
30 
31 /* Some musical notes, used by a tone generator. Values are in Hz */
32 enum musical_notes {
33 	NOTE_SILENT = 0,
34 
35 	NOTE_C_2 = 65,
36 	NOTE_CS_2 = 69,
37 	NOTE_D_2 = 73,
38 	NOTE_DS_2 = 78,
39 	NOTE_E_2 = 82,
40 	NOTE_F_2 = 87,
41 	NOTE_FS_2 = 93,
42 	NOTE_G_2 = 98,
43 	NOTE_GS_2 = 104,
44 	NOTE_A_2 = 110,
45 	NOTE_AS_2 = 117,
46 	NOTE_B_2 = 123,
47 	NOTE_C_3 = 131,
48 	NOTE_CS_3 = 139,
49 	NOTE_D_3 = 147,
50 	NOTE_DS_3 = 156,
51 	NOTE_E_3 = 165,
52 	NOTE_F_3 = 175,
53 	NOTE_FS_3 = 185,
54 	NOTE_G_3 = 196,
55 	NOTE_GS_3 = 208,
56 	NOTE_A_3 = 220,
57 	NOTE_AS_3 = 233,
58 	NOTE_B_3 = 247,
59 	NOTE_C_4 = 262,
60 	NOTE_CS_4 = 277,
61 	NOTE_D_4 = 294,
62 	NOTE_DS_4 = 311,
63 	NOTE_E_4 = 330,
64 	NOTE_F_4 = 349,
65 	NOTE_FS_4 = 370,
66 	NOTE_G_4 = 392,
67 	NOTE_GS_4 = 415,
68 	NOTE_A_4 = 440,
69 	NOTE_AS_4 = 466,
70 	NOTE_B_4 = 494,
71 	NOTE_C_5 = 523,
72 	NOTE_CS_5 = 554,
73 	NOTE_D_5 = 587,
74 	NOTE_DS_5 = 622,
75 	NOTE_E_5 = 659,
76 	NOTE_F_5 = 698,
77 	NOTE_FS_5 = 740,
78 	NOTE_G_5 = 784,
79 	NOTE_GS_5 = 831,
80 	NOTE_A_5 = 880,
81 	NOTE_AS_5 = 932,
82 	NOTE_B_5 = 988,
83 	NOTE_C_6 = 1047,
84 	NOTE_CS_6 = 1109,
85 	NOTE_D_6 = 1175,
86 	NOTE_DS_6 = 1245,
87 	NOTE_E_6 = 1319,
88 	NOTE_F_6 = 1397,
89 	NOTE_FS_6 = 1480,
90 	NOTE_G_6 = 1568,
91 	NOTE_GS_6 = 1661,
92 	NOTE_A_6 = 1760,
93 	NOTE_AS_6 = 1865,
94 	NOTE_B_6 = 1976,
95 	NOTE_C_7 = 2093
96 };
97 
98 /**
99  * struct vidtv_encoder - A generic encoder type.
100  * @id: So we can cast to a concrete implementation when needed.
101  * @name: Usually the same as the stream name.
102  * @encoder_buf: The encoder internal buffer for the access units.
103  * @encoder_buf_sz: The encoder buffer size, in bytes
104  * @encoder_buf_offset: Our byte position in the encoder buffer.
105  * @sample_count: How many samples we have encoded in total.
106  * @access_units: encoder payload units, used for clock references
107  * @src_buf: The source of raw data to be encoded, encoder might set a
108  * default if null.
109  * @src_buf_sz: size of @src_buf.
110  * @src_buf_offset: Our position in the source buffer.
111  * @is_video_encoder: Whether this a video encoder (as opposed to audio)
112  * @ctx: Encoder-specific state.
113  * @stream_id: Examples: Audio streams (0xc0-0xdf), Video streams
114  * (0xe0-0xef).
115  * @es_pid: The TS PID to use for the elementary stream in this encoder.
116  * @encode: Prepare enough AUs for the given amount of time.
117  * @clear: Clear the encoder output.
118  * @sync: Attempt to synchronize with this encoder.
119  * @sampling_rate_hz: The sampling rate (or fps, if video) used.
120  * @last_sample_cb: Called when the encoder runs out of data.This is
121  *		    so the source can read data in a
122  *		    piecemeal fashion instead of having to
123  *		    provide it all at once.
124  * @destroy: Destroy this encoder, freeing allocated resources.
125  * @next: Next in the chain
126  */
127 struct vidtv_encoder {
128 	enum vidtv_encoder_id id;
129 	char *name;
130 
131 	u8 *encoder_buf;
132 	u32 encoder_buf_sz;
133 	u32 encoder_buf_offset;
134 
135 	u64 sample_count;
136 
137 	struct vidtv_access_unit *access_units;
138 
139 	void *src_buf;
140 	u32 src_buf_sz;
141 	u32 src_buf_offset;
142 
143 	bool is_video_encoder;
144 	void *ctx;
145 
146 	__be16 stream_id;
147 
148 	__be16 es_pid;
149 
150 	void *(*encode)(struct vidtv_encoder *e);
151 
152 	u32 (*clear)(struct vidtv_encoder *e);
153 
154 	struct vidtv_encoder *sync;
155 
156 	u32 sampling_rate_hz;
157 
158 	void (*last_sample_cb)(u32 sample_no);
159 
160 	void (*destroy)(struct vidtv_encoder *e);
161 
162 	struct vidtv_encoder *next;
163 };
164 
165 #endif /* VIDTV_ENCODER_H */
166