Browse code

libutvideo: Add Ut Video encoder wrapper

All colorspaces are supported.

Renamed libutvideo.cpp to libutvideodec.cpp.

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

Derek Buitenhuis authored on 2012/03/06 05:29:23
Showing 10 changed files
... ...
@@ -12,6 +12,7 @@ version next:
12 12
 - WMA Lossless decoder
13 13
 - bluray protocol
14 14
 - blackdetect filter
15
+- libutvideo encoder wrapper (--enable-libutvideo)
15 16
 
16 17
 
17 18
 version 0.10:
... ...
@@ -171,6 +171,7 @@ Codecs:
171 171
   libschroedinger*                      David Conrad
172 172
   libspeexdec.c                         Justin Ruggles
173 173
   libtheoraenc.c                        David Conrad
174
+  libutvideo*                           Derek Buitenhuis
174 175
   libvorbis.c                           David Conrad
175 176
   libxavs.c                             Stefan Gehrer
176 177
   libx264.c                             Mans Rullgard, Jason Garrett-Glaser
... ...
@@ -194,7 +194,7 @@ External library support:
194 194
   --enable-libspeex        enable Speex support via libspeex [no]
195 195
   --enable-libstagefright-h264  enable H.264 decoding via libstagefright [no]
196 196
   --enable-libtheora       enable Theora encoding via libtheora [no]
197
-  --enable-libutvideo      enable Ut Video decoding via libutvideo [no]
197
+  --enable-libutvideo      enable Ut Video encoding and decoding via libutvideo [no]
198 198
   --enable-libv4l2         enable libv4l2/v4l-utils [no]
199 199
   --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
200 200
   --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
... ...
@@ -1575,6 +1575,7 @@ libx264rgb_encoder_deps="libx264"
1575 1575
 libxavs_encoder_deps="libxavs"
1576 1576
 libxvid_encoder_deps="libxvid"
1577 1577
 libutvideo_decoder_deps="libutvideo gpl"
1578
+libutvideo_encoder_deps="libutvideo gpl"
1578 1579
 
1579 1580
 # demuxers / muxers
1580 1581
 ac3_demuxer_select="ac3_parser"
... ...
@@ -647,7 +647,8 @@ OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
647 647
 OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o
648 648
 OBJS-$(CONFIG_LIBSTAGEFRIGHT_H264_DECODER)+= libstagefright.o
649 649
 OBJS-$(CONFIG_LIBTHEORA_ENCODER)          += libtheoraenc.o
650
-OBJS-$(CONFIG_LIBUTVIDEO_DECODER)         += libutvideo.o
650
+OBJS-$(CONFIG_LIBUTVIDEO_DECODER)         += libutvideodec.o
651
+OBJS-$(CONFIG_LIBUTVIDEO_ENCODER)         += libutvideoenc.o
651 652
 OBJS-$(CONFIG_LIBVO_AACENC_ENCODER)       += libvo-aacenc.o mpeg4audio.o
652 653
 OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER)     += libvo-amrwbenc.o
653 654
 OBJS-$(CONFIG_LIBVORBIS_ENCODER)          += libvorbis.o vorbis_data.o
... ...
@@ -412,7 +412,7 @@ void avcodec_register_all(void)
412 412
     REGISTER_ENCDEC  (LIBSPEEX, libspeex);
413 413
     REGISTER_DECODER (LIBSTAGEFRIGHT_H264, libstagefright_h264);
414 414
     REGISTER_ENCODER (LIBTHEORA, libtheora);
415
-    REGISTER_DECODER (LIBUTVIDEO, libutvideo);
415
+    REGISTER_ENCDEC  (LIBUTVIDEO, libutvideo);
416 416
     REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc);
417 417
     REGISTER_ENCODER (LIBVO_AMRWBENC, libvo_amrwbenc);
418 418
     REGISTER_ENCODER (LIBVORBIS, libvorbis);
