Let's also include aclocal.m4
[asterisk-bristuff.git] / formats / format_h263.c
blobbaa7a3efd804e643393eda55dd1040d14e8ea1ed
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Save to raw, headerless h263 data.
22 * \arg File name extension: h263
23 * \ingroup formats
24 * \arg See \ref AstVideo
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <unistd.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.h>
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
48 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
50 /* Portions of the conversion code are by guido@sienanet.it */
52 /* According to:
53 * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
54 * the maximum actual frame size is not 2048, but 8192. Since the maximum
55 * theoretical limit is not much larger (32k = 15bits), we'll go for that
56 * size to ensure we don't corrupt frames sent to us (unless they're
57 * ridiculously large). */
58 #define BUF_SIZE 32768 /* Four real h.263 Frames */
60 struct h263_desc {
61 unsigned int lastts;
65 static int h263_open(struct ast_filestream *s)
67 unsigned int ts;
68 int res;
70 if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
71 ast_log(LOG_WARNING, "Empty file!\n");
72 return -1;
74 return 0;
77 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
79 int res;
80 int mark;
81 unsigned short len;
82 unsigned int ts;
83 struct h263_desc *fs = (struct h263_desc *)s->_private;
85 /* Send a frame from the file to the appropriate channel */
86 if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
87 return NULL;
88 len = ntohs(len);
89 mark = (len & 0x8000) ? 1 : 0;
90 len &= 0x7fff;
91 if (len > BUF_SIZE) {
92 ast_log(LOG_WARNING, "Length %d is too long\n", len);
93 return NULL;
95 s->fr.frametype = AST_FRAME_VIDEO;
96 s->fr.subclass = AST_FORMAT_H263;
97 s->fr.mallocd = 0;
98 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
99 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
100 if (res)
101 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
102 return NULL;
104 s->fr.samples = fs->lastts; /* XXX what ? */
105 s->fr.datalen = len;
106 s->fr.subclass |= mark;
107 s->fr.delivery.tv_sec = 0;
108 s->fr.delivery.tv_usec = 0;
109 if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
110 fs->lastts = ntohl(ts);
111 *whennext = fs->lastts * 4/45;
112 } else
113 *whennext = 0;
114 return &s->fr;
117 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
119 int res;
120 unsigned int ts;
121 unsigned short len;
122 int subclass;
123 int mark=0;
124 if (f->frametype != AST_FRAME_VIDEO) {
125 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
126 return -1;
128 subclass = f->subclass;
129 if (subclass & 0x1)
130 mark=0x8000;
131 subclass &= ~0x1;
132 if (subclass != AST_FORMAT_H263) {
133 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
134 return -1;
136 ts = htonl(f->samples);
137 if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
138 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
139 return -1;
141 len = htons(f->datalen | mark);
142 if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
143 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
144 return -1;
146 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
147 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
148 return -1;
150 return 0;
153 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
155 /* No way Jose */
156 return -1;
159 static int h263_trunc(struct ast_filestream *fs)
161 /* Truncate file to current length */
162 if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
163 return -1;
164 return 0;
167 static off_t h263_tell(struct ast_filestream *fs)
169 off_t offset = ftello(fs->f);
170 return offset; /* XXX totally bogus, needs fixing */
173 static const struct ast_format h263_f = {
174 .name = "h263",
175 .exts = "h263",
176 .format = AST_FORMAT_H263,
177 .open = h263_open,
178 .write = h263_write,
179 .seek = h263_seek,
180 .trunc = h263_trunc,
181 .tell = h263_tell,
182 .read = h263_read,
183 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
184 .desc_size = sizeof(struct h263_desc),
187 static int load_module(void)
189 return ast_format_register(&h263_f);
192 static int unload_module(void)
194 return ast_format_unregister(h263_f.name);
197 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw H.263 data");