# -*- coding: utf-8 -*-
# Copyright (C) 2005-2006  Joe Wreschnig
# Copyright (C) 2006-2007  Lukas Lalinsky
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import struct

from mutagen._util import MutagenError


class error(MutagenError):
    """Error raised by :mod:`mutagen.asf`"""


class ASFError(error):
    pass


class ASFHeaderError(error):
    pass


def guid2bytes(s):
    """Converts a GUID to the serialized bytes representation"""

    assert isinstance(s, str)
    assert len(s) == 36

    p = struct.pack
    return b"".join([
        p("<IHH", int(s[:8], 16), int(s[9:13], 16), int(s[14:18], 16)),
        p(">H", int(s[19:23], 16)),
        p(">Q", int(s[24:], 16))[2:],
    ])


def bytes2guid(s):
    """Converts a serialized GUID to a text GUID"""

    assert isinstance(s, bytes)

    u = struct.unpack
    v = []
    v.extend(u("<IHH", s[:8]))
    v.extend(u(">HQ", s[8:10] + b"\x00\x00" + s[10:]))
    return "%08X-%04X-%04X-%04X-%012X" % tuple(v)


# Names from http://windows.microsoft.com/en-za/windows7/c00d10d1-[0-9A-F]{1,4}
CODECS = {
    0x0000: "Unknown Wave Format",
    0x0001: "Microsoft PCM Format",
    0x0002: "Microsoft ADPCM Format",
    0x0003: "IEEE Float",
    0x0004: "Compaq Computer VSELP",
    0x0005: "IBM CVSD",
    0x0006: "Microsoft CCITT A-Law",
    0x0007: "Microsoft CCITT u-Law",
    0x0008: "Microsoft DTS",
    0x0009: "Microsoft DRM",
    0x000A: "Windows Media Audio 9 Voice",
    0x000B: "Windows Media Audio 10 Voice",
    0x000C: "OGG Vorbis",
    0x000D: "FLAC",
    0x000E: "MOT AMR",
    0x000F: "Nice Systems IMBE",
    0x0010: "OKI ADPCM",
    0x0011: "Intel IMA ADPCM",
    0x0012: "Videologic MediaSpace ADPCM",
    0x0013: "Sierra Semiconductor ADPCM",
    0x0014: "Antex Electronics G.723 ADPCM",
    0x0015: "DSP Solutions DIGISTD",
    0x0016: "DSP Solutions DIGIFIX",
    0x0017: "Dialogic OKI ADPCM",
    0x0018: "MediaVision ADPCM",
    0x0019: "Hewlett-Packard CU codec",
    0x001A: "Hewlett-Packard Dynamic Voice",
    0x0020: "Yamaha ADPCM",
    0x0021: "Speech Compression SONARC",
    0x0022: "DSP Group True Speech",
    0x0023: "Echo Speech EchoSC1",
    0x0024: "Ahead Inc. Audiofile AF36",
    0x0025: "Audio Processing Technology APTX",
    0x0026: "Ahead Inc. AudioFile AF10",
    0x0027: "Aculab Prosody 1612",
    0x0028: "Merging Technologies S.A. LRC",
    0x0030: "Dolby Labs AC2",
    0x0031: "Microsoft GSM 6.10",
    0x0032: "Microsoft MSNAudio",
    0x0033: "Antex Electronics ADPCME",
    0x0034: "Control Resources VQLPC",
    0x0035: "DSP Solutions Digireal",
    0x0036: "DSP Solutions DigiADPCM",
    0x0037: "Control Resources CR10",
    0x0038: "Natural MicroSystems VBXADPCM",
    0x0039: "Crystal Semiconductor IMA ADPCM",
    0x003A: "Echo Speech EchoSC3",
    0x003B: "Rockwell ADPCM",
    0x003C: "Rockwell DigiTalk",
    0x003D: "Xebec Multimedia Solutions",
    0x0040: "Antex Electronics G.721 ADPCM",
    0x0041: "Antex Electronics G.728 CELP",
    0x0042: "Intel G.723",
    0x0043: "Intel G.723.1",
    0x0044: "Intel G.729 Audio",
    0x0045: "Sharp G.726 Audio",
    0x0050: "Microsoft MPEG-1",
    0x0052: "InSoft RT24",
    0x0053: "InSoft PAC",
    0x0055: "MP3 - MPEG Layer III",
    0x0059: "Lucent G.723",
    0x0060: "Cirrus Logic",
    0x0061: "ESS Technology ESPCM",
    0x0062: "Voxware File-Mode",
    0x0063: "Canopus Atrac",
    0x0064: "APICOM G.726 ADPCM",
    0x0065: "APICOM G.722 ADPCM",
    0x0066: "Microsoft DSAT",
    0x0067: "Microsoft DSAT Display",
    0x0069: "Voxware Byte Aligned",
    0x0070: "Voxware AC8",
    0x0071: "Voxware AC10",
    0x0072: "Voxware AC16",
    0x0073: "Voxware AC20",
    0x0074: "Voxware RT24 MetaVoice",
    0x0075: "Voxware RT29 MetaSound",
    0x0076: "Voxware RT29HW",
    0x0077: "Voxware VR12",
    0x0078: "Voxware VR18",
    0x0079: "Voxware TQ40",
    0x007A: "Voxware SC3",
    0x007B: "Voxware SC3",
    0x0080: "Softsound",
    0x0081: "Voxware TQ60",
    0x0082: "Microsoft MSRT24",
    0x0083: "AT&T Labs G.729A",
    0x0084: "Motion Pixels MVI MV12",
    0x0085: "DataFusion Systems G.726",
    0x0086: "DataFusion Systems GSM610",
    0x0088: "Iterated Systems ISIAudio",
    0x0089: "Onlive",
    0x008A: "Multitude FT SX20",
    0x008B: "Infocom ITS ACM G.721",
    0x008C: "Convedia G.729",
    0x008D: "Congruency Audio",
    0x0091: "Siemens Business Communications SBC24",
    0x0092: "Sonic Foundry Dolby AC3 SPDIF",
    0x0093: "MediaSonic G.723",
    0x0094: "Aculab Prosody 8KBPS",
    0x0097: "ZyXEL ADPCM",
    0x0098: "Philips LPCBB",
    0x0099: "Studer Professional Audio AG Packed",
    0x00A0: "Malden Electronics PHONYTALK",
    0x00A1: "Racal Recorder GSM",
    0x00A2: "Racal Recorder G720.a",
    0x00A3: "Racal Recorder G723.1",
    0x00A4: "Racal Recorder Tetra ACELP",
    0x00B0: "NEC AAC",
    0x00FF: "CoreAAC Audio",
    0x0100: "Rhetorex ADPCM",
    0x0101: "BeCubed Software IRAT",
    0x0111: "Vivo G.723",
    0x0112: "Vivo Siren",
    0x0120: "Philips CELP",
    0x0121: "Philips Grundig",
    0x0123: "Digital G.723",
    0x0125: "Sanyo ADPCM",
    0x0130: "Sipro Lab Telecom ACELP.net",
    0x0131: "Sipro Lab Telecom ACELP.4800",
    0x0132: "Sipro Lab Telecom ACELP.8V3",
    0x0133: "Sipro Lab Telecom ACELP.G.729",
    0x0134: "Sipro Lab Telecom ACELP.G.729A",
    0x0135: "Sipro Lab Telecom ACELP.KELVIN",
    0x0136: "VoiceAge AMR",
    0x0140: "Dictaphone G.726 ADPCM",
    0x0141: "Dictaphone CELP68",
    0x0142: "Dictaphone CELP54",
    0x0150: "Qualcomm PUREVOICE",
    0x0151: "Qualcomm HALFRATE",
    0x0155: "Ring Zero Systems TUBGSM",
    0x0160: "Windows Media Audio Standard",
    0x0161: "Windows Media Audio 9 Standard",
    0x0162: "Windows Media Audio 9 Professional",
    0x0163: "Windows Media Audio 9 Lossless",
    0x0164: "Windows Media Audio Pro over SPDIF",
    0x0170: "Unisys NAP ADPCM",
    0x0171: "Unisys NAP ULAW",
    0x0172: "Unisys NAP ALAW",
    0x0173: "Unisys NAP 16K",
    0x0174: "Sycom ACM SYC008",
    0x0175: "Sycom ACM SYC701 G725",
    0x0176: "Sycom ACM SYC701 CELP54",
    0x0177: "Sycom ACM SYC701 CELP68",
    0x0178: "Knowledge Adventure ADPCM",
    0x0180: "Fraunhofer IIS MPEG-2 AAC",
    0x0190: "Digital Theater Systems DTS",
    0x0200: "Creative Labs ADPCM",
    0x0202: "Creative Labs FastSpeech8",
    0x0203: "Creative Labs FastSpeech10",
    0x0210: "UHER informatic GmbH ADPCM",
    0x0215: "Ulead DV Audio",
    0x0216: "Ulead DV Audio",
    0x0220: "Quarterdeck",
    0x0230: "I-link Worldwide ILINK VC",
    0x0240: "Aureal Semiconductor RAW SPORT",
    0x0249: "Generic Passthru",
    0x0250: "Interactive Products HSX",
    0x0251: "Interactive Products RPELP",
    0x0260: "Consistent Software CS2",
    0x0270: "Sony SCX",
    0x0271: "Sony SCY",
    0x0272: "Sony ATRAC3",
    0x0273: "Sony SPC",
    0x0280: "Telum Audio",
    0x0281: "Telum IA Audio",
    0x0285: "Norcom Voice Systems ADPCM",
    0x0300: "Fujitsu TOWNS SND",
    0x0350: "Micronas SC4 Speech",
    0x0351: "Micronas CELP833",
    0x0400: "Brooktree BTV Digital",
    0x0401: "Intel Music Coder",
    0x0402: "Intel Audio",
    0x0450: "QDesign Music",
    0x0500: "On2 AVC0 Audio",
    0x0501: "On2 AVC1 Audio",
    0x0680: "AT&T Labs VME VMPCM",
    0x0681: "AT&T Labs TPC",
    0x08AE: "ClearJump Lightwave Lossless",
    0x1000: "Olivetti GSM",
    0x1001: "Olivetti ADPCM",
    0x1002: "Olivetti CELP",
    0x1003: "Olivetti SBC",
    0x1004: "Olivetti OPR",
    0x1100: "Lernout & Hauspie",
    0x1101: "Lernout & Hauspie CELP",
    0x1102: "Lernout & Hauspie SBC8",
    0x1103: "Lernout & Hauspie SBC12",
    0x1104: "Lernout & Hauspie SBC16",
    0x1400: "Norris Communication",
    0x1401: "ISIAudio",
    0x1500: "AT&T Labs Soundspace Music Compression",
    0x1600: "Microsoft MPEG ADTS AAC",
    0x1601: "Microsoft MPEG RAW AAC",
    0x1608: "Nokia MPEG ADTS AAC",
    0x1609: "Nokia MPEG RAW AAC",
    0x181C: "VoxWare MetaVoice RT24",
    0x1971: "Sonic Foundry Lossless",
    0x1979: "Innings Telecom ADPCM",
    0x1FC4: "NTCSoft ALF2CD ACM",
    0x2000: "Dolby AC3",
    0x2001: "DTS",
    0x4143: "Divio AAC",
    0x4201: "Nokia Adaptive Multi-Rate",
    0x4243: "Divio G.726",
    0x4261: "ITU-T H.261",
    0x4263: "ITU-T H.263",
    0x4264: "ITU-T H.264",
    0x674F: "Ogg Vorbis Mode 1",
    0x6750: "Ogg Vorbis Mode 2",
    0x6751: "Ogg Vorbis Mode 3",
    0x676F: "Ogg Vorbis Mode 1+",
    0x6770: "Ogg Vorbis Mode 2+",
    0x6771: "Ogg Vorbis Mode 3+",
    0x7000: "3COM NBX Audio",
    0x706D: "FAAD AAC Audio",
    0x77A1: "True Audio Lossless Audio",
    0x7A21: "GSM-AMR CBR 3GPP Audio",
    0x7A22: "GSM-AMR VBR 3GPP Audio",
    0xA100: "Comverse Infosys G723.1",
    0xA101: "Comverse Infosys AVQSBC",
    0xA102: "Comverse Infosys SBC",
    0xA103: "Symbol Technologies G729a",
    0xA104: "VoiceAge AMR WB",
    0xA105: "Ingenient Technologies G.726",
    0xA106: "ISO/MPEG-4 Advanced Audio Coding (AAC)",
    0xA107: "Encore Software Ltd's G.726",
    0xA108: "ZOLL Medical Corporation ASAO",
    0xA109: "Speex Voice",
    0xA10A: "Vianix MASC Speech Compression",
    0xA10B: "Windows Media 9 Spectrum Analyzer Output",
    0xA10C: "Media Foundation Spectrum Analyzer Output",
    0xA10D: "GSM 6.10 (Full-Rate) Speech",
    0xA10E: "GSM 6.20 (Half-Rate) Speech",
    0xA10F: "GSM 6.60 (Enchanced Full-Rate) Speech",
    0xA110: "GSM 6.90 (Adaptive Multi-Rate) Speech",
    0xA111: "GSM Adaptive Multi-Rate WideBand Speech",
    0xA112: "Polycom G.722",
    0xA113: "Polycom G.728",
    0xA114: "Polycom G.729a",
    0xA115: "Polycom Siren",
    0xA116: "Global IP Sound ILBC",
    0xA117: "Radio Time Time Shifted Radio",
    0xA118: "Nice Systems ACA",
    0xA119: "Nice Systems ADPCM",
    0xA11A: "Vocord Group ITU-T G.721",
    0xA11B: "Vocord Group ITU-T G.726",
    0xA11C: "Vocord Group ITU-T G.722.1",
    0xA11D: "Vocord Group ITU-T G.728",
    0xA11E: "Vocord Group ITU-T G.729",
    0xA11F: "Vocord Group ITU-T G.729a",
    0xA120: "Vocord Group ITU-T G.723.1",
    0xA121: "Vocord Group LBC",
    0xA122: "Nice G.728",
    0xA123: "France Telecom G.729 ACM Audio",
    0xA124: "CODIAN Audio",
    0xCC12: "Intel YUV12 Codec",
    0xCFCC: "Digital Processing Systems Perception Motion JPEG",
    0xD261: "DEC H.261",
    0xD263: "DEC H.263",
    0xFFFE: "Extensible Wave Format",
    0xFFFF: "Unregistered",
}
