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.

358 lines
9.8KB

  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. #undef NDEBUG
  21. #include <assert.h>
  22. #define VIDEO_FIFO_SIZE 512
  23. typedef struct FLVFrame {
  24. int type;
  25. int timestamp;
  26. int flags;
  27. uint8_t *data;
  28. int size;
  29. struct FLVFrame *next;
  30. } FLVFrame;
  31. typedef struct FLVContext {
  32. int hasAudio;
  33. int hasVideo;
  34. int initDelay;
  35. int64_t sampleCount;
  36. int64_t frameCount;
  37. FLVFrame *frames;
  38. } FLVContext;
  39. #ifdef CONFIG_MP3LAME
  40. static const int sSampleRates[3][4] = {
  41. {44100, 48000, 32000, 0},
  42. {22050, 24000, 16000, 0},
  43. {11025, 12000, 8000, 0},
  44. };
  45. static const int sBitRates[2][3][15] = {
  46. { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
  47. { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
  48. { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
  49. },
  50. { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
  51. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
  52. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
  53. },
  54. };
  55. static const int sSamplesPerFrame[3][3] =
  56. {
  57. { 384, 1152, 1152 },
  58. { 384, 1152, 576 },
  59. { 384, 1152, 576 }
  60. };
  61. static const int sBitsPerSlot[3] = {
  62. 32,
  63. 8,
  64. 8
  65. };
  66. static int mp3info(void *data, int *byteSize, int *samplesPerFrame, int *sampleRate, int *isMono )
  67. {
  68. uint8_t *dataTmp = (uint8_t *)data;
  69. uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
  70. int layerID = 3 - ((header >> 17) & 0x03);
  71. int bitRateID = ((header >> 12) & 0x0f);
  72. int sampleRateID = ((header >> 10) & 0x03);
  73. int bitRate = 0;
  74. int bitsPerSlot = sBitsPerSlot[layerID];
  75. int isPadded = ((header >> 9) & 0x01);
  76. if ( (( header >> 21 ) & 0x7ff) != 0x7ff ) {
  77. return 0;
  78. }
  79. if ( !isPadded ) {
  80. // printf("Fatal error: mp3 data is not padded!\n");
  81. // exit(0);
  82. }
  83. *isMono = ((header >> 6) & 0x03) == 0x03;
  84. if ( (header >> 19 ) & 0x01 ) {
  85. //MPEG1
  86. *sampleRate = sSampleRates[0][sampleRateID];
  87. bitRate = sBitRates[0][layerID][bitRateID] * 1000;
  88. *samplesPerFrame = sSamplesPerFrame[0][layerID];
  89. } else {
  90. if ( (header >> 20) & 0x01 ) {
  91. //MPEG2
  92. *sampleRate = sSampleRates[1][sampleRateID];
  93. bitRate = sBitRates[1][layerID][bitRateID] * 1000;
  94. *samplesPerFrame = sSamplesPerFrame[1][layerID];
  95. } else {
  96. //MPEG2.5
  97. *sampleRate = sSampleRates[2][sampleRateID];
  98. bitRate = sBitRates[1][layerID][bitRateID] * 1000;
  99. *samplesPerFrame = sSamplesPerFrame[2][layerID];
  100. }
  101. }
  102. *byteSize = ( ( ( ( *samplesPerFrame * (bitRate / bitsPerSlot) ) / *sampleRate ) + isPadded ) );
  103. return 1;
  104. }
  105. #endif // CONFIG_MP3LAME
  106. static int flv_write_header(AVFormatContext *s)
  107. {
  108. ByteIOContext *pb = &s->pb;
  109. FLVContext *flv = s->priv_data;
  110. av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
  111. flv->hasAudio = 0;
  112. flv->hasVideo = 0;
  113. flv->initDelay = -1;
  114. flv->frames = 0;
  115. put_tag(pb,"FLV");
  116. put_byte(pb,1);
  117. put_byte(pb,0); // delayed write
  118. put_be32(pb,9);
  119. put_be32(pb,0);
  120. return 0;
  121. }
  122. static void put_be24(ByteIOContext *pb, int value)
  123. {
  124. put_byte(pb, (value>>16) & 0xFF );
  125. put_byte(pb, (value>> 8) & 0xFF );
  126. put_byte(pb, (value>> 0) & 0xFF );
  127. }
  128. static void InsertSorted(FLVContext *flv, FLVFrame *frame)
  129. {
  130. if ( !flv->frames ) {
  131. flv->frames = frame;
  132. } else {
  133. FLVFrame *trav = flv->frames;
  134. FLVFrame *prev = 0;
  135. for (;trav;) {
  136. if ( trav->timestamp >= frame->timestamp ) {
  137. frame->next = trav;
  138. if ( prev ) {
  139. prev->next = frame;
  140. } else {
  141. flv->frames = frame;
  142. }
  143. break;
  144. }
  145. prev = trav;
  146. trav = trav->next;
  147. }
  148. if ( !trav ) {
  149. prev->next = frame;
  150. }
  151. }
  152. }
  153. static void DumpFrame(ByteIOContext *pb, FLVFrame *frame)
  154. {
  155. put_byte(pb,frame->type); // message type
  156. put_be24(pb,frame->size+1); // include flags
  157. put_be24(pb,frame->timestamp); // time stamp
  158. put_be32(pb,0); // reserved
  159. put_byte(pb,frame->flags);
  160. put_buffer(pb, frame->data, frame->size);
  161. put_be32(pb,frame->size+1+11); // reserved
  162. av_free(frame->data);
  163. }
  164. static void Dump(FLVContext *flv, ByteIOContext *pb, int count)
  165. {
  166. int c=0;
  167. FLVFrame *trav = flv->frames;
  168. FLVFrame *prev = 0;
  169. for (;trav;c++) {
  170. trav = trav->next;
  171. }
  172. trav = flv->frames;
  173. for ( ; c >= count; c-- ) {
  174. DumpFrame(pb,trav);
  175. prev = trav;
  176. trav = trav->next;
  177. av_free(prev);
  178. }
  179. flv->frames = trav;
  180. }
  181. static int flv_write_trailer(AVFormatContext *s)
  182. {
  183. int64_t file_size;
  184. int flags = 0;
  185. ByteIOContext *pb = &s->pb;
  186. FLVContext *flv = s->priv_data;
  187. Dump(flv,pb,1);
  188. file_size = url_ftell(pb);
  189. flags |= flv->hasAudio ? 4 : 0;
  190. flags |= flv->hasVideo ? 1 : 0;
  191. url_fseek(pb, 4, SEEK_SET);
  192. put_byte(pb,flags);
  193. url_fseek(pb, file_size, SEEK_SET);
  194. return 0;
  195. }
  196. static int flv_write_packet(AVFormatContext *s, int stream_index,
  197. const uint8_t *buf, int size, int64_t timestamp)
  198. {
  199. ByteIOContext *pb = &s->pb;
  200. AVCodecContext *enc = &s->streams[stream_index]->codec;
  201. FLVContext *flv = s->priv_data;
  202. FLVFrame *frame = av_malloc(sizeof(FLVFrame));
  203. frame->next = 0;
  204. frame->size = size;
  205. frame->data = av_malloc(size);
  206. frame->timestamp = timestamp;
  207. memcpy(frame->data,buf,size);
  208. // av_log(s, AV_LOG_DEBUG, "type:%d pts: %lld size:%d\n", enc->codec_type, timestamp, size);
  209. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  210. frame->type = 9;
  211. frame->flags = 2; // choose h263
  212. frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator
  213. //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate );
  214. //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate);
  215. flv->hasVideo = 1;
  216. InsertSorted(flv,frame);
  217. flv->frameCount ++;
  218. }
  219. else if (enc->codec_type == CODEC_TYPE_AUDIO) {
  220. int soundFormat = 0x02;
  221. switch (enc->sample_rate) {
  222. case 44100:
  223. soundFormat |= 0x0C;
  224. break;
  225. case 22050:
  226. soundFormat |= 0x08;
  227. break;
  228. case 11025:
  229. soundFormat |= 0x04;
  230. break;
  231. case 8000: //nellymoser only
  232. case 5512: //not mp3
  233. soundFormat |= 0x00;
  234. break;
  235. default:
  236. assert(0);
  237. }
  238. if (enc->channels > 1) {
  239. soundFormat |= 0x01;
  240. }
  241. switch(enc->codec_id){
  242. case CODEC_ID_MP3:
  243. soundFormat |= 0x20;
  244. break;
  245. case 0:
  246. soundFormat |= enc->codec_tag<<4;
  247. break;
  248. default:
  249. assert(0);
  250. }
  251. assert(size);
  252. if ( flv->initDelay == -1 ) {
  253. flv->initDelay = timestamp;
  254. }
  255. frame->type = 8;
  256. frame->flags = soundFormat;
  257. // if ( flv->audioTime == -1 ) {
  258. // flv->audioTime = ( ( ( flv->sampleCount - enc->delay ) * 8000 ) / flv->audioRate ) - flv->initDelay - 250;
  259. // if ( flv->audioTime < 0 ) {
  260. // flv->audioTime = 0;
  261. // }
  262. // }
  263. #ifdef CONFIG_MP3LAME
  264. if (enc->codec_id == CODEC_ID_MP3 ) {
  265. int mp3FrameSize = 0;
  266. int mp3SampleRate = 0;
  267. int mp3IsMono = 0;
  268. int mp3SamplesPerFrame = 0;
  269. /* copy out mp3 header from ring buffer */
  270. if(!mp3info(buf,&mp3FrameSize,&mp3SamplesPerFrame,&mp3SampleRate,&mp3IsMono))
  271. assert(0);
  272. assert ( size == mp3FrameSize );
  273. assert(enc->sample_rate == mp3SampleRate);
  274. // assert(enc->frame_size == mp3SamplesPerFrame);
  275. //av_log(NULL, AV_LOG_DEBUG, "sizes: %d %d\n", enc->frame_size, mp3SamplesPerFrame);
  276. frame->timestamp = (1000*flv->sampleCount + enc->sample_rate/2)/(enc->sample_rate);
  277. flv->sampleCount += mp3SamplesPerFrame;
  278. }
  279. #endif
  280. // We got audio! Make sure we set this to the global flags on closure
  281. flv->hasAudio = 1;
  282. InsertSorted(flv,frame);
  283. }else
  284. assert(0);
  285. Dump(flv,pb,128);
  286. put_flush_packet(pb);
  287. return 0;
  288. }
  289. static AVOutputFormat flv_oformat = {
  290. "flv",
  291. "flv format",
  292. "video/x-flashvideo",
  293. "flv",
  294. sizeof(FLVContext),
  295. #ifdef CONFIG_MP3LAME
  296. CODEC_ID_MP3,
  297. #else // CONFIG_MP3LAME
  298. CODEC_ID_NONE,
  299. #endif // CONFIG_MP3LAME
  300. CODEC_ID_FLV1,
  301. flv_write_header,
  302. flv_write_packet,
  303. flv_write_trailer,
  304. };
  305. int flvenc_init(void)
  306. {
  307. av_register_output_format(&flv_oformat);
  308. return 0;
  309. }