mp4-inspector.test.js 35.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
'use strict';
/*
  ======== A Handy Little QUnit Reference ========
  http://api.qunitjs.com/

  Test methods:
  module(name, {[setup][ ,teardown]})
  QUnit.test(name, callback)
  expect(numberOfAssertions)
  stop(increment)
  start(decrement)
  Test assertions:
  assert.ok(value, [message])
  assert.equal(actual, expected, [message])
  notEqual(actual, expected, [message])
  assert.deepEqual(actual, expected, [message])
  notDeepEqual(actual, expected, [message])
  assert.strictEqual(actual, expected, [message])
  notStrictEqual(actual, expected, [message])
  throws(block, [expected], [message])
*/
var
  mp4 = require('../lib/mp4'),
  mp4Helpers = require('./utils/mp4-helpers'),
  QUnit = require('qunit'),
  typeBytes = mp4Helpers.typeBytes,
  box = mp4Helpers.box,
  unityMatrix = mp4Helpers.unityMatrix,

  mvhd0 = box('mvhd',
             0x00, // version 0
             0x00, 0x00, 0x00, // flags
             0x00, 0x00, 0x00, 0x01, // creation_time
             0x00, 0x00, 0x00, 0x02, // modification_time
             0x00, 0x00, 0x00, 0x3c, // timescale
             0x00, 0x00, 0x02, 0x58, // 600 = 0x258 duration
             0x00, 0x01, 0x00, 0x00, // 1.0 rate
             0x01, 0x00, // 1.0 volume
             0x00, 0x00, // reserved
             0x00, 0x00, 0x00, 0x00, // reserved
             0x00, 0x00, 0x00, 0x00, // reserved
             unityMatrix,
             0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, // pre_defined
             0x00, 0x00, 0x00, 0x02),

  tkhd0 = box('tkhd',
              0x00, // version 0
              0x00, 0x00, 0x00, // flags
              0x00, 0x00, 0x00, 0x02, // creation_time
              0x00, 0x00, 0x00, 0x03, // modification_time
              0x00, 0x00, 0x00, 0x01, // track_ID
              0x00, 0x00, 0x00, 0x00, // reserved
              0x00, 0x00, 0x02, 0x58, // 600 = 0x258 duration
              0x00, 0x00, 0x00, 0x00,
              0x00, 0x00, 0x00, 0x00, // reserved
              0x00, 0x00, // layer
              0x00, 0x00, // alternate_group
              0x00, 0x00, // non-audio track volume
              0x00, 0x00, // reserved
              unityMatrix,
              0x01, 0x2c, 0x80, 0x00, // 300.5 in 16.16 fixed point
              0x00, 0x96, 0x80, 0x00), // 150.5 in 16.16 fixed point
  mdhd0 = box('mdhd',
              0x00, // version 0
              0x00, 0x00, 0x00, // flags
              0x00, 0x00, 0x00, 0x02, // creation_time
              0x00, 0x00, 0x00, 0x03, // modification_time
              0x00, 0x00, 0x00, 0x3c, // timescale
              0x00, 0x00, 0x02, 0x58, // 600 = 0x258 duration
              0x15, 0xc7, // 'eng' language
              0x00, 0x00);

QUnit.module('MP4 Inspector');

QUnit.test('produces an empty array for empty input', function(assert) {
  assert.strictEqual(mp4.tools.inspect(new Uint8Array([])).length, 0, 'returned an empty array');
});

QUnit.test('can parse a Box', function(assert) {
  var box = new Uint8Array([
    0x00, 0x00, 0x00, 0x00, // size 0
    0x00, 0x00, 0x00, 0x00 // boxtype 0
  ]);
  assert.deepEqual(mp4.tools.inspect(box), [{
    type: '\u0000\u0000\u0000\u0000',
    size: 0,
    data: box.subarray(box.byteLength)
  }], 'parsed a Box');
});

QUnit.test('can parse an ftyp', function(assert) {
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(box('ftyp',
    0x61, 0x76, 0x63, 0x31, // major brand
    0x00, 0x00, 0x00, 0x02, // minor version
    98, 111, 111, 112, // compatible brands
    98, 101, 101, 112 // compatible brands
  ))), [{
    type: 'ftyp',
    size: 4 * 6,
    majorBrand: 'avc1',
    minorVersion: 2,
    compatibleBrands: ['boop', 'beep']
  }], 'parsed an ftyp');
});

