You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.7KB

  1. /*
  2. * FLV encoder.
  3. * Copyright (c) 2003 The FFmpeg Project.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #undef NDEBUG
  21. #include <assert.h>
  22. typedef struct FLVContext {
  23. int hasAudio;
  24. int hasVideo;
  25. int reserved;
  26. } FLVContext;
  27. static void put_be24(ByteIOContext *pb, int value)
  28. {
  29. put_byte(pb, (value>>16) & 0xFF );
  30. put_byte(pb, (value>> 8) & 0xFF );
  31. put_byte(pb, (value>> 0) & 0xFF );
  32. }
  33. static int get_audio_flags(AVCodecContext *enc){
  34. int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0;
  35. switch (enc->sample_rate) {
  36. case 44100:
  37. flags |= 0x0C;
  38. break;
  39. case 22050:
  40. flags |= 0x08;
  41. break;
  42. case 11025:
  43. flags |= 0x04;
  44. break;
  45. case 8000: //nellymoser only
  46. case 5512: //not mp3
  47. flags |= 0x00;
  48. break;
  49. default:
  50. return -1;
  51. }
  52. if (enc->channels > 1) {
  53. flags |= 0x01;
  54. }
  55. switch(enc->codec_id){
  56. case CODEC_ID_MP3:
  57. flags |= 0x20 | 0x2;
  58. break;
  59. case CODEC_ID_PCM_S8:
  60. break;
  61. case CODEC_ID_PCM_S16BE:
  62. flags |= 0x60 | 0x2;
  63. break;
  64. case CODEC_ID_PCM_S16LE:
  65. flags |= 0x2;
  66. break;
  67. case 0:
  68. flags |= enc->codec_tag<<4;
  69. break;
  70. default:
  71. return -1;
  72. }
  73. return flags;
  74. }
  75. static int flv_write_header(AVFormatContext *s)
  76. {
  77. ByteIOContext *pb = &s->pb;
  78. FLVContext *flv = s->priv_data;
  79. int i;
  80. flv->hasAudio = 0;
  81. flv->hasVideo = 0;
  82. put_tag(pb,"FLV");
  83. put_byte(pb,1);
  84. put_byte(pb,0); // delayed write
  85. put_be32(pb,9);
  86. put_be32(pb,0);
  87. for(i=0; i<s->nb_streams; i++){
  88. AVCodecContext *enc = &s->streams[i]->codec;
  89. av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
  90. if(enc->codec_tag == 5){
  91. put_byte(pb,8); // message type
  92. put_be24(pb,0); // include flags
  93. put_be24(pb,0); // time stamp
  94. put_be32(pb,0); // reserved
  95. put_be32(pb,11); // size
  96. flv->reserved=5;
  97. }
  98. if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
  99. return -1;
  100. }
  101. return 0;
  102. }
  103. static int flv_write_trailer(AVFormatContext *s)
  104. {
  105. int64_t file_size;
  106. int flags = 0;
  107. ByteIOContext *pb = &s->pb;
  108. FLVContext *flv = s->priv_data;
  109. file_size = url_ftell(pb);
  110. flags |= flv->hasAudio ? 4 : 0;
  111. flags |= flv->hasVideo ? 1 : 0;
  112. url_fseek(pb, 4, SEEK_SET);
  113. put_byte(pb,flags);
  114. url_fseek(pb, file_size, SEEK_SET);
  115. return 0;
  116. }
  117. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  118. {
  119. ByteIOContext *pb = &s->pb;
  120. AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
  121. FLVContext *flv = s->priv_data;
  122. int size= pkt->size;
  123. int flags;
  124. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
  125. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  126. put_byte(pb, 9);
  127. flags = 2; // choose h263
  128. flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
  129. flv->hasVideo = 1;
  130. } else {
  131. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  132. flags = get_audio_flags(enc);
  133. assert(size);
  134. put_byte(pb, 8);
  135. // We got audio! Make sure we set this to the global flags on closure
  136. flv->hasAudio = 1;
  137. }
  138. put_be24(pb,size+1); // include flags
  139. put_be24(pb,pkt->pts);
  140. put_be32(pb,flv->reserved);
  141. put_byte(pb,flags);
  142. put_buffer(pb, pkt->data, size);
  143. put_be32(pb,size+1+11); // previous tag size
  144. put_flush_packet(pb);
  145. return 0;
  146. }
  147. static AVOutputFormat flv_oformat = {
  148. "flv",
  149. "flv format",
  150. "video/x-flv",
  151. "flv",
  152. sizeof(FLVContext),
  153. #ifdef CONFIG_MP3LAME
  154. CODEC_ID_MP3,
  155. #else // CONFIG_MP3LAME
  156. CODEC_ID_NONE,
  157. #endif // CONFIG_MP3LAME
  158. CODEC_ID_FLV1,
  159. flv_write_header,
  160. flv_write_packet,
  161. flv_write_trailer,
  162. };
  163. int flvenc_init(void)
  164. {
  165. av_register_output_format(&flv_oformat);
  166. return 0;
  167. }