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.

189 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 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 0:
  63. flags |= enc->codec_tag<<4;
  64. break;
  65. default:
  66. av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
  67. return -1;
  68. }
  69. return flags;
  70. }
  71. static int flv_write_header(AVFormatContext *s)
  72. {
  73. ByteIOContext *pb = &s->pb;
  74. FLVContext *flv = s->priv_data;
  75. int i;
  76. flv->hasAudio = 0;
  77. flv->hasVideo = 0;
  78. put_tag(pb,"FLV");
  79. put_byte(pb,1);
  80. put_byte(pb,0); // delayed write
  81. put_be32(pb,9);
  82. put_be32(pb,0);
  83. for(i=0; i<s->nb_streams; i++){
  84. AVCodecContext *enc = s->streams[i]->codec;
  85. av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
  86. if(enc->codec_tag == 5){
  87. put_byte(pb,8); // message type
  88. put_be24(pb,0); // include flags
  89. put_be24(pb,0); // time stamp
  90. put_be32(pb,0); // reserved
  91. put_be32(pb,11); // size
  92. flv->reserved=5;
  93. }
  94. if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static int flv_write_trailer(AVFormatContext *s)
  100. {
  101. int64_t file_size;
  102. int flags = 0;
  103. ByteIOContext *pb = &s->pb;
  104. FLVContext *flv = s->priv_data;
  105. file_size = url_ftell(pb);
  106. flags |= flv->hasAudio ? 4 : 0;
  107. flags |= flv->hasVideo ? 1 : 0;
  108. url_fseek(pb, 4, SEEK_SET);
  109. put_byte(pb,flags);
  110. url_fseek(pb, file_size, SEEK_SET);
  111. return 0;
  112. }
  113. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  114. {
  115. ByteIOContext *pb = &s->pb;
  116. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  117. FLVContext *flv = s->priv_data;
  118. int size= pkt->size;
  119. int flags;
  120. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
  121. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  122. put_byte(pb, 9);
  123. flags = 2; // choose h263
  124. flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
  125. flv->hasVideo = 1;
  126. } else {
  127. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  128. flags = get_audio_flags(enc);
  129. assert(size);
  130. put_byte(pb, 8);
  131. // We got audio! Make sure we set this to the global flags on closure
  132. flv->hasAudio = 1;
  133. }
  134. put_be24(pb,size+1); // include flags
  135. put_be24(pb,pkt->pts);
  136. put_be32(pb,flv->reserved);
  137. put_byte(pb,flags);
  138. put_buffer(pb, pkt->data, size);
  139. put_be32(pb,size+1+11); // previous tag size
  140. put_flush_packet(pb);
  141. return 0;
  142. }
  143. static AVOutputFormat flv_oformat = {
  144. "flv",
  145. "flv format",
  146. "video/x-flv",
  147. "flv",
  148. sizeof(FLVContext),
  149. #ifdef CONFIG_MP3LAME
  150. CODEC_ID_MP3,
  151. #else // CONFIG_MP3LAME
  152. CODEC_ID_NONE,
  153. #endif // CONFIG_MP3LAME
  154. CODEC_ID_FLV1,
  155. flv_write_header,
  156. flv_write_packet,
  157. flv_write_trailer,
  158. };
  159. int flvenc_init(void)
  160. {
  161. av_register_output_format(&flv_oformat);
  162. return 0;
  163. }