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.

384 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. *sampleRate = sSampleRates[0][sampleRateID];
  94. bitRate = sBitRates[0][layerID][bitRateID] * 1000;
  95. *samplesPerFrame = sSamplesPerFrame[0][layerID];
  96. } else {
  97. if ( (header >> 20) & 0x01 ) {
  98. *sampleRate = sSampleRates[1][sampleRateID];
  99. bitRate = sBitRates[1][layerID][bitRateID] * 1000;
  100. *samplesPerFrame = sSamplesPerFrame[1][layerID];
  101. } else {
  102. *sampleRate = sSampleRates[2][sampleRateID];
  103. bitRate = sBitRates[1][layerID][bitRateID] * 1000;
  104. *samplesPerFrame = sSamplesPerFrame[2][layerID];
  105. }
  106. }
  107. *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) * bitsPerSlot);
  108. return 1;
  109. }
  110. #endif // CONFIG_MP3LAME
  111. static int flv_write_header(AVFormatContext *s)
  112. {
  113. ByteIOContext *pb = &s->pb;
  114. FLVContext *flv = s->priv_data;
  115. av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
  116. flv->hasAudio = 0;
  117. flv->hasVideo = 0;
  118. #ifdef CONFIG_MP3LAME
  119. flv->audioTime = -1;
  120. flv->audioFifo = av_malloc(AUDIO_FIFO_SIZE);
  121. flv->audioInPos = 0;
  122. flv->audioOutPos = 0;
  123. flv->audioSize = 0;
  124. flv->audioRate = 44100;
  125. flv->initDelay = -1;
  126. flv->soundDelay = 0;
  127. #endif // CONFIG_MP3LAME
  128. flv->frames = 0;
  129. put_tag(pb,"FLV");
  130. put_byte(pb,1);
  131. put_byte(pb,0); // delayed write
  132. put_be32(pb,9);
  133. put_be32(pb,0);
  134. return 0;
  135. }
  136. static void put_be24(ByteIOContext *pb, int value)
  137. {
  138. put_byte(pb, (value>>16) & 0xFF );
  139. put_byte(pb, (value>> 8) & 0xFF );
  140. put_byte(pb, (value>> 0) & 0xFF );
  141. }
  142. static void InsertSorted(FLVContext *flv, FLVFrame *frame)
  143. {
  144. if ( !flv->frames ) {
  145. flv->frames = frame;
  146. } else {
  147. FLVFrame *trav = flv->frames;
  148. FLVFrame *prev = 0;
  149. for (;trav;) {
  150. if ( trav->timestamp >= frame->timestamp ) {
  151. frame->next = trav;
  152. if ( prev ) {
  153. prev->next = frame;
  154. } else {
  155. flv->frames = frame;
  156. }
  157. break;
  158. }
  159. prev = trav;
  160. trav = trav->next;
  161. }
  162. if ( !trav ) {
  163. prev->next = frame;
  164. }
  165. }
  166. }
  167. static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
  168. {
  169. put_byte(pb,frame->type); // message type
  170. put_be24(pb,frame->size+1); // include flags
  171. put_be24(pb,frame->timestamp); // time stamp
  172. put_be32(pb,0); // reserved
  173. put_byte(pb,frame->flags);
  174. put_buffer(pb, frame->data, frame->size);
  175. put_be32(pb,frame->size+1+11); // reserved
  176. av_free(frame->data);
  177. }
  178. static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
  179. {
  180. int c=0;
  181. FLVFrame *trav = flv->frames;
  182. FLVFrame *prev = 0;
  183. for (;trav;c++) {
  184. trav = trav->next;
  185. }
  186. trav = flv->frames;
  187. for ( ; c >= count; c-- ) {
  188. DumpFrame(pb,trav);
  189. prev = trav;
  190. trav = trav->next;
  191. av_free(prev);
  192. }
  193. flv->frames = trav;
  194. }
  195. static int flv_write_trailer(AVFormatContext *s)
  196. {
  197. int64_t file_size;
  198. int flags = 0;
  199. ByteIOContext *pb = &s->pb;
  200. FLVContext *flv = s->priv_data;
  201. Dump(flv,pb,1);
  202. file_size = url_ftell(pb);
  203. flags |= flv->hasAudio ? 4 : 0;
  204. flags |= flv->hasVideo ? 1 : 0;
  205. url_fseek(pb, 4, SEEK_SET);
  206. put_byte(pb,flags);
  207. url_fseek(pb, file_size, SEEK_SET);
  208. return 0;
  209. }
  210. static int flv_write_packet(AVFormatContext *s, int stream_index,
  211. const uint8_t *buf, int size, int64_t timestamp)
  212. {
  213. ByteIOContext *pb = &s->pb;
  214. AVCodecContext *enc = &s->streams[stream_index]->codec;
  215. FLVContext *flv = s->priv_data;
  216. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  217. FLVFrame *frame = av_malloc(sizeof(FLVFrame));
  218. frame->next = 0;
  219. frame->type = 9;
  220. frame->flags = 2; // choose h263
  221. frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
  222. frame->timestamp = timestamp;
  223. //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
  224. //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
  225. frame->size = size;
  226. frame->data = av_malloc(size);
  227. memcpy(frame->data,buf,size);
  228. flv->hasVideo = 1;
  229. InsertSorted(flv,frame);
  230. flv->frameCount ++;
  231. }
  232. else if (enc->codec_type == CODEC_TYPE_AUDIO) {
  233. #ifdef CONFIG_MP3LAME
  234. if (enc->codec_id == CODEC_ID_MP3 ) {
  235. int c=0;
  236. for (;c<size;c++) {
  237. flv->audioFifo[(flv->audioOutPos+c)%AUDIO_FIFO_SIZE] = buf[c];
  238. }
  239. flv->audioSize += size;
  240. flv->audioOutPos += size;
  241. flv->audioOutPos %= AUDIO_FIFO_SIZE;
  242. if ( flv->initDelay == -1 ) {
  243. flv->initDelay = timestamp;
  244. }
  245. if ( flv->audioTime == -1 ) {
  246. flv->audioTime = timestamp;
  247. // flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
  248. // if ( flv->audioTime < 0 ) {
  249. // flv->audioTime = 0;
  250. // }
  251. }
  252. }
  253. for ( ; flv->audioSize >= 4 ; ) {
  254. int mp3FrameSize = 0;
  255. int mp3SampleRate = 0;
  256. int mp3IsMono = 0;
  257. int mp3SamplesPerFrame = 0;
  258. if ( mp3info(&flv->audioFifo[flv->audioInPos],&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono) ) {
  259. if ( flv->audioSize >= mp3FrameSize ) {
  260. int soundFormat = 0x22;
  261. int c=0;
  262. FLVFrame *frame = av_malloc(sizeof(FLVFrame));
  263. flv->audioRate = mp3SampleRate;
  264. switch (mp3SampleRate) {
  265. case 44100:
  266. soundFormat |= 0x0C;
  267. break;
  268. case 22050:
  269. soundFormat |= 0x08;
  270. break;
  271. case 11025:
  272. soundFormat |= 0x04;
  273. break;
  274. }
  275. if ( !mp3IsMono ) {
  276. soundFormat |= 0x01;
  277. }
  278. frame->next = 0;
  279. frame->type = 8;
  280. frame->flags = soundFormat;
  281. frame->timestamp = flv->audioTime;
  282. frame->size = mp3FrameSize;
  283. frame->data = av_malloc(mp3FrameSize);
  284. for (;c<mp3FrameSize;c++) {
  285. frame->data[c] = flv->audioFifo[(flv->audioInPos+c)%AUDIO_FIFO_SIZE];
  286. }
  287. flv->audioInPos += mp3FrameSize;
  288. flv->audioSize -= mp3FrameSize;
  289. flv->audioInPos %= AUDIO_FIFO_SIZE;
  290. flv->sampleCount += mp3SamplesPerFrame;
  291. // Reset audio for next round
  292. flv->audioTime = -1;
  293. // We got audio! Make sure we set this to the global flags on closure
  294. flv->hasAudio = 1;
  295. InsertSorted(flv,frame);
  296. }
  297. break;
  298. }
  299. flv->audioInPos ++;
  300. flv->audioSize --;
  301. flv->audioInPos %= AUDIO_FIFO_SIZE;
  302. // no audio in here!
  303. flv->audioTime = -1;
  304. }
  305. #endif
  306. }
  307. Dump(flv,pb,128);
  308. put_flush_packet(pb);
  309. return 0;
  310. }
  311. static AVOutputFormat flv_oformat = {
  312. "flv",
  313. "flv format",
  314. "video/x-flashvideo",
  315. "flv",
  316. sizeof(FLVContext),
  317. #ifdef CONFIG_MP3LAME
  318. CODEC_ID_MP3,
  319. #else // CONFIG_MP3LAME
  320. CODEC_ID_NONE,
  321. #endif // CONFIG_MP3LAME
  322. CODEC_ID_FLV1,
  323. flv_write_header,
  324. flv_write_packet,
  325. flv_write_trailer,
  326. };
  327. int flvenc_init(void)
  328. {
  329. av_register_output_format(&flv_oformat);
  330. return 0;
  331. }