forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.pyx
More file actions
164 lines (129 loc) · 5.04 KB
/
Copy pathpacket.pyx
File metadata and controls
164 lines (129 loc) · 5.04 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
cimport libav as lib
from av.bytesource cimport bytesource
from av.utils cimport avrational_to_faction, err_check
cdef class Packet(Buffer):
"""A packet of encoded data within a :class:`~av.format.Stream`.
This may, or may not include a complete object within a stream.
:meth:`decode` must be called to extract encoded data.
"""
def __cinit__(self, input=None):
with nogil:
lib.av_init_packet(&self.struct)
def __init__(self, input=None):
cdef size_t size = 0
cdef ByteSource source = None
if input is None:
return
if isinstance(input, (int, long)):
size = input
else:
source = bytesource(input)
size = source.length
if size:
err_check(lib.av_new_packet(&self.struct, size))
if source is not None:
self.update_buffer(source)
# TODO: Hold onto the source, and copy its pointer
# instead of its data.
#self.source = source
def __dealloc__(self):
with nogil:
lib.av_free_packet(&self.struct)
def __repr__(self):
return '<av.%s of #%d, dts=%s, pts=%s; %s bytes at 0x%x>' % (
self.__class__.__name__,
self.stream.index if self.stream else 0,
self.dts,
self.pts,
self.struct.size,
id(self),
)
# Buffer protocol.
cdef size_t _buffer_size(self):
return self.struct.size
cdef void* _buffer_ptr(self):
return self.struct.data
#cdef bint _buffer_writable(self):
# return self.source is None
def copy(self):
raise NotImplementedError()
cdef Packet copy = Packet()
# copy.struct.size = self.struct.size
# copy.struct.data = NULL
return copy
cdef int _retime(self, lib.AVRational src, lib.AVRational dst) except -1:
if not src.num:
src = self._time_base
if not dst.num:
dst = self._time_base
if not src.num:
raise ValueError('No src time_base.')
if not dst.num:
raise ValueError('No dst time_base.')
if self.struct.pts != lib.AV_NOPTS_VALUE:
self.struct.pts = lib.av_rescale_q(
self.struct.pts,
src, dst
)
if self.struct.dts != lib.AV_NOPTS_VALUE:
self.struct.dts = lib.av_rescale_q(
self.struct.dts,
src, dst
)
if self.struct.duration > 0:
self.struct.duration = lib.av_rescale_q(
self.struct.duration,
src, dst
)
self._time_base = dst
return 0 # Just for exception.
def decode(self, count=0):
"""Decode the data in this packet into a list of Frames."""
return self.stream.decode(self, count)
def decode_one(self):
"""Decode the first frame from this packet.
Returns ``None`` if there is no frame."""
res = self.stream.decode(self, count=1)
return res[0] if res else None
# Looks circular, but isn't. Silly Cython.
property stream:
def __get__(self):
return self.stream
def __set__(self, Stream value):
# Rescale times.
cdef lib.AVStream *old = self.stream._stream
cdef lib.AVStream *new = value._stream
if self.struct.pts != lib.AV_NOPTS_VALUE:
self.struct.pts = lib.av_rescale_q_rnd(self.struct.pts, old.time_base, new.time_base, lib.AV_ROUND_NEAR_INF)
if self.struct.dts != lib.AV_NOPTS_VALUE:
self.struct.dts = lib.av_rescale_q_rnd(self.struct.dts, old.time_base, new.time_base, lib.AV_ROUND_NEAR_INF)
self.struct.duration = lib.av_rescale_q(self.struct.duration, old.time_base, new.time_base)
self.stream = value
self.struct.stream_index = value.index
property time_base:
def __get__(self):
return avrational_to_faction(&self._time_base)
def __set__(self, value):
self._time_base.num = value.numerator
self._time_base.den = value.denominator
property pts:
def __get__(self): return None if self.struct.pts == lib.AV_NOPTS_VALUE else self.struct.pts
def __set__(self, v):
if v is None:
self.struct.pts = lib.AV_NOPTS_VALUE
else:
self.struct.pts = v
property dts:
def __get__(self):
return None if self.struct.dts == lib.AV_NOPTS_VALUE else self.struct.dts
def __set__(self, v):
if v is None:
self.struct.dts = lib.AV_NOPTS_VALUE
else:
self.struct.dts = v
property pos:
def __get__(self): return None if self.struct.pos == -1 else self.struct.pos
property size:
def __get__(self): return self.struct.size
property duration:
def __get__(self): return None if self.struct.duration == lib.AV_NOPTS_VALUE else self.struct.duration