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.

157 lines
3.7KB

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