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.

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