FFmpeg
mms.c
Go to the documentation of this file.
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 #include "libavutil/mem.h"
27 
28 #define MMS_MAX_STREAMS 256 /**< arbitrary sanity check value */
29 
30 int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
31 {
32  char *pos;
33  int size_to_copy;
34  int remaining_size = mms->asf_header_size - mms->asf_header_read_size;
35  size_to_copy = FFMIN(size, remaining_size);
36  pos = mms->asf_header + mms->asf_header_read_size;
37  memcpy(buf, pos, size_to_copy);
38  if (mms->asf_header_read_size == mms->asf_header_size) {
39  av_freep(&mms->asf_header); // which contains asf header
40  }
41  mms->asf_header_read_size += size_to_copy;
42  return size_to_copy;
43 }
44 
45 int ff_mms_read_data(MMSContext *mms, uint8_t *buf, const int size)
46 {
47  int read_size;
48  read_size = FFMIN(size, mms->remaining_in_len);
49  memcpy(buf, mms->read_in_ptr, read_size);
50  mms->remaining_in_len -= read_size;
51  mms->read_in_ptr += read_size;
52  return read_size;
53 }
54 
56 {
57  uint8_t *p = mms->asf_header;
58  uint8_t *end;
59  int flags, stream_id;
60  mms->stream_num = 0;
61 
62  if (mms->asf_header_size < sizeof(ff_asf_guid) * 2 + 22 ||
63  memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
65  "Corrupt stream (invalid ASF header, size=%d)\n",
66  mms->asf_header_size);
67  return AVERROR_INVALIDDATA;
68  }
69 
70  end = mms->asf_header + mms->asf_header_size;
71 
72  p += sizeof(ff_asf_guid) + 14;
73  while(end - p >= sizeof(ff_asf_guid) + 8) {
74  uint64_t chunksize;
75  if (!memcmp(p, ff_asf_data_header, sizeof(ff_asf_guid))) {
76  chunksize = 50; // see Reference [2] section 5.1
77  } else {
78  chunksize = AV_RL64(p + sizeof(ff_asf_guid));
79  }
80  if (!chunksize || chunksize > end - p) {
82  "Corrupt stream (header chunksize %"PRId64" is invalid)\n",
83  chunksize);
84  return AVERROR_INVALIDDATA;
85  }
86  if (!memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
87  /* read packet size */
88  if (end - p > sizeof(ff_asf_guid) * 2 + 68) {
89  mms->asf_packet_len = AV_RL32(p + sizeof(ff_asf_guid) * 2 + 64);
90  if (mms->asf_packet_len <= 0 || mms->asf_packet_len > sizeof(mms->in_buffer)) {
92  "Corrupt stream (too large pkt_len %d)\n",
93  mms->asf_packet_len);
94  return AVERROR_INVALIDDATA;
95  }
96  }
97  } else if (!memcmp(p, ff_asf_stream_header, sizeof(ff_asf_guid))) {
98  if (end - p >= (sizeof(ff_asf_guid) * 3 + 26)) {
99  flags = AV_RL16(p + sizeof(ff_asf_guid)*3 + 24);
100  stream_id = flags & 0x7F;
101  //The second condition is for checking CS_PKT_STREAM_ID_REQUEST packet size,
102  //we can calculate the packet size by stream_num.
103  //Please see function send_stream_selection_request().
104  if (mms->stream_num < MMS_MAX_STREAMS &&
105  46 + mms->stream_num * 6 < sizeof(mms->out_buffer)) {
106  mms->streams = av_fast_realloc(mms->streams,
107  &mms->nb_streams_allocated,
108  (mms->stream_num + 1) * sizeof(MMSStream));
109  if (!mms->streams)
110  return AVERROR(ENOMEM);
111  mms->streams[mms->stream_num].id = stream_id;
112  mms->stream_num++;
113  } else {
114  av_log(mms->mms_hd, AV_LOG_ERROR,
115  "Corrupt stream (too many A/V streams)\n");
116  return AVERROR_INVALIDDATA;
117  }
118  }
119  } else if (!memcmp(p, ff_asf_ext_stream_header, sizeof(ff_asf_guid))) {
120  if (end - p >= 88) {
121  int stream_count = AV_RL16(p + 84), ext_len_count = AV_RL16(p + 86);
122  uint64_t skip_bytes = 88;
123  while (stream_count--) {
124  if (end - p < skip_bytes + 4) {
125  av_log(mms->mms_hd, AV_LOG_ERROR,
126  "Corrupt stream (next stream name length is not in the buffer)\n");
127  return AVERROR_INVALIDDATA;
128  }
129  skip_bytes += 4 + AV_RL16(p + skip_bytes + 2);
130  }
131  while (ext_len_count--) {
132  if (end - p < skip_bytes + 22) {
133  av_log(mms->mms_hd, AV_LOG_ERROR,
134  "Corrupt stream (next extension system info length is not in the buffer)\n");
135  return AVERROR_INVALIDDATA;
136  }
137  skip_bytes += 22 + AV_RL32(p + skip_bytes + 18);
138  }
139  if (end - p < skip_bytes) {
140  av_log(mms->mms_hd, AV_LOG_ERROR,
141  "Corrupt stream (the last extension system info length is invalid)\n");
142  return AVERROR_INVALIDDATA;
143  }
144  if (chunksize - skip_bytes > 24)
145  chunksize = skip_bytes;
146  }
147  } else if (!memcmp(p, ff_asf_head1_guid, sizeof(ff_asf_guid))) {
148  chunksize = 46; // see references [2] section 3.4. This should be set 46.
149  if (chunksize > end - p) {
150  av_log(mms->mms_hd, AV_LOG_ERROR,
151  "Corrupt stream (header chunksize %"PRId64" is invalid)\n",
152  chunksize);
153  return AVERROR_INVALIDDATA;
154  }
155  }
156  p += chunksize;
157  }
158 
159  return 0;
160 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
MMSContext::asf_header
uint8_t * asf_header
Internal handling of the ASF header.
Definition: mms.h:49
MMSContext::asf_packet_len
int asf_packet_len
Definition: mms.h:52
MMSContext::stream_num
int stream_num
stream numbers.
Definition: mms.h:56
mms.h
MMSContext
Definition: mms.h:30
ff_asf_ext_stream_header
const ff_asf_guid ff_asf_ext_stream_header
Definition: asf_tags.c:35
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
ff_mms_asf_header_parser
int ff_mms_asf_header_parser(MMSContext *mms)
Definition: mms.c:55
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
intreadwrite.h
ff_mms_read_header
int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
Definition: mms.c:30
MMS_MAX_STREAMS
#define MMS_MAX_STREAMS
arbitrary sanity check value
Definition: mms.c:28
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
MMSContext::asf_header_read_size
int asf_header_read_size
Definition: mms.h:53
MMSContext::in_buffer
uint8_t in_buffer[65536]
Buffer for incoming packets.
Definition: mms.h:42
ff_asf_guid
uint8_t ff_asf_guid[16]
Definition: riff.h:96
ff_asf_head1_guid
const ff_asf_guid ff_asf_head1_guid
Definition: asf_tags.c:78
MMSContext::out_buffer
uint8_t out_buffer[512]
Buffer for outgoing packet.
Definition: mms.h:37
ff_asf_data_header
const ff_asf_guid ff_asf_data_header
Definition: asf_tags.c:74
MMSStream::id
int id
Definition: mms.h:27
ff_mms_read_data
int ff_mms_read_data(MMSContext *mms, uint8_t *buf, const int size)
Definition: mms.c:45
ff_asf_file_header
const ff_asf_guid ff_asf_file_header
Definition: asf_tags.c:27
MMSContext::remaining_in_len
int remaining_in_len
Reading length from incoming buffer.
Definition: mms.h:44
size
int size
Definition: twinvq_data.h:10344
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_asf_stream_header
const ff_asf_guid ff_asf_stream_header
Definition: asf_tags.c:31
pos
unsigned int pos
Definition: spdifenc.c:414
asf.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
MMSContext::asf_header_size
int asf_header_size
Size of stored ASF header.
Definition: mms.h:50
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
skip_bytes
static const av_unused uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
Definition: cabac_functions.h:203
MMSStream
Definition: mms.h:26
ff_asf_header
const ff_asf_guid ff_asf_header
Definition: asf_tags.c:23
MMSContext::mms_hd
URLContext * mms_hd
TCP connection handle.
Definition: mms.h:31
MMSContext::nb_streams_allocated
unsigned int nb_streams_allocated
allocated size of streams
Definition: mms.h:57
MMSContext::streams
MMSStream * streams
Definition: mms.h:32
MMSContext::read_in_ptr
uint8_t * read_in_ptr
Pointer for reading from incoming buffer.
Definition: mms.h:43