Browse code

Theora encoding via libtheora. courtesy of Paul Richards, paul.richards gmail com

Originally committed as revision 7698 to svn://svn.ffmpeg.org/ffmpeg/trunk

Diego Biurrun authored on 2007/01/25 09:16:28
Showing 7 changed files
... ...
@@ -68,6 +68,7 @@ version <next>
68 68
 - Intel Music decoder
69 69
 - Musepack decoder
70 70
 - Flash Screen Video encoder
71
+- Theora encoding via libtheora
71 72
 
72 73
 version 0.4.9-pre1:
73 74
 
... ...
@@ -89,6 +89,7 @@ show_help(){
89 89
   echo "  --enable-mp3lame         enable MP3 encoding via libmp3lame [default=no]"
90 90
   echo "  --enable-libnut          enable NUT support via libnut [default=no]"
91 91
   echo "  --enable-libogg          enable Ogg support via libogg [default=no]"
92
+  echo "  --enable-libtheora       enable Theora support via libtheora [default=no]"
92 93
   echo "  --enable-vorbis          enable Vorbis support via libvorbis [default=no]"
93 94
   echo "  --enable-x264            enable H.264 encoding via x264 [default=no]"
94 95
   echo "  --enable-xvid            enable Xvid encoding via xvidcore [default=no]"
... ...
@@ -444,6 +445,7 @@ CONFIG_LIST='
444 444
     libmp3lame
445 445
     libnut
446 446
     libogg
447
+    libtheora
447 448
     libvorbis
448 449
     memalign_hack
449 450
     mpegaudio_hp
... ...
@@ -514,6 +516,7 @@ dts_decoder_deps="libdts"
514 514
 faac_encoder_deps="libfaac"
515 515
 libgsm_decoder_deps="libgsm"
516 516
 libgsm_encoder_deps="libgsm"
517
+libtheora_encoder_deps="libtheora"
517 518
 mp3lame_encoder_deps="libmp3lame"
518 519
 oggvorbis_decoder_deps="libvorbis"
519 520
 oggvorbis_encoder_deps="libvorbis"
... ...
@@ -651,6 +654,7 @@ libgsm="no"
651 651
 libmp3lame="no"
652 652
 libnut="no"
653 653
 libogg="no"
654
+libtheora="no"
654 655
 libvorbis="no"
655 656
 xvid="no"
656 657
 x264="no"
... ...
@@ -1003,6 +1007,9 @@ for opt do
1003 1003
   --enable-libogg) libogg="yes"
1004 1004
     pkg_requires="$pkg_requires ogg >= 1.1"
1005 1005
   ;;
1006
+  --enable-libtheora) libtheora="yes"
1007
+    pkg_requires="$pkg_requires theora"
1008
+  ;;
1006 1009
   --enable-vorbis) libvorbis="yes"
1007 1010
     pkg_requires="$pkg_requires vorbis vorbisenc"
1008 1011
   ;;
... ...
@@ -1179,6 +1186,10 @@ EOF
1179 1179
     exit 1;
1180 1180
 fi
1181 1181
 
1182
+if test "$libtheora" = "yes" && test "$libogg" = "no"; then
1183
+    die "libogg must be enabled to enable libtheora."
1184
+fi
1185
+
1182 1186
 if test "$libvorbis" = "yes" && test "$libogg" = "no"; then
1183 1187
     die "libogg must be enabled to enable libvorbis."
1184 1188
 fi
... ...
@@ -1532,6 +1543,7 @@ done
1532 1532
 enabled libdts  && require libdts dts.h dts_init -ldts -lm
1533 1533
 enabled libgsm  && require libgsm gsm.h gsm_create -lgsm
1534 1534
 enabled libmp3lame && require LAME lame/lame.h lame_init -lmp3lame -lm
1535
+enabled libtheora && require libtheora theora/theora.h theora_info_init -ltheora -logg
1535 1536
 enabled libvorbis && require libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbis -lvorbisenc -logg
1536 1537
 enabled libogg  && require libogg ogg/ogg.h ogg_sync_init -logg
1537 1538
 enabled libnut  && require libnut libnut.h nut_demuxer_init -lnut
... ...
@@ -1868,6 +1880,7 @@ echo "libgsm enabled            $libgsm"
1868 1868
 echo "libmp3lame enabled        $libmp3lame"
