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.

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