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.

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