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