~ft/mcfs

ref: 49dc43fd64bb77001494be6721380895f1bba971 mcfs/ivf.c -rw-r--r-- 3.5 KiB
49dc43fd — Sigrid Solveig Haflínudóttir matroska: handle av1 9 months ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "common.h"
#include "packet.h"

enum {
	Timedenum = 1000ULL,
};

static int
ivfframe(Biobuf *out, u8int *p, int sz, uvlong ts, int avcsplit)
{
	u8int d[12];
	int n, len;

	d[0] = sz;
	d[1] = sz >> 8;
	d[2] = sz >> 16;
	d[3] = sz >> 24;
	d[4] = ts;
	d[5] = ts >> 8;
	d[6] = ts >> 16;
	d[7] = ts >> 24;
	d[8] = ts >> 32;
	d[9] = ts >> 40;
	d[10] = ts >> 48;
	d[11] = ts >> 56;
	if(Bwrite(out, d, 12) != 12)
		return -1;
	if(avcsplit){
		for(n = 0; n < sz; n += len+4){
			len = p[n+0]<<24 | p[n+1]<<16 | p[n+2]<<8 | p[n+3];
			p[n+0] = 0;
			p[n+1] = 0;
			p[n+2] = 0;
			p[n+3] = 1;
		}
	}

	return sz > 0 ? (Bwrite(out, p, sz) != sz ? -1 : 0) : 0;
}

int
ivfpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key)
{
	u8int d[0x20], *x, *y, *e;
	int i, n, len;
	uvlong dur;

	dur = ctx->duration / 1000000ULL;
	ts = ts / 1000000ULL;

	for(; np > 0; np--, p++){
		if(ctx->frid == 0){
			memmove(d, "DKIF", 4);
			d[4] = 0;
			d[5] = 0;
			d[6] = 0x20;
			d[7] = 0;
			if(ctx->fmt == FmtAv01)
				memmove(d+8, "AV01", 4);
			else if(ctx->fmt == FmtAvc1)
				memmove(d+8, "AVC1", 4);
			else if(ctx->fmt == FmtVp08)
				memmove(d+8, "VP80", 4);
			else if(ctx->fmt == FmtVp09)
				memmove(d+8, "VP90", 4);
			else{
				werrstr("unsupported video format %T", ctx->fmt);
				goto err;
			}
			d[12] = ctx->video.width;
			d[13] = ctx->video.width >> 8;
			d[14] = ctx->video.height;
			d[15] = ctx->video.height >> 8;
			/* timebase denum */
			d[16] = Timedenum;
			d[17] = Timedenum >> 8;
			d[18] = Timedenum >> 16;
			d[19] = Timedenum >> 24;
			/* timebase num */
			d[20] = 1;
			d[21] = 0;
			d[22] = 0;
			d[23] = 0;
			/* FIXME is it a "number of frames" or "total duration?" */
			d[24] = dur;
			d[25] = dur >> 8;
			d[26] = dur >> 16;
			d[27] = dur >> 24;
			d[28] = dur >> 32; /* FIXME is it 64 bits? */
			d[29] = dur >> 40;
			d[30] = dur >> 48;
			d[31] = dur >> 56;

			if(Bwrite(out, d, 0x20) != 0x20)
				goto err;

			if(ctx->codec.priv.sz > 0){
				if(ctx->fmt != FmtAvc1){
					werrstr("no idea what to do with codec private data for non-AVC1");
					goto err;
				}

				x = ctx->codec.priv.data;
				e = x + ctx->codec.priv.sz;
				if(x[0] != 1){
					werrstr("avc config: invalid version %d", x[0]);
					goto err;
				}
				if((x[4] & 3) != 3){
					werrstr("avc config: NAL size %d not implemented", (x[4] & 3)+1);
					goto err;
				}
				/* sps */
				x[0] = 0;
				x[1] = 0;
				x[2] = 0;
				x[3] = 1;
				n = x[5] & 0x1f;
				y = x+6;
				for(i = 0; i < n; i++){
					len = y[0] << 8 | y[1];
					y += 2;
					if(y+len > e){
						werrstr("sps out of range");
						goto err;
					}
					ctx->ps[ctx->nps].p = malloc(4+len);
					memmove(ctx->ps[ctx->nps].p, x, 4);
					memmove(ctx->ps[ctx->nps].p+4, y, len);
					ctx->ps[ctx->nps++].n = 4+len;
					ctx->frid++;
					y += len;
				}
				/* pps */
				n = *y++;
				for(i = 0; i < n; i++){
					len = y[0] << 8 | y[1];
					y += 2;
					if(y+len > e){
						werrstr("pps out of range");
						goto err;
					}
					ctx->ps[ctx->nps].p = malloc(4+len);
					memmove(ctx->ps[ctx->nps].p, x, 4);
					memmove(ctx->ps[ctx->nps].p+4, y, len);
					ctx->ps[ctx->nps++].n = 4+len;
					ctx->frid++;
					y += len;
				}
			}
		}

		if(key && ctx->fmt == FmtAvc1){
			for(i = 0; i < ctx->nps; i++)
				ivfframe(out, ctx->ps[i].p, ctx->ps[i].n, 0, 0);
		}
		if(ivfframe(out, p->data, p->sz, ts, ctx->fmt == FmtAvc1) != 0)
			goto err;
		ctx->frid++;
	}

	return 0;
err:
	werrstr("ivfpacket: %r");
	return -1;
}