• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/mms.c

Go to the documentation of this file.
00001 /*
00002  * MMS protocol common definitions.
00003  * Copyright (c) 2006,2007 Ryan Martell
00004  * Copyright (c) 2007 Björn Axelsson
00005  * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 #include "mms.h"
00024 #include "asf.h"
00025 #include "libavutil/intreadwrite.h"
00026 
00027 #if FF_API_MAX_STREAMS
00028 #define MMS_MAX_STREAMS MAX_STREAMS
00029 #else
00030 #define MMS_MAX_STREAMS 256    
00031 #endif
00032 
00033 int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
00034 {
00035     char *pos;
00036     int size_to_copy;
00037     int remaining_size = mms->asf_header_size - mms->asf_header_read_size;
00038     size_to_copy = FFMIN(size, remaining_size);
00039     pos = mms->asf_header + mms->asf_header_read_size;
00040     memcpy(buf, pos, size_to_copy);
00041     if (mms->asf_header_read_size == mms->asf_header_size) {
00042         av_freep(&mms->asf_header); // which contains asf header
00043     }
00044     mms->asf_header_read_size += size_to_copy;
00045     return size_to_copy;
00046 }
00047 
00048 int ff_mms_read_data(MMSContext *mms, uint8_t *buf, const int size)
00049 {
00050     int read_size;
00051     read_size = FFMIN(size, mms->remaining_in_len);
00052     memcpy(buf, mms->read_in_ptr, read_size);
00053     mms->remaining_in_len -= read_size;
00054     mms->read_in_ptr      += read_size;
00055     return read_size;
00056 }
00057 
00058 int ff_mms_asf_header_parser(MMSContext *mms)
00059 {
00060     uint8_t *p = mms->asf_header;
00061     uint8_t *end;
00062     int flags, stream_id;
00063     mms->stream_num = 0;
00064 
00065     if (mms->asf_header_size < sizeof(ff_asf_guid) * 2 + 22 ||
00066         memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
00067         av_log(NULL, AV_LOG_ERROR,
00068                "Corrupt stream (invalid ASF header, size=%d)\n",
00069                mms->asf_header_size);
00070         return AVERROR_INVALIDDATA;
00071     }
00072 
00073     end = mms->asf_header + mms->asf_header_size;
00074 
00075     p += sizeof(ff_asf_guid) + 14;
00076     while(end - p >= sizeof(ff_asf_guid) + 8) {
00077         uint64_t chunksize;
00078         if (!memcmp(p, ff_asf_data_header, sizeof(ff_asf_guid))) {
00079             chunksize = 50; // see Reference [2] section 5.1
00080         } else {
00081             chunksize = AV_RL64(p + sizeof(ff_asf_guid));
00082         }
00083         if (!chunksize || chunksize > end - p) {
00084             av_log(NULL, AV_LOG_ERROR,
00085                    "Corrupt stream (header chunksize %"PRId64" is invalid)\n",
00086                    chunksize);
00087             return AVERROR_INVALIDDATA;
00088         }
00089         if (!memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
00090             /* read packet size */
00091             if (end - p > sizeof(ff_asf_guid) * 2 + 68) {
00092                 mms->asf_packet_len = AV_RL32(p + sizeof(ff_asf_guid) * 2 + 64);
00093                 if (mms->asf_packet_len <= 0 || mms->asf_packet_len > sizeof(mms->in_buffer)) {
00094                     av_log(NULL, AV_LOG_ERROR,
00095                            "Corrupt stream (too large pkt_len %d)\n",
00096                            mms->asf_packet_len);
00097                     return AVERROR_INVALIDDATA;
00098                 }
00099             }
00100         } else if (!memcmp(p, ff_asf_stream_header, sizeof(ff_asf_guid))) {
00101             flags     = AV_RL16(p + sizeof(ff_asf_guid)*3 + 24);
00102             stream_id = flags & 0x7F;
00103             //The second condition is for checking CS_PKT_STREAM_ID_REQUEST packet size,
00104             //we can calcuate the packet size by stream_num.
00105             //Please see function send_stream_selection_request().
00106             if (mms->stream_num < MMS_MAX_STREAMS &&
00107                     46 + mms->stream_num * 6 < sizeof(mms->out_buffer)) {
00108                 mms->streams = av_fast_realloc(mms->streams,
00109                                    &mms->nb_streams_allocated,
00110                                    (mms->stream_num + 1) * sizeof(MMSStream));
00111                 mms->streams[mms->stream_num].id = stream_id;
00112                 mms->stream_num++;
00113             } else {
00114                 av_log(NULL, AV_LOG_ERROR,
00115                        "Corrupt stream (too many A/V streams)\n");
00116                 return AVERROR_INVALIDDATA;
00117             }
00118         } else if (!memcmp(p, ff_asf_ext_stream_header, sizeof(ff_asf_guid))) {
00119             if (end - p >= 88) {
00120                 int stream_count = AV_RL16(p + 84), ext_len_count = AV_RL16(p + 86);
00121                 uint64_t skip_bytes = 88;
00122                 while (stream_count--) {
00123                     if (end - p < skip_bytes + 4) {
00124                         av_log(NULL, AV_LOG_ERROR,
00125                                "Corrupt stream (next stream name length is not in the buffer)\n");
00126                         return AVERROR_INVALIDDATA;
00127                     }
00128                     skip_bytes += 4 + AV_RL16(p + skip_bytes + 2);
00129                 }
00130                 while (ext_len_count--) {
00131                     if (end - p < skip_bytes + 22) {
00132                         av_log(NULL, AV_LOG_ERROR,
00133                                "Corrupt stream (next extension system info length is not in the buffer)\n");
00134                         return AVERROR_INVALIDDATA;
00135                     }
00136                     skip_bytes += 22 + AV_RL32(p + skip_bytes + 18);
00137                 }
00138                 if (end - p < skip_bytes) {
00139                     av_log(NULL, AV_LOG_ERROR,
00140                            "Corrupt stream (the last extension system info length is invalid)\n");
00141                     return AVERROR_INVALIDDATA;
00142                 }
00143                 if (chunksize - skip_bytes > 24)
00144                     chunksize = skip_bytes;
00145             }
00146         } else if (!memcmp(p, ff_asf_head1_guid, sizeof(ff_asf_guid))) {
00147             chunksize = 46; // see references [2] section 3.4. This should be set 46.
00148         }
00149         p += chunksize;
00150     }
00151 
00152     return 0;
00153 }

Generated on Fri Feb 22 2013 07:24:31 for FFmpeg by  doxygen 1.7.1