2020-06-08 03:29:51 +03:00
|
|
|
package mp4
|
|
|
|
|
2021-09-14 18:01:25 +03:00
|
|
|
// Tries to decode ISOBMFF quicktime mov
|
2021-08-12 13:51:17 +03:00
|
|
|
// 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
|
2021-08-12 13:51:17 +03:00
|
|
|
|
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"
|
2022-01-17 15:32:51 +03:00
|
|
|
"fmt"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2022-08-31 11:34:32 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-12-09 19:15:21 +03:00
|
|
|
//go:embed mp4.jq
|
2022-09-10 19:28:54 +03:00
|
|
|
//go:embed mp4.md
|
2020-06-08 03:29:51 +03:00
|
|
|
var mp4FS embed.FS
|
|
|
|
|
2021-11-17 18:46:10 +03:00
|
|
|
var aacFrameFormat decode.Group
|
|
|
|
var av1CCRFormat decode.Group
|
|
|
|
var av1FrameFormat decode.Group
|
2022-11-25 21:47:42 +03:00
|
|
|
var avcAUFormat decode.Group
|
|
|
|
var avcDCRFormat decode.Group
|
2021-11-17 18:46:10 +03:00
|
|
|
var flacFrameFormat decode.Group
|
|
|
|
var flacMetadatablocksFormat decode.Group
|
2022-11-25 21:47:42 +03:00
|
|
|
var hevcAUFormat decode.Group
|
|
|
|
var hevcCDCRFormat decode.Group
|
2022-08-24 20:08:41 +03:00
|
|
|
var iccProfileFormat decode.Group
|
2021-11-17 18:46:10 +03:00
|
|
|
var id3v2Format decode.Group
|
|
|
|
var imageFormat decode.Group
|
|
|
|
var jpegFormat decode.Group
|
|
|
|
var mp3FrameFormat decode.Group
|
|
|
|
var mpegESFormat decode.Group
|
|
|
|
var mpegPESPacketSampleFormat decode.Group
|
|
|
|
var opusPacketFrameFormat decode.Group
|
2022-11-25 21:47:42 +03:00
|
|
|
var pngFormat decode.Group
|
2022-08-24 20:08:41 +03:00
|
|
|
var proResFrameFormat decode.Group
|
2021-11-17 18:46:10 +03:00
|
|
|
var protoBufWidevineFormat decode.Group
|
|
|
|
var psshPlayreadyFormat decode.Group
|
|
|
|
var vorbisPacketFormat decode.Group
|
|
|
|
var vp9FrameFormat decode.Group
|
|
|
|
var vpxCCRFormat decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.MP4,
|
2022-09-10 19:28:54 +03:00
|
|
|
Description: "ISOBMFF, QuickTime and similar",
|
2021-10-19 02:57:15 +03:00
|
|
|
Groups: []string{
|
|
|
|
format.PROBE,
|
|
|
|
format.IMAGE, // avif
|
|
|
|
},
|
|
|
|
DecodeFn: mp4Decode,
|
2021-12-09 19:15:21 +03:00
|
|
|
DecodeInArg: format.Mp4In{
|
|
|
|
DecodeSamples: true,
|
|
|
|
AllowTruncated: false,
|
|
|
|
},
|
2020-06-08 03:29:51 +03:00
|
|
|
Dependencies: []decode.Dependency{
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.AAC_FRAME}, Group: &aacFrameFormat},
|
|
|
|
{Names: []string{format.AV1_CCR}, Group: &av1CCRFormat},
|
|
|
|
{Names: []string{format.AV1_FRAME}, Group: &av1FrameFormat},
|
2022-11-25 21:47:42 +03:00
|
|
|
{Names: []string{format.AVC_AU}, Group: &avcAUFormat},
|
|
|
|
{Names: []string{format.AVC_DCR}, Group: &avcDCRFormat},
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat},
|
|
|
|
{Names: []string{format.FLAC_METADATABLOCKS}, Group: &flacMetadatablocksFormat},
|
2022-11-25 21:47:42 +03:00
|
|
|
{Names: []string{format.HEVC_AU}, Group: &hevcAUFormat},
|
|
|
|
{Names: []string{format.HEVC_DCR}, Group: &hevcCDCRFormat},
|
2022-08-24 20:08:41 +03:00
|
|
|
{Names: []string{format.ICC_PROFILE}, Group: &iccProfileFormat},
|
2021-11-17 18:46:10 +03:00
|
|
|
{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.MPEG_ES}, Group: &mpegESFormat},
|
|
|
|
{Names: []string{format.MPEG_PES_PACKET}, Group: &mpegPESPacketSampleFormat},
|
|
|
|
{Names: []string{format.OPUS_PACKET}, Group: &opusPacketFrameFormat},
|
2022-11-25 21:47:42 +03:00
|
|
|
{Names: []string{format.PNG}, Group: &pngFormat},
|
2022-08-24 20:08:41 +03:00
|
|
|
{Names: []string{format.PRORES_FRAME}, Group: &proResFrameFormat},
|
2021-11-17 18:46:10 +03:00
|
|
|
{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},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
})
|
2022-07-27 14:20:48 +03:00
|
|
|
interp.RegisterFS(mp4FS)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type stsc struct {
|
2022-01-17 15:32:51 +03:00
|
|
|
firstChunk int
|
|
|
|
samplesPerChunk int
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type moof struct {
|
|
|
|
offset int64
|
2022-01-17 15:32:51 +03:00
|
|
|
defaultSampleSize int64
|
|
|
|
defaultSampleDescriptionIndex int
|
2022-06-07 16:34:09 +03:00
|
|
|
truns []trun
|
2022-07-07 20:02:10 +03:00
|
|
|
sencs []senc
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: nothing for now
|
|
|
|
type senc struct {
|
|
|
|
entries []struct{}
|
2022-06-07 16:34:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type trun struct {
|
|
|
|
dataOffset int64
|
|
|
|
samplesSizes []int64
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type sampleDescription struct {
|
2021-09-02 19:02:04 +03:00
|
|
|
dataFormat string
|
|
|
|
originalFormat string
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-01-15 19:42:50 +03:00
|
|
|
type stsz struct {
|
2022-01-17 15:32:51 +03:00
|
|
|
size int64
|
|
|
|
count int
|
2022-01-15 19:42:50 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type track struct {
|
2022-11-25 21:47:42 +03:00
|
|
|
seenHdlr bool
|
2022-01-17 15:32:51 +03:00
|
|
|
id int
|
2020-06-08 03:29:51 +03:00
|
|
|
sampleDescriptions []sampleDescription
|
|
|
|
subType string
|
2022-01-17 15:32:51 +03:00
|
|
|
stco []int64
|
2020-06-08 03:29:51 +03:00
|
|
|
stsc []stsc
|
2022-01-15 19:42:50 +03:00
|
|
|
stsz []stsz
|
2022-05-20 16:10:41 +03:00
|
|
|
formatInArg any
|
2020-06-08 03:29:51 +03:00
|
|
|
objectType int // if data format is "mp4a"
|
2022-06-01 20:43:09 +03:00
|
|
|
defaultIVSize int
|
|
|
|
moofs []*moof // for fmp4
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-04-07 19:25:23 +03:00
|
|
|
type pathEntry struct {
|
|
|
|
typ string
|
2022-05-20 16:10:41 +03:00
|
|
|
data any
|
2022-04-07 19:25:23 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type decodeContext struct {
|
2022-06-01 20:43:09 +03:00
|
|
|
opts format.Mp4In
|
|
|
|
path []pathEntry
|
|
|
|
tracks map[int]*track
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *decodeContext) lookupTrack(id int) *track {
|
|
|
|
t, ok := ctx.tracks[id]
|
|
|
|
if !ok {
|
|
|
|
t = &track{id: id}
|
|
|
|
ctx.tracks[id] = t
|
|
|
|
}
|
|
|
|
return t
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-04-07 19:25:23 +03:00
|
|
|
func (ctx *decodeContext) isParent(typ string) bool {
|
|
|
|
return ctx.parent().typ == typ
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-04-07 19:25:23 +03:00
|
|
|
func (ctx *decodeContext) parent() pathEntry {
|
|
|
|
return ctx.path[len(ctx.path)-2]
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-06-01 20:43:09 +03:00
|
|
|
func (ctx *decodeContext) findParent(typ string) any {
|
|
|
|
for i := len(ctx.path) - 1; i >= 0; i-- {
|
|
|
|
p := ctx.path[i]
|
|
|
|
if p.typ == typ {
|
|
|
|
return p.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *decodeContext) currentTrakBox() *trakBox {
|
|
|
|
t, _ := ctx.findParent("trak").(*trakBox)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *decodeContext) currentTrafBox() *trafBox {
|
|
|
|
t, _ := ctx.findParent("traf").(*trafBox)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *decodeContext) currentMoofBox() *moofBox {
|
|
|
|
t, _ := ctx.findParent("moof").(*moofBox)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *decodeContext) currentTrack() *track {
|
|
|
|
if t := ctx.currentTrakBox(); t != nil {
|
|
|
|
return ctx.lookupTrack(t.trackID)
|
|
|
|
}
|
|
|
|
if t := ctx.currentTrafBox(); t != nil {
|
|
|
|
return ctx.lookupTrack(t.trackID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:15:21 +03:00
|
|
|
func mp4Tracks(d *decode.D, ctx *decodeContext) {
|
2020-06-08 03:29:51 +03:00
|
|
|
// keep track order stable
|
|
|
|
var sortedTracks []*track
|
2021-12-09 19:15:21 +03:00
|
|
|
for _, t := range ctx.tracks {
|
2020-06-08 03:29:51 +03:00
|
|
|
sortedTracks = append(sortedTracks, t)
|
|
|
|
}
|
2022-08-31 11:34:32 +03:00
|
|
|
slices.SortFunc(sortedTracks, func(a, b *track) bool { return a.id < b.id })
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("tracks", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for _, t := range sortedTracks {
|
2022-07-07 20:02:10 +03:00
|
|
|
decodeSampleRange := func(d *decode.D, t *track, decodeSample bool, dataFormat string, name string, firstBit int64, nBits int64, inArg any) {
|
2021-12-12 17:25:57 +03:00
|
|
|
d.RangeFn(firstBit, nBits, func(d *decode.D) {
|
2022-07-07 20:02:10 +03:00
|
|
|
if !decodeSample {
|
2021-12-09 19:15:21 +03:00
|
|
|
d.FieldRawLen(name, d.BitsLeft())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 17:25:57 +03:00
|
|
|
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":
|
2022-11-25 21:47:42 +03:00
|
|
|
d.FieldFormatLen(name, nBits, avcAUFormat, inArg)
|
2021-12-12 17:25:57 +03:00
|
|
|
case dataFormat == "hev1",
|
|
|
|
dataFormat == "hvc1":
|
2022-11-25 21:47:42 +03:00
|
|
|
d.FieldFormatLen(name, nBits, hevcAUFormat, inArg)
|
2021-12-12 17:25:57 +03:00
|
|
|
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)
|
2022-11-25 21:47:42 +03:00
|
|
|
case dataFormat == "mp4v" && t.objectType == format.MPEGObjectTypePNG:
|
|
|
|
d.FieldFormatLen(name, nBits, pngFormat, inArg)
|
2021-12-12 17:25:57 +03:00
|
|
|
case dataFormat == "jpeg":
|
|
|
|
d.FieldFormatLen(name, nBits, jpegFormat, inArg)
|
2022-08-24 20:08:41 +03:00
|
|
|
case dataFormat == "apch",
|
|
|
|
dataFormat == "apcn",
|
|
|
|
dataFormat == "scpa",
|
|
|
|
dataFormat == "apco",
|
|
|
|
dataFormat == "ap4h":
|
|
|
|
d.FieldFormatLen(name, nBits, proResFrameFormat, inArg)
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen(name, d.BitsLeft())
|
2021-12-12 17:25:57 +03:00
|
|
|
}
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("track", func(d *decode.D) {
|
2022-05-08 19:43:05 +03:00
|
|
|
d.FieldValueU("id", uint64(t.id))
|
|
|
|
|
2022-01-17 15:32:51 +03:00
|
|
|
trackSDDataFormat := "unknown"
|
2021-09-02 19:02:04 +03:00
|
|
|
if len(t.sampleDescriptions) > 0 {
|
|
|
|
sd := t.sampleDescriptions[0]
|
2022-01-17 15:32:51 +03:00
|
|
|
trackSDDataFormat = sd.dataFormat
|
2021-09-02 19:02:04 +03:00
|
|
|
if sd.originalFormat != "" {
|
2022-01-17 15:32:51 +03:00
|
|
|
trackSDDataFormat = sd.originalFormat
|
2021-09-02 19:02:04 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:30:10 +03:00
|
|
|
d.FieldValueStr("data_format", trackSDDataFormat, dataFormatNames)
|
2022-05-19 16:21:27 +03:00
|
|
|
|
|
|
|
switch trackSDDataFormat {
|
|
|
|
case "lpcm",
|
|
|
|
"raw ",
|
|
|
|
"twos",
|
|
|
|
"sowt",
|
|
|
|
"in24",
|
|
|
|
"in32",
|
|
|
|
"fl23",
|
|
|
|
"fl64",
|
|
|
|
"alaw",
|
|
|
|
"ulaw":
|
|
|
|
// TODO: treat raw samples format differently, a bit too much to have one field per sample.
|
|
|
|
// maybe in some future fq could have smart array fields
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("samples", func(d *decode.D) {
|
2022-01-17 15:32:51 +03:00
|
|
|
// TODO: warning? could also be init fragment etc
|
2022-01-15 19:42:50 +03:00
|
|
|
|
2022-01-17 15:32:51 +03:00
|
|
|
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
|
|
|
|
2022-01-17 15:32:51 +03:00
|
|
|
stszEntry := t.stsz[stszIndex]
|
2020-06-08 03:29:51 +03:00
|
|
|
stscEntry := t.stsc[stscIndex]
|
2022-01-15 19:42:50 +03:00
|
|
|
sampleOffset := t.stco[stcoIndex]
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-01-17 15:32: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 {
|
2022-01-15 19:42:50 +03:00
|
|
|
stszIndex++
|
|
|
|
if stszIndex >= len(t.stsz) {
|
2022-01-17 15:32:51 +03:00
|
|
|
// TODO: warning if unused stsc/stco entries?
|
|
|
|
break
|
2022-01-15 19:42:50 +03:00
|
|
|
}
|
2022-01-17 15:32:51 +03:00
|
|
|
|
2022-01-15 19:42:50 +03:00
|
|
|
stszEntry = t.stsz[stszIndex]
|
2022-01-17 15:32:51 +03:00
|
|
|
stszEntryNr = 0
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:32: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
|
|
|
|
2022-07-07 20:02:10 +03:00
|
|
|
decodeSampleRange(d, t, ctx.opts.DecodeSamples, trackSDDataFormat, "sample", sampleOffset*8, stszEntry.size*8, t.formatInArg)
|
2022-01-17 15:32:51 +03:00
|
|
|
|
|
|
|
sampleOffset += stszEntry.size
|
|
|
|
stscEntryNr++
|
|
|
|
stszEntryNr++
|
|
|
|
sampleNr++
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:34:09 +03:00
|
|
|
sampleNr := 0
|
2020-06-08 03:29:51 +03:00
|
|
|
for _, m := range t.moofs {
|
2022-07-07 20:02:10 +03:00
|
|
|
for trunNr, trun := range m.truns {
|
|
|
|
var senc senc
|
|
|
|
if trunNr < len(m.sencs) {
|
|
|
|
senc = m.sencs[trunNr]
|
|
|
|
}
|
2022-06-07 16:34:09 +03:00
|
|
|
sampleOffset := m.offset + trun.dataOffset
|
|
|
|
|
2022-07-07 20:02:10 +03:00
|
|
|
for trunSampleNr, sz := range trun.samplesSizes {
|
2022-06-07 16:34:09 +03:00
|
|
|
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
|
|
|
|
}
|
2021-09-02 19:02:04 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-06-07 16:34:09 +03:00
|
|
|
// logStrFn := func() string {
|
|
|
|
// return fmt.Sprintf("%d: %s: %d: (%s): sz=%d %d+%d=%d",
|
|
|
|
// t.id,
|
|
|
|
// dataFormat,
|
|
|
|
// sampleNr,
|
|
|
|
// trackSDDataFormat,
|
|
|
|
// sz,
|
|
|
|
// m.offset,
|
|
|
|
// m.dataOffset,
|
|
|
|
// sampleOffset,
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
// log.Println(logStrFn())
|
|
|
|
|
2022-07-07 20:02:10 +03:00
|
|
|
decodeSample := ctx.opts.DecodeSamples
|
|
|
|
if trunSampleNr < len(senc.entries) {
|
|
|
|
// TODO: encrypted
|
|
|
|
decodeSample = false
|
|
|
|
}
|
|
|
|
|
|
|
|
decodeSampleRange(d, t, decodeSample, dataFormat, "sample", sampleOffset*8, sz*8, t.formatInArg)
|
|
|
|
|
2022-06-07 16:34:09 +03:00
|
|
|
sampleOffset += sz
|
|
|
|
sampleNr++
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-04-07 19:25:23 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func mp4Decode(d *decode.D, in any) any {
|
2021-12-09 19:15:21 +03:00
|
|
|
mi, _ := in.(format.Mp4In)
|
|
|
|
|
2022-04-07 19:25:23 +03:00
|
|
|
ctx := &decodeContext{
|
2021-12-09 19:15:21 +03:00
|
|
|
opts: mi,
|
2022-04-07 19:25:23 +03:00
|
|
|
path: []pathEntry{{typ: "root"}},
|
2022-06-01 20:43:09 +03:00
|
|
|
tracks: map[int]*track{},
|
2022-04-07 19:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-12-09 19:15:21 +03:00
|
|
|
mp4Tracks(d, ctx)
|
2022-04-07 19:25:23 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|