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.5KB

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