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.

464 lines
15KB

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