Browse code

libutvideo: Add Ut Video Decoder Wrapper

Add a wrapper for libutvideo's decoder.

This supports decoding the following FOURCCs:
ULY0 - 4:2:0 YCbCr
ULY2 - 4:2:2 YCbCr
ULRG - RGB
ULRA - RGBA

Also, bump version.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Derek Buitenhuis authored on 2011/10/17 09:22:59
Showing 7 changed files
... ...
@@ -185,6 +185,7 @@ External library support:
185 185
   --enable-libspeex        enable Speex encoding and decoding via libspeex [no]
186 186
   --enable-libstagefright-h264  enable H.264 decoding via libstagefright [no]
187 187
   --enable-libtheora       enable Theora encoding via libtheora [no]
188
+  --enable-libutvideo      enable Ut Video decoding via libutvideo [no]
188 189
   --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
189 190
   --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
190 191
   --enable-libvorbis       enable Vorbis encoding via libvorbis,
... ...
@@ -1015,6 +1016,7 @@ CONFIG_LIST="
1015 1015
     libspeex
1016 1016
     libstagefright_h264
1017 1017
     libtheora
1018
+    libutvideo
1018 1019
     libvo_aacenc
1019 1020
     libvo_amrwbenc
1020 1021
     libvorbis
... ...
@@ -1513,6 +1515,7 @@ libvpx_encoder_deps="libvpx"
1513 1513
 libx264_encoder_deps="libx264"
1514 1514
 libxavs_encoder_deps="libxavs"
1515 1515
 libxvid_encoder_deps="libxvid"
1516
+libutvideo_decoder_deps="libutvideo gpl"
1516 1517
 
1517 1518
 # demuxers / muxers
1518 1519
 ac3_demuxer_select="ac3_parser"
... ...
@@ -3035,6 +3038,7 @@ enabled libstagefright_h264  && require_cpp libstagefright_h264 "binder/ProcessS
3035 3035
     media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h
3036 3036
     media/stagefright/OMXClient.h media/stagefright/OMXCodec.h" android::OMXClient -lstagefright -lmedia -lutils -lbinder
3037 3037
 enabled libtheora  && require  libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg
3038
+enabled libutvideo    && require_cpp utvideo "stdint.h stdlib.h utvideo/utvideo.h utvideo/Codec.h" 'CCodec*' -lutvideo -lstdc++
3038 3039
 enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc
3039 3040
 enabled libvo_amrwbenc && require libvo_amrwbenc vo-amrwbenc/enc_if.h E_IF_init -lvo-amrwbenc
3040 3041
 enabled libvorbis  && require  libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg
... ...
@@ -3361,6 +3365,7 @@ echo "libschroedinger enabled   ${libschroedinger-no}"
3361 3361
 echo "libspeex enabled          ${libspeex-no}"
3362 3362
 echo "libstagefright-h264 enabled    ${libstagefright_h264-no}"
3363 3363
 echo "libtheora enabled         ${libtheora-no}"
3364
+echo "libutvideo enabled        ${libutvideo-no}"
3364 3365
 echo "libvo-aacenc support      ${libvo_aacenc-no}"
3365 3366
 echo "libvo-amrwbenc support    ${libvo_amrwbenc-no}"
3366 3367
 echo "libvorbis enabled         ${libvorbis-no}"
... ...
@@ -606,6 +606,7 @@ OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
606 606
 OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o
607 607
 OBJS-$(CONFIG_LIBSTAGEFRIGHT_H264_DECODER)+= libstagefright.o
608 608
 OBJS-$(CONFIG_LIBTHEORA_ENCODER)          += libtheoraenc.o
609
+OBJS-$(CONFIG_LIBUTVIDEO_DECODER)         += libutvideo.o
609 610
 OBJS-$(CONFIG_LIBVO_AACENC_ENCODER)       += libvo-aacenc.o mpeg4audio.o
610 611
 OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER)     += libvo-amrwbenc.o
611 612
 OBJS-$(CONFIG_LIBVORBIS_ENCODER)          += libvorbis.o vorbis_data.o
... ...
@@ -389,6 +389,7 @@ void avcodec_register_all(void)
389 389
     REGISTER_ENCDEC  (LIBSPEEX, libspeex);
390 390
     REGISTER_DECODER (LIBSTAGEFRIGHT_H264, libstagefright_h264);
391 391
     REGISTER_ENCODER (LIBTHEORA, libtheora);
392
+    REGISTER_DECODER (LIBUTVIDEO, libutvideo);
392 393
     REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc);
393 394
     REGISTER_ENCODER (LIBVO_AMRWBENC, libvo_amrwbenc);
