Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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 <markster@digium.com>
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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 364578 $")
34 
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38 
39 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
40 
41 /* Portions of the conversion code are by guido@sienanet.it */
42 
43 /* According to:
44  * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
45  * the maximum actual frame size is not 2048, but 8192. Since the maximum
46  * theoretical limit is not much larger (32k = 15bits), we'll go for that
47  * size to ensure we don't corrupt frames sent to us (unless they're
48  * ridiculously large). */
49 #define BUF_SIZE 32768 /* Four real h.263 Frames */
50 
51 struct h263_desc {
52  unsigned int lastts;
53 };
54 
55 
56 static int h263_open(struct ast_filestream *s)
57 {
58  unsigned int ts;
59  int res;
60 
61  if ((res = 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  int res;
71  format_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)) < 1)
78  return NULL;
79  len = ntohs(len);
80  mark = (len & 0x8000) ? 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  }
88  s->fr.mallocd = 0;
90  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
91  if (res)
92  ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
93  return NULL;
94  }
95  s->fr.samples = fs->lastts; /* XXX what ? */
96  s->fr.datalen = len;
97  s->fr.subclass.codec |= mark;
98  s->fr.delivery.tv_sec = 0;
99  s->fr.delivery.tv_usec = 0;
100  if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
101  fs->lastts = ntohl(ts);
102  *whennext = fs->lastts * 4/45;
103  } else
104  *whennext = 0;
105  return &s->fr;
106 }
107 
108 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
109 {
110  int res;
111  unsigned int ts;
112  unsigned short len;
113  format_t subclass;
114  format_t mark=0;
115  if (f->frametype != AST_FRAME_VIDEO) {
116  ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
117  return -1;
118  }
119  subclass = f->subclass.codec;
120  if (subclass & 0x1)
121  mark=0x8000;
122  subclass &= ~0x1;
123  if (subclass != AST_FORMAT_H263) {
124  ast_log(LOG_WARNING, "Asked to write non-h263 frame (%s)!\n", ast_getformatname(f->subclass.codec));
125  return -1;
126  }
127  ts = htonl(f->samples);
128  if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
129  ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
130  return -1;
131  }
132  len = htons(f->datalen | mark);
133  if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
134  ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
135  return -1;
136  }
137  if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
138  ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
139  return -1;
140  }
141  return 0;
142 }
143 
144 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
145 {
146  /* No way Jose */
147  return -1;
148 }
149 
150 static int h263_trunc(struct ast_filestream *fs)
151 {
152  int fd;
153  off_t cur;
154 
155  if ((fd = fileno(fs->f)) < 0) {
156  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
157  return -1;
158  }
159  if ((cur = ftello(fs->f)) < 0) {
160  ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
161  return -1;
162  }
163  /* Truncate file to current length */
164  return ftruncate(fd, cur);
165 }
166 
167 static off_t h263_tell(struct ast_filestream *fs)
168 {
169  off_t offset = ftello(fs->f);
170  return offset; /* XXX totally bogus, needs fixing */
171 }
172 
173 static const struct ast_format h263_f = {
174  .name = "h263",
175  .exts = "h263",
176  .format = AST_FORMAT_H263,
177  .open = h263_open,
178  .write = h263_write,
179  .seek = h263_seek,
180  .trunc = h263_trunc,
181  .tell = h263_tell,
182  .read = h263_read,
183  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
184  .desc_size = sizeof(struct h263_desc),
185 };
186 
187 static int load_module(void)
188 {
189  if (ast_format_register(&h263_f))
192 }
193 
194 static int unload_module(void)
195 {
196  return ast_format_unregister(h263_f.name);
197 }
198 
200  .load = load_module,
201  .unload = unload_module,
202  .load_pri = AST_MODPRI_APP_DEPEND
203 );
union ast_frame_subclass subclass
Definition: frame.h:146
Asterisk main include file. File version handling, generic pbx functions.
unsigned int lastts
Definition: format_h263.c:52
#define ast_format_register(f)
Definition: mod_format.h:131
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
#define AST_LOG_WARNING
Definition: logger.h:149
long ts
Definition: frame.h:168
Each supported file format is described by the following structure.
Definition: mod_format.h:43
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
format_t codec
Definition: frame.h:137
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
Definition: frame.h:183
static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_h263.c:108
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
Asterisk architecture endianess compatibility definitions.
int datalen
Definition: frame.h:148
struct ast_frame fr
Definition: mod_format.h:117
static struct ast_format h263_f
Definition: format_h263.c:173
#define AST_FORMAT_H263
Definition: frame.h:283
void * _private
Definition: mod_format.h:119
int ast_format_unregister(const char *name)
Unregisters a file format.
Definition: file.c:104
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
char name[80]
Definition: mod_format.h:44
static struct ast_frame * h263_read(struct ast_filestream *s, int *whennext)
Definition: format_h263.c:68
int64_t format_t
Definition: frame_defs.h:32
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int h263_trunc(struct ast_filestream *fs)
Definition: format_h263.c:150
static int unload_module(void)
Definition: format_h263.c:194
static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_h263.c:144
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
static struct ast_format f[]
Definition: format_g726.c:181
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
struct timeval delivery
Definition: frame.h:162
int mallocd
Definition: frame.h:152
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
#define BUF_SIZE
Definition: format_h263.c:49
Data structure associated with a single frame of data.
Definition: frame.h:142
enum ast_frame_type frametype
Definition: frame.h:144
static int h263_open(struct ast_filestream *s)
Definition: format_h263.c:56
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
static int load_module(void)
Definition: format_h263.c:187
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
static off_t h263_tell(struct ast_filestream *fs)
Definition: format_h263.c:167