summaryrefslogtreecommitdiff
path: root/lib/q65_codec.f90
blob: 962c13ff0bb323497deb3abbe062a2788ea248d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module q65_codec
  use iso_c_binding
  implicit none
  private
  public :: q65_codec_create, q65_codec_destroy, select_q65_codec, q65_codec_status
  public :: q65_enc, q65_intrinsics_ff, q65_dec, q65_dec_fullaplist

  ! Codec selection is scoped to one serialized decoder operation.
  type(c_ptr), save :: selected_codec=c_null_ptr
  integer, save :: codec_status=0

  interface
    function q65_codec_create() bind(C) result(handle)
      import c_ptr
      type(c_ptr) :: handle
    end function
    subroutine q65_codec_destroy(handle) bind(C)
      import c_ptr
      type(c_ptr), value :: handle
    end subroutine
    integer(c_int) function codec_encode(handle,x,y) bind(C,name="q65_codec_encode")
      import c_ptr,c_int
      type(c_ptr), value :: handle
      integer(c_int), intent(in) :: x(*)
      integer(c_int), intent(out) :: y(*)
    end function
    integer(c_int) function codec_intrinsics(handle,s3,submode,b90ts,fading,prob) &
        bind(C,name="q65_codec_intrinsics")
      import c_ptr,c_int,c_float
      type(c_ptr), value :: handle
      real(c_float), intent(in) :: s3(*)
      integer(c_int), value :: submode,fading
      real(c_float), value :: b90ts
      real(c_float), intent(out) :: prob(*)
    end function
    integer(c_int) function codec_decode(handle,s3,prob,mask,symbols,maxiters,esnodb,xdec,rc) &
        bind(C,name="q65_codec_decode")
      import c_ptr,c_int,c_float
      type(c_ptr), value :: handle
      real(c_float), intent(in) :: s3(*),prob(*)
      integer(c_int), intent(in) :: mask(*),symbols(*)
      integer(c_int), value :: maxiters
      real(c_float), intent(out) :: esnodb
      integer(c_int), intent(out) :: xdec(*),rc
    end function
    integer(c_int) function codec_fullaplist(handle,s3,prob,words,ncw,esnodb,xdec,plog,rc) &
        bind(C,name="q65_codec_decode_fullaplist")
      import c_ptr,c_int,c_float
      type(c_ptr), value :: handle
      real(c_float), intent(in) :: s3(*),prob(*)
      integer(c_int), intent(in) :: words(*)
      integer(c_int), value :: ncw
      real(c_float), intent(out) :: esnodb,plog
      integer(c_int), intent(out) :: xdec(*),rc
    end function
    subroutine legacy_encode(x,y) bind(C,name="q65_enc_")
      import c_int
      integer(c_int), intent(in) :: x(*)
      integer(c_int), intent(out) :: y(*)
    end subroutine
    subroutine legacy_intrinsics(s3,submode,b90ts,fading,prob) bind(C,name="q65_intrinsics_ff_")
      import c_int,c_float
      real(c_float), intent(in) :: s3(*),b90ts
      integer(c_int), intent(in) :: submode,fading
      real(c_float), intent(out) :: prob(*)
    end subroutine
    subroutine legacy_decode(s3,prob,mask,symbols,maxiters,esnodb,xdec,rc) bind(C,name="q65_dec_")
      import c_int,c_float
      real(c_float), intent(in) :: s3(*),prob(*)
      integer(c_int), intent(in) :: mask(*),symbols(*),maxiters
      real(c_float), intent(out) :: esnodb
      integer(c_int), intent(out) :: xdec(*),rc
    end subroutine
    subroutine legacy_fullaplist(s3,prob,words,ncw,esnodb,xdec,plog,rc) bind(C,name="q65_dec_fullaplist_")
      import c_int,c_float
      real(c_float), intent(in) :: s3(*),prob(*)
      integer(c_int), intent(in) :: words(*),ncw
      real(c_float), intent(out) :: esnodb,plog
      integer(c_int), intent(out) :: xdec(*),rc
    end subroutine
  end interface

contains

  subroutine select_q65_codec(handle)
    type(c_ptr), intent(in) :: handle
    selected_codec=handle
    codec_status=0
  end subroutine

  integer function q65_codec_status()
    q65_codec_status=codec_status
  end function

  subroutine q65_enc(x,y)
    integer, intent(in) :: x(13)
    integer, intent(out) :: y(63)
    y=0
    if(codec_status/=0) return
    if(c_associated(selected_codec)) then
      codec_status=codec_encode(selected_codec,x,y)
    else
      call legacy_encode(x,y)
    endif
  end subroutine

  subroutine q65_intrinsics_ff(s3,submode,b90ts,fading,prob)
    real, intent(in) :: s3(*)
    integer, intent(in) :: submode,fading
    real, intent(in) :: b90ts
    real, intent(out) :: prob(64,63)
    prob=0
    if(codec_status/=0) return
    if(c_associated(selected_codec)) then
      codec_status=codec_intrinsics(selected_codec,s3,submode,b90ts,fading,prob)
    else
      call legacy_intrinsics(s3,submode,b90ts,fading,prob)
    endif
  end subroutine

  subroutine q65_dec(s3,prob,mask,symbols,maxiters,esnodb,xdec,rc)
    real, intent(in) :: s3(*),prob(64,63)
    integer, intent(in) :: mask(13),symbols(13),maxiters
    real, intent(out) :: esnodb
    integer, intent(out) :: xdec(13),rc
    esnodb=0
    xdec=0
    rc=-1
    if(codec_status/=0) return
    if(c_associated(selected_codec)) then
      codec_status=codec_decode(selected_codec,s3,prob,mask,symbols,maxiters,esnodb,xdec,rc)
    else
      call legacy_decode(s3,prob,mask,symbols,maxiters,esnodb,xdec,rc)
    endif
  end subroutine

  subroutine q65_dec_fullaplist(s3,prob,words,ncw,esnodb,xdec,plog,rc)
    real, intent(in) :: s3(*),prob(64,63)
    integer, intent(in) :: words(*),ncw
    real, intent(out) :: esnodb,plog
    integer, intent(out) :: xdec(13),rc
    esnodb=0
    plog=0
    xdec=0
    rc=-1
    if(codec_status/=0) return
    if(c_associated(selected_codec)) then
      codec_status=codec_fullaplist(selected_codec,s3,prob,words,ncw,esnodb,xdec,plog,rc)
    else
      call legacy_fullaplist(s3,prob,words,ncw,esnodb,xdec,plog,rc)
    endif
  end subroutine

end module q65_codec