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.

375 lines
10KB

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