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.

397 lines
11KB

  1. /*
  2. * FLV encoder.
  3. * Copyright (c) 2003 The FFmpeg Project.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #define VIDEO_FIFO_SIZE 512
  21. typedef struct FLVFrame {
  22. int type;
  23. int timestamp;
  24. int flags;
  25. uint8_t *data;
  26. int size;
  27. struct FLVFrame *next;
  28. } FLVFrame;
  29. typedef struct FLVContext {
  30. int hasAudio;
  31. int hasVideo;
  32. #ifdef CONFIG_MP3LAME
  33. int audioTime;
  34. int audioInPos;
  35. int audioOutPos;
  36. int audioSize;
  37. // int audioRate;
  38. int initDelay;
  39. int soundDelay;
  40. uint8_t *audioFifo;
  41. int64_t sampleCount;
  42. #endif // CONFIG_MP3LAME
  43. int64_t frameCount;
  44. FLVFrame *frames;
  45. } FLVContext;
  46. #ifdef CONFIG_MP3LAME
  47. #define AUDIO_FIFO_SIZE 65536
  48. static const int sSampleRates[3][4] = {
  49. {44100, 48000, 32000, 0},
  50. {22050, 24000, 16000, 0},
  51. {11025, 12000, 8000, 0},
  52. };
  53. static const int sBitRates[2][3][15] = {
  54. { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
  55. { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
  56. { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
  57. },
  58. { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
  59. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
  60. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
  61. },
  62. };
  63. static const int sSamplesPerFrame[3][3] =
  64. {
  65. { 384, 1152, 1152 },
  66. { 384, 1152, 576 },
  67. { 384, 1152, 576 }
  68. };
  69. static const int sBitsPerSlot[3] = {
  70. 32,
  71. 8,
  72. 8
  73. };
  74. static int mp3info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
  75. {
  76. uint8_t *dataTmp = (uint8_t *)data;
  77. uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
  78. int layerID = 3 - ((header >> 17) & 0x03);
  79. int bitRateID = ((header >> 12) & 0x0f);
  80. int sampleRateID = ((header >> 10) & 0x03);
  81. int bitRate = 0;
  82. int bitsPerSlot = sBitsPerSlot[layerID];
  83. int isPadded = ((header >> 9) & 0x01);
  84. if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
  85. return 0;
  86. }
  87. if ( !isPadded ) {
  88. // printf("Fatal error: mp3 data is not padded!\n");
  89. // exit(0);
  90. }
  91. *isMono = ((header >> 6) & 0x03) == 0x03;
  92. if ( (header >> 19 ) & 0x01 ) {
  93. //MPEG1
  94. *sampleRate = sSampleRates[0][sampleRateID];
  95. bitRate = sBitRates[0][layerID][bitRateID] * 1000;
  96. *samplesPerFrame = sSamplesPerFrame[0][layerID];
  97. } else {
  98. if ( (header >> 20) & 0x01 ) {
  99. //MPEG2
  100. *sampleRate = sSampleRates[1][sampleRateID];
  101. bitRate = sBitRates[1][layerID][bitRateID] * 1000;
  102. *samplesPerFrame = sSamplesPerFrame[1][layerID];
  103. } else {
  104. //MPEG2.5
  105. *sampleRate = sSampleRates[2][sampleRateID];
  106. bitRate = sBitRates[1][layerID][bitRateID] * 1000;
  107. *samplesPerFrame = sSamplesPerFrame[2][layerID];
  108. }
  109. }
  110. *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
  111. return 1;
  112. }
  113. #endif // CONFIG_MP3LAME
  114. static int flv_write_header(AVFormatContext *s)
  115. {
  116. ByteIOContext *pb = &s->pb;
  117. FLVContext *flv = s->priv_data;
  118. av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
  119. flv->hasAudio = 0;
  120. flv->hasVideo = 0;
  121. #ifdef CONFIG_MP3LAME
  122. flv->audioTime = -1;
  123. flv->audioFifo = av_malloc(AUDIO_FIFO_SIZE);
  124. flv->audioInPos = 0;
  125. flv->audioOutPos = 0;
  126. flv->audioSize = 0;
  127. // flv->audioRate = 44100;
  128. flv->initDelay = -1;
  129. flv->soundDelay = 0;
  130. #endif // CONFIG_MP3LAME
  131. flv->frames = 0;
  132. put_tag(pb,"FLV");
  133. put_byte(pb,1);
  134. put_byte(pb,0); // delayed write
  135. put_be32(pb,9);
  136. put_be32(pb,0);
  137. return 0;
  138. }
  139. static void put_be24(ByteIOContext *pb, int value)
  140. {
  141. put_byte(pb, (value>>16) & 0xFF );
  142. put_byte(pb, (value>> 8) & 0xFF );
  143. put_byte(pb, (value>> 0) & 0xFF );
  144. }
  145. static void InsertSorted(FLVContext *flv, FLVFrame *frame)
  146. {
  147. if ( !flv->frames ) {
  148. flv->frames = frame;
  149. } else {
  150. FLVFrame *trav = flv->frames;
  151. FLVFrame *prev = 0;
  152. for (;trav;) {
  153. if ( trav->timestamp >= frame->timestamp ) {
  154. frame->next = trav;
  155. if ( prev ) {
  156. prev->next = frame;
  157. } else {
  158. flv->frames = frame;
  159. }
  160. break;
  161. }
  162. prev = trav;
  163. trav = trav->next;
  164. }
  165. if ( !trav ) {
  166. prev->next = frame;
  167. }
  168. }
  169. }
  170. static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
  171. {
  172. put_byte(pb,frame->type); // message type
  173. put_be24(pb,frame->size+1); // include flags
  174. put_be24(pb,frame->timestamp); // time stamp
  175. put_be32(pb,0); // reserved
  176. put_byte(pb,frame->flags);
  177. put_buffer(pb, frame->data, frame->size);
  178. put_be32(pb,frame->size+1+11); // reserved
  179. av_free(frame->data);
  180. }
  181. static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
  182. {
  183. int c=0;
  184. FLVFrame *trav = flv->frames;
  185. FLVFrame *prev = 0;
  186. for (;trav;c++) {
  187. trav = trav->next;
  188. }
  189. trav = flv->frames;
  190. for ( ; c >= count; c-- ) {
  191. DumpFrame(pb,trav);
  192. prev = trav;
  193. trav = trav->next;
  194. av_free(prev);
  195. }
  196. flv->frames = trav;
  197. }
  198. static int flv_write_trailer(AVFormatContext *s)
  199. {
  200. int64_t file_size;
  201. int flags = 0;
  202. ByteIOContext *pb = &s->pb;
  203. FLVContext *flv = s->priv_data;
  204. Dump(flv,pb,1);
  205. file_size = url_ftell(pb);
  206. flags |= flv->hasAudio ? 4 : 0;
  207. flags |= flv->hasVideo ? 1 : 0;
  208. url_fseek(pb, 4, SEEK_SET);
  209. put_byte(pb,flags);
  210. url_fseek(pb, file_size, SEEK_SET);
  211. return 0;
  212. }
  213. static int flv_write_packet(AVFormatContext *s, int stream_index,
  214. const uint8_t *buf, int size, int64_t timestamp)
  215. {
  216. ByteIOContext *pb = &s->pb;
  217. AVCodecContext *enc = &s->streams[stream_index]->codec;
  218. FLVContext *flv = s->priv_data;
  219. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  220. FLVFrame *frame = av_malloc(sizeof(FLVFrame));
  221. frame->next = 0;
  222. frame->type = 9;
  223. frame->flags = 2; // choose h263
  224. frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
  225. frame->timestamp = timestamp;
  226. //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
  227. //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
  228. frame->size = size;
  229. frame->data = av_malloc(size);
  230. memcpy(frame->data,buf,size);
  231. flv->hasVideo = 1;
  232. InsertSorted(flv,frame);
  233. flv->frameCount ++;
  234. }
  235. else if (enc->codec_type == CODEC_TYPE_AUDIO) {
  236. #ifdef CONFIG_MP3LAME
  237. if (enc->codec_id == CODEC_ID_MP3 ) {
  238. int c=0;
  239. for (;c<size;c++) {
  240. flv->audioFifo[(flv->audioOutPos+c)%AUDIO_FIFO_SIZE] = buf[c];
  241. }
  242. flv->audioSize += size;
  243. flv->audioOutPos += size;
  244. flv->audioOutPos %= AUDIO_FIFO_SIZE;
  245. if ( flv->initDelay == -1 ) {
  246. flv->initDelay = timestamp;
  247. }
  248. // if ( flv->audioTime == -1 ) {
  249. flv->audioTime = timestamp;
  250. // flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
  251. // if ( flv->audioTime < 0 ) {
  252. // flv->audioTime = 0;
  253. // }
  254. // }
  255. }
  256. for ( ; flv->audioSize >= 4 ; ) {
  257. int mp3FrameSize = 0;
  258. int mp3SampleRate = 0;
  259. int mp3IsMono = 0;
  260. int mp3SamplesPerFrame = 0;
  261. int c=0;
  262. /* copy out mp3 header from ring buffer */
  263. uint8_t header[4];
  264. for (c=0; c<4; c++) {
  265. header[c] = flv->audioFifo[(flv->audioInPos+c) % AUDIO_FIFO_SIZE];
  266. }
  267. if ( mp3info(header,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
  268. if ( flv->audioSize >= mp3FrameSize ) {
  269. int soundFormat = 0x22;
  270. int c=0;
  271. FLVFrame *frame = av_malloc(sizeof(FLVFrame));
  272. // flv->audioRate = mp3SampleRate;
  273. switch (mp3SampleRate) {
  274. case 44100:
  275. soundFormat |= 0x0C;
  276. break;
  277. case 22050:
  278. soundFormat |= 0x08;
  279. break;
  280. case 11025:
  281. soundFormat |= 0x04;
  282. break;
  283. }
  284. if ( !mp3IsMono ) {
  285. soundFormat |= 0x01;
  286. }
  287. frame->next = 0;
  288. frame->type = 8;
  289. frame->flags = soundFormat;
  290. frame->timestamp = flv->audioTime;
  291. frame->timestamp = (1000*flv->sampleCount + mp3SampleRate/2)/(mp3SampleRate);
  292. frame->size = mp3FrameSize;
  293. frame->data = av_malloc(mp3FrameSize);
  294. for (;c<mp3FrameSize;c++) {
  295. frame->data[c] = flv->audioFifo[(flv->audioInPos+c)%AUDIO_FIFO_SIZE];
  296. }
  297. flv->audioInPos += mp3FrameSize;
  298. flv->audioSize -= mp3FrameSize;
  299. flv->audioInPos %= AUDIO_FIFO_SIZE;
  300. flv->sampleCount += mp3SamplesPerFrame;
  301. flv->audioTime += 1000*mp3SamplesPerFrame/mp3SampleRate;
  302. // We got audio! Make sure we set this to the global flags on closure
  303. flv->hasAudio = 1;
  304. InsertSorted(flv,frame);
  305. // av_log(NULL,AV_LOG_DEBUG, "insert sound\n");
  306. continue;
  307. }
  308. // av_log(NULL,AV_LOG_DEBUG, "insuficent data\n");
  309. break;
  310. }
  311. av_log(NULL,AV_LOG_DEBUG, "head trashed\n");
  312. flv->audioInPos ++;
  313. flv->audioSize --;
  314. flv->audioInPos %= AUDIO_FIFO_SIZE;
  315. // no audio in here!
  316. flv->audioTime = -1;
  317. }
  318. #endif
  319. }
  320. Dump(flv,pb,128);
  321. put_flush_packet(pb);
  322. return 0;
  323. }
  324. static AVOutputFormat flv_oformat = {
  325. "flv",
  326. "flv format",
  327. "video/x-flashvideo",
  328. "flv",
  329. sizeof(FLVContext),
  330. #ifdef CONFIG_MP3LAME
  331. CODEC_ID_MP3,
  332. #else // CONFIG_MP3LAME
  333. CODEC_ID_NONE,
  334. #endif // CONFIG_MP3LAME
  335. CODEC_ID_FLV1,
  336. flv_write_header,
  337. flv_write_packet,
  338. flv_write_trailer,
  339. };
  340. int flvenc_init(void)
  341. {
  342. av_register_output_format(&flv_oformat);
  343. return 0;
  344. }