-
Notifications
You must be signed in to change notification settings - Fork 6
/
StageFrightVideo.cpp
1345 lines (1156 loc) · 37.6 KB
/
StageFrightVideo.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) 2010-2013 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/***************************************************************************/
//#define DEBUG_VERBOSE 1
#include "system.h"
#include "system_gl.h"
#include "StageFrightVideo.h"
#include "android/activity/XBMCApp.h"
#include "guilib/GraphicContext.h"
#include "DVDClock.h"
#include "utils/log.h"
#include "utils/fastmemcpy.h"
#include "threads/Thread.h"
#include "threads/Event.h"
#include "settings/AdvancedSettings.h"
#include "xbmc/guilib/FrameBufferObject.h"
#include "windowing/WindowingFactory.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <binder/ProcessState.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/MediaBufferGroup.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/OMXClient.h>
#include <media/stagefright/OMXCodec.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <utils/List.h>
#include <utils/RefBase.h>
#include <ui/GraphicBuffer.h>
#include <ui/PixelFormat.h>
#include <gui/SurfaceTexture.h>
#include <new>
#include <map>
#include <queue>
#include <list>
#define OMX_QCOM_COLOR_FormatYVU420SemiPlanar 0x7FA30C00
#define CLASSNAME "CStageFrightVideo"
#define INBUFCOUNT 16
#define NUMFBOTEX 4
// EGL extension functions
static PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
static PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
#define GETEXTENSION(type, ext) \
do \
{ \
ext = (type) eglGetProcAddress(#ext); \
if (!ext) \
{ \
CLog::Log(LOGERROR, "CLinuxRendererGLES::%s - ERROR getting proc addr of " #ext "\n", __func__); \
} \
} while (0);
#define EGL_NATIVE_BUFFER_ANDROID 0x3140
#define EGL_IMAGE_PRESERVED_KHR 0x30D2
GLint glerror;
#define CheckEglError(x) while((glerror = eglGetError()) != EGL_SUCCESS) CLog::Log(LOGERROR, "EGL error in %s: %x",x, glerror);
#define CheckGlError(x) while((glerror = glGetError()) != GL_NO_ERROR) CLog::Log(LOGERROR, "GL error in %s: %x",x, glerror);
const char *MEDIA_MIMETYPE_VIDEO_WMV = "video/x-ms-wmv";
using namespace android;
static int64_t pts_dtoi(double pts)
{
return (int64_t)(pts);
}
static double pts_itod(int64_t pts)
{
return (double)pts;
}
struct Frame
{
status_t status;
int32_t width, height;
int64_t pts;
ERenderFormat format;
EGLImageKHR eglimg;
MediaBuffer* medbuf;
};
enum StageFrightQuirks
{
QuirkNone = 0,
QuirkSWRender = 0x01,
};
class CStageFrightDecodeThread;
class CStageFrightVideoPrivate : public MediaBufferObserver
{
public:
CStageFrightVideoPrivate()
: decode_thread(NULL), source(NULL), natwin(NULL)
, eglDisplay(EGL_NO_DISPLAY), eglSurface(EGL_NO_SURFACE), eglContext(EGL_NO_CONTEXT)
, eglInitialized(false)
, framecount(0)
, quirks(QuirkNone), cur_frame(NULL), prev_frame(NULL)
, width(-1), height(-1)
, texwidth(-1), texheight(-1)
, client(NULL), decoder(NULL), decoder_component(NULL)
, drop_state(false), resetting(false)
{}
virtual void signalBufferReturned(MediaBuffer *buffer)
{
}
MediaBuffer* getBuffer(size_t size)
{
int i=0;
for (; i<INBUFCOUNT; ++i)
if (inbuf[i]->refcount() == 0 && inbuf[i]->size() >= size)
break;
if (i == INBUFCOUNT)
{
i = 0;
for (; i<INBUFCOUNT; ++i)
if (inbuf[i]->refcount() == 0)
break;
if (i == INBUFCOUNT)
return NULL;
inbuf[i]->setObserver(NULL);
inbuf[i]->release();
inbuf[i] = new MediaBuffer(size);
inbuf[i]->setObserver(this);
}
inbuf[i]->add_ref();
inbuf[i]->set_range(0, size);
return inbuf[i];
}
bool inputBufferAvailable()
{
for (int i=0; i<INBUFCOUNT; ++i)
if (inbuf[i]->refcount() == 0)
return true;
return false;
}
void loadOESShader(GLenum shaderType, const char* pSource, GLuint* outShader)
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, ">>loadOESShader\n");
#endif
GLuint shader = glCreateShader(shaderType);
CheckGlError("loadOESShader");
if (shader) {
glShaderSource(shader, 1, &pSource, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen) {
char* buf = (char*) malloc(infoLen);
if (buf) {
glGetShaderInfoLog(shader, infoLen, NULL, buf);
printf("Shader compile log:\n%s\n", buf);
free(buf);
}
} else {
char* buf = (char*) malloc(0x1000);
if (buf) {
glGetShaderInfoLog(shader, 0x1000, NULL, buf);
printf("Shader compile log:\n%s\n", buf);
free(buf);
}
}
glDeleteShader(shader);
shader = 0;
}
}
*outShader = shader;
}
void createOESProgram(const char* pVertexSource, const char* pFragmentSource, GLuint* outPgm)
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, ">>createOESProgram\n");
#endif
GLuint vertexShader, fragmentShader;
{
loadOESShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader);
}
{
loadOESShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader);
}
GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength) {
char* buf = (char*) malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
printf("Program link log:\n%s\n", buf);
free(buf);
}
}
glDeleteProgram(program);
program = 0;
}
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
*outPgm = program;
}
void OES_shader_setUp()
{
const char vsrc[] =
"attribute vec4 vPosition;\n"
"varying vec2 texCoords;\n"
"uniform mat4 texMatrix;\n"
"void main() {\n"
" vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
" texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
" gl_Position = vPosition;\n"
"}\n";
const char fsrc[] =
"#extension GL_OES_EGL_image_external : require\n"
"precision mediump float;\n"
"uniform samplerExternalOES texSampler;\n"
"varying vec2 texCoords;\n"
"void main() {\n"
" gl_FragColor = texture2D(texSampler, texCoords);\n"
"}\n";
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, ">>OES_shader_setUp\n");
#endif
CheckGlError("OES_shader_setUp");
createOESProgram(vsrc, fsrc, &mPgm);
}
mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
}
int NP2( unsigned x ) {
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
void InitializeEGL(int w, int h)
{
texwidth = w;
texheight = h;
if (!g_Windowing.IsExtSupported("GL_TEXTURE_NPOT"))
{
texwidth = NP2(texwidth);
texheight = NP2(texheight);
}
eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglBindAPI(EGL_OPENGL_ES_API);
EGLint contextAttributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
eglContext = eglCreateContext(eglDisplay, g_Windowing.GetEGLConfig(), EGL_NO_CONTEXT, contextAttributes);
EGLint pbufferAttribs[] = {
EGL_WIDTH, texwidth,
EGL_HEIGHT, texheight,
EGL_NONE
};
eglSurface = eglCreatePbufferSurface(eglDisplay, g_Windowing.GetEGLConfig(), pbufferAttribs);
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
CheckGlError("stf init");
static const EGLint imageAttributes[] = {
EGL_IMAGE_PRESERVED_KHR, EGL_FALSE,
EGL_GL_TEXTURE_LEVEL_KHR, 0,
EGL_NONE
};
for (int i=0; i<NUMFBOTEX; ++i)
{
glGenTextures(1, &(slots[i].texid));
glBindTexture(GL_TEXTURE_2D, slots[i].texid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texwidth, texheight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
slots[i].eglimg = eglCreateImageKHR(eglDisplay, eglContext, EGL_GL_TEXTURE_2D_KHR, (EGLClientBuffer)(slots[i].texid),imageAttributes);
free_queue.push_back(std::pair<EGLImageKHR, int>(slots[i].eglimg, i));
}
glBindTexture(GL_TEXTURE_2D, 0);
fbo.Initialize();
OES_shader_setUp();
eglInitialized = true;
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s: >>> Initialized EGL: w:%d; h:%d\n", CLASSNAME, texwidth, texheight);
#endif
}
void UninitializeEGL()
{
fbo.Cleanup();
for (int i=0; i<NUMFBOTEX; ++i)
{
glDeleteTextures(1, &(slots[i].texid));
eglDestroyImageKHR(eglDisplay, slots[i].eglimg);
}
if (eglContext != EGL_NO_CONTEXT)
eglDestroyContext(eglDisplay, eglContext);
eglContext = EGL_NO_CONTEXT;
if (eglSurface != EGL_NO_SURFACE)
eglDestroySurface(eglDisplay, eglSurface);
eglSurface = EGL_NO_SURFACE;
eglInitialized = false;
}
CStageFrightDecodeThread* decode_thread;
sp<MediaSource> source;
sp<ANativeWindow> natwin;
MediaBuffer* inbuf[INBUFCOUNT];
GLuint mPgm;
GLint mPositionHandle;
GLint mTexSamplerHandle;
GLint mTexMatrixHandle;
CFrameBufferObject fbo;
EGLDisplay eglDisplay;
EGLSurface eglSurface;
EGLContext eglContext;
bool eglInitialized;
struct tex_slot
{
GLuint texid;
EGLImageKHR eglimg;
};
tex_slot slots[NUMFBOTEX];
std::list< std::pair<EGLImageKHR, int> > free_queue;
std::list< std::pair<EGLImageKHR, int> > busy_queue;
sp<MetaData> meta;
int64_t framecount;
map<int64_t, Frame*> in_queue;
map<int64_t, Frame*> out_queue;
pthread_mutex_t in_mutex;
pthread_cond_t in_condition;
pthread_mutex_t out_mutex;
pthread_cond_t out_condition;
pthread_mutex_t free_mutex;
int quirks;
Frame *cur_frame;
Frame *prev_frame;
bool source_done;
int x, y;
int width, height;
int texwidth, texheight;
OMXClient *client;
sp<MediaSource> decoder;
const char *decoder_component;
int videoColorFormat;
int videoStride;
int videoSliceHeight;
bool drop_state;
bool resetting;
#if defined(DEBUG_VERBOSE)
unsigned int cycle_time;
#endif
};
/********************************************/
class CStageFrightDecodeThread : public CThread
{
protected:
CStageFrightVideoPrivate *p;
public:
CStageFrightDecodeThread(CStageFrightVideoPrivate *priv)
: CThread("CStageFrightDecodeThread")
, p(priv)
{}
void OnStartup()
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s: entering decode thread\n", CLASSNAME);
#endif
}
void OnExit()
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s: exited decode thread\n", CLASSNAME);
#endif
}
void Process()
{
Frame* frame;
int32_t w, h, dw, dh;
int decode_done = 0;
int32_t keyframe = 0;
int32_t unreadable = 0;
MediaSource::ReadOptions readopt;
// GLuint texid;
//SetPriority(THREAD_PRIORITY_ABOVE_NORMAL);
do
{
#if defined(DEBUG_VERBOSE)
unsigned int time = XbmcThreads::SystemClockMillis();
CLog::Log(LOGDEBUG, "%s: >>> Handling frame\n", CLASSNAME);
#endif
p->cur_frame = NULL;
frame = (Frame*)malloc(sizeof(Frame));
if (!frame)
{
frame->status = VC_ERROR;
decode_done = 1;
continue;
}
frame->eglimg = EGL_NO_IMAGE_KHR;
frame->medbuf = NULL;
if (p->resetting)
{
readopt.setSeekTo(0);
p->resetting = false;
}
frame->status = p->decoder->read(&frame->medbuf, &readopt);
readopt.clearSeekTo();
if (frame->status == OK)
{
if (!frame->medbuf->graphicBuffer().get()) // hw buffers
{
if (frame->medbuf->range_length() == 0)
{
CLog::Log(LOGERROR, "%s - Invalid buffer\n", CLASSNAME);
frame->status = VC_ERROR;
decode_done = 1;
frame->medbuf->release();
frame->medbuf = NULL;
}
else
frame->format = RENDER_FMT_YUV420P;
}
else
frame->format = RENDER_FMT_EGLIMG;
}
if (frame->status == OK)
{
sp<MetaData> outFormat = p->decoder->getFormat();
outFormat->findInt32(kKeyWidth , &w);
outFormat->findInt32(kKeyHeight, &h);
if (!outFormat->findInt32(kKeyDisplayWidth , &dw))
dw = w;
if (!outFormat->findInt32(kKeyDisplayHeight, &dh))
dh = h;
if (!outFormat->findInt32(kKeyIsSyncFrame, &keyframe))
keyframe = 0;
if (!outFormat->findInt32(kKeyIsUnreadable, &unreadable))
unreadable = 0;
frame->pts = 0;
// The OMX.SEC decoder doesn't signal the modified width/height
if (p->decoder_component && !strncmp(p->decoder_component, "OMX.SEC", 7) &&
(w & 15 || h & 15))
{
if (((w + 15)&~15) * ((h + 15)&~15) * 3/2 == frame->medbuf->range_length())
{
w = (w + 15)&~15;
h = (h + 15)&~15;
}
}
frame->width = w;
frame->height = h;
frame->medbuf->meta_data()->findInt64(kKeyTime, &(frame->pts));
}
else if (frame->status == INFO_FORMAT_CHANGED)
{
int32_t cropLeft, cropTop, cropRight, cropBottom;
sp<MetaData> outFormat = p->decoder->getFormat();
outFormat->findInt32(kKeyWidth , &p->width);
outFormat->findInt32(kKeyHeight, &p->height);
cropLeft = cropTop = cropRight = cropBottom = 0;
if (!outFormat->findRect(kKeyCropRect, &cropLeft, &cropTop, &cropRight, &cropBottom))
{
p->x = 0;
p->y = 0;
}
else
{
p->x = cropLeft;
p->y = cropTop;
p->width = cropRight - cropLeft + 1;
p->height = cropBottom - cropTop + 1;
}
outFormat->findInt32(kKeyColorFormat, &p->videoColorFormat);
if (!outFormat->findInt32(kKeyStride, &p->videoStride))
p->videoStride = p->width;
if (!outFormat->findInt32(kKeySliceHeight, &p->videoSliceHeight))
p->videoSliceHeight = p->height;
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, ">>> new format col:%d, w:%d, h:%d, sw:%d, sh:%d, ctl:%d,%d; cbr:%d,%d\n", p->videoColorFormat, p->width, p->height, p->videoStride, p->videoSliceHeight, cropTop, cropLeft, cropBottom, cropRight);
#endif
if (frame->medbuf)
frame->medbuf->release();
frame->medbuf = NULL;
free(frame);
continue;
}
else
{
CLog::Log(LOGERROR, "%s - decoding error (%d)\n", CLASSNAME,frame->status);
decode_done = 1;
if (frame->medbuf)
frame->medbuf->release();
frame->medbuf = NULL;
}
if (frame->format == RENDER_FMT_EGLIMG)
{
if (!p->eglInitialized)
{
p->InitializeEGL(frame->width, frame->height);
}
else if (p->texwidth != frame->width || p->texheight != frame->height)
{
p->UninitializeEGL();
p->InitializeEGL(frame->width, frame->height);
}
if (p->free_queue.empty())
{
CLog::Log(LOGERROR, "%s::%s - Error: No free output buffers\n", CLASSNAME, __func__);
if (frame->medbuf) {
frame->medbuf->release();
}
free(frame);
continue;;
}
android::GraphicBuffer* graphicBuffer = static_cast<android::GraphicBuffer*>(frame->medbuf->graphicBuffer().get() );
native_window_set_buffers_timestamp(p->natwin.get(), frame->pts * 1000);
int err = p->natwin.get()->queueBuffer(p->natwin.get(), graphicBuffer);
if (err == 0)
frame->medbuf->meta_data()->setInt32(kKeyRendered, 1);
frame->medbuf->release();
frame->medbuf = NULL;
g_xbmcapp.UpdateStagefrightTexture();
// g_xbmcapp.GetSurfaceTexture()->updateTexImage();
if (!p->drop_state)
{
// static const EGLint eglImgAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE, EGL_NONE };
// EGLImageKHR img = eglCreateImageKHR(p->eglDisplay, EGL_NO_CONTEXT,
// EGL_NATIVE_BUFFER_ANDROID,
// (EGLClientBuffer)graphicBuffer->getNativeBuffer(),
// eglImgAttrs);
pthread_mutex_lock(&p->free_mutex);
std::list<std::pair<EGLImageKHR, int> >::iterator it = p->free_queue.begin();
int cur_slot = it->second;
pthread_mutex_unlock(&p->free_mutex);
p->fbo.BindToTexture(GL_TEXTURE_2D, p->slots[cur_slot].texid);
p->fbo.BeginRender();
glDisable(GL_DEPTH_TEST);
//glClear(GL_COLOR_BUFFER_BIT);
const GLfloat triangleVertices[] = {
-1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, -1.0f,
1.0f, 1.0f,
};
glVertexAttribPointer(p->mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, triangleVertices);
glEnableVertexAttribArray(p->mPositionHandle);
glUseProgram(p->mPgm);
glUniform1i(p->mTexSamplerHandle, 0);
// glGenTextures(1, &texid);
// glBindTexture(GL_TEXTURE_EXTERNAL_OES, texid);
// glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, img);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, g_xbmcapp.GetAndroidTexture());
GLfloat texMatrix[16];
// const GLfloat texMatrix[] = {
// 1, 0, 0, 0,
// 0, -1, 0, 0,
// 0, 0, 1, 0,
// 0, 1, 0, 1
// };
g_xbmcapp.GetStagefrightTransformMatrix(texMatrix);
// g_xbmcapp.GetSurfaceTexture()->getTransformMatrix(texMatrix);
glUniformMatrix4fv(p->mTexMatrixHandle, 1, GL_FALSE, texMatrix);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
// glDeleteTextures(1, &texid);
// eglDestroyImageKHR(p->eglDisplay, img);
p->fbo.EndRender();
glBindTexture(GL_TEXTURE_2D, 0);
frame->eglimg = p->slots[cur_slot].eglimg;
}
}
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s: >>> pushed OUT frame; w:%d, h:%d, dw:%d, dh:%d, kf:%d, ur:%d, tm:%d\n", CLASSNAME, frame->width, frame->height, dw, dh, keyframe, unreadable, XbmcThreads::SystemClockMillis() - time);
#endif
pthread_mutex_lock(&p->out_mutex);
p->cur_frame = frame;
while (p->cur_frame)
pthread_cond_wait(&p->out_condition, &p->out_mutex);
pthread_mutex_unlock(&p->out_mutex);
}
while (!decode_done && !m_bStop);
if (p->eglInitialized)
p->UninitializeEGL();
}
};
/***********************************************************/
class CustomSource : public MediaSource
{
public:
CustomSource(CStageFrightVideoPrivate *priv, sp<MetaData> meta)
{
p = priv;
source_meta = meta;
}
virtual sp<MetaData> getFormat()
{
return source_meta;
}
virtual status_t start(MetaData *params)
{
return OK;
}
virtual status_t stop()
{
return OK;
}
virtual status_t read(MediaBuffer **buffer,
const MediaSource::ReadOptions *options)
{
Frame *frame;
status_t ret;
*buffer = NULL;
int64_t time_us = -1;
MediaSource::ReadOptions::SeekMode mode;
if (options && options->getSeekTo(&time_us, &mode))
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s: reading source(%d): seek:%llu\n", CLASSNAME,p->in_queue.size(), time_us);
#endif
}
else
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s: reading source(%d)\n", CLASSNAME,p->in_queue.size());
#endif
}
pthread_mutex_lock(&p->in_mutex);
while (p->in_queue.empty() && p->decode_thread)
pthread_cond_wait(&p->in_condition, &p->in_mutex);
if (p->in_queue.empty())
{
pthread_mutex_unlock(&p->in_mutex);
return VC_ERROR;
}
std::map<int64_t,Frame*>::iterator it = p->in_queue.begin();
frame = it->second;
ret = frame->status;
*buffer = frame->medbuf;
p->in_queue.erase(it);
pthread_mutex_unlock(&p->in_mutex);
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, ">>> exiting reading source(%d); pts:%llu\n", p->in_queue.size(),frame->pts);
#endif
free(frame);
return ret;
}
private:
sp<MetaData> source_meta;
CStageFrightVideoPrivate *p;
};
/***********************************************************/
bool CStageFrightVideo::Open(CDVDStreamInfo &hints)
{
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s::Open\n", CLASSNAME);
#endif
CSingleLock lock(g_graphicsContext);
// stagefright crashes with null size. Trap this...
if (!hints.width || !hints.height)
{
CLog::Log(LOGERROR, "%s::%s - %s\n", CLASSNAME, __func__,"null size, cannot handle");
return false;
}
p = new CStageFrightVideoPrivate;
p->width = hints.width;
p->height = hints.height;
if (g_advancedSettings.m_stagefrightConfig.useSwRenderer)
p->quirks |= QuirkSWRender;
sp<MetaData> outFormat;
int32_t cropLeft, cropTop, cropRight, cropBottom;
p->meta = new MetaData;
if (p->meta == NULL)
{
goto fail;
}
const char* mimetype;
switch (hints.codec)
{
case CODEC_ID_H264:
if (g_advancedSettings.m_stagefrightConfig.useAVCcodec == 0)
return false;
mimetype = MEDIA_MIMETYPE_VIDEO_AVC;
if ( *(char*)hints.extradata == 1 )
p->meta->setData(kKeyAVCC, kTypeAVCC, hints.extradata, hints.extrasize);
break;
case CODEC_ID_MPEG4:
if (g_advancedSettings.m_stagefrightConfig.useMP4codec == 0)
return false;
mimetype = MEDIA_MIMETYPE_VIDEO_MPEG4;
break;
case CODEC_ID_MPEG2VIDEO:
if (g_advancedSettings.m_stagefrightConfig.useMPEG2codec == 0)
return false;
mimetype = MEDIA_MIMETYPE_VIDEO_MPEG2;
break;
case CODEC_ID_VP3:
case CODEC_ID_VP6:
case CODEC_ID_VP6F:
case CODEC_ID_VP8:
if (g_advancedSettings.m_stagefrightConfig.useVPXcodec == 0)
return false;
mimetype = MEDIA_MIMETYPE_VIDEO_VPX;
break;
case CODEC_ID_VC1:
case CODEC_ID_WMV3:
if (g_advancedSettings.m_stagefrightConfig.useVC1codec == 0)
return false;
mimetype = MEDIA_MIMETYPE_VIDEO_WMV;
break;
default:
return false;
break;
}
p->meta->setCString(kKeyMIMEType, mimetype);
p->meta->setInt32(kKeyWidth, p->width);
p->meta->setInt32(kKeyHeight, p->height);
android::ProcessState::self()->startThreadPool();
p->source = new CustomSource(p, p->meta);
p->client = new OMXClient;
if (p->source == NULL || p->client == NULL)
{
goto fail;
}
if (p->client->connect() != OK)
{
delete p->client;
p->client = NULL;
CLog::Log(LOGERROR, "%s::%s - %s\n", CLASSNAME, __func__,"Cannot connect OMX client");
goto fail;
}
p->natwin = NULL;
if ((p->quirks & QuirkSWRender) == 0)
{
g_xbmcapp.InitStagefrightSurface();
p->natwin = g_xbmcapp.GetAndroidVideoWindow();
native_window_api_connect(p->natwin.get(), NATIVE_WINDOW_API_MEDIA);
if (!eglCreateImageKHR)
GETEXTENSION(PFNEGLCREATEIMAGEKHRPROC, eglCreateImageKHR);
if (!eglDestroyImageKHR)
GETEXTENSION(PFNEGLDESTROYIMAGEKHRPROC, eglDestroyImageKHR);
if (!glEGLImageTargetTexture2DOES)
GETEXTENSION(PFNGLEGLIMAGETARGETTEXTURE2DOESPROC, glEGLImageTargetTexture2DOES);
}
p->decoder = OMXCodec::Create(p->client->interface(), p->meta,
false, p->source, NULL,
OMXCodec::kHardwareCodecsOnly | (p->quirks & QuirkSWRender ? OMXCodec::kClientNeedsFramebuffer : 0),
p->natwin
);
if (!(p->decoder != NULL && p->decoder->start() == OK))
{
p->decoder = NULL;
goto fail;
}
outFormat = p->decoder->getFormat();
if (!outFormat->findInt32(kKeyWidth, &p->width) || !outFormat->findInt32(kKeyHeight, &p->height)
|| !outFormat->findInt32(kKeyColorFormat, &p->videoColorFormat))
goto fail;
const char *component;
if (outFormat->findCString(kKeyDecoderComponent, &component))
{
CLog::Log(LOGDEBUG, "%s::%s - component: %s\n", CLASSNAME, __func__, component);
//Blacklist
if (!strncmp(component, "OMX.Nvidia.mp4.decode", 21) && g_advancedSettings.m_stagefrightConfig.useMP4codec != 1)
{
// Has issues with some XVID encoded MP4. Only fails after actual decoding starts...
CLog::Log(LOGERROR, "%s::%s - %s\n", CLASSNAME, __func__,"Blacklisted component (MP4)");
goto fail;
}
else if (!strncmp(component, "OMX.rk.", 7) && g_advancedSettings.m_stagefrightConfig.useAVCcodec != 1 && g_advancedSettings.m_stagefrightConfig.useMP4codec != 1)
{
if (p->width % 32 != 0 || p->height % 16 != 0)
{
// Buggy. Hard crash on non MOD16 height videos and stride errors for non MOD32 width
CLog::Log(LOGERROR, "%s::%s - %s\n", CLASSNAME, __func__,"Blacklisted component (MOD16)");
goto fail;
}
}
}
cropLeft = cropTop = cropRight = cropBottom = 0;
if (!outFormat->findRect(kKeyCropRect, &cropLeft, &cropTop, &cropRight, &cropBottom))
{
p->x = 0;
p->y = 0;
}
else
{
p->x = cropLeft;
p->y = cropTop;
p->width = cropRight - cropLeft + 1;
p->height = cropBottom - cropTop + 1;
}
if (!outFormat->findInt32(kKeyStride, &p->videoStride))
p->videoStride = p->width;
if (!outFormat->findInt32(kKeySliceHeight, &p->videoSliceHeight))
p->videoSliceHeight = p->height;
for (int i=0; i<INBUFCOUNT; ++i)
{
p->inbuf[i] = new MediaBuffer(300000);
p->inbuf[i]->setObserver(p);
}
pthread_mutex_init(&p->in_mutex, NULL);
pthread_cond_init(&p->in_condition, NULL);
pthread_mutex_init(&p->out_mutex, NULL);
pthread_cond_init(&p->out_condition, NULL);
pthread_mutex_init(&p->free_mutex, NULL);
p->decode_thread = new CStageFrightDecodeThread(p);
p->decode_thread->Create(true /*autodelete*/);
#if defined(DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, ">>> format col:%d, w:%d, h:%d, sw:%d, sh:%d, ctl:%d,%d; cbr:%d,%d\n", p->videoColorFormat, p->width, p->height, p->videoStride, p->videoSliceHeight, cropTop, cropLeft, cropBottom, cropRight);
#endif
return true;
fail:
if (p->decoder != 0)
p->decoder->stop();
if (p->client)
{
p->client->disconnect();
delete p->client;
}
if (p->decoder_component)
free(&p->decoder_component);
if ((p->quirks & QuirkSWRender) == 0)
g_xbmcapp.UninitStagefrightSurface();
return false;
}
/*** Decode ***/
int CStageFrightVideo::Decode(BYTE *pData, int iSize, double dts, double pts)
{
#if defined(DEBUG_VERBOSE)
unsigned int time = XbmcThreads::SystemClockMillis();
CLog::Log(LOGDEBUG, "%s::Decode - d:%p; s:%d; dts:%f; pts:%f\n", CLASSNAME, pData, iSize, dts, pts);
#endif