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.

346 lines
10KB

  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. #include "riff.h"
  24. #undef NDEBUG
  25. #include <assert.h>
  26. static const AVCodecTag flv_video_codec_ids[] = {
  27. {CODEC_ID_FLV1, FLV_CODECID_H263 },
  28. {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
  29. {CODEC_ID_VP6F, FLV_CODECID_VP6 },
  30. {CODEC_ID_VP6, FLV_CODECID_VP6 },
  31. {CODEC_ID_NONE, 0}
  32. };
  33. static const AVCodecTag flv_audio_codec_ids[] = {
  34. {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
  35. {CODEC_ID_PCM_S8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
  36. {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
  37. {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
  38. {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
  39. {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
  40. {CODEC_ID_NONE, 0}
  41. };
  42. typedef struct FLVContext {
  43. int hasAudio;
  44. int hasVideo;
  45. int reserved;
  46. offset_t duration_offset;
  47. offset_t filesize_offset;
  48. int64_t duration;
  49. } FLVContext;
  50. static int get_audio_flags(AVCodecContext *enc){
  51. int flags = (enc->bits_per_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
  52. switch (enc->sample_rate) {
  53. case 44100:
  54. flags |= FLV_SAMPLERATE_44100HZ;
  55. break;
  56. case 22050:
  57. flags |= FLV_SAMPLERATE_22050HZ;
  58. break;
  59. case 11025:
  60. flags |= FLV_SAMPLERATE_11025HZ;
  61. break;
  62. case 8000: //nellymoser only
  63. case 5512: //not mp3
  64. if(enc->codec_id != CODEC_ID_MP3){
  65. flags |= FLV_SAMPLERATE_SPECIAL;
  66. break;
  67. }
  68. default:
  69. av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
  70. return -1;
  71. }
  72. if (enc->channels > 1) {
  73. flags |= FLV_STEREO;
  74. }
  75. switch(enc->codec_id){
  76. case CODEC_ID_MP3:
  77. flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
  78. break;
  79. case CODEC_ID_PCM_S8:
  80. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
  81. break;
  82. case CODEC_ID_PCM_S16BE:
  83. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
  84. break;
  85. case CODEC_ID_PCM_S16LE:
  86. flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
  87. break;
  88. case CODEC_ID_ADPCM_SWF:
  89. flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
  90. break;
  91. case CODEC_ID_NELLYMOSER:
  92. flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
  93. break;
  94. case 0:
  95. flags |= enc->codec_tag<<4;
  96. break;
  97. default:
  98. av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
  99. return -1;
  100. }
  101. return flags;
  102. }
  103. static void put_amf_string(ByteIOContext *pb, const char *str)
  104. {
  105. size_t len = strlen(str);
  106. put_be16(pb, len);
  107. put_buffer(pb, str, len);
  108. }
  109. static void put_amf_double(ByteIOContext *pb, double d)
  110. {
  111. put_byte(pb, AMF_DATA_TYPE_NUMBER);
  112. put_be64(pb, av_dbl2int(d));
  113. }
  114. static void put_amf_bool(ByteIOContext *pb, int b) {
  115. put_byte(pb, AMF_DATA_TYPE_BOOL);
  116. put_byte(pb, !!b);
  117. }
  118. static int flv_write_header(AVFormatContext *s)
  119. {
  120. ByteIOContext *pb = s->pb;
  121. FLVContext *flv = s->priv_data;
  122. int i, width, height, samplerate, samplesize, channels, audiocodecid, videocodecid;
  123. double framerate = 0.0;
  124. int metadata_size_pos, data_size;
  125. flv->hasAudio = 0;
  126. flv->hasVideo = 0;
  127. for(i=0; i<s->nb_streams; i++){
  128. AVCodecContext *enc = s->streams[i]->codec;
  129. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  130. width = enc->width;
  131. height = enc->height;
  132. if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
  133. framerate = av_q2d(s->streams[i]->r_frame_rate);
  134. } else {
  135. framerate = 1/av_q2d(s->streams[i]->codec->time_base);
  136. }
  137. flv->hasVideo=1;
  138. videocodecid = enc->codec_tag;
  139. if(videocodecid == 0) {
  140. av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
  141. return -1;
  142. }
  143. } else {
  144. flv->hasAudio=1;
  145. samplerate = enc->sample_rate;
  146. channels = enc->channels;
  147. audiocodecid = enc->codec_tag;
  148. samplesize = (enc->codec_id == CODEC_ID_PCM_S8) ? 8 : 16;
  149. if(get_audio_flags(enc)<0)
  150. return -1;
  151. }
  152. av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
  153. }
  154. put_tag(pb,"FLV");
  155. put_byte(pb,1);
  156. put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * flv->hasAudio
  157. + FLV_HEADER_FLAG_HASVIDEO * flv->hasVideo);
  158. put_be32(pb,9);
  159. put_be32(pb,0);
  160. for(i=0; i<s->nb_streams; i++){
  161. if(s->streams[i]->codec->codec_tag == 5){
  162. put_byte(pb,8); // message type
  163. put_be24(pb,0); // include flags
  164. put_be24(pb,0); // time stamp
  165. put_be32(pb,0); // reserved
  166. put_be32(pb,11); // size
  167. flv->reserved=5;
  168. }
  169. }
  170. /* write meta_tag */
  171. put_byte(pb, 18); // tag type META
  172. metadata_size_pos= url_ftell(pb);
  173. put_be24(pb, 0); // size of data part (sum of all parts below)
  174. put_be24(pb, 0); // time stamp
  175. put_be32(pb, 0); // reserved
  176. /* now data of data_size size */
  177. /* first event name as a string */
  178. put_byte(pb, AMF_DATA_TYPE_STRING);
  179. put_amf_string(pb, "onMetaData"); // 12 bytes
  180. /* mixed array (hash) with size and string/type/data tuples */
  181. put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
  182. put_be32(pb, 5*flv->hasVideo + 4*flv->hasAudio + 2); // +2 for duration and file size
  183. put_amf_string(pb, "duration");
  184. flv->duration_offset= url_ftell(pb);
  185. put_amf_double(pb, 0); // delayed write
  186. if(flv->hasVideo){
  187. put_amf_string(pb, "width");
  188. put_amf_double(pb, width);
  189. put_amf_string(pb, "height");
  190. put_amf_double(pb, height);
  191. put_amf_string(pb, "videodatarate");
  192. put_amf_double(pb, s->bit_rate / 1024.0);
  193. put_amf_string(pb, "framerate");
  194. put_amf_double(pb, framerate);
  195. put_amf_string(pb, "videocodecid");
  196. put_amf_double(pb, videocodecid);
  197. }
  198. if(flv->hasAudio){
  199. put_amf_string(pb, "audiosamplerate");
  200. put_amf_double(pb, samplerate);
  201. put_amf_string(pb, "audiosamplesize");
  202. put_amf_double(pb, samplesize);
  203. put_amf_string(pb, "stereo");
  204. put_amf_bool(pb, (channels == 2));
  205. put_amf_string(pb, "audiocodecid");
  206. put_amf_double(pb, audiocodecid);
  207. }
  208. put_amf_string(pb, "filesize");
  209. flv->filesize_offset= url_ftell(pb);
  210. put_amf_double(pb, 0); // delayed write
  211. put_amf_string(pb, "");
  212. put_byte(pb, AMF_END_OF_OBJECT);
  213. /* write total size of tag */
  214. data_size= url_ftell(pb) - metadata_size_pos - 10;
  215. url_fseek(pb, metadata_size_pos, SEEK_SET);
  216. put_be24(pb, data_size);
  217. url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
  218. put_be32(pb, data_size + 11);
  219. return 0;
  220. }
  221. static int flv_write_trailer(AVFormatContext *s)
  222. {
  223. int64_t file_size;
  224. ByteIOContext *pb = s->pb;
  225. FLVContext *flv = s->priv_data;
  226. file_size = url_ftell(pb);
  227. /* update informations */
  228. url_fseek(pb, flv->duration_offset, SEEK_SET);
  229. put_amf_double(pb, flv->duration / (double)1000);
  230. url_fseek(pb, flv->filesize_offset, SEEK_SET);
  231. put_amf_double(pb, file_size);
  232. url_fseek(pb, file_size, SEEK_SET);
  233. return 0;
  234. }
  235. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  236. {
  237. ByteIOContext *pb = s->pb;
  238. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  239. FLVContext *flv = s->priv_data;
  240. int size= pkt->size;
  241. int flags, flags_size;
  242. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
  243. if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F)
  244. flags_size= 2;
  245. else
  246. flags_size= 1;
  247. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  248. put_byte(pb, FLV_TAG_TYPE_VIDEO);
  249. flags = enc->codec_tag;
  250. if(flags == 0) {
  251. av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
  252. return -1;
  253. }
  254. flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  255. } else {
  256. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  257. flags = get_audio_flags(enc);
  258. assert(size);
  259. put_byte(pb, FLV_TAG_TYPE_AUDIO);
  260. }
  261. put_be24(pb,size + flags_size);
  262. put_be24(pb,pkt->pts);
  263. put_byte(pb,pkt->pts >> 24);
  264. put_be24(pb,flv->reserved);
  265. put_byte(pb,flags);
  266. if (enc->codec_id == CODEC_ID_VP6)
  267. put_byte(pb,0);
  268. if (enc->codec_id == CODEC_ID_VP6F)
  269. put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
  270. put_buffer(pb, pkt->data, size);
  271. put_be32(pb,size+flags_size+11); // previous tag size
  272. flv->duration = pkt->pts + pkt->duration;
  273. put_flush_packet(pb);
  274. return 0;
  275. }
  276. AVOutputFormat flv_muxer = {
  277. "flv",
  278. "flv format",
  279. "video/x-flv",
  280. "flv",
  281. sizeof(FLVContext),
  282. #ifdef CONFIG_LIBMP3LAME
  283. CODEC_ID_MP3,
  284. #else // CONFIG_LIBMP3LAME
  285. CODEC_ID_ADPCM_SWF,
  286. #endif // CONFIG_LIBMP3LAME
  287. CODEC_ID_FLV1,
  288. flv_write_header,
  289. flv_write_packet,
  290. flv_write_trailer,
  291. .codec_tag= (const AVCodecTag*[]){flv_video_codec_ids, flv_audio_codec_ids, 0},
  292. };