Skip to content

Commit

Permalink
Matroska capture misc fixes
Browse files Browse the repository at this point in the history
seek position and duration and version and avi width and avi imagesize
  • Loading branch information
JeromeMartinez committed Dec 1, 2023
1 parent fce9ef6 commit b62710b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Source/Common/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ enum return_value
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#define Program_Version "0.21.11"
#define Program_Version "22.12"
//---------------------------------------------------------------------------
59 changes: 38 additions & 21 deletions Source/Common/Output_Mkv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,35 @@

#include "Common/Output_Mkv.h"
#include "Common/Merge.h"
#include "Common/Config.h"

#include <iostream>

using namespace std;

static const unsigned char matroska_writer_header[] = { 0x1A, 0x45, 0xDF, 0xA3, 0xA3, 0x42, 0x86, 0x81, 0x01, 0x42, 0xF7, 0x81, 0x01, 0x42, 0xF2, 0x81, 0x04, 0x42, 0xF3, 0x81, 0x08, 0x42, 0x82, 0x88, 0x6D, 0x61, 0x74, 0x72, 0x6F, 0x73, 0x6B, 0x61, 0x42, 0x87, 0x81, 0x04, 0x42, 0x85, 0x81, 0x02, 0x18, 0x53, 0x80, 0x67, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x11, 0x4D, 0x9B, 0x74, 0xC6, 0xEC, 0x84, 0x00, 0x00, 0x00, 0x00, 0x4D, 0xBB, 0x8B, 0x53, 0xAB, 0x84, 0x15, 0x49, 0xA9, 0x66, 0x53, 0xAC, 0x81, 0xA1, 0x4D, 0xBB, 0x8B, 0x53, 0xAB, 0x84, 0x16, 0x54, 0xAE, 0x6B, 0x53, 0xAC, 0x81, 0xEF, 0x4D, 0xBB, 0x8C, 0x53, 0xAB, 0x84, 0x12, 0x54, 0xC3, 0x67, 0x53, 0xAC, 0x82, 0x01, 0xD1, 0xEC, 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x49, 0xA9, 0x66, 0xC9, 0xEC, 0x84, 0x00, 0x00, 0x00, 0x00, 0x2A, 0xD7, 0xB1, 0x83, 0x0F, 0x42, 0x40, 0x4D, 0x80, 0x8C, 0x64, 0x76, 0x72, 0x65, 0x73, 0x63, 0x75, 0x65, 0x30, 0x2E, 0x30, 0x30, 0x57, 0x41, 0x8C, 0x64, 0x76, 0x72, 0x65, 0x73, 0x63, 0x75, 0x65, 0x30, 0x2E, 0x30, 0x30, 0xEC, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x54, 0xAE, 0x6B, 0x41, 0x14, 0xEC, 0x84, 0x00, 0x00, 0x00, 0x00, 0xAE, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0xD7, 0x81, 0x01, 0x73, 0xC5, 0x81, 0x01, 0xEC, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x81, 0x00, 0x22, 0xB5, 0x9C, 0x83, 0x75, 0x6E, 0x64, 0x83, 0x81, 0x01, 0x23, 0xE3, 0x83, 0x84, 0x01, 0xFD, 0x22, 0x8A, 0x86, 0x8F, 0x56, 0x5F, 0x4D, 0x53, 0x2F, 0x56, 0x46, 0x57, 0x2F, 0x46, 0x4F, 0x55, 0x52, 0x43, 0x43, 0xE0, 0xA6, 0xB0, 0x82, 0x00, 0x00, 0xBA, 0x82, 0x00, 0x00, 0x9A, 0x81, 0x01, 0x9D, 0x81, 0x06, 0x54, 0xB0, 0x81, 0x04, 0x54, 0xBA, 0x81, 0x03, 0x54, 0xB2, 0x81, 0x03, 0xEC, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0xA2, 0xA8, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x76, 0x32, 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAE, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0xD7, 0x81, 0x02, 0x73, 0xC5, 0x81, 0x02, 0xEC, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x81, 0x00, 0x22, 0xB5, 0x9C, 0x83, 0x75, 0x6E, 0x64, 0x86, 0x8D, 0x41, 0x5F, 0x50, 0x43, 0x4D, 0x2F, 0x49, 0x4E, 0x54, 0x2F, 0x4C, 0x49, 0x54, 0x83, 0x81, 0x02, 0xE1, 0x91, 0x9F, 0x81, 0x02, 0xB5, 0x88, 0x40, 0xE7, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x64, 0x81, 0x20, 0xEC, 0x9D, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char matroska_writer_header[] = { 0x1A, 0x45, 0xDF, 0xA3, 0xA3, 0x42, 0x86, 0x81, 0x01, 0x42, 0xF7, 0x81, 0x01, 0x42, 0xF2, 0x81, 0x04, 0x42, 0xF3, 0x81, 0x08, 0x42, 0x82, 0x88, 0x6D, 0x61, 0x74, 0x72, 0x6F, 0x73, 0x6B, 0x61, 0x42, 0x87, 0x81, 0x04, 0x42, 0x85, 0x81, 0x02, 0x18, 0x53, 0x80, 0x67, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x11, 0x4D, 0x9B, 0x74, 0xC6, 0xEC, 0x84, 0x00, 0x00, 0x00, 0x00, 0x4D, 0xBB, 0x8B, 0x53, 0xAB, 0x84, 0x15, 0x49, 0xA9, 0x66, 0x53, 0xAC, 0x81, 0xA1, 0x4D, 0xBB, 0x8B, 0x53, 0xAB, 0x84, 0x16, 0x54, 0xAE, 0x6B, 0x53, 0xAC, 0x81, 0xF3, 0xEC, 0x8D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x49, 0xA9, 0x66, 0xCD, 0xEC, 0x84, 0x00, 0x00, 0x00, 0x00, 0x2A, 0xD7, 0xB1, 0x83, 0x0F, 0x42, 0x40, 0x4D, 0x80, 0x8E, 0x44, 0x56, 0x52, 0x65, 0x73, 0x63, 0x75, 0x65, 0x20, 0x30, 0x30, 0x2E, 0x30, 0x30, 0x57, 0x41, 0x8E, 0x44, 0x56, 0x52, 0x65, 0x73, 0x63, 0x75, 0x65, 0x20, 0x30, 0x30, 0x2E, 0x30, 0x30, 0xEC, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x54, 0xAE, 0x6B, 0x41, 0x14, 0xEC, 0x84, 0x00, 0x00, 0x00, 0x00, 0xAE, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0xD7, 0x81, 0x01, 0x73, 0xC5, 0x81, 0x01, 0xEC, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x81, 0x00, 0x22, 0xB5, 0x9C, 0x83, 0x75, 0x6E, 0x64, 0x83, 0x81, 0x01, 0x23, 0xE3, 0x83, 0x84, 0x01, 0xFD, 0x22, 0x8A, 0x86, 0x8F, 0x56, 0x5F, 0x4D, 0x53, 0x2F, 0x56, 0x46, 0x57, 0x2F, 0x46, 0x4F, 0x55, 0x52, 0x43, 0x43, 0xE0, 0xA6, 0xB0, 0x82, 0x00, 0x00, 0xBA, 0x82, 0x00, 0x00, 0x9A, 0x81, 0x01, 0x9D, 0x81, 0x06, 0x54, 0xB0, 0x81, 0x04, 0x54, 0xBA, 0x81, 0x03, 0x54, 0xB2, 0x81, 0x03, 0xEC, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0xA2, 0xA8, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x76, 0x32, 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAE, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0xD7, 0x81, 0x02, 0x73, 0xC5, 0x81, 0x02, 0xEC, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x81, 0x00, 0x22, 0xB5, 0x9C, 0x83, 0x75, 0x6E, 0x64, 0x86, 0x8D, 0x41, 0x5F, 0x50, 0x43, 0x4D, 0x2F, 0x49, 0x4E, 0x54, 0x2F, 0x4C, 0x49, 0x54, 0x83, 0x81, 0x02, 0xE1, 0x91, 0x9F, 0x81, 0x02, 0xB5, 0x88, 0x40, 0xE7, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x64, 0x81, 0x20, 0xEC, 0x9D, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char matroska_writer_header_seek[] = { 0x4D, 0xBB, 0x92, 0x53, 0xAB, 0x84, 0x1C, 0x53, 0xBB, 0x6B, 0x53, 0xAC, 0x88 };
static const unsigned char matroska_writer_header_timecode[] = { 0x41, 0xE4, 0x8A, 0x41, 0xE7, 0x83, 0x31, 0x32, 0x31, 0x41, 0xF0, 0x81, 0x79 };
static const unsigned char matroska_writer_header_texttrack[] = { 0xAE, 0xA1, 0xD7, 0x81, 0x03, 0x73, 0xC5, 0x81, 0x03, 0xEC, 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0x81, 0x11, 0x86, 0x8B, 0x53, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x2F, 0x55, 0x54, 0x46, 0x38, 0x9C, 0x81, 0x00 };
static const size_t matroska_writer_header_size_offset = 0x002C;
static const size_t matroska_writer_header_seek_crc_beg = 0x0039;
static const size_t matroska_writer_header_seek_offset = 0x006A;
static const size_t matroska_writer_header_seek_crc_end = 0x007F;
static const size_t matroska_writer_header_info_crc_beg = 0x00DA;
static const size_t matroska_writer_header_totalduration_offset = 0x0118;
static const size_t matroska_writer_header_info_crc_end = 0x0123;
static const size_t matroska_writer_header_tracks_crc_beg = 0x0129;
static const size_t matroska_writer_header_defaultduration_offset = 0x015B;
static const size_t matroska_writer_header_width_offset = 0x0174;
static const size_t matroska_writer_header_height_offset = 0x0178;
static const size_t matroska_writer_header_sizeimage_offset = 0x01AF;
static const size_t matroska_writer_header_avi_width_offset = 0x019F;
static const size_t matroska_writer_header_avi_height_offset = 0x01A3;
static const size_t matroska_writer_header_timecode_offset = 0x01C3;
static const size_t matroska_writer_header_texttrack_offset = 0x021A;
static const size_t matroska_writer_header_tracks_crc_end = 0x023D;
static const size_t matroska_writer_header_muxing_offset = 0x00F3;
static const size_t matroska_writer_header_writing_offset = 0x0104;
static const size_t matroska_writer_header_totalduration_offset = 0x011C;
static const size_t matroska_writer_header_info_crc_end = 0x0127;
static const size_t matroska_writer_header_tracks_crc_beg = 0x012D;
static const size_t matroska_writer_header_defaultduration_offset = 0x015F;
static const size_t matroska_writer_header_width_offset = 0x0178;
static const size_t matroska_writer_header_height_offset = 0x017C;
static const size_t matroska_writer_header_avi_width_offset = 0x01A3;
static const size_t matroska_writer_header_avi_height_offset = 0x01A7;
static const size_t matroska_writer_header_avi_sizeimage_offset = 0x01B3;
static const size_t matroska_writer_header_timecode_offset = 0x01C7;
static const size_t matroska_writer_header_texttrack_offset = 0x021E;
static const size_t matroska_writer_header_tracks_crc_end = 0x0241;

static void store_b1(char*& buffer, int value)
{
Expand Down Expand Up @@ -228,33 +232,47 @@ void matroska_writer::write_header()
memcpy(buffer, matroska_writer_header, sizeof(matroska_writer_header));
char* cur;

// Version
if (strlen(Program_Version) == 5)
{
cur = buffer + matroska_writer_header_muxing_offset;
memcpy(cur, Program_Version, 5);
cur = buffer + matroska_writer_header_writing_offset;
memcpy(cur, Program_Version, 5);
}

// Total size + Cues + Duration
if (!cues.empty())
{
double timecode_ms = (double)frame_number * 1000 * framerate_den / framerate_num;

cur = buffer + 0x2C;
cur = buffer + matroska_writer_header_size_offset;
store_e8(cur, output_size);

cur = buffer + matroska_writer_header_seek_offset;
memcpy(cur, matroska_writer_header_seek, sizeof(matroska_writer_header_seek));
cur += sizeof(matroska_writer_header_seek);
store_b8(cur, output_size - 0x34 - (8 + 27 * cues.size()));
store_b8(cur, output_size - (8 + 6 + 27 * cues.size()));

cur = buffer + matroska_writer_header_totalduration_offset;
store_b2(cur, 0x4489); // Duration
store_b1(cur, 0x88);
store_bf8(cur, timecode_ms);
}

// Tracks
cur = buffer + matroska_writer_header_defaultduration_offset;
store_b4(cur, (unsigned)(((unsigned long long)framerate_den * 1000000000 + framerate_num / 2) / framerate_num));
cur = buffer + matroska_writer_header_width_offset;
store_b2(cur, width);
cur = buffer + matroska_writer_header_height_offset;
store_b2(cur, height);
cur = buffer + matroska_writer_header_sizeimage_offset;
store_b2(cur, width * height * 20 * 32 / (30 * 8));
cur = buffer + matroska_writer_header_avi_width_offset;
store_l2(cur, width);
cur = buffer + matroska_writer_header_avi_height_offset;
store_l2(cur, height);
cur = buffer + matroska_writer_header_avi_sizeimage_offset;
store_l4(cur, width * height * 20 * 32 / (30 * 8));
if (has_timecode)
{
cur = buffer + matroska_writer_header_timecode_offset;
Expand Down Expand Up @@ -401,8 +419,7 @@ void matroska_writer::close(std::ofstream* output)
}

output->seekp(0);
if (!output)
return;

write_header();
if (output)
write_header();
output->close();
}

0 comments on commit b62710b

Please sign in to comment.