|
| 1 | +package media |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "math" |
| 6 | + |
| 7 | + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" |
| 8 | + |
| 9 | + // Namespace imports |
| 10 | + . "github.com/djthorpe/go-errors" |
| 11 | +) |
| 12 | + |
| 13 | +//////////////////////////////////////////////////////////////////////////////// |
| 14 | +// TYPES |
| 15 | + |
| 16 | +type par struct { |
| 17 | + t MediaType |
| 18 | + audiopar |
| 19 | + videopar |
| 20 | +} |
| 21 | + |
| 22 | +type audiopar struct { |
| 23 | + Ch ff.AVChannelLayout |
| 24 | + SampleFormat ff.AVSampleFormat |
| 25 | + Samplerate int |
| 26 | +} |
| 27 | + |
| 28 | +type videopar struct { |
| 29 | + PixelFormat ff.AVPixelFormat |
| 30 | + Width int |
| 31 | + Height int |
| 32 | + Framerate ff.AVRational |
| 33 | +} |
| 34 | + |
| 35 | +var _ Parameters = (*par)(nil) |
| 36 | + |
| 37 | +//////////////////////////////////////////////////////////////////////////////// |
| 38 | +// LIFECYCLE |
| 39 | + |
| 40 | +// Create new parameters for audio sampling from number of channels, sample format and sample rate in Hz |
| 41 | +func newAudioParameters(numchannels int, samplefmt string, samplerate int) (*par, error) { |
| 42 | + // Get channel layout from number of channels |
| 43 | + var ch ff.AVChannelLayout |
| 44 | + ff.AVUtil_channel_layout_default(&ch, numchannels) |
| 45 | + if name, err := ff.AVUtil_channel_layout_describe(&ch); err != nil { |
| 46 | + return nil, err |
| 47 | + } else { |
| 48 | + return newAudioParametersEx(name, samplefmt, samplerate) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Create new parameters for audio sampling from a channel layout name, sample format and sample rate in Hz |
| 53 | +func newAudioParametersEx(channels string, samplefmt string, samplerate int) (*par, error) { |
| 54 | + par := new(par) |
| 55 | + par.t = AUDIO |
| 56 | + |
| 57 | + // Set the parameters |
| 58 | + if err := ff.AVUtil_channel_layout_from_string(&par.audiopar.Ch, channels); err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + if fmt := ff.AVUtil_get_sample_fmt(samplefmt); fmt == ff.AV_SAMPLE_FMT_NONE { |
| 62 | + return nil, ErrBadParameter.Withf("sample format %q", samplefmt) |
| 63 | + } else { |
| 64 | + par.audiopar.SampleFormat = fmt |
| 65 | + } |
| 66 | + if samplerate <= 0 { |
| 67 | + return nil, ErrBadParameter.Withf("samplerate %v", samplerate) |
| 68 | + } else { |
| 69 | + par.audiopar.Samplerate = samplerate |
| 70 | + } |
| 71 | + |
| 72 | + // Return success |
| 73 | + return par, nil |
| 74 | +} |
| 75 | + |
| 76 | +// Create new parameters for video from a width, height, pixel format and framerate in fps |
| 77 | +func newVideoParametersEx(width int, height int, pixelfmt string, framerate float64) (*par, error) { |
| 78 | + par := new(par) |
| 79 | + par.t = VIDEO |
| 80 | + |
| 81 | + // Set the parameters |
| 82 | + if width <= 0 { |
| 83 | + // Negative widths might mean "flip" but not tested yet |
| 84 | + return nil, ErrBadParameter.Withf("width %v", width) |
| 85 | + } else { |
| 86 | + par.videopar.Width = width |
| 87 | + } |
| 88 | + if height <= 0 { |
| 89 | + // Negative heights might mean "flip" but not tested yet |
| 90 | + return nil, ErrBadParameter.Withf("height %v", height) |
| 91 | + } else { |
| 92 | + par.videopar.Height = height |
| 93 | + } |
| 94 | + if fmt := ff.AVUtil_get_pix_fmt(pixelfmt); fmt == ff.AV_PIX_FMT_NONE { |
| 95 | + return nil, ErrBadParameter.Withf("pixel format %q", pixelfmt) |
| 96 | + } else { |
| 97 | + par.videopar.PixelFormat = fmt |
| 98 | + } |
| 99 | + if framerate <= 0 { |
| 100 | + return nil, ErrBadParameter.Withf("framerate %v", framerate) |
| 101 | + } else { |
| 102 | + par.videopar.Framerate = ff.AVUtil_rational_d2q(1/framerate, 0) |
| 103 | + } |
| 104 | + |
| 105 | + // Return success |
| 106 | + return par, nil |
| 107 | +} |
| 108 | + |
| 109 | +// Create new parameters for video from a frame size, pixel format and framerate in fps |
| 110 | +func newVideoParameters(frame string, pixelfmt string, framerate float64) (*par, error) { |
| 111 | + // Parse the frame size |
| 112 | + w, h, err := ff.AVUtil_parse_video_size(frame) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + return newVideoParametersEx(w, h, pixelfmt, framerate) |
| 117 | +} |
| 118 | + |
| 119 | +//////////////////////////////////////////////////////////////////////////////// |
| 120 | +// STRINGIFY |
| 121 | + |
| 122 | +func (par *par) MarshalJSON() ([]byte, error) { |
| 123 | + if par.t == AUDIO { |
| 124 | + return json.Marshal(par.audiopar) |
| 125 | + } else { |
| 126 | + return json.Marshal(par.videopar) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func (par *par) String() string { |
| 131 | + data, _ := json.MarshalIndent(par, "", " ") |
| 132 | + return string(data) |
| 133 | +} |
| 134 | + |
| 135 | +//////////////////////////////////////////////////////////////////////////////// |
| 136 | +// PUBLIC METHODS |
| 137 | + |
| 138 | +// Return type |
| 139 | +func (par *par) Type() MediaType { |
| 140 | + return par.t |
| 141 | +} |
| 142 | + |
| 143 | +// Return the channel layout |
| 144 | +func (par *par) ChannelLayout() string { |
| 145 | + if name, err := ff.AVUtil_channel_layout_describe(&par.audiopar.Ch); err != nil { |
| 146 | + return "" |
| 147 | + } else { |
| 148 | + return name |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Return the sample format |
| 153 | +func (par *par) SampleFormat() string { |
| 154 | + return ff.AVUtil_get_sample_fmt_name(par.audiopar.SampleFormat) |
| 155 | +} |
| 156 | + |
| 157 | +// Return the sample rate (Hz) |
| 158 | +func (par *par) Samplerate() int { |
| 159 | + return par.audiopar.Samplerate |
| 160 | +} |
| 161 | + |
| 162 | +// Return the sample format |
| 163 | + |
| 164 | +// Return the width of the video frame |
| 165 | +func (par *par) Width() int { |
| 166 | + return par.videopar.Width |
| 167 | +} |
| 168 | + |
| 169 | +// Return the height of the video frame |
| 170 | +func (par *par) Height() int { |
| 171 | + return par.videopar.Height |
| 172 | +} |
| 173 | + |
| 174 | +// Return the pixel format |
| 175 | +func (par *par) PixelFormat() string { |
| 176 | + return ff.AVUtil_get_pix_fmt_name(par.videopar.PixelFormat) |
| 177 | +} |
| 178 | + |
| 179 | +// Return the frame rate (fps) |
| 180 | +func (par *par) Framerate() float64 { |
| 181 | + if v := par.videopar.Framerate.Float(1); v == 0 { |
| 182 | + return math.Inf(1) |
| 183 | + } else { |
| 184 | + return 1 / v |
| 185 | + } |
| 186 | +} |
0 commit comments