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.

270 lines
8.4KB

  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 "libavcodec/internal.h"
  31. #include "v4l2_context.h"
  32. #include "v4l2_m2m.h"
  33. #include "v4l2_fmt.h"
  34. static int v4l2_try_start(AVCodecContext *avctx)
  35. {
  36. V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
  37. V4L2Context *const capture = &s->capture;
  38. V4L2Context *const output = &s->output;
  39. struct v4l2_selection selection;
  40. int ret;
  41. /* 1. start the output process */
  42. if (!output->streamon) {
  43. ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
  44. if (ret < 0) {
  45. av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON on output context\n");
  46. return ret;
  47. }
  48. }
  49. if (capture->streamon)
  50. return 0;
  51. /* 2. get the capture format */
  52. capture->format.type = capture->type;
  53. ret = ioctl(s->fd, VIDIOC_G_FMT, &capture->format);
  54. if (ret) {
  55. av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_FMT ioctl\n");
  56. return ret;
  57. }
  58. /* 2.1 update the AVCodecContext */
  59. avctx->pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO);
  60. capture->av_pix_fmt = avctx->pix_fmt;
  61. /* 3. set the crop parameters */
  62. selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  63. selection.r.height = avctx->coded_height;
  64. selection.r.width = avctx->coded_width;
  65. ret = ioctl(s->fd, VIDIOC_S_SELECTION, &selection);
  66. if (!ret) {
  67. ret = ioctl(s->fd, VIDIOC_G_SELECTION, &selection);
  68. if (ret) {
  69. av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_SELECTION ioctl\n");
  70. } else {
  71. av_log(avctx, AV_LOG_DEBUG, "crop output %dx%d\n", selection.r.width, selection.r.height);
  72. /* update the size of the resulting frame */
  73. capture->height = selection.r.height;
  74. capture->width = selection.r.width;
  75. }
  76. }
  77. /* 4. init the capture context now that we have the capture format */
  78. if (!capture->buffers) {
  79. ret = ff_v4l2_context_init(capture);
  80. if (ret) {
  81. av_log(avctx, AV_LOG_ERROR, "can't request capture buffers\n");
  82. return AVERROR(ENOMEM);
  83. }
  84. }
  85. /* 5. start the capture process */
  86. ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
  87. if (ret) {
  88. av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON, on capture context\n");
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. static int v4l2_prepare_decoder(V4L2m2mContext *s)
  94. {
  95. struct v4l2_event_subscription sub;
  96. V4L2Context *output = &s->output;
  97. int ret;
  98. /**
  99. * requirements
  100. */
  101. memset(&sub, 0, sizeof(sub));
  102. sub.type = V4L2_EVENT_SOURCE_CHANGE;
  103. ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
  104. if ( ret < 0) {
  105. if (output->height == 0 || output->width == 0) {
  106. av_log(s->avctx, AV_LOG_ERROR,
  107. "the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n"
  108. "you must provide codec_height and codec_width on input\n");
  109. return ret;
  110. }
  111. }
  112. return 0;
  113. }
  114. static int v4l2_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  115. {
  116. V4L2m2mContext *s = ((V4L2m2mPriv*)avctx->priv_data)->context;
  117. V4L2Context *const capture = &s->capture;
  118. V4L2Context *const output = &s->output;
  119. AVPacket avpkt = {0};
  120. int ret;
  121. if (s->buf_pkt.size) {
  122. avpkt = s->buf_pkt;
  123. memset(&s->buf_pkt, 0, sizeof(AVPacket));
  124. } else {
  125. ret = ff_decode_get_packet(avctx, &avpkt);
  126. if (ret < 0 && ret != AVERROR_EOF)
  127. return ret;
  128. }
  129. if (s->draining)
  130. goto dequeue;
  131. ret = ff_v4l2_context_enqueue_packet(output, &avpkt);
  132. if (ret < 0) {
  133. if (ret != AVERROR(EAGAIN))
  134. return ret;
  135. s->buf_pkt = avpkt;
  136. /* no input buffers available, continue dequeing */
  137. }
  138. if (avpkt.size) {
  139. ret = v4l2_try_start(avctx);
  140. if (ret) {
  141. av_packet_unref(&avpkt);
  142. /* cant recover */
  143. if (ret == AVERROR(ENOMEM))
  144. return ret;
  145. return 0;
  146. }
  147. }
  148. dequeue:
  149. if (!s->buf_pkt.size)
  150. av_packet_unref(&avpkt);
  151. return ff_v4l2_context_dequeue_frame(capture, frame, -1);
  152. }
  153. static av_cold int v4l2_decode_init(AVCodecContext *avctx)
  154. {
  155. V4L2Context *capture, *output;
  156. V4L2m2mContext *s;
  157. V4L2m2mPriv *priv = avctx->priv_data;
  158. int ret;
  159. ret = ff_v4l2_m2m_create_context(priv, &s);
  160. if (ret < 0)
  161. return ret;
  162. capture = &s->capture;
  163. output = &s->output;
  164. /* if these dimensions are invalid (ie, 0 or too small) an event will be raised
  165. * by the v4l2 driver; this event will trigger a full pipeline reconfig and
  166. * the proper values will be retrieved from the kernel driver.
  167. */
  168. output->height = capture->height = avctx->coded_height;
  169. output->width = capture->width = avctx->coded_width;
  170. output->av_codec_id = avctx->codec_id;
  171. output->av_pix_fmt = AV_PIX_FMT_NONE;
  172. capture->av_codec_id = AV_CODEC_ID_RAWVIDEO;
  173. capture->av_pix_fmt = avctx->pix_fmt;
  174. ret = ff_v4l2_m2m_codec_init(priv);
  175. if (ret) {
  176. av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n");
  177. s->self_ref = NULL;
  178. av_buffer_unref(&priv->context_ref);
  179. return ret;
  180. }
  181. s->avctx = avctx;
  182. return v4l2_prepare_decoder(s);
  183. }
  184. static av_cold int v4l2_decode_close(AVCodecContext *avctx)
  185. {
  186. V4L2m2mPriv *priv = avctx->priv_data;
  187. V4L2m2mContext* s = priv->context;
  188. av_packet_unref(&s->buf_pkt);
  189. return ff_v4l2_m2m_codec_end(priv);
  190. }
  191. #define OFFSET(x) offsetof(V4L2m2mPriv, x)
  192. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  193. static const AVOption options[] = {
  194. V4L_M2M_DEFAULT_OPTS,
  195. { "num_capture_buffers", "Number of buffers in the capture context",
  196. OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
  197. { NULL},
  198. };
  199. #define M2MDEC_CLASS(NAME) \
  200. static const AVClass v4l2_m2m_ ## NAME ## _dec_class = { \
  201. .class_name = #NAME "_v4l2m2m_decoder", \
  202. .item_name = av_default_item_name, \
  203. .option = options, \
  204. .version = LIBAVUTIL_VERSION_INT, \
  205. };
  206. #define M2MDEC(NAME, LONGNAME, CODEC, bsf_name) \
  207. M2MDEC_CLASS(NAME) \
  208. AVCodec ff_ ## NAME ## _v4l2m2m_decoder = { \
  209. .name = #NAME "_v4l2m2m" , \
  210. .long_name = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " decoder wrapper"), \
  211. .type = AVMEDIA_TYPE_VIDEO, \
  212. .id = CODEC , \
  213. .priv_data_size = sizeof(V4L2m2mPriv), \
  214. .priv_class = &v4l2_m2m_ ## NAME ## _dec_class, \
  215. .init = v4l2_decode_init, \
  216. .receive_frame = v4l2_receive_frame, \
  217. .close = v4l2_decode_close, \
  218. .bsfs = bsf_name, \
  219. .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
  220. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
  221. .wrapper_name = "v4l2m2m", \
  222. };
  223. M2MDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb");
  224. M2MDEC(hevc, "HEVC", AV_CODEC_ID_HEVC, "hevc_mp4toannexb");
  225. M2MDEC(mpeg1, "MPEG1", AV_CODEC_ID_MPEG1VIDEO, NULL);
  226. M2MDEC(mpeg2, "MPEG2", AV_CODEC_ID_MPEG2VIDEO, NULL);
  227. M2MDEC(mpeg4, "MPEG4", AV_CODEC_ID_MPEG4, NULL);
  228. M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL);
  229. M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL);
  230. M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL);
  231. M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL);