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.

240 lines
7.1KB

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