Skip to content

Commit

Permalink
Merge pull request #68 from edgeware/version-nr
Browse files Browse the repository at this point in the history
Introduced version number in cmd apps and mp4 package
  • Loading branch information
tobbee authored Mar 18, 2021
2 parents b56a84f + 2d3ac5d commit c20d74c
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 13 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all: mp4ff-info mp4ff-nallister mp4ff-pslister mp4ff-wvttlister

mp4ff-info mp4ff-nallister mp4ff-pslister mp4ff-wvttlister:
go build -ldflags "-X github.com/edgeware/mp4ff/mp4.commitVersion=$$(git describe --tags HEAD) -X github.com/edgeware/mp4ff/mp4.commitDate=$$(git log -1 --format=%ct)" -o out/$@ cmd/$@/main.go

clean:
rm -f out/*

install: all
cp out/* $(GOPATH)/bin/

1 change: 1 addition & 0 deletions Versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

| Version | Highlight |
| ------ | --------- |
| 0.21.0 | feat: version number in apps and mp4 package |
| 0.20.0 | feat: mp4ff-pslister better for hex SPS input |
| 0.19.0 | fix: trun optimization, feat: mfra-related boxes |
| 0.18.0 | feat: new mp4ff-wvttlister tool and fuller HEVC support |
Expand Down
14 changes: 9 additions & 5 deletions bits/bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (
// Writer writes bits into underlying io.Writer. Stops writing at first error.
// Errors that have occured can later be checked with Error().
type Writer struct {
n int // current number of bits
v uint // current accumulated value
err error // The first error caused by any write operation
n int // current number of bits
v uint // current accumulated value
err error // The first error caused by any write operation
out []byte // Slice of length 1 to avoid allocation at output

wr io.Writer
}

// NewWriter - returns a new Writer
func NewWriter(w io.Writer) *Writer {
return &Writer{
wr: w,
wr: w,
out: make([]byte, 1),
}
}

Expand All @@ -32,7 +34,9 @@ func (w *Writer) Write(bits uint, n int) {
w.n += n
for w.n >= 8 {
b := (w.v >> (uint(w.n) - 8)) & mask(8)
if err := binary.Write(w.wr, binary.BigEndian, uint8(b)); err != nil {
w.out[0] = uint8(b)
_, err := w.wr.Write(w.out)
if err != nil {
w.err = err
return
}
Expand Down
8 changes: 8 additions & 0 deletions bits/bits_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func BenchmarkWrite(b *testing.B) {
b.Fatal(err)
}
}

func BenchmarkEbspWrite(b *testing.B) {
writer := NewEBSPWriter(ioutil.Discard)
b.ResetTimer()
for i := 0; i < b.N; i++ {
writer.Write(0xff, 8)
}
}
21 changes: 13 additions & 8 deletions bits/ebsp_writer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bits

import (
"encoding/binary"
"io"
)

Expand All @@ -10,18 +9,20 @@ import (
// Stops writing at first error.
// Errors that have occured can later be checked with Error().
type EBSPWriter struct {
n int // current number of bits
v uint // current accumulated value
err error // The first error caused by any write operation
nr0 int // Number preceeding zero bytes
n int // current number of bits
v uint // current accumulated value
err error // The first error caused by any write operation
nr0 int // Number preceeding zero bytes
out []byte // Slice of length 1 to avoid allocation at output

wr io.Writer
}

// NewWriter - returns a new Writer
func NewEBSPWriter(w io.Writer) *EBSPWriter {
return &EBSPWriter{
wr: w,
wr: w,
out: make([]byte, 1),
}
}

Expand All @@ -36,13 +37,17 @@ func (w *EBSPWriter) Write(bits uint, n int) {
for w.n >= 8 {
b := (w.v >> (uint(w.n) - 8)) & mask(8)
if w.nr0 == 2 && b <= 3 {
if err := binary.Write(w.wr, binary.BigEndian, uint8(3)); err != nil {
w.out[0] = 0x3
_, err := w.wr.Write(w.out)
if err != nil {
w.err = err
return
}
w.nr0 = 0
}
if err := binary.Write(w.wr, binary.BigEndian, uint8(b)); err != nil {
w.out[0] = uint8(b)
_, err := w.wr.Write(w.out)
if err != nil {
w.err = err
return
}
Expand Down
44 changes: 44 additions & 0 deletions cmd/mp4ff-fixtrex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"flag"
"log"
"os"

"github.com/edgeware/mp4ff/mp4"
)

func main() {

inFilePath := flag.String("i", "", "Required: Path to input mp4 file")
outFilePath := flag.String("o", "", "Required: Output filepath (without extension)")
flag.Parse()

if *inFilePath == "" || *outFilePath == "" {
flag.Usage()
return
}

ifd, err := os.Open(*inFilePath)
if err != nil {
log.Fatalln(err)
}
defer ifd.Close()
parsedMp4, err := mp4.DecodeFile(ifd)
if err != nil {
log.Fatal(err)
}

parsedMp4.Init.Moov.Mvex.Trex.TrackID = 3

ofd, err := os.Create(*outFilePath)
if err != nil {
log.Fatal(err)
}
defer ofd.Close()

err = parsedMp4.Encode(ofd)
if err != nil {
log.Fatal(err)
}
}
9 changes: 9 additions & 0 deletions cmd/mp4ff-info/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build:
go build -ldflags "-X github.com/edgeware/mp4ff/mp4.commitVersion=$$(git describe --tags HEAD) -X github.com/edgeware/mp4ff/mp4.commitDate=$$(git log -1 --format=%ct)" .

install:
go install -ldflags "-X github.com/edgeware/mp4ff/mp4.commitVersion=$$(git describe --tags HEAD) -X github.com/edgeware/mp4ff/mp4.commitDate=$$(git log -1 --format=%ct)" .

linux-build:
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.commitVersion=$$(git describe --tags HEAD) -X main.commitDate=$$(git log -1 --format=%ct)"

7 changes: 7 additions & 0 deletions cmd/mp4ff-info/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ For some boxes, more details are available by using -l with a comma-separated li
all:1 - level 1 for all boxes
trun:1 - level 1 only for trun box
all:1,trun:0 - level 1 for all boxes but trun
`

var Usage = func() {
Expand All @@ -31,9 +32,15 @@ var Usage = func() {
func main() {

specBoxLevels := flag.String("l", "", "level of details, e.g. all:1 or trun:1,subs:1")
version := flag.Bool("version", false, "Get mp4ff version")

flag.Parse()

if *version {
fmt.Printf("mp4ff-info %s\n", mp4.GetVersion())
os.Exit(0)
}

var inFilePath = flag.Arg(0)
if inFilePath == "" {
Usage()
Expand Down
6 changes: 6 additions & 0 deletions cmd/mp4ff-nallister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ var Usage = func() {
func main() {
maxNrSamples := flag.Int("m", -1, "Max nr of samples to parse")
codec := flag.String("c", "avc", "Codec to parse (avc or hevc)")
version := flag.Bool("version", false, "Get mp4ff version")

flag.Parse()

if *version {
fmt.Printf("mp4ff-nallister %s\n", mp4.GetVersion())
os.Exit(0)
}

var inFilePath = flag.Arg(0)
if inFilePath == "" {
Usage()
Expand Down
6 changes: 6 additions & 0 deletions cmd/mp4ff-pslister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ func main() {
spsHex := flag.String("sps", "", "SPS in hex format")
ppsHex := flag.String("pps", "", "PPS in hex format")
codec := flag.String("c", "avc", "Codec to parse (avc or hevc or auto)")
version := flag.Bool("version", false, "Get mp4ff version")

flag.Parse()

if *version {
fmt.Printf("mp4ff-pslister %s\n", mp4.GetVersion())
os.Exit(0)
}

if *inFile == "" && *spsHex == "" {
Usage("Must specify infile or sps")
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions cmd/mp4ff-wvttlister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ var Usage = func() {
func main() {
maxNrSamples := flag.Int("m", -1, "Max nr of samples to parse")
trackID := flag.Int("t", 0, "trackID to extract (0 is unspecified)")
version := flag.Bool("version", false, "Get mp4ff version")

flag.Parse()

if *version {
fmt.Printf("mp4ff-wvttlister %s\n", mp4.GetVersion())
os.Exit(0)
}

var inFilePath = flag.Arg(0)
if inFilePath == "" {
Usage()
Expand Down
13 changes: 13 additions & 0 deletions mp4/mediasegment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,16 @@ func TestMoofEncrypted(t *testing.T) {
t.Error(err)
}
}

func BenchmarkDecodeEncode(b *testing.B) {
inFile := "testdata/1.m4s"
raw, _ := ioutil.ReadFile(inFile)

for i := 0; i < b.N; i++ {
buf := bytes.NewBuffer(raw)
f, _ := DecodeFile(buf)
var bufInSeg bytes.Buffer
f.EncodeVerbatim = true
_ = f.Encode(&bufInSeg)
}
}
22 changes: 22 additions & 0 deletions mp4/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mp4

import (
"fmt"
"strconv"
"time"
)

var (
commitVersion string = "v0.21.0" // Updated when building using Makefile
commitDate string // commitDate in Epoch seconds (inserted from Makefile)
)

// GetVersion - get version and also commitHash and commitDate if inserted via Makefile
func GetVersion() string {
seconds, _ := strconv.Atoi(commitDate)
if commitDate != "" {
t := time.Unix(int64(seconds), 0)
return fmt.Sprintf("%s, date: %s", commitVersion, t.Format("2006-01-02"))
}
return commitVersion
}

0 comments on commit c20d74c

Please sign in to comment.