00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "stdafx.h"
00033 #include "theoraencodeinputpin.h"
00034
00035 TheoraEncodeInputPin::TheoraEncodeInputPin(AbstractTransformFilter* inParentFilter, CCritSec* inFilterLock, AbstractTransformOutputPin* inOutputPin, vector<CMediaType*> inAcceptableMediaTypes)
00036 : AbstractTransformInputPin(inParentFilter, inFilterLock, inOutputPin, NAME("TheoraEncodeInputPin"), L"YV12 In", inAcceptableMediaTypes)
00037 , mXOffset(0)
00038 , mYOffset(0)
00039 , mHeight(0)
00040 , mWidth(0)
00041 , mUptoFrame(0)
00042 , mBegun(false)
00043 , mVideoFormat(NULL)
00044
00045
00046 {
00047
00048 mYUV.y = NULL;
00049 mYUV.u = NULL;
00050 mYUV.v = NULL;
00051
00052 }
00053
00054 TheoraEncodeInputPin::~TheoraEncodeInputPin(void)
00055 {
00056
00057 DestroyCodec();
00058 delete[] mYUV.y;
00059 delete[] mYUV.u;
00060 delete[] mYUV.v;
00061
00062 }
00063
00064
00065 HRESULT TheoraEncodeInputPin::deliverData(LONGLONG inStart, LONGLONG inEnd, unsigned char* inBuf, unsigned long inNumBytes) {
00066
00067
00068 IMediaSample* locSample;
00069 HRESULT locHR = mOutputPin->GetDeliveryBuffer(&locSample, &inStart, &inEnd, NULL);
00070
00071 if (locHR != S_OK) {
00072
00073 return locHR;
00074 }
00075
00076 BYTE* locBuffer = NULL;
00077
00078
00079
00080 locSample->GetPointer(&locBuffer);
00081
00082 if (locSample->GetSize() >= inNumBytes) {
00083
00084 memcpy((void*)locBuffer, (const void*)inBuf, inNumBytes);
00085
00086
00087 SetSampleParams(locSample, inNumBytes, &inStart, &inEnd);
00088
00089 {
00090 CAutoLock locLock(m_pLock);
00091
00092 HRESULT locHR = ((TheoraEncodeOutputPin*)mOutputPin)->mDataQueue->Receive(locSample);
00093 if (locHR != S_OK) {
00094 return locHR;
00095 } else {
00096 }
00097 }
00098
00099 return S_OK;
00100 } else {
00101
00102 throw 0;
00103 }
00104
00105 }
00106
00107 long TheoraEncodeInputPin::encodeYV12ToYV12(unsigned char* inBuf, long inNumBytes) {
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 unsigned char* locSourceUptoPtr = inBuf;
00148
00149
00150
00151
00152
00153
00154
00155 char* locDestUptoPtr = mYUV.y;
00156
00157
00160
00161
00162
00163
00164
00165
00166
00167
00168 if (mXOffset == 0) {
00169
00170
00171
00172 for (unsigned long line = 0; line < mHeight; line++) {
00173 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth);
00174 locSourceUptoPtr += mWidth;
00175 locDestUptoPtr += mWidth;
00176 }
00177 } else {
00178 for (unsigned long line = 0; line < mHeight; line++) {
00179
00180 memset((void*)locDestUptoPtr, NULL, mXOffset);
00181 locDestUptoPtr += mXOffset;
00182
00183
00184 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth);
00185 locSourceUptoPtr += mWidth;
00186 locDestUptoPtr += mWidth;
00187
00188
00189
00190
00191
00192
00193
00194 }
00195
00196 }
00197
00198
00199
00200 if (mYOffset != 0) {
00201 memset((void*)locDestUptoPtr, NULL, mYOffset * mYUV.y_width);
00202 locDestUptoPtr += (mYOffset * mYUV.y_width);
00203
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213 locDestUptoPtr = mYUV.v;
00214
00215
00216
00217
00218
00219
00220
00222
00223
00224
00225
00226
00228
00229
00230 if (mXOffset == 0) {
00231
00232 for (unsigned long line = 0; line < mHeight / 2; line++) {
00233 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00234 locSourceUptoPtr += (mWidth / 2);
00235 locDestUptoPtr += (mWidth / 2);
00236 }
00237 } else {
00238 for (unsigned long line = 0; line < mHeight / 2; line++) {
00239
00240 memset((void*)locDestUptoPtr, NULL, mXOffset / 2);
00241 locDestUptoPtr += (mXOffset / 2);
00242
00243
00244
00245 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00246 locSourceUptoPtr += (mWidth / 2);
00247 locDestUptoPtr += (mWidth / 2);
00248
00249
00250
00251
00252
00254
00255
00257 }
00258
00259 }
00260
00261
00262
00263 if (mYOffset != 0) {
00264 memset((void*)locDestUptoPtr, NULL, (mYOffset * mYUV.uv_width) / 2);
00265 locDestUptoPtr += ((mYOffset * mYUV.uv_width) / 2);
00266
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 locDestUptoPtr = mYUV.u;
00279
00280
00282
00283
00284
00285
00286
00288
00289
00290 if (mXOffset == 0) {
00291
00292 for (unsigned long line = 0; line < mHeight / 2; line++) {
00293 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00294 locSourceUptoPtr += (mWidth / 2);
00295 locDestUptoPtr += (mWidth / 2);
00296 }
00297 } else {
00298 for (unsigned long line = 0; line < mHeight / 2; line++) {
00299
00300 memset((void*)locDestUptoPtr, NULL, mXOffset / 2);
00301 locDestUptoPtr += (mXOffset / 2);
00302
00303
00304
00305 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00306 locSourceUptoPtr += (mWidth / 2);
00307 locDestUptoPtr += (mWidth / 2);
00308
00309
00310
00311
00312
00314
00315
00317 }
00318
00319 }
00320
00321
00322
00323 if (mYOffset != 0) {
00324 memset((void*)locDestUptoPtr, NULL, (mYOffset * mYUV.uv_width) / 2);
00325 locDestUptoPtr += ((mYOffset * mYUV.uv_width) / 2);
00326
00327 }
00328
00329
00330 return 0;
00331
00332 }
00333
00334
00335
00336 long TheoraEncodeInputPin::encodeIYUVToYV12(unsigned char* inBuf, long inNumBytes) {
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 unsigned char* locSourceUptoPtr = inBuf;
00384
00385
00386
00387
00388
00389
00390
00391 char* locDestUptoPtr = mYUV.y;
00392
00393
00395
00396
00397
00398
00399
00400
00401
00402
00403 if (mXOffset == 0) {
00404
00405 for (unsigned long line = 0; line < mHeight; line++) {
00406 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth);
00407 locSourceUptoPtr += mWidth;
00408 locDestUptoPtr += mWidth;
00409 }
00410 } else {
00411 for (unsigned long line = 0; line < mHeight; line++) {
00412
00413 memset((void*)locDestUptoPtr, NULL, mXOffset);
00414 locDestUptoPtr += mXOffset;
00415
00416
00417 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth);
00418 locSourceUptoPtr += mWidth;
00419 locDestUptoPtr += mWidth;
00420
00422
00423
00424 }
00425
00426 }
00427
00428
00429 if (mYOffset != 0) {
00430 memset((void*)locDestUptoPtr, NULL, mYOffset * mYUV.y_width);
00431 locDestUptoPtr += (mYOffset * mYUV.y_width);
00432
00433 }
00434
00435
00436
00437
00438
00439
00440 locDestUptoPtr = mYUV.u;
00441
00442
00444
00445
00446
00447
00448
00449
00450
00451
00452 if (mXOffset == 0) {
00453
00454 for (unsigned long line = 0; line < mHeight / 2; line++) {
00455 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00456 locSourceUptoPtr += (mWidth / 2);
00457 locDestUptoPtr += (mWidth / 2);
00458 }
00459 } else {
00460 for (unsigned long line = 0; line < mHeight / 2; line++) {
00461
00462 memset((void*)locDestUptoPtr, NULL, mXOffset / 2);
00463 locDestUptoPtr += (mXOffset / 2);
00464
00465
00466
00467 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00468 locSourceUptoPtr += (mWidth / 2);
00469 locDestUptoPtr += (mWidth / 2);
00470
00472
00473
00475 }
00476
00477 }
00478
00479
00480 if (mYOffset != 0) {
00481 memset((void*)locDestUptoPtr, NULL, (mYOffset * mYUV.uv_width) / 2);
00482 locDestUptoPtr += ((mYOffset * mYUV.uv_width) / 2);
00483
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493 locDestUptoPtr = mYUV.v;
00494
00495
00497
00498
00499
00500
00501
00503
00504
00505 if (mXOffset == 0) {
00506
00507 for (unsigned long line = 0; line < mHeight / 2; line++) {
00508 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00509 locSourceUptoPtr += (mWidth / 2);
00510 locDestUptoPtr += (mWidth / 2);
00511 }
00512 } else {
00513 for (unsigned long line = 0; line < mHeight / 2; line++) {
00514
00515 memset((void*)locDestUptoPtr, NULL, mXOffset / 2);
00516 locDestUptoPtr += (mXOffset / 2);
00517
00518
00519
00520 memcpy((void*)locDestUptoPtr, (const void*)locSourceUptoPtr, mWidth / 2);
00521 locSourceUptoPtr += (mWidth / 2);
00522 locDestUptoPtr += (mWidth / 2);
00523
00525
00526
00528 }
00529
00530 }
00531
00532
00533 if (mYOffset != 0) {
00534 memset((void*)locDestUptoPtr, NULL, (mYOffset * mYUV.uv_width) / 2);
00535 locDestUptoPtr += ((mYOffset * mYUV.uv_width) / 2);
00536
00537 }
00538
00539
00540
00541
00542
00543
00544
00545 return 0;
00546
00547 }
00548
00549
00550 long TheoraEncodeInputPin::encodeRGB24toYV12(unsigned char* inBuf, long inNumBytes) {
00551
00552 unsigned long locNumPixels = (inNumBytes/3);
00553 unsigned char* locAYUVBuf = new unsigned char[locNumPixels<<2];
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628 const int KR = 19596;
00629 const int KB = 7472;
00630
00631 const int ROUNDER = 32768;
00632
00633 const int G_FACTOR = 38470;
00634 const int U_FACTOR = 12716213;
00635 const int V_FACTOR = 10061022;
00636
00637 int locL = 0;
00638 int locB = 0;
00639 int locR = 0;
00640
00641
00642 unsigned char* locDestPtr = locAYUVBuf;
00643
00644
00645
00646
00647 unsigned char* locSourceEnds = inBuf + (locNumPixels * 3);
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 unsigned char* locColSourcePtr = NULL;
00668 unsigned char* locColEndPtr = NULL;
00669 unsigned long locLineLength = mWidth * 3;
00670 unsigned long col = 0;
00671 for (unsigned char* locSourcePtr = locSourceEnds - locLineLength; locSourcePtr >= inBuf; locSourcePtr -= locLineLength) {
00672
00673
00674
00675 locColSourcePtr = locSourcePtr;
00676 locColEndPtr = locColSourcePtr + locLineLength;
00677 while (locColSourcePtr < locColEndPtr) {
00678 locB = locColSourcePtr[0];
00679 locL = KB * (locB);
00680
00681 locL += G_FACTOR * (locColSourcePtr[1]);
00682
00683 locR = locColSourcePtr[2];
00684 locL += KR * (locR);
00685
00686
00687 *(locDestPtr++) = CLIP3(0, 255, ((112 * ( (locR<<16) - locL)) / V_FACTOR) + 128);
00688 *(locDestPtr++) = CLIP3(0, 255, ((112 * ( (locB<<16) - locL)) / U_FACTOR) + 128);
00689 *(locDestPtr++) = CLIP3(0, 255, locL >> 16);
00690 *(locDestPtr++) = 255;
00691
00692
00693 locColSourcePtr+=3;
00694
00695 }
00696
00697
00698 }
00699
00700
00701
00702 encodeAYUVtoYV12(locAYUVBuf, locNumPixels<<2);
00703 delete[] locAYUVBuf;
00704 locAYUVBuf = NULL;
00705
00706 return 0;
00707 }
00708
00709
00710
00711 long TheoraEncodeInputPin::encodeRGB32toYV12(unsigned char* inBuf, long inNumBytes) {
00712
00713
00714
00715 unsigned long locNumPixels = (inNumBytes/4);
00716
00717
00718
00719 unsigned char* locAYUVBuf = new unsigned char[inNumBytes];
00720
00721
00722
00723
00724 const int KR = 19596;
00725 const int KB = 7472;
00726
00727 const int ROUNDER = 32768;
00728
00729 const int G_FACTOR = 38470;
00730 const int U_FACTOR = 12716213;
00731 const int V_FACTOR = 10061022;
00732
00733 int locL = 0;
00734 int locB = 0;
00735 int locR = 0;
00736
00737
00738 unsigned char* locDestPtr = locAYUVBuf;
00739
00740
00741
00742
00743 unsigned char* locSourceEnds = inBuf + (inNumBytes);
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 unsigned char* locColSourcePtr = NULL;
00770 unsigned char* locColEndPtr = NULL;
00771 unsigned long locLineLength = mWidth * 4;
00772 unsigned long col = 0;
00773 for (unsigned char* locSourcePtr = locSourceEnds - locLineLength; locSourcePtr >= inBuf; locSourcePtr -= locLineLength) {
00774
00775
00776
00777 locColSourcePtr = locSourcePtr;
00778 locColEndPtr = locColSourcePtr + locLineLength;
00779 while (locColSourcePtr < locColEndPtr) {
00780 locB = locColSourcePtr[0];
00781 locL = KB * (locB);
00782
00783 locL += G_FACTOR * (locColSourcePtr[1]);
00784
00785 locR = locColSourcePtr[2];
00786 locL += KR * (locR);
00787
00788
00789 *(locDestPtr++) = CLIP3(0, 255, ((112 * ( (locR<<16) - locL)) / V_FACTOR) + 128);
00790 *(locDestPtr++) = CLIP3(0, 255, ((112 * ( (locB<<16) - locL)) / U_FACTOR) + 128);
00791 *(locDestPtr++) = CLIP3(0, 255, locL >> 16);
00792 *(locDestPtr++) = locColSourcePtr[3];
00793
00794
00795 locColSourcePtr+=4;
00796
00797 }
00798
00799
00800 }
00801
00802
00803
00804
00805
00806 ASSERT(locDestPtr == (locAYUVBuf + inNumBytes));
00807
00808
00809
00810
00811 encodeAYUVtoYV12(locAYUVBuf, inNumBytes);
00812 delete[] locAYUVBuf;
00813 locAYUVBuf = NULL;
00814
00815 return 0;
00816 }
00817
00818
00819
00820
00821 long TheoraEncodeInputPin::encodeAYUVtoYV12(unsigned char* inBuf, long inNumBytes) {
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 const int PIXEL_BYTE_SIZE = 4;
00836 ASSERT (mHeight % 2 == 0);
00837 ASSERT (mWidth % 2 == 0);
00838
00839 unsigned char* locSourceUptoPtr = inBuf;
00840 unsigned char* locSourceNextLine = locSourceUptoPtr + (mWidth * PIXEL_BYTE_SIZE);
00841
00842 char* locYUpto = mYUV.y;
00843 char* locUUpto = mYUV.u;
00844 char* locVUpto = mYUV.v;
00845
00846 char* locDestNextLine = locYUpto + (mYUV.y_stride);
00847
00848 int temp = 0;
00849
00850
00851 for (unsigned long line = 0; line < mHeight; line += 2) {
00852
00853
00854
00855 locYUpto = (mYUV.y + (line * mYUV.y_stride));
00856
00857 locDestNextLine = locYUpto + (mYUV.y_stride);
00858
00859
00860
00861
00862 ASSERT (locSourceUptoPtr == (inBuf + (line * (mWidth * PIXEL_BYTE_SIZE))));
00863 ASSERT (locSourceNextLine == locSourceUptoPtr + (mWidth * PIXEL_BYTE_SIZE));
00864 ASSERT (locYUpto == (mYUV.y + (line * mYUV.y_stride)));
00865 ASSERT (locDestNextLine == locYUpto + (mYUV.y_stride));
00866
00867
00868
00869 if (mXOffset != 0) {
00870 memset((void*)locYUpto, 0, mXOffset);
00871 memset((void*)locDestNextLine, 0, mXOffset);
00872 memset((void*)locUUpto, 0, mXOffset/2);
00873 memset((void*)locVUpto, 0, mXOffset/2);
00874 locYUpto += mXOffset;
00875 locDestNextLine += mXOffset;
00876 locUUpto += (mXOffset/2);
00877 locVUpto += (mXOffset/2);
00878 }
00879
00880 for (unsigned long col = 0; col < mWidth; col += 2) {
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894 temp = *(locSourceUptoPtr++);
00895
00896
00897
00898
00899
00900
00901 temp += *(locSourceUptoPtr + 3);
00902 temp += *(locSourceNextLine++);
00903
00904
00905
00906
00907
00908 temp += *(locSourceNextLine + 3);
00909 temp >>= 2;
00910 *(locVUpto++) = (unsigned char)temp;
00911
00912
00913
00914
00915
00916
00917
00918
00919 temp = *(locSourceUptoPtr++);
00920
00921
00922
00923
00924
00925 temp += *(locSourceUptoPtr + 3);
00926 temp += *(locSourceNextLine++);
00927
00928
00929
00930
00931
00932
00933 temp += *(locSourceNextLine + 3);
00934 temp >>= 2;
00935 *(locUUpto++) = (unsigned char)temp;
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945 *(locYUpto++) = *(locSourceUptoPtr++);
00946 *(locDestNextLine++) = *(locSourceNextLine++);
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968 locSourceUptoPtr += 3;
00969 *(locYUpto++) = *(locSourceUptoPtr);
00970 locSourceUptoPtr += 2;
00971
00972
00973
00974
00975
00976
00977
00978 locSourceNextLine += 3;
00979 *(locDestNextLine++) = *(locSourceNextLine);
00980 locSourceNextLine += 2;
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992 }
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007 locSourceUptoPtr = locSourceNextLine;
01008
01009 locSourceNextLine += (mWidth * PIXEL_BYTE_SIZE);
01010
01011
01012
01013 }
01014 return 0;
01015 }
01016 long TheoraEncodeInputPin::encodeYUY2ToYV12(unsigned char* inBuf, long inNumBytes) {
01017
01018
01019
01020 unsigned char* locSourceUptoPtr = inBuf;
01021
01022
01023
01024
01025 char* locYUpto = mYUV.y;
01026 char* locUUpto = mYUV.u;
01027 char* locVUpto = mYUV.v;
01028
01029
01030
01031
01032
01033
01034 for (unsigned long i = 0; i < mHeight / 2; i++) {
01035
01036
01037
01038
01039
01040
01041
01042 for (unsigned long j = 0; j < mWidth / 2; j++) {
01043 *(locYUpto++) = *(locSourceUptoPtr++);
01044 *(locUUpto++) = *(locSourceUptoPtr++);
01045 *(locYUpto++) = *(locSourceUptoPtr++);
01046 *(locVUpto++) = *(locSourceUptoPtr++);
01047 }
01048
01049
01050
01051 for (unsigned long j = 0; j < mWidth / 2; j++) {
01052
01053 *(locYUpto++) = *(locSourceUptoPtr++);
01054 locSourceUptoPtr++;
01055 *(locYUpto++) = *(locSourceUptoPtr++);
01056 locSourceUptoPtr++;
01057 }
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074 }
01075 return 0;
01076 }
01077
01078 long TheoraEncodeInputPin::encodeYVYUToYV12(unsigned char* inBuf, long inNumBytes) {
01079
01080 unsigned char* locSourceUptoPtr = inBuf;
01081
01082
01083
01084
01085
01086 char* locYUpto = mYUV.y;
01087 char* locUUpto = mYUV.u;
01088 char* locVUpto = mYUV.v;
01089
01090
01091
01092
01093
01094
01095 for (unsigned long i = 0; i < mHeight / 2; i++) {
01096
01097
01098
01099
01100 for (unsigned long j = 0; j < mWidth / 2; j++) {
01101 *(locYUpto++) = *(locSourceUptoPtr++);
01102 *(locVUpto++) = *(locSourceUptoPtr++);
01103
01104 *(locYUpto++) = *(locSourceUptoPtr++);
01105 *(locUUpto++) = *(locSourceUptoPtr++);
01106 }
01107
01108
01109
01110 for (unsigned long j = 0; j < mWidth / 2; j++) {
01111
01112 *(locYUpto++) = *(locSourceUptoPtr++);
01113 locSourceUptoPtr++;
01114 *(locYUpto++) = *(locSourceUptoPtr++);
01115 locSourceUptoPtr++;
01116 }
01117
01118 }
01119 return 0;
01120 }
01121
01122
01123 long TheoraEncodeInputPin::encodeUYVYToYV12(unsigned char* inBuf, long inNumBytes) {
01124
01125 unsigned char* locSourceUptoPtr = inBuf;
01126
01127
01128
01129
01130
01131 char* locYUpto = mYUV.y;
01132 char* locUUpto = mYUV.u;
01133 char* locVUpto = mYUV.v;
01134
01135
01136
01137
01138
01139
01140 for (unsigned long i = 0; i < mHeight / 2; i++) {
01141
01142
01143
01144 for (unsigned long j = 0; j < mWidth / 2; j++) {
01145 *(locUUpto++) = *(locSourceUptoPtr++);
01146 *(locYUpto++) = *(locSourceUptoPtr++);
01147
01148 *(locVUpto++) = *(locSourceUptoPtr++);
01149 *(locYUpto++) = *(locSourceUptoPtr++);
01150
01151 }
01152
01153
01154
01155 for (unsigned long j = 0; j < mWidth / 2; j++) {
01156
01157
01158 locSourceUptoPtr++;
01159 *(locYUpto++) = *(locSourceUptoPtr++);
01160
01161 locSourceUptoPtr++;
01162 *(locYUpto++) = *(locSourceUptoPtr++);
01163
01164 }
01165
01166
01167 }
01168 return 0;
01169 }
01170
01171
01172
01173
01174 long TheoraEncodeInputPin::TransformData(unsigned char* inBuf, long inNumBytes) {
01175
01176
01177
01178
01179
01180 LONGLONG locFrameStart = mUptoFrame;
01181 LONGLONG locFrameEnd = 0;
01182 HRESULT locHR = S_OK;
01183 if (!mBegun) {
01184
01185 mBegun = true;
01186
01187 StampedOggPacket** locHeaders;
01188 locHeaders = mTheoraEncoder.initCodec(mTheoraInfo);
01189
01190 for (int i = 0; i < 3; i++) {
01191 locHR = deliverData(0,0,locHeaders[i]->packetData(), locHeaders[i]->packetSize());
01192 delete locHeaders[i];
01193 if (locHR != S_OK) {
01194 return locHR;
01195 }
01196 }
01197 }
01198
01199
01200
01201
01202 if (mPinInputType.subtype == MEDIASUBTYPE_YUY2) {
01203
01204 encodeYUY2ToYV12(inBuf, inNumBytes);
01205 } else if (mPinInputType.subtype == MEDIASUBTYPE_AYUV) {
01206 encodeAYUVtoYV12(inBuf, inNumBytes);
01207
01208 } else if (mPinInputType.subtype == MEDIASUBTYPE_RGB32) {
01209 encodeRGB32toYV12(inBuf, inNumBytes);
01210
01211 } else if (mPinInputType.subtype == MEDIASUBTYPE_RGB24) {
01212 encodeRGB24toYV12(inBuf, inNumBytes);
01213
01214
01215 } else if (mPinInputType.subtype == MEDIASUBTYPE_YV12) {
01216
01217
01218 encodeYV12ToYV12(inBuf, inNumBytes);
01219 } else if (mPinInputType.subtype == MEDIASUBTYPE_UYVY) {
01220
01221
01222 encodeUYVYToYV12(inBuf, inNumBytes);
01223
01224 } else if (mPinInputType.subtype == MEDIASUBTYPE_YVYU) {
01225
01226
01227 encodeYVYUToYV12(inBuf, inNumBytes);
01228 } else if (mPinInputType.subtype == MEDIASUBTYPE_IYUV) {
01229
01230
01231 encodeIYUVToYV12(inBuf, inNumBytes);
01232
01233
01234 } else {
01235
01236
01237 throw 0;
01238 }
01239
01240
01241 StampedOggPacket* locPacket = mTheoraEncoder.encodeTheora(&mYUV);
01242 if (locPacket == NULL) {
01243
01244 return S_FALSE;
01245 }
01246 locFrameEnd = mUptoFrame
01247 = locPacket->endTime();
01248
01249
01250
01251 locHR = deliverData(locFrameStart, locFrameEnd, locPacket->packetData(), locPacket->packetSize());
01252 delete locPacket;
01253 return locHR;
01254
01255 }
01256 bool TheoraEncodeInputPin::ConstructCodec() {
01257
01258
01259 theora_info_init(&mTheoraInfo);
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291 mTheoraInfo.width = mYUV.y_width
01292 = mYUV.y_stride
01293 = (((mVideoFormat->bmiHeader.biWidth + 15)>>4)<<4);
01294
01295
01296 mTheoraInfo.frame_width = mWidth
01297 = mVideoFormat->bmiHeader.biWidth;
01298
01299
01300 mYUV.uv_width = mYUV.uv_stride
01301 = mYUV.y_width/2;
01302
01303
01304
01305
01306
01307 mTheoraInfo.height = mYUV.y_height
01308 = (((mVideoFormat->bmiHeader.biHeight + 15)>>4)<<4);
01309
01310
01311 mTheoraInfo.frame_height = mHeight
01312 = mVideoFormat->bmiHeader.biHeight;
01313
01314 mYUV.uv_height = mYUV.y_height/2;
01315
01316
01317
01318
01319
01320
01321 mTheoraInfo.offset_x = mXOffset
01322 = (mTheoraInfo.width - mTheoraInfo.frame_width);
01323
01324 mTheoraInfo.offset_y = mYOffset
01325 = (mHeight - mTheoraInfo.frame_height);
01326
01327
01328 unsigned long locYBuffSize = mYUV.y_height * mYUV.y_stride;
01329 mYUV.y = new char[locYBuffSize];
01330 mYUV.u = new char[locYBuffSize/4];
01331 mYUV.v = new char[locYBuffSize/4];
01332
01333
01334
01335
01336
01337
01338 unsigned long locNum = (((double)10000000 * 1000) / ((double)mVideoFormat->AvgTimePerFrame)) + (double)0.5;
01339
01340
01341 mTheoraInfo.fps_numerator = locNum;
01342 mTheoraInfo.fps_denominator = 1000;
01343
01344 mTheoraInfo.aspect_numerator=0;
01345 mTheoraInfo.aspect_denominator=0;
01346
01347 mTheoraInfo.colorspace=OC_CS_UNSPECIFIED;
01348 mTheoraInfo.target_bitrate=400000;
01349
01350
01351 mTheoraInfo.quality=30;
01352
01353 mTheoraInfo.dropframes_p=0;
01354 mTheoraInfo.quick_p=1;
01355 mTheoraInfo.keyframe_auto_p=1;
01356 mTheoraInfo.keyframe_frequency=64;
01357 mTheoraInfo.keyframe_frequency_force=64;
01358 mTheoraInfo.keyframe_data_target_bitrate=mTheoraInfo.target_bitrate*1.5;
01359 mTheoraInfo.keyframe_auto_threshold=80;
01360 mTheoraInfo.keyframe_mindistance=8;
01361 mTheoraInfo.noise_sensitivity=1;
01362
01363 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.frameRateNumerator = mTheoraInfo.fps_numerator;
01364 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.frameRateDenominator = mTheoraInfo.fps_denominator;
01365 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.maxKeyframeInterval = 6;
01366 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.pictureHeight = mHeight;
01367 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.pictureWidth = mWidth;
01368 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.colourSpace = OC_CS_UNSPECIFIED;
01369 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.outerFrameHeight = mTheoraInfo.height;
01370 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.outerFrameWidth = mTheoraInfo.width;
01371 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.xOffset = (unsigned char)mXOffset;
01372 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.yOffset = (unsigned char)mYOffset;
01373
01374
01375 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.aspectDenominator = 0;
01376 ((TheoraEncodeFilter*)mParentFilter)->mTheoraFormatBlock.aspectNumerator = 0;
01377
01378 return true;
01379
01380 }
01381 void TheoraEncodeInputPin::DestroyCodec() {
01382
01383
01384 }
01385
01386
01387
01388 HRESULT TheoraEncodeInputPin::SetMediaType(const CMediaType* inMediaType) {
01389
01390
01391 if ( inMediaType->subtype == MEDIASUBTYPE_YV12 ||
01392 inMediaType->subtype == MEDIASUBTYPE_IYUV ||
01393 inMediaType->subtype == MEDIASUBTYPE_YUY2 ||
01394 inMediaType->subtype == MEDIASUBTYPE_UYVY ||
01395 inMediaType->subtype == MEDIASUBTYPE_YVYU ||
01396 inMediaType->subtype == MEDIASUBTYPE_AYUV ||
01397 inMediaType->subtype == MEDIASUBTYPE_RGB32 ||
01398 inMediaType->subtype == MEDIASUBTYPE_RGB24
01399 ) {
01400 mVideoFormat = (VIDEOINFOHEADER*)inMediaType->pbFormat;
01401 mPinInputType = *inMediaType;
01402
01403 } else {
01404
01405 throw 0;
01406 }
01407 ConstructCodec();
01408 return CBaseInputPin::SetMediaType(inMediaType);
01409
01410
01411 }
01412
01413 theora_info* TheoraEncodeInputPin::theoraInfo() {
01414 return &mTheoraInfo;
01415 }