|
| 1 | +package media |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "image" |
| 6 | + "time" |
| 7 | + |
| 8 | + // Packages |
| 9 | + media "github.com/mutablelogic/go-media" |
| 10 | + imagex "github.com/mutablelogic/go-media/pkg/image" |
| 11 | + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" |
| 12 | + |
| 13 | + // Namespace imports |
| 14 | + . "github.com/djthorpe/go-errors" |
| 15 | +) |
| 16 | + |
| 17 | +//////////////////////////////////////////////////////////////////////////////// |
| 18 | +// TYPES |
| 19 | + |
| 20 | +type frame struct { |
| 21 | + ctx *ff.AVFrame |
| 22 | +} |
| 23 | + |
| 24 | +var ( |
| 25 | + yuvSubsampleRatio = map[ff.AVPixelFormat]image.YCbCrSubsampleRatio{ |
| 26 | + ff.AV_PIX_FMT_YUV410P: image.YCbCrSubsampleRatio410, |
| 27 | + ff.AV_PIX_FMT_YUV411P: image.YCbCrSubsampleRatio410, |
| 28 | + ff.AV_PIX_FMT_YUV420P: image.YCbCrSubsampleRatio420, |
| 29 | + ff.AV_PIX_FMT_YUV422P: image.YCbCrSubsampleRatio422, |
| 30 | + ff.AV_PIX_FMT_YUV440P: image.YCbCrSubsampleRatio420, |
| 31 | + ff.AV_PIX_FMT_YUV444P: image.YCbCrSubsampleRatio444, |
| 32 | + } |
| 33 | + yuvaSubsampleRatio = map[ff.AVPixelFormat]image.YCbCrSubsampleRatio{ |
| 34 | + ff.AV_PIX_FMT_YUVA420P: image.YCbCrSubsampleRatio420, |
| 35 | + ff.AV_PIX_FMT_YUVA422P: image.YCbCrSubsampleRatio422, |
| 36 | + ff.AV_PIX_FMT_YUVA444P: image.YCbCrSubsampleRatio444, |
| 37 | + } |
| 38 | +) |
| 39 | + |
| 40 | +//////////////////////////////////////////////////////////////////////////////// |
| 41 | +// LIFECYCLE |
| 42 | + |
| 43 | +func NewFrame(ctx *ff.AVFrame) *frame { |
| 44 | + return &frame{ctx} |
| 45 | +} |
| 46 | + |
| 47 | +//////////////////////////////////////////////////////////////////////////////// |
| 48 | +// STRINGIFY |
| 49 | + |
| 50 | +func (frame *frame) MarshalJSON() ([]byte, error) { |
| 51 | + return json.Marshal(frame.ctx) |
| 52 | +} |
| 53 | + |
| 54 | +func (frame *frame) String() string { |
| 55 | + data, _ := json.MarshalIndent(frame, "", " ") |
| 56 | + return string(data) |
| 57 | +} |
| 58 | + |
| 59 | +//////////////////////////////////////////////////////////////////////////////// |
| 60 | +// PARAMETERS |
| 61 | + |
| 62 | +// Return the media type (AUDIO, VIDEO) |
| 63 | +func (frame *frame) Type() media.MediaType { |
| 64 | + if frame.ctx.NumSamples() > 0 { |
| 65 | + return media.AUDIO |
| 66 | + } |
| 67 | + if frame.ctx.Width() != 0 && frame.ctx.Height() != 0 { |
| 68 | + return media.VIDEO |
| 69 | + } |
| 70 | + return media.NONE |
| 71 | +} |
| 72 | + |
| 73 | +// Id is unused |
| 74 | +func (frame *frame) Id() int { |
| 75 | + return 0 |
| 76 | +} |
| 77 | + |
| 78 | +// Return the timestamp as a duration, or minus one if not set |
| 79 | +func (frame *frame) Time() time.Duration { |
| 80 | + pts := frame.ctx.Pts() |
| 81 | + if pts == ff.AV_NOPTS_VALUE { |
| 82 | + return -1 |
| 83 | + } |
| 84 | + if frame.ctx.TimeBase().Den() == 0 { |
| 85 | + return -1 |
| 86 | + } |
| 87 | + return secondsToDuration(float64(pts) * ff.AVUtil_rational_q2d(frame.ctx.TimeBase())) |
| 88 | +} |
| 89 | + |
| 90 | +// Return the number of planes for a specific PixelFormat |
| 91 | +// or SampleFormat and ChannelLayout combination |
| 92 | +func (frame *frame) NumPlanes() int { |
| 93 | + return ff.AVUtil_frame_get_num_planes(frame.ctx) |
| 94 | +} |
| 95 | + |
| 96 | +// Return the byte data for a plane |
| 97 | +func (frame *frame) Bytes(plane int) []byte { |
| 98 | + return frame.ctx.Bytes(plane)[:frame.ctx.Planesize(plane)] |
| 99 | +} |
| 100 | + |
| 101 | +// Return the int16 data for a plane |
| 102 | +func (frame *frame) Int16(plane int) []int16 { |
| 103 | + sz := frame.ctx.Planesize(plane) >> 1 |
| 104 | + return frame.ctx.Int16(plane)[:sz] |
| 105 | +} |
| 106 | + |
| 107 | +//////////////////////////////////////////////////////////////////////////////// |
| 108 | +// AUDIO PARAMETERS |
| 109 | + |
| 110 | +// Return number of samples |
| 111 | +func (frame *frame) NumSamples() int { |
| 112 | + if frame.Type() != media.AUDIO { |
| 113 | + return 0 |
| 114 | + } |
| 115 | + return frame.ctx.NumSamples() |
| 116 | +} |
| 117 | + |
| 118 | +// Return channel layout |
| 119 | +func (frame *frame) ChannelLayout() string { |
| 120 | + if frame.Type() != media.AUDIO { |
| 121 | + return "" |
| 122 | + } |
| 123 | + ch := frame.ctx.ChannelLayout() |
| 124 | + if name, err := ff.AVUtil_channel_layout_describe(&ch); err != nil { |
| 125 | + return "" |
| 126 | + } else { |
| 127 | + return name |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Return the sample format |
| 132 | +func (frame *frame) SampleFormat() string { |
| 133 | + if frame.Type() != media.AUDIO { |
| 134 | + return "" |
| 135 | + } |
| 136 | + return ff.AVUtil_get_sample_fmt_name(frame.ctx.SampleFormat()) |
| 137 | +} |
| 138 | + |
| 139 | +// Return the sample rate (Hz) |
| 140 | +func (frame *frame) Samplerate() int { |
| 141 | + if frame.Type() != media.AUDIO { |
| 142 | + return 0 |
| 143 | + } |
| 144 | + return frame.ctx.SampleRate() |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +//////////////////////////////////////////////////////////////////////////////// |
| 149 | +// VIDEO PARAMETERS |
| 150 | + |
| 151 | +// Convert a frame into an image |
| 152 | +func (frame *frame) Image() (image.Image, error) { |
| 153 | + if t := frame.Type(); t != media.VIDEO { |
| 154 | + return nil, ErrBadParameter.With("unsupported frame type", t) |
| 155 | + } |
| 156 | + pixel_format := frame.ctx.PixFmt() |
| 157 | + switch pixel_format { |
| 158 | + case ff.AV_PIX_FMT_GRAY8: |
| 159 | + return &image.Gray{ |
| 160 | + Pix: frame.Bytes(0), |
| 161 | + Stride: frame.Stride(0), |
| 162 | + Rect: image.Rect(0, 0, frame.Width(), frame.Height()), |
| 163 | + }, nil |
| 164 | + case ff.AV_PIX_FMT_RGBA: |
| 165 | + return &image.RGBA{ |
| 166 | + Pix: frame.Bytes(0), |
| 167 | + Stride: frame.Stride(0), |
| 168 | + Rect: image.Rect(0, 0, frame.Width(), frame.Height()), |
| 169 | + }, nil |
| 170 | + case ff.AV_PIX_FMT_RGB24: |
| 171 | + return &imagex.RGB24{ |
| 172 | + Pix: frame.Bytes(0), |
| 173 | + Stride: frame.Stride(0), |
| 174 | + Rect: image.Rect(0, 0, frame.Width(), frame.Height()), |
| 175 | + }, nil |
| 176 | + default: |
| 177 | + if ratio, exists := yuvSubsampleRatio[pixel_format]; exists { |
| 178 | + return &image.YCbCr{ |
| 179 | + Y: frame.Bytes(0), |
| 180 | + Cb: frame.Bytes(1), |
| 181 | + Cr: frame.Bytes(2), |
| 182 | + YStride: frame.Stride(0), |
| 183 | + CStride: frame.Stride(1), |
| 184 | + SubsampleRatio: ratio, |
| 185 | + Rect: image.Rect(0, 0, frame.Width(), frame.Height()), |
| 186 | + }, nil |
| 187 | + } |
| 188 | + if ratio, exists := yuvaSubsampleRatio[pixel_format]; exists { |
| 189 | + return &image.NYCbCrA{ |
| 190 | + YCbCr: image.YCbCr{ |
| 191 | + Y: frame.Bytes(0), |
| 192 | + Cb: frame.Bytes(1), |
| 193 | + Cr: frame.Bytes(2), |
| 194 | + YStride: frame.Stride(0), |
| 195 | + CStride: frame.Stride(1), |
| 196 | + SubsampleRatio: ratio, |
| 197 | + Rect: image.Rect(0, 0, frame.Width(), frame.Height()), |
| 198 | + }, |
| 199 | + A: frame.Bytes(3), |
| 200 | + AStride: frame.Stride(3), |
| 201 | + }, nil |
| 202 | + } |
| 203 | + } |
| 204 | + return nil, ErrNotImplemented.With("unsupported pixel format", frame.ctx.PixFmt()) |
| 205 | +} |
| 206 | + |
| 207 | +// Return the number of bytes in a single row of the video frame |
| 208 | +func (frame *frame) Stride(plane int) int { |
| 209 | + if frame.Type() == media.VIDEO { |
| 210 | + return frame.ctx.Linesize(plane) |
| 211 | + } else { |
| 212 | + return 0 |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +// Return the width of the video frame |
| 217 | +func (frame *frame) Width() int { |
| 218 | + if frame.Type() != media.VIDEO { |
| 219 | + return 0 |
| 220 | + } |
| 221 | + return frame.ctx.Width() |
| 222 | +} |
| 223 | + |
| 224 | +// Return the height of the video frame |
| 225 | +func (frame *frame) Height() int { |
| 226 | + if frame.Type() != media.VIDEO { |
| 227 | + return 0 |
| 228 | + } |
| 229 | + return frame.ctx.Height() |
| 230 | +} |
| 231 | + |
| 232 | +// Return the pixel format |
| 233 | +func (frame *frame) PixelFormat() string { |
| 234 | + if frame.Type() != media.VIDEO { |
| 235 | + return "" |
| 236 | + } |
| 237 | + return ff.AVUtil_get_pix_fmt_name(frame.ctx.PixFmt()) |
| 238 | +} |
| 239 | + |
| 240 | +//////////////////////////////////////////////////////////////////////////////// |
| 241 | +// PRIVATE METHODS |
| 242 | + |
| 243 | +func secondsToDuration(seconds float64) time.Duration { |
| 244 | + return time.Duration(seconds * float64(time.Second)) |
| 245 | +} |
0 commit comments