forked from praekelt/pyffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyffmpeg.pyx
2095 lines (1811 loc) · 73.4 KB
/
pyffmpeg.pyx
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
# #######################################################################################
# Pyffmpeg
#
# Copyright (C) 2008-2009 Bertrand Nouvel <[email protected]>
# Japanese French Laboratory for Informatics
# CNRS
#
# #######################################################################################
# This file is distibuted under LGPL-3.0
# See COPYING file attached.
# #######################################################################################
#
# BETA VERSION,
# Some function may be subject to refactoring
#
# Todo :
# * why seek_before mandatory
#
# Todo:
# * Online Streaming
# * Add support for video encoding
# * More testing
# * More examples
#
# Abilities
# * Frame seeking (TO BE CHECKED again and again)
#
# Changed compared with pyffmpeg:
# * Clean up destructors
# * Added compatibility with NumPy and PIL
# * Added copyless mode for ordered streams/tracks ( when buffers are disabled)
# * Added audio support
# * MultiTrack support (possibility to pass paramer)
#
"""
#########################################################################################
###################################################################################################
# Based on Pyffmpeg 0.2 by
# Copyright (C) 2006-2007 James Evans <[email protected]>
# Authorization to change from GPL2.0 to LGPL 3.0 provided by original author for this new version
###################################################################################################
# Declaration and imports
###################################################################################################
import sys
import traceback
#import numpy
#import Image
ctypedef signed char int8_t
ctypedef unsigned char uint8_t
ctypedef signed short int16_t
ctypedef unsigned short uint16_t
ctypedef signed long int32_t
ctypedef signed long long int64_t
cdef enum:
SEEK_SET = 0
SEEK_CUR = 1
SEEK_END = 2
cdef extern from "string.h":
memcpy(void * dst, void * src, unsigned long sz)
cdef extern from "Python.h":
ctypedef int size_t
object PyBuffer_FromMemory( void *ptr, int size)
object PyBuffer_FromReadWriteMemory( void *ptr, int size)
object PyString_FromStringAndSize(char *s, int len)
void* PyMem_Malloc( size_t n)
void PyMem_Free( void *p)
#cimport numpy as np
#cdef extern from "numpy/arrayobject.h":
# void *PyArray_DATA(np.ndarray arr)
cdef extern from "libavutil/mathematics.h":
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
cdef extern from "libavformat/avio.h":
struct ByteIOContext:
pass
ctypedef long long int offset_t
int url_ferror(ByteIOContext *s)
int url_feof(ByteIOContext *s)
int url_fopen(ByteIOContext **s, char *filename, int flags)
int url_fclose(ByteIOContext *s)
offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
ByteIOContext *av_alloc_put_byte(
unsigned char *buffer,
int buffer_size,
int write_flag,
void *opaque,
void * a , void * b , void * c)
#int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
#int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
#offset_t (*seek)(void *opaque, offset_t offset, int whence))
cdef extern from "libavutil/avutil.h":
cdef enum PixelFormat:
PIX_FMT_NONE= -1,
PIX_FMT_YUV420P, #< Planar YUV 4:2:0 (1 Cr & Cb sample per 2x2 Y samples)
PIX_FMT_YUV422, #< Packed pixel, Y0 Cb Y1 Cr
PIX_FMT_RGB24, #< Packed pixel, 3 bytes per pixel, RGBRGB...
PIX_FMT_BGR24, #< Packed pixel, 3 bytes per pixel, BGRBGR...
PIX_FMT_YUV422P, #< Planar YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples)
PIX_FMT_YUV444P, #< Planar YUV 4:4:4 (1 Cr & Cb sample per 1x1 Y samples)
PIX_FMT_RGBA32, #< Packed pixel, 4 bytes per pixel, BGRABGRA..., stored in cpu endianness
PIX_FMT_YUV410P, #< Planar YUV 4:1:0 (1 Cr & Cb sample per 4x4 Y samples)
PIX_FMT_YUV411P, #< Planar YUV 4:1:1 (1 Cr & Cb sample per 4x1 Y samples)
PIX_FMT_RGB565, #< always stored in cpu endianness
PIX_FMT_RGB555, #< always stored in cpu endianness, most significant bit to 1
PIX_FMT_GRAY8,
PIX_FMT_MONOWHITE, #< 0 is white
PIX_FMT_MONOBLACK, #< 0 is black
PIX_FMT_PAL8, #< 8 bit with RGBA palette
PIX_FMT_YUVJ420P, #< Planar YUV 4:2:0 full scale (jpeg)
PIX_FMT_YUVJ422P, #< Planar YUV 4:2:2 full scale (jpeg)
PIX_FMT_YUVJ444P, #< Planar YUV 4:4:4 full scale (jpeg)
PIX_FMT_XVMC_MPEG2_MC,#< XVideo Motion Acceleration via common packet passing(xvmc_render.h)
PIX_FMT_XVMC_MPEG2_IDCT,
PIX_FMT_UYVY422, #< Packed pixel, Cb Y0 Cr Y1
PIX_FMT_UYVY411, #< Packed pixel, Cb Y0 Y1 Cr Y2 Y3
PIX_FMT_NB,
cdef extern from "libavcodec/avcodec.h":
# use an unamed enum for defines
cdef enum:
AVSEEK_FLAG_BACKWARD = 1 #< seek backward
AVSEEK_FLAG_BYTE = 2 #< seeking based on position in bytes
AVSEEK_FLAG_ANY = 4 #< seek to any frame, even non keyframes
CODEC_CAP_TRUNCATED = 0x0008
CODEC_FLAG_TRUNCATED = 0x00010000 # input bitTrack might be truncated at a random location instead of only at frame boundaries
AV_TIME_BASE = 1000000
FF_I_TYPE = 1 # Intra
FF_P_TYPE = 2 # Predicted
FF_B_TYPE = 3 # Bi-dir predicted
FF_S_TYPE = 4 # S(GMC)-VOP MPEG4
FF_SI_TYPE = 5
FF_SP_TYPE = 6
AV_NOPTS_VALUE = <int64_t>0x8000000000000000
enum AVDiscard:
# we leave some space between them for extensions (drop some keyframes for intra only or drop just some bidir frames)
AVDISCARD_NONE = -16 # discard nothing
AVDISCARD_DEFAULT= 0 # discard useless packets like 0 size packets in avi
AVDISCARD_NONREF = 8 # discard all non reference
AVDISCARD_BIDIR = 16 # discard all bidirectional frames
AVDISCARD_NONKEY = 32 # discard all frames except keyframes
AVDISCARD_ALL = 48 # discard all
# struct AVCodecContext:
# int codec_type
# int codec_id
# int flags
# int width
# int height
# int pix_fmt
# int frame_number
# int hurry_up
# int skip_idct
# int skip_frame
struct AVRational:
int num
int den
struct AVCodecContext:
int bit_rate
int bit_rate_tolerance
int flags
int sub_id
int me_method
AVRational time_base
int width
int height
int gop_size
int pix_fmt
int rate_emu
int sample_rate
int channels
int sample_fmt
int frame_size
int frame_number
int real_pict_num
int delay
float qcompress
float qblur
int qmin
int qmax
int max_qdiff
int max_b_frames
float b_quant_factor
int rc_strategy
int b_frame_strategy
int hurry_up
int rtp_mode
int rtp_payload_size
int mv_bits
int header_bits
int i_tex_bits
int p_tex_bits
int i_count
int p_count
int skip_count
int misc_bits
int frame_bits
#char codec_name [32]
int codec_type
int codec_id
unsigned int codec_tag
int workaround_bugs
int luma_elim_threshold
int chroma_elim_threshold
int strict_std_compliance
float b_quant_offset
int error_resilience
int has_b_frames
int block_align
int parse_only
int mpeg_quant
char * stats_out
char * stats_in
float rc_qsquish
float rc_qmod_amp
int rc_qmod_freq
int rc_override_count
char * rc_eq
int rc_max_rate
int rc_min_rate
int rc_buffer_size
float rc_buffer_aggressivity
float i_quant_factor
float i_quant_offset
float rc_initial_cplx
int dct_algo
float lumi_masking
float temporal_cplx_masking
float spatial_cplx_masking
float p_masking
float dark_masking
int unused
int idct_algo
int slice_count
int * slice_offset
int error_concealment
unsigned dsp_mask
int bits_per_sample
int prediction_method
AVRational sample_aspect_ratio
# AVFrame * coded_frame
int debug
int debug_mv
#uint64_t error [4]
int mb_qmin
int mb_qmax
int me_cmp
int me_sub_cmp
int mb_cmp
int ildct_cmp
int dia_size
int last_predictor_count
int pre_me
int me_pre_cmp
int pre_dia_size
int me_subpel_quality
int dtg_active_format
int me_range
int intra_quant_bias
int inter_quant_bias
int color_table_id
int internal_buffer_count
void * internal_buffer
int global_quality
int coder_type
int context_model
int slice_flags
int xvmc_acceleration
int mb_decision
uint16_t * intra_matrix
uint16_t * inter_matrix
unsigned int Track_codec_tag
int scenechange_threshold
int lmin
int lmax
#AVPaletteControl * palctrl
int noise_reduction
int rc_initial_buffer_occupancy
int inter_threshold
int flags2
int error_rate
int antialias_algo
int quantizer_noise_shaping
int thread_count
int me_threshold
int mb_threshold
int intra_dc_precision
int nsse_weight
int skip_top
int skip_bottom
int profile
int level
int lowres
int coded_width
int coded_height
int frame_skip_threshold
int frame_skip_factor
int frame_skip_exp
int frame_skip_cmp
float border_masking
int mb_lmin
int mb_lmax
int me_penalty_compensation
int bidir_refine
int brd_scale
float crf
int cqp
int keyint_min
int refs
int chromaoffset
int bframebias
int trellis
float complexityblur
int deblockalpha
int deblockbeta
int partitions
int directpred
int cutoff
int scenechange_factor
int mv0_threshold
int b_sensitivity
int compression_level
int use_lpc
int lpc_coeff_precision
int min_prediction_order
int max_prediction_order
int prediction_order_method
int min_partition_order
int max_partition_order
int64_t timecode_frame_start
int skip_frame
int skip_idct
int skip_loop_filter
enum CodecType:
CODEC_TYPE_UNKNOWN = -1
CODEC_TYPE_VIDEO = 0
CODEC_TYPE_AUDIO = 1
CODEC_TYPE_DATA = 2
CODEC_TYPE_SUBTITLE = 3
struct AVCodec:
char *name
int type
int id
int priv_data_size
int capabilities
AVCodec *next
AVRational *supported_framerates #array of supported framerates, or NULL if any, array is terminated by {0,0}
int *pix_fmts #array of supported pixel formats, or NULL if unknown, array is terminanted by -1
struct AVPacket:
int64_t pts #< presentation time stamp in time_base units
int64_t dts #< decompression time stamp in time_base units
char *data
int size
int stream_index
int flags
int duration #< presentation duration in time_base units (0 if not available)
void *priv
int64_t pos #< byte position in Track, -1 if unknown
struct AVFrame:
char *data[4]
int linesize[4]
int64_t pts
int pict_type
int key_frame
struct AVPicture:
uint8_t *data[4]
int linesize[4]
AVCodec *avcodec_find_decoder(int id)
int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
int avcodec_close(AVCodecContext *avctx)
int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
int *got_picture_ptr,
char *buf, int buf_size)
int avcodec_decode_audio2(AVCodecContext *avctx, #AVFrame *picture,
int16_t * samples, int * frames,
void *buf, int buf_size)
int avpicture_fill(AVPicture *picture, void *ptr,
int pix_fmt, int width, int height)
AVFrame *avcodec_alloc_frame()
int avpicture_get_size(int pix_fmt, int width, int height)
int avpicture_layout(AVPicture* src, int pix_fmt, int width, int height,
unsigned char *dest, int dest_size)
void avcodec_flush_buffers(AVCodecContext *avctx)
OUTPUTMODE_NUMPY=0
OUTPUTMODE_PIL=1
# ##############################################################
# Used for debugging
# ##############################################################
#class DLock:
# def __init__(self):
# self.l=threading.Lock()
# def acquire(self,*args,**kwargs):
# sys.stderr.write("MTX:"+str((self, "A", args, kwargs))+"\n")
# try:
# raise Exception
# except:
# if (hasattr(sys,"last_traceback")):
# traceback.print_tb(sys.last_traceback)
# else:
# traceback.print_tb(sys.exc_traceback)
# sys.stderr.flush()
# sys.stdout.flush()
# #return self.l.acquire(*args,**kwargs)
# return True
# def release(self):
# sys.stderr.write("MTX:"+str((self, "R"))+"\n")
# try:
# raise Exception
# except:
# if (hasattr(sys,"last_traceback")):
# traceback.print_tb(sys.last_traceback)
# else:
# traceback.print_tb(sys.exc_traceback)
# sys.stderr.flush()
# sys.stdout.flush()
# #return self.l.release()
cdef extern from "libavformat/avformat.h":
struct AVFrac:
int64_t val, num, den
void av_register_all()
struct AVProbeData:
char *filename
unsigned char *buf
int buf_size
struct AVCodecParserContext:
pass
struct AVIndexEntry:
pass
struct AVStream:
int index #/* Track index in AVFormatContext */
int id #/* format specific Track id */
AVCodecContext *codec #/* codec context */
# real base frame rate of the Track.
# for example if the timebase is 1/90000 and all frames have either
# approximately 3600 or 1800 timer ticks then r_frame_rate will be 50/1
AVRational r_frame_rate
void *priv_data
# internal data used in av_find_stream_info()
int64_t codec_info_duration
int codec_info_nb_frames
# encoding: PTS generation when outputing Track
AVFrac pts
# this is the fundamental unit of time (in seconds) in terms
# of which frame timestamps are represented. for fixed-fps content,
# timebase should be 1/framerate and timestamp increments should be
# identically 1.
AVRational time_base
int pts_wrap_bits # number of bits in pts (used for wrapping control)
# ffmpeg.c private use
int Track_copy # if TRUE, just copy Track
int discard # < selects which packets can be discarded at will and dont need to be demuxed
# FIXME move stuff to a flags field?
# quality, as it has been removed from AVCodecContext and put in AVVideoFrame
# MN:dunno if thats the right place, for it
float quality
# decoding: position of the first frame of the component, in
# AV_TIME_BASE fractional seconds.
int64_t start_time
# decoding: duration of the Track, in AV_TIME_BASE fractional
# seconds.
int64_t duration
char language[4] # ISO 639 3-letter language code (empty string if undefined)
# av_read_frame() support
int need_parsing # < 1.full parsing needed, 2.only parse headers dont repack
AVCodecParserContext *parser
int64_t cur_dts
int last_IP_duration
int64_t last_IP_pts
# av_seek_frame() support
AVIndexEntry *index_entries # only used if the format does not support seeking natively
int nb_index_entries
int index_entries_allocated_size
int64_t nb_frames # < number of frames in this Track if known or 0
uint8_t *cur_ptr
int cur_len
AVPacket cur_pkt
struct AVFormatContext:
int nb_streams
AVStream **streams
int64_t timestamp
int64_t start_time
AVStream *cur_st
#uint8_t *cur_ptr
#int cur_len
#AVPacket cur_pkt
ByteIOContext pb
# decoding: total file size. 0 if unknown
int64_t file_size
int64_t duration
# decoding: total Track bitrate in bit/s, 0 if not
# available. Never set it directly if the file_size and the
# duration are known as ffmpeg can compute it automatically. */
int bit_rate
# av_seek_frame() support
int64_t data_offset # offset of the first packet
int index_built
struct AVInputFormat:
pass
struct AVFormatParameters:
pass
int av_open_input_file(AVFormatContext **ic_ptr, char *filename,
AVInputFormat *fmt,
int buf_size,
AVFormatParameters *ap)
int av_open_input_stream(AVFormatContext **ic_ptr,
ByteIOContext *pb, char *filename,
AVInputFormat *fmt, AVFormatParameters *ap)
void av_close_input_file(AVFormatContext *ic_ptr)
void av_close_input_stream(AVFormatContext *s)
int av_find_stream_info(AVFormatContext *ic)
void dump_format(AVFormatContext *ic,
int index,
char *url,
int is_output)
void av_free_packet(AVPacket *pkt)
int av_read_packet(AVFormatContext *s, AVPacket *pkt)
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
int av_seek_frame(AVFormatContext *s, int Track_index, int64_t timestamp, int flags)
int av_seek_frame_binary(AVFormatContext *s, int Track_index, int64_t target_ts, int flags)
void av_parser_close(AVCodecParserContext *s)
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
cdef __registered
__registered = 0
cdef extern void av_free(void *ptr)
cdef extern from "libswscale/swscale.h":
cdef enum:
SWS_FAST_BILINEAR,
SWS_BILINEAR,
SWS_BICUBIC,
SWS_X,
SWS_POINT,
SWS_AREA,
SWS_BICUBLIN,
SWS_GAUSS,
SWS_SINC,
SWS_LANCZOS,
SWS_SPLINE
struct SwsContext:
pass
struct SwsFilter:
pass
SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
void sws_freeContext(SwsContext *swsContext)
int sws_scale(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,int srcSliceH, uint8_t* dst[], int dstStride[])
cdef extern from "Python.h":
ctypedef unsigned long size_t
object PyBuffer_FromMemory( void *ptr, int size)
object PyBuffer_FromReadWriteMemory( void *ptr, int size)
object PyString_FromStringAndSize(char *s, int len)
void* PyMem_Malloc( size_t n)
void PyMem_Free( void *p)
def rwbuffer_at(pos,len):
cdef unsigned long ptr=int(pos)
return PyBuffer_FromReadWriteMemory(<void *>ptr,len)
try:
import numpy
from pyffmpeg_numpybindings import *
except:
numpy=None
try:
import PIL
from PIL import Image
except:
Image=None
def py_av_register_all():
if __registered:
return
__registered = 1
av_register_all()
cdef AVRational AV_TIME_BASE_Q
AV_TIME_BASE_Q.num = 1
AV_TIME_BASE_Q.den = AV_TIME_BASE
AVCODEC_MAX_AUDIO_FRAME_SIZE=192000
##############################################
cdef av_read_frame_flush ( AVFormatContext * s ) :
cdef AVStream *st
cdef int i
#flush_packet_queue(s);
if (s.cur_st) :
#if (s.cur_st.parser):
# av_free_packet(&s.cur_st.cur_pkt)
s.cur_st = NULL
#s.cur_st.cur_ptr = NULL;
#s.cur_st.cur_len = 0;
for i in range(s.nb_streams) :
st = s.streams[i]
if (st.parser) :
av_parser_close(st.parser)
st.parser = NULL
st.last_IP_pts = AV_NOPTS_VALUE
st.cur_dts = 0
#########################################################################################################
## AudioQueue Object (This may later be exported with another object)
#########################################################################################################
cdef DEBUG(s):
sys.stderr.write("DEBUG: %s\n"%(s,))
sys.stderr.flush()
## contains pairs of timestamp, array
from audioqueue import AudioQueue, Queue_Empty, Queue_Full
##################################################################
# New class
##################################################################
py_av_register_all()
TS_AUDIOVIDEO={'video1':(CODEC_TYPE_VIDEO, -1, {}), 'audio1':(CODEC_TYPE_AUDIO, -1, {})}
TS_AUDIO={ 'audio1':(CODEC_TYPE_AUDIO, -1, {})}
TS_VIDEO={ 'video1':(CODEC_TYPE_VIDEO, -1, {})}
TS_VIDEO_PIL={ 'video1':(CODEC_TYPE_VIDEO, -1, {'outputmode':OUTPUTMODE_PIL})}
##################################################################
# Once we open a file we may recover different tracks
##################################################################
cdef class AFFMpegReader:
""" Abstract version of FFMpegReader"""
### File
cdef object filename
### used when streaming
cdef ByteIOContext *io_context
### Tracks contained in the file
cdef object tracks
cdef void * ctracks
### current timing
cdef float opts ## orginal pts recoded as a float
cdef unsigned long long int pts
cdef unsigned long long int dts
cdef unsigned long long int errjmppts # when trying to skip over buggy area
cdef unsigned long int frameno
cdef float fps # real frame per seconds (not declared one)
cdef float tps # ticks per seconds
cdef AVPacket * packet
cdef AVPacket * prepacket
cdef AVPacket packetbufa
cdef AVPacket packetbufb
cdef int altpacket
#
cdef bint observers_enabled
cdef AVFormatContext *FormatCtx
# self.prepacket=<AVPacket *>None
# self.packet=&self.packetbufa
def __new__(self):
pass
def dump(self):
pass
def open(self,char *filename, track_selector={'video1':(CODEC_TYPE_VIDEO, -1), 'audio1':(CODEC_TYPE_AUDIO, -1)}):
pass
def close(self):
pass
cdef read_packet(self):
print "FATAL Error This function is abstract and should never be called, it is likely that you compiled pyffmpeg with a too old version of pyffmpeg !!!"
print "Try running 'easy_install -U cython' and rerun the pyffmpeg2 install"
assert(False)
def process_current_packet(self):
pass
def __prefetch_packet(self):
pass
def read_until_next_frame(self):
pass
cdef class Track:
"""
A track is used for memorizing all the aspect related to
Video, or an Audio Track.
Practically a Track is managing the decoder context for itself.
"""
cdef AFFMpegReader vr
cdef int no
## cdef AVFormatContext *FormatCtx
cdef AVCodecContext *CodecCtx
cdef AVCodec *Codec
cdef AVFrame *frame
cdef AVStream *stream
cdef long start_time
cdef object packet_queue
cdef frame_queue
cdef unsigned long long int pts
cdef unsigned long long int last_pts
cdef unsigned long long int last_dts
cdef object observer
cdef int support_truncated
cdef int do_check_start
cdef int reopen_codec_on_buffer_reset
cdef __new__(Track self):
self.vr=None
self.observer=None
self.support_truncated=1
self.reopen_codec_on_buffer_reset=1
def get_no(self):
"""Returns the number of the tracks."""
return self.no
def __len__(self):
"""Returns the number of data frames on this track."""
return self.stream.nb_frames
def duration(self):
"""Return the duration of one track in PTS"""
return self.stream.duration
def duration_time(self):
""" returns the duration of one track in seconds."""
return float(self.duration())/ (<float>AV_TIME_BASE)
cdef init0(Track self, AFFMpegReader vr,int no, AVCodecContext *CodecCtx):
""" This is a private constructor """
self.vr=vr
self.CodecCtx=CodecCtx
self.no=no
self.stream = self.vr.FormatCtx.streams[self.no]
self.frame_queue=[]
self.Codec = avcodec_find_decoder(self.CodecCtx.codec_id)
self.frame = avcodec_alloc_frame()
self.start_time=self.stream.start_time
self.do_check_start=0
def init(self,observer=None, support_truncated=0, **args):
""" This is a private constructor
It supports also the following parameted from ffmpeg
skip_frame
skip_idct
skip_loop_filter
hurry_up
dct_algo
idct_algo
To set all value for keyframes_only
just set up hurry_mode to any value.
"""
self.observer=None
self.support_truncated=support_truncated
for k in args.keys():
if k not in [ "skip_frame", "skip_loop_filter", "skip_idct", "hurry_up", "hurry_mode", "dct_algo", "idct_algo", "check_start" ]:
sys.stderr.write("warning unsupported arguments in stream initialization :"+k+"\n")
if self.Codec == NULL:
raise IOError("Unable to get decoder")
if (self.Codec.capabilities & CODEC_CAP_TRUNCATED) and (self.support_truncated!=0):
self.CodecCtx.flags = self.CodecCtx.flags | CODEC_FLAG_TRUNCATED
avcodec_open(self.CodecCtx, self.Codec)
if args.has_key("hurry_mode"):
self.CodecCtx.hurry_up=2
self.CodecCtx.skip_loop_filter=32
self.CodecCtx.skip_frame=32
self.CodecCtx.skip_idct=32
if args.has_key("skip_frame"):
self.CodecCtx.skip_frame=args["skip_frame"]
if args.has_key("skip_idct"):
self.CodecCtx.skip_idct=args["skip_idct"]
if args.has_key("skip_loop_filter"):
self.CodecCtx.skip_loop_filter=args["skip_loop_filter"]
if args.has_key("hurry_up"):
self.CodecCtx.skip_loop_filter=args["hurry_up"]
if args.has_key("dct_algo"):
self.CodecCtx.dct_algo=args["dct_algo"]
if args.has_key("idct_algo"):
self.CodecCtx.idct_algo=args["idct_algo"]
if (not args.has_key("check_start") or args["check_start"]):
self.do_check_start=1
def check_start(self):
""" It seems that many file have incorrect initial time information.
The best way to avoid offset in shifting is thus to check what
is the time of the beginning of the track.
"""
if (self.do_check_start):
try:
self.seek_to_pts(0)
self.vr.read_until_next_frame()
sys.stderr.write("start time checked : pts = %d , declared was : %d\n"%(self.pts,self.start_time))
self.start_time=self.pts
self.seek_to_pts(0)
self.do_check_start=0
except Exception,e:
#DEBUG("check start FAILED " + str(e))
pass
else:
pass
def set_observer(self, observer=None):
""" An observer is a callback function that is called when a new
frame of data arrives.
Using this function you may setup the function to be called when
a frame of data is decoded on that track.
"""
self.observer=observer
def _reopencodec(self):
"""
This is used to reset the codec context.
Very often, this is the safest way to get everything clean
when seeking.
"""
if (self.CodecCtx!=NULL):
avcodec_close(self.CodecCtx)
self.CodecCtx=NULL
self.CodecCtx = self.vr.FormatCtx.streams[self.no].codec
self.Codec = avcodec_find_decoder(self.CodecCtx.codec_id)
if self.Codec == NULL:
raise IOError("Unable to get decoder")
if (self.Codec.capabilities & CODEC_CAP_TRUNCATED) and (self.support_truncated!=0):
self.CodecCtx.flags = self.CodecCtx.flags | CODEC_FLAG_TRUNCATED
ret = avcodec_open(self.CodecCtx, self.Codec)
def close(self):
"""
This closes the track. And thus closes the context."
"""
if (self.CodecCtx!=NULL):
avcodec_close(self.CodecCtx)
self.CodecCtx=NULL
def prepare_to_be_just_in_time(self):
"""
In order to avoid delay during reading, our player try always
to read a little bit of that is available ahead.
"""
pass
def reset_buffers(self):
"""
This function is used on seek to reset everything.
"""
self.pts=0
self.last_pts=0
self.last_dts=0
if (self.CodecCtx!=NULL):
avcodec_flush_buffers(self.CodecCtx)
## violent solution but the most efficient so far...
if (self.reopen_codec_on_buffer_reset):
self._reopencodec()
# cdef process_packet(self, AVPacket * pkt):
# print "FATAL : process_packet : Error This function is abstract and should never be called, it is likely that you compiled pyffmpeg with a too old version of pyffmpeg !!!"
# print "Try running 'easy_install -U cython' and rerun the pyffmpeg2 install"
# assert(False)
def seek_to_seconds(self, seconds ):
""" Seek to the specified time in seconds.
Note that seeking is always bit more complicated when we want to be exact.
* We do not use any precomputed index structure for seeking (which would make seeking exact)
* Due to codec limitations, FFMPEG often provide approximative seeking capabilites
* Sometimes "time data" in video file are invalid
* Sometimes "seeking is simply not possible"
We are working on improving our seeking capabilities.
"""
pts = (<float>seconds) * (<float>AV_TIME_BASE)
#pts=av_rescale(seconds*AV_TIME_BASE, self.stream.time_base.den, self.stream.time_base.num*AV_TIME_BASE)
self.seek_to_pts(pts)
def seek_to_pts(self, unsigned long long int pts):
""" Seek to the specified PTS
Note that seeking is always bit more complicated when we want to be exact.
* We do not use any precomputed index structure for seeking (which would make seeking exact)
* Due to codec limitations, FFMPEG often provide approximative seeking capabilites
* Sometimes "time data" in video file are invalid
* Sometimes "seeking is simply not possible"
We are working on improving our seeking capabilities.
"""
#print "seeked pts :", pts
#sys.stderr.write( "seeking to PTS %d (start_time=%d (%x)) ?\n"%(pts,self.start_time, self.start_time))
if (self.start_time!=AV_NOPTS_VALUE):
#if (pts<self.start_time):
# print "seek before start_time / ignoring start time / seeking maybe invalid (MPEG TS ?)"
# #pts+=self.stream.start_time
#else:
# #pts-=self.start_time
pts+=self.start_time
# #pts+=(self.start_time*self.get_fps())
# #pts+=(self.start_time*15)
#sys.stderr.write( "seeking to PTS %d (start_time=%d (%x)) \n"%(pts,self.start_time, self.start_time))
self.vr.seek_to(pts)
cdef class AudioPacketDecoder:
cdef uint8_t *audio_pkt_data
cdef int audio_pkt_size
cdef __new__(self):
self.audio_pkt_data =<uint8_t *>NULL
self.audio_pkt_size=0
cdef int audio_decode_frame(self, AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size, double * pts_ptr, double * audio_clock, int nchannels, int samplerate, AVPacket * pkt, int first) :
cdef double pts
cdef int n