fecdec: Defer decoding until the next group after the first _usable_ fec group starts.
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004-2009 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
13 #include "para.h"
14 #include "afh.h"
15 #include "error.h"
16 #include "string.h"
17
18 /* Taken from decoder_example.c of libvorbis-1.2.3. */
19 static int read_vorbis_comment(ogg_sync_state *oss, ogg_stream_state *stream,
20                 vorbis_info *vi, vorbis_comment *vc)
21 {
22         ogg_page page;
23         ogg_packet packet;
24         int i = 0;
25
26         while (i < 2) {
27                 while (i < 2) {
28                         int ret = ogg_sync_pageout(oss, &page);
29                         if (ret == 0)
30                                 break; /* Need more data */
31                         if (ret != 1)
32                                 continue;
33                         /*
34                          * We can ignore any errors here as they'll also become
35                          * apparent at packetout.
36                          */
37                         ogg_stream_pagein(stream, &page);
38                         while (i < 2) {
39                                 ret = ogg_stream_packetout(stream, &packet);
40                                 if (ret == 0)
41                                         break;
42                                 if (ret < 0)
43                                         return -E_STREAM_PACKETOUT;
44                                 ret = vorbis_synthesis_headerin(vi, vc,
45                                         &packet);
46                                 if (ret < 0)
47                                         return -E_VORBIS;
48                                 i++;
49                         }
50                 }
51         }
52         return 1;
53 }
54
55 static int read_vorbis_info(ogg_sync_state *oss, struct afh_info *afhi)
56 {
57         vorbis_comment vc;
58         vorbis_info vi;
59         ogg_packet packet;
60         ogg_stream_state stream;
61         ogg_page page;
62         int ret;
63         char *taginfo;
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         taginfo = make_taginfo(
97                 vorbis_comment_query(&vc, "title", 0),
98                 vorbis_comment_query(&vc, "artist", 0),
99                 vorbis_comment_query(&vc, "album", 0),
100                 vorbis_comment_query(&vc, "year", 0),
101                 vorbis_comment_query(&vc, "comment", 0)
102         );
103         PARA_DEBUG_LOG("tag info: %s\n", taginfo);
104         afhi->info_string = make_message("%s:\n%s",
105                 status_item_list[SI_AUDIO_FILE_INFO], taginfo);
106         free(taginfo);
107
108         afhi->header_offset = 0;
109         afhi->header_len = oss->returned;
110         ret = 1;
111 out:
112         vorbis_info_clear(&vi);
113         vorbis_comment_clear(&vc);
114         ogg_stream_clear(&stream);
115         return ret;
116 }
117
118 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
119                 struct timeval *result)
120 {
121         uint64_t x = (uint64_t)num_frames * 1000 * 1000
122                 / frequency / num_chunks;
123
124         result->tv_sec = x / 1000 / 1000;
125         result->tv_usec = x % (1000 * 1000);
126         PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
127                 tv2ms(result));
128 }
129
130 /* Write tech data to given audio format handler struct. */
131 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
132                 struct afh_info *afhi)
133 {
134         ogg_sync_state oss;
135         ogg_page op;
136         long len = numbytes;
137         char *buf;
138         int ret, i, j, frames_per_chunk, ct_size;
139         long long unsigned num_frames = 0;
140
141         afhi->info_string = NULL;
142         ogg_sync_init(&oss);
143         ret = -E_OGG_SYNC;
144         buf = ogg_sync_buffer(&oss, len);
145         if (!buf)
146                 goto out;
147         memcpy(buf, map, len);
148         ret = -E_OGG_SYNC;
149         if (ogg_sync_wrote(&oss, len) < 0)
150                 goto out;
151         ret = read_vorbis_info(&oss, afhi);
152         if (ret < 0)
153                 goto out;
154         oss.returned = 0;
155         oss.fill = numbytes;
156         /* count ogg packages and get duration of the file */
157         for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
158                 num_frames = ogg_page_granulepos(&op);
159         PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
160         ret = -E_OGG_EMPTY;
161         if (i == 0)
162                 goto out;
163         afhi->seconds_total = num_frames / afhi->frequency;
164         /* use roughly one page per chunk */
165         frames_per_chunk = num_frames / i;
166         PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
167                 afhi->seconds_total, frames_per_chunk);
168         ct_size = 250;
169         afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
170         afhi->chunk_table[0] = 0;
171         afhi->chunk_table[1] = afhi->header_len;
172         oss.returned = afhi->header_len;
173         oss.fill = numbytes;
174         for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
175                 int granule = ogg_page_granulepos(&op);
176
177                 while (granule > j * frames_per_chunk) {
178                         j++;
179                         if (j >= ct_size) {
180                                 ct_size *= 2;
181                                 afhi->chunk_table = para_realloc(
182                                         afhi->chunk_table,
183                                         ct_size * sizeof(uint32_t));
184                         }
185                         afhi->chunk_table[j] = oss.returned;
186                 }
187         }
188         afhi->chunks_total = j;
189         set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
190         tv_scale(3, &afhi->chunk_tv, &afhi->eof_tv);
191         ret = 0;
192 out:
193         ogg_sync_clear(&oss);
194         return ret;
195 }
196
197 static const char* ogg_suffixes[] = {"ogg", NULL};
198
199 /**
200  * The init function of the ogg vorbis audio format handler.
201  *
202  * \param afh Pointer to the struct to initialize.
203  */
204 void ogg_init(struct audio_format_handler *afh)
205 {
206         afh->get_file_info = ogg_get_file_info,
207         afh->suffixes = ogg_suffixes;
208 }