format_h263.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Save to raw, headerless h263 data.
00022  * \arg File name extension: h263
00023  * \ingroup formats
00024  * \arg See \ref AstVideo
00025  */
00026 
00027 /*** MODULEINFO
00028    <support_level>core</support_level>
00029  ***/
00030  
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00034 
00035 #include "asterisk/mod_format.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/endian.h"
00038 #include "asterisk/format_cache.h"
00039 
00040 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
00041 
00042 /* Portions of the conversion code are by guido@sienanet.it */
00043 
00044 /* According to:
00045  * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
00046  * the maximum actual frame size is not 2048, but 8192.  Since the maximum
00047  * theoretical limit is not much larger (32k = 15bits), we'll go for that
00048  * size to ensure we don't corrupt frames sent to us (unless they're
00049  * ridiculously large). */
00050 #define  BUF_SIZE 32768 /* Four real h.263 Frames */
00051 
00052 #define FRAME_ENDED 0x8000
00053 
00054 struct h263_desc {
00055    unsigned int lastts;
00056 };
00057 
00058 
00059 static int h263_open(struct ast_filestream *s)
00060 {
00061    unsigned int ts;
00062 
00063    if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
00064       ast_log(LOG_WARNING, "Empty file!\n");
00065       return -1;
00066    }
00067    return 0;
00068 }
00069 
00070 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
00071 {
00072    int res;
00073    uint32_t mark;
00074    unsigned short len;
00075    unsigned int ts;
00076    struct h263_desc *fs = (struct h263_desc *)s->_private;
00077 
00078    /* Send a frame from the file to the appropriate channel */
00079    if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
00080       return NULL;
00081    len = ntohs(len);
00082    mark = (len & FRAME_ENDED) ? 1 : 0;
00083    len &= 0x7fff;
00084    if (len > BUF_SIZE) {
00085       ast_log(LOG_WARNING, "Length %d is too long\n", len);
00086       return NULL;
00087    }
00088    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
00089    if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00090       if (res)
00091          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00092       return NULL;
00093    }
00094    s->fr.samples = fs->lastts;   /* XXX what ? */
00095    s->fr.datalen = len;
00096    s->fr.subclass.frame_ending = mark;
00097    if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
00098       fs->lastts = ntohl(ts);
00099       *whennext = fs->lastts * 4/45;
00100    } else
00101       *whennext = 0;
00102    return &s->fr;
00103 }
00104 
00105 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
00106 {
00107    int res;
00108    unsigned int ts;
00109    unsigned short len;
00110    uint32_t mark = 0;
00111    mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
00112    ts = htonl(f->samples);
00113    if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
00114          ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
00115          return -1;
00116    }
00117    len = htons(f->datalen | mark);
00118    if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
00119          ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
00120          return -1;
00121    }
00122    if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00123          ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00124          return -1;
00125    }
00126    return 0;
00127 }
00128 
00129 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00130 {
00131    /* No way Jose */
00132    return -1;
00133 }
00134 
00135 static int h263_trunc(struct ast_filestream *fs)
00136 {
00137    int fd;
00138    off_t cur;
00139 
00140    if ((fd = fileno(fs->f)) < 0) {
00141       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
00142       return -1;
00143    }
00144    if ((cur = ftello(fs->f)) < 0) {
00145       ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
00146       return -1;
00147    }
00148    /* Truncate file to current length */
00149    return ftruncate(fd, cur);
00150 }
00151 
00152 static off_t h263_tell(struct ast_filestream *fs)
00153 {
00154    off_t offset = ftello(fs->f);
00155    return offset; /* XXX totally bogus, needs fixing */
00156 }
00157 
00158 static struct ast_format_def h263_f = {
00159    .name = "h263",
00160    .exts = "h263",
00161    .open = h263_open,
00162    .write = h263_write,
00163    .seek = h263_seek,
00164    .trunc = h263_trunc,
00165    .tell = h263_tell,
00166    .read = h263_read,
00167    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00168    .desc_size = sizeof(struct h263_desc),
00169 };
00170 
00171 static int load_module(void)
00172 {
00173    h263_f.format = ast_format_h263;
00174    if (ast_format_def_register(&h263_f))
00175       return AST_MODULE_LOAD_FAILURE;
00176    return AST_MODULE_LOAD_SUCCESS;
00177 }
00178 
00179 static int unload_module(void)
00180 {
00181    return ast_format_def_unregister(h263_f.name);
00182 }
00183 
00184 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
00185    .support_level = AST_MODULE_SUPPORT_CORE,
00186    .load = load_module,
00187    .unload = unload_module,
00188    .load_pri = AST_MODPRI_APP_DEPEND
00189 );

Generated on Thu Apr 16 06:27:34 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6