Skip to content

Commit d5a5ac9

Browse files
committed
Added AVFormatFlag strings
1 parent 5181acb commit d5a5ac9

File tree

3 files changed

+127
-29
lines changed

3 files changed

+127
-29
lines changed

sys/ffmpeg61/avcodec_parameters.go

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,62 @@ import "C"
1717
////////////////////////////////////////////////////////////////////////////////
1818
// TYPES
1919

20+
type jsonAVCodecParametersAudio struct {
21+
SampleFormat AVSampleFormat `json:"format,omitempty"`
22+
SampleRate int `json:"sample_rate,omitempty"`
23+
ChannelLayout AVChannelLayout `json:"channel_layout,omitempty"`
24+
FrameSize int `json:"frame_size,omitempty"`
25+
}
26+
27+
type jsonAVCodecParameterVideo struct {
28+
PixelFormat AVPixelFormat `json:"format,omitempty"`
29+
Width int `json:"width,omitempty"`
30+
Height int `json:"height,omitempty"`
31+
SampleAspectRatio AVRational `json:"sample_aspect_ratio,omitempty"`
32+
}
33+
2034
type jsonAVCodecParameters struct {
21-
CodecType AVMediaType `json:"codec_type"`
22-
CodecID AVCodecID `json:"codec_id,omitempty"`
23-
CodecTag uint32 `json:"codec_tag,omitempty"`
24-
Format int `json:"format,omitempty"`
25-
BitRate int64 `json:"bit_rate,omitempty"`
26-
Width int `json:"width,omitempty"`
27-
Height int `json:"height,omitempty"`
28-
SampleAspectRatio AVRational `json:"sample_aspect_ratio,omitempty"`
29-
SampleRate int `json:"sample_rate,omitempty"`
30-
FrameSize int `json:"frame_size,omitempty"`
35+
CodecType AVMediaType `json:"codec_type"`
36+
CodecID AVCodecID `json:"codec_id,omitempty"`
37+
CodecTag uint32 `json:"codec_tag,omitempty"`
38+
BitRate int64 `json:"bit_rate,omitempty"`
39+
*jsonAVCodecParametersAudio
40+
*jsonAVCodecParameterVideo
3141
}
3242

3343
////////////////////////////////////////////////////////////////////////////////
3444
// STRINGIFY
3545

3646
func (ctx *AVCodecParameters) MarshalJSON() ([]byte, error) {
37-
return json.Marshal(jsonAVCodecParameters{
38-
CodecType: AVMediaType(ctx.codec_type),
39-
CodecID: AVCodecID(ctx.codec_id),
40-
CodecTag: uint32(ctx.codec_tag),
41-
Format: int(ctx.format),
42-
BitRate: int64(ctx.bit_rate),
43-
Width: int(ctx.width),
44-
Height: int(ctx.height),
45-
SampleAspectRatio: (AVRational)(ctx.sample_aspect_ratio),
46-
SampleRate: int(ctx.sample_rate),
47-
FrameSize: int(ctx.frame_size),
48-
})
47+
par := jsonAVCodecParameters{
48+
CodecType: AVMediaType(ctx.codec_type),
49+
CodecID: AVCodecID(ctx.codec_id),
50+
CodecTag: uint32(ctx.codec_tag),
51+
BitRate: int64(ctx.bit_rate),
52+
}
53+
switch ctx.CodecType() {
54+
case AVMEDIA_TYPE_AUDIO:
55+
par.jsonAVCodecParametersAudio = &jsonAVCodecParametersAudio{
56+
SampleFormat: AVSampleFormat(ctx.format),
57+
SampleRate: int(ctx.sample_rate),
58+
ChannelLayout: AVChannelLayout(ctx.ch_layout),
59+
FrameSize: int(ctx.frame_size),
60+
}
61+
case AVMEDIA_TYPE_VIDEO:
62+
par.jsonAVCodecParameterVideo = &jsonAVCodecParameterVideo{
63+
PixelFormat: AVPixelFormat(ctx.format),
64+
Width: int(ctx.width),
65+
Height: int(ctx.height),
66+
SampleAspectRatio: AVRational(ctx.sample_aspect_ratio),
67+
}
68+
}
69+
70+
return json.Marshal(par)
4971
}
5072

