format_h263.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. int 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)) < 1)
  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 (%d) (%s)!\n", res, strerror(errno));
  77. return NULL;
  78. }
  79. s->fr.samples = fs->lastts; /* XXX what ? */
  80. s->fr.datalen = len;
  81. s->fr.subclass.frame_ending = mark;
  82. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  83. fs->lastts = ntohl(ts);
  84. *whennext = fs->lastts * 4/45;
  85. } else
  86. *whennext = 0;
  87. return &s->fr;
  88. }
  89. static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
  90. {
  91. int res;
  92. unsigned int ts;
  93. unsigned short len;
  94. uint32_t mark = 0;
  95. mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
  96. ts = htonl(f->samples);
  97. if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
  98. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  99. return -1;
  100. }
  101. len = htons(f->datalen | mark);
  102. if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
  103. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  104. return -1;
  105. }
  106. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  107. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  113. {
  114. /* No way Jose */
  115. return -1;
  116. }
  117. static int h263_trunc(struct ast_filestream *fs)
  118. {
  119. int fd;
  120. off_t cur;
  121. if ((fd = fileno(fs->f)) < 0) {
  122. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
  123. return -1;
  124. }
  125. if ((cur = ftello(fs->f)) < 0) {
  126. ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
  127. return -1;
  128. }
  129. /* Truncate file to current length */
  130. return ftruncate(fd, cur);
  131. }
  132. static off_t h263_tell(struct ast_filestream *fs)
  133. {
  134. off_t offset = ftello(fs->f);
  135. return offset; /* XXX totally bogus, needs fixing */
  136. }
  137. static struct ast_format_def h263_f = {
  138. .name = "h263",
  139. .exts = "h263",
  140. .open = h263_open,
  141. .write = h263_write,
  142. .seek = h263_seek,
  143. .trunc = h263_trunc,
  144. .tell = h263_tell,
  145. .read = h263_read,
  146. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  147. .desc_size = sizeof(struct h263_desc),
  148. };
  149. static int load_module(void)
  150. {
  151. h263_f.format = ast_format_h263;
  152. if (ast_format_def_register(&h263_f))
  153. return AST_MODULE_LOAD_FAILURE;
  154. return AST_MODULE_LOAD_SUCCESS;
  155. }
  156. static int unload_module(void)
  157. {
  158. return ast_format_def_unregister(h263_f.name);
  159. }
  160. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
  161. .support_level = AST_MODULE_SUPPORT_CORE,
  162. .load = load_module,
  163. .unload = unload_module,
  164. .load_pri = AST_MODPRI_APP_DEPEND
  165. );