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.

186 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 int get_audio_flags(AVCodecContext *enc){
  28. int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0;
  29. switch (enc->sample_rate) {
  30. case 44100:
  31. flags |= 0x0C;
  32. break;
  33. case 22050:
  34. flags |= 0x08;
  35. break;
  36. case 11025:
  37. flags |= 0x04;
  38. break;
  39. case 8000: //nellymoser only
  40. case 5512: //not mp3
  41. flags |= 0x00;
  42. break;
  43. default:
  44. av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
  45. return -1;
  46. }
  47. if (enc->channels > 1) {
  48. flags |= 0x01;
  49. }
  50. switch(enc->codec_id){
  51. case CODEC_ID_MP3:
  52. flags |= 0x20 | 0x2;
  53. break;
  54. case CODEC_ID_PCM_S8:
  55. break;
  56. case CODEC_ID_PCM_S16BE:
  57. flags |= 0x60 | 0x2;
  58. break;
  59. case CODEC_ID_PCM_S16LE:
  60. flags |= 0x2;
  61. break;
  62. case CODEC_ID_ADPCM_SWF:
  63. flags |= 0x10;
  64. break;
  65. case 0:
  66. flags |= enc->codec_tag<<4;
  67. break;
  68. default:
  69. av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
  70. return -1;
  71. }
  72. return flags;
  73. }
  74. static int flv_write_header(AVFormatContext *s)
  75. {
  76. ByteIOContext *pb = &s->pb;
  77. FLVContext *flv = s->priv_data;
  78. int i;
  79. flv->hasAudio = 0;
  80. flv->hasVideo = 0;
  81. put_tag(pb,"FLV");
  82. put_byte(pb,1);
  83. put_byte(pb,0); // delayed write
  84. put_be32(pb,9);
  85. put_be32(pb,0);
  86. for(i=0; i<s->nb_streams; i++){
  87. AVCodecContext *enc = s->streams[i]->codec;
  88. av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
  89. if(enc->codec_tag == 5){
  90. put_byte(pb,8); // message type
  91. put_be24(pb,0); // include flags
  92. put_be24(pb,0); // time stamp
  93. put_be32(pb,0); // reserved
  94. put_be32(pb,11); // size
  95. flv->reserved=5;
  96. }
  97. if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. static int flv_write_trailer(AVFormatContext *s)
  103. {
  104. int64_t file_size;
  105. int flags = 0;
  106. ByteIOContext *pb = &s->pb;
  107. FLVContext *flv = s->priv_data;
  108. file_size = url_ftell(pb);
  109. flags |= flv->hasAudio ? 4 : 0;
  110. flags |= flv->hasVideo ? 1 : 0;
  111. url_fseek(pb, 4, SEEK_SET);
  112. put_byte(pb,flags);
  113. url_fseek(pb, file_size, SEEK_SET);
  114. return 0;
  115. }
  116. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  117. {
  118. ByteIOContext *pb = &s->pb;
  119. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  120. FLVContext *flv = s->priv_data;
  121. int size= pkt->size;
  122. int flags;
  123. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
  124. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  125. put_byte(pb, 9);
  126. flags = 2; // choose h263
  127. flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
  128. flv->hasVideo = 1;
  129. } else {
  130. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  131. flags = get_audio_flags(enc);
  132. assert(size);
  133. put_byte(pb, 8);
  134. // We got audio! Make sure we set this to the global flags on closure
  135. flv->hasAudio = 1;
  136. }
  137. put_be24(pb,size+1); // include flags
  138. put_be24(pb,pkt->pts);
  139. put_be32(pb,flv->reserved);
  140. put_byte(pb,flags);
  141. put_buffer(pb, pkt->data, size);
  142. put_be32(pb,size+1+11); // previous tag size
  143. put_flush_packet(pb);
  144. return 0;
  145. }
  146. AVOutputFormat flv_muxer = {
  147. "flv",
  148. "flv format",
  149. "video/x-flv",
  150. "flv",
  151. sizeof(FLVContext),
  152. #ifdef CONFIG_MP3LAME
  153. CODEC_ID_MP3,
  154. #else // CONFIG_MP3LAME
  155. CODEC_ID_NONE,
  156. #endif // CONFIG_MP3LAME
  157. CODEC_ID_FLV1,
  158. flv_write_header,
  159. flv_write_packet,
  160. flv_write_trailer,
  161. };