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.

546 lines
18KB

  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.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. int64_t delay; ///< first dts delay (needed for AVC & Speex)
  56. } FLVContext;
  57. typedef struct FLVStreamContext {
  58. int64_t last_ts; ///< last timestamp for each stream
  59. } FLVStreamContext;
  60. static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
  61. {
  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(s, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
  68. return -1;
  69. }
  70. if (enc->channels != 1) {
  71. av_log(s, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
  72. return -1;
  73. }
  74. return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
  75. } else {
  76. switch (enc->sample_rate) {
  77. case 44100:
  78. flags |= FLV_SAMPLERATE_44100HZ;
  79. break;
  80. case 22050:
  81. flags |= FLV_SAMPLERATE_22050HZ;
  82. break;
  83. case 11025:
  84. flags |= FLV_SAMPLERATE_11025HZ;
  85. break;
  86. case 16000: //nellymoser only
  87. case 8000: //nellymoser only
  88. case 5512: //not mp3
  89. if(enc->codec_id != CODEC_ID_MP3){
  90. flags |= FLV_SAMPLERATE_SPECIAL;
  91. break;
  92. }
  93. default:
  94. av_log(s, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
  95. return -1;
  96. }
  97. }
  98. if (enc->channels > 1) {
  99. flags |= FLV_STEREO;
  100. }
  101. switch(enc->codec_id){
  102. case CODEC_ID_MP3:
  103. flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
  104. break;
  105. case CODEC_ID_PCM_U8:
  106. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
  107. break;
  108. case CODEC_ID_PCM_S16BE:
  109. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
  110. break;
  111. case CODEC_ID_PCM_S16LE:
  112. flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
  113. break;
  114. case CODEC_ID_ADPCM_SWF:
  115. flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
  116. break;
  117. case CODEC_ID_NELLYMOSER:
  118. if (enc->sample_rate == 8000) {
  119. flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
  120. } else if (enc->sample_rate == 16000) {
  121. flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
  122. } else {
  123. flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
  124. }
  125. break;
  126. case 0:
  127. flags |= enc->codec_tag<<4;
  128. break;
  129. default:
  130. av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
  131. return -1;
  132. }
  133. return flags;
  134. }
  135. static void put_amf_string(AVIOContext *pb, const char *str)
  136. {
  137. size_t len = strlen(str);
  138. avio_wb16(pb, len);
  139. avio_write(pb, str, len);
  140. }
  141. static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
  142. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  143. avio_wb24(pb, 5); /* Tag Data Size */
  144. avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms*/
  145. avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
  146. avio_wb24(pb, 0); /* StreamId = 0 */
  147. avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
  148. avio_w8(pb, 2); /* AVC end of sequence */
  149. avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
  150. avio_wb32(pb, 16); /* Size of FLV tag */
  151. }
  152. static void put_amf_double(AVIOContext *pb, double d)
  153. {
  154. avio_w8(pb, AMF_DATA_TYPE_NUMBER);
  155. avio_wb64(pb, av_double2int(d));
  156. }
  157. static void put_amf_bool(AVIOContext *pb, int b) {
  158. avio_w8(pb, AMF_DATA_TYPE_BOOL);
  159. avio_w8(pb, !!b);
  160. }
  161. static int flv_write_header(AVFormatContext *s)
  162. {
  163. AVIOContext *pb = s->pb;
  164. FLVContext *flv = s->priv_data;
  165. AVCodecContext *audio_enc = NULL, *video_enc = NULL, *data_enc = NULL;
  166. int i, metadata_count = 0;
  167. double framerate = 0.0;
  168. int64_t metadata_size_pos, data_size, metadata_count_pos;
  169. AVDictionaryEntry *tag = NULL;
  170. for(i=0; i<s->nb_streams; i++){
  171. AVCodecContext *enc = s->streams[i]->codec;
  172. FLVStreamContext *sc;
  173. switch (enc->codec_type) {
  174. case 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(s, AV_LOG_ERROR, "video codec not compatible with flv\n");
  183. return -1;
  184. }
  185. break;
  186. case AVMEDIA_TYPE_AUDIO:
  187. audio_enc = enc;
  188. if (get_audio_flags(s, enc) < 0)
  189. return AVERROR_INVALIDDATA;
  190. break;
  191. case AVMEDIA_TYPE_DATA:
  192. if (enc->codec_id != CODEC_ID_TEXT) {
  193. av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. data_enc = enc;
  197. break;
  198. default:
  199. av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
  200. return -1;
  201. }
  202. avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
  203. sc = av_mallocz(sizeof(FLVStreamContext));
  204. if (!sc)
  205. return AVERROR(ENOMEM);
  206. s->streams[i]->priv_data = sc;
  207. sc->last_ts = -1;
  208. }
  209. flv->delay = AV_NOPTS_VALUE;
  210. avio_write(pb, "FLV", 3);
  211. avio_w8(pb,1);
  212. avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
  213. + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
  214. avio_wb32(pb,9);
  215. avio_wb32(pb,0);
  216. for(i=0; i<s->nb_streams; i++){
  217. if(s->streams[i]->codec->codec_tag == 5){
  218. avio_w8(pb,8); // message type
  219. avio_wb24(pb,0); // include flags
  220. avio_wb24(pb,0); // time stamp
  221. avio_wb32(pb,0); // reserved
  222. avio_wb32(pb,11); // size
  223. flv->reserved=5;
  224. }
  225. }
  226. /* write meta_tag */
  227. avio_w8(pb, 18); // tag type META
  228. metadata_size_pos= avio_tell(pb);
  229. avio_wb24(pb, 0); // size of data part (sum of all parts below)
  230. avio_wb24(pb, 0); // time stamp
  231. avio_wb32(pb, 0); // reserved
  232. /* now data of data_size size */
  233. /* first event name as a string */
  234. avio_w8(pb, AMF_DATA_TYPE_STRING);
  235. put_amf_string(pb, "onMetaData"); // 12 bytes
  236. /* mixed array (hash) with size and string/type/data tuples */
  237. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  238. metadata_count_pos = avio_tell(pb);
  239. metadata_count = 5 * !!video_enc +
  240. 5 * !!audio_enc +
  241. 1 * !!data_enc +
  242. 2; // +2 for duration and file size
  243. avio_wb32(pb, metadata_count);
  244. put_amf_string(pb, "duration");
  245. flv->duration_offset= avio_tell(pb);
  246. put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
  247. if(video_enc){
  248. put_amf_string(pb, "width");
  249. put_amf_double(pb, video_enc->width);
  250. put_amf_string(pb, "height");
  251. put_amf_double(pb, video_enc->height);
  252. put_amf_string(pb, "videodatarate");
  253. put_amf_double(pb, video_enc->bit_rate / 1024.0);
  254. put_amf_string(pb, "framerate");
  255. put_amf_double(pb, framerate);
  256. put_amf_string(pb, "videocodecid");
  257. put_amf_double(pb, video_enc->codec_tag);
  258. }
  259. if(audio_enc){
  260. put_amf_string(pb, "audiodatarate");
  261. put_amf_double(pb, audio_enc->bit_rate / 1024.0);
  262. put_amf_string(pb, "audiosamplerate");
  263. put_amf_double(pb, audio_enc->sample_rate);
  264. put_amf_string(pb, "audiosamplesize");
  265. put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
  266. put_amf_string(pb, "stereo");
  267. put_amf_bool(pb, audio_enc->channels == 2);
  268. put_amf_string(pb, "audiocodecid");
  269. put_amf_double(pb, audio_enc->codec_tag);
  270. }
  271. if (data_enc) {
  272. put_amf_string(pb, "datastream");
  273. put_amf_double(pb, 0.0);
  274. }
  275. while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  276. put_amf_string(pb, tag->key);
  277. avio_w8(pb, AMF_DATA_TYPE_STRING);
  278. put_amf_string(pb, tag->value);
  279. metadata_count++;
  280. }
  281. put_amf_string(pb, "filesize");
  282. flv->filesize_offset= avio_tell(pb);
  283. put_amf_double(pb, 0); // delayed write
  284. put_amf_string(pb, "");
  285. avio_w8(pb, AMF_END_OF_OBJECT);
  286. /* write total size of tag */
  287. data_size= avio_tell(pb) - metadata_size_pos - 10;
  288. avio_seek(pb, metadata_count_pos, SEEK_SET);
  289. avio_wb32(pb, metadata_count);
  290. avio_seek(pb, metadata_size_pos, SEEK_SET);
  291. avio_wb24(pb, data_size);
  292. avio_skip(pb, data_size + 10 - 3);
  293. avio_wb32(pb, data_size + 11);
  294. for (i = 0; i < s->nb_streams; i++) {
  295. AVCodecContext *enc = s->streams[i]->codec;
  296. if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
  297. int64_t pos;
  298. avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  299. FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
  300. avio_wb24(pb, 0); // size patched later
  301. avio_wb24(pb, 0); // ts
  302. avio_w8(pb, 0); // ts ext
  303. avio_wb24(pb, 0); // streamid
  304. pos = avio_tell(pb);
  305. if (enc->codec_id == CODEC_ID_AAC) {
  306. avio_w8(pb, get_audio_flags(s, enc));
  307. avio_w8(pb, 0); // AAC sequence header
  308. avio_write(pb, enc->extradata, enc->extradata_size);
  309. } else {
  310. avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
  311. avio_w8(pb, 0); // AVC sequence header
  312. avio_wb24(pb, 0); // composition time
  313. ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
  314. }
  315. data_size = avio_tell(pb) - pos;
  316. avio_seek(pb, -data_size - 10, SEEK_CUR);
  317. avio_wb24(pb, data_size);
  318. avio_skip(pb, data_size + 10 - 3);
  319. avio_wb32(pb, data_size + 11); // previous tag size
  320. }
  321. }
  322. return 0;
  323. }
  324. static int flv_write_trailer(AVFormatContext *s)
  325. {
  326. int64_t file_size;
  327. AVIOContext *pb = s->pb;
  328. FLVContext *flv = s->priv_data;
  329. int i;
  330. /* Add EOS tag */
  331. for (i = 0; i < s->nb_streams; i++) {
  332. AVCodecContext *enc = s->streams[i]->codec;
  333. FLVStreamContext *sc = s->streams[i]->priv_data;
  334. if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  335. enc->codec_id == CODEC_ID_H264) {
  336. put_avc_eos_tag(pb, sc->last_ts);
  337. }
  338. }
  339. file_size = avio_tell(pb);
  340. /* update information */
  341. avio_seek(pb, flv->duration_offset, SEEK_SET);
  342. put_amf_double(pb, flv->duration / (double)1000);
  343. avio_seek(pb, flv->filesize_offset, SEEK_SET);
  344. put_amf_double(pb, file_size);
  345. avio_seek(pb, file_size, SEEK_SET);
  346. return 0;
  347. }
  348. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  349. {
  350. AVIOContext *pb = s->pb;
  351. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  352. FLVContext *flv = s->priv_data;
  353. FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
  354. unsigned ts;
  355. int size= pkt->size;
  356. uint8_t *data= NULL;
  357. int flags, flags_size;
  358. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
  359. if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
  360. enc->codec_id == CODEC_ID_AAC)
  361. flags_size= 2;
  362. else if(enc->codec_id == CODEC_ID_H264)
  363. flags_size= 5;
  364. else
  365. flags_size= 1;
  366. switch (enc->codec_type) {
  367. case AVMEDIA_TYPE_VIDEO:
  368. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  369. flags = enc->codec_tag;
  370. if(flags == 0) {
  371. av_log(s, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
  372. return -1;
  373. }
  374. flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  375. break;
  376. case AVMEDIA_TYPE_AUDIO:
  377. flags = get_audio_flags(s, enc);
  378. assert(size);
  379. avio_w8(pb, FLV_TAG_TYPE_AUDIO);
  380. break;
  381. case AVMEDIA_TYPE_DATA:
  382. avio_w8(pb, FLV_TAG_TYPE_META);
  383. break;
  384. default:
  385. return AVERROR(EINVAL);
  386. }
  387. if (enc->codec_id == CODEC_ID_H264) {
  388. /* check if extradata looks like MP4 */
  389. if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
  390. if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
  391. return -1;
  392. }
  393. }
  394. if (flv->delay == AV_NOPTS_VALUE)
  395. flv->delay = -pkt->dts;
  396. if (pkt->dts < -flv->delay) {
  397. av_log(s, AV_LOG_WARNING, "Packets are not in the proper order with "
  398. "respect to DTS\n");
  399. return AVERROR(EINVAL);
  400. }
  401. ts = pkt->dts + flv->delay; // add delay to force positive dts
  402. /* check Speex packet duration */
  403. if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
  404. av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
  405. "8 frames per packet. Adobe Flash "
  406. "Player cannot handle this!\n");
  407. }
  408. if (sc->last_ts < ts)
  409. sc->last_ts = ts;
  410. avio_wb24(pb,size + flags_size);
  411. avio_wb24(pb,ts);
  412. avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
  413. avio_wb24(pb,flv->reserved);
  414. if (enc->codec_type == AVMEDIA_TYPE_DATA) {
  415. int data_size;
  416. int metadata_size_pos = avio_tell(pb);
  417. avio_w8(pb, AMF_DATA_TYPE_STRING);
  418. put_amf_string(pb, "onTextData");
  419. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  420. avio_wb32(pb, 2);
  421. put_amf_string(pb, "type");
  422. avio_w8(pb, AMF_DATA_TYPE_STRING);
  423. put_amf_string(pb, "Text");
  424. put_amf_string(pb, "text");
  425. avio_w8(pb, AMF_DATA_TYPE_STRING);
  426. put_amf_string(pb, pkt->data);
  427. put_amf_string(pb, "");
  428. avio_w8(pb, AMF_END_OF_OBJECT);
  429. /* write total size of tag */
  430. data_size = avio_tell(pb) - metadata_size_pos;
  431. avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
  432. avio_wb24(pb, data_size);
  433. avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
  434. avio_wb32(pb, data_size + 11);
  435. } else {
  436. avio_w8(pb,flags);
  437. if (enc->codec_id == CODEC_ID_VP6)
  438. avio_w8(pb,0);
  439. if (enc->codec_id == CODEC_ID_VP6F)
  440. avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
  441. else if (enc->codec_id == CODEC_ID_AAC)
  442. avio_w8(pb,1); // AAC raw
  443. else if (enc->codec_id == CODEC_ID_H264) {
  444. avio_w8(pb,1); // AVC NALU
  445. avio_wb24(pb,pkt->pts - pkt->dts);
  446. }
  447. avio_write(pb, data ? data : pkt->data, size);
  448. avio_wb32(pb,size+flags_size+11); // previous tag size
  449. flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
  450. }
  451. avio_flush(pb);
  452. av_free(data);
  453. return pb->error;
  454. }
  455. AVOutputFormat ff_flv_muxer = {
  456. .name = "flv",
  457. .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
  458. .mime_type = "video/x-flv",
  459. .extensions = "flv",
  460. .priv_data_size = sizeof(FLVContext),
  461. #if CONFIG_LIBMP3LAME
  462. .audio_codec = CODEC_ID_MP3,
  463. #else // CONFIG_LIBMP3LAME
  464. .audio_codec = CODEC_ID_ADPCM_SWF,
  465. #endif // CONFIG_LIBMP3LAME
  466. .video_codec = CODEC_ID_FLV1,
  467. .write_header = flv_write_header,
  468. .write_packet = flv_write_packet,
  469. .write_trailer = flv_write_trailer,
  470. .codec_tag = (const AVCodecTag* const []){
  471. flv_video_codec_ids, flv_audio_codec_ids, 0
  472. },
  473. .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
  474. AVFMT_TS_NONSTRICT,
  475. };