format_h264.c 4.6 KB

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