Changeset 42
- Timestamp:
- 18/02/11 13:04:53 (15 months ago)
- 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 1 CFLAGS=-Wall -pedantic -I.. -g -cstd=gnu99 2 LDFLAGS=-g 3 LDLIBS=-ljpeg -lm -lpthread -lcurl -lavcodec -ldmtx -lswscale 3 4 4 5 all: grab 5 6 6 7 grab: grab.o web.o 7 cc -o grab grab.o web.o ../.libs/libdmtx.a $(LDFLAGS) -
trunk/doorcam/grab.c
r41 r42 3 3 #include <sys/ioctl.h> 4 4 #include <errno.h> 5 #include <err.h> 5 6 #include <string.h> 6 7 #include <unistd.h> … … 15 16 #include <sys/time.h> 16 17 #include <curl/curl.h> 17 #include <ffmpeg/avcodec.h> 18 #include <libavcodec/avcodec.h> 19 #include <libswscale/swscale.h> 18 20 19 21 int verbose=0; … … 410 412 /* look for barcodes */ 411 413 DmtxImage *image; 412 DmtxDecode decode;413 DmtxRegion region;414 DmtxDecode *decode; 415 DmtxRegion *region; 414 416 DmtxMessage *message; 415 DmtxPixelLoc pMin,pMax;416 417 DmtxTime timeout; 417 418 int count=0, errors=0; … … 419 420 struct gotlist *fp; 420 421 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 432 426 while (1) { 433 427 /* waste at most 300mS looking for the first block */ 434 428 timeout = dmtxTimeAdd(dmtxTimeNow(), decode_timeout); 435 region = dmtx DecodeFindNextRegion(&decode, &timeout);436 if (region .found != DMTX_REGION_FOUND) break;429 region = dmtxRegionFindNext(decode, &timeout); 430 if (region == NULL) break; 437 431 count++; 438 432 439 message = dmtxDecodeMatrixRegion(image, ®ion, 1); 433 message = dmtxDecodeMatrixRegion(decode, region, DmtxUndefined); 434 440 435 if (message == NULL) { 441 436 errors++; 437 dmtxRegionDestroy(®ion); 442 438 continue; 443 439 } … … 455 451 found = fp; 456 452 } 457 dmtxMessageFree(&message); 458 } 459 dmtxDecodeStructDeInit(&decode); 460 dmtxImageFree(&image); 453 dmtxMessageDestroy(&message); 454 dmtxRegionDestroy(®ion); 455 } 456 dmtxDecodeDestroy(&decode); 457 dmtxImageDestroy(&image); 461 458 while (found != NULL) { 462 459 fp = found->next; … … 489 486 490 487 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)) { 492 489 fprintf(stderr, "%s does not support capture.\n", device); 493 490 return (void *)1; 494 491 } 495 if (! vcap.capabilities & V4L2_CAP_STREAMING) {492 if (!(vcap.capabilities & V4L2_CAP_STREAMING)) { 496 493 fprintf(stderr, "%s does not support streaming mode.\n", device) 497 494 ; … … 766 763 int done; 767 764 int fsize = gwidth * gheight; 765 static struct SwsContext *img_convert_ctx = NULL; 768 766 769 767 codec = avcodec_find_decoder(CODEC_ID_MJPEG); … … 778 776 picture = avcodec_alloc_frame(); 779 777 if (avcodec_open(c, codec) < 0) { 780 err( "failed to open decoder");778 err(1, "failed to open decoder"); 781 779 return 0; 782 780 } … … 789 787 #endif 790 788 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); 792 790 avcodec_close(c); 793 791 av_free(picture); … … 797 795 outlen = 0; 798 796 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); 799 799 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); 805 804 outlen = fsize*3; 806 805 } else 807 806 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); 808 809 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); 814 814 outlen = fsize*2; 815 815 } else 816 816 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); 826 819 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); 836 826 outlen = fsize + fsize/2; 837 827 }else { 838 err("unrecognised format in decode");828 warn("unrecognised format in decode"); 839 829 outlen = 0; 840 830 }
