// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-zoe/pss-demux.c
// Function: mpegDetectAndDumpPrivateStream
void mpegDetectAndDumpPrivateStream( FILE *f, unsigned int offset, FILE *aout, FILE *sout, FILE *pout, char *name ) {
	fseek( f, offset, SEEK_SET );
	uint32_t streamSize, headSize;
	char *outfilename = NULL;
	
	mpegPesHeader pesHead;
	mpegPrivate1ExtensionHead extHead;
	
	
	mpegPrivate1ExtensionKonami sonyExtHead;
	konamiPrivate1Header sonyPrivateHead;
	
	
	fread( &pesHead, sizeof(pesHead), 1, f );
	headSize = pesHead.size[0] << 8 | pesHead.size[1];
	fread( &extHead, sizeof(extHead), 1, f );
	fseek( f, extHead.size, SEEK_CUR );
	
	fread( &sonyExtHead, sizeof(sonyExtHead), 1, f );
	fread( &sonyPrivateHead, sizeof(sonyPrivateHead), 1, f );
	
	switch(sonyExtHead.streamId) {
		case ZOEPT_VAG: { // stream 0x01
			if( ftell(aout) == 0 ) {
				streamSize = headSize - 3 - extHead.size - 4 - 16 - (0x800-0x10); // subtract 3 for extHead size that is included in pesHead but not extHead, subtract 4 for sony extension, subtract 16 for sony header subtract 0x800-0x10 for alignment
				fseek( f, 0x800-0x10, SEEK_CUR ); // alignment stuffs
				audioSamplerate[2] = sonyPrivateHead.samplerate[0]; audioSamplerate[3] = sonyPrivateHead.samplerate[1];
				audioNumChannels = sonyPrivateHead.channels;
				unsigned char null[0x40] = {0};
				if( audioNumChannels == 1 ) fwrite( null, 1, 0x30, aout );
				else if( audioNumChannels == 2 ) fwrite( null, 1, 0x40, aout );
			}
			else {
				streamSize = headSize - 3 - extHead.size - 4;
				fseek( f, -0x10, SEEK_CUR ); // seek back size of sonyExtHead because only the first audio block contains it
			}
			writeFile( f, streamSize, aout );
			break;
		}
		case ZOEPT_DMX: { // stream 0x05
			fseek( f, -0x10, SEEK_CUR );
			writeFile( f, headSize, pout );
			break;
		}
		default: { // subtitle streams, always seem to fit into one PES
			streamSize = sonyPrivateHead.size; // for subs this is the actual size, so we can use it here
			outfilename = malloc( strlen( name ) + 7 + 2 + 4 + 1 );
			sprintf( outfilename, "%s-stream%02x.bin", name, sonyExtHead.streamId );
			if( !(sout = fopen( outfilename, "wb" ))) {
				printf("Couldnt open file %s\n", outfilename);
				return;
			}
			writeFile( f, streamSize, sout );
			fclose(sout);
			free(outfilename);
			outfilename = NULL;
			break;
		}
	}
	fseek( f, offset+headSize+6, SEEK_SET );
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbp
movq %r8, 0x10(%rsp)
movq %rdx, %r12
movq %rdi, %rbx
movq %rsi, 0x18(%rsp)
movl %esi, %esi
xorl %edx, %edx
callq 0x10d0
leaq 0x3a(%rsp), %r14
pushq $0x6
popq %rsi
pushq $0x1
popq %r15
movq %r14, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1060
movzwl 0x4(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq %rax, (%rsp)
leaq 0xd(%rsp), %r14
pushq $0x3
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1060
movzbl 0x2(%r14), %r13d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10d0
leaq 0x36(%rsp), %r14
pushq $0x4
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1060
leaq 0x26(%rsp), %rdi
pushq $0x10
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1060
movzbl 0x3(%r14), %r14d
cmpl $0x5, %r14d
je 0x1458
cmpl $0x1, %r14d
jne 0x147a
movq %r12, %rdi
callq 0x10a0
testq %rax, %rax
je 0x14e9
movq (%rsp), %r14
leal -0x7(%r14), %ebp
pushq $-0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x10d0
jmp 0x1575
pushq $-0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x10d0
movq %rbx, %rdi
movq (%rsp), %r14
movl %r14d, %esi
movq 0x10(%rsp), %rdx
jmp 0x1580
movzwl 0x2a(%rsp), %r12d
movq %rbp, %rdi
callq 0x1080
leaq 0xe(%rax), %rdi
callq 0x10b0
movq %rax, %r15
leaq 0xbcf(%rip), %rsi       # 0x206a
movq %rax, %rdi
movq %rbp, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1100
leaq 0xbca(%rip), %rsi       # 0x207c
movq %r15, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1539
movq %rax, %r14
movq %rbx, %rdi
movl %r12d, %esi
movq %rax, %rdx
callq 0x121c
movq %r14, %rdi
callq 0x1070
movq %r15, %rdi
callq 0x1030
movq (%rsp), %r14
jmp 0x1585
pushq $0x1
popq %rdx
movl $0x7f0, %esi            # imm = 0x7F0
movq %rbx, %rdi
callq 0x10d0
movzwl 0x2c(%rsp), %eax
movw %ax, 0x2bae(%rip)       # 0x40b3
movzbl 0x2e(%rsp), %eax
movb %al, 0x2ba5(%rip)       # 0x40b5
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
cmpl $0x1, %eax
movq (%rsp), %r14
je 0x155b
cmpl $0x2, %eax
jne 0x156e
pushq $0x40
jmp 0x155d
leaq 0xb3f(%rip), %rdi       # 0x207f
movq %r15, %rsi
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1090
pushq $0x30
popq %rdx
leaq 0x40(%rsp), %rdi
pushq $0x1
popq %rsi
movq %r12, %rcx
callq 0x1110
leal -0x807(%r14), %ebp
subl %r13d, %ebp
movq %rbx, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x121c
movq 0x18(%rsp), %rax
leal (%rax,%r14), %esi
addl $0x6, %esi
movq %rbx, %rdi
xorl %edx, %edx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0