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.

252 lines
7.5KB

  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/avassert.h"
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. }
  31. #include "libutvideo.h"
  32. #include "put_bits.h"
  33. static av_cold int utvideo_encode_init(AVCodecContext *avctx)
  34. {
  35. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  36. UtVideoExtra *info;
  37. uint32_t flags, in_format;
  38. int ret;
  39. switch (avctx->pix_fmt) {
  40. case AV_PIX_FMT_YUV420P:
  41. in_format = UTVF_YV12;
  42. avctx->bits_per_coded_sample = 12;
  43. if (avctx->colorspace == AVCOL_SPC_BT709)
  44. avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
  45. else
  46. avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
  47. break;
  48. case AV_PIX_FMT_YUYV422:
  49. in_format = UTVF_YUYV;
  50. avctx->bits_per_coded_sample = 16;
  51. if (avctx->colorspace == AVCOL_SPC_BT709)
  52. avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
  53. else
  54. avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
  55. break;
  56. case AV_PIX_FMT_BGR24:
  57. in_format = UTVF_NFCC_BGR_BU;
  58. avctx->bits_per_coded_sample = 24;
  59. avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
  60. break;
  61. case AV_PIX_FMT_RGB32:
  62. in_format = UTVF_NFCC_BGRA_BU;
  63. avctx->bits_per_coded_sample = 32;
  64. avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
  65. break;
  66. default:
  67. return AVERROR(EINVAL);
  68. }
  69. /* Check before we alloc anything */
  70. if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
  71. av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
  72. return AVERROR(EINVAL);
  73. }
  74. flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
  75. avctx->priv_data = utv;
  76. avctx->coded_frame = av_frame_alloc();
  77. /* Alloc extradata buffer */
  78. info = (UtVideoExtra *)av_malloc(sizeof(*info));
  79. if (!info) {
  80. av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
  81. return AVERROR(ENOMEM);
  82. }
  83. /*
  84. * We use this buffer to hold the data that Ut Video returns,
  85. * since we cannot decode planes separately with it.
  86. */
  87. ret = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  88. if (ret < 0)
  89. return ret;
  90. utv->buf_size = ret;
  91. utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
  92. if (utv->buffer == NULL) {
  93. av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
  94. av_free(info);
  95. return AVERROR(ENOMEM);
  96. }
  97. /*
  98. * Create a Ut Video instance. Since the function wants
  99. * an "interface name" string, pass it the name of the lib.
  100. */
  101. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  102. /* Initialize encoder */
  103. utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
  104. CBGROSSWIDTH_WINDOWS);
  105. /* Get extradata from encoder */
  106. avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
  107. utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
  108. avctx->width, avctx->height,
  109. CBGROSSWIDTH_WINDOWS);
  110. avctx->extradata = (uint8_t *)info;
  111. /* Set flags */
  112. utv->codec->SetState(&flags, sizeof(flags));
  113. return 0;
  114. }
  115. static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  116. const AVFrame *pic, int *got_packet)
  117. {
  118. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  119. int w = avctx->width, h = avctx->height;
  120. int ret, rgb_size, i;
  121. bool keyframe;
  122. uint8_t *y, *u, *v;
  123. uint8_t *dst;
  124. /* Alloc buffer */
  125. if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
  126. return ret;
  127. dst = pkt->data;
  128. /* Move input if needed data into Ut Video friendly buffer */
  129. switch (avctx->pix_fmt) {
  130. case AV_PIX_FMT_YUV420P:
  131. y = utv->buffer;
  132. u = y + w * h;
  133. v = u + w * h / 4;
  134. for (i = 0; i < h; i++) {
  135. memcpy(y, pic->data[0] + i * pic->linesize[0], w);
  136. y += w;
  137. }
  138. for (i = 0; i < h / 2; i++) {
  139. memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
  140. memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
  141. u += w >> 1;
  142. v += w >> 1;
  143. }
  144. break;
  145. case AV_PIX_FMT_YUYV422:
  146. for (i = 0; i < h; i++)
  147. memcpy(utv->buffer + i * (w << 1),
  148. pic->data[0] + i * pic->linesize[0], w << 1);
  149. break;
  150. case AV_PIX_FMT_BGR24:
  151. case AV_PIX_FMT_RGB32:
  152. /* Ut Video takes bottom-up BGR */
  153. rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
  154. for (i = 0; i < h; i++)
  155. memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
  156. pic->data[0] + i * pic->linesize[0],
  157. w * rgb_size);
  158. break;
  159. default:
  160. return AVERROR(EINVAL);
  161. }
  162. /* Encode frame */
  163. pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
  164. if (!pkt->size) {
  165. av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
  166. return AVERROR_INVALIDDATA;
  167. }
  168. /*
  169. * Ut Video is intra-only and every frame is a keyframe,
  170. * and the API always returns true. In case something
  171. * durastic changes in the future, such as inter support,
  172. * assert that this is true.
  173. */
  174. av_assert2(keyframe == true);
  175. avctx->coded_frame->key_frame = 1;
  176. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  177. pkt->flags |= AV_PKT_FLAG_KEY;
  178. *got_packet = 1;
  179. return 0;
  180. }
  181. static av_cold int utvideo_encode_close(AVCodecContext *avctx)
  182. {
  183. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  184. av_freep(&avctx->coded_frame);
  185. av_freep(&avctx->extradata);
  186. av_freep(&utv->buffer);
  187. utv->codec->EncodeEnd();
  188. CCodec::DeleteInstance(utv->codec);
  189. return 0;
  190. }
  191. AVCodec ff_libutvideo_encoder = {
  192. "libutvideo",
  193. NULL_IF_CONFIG_SMALL("Ut Video"),
  194. AVMEDIA_TYPE_VIDEO,
  195. AV_CODEC_ID_UTVIDEO,
  196. CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
  197. NULL, /* supported_framerates */
  198. (const enum AVPixelFormat[]) {
  199. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
  200. AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
  201. },
  202. NULL, /* supported_samplerates */
  203. NULL, /* sample_fmts */
  204. NULL, /* channel_layouts */
  205. 0, /* max_lowres */
  206. NULL, /* priv_class */
  207. NULL, /* profiles */
  208. sizeof(UtVideoContext),
  209. NULL, /* next */
  210. NULL, /* init_thread_copy */
  211. NULL, /* update_thread_context */
  212. NULL, /* defaults */
  213. NULL, /* init_static_data */
  214. utvideo_encode_init,
  215. NULL, /* encode */
  216. utvideo_encode_frame,
  217. NULL, /* decode */
  218. utvideo_encode_close,
  219. NULL, /* flush */
  220. };