Merge branch 't/utf8'
[paraslash.git] / afh.c
1 /*
2 * Copyright (C) 2008-2013 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
8
9 #include <regex.h>
10 #include <sys/time.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "afh.cmdline.h"
15 #include "fd.h"
16 #include "afh.h"
17 #include "error.h"
18 #include "version.h"
19
20 static struct afh_args_info conf;
21 INIT_AFH_ERRLISTS;
22
23 static int loglevel;
24 INIT_STDERR_LOGGING(loglevel)
25
26 static void print_info(int audio_format_num, struct afh_info *afhi)
27 {
28 char *msg;
29
30 afh_get_afhi_txt(audio_format_num, afhi, &msg);
31 printf("%s", msg);
32 free(msg);
33 }
34
35 static void print_chunk_table(struct afh_info *afhi)
36 {
37 int i;
38
39 if (conf.parser_friendly_given) {
40 printf("chunk_table: ");
41 for (i = 0; i <= afhi->chunks_total; i++)
42 printf("%u ", afhi->chunk_table[i]);
43 printf("\n");
44 return;
45 }
46 for (i = 1; i <= afhi->chunks_total; i++) {
47 struct timeval tv;
48 long unsigned from, to;
49 tv_scale(i - 1, &afhi->chunk_tv, &tv);
50 from = tv2ms(&tv);
51 tv_scale(i, &afhi->chunk_tv, &tv);
52 to = tv2ms(&tv);
53 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
54 from / 1000, from % 1000, to / 1000, to % 1000,
55 afhi->chunk_table[i - 1], afhi->chunk_table[i],
56 afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
57 }
58 }
59
60 /**
61 * The main function of para_afh.
62 *
63 * \param argc Usual argument count.
64 * \param argv Usual argument vector.
65 *
66 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
67 */
68 int main(int argc, char **argv)
69 {
70 int i, ret, audio_format_num, fd;
71 void *audio_file_data;
72 size_t audio_file_size;
73 struct afh_info afhi;
74
75 afh_cmdline_parser(argc, argv, &conf);
76 HANDLE_VERSION_FLAG("afh", conf);
77 loglevel = get_loglevel_by_name(conf.loglevel_arg);
78 ret = -E_AFH_SYNTAX;
79 if (conf.inputs_num == 0)
80 goto out;
81 afh_init();
82 for (i = 0; i < conf.inputs_num; i++) {
83 int ret2;
84 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
85 &audio_file_size, &fd);
86 if (ret < 0) {
87 PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
88 goto out;
89 }
90 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
91 fd, &afhi);
92 if (ret < 0)
93 goto out;
94
95 audio_format_num = ret;
96 printf("File %d: %s\n", i + 1, conf.inputs[i]);
97 print_info(audio_format_num, &afhi);
98 if (conf.chunk_table_given)
99 print_chunk_table(&afhi);
100 printf("\n");
101 clear_afhi(&afhi);
102 ret2 = para_munmap(audio_file_data, audio_file_size);
103 if (ret2 < 0 && ret >= 0)
104 ret = ret2;
105 if (ret < 0)
106 break;
107 }
108 out:
109 if (ret < 0)
110 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
111 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
112 }