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.

470 lines
15KB

  1. /*
  2. * FLV muxer
  3. * Copyright (c) 2003 The Libav Project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intfloat_readwrite.h"
  22. #include "avformat.h"
  23. #include "flv.h"
  24. #include "internal.h"
  25. #include "avc.h"
  26. #include "metadata.h"
  27. #include "libavutil/dict.h"
  28. #undef NDEBUG
  29. #include <assert.h>
  30. static const AVCodecTag flv_video_codec_ids[] = {
  31. {CODEC_ID_FLV1, FLV_CODECID_H263 },
  32. {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
  33. {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
  34. {CODEC_ID_VP6F, FLV_CODECID_VP6 },
  35. {CODEC_ID_VP6, FLV_CODECID_VP6 },
  36. {CODEC_ID_H264, FLV_CODECID_H264 },
  37. {CODEC_ID_NONE, 0}
  38. };
  39. static const AVCodecTag flv_audio_codec_ids[] = {
  40. {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
  41. {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
  42. {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
  43. {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
  44. {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
  45. {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
  46. {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
  47. {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
  48. {CODEC_ID_NONE, 0}
  49. };
  50. typedef struct FLVContext {
  51. int reserved;
  52. int64_t duration_offset;
  53. int64_t filesize_offset;
  54. int64_t duration;
  55. int delay; ///< first dts delay for AVC
  56. int64_t last_ts;
  57. } FLVContext;
  58. static int get_audio_flags(AVCodecContext *enc){
  59. int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
  60. if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
  61. return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
  62. else if (enc->codec_id == CODEC_ID_SPEEX) {
  63. if (enc->sample_rate != 16000) {
  64. av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
  65. return -1;
  66. }
  67. if (enc->channels != 1) {
  68. av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
  69. return -1;
  70. }
  71. return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
  72. } else {
  73. switch (enc->sample_rate) {
  74. case 44100:
  75. flags |= FLV_SAMPLERATE_44100HZ;
  76. break;
  77. case 22050:
  78. flags |= FLV_SAMPLERATE_22050HZ;
  79. break;
  80. case 11025:
  81. flags |= FLV_SAMPLERATE_11025HZ;
  82. break;
  83. case 8000: //nellymoser only
  84. case 5512: //not mp3
  85. if(enc->codec_id != CODEC_ID_MP3){
  86. flags |= FLV_SAMPLERATE_SPECIAL;
  87. break;
  88. }
  89. default:
  90. av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
  91. return -1;
  92. }
  93. }
  94. if (enc->channels > 1) {
  95. flags |= FLV_STEREO;
  96. }
  97. switch(enc->codec_id){
  98. case CODEC_ID_MP3:
  99. flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
  100. break;
  101. case CODEC_ID_PCM_U8:
  102. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
  103. break;
  104. case CODEC_ID_PCM_S16BE:
  105. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
  106. break;
  107. case CODEC_ID_PCM_S16LE:
  108. flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
  109. break;
  110. case CODEC_ID_ADPCM_SWF:
  111. flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
  112. break;
  113. case CODEC_ID_NELLYMOSER:
  114. if (enc->sample_rate == 8000) {
  115. flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
  116. } else {
  117. flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
  118. }
  119. break;
  120. case 0:
  121. flags |= enc->codec_tag<<4;
  122. break;
  123. default:
  124. av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
  125. return -1;
  126. }
  127. return flags;
  128. }
  129. static void put_amf_string(AVIOContext *pb, const char *str)
  130. {
  131. size_t len = strlen(str);
  132. avio_wb16(pb, len);
  133. avio_write(pb, str, len);
  134. }
  135. static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
  136. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  137. avio_wb24(pb, 5); /* Tag Data Size */
  138. avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms*/
  139. avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
  140. avio_wb24(pb, 0); /* StreamId = 0 */
  141. avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
  142. avio_w8(pb, 2); /* AVC end of sequence */
  143. avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
  144. avio_wb32(pb, 16); /* Size of FLV tag */
  145. }
  146. static void put_amf_double(AVIOContext *pb, double d)
  147. {
  148. avio_w8(pb, AMF_DATA_TYPE_NUMBER);
  149. avio_wb64(pb, av_dbl2int(d));
  150. }
  151. static void put_amf_bool(AVIOContext *pb, int b) {
  152. avio_w8(pb, AMF_DATA_TYPE_BOOL);
  153. avio_w8(pb, !!b);
  154. }
  155. static int flv_write_header(AVFormatContext *s)
  156. {
  157. AVIOContext *pb = s->pb;
  158. FLVContext *flv = s->priv_data;
  159. AVCodecContext *audio_enc = NULL, *video_enc = NULL;
  160. int i, metadata_count = 0;
  161. double framerate = 0.0;
  162. int64_t metadata_size_pos, data_size, metadata_count_pos;
  163. AVDictionaryEntry *tag = NULL;
  164. for(i=0; i<s->nb_streams; i++){
  165. AVCodecContext *enc = s->streams[i]->codec;
  166. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  167. if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
  168. framerate = av_q2d(s->streams[i]->r_frame_rate);
  169. } else {
  170. framerate = 1/av_q2d(s->streams[i]->codec->time_base);
  171. }
  172. video_enc = enc;
  173. if(enc->codec_tag == 0) {
  174. av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
  175. return -1;
  176. }
  177. } else {
  178. audio_enc = enc;
  179. if(get_audio_flags(enc)<0)
  180. return -1;
  181. }
  182. av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
  183. }
  184. avio_write(pb, "FLV", 3);
  185. avio_w8(pb,1);
  186. avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
  187. + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
  188. avio_wb32(pb,9);
  189. avio_wb32(pb,0);
  190. for(i=0; i<s->nb_streams; i++){
  191. if(s->streams[i]->codec->codec_tag == 5){
  192. avio_w8(pb,8); // message type
  193. avio_wb24(pb,0); // include flags
  194. avio_wb24(pb,0); // time stamp
  195. avio_wb32(pb,0); // reserved
  196. avio_wb32(pb,11); // size
  197. flv->reserved=5;
  198. }
  199. }
  200. flv->last_ts = -1;
  201. /* write meta_tag */
  202. avio_w8(pb, 18); // tag type META
  203. metadata_size_pos= avio_tell(pb);
  204. avio_wb24(pb, 0); // size of data part (sum of all parts below)
  205. avio_wb24(pb, 0); // time stamp
  206. avio_wb32(pb, 0); // reserved
  207. /* now data of data_size size */
  208. /* first event name as a string */
  209. avio_w8(pb, AMF_DATA_TYPE_STRING);
  210. put_amf_string(pb, "onMetaData"); // 12 bytes
  211. /* mixed array (hash) with size and string/type/data tuples */
  212. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  213. metadata_count_pos = avio_tell(pb);
  214. metadata_count = 5*!!video_enc + 5*!!audio_enc + 2; // +2 for duration and file size
  215. avio_wb32(pb, metadata_count);
  216. put_amf_string(pb, "duration");
  217. flv->duration_offset= avio_tell(pb);
  218. put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
  219. if(video_enc){
  220. put_amf_string(pb, "width");
  221. put_amf_double(pb, video_enc->width);
  222. put_amf_string(pb, "height");
  223. put_amf_double(pb, video_enc->height);
  224. put_amf_string(pb, "videodatarate");
  225. put_amf_double(pb, video_enc->bit_rate / 1024.0);
  226. put_amf_string(pb, "framerate");
  227. put_amf_double(pb, framerate);
  228. put_amf_string(pb, "videocodecid");
  229. put_amf_double(pb, video_enc->codec_tag);
  230. }
  231. if(audio_enc){
  232. put_amf_string(pb, "audiodatarate");
  233. put_amf_double(pb, audio_enc->bit_rate / 1024.0);
  234. put_amf_string(pb, "audiosamplerate");
  235. put_amf_double(pb, audio_enc->sample_rate);
  236. put_amf_string(pb, "audiosamplesize");
  237. put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
  238. put_amf_string(pb, "stereo");
  239. put_amf_bool(pb, audio_enc->channels == 2);
  240. put_amf_string(pb, "audiocodecid");
  241. put_amf_double(pb, audio_enc->codec_tag);
  242. }
  243. while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  244. put_amf_string(pb, tag->key);
  245. avio_w8(pb, AMF_DATA_TYPE_STRING);
  246. put_amf_string(pb, tag->value);
  247. metadata_count++;
  248. }
  249. put_amf_string(pb, "filesize");
  250. flv->filesize_offset= avio_tell(pb);
  251. put_amf_double(pb, 0); // delayed write
  252. put_amf_string(pb, "");
  253. avio_w8(pb, AMF_END_OF_OBJECT);
  254. /* write total size of tag */
  255. data_size= avio_tell(pb) - metadata_size_pos - 10;
  256. avio_seek(pb, metadata_count_pos, SEEK_SET);
  257. avio_wb32(pb, metadata_count);
  258. avio_seek(pb, metadata_size_pos, SEEK_SET);
  259. avio_wb24(pb, data_size);
  260. avio_skip(pb, data_size + 10 - 3);
  261. avio_wb32(pb, data_size + 11);
  262. for (i = 0; i < s->nb_streams; i++) {
  263. AVCodecContext *enc = s->streams[i]->codec;
  264. if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
  265. int64_t pos;
  266. avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  267. FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
  268. avio_wb24(pb, 0); // size patched later
  269. avio_wb24(pb, 0); // ts
  270. avio_w8(pb, 0); // ts ext
  271. avio_wb24(pb, 0); // streamid
  272. pos = avio_tell(pb);
  273. if (enc->codec_id == CODEC_ID_AAC) {
  274. avio_w8(pb, get_audio_flags(enc));
  275. avio_w8(pb, 0); // AAC sequence header
  276. avio_write(pb, enc->extradata, enc->extradata_size);
  277. } else {
  278. avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
  279. avio_w8(pb, 0); // AVC sequence header
  280. avio_wb24(pb, 0); // composition time
  281. ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
  282. }
  283. data_size = avio_tell(pb) - pos;
  284. avio_seek(pb, -data_size - 10, SEEK_CUR);
  285. avio_wb24(pb, data_size);
  286. avio_skip(pb, data_size + 10 - 3);
  287. avio_wb32(pb, data_size + 11); // previous tag size
  288. }
  289. }
  290. return 0;
  291. }
  292. static int flv_write_trailer(AVFormatContext *s)
  293. {
  294. int64_t file_size;
  295. AVIOContext *pb = s->pb;
  296. FLVContext *flv = s->priv_data;
  297. int i;
  298. /* Add EOS tag */
  299. for (i = 0; i < s->nb_streams; i++) {
  300. AVCodecContext *enc = s->streams[i]->codec;
  301. if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  302. enc->codec_id == CODEC_ID_H264) {
  303. put_avc_eos_tag(pb, flv->last_ts);
  304. }
  305. }
  306. file_size = avio_tell(pb);
  307. /* update informations */
  308. avio_seek(pb, flv->duration_offset, SEEK_SET);
  309. put_amf_double(pb, flv->duration / (double)1000);
  310. avio_seek(pb, flv->filesize_offset, SEEK_SET);
  311. put_amf_double(pb, file_size);
  312. avio_seek(pb, file_size, SEEK_SET);
  313. return 0;
  314. }
  315. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  316. {
  317. AVIOContext *pb = s->pb;
  318. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  319. FLVContext *flv = s->priv_data;
  320. unsigned ts;
  321. int size= pkt->size;
  322. uint8_t *data= NULL;
  323. int flags, flags_size;
  324. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
  325. if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
  326. enc->codec_id == CODEC_ID_AAC)
  327. flags_size= 2;
  328. else if(enc->codec_id == CODEC_ID_H264)
  329. flags_size= 5;
  330. else
  331. flags_size= 1;
  332. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  333. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  334. flags = enc->codec_tag;
  335. if(flags == 0) {
  336. av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
  337. return -1;
  338. }
  339. flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  340. } else {
  341. assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
  342. flags = get_audio_flags(enc);
  343. assert(size);
  344. avio_w8(pb, FLV_TAG_TYPE_AUDIO);
  345. }
  346. if (enc->codec_id == CODEC_ID_H264) {
  347. /* check if extradata looks like mp4 formated */
  348. if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
  349. if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
  350. return -1;
  351. }
  352. }
  353. if (!flv->delay && pkt->dts < 0)
  354. flv->delay = -pkt->dts;
  355. ts = pkt->dts + flv->delay; // add delay to force positive dts
  356. /* check Speex packet duration */
  357. if (enc->codec_id == CODEC_ID_SPEEX && ts - flv->last_ts > 160) {
  358. av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
  359. "8 frames per packet. Adobe Flash "
  360. "Player cannot handle this!\n");
  361. }
  362. if (flv->last_ts < ts)
  363. flv->last_ts = ts;
  364. avio_wb24(pb,size + flags_size);
  365. avio_wb24(pb,ts);
  366. avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
  367. avio_wb24(pb,flv->reserved);
  368. avio_w8(pb,flags);
  369. if (enc->codec_id == CODEC_ID_VP6)
  370. avio_w8(pb,0);
  371. if (enc->codec_id == CODEC_ID_VP6F)
  372. avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
  373. else if (enc->codec_id == CODEC_ID_AAC)
  374. avio_w8(pb,1); // AAC raw
  375. else if (enc->codec_id == CODEC_ID_H264) {
  376. avio_w8(pb,1); // AVC NALU
  377. avio_wb24(pb,pkt->pts - pkt->dts);
  378. }
  379. avio_write(pb, data ? data : pkt->data, size);
  380. avio_wb32(pb,size+flags_size+11); // previous tag size
  381. flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
  382. avio_flush(pb);
  383. av_free(data);
  384. return pb->error;
  385. }
  386. AVOutputFormat ff_flv_muxer = {
  387. .name = "flv",
  388. .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
  389. .mime_type = "video/x-flv",
  390. .extensions = "flv",
  391. .priv_data_size = sizeof(FLVContext),
  392. #if CONFIG_LIBMP3LAME
  393. .audio_codec = CODEC_ID_MP3,
  394. #else // CONFIG_LIBMP3LAME
  395. .audio_codec = CODEC_ID_ADPCM_SWF,
  396. #endif // CONFIG_LIBMP3LAME
  397. .video_codec = CODEC_ID_FLV1,
  398. .write_header = flv_write_header,
  399. .write_packet = flv_write_packet,
  400. .write_trailer = flv_write_trailer,
  401. .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
  402. .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
  403. };