-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoderStream.cs
More file actions
325 lines (258 loc) · 8.79 KB
/
Copy pathDecoderStream.cs
File metadata and controls
325 lines (258 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#region LGPL License
//
// DecoderStream.cs
//
// Author:
// Justin Cherniak ([email protected]
//
// Copyright (C) 2008 Justin Cherniak
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License version
// 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
#endregion
using System;
using System.Diagnostics;
using System.IO;
using FFmpegSharp.Interop;
using FFmpegSharp.Interop.Codec;
using FFmpegSharp.Interop.Format;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace FFmpegSharp
{
public unsafe abstract class DecoderStream : Stream, IMediaStream
{
#region Fields
protected MediaFile m_file;
protected AVCodecContext m_avCodecCtx;
protected AVStream m_avStream;
protected uint m_streamIdx;
protected bool m_disposed;
protected byte[] m_buffer = null;
protected int m_bufferUsedSize = 0;
protected long m_position;
private bool m_codecOpen = false;
private TimeSpan m_timestamp = TimeSpan.Zero;
private Queue<AVPacket> m_packetQueue = new Queue<AVPacket>();
private long m_rawPts;
private long m_rawDts;
#endregion
#region Properties
internal Queue<AVPacket> PacketQueue
{
get { return m_packetQueue; }
}
public override long Length
{
get { return (long)(Math.Ceiling(Duration.TotalSeconds * UncompressedBytesPerSecond)); }
}
public TimeSpan Duration
{
get { return m_file.Duration; }
}
public TimeSpan Timestamp
{
get { return m_timestamp; }
}
public abstract int UncompressedBytesPerSecond { get; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Position
{
get { return m_position; }
set { this.Seek(value, SeekOrigin.Begin); }
}
public long Pts
{
get { return m_rawPts; }
}
public long Dts
{
get { return m_rawDts; }
}
#endregion
#region Constructors / destructor
public DecoderStream(MediaFile file, AVStream* stream)
{
// Initialize instance variables
m_disposed = false;
m_position = m_bufferUsedSize = 0;
m_file = file;
m_avStream = *stream;
m_avCodecCtx = *m_avStream.codec;
// Open the decoding codec
AVCodec* avCodec = FFmpeg.avcodec_find_decoder(m_avCodecCtx.codec_id);
if (avCodec == null)
throw new DecoderException("No decoder found");
if (FFmpeg.avcodec_open(ref m_avCodecCtx, avCodec) < 0)
throw new DecoderException("Error opening codec");
m_codecOpen = true;
}
public DecoderStream(MediaFile file, ref AVStream stream)
{
// Initialize instance variables
m_disposed = false;
m_position = m_bufferUsedSize = 0;
m_file = file;
m_avStream = stream;
m_avCodecCtx = *m_avStream.codec;
// Open the decoding codec
AVCodec* avCodec = FFmpeg.avcodec_find_decoder(m_avCodecCtx.codec_id);
if (avCodec == null)
throw new DecoderException("No decoder found");
if (FFmpeg.avcodec_open(ref m_avCodecCtx, avCodec) < 0)
throw new DecoderException("Error opening codec");
m_codecOpen = true;
}
~DecoderStream()
{
Dispose(false);
}
#endregion
#region Methods
public override int Read(byte[] buffer, int offset, int count)
{
int totalRead = 0;
while (count > 0)
{
if (m_bufferUsedSize == 0)
{
try
{
ReadNextPacket();
}
catch (EndOfStreamException)
{
break;
}
}
if (m_bufferUsedSize > 0)
{
int len = Math.Min(count, m_bufferUsedSize);
Utils.CopyArray(m_buffer, 0, buffer, offset, len);
m_bufferUsedSize -= len;
offset += len;
count -= len;
totalRead += len;
if (m_bufferUsedSize > 0)
Utils.CopyArray(m_buffer, len, m_buffer, 0, m_bufferUsedSize);
}
Debug.Assert(m_bufferUsedSize >= 0);
}
m_position += totalRead;
m_timestamp += TimeSpan.FromSeconds(totalRead / UncompressedBytesPerSecond);
if (totalRead == 0)
return -1;
return totalRead;
}
private void ReadNextPacket()
{
if (m_position >= Length)
throw new System.IO.EndOfStreamException();
if (m_disposed)
return;
AVPacket packet;
bool retry = false;
do
{
while (PacketQueue.Count == 0)
m_file.EnqueueNextPacket();
packet = PacketQueue.Dequeue();
try
{
retry = !DecodePacket(ref packet);
}
finally
{
FFmpeg.av_free_packet(ref packet);
}
} while (retry);
m_rawDts = packet.dts;
m_rawPts = packet.pts;
}
protected abstract bool DecodePacket(ref AVPacket packet);
public override long Seek(long offset, SeekOrigin origin)
{
TimeSpan position = Seek(TimeSpan.FromSeconds(offset / UncompressedBytesPerSecond), origin);
return (long)(position.TotalSeconds * UncompressedBytesPerSecond);
}
public TimeSpan Seek(TimeSpan offset, SeekOrigin origin)
{
TimeSpan newPosition = TimeSpan.Zero;
switch (origin)
{
case SeekOrigin.Begin:
newPosition = offset;
break;
case SeekOrigin.Current:
newPosition = offset + Timestamp;
break;
case SeekOrigin.End:
newPosition = Duration - Timestamp;
break;
}
if (newPosition < TimeSpan.Zero)
newPosition = TimeSpan.Zero;
else if (newPosition > Duration)
newPosition = Duration;
bool backward = newPosition > Timestamp;
AVSEEK_FLAG flags = AVSEEK_FLAG.Any | (backward ? AVSEEK_FLAG.Backward: 0);
long position = (long)(newPosition.TotalSeconds * FFmpeg.AV_TIME_BASE);
if (FFmpeg.av_seek_frame(ref m_file.FormatContext, -1, position, flags) < 0)
{
m_position = Length;
m_timestamp = Duration;
return Duration;
}
FFmpeg.avcodec_flush_buffers(ref m_avCodecCtx);
m_timestamp = offset;
m_position = (long)(m_timestamp.TotalSeconds * UncompressedBytesPerSecond);
return m_timestamp;
}
public override void Flush()
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
protected override void Dispose(bool disposing)
{
if (!m_disposed)
{
m_disposed = true;
// BROKEN: Throwing exception on video codec close with MPEG4
// if (m_codecOpen)
// FFmpeg.avcodec_close(ref m_avCodecCtx);
}
}
#endregion
}
}