Skip to content

Commit

Permalink
Merge pull request #763 from wader/mp3_frmae_xing-better-lame-ext-detect
Browse files Browse the repository at this point in the history
mp3_frame_xing: Detect lame ext more similar to ffmpeg and mediainfo
  • Loading branch information
wader authored Sep 8, 2023
2 parents b059614 + 0699c80 commit d90772e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
51 changes: 32 additions & 19 deletions format/mp3/mp3_frame_xing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func init() {

func mp3FrameTagXingDecode(d *decode.D) any {
d.FieldUTF8("header", 4, d.StrAssert("Xing", "Info"))
lamePresent := false
qualityPresent := false
tocPresent := false
bytesPresent := false
framesPresent := false
d.FieldStruct("present_flags", func(d *decode.D) {
d.FieldU("unused", 28)
d.FieldU("unused", 27)
lamePresent = d.FieldBool("lame")
qualityPresent = d.FieldBool("quality")
tocPresent = d.FieldBool("toc")
bytesPresent = d.FieldBool("bytes")
Expand All @@ -51,24 +53,35 @@ func mp3FrameTagXingDecode(d *decode.D) any {
d.FieldU32BE("quality")
}

d.FieldUTF8("encoder", 9)
d.FieldU4("tag_revision")
d.FieldU4("vbr_method")
d.FieldU8("lowpass_filter") // TODO: /100
d.FieldU32("replay_gain_peak")
d.FieldU16("radio_replay_gain")
d.FieldU16("audiophile_replay_gain")
d.FieldU4("lame_flags")
d.FieldU4("lame_ath_type")
d.FieldU8("abr_vbr") // TODO:
d.FieldU12("encoder_delay") // TODO:
d.FieldU12("encoder_padding") // TODO:
d.FieldU8("misc") // TODO:
d.FieldU8("mp3_gain") // TODO:
d.FieldU16("preset") // TODO:
d.FieldU32("length")
d.FieldU16("music_crc") // TODO:
d.FieldU16("tag_crc") // TODO:
// this is mix of what ffmpeg and mediainfo does to detect lame extensions
peekLame, _ := d.TryPeekBytes(4)
peekLaneStr := string(peekLame)
hasLameHeader := (peekLaneStr == "LAME" ||
peekLaneStr == "Lavf" ||
peekLaneStr == "Lavc" ||
peekLaneStr == "GOGO" ||
peekLaneStr == "L3.9")

if lamePresent || hasLameHeader {
d.FieldUTF8("encoder", 9)
d.FieldU4("tag_revision")
d.FieldU4("vbr_method")
d.FieldU8("lowpass_filter") // TODO: /100
d.FieldU32("replay_gain_peak")
d.FieldU16("radio_replay_gain")
d.FieldU16("audiophile_replay_gain")
d.FieldU4("lame_flags")
d.FieldU4("lame_ath_type")
d.FieldU8("abr_vbr") // TODO:
d.FieldU12("encoder_delay") // TODO:
d.FieldU12("encoder_padding") // TODO:
d.FieldU8("misc") // TODO:
d.FieldU8("mp3_gain") // TODO:
d.FieldU16("preset") // TODO:
d.FieldU32("length")
d.FieldU16("music_crc") // TODO:
d.FieldU16("tag_crc") // TODO:
}

return nil
}
3 changes: 2 additions & 1 deletion format/mp3/testdata/mp3_frame_info.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ $ fq -d mp3_frame_xing dv mp3_frame_info
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: mp3_frame_info (mp3_frame_xing) 0x0-0x9b.7 (156)
0x00|49 6e 66 6f |Info | header: "Info" (valid) 0x0-0x3.7 (4)
| | | present_flags{}: 0x4-0x7.7 (4)
0x00| 00 00 00 0f | .... | unused: 0 0x4-0x7.3 (3.4)
0x00| 00 00 00 0f | .... | unused: 0 0x4-0x7.2 (3.3)
0x00| 0f | . | lame: false 0x7.3-0x7.3 (0.1)
0x00| 0f | . | quality: true 0x7.4-0x7.4 (0.1)
0x00| 0f | . | toc: true 0x7.5-0x7.5 (0.1)
0x00| 0f | . | bytes: true 0x7.6-0x7.6 (0.1)
Expand Down
3 changes: 2 additions & 1 deletion format/mp3/testdata/mp3_frame_xing.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ $ fq -d mp3_frame_xing dv mp3_frame_xing
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: mp3_frame_xing (mp3_frame_xing) 0x0-0x9b.7 (156)
0x00|58 69 6e 67 |Xing | header: "Xing" (valid) 0x0-0x3.7 (4)
| | | present_flags{}: 0x4-0x7.7 (4)
0x00| 00 00 00 0f | .... | unused: 0 0x4-0x7.3 (3.4)
0x00| 00 00 00 0f | .... | unused: 0 0x4-0x7.2 (3.3)
0x00| 0f | . | lame: false 0x7.3-0x7.3 (0.1)
0x00| 0f | . | quality: true 0x7.4-0x7.4 (0.1)
0x00| 0f | . | toc: true 0x7.5-0x7.5 (0.1)
0x00| 0f | . | bytes: true 0x7.6-0x7.6 (0.1)
Expand Down
9 changes: 7 additions & 2 deletions pkg/interp/testdata/display.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mp3> .frames[0].tag | "d", d, "da", da, "dd", dd, "dv", dv, "ddv", ddv
0x40| 49 6e 66 6f | Info | header: "Info" (valid)
| | | present_flags{}:
0x40| 00 00 00 0f | .... | unused: 0
0x40| 0f | . | lame: false
0x40| 0f | . | quality: true
0x40| 0f | . | toc: true
0x40| 0f | . | bytes: true
Expand Down Expand Up @@ -171,6 +172,7 @@ mp3> .frames[0].tag | "d", d, "da", da, "dd", dd, "dv", dv, "ddv", ddv
0x40| 49 6e 66 6f | Info | header: "Info" (valid)
| | | present_flags{}:
0x40| 00 00 00 0f | .... | unused: 0
0x40| 0f | . | lame: false
0x40| 0f | . | quality: true
0x40| 0f | . | toc: true
0x40| 0f | . | bytes: true
Expand Down Expand Up @@ -305,6 +307,7 @@ mp3> .frames[0].tag | "d", d, "da", da, "dd", dd, "dv", dv, "ddv", ddv
0x40| 49 6e 66 6f | Info | header: "Info" (valid)
| | | present_flags{}:
0x40| 00 00 00 0f | .... | unused: 0
0x40| 0f | . | lame: false
0x40| 0f | . | quality: true
0x40| 0f | . | toc: true
0x40| 0f | . | bytes: true
Expand Down Expand Up @@ -438,7 +441,8 @@ mp3> .frames[0].tag | "d", d, "da", da, "dd", dd, "dv", dv, "ddv", ddv
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.frames[0].tag{}: (mp3_frame_xing) 0x42-0xdd.7 (156)
0x40| 49 6e 66 6f | Info | header: "Info" (valid) 0x42-0x45.7 (4)
| | | present_flags{}: 0x46-0x49.7 (4)
0x40| 00 00 00 0f | .... | unused: 0 0x46-0x49.3 (3.4)
0x40| 00 00 00 0f | .... | unused: 0 0x46-0x49.2 (3.3)
0x40| 0f | . | lame: false 0x49.3-0x49.3 (0.1)
0x40| 0f | . | quality: true 0x49.4-0x49.4 (0.1)
0x40| 0f | . | toc: true 0x49.5-0x49.5 (0.1)
0x40| 0f | . | bytes: true 0x49.6-0x49.6 (0.1)
Expand Down Expand Up @@ -572,7 +576,8 @@ mp3> .frames[0].tag | "d", d, "da", da, "dd", dd, "dv", dv, "ddv", ddv
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.frames[0].tag{}: (mp3_frame_xing) 0x42-0xdd.7 (156)
0x40| 49 6e 66 6f | Info | header: "Info" (valid) 0x42-0x45.7 (4)
| | | present_flags{}: 0x46-0x49.7 (4)
0x40| 00 00 00 0f | .... | unused: 0 0x46-0x49.3 (3.4)
0x40| 00 00 00 0f | .... | unused: 0 0x46-0x49.2 (3.3)
0x40| 0f | . | lame: false 0x49.3-0x49.3 (0.1)
0x40| 0f | . | quality: true 0x49.4-0x49.4 (0.1)
0x40| 0f | . | toc: true 0x49.5-0x49.5 (0.1)
0x40| 0f | . | bytes: true 0x49.6-0x49.6 (0.1)
Expand Down
2 changes: 2 additions & 0 deletions pkg/interp/testdata/value_array.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ mp3> .headers[0] = 1
"present_flags": {
"bytes": true,
"frames": true,
"lame": false,
"quality": true,
"toc": true,
"unused": 0
Expand Down Expand Up @@ -608,6 +609,7 @@ mp3> .headers[0] |= empty
"present_flags": {
"bytes": true,
"frames": true,
"lame": false,
"quality": true,
"toc": true,
"unused": 0
Expand Down
2 changes: 2 additions & 0 deletions pkg/interp/testdata/value_object.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ mp3> .headers[0].header.flags.a = 1
"present_flags": {
"bytes": true,
"frames": true,
"lame": false,
"quality": true,
"toc": true,
"unused": 0
Expand Down Expand Up @@ -561,6 +562,7 @@ mp3> .headers[0].header.flags.a |= empty
"present_flags": {
"bytes": true,
"frames": true,
"lame": false,
"quality": true,
"toc": true,
"unused": 0
Expand Down

0 comments on commit d90772e

Please sign in to comment.