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.

179 lines
4.6KB

  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. unsigned int get_be24(ByteIOContext *s)
  21. {
  22. unsigned int val;
  23. val = get_byte(s) << 16;
  24. val |= get_byte(s) << 8;
  25. val |= get_byte(s);
  26. return val;
  27. }
  28. static int flv_probe(AVProbeData *p)
  29. {
  30. const uint8_t *d;
  31. if (p->buf_size < 6)
  32. return 0;
  33. d = p->buf;
  34. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
  35. return 50;
  36. }
  37. return 0;
  38. }
  39. static int flv_read_header(AVFormatContext *s,
  40. AVFormatParameters *ap)
  41. {
  42. int offset, flags;
  43. AVStream *st;
  44. s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
  45. av_set_pts_info(s, 24, 1, 1000); /* 24 bit pts in ms */
  46. url_fskip(&s->pb, 4);
  47. flags = get_byte(&s->pb);
  48. offset = get_be32(&s->pb);
  49. url_fseek(&s->pb, offset, SEEK_SET);
  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;
  55. AVStream *st;
  56. for(;;){
  57. url_fskip(&s->pb, 4); /* size of previous packet */
  58. type = get_byte(&s->pb);
  59. size = get_be24(&s->pb);
  60. pts = get_be24(&s->pb);
  61. // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
  62. if (url_feof(&s->pb))
  63. return -EIO;
  64. url_fskip(&s->pb, 4); /* reserved */
  65. flags = 0;
  66. if(size == 0)
  67. continue;
  68. if (type == 8) {
  69. is_audio=1;
  70. flags = get_byte(&s->pb);
  71. size--;
  72. } else if (type == 9) {
  73. is_audio=0;
  74. flags = get_byte(&s->pb);
  75. size--;
  76. } else {
  77. /* skip packet */
  78. av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  79. url_fskip(&s->pb, size);
  80. continue;
  81. }
  82. /* now find stream */
  83. for(i=0;i<s->nb_streams;i++) {
  84. st = s->streams[i];
  85. if (st->id == is_audio)
  86. break;
  87. }
  88. if(i == s->nb_streams){
  89. st = av_new_stream(s, is_audio);
  90. if (!st)
  91. return AVERROR_NOMEM;
  92. st->codec.frame_rate_base= 0;
  93. }
  94. break;
  95. }
  96. if(is_audio){
  97. if(st->codec.sample_rate == 0){
  98. st->codec.codec_type = CODEC_TYPE_AUDIO;
  99. st->codec.channels = (flags&1)+1;
  100. if((flags >> 4) == 5)
  101. st->codec.sample_rate= 8000;
  102. else
  103. st->codec.sample_rate = (44100<<((flags>>2)&3))>>3;
  104. switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
  105. case 2: st->codec.codec_id = CODEC_ID_MP3; break;
  106. default:
  107. st->codec.codec_tag= (flags >> 4);
  108. }
  109. }
  110. }else{
  111. if(st->codec.frame_rate_base == 0){
  112. st->codec.codec_type = CODEC_TYPE_VIDEO;
  113. //guess the frame rate
  114. if(pts){
  115. st->codec.frame_rate_base=1;
  116. st->codec.frame_rate= (1000 + pts/2)/pts;
  117. }
  118. switch(flags & 0xF){
  119. case 2: st->codec.codec_id = CODEC_ID_FLV1; break;
  120. default:
  121. st->codec.codec_tag= flags & 0xF;
  122. }
  123. }
  124. }
  125. if (av_new_packet(pkt, size) < 0)
  126. return -EIO;
  127. ret = get_buffer(&s->pb, pkt->data, size);
  128. if (ret <= 0) {
  129. av_free_packet(pkt);
  130. return -EIO;
  131. }
  132. /* note: we need to modify the packet size here to handle the last
  133. packet */
  134. pkt->size = ret;
  135. pkt->pts = pts;
  136. pkt->stream_index = st->index;
  137. return ret;
  138. }
  139. static int flv_read_close(AVFormatContext *s)
  140. {
  141. return 0;
  142. }
  143. AVInputFormat flv_iformat = {
  144. "flv",
  145. "flv format",
  146. 0,
  147. flv_probe,
  148. flv_read_header,
  149. flv_read_packet,
  150. flv_read_close,
  151. .extensions = "flv",
  152. .value = CODEC_ID_FLV1,
  153. };
  154. int flvdec_init(void)
  155. {
  156. av_register_input_format(&flv_iformat);
  157. return 0;
  158. }