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.

342 lines
9.9KB

  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. #undef NDEBUG
  25. #include <assert.h>
  26. static const AVCodecTag flv_video_codec_ids[] = {
  27. {CODEC_ID_FLV1, FLV_CODECID_H263 },
  28. {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
  29. {CODEC_ID_VP6F, FLV_CODECID_VP6 },
  30. {CODEC_ID_VP6, FLV_CODECID_VP6 },
  31. {CODEC_ID_NONE, 0}
  32. };
  33. static const AVCodecTag flv_audio_codec_ids[] = {
  34. {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
  35. {CODEC_ID_PCM_S8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
  36. {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
  37. {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
  38. {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
  39. {CODEC_ID_NONE, 0}
  40. };
  41. typedef struct FLVContext {
  42. int hasAudio;
  43. int hasVideo;
  44. int reserved;
  45. offset_t duration_offset;
  46. offset_t filesize_offset;
  47. int64_t duration;
  48. } FLVContext;
  49. static int get_audio_flags(AVCodecContext *enc){
  50. int flags = (enc->bits_per_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
  51. switch (enc->sample_rate) {
  52. case 44100:
  53. flags |= FLV_SAMPLERATE_44100HZ;
  54. break;
  55. case 22050:
  56. flags |= FLV_SAMPLERATE_22050HZ;
  57. break;
  58. case 11025:
  59. flags |= FLV_SAMPLERATE_11025HZ;
  60. break;
  61. case 8000: //nellymoser only
  62. case 5512: //not mp3
  63. if(enc->codec_id != CODEC_ID_MP3){
  64. flags |= FLV_SAMPLERATE_SPECIAL;
  65. break;
  66. }
  67. default:
  68. av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
  69. return -1;
  70. }
  71. if (enc->channels > 1) {
  72. flags |= FLV_STEREO;
  73. }
  74. switch(enc->codec_id){
  75. case CODEC_ID_MP3:
  76. flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
  77. break;
  78. case CODEC_ID_PCM_S8:
  79. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
  80. break;
  81. case CODEC_ID_PCM_S16BE:
  82. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
  83. break;
  84. case CODEC_ID_PCM_S16LE:
  85. flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
  86. break;
  87. case CODEC_ID_ADPCM_SWF:
  88. flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
  89. break;
  90. case 0:
  91. flags |= enc->codec_tag<<4;
  92. break;
  93. default:
  94. av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
  95. return -1;
  96. }
  97. return flags;
  98. }
  99. static void put_amf_string(ByteIOContext *pb, const char *str)
  100. {
  101. size_t len = strlen(str);
  102. put_be16(pb, len);
  103. put_buffer(pb, str, len);
  104. }
  105. static void put_amf_double(ByteIOContext *pb, double d)
  106. {
  107. put_byte(pb, AMF_DATA_TYPE_NUMBER);
  108. put_be64(pb, av_dbl2int(d));
  109. }
  110. static void put_amf_bool(ByteIOContext *pb, int b) {
  111. put_byte(pb, AMF_DATA_TYPE_BOOL);
  112. put_byte(pb, !!b);
  113. }
  114. static int flv_write_header(AVFormatContext *s)
  115. {
  116. ByteIOContext *pb = s->pb;
  117. FLVContext *flv = s->priv_data;
  118. int i, width, height, samplerate, samplesize, channels, audiocodecid, videocodecid;
  119. double framerate = 0.0;
  120. int metadata_size_pos, data_size;
  121. flv->hasAudio = 0;
  122. flv->hasVideo = 0;
  123. for(i=0; i<s->nb_streams; i++){
  124. AVCodecContext *enc = s->streams[i]->codec;
  125. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  126. width = enc->width;
  127. height = enc->height;
  128. if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
  129. framerate = av_q2d(s->streams[i]->r_frame_rate);
  130. } else {
  131. framerate = 1/av_q2d(s->streams[i]->codec->time_base);
  132. }
  133. flv->hasVideo=1;
  134. videocodecid = enc->codec_tag;
  135. if(videocodecid == 0) {
  136. av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
  137. return -1;
  138. }
  139. } else {
  140. flv->hasAudio=1;
  141. samplerate = enc->sample_rate;
  142. channels = enc->channels;
  143. audiocodecid = enc->codec_tag;
  144. samplesize = (enc->codec_id == CODEC_ID_PCM_S8) ? 8 : 16;
  145. if(get_audio_flags(enc)<0)
  146. return -1;
  147. }
  148. av_set_pts_info(s->streams[i], 24, 1, 1000); /* 24 bit pts in ms */
  149. }
  150. put_tag(pb,"FLV");
  151. put_byte(pb,1);
  152. put_byte(pb, FLV_HEADER_FLAG_HASAUDIO * flv->hasAudio
  153. + FLV_HEADER_FLAG_HASVIDEO * flv->hasVideo);
  154. put_be32(pb,9);
  155. put_be32(pb,0);
  156. for(i=0; i<s->nb_streams; i++){
  157. if(s->streams[i]->codec->codec_tag == 5){
  158. put_byte(pb,8); // message type
  159. put_be24(pb,0); // include flags
  160. put_be24(pb,0); // time stamp
  161. put_be32(pb,0); // reserved
  162. put_be32(pb,11); // size
  163. flv->reserved=5;
  164. }
  165. }
  166. /* write meta_tag */
  167. put_byte(pb, 18); // tag type META
  168. metadata_size_pos= url_ftell(pb);
  169. put_be24(pb, 0); // size of data part (sum of all parts below)
  170. put_be24(pb, 0); // time stamp
  171. put_be32(pb, 0); // reserved
  172. /* now data of data_size size */
  173. /* first event name as a string */
  174. put_byte(pb, AMF_DATA_TYPE_STRING);
  175. put_amf_string(pb, "onMetaData"); // 12 bytes
  176. /* mixed array (hash) with size and string/type/data tuples */
  177. put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
  178. put_be32(pb, 5*flv->hasVideo + 4*flv->hasAudio + 2); // +2 for duration and file size
  179. put_amf_string(pb, "duration");
  180. flv->duration_offset= url_ftell(pb);
  181. put_amf_double(pb, 0); // delayed write
  182. if(flv->hasVideo){
  183. put_amf_string(pb, "width");
  184. put_amf_double(pb, width);
  185. put_amf_string(pb, "height");
  186. put_amf_double(pb, height);
  187. put_amf_string(pb, "videodatarate");
  188. put_amf_double(pb, s->bit_rate / 1024.0);
  189. put_amf_string(pb, "framerate");
  190. put_amf_double(pb, framerate);
  191. put_amf_string(pb, "videocodecid");
  192. put_amf_double(pb, videocodecid);
  193. }
  194. if(flv->hasAudio){
  195. put_amf_string(pb, "audiosamplerate");
  196. put_amf_double(pb, samplerate);
  197. put_amf_string(pb, "audiosamplesize");
  198. put_amf_double(pb, samplesize);
  199. put_amf_string(pb, "stereo");
  200. put_amf_bool(pb, (channels == 2));
  201. put_amf_string(pb, "audiocodecid");
  202. put_amf_double(pb, audiocodecid);
  203. }
  204. put_amf_string(pb, "filesize");
  205. flv->filesize_offset= url_ftell(pb);
  206. put_amf_double(pb, 0); // delayed write
  207. put_amf_string(pb, "");
  208. put_byte(pb, AMF_END_OF_OBJECT);
  209. /* write total size of tag */
  210. data_size= url_ftell(pb) - metadata_size_pos - 10;
  211. url_fseek(pb, metadata_size_pos, SEEK_SET);
  212. put_be24(pb, data_size);
  213. url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
  214. put_be32(pb, data_size + 11);
  215. return 0;
  216. }
  217. static int flv_write_trailer(AVFormatContext *s)
  218. {
  219. int64_t file_size;
  220. ByteIOContext *pb = s->pb;
  221. FLVContext *flv = s->priv_data;
  222. file_size = url_ftell(pb);
  223. /* update informations */
  224. url_fseek(pb, flv->duration_offset, SEEK_SET);
  225. put_amf_double(pb, flv->duration / (double)1000);
  226. url_fseek(pb, flv->filesize_offset, SEEK_SET);
  227. put_amf_double(pb, file_size);
  228. url_fseek(pb, file_size, SEEK_SET);
  229. return 0;
  230. }
  231. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  232. {
  233. ByteIOContext *pb = s->pb;
  234. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  235. FLVContext *flv = s->priv_data;
  236. int size= pkt->size;
  237. int flags, flags_size;
  238. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
  239. if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F)
  240. flags_size= 2;
  241. else
  242. flags_size= 1;
  243. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  244. put_byte(pb, FLV_TAG_TYPE_VIDEO);
  245. flags = enc->codec_tag;
  246. if(flags == 0) {
  247. av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
  248. return -1;
  249. }
  250. flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  251. } else {
  252. assert(enc->codec_type == CODEC_TYPE_AUDIO);
  253. flags = get_audio_flags(enc);
  254. assert(size);
  255. put_byte(pb, FLV_TAG_TYPE_AUDIO);
  256. }
  257. put_be24(pb,size + flags_size);
  258. put_be24(pb,pkt->pts);
  259. put_byte(pb,pkt->pts >> 24);
  260. put_be24(pb,flv->reserved);
  261. put_byte(pb,flags);
  262. if (enc->codec_id == CODEC_ID_VP6)
  263. put_byte(pb,0);
  264. if (enc->codec_id == CODEC_ID_VP6F)
  265. put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
  266. put_buffer(pb, pkt->data, size);
  267. put_be32(pb,size+flags_size+11); // previous tag size
  268. flv->duration = pkt->pts + pkt->duration;
  269. put_flush_packet(pb);
  270. return 0;
  271. }
  272. AVOutputFormat flv_muxer = {
  273. "flv",
  274. "flv format",
  275. "video/x-flv",
  276. "flv",
  277. sizeof(FLVContext),
  278. #ifdef CONFIG_LIBMP3LAME
  279. CODEC_ID_MP3,
  280. #else // CONFIG_LIBMP3LAME
  281. CODEC_ID_ADPCM_SWF,
  282. #endif // CONFIG_LIBMP3LAME
  283. CODEC_ID_FLV1,
  284. flv_write_header,
  285. flv_write_packet,
  286. flv_write_trailer,
  287. .codec_tag= (const AVCodecTag*[]){flv_video_codec_ids, flv_audio_codec_ids, 0},
  288. };