forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.pyx
More file actions
321 lines (245 loc) · 10.9 KB
/
Copy pathcontext.pyx
File metadata and controls
321 lines (245 loc) · 10.9 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
from libc.stdint cimport uint8_t, int64_t
from libc.string cimport memcpy
from libc.stdlib cimport malloc, realloc, free
from cpython cimport PyWeakref_NewRef
cimport libav as lib
from av.codec.codec cimport Codec, wrap_codec
from av.packet cimport Packet
from av.utils cimport err_check, avdict_to_dict, avrational_to_faction, to_avrational, media_type_to_string
from av.dictionary cimport _Dictionary
from av.dictionary import Dictionary
cdef object _cinit_sentinel = object()
cdef CodecContext wrap_codec_context(lib.AVCodecContext *c_ctx, lib.AVCodec *c_codec, ContainerProxy container):
"""Build an av.CodecContext for an existing AVCodecContext."""
cdef CodecContext py_ctx
# TODO: This.
if c_ctx.codec_type == lib.AVMEDIA_TYPE_VIDEO:
from av.video.codeccontext import VideoCodecContext
py_ctx = VideoCodecContext(_cinit_sentinel)
elif c_ctx.codec_type == lib.AVMEDIA_TYPE_AUDIO:
from av.audio.codeccontext import AudioCodecContext
py_ctx = AudioCodecContext(_cinit_sentinel)
elif c_ctx.codec_type == lib.AVMEDIA_TYPE_SUBTITLE:
from av.subtitles.codeccontext import SubtitleCodecContext
py_ctx = SubtitleCodecContext(_cinit_sentinel)
else:
py_ctx = CodecContext(_cinit_sentinel)
py_ctx.container = container
py_ctx._init(c_ctx, c_codec)
return py_ctx
cdef class CodecContext(object):
@staticmethod
def create(codec, mode=None):
cdef Codec cy_codec = codec if isinstance(codec, Codec) else Codec(codec, mode)
cdef lib.AVCodecContext *c_ctx = lib.avcodec_alloc_context3(cy_codec.ptr)
err_check(lib.avcodec_get_context_defaults3(c_ctx, cy_codec.ptr))
return wrap_codec_context(c_ctx, cy_codec.ptr, None)
def __cinit__(self, sentinel=None, *args, **kwargs):
if sentinel is not _cinit_sentinel:
raise RuntimeError('Cannot instantiate CodecContext')
cdef _init(self, lib.AVCodecContext *ptr, lib.AVCodec *codec):
self.ptr = ptr
if self.ptr.codec and codec and self.ptr.codec != codec:
raise RuntimeError('Wrapping CodecContext with mismatched codec.')
self.codec = wrap_codec(codec if codec != NULL else self.ptr.codec)
@property
def is_open(self):
return lib.avcodec_is_open(self.ptr)
cpdef open(self, bint strict=True):
if lib.avcodec_is_open(self.ptr):
if strict:
raise ValueError('CodecContext is already open.')
return
# We might pass partial frames.
# TODO: What is this for?! This is causing problems with raw decoding
# as the internal parser doesn't seem to see a frame until it sees
# the next one.
# if self.codec.ptr.capabilities & lib.CODEC_CAP_TRUNCATED:
# self.ptr.flags |= lib.CODEC_FLAG_TRUNCATED
# TODO: Do this better.
cdef _Dictionary options = Dictionary()
options.update(self.options or {})
# for k, v in self.options.iteritems():
# options[k] = v
err_check(lib.avcodec_open2(self.ptr, self.codec.ptr, &options.ptr))
self.options = dict(options)
cpdef close(self, bint strict=True):
if not lib.avcodec_is_open(self.ptr):
if strict:
raise ValueError('CodecContext is already closed.')
return
err_check(lib.avcodec_close(self.ptr))
def __dealloc__(self):
if self.ptr and self.container is None:
lib.avcodec_close(self.ptr)
lib.avcodec_free_context(&self.ptr)
if self.parser:
lib.av_parser_close(self.parser)
if self.parse_buffer:
free(self.parse_buffer)
def __repr__(self):
return '<av.%s %s/%s at 0x%x>' % (
self.__class__.__name__,
self.type or '<notype>',
self.name or '<nocodec>',
id(self),
)
def parse(self, str input_, allow_stream=False):
if not self.parser:
self.parser = lib.av_parser_init(self.codec.ptr.id)
if not self.parser:
if allow_stream:
return [Packet(input_)]
else:
raise ValueError('no parser for %s' % self.codec.name)
cdef size_t new_buffer_size
cdef unsigned char *c_input
if input_ is not None:
# Make sure we have enough buffer.
new_buffer_size = self.parse_buffer_size + len(input_)
if new_buffer_size > self.parse_buffer_max_size:
self.parse_buffer = <unsigned char*>realloc(<void*>self.parse_buffer, new_buffer_size)
self.parse_buffer_max_size = new_buffer_size
# Copy to the end of the buffer.
c_input = input_ # for casting
memcpy(self.parse_buffer + self.parse_buffer_size, c_input, len(input_))
self.parse_buffer_size = new_buffer_size
cdef size_t base = 0
cdef size_t used = 0 # To signal to the while.
cdef Packet packet = None
packets = []
while base < self.parse_buffer_size:
packet = Packet()
with nogil:
used = lib.av_parser_parse2(
self.parser,
self.ptr,
&packet.struct.data, &packet.struct.size,
self.parse_buffer + base, self.parse_buffer_size - base,
0, 0,
self.parse_pos
)
err_check(used)
if packet.struct.size:
packets.append(packet)
if used:
self.parse_pos += used
base += used
if not (used or packet.struct.size):
break
if base:
# Shuffle the buffer.
memcpy(self.parse_buffer, self.parse_buffer + base, base)
self.parse_buffer_size -= base
return packets
cpdef encode(self, Frame frame=None):
self.open(strict=False)
if self.container:
self.container.start_encoding()
cdef Packet packet = self._encode(frame)
if packet:
packet._time_base = self.ptr.time_base
return packet
cdef _encode(self, Frame frame):
raise NotImplementedError('Base CodecContext cannot encode frames.')
cpdef decode(self, Packet packet, int count=0):
"""Decode a list of :class:`.Frame` from the given :class:`.Packet`.
If the packet is None, the buffers will be flushed. This is useful if
you do not want the library to automatically re-order frames for you
(if they are encoded with a codec that has B-frames).
"""
if packet is None:
packet = Packet() # Makes our control flow easier.
if not self.codec.ptr:
raise ValueError('cannot decode unknown codec')
self.open(strict=False)
cdef int data_consumed = 0
cdef list decoded_objs = []
cdef uint8_t *original_data = packet.struct.data
cdef int original_size = packet.struct.size
cdef bint is_flushing = not (packet.struct.data and packet.struct.size)
# Keep decoding while there is data.
while is_flushing or packet.struct.size > 0:
if is_flushing:
packet.struct.data = NULL
packet.struct.size = 0
decoded = self._decode_one(&packet.struct, &data_consumed)
packet.struct.data += data_consumed
packet.struct.size -= data_consumed
if decoded:
if isinstance(decoded, Frame):
self._setup_decoded_frame(decoded)
decoded_objs.append(decoded)
# Sometimes we will error if we try to flush the stream
# (e.g. MJPEG webcam streams), and so we must be able to
# bail after the first, even though buffers may build up.
if count and len(decoded_objs) >= count:
break
# Sometimes there are no frames, and no data is consumed, and this
# is ok. However, no more frames are going to be pulled out of here.
# (It is possible for data to not be consumed as long as there are
# frames, e.g. during flushing.)
elif not data_consumed:
break
# Restore the packet.
packet.struct.data = original_data
packet.struct.size = original_size
return decoded_objs
cdef _setup_decoded_frame(self, Frame frame):
# In FFMpeg <= 3.0, and all LibAV we know of, the frame's pts may be
# unset at this stage, and he PTS from a packet is the correct one while
# decoding, and it is copied to pkt_pts during creation of a frame.
# TODO: Look into deprecation of pkt_pts in FFmpeg > 3.0
if frame.ptr.pts == lib.AV_NOPTS_VALUE:
frame.ptr.pts = frame.ptr.pkt_pts
# We don't have to worry about context vs. stream time_base during decoding
# as they should be the same.
frame._time_base = self.ptr.time_base
frame.index = self.ptr.frame_number - 1
cdef _decode_one(self, lib.AVPacket *packet, int *data_consumed):
raise NotImplementedError('Base CodecContext cannot decode packets.')
property time_base:
def __get__(self):
return avrational_to_faction(&self.ptr.time_base)
def __set__(self, value):
to_avrational(value, &self.ptr.time_base)
property name:
def __get__(self):
return self.codec.name
property type:
def __get__(self):
return self.codec.type
property profile:
def __get__(self):
if self.ptr.codec and lib.av_get_profile_name(self.ptr.codec, self.ptr.profile):
return lib.av_get_profile_name(self.ptr.codec, self.ptr.profile)
else:
return None
# TODO: Deprecate.
property rate:
def __get__(self):
if self.ptr:
return self.ptr.ticks_per_frame * avrational_to_faction(&self.ptr.time_base)
property bit_rate:
def __get__(self):
return self.ptr.bit_rate if self.ptr and self.ptr.bit_rate > 0 else None
def __set__(self, int value):
self.ptr.bit_rate = value
property max_bit_rate:
def __get__(self):
if self.ptr and self.ptr.rc_max_rate > 0:
return self.ptr.rc_max_rate
else:
return None
property bit_rate_tolerance:
def __get__(self):
return self.ptr.bit_rate_tolerance if self.ptr else None
def __set__(self, int value):
self.ptr.bit_rate_tolerance = value
# TODO: Does it conceptually make sense that this is on streams, instead
# of on the container?
property thread_count:
def __get__(self):
return self.ptr.thread_count
def __set__(self, int value):
self.ptr.thread_count = value