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.

164 lines
4.1KB

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