| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-04-18 23:50:59 +00:00
										 |  |  |  * Copyright (C) 1999 - 2006, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \brief Save to raw, headerless h263 data. | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \arg File name extension: h263 | 
					
						
							|  |  |  |  * \ingroup formats | 
					
						
							| 
									
										
										
										
											2006-06-08 08:15:43 +00:00
										 |  |  |  * \arg See \ref AstVideo | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:53:49 +00:00
										 |  |  | #include "asterisk/mod_format.h"
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:12:19 +00:00
										 |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/endian.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-01-22 21:30:04 +00:00
										 |  |  | /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */ | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Portions of the conversion code are by guido@sienanet.it */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-17 16:31:43 +00:00
										 |  |  | /* According to:
 | 
					
						
							|  |  |  |  * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
 | 
					
						
							|  |  |  |  * the maximum actual frame size is not 2048, but 8192.  Since the maximum | 
					
						
							|  |  |  |  * theoretical limit is not much larger (32k = 15bits), we'll go for that | 
					
						
							|  |  |  |  * size to ensure we don't corrupt frames sent to us (unless they're | 
					
						
							|  |  |  |  * ridiculously large). */ | 
					
						
							|  |  |  | #define	BUF_SIZE	32768	/* Four real h.263 Frames */
 | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct h263_desc { | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	unsigned int lastts; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | static int h263_open(struct ast_filestream *s) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned int ts; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Empty file!\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	format_t mark; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	unsigned short len; | 
					
						
							|  |  |  | 	unsigned int ts; | 
					
						
							| 
									
										
										
										
											2007-11-29 17:50:44 +00:00
										 |  |  | 	struct h263_desc *fs = (struct h263_desc *)s->_private; | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	/* Send a frame from the file to the appropriate channel */ | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	len = ntohs(len); | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	mark = (len & 0x8000) ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	len &= 0x7fff; | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	if (len > BUF_SIZE) { | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Length %d is too long\n", len); | 
					
						
							| 
									
										
										
										
											2006-07-17 15:56:16 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	s->fr.frametype = AST_FRAME_VIDEO; | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	s->fr.subclass.codec = AST_FORMAT_H263; | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	s->fr.mallocd = 0; | 
					
						
							| 
									
										
										
										
											2006-04-09 22:31:38 +00:00
										 |  |  | 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); | 
					
						
							| 
									
										
										
										
											2008-05-22 16:29:54 +00:00
										 |  |  | 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 		if (res) | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	s->fr.samples = fs->lastts;	/* XXX what ? */ | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	s->fr.datalen = len; | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	s->fr.subclass.codec |= mark; | 
					
						
							| 
									
										
										
										
											2005-09-10 18:46:38 +00:00
										 |  |  | 	s->fr.delivery.tv_sec = 0; | 
					
						
							|  |  |  | 	s->fr.delivery.tv_usec = 0; | 
					
						
							| 
									
										
										
										
											2005-10-16 16:12:51 +00:00
										 |  |  | 	if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 		fs->lastts = ntohl(ts); | 
					
						
							|  |  |  | 		*whennext = fs->lastts * 4/45; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	} else | 
					
						
							|  |  |  | 		*whennext = 0; | 
					
						
							|  |  |  | 	return &s->fr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int h263_write(struct ast_filestream *fs, struct ast_frame *f) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	unsigned int ts; | 
					
						
							|  |  |  | 	unsigned short len; | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	format_t subclass; | 
					
						
							|  |  |  | 	format_t mark=0; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	if (f->frametype != AST_FRAME_VIDEO) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	subclass = f->subclass.codec; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 	if (subclass & 0x1) | 
					
						
							|  |  |  | 		mark=0x8000; | 
					
						
							|  |  |  | 	subclass &= ~0x1; | 
					
						
							|  |  |  | 	if (subclass != AST_FORMAT_H263) { | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Asked to write non-h263 frame (%s)!\n", ast_getformatname(f->subclass.codec)); | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ts = htonl(f->samples); | 
					
						
							| 
									
										
										
										
											2005-10-16 16:12:51 +00:00
										 |  |  | 	if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) { | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	len = htons(f->datalen | mark); | 
					
						
							| 
									
										
										
										
											2005-10-16 16:12:51 +00:00
										 |  |  | 	if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) { | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-05-22 16:29:54 +00:00
										 |  |  | 	if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-20 23:35:12 +00:00
										 |  |  | static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* No way Jose */ | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int h263_trunc(struct ast_filestream *fs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Truncate file to current length */ | 
					
						
							| 
									
										
										
										
											2006-02-20 23:35:12 +00:00
										 |  |  | 	if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-20 23:35:12 +00:00
										 |  |  | static off_t h263_tell(struct ast_filestream *fs) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	off_t offset = ftello(fs->f); | 
					
						
							|  |  |  | 	return offset;	/* XXX totally bogus, needs fixing */ | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | static const struct ast_format h263_f = { | 
					
						
							|  |  |  | 	.name = "h263", | 
					
						
							| 
									
										
										
										
											2006-04-18 23:50:59 +00:00
										 |  |  | 	.exts = "h263", | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	.format = AST_FORMAT_H263, | 
					
						
							|  |  |  | 	.open = h263_open, | 
					
						
							|  |  |  | 	.write = h263_write, | 
					
						
							|  |  |  | 	.seek = h263_seek, | 
					
						
							|  |  |  | 	.trunc = h263_trunc, | 
					
						
							|  |  |  | 	.tell = h263_tell, | 
					
						
							|  |  |  | 	.read = h263_read, | 
					
						
							|  |  |  | 	.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, | 
					
						
							|  |  |  | 	.desc_size = sizeof(struct h263_desc), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 	if (ast_format_register(&h263_f)) | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	return ast_format_unregister(h263_f.name); | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | }	 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-08 18:00:16 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data", | 
					
						
							|  |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							|  |  |  | 	.load_pri = 10, | 
					
						
							|  |  |  | ); |