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.

193 lines
5.4KB

  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. s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
  44. url_fskip(&s->pb, 4);
  45. flags = get_byte(&s->pb);
  46. offset = get_be32(&s->pb);
  47. url_fseek(&s->pb, offset, SEEK_SET);
  48. return 0;
  49. }
  50. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  51. {
  52. int ret, i, type, size, pts, flags, is_audio;
  53. AVStream *st = NULL;
  54. for(;;){
  55. url_fskip(&s->pb, 4); /* size of previous packet */
  56. type = get_byte(&s->pb);
  57. size = get_be24(&s->pb);
  58. pts = get_be24(&s->pb);
  59. // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
  60. if (url_feof(&s->pb))
  61. return AVERROR_IO;
  62. url_fskip(&s->pb, 4); /* reserved */
  63. flags = 0;
  64. if(size == 0)
  65. continue;
  66. if (type == 8) {
  67. is_audio=1;
  68. flags = get_byte(&s->pb);
  69. size--;
  70. } else if (type == 9) {
  71. is_audio=0;
  72. flags = get_byte(&s->pb);
  73. size--;
  74. } else {
  75. /* skip packet */
  76. av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  77. url_fskip(&s->pb, size);
  78. continue;
  79. }
  80. /* now find stream */
  81. for(i=0;i<s->nb_streams;i++) {
  82. st = s->streams[i];
  83. if (st->id == is_audio)
  84. break;
  85. }
  86. if(i == s->nb_streams){
  87. st = av_new_stream(s, is_audio);
  88. if (!st)
  89. return AVERROR_NOMEM;
  90. av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
  91. st->codec.frame_rate_base= 1;
  92. st->codec.frame_rate= 1000;
  93. }
  94. // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
  95. if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio))
  96. ||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio))
  97. || st->discard >= AVDISCARD_ALL
  98. ){
  99. url_fskip(&s->pb, size);
  100. continue;
  101. }
  102. break;
  103. }
  104. if(is_audio){
  105. if(st->codec.sample_rate == 0){
  106. st->codec.codec_type = CODEC_TYPE_AUDIO;
  107. st->codec.channels = (flags&1)+1;
  108. if((flags >> 4) == 5)
  109. st->codec.sample_rate= 8000;
  110. else
  111. st->codec.sample_rate = (44100<<((flags>>2)&3))>>3;
  112. switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
  113. case 0: if (flags&2) st->codec.codec_id = CODEC_ID_PCM_S16BE;
  114. else st->codec.codec_id = CODEC_ID_PCM_S8; break;
  115. case 1: st->codec.codec_id = CODEC_ID_ADPCM_SWF; break;
  116. case 2: st->codec.codec_id = CODEC_ID_MP3; break;
  117. // this is not listed at FLV but at SWF, strange...
  118. case 3: if (flags&2) st->codec.codec_id = CODEC_ID_PCM_S16LE;
  119. else st->codec.codec_id = CODEC_ID_PCM_S8; break;
  120. default:
  121. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
  122. st->codec.codec_tag= (flags >> 4);
  123. }
  124. st->codec.bits_per_sample = (flags & 2) ? 16 : 8;
  125. }
  126. }else{
  127. st->codec.codec_type = CODEC_TYPE_VIDEO;
  128. switch(flags & 0xF){
  129. case 2: st->codec.codec_id = CODEC_ID_FLV1; break;
  130. default:
  131. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
  132. st->codec.codec_tag= flags & 0xF;
  133. }
  134. }
  135. if (av_new_packet(pkt, size) < 0)
  136. return AVERROR_IO;
  137. ret = get_buffer(&s->pb, pkt->data, size);
  138. if (ret <= 0) {
  139. av_free_packet(pkt);
  140. return AVERROR_IO;
  141. }
  142. /* note: we need to modify the packet size here to handle the last
  143. packet */
  144. pkt->size = ret;
  145. pkt->pts = pts;
  146. pkt->stream_index = st->index;
  147. if (is_audio || ((flags >> 4)==1))
  148. pkt->flags |= PKT_FLAG_KEY;
  149. return ret;
  150. }
  151. static int flv_read_close(AVFormatContext *s)
  152. {
  153. return 0;
  154. }
  155. AVInputFormat flv_iformat = {
  156. "flv",
  157. "flv format",
  158. 0,
  159. flv_probe,
  160. flv_read_header,
  161. flv_read_packet,
  162. flv_read_close,
  163. .extensions = "flv",
  164. .value = CODEC_ID_FLV1,
  165. };
  166. int flvdec_init(void)
  167. {
  168. av_register_input_format(&flv_iformat);
  169. return 0;
  170. }