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.

524 lines
18KB

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