Changeset 42

Show
Ignore:
Timestamp:
18/02/11 13:04:53 (15 months ago)
Author:
arthur
Message:

update doorcam for new libdmtx and avcodec

Location:
trunk/doorcam
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/doorcam/Makefile

    r38 r42  
    1 CFLAGS=-Wall -pedantic -I.. -g 
    2 LDFLAGS=-ljpeg -lm -lpthread -lcurl -lavcodec -g 
     1CFLAGS=-Wall -pedantic -I.. -g -cstd=gnu99 
     2LDFLAGS=-g 
     3LDLIBS=-ljpeg -lm -lpthread -lcurl -lavcodec -ldmtx -lswscale 
    34 
    45all: grab 
    56 
    67grab: grab.o web.o 
    7         cc -o grab grab.o web.o ../.libs/libdmtx.a $(LDFLAGS) 
  • trunk/doorcam/grab.c

    r41 r42  
    33#include <sys/ioctl.h> 
    44#include <errno.h> 
     5#include <err.h> 
    56#include <string.h> 
    67#include <unistd.h> 
     
    1516#include <sys/time.h> 
    1617#include <curl/curl.h> 
    17 #include <ffmpeg/avcodec.h> 
     18#include <libavcodec/avcodec.h> 
     19#include <libswscale/swscale.h> 
    1820 
    1921int verbose=0; 
     
    410412        /* look for barcodes */ 
    411413        DmtxImage *image; 
    412         DmtxDecode decode; 
    413         DmtxRegion region; 
     414        DmtxDecode *decode; 
     415        DmtxRegion *region; 
    414416        DmtxMessage *message; 
    415         DmtxPixelLoc pMin,pMax; 
    416417        DmtxTime timeout; 
    417418        int count=0, errors=0; 
     
    419420        struct gotlist *fp; 
    420421 
    421                  
    422         pMin.X = 0; 
    423         pMin.Y = 0; 
    424         pMax.X = gwidth - 1; 
    425         pMax.Y = gheight - 1; 
    426  
    427         image = dmtxImageMalloc(gwidth, gheight); 
    428         memcpy(image->pxl, img, gwidth*gheight*3); 
    429         image->pageCount = 1; 
    430  
    431         decode = dmtxDecodeStructInit(image); 
     422        image = dmtxImageCreate(img, gwidth, gheight, DmtxPack24bppRGB); 
     423        dmtxImageSetProp(image, DmtxPropImageFlip, DmtxFlipNone); 
     424        decode = dmtxDecodeCreate(image, 1); 
     425 
    432426        while (1) { 
    433427                /* waste at most 300mS looking for the first block */ 
    434428                timeout = dmtxTimeAdd(dmtxTimeNow(), decode_timeout); 
    435                 region = dmtxDecodeFindNextRegion(&decode, &timeout); 
    436                 if (region.found != DMTX_REGION_FOUND) break; 
     429                region = dmtxRegionFindNext(decode, &timeout); 
     430                if (region == NULL) break; 
    437431                count++; 
    438432 
    439                 message = dmtxDecodeMatrixRegion(image, &region, 1); 
     433                message = dmtxDecodeMatrixRegion(decode, region, DmtxUndefined); 
     434 
    440435                if (message == NULL) { 
    441436                        errors++; 
     437                        dmtxRegionDestroy(&region); 
    442438                        continue; 
    443439                } 
     
    455451                        found = fp; 
    456452                } 
    457                 dmtxMessageFree(&message); 
    458         } 
    459         dmtxDecodeStructDeInit(&decode); 
    460         dmtxImageFree(&image); 
     453                dmtxMessageDestroy(&message); 
     454                dmtxRegionDestroy(&region); 
     455        } 
     456        dmtxDecodeDestroy(&decode); 
     457        dmtxImageDestroy(&image); 
    461458        while (found != NULL) { 
    462459                fp = found->next; 
     
    489486 
    490487        if (verbose) printf("%s: %s (%s)\n", vcap.driver, vcap.card, vcap.bus_info); 
    491         if (!vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE) { 
     488        if (!(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { 
    492489                fprintf(stderr, "%s does not support capture.\n", device); 
    493490                return (void *)1; 
    494491        } 
    495         if (!vcap.capabilities & V4L2_CAP_STREAMING) { 
     492        if (!(vcap.capabilities & V4L2_CAP_STREAMING)) { 
    496493                fprintf(stderr, "%s does not support streaming mode.\n", device) 
    497494; 
     
    766763        int done; 
    767764        int fsize = gwidth * gheight; 
     765        static struct SwsContext *img_convert_ctx = NULL; 
    768766 
    769767        codec = avcodec_find_decoder(CODEC_ID_MJPEG); 
     
    778776        picture = avcodec_alloc_frame(); 
    779777        if (avcodec_open(c, codec) < 0) { 
    780                 err("failed to open decoder"); 
     778                err(1, "failed to open decoder"); 
    781779                return 0; 
    782780        } 
     
    789787#endif 
    790788        if (outlen < 0  || done==0 ) { 
    791                 err("jpeg decode failed used=%d/%d done=%d", outlen, len, done); 
     789                warn("jpeg decode failed used=%d/%d done=%d", outlen, len, done); 
    792790                avcodec_close(c); 
    793791                av_free(picture); 
     
    797795        outlen = 0; 
    798796        if (pixfmt_out == V4L2_PIX_FMT_RGB24) { 
     797                if (img_convert_ctx == NULL) 
     798                        img_convert_ctx = sws_getContext(  c->width, c->height, c->pix_fmt, gwidth, gheight, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); 
    799799                outpic.data[0] = data_out; 
    800                 outpic.data[1] = NULL; 
    801                 outpic.data[2] = NULL; 
    802                 outpic.linesize[0] = gwidth * 3; 
    803                 if (img_convert(&outpic, PIX_FMT_RGB24, (AVPicture *)picture, c->pix_fmt, gwidth, gheight)) 
    804                         err("format convert to yuyv failed"); 
     800                outpic.data[1] = NULL; 
     801                outpic.data[2] = NULL; 
     802                outpic.linesize[0] = gwidth * 3; 
     803                sws_scale(img_convert_ctx, picture->data, picture->linesize, 0, c->height, outpic.data, outpic.linesize); 
    805804                outlen = fsize*3; 
    806805        } else 
    807806        if (pixfmt_out == V4L2_PIX_FMT_YUYV) { 
     807                if (img_convert_ctx == NULL) 
     808                        img_convert_ctx = sws_getContext(  c->width, c->height, c->pix_fmt, gwidth, gheight, PIX_FMT_YUYV422, SWS_BICUBIC, NULL, NULL, NULL); 
    808809                outpic.data[0] = data_out; 
    809                 outpic.data[1] = NULL; 
    810                 outpic.data[2] = NULL; 
    811                 outpic.linesize[0] = gwidth * 2; 
    812                 if (img_convert(&outpic, PIX_FMT_YUYV422, (AVPicture *)picture, c->pix_fmt, gwidth, gheight))  
    813                         err("format convert to yuyv failed"); 
     810                outpic.data[1] = NULL; 
     811                outpic.data[2] = NULL; 
     812                outpic.linesize[0] = gwidth * 2; 
     813                sws_scale(img_convert_ctx, picture->data, picture->linesize, 0, c->height, outpic.data, outpic.linesize); 
    814814                outlen = fsize*2; 
    815815        } else 
    816816        if (pixfmt_out == V4L2_PIX_FMT_YUV420) { 
    817 #if 0 
    818                 uint8_t *p = data_out; 
    819                 memcpy(p, picture->data[0], fsize); 
    820                 p += fsize; 
    821                 memcpy(p, picture->data[1], fsize/4); 
    822                 p += fsize/4; 
    823                 memcpy(p, picture->data[2], fsize/4); 
    824                 outlen = fsize + fsize/2; 
    825 #else 
     817                if (img_convert_ctx == NULL) 
     818                        img_convert_ctx = sws_getContext(  c->width, c->height, c->pix_fmt, gwidth, gheight, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); 
    826819                outpic.data[0] = data_out; 
    827                 outpic.data[1] = outpic.data[0] + fsize; 
    828                 outpic.data[2] = outpic.data[1] + fsize/4; 
    829                 outpic.linesize[0] = gwidth; 
    830                 outpic.linesize[1] = gwidth/2; 
    831                 outpic.linesize[2] = gwidth/2; 
    832                 //420 yu12 
    833                 if (img_convert(&outpic, PIX_FMT_YUV420P, (AVPicture *)picture, c->pix_fmt, gwidth, gheight))  
    834                         err("image convert to 420p failed"); 
    835 #endif 
     820                outpic.data[1] = outpic.data[0] + fsize; 
     821                outpic.data[2] = outpic.data[1] + fsize/4; 
     822                outpic.linesize[0] = gwidth; 
     823                outpic.linesize[1] = gwidth/2; 
     824                outpic.linesize[2] = gwidth/2; 
     825                sws_scale(img_convert_ctx, picture->data, picture->linesize, 0, c->height, outpic.data, outpic.linesize); 
    836826                outlen = fsize + fsize/2; 
    837827        }else { 
    838                 err("unrecognised format in decode"); 
     828                warn("unrecognised format in decode"); 
    839829                outlen = 0; 
    840830        }