2011-10-17 03:22:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 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)
|
2011-10-17 03:22:59 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "avcodec.h"
|
|
|
|
}
|
|
|
|
|
2012-03-04 03:53:59 +03:00
|
|
|
#include "libutvideo.h"
|
2011-10-17 03:22:59 +03:00
|
|
|
#include "get_bits.h"
|
|
|
|
|
|
|
|
static av_cold int utvideo_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
|
|
|
|
UtVideoExtra info;
|
2011-11-07 07:51:47 +03:00
|
|
|
int format;
|
2011-11-07 20:02:30 +03:00
|
|
|
int begin_ret;
|
2011-10-17 03:22:59 +03:00
|
|
|
|
2011-11-15 02:06:02 +03:00
|
|
|
if (avctx->extradata_size != 4*4) {
|
2011-10-17 03:22:59 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read extradata */
|
|
|
|
info.version = AV_RL32(avctx->extradata);
|
|
|
|
info.original_format = AV_RL32(avctx->extradata + 4);
|
2011-11-07 20:20:22 +03:00
|
|
|
info.frameinfo_size = AV_RL32(avctx->extradata + 8);
|
2011-10-17 03:22:59 +03:00
|
|
|
info.flags = AV_RL32(avctx->extradata + 12);
|
|
|
|
|
2011-11-07 07:51:47 +03:00
|
|
|
/* Pick format based on FOURCC */
|
2011-11-15 02:06:02 +03:00
|
|
|
switch (avctx->codec_tag) {
|
2013-06-09 15:08:03 +03:00
|
|
|
#ifdef UTV_BT709
|
|
|
|
case MKTAG('U', 'L', 'H', '0'):
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
|
|
avctx->colorspace = AVCOL_SPC_BT709;
|
|
|
|
format = UTVF_YV12;
|
|
|
|
break;
|
|
|
|
case MKTAG('U', 'L', 'H', '2'):
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUYV422;
|
|
|
|
avctx->colorspace = AVCOL_SPC_BT709;
|
|
|
|
format = UTVF_YUY2;
|
|
|
|
break;
|
|
|
|
#endif
|
2011-11-15 02:06:02 +03:00
|
|
|
case MKTAG('U', 'L', 'Y', '0'):
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
2011-11-15 02:06:02 +03:00
|
|
|
format = UTVF_YV12;
|
|
|
|
break;
|
|
|
|
case MKTAG('U', 'L', 'Y', '2'):
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUYV422;
|
2011-11-15 02:06:02 +03:00
|
|
|
format = UTVF_YUY2;
|
|
|
|
break;
|
|
|
|
case MKTAG('U', 'L', 'R', 'G'):
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
2013-03-09 01:42:24 +03:00
|
|
|
format = UTVF_NFCC_BGR_BU;
|
2011-11-15 02:06:02 +03:00
|
|
|
break;
|
|
|
|
case MKTAG('U', 'L', 'R', 'A'):
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB32;
|
2013-03-09 01:42:24 +03:00
|
|
|
format = UTVF_NFCC_BGRA_BU;
|
2011-11-15 02:06:02 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
|
|
|
|
return -1;
|
2011-10-17 03:22:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only allocate the buffer once */
|
2011-12-02 01:20:19 +03:00
|
|
|
utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
|
2012-03-04 03:53:59 +03:00
|
|
|
utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
|
2011-10-17 03:22:59 +03:00
|
|
|
|
2012-03-04 03:53:59 +03:00
|
|
|
if (utv->buffer == NULL) {
|
2011-10-17 03:22:59 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-07 20:20:29 +03:00
|
|
|
/* Allocate the output frame */
|
2013-11-17 01:48:16 +03:00
|
|
|
avctx->coded_frame = av_frame_alloc();
|
2011-10-17 03:22:59 +03:00
|
|
|
|
|
|
|
/* Ut Video only supports 8-bit */
|
|
|
|
avctx->bits_per_raw_sample = 8;
|
|
|
|
|
|
|
|
/* Is it interlaced? */
|
|
|
|
avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
|
|
|
|
|
|
|
|
/* Apparently Ut Video doesn't store this info... */
|
|
|
|
avctx->coded_frame->top_field_first = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 Decoding */
|
2011-11-07 20:02:30 +03:00
|
|
|
begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
|
2011-11-07 07:51:47 +03:00
|
|
|
CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
|
2011-10-17 03:22:59 +03:00
|
|
|
|
2011-11-07 20:02:30 +03:00
|
|
|
/* Check to see if the decoder initlized properly */
|
2011-11-15 02:06:02 +03:00
|
|
|
if (begin_ret != 0) {
|
2011-11-07 20:02:30 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Could not initialize decoder: %d\n", begin_ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-17 03:22:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
|
2012-12-05 21:24:43 +03:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2011-10-17 03:22:59 +03:00
|
|
|
{
|
|
|
|
UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
|
|
|
|
AVFrame *pic = avctx->coded_frame;
|
2011-11-07 20:20:26 +03:00
|
|
|
int w = avctx->width, h = avctx->height;
|
2011-10-17 03:22:59 +03:00
|
|
|
|
|
|
|
/* Set flags */
|
|
|
|
pic->reference = 0;
|
|
|
|
pic->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
pic->key_frame = 1;
|
|
|
|
|
|
|
|
/* Decode the frame */
|
2012-03-04 03:53:59 +03:00
|
|
|
utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
|
2011-10-17 03:22:59 +03:00
|
|
|
|
|
|
|
/* Set the output data depending on the colorspace */
|
2011-11-15 02:06:02 +03:00
|
|
|
switch (avctx->pix_fmt) {
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_YUV420P:
|
2011-11-15 02:06:02 +03:00
|
|
|
pic->linesize[0] = w;
|
|
|
|
pic->linesize[1] = pic->linesize[2] = w / 2;
|
2012-03-04 03:53:59 +03:00
|
|
|
pic->data[0] = utv->buffer;
|
|
|
|
pic->data[2] = utv->buffer + (w * h);
|
2011-11-15 02:06:02 +03:00
|
|
|
pic->data[1] = pic->data[2] + (w * h / 4);
|
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_YUYV422:
|
2011-11-15 02:06:02 +03:00
|
|
|
pic->linesize[0] = w * 2;
|
2012-03-04 03:53:59 +03:00
|
|
|
pic->data[0] = utv->buffer;
|
2011-11-15 02:06:02 +03:00
|
|
|
break;
|
2012-10-08 21:54:00 +03:00
|
|
|
case AV_PIX_FMT_BGR24:
|
|
|
|
case AV_PIX_FMT_RGB32:
|
2011-11-15 02:06:02 +03:00
|
|
|
/* Make the linesize negative, since Ut Video uses bottom-up BGR */
|
2012-10-08 21:54:00 +03:00
|
|
|
pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
|
2012-03-04 03:53:59 +03:00
|
|
|
pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
|
2011-11-15 02:06:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-10-17 03:22:59 +03:00
|
|
|
|
2012-12-05 21:24:43 +03:00
|
|
|
*got_frame = 1;
|
2013-12-10 02:50:09 +03:00
|
|
|
av_frame_move_ref((AVFrame*)data, pic);
|
2011-10-17 03:22:59 +03:00
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int utvideo_decode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
|
|
|
|
|
|
|
|
/* Free output */
|
2013-12-10 02:52:12 +03:00
|
|
|
av_frame_free(&avctx->coded_frame);
|
2012-03-04 03:53:59 +03:00
|
|
|
av_freep(&utv->buffer);
|
2011-10-17 03:22:59 +03:00
|
|
|
|
|
|
|
/* Finish decoding and clean up the instance */
|
|
|
|
utv->codec->DecodeEnd();
|
|
|
|
CCodec::DeleteInstance(utv->codec);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_libutvideo_decoder = {
|
2011-11-04 20:01:19 +03:00
|
|
|
"libutvideo",
|
2012-02-07 20:28:00 +03:00
|
|
|
NULL_IF_CONFIG_SMALL("Ut Video"),
|
2011-10-17 03:22:59 +03:00
|
|
|
AVMEDIA_TYPE_VIDEO,
|
2012-08-07 23:45:46 +03:00
|
|
|
AV_CODEC_ID_UTVIDEO,
|
2012-02-07 20:28:00 +03:00
|
|
|
0, //capabilities
|
|
|
|
NULL, //supported_framerates
|
|
|
|
NULL, //pix_fmts
|
|
|
|
NULL, //supported_samplerates
|
|
|
|
NULL, //sample_fmts
|
|
|
|
NULL, //channel_layouts
|
|
|
|
0, //max_lowres
|
|
|
|
NULL, //priv_class
|
|
|
|
NULL, //profiles
|
2011-10-17 03:22:59 +03:00
|
|
|
sizeof(UtVideoContext),
|
2012-02-07 20:28:00 +03:00
|
|
|
NULL, //next
|
|
|
|
NULL, //init_thread_copy
|
|
|
|
NULL, //update_thread_context
|
|
|
|
NULL, //defaults
|
|
|
|
NULL, //init_static_data
|
2011-10-17 03:22:59 +03:00
|
|
|
utvideo_decode_init,
|
2012-02-07 20:28:00 +03:00
|
|
|
NULL, //encode
|
|
|
|
NULL, //encode2
|
2011-10-17 03:22:59 +03:00
|
|
|
utvideo_decode_frame,
|
2012-02-07 20:28:00 +03:00
|
|
|
utvideo_decode_close,
|
2011-10-17 03:22:59 +03:00
|
|
|
};
|