Asterisk - The Open Source Telephony Project GIT-master-f36a736
format_h264.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, 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 h264 data.
22 * \arg File name extension: h264
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"
37
38/* Some Ideas for this code came from makeh264e.c by Jeffrey Chilton */
39
40/* Portions of the conversion code are by guido@sienanet.it */
41/*! \todo Check this buf size estimate, it may be totally wrong for large frame video */
42
43#define FRAME_ENDED 0x8000
44
45#define BUF_SIZE 4096 /* Two Real h264 Frames */
46struct h264_desc {
47 unsigned int lastts;
48};
49
50static int h264_open(struct ast_filestream *s)
51{
52 unsigned int ts;
53 if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
54 ast_log(LOG_WARNING, "Empty file!\n");
55 return -1;
56 }
57 return 0;
58}
59
60static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
61{
62 size_t res;
63 int mark = 0;
64 unsigned short len;
65 unsigned int ts;
66 struct h264_desc *fs = (struct h264_desc *)s->_private;
67
68 /* Send a frame from the file to the appropriate channel */
69 if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
70 return NULL;
71 len = ntohs(len);
72 mark = (len & FRAME_ENDED) ? 1 : 0;
73 len &= 0x7fff;
74 if (len > BUF_SIZE) {
75 ast_log(LOG_WARNING, "Length %d is too long\n", len);
76 len = BUF_SIZE; /* XXX truncate */
77 }
79 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
80 if (res) {
81 ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
83 strerror(errno));
84 }
85 return NULL;
86 }
87 s->fr.samples = fs->lastts;
88 s->fr.datalen = len;
89 s->fr.subclass.frame_ending = mark;
90 if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
91 fs->lastts = ntohl(ts);
92 *whennext = fs->lastts * 4/45;
93 } else
94 *whennext = 0;
95 return &s->fr;
96}
97
98static int h264_write(struct ast_filestream *s, struct ast_frame *f)
99{
100 int res;
101 unsigned int ts;
102 unsigned short len;
103 int mark;
104
105 mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
106 ts = htonl(f->samples);
107 if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
108 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
109 return -1;
110 }
111 len = htons(f->datalen | mark);
112 if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
113 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
114 return -1;
115 }
116 if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
117 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
118 return -1;
119 }
120 return 0;
121}
122
123static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
124{
125 /* No way Jose */
126 return -1;
127}
128
129static int h264_trunc(struct ast_filestream *fs)
130{
131 int fd;
132 off_t cur;
133
134 if ((fd = fileno(fs->f)) < 0) {
135 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno));
136 return -1;
137 }
138 if ((cur = ftello(fs->f)) < 0) {
139 ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno));
140 return -1;
141 }
142 /* Truncate file to current length */
143 return ftruncate(fd, cur);
144}
145
146static off_t h264_tell(struct ast_filestream *fs)
147{
148 off_t offset = ftell(fs->f);
149 return offset; /* XXX totally bogus, needs fixing */
150}
151
152static struct ast_format_def h264_f = {
153 .name = "h264",
154 .exts = "h264",
155 .open = h264_open,
156 .write = h264_write,
157 .seek = h264_seek,
158 .trunc = h264_trunc,
159 .tell = h264_tell,
160 .read = h264_read,
161 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
162 .desc_size = sizeof(struct h264_desc),
163};
164
165static int load_module(void)
166{
171}
172
173static int unload_module(void)
174{
176}
177
179 .support_level = AST_MODULE_SUPPORT_CORE,
180 .load = load_module,
181 .unload = unload_module,
182 .load_pri = AST_MODPRI_APP_DEPEND
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
Asterisk architecture endianess compatibility definitions.
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
struct ast_format * ast_format_h264
Built-in cached h264 format.
Definition: format_cache.c:176
static int h264_write(struct ast_filestream *s, struct ast_frame *f)
Definition: format_h264.c:98
static off_t h264_tell(struct ast_filestream *fs)
Definition: format_h264.c:146
static int h264_trunc(struct ast_filestream *fs)
Definition: format_h264.c:129
#define BUF_SIZE
Definition: format_h264.c:45
#define FRAME_ENDED
Definition: format_h264.c:43
static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_h264.c:123
static struct ast_frame * h264_read(struct ast_filestream *s, int *whennext)
Definition: format_h264.c:60
static int load_module(void)
Definition: format_h264.c:165
static int h264_open(struct ast_filestream *s)
Definition: format_h264.c:50
static struct ast_format_def h264_f
Definition: format_h264.c:152
static int unload_module(void)
Definition: format_h264.c:173
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
#define AST_LOG_WARNING
#define LOG_WARNING
int errno
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_format_def_register(f)
Definition: mod_format.h:136
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_APP_DEPEND
Definition: module.h:342
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define NULL
Definition: resample.c:96
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
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
void * _private
Definition: mod_format.h:124
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char name[80]
Definition: mod_format.h:44
struct ast_format * format
Definition: mod_format.h:48
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
unsigned int lastts
Definition: format_h264.c:47