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.

420 lines
13KB

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