As pointed out on the -dev list, actually use the result of find_peer() so that
[asterisk-bristuff.git] / formats / format_h263.c
blob9b6ac6764f6725d23427d88c6d1d01dfb7e87a7c
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 "asterisk/mod_format.h"
32 #include "asterisk/module.h"
33 #include "asterisk/endian.h"
35 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
37 /* Portions of the conversion code are by guido@sienanet.it */
39 /* According to:
40 * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
41 * the maximum actual frame size is not 2048, but 8192. Since the maximum
42 * theoretical limit is not much larger (32k = 15bits), we'll go for that
43 * size to ensure we don't corrupt frames sent to us (unless they're
44 * ridiculously large). */
45 #define BUF_SIZE 32768 /* Four real h.263 Frames */
47 struct h263_desc {
48 unsigned int lastts;
52 static int h263_open(struct ast_filestream *s)
54 unsigned int ts;
55 int res;
57 if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
58 ast_log(LOG_WARNING, "Empty file!\n");
59 return -1;
61 return 0;
64 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
66 int res;
67 int mark;
68 unsigned short len;
69 unsigned int ts;
70 struct h263_desc *fs = (struct h263_desc *)s->_private;
72 /* Send a frame from the file to the appropriate channel */
73 if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
74 return NULL;
75 len = ntohs(len);
76 mark = (len & 0x8000) ? 1 : 0;
77 len &= 0x7fff;
78 if (len > BUF_SIZE) {
79 ast_log(LOG_WARNING, "Length %d is too long\n", len);
80 return NULL;
82 s->fr.frametype = AST_FRAME_VIDEO;
83 s->fr.subclass = AST_FORMAT_H263;
84 s->fr.mallocd = 0;
85 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
86 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
87 if (res)
88 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
89 return NULL;
91 s->fr.samples = fs->lastts; /* XXX what ? */
92 s->fr.datalen = len;
93 s->fr.subclass |= mark;
94 s->fr.delivery.tv_sec = 0;
95 s->fr.delivery.tv_usec = 0;
96 if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
97 fs->lastts = ntohl(ts);
98 *whennext = fs->lastts * 4/45;
99 } else
100 *whennext = 0;
101 return &s->fr;
104 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
106 int res;
107 unsigned int ts;
108 unsigned short len;
109 int subclass;
110 int mark=0;
111 if (f->frametype != AST_FRAME_VIDEO) {
112 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
113 return -1;
115 subclass = f->subclass;
116 if (subclass & 0x1)
117 mark=0x8000;
118 subclass &= ~0x1;
119 if (subclass != AST_FORMAT_H263) {
120 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
121 return -1;
123 ts = htonl(f->samples);
124 if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
125 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
126 return -1;
128 len = htons(f->datalen | mark);
129 if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
130 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
131 return -1;
133 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
134 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
135 return -1;
137 return 0;
140 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
142 /* No way Jose */
143 return -1;
146 static int h263_trunc(struct ast_filestream *fs)
148 /* Truncate file to current length */
149 if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
150 return -1;
151 return 0;
154 static off_t h263_tell(struct ast_filestream *fs)
156 off_t offset = ftello(fs->f);
157 return offset; /* XXX totally bogus, needs fixing */
160 static const struct ast_format h263_f = {
161 .name = "h263",
162 .exts = "h263",
163 .format = AST_FORMAT_H263,
164 .open = h263_open,
165 .write = h263_write,
166 .seek = h263_seek,
167 .trunc = h263_trunc,
168 .tell = h263_tell,
169 .read = h263_read,
170 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
171 .desc_size = sizeof(struct h263_desc),
174 static int load_module(void)
176 if (ast_format_register(&h263_f))
177 return AST_MODULE_LOAD_FAILURE;
178 return AST_MODULE_LOAD_SUCCESS;
181 static int unload_module(void)
183 return ast_format_unregister(h263_f.name);
186 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw H.263 data");