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.

242 lines
7.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. */
  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_packet(pkt, utv->buf_size)) < 0) {
  115. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  116. return ret;
  117. }
  118. dst = pkt->data;
  119. /* Move input if needed data into Ut Video friendly buffer */
  120. switch (avctx->pix_fmt) {
  121. case PIX_FMT_YUV420P:
  122. y = utv->buffer;
  123. u = y + w * h;
  124. v = u + w * h / 4;
  125. for (i = 0; i < h; i++) {
  126. memcpy(y, pic->data[0] + i * pic->linesize[0], w);
  127. y += w;
  128. }
  129. for (i = 0; i < h / 2; i++) {
  130. memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
  131. memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
  132. u += w >> 1;
  133. v += w >> 1;
  134. }
  135. break;
  136. case PIX_FMT_YUYV422:
  137. for (i = 0; i < h; i++)
  138. memcpy(utv->buffer + i * (w << 1),
  139. pic->data[0] + i * pic->linesize[0], w << 1);
  140. break;
  141. case PIX_FMT_BGR24:
  142. case PIX_FMT_RGB32:
  143. /* Ut Video takes bottom-up BGR */
  144. rgb_size = avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4;
  145. for (i = 0; i < h; i++)
  146. memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
  147. pic->data[0] + i * pic->linesize[0],
  148. w * rgb_size);
  149. break;
  150. default:
  151. return AVERROR(EINVAL);
  152. }
  153. /* Encode frame */
  154. pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
  155. if (!pkt->size) {
  156. av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
  157. return AVERROR_INVALIDDATA;
  158. }
  159. /*
  160. * Ut Video is intra-only and every frame is a keyframe,
  161. * and the API always returns true. In case something
  162. * durastic changes in the future, such as inter support,
  163. * assert that this is true.
  164. */
  165. av_assert2(keyframe == true);
  166. avctx->coded_frame->key_frame = 1;
  167. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  168. pkt->flags |= AV_PKT_FLAG_KEY;
  169. *got_packet = 1;
  170. return 0;
  171. }
  172. static av_cold int utvideo_encode_close(AVCodecContext *avctx)
  173. {
  174. UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
  175. av_freep(&avctx->coded_frame);
  176. av_freep(&avctx->extradata);
  177. av_freep(&utv->buffer);
  178. utv->codec->EncodeEnd();
  179. CCodec::DeleteInstance(utv->codec);
  180. return 0;
  181. }
  182. AVCodec ff_libutvideo_encoder = {
  183. "libutvideo",
  184. NULL_IF_CONFIG_SMALL("Ut Video"),
  185. AVMEDIA_TYPE_VIDEO,
  186. CODEC_ID_UTVIDEO,
  187. CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
  188. NULL, /* supported_framerates */
  189. (const enum PixelFormat[]) {
  190. PIX_FMT_YUV420P, PIX_FMT_YUYV422, PIX_FMT_BGR24,
  191. PIX_FMT_RGB32, PIX_FMT_NONE
  192. },
  193. NULL, /* supported_samplerates */
  194. NULL, /* sample_fmts */
  195. NULL, /* channel_layouts */
  196. 0, /* max_lowres */
  197. NULL, /* priv_class */
  198. NULL, /* profiles */
  199. sizeof(UtVideoContext),
  200. NULL, /* next */
  201. NULL, /* init_thread_copy */
  202. NULL, /* update_thread_context */
  203. NULL, /* defaults */
  204. NULL, /* init_static_data */
  205. utvideo_encode_init,
  206. NULL, /* encode */
  207. utvideo_encode_frame,
  208. NULL, /* decode */
  209. utvideo_encode_close,
  210. NULL, /* flush */
  211. };