Browse code

Dirac decoding/encoding support via libdirac. patch by Anuradha Suraparaju, anuradha rd.bbc.co uk

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

Anuradha Suraparaju authored on 2008/05/03 12:47:26
Showing 6 changed files
... ...
@@ -90,6 +90,7 @@ show_help(){
90 90
   echo "  --enable-libamr-wb       enable libamr-wb floating point audio codec"
91 91
   echo "  --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394"
92 92
   echo "                           and libraw1394 [default=no]"
93
+  echo "  --enable-libdirac        enable Dirac support via libdirac [default=no]"
93 94
   echo "  --enable-libfaac         enable FAAC support via libfaac [default=no]"
94 95
   echo "  --enable-libfaad         enable FAAD support via libfaad [default=no]"
95 96
   echo "  --enable-libfaadbin      open libfaad.so.0 at runtime [default=no]"
... ...
@@ -641,6 +642,7 @@ CONFIG_LIST="
641 641
     libamr_nb
642 642
     libamr_wb
643 643
     libdc1394
644
+    libdirac
644 645
     libfaac
645 646
     libfaad
646 647
     libfaadbin
... ...
@@ -822,6 +824,8 @@ libamr_nb_decoder_deps="libamr_nb"
822 822
 libamr_nb_encoder_deps="libamr_nb"
823 823
 libamr_wb_decoder_deps="libamr_wb"
824 824
 libamr_wb_encoder_deps="libamr_wb"
825
+libdirac_decoder_deps="libdirac"
826
+libdirac_encoder_deps="libdirac"
825 827
 libfaac_encoder_deps="libfaac"
826 828
 libfaad_decoder_deps="libfaad"
827 829
 libfaadbin_decoder_extralibs='$ldl'
... ...
@@ -1625,6 +1629,9 @@ enabled avisynth   && require2 vfw32 "windows.h vfw.h" AVIFileInit -lvfw32
1625 1625
 enabled liba52     && require  liba52 a52dec/a52.h a52_init -la52
1626 1626
 enabled libamr_nb  && require  libamrnb amrnb/interf_dec.h Speech_Decode_Frame_init -lamrnb -lm
1627 1627
 enabled libamr_wb  && require  libamrwb amrwb/dec_if.h D_IF_init -lamrwb -lm
1628
+enabled libdirac   && add_cflags "$(pkg-config --cflags dirac)" \
1629
+                   && require  libdirac libdirac_decoder/dirac_parser.h dirac_decoder_init -ldirac_decoder \
1630
+                   && require  libdirac libdirac_encoder/dirac_encoder.h dirac_encoder_init -ldirac_encoder
1628 1631
 enabled libfaac    && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac
1629 1632
 enabled libfaad    && require2 libfaad faad.h faacDecOpen -lfaad
1630 1633
 enabled libgsm     && require  libgsm gsm.h gsm_create -lgsm
... ...
@@ -1845,6 +1852,7 @@ check_deps $CONFIG_LIST       \
1845 1845
            $PROTOCOL_LIST     \
1846 1846
 
1847 1847
 enabled libdc1394 && append pkg_requires "libraw1394"
1848
+enabled libdirac  && append pkg_requires "dirac"
1848 1849
 enabled libtheora && append pkg_requires "theora"
1849 1850
 enabled libvorbis && append pkg_requires "vorbisenc"
1850 1851
 
... ...
@@ -1908,6 +1916,7 @@ echo "liba52 dlopened           ${liba52bin-no}"
1908 1908
 echo "libamr-nb support         ${libamr_nb-no}"
1909 1909
 echo "libamr-wb support         ${libamr_wb-no}"
1910 1910
 echo "libdc1394 support         ${libdc1394-no}"
1911
+echo "libdirac enabled          ${libdirac-no}"
1911 1912
 echo "libfaac enabled           ${libfaac-no}"
1912 1913
 echo "libfaad enabled           ${libfaad-no}"
1913 1914
 echo "libfaad dlopened          ${libfaadbin-no}"
... ...
@@ -319,6 +319,8 @@ OBJS-$(CONFIG_RTP_MUXER)               += mpegvideo.o
319 319
 OBJS-$(CONFIG_LIBA52)                  += liba52.o
320 320
 OBJS-$(CONFIG_LIBAMR_NB)               += libamr.o
321 321
 OBJS-$(CONFIG_LIBAMR_WB)               += libamr.o
322
+OBJS-$(CONFIG_LIBDIRAC_DECODER)        += libdiracdec.o
323
+OBJS-$(CONFIG_LIBDIRAC_ENCODER)        += libdiracenc.o libdirac_libschro.o
322 324
 OBJS-$(CONFIG_LIBFAAC)                 += libfaac.o
323 325
 OBJS-$(CONFIG_LIBFAAD)                 += libfaad.o
324 326
 OBJS-$(CONFIG_LIBGSM)                  += libgsm.o
... ...
@@ -277,6 +277,7 @@ void avcodec_register_all(void)
277 277
     REGISTER_DECODER (LIBA52, liba52);
278 278
     REGISTER_ENCDEC  (LIBAMR_NB, libamr_nb);
279 279
     REGISTER_ENCDEC  (LIBAMR_WB, libamr_wb);
280
+    REGISTER_ENCDEC  (LIBDIRAC, libdirac);
280 281
     REGISTER_ENCODER (LIBFAAC, libfaac);
281 282
     REGISTER_DECODER (LIBFAAD, libfaad);
282 283
     REGISTER_ENCDEC  (LIBGSM, libgsm);
283 284
new file mode 100644
... ...
@@ -0,0 +1,44 @@
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 libdirac.h
22
+* data structures common to libdiracenc.c and libdiracdec.c
23
+*/
24
+
25
+#ifndef FFMPEG_LIBDIRAC_H
26
+#define FFMPEG_LIBDIRAC_H
27
+
28
+#include "avcodec.h"
29
+#include <libdirac_common/dirac_types.h>
30
+
31
+/**
32
+* Table providing a Dirac chroma format to FFmpeg pixel format mapping.
33
+*/
34
+static const struct {
35
+    enum PixelFormat ff_pix_fmt;
36
+    dirac_chroma_t dirac_pix_fmt;
37
+} ffmpeg_dirac_pixel_format_map[] = {
38
+    { PIX_FMT_YUV420P, format420 },
39
+    { PIX_FMT_YUV422P, format422 },
40
+    { PIX_FMT_YUV444P, format444 },
41
+};
42
+
43
+#endif /* FFMPEG_LIBDIRAC_H */
0 44
new file mode 100644
... ...
@@ -0,0 +1,208 @@
0
+/*
1
+ * Dirac decoder support via libdirac library
2
+ * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
3
+ * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+* @file libdiracdec.c
24
+* Dirac decoder support via libdirac library; more details about the Dirac
25
+* project can be found at http://dirac.sourceforge.net/.
26
+* The libdirac_decoder library implements Dirac specification version 2.2
27
+* (http://dirac.sourceforge.net/specification.html).
28
+*/
29
+
30
+#include "libdirac.h"
31
+
32
+#undef NDEBUG
33
+#include <assert.h>
34
+
35
+#include <libdirac_decoder/dirac_parser.h>
36
+
37
+/** contains a single frame returned from Dirac */
38
+typedef struct FfmpegDiracDecoderParams
39
+{
40
+    /** decoder handle */
41
+    dirac_decoder_t* p_decoder;
42
+
43
+    /** buffer to hold decoded frame */
44
+    unsigned char* p_out_frame_buf;
45
+} FfmpegDiracDecoderParams;
46
+
47
+
48
+/**
49
+* returns FFmpeg chroma format
50
+*/
51
+static enum PixelFormat GetFfmpegChromaFormat(dirac_chroma_t dirac_pix_fmt)
52
+{
53
+    int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
54
+                      sizeof(ffmpeg_dirac_pixel_format_map[0]);
55
+    int idx;
56
+
57
+    for (idx = 0; idx < num_formats; ++idx) {
58
+        if (ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt == dirac_pix_fmt) {
59
+            return ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt;
60
+        }
61
+    }
62
+    return PIX_FMT_NONE;
63
+}
64
+
65
+static int libdirac_decode_init(AVCodecContext *avccontext)
66
+{
67
+
68
+    FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data ;
69
+    p_dirac_params->p_decoder =  dirac_decoder_init(avccontext->debug);
70
+
71
+    if (!p_dirac_params->p_decoder)
72
+        return -1;
73
+
74
+    return 0 ;
75
+}
76
+
77
+static int libdirac_decode_frame(AVCodecContext *avccontext,
78
+                                 void *data, int *data_size,
79
+                                 const uint8_t *buf, int buf_size)
80
+{
81
+
82
+    FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
83
+    AVPicture *picture = data;
84
+    AVPicture pic;
85
+    int pict_size;
86
+    unsigned char *buffer[3];
87
+
88
+    *data_size = 0;
89
+
90
+    if (buf_size>0)
91
+        /* set data to decode into buffer */
92
+        dirac_buffer (p_dirac_params->p_decoder, buf, buf+buf_size);
93
+
94
+    while (1) {
95
+         /* parse data and process result */
96
+        DecoderState state = dirac_parse (p_dirac_params->p_decoder);
97
+        switch (state)
98
+        {
99
+        case STATE_BUFFER:
100
+            return buf_size;
101
+
102
+        case STATE_SEQUENCE:
103
+        {
104
+            /* tell FFmpeg about sequence details */
105
+            dirac_sourceparams_t *src_params =
106
+                                  &p_dirac_params->p_decoder->src_params;
107
+
108
+            if (avcodec_check_dimensions(avccontext, src_params->width,
109
+                                         src_params->height) < 0) {
110
+                av_log(avccontext, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n",
111
+                       src_params->width, src_params->height);
112
+                avccontext->height = avccontext->width = 0;
113
+                return -1;
114
+            }
115
+
116
+            avccontext->height = src_params->height;
117
+            avccontext->width  = src_params->width;
118
+
119
+            avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
120
+            if (avccontext->pix_fmt == PIX_FMT_NONE) {
121
+                av_log (avccontext, AV_LOG_ERROR,
122
+                        "Dirac chroma format %d not supported currently\n",
123
+                        src_params->chroma);
124
+                return -1;
125
+            }
126
+
127
+            avccontext->time_base.den = src_params->frame_rate.numerator;
128
+            avccontext->time_base.num = src_params->frame_rate.denominator;
129
+
130
+            /* calculate output dimensions */
131
+            avpicture_fill(&pic, NULL, avccontext->pix_fmt,
132
+                           avccontext->width, avccontext->height);
133
+
134
+            pict_size = avpicture_get_size(avccontext->pix_fmt,
135
+                                           avccontext->width,
136
+                                           avccontext->height);
137
+
138
+            /* allocate output buffer */
139
+            if (p_dirac_params->p_out_frame_buf == NULL)
140
+                p_dirac_params->p_out_frame_buf = av_malloc (pict_size);
141
+            buffer[0] = p_dirac_params->p_out_frame_buf;
142
+            buffer[1] = p_dirac_params->p_out_frame_buf +
143
+                        pic.linesize[0] * avccontext->height;
144
+            buffer[2] = buffer[1] +
145
+                        pic.linesize[1] * src_params->chroma_height;
146
+
147
+            /* tell Dirac about output destination */
148
+            dirac_set_buf(p_dirac_params->p_decoder, buffer, NULL);
149
+            break;
150
+        }
151
+        case STATE_SEQUENCE_END:
152
+            break;
153
+
154
+        case STATE_PICTURE_AVAIL:
155
+            /* fill picture with current buffer data from Dirac */
156
+            avpicture_fill(picture, p_dirac_params->p_out_frame_buf,
157
+                           avccontext->pix_fmt,
158
+                           avccontext->width, avccontext->height);
159
+            *data_size = sizeof(AVPicture);
160
+            return buf_size;
161
+
162
+        case STATE_INVALID:
163
+            return -1;
164
+
165
+        default:
166
+            break;
167
+        }
168
+    }
169
+
170
+    return buf_size;
171
+}
172
+
173
+
174
+static int libdirac_decode_close(AVCodecContext *avccontext)
175
+{
176
+    FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
177
+    dirac_decoder_close (p_dirac_params->p_decoder);
178
+
179
+    av_freep(&p_dirac_params->p_out_frame_buf);
180
+
181
+    return 0 ;
182
+}
183
+
184
+static void libdirac_flush (AVCodecContext *avccontext)
185
+{
186
+    /* Got a seek request. We will need free memory held in the private
187
+     * context and free the current Dirac decoder handle and then open
188
+     * a new decoder handle. */
189
+    libdirac_decode_close (avccontext);
190
+    libdirac_decode_init (avccontext);
191
+    return;
192
+}
193
+
194
+
195
+
196
+AVCodec libdirac_decoder = {
197
+    "libdirac",
198
+    CODEC_TYPE_VIDEO,
199
+    CODEC_ID_DIRAC,
200
+    sizeof(FfmpegDiracDecoderParams),
201
+    libdirac_decode_init,
202
+    NULL,
203
+    libdirac_decode_close,
204
+    libdirac_decode_frame,
205
+    CODEC_CAP_DELAY,
206
+    .flush = libdirac_flush
207
+} ;
0 208
new file mode 100644
... ...
@@ -0,0 +1,373 @@
0
+/*
1
+ * Dirac encoding support via libdirac library
2
+ * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
3
+ * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+* @file libdiracenc.c
24
+* Dirac encoding support via libdirac library; more details about the
25
+* Dirac project can be found at http://dirac.sourceforge.net/.
26
+* The libdirac_encoder library implements Dirac specification version 2.2
27
+* (http://dirac.sourceforge.net/specification.html).
28
+*/
29
+
30
+#include "libdirac_libschro.h"
31
+#include "libdirac.h"
32
+
33
+#undef NDEBUG
34
+#include <assert.h>
35
+
36
+
37
+#include <libdirac_encoder/dirac_encoder.h>
38
+
39
+/** Dirac encoder private data */
40
+typedef struct FfmpegDiracEncoderParams
41
+{
42
+    /** Dirac encoder context */
43
+    dirac_encoder_context_t enc_ctx;
44
+
45
+    /** frame being encoded */
46
+    AVFrame picture;
47
+
48
+    /** frame size */
49
+    int frame_size;
50
+
51
+    /** Dirac encoder handle */
52
+    dirac_encoder_t* p_encoder;
53
+
54
+    /** input frame buffer */
55
+    unsigned char *p_in_frame_buf;
56
+
57
+    /** queue storing encoded frames */
58
+    FfmpegDiracSchroQueue enc_frame_queue;
59
+
60
+    /** end of sequence signalled by user, 0 - false, 1 - true */
61
+    int eos_signalled;
62
+
63
+    /** end of sequence returned by encoder, 0 - false, 1 - true */
64
+    int eos_pulled;
65
+} FfmpegDiracEncoderParams;
66
+
67
+/**
68
+* Works out Dirac-compatible chroma format.
69
+*/
70
+static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt)
71
+{
72
+    int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
73
+                      sizeof(ffmpeg_dirac_pixel_format_map[0]);
74
+    int idx;
75
+
76
+    for (idx = 0; idx < num_formats; ++idx) {
77
+        if (ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt == ff_pix_fmt) {
78
+            return ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt;
79
+        }
80
+    }
81
+    return formatNK;
82
+}
83
+
84
+/**
85
+* Dirac video preset table. Ensure that this tables matches up correctly
86
+* with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
87
+*/
88
+static const VideoFormat ff_dirac_video_formats[]={
89
+    VIDEO_FORMAT_CUSTOM           ,
90
+    VIDEO_FORMAT_QSIF525          ,
91
+    VIDEO_FORMAT_QCIF             ,
92
+    VIDEO_FORMAT_SIF525           ,
93
+    VIDEO_FORMAT_CIF              ,
94
+    VIDEO_FORMAT_4SIF525          ,
95
+    VIDEO_FORMAT_4CIF             ,
96
+    VIDEO_FORMAT_SD_480I60        ,
97
+    VIDEO_FORMAT_SD_576I50        ,
98
+    VIDEO_FORMAT_HD_720P60        ,
99
+    VIDEO_FORMAT_HD_720P50        ,
100
+    VIDEO_FORMAT_HD_1080I60       ,
101
+    VIDEO_FORMAT_HD_1080I50       ,
102
+    VIDEO_FORMAT_HD_1080P60       ,
103
+    VIDEO_FORMAT_HD_1080P50       ,
104
+    VIDEO_FORMAT_DIGI_CINEMA_2K24 ,
105
+    VIDEO_FORMAT_DIGI_CINEMA_4K24 ,
106
+};
107
+
108
+/**
109
+* Returns the video format preset matching the input video dimensions and
110
+* time base.
111
+*/
112
+static VideoFormat GetDiracVideoFormatPreset (AVCodecContext *avccontext)
113
+{
114
+    unsigned int num_formats = sizeof(ff_dirac_video_formats) /
115
+                               sizeof(ff_dirac_video_formats[0]);
116
+
117
+    unsigned int idx = ff_dirac_schro_get_video_format_idx (avccontext);
118
+
119
+    return (idx < num_formats) ?
120
+                 ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM;
121
+}
122
+
123
+static int libdirac_encode_init(AVCodecContext *avccontext)
124
+{
125
+
126
+    FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
127
+    int no_local = 1;
128
+    int verbose = avccontext->debug;
129
+    VideoFormat preset;
130
+
131
+    /* get Dirac preset */
132
+    preset = GetDiracVideoFormatPreset(avccontext);
133
+
134
+    /* initialize the encoder context */
135
+    dirac_encoder_context_init (&(p_dirac_params->enc_ctx), preset);
136
+
137
+    p_dirac_params->enc_ctx.src_params.chroma =
138
+                                GetDiracChromaFormat(avccontext->pix_fmt);
139
+
140
+    if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) {
141
+        av_log (avccontext, AV_LOG_ERROR,
142
+                "Unsupported pixel format %d. This codec supports only "
143
+                "Planar YUV formats (yuv420p, yuv422p, yuv444p\n",
144
+                avccontext->pix_fmt);
145
+        return -1;
146
+    }
147
+
148
+    p_dirac_params->enc_ctx.src_params.frame_rate.numerator   =
149
+                                             avccontext->time_base.den;
150
+    p_dirac_params->enc_ctx.src_params.frame_rate.denominator =
151
+                                             avccontext->time_base.num;
152
+
153
+    p_dirac_params->enc_ctx.src_params.width  = avccontext->width;
154
+    p_dirac_params->enc_ctx.src_params.height = avccontext->height;
155
+
156
+    p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
157
+                                                    avccontext->width,
158
+                                                    avccontext->height);
159
+
160
+    avccontext->coded_frame = &p_dirac_params->picture;
161
+
162
+    if (no_local) {
163
+        p_dirac_params->enc_ctx.decode_flag = 0;
164
+        p_dirac_params->enc_ctx.instr_flag  = 0;
165
+    } else {
166
+        p_dirac_params->enc_ctx.decode_flag = 1;
167
+        p_dirac_params->enc_ctx.instr_flag  = 1;
168
+    }
169
+
170
+    /* Intra-only sequence */
171
+    if (avccontext->gop_size == 0 )
172
+        p_dirac_params->enc_ctx.enc_params.num_L1 = 0;
173
+    else
174
+        avccontext->has_b_frames = 1;
175
+
176
+    if (avccontext->flags & CODEC_FLAG_QSCALE) {
177
+        if (avccontext->global_quality != 0) {
178
+            p_dirac_params->enc_ctx.enc_params.qf =
179
+                            avccontext->global_quality / (FF_QP2LAMBDA*10.0);
180
+            /* if it is not default bitrate then send target rate. */
181
+            if (avccontext->bit_rate >= 1000 &&
182
+                avccontext->bit_rate != 200000) {
183
+               p_dirac_params->enc_ctx.enc_params.trate =
184
+                             avccontext->bit_rate / 1000;
185
+            }
186
+        } else
187
+            p_dirac_params->enc_ctx.enc_params.lossless = 1;
188
+    } else if (avccontext->bit_rate >= 1000)
189
+        p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000;
190
+
191
+    if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) &&
192
+         avccontext->bit_rate == 200000) {
193
+        p_dirac_params->enc_ctx.enc_params.trate = 0;
194
+    }
195
+
196
+    if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) {
197
+        /* all material can be coded as interlaced or progressive
198
+         * irrespective of the type of source material */
199
+        p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1;
200
+    }
201
+
202
+    p_dirac_params->p_encoder = dirac_encoder_init (&(p_dirac_params->enc_ctx),
203
+                                                    verbose );
204
+
205
+    if (!p_dirac_params->p_encoder) {
206
+        av_log(avccontext, AV_LOG_ERROR,
207
+               "Unrecoverable Error: dirac_encoder_init failed. ");
208
+        return EXIT_FAILURE;
209
+    }
210
+
211
+    /* allocate enough memory for the incoming data */
212
+    p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size);
213
+
214
+    /* initialize the encoded frame queue */
215
+    ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue);
216
+
217
+    return 0 ;
218
+}
219
+
220
+static void DiracFreeFrame (void *data)
221
+{
222
+    FfmpegDiracSchroEncodedFrame *enc_frame = data;
223
+
224
+    av_freep (&(enc_frame->p_encbuf));
225
+    av_free(enc_frame);
226
+}
227
+
228
+static int libdirac_encode_frame(AVCodecContext *avccontext,
229
+                                 unsigned char *frame,
230
+                                 int buf_size, void *data)
231
+{
232
+    int enc_size = 0;
233
+    dirac_encoder_state_t state;
234
+    FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
235
+    FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
236
+    FfmpegDiracSchroEncodedFrame* p_next_output_frame = NULL;
237
+    int go = 1;
238
+
239
+    if (data == NULL) {
240
+        /* push end of sequence if not already signalled */
241
+        if (!p_dirac_params->eos_signalled) {
242
+            dirac_encoder_end_sequence( p_dirac_params->p_encoder );
243
+            p_dirac_params->eos_signalled = 1;
244
+        }
245
+    } else {
246
+
247
+        /* Allocate frame data to Dirac input buffer.
248
+         * Input line size may differ from what the codec supports,
249
+         * especially when transcoding from one format to another.
250
+         * So use avpicture_layout to copy the frame. */
251
+        avpicture_layout ((AVPicture *)data, avccontext->pix_fmt,
252
+                          avccontext->width, avccontext->height,
253
+                          p_dirac_params->p_in_frame_buf,
254
+                          p_dirac_params->frame_size);
255
+
256
+        /* load next frame */
257
+        if (dirac_encoder_load (p_dirac_params->p_encoder,
258
+                                p_dirac_params->p_in_frame_buf,
259
+                                p_dirac_params->frame_size ) < 0) {
260
+            av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error."
261
+                   " dirac_encoder_load failed...\n");
262
+            return -1;
263
+        }
264
+    }
265
+
266
+    if (p_dirac_params->eos_pulled)
267
+        go = 0;
268
+
269
+    while(go) {
270
+        p_dirac_params->p_encoder->enc_buf.buffer = frame;
271
+        p_dirac_params->p_encoder->enc_buf.size   = buf_size;
272
+        /* process frame */
273
+        state = dirac_encoder_output ( p_dirac_params->p_encoder );
274
+
275
+        switch (state)
276
+        {
277
+        case ENC_STATE_AVAIL:
278
+        case ENC_STATE_EOS:
279
+            assert (p_dirac_params->p_encoder->enc_buf.size > 0);
280
+            /* create output frame */
281
+            p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
282
+            /* set output data */
283
+            p_frame_output->p_encbuf =
284
+                         av_malloc(p_dirac_params->p_encoder->enc_buf.size);
285
+
286
+            memcpy(p_frame_output->p_encbuf,
287
+                   p_dirac_params->p_encoder->enc_buf.buffer,
288
+                   p_dirac_params->p_encoder->enc_buf.size);
289
+
290
+            p_frame_output->size = p_dirac_params->p_encoder->enc_buf.size;
291
+
292
+            p_frame_output->frame_num =
293
+                            p_dirac_params->p_encoder->enc_pparams.pnum;
294
+
295
+            if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE &&
296
+                p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE)
297
+                p_frame_output->key_frame = 1;
298
+
299
+            ff_dirac_schro_queue_push_back (&p_dirac_params->enc_frame_queue,
300
+                                            p_frame_output);
301
+
302
+            if (state == ENC_STATE_EOS) {
303
+                p_dirac_params->eos_pulled = 1;
304
+                go = 0;
305
+            }
306
+            break;
307
+
308
+        case ENC_STATE_BUFFER:
309
+            go = 0;
310
+            break;
311
+
312
+        case ENC_STATE_INVALID:
313
+            av_log(avccontext, AV_LOG_ERROR,
314
+                   "Unrecoverable Dirac Encoder Error. Quitting...\n");
315
+            return -1;
316
+
317
+        default:
318
+            av_log(avccontext, AV_LOG_ERROR, "Unknown Dirac Encoder state\n");
319
+            return -1;
320
+        }
321
+    }
322
+
323
+    /* copy 'next' frame in queue */
324
+    p_next_output_frame =
325
+          ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue);
326
+
327
+    if (p_next_output_frame == NULL)
328
+        return 0;
329
+
330
+    memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size);
331
+    avccontext->coded_frame->key_frame = p_next_output_frame->key_frame;
332
+    /* Use the frame number of the encoded frame as the pts. It is OK to do
333
+     * so since Dirac is a constant framerate codec. It expects input to be
334
+     * of constant framerate. */
335
+    avccontext->coded_frame->pts = p_next_output_frame->frame_num;
336
+    enc_size = p_next_output_frame->size;
337
+
338
+    /* free frame */
339
+    DiracFreeFrame(p_next_output_frame);
340
+
341
+    return enc_size;
342
+}
343
+
344
+static int libdirac_encode_close(AVCodecContext *avccontext)
345
+{
346
+    FfmpegDiracEncoderParams* p_dirac_params  = avccontext->priv_data;
347
+
348
+    /* close the encoder */
349
+    dirac_encoder_close(p_dirac_params->p_encoder );
350
+
351
+    /* free data in the output frame queue */
352
+    ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue,
353
+                              DiracFreeFrame);
354
+
355
+    /* free the input frame buffer */
356
+    av_freep(&p_dirac_params->p_in_frame_buf);
357
+
358
+    return 0 ;
359
+}
360
+
361
+
362
+AVCodec libdirac_encoder = {
363
+    "libdirac",
364
+    CODEC_TYPE_VIDEO,
365
+    CODEC_ID_DIRAC,
366
+    sizeof(FfmpegDiracEncoderParams),
367
+    libdirac_encode_init,
368
+    libdirac_encode_frame,
369
+    libdirac_encode_close,
370
+   .capabilities= CODEC_CAP_DELAY,
371
+   .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, -1},
372
+} ;