b00ae0fb30e8e857177cb99dda78ee0c026c10e6
[asterisk/asterisk.git] / formats / format_h263.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Save to raw, headerless h263 data.
22  * \arg File name extension: h263
23  * \ingroup formats
24  * \arg See \ref AstVideo
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30  
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38
39 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
40
41 /* Portions of the conversion code are by guido@sienanet.it */
42
43 /* According to:
44  * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
45  * the maximum actual frame size is not 2048, but 8192.  Since the maximum
46  * theoretical limit is not much larger (32k = 15bits), we'll go for that
47  * size to ensure we don't corrupt frames sent to us (unless they're
48  * ridiculously large). */
49 #define BUF_SIZE        32768   /* Four real h.263 Frames */
50
51 struct h263_desc {
52         unsigned int lastts;
53 };
54
55
56 static int h263_open(struct ast_filestream *s)
57 {
58         unsigned int ts;
59
60         if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
61                 ast_log(LOG_WARNING, "Empty file!\n");
62                 return -1;
63         }
64         return 0;
65 }
66
67 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
68 {
69         int res;
70         uint32_t mark;
71         unsigned short len;
72         unsigned int ts;
73         struct h263_desc *fs = (struct h263_desc *)s->_private;
74
75         /* Send a frame from the file to the appropriate channel */
76         if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
77                 return NULL;
78         len = ntohs(len);
79         mark = (len & 0x8000) ? 1 : 0;
80         len &= 0x7fff;
81         if (len > BUF_SIZE) {
82                 ast_log(LOG_WARNING, "Length %d is too long\n", len);
83                 return NULL;
84         }
85         s->fr.frametype = AST_FRAME_VIDEO;
86         ast_format_set(&s->fr.subclass.format, AST_FORMAT_H263, 0);
87         s->fr.mallocd = 0;
88         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
89         if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
90                 if (res)
91                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
92                 return NULL;
93         }
94         s->fr.samples = fs->lastts;     /* XXX what ? */
95         s->fr.datalen = len;
96         if (mark) {
97                 ast_format_set_video_mark(&s->fr.subclass.format);
98         }
99         s->fr.delivery.tv_sec = 0;
100         s->fr.delivery.tv_usec = 0;
101         if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
102                 fs->lastts = ntohl(ts);
103                 *whennext = fs->lastts * 4/45;
104         } else
105                 *whennext = 0;
106         return &s->fr;
107 }
108
109 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
110 {
111         int res;
112         unsigned int ts;
113         unsigned short len;
114         uint32_t mark = 0;
115         if (f->frametype != AST_FRAME_VIDEO) {
116                 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
117                 return -1;
118         }
119         mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0;
120         if (f->subclass.format.id != AST_FORMAT_H263) {
121                 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%s)!\n", ast_getformatname(&f->subclass.format));
122                 return -1;
123         }
124         ts = htonl(f->samples);
125         if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
126                         ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
127                         return -1;
128         }
129         len = htons(f->datalen | mark);
130         if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
131                         ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
132                         return -1;
133         }
134         if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
135                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
136                         return -1;
137         }
138         return 0;
139 }
140
141 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
142 {
143         /* No way Jose */
144         return -1;
145 }
146
147 static int h263_trunc(struct ast_filestream *fs)
148 {
149         /* Truncate file to current length */
150         if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
151                 return -1;
152         return 0;
153 }
154
155 static off_t h263_tell(struct ast_filestream *fs)
156 {
157         off_t offset = ftello(fs->f);
158         return offset;  /* XXX totally bogus, needs fixing */
159 }
160
161 static struct ast_format_def h263_f = {
162         .name = "h263",
163         .exts = "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),
172 };
173
174 static int load_module(void)
175 {
176         ast_format_set(&h263_f.format, AST_FORMAT_H263, 0);
177         if (ast_format_def_register(&h263_f))
178                 return AST_MODULE_LOAD_FAILURE;
179         return AST_MODULE_LOAD_SUCCESS;
180 }
181
182 static int unload_module(void)
183 {
184         return ast_format_def_unregister(h263_f.name);
185 }
186
187 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
188         .load = load_module,
189         .unload = unload_module,
190         .load_pri = AST_MODPRI_APP_DEPEND
191 );