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