1869 1869
 echo "libnut enabled            $libnut"
1870 1870
 echo "libogg enabled            $libogg"
1871
+echo "libtheora enabled         $libtheora"
1871 1872
 echo "libvorbis enabled         $libvorbis"
1872 1873
 echo "x264 enabled              $x264"
1873 1874
 echo "XviD enabled              $xvid"
... ...
@@ -969,7 +969,7 @@ following image formats are supported:
969 969
 @item On2 VP3                @tab     @tab  X @tab still experimental
970 970
 @item On2 VP5                @tab     @tab  X @tab fourcc: VP50
971 971
 @item On2 VP6                @tab     @tab  X @tab fourcc: VP60,VP61,VP62
972
-@item Theora                 @tab     @tab  X @tab still experimental
972
+@item Theora                 @tab  X  @tab  X @tab still experimental
973 973
 @item Intel Indeo 3          @tab     @tab  X
974 974
 @item FLV                    @tab  X  @tab  X @tab Sorenson H.263 used in Flash
975 975
 @item Flash Screen Video     @tab     @tab  X @tab fourcc: FSV1
... ...
@@ -258,6 +258,7 @@ OBJS-$(CONFIG_LIBFAAC)                 += faac.o
258 258
 OBJS-$(CONFIG_LIBFAAD)                 += faad.o
259 259
 OBJS-$(CONFIG_LIBGSM)                  += libgsm.o
260 260
 OBJS-$(CONFIG_LIBMP3LAME)              += mp3lameaudio.o
261
+OBJS-$(CONFIG_LIBTHEORA)               += libtheoraenc.o
261 262
 OBJS-$(CONFIG_LIBVORBIS)               += oggvorbis.o
262 263
 OBJS-$(CONFIG_X264)                    += x264.o
263 264
 OBJS-$(CONFIG_XVID)                    += xvidff.o xvid_rc.o
... ...
@@ -164,6 +164,7 @@ void avcodec_register_all(void)
164 164
     REGISTER_ENCDEC (FLAC, flac);
165 165
     REGISTER_DECODER(IMC, imc);
166 166
     REGISTER_ENCDEC (LIBGSM, libgsm);
167
+    REGISTER_ENCODER(LIBTHEORA, libtheora);
167 168
     REGISTER_DECODER(MACE3, mace3);
168 169
     REGISTER_DECODER(MACE6, mace6);
169 170
     REGISTER_ENCDEC (MP2, mp2);
... ...
@@ -2162,6 +2162,7 @@ extern AVCodec h264_encoder;
2162 2162
 extern AVCodec huffyuv_encoder;
2163 2163
 extern AVCodec jpegls_encoder;
2164 2164
 extern AVCodec libgsm_encoder;
2165
+extern AVCodec libtheora_encoder;
2165 2166
 extern AVCodec ljpeg_encoder;
2166 2167
 extern AVCodec mdec_encoder;
2167 2168
 extern AVCodec mjpeg_encoder;
