Add dummy-select from http://bugs.digium.com/13132
[asterisk-bristuff.git] / formats / format_h264.c
blobbc70a75c780e8a2ddff49f376e8702f9a8056da6
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, 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 h264 data.
22 * \arg File name extension: h264
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 makeh264e.c by Jeffrey Chilton */
50 /* Portions of the conversion code are by guido@sienanet.it */
51 /*! \todo Check this buf size estimate, it may be totally wrong for large frame video */
53 #define BUF_SIZE 4096 /* Two Real h264 Frames */
54 struct h264_desc {
55 unsigned int lastts;
58 static int h264_open(struct ast_filestream *s)
60 unsigned int ts;
61 int res;
62 if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
63 ast_log(LOG_WARNING, "Empty file!\n");
64 return -1;
66 return 0;
69 static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
71 int res;
72 int mark=0;
73 unsigned short len;
74 unsigned int ts;
75 struct h264_desc *fs = (struct h264_desc *)s->_private;
77 /* Send a frame from the file to the appropriate channel */
78 if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
79 return NULL;
80 len = ntohs(len);
81 mark = (len & 0x8000) ? 1 : 0;
82 len &= 0x7fff;
83 if (len > BUF_SIZE) {
84 ast_log(LOG_WARNING, "Length %d is too long\n", len);
85 len = BUF_SIZE; /* XXX truncate */
87 s->fr.frametype = AST_FRAME_VIDEO;
88 s->fr.subclass = AST_FORMAT_H264;
89 s->fr.mallocd = 0;
90 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
91 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
92 if (res)
93 ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
94 return NULL;
96 s->fr.samples = fs->lastts;
97 s->fr.datalen = len;
98 s->fr.subclass |= mark;
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;
109 static int h264_write(struct ast_filestream *s, struct ast_frame *f)
111 int res;
112 unsigned int ts;
113 unsigned short len;
114 int mark;
116 if (f->frametype != AST_FRAME_VIDEO) {
117 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
118 return -1;
120 mark = (f->subclass & 0x1) ? 0x8000 : 0;
121 if ((f->subclass & ~0x1) != AST_FORMAT_H264) {
122 ast_log(LOG_WARNING, "Asked to write non-h264 frame (%d)!\n", f->subclass);
123 return -1;
125 ts = htonl(f->samples);
126 if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
127 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
128 return -1;
130 len = htons(f->datalen | mark);
131 if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
132 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
133 return -1;
135 if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
136 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
137 return -1;
139 return 0;
142 static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
144 /* No way Jose */
145 return -1;
148 static int h264_trunc(struct ast_filestream *fs)
150 /* Truncate file to current length */
151 if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
152 return -1;
153 return 0;
156 static off_t h264_tell(struct ast_filestream *fs)
158 off_t offset = ftell(fs->f);
159 return offset; /* XXX totally bogus, needs fixing */
162 static const struct ast_format h264_f = {
163 .name = "h264",
164 .exts = "h264",
165 .format = AST_FORMAT_H264,
166 .open = h264_open,
167 .write = h264_write,
168 .seek = h264_seek,
169 .trunc = h264_trunc,
170 .tell = h264_tell,
171 .read = h264_read,
172 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
173 .desc_size = sizeof(struct h264_desc),
176 static int load_module(void)
178 return ast_format_register(&h264_f);
181 static int unload_module(void)
183 return ast_format_unregister(h264_f.name);
186 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw H.264 data");