msnwc_tcp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2008 Ramiro Polla
  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. #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 && height==240) && !(width==160 && height==120))
  47. continue;
  48. bytestream += 2; // keyframe
  49. bytestream += 4; // size
  50. fourcc = bytestream_get_le32(&bytestream);
  51. if(fourcc != MKTAG('M', 'L', '2', '0'))
  52. continue;
  53. if(i) {
  54. if(i < 14) /* starts with SwitchBoard connection info */
  55. return AVPROBE_SCORE_MAX / 2;
  56. else /* starts in the middle of stream */
  57. return AVPROBE_SCORE_MAX / 3;
  58. } else {
  59. return AVPROBE_SCORE_MAX;
  60. }
  61. }
  62. return -1;
  63. }
  64. static int msnwc_tcp_read_header(AVFormatContext *ctx)
  65. {
  66. AVIOContext *pb = ctx->pb;
  67. AVCodecContext *codec;
  68. AVStream *st;
  69. st = avformat_new_stream(ctx, NULL);
  70. if(!st)
  71. return AVERROR(ENOMEM);
  72. codec = st->codec;
  73. codec->codec_type = AVMEDIA_TYPE_VIDEO;
  74. codec->codec_id = AV_CODEC_ID_MIMIC;
  75. codec->codec_tag = MKTAG('M', 'L', '2', '0');
  76. avpriv_set_pts_info(st, 32, 1, 1000);
  77. /* Some files start with "connected\r\n\r\n".
  78. * So skip until we find the first byte of struct size */
  79. while(avio_r8(pb) != HEADER_SIZE && !url_feof(pb));
  80. if(url_feof(pb)) {
  81. av_log(ctx, AV_LOG_ERROR, "Could not find valid start.\n");
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. static int msnwc_tcp_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  87. {
  88. AVIOContext *pb = ctx->pb;
  89. uint16_t keyframe;
  90. uint32_t size, timestamp;
  91. avio_skip(pb, 1); /* one byte has been read ahead */
  92. avio_skip(pb, 2);
  93. avio_skip(pb, 2);
  94. keyframe = avio_rl16(pb);
  95. size = avio_rl32(pb);
  96. avio_skip(pb, 4);
  97. avio_skip(pb, 4);
  98. timestamp = avio_rl32(pb);
  99. if(!size || av_get_packet(pb, pkt, size) != size)
  100. return -1;
  101. avio_skip(pb, 1); /* Read ahead one byte of struct size like read_header */
  102. pkt->pts = timestamp;
  103. pkt->dts = timestamp;
  104. pkt->stream_index = 0;
  105. /* Some aMsn generated videos (or was it Mercury Messenger?) don't set
  106. * this bit and rely on the codec to get keyframe information */
  107. if(keyframe&1)
  108. pkt->flags |= AV_PKT_FLAG_KEY;
  109. return HEADER_SIZE + size;
  110. }
  111. AVInputFormat ff_msnwc_tcp_demuxer = {
  112. .name = "msnwctcp",
  113. .long_name = NULL_IF_CONFIG_SMALL("MSN TCP Webcam stream"),
  114. .read_probe = msnwc_tcp_probe,
  115. .read_header = msnwc_tcp_read_header,
  116. .read_packet = msnwc_tcp_read_packet,
  117. };