You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.2KB

  1. /*
  2. * V4L2 mem2mem decoders
  3. *
  4. * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
  5. * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <linux/videodev2.h>
  24. #include <sys/ioctl.h>
  25. #include "libavutil/pixfmt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/opt.h"
  28. #include "libavcodec/avcodec.h"
  29. #include "libavcodec/decode.h"
  30. #include "v4l2_context.h"
  31. #include "v4l2_m2m.h"
  32. #include "v4l2_fmt.h"
  33. static int v4l2_try_start(AVCodecContext *avctx)
  34. {
  35. V4L2m2mContext *s = avctx->priv_data;
  36. V4L2Context *const capture = &s->capture;
  37. V4L2Context *const output = &s->output;
  38. struct v4l2_selection selection;
  39. int ret;
  40. /* 1. start the output process */
  41. if (!output->streamon) {
  42. ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
  43. if (ret < 0) {
  44. av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON on output context\n");
  45. return ret;
  46. }
  47. }
  48. if (capture->streamon)
  49. return 0;
  50. /* 2. get the capture format */
  51. capture->format.type = capture->type;
  52. ret = ioctl(s->fd, VIDIOC_G_FMT, &capture->format);
  53. if (ret) {
  54. av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_FMT ioctl\n");
  55. return ret;
  56. }
  57. /* 2.1 update the AVCodecContext */
  58. avctx->pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO);
  59. capture->av_pix_fmt = avctx->pix_fmt;
  60. /* 3. set the crop parameters */
  61. selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  62. selection.r.height = avctx->coded_height;
  63. selection.r.width = avctx->coded_width;
  64. ret = ioctl(s->fd, VIDIOC_S_SELECTION, &selection);
  65. if (!ret) {
  66. ret = ioctl(s->fd, VIDIOC_G_SELECTION, &selection);
  67. if (ret) {
  68. av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_SELECTION ioctl\n");
  69. } else {
  70. av_log(avctx, AV_LOG_DEBUG, "crop output %dx%d\n", selection.r.width, selection.r.height);
  71. /* update the size of the resulting frame */
  72. capture->height = selection.r.height;
  73. capture->width = selection.r.width;
  74. }
  75. }
  76. /* 4. init the capture context now that we have the capture format */
  77. if (!capture->buffers) {
  78. ret = ff_v4l2_context_init(capture);
  79. if (ret) {
  80. av_log(avctx, AV_LOG_DEBUG, "can't request output buffers\n");
  81. return ret;
  82. }
  83. }
  84. /* 5. start the capture process */
  85. ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
  86. if (ret) {
  87. av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON, on capture context\n");
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. static int v4l2_prepare_decoder(V4L2m2mContext *s)
  93. {
  94. struct v4l2_event_subscription sub;
  95. V4L2Context *output = &s->output;
  96. int ret;
  97. /**
  98. * requirements
  99. */
  100. memset(&sub, 0, sizeof(sub));
  101. sub.type = V4L2_EVENT_SOURCE_CHANGE;
  102. ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
  103. if ( ret < 0) {
  104. if (output->height == 0 || output->width == 0) {
  105. av_log(s->avctx, AV_LOG_ERROR,
  106. "the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n"
  107. "you must provide codec_height and codec_width on input\n");
  108. return ret;
  109. }
  110. }
  111. return 0;
  112. }
  113. static int v4l2_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  114. {
  115. V4L2m2mContext *s = avctx->priv_data;
  116. V4L2Context *const capture = &s->capture;
  117. V4L2Context *const output = &s->output;
  118. AVPacket avpkt = {0};
  119. int ret;
  120. ret = ff_decode_get_packet(avctx, &avpkt);
  121. if (ret < 0 && ret != AVERROR_EOF)
  122. return ret;
  123. if (s->draining)
  124. goto dequeue;
  125. ret = ff_v4l2_context_enqueue_packet(output, &avpkt);
  126. if (ret < 0) {
  127. if (ret != AVERROR(ENOMEM))
  128. return ret;
  129. /* no input buffers available, continue dequeing */
  130. }
  131. if (avpkt.size) {
  132. ret = v4l2_try_start(avctx);
  133. if (ret)
  134. return 0;
  135. }
  136. dequeue:
  137. return ff_v4l2_context_dequeue_frame(capture, frame);
  138. }
  139. static av_cold int v4l2_decode_init(AVCodecContext *avctx)
  140. {
  141. V4L2m2mContext *s = avctx->priv_data;
  142. V4L2Context *capture = &s->capture;
  143. V4L2Context *output = &s->output;
  144. int ret;
  145. /* if these dimensions are invalid (ie, 0 or too small) an event will be raised
  146. * by the v4l2 driver; this event will trigger a full pipeline reconfig and
  147. * the proper values will be retrieved from the kernel driver.
  148. */
  149. output->height = capture->height = avctx->coded_height;
  150. output->width = capture->width = avctx->coded_width;
  151. output->av_codec_id = avctx->codec_id;
  152. output->av_pix_fmt = AV_PIX_FMT_NONE;
  153. capture->av_codec_id = AV_CODEC_ID_RAWVIDEO;
  154. capture->av_pix_fmt = avctx->pix_fmt;
  155. ret = ff_v4l2_m2m_codec_init(avctx);
  156. if (ret) {
  157. av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n");
  158. return ret;
  159. }
  160. return v4l2_prepare_decoder(s);
  161. }
  162. #define OFFSET(x) offsetof(V4L2m2mContext, x)
  163. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  164. static const AVOption options[] = {
  165. V4L_M2M_DEFAULT_OPTS,
  166. { "num_capture_buffers", "Number of buffers in the capture context",
  167. OFFSET(capture.num_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
  168. { NULL},
  169. };
  170. #define M2MDEC(NAME, LONGNAME, CODEC, bsf_name) \
  171. static const AVClass v4l2_m2m_ ## NAME ## _dec_class = {\
  172. .class_name = #NAME "_v4l2_m2m_decoder",\
  173. .item_name = av_default_item_name,\
  174. .option = options,\
  175. .version = LIBAVUTIL_VERSION_INT,\
  176. };\
  177. \
  178. AVCodec ff_ ## NAME ## _v4l2m2m_decoder = { \
  179. .name = #NAME "_v4l2m2m" ,\
  180. .long_name = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " decoder wrapper"),\
  181. .type = AVMEDIA_TYPE_VIDEO,\
  182. .id = CODEC ,\
  183. .priv_data_size = sizeof(V4L2m2mContext),\
  184. .priv_class = &v4l2_m2m_ ## NAME ## _dec_class,\
  185. .init = v4l2_decode_init,\
  186. .receive_frame = v4l2_receive_frame,\
  187. .close = ff_v4l2_m2m_codec_end,\
  188. .bsfs = bsf_name, \
  189. };
  190. M2MDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb");
  191. M2MDEC(hevc, "HEVC", AV_CODEC_ID_HEVC, "hevc_mp4toannexb");
  192. M2MDEC(mpeg1, "MPEG1", AV_CODEC_ID_MPEG1VIDEO, NULL);
  193. M2MDEC(mpeg2, "MPEG2", AV_CODEC_ID_MPEG2VIDEO, NULL);
  194. M2MDEC(mpeg4, "MPEG4", AV_CODEC_ID_MPEG4, NULL);
  195. M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL);
  196. M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL);
  197. M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL);
  198. M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL);