Statistics
| Branch: | Revision:

ffmpeg / libavcodec / libschroedingerenc.c @ 614748ce

History | View | Annotate | Download (14.5 KB)

1
/*
2
 * Dirac encoder support via Schroedinger libraries
3
 * Copyright (c) 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
24
* Dirac encoder support via libschroedinger-1.0 libraries. More details about
25
* the Schroedinger project can be found at http://www.diracvideo.org/.
26
* The library implements Dirac Specification Version 2.2
27
* (http://dirac.sourceforge.net/specification.html).
28
*/
29

    
30
#undef NDEBUG
31
#include <assert.h>
32

    
33
#include <schroedinger/schro.h>
34
#include <schroedinger/schrodebug.h>
35
#include <schroedinger/schrovideoformat.h>
36

    
37
#include "avcodec.h"
38
#include "libdirac_libschro.h"
39
#include "libschroedinger.h"
40

    
41

    
42
/** libschroedinger encoder private data */
43
typedef struct FfmpegSchroEncoderParams {
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
    /** buffer to store encoder output before writing it to the frame queue*/
60
    unsigned char *enc_buf;
61

    
62
    /** Size of encoder buffer*/
63
    int enc_buf_size;
64

    
65
    /** queue storing encoded frames */
66
    FfmpegDiracSchroQueue enc_frame_queue;
67

    
68
    /** end of sequence signalled */
69
    int eos_signalled;
70

    
71
    /** end of sequence pulled */
72
    int eos_pulled;
73
} FfmpegSchroEncoderParams;
74

    
75
/**
76
* Works out Schro-compatible chroma format.
77
*/
78
static int SetSchroChromaFormat(AVCodecContext *avccontext)
79
{
80
    int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
81
                      sizeof(ffmpeg_schro_pixel_format_map[0]);
82
    int idx;
83

    
84
    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
85

    
86
    for (idx = 0; idx < num_formats; ++idx) {
87
        if (ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt ==
88
            avccontext->pix_fmt) {
89
            p_schro_params->format->chroma_format =
90
                            ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt;
91
            return 0;
92
        }
93
    }
94

    
95
    av_log(avccontext, AV_LOG_ERROR,
96
           "This codec currently only supports planar YUV 4:2:0, 4:2:2"
97
           " and 4:4:4 formats.\n");
98

    
99
    return -1;
100
}
101

    
102
static int libschroedinger_encode_init(AVCodecContext *avccontext)
103
{
104
    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
105
    SchroVideoFormatEnum preset;
106

    
107
    /* Initialize the libraries that libschroedinger depends on. */
108
    schro_init();
109

    
110
    /* Create an encoder object. */
111
    p_schro_params->encoder = schro_encoder_new();
112

    
113
    if (!p_schro_params->encoder) {
114
        av_log(avccontext, AV_LOG_ERROR,
115
               "Unrecoverable Error: schro_encoder_new failed. ");
116
        return -1;
117
    }
118

    
119
    /* Initialize the format. */
120
    preset = ff_get_schro_video_format_preset(avccontext);
121
    p_schro_params->format =
122
                    schro_encoder_get_video_format(p_schro_params->encoder);
123
    schro_video_format_set_std_video_format(p_schro_params->format, preset);
124
    p_schro_params->format->width  = avccontext->width;
125
    p_schro_params->format->height = avccontext->height;
126

    
127
    if (SetSchroChromaFormat(avccontext) == -1)
128
        return -1;
129

    
130
    if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
131
                                  &p_schro_params->frame_format) == -1) {
132
        av_log(avccontext, AV_LOG_ERROR,
133
               "This codec currently supports only planar YUV 4:2:0, 4:2:2"
134
               " and 4:4:4 formats.\n");
135
        return -1;
136
    }
137

    
138
    p_schro_params->format->frame_rate_numerator   = avccontext->time_base.den;
139
    p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
140

    
141
    p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
142
                                                    avccontext->width,
143
                                                    avccontext->height);
144

    
145
    avccontext->coded_frame = &p_schro_params->picture;
146

    
147
    if (!avccontext->gop_size) {
148
        schro_encoder_setting_set_double(p_schro_params->encoder,
149
                                         "gop_structure",
150
                                         SCHRO_ENCODER_GOP_INTRA_ONLY);
151

    
152
        if (avccontext->coder_type == FF_CODER_TYPE_VLC)
153
            schro_encoder_setting_set_double(p_schro_params->encoder,
154
                                             "enable_noarith", 1);
155
    } else {
156
        avccontext->has_b_frames = 1;
157
    }