394 395
     REGISTER_ENCODER (LIBVORBIS, libvorbis);
... ...
@@ -212,6 +212,7 @@ enum CodecID {
212 212
     CODEC_ID_VC1IMAGE,
213 213
     CODEC_ID_8SVX_RAW,
214 214
     CODEC_ID_G2M,
215
+    CODEC_ID_UTVIDEO = 0x800,
215 216
 
216 217
     /* various PCM "codecs" */
217 218
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
218 219
new file mode 100644
... ...
@@ -0,0 +1,230 @@
0
+/*
1
+ * Copyright (c) 2011 Derek Buitenhuis
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU General Public
7
+ * License as published by the Free Software Foundation;
8
+ * version 2 of the License.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Known FOURCCs:
23
+ *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
24
+ */
25
+
26
+extern "C" {
27
+#include "avcodec.h"
28
+}
29
+
30
+#include <stdlib.h>
31
+#include <utvideo/utvideo.h>
32
+#include <utvideo/Codec.h>
33
+
34
+#include "get_bits.h"
35
+
36
+typedef struct {
37
+    uint32_t version;
38
+    uint32_t original_format;
39
+    uint32_t stripes;
40
+    uint32_t flags;
41
+} UtVideoExtra;
42
+
43
+typedef struct {
44
+    CCodec *codec;
45
+    unsigned int buf_size;
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 defined_fourcc = 0;
54
+
55
+    if(avctx->extradata_size != 4*4)
56
+    {
57
+        av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
58
+        return -1;
59
+    }
60
+
61
+    /* Read extradata */
62
+    info.version = AV_RL32(avctx->extradata);
63
+    info.original_format = AV_RL32(avctx->extradata + 4);
64
+    info.stripes = AV_RL32(avctx->extradata + 8);
65
+    info.flags = AV_RL32(avctx->extradata + 12);
66
+
67
+    /* Try to output the original format */
68
+    switch(UNFCC(info.original_format))
69
+    {
70
+        case UTVF_YV12:
71
+            avctx->pix_fmt = PIX_FMT_YUV420P;
72
+            break;
73
+        case UTVF_YUY2:
74
+        case UTVF_YUYV:
75
+        case UTVF_YUNV:
76
+            avctx->pix_fmt = PIX_FMT_YUYV422;
77
+            break;
78
+        case UTVF_UYVY:
79
+        case UTVF_UYNV:
80
+            avctx->pix_fmt = PIX_FMT_UYVY422;
81
+            break;
82
+        case UTVF_RGB24_WIN:
83
+            avctx->pix_fmt = PIX_FMT_BGR24;
84
+            break;
85
+        case UTVF_RGB32_WIN:
86
+            avctx->pix_fmt = PIX_FMT_RGB32;
87
+            break;
88
+        case UTVF_ARGB32_WIN:
89
+            avctx->pix_fmt = PIX_FMT_ARGB;
90
+            break;
91
+        case 0:
92
+            /* Fall back on FOURCC */
93
+            switch(UNFCC(avctx->codec_tag))
94
+            {
95
+                case UTVF_ULY0:
96
+                    avctx->pix_fmt = PIX_FMT_YUV420P;
97
+                    defined_fourcc = UTVF_YV12;
98
+                    break;
99
+                case UTVF_ULY2:
100
+                    avctx->pix_fmt = PIX_FMT_YUYV422;
101
+                    defined_fourcc = UTVF_YUY2;
102
+                    break;
103
+                case UTVF_ULRG:
104
+                    avctx->pix_fmt = PIX_FMT_BGR24;
105
+                    defined_fourcc = UTVF_RGB24_WIN;
106
+                    break;
107
+                case UTVF_ULRA:
108
+                    avctx->pix_fmt = PIX_FMT_RGB32;
109
+                    defined_fourcc = UTVF_RGB32_WIN;
110
+                    break;
111
+            }
112
+            break;
113
+        default:
114
+            av_log(avctx, AV_LOG_ERROR,
115
+                  "Codec ExtraData is Corrupt or Invalid: %X\n", info.original_format);
116
+            return -1;
117
+    }
118
+
119
+    /* Only allocate the buffer once */
120
+    utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
121
+    utv->output = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
122
+
123
+    if(utv->output == NULL)
124
+    {
125
+        av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
126
+        return -1;
127
+    }
128
+
129
+    /* Allocate the output frame  */
130
+    avctx->coded_frame = avcodec_alloc_frame();
131
+
132
+    /* Ut Video only supports 8-bit */
133
+    avctx->bits_per_raw_sample = 8;
134
+
135
+    /* Is it interlaced? */
136
+    avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
137
+
138
+    /* Apparently Ut Video doesn't store this info... */
139
+    avctx->coded_frame->top_field_first = 1;
140
+
141
+    /*
142
+     * Create a Ut Video instance. Since the function wants
143
+     * an "interface name" string, pass it the name of the lib.
144
+     */
145
+    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
146
+
147
+    /* Initialize Decoding */
148
+    utv->codec->DecodeBegin(defined_fourcc ? defined_fourcc : UNFCC(info.original_format),
149
+                            avctx->width, avctx->height, CBGROSSWIDTH_WINDOWS, &info,
150
+                            sizeof(UtVideoExtra));
151
+
152
+    return 0;
153
+}
154
+
155
+static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
156
+                                int *data_size, AVPacket *avpkt)
157
+{
158
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
159
+    AVFrame *pic = avctx->coded_frame;
160
+    unsigned int w = avctx->width, h = avctx->height;
161
+
162
+    /* Set flags */
163
+    pic->reference = 0;
164
+    pic->pict_type = AV_PICTURE_TYPE_I;
165
+    pic->key_frame = 1;
166
+
167
+    /* Decode the frame */
168
+    utv->codec->DecodeFrame(utv->output, avpkt->data, true);
169
+
170
+    /* Set the output data depending on the colorspace */
171
+    switch(avctx->pix_fmt)
172
+    {
173
+        case PIX_FMT_YUV420P:
174
+            pic->linesize[0] = w;
175
+            pic->linesize[1] = pic->linesize[2] = w / 2;
176
+            pic->data[0] = utv->output;
177
+            pic->data[2] = utv->output + (w * h);
178
+            pic->data[1] = pic->data[2] + (w * h / 4);
179
+            break;
180
+        case PIX_FMT_YUYV422:
181
+        case PIX_FMT_UYVY422:
182
+            pic->linesize[0] = w * 2;
183
+            pic->data[0] = utv->output;
184
+            break;
185
+        case PIX_FMT_BGR24:
186
+        case PIX_FMT_RGB32:
187
+            /* Make the linesize negative, since Ut Video uses bottom-up BGR */
188
+            pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
189
+            pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
190
+            break;
191
+     }
192
+
193
+    *data_size = sizeof(AVFrame);
194
+    *(AVFrame *)data = *pic;
195
+
196
+    return avpkt->size;
197
+}
198
+
199
+static av_cold int utvideo_decode_close(AVCodecContext *avctx)
200
+{
201
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
202
+
203
+    /* Free output */
204
+    av_freep(&avctx->coded_frame);
205
+    av_freep(&utv->output);
206
+
207
+    /* Finish decoding and clean up the instance */
208
+    utv->codec->DecodeEnd();
209
+    CCodec::DeleteInstance(utv->codec);
210
+
211
+    return 0;
212
+}
213
+
214
+AVCodec ff_libutvideo_decoder = {
215
+    "utvideo",
216
+    AVMEDIA_TYPE_VIDEO,
217
+    CODEC_ID_UTVIDEO,
218
+    sizeof(UtVideoContext),
219
+    utvideo_decode_init,
220
+    NULL,
221
+    utvideo_decode_close,
222
+    utvideo_decode_frame,
223
+    CODEC_CAP_LOSSLESS,
224
+    NULL,
225
+    NULL,
226
+    NULL,
227
+    NULL,
228
+    NULL_IF_CONFIG_SMALL("Ut Video"),
229
+};
... ...
@@ -21,8 +21,8 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR 20
25
-#define LIBAVCODEC_VERSION_MICRO  1
24
+#define LIBAVCODEC_VERSION_MINOR 21
25
+#define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -281,6 +281,10 @@ const AVCodecTag ff_codec_bmp_tags[] = {
281 281
     { CODEC_ID_G2M,          MKTAG('G', '2', 'M', '3') },
282 282
     { CODEC_ID_G2M,          MKTAG('G', '2', 'M', '4') },
283 283
     { CODEC_ID_AMV,          MKTAG('A', 'M', 'V', 'F') },
284
+    { CODEC_ID_UTVIDEO,      MKTAG('U', 'L', 'Y', '0') },
285
+    { CODEC_ID_UTVIDEO,      MKTAG('U', 'L', 'Y', '2') },
286
+    { CODEC_ID_UTVIDEO,      MKTAG('U', 'L', 'R', 'G') },
287
+    { CODEC_ID_UTVIDEO,      MKTAG('U', 'L', 'R', 'A') },
284 288
     { CODEC_ID_NONE,         0 }
285 289
 };
286 290