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 */
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  * @src_buf: The source of raw data to be encoded, encoder might set a
107  * default if null.
108  * @src_buf_offset: Our position in the source buffer.
109  * @is_video_encoder: Whether this a video encoder (as opposed to audio)
110  * @ctx: Encoder-specific state.
111  * @stream_id: Examples: Audio streams (0xc0-0xdf), Video streams
112  * (0xe0-0xef).
113  * @es_id: The TS PID to use for the elementary stream in this encoder.
114  * @encode: Prepare enough AUs for the given amount of time.
115  * @clear: Clear the encoder output.
116  * @sync: Attempt to synchronize with this encoder.
117  * @sampling_rate_hz: The sampling rate (or fps, if video) used.
118  * @last_sample_cb: Called when the encoder runs out of data.This is
119  *		    so the source can read data in a
120  *		    piecemeal fashion instead of having to
121  *		    provide it all at once.
122  * @destroy: Destroy this encoder, freeing allocated resources.
123  * @next: Next in the chain
124  */
125 struct vidtv_encoder {
126 	enum vidtv_encoder_id id;
127 	char *name;
128 
129 	u8 *encoder_buf;
130 	u32 encoder_buf_sz;
131 	u32 encoder_buf_offset;
132 
133 	u64 sample_count;
134 	int last_duration;
135 	int note_offset;
136 	enum musical_notes last_tone;
137 
138 	struct vidtv_access_unit *access_units;
139 
140 	void *src_buf;
141 	u32 src_buf_sz;
142 	u32 src_buf_offset;
143 
144 	bool is_video_encoder;
145 	void *ctx;
146 
147 	__be16 stream_id;
148 
149 	__be16 es_pid;
150 
151 	void *(*encode)(struct vidtv_encoder *e);
152 
153 	u32 (*clear)(struct vidtv_encoder *e);
154 
155 	struct vidtv_encoder *sync;
156 
157 	u32 sampling_rate_hz;
158 
159 	void (*last_sample_cb)(u32 sample_no);
160 
161 	void (*destroy)(struct vidtv_encoder *e);
162 
163 	struct vidtv_encoder *next;
164 };
165 
166 #endif /* VIDTV_ENCODER_H */
167