aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanimir Varbanov <stanimir.varbanov@linaro.org>2016-06-13 18:17:03 +0300
committerStanimir Varbanov <stanimir.varbanov@linaro.org>2016-06-13 18:34:41 +0300
commit4a72ce038865afdaaddc1fa913dd0ecad8849055 (patch)
treebee1bc2ce8c866c87a296871471165c655b6df58
parent9390a486e2afe3873062f5f0bcc5fa79be7d5450 (diff)
downloadv4l2-encode-4a72ce038865afdaaddc1fa913dd0ecad8849055.tar.gz
adding command line args to set bitrate and framerate
Signed-off-by: Stanimir Varbanov <stanimir.varbanov@linaro.org>
-rw-r--r--args.c12
-rw-r--r--common.h3
-rw-r--r--main.c15
-rw-r--r--video.c88
4 files changed, 58 insertions, 60 deletions
diff --git a/args.c b/args.c
index f6bb18c..e2248a6 100644
--- a/args.c
+++ b/args.c
@@ -35,7 +35,7 @@ void print_usage(char *name)
printf("Usage:\n");
printf("\t%s\n", name);
printf("\t-c <codec> - The codec to be used for encoding\n");
- printf("\t\t Available codecs: mpeg4, h264\n");
+ printf("\t\t Available codecs: mpeg4, h264, h263, xvid, mpeg2, mpeg1, vp8\n");
printf("\t-d use dmabuf instead of mmap\n");
printf("\t-m <device> - video encoder device (e.g. /dev/video33)\n");
printf("\t-w video width\n");
@@ -43,6 +43,8 @@ void print_usage(char *name)
printf("\t-f save encoded frames on disk\n");
printf("\t-g disable test pattern generator\n");
printf("\t-v verbose mode\n");
+ printf("\t-B bitrate\n");
+ printf("\t-F framerate\n");
printf("\n");
}
@@ -77,7 +79,7 @@ int parse_args(struct instance *i, int argc, char **argv)
init_to_defaults(i);
- while ((c = getopt(argc, argv, "w:h:c:d:m:f:n:gv")) != -1) {
+ while ((c = getopt(argc, argv, "w:h:c:d:m:f:n:gvB:F:")) != -1) {
switch (c) {
case 'c':
i->codec = get_codec(optarg);
@@ -108,6 +110,12 @@ int parse_args(struct instance *i, int argc, char **argv)
case 'v':
i->verbose = true;
break;
+ case 'B':
+ i->bitrate = atoi(optarg);
+ break;
+ case 'F':
+ i->framerate = atoi(optarg);
+ break;
default:
err("Bad argument");
return -1;
diff --git a/common.h b/common.h
index 6752a33..cd2c0d8 100644
--- a/common.h
+++ b/common.h
@@ -156,6 +156,9 @@ struct instance {
/* video decoder related parameters */
struct video video;
+ unsigned int bitrate;
+ unsigned int framerate;
+
/* Codec to be used for encoding */
unsigned long codec;
char *codec_name;
diff --git a/main.c b/main.c
index 1261715..2d3e5fd 100644
--- a/main.c
+++ b/main.c
@@ -44,7 +44,7 @@
/* This is the size of the buffer for the compressed stream.
* It limits the maximum compressed frame size. */
-#define STREAM_BUUFER_SIZE (1024 * 1024)
+#define STREAM_BUFFER_SIZE (1024 * 1024)
/* The number of compress4ed stream buffers */
#define STREAM_BUFFER_CNT 2
@@ -352,7 +352,7 @@ int main(int argc, char **argv)
if (ret)
goto err;
- ret = video_setup_output(&inst, inst.codec, STREAM_BUUFER_SIZE, 1);
+ ret = video_setup_output(&inst, inst.codec, STREAM_BUFFER_SIZE, 1);
if (ret)
goto err;
@@ -382,17 +382,6 @@ int main(int argc, char **argv)
if (ret)
goto err;
- if (0) {
- struct v4l2_control cntrl;
-
- cntrl.id = V4L2_CID_MPEG_VIDC_VIDEO_REQUEST_SEQ_HEADER;
- cntrl.value = 1;
-
- ret = ioctl(inst.video.fd, VIDIOC_S_CTRL, &cntrl);
- if (ret)
- err("request seq header failed (%s)", strerror(errno));
- }
-
dbg("Launching threads");
if (pthread_create(&input_thread, NULL, input_thread_func, &inst))
diff --git a/video.c b/video.c
index 3440390..e6f9a58 100644
--- a/video.c
+++ b/video.c
@@ -84,73 +84,71 @@ int video_set_control(struct instance *i)
parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
parm.parm.output.timeperframe.numerator = 1;
- parm.parm.output.timeperframe.denominator = 30;
+ if (i->framerate <= 30 && i->framerate >= 1)
+ parm.parm.output.timeperframe.denominator = i->framerate;
+ else
+ parm.parm.output.timeperframe.denominator = 30;
+
+ info("setting framerate: %u", i->framerate);
- info("setting framerate");
ret = ioctl(i->video.fd, VIDIOC_S_PARM, &parm);
if (ret)
err("set framerate (%s)", strerror(errno));
cntrl.id = V4L2_CID_MPEG_VIDEO_BITRATE;
- cntrl.value = 10 * 1024 * 1024;
+ if (i->bitrate <= 160000000 && i->bitrate >= 32000)
+ cntrl.value = i->bitrate;
+ else
+ cntrl.value = 10 * 1024 * 1024;
info("setting bitrate: %u", cntrl.value);
- ret = ioctl(i->video.fd, VIDIOC_S_CTRL, &cntrl);
- if (ret)
- err("set control - bitrate (%s)", strerror(errno));
-
- cntrl.id = V4L2_CID_MPEG_VIDC_VIDEO_H264_VUI_TIMING_INFO;
- cntrl.value = V4L2_MPEG_VIDC_VIDEO_H264_VUI_TIMING_INFO_ENABLED;
ret = ioctl(i->video.fd, VIDIOC_S_CTRL, &cntrl);
if (ret)
- err("set control - vui timing info (%s)", strerror(errno));
+ err("set control - bitrate (%s)", strerror(errno));
- {
- struct v4l2_ext_controls ctrls;
- struct v4l2_ext_control ctrl[32];
- int c = 0;
+ struct v4l2_ext_controls ctrls;
+ struct v4l2_ext_control ctrl[32];
+ int c = 0;
- memset (&ctrls, 0, sizeof(ctrls));
- ctrls.controls = ctrl;
- ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
+ memset (&ctrls, 0, sizeof(ctrls));
+ ctrls.controls = ctrl;
+ ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
- ctrl[c].id = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
- ctrl[c].value = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
- c++;
+ ctrl[c].id = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
+ ctrl[c].value = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
+ c++;
- ctrl[c].id = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
- ctrl[c].value = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
- c++;
+ ctrl[c].id = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
+ ctrl[c].value = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
+ c++;
-// ctrl[c].id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE;
-// ctrl[c].value = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
-// c++;
+ ctrl[c].id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE;
+ ctrl[c].value = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
+ c++;
- ctrl[c].id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
- ctrl[c].value = 1;
- c++;
+ ctrl[c].id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
+ ctrl[c].value = 1;
+ c++;
- /* intra_period = pframes + bframes + 1
- * bframes/pframes must be integer
- *
- */
+ /*
+ * intra_period = pframes + bframes + 1
+ * bframes/pframes must be integer
+ */
- ctrl[c].id = V4L2_CID_MPEG_VIDEO_B_FRAMES;
- ctrl[c].value = 0;
- c++;
+ ctrl[c].id = V4L2_CID_MPEG_VIDEO_B_FRAMES;
+ ctrl[c].value = 0;
+ c++;
- ctrl[c].id = V4L2_CID_MPEG_VIDC_VIDEO_NUM_P_FRAMES;
- ctrl[c].value = 29;//2 * 15 - 1;
- c++;
+// ctrl[c].id = V4L2_CID_MPEG_VIDC_VIDEO_NUM_P_FRAMES;
+// ctrl[c].value = 2 * 15 - 1;
+// c++;
- ctrls.count = c;
+ ctrls.count = c;
- ret = ioctl(i->video.fd, VIDIOC_S_EXT_CTRLS, &ctrls);
- if (ret)
- err("set ext control - profile/level (%s)",
- strerror(errno));
- }
+ ret = ioctl(i->video.fd, VIDIOC_S_EXT_CTRLS, &ctrls);
+ if (ret)
+ err("set ext controls (%s)", strerror(errno));
return 0;
}