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.

282 lines
8.2KB

  1. /*
  2. * Copyright (c) 2012 Derek Buitenhuis
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation;
  9. * version 2 of the License.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Known FOURCCs:
  23. * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA),
  24. * 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
  25. */
  26. extern "C" {
  27. #include "libavutil/opt.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. }
  33. #include "libutvideo.h"
  34. #include "put_bits.h"
  35. static av_cold int utvideo_encode_init(AVCodecContext *avctx)
  36. {
  37. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  38. UtVideoExtra *info;
  39. uint32_t flags, in_format;
  40. int ret;
  41. switch (avctx->pix_fmt) {
  42. case AV_PIX_FMT_YUV420P:
  43. in_format = UTVF_YV12;
  44. avctx->bits_per_coded_sample = 12;
  45. if (avctx->colorspace == AVCOL_SPC_BT709)
  46. avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
  47. else
  48. avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
  49. break;
  50. case AV_PIX_FMT_YUYV422:
  51. in_format = UTVF_YUYV;
  52. avctx->bits_per_coded_sample = 16;
  53. if (avctx->colorspace == AVCOL_SPC_BT709)
  54. avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
  55. else
  56. avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
  57. break;
  58. case AV_PIX_FMT_BGR24:
  59. in_format = UTVF_NFCC_BGR_BU;
  60. avctx->bits_per_coded_sample = 24;
  61. avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
  62. break;
  63. case AV_PIX_FMT_RGB32:
  64. in_format = UTVF_NFCC_BGRA_BU;
  65. avctx->bits_per_coded_sample = 32;
  66. avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
  67. break;
  68. default:
  69. return AVERROR(EINVAL);
  70. }
  71. #if FF_API_PRIVATE_OPT
  72. FF_DISABLE_DEPRECATION_WARNINGS
  73. if (avctx->prediction_method)
  74. utv->pred = avctx->prediction_method;
  75. FF_ENABLE_DEPRECATION_WARNINGS
  76. #endif
  77. /* Check before we alloc anything */
  78. if (utv->pred != 0 && utv->pred != 2) {
  79. av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
  80. return AVERROR(EINVAL);
  81. }
  82. flags = ((utv->pred + 1) << 8) | (avctx->thread_count - 1);
  83. avctx->priv_data = utv;
  84. /* Alloc extradata buffer */
  85. info = (UtVideoExtra *)av_malloc(sizeof(*info));
  86. if (!info) {
  87. av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
  88. return AVERROR(ENOMEM);
  89. }
  90. /*
  91. * We use this buffer to hold the data that Ut Video returns,
  92. * since we cannot decode planes separately with it.
  93. */
  94. ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
  95. if (ret < 0) {
  96. av_free(info);
  97. return ret;
  98. }
  99. utv->buf_size = ret;
  100. utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
  101. if (utv->buffer == NULL) {
  102. av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
  103. av_free(info);
  104. return AVERROR(ENOMEM);
  105. }
  106. /*
  107. * Create a Ut Video instance. Since the function wants
  108. * an "interface name" string, pass it the name of the lib.
  109. */
  110. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  111. /* Initialize encoder */
  112. utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
  113. CBGROSSWIDTH_WINDOWS);
  114. /* Get extradata from encoder */
  115. avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
  116. utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
  117. avctx->width, avctx->height,
  118. CBGROSSWIDTH_WINDOWS);
  119. avctx->extradata = (uint8_t *)info;
  120. /* Set flags */
  121. utv->codec->SetState(&flags, sizeof(flags));
  122. return 0;
  123. }
  124. static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  125. const AVFrame *pic, int *got_packet)
  126. {
  127. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  128. int w = avctx->width, h = avctx->height;
  129. int ret, rgb_size, i;
  130. bool keyframe;
  131. uint8_t *y, *u, *v;
  132. uint8_t *dst;
  133. /* Alloc buffer */
  134. if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
  135. return ret;
  136. dst = pkt->data;
  137. /* Move input if needed data into Ut Video friendly buffer */
  138. switch (avctx->pix_fmt) {
  139. case AV_PIX_FMT_YUV420P:
  140. y = utv->buffer;
  141. u = y + w * h;
  142. v = u + w * h / 4;
  143. for (i = 0; i < h; i++) {
  144. memcpy(y, pic->data[0] + i * pic->linesize[0], w);
  145. y += w;
  146. }
  147. for (i = 0; i < h / 2; i++) {
  148. memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
  149. memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
  150. u += w >> 1;
  151. v += w >> 1;
  152. }
  153. break;
  154. case AV_PIX_FMT_YUYV422:
  155. for (i = 0; i < h; i++)
  156. memcpy(utv->buffer + i * (w << 1),
  157. pic->data[0] + i * pic->linesize[0], w << 1);
  158. break;
  159. case AV_PIX_FMT_BGR24:
  160. case AV_PIX_FMT_RGB32:
  161. /* Ut Video takes bottom-up BGR */
  162. rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
  163. for (i = 0; i < h; i++)
  164. memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
  165. pic->data[0] + i * pic->linesize[0],
  166. w * rgb_size);
  167. break;
  168. default:
  169. return AVERROR(EINVAL);
  170. }
  171. /* Encode frame */
  172. pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
  173. if (!pkt->size) {
  174. av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
  175. return AVERROR_INVALIDDATA;
  176. }
  177. /*
  178. * Ut Video is intra-only and every frame is a keyframe,
  179. * and the API always returns true. In case something
  180. * durastic changes in the future, such as inter support,
  181. * assert that this is true.
  182. */
  183. av_assert2(keyframe == true);
  184. pkt->flags |= AV_PKT_FLAG_KEY;
  185. *got_packet = 1;
  186. return 0;
  187. }
  188. static av_cold int utvideo_encode_close(AVCodecContext *avctx)
  189. {
  190. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  191. av_freep(&avctx->extradata);
  192. av_freep(&utv->buffer);
  193. utv->codec->EncodeEnd();
  194. CCodec::DeleteInstance(utv->codec);
  195. return 0;
  196. }
  197. #define OFFSET(x) offsetof(UtVideoContext, x)
  198. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  199. static const AVOption options[] = {
  200. { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, 0, 0, 2, VE, "pred" },
  201. { "left", NULL, 0, AV_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, VE, "pred" },
  202. { "median", NULL, 0, AV_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, VE, "pred" },
  203. { NULL },
  204. };
  205. static const AVClass utvideo_class = {
  206. "libutvideo",
  207. av_default_item_name,
  208. options,
  209. LIBAVUTIL_VERSION_INT,
  210. 0,
  211. 0,
  212. NULL,
  213. NULL,
  214. AV_CLASS_CATEGORY_NA,
  215. NULL,
  216. NULL,
  217. };
  218. AVCodec ff_libutvideo_encoder = {
  219. "libutvideo",
  220. NULL_IF_CONFIG_SMALL("Ut Video"),
  221. AVMEDIA_TYPE_VIDEO,
  222. AV_CODEC_ID_UTVIDEO,
  223. AV_CODEC_CAP_AUTO_THREADS | (int)AV_CODEC_CAP_LOSSLESS | AV_CODEC_CAP_INTRA_ONLY,
  224. NULL, /* supported_framerates */
  225. (const enum AVPixelFormat[]) {
  226. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
  227. AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
  228. },
  229. NULL, /* supported_samplerates */
  230. NULL, /* sample_fmts */
  231. NULL, /* channel_layouts */
  232. 0, /* max_lowres */
  233. &utvideo_class, /* priv_class */
  234. NULL, /* profiles */
  235. sizeof(UtVideoContext),
  236. NULL, /* next */
  237. NULL, /* init_thread_copy */
  238. NULL, /* update_thread_context */
  239. NULL, /* defaults */
  240. NULL, /* init_static_data */
  241. utvideo_encode_init,
  242. NULL, /* encode */
  243. utvideo_encode_frame,
  244. NULL, /* decode */
  245. utvideo_encode_close,
  246. NULL, /* flush */
  247. };