]> git.sesse.net Git - ffmpeg/blob - libavcodec/libutvideoenc.cpp
Merge commit 'cc602061ee860b041013397e27a036b85cd87b09'
[ffmpeg] / libavcodec / libutvideoenc.cpp
1 /*
2  * Copyright (c) 2012 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 "libavutil/avassert.h"
30 #include "libavutil/imgutils.h"
31 #include "avcodec.h"
32 #include "internal.h"
33 }
34
35 #include "libutvideo.h"
36 #include "put_bits.h"
37
38 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
39 {
40     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
41     UtVideoExtra *info;
42     uint32_t flags, in_format;
43     int ret;
44
45     switch (avctx->pix_fmt) {
46     case AV_PIX_FMT_YUV420P:
47         in_format = UTVF_YV12;
48         avctx->bits_per_coded_sample = 12;
49         if (avctx->colorspace == AVCOL_SPC_BT709)
50             avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
51         else
52             avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
53         break;
54     case AV_PIX_FMT_YUYV422:
55         in_format = UTVF_YUYV;
56         avctx->bits_per_coded_sample = 16;
57         if (avctx->colorspace == AVCOL_SPC_BT709)
58             avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
59         else
60             avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
61         break;
62     case AV_PIX_FMT_BGR24:
63         in_format = UTVF_NFCC_BGR_BU;
64         avctx->bits_per_coded_sample = 24;
65         avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
66         break;
67     case AV_PIX_FMT_RGB32:
68         in_format = UTVF_NFCC_BGRA_BU;
69         avctx->bits_per_coded_sample = 32;
70         avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
71         break;
72     default:
73         return AVERROR(EINVAL);
74     }
75
76 #if FF_API_PRIVATE_OPT
77 FF_DISABLE_DEPRECATION_WARNINGS
78     if (avctx->prediction_method)
79         utv->pred = avctx->prediction_method;
80 FF_ENABLE_DEPRECATION_WARNINGS
81 #endif
82
83     /* Check before we alloc anything */
84     if (utv->pred != 0 && utv->pred != 2) {
85         av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
86         return AVERROR(EINVAL);
87     }
88
89     flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
90
91     avctx->priv_data = utv;
92
93     /* Alloc extradata buffer */
94     info = (UtVideoExtra *)av_malloc(sizeof(*info));
95
96     if (!info) {
97         av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
98         return AVERROR(ENOMEM);
99     }
100
101     /*
102      * We use this buffer to hold the data that Ut Video returns,
103      * since we cannot decode planes separately with it.
104      */
105     ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
106     if (ret < 0) {
107         av_free(info);
108         return ret;
109     }
110     utv->buf_size = ret;
111
112     utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
113
114     if (utv->buffer == NULL) {
115         av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
116         av_free(info);
117         return AVERROR(ENOMEM);
118     }
119
120     /*
121      * Create a Ut Video instance. Since the function wants
122      * an "interface name" string, pass it the name of the lib.
123      */
124     utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
125
126     /* Initialize encoder */
127     utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
128                             CBGROSSWIDTH_WINDOWS);
129
130     /* Get extradata from encoder */
131     avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
132     utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
133                                    avctx->width, avctx->height,
134                                    CBGROSSWIDTH_WINDOWS);
135     avctx->extradata = (uint8_t *)info;
136
137     /* Set flags */
138     utv->codec->SetState(&flags, sizeof(flags));
139
140     return 0;
141 }
142
143 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
144                                 const AVFrame *pic, int *got_packet)
145 {
146     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
147     int w = avctx->width, h = avctx->height;
148     int ret, rgb_size, i;
149     bool keyframe;
150     uint8_t *y, *u, *v;
151     uint8_t *dst;
152
153     /* Alloc buffer */
154     if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size, 0)) < 0)
155         return ret;
156
157     dst = pkt->data;
158
159     /* Move input if needed data into Ut Video friendly buffer */
160     switch (avctx->pix_fmt) {
161     case AV_PIX_FMT_YUV420P:
162         y = utv->buffer;
163         u = y + w * h;
164         v = u + w * h / 4;
165         for (i = 0; i < h; i++) {
166             memcpy(y, pic->data[0] + i * pic->linesize[0], w);
167             y += w;
168         }
169         for (i = 0; i < h / 2; i++) {
170             memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
171             memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
172             u += w >> 1;
173             v += w >> 1;
174         }
175         break;
176     case AV_PIX_FMT_YUYV422:
177         for (i = 0; i < h; i++)
178             memcpy(utv->buffer + i * (w << 1),
179                    pic->data[0] + i * pic->linesize[0], w << 1);
180         break;
181     case AV_PIX_FMT_BGR24:
182     case AV_PIX_FMT_RGB32:
183         /* Ut Video takes bottom-up BGR */
184         rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
185         for (i = 0; i < h; i++)
186             memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
187                    pic->data[0] + i * pic->linesize[0],
188                    w * rgb_size);
189         break;
190     default:
191         return AVERROR(EINVAL);
192     }
193
194     /* Encode frame */
195     pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
196
197     if (!pkt->size) {
198         av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
199         return AVERROR_INVALIDDATA;
200     }
201
202     /*
203      * Ut Video is intra-only and every frame is a keyframe,
204      * and the API always returns true. In case something
205      * durastic changes in the future, such as inter support,
206      * assert that this is true.
207      */
208     av_assert2(keyframe == true);
209
210     pkt->flags |= AV_PKT_FLAG_KEY;
211     *got_packet = 1;
212     return 0;
213 }
214
215 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
216 {
217     UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
218
219     av_freep(&avctx->extradata);
220     av_freep(&utv->buffer);
221
222     utv->codec->EncodeEnd();
223     CCodec::DeleteInstance(utv->codec);
224
225     return 0;
226 }
227
228 #define OFFSET(x) offsetof(UtVideoContext, x)
229 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
230 static const AVOption options[] = {
231     { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, 0, 0, 2, VE, "pred" },
232     { "left",   NULL, 0, AV_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, VE, "pred" },
233     { "median",   NULL, 0, AV_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, VE, "pred" },
234 };
235
236 static const AVClass utvideo_class = {
237     "libutvideo",
238     av_default_item_name,
239     options.
240     LIBAVUTIL_VERSION,
241     0,
242     0,
243     NULL,
244     NULL,
245     0,
246     NULL,
247     NULL,
248 };
249
250 AVCodec ff_libutvideo_encoder = {
251     "libutvideo",
252     NULL_IF_CONFIG_SMALL("Ut Video"),
253     AVMEDIA_TYPE_VIDEO,
254     AV_CODEC_ID_UTVIDEO,
255     AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_LOSSLESS | AV_CODEC_CAP_INTRA_ONLY,
256     NULL, /* supported_framerates */
257     (const enum AVPixelFormat[]) {
258         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
259         AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
260     },
261     NULL, /* supported_samplerates */
262     NULL, /* sample_fmts */
263     NULL, /* channel_layouts */
264     0,    /* max_lowres */
265     &utvideo_class, /* priv_class */
266     NULL, /* profiles */
267     sizeof(UtVideoContext),
268     NULL, /* next */
269     NULL, /* init_thread_copy */
270     NULL, /* update_thread_context */
271     NULL, /* defaults */
272     NULL, /* init_static_data */
273     utvideo_encode_init,
274     NULL, /* encode */
275     utvideo_encode_frame,
276     NULL, /* decode */
277     utvideo_encode_close,
278     NULL, /* flush */
279 };