format_h264.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. s->fr.subclass.codec = AST_FORMAT_H264;
  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. s->fr.subclass.codec |= mark;
  79. s->fr.delivery.tv_sec = 0;
  80. s->fr.delivery.tv_usec = 0;
  81. if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
  82. fs->lastts = ntohl(ts);
  83. *whennext = fs->lastts * 4/45;
  84. } else
  85. *whennext = 0;
  86. return &s->fr;
  87. }
  88. static int h264_write(struct ast_filestream *s, struct ast_frame *f)
  89. {
  90. int res;
  91. unsigned int ts;
  92. unsigned short len;
  93. int mark;
  94. if (f->frametype != AST_FRAME_VIDEO) {
  95. ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
  96. return -1;
  97. }
  98. mark = (f->subclass.codec & 0x1) ? 0x8000 : 0;
  99. if ((f->subclass.codec & ~0x1) != AST_FORMAT_H264) {
  100. ast_log(LOG_WARNING, "Asked to write non-h264 frame (%s)!\n", ast_getformatname(f->subclass.codec));
  101. return -1;
  102. }
  103. ts = htonl(f->samples);
  104. if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
  105. ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
  106. return -1;
  107. }
  108. len = htons(f->datalen | mark);
  109. if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
  110. ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
  111. return -1;
  112. }
  113. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  114. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  120. {
  121. /* No way Jose */
  122. return -1;
  123. }
  124. static int h264_trunc(struct ast_filestream *fs)
  125. {
  126. /* Truncate file to current length */
  127. if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
  128. return -1;
  129. return 0;
  130. }
  131. static off_t h264_tell(struct ast_filestream *fs)
  132. {
  133. off_t offset = ftell(fs->f);
  134. return offset; /* XXX totally bogus, needs fixing */
  135. }
  136. static const struct ast_format h264_f = {
  137. .name = "h264",
  138. .exts = "h264",
  139. .format = AST_FORMAT_H264,
  140. .open = h264_open,
  141. .write = h264_write,
  142. .seek = h264_seek,
  143. .trunc = h264_trunc,
  144. .tell = h264_tell,
  145. .read = h264_read,
  146. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  147. .desc_size = sizeof(struct h264_desc),
  148. };
  149. static int load_module(void)
  150. {
  151. if (ast_format_register(&h264_f))
  152. return AST_MODULE_LOAD_FAILURE;
  153. return AST_MODULE_LOAD_SUCCESS;
  154. }
  155. static int unload_module(void)
  156. {
  157. return ast_format_unregister(h264_f.name);
  158. }
  159. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.264 data",
  160. .load = load_module,
  161. .unload = unload_module,
  162. .load_pri = AST_MODPRI_APP_DEPEND
  163. );