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.
21 * \brief Save to raw, headerless h264 data.
22 * \arg File name extension: h264
24 * \arg See \ref AstVideo
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
39 /* Some Ideas for this code came from makeh264e.c by Jeffrey Chilton */
41 /* Portions of the conversion code are by guido@sienanet.it */
42 /*! \todo Check this buf size estimate, it may be totally wrong for large frame video */
44 #define BUF_SIZE 4096 /* Two Real h264 Frames */
49 static int h264_open(struct ast_filestream *s)
52 if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
53 ast_log(LOG_WARNING, "Empty file!\n");
59 static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
65 struct h264_desc *fs = (struct h264_desc *)s->_private;
67 /* Send a frame from the file to the appropriate channel */
68 if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
71 mark = (len & 0x8000) ? 1 : 0;
74 ast_log(LOG_WARNING, "Length %d is too long\n", len);
75 len = BUF_SIZE; /* XXX truncate */
77 s->fr.frametype = AST_FRAME_VIDEO;
78 ast_format_set(&s->fr.subclass.format, AST_FORMAT_H264, 0);
80 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
81 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
83 ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
86 s->fr.samples = fs->lastts;
89 ast_format_set_video_mark(&s->fr.subclass.format);
91 s->fr.delivery.tv_sec = 0;
92 s->fr.delivery.tv_usec = 0;
93 if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
94 fs->lastts = ntohl(ts);
95 *whennext = fs->lastts * 4/45;
101 static int h264_write(struct ast_filestream *s, struct ast_frame *f)
108 if (f->frametype != AST_FRAME_VIDEO) {
109 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
112 mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0;
113 if (f->subclass.format.id != AST_FORMAT_H264) {
114 ast_log(LOG_WARNING, "Asked to write non-h264 frame (%s)!\n", ast_getformatname(&f->subclass.format));
117 ts = htonl(f->samples);
118 if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
119 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
122 len = htons(f->datalen | mark);
123 if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
124 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
127 if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
128 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
134 static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
140 static int h264_trunc(struct ast_filestream *fs)
145 if ((fd = fileno(fs->f)) < 0) {
146 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno));
149 if ((cur = ftello(fs->f) < 0)) {
150 ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno));
153 /* Truncate file to current length */
154 return ftruncate(fd, cur);
157 static off_t h264_tell(struct ast_filestream *fs)
159 off_t offset = ftell(fs->f);
160 return offset; /* XXX totally bogus, needs fixing */
163 static struct ast_format_def h264_f = {
172 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
173 .desc_size = sizeof(struct h264_desc),
176 static int load_module(void)
178 ast_format_set(&h264_f.format, AST_FORMAT_H264, 0);
179 if (ast_format_def_register(&h264_f))
180 return AST_MODULE_LOAD_FAILURE;
181 return AST_MODULE_LOAD_SUCCESS;
184 static int unload_module(void)
186 return ast_format_def_unregister(h264_f.name);
189 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.264 data",
191 .unload = unload_module,
192 .load_pri = AST_MODPRI_APP_DEPEND