Asterisk - The Open Source Telephony Project  18.5.0
format_h263.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  *
21  * \brief Save to raw, headerless h263 data.
22  * \arg File name extension: h263
23  * \ingroup formats
24  * \arg See \ref AstVideo
25  */
26 
27 /*** MODULEINFO
28  <support_level>core</support_level>
29  ***/
30 
31 #include "asterisk.h"
32 
33 #include "asterisk/mod_format.h"
34 #include "asterisk/module.h"
35 #include "asterisk/endian.h"
36 #include "asterisk/format_cache.h"
37 
38 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
39 
40 /* Portions of the conversion code are by [email protected] */
41 
42 /* According to:
43  * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
44  * the maximum actual frame size is not 2048, but 8192. Since the maximum
45  * theoretical limit is not much larger (32k = 15bits), we'll go for that
46  * size to ensure we don't corrupt frames sent to us (unless they're
47  * ridiculously large). */
48 #define BUF_SIZE 32768 /* Four real h.263 Frames */
49 
50 #define FRAME_ENDED 0x8000
51 
52 struct h263_desc {
53  unsigned int lastts;
54 };
55 
56 
57 static int h263_open(struct ast_filestream *s)
58 {
59  unsigned int ts;
60 
61  if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
62  ast_log(LOG_WARNING, "Empty file!\n");
63  return -1;
64  }
65  return 0;
66 }
67 
68 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
69 {
70  size_t res;
71  uint32_t mark;
72  unsigned short len;
73  unsigned int ts;
74  struct h263_desc *fs = (struct h263_desc *)s->_private;
75 
76  /* Send a frame from the file to the appropriate channel */
77  if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
78  return NULL;
79  len = ntohs(len);
80  mark = (len & FRAME_ENDED) ? 1 : 0;
81  len &= 0x7fff;
82  if (len > BUF_SIZE) {
83  ast_log(LOG_WARNING, "Length %d is too long\n", len);
84  return NULL;
85  }
87  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
88  if (res) {
89  ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
91  strerror(errno));
92  }
93  return NULL;
94  }
95  s->fr.samples = fs->lastts; /* XXX what ? */
96  s->fr.datalen = len;
97  s->fr.subclass.frame_ending = mark;
98  if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
99  fs->lastts = ntohl(ts);
100  *whennext = fs->lastts * 4/45;
101  } else
102  *whennext = 0;
103  return &s->fr;
104 }
105 
106 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
107 {
108  int res;
109  unsigned int ts;
110  unsigned short len;
111  uint32_t mark = 0;
112  mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
113  ts = htonl(f->samples);
114  if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
115  ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
116  return -1;
117  }
118  len = htons(f->datalen | mark);
119  if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
120  ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
121  return -1;
122  }
123  if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
124  ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
125  return -1;
126  }
127  return 0;
128 }
129 
130 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
131 {
132  /* No way Jose */
133  return -1;
134 }
135 
136 static int h263_trunc(struct ast_filestream *fs)
137 {
138  int fd;
139  off_t cur;
140 
141  if ((fd = fileno(fs->f)) < 0) {
142  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
143  return -1;
144  }
145  if ((cur = ftello(fs->f)) < 0) {
146  ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
147  return -1;
148  }
149  /* Truncate file to current length */
150  return ftruncate(fd, cur);
151 }
152 
153 static off_t h263_tell(struct ast_filestream *fs)
154 {
155  off_t offset = ftello(fs->f);
156  return offset; /* XXX totally bogus, needs fixing */
157 }
158 
159 static struct ast_format_def h263_f = {
160  .name = "h263",
161  .exts = "h263",
162  .open = h263_open,
163  .write = h263_write,
164  .seek = h263_seek,
165  .trunc = h263_trunc,
166  .tell = h263_tell,
167  .read = h263_read,
168  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
169  .desc_size = sizeof(struct h263_desc),
170 };
171 
172 static int load_module(void)
173 {
174  h263_f.format = ast_format_h263;
175  if (ast_format_def_register(&h263_f))
178 }
179 
180 static int unload_module(void)
181 {
182  return ast_format_def_unregister(h263_f.name);
183 }
184 
186  .support_level = AST_MODULE_SUPPORT_CORE,
187  .load = load_module,
188  .unload = unload_module,
189  .load_pri = AST_MODPRI_APP_DEPEND
190 );
Asterisk main include file. File version handling, generic pbx functions.
unsigned int lastts
Definition: format_h263.c:53
#define LOG_WARNING
Definition: logger.h:274
#define AST_LOG_WARNING
Definition: logger.h:279
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
Each supported file format is described by the following structure.
Definition: mod_format.h:43
struct ast_frame_subclass subclass
Header for providers of file and format handling routines. Clients of these routines should include "...
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_log
Definition: astobj2.c:42
static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_h263.c:106
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Asterisk architecture endianess compatibility definitions.
#define ast_format_def_register(f)
Definition: mod_format.h:136
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
struct ast_format * format
Definition: mod_format.h:48
void * _private
Definition: mod_format.h:124
static struct ast_format_def h263_f
Definition: format_h263.c:159
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
static struct ast_frame * h263_read(struct ast_filestream *s, int *whennext)
Definition: format_h263.c:68
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
static int h263_trunc(struct ast_filestream *fs)
Definition: format_h263.c:136
static int unload_module(void)
Definition: format_h263.c:180
static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_h263.c:130
char name[80]
Definition: mod_format.h:44
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
#define BUF_SIZE
Definition: format_h263.c:48
Data structure associated with a single frame of data.
union ast_frame::@263 data
#define FRAME_ENDED
Definition: format_h263.c:50
static int h263_open(struct ast_filestream *s)
Definition: format_h263.c:57
struct ast_format * format
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
struct ast_format * ast_format_h263
Built-in cached h263 format.
Definition: format_cache.c:171
static int load_module(void)
Definition: format_h263.c:172
Media Format Cache API.
static off_t h263_tell(struct ast_filestream *fs)
Definition: format_h263.c:153