5173
func (ctx *AVCodecParameters) String() string {
52-
if str, err := json.MarshalIndent(ctx, "", " "); err != nil {
53-
return err.Error()
54-
} else {
55-
return string(str)
56-
}
74+
data, _ := json.MarshalIndent(ctx, "", " ")
75+
return string(data)
5776
}
5877

5978
////////////////////////////////////////////////////////////////////////////////

sys/ffmpeg61/avformat.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ type jsonAVFormatContext struct {
112112
Input *AVInputFormat `json:"input_format,omitempty"`
113113
Output *AVOutputFormat `json:"output_format,omitempty"`
114114
Url string `json:"url,omitempty"`
115-
NumStreams uint `json:"num_streams,omitempty"`
115+
NumStreams uint `json:"nb_streams,omitempty"`
116116
Streams []*AVStream `json:"streams,omitempty"`
117117
StartTime int64 `json:"start_time,omitempty"`
118118
Duration int64 `json:"duration,omitempty"`
119119
BitRate int64 `json:"bit_rate,omitempty"`
120+
PacketSize uint `json:"packet_size,omitempty"`
120121
Flags AVFormatFlag `json:"flags,omitempty"`
121122
}
122123

@@ -131,6 +132,7 @@ func (ctx *AVFormatContext) MarshalJSON() ([]byte, error) {
131132
StartTime: int64(ctx.start_time),
132133
Duration: int64(ctx.duration),
133134
BitRate: int64(ctx.bit_rate),
135+
PacketSize: uint(ctx.packet_size),
134136
Flags: AVFormatFlag(ctx.flags),
135137
})
136138
}
@@ -263,6 +265,83 @@ func (ctx *AVFormatContext) Duration() int64 {
263265
////////////////////////////////////////////////////////////////////////////////
264266
// AVFormatFlag
265267

268+
const (
269+
AVFMT_FLAG_NONE AVFormatFlag = 0
270+
AVFMT_FLAG_GENPTS AVFormatFlag = C.AVFMT_FLAG_GENPTS ///< Generate missing pts even if it requires parsing future frames.
271+
AVFMT_FLAG_IGNIDX AVFormatFlag = C.AVFMT_FLAG_IGNIDX ///< Ignore index.
272+
AVFMT_FLAG_NONBLOCK AVFormatFlag = C.AVFMT_FLAG_NONBLOCK ///< Do not block when reading packets from input.
273+
AVFMT_FLAG_IGNDTS AVFormatFlag = C.AVFMT_FLAG_IGNDTS ///< Ignore DTS on frames that contain both DTS & PTS
274+
AVFMT_FLAG_NOFILLIN AVFormatFlag = C.AVFMT_FLAG_NOFILLIN ///< Do not infer any values from other values, just return what is stored in the container
275+
AVFMT_FLAG_NOPARSE AVFormatFlag = C.AVFMT_FLAG_NOPARSE ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled
276+
AVFMT_FLAG_NOBUFFER AVFormatFlag = C.AVFMT_FLAG_NOBUFFER ///< Do not buffer frames when possible
277+
AVFMT_FLAG_CUSTOM_IO AVFormatFlag = C.AVFMT_FLAG_CUSTOM_IO ///< The caller has supplied a custom AVIOContext, don't avio_close() it.
278+
AVFMT_FLAG_DISCARD_CORRUPT AVFormatFlag = C.AVFMT_FLAG_DISCARD_CORRUPT ///< Discard frames marked corrupted
279+
AVFMT_FLAG_FLUSH_PACKETS AVFormatFlag = C.AVFMT_FLAG_FLUSH_PACKETS ///< Flush the AVIOContext every packet.
280+
AVFMT_FLAG_BITEXACT AVFormatFlag = C.AVFMT_FLAG_BITEXACT // When muxing, try to avoid writing any random/volatile data to the output.
281+
AVFMT_FLAG_SORT_DTS AVFormatFlag = C.AVFMT_FLAG_SORT_DTS ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)
282+
AVFMT_FLAG_FAST_SEEK AVFormatFlag = C.AVFMT_FLAG_FAST_SEEK ///< Enable fast, but inaccurate seeks for some formats
283+
AVFMT_FLAG_SHORTEST AVFormatFlag = C.AVFMT_FLAG_SHORTEST ///< Stop muxing when the shortest stream stops.
284+
AVFMT_FLAG_AUTO_BSF AVFormatFlag = C.AVFMT_FLAG_AUTO_BSF ///< Add bitstream filters as requested by the muxer
285+
AVFMT_FLAG_MIN = AVFMT_FLAG_GENPTS
286+
AVFMT_FLAG_MAX = AVFMT_FLAG_AUTO_BSF
287+
)
288+
289+
func (f AVFormatFlag) FlagString() string {
290+
switch f {
291+
case AVFMT_FLAG_NONE:
292+
return "AVFMT_FLAG_NONE"
293+
case AVFMT_FLAG_GENPTS:
294+
return "AVFMT_FLAG_GENPTS"
295+
case AVFMT_FLAG_IGNIDX:
296+
return "AVFMT_FLAG_IGNIDX"
297+
case AVFMT_FLAG_NONBLOCK:
298+
return "AVFMT_FLAG_NONBLOCK"
299+
case AVFMT_FLAG_IGNDTS:
300+
return "AVFMT_FLAG_IGNDTS"
301+
case AVFMT_FLAG_NOFILLIN:
302+
return "AVFMT_FLAG_NOFILLIN"
303+
case AVFMT_FLAG_NOPARSE:
304+
return "AVFMT_FLAG_NOPARSE"
305+
case AVFMT_FLAG_NOBUFFER:
306+
return "AVFMT_FLAG_NOBUFFER"
307+
case AVFMT_FLAG_CUSTOM_IO:
308+
return "AVFMT_FLAG_CUSTOM_IO"
309+
case AVFMT_FLAG_DISCARD_CORRUPT:
310+
return "AVFMT_FLAG_DISCARD_CORRUPT"
311+
case AVFMT_FLAG_FLUSH_PACKETS:
312+
return "AVFMT_FLAG_FLUSH_PACKETS"
313+
case AVFMT_FLAG_BITEXACT:
314+
return "AVFMT_FLAG_BITEXACT"
315+
case AVFMT_FLAG_SORT_DTS:
316+
return "AVFMT_FLAG_SORT_DTS"
317+
case AVFMT_FLAG_FAST_SEEK:
318+
return "AVFMT_FLAG_FAST_SEEK"
319+
case AVFMT_FLAG_SHORTEST:
320+
return "AVFMT_FLAG_SHORTEST"
321+
case AVFMT_FLAG_AUTO_BSF:
322+
return "AVFMT_FLAG_AUTO_BSF"
323+
default:
324+
return fmt.Sprintf("AVFormatFlag(0x%06X)", int(f))
325+
}
326+
}
327+
328+
func (f AVFormatFlag) MarshalJSON() ([]byte, error) {
329+
return json.Marshal(f.String())
330+
}
331+
332+
func (f AVFormatFlag) String() string {
333+
if f == AVFMT_FLAG_NONE {
334+
return f.FlagString()
335+
}
336+
str := ""
337+
for i := AVFMT_FLAG_MIN; i <= AVFMT_FLAG_MAX; i <<= 1 {
338+
if f&i != 0 {
339+
str += "|" + i.FlagString()
340+
}
341+
}
342+
return str[1:]
343+
}
344+
266345
func (f AVFormatFlag) Is(flag AVFormatFlag) bool {
267346
return f&flag == flag
268347
}

writer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Test_writer_001(t *testing.T) {
2323

2424
// Write audio file
2525
filename := filepath.Join(t.TempDir(), t.Name()+".mp3")
26-
stream, err := manager.AudioParameters("mono", "s16", 22050)
26+
stream, err := manager.AudioParameters("mono", "fltp", 22050)
2727
if !assert.NoError(err) {
2828
t.SkipNow()
2929
}

0 commit comments

Comments
 (0)