-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
theorafile.c
689 lines (626 loc) · 14.6 KB
/
theorafile.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
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
/* Theorafile - Ogg Theora Video Decoder Library
*
* Copyright (c) 2017-2024 Ethan Lee.
* Based on TheoraPlay, Copyright (c) 2011-2016 Ryan C. Gordon.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <[email protected]>
*
*/
#include "theorafile.h"
#include <stdio.h> /* fopen and friends */
#include <stdlib.h> /* realloc */
#include <string.h> /* memcpy, memset */
#define TF_DEFAULT_BUFFER_SIZE 4096
#ifdef _WIN32
#define inline __inline
#endif /* _WIN32 */
static inline int INTERNAL_readOggData(OggTheora_File *file)
{
long buflen = TF_DEFAULT_BUFFER_SIZE;
char *buffer = ogg_sync_buffer(&file->sync, buflen);
if (buffer == NULL)
{
/* If you made it here, you ran out of RAM (wait, what?) */
return -1;
}
buflen = file->io.read_func(buffer, 1, buflen, file->datasource);
if (buflen <= 0)
{
return 0;
}
return (ogg_sync_wrote(&file->sync, buflen) == 0) ? 1 : -1;
}
static inline void INTERNAL_queueOggPage(OggTheora_File *file)
{
if (file->tpackets)
{
ogg_stream_pagein(&file->tstream[file->ttrack], &file->page);
}
if (file->vpackets)
{
ogg_stream_pagein(&file->vstream[file->vtrack], &file->page);
}
}
static inline int INTERNAL_getNextPacket(
OggTheora_File *file,
ogg_stream_state *stream,
ogg_packet *packet
) {
while (ogg_stream_packetout(stream, packet) <= 0)
{
const int rc = INTERNAL_readOggData(file);
if (rc == 0)
{
file->eos = 1;
return 0;
}
else if (rc < 0)
{
/* If you made it here, something REALLY bad happened.
*
* Unfortunately, ogg_sync_wrote does not give out any
* codes, so I have no idea what that something is.
*
* Be sure you're not doing something nasty like
* accessing one file via multiple threads at one time.
* -flibit
*/
file->eos = 1;
return 0;
}
else
{
while (ogg_sync_pageout(&file->sync, &file->page) > 0)
{
INTERNAL_queueOggPage(file);
}
}
}
return 1;
}
int tf_open_callbacks(void *datasource, OggTheora_File *file, tf_callbacks io)
{
ogg_packet packet;
ogg_stream_state filler;
th_setup_info *tsetup = NULL;
int pp_level_max = 0;
int errcode = TF_EUNKNOWN;
vorbis_info vinfo;
vorbis_comment vcomment;
th_info tinfo;
th_comment tcomment;
int i;
if (datasource == NULL)
{
return TF_ENODATASOURCE;
}
memset(file, '\0', sizeof(OggTheora_File));
file->datasource = datasource;
file->io = io;
#define TF_OPEN_ASSERT(cond) \
if (cond) goto fail;
ogg_sync_init(&file->sync);
vorbis_info_init(&vinfo);
vorbis_comment_init(&vcomment);
th_info_init(&tinfo);
th_comment_init(&tcomment);
/* Is there even data for us to read...? */
TF_OPEN_ASSERT(INTERNAL_readOggData(file) <= 0)
/* Read header */
while (ogg_sync_pageout(&file->sync, &file->page) > 0)
{
if (!ogg_page_bos(&file->page))
{
/* Not a header! */
INTERNAL_queueOggPage(file);
break;
}
ogg_stream_init(&filler, ogg_page_serialno(&file->page));
ogg_stream_pagein(&filler, &file->page);
ogg_stream_packetout(&filler, &packet);
if (th_decode_headerin(
&tinfo,
&tcomment,
&tsetup,
&packet
) >= 0) {
file->tinfo = realloc(file->tinfo, (file->ttracks + 1) * sizeof(file->tinfo[0]));
file->tcomment = realloc(file->tcomment, (file->ttracks + 1) * sizeof(file->tcomment[0]));
file->tstream = realloc(file->tstream, (file->ttracks + 1) * sizeof(file->tstream[0]));
file->tdec = realloc(file->tdec, (file->ttracks + 1) * sizeof(file->tdec[0]));
file->tinfo[file->ttracks] = tinfo;
file->tcomment[file->ttracks] = tcomment;
memcpy(&file->tstream[file->ttracks], &filler, sizeof(filler));
file->ttracks += 1;
file->tpackets += 1;
/* Reset this for other possible Theora streams */
th_info_init(&tinfo);
th_comment_init(&tcomment);
}
else if (vorbis_synthesis_headerin(
&vinfo,
&vcomment,
&packet
) >= 0) {
file->vinfo = realloc(file->vinfo, (file->vtracks + 1) * sizeof(file->vinfo[0]));
file->vcomment = realloc(file->vcomment, (file->vtracks + 1) * sizeof(file->vcomment[0]));
file->vstream = realloc(file->vstream, (file->vtracks + 1) * sizeof(file->vstream[0]));
file->vinfo[file->vtracks] = vinfo;
file->vcomment[file->vtracks] = vcomment;
memcpy(&file->vstream[file->vtracks], &filler, sizeof(filler));
file->vtracks += 1;
file->vpackets += 1;
/* Reset this for other possible Vorbis streams */
vorbis_info_init(&vinfo);
vorbis_comment_init(&vcomment);
}
else
{
/* Whatever it is, we don't care about it */
ogg_stream_clear(&filler);
}
}
vorbis_comment_clear(&vcomment);
vorbis_info_clear(&vinfo);
th_info_init(&tinfo);
th_comment_init(&tcomment);
/* No audio OR video? */
TF_OPEN_ASSERT(!file->tpackets && !file->vpackets)
/* Apparently there are 2 more theora and 2 more vorbis headers next. */
#define TPACKETS (file->tpackets && (file->tpackets < (file->ttracks + 2)))
#define VPACKETS (file->vpackets && (file->vpackets < (file->vtracks + 2)))
while (TPACKETS || VPACKETS)
{
while (TPACKETS)
{
if (ogg_stream_packetout(
&file->tstream[file->ttrack],
&packet
) != 1) {
/* Get more data? */
break;
}
TF_OPEN_ASSERT(!th_decode_headerin(
&file->tinfo[0],
&file->tcomment[0],
&tsetup,
&packet
))
file->tpackets += 1;
}
while (VPACKETS)
{
if (ogg_stream_packetout(
&file->vstream[file->vtrack],
&packet
) != 1) {
/* Get more data? */
break;
}
TF_OPEN_ASSERT(vorbis_synthesis_headerin(
&file->vinfo[0],
&file->vcomment[0],
&packet
))
file->vpackets += 1;
}
/* Get another page, try again? */
if (ogg_sync_pageout(&file->sync, &file->page) > 0)
{
INTERNAL_queueOggPage(file);
}
else
{
TF_OPEN_ASSERT(INTERNAL_readOggData(file) < 0)
}
}
#undef TPACKETS
#undef VPACKETS
/* Set up Theora stream */
for (i = 0; i < file->ttracks; i += 1)
{
/* th_decode_alloc() docs say to check for
* insanely large frames yourself.
*/
TF_OPEN_ASSERT(
(file->tinfo[i].frame_width > 99999) ||
(file->tinfo[i].frame_height > 99999)
)
/* FIXME: We treat "unspecified" as NTSC :shrug: */
if ( (file->tinfo[i].colorspace != TH_CS_UNSPECIFIED) &&
(file->tinfo[i].colorspace != TH_CS_ITU_REC_470M) &&
(file->tinfo[i].colorspace != TH_CS_ITU_REC_470BG) )
{
errcode = TF_EUNSUPPORTED;
goto fail;
}
if ( file->tinfo[i].pixel_fmt != TH_PF_420 &&
file->tinfo[i].pixel_fmt != TH_PF_422 &&
file->tinfo[i].pixel_fmt != TH_PF_444 )
{
errcode = TF_EUNSUPPORTED;
goto fail;
}
/* The decoder, at last! */
file->tdec[i] = th_decode_alloc(&file->tinfo[i], tsetup);
TF_OPEN_ASSERT(!file->tdec[i])
/* Disable all post-processing in the decoder.
* FIXME: Maybe an API to set this?
* FIXME: Could be TH_DECCTL_GET_PPLEVEL_MAX, for example!
* FIXME: Theoretically we could enable post-processing and then
* FIXME: drop the quality level if we're not keeping up.
*/
th_decode_ctl(
file->tdec[i],
TH_DECCTL_SET_PPLEVEL,
&pp_level_max,
sizeof(pp_level_max)
);
}
/* Done with this now */
if (tsetup != NULL)
{
th_setup_free(tsetup);
tsetup = NULL;
}
/* Set up Vorbis stream */
if (file->vpackets)
{
file->vdsp_init = vorbis_synthesis_init(
&file->vdsp,
&file->vinfo[file->vtrack]
) == 0;
TF_OPEN_ASSERT(!file->vdsp_init)
file->vblock_init = vorbis_block_init(
&file->vdsp,
&file->vblock
) == 0;
TF_OPEN_ASSERT(!file->vblock_init)
}
#undef TF_OPEN_ASSERT
/* Finally. */
return 0;
fail:
if (tsetup != NULL)
{
th_setup_free(tsetup);
}
tf_close(file);
return errcode;
}
static size_t default_read_func(void* ptr, size_t size, size_t nmemb, void* datasource)
{
return fread(ptr, size, nmemb, datasource);
}
static int default_seek_func(void* datasource, ogg_int64_t offset, int origin)
{
return fseek(datasource, offset, origin);
}
static int default_close_func(void* datasource)
{
return fclose(datasource);
}
int tf_fopen(const char *fname, OggTheora_File *file)
{
tf_callbacks io =
{
default_read_func,
default_seek_func,
default_close_func,
};
return tf_open_callbacks(
fopen(fname, "rb"),
file,
io
);
}
void tf_close(OggTheora_File *file)
{
int i;
/* Theora Data */
for (i = 0; i < file->ttracks; i += 1)
{
if (file->tdec[i] != NULL)
{
th_decode_free(file->tdec[i]);
}
}
free(file->tdec);
/* Vorbis Data */
if (file->vblock_init)
{
vorbis_block_clear(&file->vblock);
}
if (file->vdsp_init)
{
vorbis_dsp_clear(&file->vdsp);
}
/* Stream Data */
for (i = 0; i < file->ttracks; i += 1)
{
ogg_stream_clear(&file->tstream[i]);
}
for (i = 0; i < file->vtracks; i += 1)
{
ogg_stream_clear(&file->vstream[i]);
}
/* Metadata */
for (i = 0; i < file->ttracks; i += 1)
{
th_info_clear(&file->tinfo[i]);
th_comment_clear(&file->tcomment[i]);
}
for (i = 0; i < file->vtracks; i += 1)
{
vorbis_comment_clear(&file->vcomment[i]);
vorbis_info_clear(&file->vinfo[i]);
}
free(file->tstream);
free(file->tcomment);
free(file->vstream);
free(file->vcomment);
free(file->vinfo);
free(file->tinfo);
/* Current State */
ogg_sync_clear(&file->sync);
/* I/O Data */
if (file->io.close_func != NULL)
{
file->io.close_func(file->datasource);
}
}
int tf_hasvideo(OggTheora_File *file)
{
return file->tpackets != 0;
}
int tf_hasaudio(OggTheora_File *file)
{
return file->vpackets != 0;
}
void tf_videoinfo(
OggTheora_File *file,
int *width,
int *height,
double *fps,
th_pixel_fmt *fmt
) {
if (width != NULL)
{
*width = file->tinfo[file->ttrack].pic_width;
}
if (height != NULL)
{
*height = file->tinfo[file->ttrack].pic_height;
}
if (fps != NULL)
{
if (file->tinfo[file->ttrack].fps_denominator != 0)
{
*fps = (
((double) file->tinfo[file->ttrack].fps_numerator) /
((double) file->tinfo[file->ttrack].fps_denominator)
);
}
else
{
*fps = 0.0;
}
}
if (fmt != NULL)
{
*fmt = file->tinfo[file->ttrack].pixel_fmt;
}
}
void tf_audioinfo(OggTheora_File *file, int *channels, int *samplerate)
{
if (channels != NULL)
{
*channels = file->vinfo[file->vtrack].channels;
}
if (samplerate != NULL)
{
*samplerate = file->vinfo[file->vtrack].rate;
}
}
int tf_setaudiotrack(OggTheora_File *file, int vtrack)
{
/* Note there may be a slight delay changing track midstream. */
if (vtrack >= 0 && vtrack < file->vtracks)
{
file->vtrack = vtrack;
return 1;
}
else
{
return 0;
}
}
int tf_setvideotrack(OggTheora_File *file, int ttrack)
{
/* Note there may be a slight delay changing track midstream. */
if (ttrack >= 0 && ttrack < file->ttracks)
{
file->ttrack = ttrack;
return 1;
}
else
{
return 0;
}
}
int tf_eos(OggTheora_File *file)
{
return file->eos;
}
void tf_reset(OggTheora_File *file)
{
if (file->tpackets)
{
ogg_stream_reset(&file->tstream[file->ttrack]);
}
if (file->vpackets)
{
ogg_stream_reset(&file->vstream[file->vtrack]);
}
ogg_sync_reset(&file->sync);
file->io.seek_func(file->datasource, 0, SEEK_SET);
file->eos = 0;
}
int tf_readvideo(OggTheora_File *file, char *buffer, int numframes)
{
int i;
char *dst = buffer;
ogg_int64_t granulepos = 0;
ogg_packet packet;
th_ycbcr_buffer ycbcr;
int rc;
int w, h, off;
unsigned char *plane;
int stride;
int retval = 0;
for (i = 0; i < numframes; i += 1)
{
/* Keep trying to get a usable packet */
if (!INTERNAL_getNextPacket(file, &file->tstream[file->ttrack], &packet))
{
/* ... unless there's nothing left for us to read. */
if (retval)
{
break;
}
return 0;
}
rc = th_decode_packetin(
file->tdec[file->ttrack],
&packet,
&granulepos
);
if (rc == 0) /* New frame! */
{
retval = 1;
}
else if (rc != TH_DUPFRAME)
{
return 0; /* Why did we get here...? */
}
}
if (retval) /* New frame! */
{
if (th_decode_ycbcr_out(file->tdec[file->ttrack], ycbcr) != 0)
{
return 0; /* Uhh?! */
}
#define TF_COPY_CHANNEL(chan) \
plane = ycbcr[chan].data + off; \
stride = ycbcr[chan].stride; \
for (i = 0; i < h; i += 1, dst += w) \
{ \
memcpy( \
dst, \
plane + (stride * i), \
w \
); \
}
/* Y */
w = file->tinfo[file->ttrack].pic_width;
h = file->tinfo[file->ttrack].pic_height;
off = (
(file->tinfo[file->ttrack].pic_x & ~1) +
ycbcr[0].stride *
(file->tinfo[file->ttrack].pic_y & ~1)
);
TF_COPY_CHANNEL(0)
/* U/V */
if (file->tinfo[file->ttrack].pixel_fmt == TH_PF_420)
{
/* Subsampled in both dimensions */
w /= 2;
h /= 2;
off = (
(file->tinfo[file->ttrack].pic_x / 2) +
(ycbcr[1].stride) *
(file->tinfo[file->ttrack].pic_y / 2)
);
}
else if (file->tinfo[file->ttrack].pixel_fmt == TH_PF_422)
{
/* Subsampled only horizontally */
w /= 2;
off = (
(file->tinfo[file->ttrack].pic_x / 2) +
(ycbcr[1].stride) *
(file->tinfo[file->ttrack].pic_y & ~1)
);
}
TF_COPY_CHANNEL(1)
TF_COPY_CHANNEL(2)
#undef TF_COPY_CHANNEL
}
return retval;
}
int tf_readaudio(OggTheora_File *file, float *buffer, int samples)
{
int offset = 0;
int chan, frame;
ogg_packet packet;
float **pcm = NULL;
while (offset < samples)
{
const int frames = vorbis_synthesis_pcmout(&file->vdsp, &pcm);
if (frames > 0)
{
/* I bet this beats the crap out of the CPU cache... */
for (frame = 0; frame < frames; frame += 1)
for (chan = 0; chan < file->vinfo[file->vtrack].channels; chan += 1)
{
buffer[offset++] = pcm[chan][frame];
if (offset >= samples)
{
vorbis_synthesis_read(
&file->vdsp,
frame
);
return offset;
}
}
vorbis_synthesis_read(&file->vdsp, frames);
}
else /* No audio available left in current packet? */
{
/* Keep trying to get a usable packet */
if (!INTERNAL_getNextPacket(file, &file->vstream[file->vtrack], &packet))
{
/* ... unless there's nothing left for us to read. */
return offset;
}
if (vorbis_synthesis(
&file->vblock,
&packet
) == 0) {
vorbis_synthesis_blockin(
&file->vdsp,
&file->vblock
);
}
}
}
return offset;
}