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.

277 lines
7.7KB

  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. for(i=0; i<s->nb_streams; i++){
  102. AVCodecContext *enc = s->streams[i]->codec;
  103. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  104. width = enc->width;
  105. height = enc->height;
  106. if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
  107. framerate = av_q2d(s->streams[i]->r_frame_rate);
  108. } else {
  109. framerate = 1/av_q2d(s->streams[i]->codec->time_base);
  110. }
  111. flv->hasVideo=1;
  112. } else {
  113. flv->hasAudio=1;
  114. samplerate = enc->sample_rate;
  115. if(get_audio_flags(enc)<0)
  116. return -1;
  117. }
  118. av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
  119. }
  120. put_tag(pb,"FLV");
  121. put_byte(pb,1);
  122. put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * flv->hasAudio
  123. + FLV_HEADER_FLAG_HASVIDEO * flv->hasVideo);
  124. put_be32(pb,9);
  125. put_be32(pb,0);
  126. for(i=0; i<s->nb_streams; i++){
  127. if(s->streams[i]->codec->codec_tag == 5){
  128. put_byte(pb,8); // message type
  129. put_be24(pb,0); // include flags
  130. put_be24(pb,0); // time stamp
  131. put_be32(pb,0); // reserved
  132. put_be32(pb,11); // size
  133. flv->reserved=5;
  134. }
  135. }
  136. /* write meta_tag */
  137. put_byte(pb, 18); // tag type META
  138. metadata_size_pos= url_ftell(pb);
  139. put_be24(pb, 0); // size of data part (sum of all parts below)
  140. put_be24(pb, 0); // time stamp
  141. put_be32(pb, 0); // reserved
  142. /* now data of data_size size */
  143. /* first event name as a string */
  144. put_byte(pb, AMF_DATA_TYPE_STRING);
  145. put_amf_string(pb, "onMetaData"); // 12 bytes
  146. /* mixed array (hash) with size and string/type/data tuples */
  147. put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
  148. put_be32(pb, 4*flv->hasVideo + flv->hasAudio + 2); // +2 for duration and file size
  149. put_amf_string(pb, "duration");
  150. flv->duration_offset= url_ftell(pb);
  151. put_amf_double(pb, 0); // delayed write
  152. if(flv->hasVideo){
  153. put_amf_string(pb, "width");
  154. put_amf_double(pb, width);
  155. put_amf_string(pb, "height");
  156. put_amf_double(pb, height);
  157. put_amf_string(pb, "videodatarate");
  158. put_amf_double(pb, s->bit_rate / 1024.0);
  159. put_amf_string(pb, "framerate");
  160. put_amf_double(pb, framerate);
  161. }
  162. if(flv->hasAudio){
  163. put_amf_string(pb, "audiosamplerate");
  164. put_amf_double(pb, samplerate);
  165. }
  166. put_amf_string(pb, "filesize");
  167. flv->filesize_offset= url_ftell(pb);
  168. put_amf_double(pb, 0); // delayed write
  169. put_amf_string(pb, "");
  170. put_byte(pb, AMF_END_OF_OBJECT);
  171. /* write total size of tag */
  172. data_size= url_ftell(pb) - metadata_size_pos - 10;
  173. url_fseek(pb, metadata_size_pos, SEEK_SET);
  174. put_be24(pb, data_size);
  175. url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
  176. put_be32(pb, data_size + 11);
  177. return 0;
  178. }
  179. static int flv_write_trailer(AVFormatContext *s)
  180. {
  181. int64_t file_size;
  182. ByteIOContext *pb = &s->pb;
  183. FLVContext *flv = s->priv_data;
  184. file_size = url_ftell(pb);
  185. /* update informations */
  186. url_fseek(pb, flv->duration_offset, SEEK_SET);
  187. put_amf_double(pb, flv->duration / (double)1000);
  188. url_fseek(pb, flv->filesize_offset, SEEK_SET);
  189. put_amf_double(pb, file_size);
  190. url_fseek(pb, file_size, SEEK_SET);
  191. return 0;
  192. }
  193. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  194. {
  195. ByteIOContext *pb = &s->pb;
  196. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  197. FLVContext *flv = s->priv_data;
  198. int size= pkt->size;
  199. int flags;
  200. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
  201. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  202. put_byte(pb, FLV_TAG_TYPE_VIDEO);
  203. flags = FLV_CODECID_H263;
  204. flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  205. } else {
  206. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  207. flags = get_audio_flags(enc);
  208. assert(size);
  209. put_byte(pb, FLV_TAG_TYPE_AUDIO);
  210. }
  211. put_be24(pb,size+1); // include flags
  212. put_be24(pb,pkt->pts);
  213. put_be32(pb,flv->reserved);
  214. put_byte(pb,flags);
  215. put_buffer(pb, pkt->data, size);
  216. put_be32(pb,size+1+11); // previous tag size
  217. flv->duration = pkt->pts + pkt->duration;
  218. put_flush_packet(pb);
  219. return 0;
  220. }
  221. AVOutputFormat flv_muxer = {
  222. "flv",
  223. "flv format",
  224. "video/x-flv",
  225. "flv",
  226. sizeof(FLVContext),
  227. #ifdef CONFIG_MP3LAME
  228. CODEC_ID_MP3,
  229. #else // CONFIG_MP3LAME
  230. CODEC_ID_NONE,
  231. #endif // CONFIG_MP3LAME
  232. CODEC_ID_FLV1,
  233. flv_write_header,
  234. flv_write_packet,
  235. flv_write_trailer,
  236. };