Merge slimey's Solaris compatibility (with small mods) (bug #2740)
[asterisk/asterisk.git] / formats / format_h263.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Save to raw, headerless h263 data.
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/file.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/sched.h>
19 #include <asterisk/module.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #ifdef __linux__
29 #include <endian.h>
30 #else
31 #ifdef SOLARIS
32 #include "solaris-compat/compat.h"
33 #else
34 #include <machine/endian.h>
35 #endif
36 #endif
37
38 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
39
40 /* Portions of the conversion code are by guido@sienanet.it */
41
42 struct ast_filestream {
43         void *reserved[AST_RESERVED_POINTERS];
44         /* Believe it or not, we must decode/recode to account for the
45            weird MS format */
46         /* This is what a filestream means to us */
47         int fd; /* Descriptor */
48         unsigned int lastts;
49         struct ast_frame fr;                            /* Frame information */
50         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
51         char empty;                                                     /* Empty character */
52         unsigned char h263[4096];                               /* Two Real h263 Frames */
53 };
54
55
56 AST_MUTEX_DEFINE_STATIC(h263_lock);
57 static int glistcnt = 0;
58
59 static char *name = "h263";
60 static char *desc = "Raw h263 data";
61 static char *exts = "h263";
62
63 static struct ast_filestream *h263_open(int fd)
64 {
65         /* We don't have any header to read or anything really, but
66            if we did, it would go here.  We also might want to check
67            and be sure it's a valid file.  */
68         struct ast_filestream *tmp;
69         unsigned int ts;
70         int res;
71         if ((res = read(fd, &ts, sizeof(ts))) < sizeof(ts)) {
72                 ast_log(LOG_WARNING, "Empty file!\n");
73                 return NULL;
74         }
75                 
76         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
77                 memset(tmp, 0, sizeof(struct ast_filestream));
78                 if (ast_mutex_lock(&h263_lock)) {
79                         ast_log(LOG_WARNING, "Unable to lock h263 list\n");
80                         free(tmp);
81                         return NULL;
82                 }
83                 tmp->fd = fd;
84                 tmp->fr.data = tmp->h263;
85                 tmp->fr.frametype = AST_FRAME_VIDEO;
86                 tmp->fr.subclass = AST_FORMAT_H263;
87                 /* datalen will vary for each frame */
88                 tmp->fr.src = name;
89                 tmp->fr.mallocd = 0;
90                 glistcnt++;
91                 ast_mutex_unlock(&h263_lock);
92                 ast_update_use_count();
93         }
94         return tmp;
95 }
96
97 static struct ast_filestream *h263_rewrite(int fd, const char *comment)
98 {
99         /* We don't have any header to read or anything really, but
100            if we did, it would go here.  We also might want to check
101            and be sure it's a valid file.  */
102         struct ast_filestream *tmp;
103         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
104                 memset(tmp, 0, sizeof(struct ast_filestream));
105                 if (ast_mutex_lock(&h263_lock)) {
106                         ast_log(LOG_WARNING, "Unable to lock h263 list\n");
107                         free(tmp);
108                         return NULL;
109                 }
110                 tmp->fd = fd;
111                 glistcnt++;
112                 ast_mutex_unlock(&h263_lock);
113                 ast_update_use_count();
114         } else
115                 ast_log(LOG_WARNING, "Out of memory\n");
116         return tmp;
117 }
118
119 static void h263_close(struct ast_filestream *s)
120 {
121         if (ast_mutex_lock(&h263_lock)) {
122                 ast_log(LOG_WARNING, "Unable to lock h263 list\n");
123                 return;
124         }
125         glistcnt--;
126         ast_mutex_unlock(&h263_lock);
127         ast_update_use_count();
128         close(s->fd);
129         free(s);
130         s = NULL;
131 }
132
133 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
134 {
135         int res;
136         int mark=0;
137         unsigned short len;
138         unsigned int ts;
139         /* Send a frame from the file to the appropriate channel */
140         s->fr.frametype = AST_FRAME_VIDEO;
141         s->fr.subclass = AST_FORMAT_H263;
142         s->fr.offset = AST_FRIENDLY_OFFSET;
143         s->fr.mallocd = 0;
144         s->fr.data = s->h263;
145         if ((res = read(s->fd, &len, sizeof(len))) < 1) {
146                 return NULL;
147         }
148         len = ntohs(len);
149         if (len & 0x8000) {
150                 mark = 1;
151         }
152         len &= 0x7fff;
153         if (len > sizeof(s->h263)) {
154                 ast_log(LOG_WARNING, "Length %d is too long\n", len);
155         }
156         if ((res = read(s->fd, s->h263, len)) != len) {
157                 if (res)
158                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
159                 return NULL;
160         }
161         s->fr.samples = s->lastts;
162         s->fr.datalen = len;
163         s->fr.subclass |= mark;
164         if ((res = read(s->fd, &ts, sizeof(ts))) == sizeof(ts)) {
165                 s->lastts = *whennext = ntohl(ts) * 4/45;
166         } else
167                 *whennext = 0;
168         return &s->fr;
169 }
170
171 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
172 {
173         int res;
174         unsigned int ts;
175         unsigned short len;
176         int subclass;
177         int mark=0;
178         if (f->frametype != AST_FRAME_VIDEO) {
179                 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
180                 return -1;
181         }
182         subclass = f->subclass;
183         if (subclass & 0x1)
184                 mark=0x8000;
185         subclass &= ~0x1;
186         if (subclass != AST_FORMAT_H263) {
187                 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
188                 return -1;
189         }
190         ts = htonl(f->samples);
191         if ((res = write(fs->fd, &ts, sizeof(ts))) != sizeof(ts)) {
192                         ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
193                         return -1;
194         }
195         len = htons(f->datalen | mark);
196         if ((res = write(fs->fd, &len, sizeof(len))) != sizeof(len)) {
197                         ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
198                         return -1;
199         }
200         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
201                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
202                         return -1;
203         }
204         return 0;
205 }
206
207 static char *h263_getcomment(struct ast_filestream *s)
208 {
209         return NULL;
210 }
211
212 static int h263_seek(struct ast_filestream *fs, long sample_offset, int whence)
213 {
214         /* No way Jose */
215         return -1;
216 }
217
218 static int h263_trunc(struct ast_filestream *fs)
219 {
220         /* Truncate file to current length */
221         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
222                 return -1;
223         return 0;
224 }
225
226 static long h263_tell(struct ast_filestream *fs)
227 {
228         off_t offset;
229         offset = lseek(fs->fd, 0, SEEK_CUR);
230         return (offset/20)*160;
231 }
232
233 int load_module()
234 {
235         return ast_format_register(name, exts, AST_FORMAT_H263,
236                                                                 h263_open,
237                                                                 h263_rewrite,
238                                                                 h263_write,
239                                                                 h263_seek,
240                                                                 h263_trunc,
241                                                                 h263_tell,
242                                                                 h263_read,
243                                                                 h263_close,
244                                                                 h263_getcomment);
245                                                                 
246                                                                 
247 }
248
249 int unload_module()
250 {
251         return ast_format_unregister(name);
252 }       
253
254 int usecount()
255 {
256         int res;
257         if (ast_mutex_lock(&h263_lock)) {
258                 ast_log(LOG_WARNING, "Unable to lock h263 list\n");
259                 return -1;
260         }
261         res = glistcnt;
262         ast_mutex_unlock(&h263_lock);
263         return res;
264 }
265
266 char *description()
267 {
268         return desc;
269 }
270
271
272 char *key()
273 {
274         return ASTERISK_GPL_KEY;
275 }