Browse code

Add Dirac support through libschroedinger. patch by Anuradha Suraparaju, anuradha rd.bbc.co uk

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

Anuradha Suraparaju authored on 2008/05/03 22:59:45
Showing 7 changed files
... ...
@@ -98,6 +98,7 @@ show_help(){
98 98
   echo "  --enable-libmp3lame      enable MP3 encoding via libmp3lame [default=no]"
99 99
   echo "  --enable-libnut          enable NUT (de)muxing via libnut,"
100 100
   echo "                           native demuxer exists [default=no]"
101
+  echo "  --enable-libschroedinger enable Dirac support via libschroedinger [default=no]"
101 102
   echo "  --enable-libtheora       enable Theora encoding via libtheora [default=no]"
102 103
   echo "  --enable-libvorbis       enable Vorbis encoding via libvorbis,"
103 104
   echo "                           native implementation exists [default=no]"
... ...
@@ -649,6 +650,7 @@ CONFIG_LIST="
649 649
     libgsm
650 650
     libmp3lame
651 651
     libnut
652
+    libschroedinger
652 653
     libtheora
653 654
     libvorbis
654 655
     libx264
... ...
@@ -834,6 +836,8 @@ libgsm_encoder_deps="libgsm"
834 834
 libgsm_ms_decoder_deps="libgsm"
835 835
 libgsm_ms_encoder_deps="libgsm"
836 836
 libmp3lame_encoder_deps="libmp3lame"
837
+libschroedinger_decoder_deps="libschroedinger"
838
+libschroedinger_encoder_deps="libschroedinger"
837 839
 libtheora_encoder_deps="libtheora"
838 840
 libvorbis_encoder_deps="libvorbis"
839 841
 libx264_encoder_deps="libx264"
... ...
@@ -1637,6 +1641,8 @@ enabled libfaad    && require2 libfaad faad.h faacDecOpen -lfaad
1637 1637
 enabled libgsm     && require  libgsm gsm.h gsm_create -lgsm
1638 1638
 enabled libmp3lame && require  LAME lame/lame.h lame_init -lmp3lame -lm
1639 1639
 enabled libnut     && require  libnut libnut.h nut_demuxer_init -lnut
1640
+enabled libschroedinger && add_cflags $(pkg-config --cflags schroedinger-1.0) \
1641
+                        && require libschroedinger schroedinger/schro.h schro_init -lschroedinger-1.0 -lpthread -loil-0.3 -lm -lrt
1640 1642
 enabled libtheora  && require  libtheora theora/theora.h theora_info_init -ltheora -logg
1641 1643
 enabled libvorbis  && require  libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg
1642 1644
 enabled libx264    && require  x264 x264.h x264_encoder_open -lx264 -lm
... ...
@@ -1923,6 +1929,7 @@ echo "libfaad dlopened          ${libfaadbin-no}"
1923 1923
 echo "libgsm enabled            ${libgsm-no}"
1924 1924
 echo "libmp3lame enabled        ${libmp3lame-no}"
1925 1925
 echo "libnut enabled            ${libnut-no}"
1926
+echo "libschroedinger enabled   ${libschroedinger-no}"
1926 1927
 echo "libtheora enabled         ${libtheora-no}"
1927 1928
 echo "libvorbis enabled         ${libvorbis-no}"
1928 1929
 echo "x264 enabled              ${libx264-no}"
... ...
@@ -325,6 +325,8 @@ OBJS-$(CONFIG_LIBFAAC)                 += libfaac.o
325 325
 OBJS-$(CONFIG_LIBFAAD)                 += libfaad.o
326 326
 OBJS-$(CONFIG_LIBGSM)                  += libgsm.o
327 327
 OBJS-$(CONFIG_LIBMP3LAME)              += libmp3lame.o
328
+OBJS-$(CONFIG_LIBSCHROEDINGER_DECODER) += libschroedingerdec.o libschroedinger.o libdirac_libschro.o
329
+OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER) += libschroedingerenc.o libschroedinger.o libdirac_libschro.o
328 330
 OBJS-$(CONFIG_LIBTHEORA)               += libtheoraenc.o
329 331
 OBJS-$(CONFIG_LIBVORBIS)               += libvorbis.o
330 332
 OBJS-$(CONFIG_LIBX264)                 += libx264.o