QUnit.test('can parse a pdin', function(assert) {
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(box('pdin',
    0x01, // version 1
    0x01, 0x02, 0x03, // flags
    0x00, 0x00, 0x04, 0x00, // 1024 = 0x400 bytes/second rate
    0x00, 0x00, 0x00, 0x01 // initial delay
  ))), [{
    size: 20,
    type: 'pdin',
    version: 1,
    flags: new Uint8Array([1, 2, 3]),
    rate: 1024,
    initialDelay: 1
  }], 'parsed a pdin');
});

QUnit.test('can parse an mdat', function(assert) {
  var mdat = new Uint8Array(box('mdat',
      0, 0, 0, 4, // length
      0x01, 0x02, 0x03, 0x04 // data
    ));
  assert.deepEqual(mp4.tools.inspect(mdat), [{
    size: 16,
    type: 'mdat',
    nals: [
      'slice_layer_without_partitioning_rbsp'
    ],
    byteLength: 8
  }], 'parsed an mdat');
});

QUnit.test('can parse a free or skip', function(assert) {
  var
    free = new Uint8Array(box('free',
                              0x01, 0x02, 0x03, 0x04)), // data
    skip = new Uint8Array(box('skip',
                              0x01, 0x02, 0x03, 0x04)); // data

  assert.deepEqual(mp4.tools.inspect(free), [{
      size: 12,
      type: 'free',
      data: free.subarray(free.byteLength - 4)
    }], 'parsed a free');
  assert.deepEqual(mp4.tools.inspect(skip), [{
      size: 12,
      type: 'skip',
      data: skip.subarray(skip.byteLength - 4)
    }], 'parsed a skip');
});

QUnit.test('can parse a version 0 mvhd', function(assert) {
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(mvhd0)), [{
    type: 'mvhd',
    version: 0,
    flags: new Uint8Array([0, 0, 0]),
    creationTime: new Date(1000 - 2082844800000),
    modificationTime: new Date(2000 - 2082844800000),
    timescale: 60,
    duration: 600,
    rate: 1,
    volume: 1,
    matrix: new Uint32Array(unityMatrix),
    size: 108,
    nextTrackId: 2
  }]);
});

QUnit.test('can parse a version 0 tkhd', function(assert) {
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(tkhd0)), [{
    type: 'tkhd',
    version: 0,
    flags: new Uint8Array([0, 0, 0]),
    creationTime: new Date(2000 - 2082844800000),
    modificationTime: new Date(3000 - 2082844800000),
    size: 92,
    trackId: 1,
    duration: 600,
    layer: 0,
    alternateGroup: 0,
    volume: 0,
    matrix: new Uint32Array(unityMatrix),
    width: 300.5,
    height: 150.5
  }]);
});

QUnit.test('can parse a version 0 mdhd', function(assert) {
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(mdhd0)), [{
    type: 'mdhd',
    version: 0,
    flags: new Uint8Array([0, 0, 0]),
    creationTime: new Date(2000 - 2082844800000),
    modificationTime: new Date(3000 - 2082844800000),
    size: 32,
    timescale: 60,
    duration: 600,
    language: 'eng'
  }]);
});

