forked from doublec/plogg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplogg.cpp
1249 lines (1076 loc) · 35.1 KB
/
plogg.cpp
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2009, Chris Double. All Rights Reserved.
// See the license at the end of this file.
#include <cassert>
#include <cmath>
#include <map>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <ogg/ogg.h>
#include <theora/theora.h>
#include <theora/theoradec.h>
#include <vorbis/codec.h>
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
// For OpenGL support.
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
// Timing.
#include <sys/time.h>
#include <float.h>
// Texture streaming.
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LINUX 1
#include "bc_cat.h"
#include <GLES2/gl2ext.h>
#define UYVY_CPU_BUF 0
#define CMEM 0
#define TEST_GETPHYS 0
#define SOUND 0
#if CMEM
#include "cmem.h"
#endif
PFNGLTEXBINDSTREAMIMGPROC glTexBindStreamIMG;
PFNGLGETTEXSTREAMDEVICENAMEIMGPROC glGetTexStreamDeviceNameIMG;
PFNGLGETTEXSTREAMDEVICEATTRIBUTEIVIMGPROC glGetTexStreamDeviceAttributeivIMG;
extern "C" {
#include <sydney_audio.h>
}
using namespace std;
enum StreamType {
TYPE_VORBIS,
TYPE_THEORA,
TYPE_UNKNOWN
};
class OggStream;
class TheoraDecode {
public:
th_info mInfo;
th_comment mComment;
th_setup_info* mSetup;
th_dec_ctx* mCtx;
public:
TheoraDecode() :
mSetup(0),
mCtx(0)
{
th_info_init(&mInfo);
th_comment_init(&mComment);
}
void initForData(OggStream* stream);
~TheoraDecode() {
if (mSetup)
th_setup_free(mSetup);
if (mCtx)
th_decode_free(mCtx);
}
};
class VorbisDecode {
public:
vorbis_info mInfo;
vorbis_comment mComment;
vorbis_dsp_state mDsp;
vorbis_block mBlock;
public:
VorbisDecode()
{
vorbis_info_init(&mInfo);
vorbis_comment_init(&mComment);
}
void initForData(OggStream* stream);
};
class OggStream
{
public:
int mSerial;
ogg_stream_state mState;
StreamType mType;
bool mActive;
TheoraDecode mTheora;
VorbisDecode mVorbis;
public:
OggStream(int serial = -1) :
mSerial(serial),
mType(TYPE_UNKNOWN),
mActive(true)
{
}
~OggStream() {
int ret = ogg_stream_clear(&mState);
assert(ret == 0);
}
};
void TheoraDecode::initForData(OggStream* stream) {
stream->mTheora.mCtx =
th_decode_alloc(&stream->mTheora.mInfo,
stream->mTheora.mSetup);
assert(stream->mTheora.mCtx != NULL);
int ppmax = 0;
int ret = th_decode_ctl(stream->mTheora.mCtx,
TH_DECCTL_GET_PPLEVEL_MAX,
&ppmax,
sizeof(ppmax));
assert(ret == 0);
// Set to a value between 0 and ppmax inclusive to experiment with
// this parameter.
ppmax = 0;
ret = th_decode_ctl(stream->mTheora.mCtx,
TH_DECCTL_SET_PPLEVEL,
&ppmax,
sizeof(ppmax));
assert(ret == 0);
}
void VorbisDecode::initForData(OggStream* stream) {
int ret = vorbis_synthesis_init(&stream->mVorbis.mDsp, &stream->mVorbis.mInfo);
assert(ret == 0);
ret = vorbis_block_init(&stream->mVorbis.mDsp, &stream->mVorbis.mBlock);
assert(ret == 0);
}
typedef map<int, OggStream*> StreamMap;
void make_hildon_leave_us_alone(Display * dpy, Window win)
{
Atom atom = XInternAtom(dpy, "_HILDON_NON_COMPOSITED_WINDOW", False);
assert(atom);
long atomval = 1;
XChangeProperty(dpy, win, atom, XA_INTEGER, 32, PropModeReplace,
(unsigned char*) &atomval, 1);
}
struct DisplaySink
{
virtual void Show(th_dec_ctx* dec, th_ycbcr_buffer const& buffer) = 0;
};
class SDL_DisplaySink : public DisplaySink
{
public:
SDL_DisplaySink() : mSurface(0), mOverlay(0) {
int r = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
assert(r == 0);
}
void Show(th_dec_ctx* dec, th_ycbcr_buffer const& buffer) {
if (!mSurface) {
mSurface = SDL_SetVideoMode(buffer[0].width,
buffer[0].height,
16,
SDL_SWSURFACE | SDL_FULLSCREEN);
assert(mSurface);
mOverlay = SDL_CreateYUVOverlay(buffer[0].width,
buffer[0].height,
SDL_YV12_OVERLAY,
mSurface);
assert(mOverlay);
SDL_SysWMinfo winfo;
SDL_VERSION(&winfo.version);
int r = SDL_GetWMInfo(&winfo);
assert(r == 1);
Display* dpys[2];
dpys[0] = winfo.info.x11.display;
dpys[1] = winfo.info.x11.gfxdisplay;
Window wins[3];
wins[0] = winfo.info.x11.window;
wins[1] = winfo.info.x11.fswindow;
wins[2] = winfo.info.x11.wmwindow;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
make_hildon_leave_us_alone(dpys[i], wins[j]);
}
}
}
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = buffer[0].width;
rect.h = buffer[0].height;
SDL_LockYUVOverlay(mOverlay);
for (int i = 0; i < buffer[0].height; ++i)
memcpy(mOverlay->pixels[0]+(mOverlay->pitches[0]*i),
buffer[0].data+(buffer[0].stride*i),
mOverlay->pitches[0]);
for (int i = 0; i < buffer[2].height; ++i)
memcpy(mOverlay->pixels[2]+(mOverlay->pitches[2]*i),
buffer[1].data+(buffer[1].stride*i),
mOverlay->pitches[2]);
for (int i = 0; i < buffer[1].height; ++i)
memcpy(mOverlay->pixels[1]+(mOverlay->pitches[1]*i),
buffer[2].data+(buffer[2].stride*i),
mOverlay->pitches[1]);
SDL_UnlockYUVOverlay(mOverlay);
SDL_DisplayYUVOverlay(mOverlay, &rect);
}
virtual ~SDL_DisplaySink() {
if (mSurface) {
SDL_FreeYUVOverlay(mOverlay);
SDL_FreeSurface(mSurface);
}
SDL_Quit();
}
private:
SDL_Surface* mSurface;
SDL_Overlay* mOverlay;
};
class GL_DisplaySink : public DisplaySink
{
public:
GL_DisplaySink(int glMode) : mDisplay(0), mContext(0), mSurface(0), mMode(glMode), mMappedTexture(0) {
assert(mMode >= 0 && mMode <= 5);
}
void Show(th_dec_ctx* dec, th_ycbcr_buffer const& buffer) {
if (!mDisplay) {
init_x11(buffer[0].width, buffer[0].height, true);
init_gles();
if (mMode == 3) {
setup_bcbufs(NULL, buffer[0].width, buffer[0].height);
} else if (mMode == 4) {
setup_bcbufs(dec, buffer[0].width, buffer[0].height);
}
}
struct timeval start, end, dt;
gettimeofday(&start, NULL);
if (mMode == 3) {
unsigned char* p = (unsigned char*) mMappedTexture;
for (unsigned int i = 0; i < buffer[0].height; ++i) {
unsigned char* y = buffer[0].data + (i * buffer[0].stride);
unsigned char* u = buffer[1].data + (i / 2 * buffer[1].stride);
unsigned char* v = buffer[2].data + (i / 2 * buffer[2].stride);
for (unsigned int j = 0; j < buffer[0].width; j += 2) {
p[0] = u[j / 2];
p[1] = y[j];
p[2] = v[j / 2];
p[3] = y[j + 1];
p += 4;
}
}
} else if (mMode == 4) {
#if UYVY_CPU_BUF
memcpy(mMappedTexture, mUYVYBuffer, buffer[0].height * buffer[0].width * 2);
#endif
} else {
glActiveTexture(GL_TEXTURE0);
bind_texture(mTextures[0], buffer[0].width, buffer[0].height,
buffer[0].stride, buffer[0].data);
if (mMode != 2) {
glActiveTexture(GL_TEXTURE1);
bind_texture(mTextures[1], buffer[1].width, buffer[1].height,
buffer[1].stride, buffer[1].data);
glActiveTexture(GL_TEXTURE2);
bind_texture(mTextures[2], buffer[2].width, buffer[2].height,
buffer[2].stride, buffer[2].data);
}
}
gettimeofday(&end, NULL);
timersub(&end, &start, &dt);
//printf("%f\t", dt.tv_sec * 1000.0 + dt.tv_usec / 1000.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat const identity[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
glUniformMatrix4fv(mLocation, 1, GL_FALSE, identity);
glEnableVertexAttribArray(0);
GLfloat const vertices[] = {
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
1.0, -1.0, 0.0
};
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(1);
GLfloat const coords[] = {
0.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0
};
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, coords);
gettimeofday(&start, NULL);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
gettimeofday(&end, NULL);
timersub(&end, &start, &dt);
//printf("%f\n", dt.tv_sec * 1000.0 + dt.tv_usec / 1000.0);
eglSwapBuffers(mDisplay, mSurface);
}
virtual ~GL_DisplaySink() {
if (mDisplay) {
glDeleteProgram(mProgram);
glDeleteShader(mVertexShader);
glDeleteShader(mFragmentShader);
eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(mDisplay, mSurface);
eglDestroyContext(mDisplay, mContext);
eglTerminate(mDisplay);
#if UYVY_CPU_BUF
delete []mUYVYBuffer;
#endif
}
}
private:
void init_x11(unsigned int w, unsigned int h, bool fullscreen) {
/*
* Create a Window in 100 easy steps.
*/
Display* xdpy = XOpenDisplay(NULL);
assert(xdpy);
int screen = XDefaultScreen(xdpy);
Window root = RootWindow(xdpy, screen);
int depth = DefaultDepth(xdpy, screen);
XVisualInfo visual;
XMatchVisualInfo(xdpy, screen, depth, TrueColor, &visual);
Colormap cmap = XCreateColormap(xdpy, root, visual.visual, AllocNone);
XSetWindowAttributes xswa;
memset(&xswa, 0, sizeof(xswa));
xswa.colormap = cmap;
xswa.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask |
ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
unsigned int mask = CWEventMask | CWColormap;
Window xwin = XCreateWindow(xdpy, root, 0, 0,
w, h, 0,
CopyFromParent, InputOutput,
CopyFromParent, mask, &xswa);
assert(xwin);
if (fullscreen) {
Atom xatom = XInternAtom(xdpy, "_NET_WM_STATE", False);
Atom xstate = XInternAtom(xdpy, "_NET_WM_STATE_FULLSCREEN", False);
assert(xatom && xstate);
XChangeProperty(xdpy, xwin, xatom, XA_ATOM, 32, PropModeReplace,
(unsigned char*) &xstate, 1);
make_hildon_leave_us_alone(xdpy, xwin);
}
XMapWindow(xdpy, xwin);
XFlush(xdpy);
init_egl(xdpy, xwin);
}
void init_egl(Display* display, Window window) {
/*
* Initialize OpenGL ES 2.0 via EGL.
*/
EGLDisplay dpy = eglGetDisplay((NativeDisplayType) display);
if (eglInitialize(dpy, NULL, NULL) != EGL_TRUE)
assert(false);
EGLint attrs[] = {
EGL_BUFFER_SIZE, 16,
EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER,
EGL_DEPTH_SIZE, 16,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLConfig cfg;
EGLint n_cfgs;
if (eglChooseConfig(dpy, attrs, &cfg, 1, &n_cfgs) != EGL_TRUE)
assert(false);
EGLSurface srf = eglCreateWindowSurface(dpy, cfg,
(NativeWindowType) window, NULL);
if (srf == EGL_NO_SURFACE)
assert(false);
eglBindAPI(EGL_OPENGL_ES_API);
EGLint ctxattrs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLContext ctx = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, ctxattrs);
if (ctx == EGL_NO_CONTEXT)
assert(false);
if (eglMakeCurrent(dpy, srf, srf, ctx) != EGL_TRUE)
assert(false);
mDisplay = dpy;
mContext = ctx;
mSurface = srf;
}
void init_gles() {
GLint maxUnits;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxUnits);
assert(maxUnits >= 3);
GLubyte const* glExts = glGetString(GL_EXTENSIONS);
assert(strstr((char const*) glExts, "GL_IMG_texture_npot"));
printf("vendor: %s\n", glGetString(GL_VENDOR));
printf("renderer: %s\n", glGetString(GL_RENDERER));
printf("version: %s\n", glGetString(GL_VERSION));
printf("shading language version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
printf("extensions: %s\n", glGetString(GL_EXTENSIONS));
char const* vshader =
"attribute vec4 myVertex;\n"
"attribute vec4 myUV;\n"
"uniform mat4 myPMVMatrix;\n"
"varying vec2 myTexCoord;\n"
"void main()\n"
"{\n"
"gl_Position = myPMVMatrix * myVertex;\n"
"myTexCoord = myUV.st;\n"
"}\n";
mVertexShader = compile_shader(GL_VERTEX_SHADER, vshader);
char const* fshaders[] = {
// Obvious colour conversion implementation.
"uniform sampler2D ytx, utx, vtx;\n"
"varying mediump vec2 myTexCoord;\n"
"void main()\n"
"{\n"
"lowp float y = (texture2D(ytx, myTexCoord).r - 0.0625) * 1.1643;\n"
"lowp float u = texture2D(utx, myTexCoord).r - 0.5;\n"
"lowp float v = texture2D(vtx, myTexCoord).r - 0.5;\n"
"lowp float r = y + 1.5958 * v;\n"
"lowp float g = y - 0.39173 * u - 0.8129 * v;\n"
"lowp float b = y + 2.017 * u;\n"
"gl_FragColor = vec4(r, g, b, 1.0);\n"
"}\n",
// Slightly optimized routine.
"uniform sampler2D ytx, utx, vtx;\n"
"uniform mediump mat3 yuv2rgb;\n"
"varying mediump vec2 myTexCoord;\n"
"void main()\n"
"{\n"
"lowp vec3 yuv = vec3(texture2D(ytx, myTexCoord).x, texture2D(utx, myTexCoord).x, texture2D(vtx, myTexCoord).x);\n"
"yuv -= vec3(0.0, 0.5, 0.5);\n"
"gl_FragColor = vec4(yuv2rgb * yuv, 1.0);\n"
"}\n",
// Greyscale "fast" routine.
"uniform sampler2D ytx;\n"
"varying mediump vec2 myTexCoord;\n"
"void main()\n"
"{\n"
"gl_FragColor = texture2D(ytx, myTexCoord);\n"
"}\n",
// Texture streaming.
"#extension GL_IMG_texture_stream2 : enable\n"
"uniform samplerStreamIMG tx;\n"
"varying mediump vec2 myTexCoord;\n"
"void main()\n"
"{\n"
"gl_FragColor = textureStreamIMG(tx, myTexCoord);\n"
"}\n",
// More optimized routine.
"precision highp float;\n"
"uniform sampler2D ytx, utx, vtx;\n"
"uniform mat3 yuv2rgb;\n"
"varying vec2 myTexCoord;\n"
"void main()\n"
"{\n"
"vec3 yuv = vec3(texture2D(ytx, myTexCoord).x, texture2D(utx, myTexCoord).x, texture2D(vtx, myTexCoord).x);\n"
"yuv -= vec3(0.0, 0.5, 0.5);\n"
"gl_FragColor = vec4(yuv2rgb * yuv, 1.0);\n"
"}\n"
};
int mode = mMode;
if (mMode == 4) {
mode = 3;
}
if (mMode == 5) {
mode = 4;
}
mFragmentShader = compile_shader(GL_FRAGMENT_SHADER, fshaders[mode]);
mProgram = glCreateProgram();
assert(mProgram);
glAttachShader(mProgram, mVertexShader);
glAttachShader(mProgram, mFragmentShader);
glBindAttribLocation(mProgram, 0, "myVertex");
glBindAttribLocation(mProgram, 1, "myUV");
glLinkProgram(mProgram);
GLint linked;
glGetProgramiv(mProgram, GL_LINK_STATUS, &linked);
if (!linked) {
char buf[4096];
glGetProgramInfoLog(mProgram, sizeof(buf), NULL, buf);
printf("link error: %s\n", buf);
}
assert(linked);
glUseProgram(mProgram);
mLocation = glGetUniformLocation(mProgram, "myPMVMatrix");
if (mMode == 3 || mMode == 4) {
glUniform1i(glGetUniformLocation(mProgram, "tx"), 0);
} else {
if (mMode == 1 || mMode == 5) {
static GLfloat const yuv2rgb[] = {
1.0, 1.0, 1.0,
0.0, -0.34414, 1.772,
1.402, -0.71414, 0.0
};
glUniformMatrix3fv(glGetUniformLocation(mProgram, "yuv2rgb"), 1, GL_FALSE, yuv2rgb);
}
glUniform1i(glGetUniformLocation(mProgram, "ytx"), 0);
glGenTextures(1, &mTextures[0]);
assert(mTextures[0]);
if (mMode == 0 || mMode == 1 || mMode == 5) {
glUniform1i(glGetUniformLocation(mProgram, "utx"), 1);
glUniform1i(glGetUniformLocation(mProgram, "vtx"), 2);
glGenTextures(2, &mTextures[1]);
assert(mTextures[1] && mTextures[2]);
}
}
}
GLuint compile_shader(GLenum type, char const* src) {
GLuint shader = glCreateShader(type);
assert(shader);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
char buf[4096];
glGetShaderInfoLog(shader, sizeof(buf), NULL, buf);
printf("compile error: %s\n", buf);
}
assert(compiled);
return shader;
}
void bind_texture(GLuint texID, unsigned int w, unsigned int h, unsigned int stride,
unsigned char const* data) {
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// This would be nice to use if it existed in GLES.
// glPixelStorei(GL_UNPACK_ROW_LENGTH, buffer[1].stride);
// We could use glPixelStorei(GL_UNPACK_ALIGNMENT), but it seems
// like most Theora frames have more than 16-32 bytes of extra
// stride.
// But it doesn't, so we have to upload row-by-row, accounting for
// the stride ourselves.
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
for (unsigned int y = 0; y < h; ++y) {
unsigned char const* row = data + (y * stride);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, y, w, 1,
GL_LUMINANCE, GL_UNSIGNED_BYTE, row);
}
}
void setup_bcbufs(th_dec_ctx *dec, unsigned int w, unsigned int h) {
GLubyte const* glExts = glGetString(GL_EXTENSIONS);
assert(strstr((char const*) glExts, "GL_IMG_texture_stream"));
glTexBindStreamIMG =
(PFNGLTEXBINDSTREAMIMGPROC) eglGetProcAddress("glTexBindStreamIMG");
glGetTexStreamDeviceNameIMG =
(PFNGLGETTEXSTREAMDEVICENAMEIMGPROC) eglGetProcAddress("glGetTexStreamDeviceNameIMG");
glGetTexStreamDeviceAttributeivIMG =
(PFNGLGETTEXSTREAMDEVICEATTRIBUTEIVIMGPROC) eglGetProcAddress("glGetTexStreamDeviceAttributeivIMG");
assert(glTexBindStreamIMG && glGetTexStreamDeviceNameIMG && glGetTexStreamDeviceAttributeivIMG);
// XXXkinetik what's the point of ndelay?
int fd = open("/dev/bc_cat", O_RDWR | O_NDELAY);
assert(fd >= 0);
bc_buf_params_t param;
param.count = 1;
assert(w % 32 == 0);
param.width = w;
param.height = h;
// XXXkinetik example uses UYVY (packed), we want YV12(?) which is
// planar packed is single buf per frame, how would planar look?
// doesn't matter - driver doesn't support it anyway
param.pixel_fmt = PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY;
#if !CMEM
param.type = BC_MEMORY_MMAP;
#else
param.type = BC_MEMORY_USERPTR;
#endif
int r = ioctl(fd, BCIOREQ_BUFFERS, ¶m);
assert(r >= 0);
printf("BC setup: %d bufs of %dx%d (stride %d, size %d)\n",
param.count, param.width, param.height, param.stride, param.size);
#if !CMEM
BCIO_package buf_param;
buf_param.input = 0;
r = ioctl(fd, BCIOGET_BUFFERPHYADDR, &buf_param);
assert(r >= 0);
size_t size = w * h * 2;
void* p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf_param.output);
assert(p != MAP_FAILED);
mMappedTexture = p;
#if TEST_GETPHYS
r = CMEM_init();
void *x = (void*)CMEM_getPhys(p);
fprintf(stderr, "%p %p %p\n", p, x, buf_param.output);
#endif
if (mMode == 4) {
#if UYVY_CPU_BUF
mUYVYBuffer = new unsigned char[size];
thdsp_decode_set_uyvy_buffer(dec, mUYVYBuffer, w * 2, size);
#else
thdsp_decode_set_uyvy_buffer(dec, (unsigned char*)mMappedTexture, w * 2, size);
#endif
}
#else
fprintf(stderr, "b4 init\n"); sleep(1);
r = CMEM_init();
assert(r >= 0);
fprintf(stderr, "af init %d\n", r); sleep(1);
CMEM_AllocParams cmem_params;
cmem_params.type = CMEM_HEAP;
cmem_params.flags = CMEM_CACHED;
cmem_params.alignment = 32;
void* p = CMEM_alloc(param.size, &cmem_params);
assert(p);
fprintf(stderr, "af alloc\n"); sleep(1);
bc_buf_ptr_t phys_buf;
phys_buf.index = 0;
phys_buf.size = param.size;
phys_buf.pa = CMEM_getPhys(p);
r = ioctl(fd, BCIOSET_BUFFERPHYADDR, &phys_buf);
assert(r >= 0);
fprintf(stderr, "af setbuf\n"); sleep(1);
mMappedTexture = p;
#endif
GLubyte const* devname = glGetTexStreamDeviceNameIMG(0);
printf("BC GL: devname=%s", devname);
GLint nbufs;
glGetTexStreamDeviceAttributeivIMG(0, GL_TEXTURE_STREAM_DEVICE_NUM_BUFFERS_IMG, &nbufs);
GLint width;
glGetTexStreamDeviceAttributeivIMG(0, GL_TEXTURE_STREAM_DEVICE_WIDTH_IMG, &width);
GLint height;
glGetTexStreamDeviceAttributeivIMG(0, GL_TEXTURE_STREAM_DEVICE_HEIGHT_IMG, &height);
GLint fmt;
glGetTexStreamDeviceAttributeivIMG(0, GL_TEXTURE_STREAM_DEVICE_FORMAT_IMG, &fmt);
printf("BC GL: nbufs %d, %dx%d, fmt=%d\n", nbufs, width, height, fmt);
glTexBindStreamIMG(0, 0);
}
EGLDisplay mDisplay;
EGLContext mContext;
EGLSurface mSurface;
int mMode;
GLuint mVertexShader;
GLuint mFragmentShader;
GLuint mProgram;
GLuint mLocation;
GLuint mTextures[3];
void* mMappedTexture;
#if UYVY_CPU_BUF
unsigned char *mUYVYBuffer;
#endif
};
class Null_DisplaySink : public DisplaySink
{
public:
Null_DisplaySink() {
}
void Show(th_dec_ctx* dec, th_ycbcr_buffer const& buffer) {
}
virtual ~Null_DisplaySink() {
}
};
class OggDecoder
{
public:
StreamMap mStreams;
DisplaySink* mDisplaySink;
sa_stream_t* mAudio;
ogg_int64_t mGranulepos;
private:
bool mVideoFrameQueued;
th_ycbcr_buffer mVideoBuffer;
struct timeval mTimeStamp[100];
unsigned int mTimeStampPos;
private:
bool handle_theora_header(OggStream* stream, ogg_packet* packet);
bool handle_vorbis_header(OggStream* stream, ogg_packet* packet);
void read_headers(istream& stream, ogg_sync_state* state);
bool read_page(istream& stream, ogg_sync_state* state, ogg_page* page);
bool read_packet(istream& is, ogg_sync_state* state, OggStream* stream, ogg_packet* packet);
void handle_theora_data(OggStream* stream, ogg_packet* packet);
void handle_theora_out();
public:
OggDecoder(DisplaySink* display_sink) :
mDisplaySink(display_sink),
mAudio(0),
mGranulepos(0),
mVideoFrameQueued(false),
mTimeStampPos(0)
{
memset(mVideoBuffer,0,sizeof(mVideoBuffer));
}
~OggDecoder() {
if (mAudio) {
sa_stream_drain(mAudio);
sa_stream_destroy(mAudio);
}
for(StreamMap::iterator it = mStreams.begin(); it != mStreams.end(); ++it) {
OggStream* stream = (*it).second;
delete stream;
}
delete mDisplaySink;
}
void play(istream& stream);
};
bool OggDecoder::read_page(istream& stream, ogg_sync_state* state, ogg_page* page) {
int ret = 0;
// If we've hit end of file we still need to continue processing
// any remaining pages that we've got buffered.
if (!stream.good())
return ogg_sync_pageout(state, page) == 1;
while((ret = ogg_sync_pageout(state, page)) != 1) {
// Returns a buffer that can be written too
// with the given size. This buffer is stored
// in the ogg synchronisation structure.
char* buffer = ogg_sync_buffer(state, 4096);
assert(buffer);
// Read from the file into the buffer
stream.read(buffer, 4096);
int bytes = stream.gcount();
if (bytes == 0) {
// End of file.
continue;
}
// Update the synchronisation layer with the number
// of bytes written to the buffer
ret = ogg_sync_wrote(state, bytes);
assert(ret == 0);
}
return true;
}
bool OggDecoder::read_packet(istream& is, ogg_sync_state* state, OggStream* stream, ogg_packet* packet) {
int ret = 0;
while ((ret = ogg_stream_packetout(&stream->mState, packet)) != 1) {
ogg_page page;
if (!read_page(is, state, &page))
return false;
int serial = ogg_page_serialno(&page);
assert(mStreams.find(serial) != mStreams.end());
OggStream* pageStream = mStreams[serial];
// Drop data for streams we're not interested in.
if (stream->mActive) {
ret = ogg_stream_pagein(&pageStream->mState, &page);
assert(ret == 0);
}
}
return true;
}
void OggDecoder::read_headers(istream& stream, ogg_sync_state* state) {
ogg_page page;
bool headersDone = false;
while (!headersDone && read_page(stream, state, &page)) {
int ret = 0;
int serial = ogg_page_serialno(&page);
OggStream* stream = 0;
if(ogg_page_bos(&page)) {
// At the beginning of the stream, read headers
// Initialize the stream, giving it the serial
// number of the stream for this page.
stream = new OggStream(serial);
ret = ogg_stream_init(&stream->mState, serial);
assert(ret == 0);
mStreams[serial] = stream;
}
assert(mStreams.find(serial) != mStreams.end());
stream = mStreams[serial];
// Add a complete page to the bitstream
ret = ogg_stream_pagein(&stream->mState, &page);
assert(ret == 0);
// Process all available header packets in the stream. When we hit
// the first data stream we don't decode it, instead we
// return. The caller can then choose to process whatever data
// streams it wants to deal with.
ogg_packet packet;
while (!headersDone &&
(ret = ogg_stream_packetpeek(&stream->mState, &packet)) != 0) {
assert(ret == 1);
// A packet is available. If it is not a header packet we exit.
// If it is a header packet, process it as normal.
headersDone = headersDone || handle_theora_header(stream, &packet);
headersDone = headersDone || handle_vorbis_header(stream, &packet);
if (!headersDone) {
// Consume the packet
ret = ogg_stream_packetout(&stream->mState, &packet);
assert(ret == 1);
}
}
}
}
void OggDecoder::play(istream& is) {
ogg_sync_state state;
int ret = ogg_sync_init(&state);
assert(ret == 0);
// Read headers for all streams
read_headers(is, &state);
// Find and initialize the first theora and vorbis
// streams. According to the Theora spec these can be considered the
// 'primary' streams for playback.
OggStream* video = 0;
OggStream* audio = 0;
for(StreamMap::iterator it = mStreams.begin(); it != mStreams.end(); ++it) {
OggStream* stream = (*it).second;
if (!video && stream->mType == TYPE_THEORA) {
video = stream;
video->mTheora.initForData(video);
}
else if (!audio && stream->mType == TYPE_VORBIS) {
audio = stream;
audio->mVorbis.initForData(audio);
}
else
stream->mActive = false;
}
assert(audio);
if (video) {
cout << "Video stream is "
<< video->mSerial << " "
<< video->mTheora.mInfo.frame_width << "x" << video->mTheora.mInfo.frame_height
<< endl;
}
cout << "Audio stream is "
<< audio->mSerial << " "
<< audio->mVorbis.mInfo.channels << " channels "
<< audio->mVorbis.mInfo.rate << "KHz"
<< endl;
ret = sa_stream_create_pcm(&mAudio,
NULL,
SA_MODE_WRONLY,
SA_PCM_FORMAT_S16_NE,
audio->mVorbis.mInfo.rate,
audio->mVorbis.mInfo.channels);
assert(ret == SA_SUCCESS);
ret = sa_stream_open(mAudio);
assert(ret == SA_SUCCESS);
// Read audio packets, sending audio data to the sound hardware.
// When it's time to display a frame, decode the frame and display it.
ogg_packet packet;
while (read_packet(is, &state, audio, &packet)) {
if (vorbis_synthesis(&audio->mVorbis.mBlock, &packet) == 0) {
ret = vorbis_synthesis_blockin(&audio->mVorbis.mDsp, &audio->mVorbis.mBlock);
assert(ret == 0);
}
float** pcm = 0;
int samples = 0;
while ((samples = vorbis_synthesis_pcmout(&audio->mVorbis.mDsp, &pcm)) > 0) {
if (mAudio) {
if (samples > 0) {
size_t size = samples * audio->mVorbis.mInfo.channels;
short* buffer = new short[size];
short* p = buffer;
for (int i=0;i < samples; ++i) {
for(int j=0; j < audio->mVorbis.mInfo.channels; ++j) {
int v = static_cast<int>(floorf(0.5 + pcm[j][i]*32767.0));
if (v > 32767) v = 32767;
if (v <-32768) v = -32768;
*p++ = v;
}
}