libavcodec/libutvideodec.cpp
1de357d6
 /*
  * 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:
8ed6c13f
  *     '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)
1de357d6
  */
 
 extern "C" {
 #include "avcodec.h"
 }
 
d6e4e69a
 #include "libutvideo.h"
1de357d6
 #include "get_bits.h"
 
 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
 {
     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
     UtVideoExtra info;
e3475198
     int format;
15960951
     int begin_ret;
1de357d6
 
34958174
     if (avctx->extradata_size != 4*4) {
1de357d6
         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);
f44f093d
     info.frameinfo_size = AV_RL32(avctx->extradata + 8);
1de357d6
     info.flags = AV_RL32(avctx->extradata + 12);
 
e3475198
     /* Pick format based on FOURCC */
34958174
     switch (avctx->codec_tag) {
8ed6c13f
 #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
34958174
     case MKTAG('U', 'L', 'Y', '0'):
ac627b3d
         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
34958174
         format = UTVF_YV12;
         break;
     case MKTAG('U', 'L', 'Y', '2'):
ac627b3d
         avctx->pix_fmt = AV_PIX_FMT_YUYV422;
34958174
         format = UTVF_YUY2;
         break;
     case MKTAG('U', 'L', 'R', 'G'):
ac627b3d
         avctx->pix_fmt = AV_PIX_FMT_BGR24;
98c4268e
         format = UTVF_NFCC_BGR_BU;
34958174
         break;
     case MKTAG('U', 'L', 'R', 'A'):
ac627b3d
         avctx->pix_fmt = AV_PIX_FMT_RGB32;
98c4268e
         format = UTVF_NFCC_BGRA_BU;
34958174
         break;
     default:
         av_log(avctx, AV_LOG_ERROR,
               "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
         return -1;
1de357d6
     }
 
     /* Only allocate the buffer once */
69035f3e
     utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
d6e4e69a
     utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
1de357d6
 
d6e4e69a
     if (utv->buffer == NULL) {
1de357d6
         av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
         return -1;
     }
 
b580ab6b
     /* Allocate the output frame */
1de357d6
     avctx->coded_frame = avcodec_alloc_frame();
 
     /* 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 */
15960951
     begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
e3475198
                             CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
1de357d6
 
15960951
     /* Check to see if the decoder initlized properly */
34958174
     if (begin_ret != 0) {
15960951
         av_log(avctx, AV_LOG_ERROR,
                "Could not initialize decoder: %d\n", begin_ret);
         return -1;
     }
 
1de357d6
     return 0;
 }
 
 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
4012cd6c
                                 int *got_frame, AVPacket *avpkt)
1de357d6
 {
     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
     AVFrame *pic = avctx->coded_frame;
1ff1b5f7
     int w = avctx->width, h = avctx->height;
1de357d6
 
     /* Set flags */
     pic->reference = 0;
     pic->pict_type = AV_PICTURE_TYPE_I;
     pic->key_frame = 1;
 
     /* Decode the frame */
d6e4e69a
     utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
1de357d6
 
     /* Set the output data depending on the colorspace */
34958174
     switch (avctx->pix_fmt) {
ac627b3d
     case AV_PIX_FMT_YUV420P:
34958174
         pic->linesize[0] = w;
         pic->linesize[1] = pic->linesize[2] = w / 2;
d6e4e69a
         pic->data[0] = utv->buffer;
         pic->data[2] = utv->buffer + (w * h);
34958174
         pic->data[1] = pic->data[2] + (w * h / 4);
         break;
ac627b3d
     case AV_PIX_FMT_YUYV422:
34958174
         pic->linesize[0] = w * 2;
d6e4e69a
         pic->data[0] = utv->buffer;
34958174
         break;
ac627b3d
     case AV_PIX_FMT_BGR24:
     case AV_PIX_FMT_RGB32:
34958174
         /* Make the linesize negative, since Ut Video uses bottom-up BGR */
ac627b3d
         pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
d6e4e69a
         pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
34958174
         break;
     }
1de357d6
 
4012cd6c
     *got_frame = 1;
ed621efb
     av_frame_move_ref((AVFrame*)data, pic);
1de357d6
 
     return avpkt->size;
 }
 
 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
 {
     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
 
     /* Free output */
     av_freep(&avctx->coded_frame);
d6e4e69a
     av_freep(&utv->buffer);
1de357d6
 
     /* Finish decoding and clean up the instance */
     utv->codec->DecodeEnd();
     CCodec::DeleteInstance(utv->codec);
 
     return 0;
 }
 
 AVCodec ff_libutvideo_decoder = {
682e0eaf
     "libutvideo",
bf5c0308
     NULL_IF_CONFIG_SMALL("Ut Video"),
1de357d6
     AVMEDIA_TYPE_VIDEO,
7a72695c
     AV_CODEC_ID_UTVIDEO,
bf5c0308
     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
1de357d6
     sizeof(UtVideoContext),
bf5c0308
     NULL, //next
     NULL, //init_thread_copy
     NULL, //update_thread_context
     NULL, //defaults
     NULL, //init_static_data
1de357d6
     utvideo_decode_init,
bf5c0308
     NULL, //encode
     NULL, //encode2
1de357d6
     utvideo_decode_frame,
bf5c0308
     utvideo_decode_close,
1de357d6
 };