QUnit.test('can parse a moov', function(assert) {
  var data = mp4Helpers.sampleMoov;

  QUnit.dump.maxDepth = 100;
  var result = QUnit.dump.parse(mp4.tools.inspect(new Uint8Array(data)));
  var expected = QUnit.dump.parse([{
    type: 'moov',
    size: 1129,
    boxes: [{
      type: 'mvhd',
      version: 1,
      flags: new Uint8Array([0, 0, 0]),
      creationTime: new Date(1000 - 2082844800000),
      modificationTime: new Date(2000 - 2082844800000),
      timescale: 1000,
      duration: 600,
      rate: 1,
      size: 120,
      volume: 1,
      matrix: new Uint32Array(unityMatrix),
      nextTrackId: 2
    }, {
      type: 'trak',
      size: 519,
      boxes: [{
        type: 'tkhd',
        flags: new Uint8Array([0, 0, 0]),
        version: 1,
        creationTime: new Date(2000 - 2082844800000),
        modificationTime: new Date(3000 - 2082844800000),
        size: 104,
        trackId: 1,
        duration: 600,
        layer: 0,
        alternateGroup: 0,
        volume: 0,
        matrix: new Uint32Array(unityMatrix),
        width: 300,
        height: 150
      }, {
        type: 'edts',
        size: 36,
        boxes: [{
          type: 'elst',
          size: 28,
          version: 0,
          flags: new Uint8Array([0, 0, 0]),
          edits: [{
            segmentDuration: 0,
            mediaTime: 1024,
            mediaRate: 1.5
          }]
        }]
      }, {
        type: 'mdia',
        size: 371,
        boxes: [{
          type: 'mdhd',
          version: 1,
          flags: new Uint8Array([0, 0, 0]),
          creationTime: new Date(2000 - 2082844800000),
          modificationTime: new Date(3000 - 2082844800000),
          timescale: 90e3,
          duration: 600,
          language: 'eng',
          size: 44
        }, {
          type: 'hdlr',
          version: 1,
          flags: new Uint8Array([0, 0, 0]),
          handlerType: 'vide',
          name: 'one',
          size: 37
        }, {
          type: 'minf',
          size: 282,
          boxes: [{
            type: 'dinf',
            size: 36,
            boxes: [{
              type: 'dref',
              size: 28,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              dataReferences: [{
                type: 'url ',
                size: 12,
                version: 0,
                flags: new Uint8Array([0, 0, 1])
              }]
            }]
          }, {
            type: 'stbl',
            size: 238,
            boxes: [{
              type: 'stsd',
              size: 114,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              sampleDescriptions: [{
                config: [
                  {
                    avcLevelIndication: 13,
                    avcProfileIndication: 77,
                    configurationVersion: 0,
                    lengthSizeMinusOne: 0,
                    pps: [],
                    profileCompatibility: 64,
                    size: 0,
                    sps: [],
                    type: 'avcC'
                  }
                ],
                dataReferenceIndex: 0,
                depth: 0,
                frameCount: 0,
                height: 0,
                horizresolution: 0,
                size: 98,
                type: 'avc1',
                vertresolution: 0,
                width: 0
              }
              ]
            }, {
              type: 'stts',
              size: 24,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              timeToSamples: [{
                sampleCount: 1,
                sampleDelta: 1
              }]
            }, {
              type: 'stsc',
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              sampleToChunks: [{
                firstChunk: 2,
                samplesPerChunk: 3,
                sampleDescriptionIndex: 1
              }],
              size: 28
            }, {
              type: 'stco',
              size: 20,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              chunkOffsets: [1]
            }, {
              type: 'stss',
              size: 20,
              version: 0,
              flags: new Uint8Array([0, 0, 0]),
              syncSamples: [1]
            }, {
              type: 'ctts',
              size: 24,
              version: 0,
              flags: new Uint8Array([0, 0, 0]),
              compositionOffsets: [
                { sampleCount: 1, sampleOffset: 1 }
              ]
            }]
          }]
        }]
      }]
    }, {
      type: 'trak',
      size: 482,
      boxes: [{
        type: 'tkhd',
        flags: new Uint8Array([0, 0, 0]),
        version: 1,
        creationTime: new Date(2000 - 2082844800000),
        modificationTime: new Date(3000 - 2082844800000),
        size: 104,
        trackId: 2,
        duration: 600,
        layer: 0,
        alternateGroup: 0,
        volume: 0,
        matrix: new Uint32Array(unityMatrix),
        width: 300,
        height: 150
      }, {
        type: 'edts',
        size: 44,
        boxes: [{
          type: 'elst',
          size: 36,
          version: 1,
          flags: new Uint8Array([0, 0, 0]),
          edits: [{
            segmentDuration: 0,
            mediaTime: 1152921504606847000,
            mediaRate: 1.5
          }]
        }]
      }, {
        type: 'mdia',
        size: 326,
        boxes: [{
          type: 'mdhd',
          version: 1,
          flags: new Uint8Array([0, 0, 0]),
          creationTime: new Date(2000 - 2082844800000),
          modificationTime: new Date(3000 - 2082844800000),
          timescale: 90e3,
          duration: 600,
          language: 'eng',
          size: 44
        }, {
          type: 'hdlr',
          version: 1,
          flags: new Uint8Array([0, 0, 0]),
          handlerType: 'soun',
          name: 'one',
          size: 37
        }, {
          type: 'minf',
          size: 237,
          boxes: [{
            type: 'dinf',
            size: 36,
            boxes: [{
              type: 'dref',
              size: 28,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              dataReferences: [{
                type: 'url ',
                size: 12,
                version: 0,
                flags: new Uint8Array([0, 0, 1])
              }]
            }]
          }, {
            type: 'stbl',
            size: 193,
            boxes: [{
              type: 'stsd',
              size: 89,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              sampleDescriptions: [{
                channelcount: 0,
                dataReferenceIndex: 0,
                samplerate: 0,
                samplesize: 0,
                size: 73,
                streamDescriptor: {
                  decoderConfig: {
                    avgBitrate: 0,
                    bufferSize: 0,
                    decoderConfigDescriptor: {
                      audioObjectType: 0,
                      channelConfiguration: 0,
                      length: 0,
                      samplingFrequencyIndex: 0,
                      tag: 0
                    },
                    maxBitrate: 0,
                    objectProfileIndication: 64,
                    streamType: 2
                  },
                  esId: 0,
                  flags: {
                    0: 0,
                    1: 0,
                    2: 0
                  },
                  size: 0,
                  streamPriority: 0,
                  type: 'esds',
                  version: 0
                },
                type: 'mp4a'
              }]
            }, {
              type: 'stts',
              size: 24,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              timeToSamples: [{
                sampleCount: 1,
                sampleDelta: 1
              }]
            }, {
              type: 'stsc',
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              sampleToChunks: [{
                firstChunk: 2,
                samplesPerChunk: 3,
                sampleDescriptionIndex: 1
              }],
              size: 28
            }, {
              type: 'ctts',
              size: 24,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              compositionOffsets: [
                { sampleCount: 1, sampleOffset: -1 }
              ]
            }, {
              type: 'stco',
              size: 20,
              version: 1,
              flags: new Uint8Array([0, 0, 0]),
              chunkOffsets: [1]
            }]
          }]
        }]
      }]
    }]
  }]);
  assert.equal(result, expected, 'can parse moov');
});

