msnwc_tcp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2008 Ramiro Polla <ramiro@lisha.ufsc.br>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavcodec/bytestream.h"
  21. #include "avformat.h"
  22. #define HEADER_SIZE 24
  23. /*
  24. * Header structure:
  25. * uint16_t ss; // struct size
  26. * uint16_t width; // frame width
  27. * uint16_t height; // frame height
  28. * uint16_t ff; // keyframe + some other info(???)
  29. * uint32_t size; // size of data
  30. * uint32_t fourcc; // ML20
  31. * uint32_t u3; // ?
  32. * uint32_t ts; // time
  33. */
  34. static int msnwc_tcp_probe(AVProbeData *p)
  35. {
  36. int i;
  37. for(i = 0 ; i + HEADER_SIZE <= p->buf_size ; i++) {
  38. uint16_t width, height;
  39. uint32_t fourcc;
  40. const uint8_t *bytestream = p->buf+i;
  41. if(bytestream_get_le16(&bytestream) != HEADER_SIZE)
  42. continue;
  43. width = bytestream_get_le16(&bytestream);
  44. height = bytestream_get_le16(&bytestream);
  45. if(!(width==320 && height==240) && !(width==160 && height==120))
  46. continue;
  47. bytestream += 2; // keyframe
  48. bytestream += 4; // size
  49. fourcc = bytestream_get_le32(&bytestream);
  50. if(fourcc != MKTAG('M', 'L', '2', '0'))
  51. continue;
  52. if(i) {
  53. if(i < 14) /* starts with SwitchBoard connection info */
  54. return AVPROBE_SCORE_MAX / 2;
  55. else /* starts in the middle of stream */
  56. return AVPROBE_SCORE_MAX / 3;
  57. } else {
  58. return AVPROBE_SCORE_MAX;
  59. }
  60. }
  61. return -1;
  62. }
  63. static int msnwc_tcp_read_header(AVFormatContext *ctx, AVFormatParameters *ap)
  64. {
  65. ByteIOContext *pb = ctx->pb;
  66. AVCodecContext *codec;
  67. AVStream *st;
  68. st = av_new_stream(ctx, 0);
  69. if(!st)
  70. return AVERROR_NOMEM;
  71. codec = st->codec;
  72. codec->codec_type = CODEC_TYPE_VIDEO;
  73. codec->codec_id = CODEC_ID_MIMIC;
  74. codec->codec_tag = MKTAG('M', 'L', '2', '0');
  75. av_set_pts_info(st, 32, 1, 1000);
  76. /* Some files start with "connected\r\n\r\n".
  77. * So skip until we find the first byte of struct size */
  78. while(get_byte(pb) != HEADER_SIZE && !url_feof(pb));
  79. if(url_feof(pb)) {
  80. av_log(ctx, AV_LOG_ERROR, "Could not find valid start.");
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. static int msnwc_tcp_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  86. {
  87. ByteIOContext *pb = ctx->pb;
  88. uint16_t keyframe;
  89. uint32_t size, timestamp;
  90. url_fskip(pb, 1); /* one byte has been read ahead */
  91. url_fskip(pb, 2);
  92. url_fskip(pb, 2);
  93. keyframe = get_le16(pb);
  94. size = get_le32(pb);
  95. url_fskip(pb, 4);
  96. url_fskip(pb, 4);
  97. timestamp = get_le32(pb);
  98. if(!size || av_get_packet(pb, pkt, size) != size)
  99. return -1;
  100. url_fskip(pb, 1); /* Read ahead one byte of struct size like read_header */
  101. pkt->pts = timestamp;
  102. pkt->dts = timestamp;
  103. pkt->stream_index = 0;
  104. /* Some aMsn generated videos (or was it Mercury Messenger?) don't set
  105. * this bit and rely on the codec to get keyframe information */
  106. if(keyframe&1)
  107. pkt->flags |= PKT_FLAG_KEY;
  108. return HEADER_SIZE + size;
  109. }
  110. AVInputFormat msnwc_tcp_demuxer = {
  111. "msnwctcp",
  112. NULL_IF_CONFIG_SMALL("MSN TCP Webcam stream"),
  113. 0,
  114. msnwc_tcp_probe,
  115. msnwc_tcp_read_header,
  116. msnwc_tcp_read_packet,
  117. };