format_h263.c 6.3 KB

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