summaryrefslogtreecommitdiffstats
path: root/iphone/ZBarCaptureReader.m
blob: b3dd65c0b5aefd0cff0d54c1f57e68e0d6e339b7 (plain)
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
//------------------------------------------------------------------------
//  Copyright 2010 (c) Jeff Brown <spadix@users.sourceforge.net>
//
//  This file is part of the ZBar Bar Code Reader.
//
//  The ZBar Bar Code Reader is free software; you can redistribute it
//  and/or modify it under the terms of the GNU Lesser Public License as
//  published by the Free Software Foundation; either version 2.1 of
//  the License, or (at your option) any later version.
//
//  The ZBar Bar Code Reader 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 Public License for more details.
//
//  You should have received a copy of the GNU Lesser Public License
//  along with the ZBar Bar Code Reader; if not, write to the Free
//  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
//  Boston, MA  02110-1301  USA
//
//  http://sourceforge.net/projects/zbar
//------------------------------------------------------------------------

#import <libkern/OSAtomic.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>
#import <CoreVideo/CoreVideo.h>
#import <ZBarSDK/ZBarCaptureReader.h>
#import <ZBarSDK/ZBarImageScanner.h>
#import "ZBarCVImage.h"

#define MODULE ZBarCaptureReader
#import "debug.h"

enum {
    STOPPED = 0,
    RUNNING = 1,
    PAUSED = 2,
    CAPTURE = 4,
};

@implementation ZBarCaptureReader

@synthesize captureOutput, captureDelegate, scanner, scanCrop, size,
    framesPerSecond, enableCache;
@dynamic enableReader;

- (void) initResult
{
    [result release];
    result = [ZBarCVImage new];
    result.format = [ZBarImage fourcc: @"CV2P"];
}

