2012-03-05 23:29:23 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Derek Buitenhuis
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation;
|
|
|
|
* version 2 of the License.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Known FOURCCs:
|
2013-06-09 15:08:03 +03:00
|
|
|
* 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA),
|
|
|
|
* 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
|
2012-03-05 23:29:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
extern "C" {
|
2016-02-03 21:27:24 +02:00
|
|
|
#include "libavutil/opt.h"
|
2012-03-05 23:29:23 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2016-01-17 07:41:54 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-03-05 23:29:23 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "internal.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "libutvideo.h"
|
|
|
|
#include "put_bits.h"
|
|
|
|
|
|
|
|
static av_cold int utvideo_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
|
|
|
|
UtVideoExtra *info;
|
|
|
|
uint32_t flags, in_format;
|
2014-12-11 14:55:26 +02:00
|
|
|
int ret;
|
2012-03-05 23:29:23 +03:00
|
|
|
|
|
|
|
switch (avctx->pix_fmt) {
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_YUV420P:
|
2012-03-05 23:29:23 +03:00
|
|
|
in_format = UTVF_YV12;
|
|
|
|
avctx->bits_per_coded_sample = 12;
|
2014-02-15 09:58:14 +03:00
|
|
|
if (avctx->colorspace == AVCOL_SPC_BT709)
|
|
|
|
avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
|
|
|
|
else
|
|
|
|
avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
|
2012-03-05 23:29:23 +03:00
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_YUYV422:
|
2012-03-05 23:29:23 +03:00
|
|
|
in_format = UTVF_YUYV;
|
|
|
|
avctx->bits_per_coded_sample = 16;
|
2014-02-15 09:58:14 +03:00
|
|
|
if (avctx->colorspace == AVCOL_SPC_BT709)
|
|
|
|
avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
|
|
|
|
else
|
|
|
|
avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
|
2012-03-05 23:29:23 +03:00
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_BGR24:
|
2013-03-09 01:42:24 +03:00
|
|
|
in_format = UTVF_NFCC_BGR_BU;
|
2012-03-05 23:29:23 +03:00
|
|
|
avctx->bits_per_coded_sample = 24;
|
|
|
|
avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_RGB32:
|
2013-03-09 01:42:24 +03:00
|
|
|
in_format = UTVF_NFCC_BGRA_BU;
|
2012-03-05 23:29:23 +03:00
|
|
|
avctx->bits_per_coded_sample = 32;
|
|
|
|
avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:49:19 +02:00
|
|
|
#if FF_API_PRIVATE_OPT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
if (avctx->prediction_method)
|
|
|
|
utv->pred = avctx->prediction_method;
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
|
|
|
|
2012-03-05 23:29:23 +03:00
|
|
|
/* Check before we alloc anything */
|
2016-02-03 18:49:19 +02:00
|
|
|
if (utv->pred != 0 && utv->pred != 2) {
|
2012-03-05 23:29:23 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:27:24 +02:00
|
|
|
flags = ((utv->pred + 1) << 8) | (avctx->thread_count - 1);
|
2012-03-05 23:29:23 +03:00
|
|
|
|
|
|
|
avctx->priv_data = utv;
|
|
|
|
|
|
|
|
/* Alloc extradata buffer */
|
|
|
|
info = (UtVideoExtra *)av_malloc(sizeof(*info));
|
|
|
|
|
2014-08-15 21:33:21 +03:00
|
|
|
if (!info) {
|
2012-03-05 23:29:23 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use this buffer to hold the data that Ut Video returns,
|
|
|
|
* since we cannot decode planes separately with it.
|
|
|
|
*/
|
2016-01-17 07:41:54 +02:00
|
|
|
ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
|
2015-05-15 23:53:24 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_free(info);
|
2014-12-11 14:55:26 +02:00
|
|
|
return ret;
|
2015-05-15 23:53:24 +02:00
|
|
|
}
|
2014-12-11 14:55:26 +02:00
|
|
|
utv->buf_size = ret;
|
|
|
|
|
2012-03-05 23:29:23 +03:00
|
|
|
utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
|
|
|
|
|
|
|
|
if (utv->buffer == NULL) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
|
2014-12-11 16:44:05 +02:00
|
|
|
av_free(info);
|
2012-03-05 23:29:23 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a Ut Video instance. Since the function wants
|
|
|
|
* an "interface name" string, pass it the name of the lib.
|
|
|
|
*/
|
|
|
|
utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
|
|
|
|
|
|
|
|
/* Initialize encoder */
|
|
|
|
utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
|
|
|
|
CBGROSSWIDTH_WINDOWS);
|
|
|
|
|
|
|
|
/* Get extradata from encoder */
|
|
|
|
avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
|
|
|
|
utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
|
|
|
|
avctx->width, avctx->height,
|
|
|
|
CBGROSSWIDTH_WINDOWS);
|
|
|
|
avctx->extradata = (uint8_t *)info;
|
|
|
|
|
|
|
|
/* Set flags */
|
|
|
|
utv->codec->SetState(&flags, sizeof(flags));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *pic, int *got_packet)
|
|
|
|
{
|
|
|
|
UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
|
|
|
|
int w = avctx->width, h = avctx->height;
|
|
|
|
int ret, rgb_size, i;
|
|
|
|
bool keyframe;
|
|
|
|
uint8_t *y, *u, *v;
|
|
|
|
uint8_t *dst;
|
|
|
|
|
|
|
|
/* Alloc buffer */
|
2015-07-27 19:36:16 +02:00
|
|
|
if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
|
2012-03-05 23:29:23 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
dst = pkt->data;
|
|
|
|
|
|
|
|
/* Move input if needed data into Ut Video friendly buffer */
|
|
|
|
switch (avctx->pix_fmt) {
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_YUV420P:
|
2012-03-05 23:29:23 +03:00
|
|
|
y = utv->buffer;
|
|
|
|
u = y + w * h;
|
|
|
|
v = u + w * h / 4;
|
|
|
|
for (i = 0; i < h; i++) {
|
|
|
|
memcpy(y, pic->data[0] + i * pic->linesize[0], w);
|
|
|
|
y += w;
|
|
|
|
}
|
|
|
|
for (i = 0; i < h / 2; i++) {
|
|
|
|
memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
|
|
|
|
memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
|
|
|
|
u += w >> 1;
|
|
|
|
v += w >> 1;
|
|
|
|
}
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_YUYV422:
|
2012-03-05 23:29:23 +03:00
|
|
|
for (i = 0; i < h; i++)
|
|
|
|
memcpy(utv->buffer + i * (w << 1),
|
|
|
|
pic->data[0] + i * pic->linesize[0], w << 1);
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_BGR24:
|
|
|
|
case AV_PIX_FMT_RGB32:
|
2012-03-05 23:29:23 +03:00
|
|
|
/* Ut Video takes bottom-up BGR */
|
2012-10-08 21:54:00 +03:00
|
|
|
rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
|
2012-03-05 23:29:23 +03:00
|
|
|
for (i = 0; i < h; i++)
|
|
|
|
memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
|
|
|
|
pic->data[0] + i * pic->linesize[0],
|
|
|
|
w * rgb_size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode frame */
|
|
|
|
pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
|
|
|
|
|
|
|
|
if (!pkt->size) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ut Video is intra-only and every frame is a keyframe,
|
|
|
|
* and the API always returns true. In case something
|
|
|
|
* durastic changes in the future, such as inter support,
|
|
|
|
* assert that this is true.
|
|
|
|
*/
|
|
|
|
av_assert2(keyframe == true);
|
|
|
|
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
*got_packet = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int utvideo_encode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
|
|
|
|
|
|
|
|
av_freep(&avctx->extradata);
|
|
|
|
av_freep(&utv->buffer);
|
|
|
|
|
|
|
|
utv->codec->EncodeEnd();
|
|
|
|
CCodec::DeleteInstance(utv->codec);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:49:19 +02:00
|
|
|
#define OFFSET(x) offsetof(UtVideoContext, x)
|
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, 0, 0, 2, VE, "pred" },
|
|
|
|
{ "left", NULL, 0, AV_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, VE, "pred" },
|
|
|
|
{ "median", NULL, 0, AV_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, VE, "pred" },
|
2016-02-04 03:17:57 +02:00
|
|
|
{ NULL },
|
2016-02-03 18:49:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass utvideo_class = {
|
|
|
|
"libutvideo",
|
|
|
|
av_default_item_name,
|
2016-02-03 21:27:24 +02:00
|
|
|
options,
|
|
|
|
LIBAVUTIL_VERSION_INT,
|
2016-02-03 18:49:19 +02:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2016-02-03 21:27:24 +02:00
|
|
|
AV_CLASS_CATEGORY_NA,
|
2016-02-03 18:49:19 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2012-03-05 23:29:23 +03:00
|
|
|
AVCodec ff_libutvideo_encoder = {
|
|
|
|
"libutvideo",
|
|
|
|
NULL_IF_CONFIG_SMALL("Ut Video"),
|
|
|
|
AVMEDIA_TYPE_VIDEO,
|
2012-08-07 23:45:46 +03:00
|
|
|
AV_CODEC_ID_UTVIDEO,
|
2015-08-02 12:48:23 +02:00
|
|
|
AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_LOSSLESS | AV_CODEC_CAP_INTRA_ONLY,
|
2012-03-05 23:29:23 +03:00
|
|
|
NULL, /* supported_framerates */
|
2012-10-08 21:54:00 +03:00
|
|
|
(const enum AVPixelFormat[]) {
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
|
|
|
|
AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
|
2012-03-05 23:29:23 +03:00
|
|
|
},
|
|
|
|
NULL, /* supported_samplerates */
|
|
|
|
NULL, /* sample_fmts */
|
|
|
|
NULL, /* channel_layouts */
|
|
|
|
0, /* max_lowres */
|
2016-02-03 18:49:19 +02:00
|
|
|
&utvideo_class, /* priv_class */
|
2012-03-05 23:29:23 +03:00
|
|
|
NULL, /* profiles */
|
|
|
|
sizeof(UtVideoContext),
|
|
|
|
NULL, /* next */
|
|
|
|
NULL, /* init_thread_copy */
|
|
|
|
NULL, /* update_thread_context */
|
|
|
|
NULL, /* defaults */
|
|
|
|
NULL, /* init_static_data */
|
|
|
|
utvideo_encode_init,
|
|
|
|
NULL, /* encode */
|
|
|
|
utvideo_encode_frame,
|
|
|
|
NULL, /* decode */
|
|
|
|
utvideo_encode_close,
|
|
|
|
NULL, /* flush */
|
|
|
|
};
|