123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- extern "C" {
- #include "libavutil/avassert.h"
- #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;
- switch (avctx->pix_fmt) {
- case AV_PIX_FMT_YUV420P:
- in_format = UTVF_YV12;
- avctx->bits_per_coded_sample = 12;
- avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
- break;
- case AV_PIX_FMT_YUYV422:
- in_format = UTVF_YUYV;
- avctx->bits_per_coded_sample = 16;
- avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
- break;
- case AV_PIX_FMT_BGR24:
- in_format = UTVF_NFCC_BGR_BU;
- avctx->bits_per_coded_sample = 24;
- avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
- break;
- case AV_PIX_FMT_RGB32:
- in_format = UTVF_NFCC_BGRA_BU;
- avctx->bits_per_coded_sample = 32;
- avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
- break;
- default:
- return AVERROR(EINVAL);
- }
-
- if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
- av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
- return AVERROR(EINVAL);
- }
- flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
- avctx->priv_data = utv;
- avctx->coded_frame = av_frame_alloc();
-
- info = (UtVideoExtra *)av_malloc(sizeof(*info));
- if (info == NULL) {
- av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
- return AVERROR(ENOMEM);
- }
-
- utv->buf_size = avpicture_get_size(avctx->pix_fmt,
- avctx->width, avctx->height);
- 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");
- return AVERROR(ENOMEM);
- }
-
- utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
-
- utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
- CBGROSSWIDTH_WINDOWS);
-
- 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;
-
- 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;
-
- if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
- return ret;
- dst = pkt->data;
-
- switch (avctx->pix_fmt) {
- case AV_PIX_FMT_YUV420P:
- 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;
- case AV_PIX_FMT_YUYV422:
- for (i = 0; i < h; i++)
- memcpy(utv->buffer + i * (w << 1),
- pic->data[0] + i * pic->linesize[0], w << 1);
- break;
- case AV_PIX_FMT_BGR24:
- case AV_PIX_FMT_RGB32:
-
- rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
- 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);
- }
-
- pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
- if (!pkt->size) {
- av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
- return AVERROR_INVALIDDATA;
- }
-
- av_assert2(keyframe == true);
- avctx->coded_frame->key_frame = 1;
- avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
- 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->coded_frame);
- av_freep(&avctx->extradata);
- av_freep(&utv->buffer);
- utv->codec->EncodeEnd();
- CCodec::DeleteInstance(utv->codec);
- return 0;
- }
- AVCodec ff_libutvideo_encoder = {
- "libutvideo",
- NULL_IF_CONFIG_SMALL("Ut Video"),
- AVMEDIA_TYPE_VIDEO,
- AV_CODEC_ID_UTVIDEO,
- CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
- NULL,
- (const enum AVPixelFormat[]) {
- AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
- AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
- },
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- sizeof(UtVideoContext),
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- utvideo_encode_init,
- NULL,
- utvideo_encode_frame,
- NULL,
- utvideo_encode_close,
- NULL,
- };
|