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.

249 lines
7.4KB

  1. /*
  2. * FLV encoder.
  3. * Copyright (c) 2003 The FFmpeg Project.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. static int flv_probe(AVProbeData *p)
  23. {
  24. const uint8_t *d;
  25. if (p->buf_size < 6)
  26. return 0;
  27. d = p->buf;
  28. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
  29. return 50;
  30. }
  31. return 0;
  32. }
  33. static int flv_read_header(AVFormatContext *s,
  34. AVFormatParameters *ap)
  35. {
  36. int offset, flags, size;
  37. s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
  38. url_fskip(&s->pb, 4);
  39. flags = get_byte(&s->pb);
  40. offset = get_be32(&s->pb);
  41. if(!url_is_streamed(&s->pb)){
  42. const int fsize= url_fsize(&s->pb);
  43. url_fseek(&s->pb, fsize-4, SEEK_SET);
  44. size= get_be32(&s->pb);
  45. url_fseek(&s->pb, fsize-3-size, SEEK_SET);
  46. if(size == get_be24(&s->pb) + 11){
  47. s->duration= get_be24(&s->pb) * (int64_t)AV_TIME_BASE / 1000;
  48. }
  49. }
  50. url_fseek(&s->pb, offset, SEEK_SET);
  51. s->start_time = 0;
  52. return 0;
  53. }
  54. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  55. {
  56. int ret, i, type, size, pts, flags, is_audio, next, pos;
  57. AVStream *st = NULL;
  58. for(;;){
  59. pos = url_ftell(&s->pb);
  60. url_fskip(&s->pb, 4); /* size of previous packet */
  61. type = get_byte(&s->pb);
  62. size = get_be24(&s->pb);
  63. pts = get_be24(&s->pb);
  64. // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
  65. if (url_feof(&s->pb))
  66. return AVERROR_IO;
  67. url_fskip(&s->pb, 4); /* reserved */
  68. flags = 0;
  69. if(size == 0)
  70. continue;
  71. next= size + url_ftell(&s->pb);
  72. if (type == 8) {
  73. is_audio=1;
  74. flags = get_byte(&s->pb);
  75. } else if (type == 9) {
  76. is_audio=0;
  77. flags = get_byte(&s->pb);
  78. } else if (type == 18 && size > 13+1+4) {
  79. url_fskip(&s->pb, 13); //onMetaData blah
  80. if(get_byte(&s->pb) == 8){
  81. url_fskip(&s->pb, 4);
  82. }
  83. while(url_ftell(&s->pb) + 5 < next){
  84. char tmp[128];
  85. int type, len;
  86. double d= 0;
  87. len= get_be16(&s->pb);
  88. if(len >= sizeof(tmp) || !len)
  89. break;
  90. get_buffer(&s->pb, tmp, len);
  91. tmp[len]=0;
  92. type= get_byte(&s->pb);
  93. if(type==0){
  94. d= av_int2dbl(get_be64(&s->pb));
  95. }else if(type==2){
  96. len= get_be16(&s->pb);
  97. if(len >= sizeof(tmp))
  98. break;
  99. url_fskip(&s->pb, len);
  100. }else if(type==8){
  101. //array
  102. break;
  103. }else if(type==11){
  104. d= av_int2dbl(get_be64(&s->pb));
  105. get_be16(&s->pb);
  106. }
  107. if(!strcmp(tmp, "duration")){
  108. s->duration = d*AV_TIME_BASE;
  109. }else if(!strcmp(tmp, "videodatarate")){
  110. }else if(!strcmp(tmp, "audiodatarate")){
  111. }
  112. }
  113. url_fseek(&s->pb, next, SEEK_SET);
  114. continue;
  115. } else {
  116. /* skip packet */
  117. av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  118. url_fseek(&s->pb, next, SEEK_SET);
  119. continue;
  120. }
  121. /* now find stream */
  122. for(i=0;i<s->nb_streams;i++) {
  123. st = s->streams[i];
  124. if (st->id == is_audio)
  125. break;
  126. }
  127. if(i == s->nb_streams){
  128. st = av_new_stream(s, is_audio);
  129. if (!st)
  130. return AVERROR_NOMEM;
  131. av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
  132. st->codec->time_base= (AVRational){1,1000};
  133. }
  134. // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
  135. if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio))
  136. ||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio))
  137. || st->discard >= AVDISCARD_ALL
  138. ){
  139. url_fseek(&s->pb, next, SEEK_SET);
  140. continue;
  141. }
  142. if ((flags >> 4)==1)
  143. av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME);
  144. break;
  145. }
  146. if(is_audio){
  147. if(st->codec->sample_rate == 0){
  148. st->codec->codec_type = CODEC_TYPE_AUDIO;
  149. st->codec->channels = (flags&1)+1;
  150. if((flags >> 4) == 5)
  151. st->codec->sample_rate= 8000;
  152. else
  153. st->codec->sample_rate = (44100<<((flags>>2)&3))>>3;
  154. switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
  155. case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE;
  156. else st->codec->codec_id = CODEC_ID_PCM_S8; break;
  157. case 1: st->codec->codec_id = CODEC_ID_ADPCM_SWF; break;
  158. case 2: st->codec->codec_id = CODEC_ID_MP3; break;
  159. // this is not listed at FLV but at SWF, strange...
  160. case 3: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16LE;
  161. else st->codec->codec_id = CODEC_ID_PCM_S8; break;
  162. default:
  163. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
  164. st->codec->codec_tag= (flags >> 4);
  165. }
  166. st->codec->bits_per_sample = (flags & 2) ? 16 : 8;
  167. }
  168. }else{
  169. st->codec->codec_type = CODEC_TYPE_VIDEO;
  170. switch(flags & 0xF){
  171. case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
  172. case 3: st->codec->codec_id = CODEC_ID_FLASHSV; break;
  173. case 4:
  174. st->codec->codec_id = CODEC_ID_VP6F;
  175. get_byte(&s->pb); /* width and height adjustment */
  176. size--;
  177. break;
  178. default:
  179. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
  180. st->codec->codec_tag= flags & 0xF;
  181. }
  182. }
  183. ret= av_get_packet(&s->pb, pkt, size - 1);
  184. if (ret <= 0) {
  185. return AVERROR_IO;
  186. }
  187. /* note: we need to modify the packet size here to handle the last
  188. packet */
  189. pkt->size = ret;
  190. pkt->pts = pts;
  191. pkt->stream_index = st->index;
  192. if (is_audio || ((flags >> 4)==1))
  193. pkt->flags |= PKT_FLAG_KEY;
  194. return ret;
  195. }
  196. static int flv_read_close(AVFormatContext *s)
  197. {
  198. return 0;
  199. }
  200. static int flv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  201. {
  202. AVStream *st = s->streams[stream_index];
  203. int index = av_index_search_timestamp(st, timestamp, flags);
  204. if (index < 0)
  205. return -1;
  206. url_fseek(&s->pb, st->index_entries[index].pos, SEEK_SET);
  207. return 0;
  208. }
  209. AVInputFormat flv_demuxer = {
  210. "flv",
  211. "flv format",
  212. 0,
  213. flv_probe,
  214. flv_read_header,
  215. flv_read_packet,
  216. flv_read_close,
  217. flv_read_seek,
  218. .extensions = "flv",
  219. .value = CODEC_ID_FLV1,
  220. };