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.

476 lines
16KB

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