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.

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