2 * Copyright (c) 2012 Derek Buitenhuis
4 * This file is part of FFmpeg.
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.
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.
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
24 * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA),
25 * 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
29 #include "libavutil/opt.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/imgutils.h"
36 #include "libutvideo.h"
39 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
41 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
43 uint32_t flags, in_format;
46 switch (avctx->pix_fmt) {
47 case AV_PIX_FMT_YUV420P:
48 in_format = UTVF_YV12;
49 avctx->bits_per_coded_sample = 12;
50 if (avctx->colorspace == AVCOL_SPC_BT709)
51 avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
53 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
55 case AV_PIX_FMT_YUYV422:
56 in_format = UTVF_YUYV;
57 avctx->bits_per_coded_sample = 16;
58 if (avctx->colorspace == AVCOL_SPC_BT709)
59 avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
61 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
63 case AV_PIX_FMT_BGR24:
64 in_format = UTVF_NFCC_BGR_BU;
65 avctx->bits_per_coded_sample = 24;
66 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
68 case AV_PIX_FMT_RGB32:
69 in_format = UTVF_NFCC_BGRA_BU;
70 avctx->bits_per_coded_sample = 32;
71 avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
74 return AVERROR(EINVAL);
77 #if FF_API_PRIVATE_OPT
78 FF_DISABLE_DEPRECATION_WARNINGS
79 if (avctx->prediction_method)
80 utv->pred = avctx->prediction_method;
81 FF_ENABLE_DEPRECATION_WARNINGS
84 /* Check before we alloc anything */
85 if (utv->pred != 0 && utv->pred != 2) {
86 av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
87 return AVERROR(EINVAL);
90 flags = ((utv->pred + 1) << 8) | (avctx->thread_count - 1);
92 avctx->priv_data = utv;
94 /* Alloc extradata buffer */
95 info = (UtVideoExtra *)av_malloc(sizeof(*info));
98 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
99 return AVERROR(ENOMEM);
103 * We use this buffer to hold the data that Ut Video returns,
104 * since we cannot decode planes separately with it.
106 ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
113 utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
115 if (utv->buffer == NULL) {
116 av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
118 return AVERROR(ENOMEM);
122 * Create a Ut Video instance. Since the function wants
123 * an "interface name" string, pass it the name of the lib.
125 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
127 /* Initialize encoder */
128 utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
129 CBGROSSWIDTH_WINDOWS);
131 /* Get extradata from encoder */
132 avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
133 utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
134 avctx->width, avctx->height,
135 CBGROSSWIDTH_WINDOWS);
136 avctx->extradata = (uint8_t *)info;
139 utv->codec->SetState(&flags, sizeof(flags));
144 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
145 const AVFrame *pic, int *got_packet)
147 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
148 int w = avctx->width, h = avctx->height;
149 int ret, rgb_size, i;
155 if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
160 /* Move input if needed data into Ut Video friendly buffer */
161 switch (avctx->pix_fmt) {
162 case AV_PIX_FMT_YUV420P:
166 for (i = 0; i < h; i++) {
167 memcpy(y, pic->data[0] + i * pic->linesize[0], w);
170 for (i = 0; i < h / 2; i++) {
171 memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
172 memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
177 case AV_PIX_FMT_YUYV422:
178 for (i = 0; i < h; i++)
179 memcpy(utv->buffer + i * (w << 1),
180 pic->data[0] + i * pic->linesize[0], w << 1);
182 case AV_PIX_FMT_BGR24:
183 case AV_PIX_FMT_RGB32:
184 /* Ut Video takes bottom-up BGR */
185 rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
186 for (i = 0; i < h; i++)
187 memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
188 pic->data[0] + i * pic->linesize[0],
192 return AVERROR(EINVAL);
196 pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
199 av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
200 return AVERROR_INVALIDDATA;
204 * Ut Video is intra-only and every frame is a keyframe,
205 * and the API always returns true. In case something
206 * durastic changes in the future, such as inter support,
207 * assert that this is true.
209 av_assert2(keyframe == true);
211 pkt->flags |= AV_PKT_FLAG_KEY;
216 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
218 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
220 av_freep(&avctx->extradata);
221 av_freep(&utv->buffer);
223 utv->codec->EncodeEnd();
224 CCodec::DeleteInstance(utv->codec);
229 #define OFFSET(x) offsetof(UtVideoContext, x)
230 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
231 static const AVOption options[] = {
232 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, 0, 0, 2, VE, "pred" },
233 { "left", NULL, 0, AV_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, VE, "pred" },
234 { "median", NULL, 0, AV_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, VE, "pred" },
238 static const AVClass utvideo_class = {
240 av_default_item_name,
242 LIBAVUTIL_VERSION_INT,
247 AV_CLASS_CATEGORY_NA,
252 AVCodec ff_libutvideo_encoder = {
254 NULL_IF_CONFIG_SMALL("Ut Video"),
257 AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_LOSSLESS | AV_CODEC_CAP_INTRA_ONLY,
258 NULL, /* supported_framerates */
259 (const enum AVPixelFormat[]) {
260 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
261 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
263 NULL, /* supported_samplerates */
264 NULL, /* sample_fmts */
265 NULL, /* channel_layouts */
267 &utvideo_class, /* priv_class */
269 sizeof(UtVideoContext),
271 NULL, /* init_thread_copy */
272 NULL, /* update_thread_context */
274 NULL, /* init_static_data */
277 utvideo_encode_frame,
279 utvideo_encode_close,