QUnit.test('can parse an mvex', function(assert) {
  var mvex =
    box('mvex',
        box('trex',
            0x00, // version
            0x00, 0x00, 0x00, // flags
            0x00, 0x00, 0x00, 0x01, // track_ID
            0x00, 0x00, 0x00, 0x01, // default_sample_description_index
            0x00, 0x00, 0x00, 0x02, // default_sample_duration
            0x00, 0x00, 0x00, 0x03, // default_sample_size
            0x00, 0x61, 0x00, 0x01)); // default_sample_flags
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(mvex)), [{
    type: 'mvex',
    size: 40,
    boxes: [{
      type: 'trex',
      size: 32,
      version: 0,
      flags: new Uint8Array([0, 0, 0]),
      trackId: 1,
      defaultSampleDescriptionIndex: 1,
      defaultSampleDuration: 2,
      defaultSampleSize: 3,
      sampleDependsOn: 0,
      sampleIsDependedOn: 1,
      sampleHasRedundancy: 2,
      samplePaddingValue: 0,
      sampleIsDifferenceSample: true,
      sampleDegradationPriority: 1
    }]
  }], 'parsed an mvex');
});

QUnit.test('can parse a video stsd', function(assert) {
  var data = box('stsd',
                 0x00, // version 0
                 0x00, 0x00, 0x00, // flags
                 0x00, 0x00, 0x00, 0x01,
                 box('avc1',
                     0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, // reserved
                     0x00, 0x01, // data_reference_index
                     0x00, 0x00, // pre_defined
                     0x00, 0x00, // reserved
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00, // pre_defined
                     0x01, 0x2c, // width = 300
                     0x00, 0x96, // height = 150
                     0x00, 0x48, 0x00, 0x00, // horizresolution
                     0x00, 0x48, 0x00, 0x00, // vertresolution
                     0x00, 0x00, 0x00, 0x00, // reserved
                     0x00, 0x01, // frame_count
                     0x04,
                     typeBytes('avc1'),
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, // compressorname
                     0x00, 0x18, // depth = 24
                     0x11, 0x11, // pre_defined
                     box('avcC',
                        0x01, // configurationVersion
                        0x00, // AVCProfileIndication??
                        0x00, // profile_compatibility
                        0x00, // AVCLevelIndication
                        0x1c, // lengthSizeMinusOne
                        0xe1, // numOfSequenceParameterSets
                        0x00, 0x01, // sequenceParameterSetLength
                        0x00, // "SPS"
                        0x02, // numOfPictureParameterSets
                        0x00, 0x02, // pictureParameterSetLength
                        0x01, 0x02, // "PPS"
                        0x00, 0x01, // pictureParameterSetLength
                        0xff), // "PPS"
                     box('btrt',
                        0x00, 0x00, 0x00, 0x00, // bufferSizeDB
                        0x00, 0x00, 0x00, 0x01, // maxBitrate
                        0x00, 0x00, 0x00, 0x01), // avgBitrate
                     box('pasp',
                        0x00, 0x00, 0x00, 0x01, // hSpacing
                        0x00, 0x00, 0x00, 0x01))); // vSpacing
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)), [{
    type: 'stsd',
    size: 163,
    version: 0,
    flags: new Uint8Array([0, 0, 0]),
    sampleDescriptions: [{
      type: 'avc1',
      size: 147,
      dataReferenceIndex: 1,
      width: 300,
      height: 150,
      horizresolution: 72,
      vertresolution: 72,
      frameCount: 1,
      depth: 24,
      config: [{
        type: 'avcC',
        size: 25,
        configurationVersion: 1,
        avcProfileIndication: 0,
        profileCompatibility: 0,
        avcLevelIndication: 0,
        lengthSizeMinusOne: 0,
        sps: [new Uint8Array(1)],
        pps: [new Uint8Array([1, 2]),
              new Uint8Array([0xff])]
      }, {
        type: 'btrt',
        size: 20,
        bufferSizeDB: 0,
        maxBitrate: 1,
        avgBitrate: 1
      }, {
        type: 'pasp',
        size: 16,
        data: new Uint8Array([0, 0, 0, 1, 0, 0, 0, 1])
      }]
    }]
  }]);
});