- (id) initWithImageScanner: (ZBarImageScanner*) _scanner
{
    self = [super init];
    if(!self)
        return(nil);

    t_fps = t_frame = timer_now();
    enableCache = YES;

    scanner = [_scanner retain];
    scanCrop = CGRectMake(0, 0, 1, 1);
    image = [ZBarImage new];
    image.format = [ZBarImage fourcc: @"Y800"];
    [self initResult];

    captureOutput = [AVCaptureVideoDataOutput new];
    captureOutput.alwaysDiscardsLateVideoFrames = YES;

#ifdef FIXED_8697526
    /* iOS 4.2 introduced a bug that causes [session startRunning] to
     * hang if the session has a preview layer and this property is
     * specified at the output.  As this happens to be the default
     * setting for the currently supported devices, it can be omitted
     * without causing a functional problem (for now...).  Of course,
     * we still have no idea what the real problem is, or how robust
     * this is as a workaround...
     */
    captureOutput.videoSettings = 
        [NSDictionary
            dictionaryWithObject:
                [NSNumber numberWithInt:
                    kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]
            forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
#endif

    queue = dispatch_queue_create("ZBarCaptureReader", NULL);
    [captureOutput setSampleBufferDelegate:
                       (id<AVCaptureVideoDataOutputSampleBufferDelegate>)self
                   queue: queue];

    return(self);
}

- (id) init
{
    self = [self initWithImageScanner:
               [[ZBarImageScanner new]
                   autorelease]];
    if(!self)
        return(nil);

    [scanner setSymbology: 0
             config: ZBAR_CFG_X_DENSITY
             to: 3];
    [scanner setSymbology: 0
             config: ZBAR_CFG_Y_DENSITY
             to: 3];
    return(self);
}

- (void) dealloc
{
    captureDelegate = nil;

    // queue continues to run after stopping (NB even after DidStopRunning!);
    // ensure released delegate is not called. (also NB that the queue
    // may not be null, even in this case...)
    [captureOutput setSampleBufferDelegate: nil
                   queue: queue];
    [captureOutput release];
    captureOutput = nil;
    dispatch_release(queue);

    [image release];
    image = nil;
    [result release];
    result = nil;
    [scanner release];
    scanner = nil;
    [super dealloc];
}

- (BOOL) enableReader
{
    return(OSAtomicOr32Barrier(0, &state) & RUNNING);
}

- (void) setEnableReader: (BOOL) enable
{
    if(!enable)
        OSAtomicAnd32Barrier(STOPPED, &state);
    else if(!(OSAtomicOr32OrigBarrier(RUNNING, &state) & RUNNING)) {
        OSAtomicAnd32Barrier(~PAUSED, &state);
        @synchronized(scanner) {
            scanner.enableCache = enableCache;
        }
    }
}

- (void) willStartRunning
{
    self.enableReader = YES;
}

- (void) willStopRunning
{
    self.enableReader = NO;
}

- (void) flushCache
{
    @synchronized(scanner) {
        scanner.enableCache = enableCache;
    }
}

- (void) captureFrame
{
    OSAtomicOr32(CAPTURE, &state);
}

- (void) setCaptureDelegate: (id<ZBarCaptureDelegate>) delegate
{
    @synchronized(scanner) {
        captureDelegate = delegate;
    }
}

- (void) cropUpdate
{
    @synchronized(scanner) {
        image.crop = CGRectMake(scanCrop.origin.x * width,
                                scanCrop.origin.y * height,
                                scanCrop.size.width * width,
                                scanCrop.size.height * height);
    }
}

- (void) setScanCrop: (CGRect) crop
{
    if(CGRectEqualToRect(scanCrop, crop))
        return;
    scanCrop = crop;
    [self cropUpdate];
}

- (void) didTrackSymbols: (ZBarSymbolSet*) syms
{
    [captureDelegate
        captureReader: self
        didTrackSymbols: syms];
}

- (void) didReadNewSymbolsFromImage: (ZBarImage*) img
{
    timer_start;
    [captureDelegate
        captureReader: self
        didReadNewSymbolsFromImage: img];
    OSAtomicAnd32Barrier(~PAUSED, &state);
    zlog(@"latency: delegate=%gs total=%gs",
         timer_elapsed(t_start, timer_now()),
         timer_elapsed(t_scan, timer_now()));
}

- (void) setFramesPerSecond: (CGFloat) fps
{
    framesPerSecond = fps;
}

- (void) updateFPS: (NSNumber*) val
{
    [self setFramesPerSecond: val.doubleValue];
}

- (void) setSize: (CGSize) _size
{
    size = _size;
}

- (void) updateSize: (CFDictionaryRef) val
{
    CGSize _size;
    if(CGSizeMakeWithDictionaryRepresentation(val, &_size))
        [self setSize: _size];
}

- (void)  captureOutput: (AVCaptureOutput*) output
  didOutputSampleBuffer: (CMSampleBufferRef) samp
         fromConnection: (AVCaptureConnection*) conn
{
    // queue is apparently not flushed when stopping;
    // only process when running
    uint32_t _state = OSAtomicOr32Barrier(0, &state);
    if((_state & (PAUSED | RUNNING)) != RUNNING)
        return;

    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    image.sequence = framecnt++;

    uint64_t now = timer_now();
    double dt = timer_elapsed(t_frame, now);
    t_frame = now;
    if(dt > 2) {
        t_fps = now;
        dt_frame = 0;
    }
    else if(!dt_frame)
        dt_frame = dt;
    dt_frame = (dt_frame + dt) / 2;
    if(timer_elapsed(t_fps, now) >= 1) {
        [self performSelectorOnMainThread: @selector(updateFPS:)
              withObject: [NSNumber numberWithDouble: 1 / dt_frame]
              waitUntilDone: NO];
        t_fps = now;
    }

    CVImageBufferRef buf = CMSampleBufferGetImageBuffer(samp);
    if(CMSampleBufferGetNumSamples(samp) != 1 ||
       !CMSampleBufferIsValid(samp) ||
       !CMSampleBufferDataIsReady(samp) ||
       !buf) {
        zlog(@"ERROR: invalid sample");
        goto error;
    }

    OSType format = CVPixelBufferGetPixelFormatType(buf);
    int planes = CVPixelBufferGetPlaneCount(buf);

    if(format != kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange ||
       !planes) {
        zlog(@"ERROR: invalid buffer format");
        goto error;
    }

    int w = CVPixelBufferGetBytesPerRowOfPlane(buf, 0);
    int h = CVPixelBufferGetHeightOfPlane(buf, 0);
    CVReturn rc =
        CVPixelBufferLockBaseAddress(buf, kCVPixelBufferLock_ReadOnly);
    if(!w || !h || rc) {
        zlog(@"ERROR: invalid buffer data");
        goto error;
    }

    void *data = CVPixelBufferGetBaseAddressOfPlane(buf, 0);
    if(data) {
        [image setData: data
               withLength: w * h];

        BOOL doTrack = NO;
        int ngood = 0;
        ZBarSymbolSet *syms = nil;
        @synchronized(scanner) {
            if(width != w || height != h) {
                width = w;
                height = h;
                CGSize _size = CGSizeMake(w, h);
                CFDictionaryRef sized =
                    CGSizeCreateDictionaryRepresentation(_size);
                if(sized) {
                    [self performSelectorOnMainThread: @selector(updateSize:)
                          withObject: (id)sized
                          waitUntilDone: NO];
                    CFRelease(sized);
                }
                image.size = _size;
                [self cropUpdate];
            }

            ngood = [scanner scanImage: image];
            syms = scanner.results;
            doTrack = [captureDelegate respondsToSelector:
                          @selector(captureReader:didTrackSymbols:)];
        }
        now = timer_now();

        if(ngood >= 0) {
            // return unfiltered results for tracking feedback
            syms.filterSymbols = NO;
            int nraw = syms.count;
            if(nraw > 0 || (_state & CAPTURE))
                zlog(@"scan image: %dx%d crop=%@ ngood=%d nraw=%d st=%d",
                     w, h, NSStringFromCGRect(image.crop), ngood, nraw, _state);

            if(ngood || (_state & CAPTURE)) {
                // copy image data so we can release the buffer
                result.size = CGSizeMake(w, h);
                result.pixelBuffer = buf;
                result.symbols = syms;
                t_scan = now;
                OSAtomicXor32Barrier((_state & CAPTURE) | PAUSED, &state);
                [self performSelectorOnMainThread:
                          @selector(didReadNewSymbolsFromImage:)
                      withObject: result
                      waitUntilDone: NO];
                [self initResult];
            }

            if(nraw && doTrack)
                [self performSelectorOnMainThread:
                          @selector(didTrackSymbols:)
                      withObject: syms
                      waitUntilDone: NO];
        }
        [image setData: NULL
               withLength: 0];
    }
    else
        zlog(@"ERROR: invalid data");
    CVPixelBufferUnlockBaseAddress(buf, kCVPixelBufferLock_ReadOnly);

 error:
    [pool release];
}

@end