format_h264.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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 h264 data.
  21. * \arg File name extension: h264
  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 makeh264e.c by Jeffrey Chilton */
  35. /* Portions of the conversion code are by guido@sienanet.it */
  36. /*! \todo Check this buf size estimate, it may be totally wrong for large frame video */
  37. #define FRAME_ENDED 0x8000
  38. #define BUF_SIZE 4096 /* Two Real h264 Frames */
  39. struct h264_desc {
  40. unsigned int lastts;
  41. };
  42. static int h264_open(struct ast_filestream *s)
  43. {
  44. unsigned int ts;
  45. if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
  46. ast_log(LOG_WARNING, "Empty file!\n");
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
  52. {
  53. size_t res;
  54. int mark = 0;
  55. unsigned short len;
  56. unsigned int ts;
  57. struct h264_desc *fs = (struct h264_desc *)s->_private;
  58. /* Send a frame from the file to the appropriate channel */
  59. if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
  60. return NULL;
  61. len = ntohs(len);
  62. mark = (len & FRAME_ENDED) ? 1 : 0;
  63. len &= 0x7fff;
  64. if (len > BUF_SIZE) {
  65. ast_log(LOG_WARNING, "Length %d is too long\n", len);
  66. len = BUF_SIZE; /* XXX truncate */
  67. }
  68. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
  69. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  70. if (res) {
  71. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  72. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  73. strerror(errno));
  74. }
  75. return NULL;
  76. }
  77. s->fr.samples = fs->lastts;
  78. s->fr.datalen = len;
  79. s->fr.subclass.frame_ending = mark;
  80. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  81. fs->lastts = ntohl(ts);
  82. *whennext = fs->lastts * 4/45;
  83. } else
  84. *whennext = 0;
  85. return &s->fr;
  86. }
  87. static int h264_write(struct ast_filestream *s, struct ast_frame *f)
  88. {
  89. int res;
  90. unsigned int ts;
  91. unsigned short len;
  92. int mark;
  93. mark = f->subclass.frame_ending ? FRAME_ENDED : 0;
  94. ts = htonl(f->samples);
  95. if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
  96. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  97. return -1;
  98. }
  99. len = htons(f->datalen | mark);
  100. if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
  101. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  102. return -1;
  103. }
  104. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  105. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  111. {
  112. /* No way Jose */
  113. return -1;
  114. }
  115. static int h264_trunc(struct ast_filestream *fs)
  116. {
  117. int fd;
  118. off_t cur;
  119. if ((fd = fileno(fs->f)) < 0) {
  120. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno));
  121. return -1;
  122. }
  123. if ((cur = ftello(fs->f)) < 0) {
  124. ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno));
  125. return -1;
  126. }
  127. /* Truncate file to current length */
  128. return ftruncate(fd, cur);
  129. }
  130. static off_t h264_tell(struct ast_filestream *fs)
  131. {
  132. off_t offset = ftell(fs->f);
  133. return offset; /* XXX totally bogus, needs fixing */
  134. }
  135. static struct ast_format_def h264_f = {
  136. .name = "h264",
  137. .exts = "h264",
  138. .open = h264_open,
  139. .write = h264_write,
  140. .seek = h264_seek,
  141. .trunc = h264_trunc,
  142. .tell = h264_tell,
  143. .read = h264_read,
  144. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  145. .desc_size = sizeof(struct h264_desc),
  146. };
  147. static int load_module(void)
  148. {
  149. h264_f.format = ast_format_h264;
  150. if (ast_format_def_register(&h264_f))
  151. return AST_MODULE_LOAD_DECLINE;
  152. return AST_MODULE_LOAD_SUCCESS;
  153. }
  154. static int unload_module(void)
  155. {
  156. return ast_format_def_unregister(h264_f.name);
  157. }
  158. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.264 data",
  159. .support_level = AST_MODULE_SUPPORT_CORE,
  160. .load = load_module,
  161. .unload = unload_module,
  162. .load_pri = AST_MODPRI_APP_DEPEND
  163. );