]> git.sesse.net Git - ffmpeg/blob - libavcodec/libutvideo.cpp
Merge remote-tracking branch 'qatar/master'
[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 stripes;
41     uint32_t flags;
42 } UtVideoExtra;
43
44 typedef struct {
45     CCodec *codec;
46     unsigned int buf_size;
47     uint8_t *output;
48 } UtVideoContext;
49
50 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
51 {
52     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
53     UtVideoExtra info;
54     int defined_fourcc = 0;
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.stripes = AV_RL32(avctx->extradata + 8);
66     info.flags = AV_RL32(avctx->extradata + 12);
67
68     /* Try to output the original format */
69     switch(UNFCC(info.original_format))
70     {
71         case UTVF_YV12:
72             avctx->pix_fmt = PIX_FMT_YUV420P;
73             break;
74         case UTVF_YUY2:
75         case UTVF_YUYV:
76         case UTVF_YUNV:
77             avctx->pix_fmt = PIX_FMT_YUYV422;
78             break;
79         case UTVF_UYVY:
80         case UTVF_UYNV:
81             avctx->pix_fmt = PIX_FMT_UYVY422;
82             break;
83         case UTVF_RGB24_WIN:
84             avctx->pix_fmt = PIX_FMT_BGR24;
85             break;
86         case UTVF_RGB32_WIN:
87             avctx->pix_fmt = PIX_FMT_RGB32;
88             break;
89         case UTVF_ARGB32_WIN:
90             avctx->pix_fmt = PIX_FMT_ARGB;
91             break;
92         case 0:
93             /* Fall back on FOURCC */
94             switch(UNFCC(avctx->codec_tag))
95             {
96                 case UTVF_ULY0:
97                     avctx->pix_fmt = PIX_FMT_YUV420P;
98                     defined_fourcc = UTVF_YV12;
99                     break;
100                 case UTVF_ULY2:
101                     avctx->pix_fmt = PIX_FMT_YUYV422;
102                     defined_fourcc = UTVF_YUY2;
103                     break;
104                 case UTVF_ULRG:
105                     avctx->pix_fmt = PIX_FMT_BGR24;
106                     defined_fourcc = UTVF_RGB24_WIN;
107                     break;
108                 case UTVF_ULRA:
109                     avctx->pix_fmt = PIX_FMT_RGB32;
110                     defined_fourcc = UTVF_RGB32_WIN;
111                     break;
112             }
113             break;
114         default:
115             av_log(avctx, AV_LOG_ERROR,
116                   "Codec ExtraData is Corrupt or Invalid: %X\n", info.original_format);
117             return -1;
118     }
119
120     /* Only allocate the buffer once */
121     utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
122     utv->output = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
123
124     if(utv->output == NULL)
125     {
126         av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
127         return -1;
128     }
129
130     /* Allocate the output frame  */
131     avctx->coded_frame = avcodec_alloc_frame();
132
133     /* Ut Video only supports 8-bit */
134     avctx->bits_per_raw_sample = 8;
135
136     /* Is it interlaced? */
137     avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
138
139     /* Apparently Ut Video doesn't store this info... */
140     avctx->coded_frame->top_field_first = 1;
141
142     /*
143      * Create a Ut Video instance. Since the function wants
144      * an "interface name" string, pass it the name of the lib.
145      */
146     utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
147
148     /* Initialize Decoding */
149     utv->codec->DecodeBegin(defined_fourcc ? defined_fourcc : UNFCC(info.original_format),
150                             avctx->width, avctx->height, CBGROSSWIDTH_WINDOWS, &info,
151                             sizeof(UtVideoExtra));
152
153     return 0;
154 }
155
156 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
157                                 int *data_size, AVPacket *avpkt)
158 {
159     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
160     AVFrame *pic = avctx->coded_frame;
161     unsigned int w = avctx->width, h = avctx->height;
162
163     /* Set flags */
164     pic->reference = 0;
165     pic->pict_type = AV_PICTURE_TYPE_I;
166     pic->key_frame = 1;
167
168     /* Decode the frame */
169     utv->codec->DecodeFrame(utv->output, avpkt->data, true);
170
171     /* Set the output data depending on the colorspace */
172     switch(avctx->pix_fmt)
173     {
174         case PIX_FMT_YUV420P:
175             pic->linesize[0] = w;
176             pic->linesize[1] = pic->linesize[2] = w / 2;
177             pic->data[0] = utv->output;
178             pic->data[2] = utv->output + (w * h);
179             pic->data[1] = pic->data[2] + (w * h / 4);
180             break;
181         case PIX_FMT_YUYV422:
182         case PIX_FMT_UYVY422:
183             pic->linesize[0] = w * 2;
184             pic->data[0] = utv->output;
185             break;
186         case PIX_FMT_BGR24:
187         case PIX_FMT_RGB32:
188             /* Make the linesize negative, since Ut Video uses bottom-up BGR */
189             pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
190             pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
191             break;
192      }
193
194     *data_size = sizeof(AVFrame);
195     *(AVFrame *)data = *pic;
196
197     return avpkt->size;
198 }
199
200 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
201 {
202     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
203
204     /* Free output */
205     av_freep(&avctx->coded_frame);
206     av_freep(&utv->output);
207
208     /* Finish decoding and clean up the instance */
209     utv->codec->DecodeEnd();
210     CCodec::DeleteInstance(utv->codec);
211
212     return 0;
213 }
214
215 AVCodec ff_libutvideo_decoder = {
216     "utvideo",
217     AVMEDIA_TYPE_VIDEO,
218     CODEC_ID_UTVIDEO,
219     sizeof(UtVideoContext),
220     utvideo_decode_init,
221     NULL,
222     utvideo_decode_close,
223     utvideo_decode_frame,
224     CODEC_CAP_LOSSLESS,
225     NULL,
226     NULL,
227     NULL,
228     NULL,
229     NULL_IF_CONFIG_SMALL("Ut Video"),
230 };