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.

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