436005e0db128796618e6b34285dc06110a04c8e
[paraslash.git] / aac_afh.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18 /** \file aac_afh.c para_server's aac audio format handler */
19
20
21 #include "server.cmdline.h"
22 #include "server.h"
23 #include "afs.h"
24 #include "error.h"
25 #include "string.h"
26 #include "aac.h"
27
28 /* must be big enough to hold header */
29 #define DEFAULT_INBUF_SIZE 65536
30
31 static FILE *infile;
32 static int inbuf_size;
33 static unsigned char *inbuf;
34 static ssize_t *chunk_table;
35 struct audio_format *af;
36
37 unsigned num_chunks, chunk_num;
38
39 NeAACDecHandle handle;
40 static void aac_close_audio_file(void)
41 {
42 }
43
44 /*
45 * Init m4a file and write some tech data to given pointers.
46 */
47 static int aac_get_file_info(FILE *file, char *info_str, long unsigned *frames,
48 int *seconds)
49 {
50 int ret, skip, decoder_len;
51 unsigned inbuf_len;
52 unsigned long rate = 0;
53 unsigned char channels = 0;
54
55 free(inbuf);
56 inbuf_size = DEFAULT_INBUF_SIZE;
57 inbuf = para_malloc(inbuf_size);
58 infile = file;
59
60 PARA_INFO_LOG("file: %p\n", file);
61 ret = read(fileno(infile), inbuf, inbuf_size);
62 PARA_INFO_LOG("read %d bytes\n", ret);
63 if (ret <= 0)
64 return -E_AAC_READ;
65 PARA_INFO_LOG("checking aac %d bytes\n", ret);
66 inbuf_len = ret;
67 ret = aac_find_esds(inbuf, inbuf_len, &skip);
68 if (ret < 0)
69 return ret;
70 decoder_len = ret;
71 handle = aac_open();
72 ret = NeAACDecInit(handle, inbuf + skip,
73 inbuf_len - skip, &rate, &channels);
74 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
75
76 return 1;
77 }
78
79 /*
80 * Simple stream reposition routine
81 */
82 static int aac_reposition_stream(long unsigned request)
83 {
84 return -E_AAC_REPOS;
85 }
86
87 static int get_chunk_size(long unsigned chunk_num)
88 {
89 int ret;
90 if (chunk_num >= num_chunks)
91 return -1;
92 ret = chunk_table[chunk_num + 1] - chunk_table[chunk_num];
93 if (!ret)
94 return ret;
95 #if 0
96 PARA_DEBUG_LOG("chunk %d: %lli-%lli (%lli bytes)\n",
97 chunk_num,
98 chunk_table[chunk_num],
99 chunk_table[chunk_num + 1] - 1,
100 ret);
101 #endif
102 return ret;
103 }
104
105 char *aac_read_chunk(long unsigned current_chunk, ssize_t *len)
106 {
107 return (char *)inbuf;
108 }
109
110 void aac_afh_init(void *p)
111 {
112 af = p;
113 af->reposition_stream = aac_reposition_stream;
114 af->get_file_info = aac_get_file_info,
115 af->read_chunk = aac_read_chunk;
116 af->close_audio_file = aac_close_audio_file;
117 af->get_header_info = NULL;
118 af->chunk_tv.tv_sec = 0;
119 af->chunk_tv.tv_usec = 250 * 1000;
120 tv_scale(3, &af->chunk_tv, &af->eof_tv);
121 }