flvenc.c 15 KB

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