... ...
@@ -283,6 +283,7 @@ void avcodec_register_all(void)
283 283
     REGISTER_ENCDEC  (LIBGSM, libgsm);
284 284
     REGISTER_ENCDEC  (LIBGSM_MS, libgsm_ms);
285 285
     REGISTER_ENCODER (LIBMP3LAME, libmp3lame);
286
+    REGISTER_ENCDEC  (LIBSCHROEDINGER, libschroedinger);
286 287
     REGISTER_ENCODER (LIBTHEORA, libtheora);
287 288
     REGISTER_ENCODER (LIBVORBIS, libvorbis);
288 289
     REGISTER_ENCODER (LIBX264, libx264);
289 290
new file mode 100644
... ...
@@ -0,0 +1,80 @@
0
+/*
1
+ * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+* @file libschroedinger.c
22
+* function definitions common to libschroedingerdec.c and libschroedingerenc.c
23
+*/
24
+
25
+#include "libdirac_libschro.h"
26
+#include "libschroedinger.h"
27
+
28
+/**
29
+* Schroedinger video preset table. Ensure that this tables matches up correctly
30
+* with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
31
+*/
32
+static const SchroVideoFormatEnum ff_schro_video_formats[]={
33
+    SCHRO_VIDEO_FORMAT_CUSTOM     ,
34
+    SCHRO_VIDEO_FORMAT_QSIF       ,
35
+    SCHRO_VIDEO_FORMAT_QCIF       ,
36
+    SCHRO_VIDEO_FORMAT_SIF        ,
37
+    SCHRO_VIDEO_FORMAT_CIF        ,
38
+    SCHRO_VIDEO_FORMAT_4SIF       ,
39
+    SCHRO_VIDEO_FORMAT_4CIF       ,
40
+    SCHRO_VIDEO_FORMAT_SD480I_60  ,
41
+    SCHRO_VIDEO_FORMAT_SD576I_50  ,
42
+    SCHRO_VIDEO_FORMAT_HD720P_60  ,
43
+    SCHRO_VIDEO_FORMAT_HD720P_50  ,
44
+    SCHRO_VIDEO_FORMAT_HD1080I_60 ,
45
+    SCHRO_VIDEO_FORMAT_HD1080I_50 ,
46
+    SCHRO_VIDEO_FORMAT_HD1080P_60 ,
47
+    SCHRO_VIDEO_FORMAT_HD1080P_50 ,
48
+    SCHRO_VIDEO_FORMAT_DC2K_24    ,
49
+    SCHRO_VIDEO_FORMAT_DC4K_24    ,
50
+};
51
+
52
+SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avccontext)
53
+{
54
+    unsigned int num_formats = sizeof(ff_schro_video_formats) /
55
+                               sizeof(ff_schro_video_formats[0]);
56
+
57
+    unsigned int idx = ff_dirac_schro_get_video_format_idx (avccontext);
58
+
59
+    return (idx < num_formats) ?
60
+                 ff_schro_video_formats[idx] : SCHRO_VIDEO_FORMAT_CUSTOM;
61
+}
62
+
63
+int ff_get_schro_frame_format (SchroChromaFormat schro_pix_fmt,
64
+                               SchroFrameFormat  *schro_frame_fmt)
65
+{
66
+    unsigned int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
67
+                               sizeof(ffmpeg_schro_pixel_format_map[0]);
68
+
69
+    int idx;
70
+
71
+    for (idx = 0; idx < num_formats; ++idx) {
72
+        if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
73
+            *schro_frame_fmt =
74
+                         ffmpeg_schro_pixel_format_map[idx].schro_frame_fmt;
75
+            return 0;
76
+        }
77
+    }
78
+    return -1;
79
+}
0 80
new file mode 100644
... ...
@@ -0,0 +1,61 @@
0
+/*
1
+ * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+* @file libschroedinger.h
22
+* data structures common to libschroedingerdec.c and libschroedingerenc.c
23
+*/
24
+
25
+#ifndef FFMPEG_LIBSCHROEDINGER_H
26
+#define FFMPEG_LIBSCHROEDINGER_H
27
+
28
+#include "config.h"
29
+
30
+#ifdef CONFIG_LIBSCHROEDINGER
31
+
32
+#include <schroedinger/schrobitstream.h>
33
+#include <schroedinger/schroframe.h>
34
+#include "avcodec.h"
35
+
36
+static const struct {
37
+    enum PixelFormat  ff_pix_fmt;
38
+    SchroChromaFormat schro_pix_fmt;
39
+    SchroFrameFormat  schro_frame_fmt;
40
+} ffmpeg_schro_pixel_format_map[] = {
41
+    { PIX_FMT_YUV420P, SCHRO_CHROMA_420, SCHRO_FRAME_FORMAT_U8_420 },
42
+    { PIX_FMT_YUV422P, SCHRO_CHROMA_422, SCHRO_FRAME_FORMAT_U8_422 },
43
+    { PIX_FMT_YUV444P, SCHRO_CHROMA_444, SCHRO_FRAME_FORMAT_U8_444 },
44
+};
45
+
46
+/**
47
+* Returns the video format preset matching the input video dimensions and
48
+* time base.
49
+*/
50
+SchroVideoFormatEnum ff_get_schro_video_format_preset (AVCodecContext *avccontext);
51
+
52
+/**
53
+* Sets the Schroedinger frame format corresponding to the Schro chroma format
54
+* passed. Returns 0 on success, -1 on failure.
55
+*/
56
+int ff_get_schro_frame_format (SchroChromaFormat schro_chroma_fmt,
57
+                               SchroFrameFormat  *schro_frame_fmt);
58
+
59
+#endif /* CONFIG_LIBSCHROEDINGER */
60
+#endif /* FFMPEG_LIBSCHROEDINGER_H */
0 61
new file mode 100644
... ...
@@ -0,0 +1,310 @@
0
+/*
1
+ * Dirac decoder support via Schroedinger libraries
2
+ * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
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 Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
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
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser 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 libschroedingerdec.c
23
+* Dirac decoder support via libschroedinger-1.0 libraries. More details about
24
+* the Schroedinger project can be found at http://www.diracvideo.org/.
25
+* The library implements Dirac Specification Version 2.2.
26
+* (http://dirac.sourceforge.net/specification.html).
27
+*/
28
+
29
+#include "avcodec.h"
30
+#include "libdirac_libschro.h"
31
+#include "libschroedinger.h"
32
+
33
+#undef NDEBUG
34
+#include <assert.h>
35
+
36
+
37
+#include <schroedinger/schro.h>
38
+#include <schroedinger/schrodebug.h>
39
+#include <schroedinger/schrovideoformat.h>
40
+
41
+/** libschroedinger decoder private data */
42
+typedef struct FfmpegSchroDecoderParams
43
+{
44
+    /** Schroedinger video format */
45
+    SchroVideoFormat *format;
46
+
47
+    /** Schroedinger frame format */
48
+    SchroFrameFormat frame_format;
49
+
50
+    /** decoder handle */
51
+    SchroDecoder* decoder;
52
+
53
+    /** queue storing decoded frames */
54
+    FfmpegDiracSchroQueue dec_frame_queue;
55
+
56
+    /** end of sequence signalled */
57
+    int eos_signalled;
58
+
59
+    /** end of sequence pulled */
60
+    int eos_pulled;
61
+
62
+    /** decoded picture */
63
+    AVPicture dec_pic;
64
+} FfmpegSchroDecoderParams;
65
+
66
+/**
67
+* Returns FFmpeg chroma format.
68
+*/
69
+static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt)
70
+{
71
+    int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
72
+                      sizeof(ffmpeg_schro_pixel_format_map[0]);
73
+    int idx;
74
+
75
+    for (idx = 0; idx < num_formats; ++idx) {
76
+        if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
77
+            return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt;
78
+        }
79
+    }
80
+    return PIX_FMT_NONE;
81
+}
82
+
83
+/*-------------------------DECODER------------------------------------------*/
84
+
85
+static int libschroedinger_decode_init(AVCodecContext *avccontext)
86
+{
87
+
88
+    FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ;
89
+    /* First of all, initialize our supporting libraries. */
90
+    schro_init();
91
+
92
+    schro_debug_set_level(avccontext->debug);
93
+    p_schro_params->decoder =  schro_decoder_new();
94
+    schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
95
+
96
+    if (!p_schro_params->decoder)
97
+        return -1;
98
+
99
+    /* Initialize the decoded frame queue. */
100
+    ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
101
+    return 0 ;
102
+}
103
+
104
+static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
105
+                                                void *priv)
106
+{
107
+    av_freep(&priv);
108
+}
109
+
110
+static void libschroedinger_decode_frame_free (void *frame)
111
+{
112
+    schro_frame_unref(frame);
113
+}
114
+
115
+static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
116
+{
117
+    FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
118
+    SchroDecoder *decoder = p_schro_params->decoder;
119
+
120
+    p_schro_params->format = schro_decoder_get_video_format (decoder);
121
+
122
+    /* Tell FFmpeg about sequence details. */
123
+    if(avcodec_check_dimensions(avccontext, p_schro_params->format->width,
124
+                                p_schro_params->format->height) < 0) {
125
+        av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
126
+               p_schro_params->format->width, p_schro_params->format->height);
127
+        avccontext->height = avccontext->width = 0;
128
+        return -1;
129
+    }
130
+    avccontext->height  = p_schro_params->format->height;
131
+    avccontext->width   = p_schro_params->format->width;
132
+    avccontext->pix_fmt =
133
+                   GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
134
+
135
+    if (ff_get_schro_frame_format( p_schro_params->format->chroma_format,
136
+                                   &p_schro_params->frame_format) == -1) {
137
+        av_log (avccontext, AV_LOG_ERROR,
138
+                "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
139
+                "and 4:4:4 formats.\n");
140
+        return -1;
141
+    }
142
+
143
+    avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
144
+    avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
145
+
146
+    if (p_schro_params->dec_pic.data[0] == NULL)
147
+    {
148
+        avpicture_alloc(&p_schro_params->dec_pic,
149
+                        avccontext->pix_fmt,
150
+                        avccontext->width,
151
+                        avccontext->height);
152
+    }
153
+}
154
+
155
+static int libschroedinger_decode_frame(AVCodecContext *avccontext,
156
+                                        void *data, int *data_size,
157
+                                        const uint8_t *buf, int buf_size)
158
+{
159
+
160
+    FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
161
+    SchroDecoder *decoder = p_schro_params->decoder;
162
+    SchroVideoFormat *format;
163
+    AVPicture *picture = data;
164
+    SchroBuffer *enc_buf;
165
+    SchroFrame* frame;
166
+    int state;
167
+    int go = 1;
168
+
169
+    *data_size = 0;
170
+
171
+    if (buf_size>0) {
172
+        unsigned char *in_buf = av_malloc(buf_size);
173
+        memcpy (in_buf, buf, buf_size);
174
+        enc_buf = schro_buffer_new_with_data (in_buf, buf_size);
175
+        enc_buf->free = libschroedinger_decode_buffer_free;
176
+        enc_buf->priv = in_buf;
177
+        /* Push buffer into decoder. */
178
+        state = schro_decoder_push (decoder, enc_buf);
179
+        if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
180
+            libschroedinger_handle_first_access_unit(avccontext);
181
+    } else {
182
+        if (!p_schro_params->eos_signalled) {
183
+            state = schro_decoder_push_end_of_stream(decoder);
184
+            p_schro_params->eos_signalled = 1;
185
+        }
186
+    }
187
+
188
+    format = p_schro_params->format;
189
+
190
+    while (go) {
191
+        /* Parse data and process result. */
192
+        state = schro_decoder_wait (decoder);
193
+        switch (state)
194
+        {
195
+        case SCHRO_DECODER_FIRST_ACCESS_UNIT:
196
+            libschroedinger_handle_first_access_unit (avccontext);
197
+            break;
198
+
199
+        case SCHRO_DECODER_NEED_BITS:
200
+            /* Need more input data - stop iterating over what we have. */
201
+            go = 0;
202
+            break;
203
+
204
+        case SCHRO_DECODER_NEED_FRAME:
205
+            /* Decoder needs a frame - create one and push it in. */
206
+
207
+            frame = schro_frame_new_and_alloc(NULL,
208
+                                              p_schro_params->frame_format,
209
+                                              format->width,
210
+                                              format->height);
211
+            schro_decoder_add_output_picture (decoder, frame);
212
+            break;
213
+
214
+        case SCHRO_DECODER_OK:
215
+            /* Pull a frame out of the decoder. */
216
+            frame = schro_decoder_pull (decoder);
217
+
218
+            if (frame) {
219
+                ff_dirac_schro_queue_push_back(
220
+                                             &p_schro_params->dec_frame_queue,
221
+                                             frame);
222
+            }
223
+            break;
224
+        case SCHRO_DECODER_EOS:
225
+            go = 0;
226
+            p_schro_params->eos_pulled = 1;
227
+            schro_decoder_reset (decoder);
228
+            break;
229
+
230
+        case SCHRO_DECODER_ERROR:
231
+            return -1;
232
+            break;
233
+        }
234
+    }
235
+
236
+    /* Grab next frame to be returned from the top of the queue. */
237
+    frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
238
+
239
+    if (frame != NULL) {
240
+        memcpy (p_schro_params->dec_pic.data[0],
241
+                frame->components[0].data,
242
+                frame->components[0].length);
243
+
244
+        memcpy (p_schro_params->dec_pic.data[1],
245
+                frame->components[1].data,
246
+                frame->components[1].length);
247
+
248
+        memcpy (p_schro_params->dec_pic.data[2],
249
+                frame->components[2].data,
250
+                frame->components[2].length);
251
+
252
+        /* Fill picture with current buffer data from Schroedinger. */
253
+        avpicture_fill(picture, p_schro_params->dec_pic.data[0],
254
+                       avccontext->pix_fmt,
255
+                       avccontext->width, avccontext->height);
256
+
257
+        *data_size = sizeof(AVPicture);
258
+
259
+        /* Now free the frame resources. */
260
+        libschroedinger_decode_frame_free (frame);
261
+    }
262
+    return buf_size;
263
+}
264
+
265
+
266
+static int libschroedinger_decode_close(AVCodecContext *avccontext)
267
+{
268
+    FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
269
+    /* Free the decoder. */
270
+    schro_decoder_free (p_schro_params->decoder);
271
+    av_freep(&p_schro_params->format);
272
+
273
+    avpicture_free (&p_schro_params->dec_pic);
274
+
275
+    /* Free data in the output frame queue. */
276
+    ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
277
+                               libschroedinger_decode_frame_free);
278
+
279
+    return 0 ;
280
+}
281
+
282
+static void libschroedinger_flush (AVCodecContext *avccontext)
283
+{
284
+    /* Got a seek request. Free the decoded frames queue and then reset
285
+     * the decoder */
286
+    FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
287
+
288
+    /* Free data in the output frame queue. */
289
+    ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
290
+                               libschroedinger_decode_frame_free);
291
+
292
+    ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
293
+    schro_decoder_reset(p_schro_params->decoder);
294
+    p_schro_params->eos_pulled = 0;
295
+    p_schro_params->eos_signalled = 0;
296
+}
297
+
298
+AVCodec libschroedinger_decoder = {
299
+     "libschroedinger",
300
+    CODEC_TYPE_VIDEO,
301
+    CODEC_ID_DIRAC,
302
+    sizeof(FfmpegSchroDecoderParams),
303
+    libschroedinger_decode_init,
304
+    NULL,
305
+    libschroedinger_decode_close,
306
+    libschroedinger_decode_frame,
307
+    CODEC_CAP_DELAY,
308
+    .flush = libschroedinger_flush
309
+};
0 310
new file mode 100644
... ...
@@ -0,0 +1,387 @@
0
+/*
1
+ * Dirac encoder support via Schroedinger libraries
2
+ * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
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 Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
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
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser 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 libschroedingerenc.c
23
+* Dirac encoder support via libschroedinger-1.0 libraries. More details about
24
+* the Schroedinger project can be found at http://www.diracvideo.org/.
25
+* The library implements Dirac Specification Version 2.2
26
+* (http://dirac.sourceforge.net/specification.html).
27
+*/
28
+
29
+#undef NDEBUG
30
+#include <assert.h>
31
+
32
+#include <schroedinger/schro.h>
33
+#include <schroedinger/schrodebug.h>
34
+#include <schroedinger/schrovideoformat.h>
35
+
36
+#include "avcodec.h"
37
+#include "libdirac_libschro.h"
38
+#include "libschroedinger.h"
39
+
40
+
41
+/** libschroedinger encoder private data */
42
+typedef struct FfmpegSchroEncoderParams
43
+{
44
+    /** Schroedinger video format */
45
+    SchroVideoFormat *format;
46
+
47
+    /** Schroedinger frame format */
48
+    SchroFrameFormat frame_format;
49
+
50
+    /** frame being encoded */
51
+    AVFrame picture;
52
+
53
+    /** frame size */
54
+    int frame_size;
55
+
56
+    /** Schroedinger encoder handle*/
57
+    SchroEncoder* encoder;
58
+
59
+    /** queue storing encoded frames */
60
+    FfmpegDiracSchroQueue enc_frame_queue;
61
+
62
+    /** end of sequence signalled */
63
+    int eos_signalled;
64
+
65
+    /** end of sequence pulled */
66
+    int eos_pulled;
67
+} FfmpegSchroEncoderParams;
68
+
69
+/**
70
+* Works out Schro-compatible chroma format.
71
+*/
72
+static int SetSchroChromaFormat(AVCodecContext *avccontext)
73
+{
74
+    int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
75
+                      sizeof(ffmpeg_schro_pixel_format_map[0]);
76
+    int idx;
77
+
78
+    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
79
+
80
+    for (idx = 0; idx < num_formats; ++idx) {
81
+        if (ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt ==
82
+                                                       avccontext->pix_fmt) {
83
+            p_schro_params->format->chroma_format =
84
+                            ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt;
85
+            return 0;
86
+        }
87
+    }
88
+
89
+    av_log (avccontext, AV_LOG_ERROR,
90
+            "This codec currently only supports planar YUV 4:2:0, 4:2:2"
91
+            " and 4:4:4 formats.\n");
92
+
93
+    return -1;
94
+}
95
+
96
+static int libschroedinger_encode_init(AVCodecContext *avccontext)
97
+{
98
+    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
99
+    SchroVideoFormatEnum preset;
100
+
101
+    /* Initialize the libraries that libschroedinger depends on. */
102
+    schro_init();
103
+
104
+    /* Create an encoder object. */
105
+    p_schro_params->encoder = schro_encoder_new();
106
+
107
+    if (!p_schro_params->encoder) {
108
+        av_log(avccontext, AV_LOG_ERROR,
109
+               "Unrecoverable Error: schro_encoder_new failed. ");
110
+        return -1;
111
+    }
112
+
113
+    /* Initialize the format. */
114
+    preset = ff_get_schro_video_format_preset(avccontext);
115
+    p_schro_params->format =
116
+                    schro_encoder_get_video_format(p_schro_params->encoder);
117
+    schro_video_format_set_std_video_format (p_schro_params->format, preset);
118
+    p_schro_params->format->width = avccontext->width;
119
+    p_schro_params->format->height = avccontext->height;
120
+
121
+    if (SetSchroChromaFormat(avccontext) == -1)
122
+        return -1;
123
+
124
+    if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
125
+                                  &p_schro_params->frame_format) == -1) {
126
+        av_log (avccontext, AV_LOG_ERROR,
127
+                "This codec currently supports only planar YUV 4:2:0, 4:2:2"
128
+                " and 4:4:4 formats.\n");
129
+        return -1;
130
+    }
131
+
132
+    p_schro_params->format->frame_rate_numerator   = avccontext->time_base.den;
133
+    p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
134
+
135
+    p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
136
+                                                    avccontext->width,
137
+                                                    avccontext->height);
138
+
139
+    avccontext->coded_frame = &p_schro_params->picture;
140
+
141
+    if (avccontext->gop_size == 0){
142
+        schro_encoder_setting_set_double (p_schro_params->encoder,
143
+                                          "gop_structure",
144
+                                          SCHRO_ENCODER_GOP_INTRA_ONLY);
145
+    }
146
+    else {
147
+        schro_encoder_setting_set_double (p_schro_params->encoder,
148
+                                          "gop_structure",
149
+                                          SCHRO_ENCODER_GOP_BIREF);
150
+        avccontext->has_b_frames = 1;
151
+    }
152
+
153
+    /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
154
+    if (avccontext->flags & CODEC_FLAG_QSCALE) {
155
+        if (avccontext->global_quality == 0) {
156
+            /* lossless coding */
157
+            schro_encoder_setting_set_double (p_schro_params->encoder,
158
+                                          "rate_control",
159
+                                          SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
160
+        } else {
161
+            int noise_threshold;
162
+            schro_encoder_setting_set_double (p_schro_params->encoder,
163
+                          "rate_control",
164
+                          SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD);
165
+
166
+            noise_threshold = avccontext->global_quality/FF_QP2LAMBDA;
167
+            if (noise_threshold > 100)
168
+                noise_threshold = 100;
169
+            schro_encoder_setting_set_double (p_schro_params->encoder,
170
+                                              "noise_threshold",
171
+                                              noise_threshold);
172
+        }
173
+    }
174
+    else {
175
+        schro_encoder_setting_set_double ( p_schro_params->encoder,
176
+                               "rate_control",
177
+                               SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
178
+
179
+        schro_encoder_setting_set_double (p_schro_params->encoder,
180
+                                          "bitrate",
181
+                                          avccontext->bit_rate);
182
+
183
+    }
184
+
185
+    if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) {
186
+        /* All material can be coded as interlaced or progressive
187
+           irrespective of the type of source material. */
188
+        schro_encoder_setting_set_double (p_schro_params->encoder,
189
+                                            "interlaced_coding", 1);
190
+    }
191
+
192
+    /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
193
+     * and libdirac support other bit-depth data. */
194
+    schro_video_format_set_std_signal_range(p_schro_params->format,
195
+                                            SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
196
+
197
+
198
+    /* Hardcode motion vector precision to quarter pixel. */
199
+    schro_encoder_setting_set_double (p_schro_params->encoder,
200
+                                      "mv_precision", 2);
201
+
202
+    /* Set the encoder format. */
203
+    schro_encoder_set_video_format(p_schro_params->encoder,
204
+                                   p_schro_params->format);
205
+
206
+    /* Set the debug level. */
207
+    schro_debug_set_level (avccontext->debug);
208
+
209
+    schro_encoder_start (p_schro_params->encoder);
210
+
211
+    /* Initialize the encoded frame queue. */
212
+    ff_dirac_schro_queue_init (&p_schro_params->enc_frame_queue);
213
+    return 0 ;
214
+}
215
+
216
+static SchroFrame *libschroedinger_frame_from_data (AVCodecContext *avccontext,
217
+                                                    void *in_data)
218
+{
219
+    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
220
+    SchroFrame *in_frame;
221
+    /* Input line size may differ from what the codec supports. Especially
222
+     * when transcoding from one format to another. So use avpicture_layout
223
+     * to copy the frame. */
224
+    in_frame = schro_frame_new_and_alloc (NULL,
225
+                                          p_schro_params->frame_format,
226
+                                          p_schro_params->format->width,
227
+                                          p_schro_params->format->height);
228
+
229
+    avpicture_layout ((AVPicture *)in_data, avccontext->pix_fmt,
230
+                      avccontext->width, avccontext->height,
231
+                      in_frame->components[0].data,
232
+                      p_schro_params->frame_size);
233
+
234
+    return in_frame;
235
+}
236
+
237
+static void SchroedingerFreeFrame(void *data)
238
+{
239
+    FfmpegDiracSchroEncodedFrame *enc_frame = data;
240
+
241
+    av_freep (&(enc_frame->p_encbuf));
242
+    av_free(enc_frame);
243
+}
244
+
245
+static int libschroedinger_encode_frame(AVCodecContext *avccontext,
246
+                                        unsigned char *frame,
247
+                                        int buf_size, void *data)
248
+{
249
+    int enc_size = 0;
250
+    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
251
+    SchroEncoder *encoder = p_schro_params->encoder;
252
+    struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
253
+    int go = 1;
254
+    SchroBuffer *enc_buf;
255
+    int presentation_frame;
256
+    int parse_code;
257
+
258
+    if(data == NULL) {
259
+        /* Push end of sequence if not already signalled. */
260
+        if (!p_schro_params->eos_signalled) {
261
+            schro_encoder_end_of_stream(encoder);
262
+            p_schro_params->eos_signalled = 1;
263
+        }
264
+    } else {
265
+        /* Allocate frame data to schro input buffer. */
266
+        SchroFrame *in_frame = libschroedinger_frame_from_data (avccontext,
267
+                                                                data);
268
+        /* Load next frame. */
269
+        schro_encoder_push_frame(encoder, in_frame);
270
+    }
271
+
272
+    if (p_schro_params->eos_pulled)
273
+        go = 0;
274
+
275
+    /* Now check to see if we have any output from the encoder. */
276
+    while (go) {
277
+        SchroStateEnum  state;
278
+        state = schro_encoder_wait(encoder);
279
+        switch (state)
280
+        {
281
+        case SCHRO_STATE_HAVE_BUFFER:
282
+        case SCHRO_STATE_END_OF_STREAM:
283
+            enc_buf = schro_encoder_pull (encoder,
284
+                                          &presentation_frame);
285
+            assert (enc_buf->length > 0);
286
+            assert (enc_buf->length <= buf_size);
287
+
288
+            /* Create output frame. */
289
+            p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
290
+            /* Set output data. */
291
+            p_frame_output->size     = enc_buf->length;
292
+            p_frame_output->p_encbuf = av_malloc(enc_buf->length);
293
+            memcpy(p_frame_output->p_encbuf, enc_buf->data, enc_buf->length);
294
+
295
+            parse_code = enc_buf->data[4];
296
+            if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
297
+                SCHRO_PARSE_CODE_IS_REFERENCE(parse_code)) {
298
+                p_frame_output->key_frame = 1;
299
+            }
300
+
301
+            /* Parse the coded frame number from the bitstream. Bytes 14
302
+             * through 17 represesent the frame number. */
303
+            if (SCHRO_PARSE_CODE_IS_PICTURE(parse_code))
304
+            {
305
+                assert (enc_buf->length >= 17);
306
+                p_frame_output->frame_num = (enc_buf->data[13] << 24) +
307
+                                            (enc_buf->data[14] << 16) +
308
+                                            (enc_buf->data[15] <<  8) +
309
+                                             enc_buf->data[16];
310
+            }
311
+
312
+            ff_dirac_schro_queue_push_back (&p_schro_params->enc_frame_queue,
313
+                                            p_frame_output);
314
+            schro_buffer_unref (enc_buf);
315
+
316
+            if (state == SCHRO_STATE_END_OF_STREAM) {
317
+                p_schro_params->eos_pulled = 1;
318
+                   go = 0;
319
+            }
320
+            break;
321
+
322
+        case SCHRO_STATE_NEED_FRAME:
323
+            go = 0;
324
+            break;
325
+
326
+        case SCHRO_STATE_AGAIN:
327
+            break;
328
+
329
+        default:
330
+            av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
331
+            return -1;
332
+        }
333
+    }
334
+
335
+    /* Copy 'next' frame in queue. */
336
+    p_frame_output =
337
+               ff_dirac_schro_queue_pop (&p_schro_params->enc_frame_queue);
338
+
339
+    if (p_frame_output == NULL)
340
+        return 0;
341
+
342
+    memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
343
+    avccontext->coded_frame->key_frame = p_frame_output->key_frame;
344
+    /* Use the frame number of the encoded frame as the pts. It is OK to
345
+     * do so since Dirac is a constant frame rate codec. It expects input
346
+     * to be of constant frame rate. */
347
+    avccontext->coded_frame->pts = p_frame_output->frame_num;
348
+    enc_size = p_frame_output->size;
349
+
350
+    /* free frame */
351
+    SchroedingerFreeFrame (p_frame_output);
352
+
353
+    return enc_size;
354
+}
355
+
356
+
357
+static int libschroedinger_encode_close(AVCodecContext *avccontext)
358
+{
359
+
360
+    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
361
+
362
+     /* Close the encoder. */
363
+    schro_encoder_free(p_schro_params->encoder);
364
+
365
+    /* Free data in the output frame queue. */
366
+    ff_dirac_schro_queue_free (&p_schro_params->enc_frame_queue,
367
+                               SchroedingerFreeFrame);
368
+
369
+    /* Free the video format structure. */
370
+    av_freep(&p_schro_params->format);
371
+
372
+    return 0 ;
373
+}
374
+
375
+
376
+AVCodec libschroedinger_encoder = {
377
+    "libschroedinger",
378
+    CODEC_TYPE_VIDEO,
379
+    CODEC_ID_DIRAC,
380
+    sizeof(FfmpegSchroEncoderParams),
381
+    libschroedinger_encode_init,
382
+    libschroedinger_encode_frame,
383
+    libschroedinger_encode_close,
384
+   .capabilities= CODEC_CAP_DELAY,
385
+   .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, -1},
386
+};