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.

219 lines
6.4KB

  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. static int flv_probe(AVProbeData *p)
  21. {
  22. const uint8_t *d;
  23. if (p->buf_size < 6)
  24. return 0;
  25. d = p->buf;
  26. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
  27. return 50;
  28. }
  29. return 0;
  30. }
  31. static int flv_read_header(AVFormatContext *s,
  32. AVFormatParameters *ap)
  33. {
  34. int offset, flags;
  35. s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
  36. url_fskip(&s->pb, 4);
  37. flags = get_byte(&s->pb);
  38. offset = get_be32(&s->pb);
  39. url_fseek(&s->pb, offset, SEEK_SET);
  40. return 0;
  41. }
  42. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  43. {
  44. int ret, i, type, size, pts, flags, is_audio, next;
  45. AVStream *st = NULL;
  46. for(;;){
  47. url_fskip(&s->pb, 4); /* size of previous packet */
  48. type = get_byte(&s->pb);
  49. size = get_be24(&s->pb);
  50. pts = get_be24(&s->pb);
  51. // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
  52. if (url_feof(&s->pb))
  53. return AVERROR_IO;
  54. url_fskip(&s->pb, 4); /* reserved */
  55. flags = 0;
  56. if(size == 0)
  57. continue;
  58. next= size + url_ftell(&s->pb);
  59. if (type == 8) {
  60. is_audio=1;
  61. flags = get_byte(&s->pb);
  62. } else if (type == 9) {
  63. is_audio=0;
  64. flags = get_byte(&s->pb);
  65. } else if (type == 18 && size > 13+1+4) {
  66. url_fskip(&s->pb, 13); //onMetaData blah
  67. if(get_byte(&s->pb) == 8){
  68. url_fskip(&s->pb, 4);
  69. }
  70. while(url_ftell(&s->pb) + 5 < next){
  71. char tmp[128];
  72. int type, len;
  73. double d= 0;
  74. len= get_be16(&s->pb);
  75. if(len >= sizeof(tmp) || !len)
  76. break;
  77. get_buffer(&s->pb, tmp, len);
  78. tmp[len]=0;
  79. type= get_byte(&s->pb);
  80. if(type==0){
  81. d= av_int2dbl(get_be64(&s->pb));
  82. }else if(type==2){
  83. len= get_be16(&s->pb);
  84. if(len >= sizeof(tmp))
  85. break;
  86. url_fskip(&s->pb, len);
  87. }else if(type==8){
  88. //array
  89. break;
  90. }else if(type==11){
  91. d= av_int2dbl(get_be64(&s->pb));
  92. get_be16(&s->pb);
  93. }
  94. if(!strcmp(tmp, "duration")){
  95. s->duration = d*AV_TIME_BASE;
  96. }else if(!strcmp(tmp, "videodatarate")){
  97. }else if(!strcmp(tmp, "audiodatarate")){
  98. }
  99. }
  100. url_fseek(&s->pb, next, SEEK_SET);
  101. continue;
  102. } else {
  103. /* skip packet */
  104. av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  105. url_fseek(&s->pb, next, SEEK_SET);
  106. continue;
  107. }
  108. /* now find stream */
  109. for(i=0;i<s->nb_streams;i++) {
  110. st = s->streams[i];
  111. if (st->id == is_audio)
  112. break;
  113. }
  114. if(i == s->nb_streams){
  115. st = av_new_stream(s, is_audio);
  116. if (!st)
  117. return AVERROR_NOMEM;
  118. av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
  119. st->codec->time_base= (AVRational){1,1000};
  120. }
  121. // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
  122. if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio))
  123. ||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio))
  124. || st->discard >= AVDISCARD_ALL
  125. ){
  126. url_fseek(&s->pb, next, SEEK_SET);
  127. continue;
  128. }
  129. break;
  130. }
  131. if(is_audio){
  132. if(st->codec->sample_rate == 0){
  133. st->codec->codec_type = CODEC_TYPE_AUDIO;
  134. st->codec->channels = (flags&1)+1;
  135. if((flags >> 4) == 5)
  136. st->codec->sample_rate= 8000;
  137. else
  138. st->codec->sample_rate = (44100<<((flags>>2)&3))>>3;
  139. switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
  140. case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE;
  141. else st->codec->codec_id = CODEC_ID_PCM_S8; break;
  142. case 1: st->codec->codec_id = CODEC_ID_ADPCM_SWF; break;
  143. case 2: st->codec->codec_id = CODEC_ID_MP3; break;
  144. // this is not listed at FLV but at SWF, strange...
  145. case 3: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16LE;
  146. else st->codec->codec_id = CODEC_ID_PCM_S8; break;
  147. default:
  148. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
  149. st->codec->codec_tag= (flags >> 4);
  150. }
  151. st->codec->bits_per_sample = (flags & 2) ? 16 : 8;
  152. }
  153. }else{
  154. st->codec->codec_type = CODEC_TYPE_VIDEO;
  155. switch(flags & 0xF){
  156. case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
  157. default:
  158. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
  159. st->codec->codec_tag= flags & 0xF;
  160. }
  161. }
  162. ret= av_get_packet(&s->pb, pkt, size - 1);
  163. if (ret <= 0) {
  164. return AVERROR_IO;
  165. }
  166. /* note: we need to modify the packet size here to handle the last
  167. packet */
  168. pkt->size = ret;
  169. pkt->pts = pts;
  170. pkt->stream_index = st->index;
  171. if (is_audio || ((flags >> 4)==1))
  172. pkt->flags |= PKT_FLAG_KEY;
  173. return ret;
  174. }
  175. static int flv_read_close(AVFormatContext *s)
  176. {
  177. return 0;
  178. }
  179. AVInputFormat flv_iformat = {
  180. "flv",
  181. "flv format",
  182. 0,
  183. flv_probe,
  184. flv_read_header,
  185. flv_read_packet,
  186. flv_read_close,
  187. .extensions = "flv",
  188. .value = CODEC_ID_FLV1,
  189. };
  190. int flvdec_init(void)
  191. {
  192. av_register_input_format(&flv_iformat);
  193. return 0;
  194. }