queue.h usage

from http://jiangpingyu.blog.163.com/blog/static/55453685200831032237166/

/*
 * TAILQ example program.
 */
#include <stdlib.h>
#include <stdio.h>
/*
 * On many OpenBSD/NetBSD/FreeBSD you could include <sys/queue.h>, but
 * for portability we’ll include the local copy.
 */
#include "queue.h"
/*
 * This structure defines each item in our tail queue.  It must also
 * contain an item (TAILQ_ENTRY) that points to the next and previous
 * items in the tail queue.
 *
 * For simplicity, we will be creating a list of integers.
 */
struct tailq_entry {
int value;
/*
         * This holds the pointers to the next and previous entries in
         * the tail queue.
         */
TAILQ_ENTRY(tailq_entry) entries;
};
/*
 * Our tail queue requires a head, this is defined using the
 * TAILQ_HEAD macro.
 */
TAILQ_HEAD(, tailq_entry) my_tailq_head;
int
main(int argc, char **argv)
{
/* Define a pointer to an item in the tail queue. */
struct tailq_entry *item;
/* In some cases we have to track a temporary item. */
struct tailq_entry *tmp_item;
int i;
/* Initialize the tail queue. */
TAILQ_INIT(&my_tailq_head);
/* Add 10 items to the tailq queue. */
for (i = 0; i < 10; i++) {
/*
                 * Each item we want to add to the tail queue must be
                 * allocated.
                 */
item = malloc(sizeof(*item));
if (item == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
/* Set the value. */
item->value = i;
/*
                 * Add our item to the end of tail queue. The first
                 * argument is a pointer to the head of our tail
                 * queue, the second is the item we want to add, and
                 * the third argument is the name of the struct
                 * variable that points to the next and previous items
                 * in the tail queue.
                 */
TAILQ_INSERT_TAIL(&my_tailq_head, item, entries);
}
/* Traverse the tail queue forward. */
printf("Forward traversal: ");
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("n");
/* Insert a new item after the item with value 5. */
printf("Adding new item after 5: ");
TAILQ_FOREACH(item, &my_tailq_head, entries) {
if (item->value == 5) {
struct tailq_entry *new_item =
malloc(sizeof(*new_item));
if (new_item == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
new_item->value = 10;
TAILQ_INSERT_AFTER(&my_tailq_head, item, new_item,
entries);
break;
}
}
/* Do another forward traversal to show the newly added item. */
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("n");
/*
         * Delete the item with the value 3.
         *
         * We can’t use TAILQ_FOREACH here as TAILQ_FOREACH is not
         * safe against deletions during the traversal.  Some variants
         * of queue.h have TAILQ_FOREACH_MUTABLE or TAILQ_FOREACH_SAFE
         * which are safe against deletions.
         */
printf("Deleting item with value 3: ");
for (item = TAILQ_FIRST(&my_tailq_head); item != NULL; item = tmp_item)
{
tmp_item = TAILQ_NEXT(item, entries);
if (item->value == 3) {
/* Remove the item from the tail queue. */
TAILQ_REMOVE(&my_tailq_head, item, entries);
/* Free the item as we don’t need it anymore. */
free(item);
break;
}
}
/* Another forward traversal to show that the value 3 is now gone. */
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("n");
/* Free the entire tail queue. */
while (item = TAILQ_FIRST(&my_tailq_head)) {
TAILQ_REMOVE(&my_tailq_head, item, entries);
free(item);
}
/* The tail queue should now be empty. */
if (!TAILQ_EMPTY(&my_tailq_head))
printf("tail queue is NOT empty!n");
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Amlogic SoC播放H.264视频的示例代码: ```C #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <linux/videodev2.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include "amvideocap.h" // 视频设备名称 #define DEVICE_NAME "/dev/video0" int main(int argc, char *argv[]) { int fd, ret; struct v4l2_capability cap; struct v4l2_format fmt; struct v4l2_requestbuffers req; struct v4l2_buffer buf; void *buffer_start[4]; size_t buffer_length[4]; size_t num_buffers; int i, j; char *filename = NULL; int frame_count = 0; if (argc < 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); exit(EXIT_FAILURE); } filename = argv[1]; // 打开视频设备 fd = open(DEVICE_NAME, O_RDWR); if (fd == -1) { fprintf(stderr, "Failed to open device: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 查询设备能力 ret = ioctl(fd, VIDIOC_QUERYCAP, &cap); if (ret == -1) { fprintf(stderr, "Failed to query capability: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 检查设备是否支持视频捕捉 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { fprintf(stderr, "Device does not support video capture\n"); exit(EXIT_FAILURE); } // 检查设备是否支持H.264编码 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) { fprintf(stderr, "Device does not support multiplanar video capture\n"); exit(EXIT_FAILURE); } // 设置视频格式 memset(&fmt, 0, sizeof(fmt)); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; fmt.fmt.pix_mp.width = 1920; // 视频宽度 fmt.fmt.pix_mp.height = 1080; // 视频高度 fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264; // 视频编码格式 fmt.fmt.pix_mp.num_planes = 1; // 每帧数据平面数 ret = ioctl(fd, VIDIOC_S_FMT, &fmt); if (ret == -1) { fprintf(stderr, "Failed to set format: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 请求视频缓冲区 memset(&req, 0, sizeof(req)); req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; req.memory = V4L2_MEMORY_MMAP; req.count = 4; ret = ioctl(fd, VIDIOC_REQBUFS, &req); if (ret == -1) { fprintf(stderr, "Failed to request buffers: %s\n", strerror(errno)); exit(EXIT_FAILURE); } num_buffers = req.count; // 映射视频缓冲区 for (i = 0; i < num_buffers; i++) { struct v4l2_plane planes[1]; struct v4l2_buffer buf; memset(&buf, 0, sizeof(buf)); memset(&planes, 0, sizeof(planes)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; buf.m.planes = planes; buf.length = 1; ret = ioctl(fd, VIDIOC_QUERYBUF, &buf); if (ret == -1) { fprintf(stderr, "Failed to query buffer %d: %s\n", i, strerror(errno)); exit(EXIT_FAILURE); } buffer_length[i] = buf.m.planes[0].length; buffer_start[i] = mmap(NULL, buffer_length[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.planes[0].m.mem_offset); if (buffer_start[i] == MAP_FAILED) { fprintf(stderr, "Failed to mmap buffer %d: %s\n", i, strerror(errno)); exit(EXIT_FAILURE); } } // 将视频缓冲区入队 for (i = 0; i < num_buffers; i++) { struct v4l2_plane planes[1]; struct v4l2_buffer buf; memset(&buf, 0, sizeof(buf)); memset(&planes, 0, sizeof(planes)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; buf.m.planes = planes; buf.length = 1; ret = ioctl(fd, VIDIOC_QBUF, &buf); if (ret == -1) { fprintf(stderr, "Failed to queue buffer %d: %s\n", i, strerror(errno)); exit(EXIT_FAILURE); } } // 启动视频捕获 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; ret = ioctl(fd, VIDIOC_STREAMON, &type); if (ret == -1) { fprintf(stderr, "Failed to start streaming: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 打开输出文件 int output_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP); if (output_fd == -1) { fprintf(stderr, "Failed to open output file: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 循环获取视频帧数据并写入输出文件 while (frame_count < 100) { // 从视频缓冲区取出一帧数据 memset(&buf, 0, sizeof(buf)); memset(&buf.m, 0, sizeof(buf.m)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; buf.memory = V4L2_MEMORY_MMAP; ret = ioctl(fd, VIDIOC_DQBUF, &buf); if (ret == -1) { fprintf(stderr, "Failed to dequeue buffer: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 将帧数据写入输出文件 size_t bytes_written = write(output_fd, buffer_start[buf.index], buffer_length[buf.index]); if (bytes_written != buffer_length[buf.index]) { fprintf(stderr, "Failed to write frame to output file: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 将视频缓冲区重新入队 ret = ioctl(fd, VIDIOC_QBUF, &buf); if (ret == -1) { fprintf(stderr, "Failed to queue buffer: %s\n", strerror(errno)); exit(EXIT_FAILURE); } frame_count++; } // 停止视频捕获 ret = ioctl(fd, VIDIOC_STREAMOFF, &type); if (ret == -1) { fprintf(stderr, "Failed to stop streaming: %s\n", strerror(errno)); exit(EXIT_FAILURE); } // 关闭视频设备 close(fd); // 关闭输出文件 close(output_fd); // 释放视频缓冲区 for (i = 0; i < num_buffers; i++) { munmap(buffer_start[i], buffer_length[i]); } exit(EXIT_SUCCESS); } ``` 这段代码使用V4L2接口与Amlogic SoC的视频设备进行交互,从视频设备中获取H.264编码的视频帧数据,并将其写入文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值