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) {
44 in_format = UTVF_YV12;
45 avctx->bits_per_coded_sample = 12;
46 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
49 in_format = UTVF_YUYV;
50 avctx->bits_per_coded_sample = 16;
51 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
54 in_format = UTVF_RGB24_WIN;
55 avctx->bits_per_coded_sample = 24;
56 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
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_packet(pkt, utv->buf_size)) < 0) {
134 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
140 /* Move input if needed data into Ut Video friendly buffer */
141 switch (avctx->pix_fmt) {
142 case PIX_FMT_YUV420P:
146 for (i = 0; i < h; i++) {
147 memcpy(y, pic->data[0] + i * pic->linesize[0], w);
150 for (i = 0; i < h / 2; i++) {
151 memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
152 memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
157 case PIX_FMT_YUYV422:
158 for (i = 0; i < h; i++)
159 memcpy(utv->buffer + i * (w << 1),
160 pic->data[0] + i * pic->linesize[0], w << 1);
164 /* Ut Video takes bottom-up BGR */
165 rgb_size = avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4;
166 for (i = 0; i < h; i++)
167 memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
168 pic->data[0] + i * pic->linesize[0],
172 return AVERROR(EINVAL);
176 pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
179 av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
180 return AVERROR_INVALIDDATA;
184 * Ut Video is intra-only and every frame is a keyframe,
185 * and the API always returns true. In case something
186 * durastic changes in the future, such as inter support,
187 * assert that this is true.
189 av_assert2(keyframe == true);
190 avctx->coded_frame->key_frame = 1;
191 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
193 pkt->flags |= AV_PKT_FLAG_KEY;
198 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
200 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
202 av_freep(&avctx->coded_frame);
203 av_freep(&avctx->extradata);
204 av_freep(&utv->buffer);
206 utv->codec->EncodeEnd();
207 CCodec::DeleteInstance(utv->codec);
212 AVCodec ff_libutvideo_encoder = {
214 NULL_IF_CONFIG_SMALL("Ut Video"),
217 CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
218 NULL, /* supported_framerates */
219 (const enum PixelFormat[]) {
220 PIX_FMT_YUV420P, PIX_FMT_YUYV422, PIX_FMT_BGR24,
221 PIX_FMT_RGB32, PIX_FMT_NONE
223 NULL, /* supported_samplerates */
224 NULL, /* sample_fmts */
225 NULL, /* channel_layouts */
227 NULL, /* priv_class */
229 sizeof(UtVideoContext),
231 NULL, /* init_thread_copy */
232 NULL, /* update_thread_context */
234 NULL, /* init_static_data */
237 utvideo_encode_frame,
239 utvideo_encode_close,