2168 2169
new file mode 100644
... ...
@@ -0,0 +1,278 @@
0
+/*
1
+ * Copyright (c) 2006 Paul Richards <paul.richards@gmail.com>
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 Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
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
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+ */
19
+
20
+/*!
21
+ * \file theoraenc.c
22
+ * \brief Theora encoder using libtheora.
23
+ * \author Paul Richards <paul.richards@gmail.com>
24
+ *
25
+ * A lot of this is copy / paste from other output codecs in
26
+ * libavcodec or pure guesswork (or both).
27
+ *
28
+ * I have used t_ prefixes on variables which are libtheora types
29
+ * and o_ prefixes on variables which are libogg types.
30
+ */
31
+
32
+/* FFmpeg includes */
33
+#include "avcodec.h"
34
+#include "log.h"
35
+
36
+/* libtheora includes */
37
+#include <theora/theora.h>
38
+
39
+typedef struct TheoraContext{
40
+    theora_state t_state;
41
+} TheoraContext;
42
+
43
+/*!
44
+    Concatenates an ogg_packet into the extradata.
45
+*/
46
+static int concatenate_packet(unsigned int* offset, AVCodecContext* avc_context, const ogg_packet* packet)
47
+{
48
+    char* message = NULL;
49
+    uint8_t* newdata = NULL;
50
+    int newsize = avc_context->extradata_size + 2 + packet->bytes;
51
+
52
+    if (packet->bytes < 0) {
53
+        message = "ogg_packet has negative size";
54
+    } else if (packet->bytes > 0xffff) {
55
+        message = "ogg_packet is larger than 65535 bytes";
56
+    } else if (newsize < avc_context->extradata_size) {
57
+        message = "extradata_size would overflow";
58
+    } else {
59
+        newdata = av_realloc(avc_context->extradata, newsize);
60
+        if (newdata == NULL) {
61
+            message = "av_realloc failed";
62
+        }
63
+    }
64
+    if (message != NULL) {
65
+        av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
66
+        return -1;
67
+    }
68
+
69
+    avc_context->extradata = newdata;
70
+    avc_context->extradata_size = newsize;
71
+    avc_context->extradata[ (*offset)++ ] = packet->bytes >> 8;
72
+    avc_context->extradata[ (*offset)++ ] = packet->bytes & 0xff;
73
+    memcpy( avc_context->extradata + (*offset), packet->packet, packet->bytes );
74
+    (*offset) += packet->bytes;
75
+    return 0;
76
+}
77
+
78
+static int encode_init(AVCodecContext* avc_context)
79
+{
80
+    theora_info t_info;
81
+    theora_comment t_comment;
82
+    ogg_packet o_packet;
83
+    unsigned int offset;
84
+    TheoraContext *h = avc_context->priv_data;
85
+
86
+    /* Set up the theora_info struct */
87
+    theora_info_init( &t_info );
88
+    t_info.width = avc_context->width;
89
+    t_info.height = avc_context->height;
90
+    t_info.frame_width = avc_context->width;
91
+    t_info.frame_height = avc_context->height;
92
+    t_info.offset_x = 0;
93
+    t_info.offset_y = 0;
94
+    t_info.fps_numerator = avc_context->time_base.den;
95
+    t_info.fps_denominator = avc_context->time_base.num;
96
+    if (avc_context->sample_aspect_ratio.num != 0) {
97
+        t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
98
+        t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
99
+    } else {
100
+        t_info.aspect_numerator = 1;
101
+        t_info.aspect_denominator = 1;
102
+    }
103
+    t_info.colorspace = OC_CS_UNSPECIFIED;
104
+    t_info.pixelformat = OC_PF_420;
105
+    t_info.target_bitrate = avc_context->bit_rate;
106
+    t_info.keyframe_frequency = avc_context->gop_size;
107
+    t_info.keyframe_frequency_force = avc_context->gop_size;
108
+    t_info.keyframe_mindistance = avc_context->keyint_min;
109
+    t_info.quality = 0;
110
+
111
+    t_info.quick_p = 1;
112
+    t_info.dropframes_p = 0;
113
+    t_info.keyframe_auto_p = 1;
114
+    t_info.keyframe_data_target_bitrate = t_info.target_bitrate * 1.5;
115
+    t_info.keyframe_auto_threshold = 80;
116
+    t_info.noise_sensitivity = 1;
117
+    t_info.sharpness = 0;
118
+
119
+    /* Now initialise libtheora */
120
+    if (theora_encode_init( &(h->t_state), &t_info ) != 0) {
121
+        av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
122
+        return -1;
123
+    }
124
+
125
+    /* Clear up theora_info struct */
126
+    theora_info_clear( &t_info );
127
+
128
+    /*
129
+        Output first header packet consisting of theora
130
+        header, comment, and tables.
131
+
132
+        Each one is prefixed with a 16bit size, then they
133
+        are concatenated together into ffmpeg's extradata.
134
+    */
135
+    offset = 0;
136
+
137
+    /* Header */
138
+    theora_encode_header( &(h->t_state), &o_packet );
139
+    if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
140
+        return -1;
141
+    }
142
+
143
+    /* Comment */
144
+    theora_comment_init( &t_comment );
145
+    theora_encode_comment( &t_comment, &o_packet );
146
+    if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
147
+        return -1;
148
+    }
149
+
150
+    /* Tables */
151
+    theora_encode_tables( &(h->t_state), &o_packet );
152
+    if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
153
+        return -1;
154
+    }
155
+
156
+    /* Clear up theora_comment struct */
157
+    theora_comment_clear( &t_comment );
158
+
159
+    /* Set up the output AVFrame */
160
+    avc_context->coded_frame= avcodec_alloc_frame();
161
+
162
+    return 0;
163
+}
164
+
165
+static int encode_frame(
166
+    AVCodecContext* avc_context,
167
+    uint8_t *outbuf,
168
+    int buf_size,
169
+    void *data)
170
+{
171
+    yuv_buffer t_yuv_buffer;
172
+    TheoraContext *h = avc_context->priv_data;
173
+    AVFrame *frame = data;
174
+    ogg_packet o_packet;
175
+    int result;
176
+
177
+    assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
178
+
179
+    /* Copy planes to the theora yuv_buffer */
180
+    if (frame->linesize[1] != frame->linesize[2]) {
181
+        av_log(avc_context, AV_LOG_ERROR, "U and V stride differ\n");
182
+        return -1;
183
+    }
184
+
185
+    t_yuv_buffer.y_width = avc_context->width;
186
+    t_yuv_buffer.y_height = avc_context->height;
187
+    t_yuv_buffer.y_stride = frame->linesize[0];
188
+    t_yuv_buffer.uv_width = t_yuv_buffer.y_width / 2;
189
+    t_yuv_buffer.uv_height = t_yuv_buffer.y_height / 2;
190
+    t_yuv_buffer.uv_stride = frame->linesize[1];
191
+
192
+    t_yuv_buffer.y = frame->data[0];
193
+    t_yuv_buffer.u = frame->data[1];
194
+    t_yuv_buffer.v = frame->data[2];
195
+
196
+    /* Now call into theora_encode_YUVin */
197
+    result = theora_encode_YUVin( &(h->t_state), &t_yuv_buffer );
198
+    if (result != 0) {
199
+        const char* message;
200
+        switch (result) {
201
+            case -1:
202
+                message = "differing frame sizes";
203
+                break;
204
+            case OC_EINVAL:
205
+                message = "encoder is not ready or is finished";
206
+                break;
207
+            default:
208
+                message = "unknown reason";
209
+                break;
210
+        }
211
+        av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
212
+        return -1;
213
+    }
214
+
215
+    /* Pick up returned ogg_packet */
216
+    result = theora_encode_packetout( &(h->t_state), 0, &o_packet );
217
+    switch (result) {
218
+        case 0:
219
+            /* No packet is ready */
220
+            return 0;
221
+        case 1:
222
+            /* Success, we have a packet */
223
+            break;
224
+        default:
225
+            av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
226
+            return -1;
227
+    }
228
+
229
+    /* Copy ogg_packet content out to buffer */
230
+    if (buf_size < o_packet.bytes) {
231
+        av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
232
+        return -1;
233
+    }
234
+    memcpy(outbuf, o_packet.packet, o_packet.bytes);
235
+
236
+    return o_packet.bytes;
237
+}
238
+
239
+static int encode_close(AVCodecContext* avc_context)
240
+{
241
+    ogg_packet o_packet;
242
+    TheoraContext *h = avc_context->priv_data;
243
+    int result;
244
+    const char* message;
245
+
246
+    result = theora_encode_packetout( &(h->t_state), 1, &o_packet );
247
+    theora_clear( &(h->t_state) );
248
+    switch (result) {
249
+        case 0:/* No packet is ready */
250
+        case -1:/* Encoding finished */
251
+            return 0;
252
+        case 1:
253
+            /* We have a packet */
254
+            message = "gave us a packet";
255
+            break;
256
+        default:
257
+            message = "unknown reason";
258
+            break;
259
+    }
260
+    av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
261
+    return -1;
262
+}
263
+
264
+static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, -1 };
265
+
266
+/*! AVCodec struct exposed to libavcodec */
267
+AVCodec libtheora_encoder =
268
+{
269
+    .name = "libtheora",
270
+    .type = CODEC_TYPE_VIDEO,
271
+    .id = CODEC_ID_THEORA,
272
+    .priv_data_size = sizeof(TheoraContext),
273
+    .init = encode_init,
274
+    .close = encode_close,
275
+    .encode = encode_frame,
276
+    .pix_fmts = supported_pixel_formats,
277
+};