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.

279 lines
7.8KB

  1. /*
  2. * FLV muxer
  3. * Copyright (c) 2003 The FFmpeg Project.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "flv.h"
  23. #undef NDEBUG
  24. #include <assert.h>
  25. typedef struct FLVContext {
  26. int hasAudio;
  27. int hasVideo;
  28. int reserved;
  29. offset_t duration_offset;
  30. offset_t filesize_offset;
  31. int64_t duration;
  32. } FLVContext;
  33. static int get_audio_flags(AVCodecContext *enc){
  34. int flags = (enc->bits_per_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
  35. switch (enc->sample_rate) {
  36. case 44100:
  37. flags |= FLV_SAMPLERATE_44100HZ;
  38. break;
  39. case 22050:
  40. flags |= FLV_SAMPLERATE_22050HZ;
  41. break;
  42. case 11025:
  43. flags |= FLV_SAMPLERATE_11025HZ;
  44. break;
  45. case 8000: //nellymoser only
  46. case 5512: //not mp3
  47. flags |= FLV_SAMPLERATE_SPECIAL;
  48. break;
  49. default:
  50. av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
  51. return -1;
  52. }
  53. if (enc->channels > 1) {
  54. flags |= FLV_STEREO;
  55. }
  56. switch(enc->codec_id){
  57. case CODEC_ID_MP3:
  58. flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
  59. break;
  60. case CODEC_ID_PCM_S8:
  61. flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_8BIT;
  62. break;
  63. case CODEC_ID_PCM_S16BE:
  64. flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_16BIT;
  65. break;
  66. case CODEC_ID_PCM_S16LE:
  67. flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
  68. break;
  69. case CODEC_ID_ADPCM_SWF:
  70. flags |= FLV_CODECID_ADPCM;
  71. break;
  72. case 0:
  73. flags |= enc->codec_tag<<4;
  74. break;
  75. default:
  76. av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
  77. return -1;
  78. }
  79. return flags;
  80. }
  81. static void put_amf_string(ByteIOContext *pb, const char *str)
  82. {
  83. size_t len = strlen(str);
  84. put_be16(pb, len);
  85. put_buffer(pb, str, len);
  86. }
  87. static void put_amf_double(ByteIOContext *pb, double d)
  88. {
  89. put_byte(pb, AMF_DATA_TYPE_NUMBER);
  90. put_be64(pb, av_dbl2int(d));
  91. }
  92. static int flv_write_header(AVFormatContext *s)
  93. {
  94. ByteIOContext *pb = &s->pb;
  95. FLVContext *flv = s->priv_data;
  96. int i, width, height, samplerate;
  97. double framerate = 0.0;
  98. int metadata_size_pos, data_size;
  99. flv->hasAudio = 0;
  100. flv->hasVideo = 0;
  101. put_tag(pb,"FLV");
  102. put_byte(pb,1);
  103. put_byte(pb,0); // delayed write
  104. put_be32(pb,9);
  105. put_be32(pb,0);
  106. for(i=0; i<s->nb_streams; i++){
  107. AVCodecContext *enc = s->streams[i]->codec;
  108. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  109. width = enc->width;
  110. height = enc->height;
  111. if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
  112. framerate = av_q2d(s->streams[i]->r_frame_rate);
  113. } else {
  114. framerate = 1/av_q2d(s->streams[i]->codec->time_base);
  115. }
  116. flv->hasVideo=1;
  117. } else {
  118. flv->hasAudio=1;
  119. samplerate = enc->sample_rate;
  120. }
  121. av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
  122. if(enc->codec_tag == 5){
  123. put_byte(pb,8); // message type
  124. put_be24(pb,0); // include flags
  125. put_be24(pb,0); // time stamp
  126. put_be32(pb,0); // reserved
  127. put_be32(pb,11); // size
  128. flv->reserved=5;
  129. }
  130. if(enc->codec_type == CODEC_TYPE_AUDIO && get_audio_flags(enc)<0)
  131. return -1;
  132. }
  133. /* write meta_tag */
  134. put_byte(pb, 18); // tag type META
  135. metadata_size_pos= url_ftell(pb);
  136. put_be24(pb, 0); // size of data part (sum of all parts below)
  137. put_be24(pb, 0); // time stamp
  138. put_be32(pb, 0); // reserved
  139. /* now data of data_size size */
  140. /* first event name as a string */
  141. put_byte(pb, AMF_DATA_TYPE_STRING);
  142. put_amf_string(pb, "onMetaData"); // 12 bytes
  143. /* mixed array (hash) with size and string/type/data tuples */
  144. put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
  145. put_be32(pb, 4*flv->hasVideo + flv->hasAudio + 2); // +2 for duration and file size
  146. put_amf_string(pb, "duration");
  147. flv->duration_offset= url_ftell(pb);
  148. put_amf_double(pb, 0); // delayed write
  149. if(flv->hasVideo){
  150. put_amf_string(pb, "width");
  151. put_amf_double(pb, width);
  152. put_amf_string(pb, "height");
  153. put_amf_double(pb, height);
  154. put_amf_string(pb, "videodatarate");
  155. put_amf_double(pb, s->bit_rate / 1024.0);
  156. put_amf_string(pb, "framerate");
  157. put_amf_double(pb, framerate);
  158. }
  159. if(flv->hasAudio){
  160. put_amf_string(pb, "audiosamplerate");
  161. put_amf_double(pb, samplerate);
  162. }
  163. put_amf_string(pb, "filesize");
  164. flv->filesize_offset= url_ftell(pb);
  165. put_amf_double(pb, 0); // delayed write
  166. put_amf_string(pb, "");
  167. put_byte(pb, AMF_END_OF_OBJECT);
  168. /* write total size of tag */
  169. data_size= url_ftell(pb) - metadata_size_pos - 10;
  170. url_fseek(pb, metadata_size_pos, SEEK_SET);
  171. put_be24(pb, data_size);
  172. url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
  173. put_be32(pb, data_size + 11);
  174. return 0;
  175. }
  176. static int flv_write_trailer(AVFormatContext *s)
  177. {
  178. int64_t file_size;
  179. int flags = 0;
  180. ByteIOContext *pb = &s->pb;
  181. FLVContext *flv = s->priv_data;
  182. file_size = url_ftell(pb);
  183. flags |= flv->hasAudio ? FLV_HEADER_FLAG_HASAUDIO : 0;
  184. flags |= flv->hasVideo ? FLV_HEADER_FLAG_HASVIDEO : 0;
  185. url_fseek(pb, 4, SEEK_SET);
  186. put_byte(pb,flags);
  187. /* update informations */
  188. url_fseek(pb, flv->duration_offset, SEEK_SET);
  189. put_amf_double(pb, flv->duration / (double)1000);
  190. url_fseek(pb, flv->filesize_offset, SEEK_SET);
  191. put_amf_double(pb, file_size);
  192. url_fseek(pb, file_size, SEEK_SET);
  193. return 0;
  194. }
  195. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  196. {
  197. ByteIOContext *pb = &s->pb;
  198. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  199. FLVContext *flv = s->priv_data;
  200. int size= pkt->size;
  201. int flags;
  202. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
  203. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  204. put_byte(pb, FLV_TAG_TYPE_VIDEO);
  205. flags = FLV_CODECID_H263;
  206. flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  207. } else {
  208. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  209. flags = get_audio_flags(enc);
  210. assert(size);
  211. put_byte(pb, FLV_TAG_TYPE_AUDIO);
  212. }
  213. put_be24(pb,size+1); // include flags
  214. put_be24(pb,pkt->pts);
  215. put_be32(pb,flv->reserved);
  216. put_byte(pb,flags);
  217. put_buffer(pb, pkt->data, size);
  218. put_be32(pb,size+1+11); // previous tag size
  219. flv->duration = pkt->pts + pkt->duration;
  220. put_flush_packet(pb);
  221. return 0;
  222. }
  223. AVOutputFormat flv_muxer = {
  224. "flv",
  225. "flv format",
  226. "video/x-flv",
  227. "flv",
  228. sizeof(FLVContext),
  229. #ifdef CONFIG_MP3LAME
  230. CODEC_ID_MP3,
  231. #else // CONFIG_MP3LAME
  232. CODEC_ID_NONE,
  233. #endif // CONFIG_MP3LAME
  234. CODEC_ID_FLV1,
  235. flv_write_header,
  236. flv_write_packet,
  237. flv_write_trailer,
  238. };