format_h264.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /* 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 BUF_SIZE 4096 /* Two Real h264 Frames */
  37. struct h264_desc {
  38. unsigned int lastts;
  39. };
  40. static int h264_open(struct ast_filestream *s)
  41. {
  42. unsigned int ts;
  43. int res;
  44. if ((res = 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. int 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)) < 1)
  59. return NULL;
  60. len = ntohs(len);
  61. mark = (len & 0x8000) ? 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. s->fr.frametype = AST_FRAME_VIDEO;
  68. ast_format_set(&s->fr.subclass.format, AST_FORMAT_H264, 0);
  69. s->fr.mallocd = 0;
  70. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
  71. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  72. if (res)
  73. ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
  74. return NULL;
  75. }
  76. s->fr.samples = fs->lastts;
  77. s->fr.datalen = len;
  78. if (mark) {
  79. ast_format_set_video_mark(&s->fr.subclass.format);
  80. }
  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 h264_write(struct ast_filestream *s, struct ast_frame *f)
  91. {
  92. int res;
  93. unsigned int ts;
  94. unsigned short len;
  95. int mark;
  96. if (f->frametype != AST_FRAME_VIDEO) {
  97. ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
  98. return -1;
  99. }
  100. mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0;
  101. if (f->subclass.format.id != AST_FORMAT_H264) {
  102. ast_log(LOG_WARNING, "Asked to write non-h264 frame (%s)!\n", ast_getformatname(&f->subclass.format));
  103. return -1;
  104. }
  105. ts = htonl(f->samples);
  106. if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
  107. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  108. return -1;
  109. }
  110. len = htons(f->datalen | mark);
  111. if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
  112. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  113. return -1;
  114. }
  115. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  116. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  122. {
  123. /* No way Jose */
  124. return -1;
  125. }
  126. static int h264_trunc(struct ast_filestream *fs)
  127. {
  128. int fd;
  129. off_t cur;
  130. if ((fd = fileno(fs->f)) < 0) {
  131. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno));
  132. return -1;
  133. }
  134. if ((cur = ftello(fs->f)) < 0) {
  135. ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno));
  136. return -1;
  137. }
  138. /* Truncate file to current length */
  139. return ftruncate(fd, cur);
  140. }
  141. static off_t h264_tell(struct ast_filestream *fs)
  142. {
  143. off_t offset = ftell(fs->f);
  144. return offset; /* XXX totally bogus, needs fixing */
  145. }
  146. static struct ast_format_def h264_f = {
  147. .name = "h264",
  148. .exts = "h264",
  149. .open = h264_open,
  150. .write = h264_write,
  151. .seek = h264_seek,
  152. .trunc = h264_trunc,
  153. .tell = h264_tell,
  154. .read = h264_read,
  155. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  156. .desc_size = sizeof(struct h264_desc),
  157. };
  158. static int load_module(void)
  159. {
  160. ast_format_set(&h264_f.format, AST_FORMAT_H264, 0);
  161. if (ast_format_def_register(&h264_f))
  162. return AST_MODULE_LOAD_FAILURE;
  163. return AST_MODULE_LOAD_SUCCESS;
  164. }
  165. static int unload_module(void)
  166. {
  167. return ast_format_def_unregister(h264_f.name);
  168. }
  169. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.264 data",
  170. .load = load_module,
  171. .unload = unload_module,
  172. .load_pri = AST_MODPRI_APP_DEPEND
  173. );