format_h263.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /*! \file
  19. *
  20. * \brief Save to raw, headerless h263 data.
  21. * \arg File name extension: h263
  22. * \ingroup formats
  23. * \arg See \ref AstVideo
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/mod_format.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/endian.h"
  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. /* According to:
  36. * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
  37. * the maximum actual frame size is not 2048, but 8192. Since the maximum
  38. * theoretical limit is not much larger (32k = 15bits), we'll go for that
  39. * size to ensure we don't corrupt frames sent to us (unless they're
  40. * ridiculously large). */
  41. #define BUF_SIZE 32768 /* Four real h.263 Frames */
  42. struct h263_desc {
  43. unsigned int lastts;
  44. };
  45. static int h263_open(struct ast_filestream *s)
  46. {
  47. unsigned int ts;
  48. int res;
  49. if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
  50. ast_log(LOG_WARNING, "Empty file!\n");
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
  56. {
  57. int res;
  58. uint32_t mark;
  59. unsigned short len;
  60. unsigned int ts;
  61. struct h263_desc *fs = (struct h263_desc *)s->_private;
  62. /* Send a frame from the file to the appropriate channel */
  63. if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
  64. return NULL;
  65. len = ntohs(len);
  66. mark = (len & 0x8000) ? 1 : 0;
  67. len &= 0x7fff;
  68. if (len > BUF_SIZE) {
  69. ast_log(LOG_WARNING, "Length %d is too long\n", len);
  70. return NULL;
  71. }
  72. s->fr.frametype = AST_FRAME_VIDEO;
  73. ast_format_set(&s->fr.subclass.format, AST_FORMAT_H263, 0);
  74. s->fr.mallocd = 0;
  75. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
  76. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  77. if (res)
  78. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  79. return NULL;
  80. }
  81. s->fr.samples = fs->lastts; /* XXX what ? */
  82. s->fr.datalen = len;
  83. if (mark) {
  84. ast_format_set_video_mark(&s->fr.subclass.format);
  85. }
  86. s->fr.delivery.tv_sec = 0;
  87. s->fr.delivery.tv_usec = 0;
  88. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  89. fs->lastts = ntohl(ts);
  90. *whennext = fs->lastts * 4/45;
  91. } else
  92. *whennext = 0;
  93. return &s->fr;
  94. }
  95. static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
  96. {
  97. int res;
  98. unsigned int ts;
  99. unsigned short len;
  100. uint32_t mark = 0;
  101. if (f->frametype != AST_FRAME_VIDEO) {
  102. ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
  103. return -1;
  104. }
  105. mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0;
  106. if (f->subclass.format.id != AST_FORMAT_H263) {
  107. ast_log(LOG_WARNING, "Asked to write non-h263 frame (%s)!\n", ast_getformatname(&f->subclass.format));
  108. return -1;
  109. }
  110. ts = htonl(f->samples);
  111. if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
  112. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  113. return -1;
  114. }
  115. len = htons(f->datalen | mark);
  116. if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
  117. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  118. return -1;
  119. }
  120. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  121. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  127. {
  128. /* No way Jose */
  129. return -1;
  130. }
  131. static int h263_trunc(struct ast_filestream *fs)
  132. {
  133. int fd;
  134. off_t cur;
  135. if ((fd = fileno(fs->f)) < 0) {
  136. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
  137. return -1;
  138. }
  139. if ((cur = ftello(fs->f)) < 0) {
  140. ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
  141. return -1;
  142. }
  143. /* Truncate file to current length */
  144. return ftruncate(fd, cur);
  145. }
  146. static off_t h263_tell(struct ast_filestream *fs)
  147. {
  148. off_t offset = ftello(fs->f);
  149. return offset; /* XXX totally bogus, needs fixing */
  150. }
  151. static struct ast_format_def h263_f = {
  152. .name = "h263",
  153. .exts = "h263",
  154. .open = h263_open,
  155. .write = h263_write,
  156. .seek = h263_seek,
  157. .trunc = h263_trunc,
  158. .tell = h263_tell,
  159. .read = h263_read,
  160. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  161. .desc_size = sizeof(struct h263_desc),
  162. };
  163. static int load_module(void)
  164. {
  165. ast_format_set(&h263_f.format, AST_FORMAT_H263, 0);
  166. if (ast_format_def_register(&h263_f))
  167. return AST_MODULE_LOAD_FAILURE;
  168. return AST_MODULE_LOAD_SUCCESS;
  169. }
  170. static int unload_module(void)
  171. {
  172. return ast_format_def_unregister(h263_f.name);
  173. }
  174. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
  175. .load = load_module,
  176. .unload = unload_module,
  177. .load_pri = AST_MODPRI_APP_DEPEND
  178. );