flvenc.c 14 KB

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