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