00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 233694 $")
00030
00031 #include "asterisk/mod_format.h"
00032 #include "asterisk/module.h"
00033 #include "asterisk/endian.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #define BUF_SIZE 32768
00046
00047 struct h263_desc {
00048 unsigned int lastts;
00049 };
00050
00051
00052 static int h263_open(struct ast_filestream *s)
00053 {
00054 unsigned int ts;
00055 int res;
00056
00057 if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
00058 ast_log(LOG_WARNING, "Empty file!\n");
00059 return -1;
00060 }
00061 return 0;
00062 }
00063
00064 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
00065 {
00066 int res;
00067 int mark;
00068 unsigned short len;
00069 unsigned int ts;
00070 struct h263_desc *fs = (struct h263_desc *)s->_private;
00071
00072
00073 if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
00074 return NULL;
00075 len = ntohs(len);
00076 mark = (len & 0x8000) ? 1 : 0;
00077 len &= 0x7fff;
00078 if (len > BUF_SIZE) {
00079 ast_log(LOG_WARNING, "Length %d is too long\n", len);
00080 return NULL;
00081 }
00082 s->fr.frametype = AST_FRAME_VIDEO;
00083 s->fr.subclass = AST_FORMAT_H263;
00084 s->fr.mallocd = 0;
00085 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
00086 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00087 if (res)
00088 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00089 return NULL;
00090 }
00091 s->fr.samples = fs->lastts;
00092 s->fr.datalen = len;
00093 s->fr.subclass |= mark;
00094 s->fr.delivery.tv_sec = 0;
00095 s->fr.delivery.tv_usec = 0;
00096 if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
00097 fs->lastts = ntohl(ts);
00098 *whennext = fs->lastts * 4/45;
00099 } else
00100 *whennext = 0;
00101 return &s->fr;
00102 }
00103
00104 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
00105 {
00106 int res;
00107 unsigned int ts;
00108 unsigned short len;
00109 int subclass;
00110 int mark=0;
00111 if (f->frametype != AST_FRAME_VIDEO) {
00112 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
00113 return -1;
00114 }
00115 subclass = f->subclass;
00116 if (subclass & 0x1)
00117 mark=0x8000;
00118 subclass &= ~0x1;
00119 if (subclass != AST_FORMAT_H263) {
00120 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
00121 return -1;
00122 }
00123 ts = htonl(f->samples);
00124 if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
00125 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
00126 return -1;
00127 }
00128 len = htons(f->datalen | mark);
00129 if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
00130 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
00131 return -1;
00132 }
00133 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00134 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00135 return -1;
00136 }
00137 return 0;
00138 }
00139
00140 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00141 {
00142
00143 return -1;
00144 }
00145
00146 static int h263_trunc(struct ast_filestream *fs)
00147 {
00148
00149 if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
00150 return -1;
00151 return 0;
00152 }
00153
00154 static off_t h263_tell(struct ast_filestream *fs)
00155 {
00156 off_t offset = ftello(fs->f);
00157 return offset;
00158 }
00159
00160 static const struct ast_format h263_f = {
00161 .name = "h263",
00162 .exts = "h263",
00163 .format = AST_FORMAT_H263,
00164 .open = h263_open,
00165 .write = h263_write,
00166 .seek = h263_seek,
00167 .trunc = h263_trunc,
00168 .tell = h263_tell,
00169 .read = h263_read,
00170 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00171 .desc_size = sizeof(struct h263_desc),
00172 };
00173
00174 static int load_module(void)
00175 {
00176 if (ast_format_register(&h263_f))
00177 return AST_MODULE_LOAD_FAILURE;
00178 return AST_MODULE_LOAD_SUCCESS;
00179 }
00180
00181 static int unload_module(void)
00182 {
00183 return ast_format_unregister(h263_f.name);
00184 }
00185
00186 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
00187 .load = load_module,
00188 .unload = unload_module,
00189 .load_pri = 10,
00190 );