format_h263.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "asterisk/format_cache.h"
  34. /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
  35. /* Portions of the conversion code are by guido@sienanet.it */
  36. /* According to:
  37. * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
  38. * the maximum actual frame size is not 2048, but 8192. Since the maximum
  39. * theoretical limit is not much larger (32k = 15bits), we'll go for that
  40. * size to ensure we don't corrupt frames sent to us (unless they're
  41. * ridiculously large). */
  42. #define BUF_SIZE 32768 /* Four real h.263 Frames */
  43. #define FRAME_ENDED 0x8000
  44. struct h263_desc {
  45. unsigned int lastts;
  46. };
  47. static int h263_open(struct ast_filestream *s)
  48. {
  49. unsigned int ts;
  50. if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
  51. ast_log(LOG_WARNING, "Empty file!\n");
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
  57. {
  58. size_t res;
  59. uint32_t mark;
  60. unsigned short len;
  61. unsigned int ts;
  62. struct h263_desc *fs = (struct h263_desc *)s->_private;
  63. /* Send a frame from the file to the appropriate channel */
  64. if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
  65. return NULL;
  66. len = ntohs(len);
  67. mark = (len & FRAME_ENDED) ? 1 : 0;
  68. len &= 0x7fff;
  69. if (len > BUF_SIZE) {
  70. ast_log(LOG_WARNING, "Length %d is too long\n", len);
  71. return NULL;
  72. }
  73. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
  74. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  75. if (res) {
  76. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  77. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  78. strerror(errno));
  79. }
  80. return NULL;
  81. }
  82. s->fr.samples = fs->lastts; /* XXX what ? */
  83. s->fr.datalen = len;
  84. s->fr.subclass.frame_ending = mark;
  85. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  86. fs->lastts = ntohl(ts);
  87. *whennext = fs->lastts * 4/45;
  88. } else
  89. *whennext = 0;
  90. return &s->fr;
  91. }
  92. static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
  93. {
  94. int res;
  95. unsigned int ts;
  96. unsigned short len;
  97. uint32_t mark = 0;
  98. mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
  99. ts = htonl(f->samples);
  100. if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
  101. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  102. return -1;
  103. }
  104. len = htons(f->datalen | mark);
  105. if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
  106. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  107. return -1;
  108. }
  109. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  110. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  116. {
  117. /* No way Jose */
  118. return -1;
  119. }
  120. static int h263_trunc(struct ast_filestream *fs)
  121. {
  122. int fd;
  123. off_t cur;
  124. if ((fd = fileno(fs->f)) < 0) {
  125. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
  126. return -1;
  127. }
  128. if ((cur = ftello(fs->f)) < 0) {
  129. ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
  130. return -1;
  131. }
  132. /* Truncate file to current length */
  133. return ftruncate(fd, cur);
  134. }
  135. static off_t h263_tell(struct ast_filestream *fs)
  136. {
  137. off_t offset = ftello(fs->f);
  138. return offset; /* XXX totally bogus, needs fixing */
  139. }
  140. static struct ast_format_def h263_f = {
  141. .name = "h263",
  142. .exts = "h263",
  143. .open = h263_open,
  144. .write = h263_write,
  145. .seek = h263_seek,
  146. .trunc = h263_trunc,
  147. .tell = h263_tell,
  148. .read = h263_read,
  149. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  150. .desc_size = sizeof(struct h263_desc),
  151. };
  152. static int load_module(void)
  153. {
  154. h263_f.format = ast_format_h263;
  155. if (ast_format_def_register(&h263_f))
  156. return AST_MODULE_LOAD_DECLINE;
  157. return AST_MODULE_LOAD_SUCCESS;
  158. }
  159. static int unload_module(void)
  160. {
  161. return ast_format_def_unregister(h263_f.name);
  162. }
  163. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
  164. .support_level = AST_MODULE_SUPPORT_CORE,
  165. .load = load_module,
  166. .unload = unload_module,
  167. .load_pri = AST_MODPRI_APP_DEPEND
  168. );