This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
FFusionCodec.c
1377 lines (1144 loc) · 47.5 KB
/
FFusionCodec.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
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
//---------------------------------------------------------------------------
//FFusion
//Alternative DivX Codec for MacOS X
//version 2.2 (build 72)
//by Jerome Cornet
//Copyright 2002-2003 Jerome Cornet
//parts by Dan Christiansen
//Copyright 2003 Dan Christiansen
//from DivOSX by Jamby
//Copyright 2001-2002 Jamby
//uses libavcodec from ffmpeg 0.4.6
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library 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
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//---------------------------------------------------------------------------
// Source Code
//---------------------------------------------------------------------------
#include <Carbon/Carbon.h>
#include <QuickTime/QuickTime.h>
#include <Accelerate/Accelerate.h>
#include <sys/sysctl.h>
#include <pthread.h>
#include <libavcodec/avcodec.h>
#include "PerianResourceIDs.h"
#include "Codecprintf.h"
#include "ColorConversions.h"
#include "bitstream_info.h"
#include "FrameBuffer.h"
#include "CommonUtils.h"
#include "CodecIDs.h"
#include "FFmpegUtils.h"
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
// 64 because that's 2 * ffmpeg's INTERNAL_BUFFER_SIZE and QT sometimes uses more than 32
#define FFUSION_MAX_BUFFERS 64
#define kNumPixelFormatsSupportedFFusion 1
typedef struct
{
AVFrame *frame;
AVPicture picture;
int frameNumber;
uint8_t retainCount;
uint8_t ffmpegUsing;
} FFusionBuffer;
typedef CF_ENUM(CFIndex, FFusionPacked)
{
PACKED_QUICKTIME_KNOWS_ORDER, /* This is for non-stupid container formats which actually know the difference between decode and display order */
PACKED_ALL_IN_FIRST_FRAME, /* This is for the divx hack which contains a P frame and all subsequent B frames in a single frame. Ex: I, PB, -, PB, -, I...*/
PACKED_DELAY_BY_ONE_FRAME /* This is for stupid containers where frames are soley writen in decode order. Ex: I, P, B, P, B, P, I.... */
};
/* Why do these small structs? It makes the usage of these variables clearer, no other reason */
/* globs used by the BeginBand routine */
struct begin_glob
{
FFusionParserContext *parser;
int lastFrame;
int lastIFrame;
FrameData *lastPFrameData;
uint8_t lastFrameType;
uint8_t futureType;
};
/* globs used by the DecodeBand routine */
struct decode_glob
{
int lastFrame;
FFusionBuffer *futureBuffer;
};
struct per_frame_decode_stats
{
int begin_calls;
int decode_calls;
int draw_calls;
int end_calls;
};
struct decode_stats
{
struct per_frame_decode_stats type[4];
int max_frames_begun;
int max_frames_decoded;
};
typedef struct
{
ComponentInstance self;
ComponentInstance delegateComponent;
ComponentInstance target;
ImageCodecMPDrawBandUPP drawBandUPP;
Handle pixelTypes;
AVCodec *avCodec;
AVCodecContext *avContext;
OSType componentType;
FILE *fileLog;
AVPicture *lastDisplayedPicture;
FFusionPacked packedType;
FFusionBuffer buffers[FFUSION_MAX_BUFFERS]; // the buffers which the codec has retained
int lastAllocatedBuffer; // the index of the buffer which was last allocated
// by the codec (and is the latest in decode order)
struct begin_glob begin;
FFusionData data;
struct decode_glob decode;
struct decode_stats stats;
CCConverterContext colorConv;
} FFusionGlobalsRecord, *FFusionGlobals;
typedef struct
{
uint8_t decoded;
int frameNumber;
int GOPStartFrameNumber;
int bufferSize;
FFusionBuffer *buffer;
FrameData *frameData;
} FFusionDecompressRecord;
//---------------------------------------------------------------------------
// Prototypes of private subroutines
//---------------------------------------------------------------------------
static OSErr FFusionDecompress(FFusionGlobals glob, AVCodecContext *context, UInt8 *dataPtr, AVFrame *picture, int length);
static int FFusionGetBuffer(AVCodecContext *s, AVFrame *pic);
static void FFusionReleaseBuffer(AVCodecContext *s, AVFrame *pic);
static FFusionBuffer *retainBuffer(FFusionGlobals glob, FFusionBuffer *buf);
static void releaseBuffer(AVCodecContext *s, AVFrame *pic);
extern CFMutableStringRef CopyHomeDirectory();
#define FFusionDebugPrint(x...) if (glob->fileLog) Codecprintf(glob->fileLog, x);
#define not(x) ((x) ? "" : "not ")
//---------------------------------------------------------------------------
// Component Dispatcher
//---------------------------------------------------------------------------
#define IMAGECODEC_BASENAME() FFusionCodec
#define IMAGECODEC_GLOBALS() FFusionGlobals storage
#define CALLCOMPONENT_BASENAME() IMAGECODEC_BASENAME()
#define CALLCOMPONENT_GLOBALS() IMAGECODEC_GLOBALS()
#define COMPONENT_UPP_PREFIX() uppImageCodec
#define COMPONENT_DISPATCH_FILE "FFusionCodecDispatch.h"
#define COMPONENT_SELECT_PREFIX() kImageCodec
#define GET_DELEGATE_COMPONENT() (storage->delegateComponent)
#include <QuickTime/ImageCodec.k.h>
#pragma GCC visibility push(default)
#include <QuickTime/ComponentDispatchHelper.c>
#pragma GCC visibility pop
static void *launchUpdateChecker(void *args)
{
FSRef *ref = (FSRef *)args;
LSOpenFSRef(ref, NULL);
free(ref);
return NULL;
}
Boolean FFusionAlreadyRanUpdateCheck = 0;
void FFusionRunUpdateCheck()
{
if (FFusionAlreadyRanUpdateCheck) return;
CFDateRef lastRunDate = CopyPreferencesValueTyped(CFSTR("NextRunDate"), CFDateGetTypeID());
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
FFusionAlreadyRanUpdateCheck = 1;
if (lastRunDate) {
Boolean exit = CFDateGetAbsoluteTime(lastRunDate) > now;
CFRelease(lastRunDate);
if (exit) return;
}
//Two places to check, home dir and /
CFMutableStringRef location = CopyHomeDirectory();
CFStringAppend(location, CFSTR("/Library/PreferencePanes/Perian.prefPane/Contents/Resources/PerianUpdateChecker.app"));
char fileRep[1024];
FSRef *updateCheckRef = malloc(sizeof(FSRef));
Boolean doCheck = FALSE;
if(CFStringGetFileSystemRepresentation(location, fileRep, 1024))
if(FSPathMakeRef((UInt8 *)fileRep, updateCheckRef, NULL) == noErr)
doCheck = TRUE;
CFRelease(location);
if(doCheck == FALSE)
{
CFStringRef absLocation = CFSTR("/Library/PreferencePanes/Perian.prefPane/Contents/Resources/PerianUpdateChecker.app");
if(CFStringGetFileSystemRepresentation(absLocation, fileRep, 1024))
if(FSPathMakeRef((UInt8 *)fileRep, updateCheckRef, NULL) != noErr)
goto err; //We have failed
}
pthread_t thread;
pthread_create(&thread, NULL, launchUpdateChecker, updateCheckRef);
return;
err:
free(updateCheckRef);
}
static void RecomputeMaxCounts(FFusionGlobals glob)
{
int i;
int begun = 0, decoded = 0, ended = 0, drawn = 0;
for (i = 0; i < 4; i++) {
struct per_frame_decode_stats *f = &glob->stats.type[i];
begun += f->begin_calls;
decoded += f->decode_calls;
drawn += f->draw_calls;
ended += f->end_calls;
}
signed begin_diff = begun - ended, decode_diff = decoded - drawn;
if (abs(begin_diff) > glob->stats.max_frames_begun) glob->stats.max_frames_begun = begin_diff;
if (abs(decode_diff) > glob->stats.max_frames_decoded) glob->stats.max_frames_decoded = decode_diff;
}
static void DumpFrameDropStats(FFusionGlobals glob)
{
static const char types[4] = {'?', 'I', 'P', 'B'};
int i;
if (!glob->fileLog || glob->decode.lastFrame == 0) return;
Codecprintf(glob->fileLog, "%p frame drop stats\nType\t| BeginBand\t| DecodeBand\t| DrawBand\t| dropped before decode\t| dropped before draw\n", glob);
for (i = 0; i < 4; i++) {
struct per_frame_decode_stats *f = &glob->stats.type[i];
Codecprintf(glob->fileLog, "%c\t| %d\t\t| %d\t\t| %d\t\t| %d/%f%%\t\t| %d/%f%%\n", types[i], f->begin_calls, f->decode_calls, f->draw_calls,
f->begin_calls - f->decode_calls,(f->begin_calls > f->decode_calls) ? ((float)(f->begin_calls - f->decode_calls)/(float)f->begin_calls) * 100. : 0.,
f->decode_calls - f->draw_calls,(f->decode_calls > f->draw_calls) ? ((float)(f->decode_calls - f->draw_calls)/(float)f->decode_calls) * 100. : 0.);
}
}
static enum PixelFormat FindPixFmtFromVideo(AVCodec *codec, AVCodecContext *avctx, Ptr data, int bufferSize)
{
AVCodecContext *tmpContext;
AVFrame *tmpFrame;
int got_picture;
enum PixelFormat pix_fmt;
tmpContext = avcodec_alloc_context3(codec);
tmpFrame = avcodec_alloc_frame();
avcodec_copy_context(tmpContext, avctx);
if (avcodec_open2(tmpContext, codec, NULL)) {
pix_fmt = PIX_FMT_NONE;
goto bail;
}
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = (UInt8*)data;
pkt.size = bufferSize;
avcodec_decode_video2(tmpContext, tmpFrame, &got_picture, &pkt);
pix_fmt = tmpContext->pix_fmt;
avcodec_close(tmpContext);
bail:
av_freep(&tmpContext);
av_freep(&tmpFrame);
return pix_fmt;
}
static void SetSkipLoopFilter(FFusionGlobals glob, AVCodecContext *avctx)
{
Boolean keyExists = FALSE;
int loopFilterSkip = CFPreferencesGetAppIntegerValue(CFSTR("SkipLoopFilter"), PERIAN_PREF_DOMAIN, &keyExists);
if(keyExists)
{
enum AVDiscard loopFilterValue = AVDISCARD_DEFAULT;
switch (loopFilterSkip) {
case 1:
loopFilterValue = AVDISCARD_NONREF;
break;
case 2:
loopFilterValue = AVDISCARD_BIDIR;
break;
case 3:
loopFilterValue = AVDISCARD_NONKEY;
break;
case 4:
loopFilterValue = AVDISCARD_ALL;
break;
default:
break;
}
avctx->skip_loop_filter = loopFilterValue;
FFusionDebugPrint("%p Preflight set skip loop filter to %d", glob, loopFilterValue);
}
}
// A list of codec types (mostly official Apple codecs) which always have DTS info.
// This is the wrong way to do it, instead we should check for a ctts atom directly.
// This way causes files to play frames out of order if we guess wrong. Doesn't seem
// possible to do it right, though.
FFusionPacked DefaultPackedTypeForCodec(OSType codec)
{
switch (codec) {
case kMPEG1VisualCodecType:
case kMPEG2VisualCodecType:
case 'hdv1':
case kMPEG4VisualCodecType:
case kH264CodecType:
return PACKED_QUICKTIME_KNOWS_ORDER;
default:
return PACKED_ALL_IN_FIRST_FRAME;
}
}
void setFutureFrame(FFusionGlobals glob, FFusionBuffer *newFuture)
{
FFusionBuffer *temp = glob->decode.futureBuffer;
if(newFuture != NULL)
retainBuffer(glob, newFuture);
glob->decode.futureBuffer = newFuture;
if(temp != NULL)
releaseBuffer(glob->avContext, temp->frame);
}
//---------------------------------------------------------------------------
// Component Routines
//---------------------------------------------------------------------------
// -- This Image Decompressor Use the Base Image Decompressor Component --
// The base image decompressor is an Apple-supplied component
// that makes it easier for developers to create new decompressors.
// The base image decompressor does most of the housekeeping and
// interface functions required for a QuickTime decompressor component,
// including scheduling for asynchronous decompression.
//-----------------------------------------------------------------
// Component Open Request - Required
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecOpen(FFusionGlobals glob, ComponentInstance self)
{
ComponentResult err;
ComponentDescription descout;
GetComponentInfo((Component)self, &descout, 0, 0, 0);
// FourCCprintf("Opening component for type ", descout.componentSubType);
// Allocate memory for our globals, set them up and inform the component manager that we've done so
glob = (FFusionGlobals)NewPtrClear(sizeof(FFusionGlobalsRecord));
if ((err = MemError()))
{
Codecprintf(NULL, "Unable to allocate globals! Exiting.\n");
}
else
{
CFStringRef pathToLogFile = CopyPreferencesValueTyped(CFSTR("DebugLogFile"), CFStringGetTypeID());
char path[PATH_MAX];
SetComponentInstanceStorage(self, (Handle)glob);
glob->self = self;
glob->target = self;
glob->drawBandUPP = NULL;
glob->pixelTypes = NewHandleClear((kNumPixelFormatsSupportedFFusion+1) * sizeof(OSType));
glob->avCodec = 0;
glob->componentType = descout.componentSubType;
glob->data.frames = NULL;
glob->begin.parser = NULL;
if (pathToLogFile) {
CFStringGetCString(pathToLogFile, path, PATH_MAX, kCFStringEncodingUTF8);
CFRelease(pathToLogFile);
glob->fileLog = fopen(path, "a");
}
// Open and target an instance of the base decompressor as we delegate
// most of our calls to the base decompressor instance
err = OpenADefaultComponent(decompressorComponentType, kBaseCodecType, &glob->delegateComponent);
if (!err)
{
ComponentSetTarget(glob->delegateComponent, self);
}
else
{
Codecprintf(glob->fileLog, "Error opening the base image decompressor! Exiting.\n");
}
// we allocate some space for copying the frame data since we need some padding at the end
// for ffmpeg's optimized bitstream readers. Size doesn't really matter, it'll grow if need be
FFusionDataSetup(&(glob->data), 256, 1024*1024);
FFusionRunUpdateCheck();
}
FFusionDebugPrint("%p opened for '%s'\n", glob, FourCCString(glob->componentType));
return err;
}
//-----------------------------------------------------------------
// Component Close Request - Required
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecClose(FFusionGlobals glob, ComponentInstance self)
{
FFusionDebugPrint("%p closed.\n", glob);
DumpFrameDropStats(glob);
// Make sure to close the base component and deallocate our storage
if (glob)
{
if (glob->delegateComponent)
{
CloseComponent(glob->delegateComponent);
}
if (glob->drawBandUPP)
{
DisposeImageCodecMPDrawBandUPP(glob->drawBandUPP);
}
setFutureFrame(glob, NULL);
if (glob->avContext)
{
if (glob->avContext->extradata)
free(glob->avContext->extradata);
if (glob->avContext->codec) avcodec_close(glob->avContext);
av_free(glob->avContext);
}
if (glob->begin.parser)
{
ffusionParserFree(glob->begin.parser);
}
if (glob->pixelTypes)
{
DisposeHandle(glob->pixelTypes);
}
CCCloseConverter(&glob->colorConv);
FFusionDataFree(&(glob->data));
if(glob->fileLog)
fclose(glob->fileLog);
memset(glob, 0, sizeof(FFusionGlobalsRecord));
DisposePtr((Ptr)glob);
}
return noErr;
}
//-----------------------------------------------------------------
// Component Version Request - Required
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecVersion(FFusionGlobals glob)
{
return kFFusionCodecVersion;
}
//-----------------------------------------------------------------
// Component Target Request
//-----------------------------------------------------------------
// Allows another component to "target" you i.e., you call
// another component whenever you would call yourself (as a result
// of your component being used by another component)
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecTarget(FFusionGlobals glob, ComponentInstance target)
{
glob->target = target;
return noErr;
}
//-----------------------------------------------------------------
// Component GetMPWorkFunction Request
//-----------------------------------------------------------------
// Allows your image decompressor component to perform asynchronous
// decompression in a single MP task by taking advantage of the
// Base Decompressor. If you implement this selector, your DrawBand
// function must be MP-safe. MP safety means not calling routines
// that may move or purge memory and not calling any routines which
// might cause 68K code to be executed. Ideally, your DrawBand
// function should not make any API calls whatsoever. Obviously
// don't implement this if you're building a 68k component.
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecGetMPWorkFunction(FFusionGlobals glob, ComponentMPWorkFunctionUPP *workFunction, void **refCon)
{
if (glob->drawBandUPP == NULL)
glob->drawBandUPP = NewImageCodecMPDrawBandUPP((ImageCodecMPDrawBandProcPtr)FFusionCodecDrawBand);
return ImageCodecGetBaseMPWorkFunction(glob->delegateComponent, workFunction, refCon, glob->drawBandUPP, glob);
}
//-----------------------------------------------------------------
// ImageCodecInitialize
//-----------------------------------------------------------------
// The first function call that your image decompressor component
// receives from the base image decompressor is always a call to
// ImageCodecInitialize . In response to this call, your image
// decompressor component returns an ImageSubCodecDecompressCapabilities
// structure that specifies its capabilities.
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecInitialize(FFusionGlobals glob, ImageSubCodecDecompressCapabilities *cap)
{
// Secifies the size of the ImageSubCodecDecompressRecord structure
// and say we can support asyncronous decompression
// With the help of the base image decompressor, any image decompressor
// that uses only interrupt-safe calls for decompression operations can
// support asynchronous decompression.
cap->decompressRecordSize = sizeof(FFusionDecompressRecord) + 12;
cap->canAsync = true;
// QT 7
if(cap->recordSize > offsetof(ImageSubCodecDecompressCapabilities, baseCodecShouldCallDecodeBandForAllFrames))
{
cap->subCodecIsMultiBufferAware = true;
cap->subCodecSupportsOutOfOrderDisplayTimes = true;
cap->baseCodecShouldCallDecodeBandForAllFrames = false;
cap->subCodecSupportsScheduledBackwardsPlaybackWithDifferenceFrames = true;
cap->subCodecSupportsDrawInDecodeOrder = true;
cap->subCodecSupportsDecodeSmoothing = true;
}
return noErr;
}
static inline int shouldDecode(FFusionGlobals glob, enum CodecID codecID)
{
FFusionDecodeAbilities decode = FFUSION_PREFER_DECODE;
if (glob->componentType == 'avc1')
decode = ffusionIsParsedVideoDecodable(glob->begin.parser);
if(decode > FFUSION_CANNOT_DECODE &&
(codecID == CODEC_ID_H264 || codecID == CODEC_ID_MPEG4) && CFPreferencesGetAppBooleanValue(CFSTR("PreferAppleCodecs"), PERIAN_PREF_DOMAIN, NULL))
decode = FFUSION_PREFER_NOT_DECODE;
if(decode > FFUSION_CANNOT_DECODE)
if(IsForcedDecodeEnabled())
decode = FFUSION_PREFER_DECODE;
return decode > FFUSION_PREFER_NOT_DECODE;
}
//-----------------------------------------------------------------
// ImageCodecPreflight
//-----------------------------------------------------------------
// The base image decompressor gets additional information about the
// capabilities of your image decompressor component by calling
// ImageCodecPreflight. The base image decompressor uses this
// information when responding to a call to the ImageCodecPredecompress
// function, which the ICM makes before decompressing an image. You
// are required only to provide values for the wantedDestinationPixelSize
// and wantedDestinationPixelTypes fields and can also modify other
// fields if necessary.
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecPreflight(FFusionGlobals glob, CodecDecompressParams *p)
{
OSType *pos;
int index;
CodecCapabilities *capabilities = p->capabilities;
int count = 0;
Handle imgDescExt;
OSErr err = noErr;
// We first open libavcodec library and the codec corresponding
// to the fourCC if it has not been done before
FFusionDebugPrint("%p Preflight called.\n", glob);
FFusionDebugPrint("%p Frame dropping is %senabled\n", glob, not(IsFrameDroppingEnabled()));
if (!glob->avCodec)
{
FFInitFFmpeg();
initFFusionParsers();
OSType componentType = glob->componentType;
enum CodecID codecID = getCodecID(componentType);
glob->packedType = DefaultPackedTypeForCodec(componentType);
if(codecID == CODEC_ID_NONE)
{
Codecprintf(glob->fileLog, "Warning! Unknown codec type! Using MPEG4 by default.\n");
codecID = CODEC_ID_MPEG4;
}
glob->avCodec = avcodec_find_decoder(codecID);
// if(glob->packedType != PACKED_QUICKTIME_KNOWS_ORDER)
glob->begin.parser = ffusionParserInit(codecID);
if ((codecID == CODEC_ID_MPEG4 || codecID == CODEC_ID_H264) && !glob->begin.parser)
Codecprintf(glob->fileLog, "This is a parseable format, but we couldn't open a parser!\n");
// we do the same for the AVCodecContext since all context values are
// correctly initialized when calling the alloc function
glob->avContext = avcodec_alloc_context3(glob->avCodec);
// Use low delay
glob->avContext->flags |= CODEC_FLAG_LOW_DELAY;
// Image size is mandatory for DivX-like codecs
glob->avContext->width = (**p->imageDescription).width;
glob->avContext->height = (**p->imageDescription).height;
glob->avContext->bits_per_coded_sample = (**p->imageDescription).depth;
// We also pass the FourCC since it allows the H263 hybrid decoder
// to make the difference between the various flavours of DivX
glob->avContext->codec_tag = Endian32_Swap(glob->componentType);
glob->avContext->codec_id = codecID;
// avc1 requires the avcC extension
if (glob->componentType == 'avc1') {
count = isImageDescriptionExtensionPresent(p->imageDescription, 'avcC');
if (count >= 1) {
imgDescExt = NewHandle(0);
GetImageDescriptionExtension(p->imageDescription, &imgDescExt, 'avcC', 1);
glob->avContext->extradata = calloc(1, GetHandleSize(imgDescExt) + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(glob->avContext->extradata, *imgDescExt, GetHandleSize(imgDescExt));
glob->avContext->extradata_size = GetHandleSize(imgDescExt);
DisposeHandle(imgDescExt);
} else {
count = isImageDescriptionExtensionPresent(p->imageDescription, 'strf');
// avc1 in AVI, need to reorder frames
if (count >= 1)
glob->packedType = PACKED_ALL_IN_FIRST_FRAME;
}
} else if (glob->componentType == 'mp4v') {
count = isImageDescriptionExtensionPresent(p->imageDescription, 'esds');
if (count >= 1) {
imgDescExt = NewHandle(0);
GetImageDescriptionExtension(p->imageDescription, &imgDescExt, 'esds', 1);
ReadESDSDescExt(imgDescExt, &glob->avContext->extradata, &glob->avContext->extradata_size);
DisposeHandle(imgDescExt);
}
} else if (glob->componentType == kVideoFormatTheora) {
count = isImageDescriptionExtensionPresent(p->imageDescription, kVideoFormatTheora);
if (count >= 1) {
imgDescExt = NewHandle(0);
GetImageDescriptionExtension(p->imageDescription, &imgDescExt, kVideoFormatTheora, 1);
glob->avContext->extradata = calloc(1, GetHandleSize(imgDescExt) + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(glob->avContext->extradata, *imgDescExt, GetHandleSize(imgDescExt));
glob->avContext->extradata_size = GetHandleSize(imgDescExt);
DisposeHandle(imgDescExt);
}
} else {
count = isImageDescriptionExtensionPresent(p->imageDescription, 'strf');
if (count >= 1) {
imgDescExt = NewHandle(0);
GetImageDescriptionExtension(p->imageDescription, &imgDescExt, 'strf', 1);
if (GetHandleSize(imgDescExt) - 40 > 0) {
glob->avContext->extradata = calloc(1, GetHandleSize(imgDescExt) - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(glob->avContext->extradata, *imgDescExt + 40, GetHandleSize(imgDescExt) - 40);
glob->avContext->extradata_size = GetHandleSize(imgDescExt) - 40;
}
DisposeHandle(imgDescExt);
}
}
FFusionDebugPrint("%p preflighted for %dx%d '%s'. (%d bytes of extradata)\n", glob, glob->avContext->width, glob->avContext->height, FourCCString(glob->componentType), glob->avContext->extradata_size);
if(glob->avContext->extradata_size != 0 && glob->begin.parser != NULL) {
if (!ffusionParseExtraData(glob->begin.parser, glob->avContext->extradata, glob->avContext->extradata_size))
err = featureUnsupported;
}
if (glob->fileLog)
ffusionLogDebugInfo(glob->begin.parser, glob->fileLog);
if (!shouldDecode(glob, codecID))
err = featureUnsupported;
// some hooks into ffmpeg's buffer allocation to get frames in
// decode order without delay more easily
glob->avContext->opaque = glob;
glob->avContext->get_buffer = FFusionGetBuffer;
glob->avContext->release_buffer = FFusionReleaseBuffer;
// cap threads at a smaller number to be polite
glob->avContext->thread_count = 2;
// deblock skipping for h264
SetSkipLoopFilter(glob, glob->avContext);
//fast flag
if (CFPreferencesGetAppBooleanValue(CFSTR("UseFastDecode"), PERIAN_PREF_DOMAIN, NULL))
glob->avContext->flags2 |= CODEC_FLAG2_FAST;
// Finally we open the avcodec
if (avcodec_open2(glob->avContext, glob->avCodec, NULL))
{
Codecprintf(glob->fileLog, "Error opening avcodec!\n");
err = paramErr;
}
// codec was opened, but didn't give us its pixfmt
// we have to decode the first frame to find out one
else if (glob->avContext->pix_fmt == PIX_FMT_NONE && p->bufferSize && p->data) {
glob->avContext->pix_fmt = FindPixFmtFromVideo(glob->avCodec, glob->avContext, p->data, p->bufferSize);
// first frame is invalid so we still have no pixfmt
if (glob->avContext->pix_fmt == PIX_FMT_NONE)
err = paramErr;
}
}
// Specify the minimum image band height supported by the component
// bandInc specifies a common factor of supported image band heights -
// if your component supports only image bands that are an even
// multiple of some number of pixels high report this common factor in bandInc
capabilities->bandMin = (**p->imageDescription).height;
capabilities->bandInc = capabilities->bandMin;
// libavcodec 0.4.x is no longer stream based i.e. you cannot pass just
// an arbitrary amount of data to the library.
// Instead we have to tell QT to just pass the data corresponding
// to one frame
capabilities->flags |= codecWantsSpecialScaling;
p->requestedBufferWidth = (**p->imageDescription).width;
p->requestedBufferHeight = (**p->imageDescription).height;
// Indicate the pixel depth the component can use with the specified image
// normally should be capabilities->wantedPixelSize = (**p->imageDescription).depth;
// but we don't care since some DivX are so bugged that the depth information
// is not correct
capabilities->wantedPixelSize = 0;
pos = *((OSType **)glob->pixelTypes);
index = 0;
if (!err) {
OSType qtPixFmt = FFPixFmtToFourCC(CCOutputPixFmtForInput(glob->avContext->pix_fmt));
/*
an error here means either
1) a color converter for this format isn't implemented
2) we know QT doesn't like this format and will give us argb 32bit instead
in the case of 2 we have to special-case bail right here, since errors
in BeginBand are ignored
*/
if (qtPixFmt)
pos[index++] = qtPixFmt;
else
err = featureUnsupported;
}
p->wantedDestinationPixelTypes = (OSType **)glob->pixelTypes;
// Specify the number of pixels the image must be extended in width and height if
// the component cannot accommodate the image at its given width and height
// It is not the case here
capabilities->extendWidth = 0;
capabilities->extendHeight = 0;
capabilities->flags |= codecCanAsync | codecCanAsyncWhen;
FFusionDebugPrint("%p Preflight requesting colorspace '%s'. (error %d)\n", glob, FourCCString(pos[0]), err);
return err;
}
static int qtTypeForFrameInfo(int original, int fftype, int skippable)
{
if(fftype == AV_PICTURE_TYPE_I)
{
if(!skippable)
return kCodecFrameTypeKey;
}
else if(skippable && IsFrameDroppingEnabled())
return kCodecFrameTypeDroppableDifference;
else if(fftype != 0)
return kCodecFrameTypeDifference;
return original;
}
//-----------------------------------------------------------------
// ImageCodecBeginBand
//-----------------------------------------------------------------
// The ImageCodecBeginBand function allows your image decompressor
// component to save information about a band before decompressing
// it. This function is never called at interrupt time. The base
// image decompressor preserves any changes your component makes to
// any of the fields in the ImageSubCodecDecompressRecord or
// CodecDecompressParams structures. If your component supports
// asynchronous scheduled decompression, it may receive more than
// one ImageCodecBeginBand call before receiving an ImageCodecDrawBand
// call.
//-----------------------------------------------------------------
pascal ComponentResult FFusionCodecBeginBand(FFusionGlobals glob, CodecDecompressParams *p, ImageSubCodecDecompressRecord *drp, long flags)
{
FFusionDecompressRecord *myDrp = (FFusionDecompressRecord *)drp->userDecompressRecord;
int redisplayFirstFrame = 0;
int type = 0;
int skippable = 0;
myDrp->decoded = p->frameTime ? (0 != (p->frameTime->flags & icmFrameAlreadyDecoded)) : false;
myDrp->frameData = NULL;
myDrp->buffer = NULL;
FFusionDebugPrint("%p BeginBand #%ld. (%sdecoded, packed %d)\n", glob, p->frameNumber, not(myDrp->decoded), glob->packedType);
if (!glob->avContext) {
Codecprintf(glob->fileLog, "Perian: QT tried to call BeginBand without preflighting!\n");
return internalComponentErr;
}
if (p->frameNumber <= 1 && p->dstPixMap.pixelFormat != **(OSType**)glob->pixelTypes) {
Codecprintf(NULL, "QT gave us unwanted pixelFormat %s (%08x), this will not work\n", FourCCString(p->dstPixMap.pixelFormat), (unsigned)p->dstPixMap.pixelFormat);
}
if(myDrp->decoded)
{
int i;
myDrp->frameNumber = p->frameNumber;
for (i = 0; i < FFUSION_MAX_BUFFERS; i++) {
if (glob->buffers[i].retainCount && glob->buffers[i].frameNumber == myDrp->frameNumber) {
myDrp->buffer = retainBuffer(glob, &glob->buffers[i]);
break;
}
}
return noErr;
}
if(glob->packedType != PACKED_QUICKTIME_KNOWS_ORDER && p->frameNumber != glob->begin.lastFrame + 1)
{
if(glob->decode.lastFrame < p->frameNumber && p->frameNumber < glob->begin.lastFrame + 1)
{
/* We already began this sucker, but haven't decoded it yet, find the data */
FrameData *frameData = NULL;
frameData = FFusionDataFind(&glob->data, p->frameNumber);
if(frameData != NULL)
{
myDrp->frameData = frameData;
drp->frameType = qtTypeForFrameInfo(drp->frameType, myDrp->frameData->type, myDrp->frameData->skippabble);
myDrp->frameNumber = p->frameNumber;
myDrp->GOPStartFrameNumber = glob->begin.lastIFrame;
return noErr;
}
}
else
{
/* Reset context, safe marking in such a case */
glob->begin.lastFrameType = AV_PICTURE_TYPE_I;
FFusionDataReadUnparsed(&(glob->data));
glob->begin.lastPFrameData = NULL;
redisplayFirstFrame = 1;
}
}
if(glob->begin.parser != NULL)
{
int parsedBufSize = 0;
uint8_t *buffer = (uint8_t *)drp->codecData;
int bufferSize = p->bufferSize;
int skipped = 0;
if(glob->data.unparsedFrames.dataSize != 0)
{
buffer = glob->data.unparsedFrames.buffer;
bufferSize = glob->data.unparsedFrames.dataSize;
}
if(ffusionParse(glob->begin.parser, buffer, bufferSize, &parsedBufSize, &type, &skippable, &skipped) == 0 && (!skipped || glob->begin.futureType))
{
/* parse failed */
myDrp->bufferSize = bufferSize;
if(glob->begin.futureType != 0)
{
/* Assume our previously decoded P frame */
type = glob->begin.futureType;
glob->begin.futureType = 0;
myDrp->frameData = glob->begin.lastPFrameData;
}
else
{
Codecprintf(glob->fileLog, "parse failed frame %ld with size %d\n", p->frameNumber, bufferSize);
if(glob->data.unparsedFrames.dataSize != 0)
Codecprintf(glob->fileLog, ", parser had extra data\n");
}
}
else if(glob->packedType != PACKED_QUICKTIME_KNOWS_ORDER)
{
if(type == AV_PICTURE_TYPE_B && glob->packedType == PACKED_ALL_IN_FIRST_FRAME && glob->begin.futureType == 0)
/* Badly framed. We hit a B frame after it was supposed to be displayed, switch to delaying by a frame */
glob->packedType = PACKED_DELAY_BY_ONE_FRAME;
else if(glob->packedType == PACKED_DELAY_BY_ONE_FRAME && parsedBufSize < bufferSize - 16)
/* Seems to be switching back to packed in one frame; switch back*/
glob->packedType = PACKED_ALL_IN_FIRST_FRAME;
myDrp->frameData = FFusionDataAppend(&(glob->data), buffer, parsedBufSize, type);
if(type != AV_PICTURE_TYPE_I)
myDrp->frameData->prereqFrame = glob->begin.lastPFrameData;
if(glob->packedType == PACKED_DELAY_BY_ONE_FRAME)
{
if(type != AV_PICTURE_TYPE_B)
{
FrameData *nextPFrame = myDrp->frameData;
FrameData *lastPFrame = glob->begin.lastPFrameData;
if(lastPFrame != NULL)
/* Mark the next P or I frame, predictive decoding */
lastPFrame->nextFrame = nextPFrame;
myDrp->frameData = lastPFrame;
glob->begin.lastPFrameData = nextPFrame;
if(redisplayFirstFrame)
myDrp->frameData = nextPFrame;
}
int displayType = glob->begin.lastFrameType;
glob->begin.lastFrameType = type;
type = displayType;
}
else if(type != AV_PICTURE_TYPE_B)
glob->begin.lastPFrameData = myDrp->frameData;
if(type == AV_PICTURE_TYPE_I && glob->packedType == PACKED_ALL_IN_FIRST_FRAME)
/* Wipe memory of past P frames */
glob->begin.futureType = 0;
if(parsedBufSize < p->bufferSize)
{
int oldType = type;
buffer += parsedBufSize;
bufferSize -= parsedBufSize;
int success = ffusionParse(glob->begin.parser, buffer, bufferSize, &parsedBufSize, &type, &skippable, &skipped);
if(success && type == AV_PICTURE_TYPE_B)
{
/* A B frame follows us, so setup the P frame for the future and set dependencies */
glob->begin.futureType = oldType;
if(glob->begin.lastPFrameData != NULL)
/* Mark the next P or I frame, predictive decoding */
glob->begin.lastPFrameData->nextFrame = myDrp->frameData;
glob->begin.lastPFrameData = myDrp->frameData;
myDrp->frameData = FFusionDataAppend(&(glob->data), buffer, parsedBufSize, type);
myDrp->frameData->prereqFrame = glob->begin.lastPFrameData;
buffer += parsedBufSize;
bufferSize -= parsedBufSize;
}