format_h264.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, 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 h264 data.
00022  * \arg File name extension: h264
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 makeh264e.c by Jeffrey Chilton */
00041 
00042 /* Portions of the conversion code are by guido@sienanet.it */
00043 /*! \todo Check this buf size estimate, it may be totally wrong for large frame video */
00044 
00045 #define FRAME_ENDED  0x8000
00046 
00047 #define BUF_SIZE  4096  /* Two Real h264 Frames */
00048 struct h264_desc {
00049    unsigned int lastts;
00050 };
00051 
00052 static int h264_open(struct ast_filestream *s)
00053 {
00054    unsigned int ts;
00055    if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
00056       ast_log(LOG_WARNING, "Empty file!\n");
00057       return -1;
00058    }
00059    return 0;
00060 }
00061 
00062 static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
00063 {
00064    int res;
00065    int mark = 0;
00066    unsigned short len;
00067    unsigned int ts;
00068    struct h264_desc *fs = (struct h264_desc *)s->_private;
00069 
00070    /* Send a frame from the file to the appropriate channel */
00071    if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
00072       return NULL;
00073    len = ntohs(len);
00074    mark = (len & FRAME_ENDED) ? 1 : 0;
00075    len &= 0x7fff;
00076    if (len > BUF_SIZE) {
00077       ast_log(LOG_WARNING, "Length %d is too long\n", len);
00078       len = BUF_SIZE;   /* XXX truncate */
00079    }
00080    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
00081    if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00082       if (res)
00083          ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
00084       return NULL;
00085    }
00086    s->fr.samples = fs->lastts;
00087    s->fr.datalen = len;
00088    s->fr.subclass.frame_ending = mark;
00089    if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
00090       fs->lastts = ntohl(ts);
00091       *whennext = fs->lastts * 4/45;
00092    } else
00093       *whennext = 0;
00094    return &s->fr;
00095 }
00096 
00097 static int h264_write(struct ast_filestream *s, struct ast_frame *f)
00098 {
00099    int res;
00100    unsigned int ts;
00101    unsigned short len;
00102    int mark;
00103 
00104    mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
00105    ts = htonl(f->samples);
00106    if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
00107       ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
00108       return -1;
00109    }
00110    len = htons(f->datalen | mark);
00111    if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
00112       ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
00113       return -1;
00114    }
00115    if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
00116       ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00117       return -1;
00118    }
00119    return 0;
00120 }
00121 
00122 static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00123 {
00124    /* No way Jose */
00125    return -1;
00126 }
00127 
00128 static int h264_trunc(struct ast_filestream *fs)
00129 {
00130    int fd;
00131    off_t cur;
00132 
00133    if ((fd = fileno(fs->f)) < 0) {
00134       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno));
00135       return -1;
00136    }
00137    if ((cur = ftello(fs->f)) < 0) {
00138       ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno));
00139       return -1;
00140    }
00141    /* Truncate file to current length */
00142    return ftruncate(fd, cur);
00143 }
00144 
00145 static off_t h264_tell(struct ast_filestream *fs)
00146 {
00147    off_t offset = ftell(fs->f);
00148    return offset; /* XXX totally bogus, needs fixing */
00149 }
00150 
00151 static struct ast_format_def h264_f = {
00152    .name = "h264",
00153    .exts = "h264",
00154    .open = h264_open,
00155    .write = h264_write,
00156    .seek = h264_seek,
00157    .trunc = h264_trunc,
00158    .tell = h264_tell,
00159    .read = h264_read,
00160    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00161    .desc_size = sizeof(struct h264_desc),
00162 };
00163 
00164 static int load_module(void)
00165 {
00166    h264_f.format = ast_format_h264;
00167    if (ast_format_def_register(&h264_f))
00168       return AST_MODULE_LOAD_FAILURE;
00169    return AST_MODULE_LOAD_SUCCESS;
00170 }
00171 
00172 static int unload_module(void)
00173 {
00174    return ast_format_def_unregister(h264_f.name);
00175 }
00176 
00177 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.264 data",
00178    .support_level = AST_MODULE_SUPPORT_CORE,
00179    .load = load_module,
00180    .unload = unload_module,
00181    .load_pri = AST_MODPRI_APP_DEPEND
00182 );

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