flvenc.c 15 KB

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