QUnit.test('can parse an audio stsd', function(assert) {
  var data = box('stsd',
                 0x00,                         // version 0
                 0x00, 0x00, 0x00,             // flags
                 0x00, 0x00, 0x00, 0x01,       // entry_count
                 box('mp4a',
                     0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00,         // reserved
                     0x00, 0x01,               // data_reference_index
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,   // reserved
                     0x00, 0x02,               // channelcount
                     0x00, 0x10,               // samplesize
                     0x00, 0x00,               // pre_defined
                     0x00, 0x00,               // reserved
                     0xbb, 0x80, 0x00, 0x00, // samplerate, fixed-point 16.16
                     box('esds',
                         0x00, // version 0
                         0x00, 0x00, 0x00, // flags
                         0x03, // tag, ES_DescrTag
                         0x00, // length
                         0x00, 0x01, // ES_ID
                         0x00, // streamDependenceFlag, URL_Flag, reserved, streamPriority

                         // DecoderConfigDescriptor
                         0x04, // tag, DecoderConfigDescrTag
                         0x0d, // length
                         0x40, // objectProfileIndication, AAC Main
                         0x15, // streamType, AudioStream. upstream, reserved
                         0x00, 0x00, 0xff, // bufferSizeDB
                         0x00, 0x00, 0x00, 0xff, // maxBitrate
                         0x00, 0x00, 0x00, 0xaa, // avgBitrate

                         // DecoderSpecificInfo
                         0x05, // tag, DecoderSpecificInfoTag
                         0x02, // length
                         // audioObjectType, samplingFrequencyIndex, channelConfiguration
                         0x11, 0x90,
                         // GASpecificConfig
                         0x06, 0x01, 0x02)));

  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)), [{
    version: 0,
    flags: new Uint8Array([0, 0, 0]),
    type: 'stsd',
    size: 91,
    sampleDescriptions: [{
      type: 'mp4a',
      dataReferenceIndex: 1,
      channelcount: 2,
      samplesize: 16,
      samplerate: 48000,
      size: 75,
      streamDescriptor: {
        type: 'esds',
        version: 0,
        size: 39,
        flags: new Uint8Array([0, 0, 0]),
        esId: 1,
        streamPriority: 0,
        decoderConfig: {
          objectProfileIndication: 0x40,
          streamType: 0x05,
          bufferSize: 0xff,
          maxBitrate: 0xff,
          avgBitrate: 0xaa,
          decoderConfigDescriptor: {
            tag: 5,
            length: 2,
            audioObjectType: 2,
            samplingFrequencyIndex: 3,
            channelConfiguration: 2
          }
        }
      }
    }]
  }], 'parsed an audio stsd');
});

