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.

175 lines
4.5KB

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