158

    
159
    /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
160
    if (avccontext->flags & CODEC_FLAG_QSCALE) {
161
        if (!avccontext->global_quality) {
162
            /* lossless coding */
163
            schro_encoder_setting_set_double(p_schro_params->encoder,
164
                                             "rate_control",
165
                                             SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
166
        } else {
167
            int noise_threshold;
168
            schro_encoder_setting_set_double(p_schro_params->encoder,
169
                                             "rate_control",
170
                                             SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD);
171

    
172
            noise_threshold = avccontext->global_quality / FF_QP2LAMBDA;
173
            if (noise_threshold > 100)
174
                noise_threshold = 100;
175
            schro_encoder_setting_set_double(p_schro_params->encoder,
176
                                             "noise_threshold",
177
                                             noise_threshold);
178
        }
179
    } else {
180
        schro_encoder_setting_set_double(p_schro_params->encoder,
181
                                         "rate_control",
182
                                         SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
183

    
184
        schro_encoder_setting_set_double(p_schro_params->encoder,
185
                                         "bitrate",
186
                                         avccontext->bit_rate);
187

    
188
    }
189

    
190
    if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
191
        /* All material can be coded as interlaced or progressive
192
           irrespective of the type of source material. */
193
        schro_encoder_setting_set_double(p_schro_params->encoder,
194
                                         "interlaced_coding", 1);
195

    
196
    /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
197
     * and libdirac support other bit-depth data. */
198
    schro_video_format_set_std_signal_range(p_schro_params->format,
199
                                            SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
200

    
201
    /* Set the encoder format. */
202
    schro_encoder_set_video_format(p_schro_params->encoder,
203
                                   p_schro_params->format);
204

    
205
    /* Set the debug level. */
206
    schro_debug_set_level(avccontext->debug);
207

    
208
    schro_encoder_start(p_schro_params->encoder);
209

    
210
    /* Initialize the encoded frame queue. */
211
    ff_dirac_schro_queue_init(&p_schro_params->enc_frame_queue);
212
    return 0;
213
}
214

    
215
static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
216
                                                   void *in_data)
217
{
218
    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
219
    SchroFrame *in_frame;
220
    /* Input line size may differ from what the codec supports. Especially
221
     * when transcoding from one format to another. So use avpicture_layout
222
     * to copy the frame. */
223
    in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
224

    
225
    if (in_frame)
226
        avpicture_layout((AVPicture *)in_data, avccontext->pix_fmt,
227
                          avccontext->width, avccontext->height,
228
                          in_frame->components[0].data,
229
                          p_schro_params->frame_size);
230

    
231
    return in_frame;
232
}
233

    
234
static void SchroedingerFreeFrame(void *data)
235
{
236
    FfmpegDiracSchroEncodedFrame *enc_frame = data;
237

    
238
    av_freep(&(enc_frame->p_encbuf));
239
    av_free(enc_frame);
240
}
241

    
242
static int libschroedinger_encode_frame(AVCodecContext *avccontext,
243
                                        unsigned char *frame,
244
                                        int buf_size, void *data)
245
{
246
    int enc_size = 0;
247
    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
248
    SchroEncoder *encoder = p_schro_params->encoder;
249
    struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
250
    int go = 1;
251
    SchroBuffer *enc_buf;
252
    int presentation_frame;
253
    int parse_code;
254
    int last_frame_in_sequence = 0;
255

    
256
    if (!data) {
257
        /* Push end of sequence if not already signalled. */
258
        if (!p_schro_params->eos_signalled) {
259
            schro_encoder_end_of_stream(encoder);
260
            p_schro_params->eos_signalled = 1;
261
        }
262
    } else {
263
        /* Allocate frame data to schro input buffer. */
264
        SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
265
                                                               data);
266
        /* Load next frame. */
267
        schro_encoder_push_frame(encoder, in_frame);
268
    }
269

    
270
    if (p_schro_params->eos_pulled)
271
        go = 0;
272

    
273
    /* Now check to see if we have any output from the encoder. */
274
    while (go) {
275
        SchroStateEnum state;
276
        state = schro_encoder_wait(encoder);
277
        switch (state) {
278
        case SCHRO_STATE_HAVE_BUFFER:
279
        case SCHRO_STATE_END_OF_STREAM:
280
            enc_buf = schro_encoder_pull(encoder, &presentation_frame);
281
            assert(enc_buf->length > 0);
282
            assert(enc_buf->length <= buf_size);
283
            parse_code = enc_buf->data[4];
284

    
285
            /* All non-frame data is prepended to actual frame data to
286
             * be able to set the pts correctly. So we don't write data
287
             * to the frame output queue until we actually have a frame
288
             */
289
            p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
290
                                                 p_schro_params->enc_buf_size + enc_buf->length);
291

    
292
            memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
293
                   enc_buf->data, enc_buf->length);
294
            p_schro_params->enc_buf_size += enc_buf->length;
295

    
296

    
297
            if (state == SCHRO_STATE_END_OF_STREAM) {
298
                p_schro_params->eos_pulled = 1;
299
                go = 0;
300
            }
301

    
302
            if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
303
                schro_buffer_unref(enc_buf);
304
                break;
305
            }