QUnit.test('can parse an styp', function(assert) {
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(box('styp',
    0x61, 0x76, 0x63, 0x31, // major brand
    0x00, 0x00, 0x00, 0x02, // minor version
    98, 111, 111, 112 // compatible brands
  ))), [{
    type: 'styp',
    size: 4 * 5,
    majorBrand: 'avc1',
    minorVersion: 2,
    compatibleBrands: ['boop']
  }], 'parsed an styp');
});

QUnit.test('can parse a vmhd', function(assert) {
  var data = box('vmhd',
                 0x00, // version
                 0x00, 0x00, 0x00, // flags
                 0x00, 0x00, // graphicsmode
                 0x00, 0x00,
                 0x00, 0x00,
                 0x00, 0x00); // opcolor

  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'vmhd',
              size: 20,
              version: 0,
              flags: new Uint8Array([0, 0, 0]),
              graphicsmode: 0,
              opcolor: new Uint16Array([0, 0, 0])
            }]);
});

QUnit.test('can parse an stsz', function(assert) {
  var data = box('stsz',
                 0x00, // version
                 0x00, 0x00, 0x00, // flags
                 0x00, 0x00, 0x00, 0x00, // sample_size
                 0x00, 0x00, 0x00, 0x03, // sample_count
                 0x00, 0x00, 0x00, 0x01, // entry_size
                 0x00, 0x00, 0x00, 0x02, // entry_size
                 0x00, 0x00, 0x00, 0x03); // entry_size
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'stsz',
              size: 32,
              version: 0,
              flags: new Uint8Array([0, 0, 0]),
              sampleSize: 0,
              entries: [1, 2, 3]
            }]);
});

QUnit.test('can parse a moof', function(assert) {
  var data = box('moof',
                 box('mfhd',
                     0x00, // version
                     0x00, 0x00, 0x00, // flags
                     0x00, 0x00, 0x00, 0x04), // sequence_number
                 box('traf',
                     box('tfhd',
                        0x00, // version
                        0x00, 0x00, 0x3b, // flags
                        0x00, 0x00, 0x00, 0x01, // track_ID
                        0x00, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x00, 0x01, // base_data_offset
                        0x00, 0x00, 0x00, 0x02, // sample_description_index
                        0x00, 0x00, 0x00, 0x03, // default_sample_duration,
                        0x00, 0x00, 0x00, 0x04, // default_sample_size
                        0x00, 0x00, 0x00, 0x05))); // default_sample_flags
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'moof',
              size: 72,
              boxes: [{
                type: 'mfhd',
                size: 16,
                version: 0,
                flags: new Uint8Array([0, 0, 0]),
                sequenceNumber: 4
              },
              {
                type: 'traf',
                size: 48,
                boxes: [{
                  type: 'tfhd',
                  version: 0,
                  size: 40,
                  flags: new Uint8Array([0x00, 0, 0x3b]),
                  trackId: 1,
                  baseDataOffset: 1,
                  sampleDescriptionIndex: 2,
                  defaultSampleDuration: 3,
                  defaultSampleSize: 4,
                  defaultSampleFlags: 5
                }]
              }]
            }]);
});

QUnit.test('can parse a trun', function(assert) {
  var data = box('trun',
                 0x00, // version
                 0x00, 0x0b, 0x05, // flags
                 0x00, 0x00, 0x00, 0x02, // sample_count
                 0x00, 0x00, 0x00, 0x01, // data_offset
                 // first_sample_flags
                 // r:0000 il:10 sdo:01 sido:10 shr:01 spv:111 snss:1
                 // dp:1111 1110 1101 1100
                 0x09, 0x9f, 0xfe, 0xdc,

                 0x00, 0x00, 0x00, 0x09, // sample_duration
                 0x00, 0x00, 0x00, 0xff, // sample_size
                 0x00, 0x00, 0x00, 0x00, // sample_composition_time_offset

                 0x00, 0x00, 0x00, 0x08, // sample_duration
                 0x00, 0x00, 0x00, 0xfe, // sample_size
                 0x00, 0x00, 0x00, 0x00); // sample_composition_time_offset
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'trun',
              version: 0,
              size: 48,
              flags: new Uint8Array([0, 0x0b, 0x05]),
              dataOffset: 1,
              samples: [{
                duration: 9,
                size: 0xff,
                flags: {
                  isLeading: 2,
                  dependsOn: 1,
                  isDependedOn: 2,
                  hasRedundancy: 1,
                  paddingValue: 7,
                  isNonSyncSample: 1,
                  degradationPriority: 0xfedc
                },
                compositionTimeOffset: 0
              }, {
                duration: 8,
                size: 0xfe,
                compositionTimeOffset: 0
              }]
            }]);
});

