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.

225 lines
6.7KB

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