OSDN Git Service

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