annotate ffmpeg/libavformat/msnwc_tcp.c @ 13:844d341cf643 tip

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