1
1
mirror of https://github.com/wader/fq.git synced 2024-10-04 07:27:08 +03:00
fq/format/mp4/mp4.go

326 lines
9.9 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package mp4
2021-09-14 18:01:25 +03:00
// Tries to decode ISOBMFF quicktime mov
// Uses naming from ISOBMFF when possible
// ISO/IEC 14496-12
2020-06-08 03:29:51 +03:00
// Quicktime file format https://developer.apple.com/standards/qtff-2001.pdf
// FLAC in ISOBMFF https://github.com/xiph/flac/blob/master/doc/isoflac.txt
2021-09-14 18:01:25 +03:00
// vp9 in ISOBMFF https://www.webmproject.org/vp9/mp4/
// https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/Metadata/Metadata.html#//apple_ref/doc/uid/TP40000939-CH1-SW43
2020-06-08 03:29:51 +03:00
// TODO: validate structure better? trak/stco etc
// TODO: keep track of structure somehow to detect errors
2021-09-14 18:01:25 +03:00
// TODO: ISO-14496 says mp4 mdat can begin and end with original header/trailer (no used?)
2020-06-08 03:29:51 +03:00
// TODO: split into mov and mp4 decoder?
// TODO: split into mp4_box decoder? needs complex in/out args?
// TODO: better probe, find first 2 boxes, should be free,ftyp or mdat?
import (
"embed"
"fmt"
2020-06-08 03:29:51 +03:00
"sort"
"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
"github.com/wader/fq/pkg/decode"
2020-06-08 03:29:51 +03:00
)
//go:embed *.jq
var mp4FS embed.FS
var aacFrameFormat decode.Group
var av1CCRFormat decode.Group
var av1FrameFormat decode.Group
var flacFrameFormat decode.Group
var flacMetadatablocksFormat decode.Group
var id3v2Format decode.Group
var imageFormat decode.Group
var jpegFormat decode.Group
var mp3FrameFormat decode.Group
var mpegAVCAUFormat decode.Group
var mpegAVCDCRFormat decode.Group
var mpegESFormat decode.Group
var mpegHEVCDCRFrameFormat decode.Group
var mpegHEVCSampleFormat decode.Group
var mpegPESPacketSampleFormat decode.Group
var opusPacketFrameFormat decode.Group
var protoBufWidevineFormat decode.Group
var psshPlayreadyFormat decode.Group
var vorbisPacketFormat decode.Group
var vp9FrameFormat decode.Group
var vpxCCRFormat decode.Group
2022-03-01 13:43:50 +03:00
var iccProfileFormat decode.Group
2020-06-08 03:29:51 +03:00
func init() {
registry.MustRegister(decode.Format{
2020-06-08 03:29:51 +03:00
Name: format.MP4,
Description: "MPEG-4 file and similar",
Groups: []string{
format.PROBE,
format.IMAGE, // avif
},
DecodeFn: mp4Decode,
2020-06-08 03:29:51 +03:00
Dependencies: []decode.Dependency{
{Names: []string{format.AAC_FRAME}, Group: &aacFrameFormat},
{Names: []string{format.AV1_CCR}, Group: &av1CCRFormat},
{Names: []string{format.AV1_FRAME}, Group: &av1FrameFormat},
{Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat},
{Names: []string{format.FLAC_METADATABLOCKS}, Group: &flacMetadatablocksFormat},
{Names: []string{format.ID3V2}, Group: &id3v2Format},
{Names: []string{format.IMAGE}, Group: &imageFormat},
{Names: []string{format.JPEG}, Group: &jpegFormat},
{Names: []string{format.MP3_FRAME}, Group: &mp3FrameFormat},
{Names: []string{format.AVC_AU}, Group: &mpegAVCAUFormat},
{Names: []string{format.AVC_DCR}, Group: &mpegAVCDCRFormat},
{Names: []string{format.MPEG_ES}, Group: &mpegESFormat},
{Names: []string{format.HEVC_AU}, Group: &mpegHEVCSampleFormat},
{Names: []string{format.HEVC_DCR}, Group: &mpegHEVCDCRFrameFormat},
{Names: []string{format.MPEG_PES_PACKET}, Group: &mpegPESPacketSampleFormat},
{Names: []string{format.OPUS_PACKET}, Group: &opusPacketFrameFormat},
{Names: []string{format.PROTOBUF_WIDEVINE}, Group: &protoBufWidevineFormat},
{Names: []string{format.PSSH_PLAYREADY}, Group: &psshPlayreadyFormat},
{Names: []string{format.VORBIS_PACKET}, Group: &vorbisPacketFormat},
{Names: []string{format.VP9_FRAME}, Group: &vp9FrameFormat},
{Names: []string{format.VPX_CCR}, Group: &vpxCCRFormat},
2022-03-01 13:43:50 +03:00
{Names: []string{format.ICC_PROFILE}, Group: &iccProfileFormat},
2020-06-08 03:29:51 +03:00
},
2021-09-21 17:42:35 +03:00
Files: mp4FS,
2020-06-08 03:29:51 +03:00
})
}
type stsc struct {
firstChunk int
samplesPerChunk int
2020-06-08 03:29:51 +03:00
}
type moof struct {
offset int64
defaultSampleSize int64
defaultSampleDescriptionIndex int
dataOffset int64
samplesSizes []int64
2020-06-08 03:29:51 +03:00
}
type sampleDescription struct {
dataFormat string
originalFormat string
2020-06-08 03:29:51 +03:00
}
type stsz struct {
size int64
count int
}
2020-06-08 03:29:51 +03:00
type track struct {
id int
2020-06-08 03:29:51 +03:00
sampleDescriptions []sampleDescription
subType string
stco []int64
2020-06-08 03:29:51 +03:00
stsc []stsc
stsz []stsz
formatInArg interface{}
2020-06-08 03:29:51 +03:00
objectType int // if data format is "mp4a"
moofs []*moof // for fmp4
currentMoof *moof
}
type pathEntry struct {
typ string
data interface{}
}
2020-06-08 03:29:51 +03:00
type decodeContext struct {
path []pathEntry
2020-06-08 03:29:51 +03:00
tracks map[uint32]*track
currentTrack *track
currentMoofOffset int64
}
func (ctx *decodeContext) isParent(typ string) bool {
return ctx.parent().typ == typ
2020-06-08 03:29:51 +03:00
}
func (ctx *decodeContext) parent() pathEntry {
return ctx.path[len(ctx.path)-2]
}
2020-06-08 03:29:51 +03:00
func mp4Tracks(d *decode.D, tracks map[uint32]*track) {
2020-06-08 03:29:51 +03:00
// keep track order stable
var sortedTracks []*track
for _, t := range tracks {
2020-06-08 03:29:51 +03:00
sortedTracks = append(sortedTracks, t)
}
sort.Slice(sortedTracks, func(i, j int) bool { return sortedTracks[i].id < sortedTracks[j].id })
d.FieldArray("tracks", func(d *decode.D) {
2020-06-08 03:29:51 +03:00
for _, t := range sortedTracks {
decodeSampleRange := func(d *decode.D, t *track, dataFormat string, name string, firstBit int64, nBits int64, inArg interface{}) {
2021-12-12 17:25:57 +03:00
d.RangeFn(firstBit, nBits, func(d *decode.D) {
switch {
case dataFormat == "fLaC":
d.FieldFormatLen(name, nBits, flacFrameFormat, inArg)
case dataFormat == "Opus":
d.FieldFormatLen(name, nBits, opusPacketFrameFormat, inArg)
case dataFormat == "vp09":
d.FieldFormatLen(name, nBits, vp9FrameFormat, inArg)
case dataFormat == "avc1":
d.FieldFormatLen(name, nBits, mpegAVCAUFormat, inArg)
case dataFormat == "hev1",
dataFormat == "hvc1":
d.FieldFormatLen(name, nBits, mpegHEVCSampleFormat, inArg)
case dataFormat == "av01":
d.FieldFormatLen(name, nBits, av1FrameFormat, inArg)
case dataFormat == "mp4a" && t.objectType == format.MPEGObjectTypeMP3:
d.FieldFormatLen(name, nBits, mp3FrameFormat, inArg)
case dataFormat == "mp4a" && t.objectType == format.MPEGObjectTypeAAC:
d.FieldFormatLen(name, nBits, aacFrameFormat, inArg)
case dataFormat == "mp4a" && t.objectType == format.MPEGObjectTypeVORBIS:
d.FieldFormatLen(name, nBits, vorbisPacketFormat, inArg)
case dataFormat == "mp4v" && t.objectType == format.MPEGObjectTypeMPEG2VideoMain:
d.FieldFormatLen(name, nBits, mpegPESPacketSampleFormat, inArg)
case dataFormat == "mp4v" && t.objectType == format.MPEGObjectTypeMJPEG:
d.FieldFormatLen(name, nBits, jpegFormat, inArg)
case dataFormat == "jpeg":
d.FieldFormatLen(name, nBits, jpegFormat, inArg)
2020-06-08 03:29:51 +03:00
default:
d.FieldRawLen(name, d.BitsLeft())
2021-12-12 17:25:57 +03:00
}
})
2020-06-08 03:29:51 +03:00
}
d.FieldStruct("track", func(d *decode.D) {
2020-06-08 03:29:51 +03:00
// TODO: handle progressive/fragmented mp4 differently somehow?
trackSDDataFormat := "unknown"
if len(t.sampleDescriptions) > 0 {
sd := t.sampleDescriptions[0]
trackSDDataFormat = sd.dataFormat
if sd.originalFormat != "" {
trackSDDataFormat = sd.originalFormat
}
2020-06-08 03:29:51 +03:00
}
d.FieldArray("samples", func(d *decode.D) {
// TODO: warning? could also be init fragment etc
if len(t.stsz) > 0 && len(t.stsc) > 0 && len(t.stco) > 0 {
stszIndex := 0
stszEntryNr := 0
sampleNr := 0
stscIndex := 0
stscEntryNr := 0
stcoIndex := 0
2020-06-08 03:29:51 +03:00
stszEntry := t.stsz[stszIndex]
2020-06-08 03:29:51 +03:00
stscEntry := t.stsc[stscIndex]
sampleOffset := t.stco[stcoIndex]
2020-06-08 03:29:51 +03:00
logStrFn := func() string {
return fmt.Sprintf("%d: %s: nr=%d: stsz[%d/%d] nr=%d %#v stsc[%d/%d] nr=%d %#v stco[%d/%d]=%d \n",
t.id,
trackSDDataFormat,
sampleNr,
stszIndex, len(t.stsz), stszEntryNr, stszEntry,
stscIndex, len(t.stsc), stscEntryNr, stscEntry,
stcoIndex, len(t.stco), sampleOffset,
)
}
for stszIndex < len(t.stsz) {
if stszEntryNr >= stszEntry.count {
stszIndex++
if stszIndex >= len(t.stsz) {
// TODO: warning if unused stsc/stco entries?
break
}
stszEntry = t.stsz[stszIndex]
stszEntryNr = 0
2020-06-08 03:29:51 +03:00
}
if stscEntryNr >= stscEntry.samplesPerChunk {
stscEntryNr = 0
stcoIndex++
if stcoIndex >= len(t.stco) {
d.Fatalf("outside stco: %s", logStrFn())
}
sampleOffset = t.stco[stcoIndex]
if stscIndex < len(t.stsc)-1 && stcoIndex >= t.stsc[stscIndex+1].firstChunk-1 {
stscIndex++
if stscIndex >= len(t.stsc) {
d.Fatalf("outside stsc: %s", logStrFn())
}
stscEntry = t.stsc[stscIndex]
}
}
// log.Println(logStrFn())
2020-06-08 03:29:51 +03:00
decodeSampleRange(d, t, trackSDDataFormat, "sample", sampleOffset*8, stszEntry.size*8, t.formatInArg)
sampleOffset += stszEntry.size
stscEntryNr++
stszEntryNr++
sampleNr++
2020-06-08 03:29:51 +03:00
}
}
for _, m := range t.moofs {
sampleOffset := m.offset + m.dataOffset
2020-06-08 03:29:51 +03:00
for _, sz := range m.samplesSizes {
dataFormat := trackSDDataFormat
if m.defaultSampleDescriptionIndex != 0 && m.defaultSampleDescriptionIndex-1 < len(t.sampleDescriptions) {
sd := t.sampleDescriptions[m.defaultSampleDescriptionIndex-1]
dataFormat = sd.dataFormat
if sd.originalFormat != "" {
dataFormat = sd.originalFormat
}
2020-06-08 03:29:51 +03:00
}
decodeSampleRange(d, t, dataFormat, "sample", sampleOffset*8, sz*8, t.formatInArg)
sampleOffset += sz
2020-06-08 03:29:51 +03:00
}
}
})
})
}
})
}
func mp4Decode(d *decode.D, in interface{}) interface{} {
ctx := &decodeContext{
path: []pathEntry{{typ: "root"}},
tracks: map[uint32]*track{},
}
// TODO: nicer, validate functions without field?
d.AssertLeastBytesLeft(16)
size := d.U32()
if size < 8 {
d.Fatalf("first box size too small < 8")
}
firstType := d.UTF8(4)
switch firstType {
case "styp", // mp4 segment
"ftyp", // mp4 file
"free", // seems to happen
"moov", // seems to happen
"pnot", // video preview file
"jP ": // JPEG 2000
default:
d.Errorf("no styp, ftyp, free or moov box found")
}
d.SeekRel(-8 * 8)
decodeBoxes(ctx, d)
if len(ctx.tracks) > 0 {
mp4Tracks(d, ctx.tracks)
}
2020-06-08 03:29:51 +03:00
return nil
}