Skip to content

Commit

Permalink
funcs: Make intdiv truncate to int
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 48517c7 commit 962d84d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
9 changes: 8 additions & 1 deletion format/mp4/mp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,14 @@ func mp4Decode(d *decode.D, in interface{}) interface{} {
sampleSize := t.stsz[sampleNr]
decodeSampleRange(d, t, trackSdDataFormat, "sample", int64(sampleOffset)*8, int64(sampleSize)*8, t.decodeOpts...)

// log.Printf("%s %d/%d %d/%d sample=%d/%d chunk=%d size=%d %d-%d\n", t.dataFormat, stscIndex, len(t.stsc), i, stscEntry.samplesPerChunk, sampleNr, len(t.stsz), chunkNr, sampleSize, sampleOffset, sampleOffset+uint64(sampleSize))
// log.Printf("%s %d/%d %d/%d sample=%d/%d chunk=%d size=%d %d-%d\n",
// trackSdDataFormat, stscIndex, len(t.stsc),
// i, stscEntry.samplesPerChunk,
// sampleNr, len(t.stsz),
// chunkNr,
// sampleSize,
// sampleOffset,
// sampleOffset+uint64(sampleSize))

sampleOffset += uint64(sampleSize)
sampleNr++
Expand Down
12 changes: 9 additions & 3 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ def decode($name; $opts): _decode($name; $opts);
def decode($name): _decode($name; {});
def decode: _decode("probe"; {});


# TODO: figure out a saner way to force int
def _to_int: (. % (. + 1));

# integer division
# inspried by https://github.com/itchyny/gojq/issues/63#issuecomment-765066351
def intdiv($a; $b): ($a - ($a % $b)) / $b;
def intdiv($a; $b):
( ($a | _to_int) as $a
| ($b | _to_int) as $b
| ($a - ($a % $b)) / $b
);

# valid jq identifier, start with alpha or underscore then zero or more alpha, num or underscore
def _is_ident: type == "string" and test("^[a-zA-Z_][a-zA-Z_0-9]*$");
Expand Down Expand Up @@ -204,8 +212,6 @@ def table(colmap; render):

# convert number to array of bytes
def number_to_bytes($bits):
# TODO: figure out a saner way to force int
def _to_int: (. % (. + 1));
def _number_to_bytes($d):
if . > 0 then
. % $d, (intdiv(.; $d) | _number_to_bytes($d))
Expand Down
29 changes: 29 additions & 0 deletions pkg/interp/funcs_test.jq
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,33 @@ include "funcs";
["1234", 2, ["12","34"]],
["1234", 3, ["123","4"]]
][] | . as $t | assert("\($t[0]) | chunk(\($t[1]))"; $t[2]; $t[0] | chunk($t[1])))
,
([
# 0xfffffffffffffffffffffffffffffffffffffffffffff
[1532495540865888858358347027150309183618739122183602175, 8, [
15,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255
]]
][] | . as $t | assert("\($t[0]) | number_to_bytes(\($t[1]))"; $t[2]; $t[0] | number_to_bytes($t[1])))
)

0 comments on commit 962d84d

Please sign in to comment.