format_h263.c 4.8 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. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/mod_format.h"
  28. #include "asterisk/module.h"
  29. #include "asterisk/endian.h"
  30. /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
  31. /* Portions of the conversion code are by guido@sienanet.it */
  32. /* According to:
  33. * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html
  34. * the maximum actual frame size is not 2048, but 8192. Since the maximum
  35. * theoretical limit is not much larger (32k = 15bits), we'll go for that
  36. * size to ensure we don't corrupt frames sent to us (unless they're
  37. * ridiculously large). */
  38. #define BUF_SIZE 32768 /* Four real h.263 Frames */
  39. struct h263_desc {
  40. unsigned int lastts;
  41. };
  42. static int h263_open(struct ast_filestream *s)
  43. {
  44. unsigned int ts;
  45. int res;
  46. if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
  47. ast_log(LOG_WARNING, "Empty file!\n");
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
  53. {
  54. int res;
  55. int mark;
  56. unsigned short len;
  57. unsigned int ts;
  58. struct h263_desc *fs = (struct h263_desc *)s->_private;
  59. /* Send a frame from the file to the appropriate channel */
  60. if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
  61. return NULL;
  62. len = ntohs(len);
  63. mark = (len & 0x8000) ? 1 : 0;
  64. len &= 0x7fff;
  65. if (len > BUF_SIZE) {
  66. ast_log(LOG_WARNING, "Length %d is too long\n", len);
  67. return NULL;
  68. }
  69. s->fr.frametype = AST_FRAME_VIDEO;
  70. s->fr.subclass = AST_FORMAT_H263;
  71. s->fr.mallocd = 0;
  72. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
  73. if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  74. if (res)
  75. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  76. return NULL;
  77. }
  78. s->fr.samples = fs->lastts; /* XXX what ? */
  79. s->fr.datalen = len;
  80. s->fr.subclass |= mark;
  81. s->fr.delivery.tv_sec = 0;
  82. s->fr.delivery.tv_usec = 0;
  83. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  84. fs->lastts = ntohl(ts);
  85. *whennext = fs->lastts * 4/45;
  86. } else
  87. *whennext = 0;
  88. return &s->fr;
  89. }
  90. static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
  91. {
  92. int res;
  93. unsigned int ts;
  94. unsigned short len;
  95. int subclass;
  96. int mark=0;
  97. if (f->frametype != AST_FRAME_VIDEO) {
  98. ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
  99. return -1;
  100. }
  101. subclass = f->subclass;
  102. if (subclass & 0x1)
  103. mark=0x8000;
  104. subclass &= ~0x1;
  105. if (subclass != AST_FORMAT_H263) {
  106. ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
  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, 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. /* Truncate file to current length */
  133. if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
  134. return -1;
  135. return 0;
  136. }
  137. static off_t h263_tell(struct ast_filestream *fs)
  138. {
  139. off_t offset = ftello(fs->f);
  140. return offset; /* XXX totally bogus, needs fixing */
  141. }
  142. static const struct ast_format h263_f = {
  143. .name = "h263",
  144. .exts = "h263",
  145. .format = AST_FORMAT_H263,
  146. .open = h263_open,
  147. .write = h263_write,
  148. .seek = h263_seek,
  149. .trunc = h263_trunc,
  150. .tell = h263_tell,
  151. .read = h263_read,
  152. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  153. .desc_size = sizeof(struct h263_desc),
  154. };
  155. static int load_module(void)
  156. {
  157. if (ast_format_register(&h263_f))
  158. return AST_MODULE_LOAD_FAILURE;
  159. return AST_MODULE_LOAD_SUCCESS;
  160. }
  161. static int unload_module(void)
  162. {
  163. return ast_format_unregister(h263_f.name);
  164. }
  165. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_FIRST, "Raw H.263 data",
  166. .load = load_module,
  167. .unload = unload_module,
  168. );