]> git.sesse.net Git - ffmpeg/blob - libavformat/mms.c
rename idroq.c to idroqdec.c
[ffmpeg] / libavformat / mms.c
1 /*
2  * MMS protocol common definitions.
3  * Copyright (c) 2006,2007 Ryan Martell
4  * Copyright (c) 2007 Björn Axelsson
5  * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 #include "mms.h"
24 #include "asf.h"
25 #include "libavutil/intreadwrite.h"
26
27 int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
28 {
29     char *pos;
30     int size_to_copy;
31     int remaining_size = mms->asf_header_size - mms->asf_header_read_size;
32     size_to_copy = FFMIN(size, remaining_size);
33     pos = mms->asf_header + mms->asf_header_read_size;
34     memcpy(buf, pos, size_to_copy);
35     if (mms->asf_header_read_size == mms->asf_header_size) {
36         av_freep(&mms->asf_header); // which contains asf header
37     }
38     mms->asf_header_read_size += size_to_copy;
39     return size_to_copy;
40 }
41
42 int ff_mms_read_data(MMSContext *mms, uint8_t *buf, const int size)
43 {
44     int read_size;
45     read_size = FFMIN(size, mms->remaining_in_len);
46     memcpy(buf, mms->read_in_ptr, read_size);
47     mms->remaining_in_len -= read_size;
48     mms->read_in_ptr      += read_size;
49     return read_size;
50 }
51
52 int ff_mms_asf_header_parser(MMSContext *mms)
53 {
54     uint8_t *p = mms->asf_header;
55     uint8_t *end;
56     int flags, stream_id;
57     mms->stream_num = 0;
58
59     if (mms->asf_header_size < sizeof(ff_asf_guid) * 2 + 22 ||
60         memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
61         av_log(NULL, AV_LOG_ERROR,
62                "Corrupt stream (invalid ASF header, size=%d)\n",
63                mms->asf_header_size);
64         return AVERROR_INVALIDDATA;
65     }
66
67     end = mms->asf_header + mms->asf_header_size;
68
69     p += sizeof(ff_asf_guid) + 14;
70     while(end - p >= sizeof(ff_asf_guid) + 8) {
71         uint64_t chunksize;
72         if (!memcmp(p, ff_asf_data_header, sizeof(ff_asf_guid))) {
73             chunksize = 50; // see Reference [2] section 5.1
74         } else {
75             chunksize = AV_RL64(p + sizeof(ff_asf_guid));
76         }
77         if (!chunksize || chunksize > end - p) {
78             av_log(NULL, AV_LOG_ERROR,
79                    "Corrupt stream (header chunksize %"PRId64" is invalid)\n",
80                    chunksize);
81             return AVERROR_INVALIDDATA;
82         }
83         if (!memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
84             /* read packet size */
85             if (end - p > sizeof(ff_asf_guid) * 2 + 68) {
86                 mms->asf_packet_len = AV_RL32(p + sizeof(ff_asf_guid) * 2 + 64);
87                 if (mms->asf_packet_len <= 0 || mms->asf_packet_len > sizeof(mms->in_buffer)) {
88                     av_log(NULL, AV_LOG_ERROR,
89                            "Corrupt stream (too large pkt_len %d)\n",
90                            mms->asf_packet_len);
91                     return AVERROR_INVALIDDATA;
92                 }
93             }
94         } else if (!memcmp(p, ff_asf_stream_header, sizeof(ff_asf_guid))) {
95             flags     = AV_RL16(p + sizeof(ff_asf_guid)*3 + 24);
96             stream_id = flags & 0x7F;
97             //The second condition is for checking CS_PKT_STREAM_ID_REQUEST packet size,
98             //we can calcuate the packet size by stream_num.
99             //Please see function send_stream_selection_request().
100             if (mms->stream_num < MAX_STREAMS &&
101                     46 + mms->stream_num * 6 < sizeof(mms->out_buffer)) {
102                 mms->streams = av_fast_realloc(mms->streams,
103                                    &mms->nb_streams_allocated,
104                                    (mms->stream_num + 1) * sizeof(MMSStream));
105                 mms->streams[mms->stream_num].id = stream_id;
106                 mms->stream_num++;
107             } else {
108                 av_log(NULL, AV_LOG_ERROR,
109                        "Corrupt stream (too many A/V streams)\n");
110                 return AVERROR_INVALIDDATA;
111             }
112         } else if (!memcmp(p, ff_asf_head1_guid, sizeof(ff_asf_guid))) {
113             chunksize = 46; // see references [2] section 3.4. This should be set 46.
114         }
115         p += chunksize;
116     }
117
118     return 0;
119 }