QUnit.test('can parse a trun with per-sample flags', function(assert) {
  var data = box('trun',
                 0x00, // version
                 0x00, 0x0f, 0x00, // flags
                 0x00, 0x00, 0x00, 0x01, // sample_count

                 0x00, 0x00, 0x00, 0x09, // sample_duration
                 0x00, 0x00, 0x00, 0xff, // sample_size
                 // sample_flags
                 // r:0000 il:00 sdo:01, sido:11 shr:00 spv:010 snss:0
                 // dp: 0001 0010 0011 0100
                 0x01, 0xc4, 0x12, 0x34,
                 0x00, 0x00, 0x00, 0x00); // sample_composition_time_offset
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'trun',
              version: 0,
              size: 32,
              flags: new Uint8Array([0, 0x0f, 0x00]),
              samples: [{
                duration: 9,
                size: 0xff,
                flags: {
                  isLeading: 0,
                  dependsOn: 1,
                  isDependedOn: 3,
                  hasRedundancy: 0,
                  paddingValue: 2,
                  isNonSyncSample: 0,
                  degradationPriority: 0x1234
                },
                compositionTimeOffset: 0
              }]
            }]);

});

QUnit.test('can parse an sdtp', function(assert) {
  var data = box('sdtp',
                 0x00, // version
                 0x00, 0x00, 0x00, // flags
                 // reserved + sample_depends_on +
                 // sample_is_dependend_on + sample_has_redundancy
                 0x15,
                 // reserved + sample_depends_on +
                 // sample_is_dependend_on + sample_has_redundancy
                 0x27);
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)), [{
    type: 'sdtp',
    version: 0,
    flags: new Uint8Array([0, 0, 0]),
    size: 14,
    samples: [{
      dependsOn: 1,
      isDependedOn: 1,
      hasRedundancy: 1
    }, {
      dependsOn: 2,
      isDependedOn: 1,
      hasRedundancy: 3
    }]
  }]);
});

QUnit.test('can parse a sidx', function(assert) {
  var data = box('sidx',
                 0x00, // version
                 0x00, 0x00, 0x00, // flags
                 0x00, 0x00, 0x00, 0x02, // reference_ID
                 0x00, 0x00, 0x00, 0x01, // timescale
                 0x01, 0x02, 0x03, 0x04, // earliest_presentation_time
                 0x00, 0x00, 0x00, 0x00, // first_offset
                 0x00, 0x00,             // reserved
                 0x00, 0x02,             // reference_count
                 // first reference
                 0x80, 0x00, 0x00, 0x07, // reference_type(1) + referenced_size(31)
                 0x00, 0x00, 0x00, 0x08, // subsegment_duration
                 0x80, 0x00, 0x00, 0x09, // starts_with_SAP(1) + SAP_type(3) + SAP_delta_time(28)
                 // second reference
                 0x00, 0x00, 0x00, 0x03, // reference_type(1) + referenced_size(31)
                 0x00, 0x00, 0x00, 0x04, // subsegment_duration
                 0x10, 0x00, 0x00, 0x05  // starts_with_SAP(1) + SAP_type(3) + SAP_delta_time(28)
                );
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'sidx',
              version: 0,
              flags: new Uint8Array([0, 0x00, 0x00]),
              timescale: 1,
              earliestPresentationTime: 0x01020304,
              firstOffset: 0,
              referenceId: 2,
              size: 56,
              references: [{
                referenceType: 1,
                referencedSize: 7,
                subsegmentDuration: 8,
                startsWithSap: true,
                sapType: 0,
                sapDeltaTime: 9
                }, {
                referenceType: 0,
                referencedSize: 3,
                subsegmentDuration: 4,
                startsWithSap: false,
                sapType: 1,
                sapDeltaTime: 5
                }]

            }]);
});

