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