format_h263.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Save to raw, headerless h263 data.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/sched.h>
  18. #include <asterisk/module.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <stdlib.h>
  22. #include <sys/time.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include "asterisk/endian.h"
  28. /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
  29. /* Portions of the conversion code are by guido@sienanet.it */
  30. struct ast_filestream {
  31. void *reserved[AST_RESERVED_POINTERS];
  32. /* Believe it or not, we must decode/recode to account for the
  33. weird MS format */
  34. /* This is what a filestream means to us */
  35. int fd; /* Descriptor */
  36. unsigned int lastts;
  37. struct ast_frame fr; /* Frame information */
  38. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  39. char empty; /* Empty character */
  40. unsigned char h263[4096]; /* Two Real h263 Frames */
  41. };
  42. AST_MUTEX_DEFINE_STATIC(h263_lock);
  43. static int glistcnt = 0;
  44. static char *name = "h263";
  45. static char *desc = "Raw h263 data";
  46. static char *exts = "h263";
  47. static struct ast_filestream *h263_open(int fd)
  48. {
  49. /* We don't have any header to read or anything really, but
  50. if we did, it would go here. We also might want to check
  51. and be sure it's a valid file. */
  52. struct ast_filestream *tmp;
  53. unsigned int ts;
  54. int res;
  55. if ((res = read(fd, &ts, sizeof(ts))) < sizeof(ts)) {
  56. ast_log(LOG_WARNING, "Empty file!\n");
  57. return NULL;
  58. }
  59. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  60. memset(tmp, 0, sizeof(struct ast_filestream));
  61. if (ast_mutex_lock(&h263_lock)) {
  62. ast_log(LOG_WARNING, "Unable to lock h263 list\n");
  63. free(tmp);
  64. return NULL;
  65. }
  66. tmp->fd = fd;
  67. tmp->fr.data = tmp->h263;
  68. tmp->fr.frametype = AST_FRAME_VIDEO;
  69. tmp->fr.subclass = AST_FORMAT_H263;
  70. /* datalen will vary for each frame */
  71. tmp->fr.src = name;
  72. tmp->fr.mallocd = 0;
  73. glistcnt++;
  74. ast_mutex_unlock(&h263_lock);
  75. ast_update_use_count();
  76. }
  77. return tmp;
  78. }
  79. static struct ast_filestream *h263_rewrite(int fd, char *comment)
  80. {
  81. /* We don't have any header to read or anything really, but
  82. if we did, it would go here. We also might want to check
  83. and be sure it's a valid file. */
  84. struct ast_filestream *tmp;
  85. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  86. memset(tmp, 0, sizeof(struct ast_filestream));
  87. if (ast_mutex_lock(&h263_lock)) {
  88. ast_log(LOG_WARNING, "Unable to lock h263 list\n");
  89. free(tmp);
  90. return NULL;
  91. }
  92. tmp->fd = fd;
  93. glistcnt++;
  94. ast_mutex_unlock(&h263_lock);
  95. ast_update_use_count();
  96. } else
  97. ast_log(LOG_WARNING, "Out of memory\n");
  98. return tmp;
  99. }
  100. static void h263_close(struct ast_filestream *s)
  101. {
  102. if (ast_mutex_lock(&h263_lock)) {
  103. ast_log(LOG_WARNING, "Unable to lock h263 list\n");
  104. return;
  105. }
  106. glistcnt--;
  107. ast_mutex_unlock(&h263_lock);
  108. ast_update_use_count();
  109. close(s->fd);
  110. free(s);
  111. s = NULL;
  112. }
  113. static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
  114. {
  115. int res;
  116. int mark=0;
  117. unsigned short len;
  118. unsigned int ts;
  119. /* Send a frame from the file to the appropriate channel */
  120. s->fr.frametype = AST_FRAME_VIDEO;
  121. s->fr.subclass = AST_FORMAT_H263;
  122. s->fr.offset = AST_FRIENDLY_OFFSET;
  123. s->fr.mallocd = 0;
  124. s->fr.data = s->h263;
  125. if ((res = read(s->fd, &len, sizeof(len))) < 1) {
  126. return NULL;
  127. }
  128. len = ntohs(len);
  129. if (len & 0x8000) {
  130. mark = 1;
  131. }
  132. len &= 0x7fff;
  133. if (len > sizeof(s->h263)) {
  134. ast_log(LOG_WARNING, "Length %d is too long\n", len);
  135. }
  136. if ((res = read(s->fd, s->h263, len)) != len) {
  137. if (res)
  138. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  139. return NULL;
  140. }
  141. s->fr.samples = s->lastts;
  142. s->fr.datalen = len;
  143. s->fr.subclass |= mark;
  144. if ((res = read(s->fd, &ts, sizeof(ts))) == sizeof(ts)) {
  145. s->lastts = *whennext = ntohl(ts) * 4/45;
  146. } else
  147. *whennext = 0;
  148. return &s->fr;
  149. }
  150. static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
  151. {
  152. int res;
  153. unsigned int ts;
  154. unsigned short len;
  155. int subclass;
  156. int mark=0;
  157. if (f->frametype != AST_FRAME_VIDEO) {
  158. ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
  159. return -1;
  160. }
  161. subclass = f->subclass;
  162. if (subclass & 0x1)
  163. mark=0x8000;
  164. subclass &= ~0x1;
  165. if (subclass != AST_FORMAT_H263) {
  166. ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
  167. return -1;
  168. }
  169. ts = htonl(f->samples);
  170. if ((res = write(fs->fd, &ts, sizeof(ts))) != sizeof(ts)) {
  171. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  172. return -1;
  173. }
  174. len = htons(f->datalen | mark);
  175. if ((res = write(fs->fd, &len, sizeof(len))) != sizeof(len)) {
  176. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  177. return -1;
  178. }
  179. if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
  180. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. static char *h263_getcomment(struct ast_filestream *s)
  186. {
  187. return NULL;
  188. }
  189. static int h263_seek(struct ast_filestream *fs, long sample_offset, int whence)
  190. {
  191. /* No way Jose */
  192. return -1;
  193. }
  194. static int h263_trunc(struct ast_filestream *fs)
  195. {
  196. /* Truncate file to current length */
  197. if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
  198. return -1;
  199. return 0;
  200. }
  201. static long h263_tell(struct ast_filestream *fs)
  202. {
  203. off_t offset;
  204. offset = lseek(fs->fd, 0, SEEK_CUR);
  205. return (offset/20)*160;
  206. }
  207. int load_module()
  208. {
  209. return ast_format_register(name, exts, AST_FORMAT_H263,
  210. h263_open,
  211. h263_rewrite,
  212. h263_write,
  213. h263_seek,
  214. h263_trunc,
  215. h263_tell,
  216. h263_read,
  217. h263_close,
  218. h263_getcomment);
  219. }
  220. int unload_module()
  221. {
  222. return ast_format_unregister(name);
  223. }
  224. int usecount()
  225. {
  226. int res;
  227. if (ast_mutex_lock(&h263_lock)) {
  228. ast_log(LOG_WARNING, "Unable to lock h263 list\n");
  229. return -1;
  230. }
  231. res = glistcnt;
  232. ast_mutex_unlock(&h263_lock);
  233. return res;
  234. }
  235. char *description()
  236. {
  237. return desc;
  238. }
  239. char *key()
  240. {
  241. return ASTERISK_GPL_KEY;
  242. }