gui: Combine open_stat_pipe() and para_open_stat_pipe().
[paraslash.git] / ogg_afh.c
1 /*
2 * Copyright (C) 2004-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file ogg_afh.c Audio format handler for ogg vorbis files. */
8
9 #include <inttypes.h>
10 #include <ogg/ogg.h>
11 #include <vorbis/codec.h>
12 #include <regex.h>
13
14 #include "para.h"
15 #include "afh.h"
16 #include "error.h"
17 #include "string.h"
18
19 /* Taken from decoder_example.c of libvorbis-1.2.3. */
20 static int read_vorbis_comment(ogg_sync_state *oss, ogg_stream_state *stream,
21 vorbis_info *vi, vorbis_comment *vc)
22 {
23 ogg_page page;
24 ogg_packet packet;
25 int i = 0;
26
27 while (i < 2) {
28 while (i < 2) {
29 int ret = ogg_sync_pageout(oss, &page);
30 if (ret == 0)
31 break; /* Need more data */
32 if (ret != 1)
33 continue;
34 /*
35 * We can ignore any errors here as they'll also become
36 * apparent at packetout.
37 */
38 ogg_stream_pagein(stream, &page);
39 while (i < 2) {
40 ret = ogg_stream_packetout(stream, &packet);
41 if (ret == 0)
42 break;
43 if (ret < 0)
44 return -E_STREAM_PACKETOUT;
45 ret = vorbis_synthesis_headerin(vi, vc,
46 &packet);
47 if (ret < 0)
48 return -E_VORBIS;
49 i++;
50 }
51 }
52 }
53 return 1;
54 }
55
56 static int read_vorbis_info(ogg_sync_state *oss, struct afh_info *afhi)
57 {
58 vorbis_comment vc;
59 vorbis_info vi;
60 ogg_packet packet;
61 ogg_stream_state stream;
62 ogg_page page;
63 int ret;
64
65 vorbis_info_init(&vi);
66 vorbis_comment_init(&vc);
67
68 ret = -E_SYNC_PAGEOUT;
69 if (ogg_sync_pageout(oss, &page) != 1)
70 goto out;
71
72 ret = ogg_page_serialno(&page);
73 ogg_stream_init(&stream, ret);
74
75 ret = -E_STREAM_PAGEIN;
76 if (ogg_stream_pagein(&stream, &page) < 0)
77 goto out;
78
79 ret = -E_STREAM_PACKETOUT;
80 if (ogg_stream_packetout(&stream, &packet) != 1)
81 goto out;
82
83 ret = -E_VORBIS;
84 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
85 goto out;
86 if (vi.rate == 0)
87 goto out;
88 afhi->channels = vi.channels;
89 afhi->frequency = vi.rate;
90 afhi->bitrate = vi.bitrate_nominal / 1000;
91 PARA_DEBUG_LOG("channels: %i, sampling rate: %i, bitrate: %i\n",
92 afhi->channels, afhi->frequency, afhi->bitrate);
93 ret = read_vorbis_comment(oss, &stream, &vi, &vc);
94 if (ret < 0)
95 goto out;
96 afhi->tags.artist = para_strdup(vorbis_comment_query(&vc, "artist", 0));
97 afhi->tags.title = para_strdup(vorbis_comment_query(&vc, "title", 0));
98 afhi->tags.album = para_strdup(vorbis_comment_query(&vc, "album", 0));
99 afhi->tags.year = para_strdup(vorbis_comment_query(&vc, "year", 0));
100 afhi->tags.comment = para_strdup(vorbis_comment_query(&vc, "comment", 0));
101
102 afhi->header_offset = 0;
103 afhi->header_len = oss->returned;
104 ret = 1;
105 out:
106 vorbis_info_clear(&vi);
107 vorbis_comment_clear(&vc);
108 //ogg_stream_clear(&stream);
109 return ret;
110 }
111
112 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
113 struct timeval *result)
114 {
115 uint64_t x = (uint64_t)num_frames * 1000 * 1000
116 / frequency / num_chunks;
117
118 result->tv_sec = x / 1000 / 1000;
119 result->tv_usec = x % (1000 * 1000);
120 PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
121 tv2ms(result));
122 }
123
124 /* Write tech data to given audio format handler struct. */
125 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
126 struct afh_info *afhi)
127 {
128 ogg_sync_state oss;
129 ogg_page op;
130 long len = numbytes;
131 char *buf;
132 int ret, i, j, frames_per_chunk, ct_size;
133 long long unsigned num_frames = 0;
134
135 ogg_sync_init(&oss);
136 ret = -E_OGG_SYNC;
137 buf = ogg_sync_buffer(&oss, len);
138 if (!buf)
139 goto out;
140 memcpy(buf, map, len);
141 ret = -E_OGG_SYNC;
142 if (ogg_sync_wrote(&oss, len) < 0)
143 goto out;
144 ret = read_vorbis_info(&oss, afhi);
145 if (ret < 0)
146 goto out;
147 oss.returned = 0;
148 oss.fill = numbytes;
149 /* count ogg packages and get duration of the file */
150 for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
151 num_frames = ogg_page_granulepos(&op);
152 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
153 ret = -E_OGG_EMPTY;
154 if (i == 0)
155 goto out;
156 afhi->seconds_total = num_frames / afhi->frequency;
157 /* use roughly one page per chunk */
158 frames_per_chunk = num_frames / i;
159 PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
160 afhi->seconds_total, frames_per_chunk);
161 ct_size = 250;
162 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
163 afhi->chunk_table[0] = 0;
164 afhi->chunk_table[1] = afhi->header_len;
165 oss.returned = afhi->header_len;
166 oss.fill = numbytes;
167 for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
168 int granule = ogg_page_granulepos(&op);
169
170 while (granule > j * frames_per_chunk) {
171 j++;
172 if (j >= ct_size) {
173 ct_size *= 2;
174 afhi->chunk_table = para_realloc(
175 afhi->chunk_table,
176 ct_size * sizeof(uint32_t));
177 }
178 afhi->chunk_table[j] = oss.returned;
179 }
180 }
181 afhi->chunks_total = j;
182 set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
183 ret = 0;
184 out:
185 ogg_sync_clear(&oss);
186 return ret;
187 }
188
189 static const char* ogg_suffixes[] = {"ogg", NULL};
190
191 /**
192 * The init function of the ogg vorbis audio format handler.
193 *
194 * \param afh Pointer to the struct to initialize.
195 */
196 void ogg_init(struct audio_format_handler *afh)
197 {
198 afh->get_file_info = ogg_get_file_info,
199 afh->suffixes = ogg_suffixes;
200 }