video.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #ifndef _VIDEO_H_
  24. #define _VIDEO_H_
  25. #include <config.h>
  26. #ifdef HAVE_INTTYPES_H
  27. # include <inttypes.h>
  28. #endif
  29. //#include <stdlib.h>
  30. #include <string.h>
  31. //#include <assert.h>
  32. #include <zbar.h>
  33. #include "image.h"
  34. #include "error.h"
  35. #include "mutex.h"
  36. /* number of images to preallocate */
  37. #define ZBAR_VIDEO_IMAGES_MAX 4
  38. typedef enum video_interface_e {
  39. VIDEO_INVALID = 0, /* uninitialized */
  40. VIDEO_V4L1, /* v4l protocol version 1 */
  41. VIDEO_V4L2, /* v4l protocol version 2 */
  42. VIDEO_VFW, /* video for windows */
  43. } video_interface_t;
  44. typedef enum video_iomode_e {
  45. VIDEO_READWRITE = 1, /* standard system calls */
  46. VIDEO_MMAP, /* mmap interface */
  47. VIDEO_USERPTR, /* userspace buffers */
  48. } video_iomode_t;
  49. typedef struct video_state_s video_state_t;
  50. struct zbar_video_s {
  51. errinfo_t err; /* error reporting */
  52. int fd; /* open camera device */
  53. unsigned width, height; /* video frame size */
  54. video_interface_t intf; /* input interface type */
  55. video_iomode_t iomode; /* video data transfer mode */
  56. unsigned initialized : 1; /* format selected and images mapped */
  57. unsigned active : 1; /* current streaming state */
  58. uint32_t format; /* selected fourcc */
  59. unsigned palette; /* v4l1 format index corresponding to format */
  60. uint32_t *formats; /* 0 terminated list of supported formats */
  61. unsigned long datalen; /* size of image data for selected format */
  62. unsigned long buflen; /* total size of image data buffer */
  63. void *buf; /* image data buffer */
  64. unsigned frame; /* frame count */
  65. zbar_mutex_t qlock; /* lock image queue */
  66. int num_images; /* number of allocated images */
  67. zbar_image_t **images; /* indexed list of images */
  68. zbar_image_t *nq_image; /* last image enqueued */
  69. zbar_image_t *dq_image; /* first image to dequeue (when ordered) */
  70. zbar_image_t *shadow_image; /* special case internal double buffering */
  71. video_state_t *state; /* platform/interface specific state */
  72. #ifdef HAVE_LIBJPEG
  73. struct jpeg_decompress_struct *jpeg; /* JPEG decompressor */
  74. zbar_image_t *jpeg_img; /* temporary image */
  75. #endif
  76. /* interface dependent methods */
  77. int (*init)(zbar_video_t*, uint32_t);
  78. int (*cleanup)(zbar_video_t*);
  79. int (*start)(zbar_video_t*);
  80. int (*stop)(zbar_video_t*);
  81. int (*nq)(zbar_video_t*, zbar_image_t*);
  82. zbar_image_t* (*dq)(zbar_video_t*);
  83. };
  84. /* video.next_image and video.recycle_image have to be thread safe
  85. * wrt/other apis
  86. */
  87. static inline int video_lock (zbar_video_t *vdo)
  88. {
  89. int rc = 0;
  90. if((rc = _zbar_mutex_lock(&vdo->qlock))) {
  91. err_capture(vdo, SEV_FATAL, ZBAR_ERR_LOCKING, __func__,
  92. "unable to acquire lock");
  93. vdo->err.errnum = rc;
  94. return(-1);
  95. }
  96. return(0);
  97. }
  98. static inline int video_unlock (zbar_video_t *vdo)
  99. {
  100. int rc = 0;
  101. if((rc = _zbar_mutex_unlock(&vdo->qlock))) {
  102. err_capture(vdo, SEV_FATAL, ZBAR_ERR_LOCKING, __func__,
  103. "unable to release lock");
  104. vdo->err.errnum = rc;
  105. return(-1);
  106. }
  107. return(0);
  108. }
  109. static inline int video_nq_image (zbar_video_t *vdo,
  110. zbar_image_t *img)
  111. {
  112. /* maintains queued buffers in order */
  113. img->next = NULL;
  114. if(vdo->nq_image)
  115. vdo->nq_image->next = img;
  116. vdo->nq_image = img;
  117. if(!vdo->dq_image)
  118. vdo->dq_image = img;
  119. return(video_unlock(vdo));
  120. }
  121. static inline zbar_image_t *video_dq_image (zbar_video_t *vdo)
  122. {
  123. zbar_image_t *img = vdo->dq_image;
  124. if(img) {
  125. vdo->dq_image = img->next;
  126. img->next = NULL;
  127. }
  128. if(video_unlock(vdo))
  129. /* FIXME reclaim image */
  130. return(NULL);
  131. return(img);
  132. }
  133. /* PAL interface */
  134. extern int _zbar_video_open(zbar_video_t*, const char*);
  135. #endif