419 419
deleted file mode 100644
... ...
@@ -1,199 +0,0 @@
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 "libutvideo.h"
32
-#include "get_bits.h"
33
-
34
-static av_cold int utvideo_decode_init(AVCodecContext *avctx)
35
-{
36
-    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
37
-    UtVideoExtra info;
38
-    int format;
39
-    int begin_ret;
40
-
41
-    if (avctx->extradata_size != 4*4) {
42
-        av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
43
-        return -1;
44
-    }
45
-
46
-    /* Read extradata */
47
-    info.version = AV_RL32(avctx->extradata);
48
-    info.original_format = AV_RL32(avctx->extradata + 4);
49
-    info.frameinfo_size = AV_RL32(avctx->extradata + 8);
50
-    info.flags = AV_RL32(avctx->extradata + 12);
51
-
52
-    /* Pick format based on FOURCC */
53
-    switch (avctx->codec_tag) {
54
-    case MKTAG('U', 'L', 'Y', '0'):
55
-        avctx->pix_fmt = PIX_FMT_YUV420P;
56
-        format = UTVF_YV12;
57
-        break;
58
-    case MKTAG('U', 'L', 'Y', '2'):
59
-        avctx->pix_fmt = PIX_FMT_YUYV422;
60
-        format = UTVF_YUY2;
61
-        break;
62
-    case MKTAG('U', 'L', 'R', 'G'):
63
-        avctx->pix_fmt = PIX_FMT_BGR24;
64
-        format = UTVF_RGB24_WIN;
65
-        break;
66
-    case MKTAG('U', 'L', 'R', 'A'):
67
-        avctx->pix_fmt = PIX_FMT_RGB32;
68
-        format = UTVF_RGB32_WIN;
69
-        break;
70
-    default:
71
-        av_log(avctx, AV_LOG_ERROR,
72
-              "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
73
-        return -1;
74
-    }
75
-
76
-    /* Only allocate the buffer once */
77
-    utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
78
-    utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
79
-
80
-    if (utv->buffer == NULL) {
81
-        av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
82
-        return -1;
83
-    }
84
-
85
-    /* Allocate the output frame */
86
-    avctx->coded_frame = avcodec_alloc_frame();
87
-
88
-    /* Ut Video only supports 8-bit */
89
-    avctx->bits_per_raw_sample = 8;
90
-
91
-    /* Is it interlaced? */
92
-    avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
93
-
94
-    /* Apparently Ut Video doesn't store this info... */
95
-    avctx->coded_frame->top_field_first = 1;
96
-
97
-    /*
98
-     * Create a Ut Video instance. Since the function wants
99
-     * an "interface name" string, pass it the name of the lib.
100
-     */
101
-    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
102
-
103
-    /* Initialize Decoding */
104
-    begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
105
-                            CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
106
-
107
-    /* Check to see if the decoder initlized properly */
108
-    if (begin_ret != 0) {
109
-        av_log(avctx, AV_LOG_ERROR,
110
-               "Could not initialize decoder: %d\n", begin_ret);
111
-        return -1;
112
-    }
113
-
114
-    return 0;
115
-}
116
-
117
-static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
118
-                                int *data_size, AVPacket *avpkt)
119
-{
120
-    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
121
-    AVFrame *pic = avctx->coded_frame;
122
-    int w = avctx->width, h = avctx->height;
123
-
124
-    /* Set flags */
125
-    pic->reference = 0;
126
-    pic->pict_type = AV_PICTURE_TYPE_I;
127
-    pic->key_frame = 1;
128
-
129
-    /* Decode the frame */
130
-    utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
131
-
132
-    /* Set the output data depending on the colorspace */
133
-    switch (avctx->pix_fmt) {
134
-    case PIX_FMT_YUV420P:
135
-        pic->linesize[0] = w;
136
-        pic->linesize[1] = pic->linesize[2] = w / 2;
137
-        pic->data[0] = utv->buffer;
138
-        pic->data[2] = utv->buffer + (w * h);
139
-        pic->data[1] = pic->data[2] + (w * h / 4);
140
-        break;
141
-    case PIX_FMT_YUYV422:
142
-        pic->linesize[0] = w * 2;
143
-        pic->data[0] = utv->buffer;
144
-        break;
145
-    case PIX_FMT_BGR24:
146
-    case PIX_FMT_RGB32:
147
-        /* Make the linesize negative, since Ut Video uses bottom-up BGR */
148
-        pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
149
-        pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
150
-        break;
151
-    }
152
-
153
-    *data_size = sizeof(AVFrame);
154
-    *(AVFrame *)data = *pic;
155
-
156
-    return avpkt->size;
157
-}
158
-
159
-static av_cold int utvideo_decode_close(AVCodecContext *avctx)
160
-{
161
-    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
162
-
163
-    /* Free output */
164
-    av_freep(&avctx->coded_frame);
165
-    av_freep(&utv->buffer);
166
-
167
-    /* Finish decoding and clean up the instance */
168
-    utv->codec->DecodeEnd();
169
-    CCodec::DeleteInstance(utv->codec);
170
-
171
-    return 0;
172
-}
173
-
174
-AVCodec ff_libutvideo_decoder = {
175
-    "libutvideo",
176
-    NULL_IF_CONFIG_SMALL("Ut Video"),
177
-    AVMEDIA_TYPE_VIDEO,
178
-    CODEC_ID_UTVIDEO,
179
-    0,    //capabilities
180
-    NULL, //supported_framerates
181
-    NULL, //pix_fmts
182
-    NULL, //supported_samplerates
183
-    NULL, //sample_fmts
184
-    NULL, //channel_layouts
185
-    0,    //max_lowres
186
-    NULL, //priv_class
187
-    NULL, //profiles
188
-    sizeof(UtVideoContext),
189
-    NULL, //next
190
-    NULL, //init_thread_copy
191
-    NULL, //update_thread_context
192
-    NULL, //defaults
193
-    NULL, //init_static_data
194
-    utvideo_decode_init,
195
-    NULL, //encode
196
-    NULL, //encode2
197
-    utvideo_decode_frame,
198
-    utvideo_decode_close,
199
-};
200 1
new file mode 100644
... ...
@@ -0,0 +1,199 @@
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 "libutvideo.h"
31
+#include "get_bits.h"
32
+
33
+static av_cold int utvideo_decode_init(AVCodecContext *avctx)
34
+{
35
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
36
+    UtVideoExtra info;
37
+    int format;
38
+    int begin_ret;
39
+
40
+    if (avctx->extradata_size != 4*4) {
41
+        av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
42
+        return -1;
43
+    }
44
+
45
+    /* Read extradata */
46
+    info.version = AV_RL32(avctx->extradata);
47
+    info.original_format = AV_RL32(avctx->extradata + 4);
48
+    info.frameinfo_size = AV_RL32(avctx->extradata + 8);
49
+    info.flags = AV_RL32(avctx->extradata + 12);
50
+
51
+    /* Pick format based on FOURCC */
52
+    switch (avctx->codec_tag) {
53
+    case MKTAG('U', 'L', 'Y', '0'):
54
+        avctx->pix_fmt = PIX_FMT_YUV420P;
55
+        format = UTVF_YV12;
56
+        break;
57
+    case MKTAG('U', 'L', 'Y', '2'):
58
+        avctx->pix_fmt = PIX_FMT_YUYV422;
59
+        format = UTVF_YUY2;
60
+        break;
61
+    case MKTAG('U', 'L', 'R', 'G'):
62
+        avctx->pix_fmt = PIX_FMT_BGR24;
63
+        format = UTVF_RGB24_WIN;
64
+        break;
65
+    case MKTAG('U', 'L', 'R', 'A'):
66
+        avctx->pix_fmt = PIX_FMT_RGB32;
67
+        format = UTVF_RGB32_WIN;
68
+        break;
69
+    default:
70
+        av_log(avctx, AV_LOG_ERROR,
71
+              "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
72
+        return -1;
73
+    }
74
+
75
+    /* Only allocate the buffer once */
76
+    utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
77
+    utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
78
+
79
+    if (utv->buffer == NULL) {
80
+        av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
81
+        return -1;
82
+    }
83
+
84
+    /* Allocate the output frame */
85
+    avctx->coded_frame = avcodec_alloc_frame();
86
+
87
+    /* Ut Video only supports 8-bit */
88
+    avctx->bits_per_raw_sample = 8;
89
+
90
+    /* Is it interlaced? */
91
+    avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
92
+
93
+    /* Apparently Ut Video doesn't store this info... */
94
+    avctx->coded_frame->top_field_first = 1;
95
+
96
+    /*
97
+     * Create a Ut Video instance. Since the function wants
98
+     * an "interface name" string, pass it the name of the lib.
99
+     */
100
+    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
101
+
102
+    /* Initialize Decoding */
103
+    begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
104
+                            CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
105
+
106
+    /* Check to see if the decoder initlized properly */
107
+    if (begin_ret != 0) {
108
+        av_log(avctx, AV_LOG_ERROR,
109
+               "Could not initialize decoder: %d\n", begin_ret);
110
+        return -1;
111
+    }
112
+
113
+    return 0;
114
+}
115
+
116
+static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
117
+                                int *data_size, AVPacket *avpkt)
118
+{
119
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
120
+    AVFrame *pic = avctx->coded_frame;
121
+    int w = avctx->width, h = avctx->height;
122
+
123
+    /* Set flags */
124
+    pic->reference = 0;
125
+    pic->pict_type = AV_PICTURE_TYPE_I;
126
+    pic->key_frame = 1;
127
+
128
+    /* Decode the frame */
129
+    utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
130
+
131
+    /* Set the output data depending on the colorspace */
132
+    switch (avctx->pix_fmt) {
133
+    case PIX_FMT_YUV420P:
134
+        pic->linesize[0] = w;
135
+        pic->linesize[1] = pic->linesize[2] = w / 2;
136
+        pic->data[0] = utv->buffer;
137
+        pic->data[2] = utv->buffer + (w * h);
138
+        pic->data[1] = pic->data[2] + (w * h / 4);
139
+        break;
140
+    case PIX_FMT_YUYV422:
141
+        pic->linesize[0] = w * 2;
142
+        pic->data[0] = utv->buffer;
143
+        break;
144
+    case PIX_FMT_BGR24:
145
+    case PIX_FMT_RGB32:
146
+        /* Make the linesize negative, since Ut Video uses bottom-up BGR */
147
+        pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
148
+        pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
149
+        break;
150
+    }
151
+
152
+    *data_size = sizeof(AVFrame);
153
+    *(AVFrame *)data = *pic;
154
+
155
+    return avpkt->size;
156
+}
157
+
158
+static av_cold int utvideo_decode_close(AVCodecContext *avctx)
159
+{
160
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
161
+
162
+    /* Free output */
163
+    av_freep(&avctx->coded_frame);
164
+    av_freep(&utv->buffer);
165
+
166
+    /* Finish decoding and clean up the instance */
167
+    utv->codec->DecodeEnd();
168
+    CCodec::DeleteInstance(utv->codec);
169
+
170
+    return 0;
171
+}
172
+
173
+AVCodec ff_libutvideo_decoder = {
174
+    "libutvideo",
175
+    NULL_IF_CONFIG_SMALL("Ut Video"),
176
+    AVMEDIA_TYPE_VIDEO,
177
+    CODEC_ID_UTVIDEO,
178
+    0,    //capabilities
179
+    NULL, //supported_framerates
180
+    NULL, //pix_fmts
181
+    NULL, //supported_samplerates
182
+    NULL, //sample_fmts
183
+    NULL, //channel_layouts
184
+    0,    //max_lowres
185
+    NULL, //priv_class
186
+    NULL, //profiles
187
+    sizeof(UtVideoContext),
188
+    NULL, //next
189
+    NULL, //init_thread_copy
190
+    NULL, //update_thread_context
191
+    NULL, //defaults
192
+    NULL, //init_static_data
193
+    utvideo_decode_init,
194
+    NULL, //encode
195
+    NULL, //encode2
196
+    utvideo_decode_frame,
197
+    utvideo_decode_close,
198
+};
0 199
new file mode 100644
... ...
@@ -0,0 +1,241 @@
0
+/*
1
+ * Copyright (c) 2012 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 "libavutil/avassert.h"
28
+#include "avcodec.h"
29
+#include "internal.h"
30
+}
31
+
32
+#include "libutvideo.h"
33
+#include "put_bits.h"
34
+
35
+static av_cold int utvideo_encode_init(AVCodecContext *avctx)
36
+{
37
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
38
+    UtVideoExtra *info;
39
+    uint32_t flags, in_format;
40
+
41
+    switch (avctx->pix_fmt) {
42
+    case PIX_FMT_YUV420P:
43
+        in_format = UTVF_YV12;
44
+        avctx->bits_per_coded_sample = 12;
45
+        avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
46
+        break;
47
+    case PIX_FMT_YUYV422:
48
+        in_format = UTVF_YUYV;
49
+        avctx->bits_per_coded_sample = 16;
50
+        avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
51
+        break;
52
+    case PIX_FMT_BGR24:
53
+        in_format = UTVF_RGB24_WIN;
54
+        avctx->bits_per_coded_sample = 24;
55
+        avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
56
+        break;
57
+    case PIX_FMT_RGB32:
58
+        in_format = UTVF_RGB32_WIN;
59
+        avctx->bits_per_coded_sample = 32;
60
+        avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
61
+        break;
62
+    default:
63
+        return AVERROR(EINVAL);
64
+    }
65
+
66
+    /* Check before we alloc anything */
67
+    if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
68
+        av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
69
+        return AVERROR(EINVAL);
70
+    }
71
+
72
+    flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
73
+
74
+    avctx->priv_data = utv;
75
+    avctx->coded_frame = avcodec_alloc_frame();
76
+
77
+    /* Alloc extradata buffer */
78
+    info = (UtVideoExtra *)av_malloc(sizeof(*info));
79
+
80
+    if (info == NULL) {
81
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
82
+        return AVERROR(ENOMEM);
83
+    }
84
+
85
+    /*
86
+     * We use this buffer to hold the data that Ut Video returns,
87
+     * since we cannot decode planes separately with it.
88
+     */
89
+    utv->buf_size = avpicture_get_size(avctx->pix_fmt,
90
+                                       avctx->width, avctx->height);
91
+    utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
92
+
93
+    if (utv->buffer == NULL) {
94
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
95
+        return AVERROR(ENOMEM);
96
+    }
97
+
98
+    /*
99
+     * Create a Ut Video instance. Since the function wants
100
+     * an "interface name" string, pass it the name of the lib.
101
+     */
102
+    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
103
+
104
+    /* Initialize encoder */
105
+    utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
106
+                            CBGROSSWIDTH_WINDOWS);
107
+
108
+    /* Get extradata from encoder */
109
+    avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
110
+    utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
111
+                                   avctx->width, avctx->height,
112
+                                   CBGROSSWIDTH_WINDOWS);
113
+    avctx->extradata = (uint8_t *)info;
114
+
115
+    /* Set flags */
116
+    utv->codec->SetState(&flags, sizeof(flags));
117
+
118
+    return 0;
119
+}
120
+
121
+static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
122
+                                const AVFrame *pic, int *got_packet)
123
+{
124
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
125
+    int w = avctx->width, h = avctx->height;
126
+    int ret, rgb_size, i;
127
+    bool keyframe;
128
+    uint8_t *y, *u, *v;
129
+    uint8_t *dst;
130
+
131
+    /* Alloc buffer */
132
+    if ((ret = ff_alloc_packet(pkt, utv->buf_size)) < 0) {
133
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
134
+        return ret;
135
+    }
136
+
137
+    dst = pkt->data;
138
+
139
+    /* Move input if needed data into Ut Video friendly buffer */
140
+    switch (avctx->pix_fmt) {
141
+    case PIX_FMT_YUV420P:
142
+        y = utv->buffer;
143
+        u = y + w * h;
144
+        v = u + w * h / 4;
145
+        for (i = 0; i < h; i++) {
146
+            memcpy(y, pic->data[0] + i * pic->linesize[0], w);
147
+            y += w;
148
+        }
149
+        for (i = 0; i < h / 2; i++) {
150
+            memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
151
+            memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
152
+            u += w >> 1;
153
+            v += w >> 1;
154
+        }
155
+        break;
156
+    case PIX_FMT_YUYV422:
157
+        for (i = 0; i < h; i++)
158
+            memcpy(utv->buffer + i * (w << 1),
159
+                   pic->data[0] + i * pic->linesize[0], w << 1);
160
+        break;
161
+    case PIX_FMT_BGR24:
162
+    case PIX_FMT_RGB32:
163
+        /* Ut Video takes bottom-up BGR */
164
+        rgb_size = avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4;
165
+        for (i = 0; i < h; i++)
166
+            memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
167
+                   pic->data[0] + i * pic->linesize[0],
168
+                   w * rgb_size);
169
+        break;
170
+    default:
171
+        return AVERROR(EINVAL);
172
+    }
173
+
174
+    /* Encode frame */
175
+    pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
176
+
177
+    if (!pkt->size) {
178
+        av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
179
+        return AVERROR_INVALIDDATA;
180
+    }
181
+
182
+    /*
183
+     * Ut Video is intra-only and every frame is a keyframe,
184
+     * and the API always returns true. In case something
185
+     * durastic changes in the future, such as inter support,
186
+     * assert that this is true.
187
+     */
188
+    av_assert2(keyframe == true);
189
+    avctx->coded_frame->key_frame = 1;
190
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
191
+
192
+    pkt->flags |= AV_PKT_FLAG_KEY;
193
+    *got_packet = 1;
194
+    return 0;
195
+}
196
+
197
+static av_cold int utvideo_encode_close(AVCodecContext *avctx)
198
+{
199
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
200
+
201
+    av_freep(&avctx->coded_frame);
202
+    av_freep(&avctx->extradata);
203
+    av_freep(&utv->buffer);
204
+
205
+    utv->codec->EncodeEnd();
206
+    CCodec::DeleteInstance(utv->codec);
207
+
208
+    return 0;
209
+}
210
+
211
+AVCodec ff_libutvideo_encoder = {
212
+    "libutvideo",
213
+    NULL_IF_CONFIG_SMALL("Ut Video"),
214
+    AVMEDIA_TYPE_VIDEO,
215
+    CODEC_ID_UTVIDEO,
216
+    CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
217
+    NULL, /* supported_framerates */
218
+    (const enum PixelFormat[]) {
219
+        PIX_FMT_YUV420P, PIX_FMT_YUYV422, PIX_FMT_BGR24,
220
+        PIX_FMT_RGB32, PIX_FMT_NONE
221
+    },
222
+    NULL, /* supported_samplerates */
223
+    NULL, /* sample_fmts */
224
+    NULL, /* channel_layouts */
225
+    0,    /* max_lowres */
226
+    NULL, /* priv_class */
227
+    NULL, /* profiles */
228
+    sizeof(UtVideoContext),
229
+    NULL, /* next */
230
+    NULL, /* init_thread_copy */
231
+    NULL, /* update_thread_context */
232
+    NULL, /* defaults */
233
+    NULL, /* init_static_data */
234
+    utvideo_encode_init,
235
+    NULL, /* encode */
236
+    utvideo_encode_frame,
237
+    NULL, /* decode */
238
+    utvideo_encode_close,
239
+    NULL, /* flush */
240
+};
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 54
24
-#define LIBAVCODEC_VERSION_MINOR  8
24
+#define LIBAVCODEC_VERSION_MINOR  9
25 25
 #define LIBAVCODEC_VERSION_MICRO 100
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -852,6 +852,7 @@ static int is_intra_only(AVCodecContext *enc){
852 852
         case CODEC_ID_VCR1:
853 853
         case CODEC_ID_DNXHD:
854 854
         case CODEC_ID_JPEG2000:
855
+        case CODEC_ID_UTVIDEO:
855 856
             return 1;
856 857
         default: break;
857 858
         }