/
FFTrack.m
executable file
·149 lines (124 loc) · 3.49 KB
/
FFTrack.m
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
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim, Cheol Ju. All rights reserved.
// Yong-Hoe Kim <cocoable@gmail.com>
// Cheol Ju <moosoy@gmail.com>
//
// This file is part of Movist.
//
// Movist 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 3 of the License, or
// (at your option) any later version.
//
// Movist 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 this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "FFTrack.h"
#import "MMovie_FFmpeg.h"
@implementation FFContext
+ (id)contextWithAVStream:(AVStream*)stream index:(int)index
{
return [[[FFContext alloc] initWithAVStream:stream index:index] autorelease];
}
- (id)initWithAVStream:(AVStream*)stream index:(int)index
{
if (self = [super init]) {
_streamIndex = index;
_stream = stream;
}
return self;
}
- (int)streamIndex { return _streamIndex; }
- (AVStream*)stream { return _stream; }
- (BOOL)initContext:(int*)errorCode
{
AVCodecContext* context = _stream->codec;
AVCodec* codec = avcodec_find_decoder(context->codec_id);
if (!codec) {
*errorCode = ERROR_FFMPEG_DECODER_NOT_FOUND;
return FALSE;
}
context->debug_mv = 0;
context->debug = 0;
context->workaround_bugs = 1;
context->lowres = 0;
if (context->lowres) {
context->flags |= CODEC_FLAG_EMU_EDGE;
}
context->idct_algo = FF_IDCT_AUTO;
if (0/*fast*/) {
context->flags2 |= CODEC_FLAG2_FAST;
}
context->skip_frame = AVDISCARD_DEFAULT;
context->skip_idct = AVDISCARD_DEFAULT;
context->skip_loop_filter = AVDISCARD_DEFAULT;
#ifdef __BIG_ENDIAN__
context->error_resilience = FF_ER_CAREFUL;
#else
context->error_recognition = FF_ER_CAREFUL;
#endif
context->error_concealment = 3;
if (avcodec_open(context, codec) < 0) {
*errorCode = ERROR_FFMPEG_CODEC_OPEN_FAILED;
return FALSE;
}
if (context->codec_type == CODEC_TYPE_VIDEO) {
avcodec_thread_init(context, 2);
context->thread_count = 2;
}
return TRUE;
}
- (void)cleanupContext
{
if (_stream->codec) {
avcodec_close(_stream->codec);
}
}
@end
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
AVPacket s_flushPacket;
@implementation FFTrack
+ (void)initialize
{
av_init_packet(&s_flushPacket);
s_flushPacket.data = (uint8_t*)"FLUSH";
}
- (void)setMovie:(MMovie_FFmpeg*)movie { _movie = [movie retain]; }
- (void)dealloc
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_movie release];
[super dealloc];
}
- (BOOL)initTrack:(int*)errorCode
{
if (![self initContext:errorCode]) {
return FALSE;
}
return TRUE;
}
- (void)cleanupTrack
{
TRACE(@"%s", __PRETTY_FUNCTION__);
// -[quit] should be sent before sending -[cleanupTrack].
assert(!_running);
[self cleanupContext];
}
- (void)quit
{
while (_running) {
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
- (BOOL)isEnabled { return _enabled; }
- (void)setEnabled:(BOOL)enabled { _enabled = enabled; }
- (void)putPacket:(AVPacket*)packet {}
@end