]> git.sesse.net Git - ffmpeg/blob - libavcodec/libutvideo.cpp
Set more KMVC palettes opaque.
[ffmpeg] / libavcodec / libutvideo.cpp
1 /*
2  * Copyright (c) 2011 Derek Buitenhuis
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * Known FOURCCs:
24  *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
25  */
26
27 extern "C" {
28 #include "avcodec.h"
29 }
30
31 #include <stdlib.h>
32 #include <utvideo/utvideo.h>
33 #include <utvideo/Codec.h>
34
35 #include "get_bits.h"
36
37 typedef struct {
38     uint32_t version;
39     uint32_t original_format;
40     uint32_t frameinfo_size;
41     uint32_t flags;
42 } UtVideoExtra;
43
44 typedef struct {
45     CCodec *codec;
46     uint8_t *output;
47 } UtVideoContext;
48
49 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
50 {
51     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
52     UtVideoExtra info;
53     int format;
54     int begin_ret;
55     unsigned int buf_size;
56
57     if(avctx->extradata_size != 4*4)
58     {
59         av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
60         return -1;
61     }
62
63     /* Read extradata */
64     info.version = AV_RL32(avctx->extradata);
65     info.original_format = AV_RL32(avctx->extradata + 4);
66     info.frameinfo_size = AV_RL32(avctx->extradata + 8);
67     info.flags = AV_RL32(avctx->extradata + 12);
68
69     /* Pick format based on FOURCC */
70     switch(avctx->codec_tag)
71     {
72         case MKTAG('U', 'L', 'Y', '0'):
73             avctx->pix_fmt = PIX_FMT_YUV420P;
74             format = UTVF_YV12;
75             break;
76         case MKTAG('U', 'L', 'Y', '2'):
77             avctx->pix_fmt = PIX_FMT_YUYV422;
78             format = UTVF_YUY2;
79             break;
80         case MKTAG('U', 'L', 'R', 'G'):
81             avctx->pix_fmt = PIX_FMT_BGR24;
82             format = UTVF_RGB24_WIN;
83             break;
84         case MKTAG('U', 'L', 'R', 'A'):
85             avctx->pix_fmt = PIX_FMT_RGB32;
86             format = UTVF_RGB32_WIN;
87             break;
88         default:
89             av_log(avctx, AV_LOG_ERROR,
90                   "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
91             return -1;
92     }
93
94     /* Only allocate the buffer once */
95     buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
96     utv->output = (uint8_t *)av_malloc(buf_size * sizeof(uint8_t));
97
98     if(utv->output == NULL)
99     {
100         av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
101         return -1;
102     }
103
104     /* Allocate the output frame */
105     avctx->coded_frame = avcodec_alloc_frame();
106
107     /* Ut Video only supports 8-bit */
108     avctx->bits_per_raw_sample = 8;
109
110     /* Is it interlaced? */
111     avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
112
113     /* Apparently Ut Video doesn't store this info... */
114     avctx->coded_frame->top_field_first = 1;
115
116     /*
117      * Create a Ut Video instance. Since the function wants
118      * an "interface name" string, pass it the name of the lib.
119      */
120     utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
121
122     /* Initialize Decoding */
123     begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
124                             CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
125
126     /* Check to see if the decoder initlized properly */
127     if(begin_ret != 0)
128     {
129         av_log(avctx, AV_LOG_ERROR,
130                "Could not initialize decoder: %d\n", begin_ret);
131         return -1;
132     }
133
134     return 0;
135 }
136
137 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
138                                 int *data_size, AVPacket *avpkt)
139 {
140     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
141     AVFrame *pic = avctx->coded_frame;
142     int w = avctx->width, h = avctx->height;
143
144     /* Set flags */
145     pic->reference = 0;
146     pic->pict_type = AV_PICTURE_TYPE_I;
147     pic->key_frame = 1;
148
149     /* Decode the frame */
150     utv->codec->DecodeFrame(utv->output, avpkt->data, true);
151
152     /* Set the output data depending on the colorspace */
153     switch(avctx->pix_fmt)
154     {
155         case PIX_FMT_YUV420P:
156             pic->linesize[0] = w;
157             pic->linesize[1] = pic->linesize[2] = w / 2;
158             pic->data[0] = utv->output;
159             pic->data[2] = utv->output + (w * h);
160             pic->data[1] = pic->data[2] + (w * h / 4);
161             break;
162         case PIX_FMT_YUYV422:
163             pic->linesize[0] = w * 2;
164             pic->data[0] = utv->output;
165             break;
166         case PIX_FMT_BGR24:
167         case PIX_FMT_RGB32:
168             /* Make the linesize negative, since Ut Video uses bottom-up BGR */
169             pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
170             pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
171             break;
172      }
173
174     *data_size = sizeof(AVFrame);
175     *(AVFrame *)data = *pic;
176
177     return avpkt->size;
178 }
179
180 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
181 {
182     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
183
184     /* Free output */
185     av_freep(&avctx->coded_frame);
186     av_freep(&utv->output);
187
188     /* Finish decoding and clean up the instance */
189     utv->codec->DecodeEnd();
190     CCodec::DeleteInstance(utv->codec);
191
192     return 0;
193 }
194
195 AVCodec ff_libutvideo_decoder = {
196     "libutvideo",
197     AVMEDIA_TYPE_VIDEO,
198     CODEC_ID_UTVIDEO,
199     sizeof(UtVideoContext),
200     utvideo_decode_init,
201     NULL,
202     utvideo_decode_close,
203     utvideo_decode_frame,
204     NULL,
205     NULL,
206     NULL,
207     NULL,
208     NULL,
209     NULL_IF_CONFIG_SMALL("Ut Video"),
210 };