format_h263.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
  49. ast_log(LOG_WARNING, "Empty file!\n");
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
  55. {
  56. int res;
  57. uint32_t mark;
  58. unsigned short len;
  59. unsigned int ts;
  60. struct h263_desc *fs = (struct h263_desc *)s->_private;
  61. /* Send a frame from the file to the appropriate channel */
  62. if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
  63. return NULL;
  64. len = ntohs(len);
  65. mark = (len & 0x8000) ? 1 : 0;
  66. len &= 0x7fff;
  67. if (len > BUF_SIZE) {
  68. ast_log(LOG_WARNING, "Length %d is too long\n", len);
  69. return NULL;
  70. }
  71. s->fr.frametype = AST_FRAME_VIDEO;
  72. ast_format_set(&s->fr.subclass.format, AST_FORMAT_H263, 0);
  73. s->fr.mallocd = 0;
  74. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
  75. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  76. if (res)
  77. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  78. return NULL;
  79. }
  80. s->fr.samples = fs->lastts; /* XXX what ? */
  81. s->fr.datalen = len;
  82. if (mark) {
  83. ast_format_set_video_mark(&s->fr.subclass.format);
  84. }
  85. s->fr.delivery.tv_sec = 0;
  86. s->fr.delivery.tv_usec = 0;
  87. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  88. fs->lastts = ntohl(ts);
  89. *whennext = fs->lastts * 4/45;
  90. } else
  91. *whennext = 0;
  92. return &s->fr;
  93. }
  94. static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
  95. {
  96. int res;
  97. unsigned int ts;
  98. unsigned short len;
  99. uint32_t mark = 0;
  100. if (f->frametype != AST_FRAME_VIDEO) {
  101. ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
  102. return -1;
  103. }
  104. mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0;
  105. if (f->subclass.format.id != AST_FORMAT_H263) {
  106. ast_log(LOG_WARNING, "Asked to write non-h263 frame (%s)!\n", ast_getformatname(&f->subclass.format));
  107. return -1;
  108. }
  109. ts = htonl(f->samples);
  110. if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
  111. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  112. return -1;
  113. }
  114. len = htons(f->datalen | mark);
  115. if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
  116. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  117. return -1;
  118. }
  119. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  120. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  126. {
  127. /* No way Jose */
  128. return -1;
  129. }
  130. static int h263_trunc(struct ast_filestream *fs)
  131. {
  132. int fd;
  133. off_t cur;
  134. if ((fd = fileno(fs->f)) < 0) {
  135. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno));
  136. return -1;
  137. }
  138. if ((cur = ftello(fs->f)) < 0) {
  139. ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno));
  140. return -1;
  141. }
  142. /* Truncate file to current length */
  143. return ftruncate(fd, cur);
  144. }
  145. static off_t h263_tell(struct ast_filestream *fs)
  146. {
  147. off_t offset = ftello(fs->f);
  148. return offset; /* XXX totally bogus, needs fixing */
  149. }
  150. static struct ast_format_def h263_f = {
  151. .name = "h263",
  152. .exts = "h263",
  153. .open = h263_open,
  154. .write = h263_write,
  155. .seek = h263_seek,
  156. .trunc = h263_trunc,
  157. .tell = h263_tell,
  158. .read = h263_read,
  159. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  160. .desc_size = sizeof(struct h263_desc),
  161. };
  162. static int load_module(void)
  163. {
  164. ast_format_set(&h263_f.format, AST_FORMAT_H263, 0);
  165. if (ast_format_def_register(&h263_f))
  166. return AST_MODULE_LOAD_FAILURE;
  167. return AST_MODULE_LOAD_SUCCESS;
  168. }
  169. static int unload_module(void)
  170. {
  171. return ast_format_def_unregister(h263_f.name);
  172. }
  173. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data",
  174. .load = load_module,
  175. .unload = unload_module,
  176. .load_pri = AST_MODPRI_APP_DEPEND
  177. );