flv_bytestream.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*****************************************************************************
  2. * flv_bytestream.c: flv muxer utilities
  3. *****************************************************************************
  4. * Copyright (C) 2009-2018 x264 project
  5. *
  6. * Authors: Kieran Kunhya <kieran@kunhya.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  21. *
  22. * This program is also available under a commercial proprietary license.
  23. * For more information, contact us at licensing@x264.com.
  24. *****************************************************************************/
  25. #include "output.h"
  26. #include "flv_bytestream.h"
  27. uint64_t flv_dbl2int( double value )
  28. {
  29. return (union {double f; uint64_t i;}){value}.i;
  30. }
  31. /* Put functions */
  32. void flv_put_byte( flv_buffer *c, uint8_t b )
  33. {
  34. flv_append_data( c, &b, 1 );
  35. }
  36. void flv_put_be32( flv_buffer *c, uint32_t val )
  37. {
  38. flv_put_byte( c, val >> 24 );
  39. flv_put_byte( c, val >> 16 );
  40. flv_put_byte( c, val >> 8 );
  41. flv_put_byte( c, val );
  42. }
  43. void flv_put_be64( flv_buffer *c, uint64_t val )
  44. {
  45. flv_put_be32( c, val >> 32 );
  46. flv_put_be32( c, val );
  47. }
  48. void flv_put_be16( flv_buffer *c, uint16_t val )
  49. {
  50. flv_put_byte( c, val >> 8 );
  51. flv_put_byte( c, val );
  52. }
  53. void flv_put_be24( flv_buffer *c, uint32_t val )
  54. {
  55. flv_put_be16( c, val >> 8 );
  56. flv_put_byte( c, val );
  57. }
  58. void flv_put_tag( flv_buffer *c, const char *tag )
  59. {
  60. while( *tag )
  61. flv_put_byte( c, *tag++ );
  62. }
  63. void flv_put_amf_string( flv_buffer *c, const char *str )
  64. {
  65. uint16_t len = strlen( str );
  66. flv_put_be16( c, len );
  67. flv_append_data( c, (uint8_t*)str, len );
  68. }
  69. void flv_put_amf_double( flv_buffer *c, double d )
  70. {
  71. flv_put_byte( c, AMF_DATA_TYPE_NUMBER );
  72. flv_put_be64( c, flv_dbl2int( d ) );
  73. }
  74. /* flv writing functions */
  75. flv_buffer *flv_create_writer( const char *filename )
  76. {
  77. flv_buffer *c = calloc( 1, sizeof(flv_buffer) );
  78. if( !c )
  79. return NULL;
  80. if( !strcmp( filename, "-" ) )
  81. c->fp = stdout;
  82. else
  83. c->fp = x264_fopen( filename, "wb" );
  84. if( !c->fp )
  85. {
  86. free( c );
  87. return NULL;
  88. }
  89. return c;
  90. }
  91. int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
  92. {
  93. unsigned ns = c->d_cur + size;
  94. if( ns > c->d_max )
  95. {
  96. void *dp;
  97. unsigned dn = 16;
  98. while( ns > dn )
  99. dn <<= 1;
  100. dp = realloc( c->data, dn );
  101. if( !dp )
  102. return -1;
  103. c->data = dp;
  104. c->d_max = dn;
  105. }
  106. memcpy( c->data + c->d_cur, data, size );
  107. c->d_cur = ns;
  108. return 0;
  109. }
  110. void flv_rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
  111. {
  112. *(c->data + start + 0) = length >> 16;
  113. *(c->data + start + 1) = length >> 8;
  114. *(c->data + start + 2) = length >> 0;
  115. }
  116. int flv_flush_data( flv_buffer *c )
  117. {
  118. if( !c->d_cur )
  119. return 0;
  120. if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
  121. return -1;
  122. c->d_total += c->d_cur;
  123. c->d_cur = 0;
  124. return 0;
  125. }