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)
28 #include "libavutil/avassert.h"
33 #include "libutvideo.h"
36 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
38 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
40 uint32_t flags, in_format;
42 switch (avctx->pix_fmt) {
43 case AV_PIX_FMT_YUV420P:
44 in_format = UTVF_YV12;
45 avctx->bits_per_coded_sample = 12;
46 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
48 case AV_PIX_FMT_YUYV422:
49 in_format = UTVF_YUYV;
50 avctx->bits_per_coded_sample = 16;
51 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
53 case AV_PIX_FMT_BGR24:
54 in_format = UTVF_RGB24_WIN;
55 avctx->bits_per_coded_sample = 24;
56 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
58 case AV_PIX_FMT_RGB32:
59 in_format = UTVF_RGB32_WIN;
60 avctx->bits_per_coded_sample = 32;
61 avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
64 return AVERROR(EINVAL);
67 /* Check before we alloc anything */
68 if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
69 av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
70 return AVERROR(EINVAL);
73 flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
75 avctx->priv_data = utv;
76 avctx->coded_frame = avcodec_alloc_frame();
78 /* Alloc extradata buffer */
79 info = (UtVideoExtra *)av_malloc(sizeof(*info));
82 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
83 return AVERROR(ENOMEM);
87 * We use this buffer to hold the data that Ut Video returns,
88 * since we cannot decode planes separately with it.
90 utv->buf_size = avpicture_get_size(avctx->pix_fmt,
91 avctx->width, avctx->height);
92 utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
94 if (utv->buffer == NULL) {
95 av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
96 return AVERROR(ENOMEM);
100 * Create a Ut Video instance. Since the function wants
101 * an "interface name" string, pass it the name of the lib.
103 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
105 /* Initialize encoder */
106 utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
107 CBGROSSWIDTH_WINDOWS);
109 /* Get extradata from encoder */
110 avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
111 utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
112 avctx->width, avctx->height,
113 CBGROSSWIDTH_WINDOWS);
114 avctx->extradata = (uint8_t *)info;
117 utv->codec->SetState(&flags, sizeof(flags));
122 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
123 const AVFrame *pic, int *got_packet)
125 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
126 int w = avctx->width, h = avctx->height;
127 int ret, rgb_size, i;
133 if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
138 /* Move input if needed data into Ut Video friendly buffer */
139 switch (avctx->pix_fmt) {
140 case AV_PIX_FMT_YUV420P:
144 for (i = 0; i < h; i++) {
145 memcpy(y, pic->data[0] + i * pic->linesize[0], w);
148 for (i = 0; i < h / 2; i++) {
149 memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
150 memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
155 case AV_PIX_FMT_YUYV422:
156 for (i = 0; i < h; i++)
157 memcpy(utv->buffer + i * (w << 1),
158 pic->data[0] + i * pic->linesize[0], w << 1);
160 case AV_PIX_FMT_BGR24:
161 case AV_PIX_FMT_RGB32:
162 /* Ut Video takes bottom-up BGR */
163 rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
164 for (i = 0; i < h; i++)
165 memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
166 pic->data[0] + i * pic->linesize[0],
170 return AVERROR(EINVAL);
174 pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
177 av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
178 return AVERROR_INVALIDDATA;
182 * Ut Video is intra-only and every frame is a keyframe,
183 * and the API always returns true. In case something
184 * durastic changes in the future, such as inter support,
185 * assert that this is true.
187 av_assert2(keyframe == true);
188 avctx->coded_frame->key_frame = 1;
189 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
191 pkt->flags |= AV_PKT_FLAG_KEY;
196 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
198 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
200 av_freep(&avctx->coded_frame);
201 av_freep(&avctx->extradata);
202 av_freep(&utv->buffer);
204 utv->codec->EncodeEnd();
205 CCodec::DeleteInstance(utv->codec);
210 AVCodec ff_libutvideo_encoder = {
212 NULL_IF_CONFIG_SMALL("Ut Video"),
215 CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
216 NULL, /* supported_framerates */
217 (const enum AVPixelFormat[]) {
218 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
219 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
221 NULL, /* supported_samplerates */
222 NULL, /* sample_fmts */
223 NULL, /* channel_layouts */
225 NULL, /* priv_class */
227 sizeof(UtVideoContext),
229 NULL, /* init_thread_copy */
230 NULL, /* update_thread_context */
232 NULL, /* init_static_data */
235 utvideo_encode_frame,
237 utvideo_encode_close,