]> git.sesse.net Git - ffmpeg/blob - libavcodec/libutvideo.cpp
libutvideodec: Remove CODEC_CAP_LOSSLESS
[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     unsigned int buf_size;
55
56     if(avctx->extradata_size != 4*4)
57     {
58         av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
59         return -1;
60     }
61
62     /* Read extradata */
63     info.version = AV_RL32(avctx->extradata);
64     info.original_format = AV_RL32(avctx->extradata + 4);
65     info.frameinfo_size = AV_RL32(avctx->extradata + 8);
66     info.flags = AV_RL32(avctx->extradata + 12);
67
68     /* Pick format based on FOURCC */
69     switch(avctx->codec_tag)
70     {
71         case MKTAG('U', 'L', 'Y', '0'):
72             avctx->pix_fmt = PIX_FMT_YUV420P;
73             format = UTVF_YV12;
74             break;
75         case MKTAG('U', 'L', 'Y', '2'):
76             avctx->pix_fmt = PIX_FMT_YUYV422;
77             format = UTVF_YUY2;
78             break;
79         case MKTAG('U', 'L', 'R', 'G'):
80             avctx->pix_fmt = PIX_FMT_BGR24;
81             format = UTVF_RGB24_WIN;
82             break;
83         case MKTAG('U', 'L', 'R', 'A'):
84             avctx->pix_fmt = PIX_FMT_RGB32;
85             format = UTVF_RGB32_WIN;
86             break;
87         default:
88             av_log(avctx, AV_LOG_ERROR,
89                   "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
90             return -1;
91     }
92
93     /* Only allocate the buffer once */
94     buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
95     utv->output = (uint8_t *)av_malloc(buf_size * sizeof(uint8_t));
96
97     if(utv->output == NULL)
98     {
99         av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
100         return -1;
101     }
102
103     /* Allocate the output frame */
104     avctx->coded_frame = avcodec_alloc_frame();
105
106     /* Ut Video only supports 8-bit */
107     avctx->bits_per_raw_sample = 8;
108
109     /* Is it interlaced? */
110     avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
111
112     /* Apparently Ut Video doesn't store this info... */
113     avctx->coded_frame->top_field_first = 1;
114
115     /*
116      * Create a Ut Video instance. Since the function wants
117      * an "interface name" string, pass it the name of the lib.
118      */
119     utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
120
121     /* Initialize Decoding */
122     utv->codec->DecodeBegin(format, avctx->width, avctx->height,
123                             CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
124
125     return 0;
126 }
127
128 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
129                                 int *data_size, AVPacket *avpkt)
130 {
131     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
132     AVFrame *pic = avctx->coded_frame;
133     int w = avctx->width, h = avctx->height;
134
135     /* Set flags */
136     pic->reference = 0;
137     pic->pict_type = AV_PICTURE_TYPE_I;
138     pic->key_frame = 1;
139
140     /* Decode the frame */
141     utv->codec->DecodeFrame(utv->output, avpkt->data, true);
142
143     /* Set the output data depending on the colorspace */
144     switch(avctx->pix_fmt)
145     {
146         case PIX_FMT_YUV420P:
147             pic->linesize[0] = w;
148             pic->linesize[1] = pic->linesize[2] = w / 2;
149             pic->data[0] = utv->output;
150             pic->data[2] = utv->output + (w * h);
151             pic->data[1] = pic->data[2] + (w * h / 4);
152             break;
153         case PIX_FMT_YUYV422:
154             pic->linesize[0] = w * 2;
155             pic->data[0] = utv->output;
156             break;
157         case PIX_FMT_BGR24:
158         case PIX_FMT_RGB32:
159             /* Make the linesize negative, since Ut Video uses bottom-up BGR */
160             pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
161             pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
162             break;
163      }
164
165     *data_size = sizeof(AVFrame);
166     *(AVFrame *)data = *pic;
167
168     return avpkt->size;
169 }
170
171 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
172 {
173     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
174
175     /* Free output */
176     av_freep(&avctx->coded_frame);
177     av_freep(&utv->output);
178
179     /* Finish decoding and clean up the instance */
180     utv->codec->DecodeEnd();
181     CCodec::DeleteInstance(utv->codec);
182
183     return 0;
184 }
185
186 AVCodec ff_libutvideo_decoder = {
187     "libutvideo",
188     AVMEDIA_TYPE_VIDEO,
189     CODEC_ID_UTVIDEO,
190     sizeof(UtVideoContext),
191     utvideo_decode_init,
192     NULL,
193     utvideo_decode_close,
194     utvideo_decode_frame,
195     NULL,
196     NULL,
197     NULL,
198     NULL,
199     NULL,
200     NULL_IF_CONFIG_SMALL("Ut Video"),
201 };