1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // container.h - an interface of parser/builder for formatted files.
4 //
5 // Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 //
7 // Licensed under the terms of the GNU General Public License, version 2.
8 
9 #ifndef __ALSA_UTILS_AXFER_CONTAINER__H_
10 #define __ALSA_UTILS_AXFER_CONTAINER__H_
11 
12 #include "aconfig.h"
13 
14 #define _LARGEFILE64_SOURCE
15 #include <sys/types.h>
16 
17 #include <stdbool.h>
18 #include <stdint.h>
19 
20 #include <alsa/asoundlib.h>
21 
22 #include "os_compat.h"
23 
24 enum container_type {
25 	CONTAINER_TYPE_PARSER = 0,
26 	CONTAINER_TYPE_BUILDER,
27 	CONTAINER_TYPE_COUNT,
28 };
29 
30 enum container_format {
31 	CONTAINER_FORMAT_RIFF_WAVE = 0,
32 	CONTAINER_FORMAT_AU,
33 	CONTAINER_FORMAT_VOC,
34 	CONTAINER_FORMAT_RAW,
35 	CONTAINER_FORMAT_COUNT,
36 };
37 
38 struct container_ops;
39 
40 struct container_context {
41 	enum container_type type;
42 	int fd;
43 	int (*process_bytes)(struct container_context *cntr,
44 			     void *buffer, unsigned int byte_count);
45 	bool magic_handled;
46 	bool eof;
47 	bool interrupted;
48 	bool stdio;
49 
50 	enum container_format format;
51 	uint64_t max_size;
52 	char magic[4];
53 	const struct container_ops *ops;
54 	void *private_data;
55 
56 	// Available after pre-process.
57 	unsigned int bytes_per_sample;
58 	unsigned int samples_per_frame;
59 	unsigned int frames_per_second;
60 
61 	unsigned int verbose;
62 	uint64_t handled_byte_count;
63 };
64 
65 const char *container_suffix_from_format(enum container_format format);
66 enum container_format container_format_from_path(const char *path);
67 int container_parser_init(struct container_context *cntr, int fd,
68 			  unsigned int verbose);
69 int container_builder_init(struct container_context *cntr, int fd,
70 			   enum container_format format, unsigned int verbose);
71 void container_context_destroy(struct container_context *cntr);
72 int container_context_pre_process(struct container_context *cntr,
73 				  snd_pcm_format_t *format,
74 				  unsigned int *samples_per_frame,
75 				  unsigned int *frames_per_second,
76 				  uint64_t *frame_count);
77 int container_context_process_frames(struct container_context *cntr,
78 				     void *frame_buffer,
79 				     unsigned int *frame_count);
80 int container_context_post_process(struct container_context *cntr,
81 				   uint64_t *frame_count);
82 
83 // For internal use in 'container' module.
84 
85 struct container_ops {
86 	int (*pre_process)(struct container_context *cntr,
87 			   snd_pcm_format_t *format,
88 			   unsigned int *samples_per_frame,
89 			   unsigned int *frames_per_second,
90 			   uint64_t *byte_count);
91 	int (*post_process)(struct container_context *cntr,
92 			    uint64_t handled_byte_count);
93 };
94 struct container_parser {
95 	enum container_format format;
96 	const char *const magic;
97 	uint64_t max_size;
98 	struct container_ops ops;
99 	unsigned int private_size;
100 };
101 
102 struct container_builder {
103 	enum container_format format;
104 	const char *const suffix;
105 	uint64_t max_size;
106 	struct container_ops ops;
107 	unsigned int private_size;
108 };
109 
110 int container_recursive_read(struct container_context *cntr, void *buf,
111 			     unsigned int byte_count);
112 int container_recursive_write(struct container_context *cntr, void *buf,
113 			      unsigned int byte_count);
114 int container_seek_offset(struct container_context *cntr, off_t offset);
115 
116 extern const struct container_parser container_parser_riff_wave;
117 extern const struct container_builder container_builder_riff_wave;
118 
119 extern const struct container_parser container_parser_au;
120 extern const struct container_builder container_builder_au;
121 
122 extern const struct container_parser container_parser_voc;
123 extern const struct container_builder container_builder_voc;
124 
125 extern const struct container_parser container_parser_raw;
126 extern const struct container_builder container_builder_raw;
127 
128 #endif
129