QUnit.test('can parse a version 1 sidx', function(assert) {
  var data = box('sidx',
                  0x01, // version
                  0x00, 0x00, 0x00, // flags
                  0x00, 0x00, 0x00, 0x02, // reference_ID
                  0x00, 0x00, 0x00, 0x01, // timescale
                  0x00, 0x00, 0x00, 0x00, // earliest_presentation_time
                  0x01, 0x02, 0x03, 0x04,
                  0x00, 0x00, 0x00, 0x00, // first_offset
                  0x00, 0x00, 0x00, 0x00,
                  0x00, 0x00,             // reserved
                  0x00, 0x02,             // reference_count
                  // first reference
                  0x80, 0x00, 0x00, 0x07, // reference_type(1) + referenced_size(31)
                  0x00, 0x00, 0x00, 0x08, // subsegment_duration
                  0x80, 0x00, 0x00, 0x09, // starts_with_SAP(1) + SAP_type(3) + SAP_delta_time(28)
                  // second reference
                  0x00, 0x00, 0x00, 0x03, // reference_type(1) + referenced_size(31)
                  0x00, 0x00, 0x00, 0x04, // subsegment_duration
                  0x10, 0x00, 0x00, 0x05  // starts_with_SAP(1) + SAP_type(3) + SAP_delta_time(28)
  );
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
    [{
      type: 'sidx',
      version: 1,
      flags: new Uint8Array([0, 0x00, 0x00]),
      timescale: 1,
      earliestPresentationTime: 0x01020304,
      firstOffset: 0,
      referenceId: 2,
      size: 64,
      references: [{
        referenceType: 1,
        referencedSize: 7,
        subsegmentDuration: 8,
        startsWithSap: true,
        sapType: 0,
        sapDeltaTime: 9
      }, {
        referenceType: 0,
        referencedSize: 3,
        subsegmentDuration: 4,
        startsWithSap: false,
        sapType: 1,
        sapDeltaTime: 5
      }]

    }]);
});

QUnit.test('can parse an smhd', function(assert) {
  var data = box('smhd',
                 0x00,             // version
                 0x00, 0x00, 0x00, // flags
                 0x00, 0xff,       // balance, fixed-point 8.8
                 0x00, 0x00);      // reserved

  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'smhd',
              size: 16,
              version: 0,
              flags: new Uint8Array([0, 0, 0]),
              balance: 0xff / Math.pow(2, 8)
            }],
            'parsed an smhd');
});

QUnit.test('can parse a version 0 tfdt', function(assert) {
  var data = box('tfdt',
                 0x00, // version
                 0x00, 0x00, 0x00, // flags
                 0x01, 0x02, 0x03, 0x04); // baseMediaDecodeTime
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'tfdt',
              version: 0,
              size: 16,
              flags: new Uint8Array([0, 0, 0]),
              baseMediaDecodeTime: 0x01020304
            }]);
});

QUnit.test('can parse a version 1 tfdt and return an unsigned integer value', function(assert) {
  var data = box('tfdt',
                 0x01, // version
                 0x00, 0x00, 0x00, // flags
                 0x81, 0x02, 0x03, 0x04,
                 0x05, 0x06, 0x07, 0x08); // baseMediaDecodeTime
  assert.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
            [{
              type: 'tfdt',
              version: 1,
              size: 20,
              flags: new Uint8Array([0, 0, 0]),
              baseMediaDecodeTime: 0x8102030405060708
            }]);
});

QUnit.test('can parse a series of boxes', function(assert) {
  var ftyp = [
    0x00, 0x00, 0x00, 0x18 // size 4 * 6 = 24
  ].concat(typeBytes('ftyp')).concat([
    0x69, 0x73, 0x6f, 0x6d, // major brand
    0x00, 0x00, 0x00, 0x02, // minor version
    98, 101, 101, 112, // compatible brands
    98, 111, 111, 112  // compatible brands
  ]);

  assert.deepEqual(mp4.tools.inspect(new Uint8Array(ftyp.concat(ftyp))),
            [{
              type: 'ftyp',
              size: 4 * 6,
              majorBrand: 'isom',
              minorVersion: 2,
              compatibleBrands: ['beep', 'boop']
            }, {
              type: 'ftyp',
              size: 4 * 6,
              majorBrand: 'isom',
              minorVersion: 2,
              compatibleBrands: ['beep', 'boop']
            }],
            'parsed two boxes in series');

});