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