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.

146 lines
4.2KB

  1. /*
  2. * Copyright (C) 2008 Ramiro Polla
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. #include "internal.h"
  23. #define HEADER_SIZE 24
  24. /*
  25. * Header structure:
  26. * uint16_t ss; // struct size
  27. * uint16_t width; // frame width
  28. * uint16_t height; // frame height
  29. * uint16_t ff; // keyframe + some other info(???)
  30. * uint32_t size; // size of data
  31. * uint32_t fourcc; // ML20
  32. * uint32_t u3; // ?
  33. * uint32_t ts; // time
  34. */
  35. static int msnwc_tcp_probe(AVProbeData *p)
  36. {
  37. int i;
  38. for (i = 0; i + HEADER_SIZE <= p->buf_size; i++) {
  39. uint16_t width, height;
  40. uint32_t fourcc;
  41. const uint8_t *bytestream = p->buf + i;
  42. if (bytestream_get_le16(&bytestream) != HEADER_SIZE)
  43. continue;
  44. width = bytestream_get_le16(&bytestream);
  45. height = bytestream_get_le16(&bytestream);
  46. if (!(width == 320 &&
  47. height == 240) && !(width == 160 && height == 120))
  48. continue;
  49. bytestream += 2; // keyframe
  50. bytestream += 4; // size
  51. fourcc = bytestream_get_le32(&bytestream);
  52. if (fourcc != MKTAG('M', 'L', '2', '0'))
  53. continue;
  54. if (i) {
  55. if (i < 14) /* starts with SwitchBoard connection info */
  56. return AVPROBE_SCORE_MAX / 2;
  57. else /* starts in the middle of stream */
  58. return AVPROBE_SCORE_MAX / 3;
  59. } else {
  60. return AVPROBE_SCORE_MAX;
  61. }
  62. }
  63. return 0;
  64. }
  65. static int msnwc_tcp_read_header(AVFormatContext *ctx)
  66. {
  67. AVIOContext *pb = ctx->pb;
  68. AVCodecParameters *par;
  69. AVStream *st;
  70. st = avformat_new_stream(ctx, NULL);
  71. if (!st)
  72. return AVERROR(ENOMEM);
  73. par = st->codecpar;
  74. par->codec_type = AVMEDIA_TYPE_VIDEO;
  75. par->codec_id = AV_CODEC_ID_MIMIC;
  76. par->codec_tag = MKTAG('M', 'L', '2', '0');
  77. avpriv_set_pts_info(st, 32, 1, 1000);
  78. /* Some files start with "connected\r\n\r\n".
  79. * So skip until we find the first byte of struct size */
  80. while (avio_r8(pb) != HEADER_SIZE && !pb->eof_reached) ;
  81. if (pb->eof_reached) {
  82. av_log(ctx, AV_LOG_ERROR, "Could not find valid start.");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. return 0;
  86. }
  87. static int msnwc_tcp_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  88. {
  89. AVIOContext *pb = ctx->pb;
  90. uint16_t keyframe;
  91. uint32_t size, timestamp;
  92. int ret;
  93. avio_skip(pb, 1); /* one byte has been read ahead */
  94. avio_skip(pb, 2);
  95. avio_skip(pb, 2);
  96. keyframe = avio_rl16(pb);
  97. size = avio_rl32(pb);
  98. avio_skip(pb, 4);
  99. avio_skip(pb, 4);
  100. timestamp = avio_rl32(pb);
  101. if (!size)
  102. return AVERROR_INVALIDDATA;
  103. if ((ret = av_get_packet(pb, pkt, size)) < 0)
  104. return ret;
  105. avio_skip(pb, 1); /* Read ahead one byte of struct size like read_header */
  106. pkt->pts = timestamp;
  107. pkt->dts = timestamp;
  108. pkt->stream_index = 0;
  109. /* Some aMsn generated videos (or was it Mercury Messenger?) don't set
  110. * this bit and rely on the codec to get keyframe information */
  111. if (keyframe & 1)
  112. pkt->flags |= AV_PKT_FLAG_KEY;
  113. return HEADER_SIZE + size;
  114. }
  115. AVInputFormat ff_msnwc_tcp_demuxer = {
  116. .name = "msnwctcp",
  117. .long_name = NULL_IF_CONFIG_SMALL("MSN TCP Webcam stream"),
  118. .read_probe = msnwc_tcp_probe,
  119. .read_header = msnwc_tcp_read_header,
  120. .read_packet = msnwc_tcp_read_packet,
  121. };