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.4KB

  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. /* 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. ret = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  87. if (ret < 0) {
  88. av_free(info);
  89. return ret;
  90. }
  91. utv->buf_size = ret;
  92. utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
  93. if (utv->buffer == NULL) {
  94. av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
  95. av_free(info);
  96. return AVERROR(ENOMEM);
  97. }
  98. /*
  99. * Create a Ut Video instance. Since the function wants
  100. * an "interface name" string, pass it the name of the lib.
  101. */
  102. utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
  103. /* Initialize encoder */
  104. utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
  105. CBGROSSWIDTH_WINDOWS);
  106. /* Get extradata from encoder */
  107. avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
  108. utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
  109. avctx->width, avctx->height,
  110. CBGROSSWIDTH_WINDOWS);
  111. avctx->extradata = (uint8_t *)info;
  112. /* Set flags */
  113. utv->codec->SetState(&flags, sizeof(flags));
  114. return 0;
  115. }
  116. static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  117. const AVFrame *pic, int *got_packet)
  118. {
  119. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  120. int w = avctx->width, h = avctx->height;
  121. int ret, rgb_size, i;
  122. bool keyframe;
  123. uint8_t *y, *u, *v;
  124. uint8_t *dst;
  125. /* Alloc buffer */
  126. if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
  127. return ret;
  128. dst = pkt->data;
  129. /* Move input if needed data into Ut Video friendly buffer */
  130. switch (avctx->pix_fmt) {
  131. case AV_PIX_FMT_YUV420P:
  132. y = utv->buffer;
  133. u = y + w * h;
  134. v = u + w * h / 4;
  135. for (i = 0; i < h; i++) {
  136. memcpy(y, pic->data[0] + i * pic->linesize[0], w);
  137. y += w;
  138. }
  139. for (i = 0; i < h / 2; i++) {
  140. memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
  141. memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
  142. u += w >> 1;
  143. v += w >> 1;
  144. }
  145. break;
  146. case AV_PIX_FMT_YUYV422:
  147. for (i = 0; i < h; i++)
  148. memcpy(utv->buffer + i * (w << 1),
  149. pic->data[0] + i * pic->linesize[0], w << 1);
  150. break;
  151. case AV_PIX_FMT_BGR24:
  152. case AV_PIX_FMT_RGB32:
  153. /* Ut Video takes bottom-up BGR */
  154. rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
  155. for (i = 0; i < h; i++)
  156. memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
  157. pic->data[0] + i * pic->linesize[0],
  158. w * rgb_size);
  159. break;
  160. default:
  161. return AVERROR(EINVAL);
  162. }
  163. /* Encode frame */
  164. pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
  165. if (!pkt->size) {
  166. av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
  167. return AVERROR_INVALIDDATA;
  168. }
  169. /*
  170. * Ut Video is intra-only and every frame is a keyframe,
  171. * and the API always returns true. In case something
  172. * durastic changes in the future, such as inter support,
  173. * assert that this is true.
  174. */
  175. av_assert2(keyframe == true);
  176. avctx->coded_frame->key_frame = 1;
  177. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  178. pkt->flags |= AV_PKT_FLAG_KEY;
  179. *got_packet = 1;
  180. return 0;
  181. }
  182. static av_cold int utvideo_encode_close(AVCodecContext *avctx)
  183. {
  184. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  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. };