diff --git a/mp4/libav.py b/mp4/libav.py index b1e06bb..03646fc 100644 --- a/mp4/libav.py +++ b/mp4/libav.py @@ -18,18 +18,14 @@ else: AVERROR_EOF = _errtag('E', 'O', 'F', ' ') -AV_ERROR_MAX_STRING_SIZE = 64 +_AV_ERROR_MAX_STRING_SIZE = 64 -_avutil.av_strerror.restype = ctypes.c_int -_avutil.av_strerror.argtypes = [ - ctypes.c_int, # errno - ctypes.c_char_p, # errbuf - ctypes.c_size_t] # errbuff_size - -def strerror(errno): - errbuf = ctypes.create_string_buffer(AV_ERROR_MAX_STRING_SIZE) - _avutil.av_strerror(errno, errbuf, AV_ERROR_MAX_STRING_SIZE) - return errbuf.value.decode("utf-8") +AVMEDIA_TYPE_UNKNOWN = -1 +AVMEDIA_TYPE_VIDEO = 0 +AVMEDIA_TYPE_AUDIO = 1 +AVMEDIA_TYPE_DATA = 2 +AVMEDIA_TYPE_SUBTITLE = 3 +AVMEDIA_TYPE_ATTACHMENT = 4 class AVFrame(ctypes.Structure): pass @@ -37,18 +33,6 @@ class AVFrame(ctypes.Structure): AVFrame_p = ctypes.POINTER(AVFrame) AVFrame_pp = ctypes.POINTER(AVFrame_p) -_avutil.av_frame_alloc.restype = AVFrame_p -_avutil.av_frame_alloc.argtypes = None - -def frame_alloc(): - return _avutil.av_frame_alloc() - -_avutil.av_frame_free.restype = None -_avutil.av_frame_free.argtypes = [AVFrame_pp] - -def frame_free(frame): - _avutil.av_frame_free(ctypes.byref(frame)) - class AVCodecParameters(ctypes.Structure): pass @@ -80,6 +64,57 @@ class AVFormatContext(ctypes.Structure): AVFormatContext_p = ctypes.POINTER(AVFormatContext) AVFormatContext_pp = ctypes.POINTER(AVFormatContext_p) +class AVPacket(ctypes.Structure): + _fields_ = [ + ("buf", ctypes.c_void_p), + ("pts", ctypes.c_int64), + ("dts", ctypes.c_int64), + ("data", ctypes.c_void_p), + ("size", ctypes.c_int), + ("stream_index", ctypes.c_int)] + # ... + +AVPacket_p = ctypes.POINTER(AVPacket) +AVPacket_pp = ctypes.POINTER(AVPacket_p) + +class AVCodec(ctypes.Structure): + _fields_ = [ + ("name", ctypes.c_char_p), + ("long_name", ctypes.c_char_p)] + # ... + +AVCodec_p = ctypes.POINTER(AVCodec) +AVCodec_pp = ctypes.POINTER(AVCodec_p) + +class AVCodecContext(ctypes.Structure): + pass + +AVCodecContext_p = ctypes.POINTER(AVCodecContext) +AVCodecContext_pp = ctypes.POINTER(AVCodecContext_p) + +_avutil.av_strerror.restype = ctypes.c_int +_avutil.av_strerror.argtypes = [ + ctypes.c_int, # errno + ctypes.c_char_p, # errbuf + ctypes.c_size_t] # errbuff_size + +def strerror(errno): + errbuf = ctypes.create_string_buffer(_AV_ERROR_MAX_STRING_SIZE) + _avutil.av_strerror(errno, errbuf, _AV_ERROR_MAX_STRING_SIZE) + return errbuf.value.decode("utf-8") + +_avutil.av_frame_alloc.restype = AVFrame_p +_avutil.av_frame_alloc.argtypes = None + +def frame_alloc(): + return _avutil.av_frame_alloc() + +_avutil.av_frame_free.restype = None +_avutil.av_frame_free.argtypes = [AVFrame_pp] + +def frame_free(frame): + _avutil.av_frame_free(ctypes.byref(frame)) + _avformat.avformat_alloc_context.restype = AVFormatContext_p _avformat.avformat_alloc_context.argtypes = None @@ -116,22 +151,6 @@ _avformat.avformat_find_stream_info.argtypes = [ def format_find_stream_info(context): return _avformat.avformat_find_stream_info(context, None) -AVMEDIA_TYPE_UNKNOWN = -1 -AVMEDIA_TYPE_VIDEO = 0 -AVMEDIA_TYPE_AUDIO = 1 -AVMEDIA_TYPE_DATA = 2 -AVMEDIA_TYPE_SUBTITLE = 3 -AVMEDIA_TYPE_ATTACHMENT = 4 - -class AVCodec(ctypes.Structure): - _fields_ = [ - ("name", ctypes.c_char_p), - ("long_name", ctypes.c_char_p)] - # ... - -AVCodec_p = ctypes.POINTER(AVCodec) -AVCodec_pp = ctypes.POINTER(AVCodec_p) - _avformat.av_find_best_stream.restype = ctypes.c_int _avformat.av_find_best_stream.argtypes = [ AVFormatContext_p, @@ -146,19 +165,6 @@ def format_find_best_stream(context, type): index = _avformat.av_find_best_stream(context, type, -1, -1, ctypes.byref(codec), 0) return index, codec -class AVPacket(ctypes.Structure): - _fields_ = [ - ("buf", ctypes.c_void_p), - ("pts", ctypes.c_int64), - ("dts", ctypes.c_int64), - ("data", ctypes.c_void_p), - ("size", ctypes.c_int), - ("stream_index", ctypes.c_int)] - # ... - -AVPacket_p = ctypes.POINTER(AVPacket) -AVPacket_pp = ctypes.POINTER(AVPacket_p) - _avformat.av_packet_alloc.restype = AVPacket_p _avformat.av_packet_alloc.argtypes = None @@ -177,12 +183,6 @@ _avformat.av_read_frame.argtypes = [AVFormatContext_p, AVPacket_p] def read_frame(context, packet): return _avformat.av_read_frame(context, packet) -class AVCodecContext(ctypes.Structure): - pass - -AVCodecContext_p = ctypes.POINTER(AVCodecContext) -AVCodecContext_pp = ctypes.POINTER(AVCodecContext_p) - _avcodec.avcodec_alloc_context3.restype = AVCodecContext_p _avcodec.avcodec_alloc_context3.argtypes = [AVCodec_p]