From 2f730e18229b37c255365034c1d07050a9b7cd0f Mon Sep 17 00:00:00 2001 From: Chris Moeller Date: Mon, 7 Oct 2013 11:09:38 -0700 Subject: [PATCH] Added TAK support --- .../FFMPEG/FFMPEG.xcodeproj/project.pbxproj | 132 ++ Frameworks/FFMPEG/ffmpeg-minimal/config.h | 18 +- .../FFMPEG/ffmpeg-minimal/libavcodec/dirac.c | 334 +++ .../FFMPEG/ffmpeg-minimal/libavcodec/dirac.h | 60 + .../ffmpeg-minimal/libavcodec/dirac_arith.c | 116 + .../ffmpeg-minimal/libavcodec/dirac_arith.h | 190 ++ .../ffmpeg-minimal/libavcodec/dirac_dwt.c | 572 +++++ .../ffmpeg-minimal/libavcodec/dirac_dwt.h | 123 ++ .../ffmpeg-minimal/libavcodec/dirac_parser.c | 260 +++ .../ffmpeg-minimal/libavcodec/diracdec.c | 1939 +++++++++++++++++ .../ffmpeg-minimal/libavcodec/diracdsp.c | 1 - .../FFMPEG/ffmpeg-minimal/libavcodec/golomb.c | 173 ++ .../FFMPEG/ffmpeg-minimal/libavcodec/golomb.h | 557 +++++ .../ffmpeg-minimal/libavcodec/jrevdct.c | 1155 ++++++++++ .../FFMPEG/ffmpeg-minimal/libavcodec/tak.c | 158 ++ .../FFMPEG/ffmpeg-minimal/libavcodec/tak.h | 163 ++ .../ffmpeg-minimal/libavcodec/tak_parser.c | 122 ++ .../FFMPEG/ffmpeg-minimal/libavcodec/takdec.c | 948 ++++++++ .../FFMPEG/ffmpeg-minimal/libavcodec/unary.h | 56 + .../ffmpeg-minimal/libavcodec/x86/dirac_dwt.c | 202 ++ .../ffmpeg-minimal/libavcodec/x86/dirac_dwt.h | 30 + .../libavcodec/x86/diracdsp_yasm.asm | 264 +++ .../libavcodec/x86/dsputil_init.c | 734 +++++++ .../libavcodec/x86/dsputil_mmx.c | 637 ++++++ .../ffmpeg-minimal/libavcodec/x86/idct_xvid.h | 43 + .../ffmpeg-minimal/libavformat/apetag.c | 241 ++ .../ffmpeg-minimal/libavformat/apetag.h | 44 + .../FFMPEG/ffmpeg-minimal/libavformat/img2.c | 102 + .../ffmpeg-minimal/libavformat/rawdec.c | 177 ++ .../ffmpeg-minimal/libavformat/rawdec.h | 67 + .../ffmpeg-minimal/libavformat/takdec.c | 210 ++ Frameworks/TagLib/taglib/taglib/fileref.cpp | 3 + Plugins/FFMPEG/FFMPEGDecoder.m | 4 +- Plugins/TagLib/TagLibMetadataReader.m | 4 +- 34 files changed, 9827 insertions(+), 12 deletions(-) create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_parser.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdec.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/jrevdct.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak_parser.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/takdec.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/unary.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/diracdsp_yasm.asm create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_init.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_mmx.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/idct_xvid.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavformat/img2.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.c create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.h create mode 100644 Frameworks/FFMPEG/ffmpeg-minimal/libavformat/takdec.c diff --git a/Frameworks/FFMPEG/FFMPEG.xcodeproj/project.pbxproj b/Frameworks/FFMPEG/FFMPEG.xcodeproj/project.pbxproj index c688cfc3b..d4d5f70c6 100644 --- a/Frameworks/FFMPEG/FFMPEG.xcodeproj/project.pbxproj +++ b/Frameworks/FFMPEG/FFMPEG.xcodeproj/project.pbxproj @@ -271,6 +271,39 @@ 830F0DD117FC934D00042E8F /* spdif.c in Sources */ = {isa = PBXBuildFile; fileRef = 830F0DCF17FC934D00042E8F /* spdif.c */; }; 830F0DD217FC934D00042E8F /* spdif.h in Headers */ = {isa = PBXBuildFile; fileRef = 830F0DD017FC934D00042E8F /* spdif.h */; }; 830F0DD617FC93E400042E8F /* xwma.c in Sources */ = {isa = PBXBuildFile; fileRef = 830F0DD417FC93E400042E8F /* xwma.c */; }; + 833C3795180328A300CBA602 /* takdec.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C3794180328A300CBA602 /* takdec.c */; }; + 833C379A180328B300CBA602 /* tak_parser.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C3796180328B300CBA602 /* tak_parser.c */; }; + 833C379B180328B300CBA602 /* tak.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C3797180328B300CBA602 /* tak.c */; }; + 833C379C180328B300CBA602 /* tak.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C3798180328B300CBA602 /* tak.h */; }; + 833C379D180328B300CBA602 /* takdec.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C3799180328B300CBA602 /* takdec.c */; }; + 833C379F18032A2700CBA602 /* unary.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C379E18032A2700CBA602 /* unary.h */; }; + 833C37A218032A2F00CBA602 /* rawdec.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37A018032A2F00CBA602 /* rawdec.c */; }; + 833C37A318032A2F00CBA602 /* rawdec.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37A118032A2F00CBA602 /* rawdec.h */; }; + 833C37A618032A5000CBA602 /* apetag.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37A418032A5000CBA602 /* apetag.c */; }; + 833C37A718032A5000CBA602 /* apetag.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37A518032A5000CBA602 /* apetag.h */; }; + 833C37A918032AAD00CBA602 /* img2.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37A818032AAD00CBA602 /* img2.c */; }; + 833C37B118032AEF00CBA602 /* diracdsp.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37AA18032AEF00CBA602 /* diracdsp.c */; }; + 833C37B218032AEF00CBA602 /* diracdsp.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37AB18032AEF00CBA602 /* diracdsp.h */; }; + 833C37B318032AEF00CBA602 /* dsputil.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37AC18032AEF00CBA602 /* dsputil.c */; }; + 833C37B418032AEF00CBA602 /* dsputil.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37AD18032AEF00CBA602 /* dsputil.h */; }; + 833C37B518032AEF00CBA602 /* diracdsp_mmx.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37AE18032AEF00CBA602 /* diracdsp_mmx.c */; }; + 833C37B618032AEF00CBA602 /* diracdsp_mmx.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37AF18032AEF00CBA602 /* diracdsp_mmx.h */; }; + 833C37B718032AEF00CBA602 /* dsputil_x86.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37B018032AEF00CBA602 /* dsputil_x86.h */; }; + 833C37C218032CF600CBA602 /* dirac_arith.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37BA18032CF600CBA602 /* dirac_arith.c */; }; + 833C37C318032CF600CBA602 /* dirac_arith.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37BB18032CF600CBA602 /* dirac_arith.h */; }; + 833C37C418032CF600CBA602 /* dirac_dwt.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37BC18032CF600CBA602 /* dirac_dwt.c */; }; + 833C37C518032CF600CBA602 /* dirac_dwt.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37BD18032CF600CBA602 /* dirac_dwt.h */; }; + 833C37C618032CF600CBA602 /* dirac_parser.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37BE18032CF600CBA602 /* dirac_parser.c */; }; + 833C37C718032CF600CBA602 /* dirac.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37BF18032CF600CBA602 /* dirac.c */; }; + 833C37C818032CF600CBA602 /* dirac.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37C018032CF600CBA602 /* dirac.h */; }; + 833C37C918032CF600CBA602 /* diracdec.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37C118032CF600CBA602 /* diracdec.c */; }; + 833C37CE18032D4800CBA602 /* golomb.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37CA18032D4800CBA602 /* golomb.c */; }; + 833C37CF18032D4800CBA602 /* golomb.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37CB18032D4800CBA602 /* golomb.h */; }; + 833C37D018032D4800CBA602 /* dirac_dwt.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37CC18032D4800CBA602 /* dirac_dwt.c */; }; + 833C37D118032D4800CBA602 /* dirac_dwt.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37CD18032D4800CBA602 /* dirac_dwt.h */; }; + 833C37D318032E4800CBA602 /* dsputil_init.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37D218032E4800CBA602 /* dsputil_init.c */; }; + 833C37D518032E7000CBA602 /* jrevdct.c in Sources */ = {isa = PBXBuildFile; fileRef = 833C37D418032E7000CBA602 /* jrevdct.c */; }; + 833C37D718032EA500CBA602 /* idct_xvid.h in Headers */ = {isa = PBXBuildFile; fileRef = 833C37D618032EA500CBA602 /* idct_xvid.h */; }; 83BCB8E217FCA64400760340 /* avconfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 83BCB8DF17FCA64400760340 /* avconfig.h */; settings = {ATTRIBUTES = (Public, ); }; }; 83BCB8E317FCA64400760340 /* timecode.c in Sources */ = {isa = PBXBuildFile; fileRef = 83BCB8E017FCA64400760340 /* timecode.c */; }; 83BCB8E417FCA64400760340 /* timecode.h in Headers */ = {isa = PBXBuildFile; fileRef = 83BCB8E117FCA64400760340 /* timecode.h */; }; @@ -548,6 +581,39 @@ 830F0DCF17FC934D00042E8F /* spdif.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = spdif.c; sourceTree = ""; }; 830F0DD017FC934D00042E8F /* spdif.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = spdif.h; sourceTree = ""; }; 830F0DD417FC93E400042E8F /* xwma.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xwma.c; sourceTree = ""; }; + 833C3794180328A300CBA602 /* takdec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = takdec.c; sourceTree = ""; }; + 833C3796180328B300CBA602 /* tak_parser.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = tak_parser.c; sourceTree = ""; }; + 833C3797180328B300CBA602 /* tak.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = tak.c; sourceTree = ""; }; + 833C3798180328B300CBA602 /* tak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tak.h; sourceTree = ""; }; + 833C3799180328B300CBA602 /* takdec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = takdec.c; sourceTree = ""; }; + 833C379E18032A2700CBA602 /* unary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = unary.h; sourceTree = ""; }; + 833C37A018032A2F00CBA602 /* rawdec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = rawdec.c; sourceTree = ""; }; + 833C37A118032A2F00CBA602 /* rawdec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rawdec.h; sourceTree = ""; }; + 833C37A418032A5000CBA602 /* apetag.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = apetag.c; sourceTree = ""; }; + 833C37A518032A5000CBA602 /* apetag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = apetag.h; sourceTree = ""; }; + 833C37A818032AAD00CBA602 /* img2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = img2.c; sourceTree = ""; }; + 833C37AA18032AEF00CBA602 /* diracdsp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = diracdsp.c; sourceTree = ""; }; + 833C37AB18032AEF00CBA602 /* diracdsp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = diracdsp.h; sourceTree = ""; }; + 833C37AC18032AEF00CBA602 /* dsputil.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dsputil.c; sourceTree = ""; }; + 833C37AD18032AEF00CBA602 /* dsputil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dsputil.h; sourceTree = ""; }; + 833C37AE18032AEF00CBA602 /* diracdsp_mmx.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = diracdsp_mmx.c; path = x86/diracdsp_mmx.c; sourceTree = ""; }; + 833C37AF18032AEF00CBA602 /* diracdsp_mmx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = diracdsp_mmx.h; path = x86/diracdsp_mmx.h; sourceTree = ""; }; + 833C37B018032AEF00CBA602 /* dsputil_x86.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dsputil_x86.h; path = x86/dsputil_x86.h; sourceTree = ""; }; + 833C37BA18032CF600CBA602 /* dirac_arith.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dirac_arith.c; sourceTree = ""; }; + 833C37BB18032CF600CBA602 /* dirac_arith.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dirac_arith.h; sourceTree = ""; }; + 833C37BC18032CF600CBA602 /* dirac_dwt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dirac_dwt.c; sourceTree = ""; }; + 833C37BD18032CF600CBA602 /* dirac_dwt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dirac_dwt.h; sourceTree = ""; }; + 833C37BE18032CF600CBA602 /* dirac_parser.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dirac_parser.c; sourceTree = ""; }; + 833C37BF18032CF600CBA602 /* dirac.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dirac.c; sourceTree = ""; }; + 833C37C018032CF600CBA602 /* dirac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dirac.h; sourceTree = ""; }; + 833C37C118032CF600CBA602 /* diracdec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = diracdec.c; sourceTree = ""; }; + 833C37CA18032D4800CBA602 /* golomb.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = golomb.c; sourceTree = ""; }; + 833C37CB18032D4800CBA602 /* golomb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = golomb.h; sourceTree = ""; }; + 833C37CC18032D4800CBA602 /* dirac_dwt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dirac_dwt.c; sourceTree = ""; }; + 833C37CD18032D4800CBA602 /* dirac_dwt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dirac_dwt.h; sourceTree = ""; }; + 833C37D218032E4800CBA602 /* dsputil_init.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = dsputil_init.c; sourceTree = ""; }; + 833C37D418032E7000CBA602 /* jrevdct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = jrevdct.c; sourceTree = ""; }; + 833C37D618032EA500CBA602 /* idct_xvid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = idct_xvid.h; sourceTree = ""; }; 83BCB8DF17FCA64400760340 /* avconfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = avconfig.h; sourceTree = ""; }; 83BCB8E017FCA64400760340 /* timecode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = timecode.c; sourceTree = ""; }; 83BCB8E117FCA64400760340 /* timecode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = timecode.h; sourceTree = ""; }; @@ -635,6 +701,29 @@ 830F0B8E17FC4FB900042E8F /* libavcodec */ = { isa = PBXGroup; children = ( + 833C37D418032E7000CBA602 /* jrevdct.c */, + 833C37CA18032D4800CBA602 /* golomb.c */, + 833C37CB18032D4800CBA602 /* golomb.h */, + 833C37BA18032CF600CBA602 /* dirac_arith.c */, + 833C37BB18032CF600CBA602 /* dirac_arith.h */, + 833C37BC18032CF600CBA602 /* dirac_dwt.c */, + 833C37BD18032CF600CBA602 /* dirac_dwt.h */, + 833C37BE18032CF600CBA602 /* dirac_parser.c */, + 833C37BF18032CF600CBA602 /* dirac.c */, + 833C37C018032CF600CBA602 /* dirac.h */, + 833C37C118032CF600CBA602 /* diracdec.c */, + 833C37AA18032AEF00CBA602 /* diracdsp.c */, + 833C37AB18032AEF00CBA602 /* diracdsp.h */, + 833C37AC18032AEF00CBA602 /* dsputil.c */, + 833C37AD18032AEF00CBA602 /* dsputil.h */, + 833C37AE18032AEF00CBA602 /* diracdsp_mmx.c */, + 833C37AF18032AEF00CBA602 /* diracdsp_mmx.h */, + 833C37B018032AEF00CBA602 /* dsputil_x86.h */, + 833C379E18032A2700CBA602 /* unary.h */, + 833C3796180328B300CBA602 /* tak_parser.c */, + 833C3797180328B300CBA602 /* tak.c */, + 833C3798180328B300CBA602 /* tak.h */, + 833C3799180328B300CBA602 /* takdec.c */, 830F0D9017FC8EAC00042E8F /* dvdata.c */, 830F0D9117FC8EAC00042E8F /* dvdata.h */, 830F0D8017FC8E8B00042E8F /* aac_tablegen_decl.h */, @@ -764,6 +853,12 @@ 830F0BAD17FC4FB900042E8F /* libavformat */ = { isa = PBXGroup; children = ( + 833C37A818032AAD00CBA602 /* img2.c */, + 833C37A418032A5000CBA602 /* apetag.c */, + 833C37A518032A5000CBA602 /* apetag.h */, + 833C37A018032A2F00CBA602 /* rawdec.c */, + 833C37A118032A2F00CBA602 /* rawdec.h */, + 833C3794180328A300CBA602 /* takdec.c */, 830F0DD417FC93E400042E8F /* xwma.c */, 830F0DCF17FC934D00042E8F /* spdif.c */, 830F0DD017FC934D00042E8F /* spdif.h */, @@ -948,6 +1043,10 @@ 830F0D0717FC80B400042E8F /* x86 */ = { isa = PBXGroup; children = ( + 833C37D618032EA500CBA602 /* idct_xvid.h */, + 833C37D218032E4800CBA602 /* dsputil_init.c */, + 833C37CC18032D4800CBA602 /* dirac_dwt.c */, + 833C37CD18032D4800CBA602 /* dirac_dwt.h */, 830F0DC317FC92EE00042E8F /* videodsp_init.c */, 830F0DBD17FC922C00042E8F /* h264chroma_init.c */, 830F0DA917FC8FBD00042E8F /* dct_init.c */, @@ -1007,6 +1106,7 @@ B09E94940D74834B0064F138 /* config.h in Headers */, 830F0BD217FC4FB900042E8F /* avcodec.h in Headers */, 830F0D7217FC8C1300042E8F /* ast.h in Headers */, + 833C37A718032A5000CBA602 /* apetag.h in Headers */, 830F0D8917FC8E8B00042E8F /* aac_tablegen.h in Headers */, 830F0D3117FC841B00042E8F /* imgutils.h in Headers */, 830F0C6C17FC7DB100042E8F /* bprint.h in Headers */, @@ -1030,6 +1130,8 @@ 830F0CFB17FC7F1E00042E8F /* thread.h in Headers */, 830F0D6617FC8A3300042E8F /* options_table.h in Headers */, 830F0CF317FC7F1E00042E8F /* raw.h in Headers */, + 833C37B618032AEF00CBA602 /* diracdsp_mmx.h in Headers */, + 833C37B418032AEF00CBA602 /* dsputil.h in Headers */, 830F0C3F17FC554D00042E8F /* timer.h in Headers */, 830F0D1E17FC82AB00042E8F /* colorspace.h in Headers */, 830F0DBA17FC921D00042E8F /* cpu.h in Headers */, @@ -1037,15 +1139,18 @@ 830F0BDF17FC4FB900042E8F /* put_bits.h in Headers */, 830F0C7217FC7DB100042E8F /* rc4.h in Headers */, 830F0C2517FC527400042E8F /* bswap.h in Headers */, + 833C379F18032A2700CBA602 /* unary.h in Headers */, 830F0CDD17FC7F1E00042E8F /* faandct.h in Headers */, 830F0D0B17FC80B400042E8F /* lsp_mips.h in Headers */, 830F0C2B17FC54F800042E8F /* srtp.h in Headers */, 830F0D5E17FC89BD00042E8F /* options_table.h in Headers */, 830F0CEC17FC7F1E00042E8F /* lsp.h in Headers */, 830F0D6517FC8A3300042E8F /* audiointerleave.h in Headers */, + 833C37A318032A2F00CBA602 /* rawdec.h in Headers */, 830F0D3D17FC846C00042E8F /* random_seed.h in Headers */, 830F0CCA17FC7F1E00042E8F /* bytestream.h in Headers */, 830F0D0317FC7F4000042E8F /* sinewin_tablegen.h in Headers */, + 833C37C518032CF600CBA602 /* dirac_dwt.h in Headers */, 830F0CDF17FC7F1E00042E8F /* faanidct.h in Headers */, 830F0DD217FC934D00042E8F /* spdif.h in Headers */, 830F0BDE17FC4FB900042E8F /* parser.h in Headers */, @@ -1072,6 +1177,7 @@ 830F0C6A17FC7DB100042E8F /* avassert.h in Headers */, 830F0D6D17FC8C0700042E8F /* aactab.h in Headers */, 830F0BEC17FC4FB900042E8F /* wmavoice_data.h in Headers */, + 833C37D118032D4800CBA602 /* dirac_dwt.h in Headers */, 830F0D7917FC8C7300042E8F /* mpeg4audio.h in Headers */, 830F0C3A17FC554D00042E8F /* des.h in Headers */, 830F0BF117FC4FB900042E8F /* avi.h in Headers */, @@ -1088,6 +1194,7 @@ 830F0CDB17FC7F1E00042E8F /* error_resilience.h in Headers */, 830F0C4A17FC7CA300042E8F /* avio_internal.h in Headers */, 830F0C7017FC7DB100042E8F /* pixdesc.h in Headers */, + 833C37C318032CF600CBA602 /* dirac_arith.h in Headers */, 830F0CF717FC7F1E00042E8F /* rnd_avg.h in Headers */, 830F0D4117FC848D00042E8F /* sha.h in Headers */, 830F0CEE17FC7F1E00042E8F /* mpeg12data.h in Headers */, @@ -1098,7 +1205,9 @@ 830F0CF117FC7F1E00042E8F /* ratecontrol.h in Headers */, 830F0C1417FC500B00042E8F /* fft-internal.h in Headers */, 830F0D8E17FC8E8B00042E8F /* sbr.h in Headers */, + 833C37B218032AEF00CBA602 /* diracdsp.h in Headers */, 830F0C3D17FC554D00042E8F /* internal.h in Headers */, + 833C37D718032EA500CBA602 /* idct_xvid.h in Headers */, 830F0CF617FC7F1E00042E8F /* rl.h in Headers */, 830F0D8D17FC8E8B00042E8F /* dv_profile.h in Headers */, 830F0D3917FC844E00042E8F /* parseutils.h in Headers */, @@ -1109,6 +1218,7 @@ 830F0C4C17FC7CA300042E8F /* internal.h in Headers */, 830F0BFC17FC4FB900042E8F /* bswap.h in Headers */, 830F0D8A17FC8E8B00042E8F /* aacps.h in Headers */, + 833C37C818032CF600CBA602 /* dirac.h in Headers */, 830F0BD617FC4FB900042E8F /* fft.h in Headers */, 830F0CE317FC7F1E00042E8F /* frame_thread_encoder.h in Headers */, 830F0BE617FC4FB900042E8F /* wmadata.h in Headers */, @@ -1116,11 +1226,14 @@ 830F0D2417FC82AB00042E8F /* fifo.h in Headers */, 830F0BD917FC4FB900042E8F /* mathops.h in Headers */, 830F0C3C17FC554D00042E8F /* float_dsp.h in Headers */, + 833C37CF18032D4800CBA602 /* golomb.h in Headers */, 830F0C8117FC7ED100042E8F /* crc.h in Headers */, + 833C379C180328B300CBA602 /* tak.h in Headers */, 830F0DB817FC921D00042E8F /* asm.h in Headers */, 830F0CC517FC7F1E00042E8F /* acelp_pitch_delay.h in Headers */, 830F0D8F17FC8E8B00042E8F /* sbrdsp.h in Headers */, 830F0CCE17FC7F1E00042E8F /* celp_math.h in Headers */, + 833C37B718032AEF00CBA602 /* dsputil_x86.h in Headers */, 830F0CFA17FC7F1E00042E8F /* sinewin.h in Headers */, 830F0CC717FC7F1E00042E8F /* acelp_vectors.h in Headers */, ); @@ -1189,6 +1302,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 833C37A918032AAD00CBA602 /* img2.c in Sources */, 830F0D6717FC8A3300042E8F /* options.c in Sources */, 830F0DAF17FC8FBD00042E8F /* fmtconvert_init.c in Sources */, 830F0DD617FC93E400042E8F /* xwma.c in Sources */, @@ -1203,11 +1317,15 @@ 830F0DAE17FC8FBD00042E8F /* fft_init.c in Sources */, 830F0D2F17FC841B00042E8F /* error.c in Sources */, 830F0BDB17FC4FB900042E8F /* mdct.c in Sources */, + 833C37A618032A5000CBA602 /* apetag.c in Sources */, 830F0D5817FC893E00042E8F /* bitstream.c in Sources */, + 833C37A218032A2F00CBA602 /* rawdec.c in Sources */, 830F0C7617FC7DCB00042E8F /* avstring.c in Sources */, 830F0DCD17FC933100042E8F /* w64.c in Sources */, + 833C37D518032E7000CBA602 /* jrevdct.c in Sources */, 830F0BE217FC4FB900042E8F /* utils.c in Sources */, 830F0CF917FC7F1E00042E8F /* sinewin.c in Sources */, + 833C37C418032CF600CBA602 /* dirac_dwt.c in Sources */, 830F0CE417FC7F1E00042E8F /* h264chroma_template.c in Sources */, 830F0BEF17FC4FB900042E8F /* asf.c in Sources */, 830F0BD517FC4FB900042E8F /* fft.c in Sources */, @@ -1225,13 +1343,16 @@ 830F0D3317FC841B00042E8F /* opt.c in Sources */, 830F0D7317FC8C1300042E8F /* astdec.c in Sources */, 830F0D5317FC891800042E8F /* file_open.c in Sources */, + 833C379D180328B300CBA602 /* takdec.c in Sources */, 830F0D7B17FC8E2400042E8F /* avidec.c in Sources */, 830F0D4F17FC862400042E8F /* utils.c in Sources */, 830F0BEB17FC4FB900042E8F /* wmavoice.c in Sources */, + 833C37B318032AEF00CBA602 /* dsputil.c in Sources */, 830F0CC217FC7F1E00042E8F /* acelp_filters.c in Sources */, 830F0DB017FC8FBD00042E8F /* hpeldsp_init.c in Sources */, 830F0C1B17FC523000042E8F /* url.c in Sources */, 830F0CC417FC7F1E00042E8F /* acelp_pitch_delay.c in Sources */, + 833C37C218032CF600CBA602 /* dirac_arith.c in Sources */, 830F0CEB17FC7F1E00042E8F /* lsp.c in Sources */, 830F0D1317FC815000042E8F /* constants.c in Sources */, 830F0BE417FC4FB900042E8F /* wma_common.c in Sources */, @@ -1242,9 +1363,12 @@ 830F0CE917FC7F1E00042E8F /* imgconvert.c in Sources */, 830F0CE717FC7F1E00042E8F /* hpeldsp.c in Sources */, 830F0CCB17FC7F1E00042E8F /* celp_filters.c in Sources */, + 833C37B518032AEF00CBA602 /* diracdsp_mmx.c in Sources */, 830F0BF717FC4FB900042E8F /* format.c in Sources */, 830F0CC817FC7F1E00042E8F /* avpacket.c in Sources */, + 833C37CE18032D4800CBA602 /* golomb.c in Sources */, 830F0C3917FC554D00042E8F /* des.c in Sources */, + 833C37C918032CF600CBA602 /* diracdec.c in Sources */, 830F0CD217FC7F1E00042E8F /* dct32_float.c in Sources */, 830F0D3217FC841B00042E8F /* intfloat_readwrite.c in Sources */, 830F0DC917FC931700042E8F /* pcm.c in Sources */, @@ -1255,6 +1379,7 @@ 830F0D4D17FC862400042E8F /* time.c in Sources */, 830F0C0817FC4FB900042E8F /* mem.c in Sources */, 830F0CDE17FC7F1E00042E8F /* faanidct.c in Sources */, + 833C379B180328B300CBA602 /* tak.c in Sources */, 830F0CC917FC7F1E00042E8F /* bit_depth_template.c in Sources */, 830F0D4017FC848D00042E8F /* sha.c in Sources */, 830F0BF417FC4FB900042E8F /* aviobuf.c in Sources */, @@ -1275,13 +1400,16 @@ 830F0C8217FC7ED100042E8F /* dict.c in Sources */, 830F0D7817FC8C7300042E8F /* mpeg4audio.c in Sources */, 830F0D5F17FC89BD00042E8F /* options.c in Sources */, + 833C37D318032E4800CBA602 /* dsputil_init.c in Sources */, 830F0DB917FC921D00042E8F /* cpu.c in Sources */, 830F0C7D17FC7E4E00042E8F /* mathematics.c in Sources */, 830F0BDD17FC4FB900042E8F /* parser.c in Sources */, 830F0C5C17FC7CEA00042E8F /* id3v1.c in Sources */, 830F0CFC17FC7F1E00042E8F /* videodsp.c in Sources */, 830F0D8C17FC8E8B00042E8F /* dv_profile.c in Sources */, + 833C37B118032AEF00CBA602 /* diracdsp.c in Sources */, 830F0CD017FC7F1E00042E8F /* dct.c in Sources */, + 833C3795180328A300CBA602 /* takdec.c in Sources */, 830F0DC217FC929E00042E8F /* mux.c in Sources */, 830F0BF517FC4FB900042E8F /* cutils.c in Sources */, 830F0D2E17FC841B00042E8F /* channel_layout.c in Sources */, @@ -1289,13 +1417,17 @@ 830F0BE717FC4FB900042E8F /* wmadec.c in Sources */, 830F0CC617FC7F1E00042E8F /* acelp_vectors.c in Sources */, 830F0DC417FC92EE00042E8F /* videodsp_init.c in Sources */, + 833C379A180328B300CBA602 /* tak_parser.c in Sources */, 830F0C2E17FC551F00042E8F /* asfcrypt.c in Sources */, 830F0D6C17FC8C0700042E8F /* aactab.c in Sources */, 830F0D9217FC8EAC00042E8F /* dvdata.c in Sources */, + 833C37D018032D4800CBA602 /* dirac_dwt.c in Sources */, 830F0BF617FC4FB900042E8F /* file.c in Sources */, 830F0D3817FC844E00042E8F /* parseutils.c in Sources */, 830F0D2017FC82AB00042E8F /* cpu.c in Sources */, + 833C37C618032CF600CBA602 /* dirac_parser.c in Sources */, 830F0CED17FC7F1E00042E8F /* mpeg12data.c in Sources */, + 833C37C718032CF600CBA602 /* dirac.c in Sources */, 830F0D7E17FC8E4800042E8F /* dv.c in Sources */, 830F0C7B17FC7E4E00042E8F /* crc.c in Sources */, 830F0BE817FC4FB900042E8F /* wmalosslessdec.c in Sources */, diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/config.h b/Frameworks/FFMPEG/ffmpeg-minimal/config.h index d2d4794dd..6d5ab128b 100644 --- a/Frameworks/FFMPEG/ffmpeg-minimal/config.h +++ b/Frameworks/FFMPEG/ffmpeg-minimal/config.h @@ -13,6 +13,10 @@ #define TUNECPU generic #define HAVE_MMX 0 #define HAVE_MMX_INLINE 0 +#define HAVE_SSE 0 +#define HAVE_SSE2 0 +#define HAVE_SSSE3_EXTERNAL 0 +#define HAVE_SSE4_EXTERNAL 0 #define emms_c() #define HAVE_MMX2 0 #define HAVE_MMXEXT 0 @@ -28,7 +32,7 @@ #define HAVE_LOCALTIME_R 1 #define HAVE_LRINTF 1 #undef HAVE_VHOOK -#undef CONFIG_ENCODERS +#define CONFIG_ENCODERS 0 #define CONFIG_DECODERS 1 #undef CONFIG_MPEGAUDIO_HP #undef CONFIG_VIDEO4LINUX @@ -61,7 +65,7 @@ #define CONFIG_MEMORY_POISONING 0 #define FF_MEMORY_POISON 0x2a #define CONFIG_MDCT 1 -#define CONFIG_DSPUTIL 0 +#define CONFIG_DSPUTIL 1 #define FFMPEG_CONFIGURATION "Xcode mess lol" #define FFMPEG_LICENSE "LGPL" #undef CONFIG_FFSERVER @@ -134,7 +138,7 @@ #define CONFIG_CSCD_DECODER 0 #define CONFIG_CYUV_DECODER 0 #define CONFIG_DFA_DECODER 0 -#define CONFIG_DIRAC_DECODER 0 +#define CONFIG_DIRAC_DECODER 1 #define CONFIG_DNXHD_ENCODER 0 #define CONFIG_DNXHD_DECODER 0 #define CONFIG_DPX_ENCODER 0 @@ -440,7 +444,7 @@ #define CONFIG_SONIC_DECODER 0 #define CONFIG_SONIC_LS_ENCODER 0 #define CONFIG_SONIC_LS_DECODER 0 -#define CONFIG_TAK_DECODER 0 +#define CONFIG_TAK_DECODER 1 #define CONFIG_TRUEHD_DECODER 0 #define CONFIG_TRUESPEECH_DECODER 0 #define CONFIG_TTA_ENCODER 0 @@ -652,7 +656,7 @@ #define CONFIG_CAVSVIDEO_PARSER 0 #define CONFIG_COOK_PARSER 0 #define CONFIG_DCA_PARSER 0 -#define CONFIG_DIRAC_PARSER 0 +#define CONFIG_DIRAC_PARSER 1 #define CONFIG_DNXHD_PARSER 0 #define CONFIG_DVBSUB_PARSER 0 #define CONFIG_DVDSUB_PARSER 0 @@ -671,7 +675,7 @@ #define CONFIG_PNM_PARSER 0 #define CONFIG_RV30_PARSER 0 #define CONFIG_RV40_PARSER 0 -#define CONFIG_TAK_PARSER 0 +#define CONFIG_TAK_PARSER 1 #define CONFIG_VC1_PARSER 0 #define CONFIG_VORBIS_PARSER 0 #define CONFIG_VP3_PARSER 0 @@ -980,7 +984,7 @@ #define CONFIG_SUBVIEWER_DEMUXER 0 #define CONFIG_SWF_MUXER 0 #define CONFIG_SWF_DEMUXER 0 -#define CONFIG_TAK_DEMUXER 0 +#define CONFIG_TAK_DEMUXER 1 #define CONFIG_TEE_MUXER 0 #define CONFIG_TEDCAPTIONS_DEMUXER 0 #define CONFIG_TG2_MUXER 0 diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.c new file mode 100644 index 000000000..e132acc44 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.c @@ -0,0 +1,334 @@ +/* + * Copyright (C) 2007 Marco Gerards + * Copyright (C) 2009 David Conrad + * Copyright (C) 2011 Jordi Ortiz + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Dirac Decoder + * @author Marco Gerards , David Conrad, Jordi Ortiz + */ + +#include "libavutil/imgutils.h" +#include "dirac.h" +#include "avcodec.h" +#include "golomb.h" +#include "mpeg12data.h" + +/* defaults for source parameters */ +static const dirac_source_params dirac_source_parameters_defaults[] = { + { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 }, + { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 }, + { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 }, + { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 }, + { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 }, + { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 }, + { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 }, + { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 }, + { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 }, + { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 }, + { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 }, + { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 }, + { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 }, + { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 }, + { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 }, + { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 }, + { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 }, + { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 }, + { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 }, + { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 }, + { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 }, +}; + +/* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */ +static const AVRational dirac_preset_aspect_ratios[] = { + {1, 1}, + {10, 11}, + {12, 11}, + {40, 33}, + {16, 11}, + {4, 3}, +}; + +/* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate. + * Table 10.3 Available preset frame rate values + */ +static const AVRational dirac_frame_rate[] = { + {15000, 1001}, + {25, 2}, +}; + +/* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal + * range presets */ +static const struct { + uint8_t bitdepth; + enum AVColorRange color_range; +} pixel_range_presets[] = { + {8, AVCOL_RANGE_JPEG}, + {8, AVCOL_RANGE_MPEG}, + {10, AVCOL_RANGE_MPEG}, + {12, AVCOL_RANGE_MPEG}, +}; + +static const enum AVColorPrimaries dirac_primaries[] = { + AVCOL_PRI_BT709, + AVCOL_PRI_SMPTE170M, + AVCOL_PRI_BT470BG, +}; + +static const struct { + enum AVColorPrimaries color_primaries; + enum AVColorSpace colorspace; + enum AVColorTransferCharacteristic color_trc; +} dirac_color_presets[] = { + { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 }, + { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 }, + { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 }, + { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 }, + { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ }, +}; + +/* [DIRAC_STD] Table 10.2 Supported chroma sampling formats + luma Offset */ +static const enum AVPixelFormat dirac_pix_fmt[2][3] = { + { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P }, + { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P }, +}; + +/* [DIRAC_STD] 10.3 Parse Source Parameters. + * source_parameters(base_video_format) */ +static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb, + dirac_source_params *source) +{ + AVRational frame_rate = {0,0}; + unsigned luma_depth = 8, luma_offset = 16; + int idx; + int chroma_x_shift, chroma_y_shift; + + /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */ + /* [DIRAC_STD] custom_dimensions_flag */ + if (get_bits1(gb)) { + source->width = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */ + source->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */ + } + + /* [DIRAC_STD] 10.3.3 Chroma Sampling Format. + * chroma_sampling_format(video_params) */ + /* [DIRAC_STD] custom_chroma_format_flag */ + if (get_bits1(gb)) + /* [DIRAC_STD] CHROMA_FORMAT_INDEX */ + source->chroma_format = svq3_get_ue_golomb(gb); + if (source->chroma_format > 2U) { + av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n", + source->chroma_format); + return AVERROR_INVALIDDATA; + } + + /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */ + /* [DIRAC_STD] custom_scan_format_flag */ + if (get_bits1(gb)) + /* [DIRAC_STD] SOURCE_SAMPLING */ + source->interlaced = svq3_get_ue_golomb(gb); + if (source->interlaced > 1U) + return AVERROR_INVALIDDATA; + + /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */ + if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */ + source->frame_rate_index = svq3_get_ue_golomb(gb); + + if (source->frame_rate_index > 10U) + return AVERROR_INVALIDDATA; + + if (!source->frame_rate_index) { + /* [DIRAC_STD] FRAME_RATE_NUMER */ + frame_rate.num = svq3_get_ue_golomb(gb); + /* [DIRAC_STD] FRAME_RATE_DENOM */ + frame_rate.den = svq3_get_ue_golomb(gb); + } + } + /* [DIRAC_STD] preset_frame_rate(video_params, index) */ + if (source->frame_rate_index > 0) { + if (source->frame_rate_index <= 8) + frame_rate = ff_mpeg12_frame_rate_tab[source->frame_rate_index]; + else + /* [DIRAC_STD] Table 10.3 values 9-10 */ + frame_rate = dirac_frame_rate[source->frame_rate_index-9]; + } + av_reduce(&avctx->time_base.num, &avctx->time_base.den, + frame_rate.den, frame_rate.num, 1<<30); + + /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio. + * pixel_aspect_ratio(video_params) */ + if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */ + /* [DIRAC_STD] index */ + source->aspect_ratio_index = svq3_get_ue_golomb(gb); + + if (source->aspect_ratio_index > 6U) + return AVERROR_INVALIDDATA; + + if (!source->aspect_ratio_index) { + avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb); + avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb); + } + } + /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel + * aspect ratio values */ + if (source->aspect_ratio_index > 0) + avctx->sample_aspect_ratio = + dirac_preset_aspect_ratios[source->aspect_ratio_index-1]; + + /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */ + if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */ + /* [DIRAC_STD] CLEAN_WIDTH */ + source->clean_width = svq3_get_ue_golomb(gb); + /* [DIRAC_STD] CLEAN_HEIGHT */ + source->clean_height = svq3_get_ue_golomb(gb); + /* [DIRAC_STD] CLEAN_LEFT_OFFSET */ + source->clean_left_offset = svq3_get_ue_golomb(gb); + /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */ + source->clean_right_offset = svq3_get_ue_golomb(gb); + } + + /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params) + * WARNING: Some adaptation seems to be done using the + * AVCOL_RANGE_MPEG/JPEG values */ + if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */ + /* [DIRAC_STD] index */ + source->pixel_range_index = svq3_get_ue_golomb(gb); + + if (source->pixel_range_index > 4U) + return AVERROR_INVALIDDATA; + + /* This assumes either fullrange or MPEG levels only */ + if (!source->pixel_range_index) { + luma_offset = svq3_get_ue_golomb(gb); + luma_depth = av_log2(svq3_get_ue_golomb(gb))+1; + svq3_get_ue_golomb(gb); /* chroma offset */ + svq3_get_ue_golomb(gb); /* chroma excursion */ + avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; + } + } + /* [DIRAC_STD] Table 10.5 + * Available signal range presets <--> pixel_range_presets */ + if (source->pixel_range_index > 0) { + idx = source->pixel_range_index-1; + luma_depth = pixel_range_presets[idx].bitdepth; + avctx->color_range = pixel_range_presets[idx].color_range; + } + + if (luma_depth > 8) + av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8\n"); + + avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format]; + avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_x_shift, &chroma_y_shift); + if ((source->width % (1<height % (1<color_spec_index = svq3_get_ue_golomb(gb); + + if (source->color_spec_index > 4U) + return AVERROR_INVALIDDATA; + + avctx->color_primaries = dirac_color_presets[idx].color_primaries; + avctx->colorspace = dirac_color_presets[idx].colorspace; + avctx->color_trc = dirac_color_presets[idx].color_trc; + + if (!source->color_spec_index) { + /* [DIRAC_STD] 10.3.9.1 Colour primaries */ + if (get_bits1(gb)) { + idx = svq3_get_ue_golomb(gb); + if (idx < 3U) + avctx->color_primaries = dirac_primaries[idx]; + } + /* [DIRAC_STD] 10.3.9.2 Colour matrix */ + if (get_bits1(gb)) { + idx = svq3_get_ue_golomb(gb); + if (!idx) + avctx->colorspace = AVCOL_SPC_BT709; + else if (idx == 1) + avctx->colorspace = AVCOL_SPC_BT470BG; + } + /* [DIRAC_STD] 10.3.9.3 Transfer function */ + if (get_bits1(gb) && !svq3_get_ue_golomb(gb)) + avctx->color_trc = AVCOL_TRC_BT709; + } + } else { + idx = source->color_spec_index; + avctx->color_primaries = dirac_color_presets[idx].color_primaries; + avctx->colorspace = dirac_color_presets[idx].colorspace; + avctx->color_trc = dirac_color_presets[idx].color_trc; + } + + return 0; +} + +/* [DIRAC_STD] 10. Sequence Header. sequence_header() */ +int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, + dirac_source_params *source) +{ + unsigned version_major; + unsigned video_format, picture_coding_mode; + int ret; + + /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */ + version_major = svq3_get_ue_golomb(gb); + svq3_get_ue_golomb(gb); /* version_minor */ + avctx->profile = svq3_get_ue_golomb(gb); + avctx->level = svq3_get_ue_golomb(gb); + /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in + * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */ + video_format = svq3_get_ue_golomb(gb); + + if (version_major < 2) + av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n"); + else if (version_major > 2) + av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n"); + + if (video_format > 20U) + return AVERROR_INVALIDDATA; + + /* Fill in defaults for the source parameters. */ + *source = dirac_source_parameters_defaults[video_format]; + + /* [DIRAC_STD] 10.3 Source Parameters + * Override the defaults. */ + if (ret = parse_source_parameters(avctx, gb, source)) + return ret; + + if (ret = av_image_check_size(source->width, source->height, 0, avctx)) + return ret; + + avcodec_set_dimensions(avctx, source->width, source->height); + + /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames + * currently only used to signal field coding */ + picture_coding_mode = svq3_get_ue_golomb(gb); + if (picture_coding_mode != 0) { + av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d\n", + picture_coding_mode); + return AVERROR_INVALIDDATA; + } + return 0; +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.h new file mode 100644 index 000000000..b0f955bf4 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2007 Marco Gerards + * Copyright (C) 2009 David Conrad + * Copyright (C) 2011 Jordi Ortiz + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_DIRAC_H +#define AVCODEC_DIRAC_H + +/** + * @file + * Interface to Dirac Decoder/Encoder + * @author Marco Gerards + * @author David Conrad + * @author Jordi Ortiz + */ + +#include "avcodec.h" +#include "get_bits.h" + +typedef struct dirac_source_params { + unsigned width; + unsigned height; + uint8_t chroma_format; ///< 0: 444 1: 422 2: 420 + + uint8_t interlaced; + uint8_t top_field_first; + + uint8_t frame_rate_index; ///< index into dirac_frame_rate[] + uint8_t aspect_ratio_index; ///< index into dirac_aspect_ratio[] + + uint16_t clean_width; + uint16_t clean_height; + uint16_t clean_left_offset; + uint16_t clean_right_offset; + + uint8_t pixel_range_index; ///< index into dirac_pixel_range_presets[] + uint8_t color_spec_index; ///< index into dirac_color_spec_presets[] +} dirac_source_params; + +int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, + dirac_source_params *source); + +#endif /* AVCODEC_DIRAC_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.c new file mode 100644 index 000000000..bf913928b --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.c @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2007 Marco Gerards + * Copyright (C) 2009 David Conrad + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Arithmetic decoder for Dirac + * @author Marco Gerards + */ + +#include "dirac_arith.h" + + +const uint16_t ff_dirac_prob[256] = { + 0, 2, 5, 8, 11, 15, 20, 24, + 29, 35, 41, 47, 53, 60, 67, 74, + 82, 89, 97, 106, 114, 123, 132, 141, + 150, 160, 170, 180, 190, 201, 211, 222, + 233, 244, 256, 267, 279, 291, 303, 315, + 327, 340, 353, 366, 379, 392, 405, 419, + 433, 447, 461, 475, 489, 504, 518, 533, + 548, 563, 578, 593, 609, 624, 640, 656, + 672, 688, 705, 721, 738, 754, 771, 788, + 805, 822, 840, 857, 875, 892, 910, 928, + 946, 964, 983, 1001, 1020, 1038, 1057, 1076, + 1095, 1114, 1133, 1153, 1172, 1192, 1211, 1231, + 1251, 1271, 1291, 1311, 1332, 1352, 1373, 1393, + 1414, 1435, 1456, 1477, 1498, 1520, 1541, 1562, + 1584, 1606, 1628, 1649, 1671, 1694, 1716, 1738, + 1760, 1783, 1806, 1828, 1851, 1874, 1897, 1920, + 1935, 1942, 1949, 1955, 1961, 1968, 1974, 1980, + 1985, 1991, 1996, 2001, 2006, 2011, 2016, 2021, + 2025, 2029, 2033, 2037, 2040, 2044, 2047, 2050, + 2053, 2056, 2058, 2061, 2063, 2065, 2066, 2068, + 2069, 2070, 2071, 2072, 2072, 2072, 2072, 2072, + 2072, 2071, 2070, 2069, 2068, 2066, 2065, 2063, + 2060, 2058, 2055, 2052, 2049, 2045, 2042, 2038, + 2033, 2029, 2024, 2019, 2013, 2008, 2002, 1996, + 1989, 1982, 1975, 1968, 1960, 1952, 1943, 1934, + 1925, 1916, 1906, 1896, 1885, 1874, 1863, 1851, + 1839, 1827, 1814, 1800, 1786, 1772, 1757, 1742, + 1727, 1710, 1694, 1676, 1659, 1640, 1622, 1602, + 1582, 1561, 1540, 1518, 1495, 1471, 1447, 1422, + 1396, 1369, 1341, 1312, 1282, 1251, 1219, 1186, + 1151, 1114, 1077, 1037, 995, 952, 906, 857, + 805, 750, 690, 625, 553, 471, 376, 255 +}; + +const uint8_t ff_dirac_next_ctx[DIRAC_CTX_COUNT] = { + [CTX_ZPZN_F1] = CTX_ZP_F2, + [CTX_ZPNN_F1] = CTX_ZP_F2, + [CTX_ZP_F2] = CTX_ZP_F3, + [CTX_ZP_F3] = CTX_ZP_F4, + [CTX_ZP_F4] = CTX_ZP_F5, + [CTX_ZP_F5] = CTX_ZP_F6, + [CTX_ZP_F6] = CTX_ZP_F6, + [CTX_NPZN_F1] = CTX_NP_F2, + [CTX_NPNN_F1] = CTX_NP_F2, + [CTX_NP_F2] = CTX_NP_F3, + [CTX_NP_F3] = CTX_NP_F4, + [CTX_NP_F4] = CTX_NP_F5, + [CTX_NP_F5] = CTX_NP_F6, + [CTX_NP_F6] = CTX_NP_F6, + [CTX_DELTA_Q_F] = CTX_DELTA_Q_F, +}; + +int16_t ff_dirac_prob_branchless[256][2]; + +void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length) +{ + int i; + align_get_bits(gb); + + length = FFMIN(length, get_bits_left(gb)/8); + + c->bytestream = gb->buffer + get_bits_count(gb)/8; + c->bytestream_end = c->bytestream + length; + skip_bits_long(gb, length*8); + + c->low = 0; + for (i = 0; i < 4; i++) { + c->low <<= 8; + if (c->bytestream < c->bytestream_end) + c->low |= *c->bytestream++; + else + c->low |= 0xff; + } + + c->counter = -16; + c->range = 0xffff; + + for (i = 0; i < 256; i++) { + ff_dirac_prob_branchless[i][0] = ff_dirac_prob[255-i]; + ff_dirac_prob_branchless[i][1] = -ff_dirac_prob[i]; + } + + for (i = 0; i < DIRAC_CTX_COUNT; i++) + c->contexts[i] = 0x8000; +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.h new file mode 100644 index 000000000..f9a8bba5f --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_arith.h @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2007 Marco Gerards + * Copyright (C) 2009 David Conrad + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Arithmetic decoder for Dirac + * @author Marco Gerards + */ + +#ifndef AVCODEC_DIRAC_ARITH_H +#define AVCODEC_DIRAC_ARITH_H + +#include "bytestream.h" +#include "get_bits.h" + +enum dirac_arith_contexts { + CTX_ZPZN_F1, + CTX_ZPNN_F1, + CTX_NPZN_F1, + CTX_NPNN_F1, + CTX_ZP_F2, + CTX_ZP_F3, + CTX_ZP_F4, + CTX_ZP_F5, + CTX_ZP_F6, + CTX_NP_F2, + CTX_NP_F3, + CTX_NP_F4, + CTX_NP_F5, + CTX_NP_F6, + CTX_COEFF_DATA, + CTX_SIGN_NEG, + CTX_SIGN_ZERO, + CTX_SIGN_POS, + CTX_ZERO_BLOCK, + CTX_DELTA_Q_F, + CTX_DELTA_Q_DATA, + CTX_DELTA_Q_SIGN, + + DIRAC_CTX_COUNT +}; + +// Dirac resets the arith decoder between decoding various types of data, +// so many contexts are never used simultaneously. Thus, we can reduce +// the number of contexts needed by reusing them. +#define CTX_SB_F1 CTX_ZP_F5 +#define CTX_SB_DATA 0 +#define CTX_PMODE_REF1 0 +#define CTX_PMODE_REF2 1 +#define CTX_GLOBAL_BLOCK 2 +#define CTX_MV_F1 CTX_ZP_F2 +#define CTX_MV_DATA 0 +#define CTX_DC_F1 CTX_ZP_F5 +#define CTX_DC_DATA 0 + +typedef struct { + unsigned low; + uint16_t range; + int16_t counter; + + const uint8_t *bytestream; + const uint8_t *bytestream_end; + + uint16_t contexts[DIRAC_CTX_COUNT]; +} DiracArith; + +extern const uint8_t ff_dirac_next_ctx[DIRAC_CTX_COUNT]; +extern const uint16_t ff_dirac_prob[256]; +extern int16_t ff_dirac_prob_branchless[256][2]; + +static inline void renorm(DiracArith *c) +{ +#if HAVE_FAST_CLZ + int shift = 14 - av_log2_16bit(c->range-1) + ((c->range-1)>>15); + + c->low <<= shift; + c->range <<= shift; + c->counter += shift; +#else + while (c->range <= 0x4000) { + c->low <<= 1; + c->range <<= 1; + c->counter++; + } +#endif +} + +static inline void refill(DiracArith *c) +{ + int counter = c->counter; + + if (counter >= 0) { + int new = bytestream_get_be16(&c->bytestream); + + // the spec defines overread bits to be 1, and streams rely on this + if (c->bytestream > c->bytestream_end) { + new |= 0xff; + if (c->bytestream > c->bytestream_end+1) + new |= 0xff00; + + c->bytestream = c->bytestream_end; + } + + c->low += new << counter; + counter -= 16; + } + c->counter = counter; +} + +static inline int dirac_get_arith_bit(DiracArith *c, int ctx) +{ + int prob_zero = c->contexts[ctx]; + int range_times_prob, bit; + unsigned low = c->low; + int range = c->range; + + range_times_prob = (c->range * prob_zero) >> 16; + +#if HAVE_FAST_CMOV && HAVE_INLINE_ASM + low -= range_times_prob << 16; + range -= range_times_prob; + bit = 0; + __asm__( + "cmpl %5, %4 \n\t" + "setae %b0 \n\t" + "cmovb %3, %2 \n\t" + "cmovb %5, %1 \n\t" + : "+q"(bit), "+r"(range), "+r"(low) + : "r"(c->low), "r"(c->low>>16), + "r"(range_times_prob) + ); +#else + bit = (low >> 16) >= range_times_prob; + if (bit) { + low -= range_times_prob << 16; + range -= range_times_prob; + } else { + range = range_times_prob; + } +#endif + + c->contexts[ctx] += ff_dirac_prob_branchless[prob_zero>>8][bit]; + c->low = low; + c->range = range; + + renorm(c); + refill(c); + return bit; +} + +static inline int dirac_get_arith_uint(DiracArith *c, int follow_ctx, int data_ctx) +{ + int ret = 1; + while (!dirac_get_arith_bit(c, follow_ctx)) { + ret <<= 1; + ret += dirac_get_arith_bit(c, data_ctx); + follow_ctx = ff_dirac_next_ctx[follow_ctx]; + } + return ret-1; +} + +static inline int dirac_get_arith_int(DiracArith *c, int follow_ctx, int data_ctx) +{ + int ret = dirac_get_arith_uint(c, follow_ctx, data_ctx); + if (ret && dirac_get_arith_bit(c, data_ctx+1)) + ret = -ret; + return ret; +} + +void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length); + +#endif /* AVCODEC_DIRAC_ARITH_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.c new file mode 100644 index 000000000..a4d38de9e --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.c @@ -0,0 +1,572 @@ +/* + * Copyright (C) 2004-2010 Michael Niedermayer + * Copyright (C) 2008 David Conrad + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" +#include "libavutil/attributes.h" +#include "libavutil/avassert.h" +#include "libavutil/common.h" +#include "dsputil.h" +#include "dirac_dwt.h" +#include "libavcodec/x86/dirac_dwt.h" + + +static inline int mirror(int v, int m) +{ + while ((unsigned)v > (unsigned)m) { + v = -v; + if (v < 0) + v += 2 * m; + } + return v; +} + +static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, + int width) +{ + int i; + + for (i = 0; i < width; i++) + b1[i] -= (b0[i] + b2[i] + 2) >> 2; +} + + +static av_always_inline +void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift) +{ + int i; + for (i = 0; i < w2; i++) { + dst[2*i ] = (src0[i] + add) >> shift; + dst[2*i+1] = (src1[i] + add) >> shift; + } +} + +static void horizontal_compose_dirac53i(IDWTELEM *b, IDWTELEM *temp, int w) +{ + const int w2 = w >> 1; + int x; + + temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]); + for (x = 1; x < w2; x++) { + temp[x ] = COMPOSE_53iL0 (b[x+w2-1], b[x ], b[x+w2]); + temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]); + } + temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]); + + interleave(b, temp, temp+w2, w2, 1, 1); +} + +static void horizontal_compose_dd97i(IDWTELEM *b, IDWTELEM *tmp, int w) +{ + const int w2 = w >> 1; + int x; + + tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]); + for (x = 1; x < w2; x++) + tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]); + + // extend the edges + tmp[-1] = tmp[0]; + tmp[w2+1] = tmp[w2] = tmp[w2-1]; + + for (x = 0; x < w2; x++) { + b[2*x ] = (tmp[x] + 1)>>1; + b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1; + } +} + +static void horizontal_compose_dd137i(IDWTELEM *b, IDWTELEM *tmp, int w) +{ + const int w2 = w >> 1; + int x; + + tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2 ], b[w2+1]); + tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]); + for (x = 2; x < w2-1; x++) + tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]); + tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]); + + // extend the edges + tmp[-1] = tmp[0]; + tmp[w2+1] = tmp[w2] = tmp[w2-1]; + + for (x = 0; x < w2; x++) { + b[2*x ] = (tmp[x] + 1)>>1; + b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1; + } +} + +static av_always_inline +void horizontal_compose_haari(IDWTELEM *b, IDWTELEM *temp, int w, int shift) +{ + const int w2 = w >> 1; + int x; + + for (x = 0; x < w2; x++) { + temp[x ] = COMPOSE_HAARiL0(b[x ], b[x+w2]); + temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]); + } + + interleave(b, temp, temp+w2, w2, shift, shift); +} + +static void horizontal_compose_haar0i(IDWTELEM *b, IDWTELEM *temp, int w) +{ + horizontal_compose_haari(b, temp, w, 0); +} + +static void horizontal_compose_haar1i(IDWTELEM *b, IDWTELEM *temp, int w) +{ + horizontal_compose_haari(b, temp, w, 1); +} + +static void horizontal_compose_fidelityi(IDWTELEM *b, IDWTELEM *tmp, int w) +{ + const int w2 = w >> 1; + int i, x; + IDWTELEM v[8]; + + for (x = 0; x < w2; x++) { + for (i = 0; i < 8; i++) + v[i] = b[av_clip(x-3+i, 0, w2-1)]; + tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]); + } + + for (x = 0; x < w2; x++) { + for (i = 0; i < 8; i++) + v[i] = tmp[av_clip(x-4+i, 0, w2-1)]; + tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]); + } + + interleave(b, tmp+w2, tmp, w2, 0, 0); +} + +static void horizontal_compose_daub97i(IDWTELEM *b, IDWTELEM *temp, int w) +{ + const int w2 = w >> 1; + int x, b0, b1, b2; + + temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]); + for (x = 1; x < w2; x++) { + temp[x ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x ], b[x+w2]); + temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]); + } + temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]); + + // second stage combined with interleave and shift + b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]); + b[0] = (b0 + 1) >> 1; + for (x = 1; x < w2; x++) { + b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x ], temp[x+w2]); + b1 = COMPOSE_DAUB97iH0( b0, temp[x+w2-1], b2 ); + b[2*x-1] = (b1 + 1) >> 1; + b[2*x ] = (b2 + 1) >> 1; + b0 = b2; + } + b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1; +} + +static void vertical_compose_dirac53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width) +{ + int i; + + for(i=0; ivertical_compose_l0; + vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0; + DWTCompose *cs = d->cs + level; + + int i, y = cs->y; + IDWTELEM *b[8]; + for (i = 0; i < 6; i++) + b[i] = cs->b[i]; + b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride; + b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride; + + if(y+5<(unsigned)height) vertical_compose_l0( b[5], b[6], b[7], width); + if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width); + + if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width); + if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width); + + for (i = 0; i < 6; i++) + cs->b[i] = b[i+2]; + cs->y += 2; +} + +static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int height, int stride) +{ + vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0; + vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0; + DWTCompose *cs = d->cs + level; + + int y= cs->y; + IDWTELEM *b[4] = { cs->b[0], cs->b[1] }; + b[2] = d->buffer + mirror(y+1, height-1)*stride; + b[3] = d->buffer + mirror(y+2, height-1)*stride; + + if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width); + if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width); + + if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width); + if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width); + + cs->b[0] = b[2]; + cs->b[1] = b[3]; + cs->y += 2; +} + + +static void spatial_compose_dd137i_dy(DWTContext *d, int level, int width, int height, int stride) +{ + vertical_compose_5tap vertical_compose_l0 = (void*)d->vertical_compose_l0; + vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0; + DWTCompose *cs = d->cs + level; + + int i, y = cs->y; + IDWTELEM *b[10]; + for (i = 0; i < 8; i++) + b[i] = cs->b[i]; + b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride; + b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride; + + if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width); + if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width); + + if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width); + if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width); + + for (i = 0; i < 8; i++) + cs->b[i] = b[i+2]; + cs->y += 2; +} + +// haar makes the assumption that height is even (always true for dirac) +static void spatial_compose_haari_dy(DWTContext *d, int level, int width, int height, int stride) +{ + vertical_compose_2tap vertical_compose = (void*)d->vertical_compose; + int y = d->cs[level].y; + IDWTELEM *b0 = d->buffer + (y-1)*stride; + IDWTELEM *b1 = d->buffer + (y )*stride; + + vertical_compose(b0, b1, width); + d->horizontal_compose(b0, d->temp, width); + d->horizontal_compose(b1, d->temp, width); + + d->cs[level].y += 2; +} + +// Don't do sliced idwt for fidelity; the 9 tap filter makes it a bit annoying +// Fortunately, this filter isn't used in practice. +static void spatial_compose_fidelity(DWTContext *d, int level, int width, int height, int stride) +{ + vertical_compose_9tap vertical_compose_l0 = (void*)d->vertical_compose_l0; + vertical_compose_9tap vertical_compose_h0 = (void*)d->vertical_compose_h0; + int i, y; + IDWTELEM *b[8]; + + for (y = 1; y < height; y += 2) { + for (i = 0; i < 8; i++) + b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride; + vertical_compose_h0(d->buffer + y*stride, b, width); + } + + for (y = 0; y < height; y += 2) { + for (i = 0; i < 8; i++) + b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride; + vertical_compose_l0(d->buffer + y*stride, b, width); + } + + for (y = 0; y < height; y++) + d->horizontal_compose(d->buffer + y*stride, d->temp, width); + + d->cs[level].y = height+1; +} + +static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int height, int stride) +{ + vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0; + vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0; + vertical_compose_3tap vertical_compose_l1 = (void*)d->vertical_compose_l1; + vertical_compose_3tap vertical_compose_h1 = (void*)d->vertical_compose_h1; + DWTCompose *cs = d->cs + level; + + int i, y = cs->y; + IDWTELEM *b[6]; + for (i = 0; i < 4; i++) + b[i] = cs->b[i]; + b[4] = d->buffer + mirror(y+3, height-1)*stride; + b[5] = d->buffer + mirror(y+4, height-1)*stride; + + if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width); + if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width); + if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width); + if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width); + + if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width); + if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width); + + for (i = 0; i < 4; i++) + cs->b[i] = b[i+2]; + cs->y += 2; +} + + +static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride) +{ + cs->b[0] = buffer + mirror(-3-1, height-1)*stride; + cs->b[1] = buffer + mirror(-3 , height-1)*stride; + cs->b[2] = buffer + mirror(-3+1, height-1)*stride; + cs->b[3] = buffer + mirror(-3+2, height-1)*stride; + cs->y = -3; +} + +static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride) +{ + cs->b[0] = buffer + mirror(-1-1, height-1)*stride; + cs->b[1] = buffer + mirror(-1 , height-1)*stride; + cs->y = -1; +} + +static void spatial_compose_dd97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride) +{ + cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride; + cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride; + cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride; + cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride; + cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride; + cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride; + cs->y = -5; +} + +static void spatial_compose_dd137i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride) +{ + cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride; + cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride; + cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride; + cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride; + cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride; + cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride; + cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride; + cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride; + cs->y = -5; +} + +int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height, + int stride, enum dwt_type type, int decomposition_count, + IDWTELEM *temp) +{ + int level; + + d->buffer = buffer; + d->width = width; + d->height = height; + d->stride = stride; + d->decomposition_count = decomposition_count; + d->temp = temp + 8; + + for(level=decomposition_count-1; level>=0; level--){ + int hl = height >> level; + int stride_l = stride << level; + + switch(type){ + case DWT_DIRAC_DD9_7: + spatial_compose_dd97i_init(d->cs+level, buffer, hl, stride_l); + break; + case DWT_DIRAC_LEGALL5_3: + spatial_compose53i_init2(d->cs+level, buffer, hl, stride_l); + break; + case DWT_DIRAC_DD13_7: + spatial_compose_dd137i_init(d->cs+level, buffer, hl, stride_l); + break; + case DWT_DIRAC_HAAR0: + case DWT_DIRAC_HAAR1: + d->cs[level].y = 1; + break; + case DWT_DIRAC_DAUB9_7: + spatial_compose97i_init2(d->cs+level, buffer, hl, stride_l); + break; + default: + d->cs[level].y = 0; + break; + } + } + + switch (type) { + case DWT_DIRAC_DD9_7: + d->spatial_compose = spatial_compose_dd97i_dy; + d->vertical_compose_l0 = (void*)vertical_compose53iL0; + d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0; + d->horizontal_compose = horizontal_compose_dd97i; + d->support = 7; + break; + case DWT_DIRAC_LEGALL5_3: + d->spatial_compose = spatial_compose_dirac53i_dy; + d->vertical_compose_l0 = (void*)vertical_compose53iL0; + d->vertical_compose_h0 = (void*)vertical_compose_dirac53iH0; + d->horizontal_compose = horizontal_compose_dirac53i; + d->support = 3; + break; + case DWT_DIRAC_DD13_7: + d->spatial_compose = spatial_compose_dd137i_dy; + d->vertical_compose_l0 = (void*)vertical_compose_dd137iL0; + d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0; + d->horizontal_compose = horizontal_compose_dd137i; + d->support = 7; + break; + case DWT_DIRAC_HAAR0: + case DWT_DIRAC_HAAR1: + d->spatial_compose = spatial_compose_haari_dy; + d->vertical_compose = (void*)vertical_compose_haar; + if (type == DWT_DIRAC_HAAR0) + d->horizontal_compose = horizontal_compose_haar0i; + else + d->horizontal_compose = horizontal_compose_haar1i; + d->support = 1; + break; + case DWT_DIRAC_FIDELITY: + d->spatial_compose = spatial_compose_fidelity; + d->vertical_compose_l0 = (void*)vertical_compose_fidelityiL0; + d->vertical_compose_h0 = (void*)vertical_compose_fidelityiH0; + d->horizontal_compose = horizontal_compose_fidelityi; + break; + case DWT_DIRAC_DAUB9_7: + d->spatial_compose = spatial_compose_daub97i_dy; + d->vertical_compose_l0 = (void*)vertical_compose_daub97iL0; + d->vertical_compose_h0 = (void*)vertical_compose_daub97iH0; + d->vertical_compose_l1 = (void*)vertical_compose_daub97iL1; + d->vertical_compose_h1 = (void*)vertical_compose_daub97iH1; + d->horizontal_compose = horizontal_compose_daub97i; + d->support = 5; + break; + default: + av_log(NULL, AV_LOG_ERROR, "Unknown wavelet type %d\n", type); + return -1; + } + + if (HAVE_MMX) ff_spatial_idwt_init_mmx(d, type); + + return 0; +} + +void ff_spatial_idwt_slice2(DWTContext *d, int y) +{ + int level, support = d->support; + + for (level = d->decomposition_count-1; level >= 0; level--) { + int wl = d->width >> level; + int hl = d->height >> level; + int stride_l = d->stride << level; + + while (d->cs[level].y <= FFMIN((y>>level)+support, hl)) + d->spatial_compose(d, level, wl, hl, stride_l); + } +} + diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.h new file mode 100644 index 000000000..25c13d1a6 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_dwt.h @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2004-2010 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_DIRAC_DWT_H +#define AVCODEC_DIRAC_DWT_H + +#include + +typedef int DWTELEM; +typedef short IDWTELEM; + +#define MAX_DWT_SUPPORT 8 +#define MAX_DECOMPOSITIONS 8 + +typedef struct DWTCompose { + IDWTELEM *b[MAX_DWT_SUPPORT]; + int y; +} DWTCompose; + +struct DWTContext; + +// Possible prototypes for vertical_compose functions +typedef void (*vertical_compose_2tap)(IDWTELEM *b0, IDWTELEM *b1, int width); +typedef void (*vertical_compose_3tap)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width); +typedef void (*vertical_compose_5tap)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, int width); +typedef void (*vertical_compose_9tap)(IDWTELEM *dst, IDWTELEM *b[8], int width); + +typedef struct DWTContext { + IDWTELEM *buffer; + IDWTELEM *temp; + int width; + int height; + int stride; + int decomposition_count; + int support; + + void (*spatial_compose)(struct DWTContext *cs, int level, int width, int height, int stride); + void (*vertical_compose_l0)(void); + void (*vertical_compose_h0)(void); + void (*vertical_compose_l1)(void); + void (*vertical_compose_h1)(void); + void (*vertical_compose)(void); ///< one set of lowpass and highpass combined + void (*horizontal_compose)(IDWTELEM *b, IDWTELEM *tmp, int width); + + DWTCompose cs[MAX_DECOMPOSITIONS]; +} DWTContext; + +enum dwt_type { + DWT_SNOW_DAUB9_7, + DWT_SNOW_LEGALL5_3, + DWT_DIRAC_DD9_7, + DWT_DIRAC_LEGALL5_3, + DWT_DIRAC_DD13_7, + DWT_DIRAC_HAAR0, + DWT_DIRAC_HAAR1, + DWT_DIRAC_FIDELITY, + DWT_DIRAC_DAUB9_7, + DWT_NUM_TYPES +}; + +// -1 if an error occurred, e.g. the dwt_type isn't recognized +int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height, + int stride, enum dwt_type type, int decomposition_count, + IDWTELEM *temp); + +void ff_spatial_idwt_slice2(DWTContext *d, int y); + +// shared stuff for simd optimiztions +#define COMPOSE_53iL0(b0, b1, b2)\ + (b1 - ((b0 + b2 + 2) >> 2)) + +#define COMPOSE_DIRAC53iH0(b0, b1, b2)\ + (b1 + ((b0 + b2 + 1) >> 1)) + +#define COMPOSE_DD97iH0(b0, b1, b2, b3, b4)\ + (b2 + ((-b0 + 9*b1 + 9*b3 - b4 + 8) >> 4)) + +#define COMPOSE_DD137iL0(b0, b1, b2, b3, b4)\ + (b2 - ((-b0 + 9*b1 + 9*b3 - b4 + 16) >> 5)) + +#define COMPOSE_HAARiL0(b0, b1)\ + (b0 - ((b1 + 1) >> 1)) + +#define COMPOSE_HAARiH0(b0, b1)\ + (b0 + b1) + +#define COMPOSE_FIDELITYiL0(b0, b1, b2, b3, b4, b5, b6, b7, b8)\ + (b4 - ((-8*(b0+b8) + 21*(b1+b7) - 46*(b2+b6) + 161*(b3+b5) + 128) >> 8)) + +#define COMPOSE_FIDELITYiH0(b0, b1, b2, b3, b4, b5, b6, b7, b8)\ + (b4 + ((-2*(b0+b8) + 10*(b1+b7) - 25*(b2+b6) + 81*(b3+b5) + 128) >> 8)) + +#define COMPOSE_DAUB97iL1(b0, b1, b2)\ + (b1 - ((1817*(b0 + b2) + 2048) >> 12)) + +#define COMPOSE_DAUB97iH1(b0, b1, b2)\ + (b1 - (( 113*(b0 + b2) + 64) >> 7)) + +#define COMPOSE_DAUB97iL0(b0, b1, b2)\ + (b1 + (( 217*(b0 + b2) + 2048) >> 12)) + +#define COMPOSE_DAUB97iH0(b0, b1, b2)\ + (b1 + ((6497*(b0 + b2) + 2048) >> 12)) + + +#endif /* AVCODEC_DWT_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_parser.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_parser.c new file mode 100644 index 000000000..a2a22ee5b --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/dirac_parser.c @@ -0,0 +1,260 @@ +/* + * Dirac parser + * + * Copyright (c) 2007-2008 Marco Gerards + * Copyright (c) 2008 BBC, Anuradha Suraparaju + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Dirac Parser + * @author Marco Gerards + */ + +#include + +#include "libavutil/intreadwrite.h" +#include "libavutil/mem.h" +#include "parser.h" + +#define DIRAC_PARSE_INFO_PREFIX 0x42424344 + +/** + * Find the end of the current frame in the bitstream. + * @return the position of the first byte of the next frame or -1 + */ +typedef struct DiracParseContext { + int state; + int is_synced; + int sync_offset; + int header_bytes_needed; + int overread_index; + int buffer_size; + int index; + uint8_t *buffer; + int dirac_unit_size; + uint8_t *dirac_unit; +} DiracParseContext; + +static int find_frame_end(DiracParseContext *pc, + const uint8_t *buf, int buf_size) +{ + uint32_t state = pc->state; + int i = 0; + + if (!pc->is_synced) { + for (i = 0; i < buf_size; i++) { + state = (state << 8) | buf[i]; + if (state == DIRAC_PARSE_INFO_PREFIX) { + state = -1; + pc->is_synced = 1; + pc->header_bytes_needed = 9; + pc->sync_offset = i; + break; + } + } + } + + if (pc->is_synced) { + pc->sync_offset = 0; + for (; i < buf_size; i++) { + if (state == DIRAC_PARSE_INFO_PREFIX) { + if ((buf_size-i) >= pc->header_bytes_needed) { + pc->state = -1; + return i + pc->header_bytes_needed; + } else { + pc->header_bytes_needed = 9-(buf_size-i); + break; + } + } else + state = (state << 8) | buf[i]; + } + } + pc->state = state; + return -1; +} + +typedef struct DiracParseUnit +{ + int next_pu_offset; + int prev_pu_offset; + uint8_t pu_type; +} DiracParseUnit; + +static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc, + int offset) +{ + uint8_t *start = pc->buffer + offset; + uint8_t *end = pc->buffer + pc->index; + if (start < pc->buffer || (start+13 > end)) + return 0; + pu->pu_type = start[4]; + + pu->next_pu_offset = AV_RB32(start+5); + pu->prev_pu_offset = AV_RB32(start+9); + + if (pu->pu_type == 0x10 && pu->next_pu_offset == 0) + pu->next_pu_offset = 13; + + return 1; +} + +static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx, + int next, const uint8_t **buf, int *buf_size) +{ + int parse_timing_info = (s->pts == AV_NOPTS_VALUE && + s->dts == AV_NOPTS_VALUE); + DiracParseContext *pc = s->priv_data; + + if (pc->overread_index) { + memcpy(pc->buffer, pc->buffer + pc->overread_index, + pc->index - pc->overread_index); + pc->index -= pc->overread_index; + pc->overread_index = 0; + if (*buf_size == 0 && pc->buffer[4] == 0x10) { + *buf = pc->buffer; + *buf_size = pc->index; + return 0; + } + } + + if ( next == -1) { + /* Found a possible frame start but not a frame end */ + void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, + pc->index + (*buf_size - + pc->sync_offset)); + pc->buffer = new_buffer; + memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset), + *buf_size - pc->sync_offset); + pc->index += *buf_size - pc->sync_offset; + return -1; + } else { + /* Found a possible frame start and a possible frame end */ + DiracParseUnit pu1, pu; + void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, + pc->index + next); + pc->buffer = new_buffer; + memcpy(pc->buffer + pc->index, *buf, next); + pc->index += next; + + /* Need to check if we have a valid Parse Unit. We can't go by the + * sync pattern 'BBCD' alone because arithmetic coding of the residual + * and motion data can cause the pattern triggering a false start of + * frame. So check if the previous parse offset of the next parse unit + * is equal to the next parse offset of the current parse unit then + * we can be pretty sure that we have a valid parse unit */ + if (!unpack_parse_unit(&pu1, pc, pc->index - 13) || + !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) || + pu.next_pu_offset != pu1.prev_pu_offset || + pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset + ) { + pc->index -= 9; + *buf_size = next-9; + pc->header_bytes_needed = 9; + return -1; + } + + /* All non-frame data must be accompanied by frame data. This is to + * ensure that pts is set correctly. So if the current parse unit is + * not frame data, wait for frame data to come along */ + + pc->dirac_unit = pc->buffer + pc->index - 13 - + pu1.prev_pu_offset - pc->dirac_unit_size; + + pc->dirac_unit_size += pu.next_pu_offset; + + if ((pu.pu_type&0x08) != 0x08) { + pc->header_bytes_needed = 9; + *buf_size = next; + return -1; + } + + /* Get the picture number to set the pts and dts*/ + if (parse_timing_info) { + uint8_t *cur_pu = pc->buffer + + pc->index - 13 - pu1.prev_pu_offset; + int pts = AV_RB32(cur_pu + 13); + if (s->last_pts == 0 && s->last_dts == 0) + s->dts = pts - 1; + else + s->dts = s->last_dts+1; + s->pts = pts; + if (!avctx->has_b_frames && (cur_pu[4] & 0x03)) + avctx->has_b_frames = 1; + } + if (avctx->has_b_frames && s->pts == s->dts) + s->pict_type = AV_PICTURE_TYPE_B; + + /* Finally have a complete Dirac data unit */ + *buf = pc->dirac_unit; + *buf_size = pc->dirac_unit_size; + + pc->dirac_unit_size = 0; + pc->overread_index = pc->index-13; + pc->header_bytes_needed = 9; + } + return next; +} + +static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + DiracParseContext *pc = s->priv_data; + int next; + + *poutbuf = NULL; + *poutbuf_size = 0; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { + next = buf_size; + *poutbuf = buf; + *poutbuf_size = buf_size; + /* Assume that data has been packetized into an encapsulation unit. */ + } else { + next = find_frame_end(pc, buf, buf_size); + if (!pc->is_synced && next == -1) { + /* No frame start found yet. So throw away the entire buffer. */ + return buf_size; + } + + if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) { + return buf_size; + } + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + +static void dirac_parse_close(AVCodecParserContext *s) +{ + DiracParseContext *pc = s->priv_data; + + if (pc->buffer_size > 0) + av_free(pc->buffer); +} + +AVCodecParser ff_dirac_parser = { + .codec_ids = { AV_CODEC_ID_DIRAC }, + .priv_data_size = sizeof(DiracParseContext), + .parser_parse = dirac_parse, + .parser_close = dirac_parse_close, +}; diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdec.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdec.c new file mode 100644 index 000000000..15baf7510 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdec.c @@ -0,0 +1,1939 @@ +/* + * Copyright (C) 2007 Marco Gerards + * Copyright (C) 2009 David Conrad + * Copyright (C) 2011 Jordi Ortiz + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Dirac Decoder + * @author Marco Gerards , David Conrad, Jordi Ortiz + */ + +#include "avcodec.h" +#include "dsputil.h" +#include "get_bits.h" +#include "bytestream.h" +#include "internal.h" +#include "golomb.h" +#include "dirac_arith.h" +#include "mpeg12data.h" +#include "dirac_dwt.h" +#include "dirac.h" +#include "diracdsp.h" +#include "videodsp.h" // for ff_emulated_edge_mc_8 + +/** + * The spec limits the number of wavelet decompositions to 4 for both + * level 1 (VC-2) and 128 (long-gop default). + * 5 decompositions is the maximum before >16-bit buffers are needed. + * Schroedinger allows this for DD 9,7 and 13,7 wavelets only, limiting + * the others to 4 decompositions (or 3 for the fidelity filter). + * + * We use this instead of MAX_DECOMPOSITIONS to save some memory. + */ +#define MAX_DWT_LEVELS 5 + +/** + * The spec limits this to 3 for frame coding, but in practice can be as high as 6 + */ +#define MAX_REFERENCE_FRAMES 8 +#define MAX_DELAY 5 /* limit for main profile for frame coding (TODO: field coding) */ +#define MAX_FRAMES (MAX_REFERENCE_FRAMES + MAX_DELAY + 1) +#define MAX_QUANT 68 /* max quant for VC-2 */ +#define MAX_BLOCKSIZE 32 /* maximum xblen/yblen we support */ + +/** + * DiracBlock->ref flags, if set then the block does MC from the given ref + */ +#define DIRAC_REF_MASK_REF1 1 +#define DIRAC_REF_MASK_REF2 2 +#define DIRAC_REF_MASK_GLOBAL 4 + +/** + * Value of Picture.reference when Picture is not a reference picture, but + * is held for delayed output. + */ +#define DELAYED_PIC_REF 4 + +#define ff_emulated_edge_mc ff_emulated_edge_mc_8 /* Fix: change the calls to this function regarding bit depth */ + +#define CALC_PADDING(size, depth) \ + (((size + (1 << depth) - 1) >> depth) << depth) + +#define DIVRNDUP(a, b) (((a) + (b) - 1) / (b)) + +typedef struct { + AVFrame avframe; + int interpolated[3]; /* 1 if hpel[] is valid */ + uint8_t *hpel[3][4]; + uint8_t *hpel_base[3][4]; +} DiracFrame; + +typedef struct { + union { + int16_t mv[2][2]; + int16_t dc[3]; + } u; /* anonymous unions aren't in C99 :( */ + uint8_t ref; +} DiracBlock; + +typedef struct SubBand { + int level; + int orientation; + int stride; + int width; + int height; + int quant; + IDWTELEM *ibuf; + struct SubBand *parent; + + /* for low delay */ + unsigned length; + const uint8_t *coeff_data; +} SubBand; + +typedef struct Plane { + int width; + int height; + ptrdiff_t stride; + + int idwt_width; + int idwt_height; + int idwt_stride; + IDWTELEM *idwt_buf; + IDWTELEM *idwt_buf_base; + IDWTELEM *idwt_tmp; + + /* block length */ + uint8_t xblen; + uint8_t yblen; + /* block separation (block n+1 starts after this many pixels in block n) */ + uint8_t xbsep; + uint8_t ybsep; + /* amount of overspill on each edge (half of the overlap between blocks) */ + uint8_t xoffset; + uint8_t yoffset; + + SubBand band[MAX_DWT_LEVELS][4]; +} Plane; + +typedef struct DiracContext { + AVCodecContext *avctx; + DSPContext dsp; + DiracDSPContext diracdsp; + GetBitContext gb; + dirac_source_params source; + int seen_sequence_header; + int frame_number; /* number of the next frame to display */ + Plane plane[3]; + int chroma_x_shift; + int chroma_y_shift; + + int zero_res; /* zero residue flag */ + int is_arith; /* whether coeffs use arith or golomb coding */ + int low_delay; /* use the low delay syntax */ + int globalmc_flag; /* use global motion compensation */ + int num_refs; /* number of reference pictures */ + + /* wavelet decoding */ + unsigned wavelet_depth; /* depth of the IDWT */ + unsigned wavelet_idx; + + /** + * schroedinger older than 1.0.8 doesn't store + * quant delta if only one codebook exists in a band + */ + unsigned old_delta_quant; + unsigned codeblock_mode; + + struct { + unsigned width; + unsigned height; + } codeblock[MAX_DWT_LEVELS+1]; + + struct { + unsigned num_x; /* number of horizontal slices */ + unsigned num_y; /* number of vertical slices */ + AVRational bytes; /* average bytes per slice */ + uint8_t quant[MAX_DWT_LEVELS][4]; /* [DIRAC_STD] E.1 */ + } lowdelay; + + struct { + int pan_tilt[2]; /* pan/tilt vector */ + int zrs[2][2]; /* zoom/rotate/shear matrix */ + int perspective[2]; /* perspective vector */ + unsigned zrs_exp; + unsigned perspective_exp; + } globalmc[2]; + + /* motion compensation */ + uint8_t mv_precision; /* [DIRAC_STD] REFS_WT_PRECISION */ + int16_t weight[2]; /* [DIRAC_STD] REF1_WT and REF2_WT */ + unsigned weight_log2denom; /* [DIRAC_STD] REFS_WT_PRECISION */ + + int blwidth; /* number of blocks (horizontally) */ + int blheight; /* number of blocks (vertically) */ + int sbwidth; /* number of superblocks (horizontally) */ + int sbheight; /* number of superblocks (vertically) */ + + uint8_t *sbsplit; + DiracBlock *blmotion; + + uint8_t *edge_emu_buffer[4]; + uint8_t *edge_emu_buffer_base; + + uint16_t *mctmp; /* buffer holding the MC data multipled by OBMC weights */ + uint8_t *mcscratch; + + DECLARE_ALIGNED(16, uint8_t, obmc_weight)[3][MAX_BLOCKSIZE*MAX_BLOCKSIZE]; + + void (*put_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h); + void (*avg_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h); + void (*add_obmc)(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen); + dirac_weight_func weight_func; + dirac_biweight_func biweight_func; + + DiracFrame *current_picture; + DiracFrame *ref_pics[2]; + + DiracFrame *ref_frames[MAX_REFERENCE_FRAMES+1]; + DiracFrame *delay_frames[MAX_DELAY+1]; + DiracFrame all_frames[MAX_FRAMES]; +} DiracContext; + +/** + * Dirac Specification -> + * Parse code values. 9.6.1 Table 9.1 + */ +enum dirac_parse_code { + pc_seq_header = 0x00, + pc_eos = 0x10, + pc_aux_data = 0x20, + pc_padding = 0x30, +}; + +enum dirac_subband { + subband_ll = 0, + subband_hl = 1, + subband_lh = 2, + subband_hh = 3 +}; + +static const uint8_t default_qmat[][4][4] = { + { { 5, 3, 3, 0}, { 0, 4, 4, 1}, { 0, 5, 5, 2}, { 0, 6, 6, 3} }, + { { 4, 2, 2, 0}, { 0, 4, 4, 2}, { 0, 5, 5, 3}, { 0, 7, 7, 5} }, + { { 5, 3, 3, 0}, { 0, 4, 4, 1}, { 0, 5, 5, 2}, { 0, 6, 6, 3} }, + { { 8, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0} }, + { { 8, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0} }, + { { 0, 4, 4, 8}, { 0, 8, 8, 12}, { 0, 13, 13, 17}, { 0, 17, 17, 21} }, + { { 3, 1, 1, 0}, { 0, 4, 4, 2}, { 0, 6, 6, 5}, { 0, 9, 9, 7} }, +}; + +static const int qscale_tab[MAX_QUANT+1] = { + 4, 5, 6, 7, 8, 10, 11, 13, + 16, 19, 23, 27, 32, 38, 45, 54, + 64, 76, 91, 108, 128, 152, 181, 215, + 256, 304, 362, 431, 512, 609, 724, 861, + 1024, 1218, 1448, 1722, 2048, 2435, 2896, 3444, + 4096, 4871, 5793, 6889, 8192, 9742, 11585, 13777, + 16384, 19484, 23170, 27554, 32768, 38968, 46341, 55109, + 65536, 77936 +}; + +static const int qoffset_intra_tab[MAX_QUANT+1] = { + 1, 2, 3, 4, 4, 5, 6, 7, + 8, 10, 12, 14, 16, 19, 23, 27, + 32, 38, 46, 54, 64, 76, 91, 108, + 128, 152, 181, 216, 256, 305, 362, 431, + 512, 609, 724, 861, 1024, 1218, 1448, 1722, + 2048, 2436, 2897, 3445, 4096, 4871, 5793, 6889, + 8192, 9742, 11585, 13777, 16384, 19484, 23171, 27555, + 32768, 38968 +}; + +static const int qoffset_inter_tab[MAX_QUANT+1] = { + 1, 2, 2, 3, 3, 4, 4, 5, + 6, 7, 9, 10, 12, 14, 17, 20, + 24, 29, 34, 41, 48, 57, 68, 81, + 96, 114, 136, 162, 192, 228, 272, 323, + 384, 457, 543, 646, 768, 913, 1086, 1292, + 1536, 1827, 2172, 2583, 3072, 3653, 4344, 5166, + 6144, 7307, 8689, 10333, 12288, 14613, 17378, 20666, + 24576, 29226 +}; + +/* magic number division by 3 from schroedinger */ +static inline int divide3(int x) +{ + return ((x+1)*21845 + 10922) >> 16; +} + +static DiracFrame *remove_frame(DiracFrame *framelist[], int picnum) +{ + DiracFrame *remove_pic = NULL; + int i, remove_idx = -1; + + for (i = 0; framelist[i]; i++) + if (framelist[i]->avframe.display_picture_number == picnum) { + remove_pic = framelist[i]; + remove_idx = i; + } + + if (remove_pic) + for (i = remove_idx; framelist[i]; i++) + framelist[i] = framelist[i+1]; + + return remove_pic; +} + +static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame) +{ + int i; + for (i = 0; i < maxframes; i++) + if (!framelist[i]) { + framelist[i] = frame; + return 0; + } + return -1; +} + +static int alloc_sequence_buffers(DiracContext *s) +{ + int sbwidth = DIVRNDUP(s->source.width, 4); + int sbheight = DIVRNDUP(s->source.height, 4); + int i, w, h, top_padding; + + /* todo: think more about this / use or set Plane here */ + for (i = 0; i < 3; i++) { + int max_xblen = MAX_BLOCKSIZE >> (i ? s->chroma_x_shift : 0); + int max_yblen = MAX_BLOCKSIZE >> (i ? s->chroma_y_shift : 0); + w = s->source.width >> (i ? s->chroma_x_shift : 0); + h = s->source.height >> (i ? s->chroma_y_shift : 0); + + /* we allocate the max we support here since num decompositions can + * change from frame to frame. Stride is aligned to 16 for SIMD, and + * 1<0) in arith decoding + * MAX_BLOCKSIZE padding for MC: blocks can spill up to half of that + * on each side */ + top_padding = FFMAX(1<plane[i].idwt_buf_base = av_mallocz((w+max_xblen)*h * sizeof(IDWTELEM)); + s->plane[i].idwt_tmp = av_malloc((w+16) * sizeof(IDWTELEM)); + s->plane[i].idwt_buf = s->plane[i].idwt_buf_base + top_padding*w; + if (!s->plane[i].idwt_buf_base || !s->plane[i].idwt_tmp) + return AVERROR(ENOMEM); + } + + w = s->source.width; + h = s->source.height; + + /* fixme: allocate using real stride here */ + s->sbsplit = av_malloc(sbwidth * sbheight); + s->blmotion = av_malloc(sbwidth * sbheight * 16 * sizeof(*s->blmotion)); + s->edge_emu_buffer_base = av_malloc((w+64)*MAX_BLOCKSIZE); + + s->mctmp = av_malloc((w+64+MAX_BLOCKSIZE) * (h+MAX_BLOCKSIZE) * sizeof(*s->mctmp)); + s->mcscratch = av_malloc((w+64)*MAX_BLOCKSIZE); + + if (!s->sbsplit || !s->blmotion || !s->mctmp || !s->mcscratch) + return AVERROR(ENOMEM); + return 0; +} + +static void free_sequence_buffers(DiracContext *s) +{ + int i, j, k; + + for (i = 0; i < MAX_FRAMES; i++) { + if (s->all_frames[i].avframe.data[0]) { + av_frame_unref(&s->all_frames[i].avframe); + memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated)); + } + + for (j = 0; j < 3; j++) + for (k = 1; k < 4; k++) + av_freep(&s->all_frames[i].hpel_base[j][k]); + } + + memset(s->ref_frames, 0, sizeof(s->ref_frames)); + memset(s->delay_frames, 0, sizeof(s->delay_frames)); + + for (i = 0; i < 3; i++) { + av_freep(&s->plane[i].idwt_buf_base); + av_freep(&s->plane[i].idwt_tmp); + } + + av_freep(&s->sbsplit); + av_freep(&s->blmotion); + av_freep(&s->edge_emu_buffer_base); + + av_freep(&s->mctmp); + av_freep(&s->mcscratch); +} + +static av_cold int dirac_decode_init(AVCodecContext *avctx) +{ + DiracContext *s = avctx->priv_data; + s->avctx = avctx; + s->frame_number = -1; + + if (avctx->flags&CODEC_FLAG_EMU_EDGE) { + av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported!\n"); + return AVERROR_PATCHWELCOME; + } + + ff_dsputil_init(&s->dsp, avctx); + ff_diracdsp_init(&s->diracdsp); + + return 0; +} + +static void dirac_decode_flush(AVCodecContext *avctx) +{ + DiracContext *s = avctx->priv_data; + free_sequence_buffers(s); + s->seen_sequence_header = 0; + s->frame_number = -1; +} + +static av_cold int dirac_decode_end(AVCodecContext *avctx) +{ + dirac_decode_flush(avctx); + return 0; +} + +#define SIGN_CTX(x) (CTX_SIGN_ZERO + ((x) > 0) - ((x) < 0)) + +static inline void coeff_unpack_arith(DiracArith *c, int qfactor, int qoffset, + SubBand *b, IDWTELEM *buf, int x, int y) +{ + int coeff, sign; + int sign_pred = 0; + int pred_ctx = CTX_ZPZN_F1; + + /* Check if the parent subband has a 0 in the corresponding position */ + if (b->parent) + pred_ctx += !!b->parent->ibuf[b->parent->stride * (y>>1) + (x>>1)] << 1; + + if (b->orientation == subband_hl) + sign_pred = buf[-b->stride]; + + /* Determine if the pixel has only zeros in its neighbourhood */ + if (x) { + pred_ctx += !(buf[-1] | buf[-b->stride] | buf[-1-b->stride]); + if (b->orientation == subband_lh) + sign_pred = buf[-1]; + } else { + pred_ctx += !buf[-b->stride]; + } + + coeff = dirac_get_arith_uint(c, pred_ctx, CTX_COEFF_DATA); + if (coeff) { + coeff = (coeff * qfactor + qoffset + 2) >> 2; + sign = dirac_get_arith_bit(c, SIGN_CTX(sign_pred)); + coeff = (coeff ^ -sign) + sign; + } + *buf = coeff; +} + +static inline int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset) +{ + int sign, coeff; + + coeff = svq3_get_ue_golomb(gb); + if (coeff) { + coeff = (coeff * qfactor + qoffset + 2) >> 2; + sign = get_bits1(gb); + coeff = (coeff ^ -sign) + sign; + } + return coeff; +} + +/** + * Decode the coeffs in the rectangle defined by left, right, top, bottom + * [DIRAC_STD] 13.4.3.2 Codeblock unpacking loop. codeblock() + */ +static inline void codeblock(DiracContext *s, SubBand *b, + GetBitContext *gb, DiracArith *c, + int left, int right, int top, int bottom, + int blockcnt_one, int is_arith) +{ + int x, y, zero_block; + int qoffset, qfactor; + IDWTELEM *buf; + + /* check for any coded coefficients in this codeblock */ + if (!blockcnt_one) { + if (is_arith) + zero_block = dirac_get_arith_bit(c, CTX_ZERO_BLOCK); + else + zero_block = get_bits1(gb); + + if (zero_block) + return; + } + + if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) { + int quant = b->quant; + if (is_arith) + quant += dirac_get_arith_int(c, CTX_DELTA_Q_F, CTX_DELTA_Q_DATA); + else + quant += dirac_get_se_golomb(gb); + if (quant < 0) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n"); + return; + } + b->quant = quant; + } + + b->quant = FFMIN(b->quant, MAX_QUANT); + + qfactor = qscale_tab[b->quant]; + /* TODO: context pointer? */ + if (!s->num_refs) + qoffset = qoffset_intra_tab[b->quant]; + else + qoffset = qoffset_inter_tab[b->quant]; + + buf = b->ibuf + top * b->stride; + for (y = top; y < bottom; y++) { + for (x = left; x < right; x++) { + /* [DIRAC_STD] 13.4.4 Subband coefficients. coeff_unpack() */ + if (is_arith) + coeff_unpack_arith(c, qfactor, qoffset, b, buf+x, x, y); + else + buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset); + } + buf += b->stride; + } +} + +/** + * Dirac Specification -> + * 13.3 intra_dc_prediction(band) + */ +static inline void intra_dc_prediction(SubBand *b) +{ + IDWTELEM *buf = b->ibuf; + int x, y; + + for (x = 1; x < b->width; x++) + buf[x] += buf[x-1]; + buf += b->stride; + + for (y = 1; y < b->height; y++) { + buf[0] += buf[-b->stride]; + + for (x = 1; x < b->width; x++) { + int pred = buf[x - 1] + buf[x - b->stride] + buf[x - b->stride-1]; + buf[x] += divide3(pred); + } + buf += b->stride; + } +} + +/** + * Dirac Specification -> + * 13.4.2 Non-skipped subbands. subband_coeffs() + */ +static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith) +{ + int cb_x, cb_y, left, right, top, bottom; + DiracArith c; + GetBitContext gb; + int cb_width = s->codeblock[b->level + (b->orientation != subband_ll)].width; + int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height; + int blockcnt_one = (cb_width + cb_height) == 2; + + if (!b->length) + return; + + init_get_bits8(&gb, b->coeff_data, b->length); + + if (is_arith) + ff_dirac_init_arith_decoder(&c, &gb, b->length); + + top = 0; + for (cb_y = 0; cb_y < cb_height; cb_y++) { + bottom = (b->height * (cb_y+1)) / cb_height; + left = 0; + for (cb_x = 0; cb_x < cb_width; cb_x++) { + right = (b->width * (cb_x+1)) / cb_width; + codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith); + left = right; + } + top = bottom; + } + + if (b->orientation == subband_ll && s->num_refs == 0) + intra_dc_prediction(b); +} + +static int decode_subband_arith(AVCodecContext *avctx, void *b) +{ + DiracContext *s = avctx->priv_data; + decode_subband_internal(s, b, 1); + return 0; +} + +static int decode_subband_golomb(AVCodecContext *avctx, void *arg) +{ + DiracContext *s = avctx->priv_data; + SubBand **b = arg; + decode_subband_internal(s, *b, 0); + return 0; +} + +/** + * Dirac Specification -> + * [DIRAC_STD] 13.4.1 core_transform_data() + */ +static void decode_component(DiracContext *s, int comp) +{ + AVCodecContext *avctx = s->avctx; + SubBand *bands[3*MAX_DWT_LEVELS+1]; + enum dirac_subband orientation; + int level, num_bands = 0; + + /* Unpack all subbands at all levels. */ + for (level = 0; level < s->wavelet_depth; level++) { + for (orientation = !!level; orientation < 4; orientation++) { + SubBand *b = &s->plane[comp].band[level][orientation]; + bands[num_bands++] = b; + + align_get_bits(&s->gb); + /* [DIRAC_STD] 13.4.2 subband() */ + b->length = svq3_get_ue_golomb(&s->gb); + if (b->length) { + b->quant = svq3_get_ue_golomb(&s->gb); + align_get_bits(&s->gb); + b->coeff_data = s->gb.buffer + get_bits_count(&s->gb)/8; + b->length = FFMIN(b->length, FFMAX(get_bits_left(&s->gb)/8, 0)); + skip_bits_long(&s->gb, b->length*8); + } + } + /* arithmetic coding has inter-level dependencies, so we can only execute one level at a time */ + if (s->is_arith) + avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level], + NULL, 4-!!level, sizeof(SubBand)); + } + /* golomb coding has no inter-level dependencies, so we can execute all subbands in parallel */ + if (!s->is_arith) + avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*)); +} + +/* [DIRAC_STD] 13.5.5.2 Luma slice subband data. luma_slice_band(level,orient,sx,sy) --> if b2 == NULL */ +/* [DIRAC_STD] 13.5.5.3 Chroma slice subband data. chroma_slice_band(level,orient,sx,sy) --> if b2 != NULL */ +static void lowdelay_subband(DiracContext *s, GetBitContext *gb, int quant, + int slice_x, int slice_y, int bits_end, + SubBand *b1, SubBand *b2) +{ + int left = b1->width * slice_x / s->lowdelay.num_x; + int right = b1->width *(slice_x+1) / s->lowdelay.num_x; + int top = b1->height * slice_y / s->lowdelay.num_y; + int bottom = b1->height *(slice_y+1) / s->lowdelay.num_y; + + int qfactor = qscale_tab[FFMIN(quant, MAX_QUANT)]; + int qoffset = qoffset_intra_tab[FFMIN(quant, MAX_QUANT)]; + + IDWTELEM *buf1 = b1->ibuf + top * b1->stride; + IDWTELEM *buf2 = b2 ? b2->ibuf + top * b2->stride : NULL; + int x, y; + /* we have to constantly check for overread since the spec explictly + requires this, with the meaning that all remaining coeffs are set to 0 */ + if (get_bits_count(gb) >= bits_end) + return; + + for (y = top; y < bottom; y++) { + for (x = left; x < right; x++) { + buf1[x] = coeff_unpack_golomb(gb, qfactor, qoffset); + if (get_bits_count(gb) >= bits_end) + return; + if (buf2) { + buf2[x] = coeff_unpack_golomb(gb, qfactor, qoffset); + if (get_bits_count(gb) >= bits_end) + return; + } + } + buf1 += b1->stride; + if (buf2) + buf2 += b2->stride; + } +} + +struct lowdelay_slice { + GetBitContext gb; + int slice_x; + int slice_y; + int bytes; +}; + + +/** + * Dirac Specification -> + * 13.5.2 Slices. slice(sx,sy) + */ +static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg) +{ + DiracContext *s = avctx->priv_data; + struct lowdelay_slice *slice = arg; + GetBitContext *gb = &slice->gb; + enum dirac_subband orientation; + int level, quant, chroma_bits, chroma_end; + + int quant_base = get_bits(gb, 7); /*[DIRAC_STD] qindex */ + int length_bits = av_log2(8 * slice->bytes)+1; + int luma_bits = get_bits_long(gb, length_bits); + int luma_end = get_bits_count(gb) + FFMIN(luma_bits, get_bits_left(gb)); + + /* [DIRAC_STD] 13.5.5.2 luma_slice_band */ + for (level = 0; level < s->wavelet_depth; level++) + for (orientation = !!level; orientation < 4; orientation++) { + quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0); + lowdelay_subband(s, gb, quant, slice->slice_x, slice->slice_y, luma_end, + &s->plane[0].band[level][orientation], NULL); + } + + /* consume any unused bits from luma */ + skip_bits_long(gb, get_bits_count(gb) - luma_end); + + chroma_bits = 8*slice->bytes - 7 - length_bits - luma_bits; + chroma_end = get_bits_count(gb) + FFMIN(chroma_bits, get_bits_left(gb)); + /* [DIRAC_STD] 13.5.5.3 chroma_slice_band */ + for (level = 0; level < s->wavelet_depth; level++) + for (orientation = !!level; orientation < 4; orientation++) { + quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0); + lowdelay_subband(s, gb, quant, slice->slice_x, slice->slice_y, chroma_end, + &s->plane[1].band[level][orientation], + &s->plane[2].band[level][orientation]); + } + + return 0; +} + +/** + * Dirac Specification -> + * 13.5.1 low_delay_transform_data() + */ +static void decode_lowdelay(DiracContext *s) +{ + AVCodecContext *avctx = s->avctx; + int slice_x, slice_y, bytes, bufsize; + const uint8_t *buf; + struct lowdelay_slice *slices; + int slice_num = 0; + + slices = av_mallocz(s->lowdelay.num_x * s->lowdelay.num_y * sizeof(struct lowdelay_slice)); + + align_get_bits(&s->gb); + /*[DIRAC_STD] 13.5.2 Slices. slice(sx,sy) */ + buf = s->gb.buffer + get_bits_count(&s->gb)/8; + bufsize = get_bits_left(&s->gb); + + for (slice_y = 0; bufsize > 0 && slice_y < s->lowdelay.num_y; slice_y++) + for (slice_x = 0; bufsize > 0 && slice_x < s->lowdelay.num_x; slice_x++) { + bytes = (slice_num+1) * s->lowdelay.bytes.num / s->lowdelay.bytes.den + - slice_num * s->lowdelay.bytes.num / s->lowdelay.bytes.den; + + slices[slice_num].bytes = bytes; + slices[slice_num].slice_x = slice_x; + slices[slice_num].slice_y = slice_y; + init_get_bits(&slices[slice_num].gb, buf, bufsize); + slice_num++; + + buf += bytes; + bufsize -= bytes*8; + } + + avctx->execute(avctx, decode_lowdelay_slice, slices, NULL, slice_num, + sizeof(struct lowdelay_slice)); /* [DIRAC_STD] 13.5.2 Slices */ + intra_dc_prediction(&s->plane[0].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */ + intra_dc_prediction(&s->plane[1].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */ + intra_dc_prediction(&s->plane[2].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */ + av_free(slices); +} + +static void init_planes(DiracContext *s) +{ + int i, w, h, level, orientation; + + for (i = 0; i < 3; i++) { + Plane *p = &s->plane[i]; + + p->width = s->source.width >> (i ? s->chroma_x_shift : 0); + p->height = s->source.height >> (i ? s->chroma_y_shift : 0); + p->idwt_width = w = CALC_PADDING(p->width , s->wavelet_depth); + p->idwt_height = h = CALC_PADDING(p->height, s->wavelet_depth); + p->idwt_stride = FFALIGN(p->idwt_width, 8); + + for (level = s->wavelet_depth-1; level >= 0; level--) { + w = w>>1; + h = h>>1; + for (orientation = !!level; orientation < 4; orientation++) { + SubBand *b = &p->band[level][orientation]; + + b->ibuf = p->idwt_buf; + b->level = level; + b->stride = p->idwt_stride << (s->wavelet_depth - level); + b->width = w; + b->height = h; + b->orientation = orientation; + + if (orientation & 1) + b->ibuf += w; + if (orientation > 1) + b->ibuf += b->stride>>1; + + if (level) + b->parent = &p->band[level-1][orientation]; + } + } + + if (i > 0) { + p->xblen = s->plane[0].xblen >> s->chroma_x_shift; + p->yblen = s->plane[0].yblen >> s->chroma_y_shift; + p->xbsep = s->plane[0].xbsep >> s->chroma_x_shift; + p->ybsep = s->plane[0].ybsep >> s->chroma_y_shift; + } + + p->xoffset = (p->xblen - p->xbsep)/2; + p->yoffset = (p->yblen - p->ybsep)/2; + } +} + +/** + * Unpack the motion compensation parameters + * Dirac Specification -> + * 11.2 Picture prediction data. picture_prediction() + */ +static int dirac_unpack_prediction_parameters(DiracContext *s) +{ + static const uint8_t default_blen[] = { 4, 12, 16, 24 }; + static const uint8_t default_bsep[] = { 4, 8, 12, 16 }; + + GetBitContext *gb = &s->gb; + unsigned idx, ref; + + align_get_bits(gb); + /* [DIRAC_STD] 11.2.2 Block parameters. block_parameters() */ + /* Luma and Chroma are equal. 11.2.3 */ + idx = svq3_get_ue_golomb(gb); /* [DIRAC_STD] index */ + + if (idx > 4) { + av_log(s->avctx, AV_LOG_ERROR, "Block prediction index too high\n"); + return -1; + } + + if (idx == 0) { + s->plane[0].xblen = svq3_get_ue_golomb(gb); + s->plane[0].yblen = svq3_get_ue_golomb(gb); + s->plane[0].xbsep = svq3_get_ue_golomb(gb); + s->plane[0].ybsep = svq3_get_ue_golomb(gb); + } else { + /*[DIRAC_STD] preset_block_params(index). Table 11.1 */ + s->plane[0].xblen = default_blen[idx-1]; + s->plane[0].yblen = default_blen[idx-1]; + s->plane[0].xbsep = default_bsep[idx-1]; + s->plane[0].ybsep = default_bsep[idx-1]; + } + /*[DIRAC_STD] 11.2.4 motion_data_dimensions() + Calculated in function dirac_unpack_block_motion_data */ + + if (!s->plane[0].xbsep || !s->plane[0].ybsep || s->plane[0].xbsep < s->plane[0].xblen/2 || s->plane[0].ybsep < s->plane[0].yblen/2) { + av_log(s->avctx, AV_LOG_ERROR, "Block separation too small\n"); + return -1; + } + if (s->plane[0].xbsep > s->plane[0].xblen || s->plane[0].ybsep > s->plane[0].yblen) { + av_log(s->avctx, AV_LOG_ERROR, "Block separation greater than size\n"); + return -1; + } + if (FFMAX(s->plane[0].xblen, s->plane[0].yblen) > MAX_BLOCKSIZE) { + av_log(s->avctx, AV_LOG_ERROR, "Unsupported large block size\n"); + return -1; + } + + /*[DIRAC_STD] 11.2.5 Motion vector precision. motion_vector_precision() + Read motion vector precision */ + s->mv_precision = svq3_get_ue_golomb(gb); + if (s->mv_precision > 3) { + av_log(s->avctx, AV_LOG_ERROR, "MV precision finer than eighth-pel\n"); + return -1; + } + + /*[DIRAC_STD] 11.2.6 Global motion. global_motion() + Read the global motion compensation parameters */ + s->globalmc_flag = get_bits1(gb); + if (s->globalmc_flag) { + memset(s->globalmc, 0, sizeof(s->globalmc)); + /* [DIRAC_STD] pan_tilt(gparams) */ + for (ref = 0; ref < s->num_refs; ref++) { + if (get_bits1(gb)) { + s->globalmc[ref].pan_tilt[0] = dirac_get_se_golomb(gb); + s->globalmc[ref].pan_tilt[1] = dirac_get_se_golomb(gb); + } + /* [DIRAC_STD] zoom_rotate_shear(gparams) + zoom/rotation/shear parameters */ + if (get_bits1(gb)) { + s->globalmc[ref].zrs_exp = svq3_get_ue_golomb(gb); + s->globalmc[ref].zrs[0][0] = dirac_get_se_golomb(gb); + s->globalmc[ref].zrs[0][1] = dirac_get_se_golomb(gb); + s->globalmc[ref].zrs[1][0] = dirac_get_se_golomb(gb); + s->globalmc[ref].zrs[1][1] = dirac_get_se_golomb(gb); + } else { + s->globalmc[ref].zrs[0][0] = 1; + s->globalmc[ref].zrs[1][1] = 1; + } + /* [DIRAC_STD] perspective(gparams) */ + if (get_bits1(gb)) { + s->globalmc[ref].perspective_exp = svq3_get_ue_golomb(gb); + s->globalmc[ref].perspective[0] = dirac_get_se_golomb(gb); + s->globalmc[ref].perspective[1] = dirac_get_se_golomb(gb); + } + } + } + + /*[DIRAC_STD] 11.2.7 Picture prediction mode. prediction_mode() + Picture prediction mode, not currently used. */ + if (svq3_get_ue_golomb(gb)) { + av_log(s->avctx, AV_LOG_ERROR, "Unknown picture prediction mode\n"); + return -1; + } + + /* [DIRAC_STD] 11.2.8 Reference picture weight. reference_picture_weights() + just data read, weight calculation will be done later on. */ + s->weight_log2denom = 1; + s->weight[0] = 1; + s->weight[1] = 1; + + if (get_bits1(gb)) { + s->weight_log2denom = svq3_get_ue_golomb(gb); + s->weight[0] = dirac_get_se_golomb(gb); + if (s->num_refs == 2) + s->weight[1] = dirac_get_se_golomb(gb); + } + return 0; +} + +/** + * Dirac Specification -> + * 11.3 Wavelet transform data. wavelet_transform() + */ +static int dirac_unpack_idwt_params(DiracContext *s) +{ + GetBitContext *gb = &s->gb; + int i, level; + unsigned tmp; + +#define CHECKEDREAD(dst, cond, errmsg) \ + tmp = svq3_get_ue_golomb(gb); \ + if (cond) { \ + av_log(s->avctx, AV_LOG_ERROR, errmsg); \ + return -1; \ + }\ + dst = tmp; + + align_get_bits(gb); + + s->zero_res = s->num_refs ? get_bits1(gb) : 0; + if (s->zero_res) + return 0; + + /*[DIRAC_STD] 11.3.1 Transform parameters. transform_parameters() */ + CHECKEDREAD(s->wavelet_idx, tmp > 6, "wavelet_idx is too big\n") + + CHECKEDREAD(s->wavelet_depth, tmp > MAX_DWT_LEVELS || tmp < 1, "invalid number of DWT decompositions\n") + + if (!s->low_delay) { + /* Codeblock parameters (core syntax only) */ + if (get_bits1(gb)) { + for (i = 0; i <= s->wavelet_depth; i++) { + CHECKEDREAD(s->codeblock[i].width , tmp < 1, "codeblock width invalid\n") + CHECKEDREAD(s->codeblock[i].height, tmp < 1, "codeblock height invalid\n") + } + + CHECKEDREAD(s->codeblock_mode, tmp > 1, "unknown codeblock mode\n") + } else + for (i = 0; i <= s->wavelet_depth; i++) + s->codeblock[i].width = s->codeblock[i].height = 1; + } else { + /* Slice parameters + quantization matrix*/ + /*[DIRAC_STD] 11.3.4 Slice coding Parameters (low delay syntax only). slice_parameters() */ + s->lowdelay.num_x = svq3_get_ue_golomb(gb); + s->lowdelay.num_y = svq3_get_ue_golomb(gb); + s->lowdelay.bytes.num = svq3_get_ue_golomb(gb); + s->lowdelay.bytes.den = svq3_get_ue_golomb(gb); + + if (s->lowdelay.bytes.den <= 0) { + av_log(s->avctx,AV_LOG_ERROR,"Invalid lowdelay.bytes.den\n"); + return AVERROR_INVALIDDATA; + } + + /* [DIRAC_STD] 11.3.5 Quantisation matrices (low-delay syntax). quant_matrix() */ + if (get_bits1(gb)) { + av_log(s->avctx,AV_LOG_DEBUG,"Low Delay: Has Custom Quantization Matrix!\n"); + /* custom quantization matrix */ + s->lowdelay.quant[0][0] = svq3_get_ue_golomb(gb); + for (level = 0; level < s->wavelet_depth; level++) { + s->lowdelay.quant[level][1] = svq3_get_ue_golomb(gb); + s->lowdelay.quant[level][2] = svq3_get_ue_golomb(gb); + s->lowdelay.quant[level][3] = svq3_get_ue_golomb(gb); + } + } else { + if (s->wavelet_depth > 4) { + av_log(s->avctx,AV_LOG_ERROR,"Mandatory custom low delay matrix missing for depth %d\n", s->wavelet_depth); + return AVERROR_INVALIDDATA; + } + /* default quantization matrix */ + for (level = 0; level < s->wavelet_depth; level++) + for (i = 0; i < 4; i++) { + s->lowdelay.quant[level][i] = default_qmat[s->wavelet_idx][level][i]; + /* haar with no shift differs for different depths */ + if (s->wavelet_idx == 3) + s->lowdelay.quant[level][i] += 4*(s->wavelet_depth-1 - level); + } + } + } + return 0; +} + +static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y) +{ + static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 }; + + if (!(x|y)) + return 0; + else if (!y) + return sbsplit[-1]; + else if (!x) + return sbsplit[-stride]; + + return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]]; +} + +static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask) +{ + int pred; + + if (!(x|y)) + return 0; + else if (!y) + return block[-1].ref & refmask; + else if (!x) + return block[-stride].ref & refmask; + + /* return the majority */ + pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask); + return (pred >> 1) & refmask; +} + +static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y) +{ + int i, n = 0; + + memset(block->u.dc, 0, sizeof(block->u.dc)); + + if (x && !(block[-1].ref & 3)) { + for (i = 0; i < 3; i++) + block->u.dc[i] += block[-1].u.dc[i]; + n++; + } + + if (y && !(block[-stride].ref & 3)) { + for (i = 0; i < 3; i++) + block->u.dc[i] += block[-stride].u.dc[i]; + n++; + } + + if (x && y && !(block[-1-stride].ref & 3)) { + for (i = 0; i < 3; i++) + block->u.dc[i] += block[-1-stride].u.dc[i]; + n++; + } + + if (n == 2) { + for (i = 0; i < 3; i++) + block->u.dc[i] = (block->u.dc[i]+1)>>1; + } else if (n == 3) { + for (i = 0; i < 3; i++) + block->u.dc[i] = divide3(block->u.dc[i]); + } +} + +static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref) +{ + int16_t *pred[3]; + int refmask = ref+1; + int mask = refmask | DIRAC_REF_MASK_GLOBAL; /* exclude gmc blocks */ + int n = 0; + + if (x && (block[-1].ref & mask) == refmask) + pred[n++] = block[-1].u.mv[ref]; + + if (y && (block[-stride].ref & mask) == refmask) + pred[n++] = block[-stride].u.mv[ref]; + + if (x && y && (block[-stride-1].ref & mask) == refmask) + pred[n++] = block[-stride-1].u.mv[ref]; + + switch (n) { + case 0: + block->u.mv[ref][0] = 0; + block->u.mv[ref][1] = 0; + break; + case 1: + block->u.mv[ref][0] = pred[0][0]; + block->u.mv[ref][1] = pred[0][1]; + break; + case 2: + block->u.mv[ref][0] = (pred[0][0] + pred[1][0] + 1) >> 1; + block->u.mv[ref][1] = (pred[0][1] + pred[1][1] + 1) >> 1; + break; + case 3: + block->u.mv[ref][0] = mid_pred(pred[0][0], pred[1][0], pred[2][0]); + block->u.mv[ref][1] = mid_pred(pred[0][1], pred[1][1], pred[2][1]); + break; + } +} + +static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref) +{ + int ez = s->globalmc[ref].zrs_exp; + int ep = s->globalmc[ref].perspective_exp; + int (*A)[2] = s->globalmc[ref].zrs; + int *b = s->globalmc[ref].pan_tilt; + int *c = s->globalmc[ref].perspective; + + int m = (1<u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep); + block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep); +} + +static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block, + int stride, int x, int y) +{ + int i; + + block->ref = pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF1); + block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF1); + + if (s->num_refs == 2) { + block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF2); + block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF2) << 1; + } + + if (!block->ref) { + pred_block_dc(block, stride, x, y); + for (i = 0; i < 3; i++) + block->u.dc[i] += dirac_get_arith_int(arith+1+i, CTX_DC_F1, CTX_DC_DATA); + return; + } + + if (s->globalmc_flag) { + block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_GLOBAL); + block->ref ^= dirac_get_arith_bit(arith, CTX_GLOBAL_BLOCK) << 2; + } + + for (i = 0; i < s->num_refs; i++) + if (block->ref & (i+1)) { + if (block->ref & DIRAC_REF_MASK_GLOBAL) { + global_mv(s, block, x, y, i); + } else { + pred_mv(block, stride, x, y, i); + block->u.mv[i][0] += dirac_get_arith_int(arith + 4 + 2 * i, CTX_MV_F1, CTX_MV_DATA); + block->u.mv[i][1] += dirac_get_arith_int(arith + 5 + 2 * i, CTX_MV_F1, CTX_MV_DATA); + } + } +} + +/** + * Copies the current block to the other blocks covered by the current superblock split mode + */ +static void propagate_block_data(DiracBlock *block, int stride, int size) +{ + int x, y; + DiracBlock *dst = block; + + for (x = 1; x < size; x++) + dst[x] = *block; + + for (y = 1; y < size; y++) { + dst += stride; + for (x = 0; x < size; x++) + dst[x] = *block; + } +} + +/** + * Dirac Specification -> + * 12. Block motion data syntax + */ +static int dirac_unpack_block_motion_data(DiracContext *s) +{ + GetBitContext *gb = &s->gb; + uint8_t *sbsplit = s->sbsplit; + int i, x, y, q, p; + DiracArith arith[8]; + + align_get_bits(gb); + + /* [DIRAC_STD] 11.2.4 and 12.2.1 Number of blocks and superblocks */ + s->sbwidth = DIVRNDUP(s->source.width, 4*s->plane[0].xbsep); + s->sbheight = DIVRNDUP(s->source.height, 4*s->plane[0].ybsep); + s->blwidth = 4 * s->sbwidth; + s->blheight = 4 * s->sbheight; + + /* [DIRAC_STD] 12.3.1 Superblock splitting modes. superblock_split_modes() + decode superblock split modes */ + ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb)); /* svq3_get_ue_golomb(gb) is the length */ + for (y = 0; y < s->sbheight; y++) { + for (x = 0; x < s->sbwidth; x++) { + unsigned int split = dirac_get_arith_uint(arith, CTX_SB_F1, CTX_SB_DATA); + if (split > 2) + return -1; + sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3; + } + sbsplit += s->sbwidth; + } + + /* setup arith decoding */ + ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb)); + for (i = 0; i < s->num_refs; i++) { + ff_dirac_init_arith_decoder(arith + 4 + 2 * i, gb, svq3_get_ue_golomb(gb)); + ff_dirac_init_arith_decoder(arith + 5 + 2 * i, gb, svq3_get_ue_golomb(gb)); + } + for (i = 0; i < 3; i++) + ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb)); + + for (y = 0; y < s->sbheight; y++) + for (x = 0; x < s->sbwidth; x++) { + int blkcnt = 1 << s->sbsplit[y * s->sbwidth + x]; + int step = 4 >> s->sbsplit[y * s->sbwidth + x]; + + for (q = 0; q < blkcnt; q++) + for (p = 0; p < blkcnt; p++) { + int bx = 4 * x + p*step; + int by = 4 * y + q*step; + DiracBlock *block = &s->blmotion[by*s->blwidth + bx]; + decode_block_params(s, arith, block, s->blwidth, bx, by); + propagate_block_data(block, s->blwidth, step); + } + } + + return 0; +} + +static int weight(int i, int blen, int offset) +{ +#define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) : \ + (1 + (6*(i) + offset - 1) / (2*offset - 1)) + + if (i < 2*offset) + return ROLLOFF(i); + else if (i > blen-1 - 2*offset) + return ROLLOFF(blen-1 - i); + return 8; +} + +static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride, + int left, int right, int wy) +{ + int x; + for (x = 0; left && x < p->xblen >> 1; x++) + obmc_weight[x] = wy*8; + for (; x < p->xblen >> right; x++) + obmc_weight[x] = wy*weight(x, p->xblen, p->xoffset); + for (; x < p->xblen; x++) + obmc_weight[x] = wy*8; + for (; x < stride; x++) + obmc_weight[x] = 0; +} + +static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride, + int left, int right, int top, int bottom) +{ + int y; + for (y = 0; top && y < p->yblen >> 1; y++) { + init_obmc_weight_row(p, obmc_weight, stride, left, right, 8); + obmc_weight += stride; + } + for (; y < p->yblen >> bottom; y++) { + int wy = weight(y, p->yblen, p->yoffset); + init_obmc_weight_row(p, obmc_weight, stride, left, right, wy); + obmc_weight += stride; + } + for (; y < p->yblen; y++) { + init_obmc_weight_row(p, obmc_weight, stride, left, right, 8); + obmc_weight += stride; + } +} + +static void init_obmc_weights(DiracContext *s, Plane *p, int by) +{ + int top = !by; + int bottom = by == s->blheight-1; + + /* don't bother re-initing for rows 2 to blheight-2, the weights don't change */ + if (top || bottom || by == 1) { + init_obmc_weight(p, s->obmc_weight[0], MAX_BLOCKSIZE, 1, 0, top, bottom); + init_obmc_weight(p, s->obmc_weight[1], MAX_BLOCKSIZE, 0, 0, top, bottom); + init_obmc_weight(p, s->obmc_weight[2], MAX_BLOCKSIZE, 0, 1, top, bottom); + } +} + +static const uint8_t epel_weights[4][4][4] = { + {{ 16, 0, 0, 0 }, + { 12, 4, 0, 0 }, + { 8, 8, 0, 0 }, + { 4, 12, 0, 0 }}, + {{ 12, 0, 4, 0 }, + { 9, 3, 3, 1 }, + { 6, 6, 2, 2 }, + { 3, 9, 1, 3 }}, + {{ 8, 0, 8, 0 }, + { 6, 2, 6, 2 }, + { 4, 4, 4, 4 }, + { 2, 6, 2, 6 }}, + {{ 4, 0, 12, 0 }, + { 3, 1, 9, 3 }, + { 2, 2, 6, 6 }, + { 1, 3, 3, 9 }} +}; + +/** + * For block x,y, determine which of the hpel planes to do bilinear + * interpolation from and set src[] to the location in each hpel plane + * to MC from. + * + * @return the index of the put_dirac_pixels_tab function to use + * 0 for 1 plane (fpel,hpel), 1 for 2 planes (qpel), 2 for 4 planes (qpel), and 3 for epel + */ +static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5], + int x, int y, int ref, int plane) +{ + Plane *p = &s->plane[plane]; + uint8_t **ref_hpel = s->ref_pics[ref]->hpel[plane]; + int motion_x = block->u.mv[ref][0]; + int motion_y = block->u.mv[ref][1]; + int mx, my, i, epel, nplanes = 0; + + if (plane) { + motion_x >>= s->chroma_x_shift; + motion_y >>= s->chroma_y_shift; + } + + mx = motion_x & ~(-1 << s->mv_precision); + my = motion_y & ~(-1 << s->mv_precision); + motion_x >>= s->mv_precision; + motion_y >>= s->mv_precision; + /* normalize subpel coordinates to epel */ + /* TODO: template this function? */ + mx <<= 3 - s->mv_precision; + my <<= 3 - s->mv_precision; + + x += motion_x; + y += motion_y; + epel = (mx|my)&1; + + /* hpel position */ + if (!((mx|my)&3)) { + nplanes = 1; + src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x; + } else { + /* qpel or epel */ + nplanes = 4; + for (i = 0; i < 4; i++) + src[i] = ref_hpel[i] + y*p->stride + x; + + /* if we're interpolating in the right/bottom halves, adjust the planes as needed + we increment x/y because the edge changes for half of the pixels */ + if (mx > 4) { + src[0] += 1; + src[2] += 1; + x++; + } + if (my > 4) { + src[0] += p->stride; + src[1] += p->stride; + y++; + } + + /* hpel planes are: + [0]: F [1]: H + [2]: V [3]: C */ + if (!epel) { + /* check if we really only need 2 planes since either mx or my is + a hpel position. (epel weights of 0 handle this there) */ + if (!(mx&3)) { + /* mx == 0: average [0] and [2] + mx == 4: average [1] and [3] */ + src[!mx] = src[2 + !!mx]; + nplanes = 2; + } else if (!(my&3)) { + src[0] = src[(my>>1) ]; + src[1] = src[(my>>1)+1]; + nplanes = 2; + } + } else { + /* adjust the ordering if needed so the weights work */ + if (mx > 4) { + FFSWAP(const uint8_t *, src[0], src[1]); + FFSWAP(const uint8_t *, src[2], src[3]); + } + if (my > 4) { + FFSWAP(const uint8_t *, src[0], src[2]); + FFSWAP(const uint8_t *, src[1], src[3]); + } + src[4] = epel_weights[my&3][mx&3]; + } + } + + /* fixme: v/h _edge_pos */ + if (x + p->xblen > p->width +EDGE_WIDTH/2 || + y + p->yblen > p->height+EDGE_WIDTH/2 || + x < 0 || y < 0) { + for (i = 0; i < nplanes; i++) { + ff_emulated_edge_mc(s->edge_emu_buffer[i], p->stride, + src[i], p->stride, + p->xblen, p->yblen, x, y, + p->width+EDGE_WIDTH/2, p->height+EDGE_WIDTH/2); + src[i] = s->edge_emu_buffer[i]; + } + } + return (nplanes>>1) + epel; +} + +static void add_dc(uint16_t *dst, int dc, int stride, + uint8_t *obmc_weight, int xblen, int yblen) +{ + int x, y; + dc += 128; + + for (y = 0; y < yblen; y++) { + for (x = 0; x < xblen; x += 2) { + dst[x ] += dc * obmc_weight[x ]; + dst[x+1] += dc * obmc_weight[x+1]; + } + dst += stride; + obmc_weight += MAX_BLOCKSIZE; + } +} + +static void block_mc(DiracContext *s, DiracBlock *block, + uint16_t *mctmp, uint8_t *obmc_weight, + int plane, int dstx, int dsty) +{ + Plane *p = &s->plane[plane]; + const uint8_t *src[5]; + int idx; + + switch (block->ref&3) { + case 0: /* DC */ + add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen); + return; + case 1: + case 2: + idx = mc_subpel(s, block, src, dstx, dsty, (block->ref&3)-1, plane); + s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen); + if (s->weight_func) + s->weight_func(s->mcscratch, p->stride, s->weight_log2denom, + s->weight[0] + s->weight[1], p->yblen); + break; + case 3: + idx = mc_subpel(s, block, src, dstx, dsty, 0, plane); + s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen); + idx = mc_subpel(s, block, src, dstx, dsty, 1, plane); + if (s->biweight_func) { + /* fixme: +32 is a quick hack */ + s->put_pixels_tab[idx](s->mcscratch + 32, src, p->stride, p->yblen); + s->biweight_func(s->mcscratch, s->mcscratch+32, p->stride, s->weight_log2denom, + s->weight[0], s->weight[1], p->yblen); + } else + s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen); + break; + } + s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen); +} + +static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty) +{ + Plane *p = &s->plane[plane]; + int x, dstx = p->xbsep - p->xoffset; + + block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty); + mctmp += p->xbsep; + + for (x = 1; x < s->blwidth-1; x++) { + block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty); + dstx += p->xbsep; + mctmp += p->xbsep; + } + block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty); +} + +static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen) +{ + int idx = 0; + if (xblen > 8) + idx = 1; + if (xblen > 16) + idx = 2; + + memcpy(s->put_pixels_tab, s->diracdsp.put_dirac_pixels_tab[idx], sizeof(s->put_pixels_tab)); + memcpy(s->avg_pixels_tab, s->diracdsp.avg_dirac_pixels_tab[idx], sizeof(s->avg_pixels_tab)); + s->add_obmc = s->diracdsp.add_dirac_obmc[idx]; + if (s->weight_log2denom > 1 || s->weight[0] != 1 || s->weight[1] != 1) { + s->weight_func = s->diracdsp.weight_dirac_pixels_tab[idx]; + s->biweight_func = s->diracdsp.biweight_dirac_pixels_tab[idx]; + } else { + s->weight_func = NULL; + s->biweight_func = NULL; + } +} + +static void interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height) +{ + /* chroma allocates an edge of 8 when subsampled + which for 4:2:2 means an h edge of 16 and v edge of 8 + just use 8 for everything for the moment */ + int i, edge = EDGE_WIDTH/2; + + ref->hpel[plane][0] = ref->avframe.data[plane]; + s->dsp.draw_edges(ref->hpel[plane][0], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM); /* EDGE_TOP | EDGE_BOTTOM values just copied to make it build, this needs to be ensured */ + + /* no need for hpel if we only have fpel vectors */ + if (!s->mv_precision) + return; + + for (i = 1; i < 4; i++) { + if (!ref->hpel_base[plane][i]) + ref->hpel_base[plane][i] = av_malloc((height+2*edge) * ref->avframe.linesize[plane] + 32); + /* we need to be 16-byte aligned even for chroma */ + ref->hpel[plane][i] = ref->hpel_base[plane][i] + edge*ref->avframe.linesize[plane] + 16; + } + + if (!ref->interpolated[plane]) { + s->diracdsp.dirac_hpel_filter(ref->hpel[plane][1], ref->hpel[plane][2], + ref->hpel[plane][3], ref->hpel[plane][0], + ref->avframe.linesize[plane], width, height); + s->dsp.draw_edges(ref->hpel[plane][1], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM); + s->dsp.draw_edges(ref->hpel[plane][2], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM); + s->dsp.draw_edges(ref->hpel[plane][3], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM); + } + ref->interpolated[plane] = 1; +} + +/** + * Dirac Specification -> + * 13.0 Transform data syntax. transform_data() + */ +static int dirac_decode_frame_internal(DiracContext *s) +{ + DWTContext d; + int y, i, comp, dsty; + + if (s->low_delay) { + /* [DIRAC_STD] 13.5.1 low_delay_transform_data() */ + for (comp = 0; comp < 3; comp++) { + Plane *p = &s->plane[comp]; + memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM)); + } + if (!s->zero_res) + decode_lowdelay(s); + } + + for (comp = 0; comp < 3; comp++) { + Plane *p = &s->plane[comp]; + uint8_t *frame = s->current_picture->avframe.data[comp]; + + /* FIXME: small resolutions */ + for (i = 0; i < 4; i++) + s->edge_emu_buffer[i] = s->edge_emu_buffer_base + i*FFALIGN(p->width, 16); + + if (!s->zero_res && !s->low_delay) + { + memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM)); + decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */ + } + if (ff_spatial_idwt_init2(&d, p->idwt_buf, p->idwt_width, p->idwt_height, p->idwt_stride, + s->wavelet_idx+2, s->wavelet_depth, p->idwt_tmp)) + return -1; + + if (!s->num_refs) { /* intra */ + for (y = 0; y < p->height; y += 16) { + ff_spatial_idwt_slice2(&d, y+16); /* decode */ + s->diracdsp.put_signed_rect_clamped(frame + y*p->stride, p->stride, + p->idwt_buf + y*p->idwt_stride, p->idwt_stride, p->width, 16); + } + } else { /* inter */ + int rowheight = p->ybsep*p->stride; + + select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen); + + for (i = 0; i < s->num_refs; i++) + interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height); + + memset(s->mctmp, 0, 4*p->yoffset*p->stride); + + dsty = -p->yoffset; + for (y = 0; y < s->blheight; y++) { + int h = 0, + start = FFMAX(dsty, 0); + uint16_t *mctmp = s->mctmp + y*rowheight; + DiracBlock *blocks = s->blmotion + y*s->blwidth; + + init_obmc_weights(s, p, y); + + if (y == s->blheight-1 || start+p->ybsep > p->height) + h = p->height - start; + else + h = p->ybsep - (start - dsty); + if (h < 0) + break; + + memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight); + mc_row(s, blocks, mctmp, comp, dsty); + + mctmp += (start - dsty)*p->stride + p->xoffset; + ff_spatial_idwt_slice2(&d, start + h); /* decode */ + s->diracdsp.add_rect_clamped(frame + start*p->stride, mctmp, p->stride, + p->idwt_buf + start*p->idwt_stride, p->idwt_stride, p->width, h); + + dsty += p->ybsep; + } + } + } + + + return 0; +} + +/** + * Dirac Specification -> + * 11.1.1 Picture Header. picture_header() + */ +static int dirac_decode_picture_header(DiracContext *s) +{ + int retire, picnum; + int i, j, refnum, refdist; + GetBitContext *gb = &s->gb; + + /* [DIRAC_STD] 11.1.1 Picture Header. picture_header() PICTURE_NUM */ + picnum = s->current_picture->avframe.display_picture_number = get_bits_long(gb, 32); + + + av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum); + + /* if this is the first keyframe after a sequence header, start our + reordering from here */ + if (s->frame_number < 0) + s->frame_number = picnum; + + s->ref_pics[0] = s->ref_pics[1] = NULL; + for (i = 0; i < s->num_refs; i++) { + refnum = picnum + dirac_get_se_golomb(gb); + refdist = INT_MAX; + + /* find the closest reference to the one we want */ + /* Jordi: this is needed if the referenced picture hasn't yet arrived */ + for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++) + if (s->ref_frames[j] + && FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum) < refdist) { + s->ref_pics[i] = s->ref_frames[j]; + refdist = FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum); + } + + if (!s->ref_pics[i] || refdist) + av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n"); + + /* if there were no references at all, allocate one */ + if (!s->ref_pics[i]) + for (j = 0; j < MAX_FRAMES; j++) + if (!s->all_frames[j].avframe.data[0]) { + s->ref_pics[i] = &s->all_frames[j]; + ff_get_buffer(s->avctx, &s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF); + break; + } + } + + /* retire the reference frames that are not used anymore */ + if (s->current_picture->avframe.reference) { + retire = picnum + dirac_get_se_golomb(gb); + if (retire != picnum) { + DiracFrame *retire_pic = remove_frame(s->ref_frames, retire); + + if (retire_pic) + retire_pic->avframe.reference &= DELAYED_PIC_REF; + else + av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n"); + } + + /* if reference array is full, remove the oldest as per the spec */ + while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) { + av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n"); + remove_frame(s->ref_frames, s->ref_frames[0]->avframe.display_picture_number)->avframe.reference &= DELAYED_PIC_REF; + } + } + + if (s->num_refs) { + if (dirac_unpack_prediction_parameters(s)) /* [DIRAC_STD] 11.2 Picture Prediction Data. picture_prediction() */ + return -1; + if (dirac_unpack_block_motion_data(s)) /* [DIRAC_STD] 12. Block motion data syntax */ + return -1; + } + if (dirac_unpack_idwt_params(s)) /* [DIRAC_STD] 11.3 Wavelet transform data */ + return -1; + + init_planes(s); + return 0; +} + +static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *got_frame) +{ + DiracFrame *out = s->delay_frames[0]; + int i, out_idx = 0; + int ret; + + /* find frame with lowest picture number */ + for (i = 1; s->delay_frames[i]; i++) + if (s->delay_frames[i]->avframe.display_picture_number < out->avframe.display_picture_number) { + out = s->delay_frames[i]; + out_idx = i; + } + + for (i = out_idx; s->delay_frames[i]; i++) + s->delay_frames[i] = s->delay_frames[i+1]; + + if (out) { + out->avframe.reference ^= DELAYED_PIC_REF; + *got_frame = 1; + if((ret = av_frame_ref(picture, &out->avframe)) < 0) + return ret; + } + + return 0; +} + +/** + * Dirac Specification -> + * 9.6 Parse Info Header Syntax. parse_info() + * 4 byte start code + byte parse code + 4 byte size + 4 byte previous size + */ +#define DATA_UNIT_HEADER_SIZE 13 + +/* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3 + inside the function parse_sequence() */ +static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size) +{ + DiracContext *s = avctx->priv_data; + DiracFrame *pic = NULL; + int ret, i, parse_code = buf[4]; + unsigned tmp; + + if (size < DATA_UNIT_HEADER_SIZE) + return -1; + + init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE)); + + if (parse_code == pc_seq_header) { + if (s->seen_sequence_header) + return 0; + + /* [DIRAC_STD] 10. Sequence header */ + if (avpriv_dirac_parse_sequence_header(avctx, &s->gb, &s->source)) + return -1; + + avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift); + + if (alloc_sequence_buffers(s)) + return -1; + + s->seen_sequence_header = 1; + } else if (parse_code == pc_eos) { /* [DIRAC_STD] End of Sequence */ + free_sequence_buffers(s); + s->seen_sequence_header = 0; + } else if (parse_code == pc_aux_data) { + if (buf[13] == 1) { /* encoder implementation/version */ + int ver[3]; + /* versions older than 1.0.8 don't store quant delta for + subbands with only one codeblock */ + if (sscanf(buf+14, "Schroedinger %d.%d.%d", ver, ver+1, ver+2) == 3) + if (ver[0] == 1 && ver[1] == 0 && ver[2] <= 7) + s->old_delta_quant = 1; + } + } else if (parse_code & 0x8) { /* picture data unit */ + if (!s->seen_sequence_header) { + av_log(avctx, AV_LOG_DEBUG, "Dropping frame without sequence header\n"); + return -1; + } + + /* find an unused frame */ + for (i = 0; i < MAX_FRAMES; i++) + if (s->all_frames[i].avframe.data[0] == NULL) + pic = &s->all_frames[i]; + if (!pic) { + av_log(avctx, AV_LOG_ERROR, "framelist full\n"); + return -1; + } + + avcodec_get_frame_defaults(&pic->avframe); + + /* [DIRAC_STD] Defined in 9.6.1 ... */ + tmp = parse_code & 0x03; /* [DIRAC_STD] num_refs() */ + if (tmp > 2) { + av_log(avctx, AV_LOG_ERROR, "num_refs of 3\n"); + return -1; + } + s->num_refs = tmp; + s->is_arith = (parse_code & 0x48) == 0x08; /* [DIRAC_STD] using_ac() */ + s->low_delay = (parse_code & 0x88) == 0x88; /* [DIRAC_STD] is_low_delay() */ + pic->avframe.reference = (parse_code & 0x0C) == 0x0C; /* [DIRAC_STD] is_reference() */ + pic->avframe.key_frame = s->num_refs == 0; /* [DIRAC_STD] is_intra() */ + pic->avframe.pict_type = s->num_refs + 1; /* Definition of AVPictureType in avutil.h */ + + if ((ret = ff_get_buffer(avctx, &pic->avframe, (parse_code & 0x0C) == 0x0C ? AV_GET_BUFFER_FLAG_REF : 0)) < 0) + return ret; + s->current_picture = pic; + s->plane[0].stride = pic->avframe.linesize[0]; + s->plane[1].stride = pic->avframe.linesize[1]; + s->plane[2].stride = pic->avframe.linesize[2]; + + /* [DIRAC_STD] 11.1 Picture parse. picture_parse() */ + if (dirac_decode_picture_header(s)) + return -1; + + /* [DIRAC_STD] 13.0 Transform data syntax. transform_data() */ + if (dirac_decode_frame_internal(s)) + return -1; + } + return 0; +} + +static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt) +{ + DiracContext *s = avctx->priv_data; + DiracFrame *picture = data; + uint8_t *buf = pkt->data; + int buf_size = pkt->size; + int i, data_unit_size, buf_idx = 0; + int ret; + + /* release unused frames */ + for (i = 0; i < MAX_FRAMES; i++) + if (s->all_frames[i].avframe.data[0] && !s->all_frames[i].avframe.reference) { + av_frame_unref(&s->all_frames[i].avframe); + memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated)); + } + + s->current_picture = NULL; + *got_frame = 0; + + /* end of stream, so flush delayed pics */ + if (buf_size == 0) + return get_delayed_pic(s, (AVFrame *)data, got_frame); + + for (;;) { + /*[DIRAC_STD] Here starts the code from parse_info() defined in 9.6 + [DIRAC_STD] PARSE_INFO_PREFIX = "BBCD" as defined in ISO/IEC 646 + BBCD start code search */ + for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) { + if (buf[buf_idx ] == 'B' && buf[buf_idx+1] == 'B' && + buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D') + break; + } + /* BBCD found or end of data */ + if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size) + break; + + data_unit_size = AV_RB32(buf+buf_idx+5); + if (buf_idx + data_unit_size > buf_size || !data_unit_size) { + if(buf_idx + data_unit_size > buf_size) + av_log(s->avctx, AV_LOG_ERROR, + "Data unit with size %d is larger than input buffer, discarding\n", + data_unit_size); + buf_idx += 4; + continue; + } + /* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3 inside the function parse_sequence() */ + if (dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size)) + { + av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n"); + return -1; + } + buf_idx += data_unit_size; + } + + if (!s->current_picture) + return buf_size; + + if (s->current_picture->avframe.display_picture_number > s->frame_number) { + DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number); + + s->current_picture->avframe.reference |= DELAYED_PIC_REF; + + if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) { + int min_num = s->delay_frames[0]->avframe.display_picture_number; + /* Too many delayed frames, so we display the frame with the lowest pts */ + av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n"); + delayed_frame = s->delay_frames[0]; + + for (i = 1; s->delay_frames[i]; i++) + if (s->delay_frames[i]->avframe.display_picture_number < min_num) + min_num = s->delay_frames[i]->avframe.display_picture_number; + + delayed_frame = remove_frame(s->delay_frames, min_num); + add_frame(s->delay_frames, MAX_DELAY, s->current_picture); + } + + if (delayed_frame) { + delayed_frame->avframe.reference ^= DELAYED_PIC_REF; + if((ret=av_frame_ref(data, &delayed_frame->avframe)) < 0) + return ret; + *got_frame = 1; + } + } else if (s->current_picture->avframe.display_picture_number == s->frame_number) { + /* The right frame at the right time :-) */ + if((ret=av_frame_ref(data, &s->current_picture->avframe)) < 0) + return ret; + *got_frame = 1; + } + + if (*got_frame) + s->frame_number = picture->avframe.display_picture_number + 1; + + return buf_idx; +} + +AVCodec ff_dirac_decoder = { + .name = "dirac", + .long_name = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_DIRAC, + .priv_data_size = sizeof(DiracContext), + .init = dirac_decode_init, + .close = dirac_decode_end, + .decode = dirac_decode_frame, + .capabilities = CODEC_CAP_DELAY, + .flush = dirac_decode_flush, +}; diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdsp.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdsp.c index 61f007ec5..8984c13b5 100644 --- a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdsp.c +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/diracdsp.c @@ -19,7 +19,6 @@ */ #include "config.h" - #include "dsputil.h" #include "diracdsp.h" #include "libavcodec/x86/diracdsp_mmx.h" diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.c new file mode 100644 index 000000000..937ac22ce --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.c @@ -0,0 +1,173 @@ +/* + * exp golomb vlc stuff + * Copyright (c) 2003 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * @brief + * exp golomb vlc stuff + * @author Michael Niedermayer + */ + +#include "libavutil/common.h" + +const uint8_t ff_golomb_vlc_len[512]={ +19,17,15,15,13,13,13,13,11,11,11,11,11,11,11,11,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, +7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, +5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, +5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +}; + +const uint8_t ff_ue_golomb_vlc_code[512]={ +32,32,32,32,32,32,32,32,31,32,32,32,32,32,32,32,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, + 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13,14,14,14,14, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +const int8_t ff_se_golomb_vlc_code[512]={ + 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 17, 17, 17, 17, 17, 17, 8, -8, 9, -9, 10,-10, 11,-11, 12,-12, 13,-13, 14,-14, 15,-15, + 4, 4, 4, 4, -4, -4, -4, -4, 5, 5, 5, 5, -5, -5, -5, -5, 6, 6, 6, 6, -6, -6, -6, -6, 7, 7, 7, 7, -7, -7, -7, -7, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + + +const uint8_t ff_ue_golomb_len[256]={ + 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11, +11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,13, +13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, +13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,15, +15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, +15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, +15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, +15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,17, +}; + +const uint8_t ff_interleaved_golomb_vlc_len[256]={ +9,9,7,7,9,9,7,7,5,5,5,5,5,5,5,5, +9,9,7,7,9,9,7,7,5,5,5,5,5,5,5,5, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +9,9,7,7,9,9,7,7,5,5,5,5,5,5,5,5, +9,9,7,7,9,9,7,7,5,5,5,5,5,5,5,5, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +}; + +const uint8_t ff_interleaved_ue_golomb_vlc_code[256]={ + 15,16,7, 7, 17,18,8, 8, 3, 3, 3, 3, 3, 3, 3, 3, + 19,20,9, 9, 21,22,10,10,4, 4, 4, 4, 4, 4, 4, 4, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 23,24,11,11,25,26,12,12,5, 5, 5, 5, 5, 5, 5, 5, + 27,28,13,13,29,30,14,14,6, 6, 6, 6, 6, 6, 6, 6, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +const int8_t ff_interleaved_se_golomb_vlc_code[256]={ + 8, -8, 4, 4, 9, -9, -4, -4, 2, 2, 2, 2, 2, 2, 2, 2, + 10,-10, 5, 5, 11,-11, -5, -5, -2, -2, -2, -2, -2, -2, -2, -2, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 12,-12, 6, 6, 13,-13, -6, -6, 3, 3, 3, 3, 3, 3, 3, 3, + 14,-14, 7, 7, 15,-15, -7, -7, -3, -3, -3, -3, -3, -3, -3, -3, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]={ +0, 1, 0, 0, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, +4, 5, 2, 2, 6, 7, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +8, 9, 4, 4, 10,11,5, 5, 2, 2, 2, 2, 2, 2, 2, 2, +12,13,6, 6, 14,15,7, 7, 3, 3, 3, 3, 3, 3, 3, 3, +1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}; diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.h new file mode 100644 index 000000000..66607ada2 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/golomb.h @@ -0,0 +1,557 @@ +/* + * exp golomb vlc stuff + * Copyright (c) 2003 Michael Niedermayer + * Copyright (c) 2004 Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * @brief + * exp golomb vlc stuff + * @author Michael Niedermayer and Alex Beregszaszi + */ + +#ifndef AVCODEC_GOLOMB_H +#define AVCODEC_GOLOMB_H + +#include +#include "get_bits.h" +#include "put_bits.h" + +#define INVALID_VLC 0x80000000 + +extern const uint8_t ff_golomb_vlc_len[512]; +extern const uint8_t ff_ue_golomb_vlc_code[512]; +extern const int8_t ff_se_golomb_vlc_code[512]; +extern const uint8_t ff_ue_golomb_len[256]; + +extern const uint8_t ff_interleaved_golomb_vlc_len[256]; +extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256]; +extern const int8_t ff_interleaved_se_golomb_vlc_code[256]; +extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]; + + + /** + * read unsigned exp golomb code. + */ +static inline int get_ue_golomb(GetBitContext *gb){ + unsigned int buf; + int log; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + if(buf >= (1<<27)){ + buf >>= 32 - 9; + LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_ue_golomb_vlc_code[buf]; + }else{ + log= 2*av_log2(buf) - 31; + LAST_SKIP_BITS(re, gb, 32 - log); + CLOSE_READER(re, gb); + if (CONFIG_FTRAPV && log < 0) { + av_log(0, AV_LOG_ERROR, "Invalid UE golomb code\n"); + return AVERROR_INVALIDDATA; + } + buf>>= log; + buf--; + + return buf; + } +} + +/** + * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1. + */ +static inline unsigned get_ue_golomb_long(GetBitContext *gb) +{ + unsigned buf, log; + + buf = show_bits_long(gb, 32); + log = 31 - av_log2(buf); + skip_bits_long(gb, log); + + return get_bits_long(gb, log + 1) - 1; +} + + /** + * read unsigned exp golomb code, constraint to a max of 31. + * the return value is undefined if the stored value exceeds 31. + */ +static inline int get_ue_golomb_31(GetBitContext *gb){ + unsigned int buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + buf >>= 32 - 9; + LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_ue_golomb_vlc_code[buf]; +} + +static inline unsigned svq3_get_ue_golomb(GetBitContext *gb) +{ + uint32_t buf; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + if(buf&0xAA800000){ + buf >>= 32 - 8; + LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_interleaved_ue_golomb_vlc_code[buf]; + }else{ + unsigned ret = 1; + + do { + buf >>= 32 - 8; + LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8)); + + if (ff_interleaved_golomb_vlc_len[buf] != 9){ + ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; + ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; + break; + } + ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + } while (ret<0x8000000U && HAVE_BITS_REMAINING(re, gb)); + + CLOSE_READER(re, gb); + return ret - 1; + } +} + +/** + * read unsigned truncated exp golomb code. + */ +static inline int get_te0_golomb(GetBitContext *gb, int range){ + av_assert2(range >= 1); + + if(range==1) return 0; + else if(range==2) return get_bits1(gb)^1; + else return get_ue_golomb(gb); +} + +/** + * read unsigned truncated exp golomb code. + */ +static inline int get_te_golomb(GetBitContext *gb, int range){ + av_assert2(range >= 1); + + if(range==2) return get_bits1(gb)^1; + else return get_ue_golomb(gb); +} + + +/** + * read signed exp golomb code. + */ +static inline int get_se_golomb(GetBitContext *gb){ + unsigned int buf; + int log; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + if(buf >= (1<<27)){ + buf >>= 32 - 9; + LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_se_golomb_vlc_code[buf]; + }else{ + log = av_log2(buf); + LAST_SKIP_BITS(re, gb, 31 - log); + UPDATE_CACHE(re, gb); + buf = GET_CACHE(re, gb); + + buf>>= log; + + LAST_SKIP_BITS(re, gb, 32 - log); + CLOSE_READER(re, gb); + + if(buf&1) buf= -(buf>>1); + else buf= (buf>>1); + + return buf; + } +} + +static inline int svq3_get_se_golomb(GetBitContext *gb){ + unsigned int buf; + int log; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + if(buf&0xAA800000){ + buf >>= 32 - 8; + LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); + CLOSE_READER(re, gb); + + return ff_interleaved_se_golomb_vlc_code[buf]; + }else{ + LAST_SKIP_BITS(re, gb, 8); + UPDATE_CACHE(re, gb); + buf |= 1 | (GET_CACHE(re, gb) >> 8); + + if((buf & 0xAAAAAAAA) == 0) + return INVALID_VLC; + + for(log=31; (buf & 0x80000000) == 0; log--){ + buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); + } + + LAST_SKIP_BITS(re, gb, 63 - 2*log - 8); + CLOSE_READER(re, gb); + + return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; + } +} + +static inline int dirac_get_se_golomb(GetBitContext *gb){ + uint32_t buf; + uint32_t ret; + + ret = svq3_get_ue_golomb(gb); + + if (ret) { + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf = SHOW_SBITS(re, gb, 1); + LAST_SKIP_BITS(re, gb, 1); + ret = (ret ^ buf) - buf; + CLOSE_READER(re, gb); + } + + return ret; +} + +/** + * read unsigned golomb rice code (ffv1). + */ +static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){ + unsigned int buf; + int log; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + log= av_log2(buf); + + if(log > 31-limit){ + buf >>= log - k; + buf += (30-log)<= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){ + buf >>= log - k; + buf += (30-log)<size_in_bits <= re_index) + return -1; + LAST_SKIP_BITS(re, gb, 1); + UPDATE_CACHE(re, gb); + } + SKIP_BITS(re, gb, 1); + + if(i < limit - 1){ + if(k){ + buf = SHOW_UBITS(re, gb, k); + LAST_SKIP_BITS(re, gb, k); + }else{ + buf=0; + } + + CLOSE_READER(re, gb); + return buf + (i<>1; + else return -(v>>1); + +// return (v>>1) ^ -(v&1); +} + +/** + * read signed golomb rice code (flac). + */ +static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){ + int v= get_ur_golomb_jpegls(gb, k, limit, esc_len); + return (v>>1) ^ -(v&1); +} + +/** + * read unsigned golomb rice code (shorten). + */ +static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){ + return get_ur_golomb_jpegls(gb, k, INT_MAX, 0); +} + +/** + * read signed golomb rice code (shorten). + */ +static inline int get_sr_golomb_shorten(GetBitContext* gb, int k) +{ + int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0); + if (uvar & 1) + return ~(uvar >> 1); + else + return uvar >> 1; +} + + + +#ifdef TRACE + +static inline int get_ue(GetBitContext *s, const char *file, const char *func, + int line) +{ + int show= show_bits(s, 24); + int pos= get_bits_count(s); + int i= get_ue_golomb(s); + int len= get_bits_count(s) - pos; + int bits= show>>(24-len); + + print_bin(bits, len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); + + return i; +} + +static inline int get_se(GetBitContext *s, const char *file, const char *func, + int line) +{ + int show= show_bits(s, 24); + int pos= get_bits_count(s); + int i= get_se_golomb(s); + int len= get_bits_count(s) - pos; + int bits= show>>(24-len); + + print_bin(bits, len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); + + return i; +} + +static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){ + int show= show_bits(s, 24); + int pos= get_bits_count(s); + int i= get_te0_golomb(s, r); + int len= get_bits_count(s) - pos; + int bits= show>>(24-len); + + print_bin(bits, len); + + av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); + + return i; +} + +#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__) +#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__) + +#endif + +/** + * write unsigned exp golomb code. + */ +static inline void set_ue_golomb(PutBitContext *pb, int i){ + int e; + + av_assert2(i>=0); + +#if 0 + if(i=0){ + put_bits(pb, 1, 1); + return; + } +#endif + if(i<256) + put_bits(pb, ff_ue_golomb_len[i], i+1); + else{ + e= av_log2(i+1); + + put_bits(pb, 2*e+1, i+1); + } +} + +/** + * write truncated unsigned exp golomb code. + */ +static inline void set_te_golomb(PutBitContext *pb, int i, int range){ + av_assert2(range >= 1); + av_assert2(i<=range); + + if(range==2) put_bits(pb, 1, i^1); + else set_ue_golomb(pb, i); +} + +/** + * write signed exp golomb code. 16 bits at most. + */ +static inline void set_se_golomb(PutBitContext *pb, int i){ +#if 0 + if(i<=0) i= -2*i; + else i= 2*i-1; +#elif 1 + i= 2*i-1; + if(i<0) i^= -1; //FIXME check if gcc does the right thing +#else + i= 2*i-1; + i^= (i>>31); +#endif + set_ue_golomb(pb, i); +} + +/** + * write unsigned golomb rice code (ffv1). + */ +static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ + int e; + + av_assert2(i>=0); + + e= i>>k; + if(e=0); + + e= (i>>k) + 1; + if(e 31) { + put_bits(pb, 31, 0); + e -= 31; + } + put_bits(pb, e, 1); + if(k) + put_sbits(pb, k, i); + }else{ + while(limit > 31) { + put_bits(pb, 31, 0); + limit -= 31; + } + put_bits(pb, limit , 1); + put_bits(pb, esc_len, i - 1); + } +} + +/** + * write signed golomb rice code (ffv1). + */ +static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ + int v; + + v = -2*i-1; + v ^= (v>>31); + + set_ur_golomb(pb, v, k, limit, esc_len); +} + +/** + * write signed golomb rice code (flac). + */ +static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){ + int v; + + v = -2*i-1; + v ^= (v>>31); + + set_ur_golomb_jpegls(pb, v, k, limit, esc_len); +} + +#endif /* AVCODEC_GOLOMB_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/jrevdct.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/jrevdct.c new file mode 100644 index 000000000..91780b2e4 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/jrevdct.c @@ -0,0 +1,1155 @@ +/* + * This file is part of the Independent JPEG Group's software. + * + * The authors make NO WARRANTY or representation, either express or implied, + * with respect to this software, its quality, accuracy, merchantability, or + * fitness for a particular purpose. This software is provided "AS IS", and + * you, its user, assume the entire risk as to its quality and accuracy. + * + * This software is copyright (C) 1991, 1992, Thomas G. Lane. + * All Rights Reserved except as specified below. + * + * Permission is hereby granted to use, copy, modify, and distribute this + * software (or portions thereof) for any purpose, without fee, subject to + * these conditions: + * (1) If any part of the source code for this software is distributed, then + * this README file must be included, with this copyright and no-warranty + * notice unaltered; and any additions, deletions, or changes to the original + * files must be clearly indicated in accompanying documentation. + * (2) If only executable code is distributed, then the accompanying + * documentation must state that "this software is based in part on the work + * of the Independent JPEG Group". + * (3) Permission for use of this software is granted only if the user accepts + * full responsibility for any undesirable consequences; the authors accept + * NO LIABILITY for damages of any kind. + * + * These conditions apply to any software derived from or based on the IJG + * code, not just to the unmodified library. If you use our work, you ought + * to acknowledge us. + * + * Permission is NOT granted for the use of any IJG author's name or company + * name in advertising or publicity relating to this software or products + * derived from it. This software may be referred to only as "the Independent + * JPEG Group's software". + * + * We specifically permit and encourage the use of this software as the basis + * of commercial products, provided that all warranty or liability claims are + * assumed by the product vendor. + * + * This file contains the basic inverse-DCT transformation subroutine. + * + * This implementation is based on an algorithm described in + * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT + * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, + * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. + * The primary algorithm described there uses 11 multiplies and 29 adds. + * We use their alternate method with 12 multiplies and 32 adds. + * The advantage of this method is that no data path contains more than one + * multiplication; this allows a very simple and accurate implementation in + * scaled fixed-point arithmetic, with a minimal number of shifts. + * + * I've made lots of modifications to attempt to take advantage of the + * sparse nature of the DCT matrices we're getting. Although the logic + * is cumbersome, it's straightforward and the resulting code is much + * faster. + * + * A better way to do this would be to pass in the DCT block as a sparse + * matrix, perhaps with the difference cases encoded. + */ + +/** + * @file + * Independent JPEG Group's LLM idct. + */ + +#include "libavutil/common.h" +#include "dct.h" + +#define EIGHT_BIT_SAMPLES + +#define DCTSIZE 8 +#define DCTSIZE2 64 + +#define GLOBAL + +#define RIGHT_SHIFT(x, n) ((x) >> (n)) + +typedef int16_t DCTBLOCK[DCTSIZE2]; + +#define CONST_BITS 13 + +/* + * This routine is specialized to the case DCTSIZE = 8. + */ + +#if DCTSIZE != 8 + Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ +#endif + + +/* + * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT + * on each column. Direct algorithms are also available, but they are + * much more complex and seem not to be any faster when reduced to code. + * + * The poop on this scaling stuff is as follows: + * + * Each 1-D IDCT step produces outputs which are a factor of sqrt(N) + * larger than the true IDCT outputs. The final outputs are therefore + * a factor of N larger than desired; since N=8 this can be cured by + * a simple right shift at the end of the algorithm. The advantage of + * this arrangement is that we save two multiplications per 1-D IDCT, + * because the y0 and y4 inputs need not be divided by sqrt(N). + * + * We have to do addition and subtraction of the integer inputs, which + * is no problem, and multiplication by fractional constants, which is + * a problem to do in integer arithmetic. We multiply all the constants + * by CONST_SCALE and convert them to integer constants (thus retaining + * CONST_BITS bits of precision in the constants). After doing a + * multiplication we have to divide the product by CONST_SCALE, with proper + * rounding, to produce the correct output. This division can be done + * cheaply as a right shift of CONST_BITS bits. We postpone shifting + * as long as possible so that partial sums can be added together with + * full fractional precision. + * + * The outputs of the first pass are scaled up by PASS1_BITS bits so that + * they are represented to better-than-integral precision. These outputs + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word + * with the recommended scaling. (To scale up 12-bit sample data further, an + * intermediate int32 array would be needed.) + * + * To avoid overflow of the 32-bit intermediate results in pass 2, we must + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis + * shows that the values given below are the most effective. + */ + +#ifdef EIGHT_BIT_SAMPLES +#define PASS1_BITS 2 +#else +#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#endif + +#define ONE ((int32_t) 1) + +#define CONST_SCALE (ONE << CONST_BITS) + +/* Convert a positive real constant to an integer scaled by CONST_SCALE. + * IMPORTANT: if your compiler doesn't do this arithmetic at compile time, + * you will pay a significant penalty in run time. In that case, figure + * the correct integer constant values and insert them by hand. + */ + +/* Actually FIX is no longer used, we precomputed them all */ +#define FIX(x) ((int32_t) ((x) * CONST_SCALE + 0.5)) + +/* Descale and correctly round an int32_t value that's scaled by N bits. + * We assume RIGHT_SHIFT rounds towards minus infinity, so adding + * the fudge factor is correct for either sign of X. + */ + +#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n) + +/* Multiply an int32_t variable by an int32_t constant to yield an int32_t result. + * For 8-bit samples with the recommended scaling, all the variable + * and constant values involved are no more than 16 bits wide, so a + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply; + * this provides a useful speedup on many machines. + * There is no way to specify a 16x16->32 multiply in portable C, but + * some C compilers will do the right thing if you provide the correct + * combination of casts. + * NB: for 12-bit samples, a full 32-bit multiplication will be needed. + */ + +#ifdef EIGHT_BIT_SAMPLES +#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ +#define MULTIPLY(var,const) (((int16_t) (var)) * ((int16_t) (const))) +#endif +#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ +#define MULTIPLY(var,const) (((int16_t) (var)) * ((int32_t) (const))) +#endif +#endif + +#ifndef MULTIPLY /* default definition */ +#define MULTIPLY(var,const) ((var) * (const)) +#endif + + +/* + Unlike our decoder where we approximate the FIXes, we need to use exact +ones here or successive P-frames will drift too much with Reference frame coding +*/ +#define FIX_0_211164243 1730 +#define FIX_0_275899380 2260 +#define FIX_0_298631336 2446 +#define FIX_0_390180644 3196 +#define FIX_0_509795579 4176 +#define FIX_0_541196100 4433 +#define FIX_0_601344887 4926 +#define FIX_0_765366865 6270 +#define FIX_0_785694958 6436 +#define FIX_0_899976223 7373 +#define FIX_1_061594337 8697 +#define FIX_1_111140466 9102 +#define FIX_1_175875602 9633 +#define FIX_1_306562965 10703 +#define FIX_1_387039845 11363 +#define FIX_1_451774981 11893 +#define FIX_1_501321110 12299 +#define FIX_1_662939225 13623 +#define FIX_1_847759065 15137 +#define FIX_1_961570560 16069 +#define FIX_2_053119869 16819 +#define FIX_2_172734803 17799 +#define FIX_2_562915447 20995 +#define FIX_3_072711026 25172 + +/* + * Perform the inverse DCT on one block of coefficients. + */ + +void ff_j_rev_dct(DCTBLOCK data) +{ + int32_t tmp0, tmp1, tmp2, tmp3; + int32_t tmp10, tmp11, tmp12, tmp13; + int32_t z1, z2, z3, z4, z5; + int32_t d0, d1, d2, d3, d4, d5, d6, d7; + register int16_t *dataptr; + int rowctr; + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + dataptr = data; + + for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any row in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * row DCT calculations can be simplified this way. + */ + + register int *idataptr = (int*)dataptr; + + /* WARNING: we do the same permutation as MMX idct to simplify the + video core */ + d0 = dataptr[0]; + d2 = dataptr[1]; + d4 = dataptr[2]; + d6 = dataptr[3]; + d1 = dataptr[4]; + d3 = dataptr[5]; + d5 = dataptr[6]; + d7 = dataptr[7]; + + if ((d1 | d2 | d3 | d4 | d5 | d6 | d7) == 0) { + /* AC terms all zero */ + if (d0) { + /* Compute a 32 bit value to assign. */ + int16_t dcval = (int16_t) (d0 << PASS1_BITS); + register int v = (dcval & 0xffff) | ((dcval << 16) & 0xffff0000); + + idataptr[0] = v; + idataptr[1] = v; + idataptr[2] = v; + idataptr[3] = v; + } + + dataptr += DCTSIZE; /* advance pointer to next row */ + continue; + } + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ +{ + if (d6) { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ + z1 = MULTIPLY(d2 + d6, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); + tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ + tmp2 = MULTIPLY(-d6, FIX_1_306562965); + tmp3 = MULTIPLY(d6, FIX_0_541196100); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } + } else { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ + tmp2 = MULTIPLY(d2, FIX_0_541196100); + tmp3 = MULTIPLY(d2, FIX_1_306562965); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ + tmp10 = tmp13 = (d0 + d4) << CONST_BITS; + tmp11 = tmp12 = (d0 - d4) << CONST_BITS; + } + } + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + + if (d7) { + if (d5) { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */ + z1 = d7 + d1; + z2 = d5 + d3; + z3 = d7 + d3; + z4 = d5 + d1; + z5 = MULTIPLY(z3 + z4, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-z1, FIX_0_899976223); + z2 = MULTIPLY(-z2, FIX_2_562915447); + z3 = MULTIPLY(-z3, FIX_1_961570560); + z4 = MULTIPLY(-z4, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */ + z2 = d5 + d3; + z3 = d7 + d3; + z5 = MULTIPLY(z3 + d5, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + z1 = MULTIPLY(-d7, FIX_0_899976223); + z2 = MULTIPLY(-z2, FIX_2_562915447); + z3 = MULTIPLY(-z3, FIX_1_961570560); + z4 = MULTIPLY(-d5, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 = z1 + z4; + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */ + z1 = d7 + d1; + z4 = d5 + d1; + z5 = MULTIPLY(d7 + z4, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-z1, FIX_0_899976223); + z2 = MULTIPLY(-d5, FIX_2_562915447); + z3 = MULTIPLY(-d7, FIX_1_961570560); + z4 = MULTIPLY(-z4, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 = z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */ + tmp0 = MULTIPLY(-d7, FIX_0_601344887); + z1 = MULTIPLY(-d7, FIX_0_899976223); + z3 = MULTIPLY(-d7, FIX_1_961570560); + tmp1 = MULTIPLY(-d5, FIX_0_509795579); + z2 = MULTIPLY(-d5, FIX_2_562915447); + z4 = MULTIPLY(-d5, FIX_0_390180644); + z5 = MULTIPLY(d5 + d7, FIX_1_175875602); + + z3 += z5; + z4 += z5; + + tmp0 += z3; + tmp1 += z4; + tmp2 = z2 + z3; + tmp3 = z1 + z4; + } + } + } else { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */ + z1 = d7 + d1; + z3 = d7 + d3; + z5 = MULTIPLY(z3 + d1, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-z1, FIX_0_899976223); + z2 = MULTIPLY(-d3, FIX_2_562915447); + z3 = MULTIPLY(-z3, FIX_1_961570560); + z4 = MULTIPLY(-d1, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 = z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */ + z3 = d7 + d3; + + tmp0 = MULTIPLY(-d7, FIX_0_601344887); + z1 = MULTIPLY(-d7, FIX_0_899976223); + tmp2 = MULTIPLY(d3, FIX_0_509795579); + z2 = MULTIPLY(-d3, FIX_2_562915447); + z5 = MULTIPLY(z3, FIX_1_175875602); + z3 = MULTIPLY(-z3, FIX_0_785694958); + + tmp0 += z3; + tmp1 = z2 + z5; + tmp2 += z3; + tmp3 = z1 + z5; + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */ + z1 = d7 + d1; + z5 = MULTIPLY(z1, FIX_1_175875602); + + z1 = MULTIPLY(z1, FIX_0_275899380); + z3 = MULTIPLY(-d7, FIX_1_961570560); + tmp0 = MULTIPLY(-d7, FIX_1_662939225); + z4 = MULTIPLY(-d1, FIX_0_390180644); + tmp3 = MULTIPLY(d1, FIX_1_111140466); + + tmp0 += z1; + tmp1 = z4 + z5; + tmp2 = z3 + z5; + tmp3 += z1; + } else { + /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */ + tmp0 = MULTIPLY(-d7, FIX_1_387039845); + tmp1 = MULTIPLY(d7, FIX_1_175875602); + tmp2 = MULTIPLY(-d7, FIX_0_785694958); + tmp3 = MULTIPLY(d7, FIX_0_275899380); + } + } + } + } else { + if (d5) { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */ + z2 = d5 + d3; + z4 = d5 + d1; + z5 = MULTIPLY(d3 + z4, FIX_1_175875602); + + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-d1, FIX_0_899976223); + z2 = MULTIPLY(-z2, FIX_2_562915447); + z3 = MULTIPLY(-d3, FIX_1_961570560); + z4 = MULTIPLY(-z4, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 = z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */ + z2 = d5 + d3; + + z5 = MULTIPLY(z2, FIX_1_175875602); + tmp1 = MULTIPLY(d5, FIX_1_662939225); + z4 = MULTIPLY(-d5, FIX_0_390180644); + z2 = MULTIPLY(-z2, FIX_1_387039845); + tmp2 = MULTIPLY(d3, FIX_1_111140466); + z3 = MULTIPLY(-d3, FIX_1_961570560); + + tmp0 = z3 + z5; + tmp1 += z2; + tmp2 += z2; + tmp3 = z4 + z5; + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */ + z4 = d5 + d1; + + z5 = MULTIPLY(z4, FIX_1_175875602); + z1 = MULTIPLY(-d1, FIX_0_899976223); + tmp3 = MULTIPLY(d1, FIX_0_601344887); + tmp1 = MULTIPLY(-d5, FIX_0_509795579); + z2 = MULTIPLY(-d5, FIX_2_562915447); + z4 = MULTIPLY(z4, FIX_0_785694958); + + tmp0 = z1 + z5; + tmp1 += z4; + tmp2 = z2 + z5; + tmp3 += z4; + } else { + /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */ + tmp0 = MULTIPLY(d5, FIX_1_175875602); + tmp1 = MULTIPLY(d5, FIX_0_275899380); + tmp2 = MULTIPLY(-d5, FIX_1_387039845); + tmp3 = MULTIPLY(d5, FIX_0_785694958); + } + } + } else { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */ + z5 = d1 + d3; + tmp3 = MULTIPLY(d1, FIX_0_211164243); + tmp2 = MULTIPLY(-d3, FIX_1_451774981); + z1 = MULTIPLY(d1, FIX_1_061594337); + z2 = MULTIPLY(-d3, FIX_2_172734803); + z4 = MULTIPLY(z5, FIX_0_785694958); + z5 = MULTIPLY(z5, FIX_1_175875602); + + tmp0 = z1 - z4; + tmp1 = z2 + z4; + tmp2 += z5; + tmp3 += z5; + } else { + /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */ + tmp0 = MULTIPLY(-d3, FIX_0_785694958); + tmp1 = MULTIPLY(-d3, FIX_1_387039845); + tmp2 = MULTIPLY(-d3, FIX_0_275899380); + tmp3 = MULTIPLY(d3, FIX_1_175875602); + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */ + tmp0 = MULTIPLY(d1, FIX_0_275899380); + tmp1 = MULTIPLY(d1, FIX_0_785694958); + tmp2 = MULTIPLY(d1, FIX_1_175875602); + tmp3 = MULTIPLY(d1, FIX_1_387039845); + } else { + /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */ + tmp0 = tmp1 = tmp2 = tmp3 = 0; + } + } + } + } +} + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + dataptr[0] = (int16_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS); + dataptr[7] = (int16_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS); + dataptr[1] = (int16_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS); + dataptr[6] = (int16_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS); + dataptr[2] = (int16_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS); + dataptr[5] = (int16_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS); + dataptr[3] = (int16_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS); + dataptr[4] = (int16_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS); + + dataptr += DCTSIZE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + dataptr = data; + for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) { + /* Columns of zeroes can be exploited in the same way as we did with rows. + * However, the row calculation has created many nonzero AC terms, so the + * simplification applies less often (typically 5% to 10% of the time). + * On machines with very fast multiplication, it's possible that the + * test takes more time than it's worth. In that case this section + * may be commented out. + */ + + d0 = dataptr[DCTSIZE*0]; + d1 = dataptr[DCTSIZE*1]; + d2 = dataptr[DCTSIZE*2]; + d3 = dataptr[DCTSIZE*3]; + d4 = dataptr[DCTSIZE*4]; + d5 = dataptr[DCTSIZE*5]; + d6 = dataptr[DCTSIZE*6]; + d7 = dataptr[DCTSIZE*7]; + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + if (d6) { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ + z1 = MULTIPLY(d2 + d6, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); + tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ + tmp2 = MULTIPLY(-d6, FIX_1_306562965); + tmp3 = MULTIPLY(d6, FIX_0_541196100); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } + } else { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ + tmp2 = MULTIPLY(d2, FIX_0_541196100); + tmp3 = MULTIPLY(d2, FIX_1_306562965); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ + tmp10 = tmp13 = (d0 + d4) << CONST_BITS; + tmp11 = tmp12 = (d0 - d4) << CONST_BITS; + } + } + + /* Odd part per figure 8; the matrix is unitary and hence its + * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. + */ + if (d7) { + if (d5) { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */ + z1 = d7 + d1; + z2 = d5 + d3; + z3 = d7 + d3; + z4 = d5 + d1; + z5 = MULTIPLY(z3 + z4, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-z1, FIX_0_899976223); + z2 = MULTIPLY(-z2, FIX_2_562915447); + z3 = MULTIPLY(-z3, FIX_1_961570560); + z4 = MULTIPLY(-z4, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */ + z2 = d5 + d3; + z3 = d7 + d3; + z5 = MULTIPLY(z3 + d5, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + z1 = MULTIPLY(-d7, FIX_0_899976223); + z2 = MULTIPLY(-z2, FIX_2_562915447); + z3 = MULTIPLY(-z3, FIX_1_961570560); + z4 = MULTIPLY(-d5, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 = z1 + z4; + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */ + z1 = d7 + d1; + z3 = d7; + z4 = d5 + d1; + z5 = MULTIPLY(z3 + z4, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-z1, FIX_0_899976223); + z2 = MULTIPLY(-d5, FIX_2_562915447); + z3 = MULTIPLY(-d7, FIX_1_961570560); + z4 = MULTIPLY(-z4, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 = z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */ + tmp0 = MULTIPLY(-d7, FIX_0_601344887); + z1 = MULTIPLY(-d7, FIX_0_899976223); + z3 = MULTIPLY(-d7, FIX_1_961570560); + tmp1 = MULTIPLY(-d5, FIX_0_509795579); + z2 = MULTIPLY(-d5, FIX_2_562915447); + z4 = MULTIPLY(-d5, FIX_0_390180644); + z5 = MULTIPLY(d5 + d7, FIX_1_175875602); + + z3 += z5; + z4 += z5; + + tmp0 += z3; + tmp1 += z4; + tmp2 = z2 + z3; + tmp3 = z1 + z4; + } + } + } else { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */ + z1 = d7 + d1; + z3 = d7 + d3; + z5 = MULTIPLY(z3 + d1, FIX_1_175875602); + + tmp0 = MULTIPLY(d7, FIX_0_298631336); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-z1, FIX_0_899976223); + z2 = MULTIPLY(-d3, FIX_2_562915447); + z3 = MULTIPLY(-z3, FIX_1_961570560); + z4 = MULTIPLY(-d1, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 += z1 + z3; + tmp1 = z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */ + z3 = d7 + d3; + + tmp0 = MULTIPLY(-d7, FIX_0_601344887); + z1 = MULTIPLY(-d7, FIX_0_899976223); + tmp2 = MULTIPLY(d3, FIX_0_509795579); + z2 = MULTIPLY(-d3, FIX_2_562915447); + z5 = MULTIPLY(z3, FIX_1_175875602); + z3 = MULTIPLY(-z3, FIX_0_785694958); + + tmp0 += z3; + tmp1 = z2 + z5; + tmp2 += z3; + tmp3 = z1 + z5; + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */ + z1 = d7 + d1; + z5 = MULTIPLY(z1, FIX_1_175875602); + + z1 = MULTIPLY(z1, FIX_0_275899380); + z3 = MULTIPLY(-d7, FIX_1_961570560); + tmp0 = MULTIPLY(-d7, FIX_1_662939225); + z4 = MULTIPLY(-d1, FIX_0_390180644); + tmp3 = MULTIPLY(d1, FIX_1_111140466); + + tmp0 += z1; + tmp1 = z4 + z5; + tmp2 = z3 + z5; + tmp3 += z1; + } else { + /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */ + tmp0 = MULTIPLY(-d7, FIX_1_387039845); + tmp1 = MULTIPLY(d7, FIX_1_175875602); + tmp2 = MULTIPLY(-d7, FIX_0_785694958); + tmp3 = MULTIPLY(d7, FIX_0_275899380); + } + } + } + } else { + if (d5) { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */ + z2 = d5 + d3; + z4 = d5 + d1; + z5 = MULTIPLY(d3 + z4, FIX_1_175875602); + + tmp1 = MULTIPLY(d5, FIX_2_053119869); + tmp2 = MULTIPLY(d3, FIX_3_072711026); + tmp3 = MULTIPLY(d1, FIX_1_501321110); + z1 = MULTIPLY(-d1, FIX_0_899976223); + z2 = MULTIPLY(-z2, FIX_2_562915447); + z3 = MULTIPLY(-d3, FIX_1_961570560); + z4 = MULTIPLY(-z4, FIX_0_390180644); + + z3 += z5; + z4 += z5; + + tmp0 = z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + } else { + /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */ + z2 = d5 + d3; + + z5 = MULTIPLY(z2, FIX_1_175875602); + tmp1 = MULTIPLY(d5, FIX_1_662939225); + z4 = MULTIPLY(-d5, FIX_0_390180644); + z2 = MULTIPLY(-z2, FIX_1_387039845); + tmp2 = MULTIPLY(d3, FIX_1_111140466); + z3 = MULTIPLY(-d3, FIX_1_961570560); + + tmp0 = z3 + z5; + tmp1 += z2; + tmp2 += z2; + tmp3 = z4 + z5; + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */ + z4 = d5 + d1; + + z5 = MULTIPLY(z4, FIX_1_175875602); + z1 = MULTIPLY(-d1, FIX_0_899976223); + tmp3 = MULTIPLY(d1, FIX_0_601344887); + tmp1 = MULTIPLY(-d5, FIX_0_509795579); + z2 = MULTIPLY(-d5, FIX_2_562915447); + z4 = MULTIPLY(z4, FIX_0_785694958); + + tmp0 = z1 + z5; + tmp1 += z4; + tmp2 = z2 + z5; + tmp3 += z4; + } else { + /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */ + tmp0 = MULTIPLY(d5, FIX_1_175875602); + tmp1 = MULTIPLY(d5, FIX_0_275899380); + tmp2 = MULTIPLY(-d5, FIX_1_387039845); + tmp3 = MULTIPLY(d5, FIX_0_785694958); + } + } + } else { + if (d3) { + if (d1) { + /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */ + z5 = d1 + d3; + tmp3 = MULTIPLY(d1, FIX_0_211164243); + tmp2 = MULTIPLY(-d3, FIX_1_451774981); + z1 = MULTIPLY(d1, FIX_1_061594337); + z2 = MULTIPLY(-d3, FIX_2_172734803); + z4 = MULTIPLY(z5, FIX_0_785694958); + z5 = MULTIPLY(z5, FIX_1_175875602); + + tmp0 = z1 - z4; + tmp1 = z2 + z4; + tmp2 += z5; + tmp3 += z5; + } else { + /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */ + tmp0 = MULTIPLY(-d3, FIX_0_785694958); + tmp1 = MULTIPLY(-d3, FIX_1_387039845); + tmp2 = MULTIPLY(-d3, FIX_0_275899380); + tmp3 = MULTIPLY(d3, FIX_1_175875602); + } + } else { + if (d1) { + /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */ + tmp0 = MULTIPLY(d1, FIX_0_275899380); + tmp1 = MULTIPLY(d1, FIX_0_785694958); + tmp2 = MULTIPLY(d1, FIX_1_175875602); + tmp3 = MULTIPLY(d1, FIX_1_387039845); + } else { + /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */ + tmp0 = tmp1 = tmp2 = tmp3 = 0; + } + } + } + } + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + dataptr[DCTSIZE*0] = (int16_t) DESCALE(tmp10 + tmp3, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*7] = (int16_t) DESCALE(tmp10 - tmp3, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*1] = (int16_t) DESCALE(tmp11 + tmp2, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*6] = (int16_t) DESCALE(tmp11 - tmp2, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*2] = (int16_t) DESCALE(tmp12 + tmp1, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*5] = (int16_t) DESCALE(tmp12 - tmp1, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*3] = (int16_t) DESCALE(tmp13 + tmp0, + CONST_BITS+PASS1_BITS+3); + dataptr[DCTSIZE*4] = (int16_t) DESCALE(tmp13 - tmp0, + CONST_BITS+PASS1_BITS+3); + + dataptr++; /* advance pointer to next column */ + } +} + +#undef DCTSIZE +#define DCTSIZE 4 +#define DCTSTRIDE 8 + +void ff_j_rev_dct4(DCTBLOCK data) +{ + int32_t tmp0, tmp1, tmp2, tmp3; + int32_t tmp10, tmp11, tmp12, tmp13; + int32_t z1; + int32_t d0, d2, d4, d6; + register int16_t *dataptr; + int rowctr; + + /* Pass 1: process rows. */ + /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ + /* furthermore, we scale the results by 2**PASS1_BITS. */ + + data[0] += 4; + + dataptr = data; + + for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) { + /* Due to quantization, we will usually find that many of the input + * coefficients are zero, especially the AC terms. We can exploit this + * by short-circuiting the IDCT calculation for any row in which all + * the AC terms are zero. In that case each output is equal to the + * DC coefficient (with scale factor as needed). + * With typical images and quantization tables, half or more of the + * row DCT calculations can be simplified this way. + */ + + register int *idataptr = (int*)dataptr; + + d0 = dataptr[0]; + d2 = dataptr[1]; + d4 = dataptr[2]; + d6 = dataptr[3]; + + if ((d2 | d4 | d6) == 0) { + /* AC terms all zero */ + if (d0) { + /* Compute a 32 bit value to assign. */ + int16_t dcval = (int16_t) (d0 << PASS1_BITS); + register int v = (dcval & 0xffff) | ((dcval << 16) & 0xffff0000); + + idataptr[0] = v; + idataptr[1] = v; + } + + dataptr += DCTSTRIDE; /* advance pointer to next row */ + continue; + } + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + if (d6) { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ + z1 = MULTIPLY(d2 + d6, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); + tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ + tmp2 = MULTIPLY(-d6, FIX_1_306562965); + tmp3 = MULTIPLY(d6, FIX_0_541196100); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } + } else { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ + tmp2 = MULTIPLY(d2, FIX_0_541196100); + tmp3 = MULTIPLY(d2, FIX_1_306562965); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ + tmp10 = tmp13 = (d0 + d4) << CONST_BITS; + tmp11 = tmp12 = (d0 - d4) << CONST_BITS; + } + } + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + dataptr[0] = (int16_t) DESCALE(tmp10, CONST_BITS-PASS1_BITS); + dataptr[1] = (int16_t) DESCALE(tmp11, CONST_BITS-PASS1_BITS); + dataptr[2] = (int16_t) DESCALE(tmp12, CONST_BITS-PASS1_BITS); + dataptr[3] = (int16_t) DESCALE(tmp13, CONST_BITS-PASS1_BITS); + + dataptr += DCTSTRIDE; /* advance pointer to next row */ + } + + /* Pass 2: process columns. */ + /* Note that we must descale the results by a factor of 8 == 2**3, */ + /* and also undo the PASS1_BITS scaling. */ + + dataptr = data; + for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) { + /* Columns of zeroes can be exploited in the same way as we did with rows. + * However, the row calculation has created many nonzero AC terms, so the + * simplification applies less often (typically 5% to 10% of the time). + * On machines with very fast multiplication, it's possible that the + * test takes more time than it's worth. In that case this section + * may be commented out. + */ + + d0 = dataptr[DCTSTRIDE*0]; + d2 = dataptr[DCTSTRIDE*1]; + d4 = dataptr[DCTSTRIDE*2]; + d6 = dataptr[DCTSTRIDE*3]; + + /* Even part: reverse the even part of the forward DCT. */ + /* The rotator is sqrt(2)*c(-6). */ + if (d6) { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ + z1 = MULTIPLY(d2 + d6, FIX_0_541196100); + tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); + tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ + tmp2 = MULTIPLY(-d6, FIX_1_306562965); + tmp3 = MULTIPLY(d6, FIX_0_541196100); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } + } else { + if (d2) { + /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ + tmp2 = MULTIPLY(d2, FIX_0_541196100); + tmp3 = MULTIPLY(d2, FIX_1_306562965); + + tmp0 = (d0 + d4) << CONST_BITS; + tmp1 = (d0 - d4) << CONST_BITS; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + } else { + /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ + tmp10 = tmp13 = (d0 + d4) << CONST_BITS; + tmp11 = tmp12 = (d0 - d4) << CONST_BITS; + } + } + + /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ + + dataptr[DCTSTRIDE*0] = tmp10 >> (CONST_BITS+PASS1_BITS+3); + dataptr[DCTSTRIDE*1] = tmp11 >> (CONST_BITS+PASS1_BITS+3); + dataptr[DCTSTRIDE*2] = tmp12 >> (CONST_BITS+PASS1_BITS+3); + dataptr[DCTSTRIDE*3] = tmp13 >> (CONST_BITS+PASS1_BITS+3); + + dataptr++; /* advance pointer to next column */ + } +} + +void ff_j_rev_dct2(DCTBLOCK data){ + int d00, d01, d10, d11; + + data[0] += 4; + d00 = data[0+0*DCTSTRIDE] + data[1+0*DCTSTRIDE]; + d01 = data[0+0*DCTSTRIDE] - data[1+0*DCTSTRIDE]; + d10 = data[0+1*DCTSTRIDE] + data[1+1*DCTSTRIDE]; + d11 = data[0+1*DCTSTRIDE] - data[1+1*DCTSTRIDE]; + + data[0+0*DCTSTRIDE]= (d00 + d10)>>3; + data[1+0*DCTSTRIDE]= (d01 + d11)>>3; + data[0+1*DCTSTRIDE]= (d00 - d10)>>3; + data[1+1*DCTSTRIDE]= (d01 - d11)>>3; +} + +void ff_j_rev_dct1(DCTBLOCK data){ + data[0] = (data[0] + 4)>>3; +} + +#undef FIX +#undef CONST_BITS diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.c new file mode 100644 index 000000000..ed41ca8a2 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.c @@ -0,0 +1,158 @@ +/* + * TAK common code + * Copyright (c) 2012 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/crc.h" +#include "libavutil/intreadwrite.h" +#include "tak.h" + +static const int64_t tak_channel_layouts[] = { + 0, + AV_CH_FRONT_LEFT, + AV_CH_FRONT_RIGHT, + AV_CH_FRONT_CENTER, + AV_CH_LOW_FREQUENCY, + AV_CH_BACK_LEFT, + AV_CH_BACK_RIGHT, + AV_CH_FRONT_LEFT_OF_CENTER, + AV_CH_FRONT_RIGHT_OF_CENTER, + AV_CH_BACK_CENTER, + AV_CH_SIDE_LEFT, + AV_CH_SIDE_RIGHT, + AV_CH_TOP_CENTER, + AV_CH_TOP_FRONT_LEFT, + AV_CH_TOP_FRONT_CENTER, + AV_CH_TOP_FRONT_RIGHT, + AV_CH_TOP_BACK_LEFT, + AV_CH_TOP_BACK_CENTER, + AV_CH_TOP_BACK_RIGHT, +}; + +static const uint16_t frame_duration_type_quants[] = { + 3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048, +}; + +static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type) +{ + int nb_samples, max_nb_samples; + + if (type <= TAK_FST_250ms) { + nb_samples = sample_rate * frame_duration_type_quants[type] >> + TAK_FRAME_DURATION_QUANT_SHIFT; + max_nb_samples = 16384; + } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) { + nb_samples = frame_duration_type_quants[type]; + max_nb_samples = sample_rate * + frame_duration_type_quants[TAK_FST_250ms] >> + TAK_FRAME_DURATION_QUANT_SHIFT; + } else { + return AVERROR_INVALIDDATA; + } + + if (nb_samples <= 0 || nb_samples > max_nb_samples) + return AVERROR_INVALIDDATA; + + return nb_samples; +} + +int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size) +{ + uint32_t crc, CRC; + + if (buf_size < 4) + return AVERROR_INVALIDDATA; + buf_size -= 3; + + CRC = AV_RB24(buf + buf_size); + crc = av_crc(av_crc_get_table(AV_CRC_24_IEEE), 0xCE04B7U, buf, buf_size); + if (CRC != crc) + return AVERROR_INVALIDDATA; + + return 0; +} + +void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s) +{ + uint64_t channel_mask = 0; + int frame_type, i; + + s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS); + skip_bits(gb, TAK_ENCODER_PROFILE_BITS); + + frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS); + s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS); + + s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS); + s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) + + TAK_SAMPLE_RATE_MIN; + s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) + + TAK_BPS_MIN; + s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) + + TAK_CHANNELS_MIN; + + if (get_bits1(gb)) { + skip_bits(gb, TAK_FORMAT_VALID_BITS); + if (get_bits1(gb)) { + for (i = 0; i < s->channels; i++) { + int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS); + + if (value < FF_ARRAY_ELEMS(tak_channel_layouts)) + channel_mask |= tak_channel_layouts[value]; + } + } + } + + s->ch_layout = channel_mask; + s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type); +} + +int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, + TAKStreamInfo *ti, int log_level_offset) +{ + if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) { + av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n"); + return AVERROR_INVALIDDATA; + } + + ti->flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS); + ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS); + + if (ti->flags & TAK_FRAME_FLAG_IS_LAST) { + ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1; + skip_bits(gb, 2); + } else { + ti->last_frame_samples = 0; + } + + if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) { + avpriv_tak_parse_streaminfo(gb, ti); + + if (get_bits(gb, 6)) + skip_bits(gb, 25); + align_get_bits(gb); + } + + if (ti->flags & TAK_FRAME_FLAG_HAS_METADATA) + return AVERROR_INVALIDDATA; + + skip_bits(gb, 24); + + return 0; +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.h new file mode 100644 index 000000000..e8e2dacf7 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak.h @@ -0,0 +1,163 @@ +/* + * TAK decoder/demuxer common code + * Copyright (c) 2012 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions + */ + +#ifndef AVCODEC_TAK_H +#define AVCODEC_TAK_H + +#include + +#define BITSTREAM_READER_LE +#include "get_bits.h" +#include "avcodec.h" + +#define TAK_FORMAT_DATA_TYPE_BITS 3 +#define TAK_FORMAT_SAMPLE_RATE_BITS 18 +#define TAK_FORMAT_BPS_BITS 5 +#define TAK_FORMAT_CHANNEL_BITS 4 +#define TAK_FORMAT_VALID_BITS 5 +#define TAK_FORMAT_CH_LAYOUT_BITS 6 +#define TAK_SIZE_FRAME_DURATION_BITS 4 +#define TAK_SIZE_SAMPLES_NUM_BITS 35 +#define TAK_LAST_FRAME_POS_BITS 40 +#define TAK_LAST_FRAME_SIZE_BITS 24 +#define TAK_ENCODER_CODEC_BITS 6 +#define TAK_ENCODER_PROFILE_BITS 4 +#define TAK_ENCODER_VERSION_BITS 24 +#define TAK_SAMPLE_RATE_MIN 6000 +#define TAK_CHANNELS_MIN 1 +#define TAK_BPS_MIN 8 +#define TAK_FRAME_HEADER_FLAGS_BITS 3 +#define TAK_FRAME_HEADER_SYNC_ID 0xA0FF +#define TAK_FRAME_HEADER_SYNC_ID_BITS 16 +#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS 14 +#define TAK_FRAME_HEADER_NO_BITS 21 +#define TAK_FRAME_DURATION_QUANT_SHIFT 5 +#define TAK_CRC24_BITS 24 + + +#define TAK_FRAME_FLAG_IS_LAST 0x1 +#define TAK_FRAME_FLAG_HAS_INFO 0x2 +#define TAK_FRAME_FLAG_HAS_METADATA 0x4 + +#define TAK_MAX_CHANNELS (1 << TAK_FORMAT_CHANNEL_BITS) + +#define TAK_MIN_FRAME_HEADER_BITS (TAK_FRAME_HEADER_SYNC_ID_BITS + \ + TAK_FRAME_HEADER_FLAGS_BITS + \ + TAK_FRAME_HEADER_NO_BITS + \ + TAK_CRC24_BITS) + +#define TAK_MIN_FRAME_HEADER_LAST_BITS (TAK_MIN_FRAME_HEADER_BITS + 2 + \ + TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + +#define TAK_ENCODER_BITS (TAK_ENCODER_CODEC_BITS + \ + TAK_ENCODER_PROFILE_BITS) + +#define TAK_SIZE_BITS (TAK_SIZE_SAMPLES_NUM_BITS + \ + TAK_SIZE_FRAME_DURATION_BITS) + +#define TAK_FORMAT_BITS (TAK_FORMAT_DATA_TYPE_BITS + \ + TAK_FORMAT_SAMPLE_RATE_BITS + \ + TAK_FORMAT_BPS_BITS + \ + TAK_FORMAT_CHANNEL_BITS + 1 + \ + TAK_FORMAT_VALID_BITS + 1 + \ + TAK_FORMAT_CH_LAYOUT_BITS * \ + TAK_MAX_CHANNELS) + +#define TAK_STREAMINFO_BITS (TAK_ENCODER_BITS + \ + TAK_SIZE_BITS + \ + TAK_FORMAT_BITS) + +#define TAK_MAX_FRAME_HEADER_BITS (TAK_MIN_FRAME_HEADER_LAST_BITS + \ + TAK_STREAMINFO_BITS + 31) + +#define TAK_STREAMINFO_BYTES ((TAK_STREAMINFO_BITS + 7) / 8) +#define TAK_MAX_FRAME_HEADER_BYTES ((TAK_MAX_FRAME_HEADER_BITS + 7) / 8) +#define TAK_MIN_FRAME_HEADER_BYTES ((TAK_MIN_FRAME_HEADER_BITS + 7) / 8) + +enum TAKCodecType { + TAK_CODEC_MONO_STEREO = 2, + TAK_CODEC_MULTICHANNEL = 4, +}; + +enum TAKMetaDataType { + TAK_METADATA_END = 0, + TAK_METADATA_STREAMINFO, + TAK_METADATA_SEEKTABLE, + TAK_METADATA_SIMPLE_WAVE_DATA, + TAK_METADATA_ENCODER, + TAK_METADATA_PADDING, + TAK_METADATA_MD5, + TAK_METADATA_LAST_FRAME, +}; + +enum TAKFrameSizeType { + TAK_FST_94ms = 0, + TAK_FST_125ms, + TAK_FST_188ms, + TAK_FST_250ms, + TAK_FST_4096, + TAK_FST_8192, + TAK_FST_16384, + TAK_FST_512, + TAK_FST_1024, + TAK_FST_2048, +}; + +typedef struct TAKStreamInfo { + int flags; + enum TAKCodecType codec; + int data_type; + int sample_rate; + int channels; + int bps; + int frame_num; + int frame_samples; + int last_frame_samples; + uint64_t ch_layout; + int64_t samples; +} TAKStreamInfo; + +int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size); + +/** + * Parse the Streaminfo metadata block. + * @param[in] gb pointer to GetBitContext + * @param[out] s storage for parsed information + */ +void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s); + +/** + * Validate and decode a frame header. + * @param avctx AVCodecContext to use as av_log() context + * @param[in] gb GetBitContext from which to read frame header + * @param[out] s frame information + * @param log_level_offset log level offset, can be used to silence + * error messages. + * @return non-zero on error, 0 if OK + */ +int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, + TAKStreamInfo *s, int log_level_offset); +#endif /* AVCODEC_TAK_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak_parser.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak_parser.c new file mode 100644 index 000000000..5d8460c90 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/tak_parser.c @@ -0,0 +1,122 @@ +/* + * TAK parser + * Copyright (c) 2012 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * TAK parser + **/ + +#include "tak.h" +#include "parser.h" + +typedef struct TAKParseContext { + ParseContext pc; + TAKStreamInfo ti; + int index; +} TAKParseContext; + +static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + TAKParseContext *t = s->priv_data; + ParseContext *pc = &t->pc; + int next = END_NOT_FOUND; + GetBitContext gb; + int consumed = 0; + int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { + TAKStreamInfo ti; + init_get_bits(&gb, buf, buf_size); + if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127)) + s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples + : t->ti.frame_samples; + *poutbuf = buf; + *poutbuf_size = buf_size; + return buf_size; + } + + while (buf_size || t->index + needed <= pc->index) { + if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) { + int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES, + buf_size); + const uint8_t *tmp_buf = buf; + + if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1) + return AVERROR(ENOMEM); + consumed += tmp_buf_size; + buf += tmp_buf_size; + buf_size -= tmp_buf_size; + } + + for (; t->index + needed <= pc->index; t->index++) { + if (pc->buffer[ t->index ] == 0xFF && + pc->buffer[ t->index + 1 ] == 0xA0) { + TAKStreamInfo ti; + + init_get_bits(&gb, pc->buffer + t->index, + 8 * (pc->index - t->index)); + if (!ff_tak_decode_frame_header(avctx, &gb, + pc->frame_start_found ? &ti : &t->ti, 127) && + !ff_tak_check_crc(pc->buffer + t->index, + get_bits_count(&gb) / 8)) { + if (!pc->frame_start_found) { + pc->frame_start_found = 1; + s->duration = t->ti.last_frame_samples ? + t->ti.last_frame_samples : + t->ti.frame_samples; + s->key_frame = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO); + } else { + pc->frame_start_found = 0; + next = t->index - pc->index; + t->index = 0; + goto found; + } + } + } + } + } +found: + + if (consumed && !buf_size && next == END_NOT_FOUND || + ff_combine_frame(pc, next, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size + consumed; + } + + if (next != END_NOT_FOUND) { + next += consumed; + pc->overread = FFMAX(0, -next); + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + +AVCodecParser ff_tak_parser = { + .codec_ids = { AV_CODEC_ID_TAK }, + .priv_data_size = sizeof(TAKParseContext), + .parser_parse = tak_parse, + .parser_close = ff_parse_close, +}; diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/takdec.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/takdec.c new file mode 100644 index 000000000..0edfddd7d --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/takdec.c @@ -0,0 +1,948 @@ +/* + * TAK decoder + * Copyright (c) 2012 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * TAK (Tom's lossless Audio Kompressor) decoder + * @author Paul B Mahol + */ + +#include "libavutil/internal.h" +#include "libavutil/samplefmt.h" +#include "tak.h" +#include "thread.h" +#include "avcodec.h" +#include "dsputil.h" +#include "internal.h" +#include "unary.h" + +#define MAX_SUBFRAMES 8 ///< max number of subframes per channel +#define MAX_PREDICTORS 256 + +typedef struct MCDParam { + int8_t present; ///< decorrelation parameter availability for this channel + int8_t index; ///< index into array of decorrelation types + int8_t chan1; + int8_t chan2; +} MCDParam; + +typedef struct TAKDecContext { + AVCodecContext *avctx; ///< parent AVCodecContext + DSPContext dsp; + TAKStreamInfo ti; + GetBitContext gb; ///< bitstream reader initialized to start at the current frame + + int uval; + int nb_samples; ///< number of samples in the current frame + uint8_t *decode_buffer; + unsigned int decode_buffer_size; + int32_t *decoded[TAK_MAX_CHANNELS]; ///< decoded samples for each channel + + int8_t lpc_mode[TAK_MAX_CHANNELS]; + int8_t sample_shift[TAK_MAX_CHANNELS]; ///< shift applied to every sample in the channel + int16_t predictors[MAX_PREDICTORS]; + int nb_subframes; ///< number of subframes in the current frame + int16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples + int subframe_scale; + + int8_t dmode; ///< channel decorrelation type in the current frame + + MCDParam mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters + + int8_t coding_mode[128]; + DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS]; + DECLARE_ALIGNED(16, int16_t, residues)[544]; +} TAKDecContext; + +static const int8_t mc_dmodes[] = { 1, 3, 4, 6, }; + +static const uint16_t predictor_sizes[] = { + 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0, +}; + +static const struct CParam { + int init; + int escape; + int scale; + int aescape; + int bias; +} xcodes[50] = { + { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 }, + { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 }, + { 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D }, + { 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 }, + { 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 }, + { 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 }, + { 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 }, + { 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 }, + { 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 }, + { 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 }, + { 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 }, + { 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 }, + { 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 }, + { 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 }, + { 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 }, + { 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 }, + { 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 }, + { 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 }, + { 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 }, + { 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 }, + { 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 }, + { 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 }, + { 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 }, + { 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 }, + { 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 }, + { 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 }, + { 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 }, + { 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 }, + { 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 }, + { 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 }, + { 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 }, + { 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 }, + { 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 }, + { 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 }, + { 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 }, + { 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 }, + { 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 }, + { 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 }, + { 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 }, + { 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 }, + { 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 }, + { 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 }, + { 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 }, + { 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 }, + { 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 }, + { 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 }, + { 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 }, + { 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 }, + { 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 }, + { 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 }, +}; + +static int set_bps_params(AVCodecContext *avctx) +{ + switch (avctx->bits_per_raw_sample) { + case 8: + avctx->sample_fmt = AV_SAMPLE_FMT_U8P; + break; + case 16: + avctx->sample_fmt = AV_SAMPLE_FMT_S16P; + break; + case 24: + avctx->sample_fmt = AV_SAMPLE_FMT_S32P; + break; + default: + av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample: %d\n", + avctx->bits_per_raw_sample); + return AVERROR_INVALIDDATA; + } + + return 0; +} + +static void set_sample_rate_params(AVCodecContext *avctx) +{ + TAKDecContext *s = avctx->priv_data; + int shift = 3 - (avctx->sample_rate / 11025); + shift = FFMAX(0, shift); + s->uval = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift; + s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1; +} + +static av_cold int tak_decode_init(AVCodecContext *avctx) +{ + TAKDecContext *s = avctx->priv_data; + + ff_dsputil_init(&s->dsp, avctx); + + s->avctx = avctx; + avctx->bits_per_raw_sample = avctx->bits_per_coded_sample; + + set_sample_rate_params(avctx); + + return set_bps_params(avctx); +} + +static void decode_lpc(int32_t *coeffs, int mode, int length) +{ + int i; + + if (length < 2) + return; + + if (mode == 1) { + int a1 = *coeffs++; + for (i = 0; i < length - 1 >> 1; i++) { + *coeffs += a1; + coeffs[1] += *coeffs; + a1 = coeffs[1]; + coeffs += 2; + } + if (length - 1 & 1) + *coeffs += a1; + } else if (mode == 2) { + int a1 = coeffs[1]; + int a2 = a1 + *coeffs; + coeffs[1] = a2; + if (length > 2) { + coeffs += 2; + for (i = 0; i < length - 2 >> 1; i++) { + int a3 = *coeffs + a1; + int a4 = a3 + a2; + *coeffs = a4; + a1 = coeffs[1] + a3; + a2 = a1 + a4; + coeffs[1] = a2; + coeffs += 2; + } + if (length & 1) + *coeffs += a1 + a2; + } + } else if (mode == 3) { + int a1 = coeffs[1]; + int a2 = a1 + *coeffs; + coeffs[1] = a2; + if (length > 2) { + int a3 = coeffs[2]; + int a4 = a3 + a1; + int a5 = a4 + a2; + coeffs += 3; + for (i = 0; i < length - 3; i++) { + a3 += *coeffs; + a4 += a3; + a5 += a4; + *coeffs = a5; + coeffs++; + } + } + } +} + +static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len) +{ + struct CParam code; + GetBitContext *gb = &s->gb; + int i; + + if (!mode) { + memset(decoded, 0, len * sizeof(*decoded)); + return 0; + } + + if (mode > FF_ARRAY_ELEMS(xcodes)) + return AVERROR_INVALIDDATA; + code = xcodes[mode - 1]; + + for (i = 0; i < len; i++) { + int x = get_bits_long(gb, code.init); + if (x >= code.escape && get_bits1(gb)) { + x |= 1 << code.init; + if (x >= code.aescape) { + int scale = get_unary(gb, 1, 9); + if (scale == 9) { + int scale_bits = get_bits(gb, 3); + if (scale_bits > 0) { + if (scale_bits == 7) { + scale_bits += get_bits(gb, 5); + if (scale_bits > 29) + return AVERROR_INVALIDDATA; + } + scale = get_bits_long(gb, scale_bits) + 1; + x += code.scale * scale; + } + x += code.bias; + } else + x += code.scale * scale - code.escape; + } else + x -= code.escape; + } + decoded[i] = (x >> 1) ^ -(x & 1); + } + + return 0; +} + +static int decode_residues(TAKDecContext *s, int32_t *decoded, int length) +{ + GetBitContext *gb = &s->gb; + int i, mode, ret; + + if (length > s->nb_samples) + return AVERROR_INVALIDDATA; + + if (get_bits1(gb)) { + int wlength, rval; + + wlength = length / s->uval; + + rval = length - (wlength * s->uval); + + if (rval < s->uval / 2) + rval += s->uval; + else + wlength++; + + if (wlength <= 1 || wlength > 128) + return AVERROR_INVALIDDATA; + + s->coding_mode[0] = mode = get_bits(gb, 6); + + for (i = 1; i < wlength; i++) { + int c = get_unary(gb, 1, 6); + + switch (c) { + case 6: + mode = get_bits(gb, 6); + break; + case 5: + case 4: + case 3: { + /* mode += sign ? (1 - c) : (c - 1) */ + int sign = get_bits1(gb); + mode += (-sign ^ (c - 1)) + sign; + break; + } + case 2: + mode++; + break; + case 1: + mode--; + break; + } + s->coding_mode[i] = mode; + } + + i = 0; + while (i < wlength) { + int len = 0; + + mode = s->coding_mode[i]; + do { + if (i >= wlength - 1) + len += rval; + else + len += s->uval; + i++; + + if (i == wlength) + break; + } while (s->coding_mode[i] == mode); + + if ((ret = decode_segment(s, mode, decoded, len)) < 0) + return ret; + decoded += len; + } + } else { + mode = get_bits(gb, 6); + if ((ret = decode_segment(s, mode, decoded, length)) < 0) + return ret; + } + + return 0; +} + +static int get_bits_esc4(GetBitContext *gb) +{ + if (get_bits1(gb)) + return get_bits(gb, 4) + 1; + else + return 0; +} + +static int decode_subframe(TAKDecContext *s, int32_t *decoded, + int subframe_size, int prev_subframe_size) +{ + GetBitContext *gb = &s->gb; + int tmp, x, y, i, j, ret = 0; + int dshift, size, filter_quant, filter_order; + int tfilter[MAX_PREDICTORS]; + + if (!get_bits1(gb)) + return decode_residues(s, decoded, subframe_size); + + filter_order = predictor_sizes[get_bits(gb, 4)]; + + if (prev_subframe_size > 0 && get_bits1(gb)) { + if (filter_order > prev_subframe_size) + return AVERROR_INVALIDDATA; + + decoded -= filter_order; + subframe_size += filter_order; + + if (filter_order > subframe_size) + return AVERROR_INVALIDDATA; + } else { + int lpc_mode; + + if (filter_order > subframe_size) + return AVERROR_INVALIDDATA; + + lpc_mode = get_bits(gb, 2); + if (lpc_mode > 2) + return AVERROR_INVALIDDATA; + + if ((ret = decode_residues(s, decoded, filter_order)) < 0) + return ret; + + if (lpc_mode) + decode_lpc(decoded, lpc_mode, filter_order); + } + + dshift = get_bits_esc4(gb); + size = get_bits1(gb) + 6; + + filter_quant = 10; + if (get_bits1(gb)) { + filter_quant -= get_bits(gb, 3) + 1; + if (filter_quant < 3) + return AVERROR_INVALIDDATA; + } + + s->predictors[0] = get_sbits(gb, 10); + s->predictors[1] = get_sbits(gb, 10); + s->predictors[2] = get_sbits(gb, size) << (10 - size); + s->predictors[3] = get_sbits(gb, size) << (10 - size); + if (filter_order > 4) { + tmp = size - get_bits1(gb); + + for (i = 4; i < filter_order; i++) { + if (!(i & 3)) + x = tmp - get_bits(gb, 2); + s->predictors[i] = get_sbits(gb, x) << (10 - size); + } + } + + tfilter[0] = s->predictors[0] << 6; + for (i = 1; i < filter_order; i++) { + int32_t *p1 = &tfilter[0]; + int32_t *p2 = &tfilter[i - 1]; + + for (j = 0; j < (i + 1) / 2; j++) { + x = *p1 + (s->predictors[i] * *p2 + 256 >> 9); + *p2 += s->predictors[i] * *p1 + 256 >> 9; + *p1++ = x; + p2--; + } + + tfilter[i] = s->predictors[i] << 6; + } + + x = 1 << (32 - (15 - filter_quant)); + y = 1 << ((15 - filter_quant) - 1); + for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) { + tmp = y + tfilter[j]; + s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant)); + s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant)); + } + + if ((ret = decode_residues(s, &decoded[filter_order], + subframe_size - filter_order)) < 0) + return ret; + + for (i = 0; i < filter_order; i++) + s->residues[i] = *decoded++ >> dshift; + + y = FF_ARRAY_ELEMS(s->residues) - filter_order; + x = subframe_size - filter_order; + while (x > 0) { + tmp = FFMIN(y, x); + + for (i = 0; i < tmp; i++) { + int v = 1 << (filter_quant - 1); + + if (filter_order & -16) + v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter, + filter_order & -16); + for (j = filter_order & -16; j < filter_order; j += 4) { + v += s->residues[i + j + 3] * s->filter[j + 3] + + s->residues[i + j + 2] * s->filter[j + 2] + + s->residues[i + j + 1] * s->filter[j + 1] + + s->residues[i + j ] * s->filter[j ]; + } + v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded; + *decoded++ = v; + s->residues[filter_order + i] = v >> dshift; + } + + x -= tmp; + if (x > 0) + memcpy(s->residues, &s->residues[y], 2 * filter_order); + } + + emms_c(); + + return 0; +} + +static int decode_channel(TAKDecContext *s, int chan) +{ + AVCodecContext *avctx = s->avctx; + GetBitContext *gb = &s->gb; + int32_t *decoded = s->decoded[chan]; + int left = s->nb_samples - 1; + int i = 0, ret, prev = 0; + + s->sample_shift[chan] = get_bits_esc4(gb); + if (s->sample_shift[chan] >= avctx->bits_per_raw_sample) + return AVERROR_INVALIDDATA; + + *decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]); + s->lpc_mode[chan] = get_bits(gb, 2); + s->nb_subframes = get_bits(gb, 3) + 1; + + if (s->nb_subframes > 1) { + if (get_bits_left(gb) < (s->nb_subframes - 1) * 6) + return AVERROR_INVALIDDATA; + + for (; i < s->nb_subframes - 1; i++) { + int v = get_bits(gb, 6); + + s->subframe_len[i] = (v - prev) * s->subframe_scale; + if (s->subframe_len[i] <= 0) + return AVERROR_INVALIDDATA; + + left -= s->subframe_len[i]; + prev = v; + } + + if (left <= 0) + return AVERROR_INVALIDDATA; + } + s->subframe_len[i] = left; + + prev = 0; + for (i = 0; i < s->nb_subframes; i++) { + if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0) + return ret; + decoded += s->subframe_len[i]; + prev = s->subframe_len[i]; + } + + return 0; +} + +static int decorrelate(TAKDecContext *s, int c1, int c2, int length) +{ + GetBitContext *gb = &s->gb; + int32_t *p1 = s->decoded[c1] + 1; + int32_t *p2 = s->decoded[c2] + 1; + int i; + int dshift, dfactor; + + switch (s->dmode) { + case 1: /* left/side */ + for (i = 0; i < length; i++) { + int32_t a = p1[i]; + int32_t b = p2[i]; + p2[i] = a + b; + } + break; + case 2: /* side/right */ + for (i = 0; i < length; i++) { + int32_t a = p1[i]; + int32_t b = p2[i]; + p1[i] = b - a; + } + break; + case 3: /* side/mid */ + for (i = 0; i < length; i++) { + int32_t a = p1[i]; + int32_t b = p2[i]; + a -= b >> 1; + p1[i] = a; + p2[i] = a + b; + } + break; + case 4: /* side/left with scale factor */ + FFSWAP(int32_t*, p1, p2); + case 5: /* side/right with scale factor */ + dshift = get_bits_esc4(gb); + dfactor = get_sbits(gb, 10); + for (i = 0; i < length; i++) { + int32_t a = p1[i]; + int32_t b = p2[i]; + b = dfactor * (b >> dshift) + 128 >> 8 << dshift; + p1[i] = b - a; + } + break; + case 6: + FFSWAP(int32_t*, p1, p2); + case 7: { + int length2, order_half, filter_order, dval1, dval2; + int tmp, x, code_size; + + if (length < 256) + return AVERROR_INVALIDDATA; + + dshift = get_bits_esc4(gb); + filter_order = 8 << get_bits1(gb); + dval1 = get_bits1(gb); + dval2 = get_bits1(gb); + + for (i = 0; i < filter_order; i++) { + if (!(i & 3)) + code_size = 14 - get_bits(gb, 3); + s->filter[i] = get_sbits(gb, code_size); + } + + order_half = filter_order / 2; + length2 = length - (filter_order - 1); + + /* decorrelate beginning samples */ + if (dval1) { + for (i = 0; i < order_half; i++) { + int32_t a = p1[i]; + int32_t b = p2[i]; + p1[i] = a + b; + } + } + + /* decorrelate ending samples */ + if (dval2) { + for (i = length2 + order_half; i < length; i++) { + int32_t a = p1[i]; + int32_t b = p2[i]; + p1[i] = a + b; + } + } + + + for (i = 0; i < filter_order; i++) + s->residues[i] = *p2++ >> dshift; + + p1 += order_half; + x = FF_ARRAY_ELEMS(s->residues) - filter_order; + for (; length2 > 0; length2 -= tmp) { + tmp = FFMIN(length2, x); + + for (i = 0; i < tmp; i++) + s->residues[filter_order + i] = *p2++ >> dshift; + + for (i = 0; i < tmp; i++) { + int v = 1 << 9; + + if (filter_order == 16) { + v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter, + filter_order); + } else { + v += s->residues[i + 7] * s->filter[7] + + s->residues[i + 6] * s->filter[6] + + s->residues[i + 5] * s->filter[5] + + s->residues[i + 4] * s->filter[4] + + s->residues[i + 3] * s->filter[3] + + s->residues[i + 2] * s->filter[2] + + s->residues[i + 1] * s->filter[1] + + s->residues[i ] * s->filter[0]; + } + + v = (av_clip(v >> 10, -8192, 8191) << dshift) - *p1; + *p1++ = v; + } + + memcpy(s->residues, &s->residues[tmp], 2 * filter_order); + } + + emms_c(); + break; + } + } + + return 0; +} + +static int tak_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *pkt) +{ + TAKDecContext *s = avctx->priv_data; + AVFrame *frame = data; + ThreadFrame tframe = { .f = data }; + GetBitContext *gb = &s->gb; + int chan, i, ret, hsize; + + if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES) + return AVERROR_INVALIDDATA; + + if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0) + return ret; + + if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0) + return ret; + + if (avctx->err_recognition & AV_EF_CRCCHECK) { + hsize = get_bits_count(gb) / 8; + if (ff_tak_check_crc(pkt->data, hsize)) { + av_log(avctx, AV_LOG_ERROR, "CRC error\n"); + return AVERROR_INVALIDDATA; + } + } + + if (s->ti.codec != TAK_CODEC_MONO_STEREO && + s->ti.codec != TAK_CODEC_MULTICHANNEL) { + av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec); + return AVERROR_PATCHWELCOME; + } + if (s->ti.data_type) { + av_log(avctx, AV_LOG_ERROR, + "unsupported data type: %d\n", s->ti.data_type); + return AVERROR_INVALIDDATA; + } + if (s->ti.codec == TAK_CODEC_MONO_STEREO && s->ti.channels > 2) { + av_log(avctx, AV_LOG_ERROR, + "invalid number of channels: %d\n", s->ti.channels); + return AVERROR_INVALIDDATA; + } + if (s->ti.channels > 6) { + av_log(avctx, AV_LOG_ERROR, + "unsupported number of channels: %d\n", s->ti.channels); + return AVERROR_INVALIDDATA; + } + + if (s->ti.frame_samples <= 0) { + av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n"); + return AVERROR_INVALIDDATA; + } + + if (s->ti.bps != avctx->bits_per_raw_sample) { + avctx->bits_per_raw_sample = s->ti.bps; + if ((ret = set_bps_params(avctx)) < 0) + return ret; + } + if (s->ti.sample_rate != avctx->sample_rate) { + avctx->sample_rate = s->ti.sample_rate; + set_sample_rate_params(avctx); + } + if (s->ti.ch_layout) + avctx->channel_layout = s->ti.ch_layout; + avctx->channels = s->ti.channels; + + s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples + : s->ti.frame_samples; + + frame->nb_samples = s->nb_samples; + if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0) + return ret; + ff_thread_finish_setup(avctx); + + if (avctx->bits_per_raw_sample <= 16) { + int buf_size = av_samples_get_buffer_size(NULL, avctx->channels, + s->nb_samples, + AV_SAMPLE_FMT_S32P, 0); + av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size); + if (!s->decode_buffer) + return AVERROR(ENOMEM); + ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL, + s->decode_buffer, avctx->channels, + s->nb_samples, AV_SAMPLE_FMT_S32P, 0); + if (ret < 0) + return ret; + } else { + for (chan = 0; chan < avctx->channels; chan++) + s->decoded[chan] = (int32_t *)frame->extended_data[chan]; + } + + if (s->nb_samples < 16) { + for (chan = 0; chan < avctx->channels; chan++) { + int32_t *decoded = s->decoded[chan]; + for (i = 0; i < s->nb_samples; i++) + decoded[i] = get_sbits(gb, avctx->bits_per_raw_sample); + } + } else { + if (s->ti.codec == TAK_CODEC_MONO_STEREO) { + for (chan = 0; chan < avctx->channels; chan++) + if (ret = decode_channel(s, chan)) + return ret; + + if (avctx->channels == 2) { + s->nb_subframes = get_bits(gb, 1) + 1; + if (s->nb_subframes > 1) { + s->subframe_len[1] = get_bits(gb, 6); + } + + s->dmode = get_bits(gb, 3); + if (ret = decorrelate(s, 0, 1, s->nb_samples - 1)) + return ret; + } + } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) { + if (get_bits1(gb)) { + int ch_mask = 0; + + chan = get_bits(gb, 4) + 1; + if (chan > avctx->channels) + return AVERROR_INVALIDDATA; + + for (i = 0; i < chan; i++) { + int nbit = get_bits(gb, 4); + + if (nbit >= avctx->channels) + return AVERROR_INVALIDDATA; + + if (ch_mask & 1 << nbit) + return AVERROR_INVALIDDATA; + + s->mcdparams[i].present = get_bits1(gb); + if (s->mcdparams[i].present) { + s->mcdparams[i].index = get_bits(gb, 2); + s->mcdparams[i].chan2 = get_bits(gb, 4); + if (s->mcdparams[i].index == 1) { + if ((nbit == s->mcdparams[i].chan2) || + (ch_mask & 1 << s->mcdparams[i].chan2)) + return AVERROR_INVALIDDATA; + + ch_mask |= 1 << s->mcdparams[i].chan2; + } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) { + return AVERROR_INVALIDDATA; + } + } + s->mcdparams[i].chan1 = nbit; + + ch_mask |= 1 << nbit; + } + } else { + chan = avctx->channels; + for (i = 0; i < chan; i++) { + s->mcdparams[i].present = 0; + s->mcdparams[i].chan1 = i; + } + } + + for (i = 0; i < chan; i++) { + if (s->mcdparams[i].present && s->mcdparams[i].index == 1) + if (ret = decode_channel(s, s->mcdparams[i].chan2)) + return ret; + + if (ret = decode_channel(s, s->mcdparams[i].chan1)) + return ret; + + if (s->mcdparams[i].present) { + s->dmode = mc_dmodes[s->mcdparams[i].index]; + if (ret = decorrelate(s, + s->mcdparams[i].chan2, + s->mcdparams[i].chan1, + s->nb_samples - 1)) + return ret; + } + } + } + + for (chan = 0; chan < avctx->channels; chan++) { + int32_t *decoded = s->decoded[chan]; + + if (s->lpc_mode[chan]) + decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples); + + if (s->sample_shift[chan] > 0) + for (i = 0; i < s->nb_samples; i++) + decoded[i] <<= s->sample_shift[chan]; + } + } + + align_get_bits(gb); + skip_bits(gb, 24); + if (get_bits_left(gb) < 0) + av_log(avctx, AV_LOG_DEBUG, "overread\n"); + else if (get_bits_left(gb) > 0) + av_log(avctx, AV_LOG_DEBUG, "underread\n"); + + if (avctx->err_recognition & AV_EF_CRCCHECK) { + if (ff_tak_check_crc(pkt->data + hsize, + get_bits_count(gb) / 8 - hsize)) { + av_log(avctx, AV_LOG_ERROR, "CRC error\n"); + return AVERROR_INVALIDDATA; + } + } + + /* convert to output buffer */ + switch (avctx->sample_fmt) { + case AV_SAMPLE_FMT_U8P: + for (chan = 0; chan < avctx->channels; chan++) { + uint8_t *samples = (uint8_t *)frame->extended_data[chan]; + int32_t *decoded = s->decoded[chan]; + for (i = 0; i < s->nb_samples; i++) + samples[i] = decoded[i] + 0x80; + } + break; + case AV_SAMPLE_FMT_S16P: + for (chan = 0; chan < avctx->channels; chan++) { + int16_t *samples = (int16_t *)frame->extended_data[chan]; + int32_t *decoded = s->decoded[chan]; + for (i = 0; i < s->nb_samples; i++) + samples[i] = decoded[i]; + } + break; + case AV_SAMPLE_FMT_S32P: + for (chan = 0; chan < avctx->channels; chan++) { + int32_t *samples = (int32_t *)frame->extended_data[chan]; + for (i = 0; i < s->nb_samples; i++) + samples[i] <<= 8; + } + break; + } + + *got_frame_ptr = 1; + + return pkt->size; +} + +static int init_thread_copy(AVCodecContext *avctx) +{ + TAKDecContext *s = avctx->priv_data; + s->avctx = avctx; + return 0; +} + +static int update_thread_context(AVCodecContext *dst, + const AVCodecContext *src) +{ + TAKDecContext *tsrc = src->priv_data; + TAKDecContext *tdst = dst->priv_data; + + if (dst == src) + return 0; + memcpy(&tdst->ti, &tsrc->ti, sizeof(TAKStreamInfo)); + return 0; +} + +static av_cold int tak_decode_close(AVCodecContext *avctx) +{ + TAKDecContext *s = avctx->priv_data; + + av_freep(&s->decode_buffer); + + return 0; +} + +AVCodec ff_tak_decoder = { + .name = "tak", + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_TAK, + .priv_data_size = sizeof(TAKDecContext), + .init = tak_decode_init, + .close = tak_decode_close, + .decode = tak_decode_frame, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy), + .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context), + .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS, + .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"), + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P, + AV_SAMPLE_FMT_S16P, + AV_SAMPLE_FMT_S32P, + AV_SAMPLE_FMT_NONE }, +}; diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/unary.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/unary.h new file mode 100644 index 000000000..908dc9350 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/unary.h @@ -0,0 +1,56 @@ +/* + * copyright (c) 2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_UNARY_H +#define AVCODEC_UNARY_H + +#include "get_bits.h" + +/** + * Get unary code of limited length + * @param gb GetBitContext + * @param[in] stop The bitstop value (unary code of 1's or 0's) + * @param[in] len Maximum length + * @return Unary length/index + */ +static inline int get_unary(GetBitContext *gb, int stop, int len) +{ + int i; + + for(i = 0; i < len && get_bits1(gb) != stop; i++); + return i; +} + +/** + * Get unary code terminated by a 0 with a maximum length of 33 + * @param gb GetBitContext + * @return Unary length/index + */ +static inline int get_unary_0_33(GetBitContext *gb) +{ + return get_unary(gb, 0, 33); +} + +static inline int get_unary_0_9(GetBitContext *gb) +{ + return get_unary(gb, 0, 9); +} + +#endif /* AVCODEC_UNARY_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.c new file mode 100644 index 000000000..04c514f4f --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.c @@ -0,0 +1,202 @@ +/* + * MMX optimized discrete wavelet transform + * Copyright (c) 2002-2004 Michael Niedermayer + * Copyright (c) 2010 David Conrad + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/x86/asm.h" +#include "dsputil_x86.h" +#include "dirac_dwt.h" + +#define COMPOSE_VERTICAL(ext, align) \ +void ff_vertical_compose53iL0##ext(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width); \ +void ff_vertical_compose_dirac53iH0##ext(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width); \ +void ff_vertical_compose_dd137iL0##ext(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, int width); \ +void ff_vertical_compose_dd97iH0##ext(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, int width); \ +void ff_vertical_compose_haar##ext(IDWTELEM *b0, IDWTELEM *b1, int width); \ +void ff_horizontal_compose_haar0i##ext(IDWTELEM *b, IDWTELEM *tmp, int w);\ +void ff_horizontal_compose_haar1i##ext(IDWTELEM *b, IDWTELEM *tmp, int w);\ +\ +static void vertical_compose53iL0##ext(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width) \ +{ \ + int i, width_align = width&~(align-1); \ +\ + for(i=width_align; i>1;\ + int x= w2 - (w2&(align-1));\ + ff_horizontal_compose_haar0i##ext(b, tmp, w);\ +\ + for (; x < w2; x++) {\ + b[2*x ] = tmp[x];\ + b[2*x+1] = COMPOSE_HAARiH0(b[x+w2], tmp[x]);\ + }\ +}\ +static void horizontal_compose_haar1i##ext(IDWTELEM *b, IDWTELEM *tmp, int w)\ +{\ + int w2= w>>1;\ + int x= w2 - (w2&(align-1));\ + ff_horizontal_compose_haar1i##ext(b, tmp, w);\ +\ + for (; x < w2; x++) {\ + b[2*x ] = (tmp[x] + 1)>>1;\ + b[2*x+1] = (COMPOSE_HAARiH0(b[x+w2], tmp[x]) + 1)>>1;\ + }\ +}\ +\ + +#if HAVE_YASM +#if !ARCH_X86_64 +COMPOSE_VERTICAL(_mmx, 4) +#endif +COMPOSE_VERTICAL(_sse2, 8) + + +void ff_horizontal_compose_dd97i_ssse3(IDWTELEM *b, IDWTELEM *tmp, int w); + +static void horizontal_compose_dd97i_ssse3(IDWTELEM *b, IDWTELEM *tmp, int w) +{ + int w2= w>>1; + int x= w2 - (w2&7); + ff_horizontal_compose_dd97i_ssse3(b, tmp, w); + + for (; x < w2; x++) { + b[2*x ] = (tmp[x] + 1)>>1; + b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1; + } +} +#endif + +void ff_spatial_idwt_init_mmx(DWTContext *d, enum dwt_type type) +{ +#if HAVE_YASM + int mm_flags = av_get_cpu_flags(); + +#if !ARCH_X86_64 + if (!(mm_flags & AV_CPU_FLAG_MMX)) + return; + + switch (type) { + case DWT_DIRAC_DD9_7: + d->vertical_compose_l0 = (void*)vertical_compose53iL0_mmx; + d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0_mmx; + break; + case DWT_DIRAC_LEGALL5_3: + d->vertical_compose_l0 = (void*)vertical_compose53iL0_mmx; + d->vertical_compose_h0 = (void*)vertical_compose_dirac53iH0_mmx; + break; + case DWT_DIRAC_DD13_7: + d->vertical_compose_l0 = (void*)vertical_compose_dd137iL0_mmx; + d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0_mmx; + break; + case DWT_DIRAC_HAAR0: + d->vertical_compose = (void*)vertical_compose_haar_mmx; + d->horizontal_compose = horizontal_compose_haar0i_mmx; + break; + case DWT_DIRAC_HAAR1: + d->vertical_compose = (void*)vertical_compose_haar_mmx; + d->horizontal_compose = horizontal_compose_haar1i_mmx; + break; + } +#endif + + if (!(mm_flags & AV_CPU_FLAG_SSE2)) + return; + + switch (type) { + case DWT_DIRAC_DD9_7: + d->vertical_compose_l0 = (void*)vertical_compose53iL0_sse2; + d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0_sse2; + break; + case DWT_DIRAC_LEGALL5_3: + d->vertical_compose_l0 = (void*)vertical_compose53iL0_sse2; + d->vertical_compose_h0 = (void*)vertical_compose_dirac53iH0_sse2; + break; + case DWT_DIRAC_DD13_7: + d->vertical_compose_l0 = (void*)vertical_compose_dd137iL0_sse2; + d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0_sse2; + break; + case DWT_DIRAC_HAAR0: + d->vertical_compose = (void*)vertical_compose_haar_sse2; + d->horizontal_compose = horizontal_compose_haar0i_sse2; + break; + case DWT_DIRAC_HAAR1: + d->vertical_compose = (void*)vertical_compose_haar_sse2; + d->horizontal_compose = horizontal_compose_haar1i_sse2; + break; + } + + if (!(mm_flags & AV_CPU_FLAG_SSSE3)) + return; + + switch (type) { + case DWT_DIRAC_DD9_7: + d->horizontal_compose = horizontal_compose_dd97i_ssse3; + break; + } +#endif // HAVE_YASM +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.h new file mode 100644 index 000000000..126b29029 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dirac_dwt.h @@ -0,0 +1,30 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_X86_DIRAC_DWT_H +#define AVCODEC_X86_DIRAC_DWT_H + +#include "libavcodec/dirac_dwt.h" + +void ff_horizontal_compose_dd97i_end_c(IDWTELEM *b, IDWTELEM *tmp, int w2, int x); +void ff_horizontal_compose_haar1i_end_c(IDWTELEM *b, IDWTELEM *tmp, int w2, int x); +void ff_horizontal_compose_haar0i_end_c(IDWTELEM *b, IDWTELEM *tmp, int w2, int x); + +void ff_spatial_idwt_init_mmx(DWTContext *d, enum dwt_type type); + +#endif diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/diracdsp_yasm.asm b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/diracdsp_yasm.asm new file mode 100644 index 000000000..3e9765b42 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/diracdsp_yasm.asm @@ -0,0 +1,264 @@ +;****************************************************************************** +;* Copyright (c) 2010 David Conrad +;* +;* This file is part of FFmpeg. +;* +;* FFmpeg is free software; you can redistribute it and/or +;* modify it under the terms of the GNU Lesser General Public +;* License as published by the Free Software Foundation; either +;* version 2.1 of the License, or (at your option) any later version. +;* +;* FFmpeg is distributed in the hope that it will be useful, +;* but WITHOUT ANY WARRANTY; without even the implied warranty of +;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;* Lesser General Public License for more details. +;* +;* You should have received a copy of the GNU Lesser General Public +;* License along with FFmpeg; if not, write to the Free Software +;* 51, Inc., Foundation Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +;****************************************************************************** + +%include "libavutil/x86/x86util.asm" + +SECTION_RODATA +pw_3: times 8 dw 3 +pw_7: times 8 dw 7 +pw_16: times 8 dw 16 +pw_32: times 8 dw 32 +pb_128: times 16 db 128 + +section .text + +%macro UNPACK_ADD 6 + mov%5 %1, %3 + mov%6 m5, %4 + mova m4, %1 + mova %2, m5 + punpcklbw %1, m7 + punpcklbw m5, m7 + punpckhbw m4, m7 + punpckhbw %2, m7 + paddw %1, m5 + paddw %2, m4 +%endmacro + +%macro HPEL_FILTER 1 +; dirac_hpel_filter_v_sse2(uint8_t *dst, uint8_t *src, int stride, int width); +cglobal dirac_hpel_filter_v_%1, 4,6,8, dst, src, stride, width, src0, stridex3 + mov src0q, srcq + lea stridex3q, [3*strideq] + sub src0q, stridex3q + pxor m7, m7 +.loop: + ; 7*(src[0] + src[1]) + UNPACK_ADD m0, m1, [srcq], [srcq + strideq], a,a + pmullw m0, [pw_7] + pmullw m1, [pw_7] + + ; 3*( ... + src[-2] + src[3]) + UNPACK_ADD m2, m3, [src0q + strideq], [srcq + stridex3q], a,a + paddw m0, m2 + paddw m1, m3 + pmullw m0, [pw_3] + pmullw m1, [pw_3] + + ; ... - 7*(src[-1] + src[2]) + UNPACK_ADD m2, m3, [src0q + strideq*2], [srcq + strideq*2], a,a + pmullw m2, [pw_7] + pmullw m3, [pw_7] + psubw m0, m2 + psubw m1, m3 + + ; ... - (src[-3] + src[4]) + UNPACK_ADD m2, m3, [src0q], [srcq + strideq*4], a,a + psubw m0, m2 + psubw m1, m3 + + paddw m0, [pw_16] + paddw m1, [pw_16] + psraw m0, 5 + psraw m1, 5 + packuswb m0, m1 + mova [dstq], m0 + add dstq, mmsize + add srcq, mmsize + add src0q, mmsize + sub widthd, mmsize + jg .loop + RET + +; dirac_hpel_filter_h_sse2(uint8_t *dst, uint8_t *src, int width); +cglobal dirac_hpel_filter_h_%1, 3,3,8, dst, src, width + dec widthd + pxor m7, m7 + and widthd, ~(mmsize-1) +.loop: + ; 7*(src[0] + src[1]) + UNPACK_ADD m0, m1, [srcq + widthq], [srcq + widthq + 1], u,u + pmullw m0, [pw_7] + pmullw m1, [pw_7] + + ; 3*( ... + src[-2] + src[3]) + UNPACK_ADD m2, m3, [srcq + widthq - 2], [srcq + widthq + 3], u,u + paddw m0, m2 + paddw m1, m3 + pmullw m0, [pw_3] + pmullw m1, [pw_3] + + ; ... - 7*(src[-1] + src[2]) + UNPACK_ADD m2, m3, [srcq + widthq - 1], [srcq + widthq + 2], u,u + pmullw m2, [pw_7] + pmullw m3, [pw_7] + psubw m0, m2 + psubw m1, m3 + + ; ... - (src[-3] + src[4]) + UNPACK_ADD m2, m3, [srcq + widthq - 3], [srcq + widthq + 4], u,u + psubw m0, m2 + psubw m1, m3 + + paddw m0, [pw_16] + paddw m1, [pw_16] + psraw m0, 5 + psraw m1, 5 + packuswb m0, m1 + mova [dstq + widthq], m0 + sub widthd, mmsize + jge .loop + RET +%endmacro + +%macro PUT_RECT 1 +; void put_rect_clamped(uint8_t *dst, int dst_stride, int16_t *src, int src_stride, int width, int height) +cglobal put_signed_rect_clamped_%1, 5,9,3, dst, dst_stride, src, src_stride, w, dst2, src2 + mova m0, [pb_128] + add wd, (mmsize-1) + and wd, ~(mmsize-1) + +%if ARCH_X86_64 + movsxd dst_strideq, dst_strided + movsxd src_strideq, src_strided + mov r7d, r5m + mov r8d, wd + %define wspill r8d + %define hd r7d +%else + mov r4m, wd + %define wspill r4m + %define hd r5mp +%endif + +.loopy + lea src2q, [srcq+src_strideq*2] + lea dst2q, [dstq+dst_strideq] +.loopx: + sub wd, mmsize + mova m1, [srcq +2*wq] + mova m2, [src2q+2*wq] + packsswb m1, [srcq +2*wq+mmsize] + packsswb m2, [src2q+2*wq+mmsize] + paddb m1, m0 + paddb m2, m0 + mova [dstq +wq], m1 + mova [dst2q+wq], m2 + jg .loopx + + lea srcq, [srcq+src_strideq*4] + lea dstq, [dstq+dst_strideq*2] + sub hd, 2 + mov wd, wspill + jg .loopy + RET +%endm + +%macro ADD_RECT 1 +; void add_rect_clamped(uint8_t *dst, uint16_t *src, int stride, int16_t *idwt, int idwt_stride, int width, int height) +cglobal add_rect_clamped_%1, 7,9,3, dst, src, stride, idwt, idwt_stride, w, h + mova m0, [pw_32] + add wd, (mmsize-1) + and wd, ~(mmsize-1) + +%if ARCH_X86_64 + movsxd strideq, strided + movsxd idwt_strideq, idwt_strided + mov r8d, wd + %define wspill r8d +%else + mov r5m, wd + %define wspill r5m +%endif + +.loop: + sub wd, mmsize + movu m1, [srcq +2*wq] ; FIXME: ensure alignment + paddw m1, m0 + psraw m1, 6 + movu m2, [srcq +2*wq+mmsize] ; FIXME: ensure alignment + paddw m2, m0 + psraw m2, 6 + paddw m1, [idwtq+2*wq] + paddw m2, [idwtq+2*wq+mmsize] + packuswb m1, m2 + mova [dstq +wq], m1 + jg .loop + + lea srcq, [srcq + 2*strideq] + add dstq, strideq + lea idwtq, [idwtq+ 2*idwt_strideq] + sub hd, 1 + mov wd, wspill + jg .loop + RET +%endm + +%macro ADD_OBMC 2 +; void add_obmc(uint16_t *dst, uint8_t *src, int stride, uint8_t *obmc_weight, int yblen) +cglobal add_dirac_obmc%1_%2, 6,6,5, dst, src, stride, obmc, yblen + pxor m4, m4 +.loop: +%assign i 0 +%rep %1 / mmsize + mova m0, [srcq+i] + mova m1, m0 + punpcklbw m0, m4 + punpckhbw m1, m4 + mova m2, [obmcq+i] + mova m3, m2 + punpcklbw m2, m4 + punpckhbw m3, m4 + pmullw m0, m2 + pmullw m1, m3 + movu m2, [dstq+2*i] + movu m3, [dstq+2*i+mmsize] + paddw m0, m2 + paddw m1, m3 + movu [dstq+2*i], m0 + movu [dstq+2*i+mmsize], m1 +%assign i i+mmsize +%endrep + lea srcq, [srcq+strideq] + lea dstq, [dstq+2*strideq] + add obmcq, 32 + sub yblend, 1 + jg .loop + RET +%endm + +INIT_MMX +%if ARCH_X86_64 == 0 +PUT_RECT mmx +ADD_RECT mmx + +HPEL_FILTER mmx +ADD_OBMC 32, mmx +ADD_OBMC 16, mmx +%endif +ADD_OBMC 8, mmx + +INIT_XMM +PUT_RECT sse2 +ADD_RECT sse2 + +HPEL_FILTER sse2 +ADD_OBMC 32, sse2 +ADD_OBMC 16, sse2 diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_init.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_init.c new file mode 100644 index 000000000..df7918a23 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_init.c @@ -0,0 +1,734 @@ +/* + * Copyright (c) 2000, 2001 Fabrice Bellard + * Copyright (c) 2002-2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" +#include "libavutil/attributes.h" +#include "libavutil/cpu.h" +#include "libavutil/x86/asm.h" +#include "libavutil/x86/cpu.h" +#include "libavcodec/dsputil.h" +#include "libavcodec/simple_idct.h" +#include "dsputil_x86.h" +#include "idct_xvid.h" + +void ff_put_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, + int dstStride, int src1Stride, int h); +void ff_put_no_rnd_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, + uint8_t *src2, int dstStride, + int src1Stride, int h); +void ff_avg_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, + int dstStride, int src1Stride, int h); +void ff_put_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, + int dstStride, int src1Stride, int h); +void ff_avg_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, + int dstStride, int src1Stride, int h); +void ff_put_no_rnd_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, + int dstStride, int src1Stride, int h); +void ff_put_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride, int h); +void ff_avg_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride, int h); +void ff_put_no_rnd_mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride, + int h); +void ff_put_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride, int h); +void ff_avg_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride, int h); +void ff_put_no_rnd_mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride, + int h); +void ff_put_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride); +void ff_avg_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride); +void ff_put_no_rnd_mpeg4_qpel16_v_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride); +void ff_put_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride); +void ff_avg_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride); +void ff_put_no_rnd_mpeg4_qpel8_v_lowpass_mmxext(uint8_t *dst, uint8_t *src, + int dstStride, int srcStride); +#define ff_put_no_rnd_pixels16_mmxext ff_put_pixels16_mmxext +#define ff_put_no_rnd_pixels8_mmxext ff_put_pixels8_mmxext + +void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale); +void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale); + +int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2, + int order); +int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2, + int order); +int32_t ff_scalarproduct_and_madd_int16_mmxext(int16_t *v1, const int16_t *v2, + const int16_t *v3, + int order, int mul); +int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2, + const int16_t *v3, + int order, int mul); +int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2, + const int16_t *v3, + int order, int mul); + +void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input, + const int16_t *window, unsigned int len); +void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input, + const int16_t *window, unsigned int len); +void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input, + const int16_t *window, unsigned int len); +void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input, + const int16_t *window, unsigned int len); +void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input, + const int16_t *window, unsigned int len); +void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input, + const int16_t *window, unsigned int len); + +void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w); +void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w); + +void ff_add_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *top, + const uint8_t *diff, int w, + int *left, int *left_top); +int ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src, + int w, int left); +int ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src, + int w, int left); + +void ff_vector_clip_int32_mmx (int32_t *dst, const int32_t *src, + int32_t min, int32_t max, unsigned int len); +void ff_vector_clip_int32_sse2 (int32_t *dst, const int32_t *src, + int32_t min, int32_t max, unsigned int len); +void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src, + int32_t min, int32_t max, unsigned int len); +void ff_vector_clip_int32_sse4 (int32_t *dst, const int32_t *src, + int32_t min, int32_t max, unsigned int len); + +#if HAVE_YASM + +PIXELS16(static, ff_avg, , , _mmxext) +PIXELS16(static, ff_put, , , _mmxext) + +#define QPEL_OP(OPNAME, RND, MMX) \ +static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + ff_ ## OPNAME ## pixels8_ ## MMX(dst, src, stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[8]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, \ + stride, 8); \ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half, \ + stride, stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + ff_ ## OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride, \ + stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[8]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, \ + stride, 8); \ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + 1, half, stride, \ + stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[8]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, \ + 8, stride); \ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src, half, \ + stride, stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src, \ + stride, stride); \ +} \ + \ +static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[8]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, \ + 8, stride); \ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, src + stride, half, stride,\ + stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half) + 64; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, \ + stride, 9); \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, \ + stride, 8, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half) + 64; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8, \ + stride, 9); \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, \ + stride, 8, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half) + 64; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, \ + stride, 9); \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV, \ + stride, 8, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half) + 64; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8, \ + stride, 9); \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV, \ + stride, 8, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half) + 64; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, \ + stride, 8, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half) + 64; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\ + ff_ ## OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV, \ + stride, 8, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, \ + 8, stride, 9); \ + ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, \ + stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[8 + 9]; \ + uint8_t * const halfH = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8, \ + stride, 9); \ + ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, \ + stride, 8); \ +} \ + \ +static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[9]; \ + uint8_t * const halfH = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, \ + stride, 9); \ + ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, \ + stride, 8); \ +} \ + \ +static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + ff_ ## OPNAME ## pixels16_ ## MMX(dst, src, stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[32]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, \ + stride, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, \ + stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + ff_ ## OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src, \ + stride, stride, 16);\ +} \ + \ +static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[32]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, \ + stride, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src + 1, half, \ + stride, stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[32]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, \ + stride); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, \ + stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src, \ + stride, stride); \ +} \ + \ +static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t temp[32]; \ + uint8_t * const half = (uint8_t*)temp; \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, \ + stride); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half, \ + stride, stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[16 * 2 + 17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half) + 256; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, \ + stride, 17); \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, \ + 16, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, \ + stride, 16, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[16 * 2 + 17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half) + 256; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16, \ + stride, 17); \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, \ + 16, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, \ + stride, 16, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[16 * 2 + 17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half) + 256; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, \ + stride, 17); \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, \ + 16, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV, \ + stride, 16, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[16 * 2 + 17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half) + 256; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16, \ + stride, 17); \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, \ + 16, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV, \ + stride, 16, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[16 * 2 + 17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half) + 256; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, \ + 16, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, \ + stride, 16, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[16 * 2 + 17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half) + 256; \ + uint8_t * const halfHV = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, \ + 16, 16); \ + ff_ ## OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV, \ + stride, 16, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, \ + stride, 17); \ + ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, \ + stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16, \ + stride, 17); \ + ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, \ + stride, 16); \ +} \ + \ +static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src, \ + ptrdiff_t stride) \ +{ \ + uint64_t half[17 * 2]; \ + uint8_t * const halfH = ((uint8_t*)half); \ + ff_put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, \ + stride, 17); \ + ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, \ + stride, 16); \ +} + +QPEL_OP(put_, _, mmxext) +QPEL_OP(avg_, _, mmxext) +QPEL_OP(put_no_rnd_, _no_rnd_, mmxext) +#endif /* HAVE_YASM */ + +#define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX) \ + do { \ + c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \ + c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \ + } while (0) + +static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx, + int cpu_flags) +{ +#if HAVE_MMX_INLINE + const int high_bit_depth = avctx->bits_per_raw_sample > 8; + + c->put_pixels_clamped = ff_put_pixels_clamped_mmx; + c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx; + c->add_pixels_clamped = ff_add_pixels_clamped_mmx; + + if (!high_bit_depth) { + c->clear_block = ff_clear_block_mmx; + c->clear_blocks = ff_clear_blocks_mmx; + c->draw_edges = ff_draw_edges_mmx; + } + +#if CONFIG_VIDEODSP && (ARCH_X86_32 || !HAVE_YASM) + c->gmc = ff_gmc_mmx; +#endif + + c->add_bytes = ff_add_bytes_mmx; +#endif /* HAVE_MMX_INLINE */ + +#if HAVE_MMX_EXTERNAL + if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) { + c->h263_v_loop_filter = ff_h263_v_loop_filter_mmx; + c->h263_h_loop_filter = ff_h263_h_loop_filter_mmx; + } + + c->vector_clip_int32 = ff_vector_clip_int32_mmx; +#endif /* HAVE_MMX_EXTERNAL */ +} + +static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx, + int cpu_flags) +{ +#if HAVE_MMXEXT_EXTERNAL + SET_QPEL_FUNCS(avg_qpel, 0, 16, mmxext, ); + SET_QPEL_FUNCS(avg_qpel, 1, 8, mmxext, ); + + SET_QPEL_FUNCS(put_qpel, 0, 16, mmxext, ); + SET_QPEL_FUNCS(put_qpel, 1, 8, mmxext, ); + SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, ); + SET_QPEL_FUNCS(put_no_rnd_qpel, 1, 8, mmxext, ); + + /* slower than cmov version on AMD */ + if (!(cpu_flags & AV_CPU_FLAG_3DNOW)) + c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext; + + c->scalarproduct_int16 = ff_scalarproduct_int16_mmxext; + c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext; + + if (avctx->flags & CODEC_FLAG_BITEXACT) { + c->apply_window_int16 = ff_apply_window_int16_mmxext; + } else { + c->apply_window_int16 = ff_apply_window_int16_round_mmxext; + } +#endif /* HAVE_MMXEXT_EXTERNAL */ +} + +static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx, + int cpu_flags) +{ +#if HAVE_SSE_INLINE + const int high_bit_depth = avctx->bits_per_raw_sample > 8; + + if (!high_bit_depth) { + if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) { + /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */ + c->clear_block = ff_clear_block_sse; + c->clear_blocks = ff_clear_blocks_sse; + } + } + + c->vector_clipf = ff_vector_clipf_sse; +#endif /* HAVE_SSE_INLINE */ + +#if HAVE_YASM +#if HAVE_INLINE_ASM && CONFIG_VIDEODSP + c->gmc = ff_gmc_sse; +#endif +#endif /* HAVE_YASM */ +} + +static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx, + int cpu_flags) +{ +#if HAVE_SSE2_INLINE + const int high_bit_depth = avctx->bits_per_raw_sample > 8; + + if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX && avctx->lowres == 0) { + c->idct_put = ff_idct_xvid_sse2_put; + c->idct_add = ff_idct_xvid_sse2_add; + c->idct = ff_idct_xvid_sse2; + c->idct_permutation_type = FF_SSE2_IDCT_PERM; + } +#endif /* HAVE_SSE2_INLINE */ + +#if HAVE_SSE2_EXTERNAL + c->scalarproduct_int16 = ff_scalarproduct_int16_sse2; + c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2; + if (cpu_flags & AV_CPU_FLAG_ATOM) { + c->vector_clip_int32 = ff_vector_clip_int32_int_sse2; + } else { + c->vector_clip_int32 = ff_vector_clip_int32_sse2; + } + if (avctx->flags & CODEC_FLAG_BITEXACT) { + c->apply_window_int16 = ff_apply_window_int16_sse2; + } else if (!(cpu_flags & AV_CPU_FLAG_SSE2SLOW)) { + c->apply_window_int16 = ff_apply_window_int16_round_sse2; + } + c->bswap_buf = ff_bswap32_buf_sse2; +#endif /* HAVE_SSE2_EXTERNAL */ +} + +static av_cold void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx, + int cpu_flags) +{ +#if HAVE_SSSE3_EXTERNAL + c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3; + if (cpu_flags & AV_CPU_FLAG_SSE4) // not really SSE4, just slow on Conroe + c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4; + + if (cpu_flags & AV_CPU_FLAG_ATOM) + c->apply_window_int16 = ff_apply_window_int16_ssse3_atom; + else + c->apply_window_int16 = ff_apply_window_int16_ssse3; + if (!(cpu_flags & (AV_CPU_FLAG_SSE42 | AV_CPU_FLAG_3DNOW))) // cachesplit + c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3; + c->bswap_buf = ff_bswap32_buf_ssse3; +#endif /* HAVE_SSSE3_EXTERNAL */ +} + +static av_cold void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx, + int cpu_flags) +{ +#if HAVE_SSE4_EXTERNAL + c->vector_clip_int32 = ff_vector_clip_int32_sse4; +#endif /* HAVE_SSE4_EXTERNAL */ +} + +av_cold void ff_dsputil_init_x86(DSPContext *c, AVCodecContext *avctx) +{ + int cpu_flags = av_get_cpu_flags(); + +#if HAVE_7REGS && HAVE_INLINE_ASM + if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_CMOV) + c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_cmov; +#endif + + if (X86_MMX(cpu_flags)) { +#if HAVE_INLINE_ASM + const int idct_algo = avctx->idct_algo; + + if (avctx->lowres == 0 && avctx->bits_per_raw_sample <= 8) { + if (idct_algo == FF_IDCT_AUTO || idct_algo == FF_IDCT_SIMPLEMMX) { + c->idct_put = ff_simple_idct_put_mmx; + c->idct_add = ff_simple_idct_add_mmx; + c->idct = ff_simple_idct_mmx; + c->idct_permutation_type = FF_SIMPLE_IDCT_PERM; + } else if (idct_algo == FF_IDCT_XVIDMMX) { + if (cpu_flags & AV_CPU_FLAG_SSE2) { + c->idct_put = ff_idct_xvid_sse2_put; + c->idct_add = ff_idct_xvid_sse2_add; + c->idct = ff_idct_xvid_sse2; + c->idct_permutation_type = FF_SSE2_IDCT_PERM; + } else if (cpu_flags & AV_CPU_FLAG_MMXEXT) { + c->idct_put = ff_idct_xvid_mmxext_put; + c->idct_add = ff_idct_xvid_mmxext_add; + c->idct = ff_idct_xvid_mmxext; + } else { + c->idct_put = ff_idct_xvid_mmx_put; + c->idct_add = ff_idct_xvid_mmx_add; + c->idct = ff_idct_xvid_mmx; + } + } + } +#endif /* HAVE_INLINE_ASM */ + + dsputil_init_mmx(c, avctx, cpu_flags); + } + + if (X86_MMXEXT(cpu_flags)) + dsputil_init_mmxext(c, avctx, cpu_flags); + + if (X86_SSE(cpu_flags)) + dsputil_init_sse(c, avctx, cpu_flags); + + if (X86_SSE2(cpu_flags)) + dsputil_init_sse2(c, avctx, cpu_flags); + + if (EXTERNAL_SSSE3(cpu_flags)) + dsputil_init_ssse3(c, avctx, cpu_flags); + + if (EXTERNAL_SSE4(cpu_flags)) + dsputil_init_sse4(c, avctx, cpu_flags); + + if (CONFIG_ENCODERS) + ff_dsputilenc_init_mmx(c, avctx); +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_mmx.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_mmx.c new file mode 100644 index 000000000..805bacd44 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavcodec/x86/dsputil_mmx.c @@ -0,0 +1,637 @@ +/* + * MMX optimized DSP utils + * Copyright (c) 2000, 2001 Fabrice Bellard + * Copyright (c) 2002-2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * MMX optimization by Nick Kurshev + */ + +#include "config.h" +#include "libavutil/avassert.h" +#include "libavutil/cpu.h" +#include "libavutil/x86/asm.h" +#include "libavcodec/videodsp.h" +#include "constants.h" +#include "dsputil_x86.h" +#include "diracdsp_mmx.h" + +#if HAVE_INLINE_ASM + +void ff_put_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels, + int line_size) +{ + const int16_t *p; + uint8_t *pix; + + /* read the pixels */ + p = block; + pix = pixels; + /* unrolled loop */ + __asm__ volatile ( + "movq (%3), %%mm0 \n\t" + "movq 8(%3), %%mm1 \n\t" + "movq 16(%3), %%mm2 \n\t" + "movq 24(%3), %%mm3 \n\t" + "movq 32(%3), %%mm4 \n\t" + "movq 40(%3), %%mm5 \n\t" + "movq 48(%3), %%mm6 \n\t" + "movq 56(%3), %%mm7 \n\t" + "packuswb %%mm1, %%mm0 \n\t" + "packuswb %%mm3, %%mm2 \n\t" + "packuswb %%mm5, %%mm4 \n\t" + "packuswb %%mm7, %%mm6 \n\t" + "movq %%mm0, (%0) \n\t" + "movq %%mm2, (%0, %1) \n\t" + "movq %%mm4, (%0, %1, 2) \n\t" + "movq %%mm6, (%0, %2) \n\t" + :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3), + "r"(p) + : "memory"); + pix += line_size * 4; + p += 32; + + // if here would be an exact copy of the code above + // compiler would generate some very strange code + // thus using "r" + __asm__ volatile ( + "movq (%3), %%mm0 \n\t" + "movq 8(%3), %%mm1 \n\t" + "movq 16(%3), %%mm2 \n\t" + "movq 24(%3), %%mm3 \n\t" + "movq 32(%3), %%mm4 \n\t" + "movq 40(%3), %%mm5 \n\t" + "movq 48(%3), %%mm6 \n\t" + "movq 56(%3), %%mm7 \n\t" + "packuswb %%mm1, %%mm0 \n\t" + "packuswb %%mm3, %%mm2 \n\t" + "packuswb %%mm5, %%mm4 \n\t" + "packuswb %%mm7, %%mm6 \n\t" + "movq %%mm0, (%0) \n\t" + "movq %%mm2, (%0, %1) \n\t" + "movq %%mm4, (%0, %1, 2) \n\t" + "movq %%mm6, (%0, %2) \n\t" + :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3), "r"(p) + : "memory"); +} + +#define put_signed_pixels_clamped_mmx_half(off) \ + "movq "#off"(%2), %%mm1 \n\t" \ + "movq 16 + "#off"(%2), %%mm2 \n\t" \ + "movq 32 + "#off"(%2), %%mm3 \n\t" \ + "movq 48 + "#off"(%2), %%mm4 \n\t" \ + "packsswb 8 + "#off"(%2), %%mm1 \n\t" \ + "packsswb 24 + "#off"(%2), %%mm2 \n\t" \ + "packsswb 40 + "#off"(%2), %%mm3 \n\t" \ + "packsswb 56 + "#off"(%2), %%mm4 \n\t" \ + "paddb %%mm0, %%mm1 \n\t" \ + "paddb %%mm0, %%mm2 \n\t" \ + "paddb %%mm0, %%mm3 \n\t" \ + "paddb %%mm0, %%mm4 \n\t" \ + "movq %%mm1, (%0) \n\t" \ + "movq %%mm2, (%0, %3) \n\t" \ + "movq %%mm3, (%0, %3, 2) \n\t" \ + "movq %%mm4, (%0, %1) \n\t" + +void ff_put_signed_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels, + int line_size) +{ + x86_reg line_skip = line_size; + x86_reg line_skip3; + + __asm__ volatile ( + "movq "MANGLE(ff_pb_80)", %%mm0 \n\t" + "lea (%3, %3, 2), %1 \n\t" + put_signed_pixels_clamped_mmx_half(0) + "lea (%0, %3, 4), %0 \n\t" + put_signed_pixels_clamped_mmx_half(64) + : "+&r"(pixels), "=&r"(line_skip3) + : "r"(block), "r"(line_skip) + : "memory"); +} + +void ff_add_pixels_clamped_mmx(const int16_t *block, uint8_t *pixels, + int line_size) +{ + const int16_t *p; + uint8_t *pix; + int i; + + /* read the pixels */ + p = block; + pix = pixels; + MOVQ_ZERO(mm7); + i = 4; + do { + __asm__ volatile ( + "movq (%2), %%mm0 \n\t" + "movq 8(%2), %%mm1 \n\t" + "movq 16(%2), %%mm2 \n\t" + "movq 24(%2), %%mm3 \n\t" + "movq %0, %%mm4 \n\t" + "movq %1, %%mm6 \n\t" + "movq %%mm4, %%mm5 \n\t" + "punpcklbw %%mm7, %%mm4 \n\t" + "punpckhbw %%mm7, %%mm5 \n\t" + "paddsw %%mm4, %%mm0 \n\t" + "paddsw %%mm5, %%mm1 \n\t" + "movq %%mm6, %%mm5 \n\t" + "punpcklbw %%mm7, %%mm6 \n\t" + "punpckhbw %%mm7, %%mm5 \n\t" + "paddsw %%mm6, %%mm2 \n\t" + "paddsw %%mm5, %%mm3 \n\t" + "packuswb %%mm1, %%mm0 \n\t" + "packuswb %%mm3, %%mm2 \n\t" + "movq %%mm0, %0 \n\t" + "movq %%mm2, %1 \n\t" + : "+m"(*pix), "+m"(*(pix + line_size)) + : "r"(p) + : "memory"); + pix += line_size * 2; + p += 16; + } while (--i); +} + +#define CLEAR_BLOCKS(name, n) \ +void name(int16_t *blocks) \ +{ \ + __asm__ volatile ( \ + "pxor %%mm7, %%mm7 \n\t" \ + "mov %1, %%"REG_a" \n\t" \ + "1: \n\t" \ + "movq %%mm7, (%0, %%"REG_a") \n\t" \ + "movq %%mm7, 8(%0, %%"REG_a") \n\t" \ + "movq %%mm7, 16(%0, %%"REG_a") \n\t" \ + "movq %%mm7, 24(%0, %%"REG_a") \n\t" \ + "add $32, %%"REG_a" \n\t" \ + "js 1b \n\t" \ + :: "r"(((uint8_t *)blocks) + 128 * n), \ + "i"(-128 * n) \ + : "%"REG_a \ + ); \ +} +CLEAR_BLOCKS(ff_clear_blocks_mmx, 6) +CLEAR_BLOCKS(ff_clear_block_mmx, 1) + +void ff_clear_block_sse(int16_t *block) +{ + __asm__ volatile ( + "xorps %%xmm0, %%xmm0 \n" + "movaps %%xmm0, (%0) \n" + "movaps %%xmm0, 16(%0) \n" + "movaps %%xmm0, 32(%0) \n" + "movaps %%xmm0, 48(%0) \n" + "movaps %%xmm0, 64(%0) \n" + "movaps %%xmm0, 80(%0) \n" + "movaps %%xmm0, 96(%0) \n" + "movaps %%xmm0, 112(%0) \n" + :: "r"(block) + : "memory" + ); +} + +void ff_clear_blocks_sse(int16_t *blocks) +{ + __asm__ volatile ( + "xorps %%xmm0, %%xmm0 \n" + "mov %1, %%"REG_a" \n" + "1: \n" + "movaps %%xmm0, (%0, %%"REG_a") \n" + "movaps %%xmm0, 16(%0, %%"REG_a") \n" + "movaps %%xmm0, 32(%0, %%"REG_a") \n" + "movaps %%xmm0, 48(%0, %%"REG_a") \n" + "movaps %%xmm0, 64(%0, %%"REG_a") \n" + "movaps %%xmm0, 80(%0, %%"REG_a") \n" + "movaps %%xmm0, 96(%0, %%"REG_a") \n" + "movaps %%xmm0, 112(%0, %%"REG_a") \n" + "add $128, %%"REG_a" \n" + "js 1b \n" + :: "r"(((uint8_t *)blocks) + 128 * 6), + "i"(-128 * 6) + : "%"REG_a + ); +} + +void ff_add_bytes_mmx(uint8_t *dst, uint8_t *src, int w) +{ + x86_reg i = 0; + __asm__ volatile ( + "jmp 2f \n\t" + "1: \n\t" + "movq (%1, %0), %%mm0 \n\t" + "movq (%2, %0), %%mm1 \n\t" + "paddb %%mm0, %%mm1 \n\t" + "movq %%mm1, (%2, %0) \n\t" + "movq 8(%1, %0), %%mm0 \n\t" + "movq 8(%2, %0), %%mm1 \n\t" + "paddb %%mm0, %%mm1 \n\t" + "movq %%mm1, 8(%2, %0) \n\t" + "add $16, %0 \n\t" + "2: \n\t" + "cmp %3, %0 \n\t" + "js 1b \n\t" + : "+r"(i) + : "r"(src), "r"(dst), "r"((x86_reg)w - 15) + ); + for ( ; i < w; i++) + dst[i + 0] += src[i + 0]; +} + +/* Draw the edges of width 'w' of an image of size width, height + * this MMX version can only handle w == 8 || w == 16. */ +void ff_draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, + int w, int h, int sides) +{ + uint8_t *ptr, *last_line; + int i; + + last_line = buf + (height - 1) * wrap; + /* left and right */ + ptr = buf; + if (w == 8) { + __asm__ volatile ( + "1: \n\t" + "movd (%0), %%mm0 \n\t" + "punpcklbw %%mm0, %%mm0 \n\t" + "punpcklwd %%mm0, %%mm0 \n\t" + "punpckldq %%mm0, %%mm0 \n\t" + "movq %%mm0, -8(%0) \n\t" + "movq -8(%0, %2), %%mm1 \n\t" + "punpckhbw %%mm1, %%mm1 \n\t" + "punpckhwd %%mm1, %%mm1 \n\t" + "punpckhdq %%mm1, %%mm1 \n\t" + "movq %%mm1, (%0, %2) \n\t" + "add %1, %0 \n\t" + "cmp %3, %0 \n\t" + "jb 1b \n\t" + : "+r"(ptr) + : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height) + ); + } else if(w==16){ + __asm__ volatile ( + "1: \n\t" + "movd (%0), %%mm0 \n\t" + "punpcklbw %%mm0, %%mm0 \n\t" + "punpcklwd %%mm0, %%mm0 \n\t" + "punpckldq %%mm0, %%mm0 \n\t" + "movq %%mm0, -8(%0) \n\t" + "movq %%mm0, -16(%0) \n\t" + "movq -8(%0, %2), %%mm1 \n\t" + "punpckhbw %%mm1, %%mm1 \n\t" + "punpckhwd %%mm1, %%mm1 \n\t" + "punpckhdq %%mm1, %%mm1 \n\t" + "movq %%mm1, (%0, %2) \n\t" + "movq %%mm1, 8(%0, %2) \n\t" + "add %1, %0 \n\t" + "cmp %3, %0 \n\t" + "jb 1b \n\t" + : "+r"(ptr) + : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height) + ); + } else { + av_assert1(w == 4); + __asm__ volatile ( + "1: \n\t" + "movd (%0), %%mm0 \n\t" + "punpcklbw %%mm0, %%mm0 \n\t" + "punpcklwd %%mm0, %%mm0 \n\t" + "movd %%mm0, -4(%0) \n\t" + "movd -4(%0, %2), %%mm1 \n\t" + "punpcklbw %%mm1, %%mm1 \n\t" + "punpckhwd %%mm1, %%mm1 \n\t" + "punpckhdq %%mm1, %%mm1 \n\t" + "movd %%mm1, (%0, %2) \n\t" + "add %1, %0 \n\t" + "cmp %3, %0 \n\t" + "jb 1b \n\t" + : "+r"(ptr) + : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height) + ); + } + + /* top and bottom (and hopefully also the corners) */ + if (sides & EDGE_TOP) { + for (i = 0; i < h; i += 4) { + ptr = buf - (i + 1) * wrap - w; + __asm__ volatile ( + "1: \n\t" + "movq (%1, %0), %%mm0 \n\t" + "movq %%mm0, (%0) \n\t" + "movq %%mm0, (%0, %2) \n\t" + "movq %%mm0, (%0, %2, 2) \n\t" + "movq %%mm0, (%0, %3) \n\t" + "add $8, %0 \n\t" + "cmp %4, %0 \n\t" + "jb 1b \n\t" + : "+r"(ptr) + : "r"((x86_reg)buf - (x86_reg)ptr - w), "r"((x86_reg) -wrap), + "r"((x86_reg) -wrap * 3), "r"(ptr + width + 2 * w) + ); + } + } + + if (sides & EDGE_BOTTOM) { + for (i = 0; i < h; i += 4) { + ptr = last_line + (i + 1) * wrap - w; + __asm__ volatile ( + "1: \n\t" + "movq (%1, %0), %%mm0 \n\t" + "movq %%mm0, (%0) \n\t" + "movq %%mm0, (%0, %2) \n\t" + "movq %%mm0, (%0, %2, 2) \n\t" + "movq %%mm0, (%0, %3) \n\t" + "add $8, %0 \n\t" + "cmp %4, %0 \n\t" + "jb 1b \n\t" + : "+r"(ptr) + : "r"((x86_reg)last_line - (x86_reg)ptr - w), + "r"((x86_reg)wrap), "r"((x86_reg)wrap * 3), + "r"(ptr + width + 2 * w) + ); + } + } +} + +typedef void emulated_edge_mc_func(uint8_t *dst, ptrdiff_t dst_stride, + const uint8_t *src, ptrdiff_t src_linesize, + int block_w, int block_h, + int src_x, int src_y, int w, int h); + +static av_always_inline void gmc(uint8_t *dst, uint8_t *src, + int stride, int h, int ox, int oy, + int dxx, int dxy, int dyx, int dyy, + int shift, int r, int width, int height, + emulated_edge_mc_func *emu_edge_fn) +{ + const int w = 8; + const int ix = ox >> (16 + shift); + const int iy = oy >> (16 + shift); + const int oxs = ox >> 4; + const int oys = oy >> 4; + const int dxxs = dxx >> 4; + const int dxys = dxy >> 4; + const int dyxs = dyx >> 4; + const int dyys = dyy >> 4; + const uint16_t r4[4] = { r, r, r, r }; + const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys }; + const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys }; + const uint64_t shift2 = 2 * shift; +#define MAX_STRIDE 4096U +#define MAX_H 8U + uint8_t edge_buf[(MAX_H + 1) * MAX_STRIDE]; + int x, y; + + const int dxw = (dxx - (1 << (16 + shift))) * (w - 1); + const int dyh = (dyy - (1 << (16 + shift))) * (h - 1); + const int dxh = dxy * (h - 1); + const int dyw = dyx * (w - 1); + int need_emu = (unsigned)ix >= width - w || + (unsigned)iy >= height - h; + + if ( // non-constant fullpel offset (3% of blocks) + ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) | + (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift) + // uses more than 16 bits of subpel mv (only at huge resolution) + || (dxx | dxy | dyx | dyy) & 15 + || (need_emu && (h > MAX_H || stride > MAX_STRIDE))) { + // FIXME could still use mmx for some of the rows + ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, + shift, r, width, height); + return; + } + + src += ix + iy * stride; + if (need_emu) { + emu_edge_fn(edge_buf, stride, src, stride, w + 1, h + 1, ix, iy, width, height); + src = edge_buf; + } + + __asm__ volatile ( + "movd %0, %%mm6 \n\t" + "pxor %%mm7, %%mm7 \n\t" + "punpcklwd %%mm6, %%mm6 \n\t" + "punpcklwd %%mm6, %%mm6 \n\t" + :: "r"(1< + +void ff_idct_xvid_mmx(short *block); +void ff_idct_xvid_mmx_put(uint8_t *dest, int line_size, int16_t *block); +void ff_idct_xvid_mmx_add(uint8_t *dest, int line_size, int16_t *block); + +void ff_idct_xvid_mmxext(short *block); +void ff_idct_xvid_mmxext_put(uint8_t *dest, int line_size, int16_t *block); +void ff_idct_xvid_mmxext_add(uint8_t *dest, int line_size, int16_t *block); + +void ff_idct_xvid_sse2(short *block); +void ff_idct_xvid_sse2_put(uint8_t *dest, int line_size, short *block); +void ff_idct_xvid_sse2_add(uint8_t *dest, int line_size, short *block); + +#endif /* AVCODEC_X86_IDCT_XVID_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.c new file mode 100644 index 000000000..ab9373671 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.c @@ -0,0 +1,241 @@ +/* + * APE tag handling + * Copyright (c) 2007 Benjamin Zores + * based upon libdemac from Dave Chapman. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/intreadwrite.h" +#include "libavutil/dict.h" +#include "avformat.h" +#include "avio_internal.h" +#include "apetag.h" +#include "internal.h" + +#define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31) +#define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30) +#define APE_TAG_FLAG_IS_HEADER (1 << 29) +#define APE_TAG_FLAG_IS_BINARY (1 << 1) + +static int ape_tag_read_field(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + uint8_t key[1024], *value; + uint32_t size, flags; + int i, c; + + size = avio_rl32(pb); /* field size */ + flags = avio_rl32(pb); /* field flags */ + for (i = 0; i < sizeof(key) - 1; i++) { + c = avio_r8(pb); + if (c < 0x20 || c > 0x7E) + break; + else + key[i] = c; + } + key[i] = 0; + if (c != 0) { + av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key); + return -1; + } + if (size >= UINT_MAX) + return -1; + if (flags & APE_TAG_FLAG_IS_BINARY) { + uint8_t filename[1024]; + enum AVCodecID id; + AVStream *st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + size -= avio_get_str(pb, size, filename, sizeof(filename)); + if (size <= 0) { + av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key); + return 0; + } + + av_dict_set(&st->metadata, key, filename, 0); + + if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) { + AVPacket pkt; + int ret; + + ret = av_get_packet(s->pb, &pkt, size); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Error reading cover art.\n"); + return ret; + } + + st->disposition |= AV_DISPOSITION_ATTACHED_PIC; + st->codec->codec_type = AVMEDIA_TYPE_VIDEO; + st->codec->codec_id = id; + + st->attached_pic = pkt; + st->attached_pic.stream_index = st->index; + st->attached_pic.flags |= AV_PKT_FLAG_KEY; + } else { + st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); + if (!st->codec->extradata) + return AVERROR(ENOMEM); + if (avio_read(pb, st->codec->extradata, size) != size) { + av_freep(&st->codec->extradata); + return AVERROR(EIO); + } + st->codec->extradata_size = size; + st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT; + } + } else { + value = av_malloc(size+1); + if (!value) + return AVERROR(ENOMEM); + c = avio_read(pb, value, size); + if (c < 0) { + av_free(value); + return c; + } + value[c] = 0; + av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL); + } + return 0; +} + +int64_t ff_ape_parse_tag(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + int64_t file_size = avio_size(pb); + uint32_t val, fields, tag_bytes; + uint8_t buf[8]; + int64_t tag_start; + int i; + + if (file_size < APE_TAG_FOOTER_BYTES) + return 0; + + avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET); + + avio_read(pb, buf, 8); /* APETAGEX */ + if (strncmp(buf, APE_TAG_PREAMBLE, 8)) { + return 0; + } + + val = avio_rl32(pb); /* APE tag version */ + if (val > APE_TAG_VERSION) { + av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION); + return 0; + } + + tag_bytes = avio_rl32(pb); /* tag size */ + if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) { + av_log(s, AV_LOG_ERROR, "Tag size is way too big\n"); + return 0; + } + + if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) { + av_log(s, AV_LOG_ERROR, "Invalid tag size %u.\n", tag_bytes); + return 0; + } + tag_start = file_size - tag_bytes - APE_TAG_FOOTER_BYTES; + + fields = avio_rl32(pb); /* number of fields */ + if (fields > 65536) { + av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields); + return 0; + } + + val = avio_rl32(pb); /* flags */ + if (val & APE_TAG_FLAG_IS_HEADER) { + av_log(s, AV_LOG_ERROR, "APE Tag is a header\n"); + return 0; + } + + avio_seek(pb, file_size - tag_bytes, SEEK_SET); + + for (i=0; i= 0x20 && *str <= 0x7e ) str++; + return !*str; +} + +int ff_ape_write_tag(AVFormatContext *s) +{ + AVDictionaryEntry *e = NULL; + int size, ret, count = 0; + AVIOContext *dyn_bc = NULL; + uint8_t *dyn_buf = NULL; + + if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0) + goto end; + + // flags + avio_wl32(dyn_bc, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER | + APE_TAG_FLAG_IS_HEADER); + ffio_fill(dyn_bc, 0, 8); // reserved + + while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) { + int val_len; + + if (!string_is_ascii(e->key)) { + av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n"); + continue; + } + + val_len = strlen(e->value); + avio_wl32(dyn_bc, val_len); // value length + avio_wl32(dyn_bc, 0); // item flags + avio_put_str(dyn_bc, e->key); // key + avio_write(dyn_bc, e->value, val_len); // value + count++; + } + if (!count) + goto end; + + size = avio_close_dyn_buf(dyn_bc, &dyn_buf); + if (size <= 0) + goto end; + size += 20; + + // header + avio_write(s->pb, "APETAGEX", 8); // id + avio_wl32(s->pb, APE_TAG_VERSION); // version + avio_wl32(s->pb, size); + avio_wl32(s->pb, count); + + avio_write(s->pb, dyn_buf, size - 20); + + // footer + avio_write(s->pb, "APETAGEX", 8); // id + avio_wl32(s->pb, APE_TAG_VERSION); // version + avio_wl32(s->pb, size); // size + avio_wl32(s->pb, count); // tag count + + // flags + avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER); + ffio_fill(s->pb, 0, 8); // reserved + +end: + if (dyn_bc && !dyn_buf) + avio_close_dyn_buf(dyn_bc, &dyn_buf); + av_freep(&dyn_buf); + + return ret; +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.h new file mode 100644 index 000000000..cf2a5f8ab --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/apetag.h @@ -0,0 +1,44 @@ +/* + * APE tag handling + * Copyright (c) 2007 Benjamin Zores + * based upon libdemac from Dave Chapman. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVFORMAT_APETAG_H +#define AVFORMAT_APETAG_H + +#include "avformat.h" + +#define APE_TAG_PREAMBLE "APETAGEX" +#define APE_TAG_VERSION 2000 +#define APE_TAG_FOOTER_BYTES 32 + +/** + * Read and parse an APE tag + * + * @return offset of the tag start in the file + */ +int64_t ff_ape_parse_tag(AVFormatContext *s); + +/** + * Write an APE tag into a file. + */ +int ff_ape_write_tag(AVFormatContext *s); + +#endif /* AVFORMAT_APETAG_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/img2.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/img2.c new file mode 100644 index 000000000..753935954 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/img2.c @@ -0,0 +1,102 @@ +/* + * Image format + * Copyright (c) 2000, 2001, 2002 Fabrice Bellard + * Copyright (c) 2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avstring.h" +#include "internal.h" + +typedef struct { + enum AVCodecID id; + const char *str; +} IdStrMap; + +static const IdStrMap img_tags[] = { + { AV_CODEC_ID_MJPEG, "jpeg" }, + { AV_CODEC_ID_MJPEG, "jpg" }, + { AV_CODEC_ID_MJPEG, "jps" }, + { AV_CODEC_ID_LJPEG, "ljpg" }, + { AV_CODEC_ID_JPEGLS, "jls" }, + { AV_CODEC_ID_PNG, "png" }, + { AV_CODEC_ID_PNG, "pns" }, + { AV_CODEC_ID_PNG, "mng" }, + { AV_CODEC_ID_PPM, "ppm" }, + { AV_CODEC_ID_PPM, "pnm" }, + { AV_CODEC_ID_PGM, "pgm" }, + { AV_CODEC_ID_PGMYUV, "pgmyuv" }, + { AV_CODEC_ID_PBM, "pbm" }, + { AV_CODEC_ID_PAM, "pam" }, + { AV_CODEC_ID_MPEG1VIDEO, "mpg1-img" }, + { AV_CODEC_ID_MPEG2VIDEO, "mpg2-img" }, + { AV_CODEC_ID_MPEG4, "mpg4-img" }, + { AV_CODEC_ID_FFV1, "ffv1-img" }, + { AV_CODEC_ID_RAWVIDEO, "y" }, + { AV_CODEC_ID_RAWVIDEO, "raw" }, + { AV_CODEC_ID_BMP, "bmp" }, + { AV_CODEC_ID_TARGA, "tga" }, + { AV_CODEC_ID_TIFF, "tiff" }, + { AV_CODEC_ID_TIFF, "tif" }, + { AV_CODEC_ID_SGI, "sgi" }, + { AV_CODEC_ID_PTX, "ptx" }, + { AV_CODEC_ID_PCX, "pcx" }, + { AV_CODEC_ID_BRENDER_PIX, "pix" }, + { AV_CODEC_ID_SUNRAST, "sun" }, + { AV_CODEC_ID_SUNRAST, "ras" }, + { AV_CODEC_ID_SUNRAST, "rs" }, + { AV_CODEC_ID_SUNRAST, "im1" }, + { AV_CODEC_ID_SUNRAST, "im8" }, + { AV_CODEC_ID_SUNRAST, "im24" }, + { AV_CODEC_ID_SUNRAST, "im32" }, + { AV_CODEC_ID_SUNRAST, "sunras" }, + { AV_CODEC_ID_JPEG2000, "j2c" }, + { AV_CODEC_ID_JPEG2000, "jp2" }, + { AV_CODEC_ID_JPEG2000, "jpc" }, + { AV_CODEC_ID_JPEG2000, "j2k" }, + { AV_CODEC_ID_DPX, "dpx" }, + { AV_CODEC_ID_EXR, "exr" }, + { AV_CODEC_ID_PICTOR, "pic" }, + { AV_CODEC_ID_V210X, "yuv10" }, + { AV_CODEC_ID_WEBP, "webp" }, + { AV_CODEC_ID_XBM, "xbm" }, + { AV_CODEC_ID_XFACE, "xface" }, + { AV_CODEC_ID_XWD, "xwd" }, + { AV_CODEC_ID_NONE, NULL } +}; + +static enum AVCodecID av_str2id(const IdStrMap *tags, const char *str) +{ + str = strrchr(str, '.'); + if (!str) + return AV_CODEC_ID_NONE; + str++; + + while (tags->id) { + if (!av_strcasecmp(str, tags->str)) + return tags->id; + + tags++; + } + return AV_CODEC_ID_NONE; +} + +enum AVCodecID ff_guess_image2_codec(const char *filename) +{ + return av_str2id(img_tags, filename); +} diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.c new file mode 100644 index 000000000..a9ff22ae1 --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.c @@ -0,0 +1,177 @@ +/* + * RAW demuxers + * Copyright (c) 2001 Fabrice Bellard + * Copyright (c) 2005 Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" +#include "internal.h" +#include "avio_internal.h" +#include "rawdec.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "libavutil/avassert.h" + +#define RAW_PACKET_SIZE 1024 + +int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt) +{ + int ret, size; + + size = RAW_PACKET_SIZE; + + if (av_new_packet(pkt, size) < 0) + return AVERROR(ENOMEM); + + pkt->pos= avio_tell(s->pb); + pkt->stream_index = 0; + ret = ffio_read_partial(s->pb, pkt->data, size); + if (ret < 0) { + av_free_packet(pkt); + return ret; + } + av_shrink_packet(pkt, ret); + return ret; +} + +int ff_raw_audio_read_header(AVFormatContext *s) +{ + AVStream *st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + st->codec->codec_type = AVMEDIA_TYPE_AUDIO; + st->codec->codec_id = s->iformat->raw_codec_id; + st->need_parsing = AVSTREAM_PARSE_FULL_RAW; + st->start_time = 0; + /* the parameters will be extracted from the compressed bitstream */ + + return 0; +} + +/* MPEG-1/H.263 input */ +int ff_raw_video_read_header(AVFormatContext *s) +{ + AVStream *st; + FFRawVideoDemuxerContext *s1 = s->priv_data; + int ret = 0; + + + st = avformat_new_stream(s, NULL); + if (!st) { + ret = AVERROR(ENOMEM); + goto fail; + } + + st->codec->codec_type = AVMEDIA_TYPE_VIDEO; + st->codec->codec_id = s->iformat->raw_codec_id; + st->need_parsing = AVSTREAM_PARSE_FULL_RAW; + + st->codec->time_base = av_inv_q(s1->framerate); + avpriv_set_pts_info(st, 64, 1, 1200000); + +fail: + return ret; +} + +static int ff_raw_data_read_header(AVFormatContext *s) +{ + AVStream *st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + st->codec->codec_type = AVMEDIA_TYPE_DATA; + st->codec->codec_id = s->iformat->raw_codec_id; + st->start_time = 0; + return 0; +} + +/* Note: Do not forget to add new entries to the Makefile as well. */ + +#define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x) +#define DEC AV_OPT_FLAG_DECODING_PARAM +const AVOption ff_rawvideo_options[] = { + { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC}, + { NULL }, +}; + +#if CONFIG_DATA_DEMUXER +AVInputFormat ff_data_demuxer = { + .name = "data", + .long_name = NULL_IF_CONFIG_SMALL("raw data"), + .read_header = ff_raw_data_read_header, + .read_packet = ff_raw_read_partial_packet, + .raw_codec_id = AV_CODEC_ID_NONE, +}; +#endif + +#if CONFIG_LATM_DEMUXER +AVInputFormat ff_latm_demuxer = { + .name = "latm", + .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"), + .read_header = ff_raw_audio_read_header, + .read_packet = ff_raw_read_partial_packet, + .flags = AVFMT_GENERIC_INDEX, + .extensions = "latm", + .raw_codec_id = AV_CODEC_ID_AAC_LATM, +}; +#endif + +#if CONFIG_MJPEG_DEMUXER +FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG) +#endif + +#if CONFIG_MLP_DEMUXER +AVInputFormat ff_mlp_demuxer = { + .name = "mlp", + .long_name = NULL_IF_CONFIG_SMALL("raw MLP"), + .read_header = ff_raw_audio_read_header, + .read_packet = ff_raw_read_partial_packet, + .flags = AVFMT_GENERIC_INDEX, + .extensions = "mlp", + .raw_codec_id = AV_CODEC_ID_MLP, +}; +#endif + +#if CONFIG_TRUEHD_DEMUXER +AVInputFormat ff_truehd_demuxer = { + .name = "truehd", + .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"), + .read_header = ff_raw_audio_read_header, + .read_packet = ff_raw_read_partial_packet, + .flags = AVFMT_GENERIC_INDEX, + .extensions = "thd", + .raw_codec_id = AV_CODEC_ID_TRUEHD, +}; +#endif + +#if CONFIG_SHORTEN_DEMUXER +AVInputFormat ff_shorten_demuxer = { + .name = "shn", + .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"), + .read_header = ff_raw_audio_read_header, + .read_packet = ff_raw_read_partial_packet, + .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK, + .extensions = "shn", + .raw_codec_id = AV_CODEC_ID_SHORTEN, +}; +#endif + +#if CONFIG_VC1_DEMUXER +FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1) +#endif diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.h b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.h new file mode 100644 index 000000000..59108559d --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/rawdec.h @@ -0,0 +1,67 @@ +/* + * RAW demuxers + * Copyright (C) 2007 Aurelien Jacobs + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVFORMAT_RAWDEC_H +#define AVFORMAT_RAWDEC_H + +#include "avformat.h" +#include "libavutil/log.h" +#include "libavutil/opt.h" + +typedef struct FFRawVideoDemuxerContext { + const AVClass *class; /**< Class for private options. */ + char *video_size; /**< String describing video size, set by a private option. */ + char *pixel_format; /**< Set by a private option. */ + AVRational framerate; /**< AVRational describing framerate, set by a private option. */ +} FFRawVideoDemuxerContext; + +extern const AVOption ff_rawvideo_options[]; + +int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt); + +int ff_raw_audio_read_header(AVFormatContext *s); + +int ff_raw_video_read_header(AVFormatContext *s); + +#define FF_RAWVIDEO_DEMUXER_CLASS(name)\ +static const AVClass name ## _demuxer_class = {\ + .class_name = #name " demuxer",\ + .item_name = av_default_item_name,\ + .option = ff_rawvideo_options,\ + .version = LIBAVUTIL_VERSION_INT,\ +}; + +#define FF_DEF_RAWVIDEO_DEMUXER(shortname, longname, probe, ext, id)\ +FF_RAWVIDEO_DEMUXER_CLASS(shortname)\ +AVInputFormat ff_ ## shortname ## _demuxer = {\ + .name = #shortname,\ + .long_name = NULL_IF_CONFIG_SMALL(longname),\ + .read_probe = probe,\ + .read_header = ff_raw_video_read_header,\ + .read_packet = ff_raw_read_partial_packet,\ + .extensions = ext,\ + .flags = AVFMT_GENERIC_INDEX,\ + .raw_codec_id = id,\ + .priv_data_size = sizeof(FFRawVideoDemuxerContext),\ + .priv_class = &shortname ## _demuxer_class,\ +}; + +#endif /* AVFORMAT_RAWDEC_H */ diff --git a/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/takdec.c b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/takdec.c new file mode 100644 index 000000000..2ed8a1e3b --- /dev/null +++ b/Frameworks/FFMPEG/ffmpeg-minimal/libavformat/takdec.c @@ -0,0 +1,210 @@ +/* + * Raw TAK demuxer + * Copyright (c) 2012 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/crc.h" +#include "libavcodec/tak.h" +#include "avformat.h" +#include "avio_internal.h" +#include "internal.h" +#include "rawdec.h" +#include "apetag.h" + +typedef struct TAKDemuxContext { + int mlast_frame; + int64_t data_end; +} TAKDemuxContext; + +static int tak_probe(AVProbeData *p) +{ + if (!memcmp(p->buf, "tBaK", 4)) + return AVPROBE_SCORE_EXTENSION; + return 0; +} + +static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf, + unsigned int len) +{ + return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len); +} + +static int tak_read_header(AVFormatContext *s) +{ + TAKDemuxContext *tc = s->priv_data; + AVIOContext *pb = s->pb; + GetBitContext gb; + AVStream *st; + uint8_t *buffer = NULL; + int ret; + + st = avformat_new_stream(s, 0); + if (!st) + return AVERROR(ENOMEM); + + st->codec->codec_type = AVMEDIA_TYPE_AUDIO; + st->codec->codec_id = AV_CODEC_ID_TAK; + st->need_parsing = AVSTREAM_PARSE_FULL_RAW; + + tc->mlast_frame = 0; + if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) { + avio_seek(pb, -4, SEEK_CUR); + return 0; + } + + while (!url_feof(pb)) { + enum TAKMetaDataType type; + int size; + + type = avio_r8(pb) & 0x7f; + size = avio_rl24(pb); + + switch (type) { + case TAK_METADATA_STREAMINFO: + case TAK_METADATA_LAST_FRAME: + case TAK_METADATA_ENCODER: + if (size <= 3) + return AVERROR_INVALIDDATA; + + buffer = av_malloc(size - 3 + FF_INPUT_BUFFER_PADDING_SIZE); + if (!buffer) + return AVERROR(ENOMEM); + + ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U); + if (avio_read(pb, buffer, size - 3) != size - 3) { + av_freep(&buffer); + return AVERROR(EIO); + } + if (ffio_get_checksum(s->pb) != avio_rb24(pb)) { + av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type); + if (s->error_recognition & AV_EF_EXPLODE) { + av_freep(&buffer); + return AVERROR_INVALIDDATA; + } + } + + init_get_bits8(&gb, buffer, size - 3); + break; + case TAK_METADATA_MD5: { + uint8_t md5[16]; + int i; + + if (size != 19) + return AVERROR_INVALIDDATA; + ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U); + avio_read(pb, md5, 16); + if (ffio_get_checksum(s->pb) != avio_rb24(pb)) { + av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n"); + if (s->error_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + } + + av_log(s, AV_LOG_VERBOSE, "MD5="); + for (i = 0; i < 16; i++) + av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]); + av_log(s, AV_LOG_VERBOSE, "\n"); + break; + } + case TAK_METADATA_END: { + int64_t curpos = avio_tell(pb); + + if (pb->seekable) { + ff_ape_parse_tag(s); + avio_seek(pb, curpos, SEEK_SET); + } + + tc->data_end += curpos; + return 0; + } + default: + ret = avio_skip(pb, size); + if (ret < 0) + return ret; + } + + if (type == TAK_METADATA_STREAMINFO) { + TAKStreamInfo ti; + + avpriv_tak_parse_streaminfo(&gb, &ti); + if (ti.samples > 0) + st->duration = ti.samples; + st->codec->bits_per_coded_sample = ti.bps; + if (ti.ch_layout) + st->codec->channel_layout = ti.ch_layout; + st->codec->sample_rate = ti.sample_rate; + st->codec->channels = ti.channels; + st->start_time = 0; + avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); + st->codec->extradata = buffer; + st->codec->extradata_size = size - 3; + buffer = NULL; + } else if (type == TAK_METADATA_LAST_FRAME) { + if (size != 11) + return AVERROR_INVALIDDATA; + tc->mlast_frame = 1; + tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) + + get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS); + av_freep(&buffer); + } else if (type == TAK_METADATA_ENCODER) { + av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n", + get_bits_long(&gb, TAK_ENCODER_VERSION_BITS)); + av_freep(&buffer); + } + } + + return AVERROR_EOF; +} + +static int raw_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + TAKDemuxContext *tc = s->priv_data; + int ret; + + if (tc->mlast_frame) { + AVIOContext *pb = s->pb; + int64_t size, left; + + left = tc->data_end - avio_tell(pb); + size = FFMIN(left, 1024); + if (size <= 0) + return AVERROR_EOF; + + ret = av_get_packet(pb, pkt, size); + if (ret < 0) + return ret; + + pkt->stream_index = 0; + } else { + ret = ff_raw_read_partial_packet(s, pkt); + } + + return ret; +} + +AVInputFormat ff_tak_demuxer = { + .name = "tak", + .long_name = NULL_IF_CONFIG_SMALL("raw TAK"), + .priv_data_size = sizeof(TAKDemuxContext), + .read_probe = tak_probe, + .read_header = tak_read_header, + .read_packet = raw_read_packet, + .flags = AVFMT_GENERIC_INDEX, + .extensions = "tak", + .raw_codec_id = AV_CODEC_ID_TAK, +}; diff --git a/Frameworks/TagLib/taglib/taglib/fileref.cpp b/Frameworks/TagLib/taglib/taglib/fileref.cpp index f7b5a8e20..9902638b7 100644 --- a/Frameworks/TagLib/taglib/taglib/fileref.cpp +++ b/Frameworks/TagLib/taglib/taglib/fileref.cpp @@ -138,6 +138,7 @@ StringList FileRef::defaultFileExtensions() l.append("asf"); l.append("ape"); l.append("opus"); + l.append("tak"); return l; } @@ -233,6 +234,8 @@ File *FileRef::create(FileName fileName, bool readAudioProperties, return new ASF::File(fileName, readAudioProperties, audioPropertiesStyle); if(ext == "APE") return new APE::File(fileName, readAudioProperties, audioPropertiesStyle); + if(ext == "TAK") + return new APE::File(fileName, false, audioPropertiesStyle); } return 0; diff --git a/Plugins/FFMPEG/FFMPEGDecoder.m b/Plugins/FFMPEG/FFMPEGDecoder.m index 7bf376217..7ec4fd305 100644 --- a/Plugins/FFMPEG/FFMPEGDecoder.m +++ b/Plugins/FFMPEG/FFMPEGDecoder.m @@ -293,12 +293,12 @@ int lockmgr_callback(void ** mutex, enum AVLockOp op) + (NSArray *)fileTypes { - return [NSArray arrayWithObjects:@"wma", @"asf", @"xwma", nil]; + return [NSArray arrayWithObjects:@"wma", @"asf", @"xwma", @"tak", nil]; } + (NSArray *)mimeTypes { - return [NSArray arrayWithObjects:@"application/wma", @"application/x-wma", @"audio/x-wma", @"audio/x-ms-wma", nil]; + return [NSArray arrayWithObjects:@"application/wma", @"application/x-wma", @"audio/x-wma", @"audio/x-ms-wma", @"audio/x-tak", nil]; } diff --git a/Plugins/TagLib/TagLibMetadataReader.m b/Plugins/TagLib/TagLibMetadataReader.m index 33d5fb30b..5a4ba7d2f 100644 --- a/Plugins/TagLib/TagLibMetadataReader.m +++ b/Plugins/TagLib/TagLibMetadataReader.m @@ -174,12 +174,12 @@ + (NSArray *)fileTypes { //May be a way to get a list of supported formats - return [NSArray arrayWithObjects:@"ape", @"asf", @"wma", @"ogg", @"opus", @"mpc", @"flac", @"m4a", @"mp3", nil]; + return [NSArray arrayWithObjects:@"ape", @"asf", @"wma", @"ogg", @"opus", @"mpc", @"flac", @"m4a", @"mp3", @"tak", nil]; } + (NSArray *)mimeTypes { - return [NSArray arrayWithObjects:@"audio/x-ape", @"audio/x-ms-wma", @"application/ogg", @"application/x-ogg", @"audio/x-vorbis+ogg", @"audio/x-musepack", @"audio/x-flac", @"audio/x-m4a", @"audio/mpeg", @"audio/x-mp3", nil]; + return [NSArray arrayWithObjects:@"audio/x-ape", @"audio/x-ms-wma", @"application/ogg", @"application/x-ogg", @"audio/x-vorbis+ogg", @"audio/x-musepack", @"audio/x-flac", @"audio/x-m4a", @"audio/mpeg", @"audio/x-mp3", @"audio/x-tak", nil]; } @end