306

    
307
            /* Create output frame. */
308
            p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
309
            /* Set output data. */
310
            p_frame_output->size     = p_schro_params->enc_buf_size;
311
            p_frame_output->p_encbuf = p_schro_params->enc_buf;
312
            if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
313
                SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
314
                p_frame_output->key_frame = 1;
315

    
316
            /* Parse the coded frame number from the bitstream. Bytes 14
317
             * through 17 represesent the frame number. */
318
                p_frame_output->frame_num = (enc_buf->data[13] << 24) +
319
                                            (enc_buf->data[14] << 16) +
320
                                            (enc_buf->data[15] <<  8) +
321
                                             enc_buf->data[16];
322

    
323
            ff_dirac_schro_queue_push_back(&p_schro_params->enc_frame_queue,
324
                                           p_frame_output);
325
            p_schro_params->enc_buf_size = 0;
326
            p_schro_params->enc_buf      = NULL;
327

    
328
            schro_buffer_unref(enc_buf);
329

    
330
            break;
331

    
332
        case SCHRO_STATE_NEED_FRAME:
333
            go = 0;
334
            break;
335

    
336
        case SCHRO_STATE_AGAIN:
337
            break;
338

    
339
        default:
340
            av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
341
            return -1;
342
        }
343
    }
344

    
345
    /* Copy 'next' frame in queue. */
346

    
347
    if (p_schro_params->enc_frame_queue.size == 1 &&
348
        p_schro_params->eos_pulled)
349
        last_frame_in_sequence = 1;
350

    
351
    p_frame_output = ff_dirac_schro_queue_pop(&p_schro_params->enc_frame_queue);
352

    
353
    if (!p_frame_output)
354
        return 0;
355

    
356
    memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
357
    avccontext->coded_frame->key_frame = p_frame_output->key_frame;
358
    /* Use the frame number of the encoded frame as the pts. It is OK to
359
     * do so since Dirac is a constant frame rate codec. It expects input
360
     * to be of constant frame rate. */
361
    avccontext->coded_frame->pts = p_frame_output->frame_num;
362
    enc_size = p_frame_output->size;
363

    
364
    /* Append the end of sequence information to the last frame in the
365
     * sequence. */
366
    if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
367
        memcpy(frame + enc_size, p_schro_params->enc_buf,
368
               p_schro_params->enc_buf_size);
369
        enc_size += p_schro_params->enc_buf_size;
370
        av_freep(&p_schro_params->enc_buf);
371
        p_schro_params->enc_buf_size = 0;
372
    }
373

    
374
    /* free frame */
375
    SchroedingerFreeFrame(p_frame_output);
376

    
377
    return enc_size;
378
}
379

    
380

    
381
static int libschroedinger_encode_close(AVCodecContext *avccontext)
382
{
383

    
384
    FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
385

    
386
    /* Close the encoder. */
387
    schro_encoder_free(p_schro_params->encoder);
388

    
389
    /* Free data in the output frame queue. */
390
    ff_dirac_schro_queue_free(&p_schro_params->enc_frame_queue,
391
                              SchroedingerFreeFrame);
392

    
393

    
394
    /* Free the encoder buffer. */
395
    if (p_schro_params->enc_buf_size)
396
        av_freep(&p_schro_params->enc_buf);
397

    
398
    /* Free the video format structure. */
399
    av_freep(&p_schro_params->format);
400

    
401
    return 0;
402
}
403

    
404

    
405
AVCodec libschroedinger_encoder = {
406
    "libschroedinger",
407
    AVMEDIA_TYPE_VIDEO,
408
    CODEC_ID_DIRAC,
409
    sizeof(FfmpegSchroEncoderParams),
410
    libschroedinger_encode_init,
411
    libschroedinger_encode_frame,
412
    libschroedinger_encode_close,
413
   .capabilities = CODEC_CAP_DELAY,
414
   .pix_fmts     = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
415
   .long_name    = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
416
};