]> git.sesse.net Git - ffmpeg/blob - libavcodec/libutvideodec.cpp
Merge commit 'a6674d2e7771dbf7a4a5556f5e126be83cadac96'
[ffmpeg] / libavcodec / libutvideodec.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  *     'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
26  */
27
28 extern "C" {
29 #include "avcodec.h"
30 }
31
32 #include "libutvideo.h"
33 #include "get_bits.h"
34
35 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
36 {
37     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
38     UtVideoExtra info;
39     int format;
40     int begin_ret;
41
42     if (avctx->extradata_size != 16 && avctx->extradata_size != 8 ) {
43         av_log(avctx, AV_LOG_ERROR, "Extradata size (%d) mismatch.\n", avctx->extradata_size);
44         return -1;
45     }
46
47     /* Read extradata */
48     info.version = AV_RL32(avctx->extradata);
49     info.original_format = AV_RL32(avctx->extradata + 4);
50     info.frameinfo_size = AV_RL32(avctx->extradata + 8);
51     info.flags = AV_RL32(avctx->extradata + 12);
52
53     /* Pick format based on FOURCC */
54     switch (avctx->codec_tag) {
55 #ifdef UTV_BT709
56     case MKTAG('U', 'L', 'H', '0'):
57         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
58         avctx->colorspace = AVCOL_SPC_BT709;
59         format = UTVF_YV12;
60         break;
61     case MKTAG('U', 'L', 'H', '2'):
62         avctx->pix_fmt = AV_PIX_FMT_YUYV422;
63         avctx->colorspace = AVCOL_SPC_BT709;
64         format = UTVF_YUY2;
65         break;
66 #endif
67     case MKTAG('U', 'L', 'Y', '0'):
68         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
69         format = UTVF_YV12;
70         break;
71     case MKTAG('U', 'L', 'Y', '2'):
72         avctx->pix_fmt = AV_PIX_FMT_YUYV422;
73         format = UTVF_YUY2;
74         break;
75     case MKTAG('U', 'L', 'R', 'G'):
76         avctx->pix_fmt = AV_PIX_FMT_BGR24;
77         format = UTVF_NFCC_BGR_BU;
78         break;
79     case MKTAG('U', 'L', 'R', 'A'):
80         avctx->pix_fmt = AV_PIX_FMT_RGB32;
81         format = UTVF_NFCC_BGRA_BU;
82         break;
83 #ifdef UTVF_UQY2
84     case MKTAG('U', 'Q', 'Y', '2'):
85         avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
86         format = UTVF_v210;
87         break;
88 #endif
89     default:
90         av_log(avctx, AV_LOG_ERROR,
91               "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
92         return -1;
93     }
94
95     /* Only allocate the buffer once */
96     utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
97     if (format == UTVF_v210)
98         utv->buf_size += avctx->height * ((avctx->width + 47) / 48) * 128; // the linesize used by the decoder, this does not seem to be exported
99     utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
100
101     if (utv->buffer == NULL) {
102         av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
103         return -1;
104     }
105
106     /* Allocate the output frame */
107     avctx->coded_frame = av_frame_alloc();
108
109     /* Ut Video only supports 8-bit */
110     avctx->bits_per_raw_sample = 8;
111
112     /* Is it interlaced? */
113     avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
114
115     /* Apparently Ut Video doesn't store this info... */
116     avctx->coded_frame->top_field_first = 1;
117
118     /*
119      * Create a Ut Video instance. Since the function wants
120      * an "interface name" string, pass it the name of the lib.
121      */
122     utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
123
124     /* Initialize Decoding */
125     begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
126                             CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
127
128     /* Check to see if the decoder initlized properly */
129     if (begin_ret != 0) {
130         av_log(avctx, AV_LOG_ERROR,
131                "Could not initialize decoder: %d\n", begin_ret);
132         return -1;
133     }
134
135     return 0;
136 }
137
138 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
139                                 int *got_frame, AVPacket *avpkt)
140 {
141     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
142     AVFrame *pic = avctx->coded_frame;
143     int w = avctx->width, h = avctx->height;
144
145     /* Set flags */
146     pic->reference = 0;
147     pic->pict_type = AV_PICTURE_TYPE_I;
148     pic->key_frame = 1;
149
150     /* Decode the frame */
151     utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
152
153     /* Set the output data depending on the colorspace */
154     switch (avctx->pix_fmt) {
155     case AV_PIX_FMT_YUV420P:
156         pic->linesize[0] = w;
157         pic->linesize[1] = pic->linesize[2] = w / 2;
158         pic->data[0] = utv->buffer;
159         pic->data[2] = utv->buffer + (w * h);
160         pic->data[1] = pic->data[2] + (w * h / 4);
161         break;
162     case AV_PIX_FMT_YUYV422:
163         pic->linesize[0] = w * 2;
164         pic->data[0] = utv->buffer;
165         break;
166     case AV_PIX_FMT_YUV422P10: {
167         uint16_t *y, *u, *v;
168         int i,j;
169         int linesize = ((w + 47) / 48) * 128;
170
171         pic->linesize[0] = w * 2;
172         pic->linesize[1] =
173         pic->linesize[2] = w;
174         pic->data[0] = utv->buffer + linesize * h;
175         pic->data[1] = pic->data[0] + h*pic->linesize[0];
176         pic->data[2] = pic->data[1] + h*pic->linesize[1];
177         y = (uint16_t*)pic->data[0];
178         u = (uint16_t*)pic->data[1];
179         v = (uint16_t*)pic->data[2];
180         for (j = 0; j < h; j++) {
181             const uint8_t *in = utv->buffer + j * linesize;
182
183             for (i = 0; i + 1 < w; i += 6, in += 4) {
184                 unsigned a,b;
185                 a = AV_RL32(in);
186                 in += 4;
187                 b = AV_RL32(in);
188                 *u++ = (a    ) & 0x3FF;
189                 *y++ = (a>>10) & 0x3FF;
190                 *v++ = (a>>20) & 0x3FF;
191                 *y++ = (b    ) & 0x3FF;
192
193                 if (i + 3 >= w)
194                     break;
195
196                 in += 4;
197                 a = AV_RL32(in);
198                 *u++ = (b>>10) & 0x3FF;
199                 *y++ = (b>>20) & 0x3FF;
200                 *v++ = (a    ) & 0x3FF;
201                 *y++ = (a>>10) & 0x3FF;
202
203                 if (i + 5 >= w)
204                     break;
205
206                 in += 4;
207                 b = AV_RL32(in);
208                 *u++ = (a>>20) & 0x3FF;
209                 *y++ = (b    ) & 0x3FF;
210                 *v++ = (b>>10) & 0x3FF;
211                 *y++ = (b>>20) & 0x3FF;
212             }
213         }
214         break;
215     }
216     case AV_PIX_FMT_BGR24:
217     case AV_PIX_FMT_RGB32:
218         /* Make the linesize negative, since Ut Video uses bottom-up BGR */
219         pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
220         pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
221         break;
222     }
223
224     *got_frame = 1;
225     av_frame_move_ref((AVFrame*)data, pic);
226
227     return avpkt->size;
228 }
229
230 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
231 {
232     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
233
234     /* Free output */
235     av_frame_free(&avctx->coded_frame);
236     av_freep(&utv->buffer);
237
238     /* Finish decoding and clean up the instance */
239     utv->codec->DecodeEnd();
240     CCodec::DeleteInstance(utv->codec);
241
242     return 0;
243 }
244
245 AVCodec ff_libutvideo_decoder = {
246     "libutvideo",
247     NULL_IF_CONFIG_SMALL("Ut Video"),
248     AVMEDIA_TYPE_VIDEO,
249     AV_CODEC_ID_UTVIDEO,
250     0,    //capabilities
251     NULL, //supported_framerates
252     NULL, //pix_fmts
253     NULL, //supported_samplerates
254     NULL, //sample_fmts
255     NULL, //channel_layouts
256     0,    //max_lowres
257     NULL, //priv_class
258     NULL, //profiles
259     sizeof(UtVideoContext),
260     NULL, //next
261     NULL, //init_thread_copy
262     NULL, //update_thread_context
263     NULL, //defaults
264     NULL, //init_static_data
265     utvideo_decode_init,
266     NULL, //encode
267     NULL, //encode2
268     utvideo_decode_frame,
269     utvideo_decode_close,
270 };