libutvideoenc.cpp 7.2 KB

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