-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamsplit.c
226 lines (186 loc) · 5.45 KB
/
streamsplit.c
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
/*
* Copyright © 2010 Matthew Gregan <kinetik@flim.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>
#include <theora/theoradec.h>
void
usage(void)
{
fprintf(stderr, "usage: streamsplit INTERVAL FILE\n");
exit(EXIT_FAILURE);
}
void
fatal(void)
{
perror("fatal");
exit(EXIT_FAILURE);
}
int
read_page(FILE * fp, ogg_sync_state * state, ogg_page * page)
{
if (feof(fp)) {
return ogg_sync_pageout(state, page);
}
while (!ogg_sync_pageout(state, page)) {
char * p = ogg_sync_buffer(state, 1024);
assert(p);
size_t read = fread(p, 1, 1024, fp);
if (read < 1024) {
return ogg_sync_pageout(state, page);
}
int r = ogg_sync_wrote(state, read);
assert(r == 0);
}
return 1;
}
int
read_packet(FILE * fp, ogg_sync_state * state, ogg_stream_state * stream, ogg_packet * packet)
{
while (!ogg_stream_packetout(stream, packet)) {
ogg_page page;
if (!read_page(fp, state, &page)) {
return 0;
}
int r = ogg_stream_pagein(stream, &page);
assert(r == 0);
}
return 1;
}
int
main(int argc, char * argv[])
{
if (argc != 3)
usage();
unsigned long interval = strtoul(argv[1], NULL, 10);
if ((interval == 0 && errno == EINVAL) || (interval == ULONG_MAX && errno == ERANGE))
fatal();
char const * path = argv[2];
FILE * fp = fopen(path, "rb");
if (!fp)
fatal();
ogg_sync_state state;
int r = ogg_sync_init(&state);
assert(r == 0);
/* Read headers. */
ogg_page page;
int got_headers = 0;
ogg_stream_state stream;
int serial = -1;
th_info t_info;
th_info_init(&t_info);
th_comment t_comment;
th_comment_init(&t_comment);
ogg_stream_state ostream;
th_setup_info * t_setup = NULL;
while (!got_headers && read_page(fp, &state, &page)) {
int sl = ogg_page_serialno(&page);
if (serial == -1) {
serial = sl;
r = ogg_stream_init(&stream, serial);
assert(r == 0);
r = ogg_stream_init(&ostream, serial);
assert(r == 0);
}
if (serial != sl) {
fatal();
}
r = ogg_stream_pagein(&stream, &page);
assert(r == 0);
ogg_packet packet;
while (!got_headers && ogg_stream_packetpeek(&stream, &packet)) {
r = th_decode_headerin(&t_info, &t_comment, &t_setup, &packet);
if (r == TH_ENOTFORMAT || r < 0) {
fatal();
}
got_headers = r == 0;
if (!got_headers) {
r = ogg_stream_packetout(&stream, &packet);
assert(r == 1);
r = ogg_stream_packetin(&ostream, &packet);
assert(r == 0);
}
}
}
unsigned char * headers = NULL;
size_t hsize = 0;
ogg_page opage;
while (ogg_stream_pageout(&ostream, &opage)) {
headers = realloc(headers, hsize + opage.header_len + opage.body_len);
assert(headers);
memcpy(headers + hsize, opage.header, opage.header_len);
hsize += opage.header_len;
memcpy(headers + hsize, opage.body, opage.body_len);
hsize += opage.body_len;
}
r = ogg_stream_flush(&ostream, &opage);
if (r > 0) {
headers = realloc(headers, hsize + opage.header_len + opage.body_len);
assert(headers);
memcpy(headers + hsize, opage.header, opage.header_len);
hsize += opage.header_len;
memcpy(headers + hsize, opage.body, opage.body_len);
hsize += opage.body_len;
}
th_dec_ctx * t_ctx = th_decode_alloc(&t_info, t_setup);
assert(t_ctx);
unsigned long kfcount = 0;
unsigned long count = 0;
char namebuf[100];
sprintf(namebuf, "out%04lu.ogv", count);
FILE * fw = fopen(namebuf, "wb");
fwrite(headers, 1, hsize, fw);
ogg_packet packet;
while (read_packet(fp, &state, &stream, &packet)) {
if (th_packet_iskeyframe(&packet)) {
if (kfcount > 0 && kfcount % interval == 0) {
ogg_page opage;
while (ogg_stream_pageout(&ostream, &opage)) {
fwrite(opage.header, 1, opage.header_len, fw);
fwrite(opage.body, 1, opage.body_len, fw);
}
r = ogg_stream_flush(&ostream, &opage);
if (r > 0) {
fwrite(opage.header, 1, opage.header_len, fw);
fwrite(opage.body, 1, opage.body_len, fw);
}
fclose(fw);
count += 1;
sprintf(namebuf, "out%04lu.ogv", count);
FILE * fw = fopen(namebuf, "wb");
fwrite(headers, 1, hsize, fw);
}
kfcount += 1;
}
r = ogg_stream_packetin(&ostream, &packet);
assert(r == 0);
}
while (ogg_stream_pageout(&ostream, &opage)) {
fwrite(opage.header, 1, opage.header_len, fw);
fwrite(opage.body, 1, opage.body_len, fw);
}
r = ogg_stream_flush(&ostream, &opage);
if (r > 0) {
fwrite(opage.header, 1, opage.header_len, fw);
fwrite(opage.body, 1, opage.body_len, fw);
}
fclose(fw);
return EXIT_SUCCESS;
}